Use FFMPEG and SDL to show a frame of YUV (4:2:0)

code:

#include<stdio.h>
#include<ffmpeg/avcodec.h>
#include<ffmpeg/avformat.h>
#include<stdlib.h>
#include<SDL.h>
#include<SDL_thread.h>


#define WIDTH 352
#define HEIGHT 288

int main()
{
    av_register_all();
    FILE *fin=fopen("test.yuv","rb");   
    uint8_t *buffer_in;
    AVFrame  *pFrameYUV ,*pFrameRGB;
    buffer_in=(uint8_t *)malloc(WIDTH*HEIGHT*1.5*sizeof(uint8_t));
    fread(buffer_in,sizeof(uint8_t),WIDTH*HEIGHT*1.5,fin);
     pFrameYUV=avcodec_alloc_frame();
    avpicture_fill((AVPicture *)pFrameYUV, buffer_in, 0,WIDTH,HEIGHT);

    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_Surface *screen =SDL_SetVideoMode(WIDTH,HEIGHT,0,0);
    SDL_Overlay *bmp=SDL_CreateYUVOverlay(WIDTH,HEIGHT,SDL_YV12_OVERLAY,screen);

    SDL_LockYUVOverlay(bmp);
    AVPicture pict;
    pict.data[0] = bmp->pixels[0];
    pict.data[1] = bmp->pixels[2];
    pict.data[2] = bmp->pixels[1];
    pict.linesize[0] = bmp->pitches[0];
    pict.linesize[1] = bmp->pitches[2];
    pict.linesize[2] = bmp->pitches[1];
    img_convert(&pict, PIX_FMT_YUV420P,(AVPicture *)pFrameYUV, 0,   WIDTH,HEIGHT);
    SDL_UnlockYUVOverlay(bmp);   

    SDL_Rect rect;
     rect.x = 0;
    rect.y = 0;
    rect.w = WIDTH;
    rect.h = HEIGHT;
    SDL_DisplayYUVOverlay(bmp, &rect);
   
    sleep(30);
    free(buffer_in);
    fclose(fin);
    return 0;
}

 

 

compile:     gcc -o main main.c -lavutil -lavformat -lavcode-lz -lm `sdl-config  --cflags  --libs`

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值