100行代码实现最简单的基于FFMPEG+SDL的视频播放器

来源:互联网 发布:最新网络新鲜事 编辑:程序博客网 时间:2024/04/27 22:07

ffmpeg库下载网站:http://ffmpeg.zeranoe.com/builds/


简介

FFMPEG工程浩大,可以参考的书籍又不是很多,因此很多刚学习FFMPEG的人常常感觉到无从下手。我刚接触FFMPEG的时候也感觉不知从何学起。

因此我把自己做项目过程中实现的一个非常简单的视频播放器(大约100行代码)源代码传上来,以作备忘,同时方便新手学习FFMPEG。

该播放器虽然简单,但是几乎包含了使用FFMPEG播放一个视频所有必备的API,并且使用SDL显示解码出来的视频。

并且支持流媒体等多种视频输入,处于简单考虑,没有音频部分,同时视频播放采用直接延时40ms的方式

平台使用VC2010

使用了新版的FFMPEG类库

流程图

没想到这篇文章中介绍的播放器挺受FFMPEG初学者的欢迎,因此再次更新两张流程图,方便大家学习。此外在源代码上添加了注释,方便理解。

该播放器解码的流程用图的方式可以表示称如下形式:


SDL显示YUV图像的流程图:





代码

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. int _tmain(int argc, _TCHAR* argv[])  
  2. {  
  3.     AVFormatContext *pFormatCtx;  
  4.     int             i, videoindex;  
  5.     AVCodecContext  *pCodecCtx;  
  6.     AVCodec         *pCodec;  
  7.     char filepath[]="nwn.mp4";  
  8.     av_register_all();//注册组件  
  9.     avformat_network_init();//支持网络流  
  10.     pFormatCtx = avformat_alloc_context();//初始化AVFormatContext  
  11.     if(avformat_open_input(&pFormatCtx,filepath,NULL,NULL)!=0){//打开文件  
  12.         printf("无法打开文件\n");  
  13.         return -1;  
  14.     }  
  15.     if(av_find_stream_info(pFormatCtx)<0)//查找流信息  
  16.     {  
  17.         printf("Couldn't find stream information.\n");  
  18.         return -1;  
  19.     }  
  20.     videoindex=-1;  
  21.     for(i=0; i<pFormatCtx->nb_streams; i++) //获取视频流ID  
  22.         if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)  
  23.         {  
  24.             videoindex=i;  
  25.             break;  
  26.         }  
  27.     if(videoindex==-1)  
  28.     {  
  29.         printf("Didn't find a video stream.\n");  
  30.         return -1;  
  31.     }  
  32.     pCodecCtx=pFormatCtx->streams[videoindex]->codec;  
  33.     pCodec=avcodec_find_decoder(pCodecCtx->codec_id);//查找解码器  
  34.     if(pCodec==NULL)  
  35.     {  
  36.         printf("Codec not found.\n");  
  37.         return -1;  
  38.     }  
  39.     if(avcodec_open(pCodecCtx, pCodec)<0)//打开解码器  
  40.     {  
  41.         printf("Could not open codec.\n");  
  42.         return -1;  
  43.     }  
  44.     AVFrame *pFrame,*pFrameYUV;  
  45.     pFrame=avcodec_alloc_frame();//存储解码后AVFrame  
  46.     pFrameYUV=avcodec_alloc_frame();//存储转换后AVFrame(为什么要转换?后文解释)  
  47.     uint8_t *out_buffer;  
  48.     out_buffer=new uint8_t[avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height)];//分配AVFrame所需内存  
  49.     avpicture_fill((AVPicture *)pFrameYUV, out_buffer, PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);//填充AVFrame  
  50.     //------------SDL初始化--------  
  51.     if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {    
  52.         printf( "Could not initialize SDL - %s\n", SDL_GetError());   
  53.         return -1;  
  54.     }   
  55.     SDL_Surface *screen;   
  56.     screen = SDL_SetVideoMode(pCodecCtx->width, pCodecCtx->height, 0, 0);  
  57.     if(!screen) {    
  58.         printf("SDL: could not set video mode - exiting\n");    
  59.         return -1;  
  60.     }  
  61.     SDL_Overlay *bmp;   
  62.     bmp = SDL_CreateYUVOverlay(pCodecCtx->width, pCodecCtx->height,SDL_YV12_OVERLAY, screen);   
  63.     SDL_Rect rect;  
  64.     //-----------------------------  
  65.     int ret, got_picture;  
  66.     static struct SwsContext *img_convert_ctx;  
  67.     int y_size = pCodecCtx->width * pCodecCtx->height;  
  68.   
  69.     AVPacket *packet=(AVPacket *)malloc(sizeof(AVPacket));//存储解码前数据包AVPacket  
  70.     av_new_packet(packet, y_size);  
  71.     //输出一下信息-----------------------------  
  72.     printf("文件信息-----------------------------------------\n");  
  73.     av_dump_format(pFormatCtx,0,filepath,0);  
  74.     printf("-------------------------------------------------\n");  
  75.     //------------------------------  
  76.     while(av_read_frame(pFormatCtx, packet)>=0)//循环获取压缩数据包AVPacket  
  77.     {  
  78.         if(packet->stream_index==videoindex)  
  79.         {  
  80.             ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);//解码。输入为AVPacket,输出为AVFrame  
  81.             if(ret < 0)  
  82.             {  
  83.                 printf("解码错误\n");  
  84.                 return -1;  
  85.             }  
  86.             if(got_picture)  
  87.             {  
  88.                 //像素格式转换。pFrame转换为pFrameYUV。  
  89.                 img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);   
  90.                 sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);  
  91.                 sws_freeContext(img_convert_ctx);  
  92.                 //------------SDL显示--------  
  93.                 SDL_LockYUVOverlay(bmp);  
  94.                 bmp->pixels[0]=pFrameYUV->data[0];  
  95.                 bmp->pixels[2]=pFrameYUV->data[1];  
  96.                 bmp->pixels[1]=pFrameYUV->data[2];       
  97.                 bmp->pitches[0]=pFrameYUV->linesize[0];  
  98.                 bmp->pitches[2]=pFrameYUV->linesize[1];     
  99.                 bmp->pitches[1]=pFrameYUV->linesize[2];  
  100.                 SDL_UnlockYUVOverlay(bmp);   
  101.                 rect.x = 0;      
  102.                 rect.y = 0;      
  103.                 rect.w = pCodecCtx->width;      
  104.                 rect.h = pCodecCtx->height;      
  105.                 SDL_DisplayYUVOverlay(bmp, &rect);   
  106.                 //延时40ms  
  107.                 SDL_Delay(40);  
  108.                 //------------SDL-----------  
  109.             }  
  110.         }  
  111.         av_free_packet(packet);  
  112.     }  
  113.     delete[] out_buffer;  
  114.     av_free(pFrameYUV);  
  115.     avcodec_close(pCodecCtx);  
  116.     avformat_close_input(&pFormatCtx);  
  117.   
  118.     return 0;  
  119. }  
0 0
原创粉丝点击