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`
本文介绍了一个使用FFmpeg库和SDL库将YUV格式的视频文件加载并显示到屏幕上的示例程序。该程序首先初始化所需的库,然后读取YUV文件内容到缓冲区,并将其填充到AVFrame中。接下来通过SDL库创建视频覆盖层并进行YUV到RGB的转换,最后显示图像。
3万+

被折叠的 条评论
为什么被折叠?



