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`

 

你可能感兴趣的:(File,gcc,buffer)