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

来源:互联网 发布:java自带线程池实例 编辑:程序博客网 时间:2024/05/16 00:40

转载地址:http://blog.csdn.net/leixiaohua1020/article/details/8652605


简介

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

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

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

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

平台使用VC2010,使用了新版的FFMPEG类库

该工程已经传到SourceForge上(该工程会时刻更新):

https://sourceforge.net/projects/simplestffmpegplayer/

注:本文SDL采用1.x版本。另一版本采用SDL2.0,可参考:

基于FFMPEG+SDL的视频播放器 ver2 (采用SDL2.0):http://blog.csdn.net/leixiaohua1020/article/details/38868499


流程图

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

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


SDL显示YUV图像的流程图:


简单解释几句:

SDL_Surface就是使用SDL的时候弹出的那个窗口。在SDL1.x版本中,只可以创建一个SDL_Surface。

SDL_Overlay用于显示YUV数据。一个SDL_Overlay对应一帧YUV数据。

SDL_Rect用于确定SDL_Overlay显示的位置。注意:一个SDL_Overlay可以指定多个不同的SDL_Rect,这样就可以在SDL_Surface不同位置显示相同的内容。

它们的关系如下图所示:



下图举了个例子,指定了4个SDL_Rect,可以实现4分屏的显示。



simplest_ffmpeg_player(标准版)代码

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * 最简单的基于FFmpeg的视频播放器 
  3.  * Simplest FFmpeg Player 
  4.  * 
  5.  * 雷霄骅 Lei Xiaohua 
  6.  * leixiaohua1020@126.com 
  7.  * 中国传媒大学/数字电视技术 
  8.  * Communication University of China / Digital TV Technology 
  9.  * http://blog.csdn.net/leixiaohua1020 
  10.  * 
  11.  * 本程序实现了视频文件的解码和显示(支持HEVC,H.264,MPEG2等)。 
  12.  * 是最简单的FFmpeg视频解码方面的教程。 
  13.  * 通过学习本例子可以了解FFmpeg的解码流程。 
  14.  * This software is a simplest video player based on FFmpeg. 
  15.  * Suitable for beginner of FFmpeg. 
  16.  *  
  17.  * Version:1.0 
  18.  */  
  19.   
  20.   
  21. #include "stdafx.h"  
  22.   
  23. extern "C"  
  24. {  
  25. #include "libavcodec/avcodec.h"  
  26. #include "libavformat/avformat.h"  
  27.     //新版里的图像转换结构需要引入的头文件  
  28. #include "libswscale/swscale.h"  
  29.     //SDL  
  30. #include "sdl/SDL.h"  
  31. #include "sdl/SDL_thread.h"  
  32. };  
  33.   
  34. //Full Screen  
  35. #define SHOW_FULLSCREEN 0  
  36. //Output YUV420P   
  37. #define OUTPUT_YUV420P 0  
  38.   
  39. int _tmain(int argc, _TCHAR* argv[])  
  40. {  
  41.   
  42.     AVFormatContext *pFormatCtx;  
  43.     int             i, videoindex;  
  44.     AVCodecContext  *pCodecCtx;  
  45.     AVCodec         *pCodec;  
  46.     char filepath[]="src01_480x272_22.hm10";  
  47.     av_register_all();  
  48.     avformat_network_init();  
  49.     pFormatCtx = avformat_alloc_context();  
  50.       
  51.     if(avformat_open_input(&pFormatCtx,filepath,NULL,NULL)!=0){  
  52.         printf("Couldn't open input stream.(无法打开输入流)\n");  
  53.         return -1;  
  54.     }  
  55.     if(av_find_stream_info(pFormatCtx)<0)  
  56.     {  
  57.         printf("Couldn't find stream information.(无法获取流信息)\n");  
  58.         return -1;  
  59.     }  
  60.     videoindex=-1;  
  61.     for(i=0; i<pFormatCtx->nb_streams; i++)   
  62.         if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)  
  63.         {  
  64.             videoindex=i;  
  65.             break;  
  66.         }  
  67.     if(videoindex==-1)  
  68.     {  
  69.         printf("Didn't find a video stream.(没有找到视频流)\n");  
  70.         return -1;  
  71.     }  
  72.     pCodecCtx=pFormatCtx->streams[videoindex]->codec;  
  73.     pCodec=avcodec_find_decoder(pCodecCtx->codec_id);  
  74.     if(pCodec==NULL)  
  75.     {  
  76.         printf("Codec not found.(没有找到解码器)\n");  
  77.         return -1;  
  78.     }  
  79.     if(avcodec_open2(pCodecCtx, pCodec,NULL)<0)  
  80.     {  
  81.         printf("Could not open codec.(无法打开解码器)\n");  
  82.         return -1;  
  83.     }  
  84.     AVFrame *pFrame,*pFrameYUV;  
  85.     pFrame=avcodec_alloc_frame();  
  86.     pFrameYUV=avcodec_alloc_frame();  
  87.     uint8_t *out_buffer=(uint8_t *)av_malloc(avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height));  
  88.     avpicture_fill((AVPicture *)pFrameYUV, out_buffer, PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);  
  89. //------------SDL----------------  
  90.     if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {    
  91.         printf( "Could not initialize SDL - %s\n", SDL_GetError());   
  92.         return -1;  
  93.     }   
  94.   
  95.     int screen_w=0,screen_h=0;  
  96.     SDL_Surface *screen;   
  97. #if SHOW_FULLSCREEN  
  98.     const SDL_VideoInfo *vi = SDL_GetVideoInfo();  
  99.     screen_w = vi->current_w;  
  100.     screen_h = vi->current_h;  
  101.     screen = SDL_SetVideoMode(screen_w, screen_h, 0,SDL_FULLSCREEN);  
  102. #else  
  103.     screen_w = pCodecCtx->width;  
  104.     screen_h = pCodecCtx->height;  
  105.     screen = SDL_SetVideoMode(screen_w, screen_h, 0,0);  
  106. #endif  
  107.   
  108.     if(!screen) {    
  109.         printf("SDL: could not set video mode - exiting:%s\n",SDL_GetError());    
  110.         return -1;  
  111.     }  
  112.     SDL_Overlay *bmp;   
  113.     bmp = SDL_CreateYUVOverlay(pCodecCtx->width, pCodecCtx->height,SDL_YV12_OVERLAY, screen);   
  114.     SDL_Rect rect;  
  115.   
  116.     int ret, got_picture;  
  117.   
  118.     AVPacket *packet=(AVPacket *)av_malloc(sizeof(AVPacket));  
  119.     //输出一下信息-----------------------------  
  120.     printf("File Information(文件信息)---------------------\n");  
  121.     av_dump_format(pFormatCtx,0,filepath,0);  
  122.     printf("-------------------------------------------------\n");  
  123.   
  124. #if OUTPUT_YUV420P   
  125.     FILE *fp_yuv=fopen("output.yuv","wb+");    
  126. #endif    
  127.   
  128.   
  129.     struct SwsContext *img_convert_ctx;  
  130.     img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);   
  131.     //------------------------------  
  132.     while(av_read_frame(pFormatCtx, packet)>=0)  
  133.     {  
  134.         if(packet->stream_index==videoindex)  
  135.         {  
  136.             ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);  
  137.             if(ret < 0)  
  138.             {  
  139.                 printf("Decode Error.(解码错误)\n");  
  140.                 return -1;  
  141.             }  
  142.             if(got_picture)  
  143.             {  
  144.                 sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);  
  145.                   
  146. #if OUTPUT_YUV420P  
  147.                 int y_size=pCodecCtx->width*pCodecCtx->height;    
  148.                 fwrite(pFrameYUV->data[0],1,y_size,fp_yuv);    //Y   
  149.                 fwrite(pFrameYUV->data[1],1,y_size/4,fp_yuv);  //U  
  150.                 fwrite(pFrameYUV->data[2],1,y_size/4,fp_yuv);  //V  
  151. #endif  
  152.                 SDL_LockYUVOverlay(bmp);  
  153.                 bmp->pixels[0]=pFrameYUV->data[0];  
  154.                 bmp->pixels[2]=pFrameYUV->data[1];  
  155.                 bmp->pixels[1]=pFrameYUV->data[2];       
  156.                 bmp->pitches[0]=pFrameYUV->linesize[0];  
  157.                 bmp->pitches[2]=pFrameYUV->linesize[1];     
  158.                 bmp->pitches[1]=pFrameYUV->linesize[2];  
  159.                 SDL_UnlockYUVOverlay(bmp);   
  160.                 rect.x = 0;      
  161.                 rect.y = 0;      
  162.                 rect.w = screen_w;      
  163.                 rect.h = screen_h;    
  164.                 //测试自己填充数据----------------  
  165.                 SDL_DisplayYUVOverlay(bmp, &rect);   
  166.                 //延时40ms  
  167.                 SDL_Delay(40);  
  168.             }  
  169.         }  
  170.         av_free_packet(packet);  
  171.     }  
  172.     sws_freeContext(img_convert_ctx);  
  173.   
  174. #if OUTPUT_YUV420P   
  175.     fclose(fp_yuv);  
  176. #endif   
  177.   
  178.     SDL_Quit();  
  179.   
  180.     av_free(out_buffer);  
  181.     av_free(pFrameYUV);  
  182.     avcodec_close(pCodecCtx);  
  183.     avformat_close_input(&pFormatCtx);  
  184.   
  185.     return 0;  
  186. }  

1.1版之后,新添加了一个工程:simplest_ffmpeg_player_su(SU版)。


标准版在播放视频的时候,画面显示使用延时40ms的方式。这么做有两个后果:
(1)SDL弹出的窗口无法移动,一直显示是忙碌状态
(2)画面显示并不是严格的40ms一帧,因为还没有考虑解码的时间。SU(SDL Update)版在视频解码的过程中,不再使用延时40ms的方式,而是创建了一个线程,每隔40ms发送一个自定义的消息,告知主函数进行解码显示。这样做之后:
(1)SDL弹出的窗口可以移动了
(2)画面显示是严格的40ms一帧

simplest_ffmpeg_player_su(SU版)代码

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * 最简单的基于FFmpeg的视频播放器SU(SDL升级版) 
  3.  * Simplest FFmpeg Player (SDL Update) 
  4.  * 
  5.  * 雷霄骅 Lei Xiaohua 
  6.  * leixiaohua1020@126.com 
  7.  * 中国传媒大学/数字电视技术 
  8.  * Communication University of China / Digital TV Technology 
  9.  * http://blog.csdn.net/leixiaohua1020 
  10.  * 
  11.  * 本程序实现了视频文件的解码和显示(支持HEVC,H.264,MPEG2等)。 
  12.  * 是最简单的FFmpeg视频解码方面的教程。 
  13.  * 通过学习本例子可以了解FFmpeg的解码流程。 
  14.  * 本版本中使用SDL消息机制刷新视频画面。 
  15.  * This software is a simplest video player based on FFmpeg. 
  16.  * Suitable for beginner of FFmpeg. 
  17.  *  
  18.  * Version:1.1 
  19.  *  
  20.  * 备注: 
  21.  * 标准版在播放视频的时候,画面显示使用延时40ms的方式。这么做有两个后果: 
  22.  * (1)SDL弹出的窗口无法移动,一直显示是忙碌状态 
  23.  * (2)画面显示并不是严格的40ms一帧,因为还没有考虑解码的时间。 
  24.  * SU(SDL Update)版在视频解码的过程中,不再使用延时40ms的方式,而是创建了 
  25.  * 一个线程,每隔40ms发送一个自定义的消息,告知主函数进行解码显示。这样做之后: 
  26.  * (1)SDL弹出的窗口可以移动了 
  27.  * (2)画面显示是严格的40ms一帧 
  28.  * Remark: 
  29.  * Standard Version use's SDL_Delay() to control video's frame rate, it has 2 
  30.  * disadvantages: 
  31.  * (1)SDL's Screen can't be moved and always "Busy". 
  32.  * (2)Frame rate can't be accurate because it doesn't consider the time consumed  
  33.  * by avcodec_decode_video2() 
  34.  * SU(SDL Update)Version solved 2 problems above. It create a thread to send SDL  
  35.  * Event every 40ms to tell the main loop to decode and show video frames. 
  36.  */  
  37.   
  38.   
  39. #include "stdafx.h"  
  40.   
  41. extern "C"  
  42. {  
  43. #include "libavcodec/avcodec.h"  
  44. #include "libavformat/avformat.h"  
  45.     //新版里的图像转换结构需要引入的头文件  
  46. #include "libswscale/swscale.h"  
  47.     //SDL  
  48. #include "sdl/SDL.h"  
  49. #include "sdl/SDL_thread.h"  
  50.   
  51. };  
  52.   
  53. //自定义事件  
  54. //刷新画面  
  55. #define SFM_REFRESH_EVENT  (SDL_USEREVENT + 1)  
  56.   
  57. int thread_exit=0;  
  58. //Thread  
  59. int sfp_refresh_thread(void *opaque)  
  60. {  
  61.     while (thread_exit==0) {  
  62.         SDL_Event event;  
  63.         event.type = SFM_REFRESH_EVENT;  
  64.         SDL_PushEvent(&event);  
  65.         //Wait 40 ms  
  66.         SDL_Delay(40);  
  67.     }  
  68.     return 0;  
  69. }  
  70.   
  71.   
  72. int _tmain(int argc, _TCHAR* argv[])  
  73. {  
  74.     AVFormatContext *pFormatCtx;  
  75.     int             i, videoindex;  
  76.     AVCodecContext  *pCodecCtx;  
  77.     AVCodec         *pCodec;  
  78.     char filepath[]="src01_480x272_22.hm10";  
  79.     av_register_all();  
  80.     avformat_network_init();  
  81.     pFormatCtx = avformat_alloc_context();  
  82.       
  83.     if(avformat_open_input(&pFormatCtx,filepath,NULL,NULL)!=0){  
  84.         printf("Couldn't open input stream.(无法打开输入流)\n");  
  85.         return -1;  
  86.     }  
  87.     if(av_find_stream_info(pFormatCtx)<0)  
  88.     {  
  89.         printf("Couldn't find stream information.(无法获取流信息)\n");  
  90.         return -1;  
  91.     }  
  92.     videoindex=-1;  
  93.     for(i=0; i<pFormatCtx->nb_streams; i++)   
  94.         if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)  
  95.         {  
  96.             videoindex=i;  
  97.             break;  
  98.         }  
  99.     if(videoindex==-1)  
  100.     {  
  101.         printf("Didn't find a video stream.(没有找到视频流)\n");  
  102.         return -1;  
  103.     }  
  104.     pCodecCtx=pFormatCtx->streams[videoindex]->codec;  
  105.     pCodec=avcodec_find_decoder(pCodecCtx->codec_id);  
  106.     if(pCodec==NULL)  
  107.     {  
  108.         printf("Codec not found.(没有找到解码器)\n");  
  109.         return -1;  
  110.     }  
  111.     if(avcodec_open2(pCodecCtx, pCodec,NULL)<0)  
  112.     {  
  113.         printf("Could not open codec.(无法打开解码器)\n");  
  114.         return -1;  
  115.     }  
  116.     AVFrame *pFrame,*pFrameYUV;  
  117.     pFrame=avcodec_alloc_frame();  
  118.     pFrameYUV=avcodec_alloc_frame();  
  119.     uint8_t *out_buffer=(uint8_t *)av_malloc(avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height));  
  120.     avpicture_fill((AVPicture *)pFrameYUV, out_buffer, PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);  
  121. //------------SDL----------------  
  122.     if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {    
  123.         printf( "Could not initialize SDL - %s\n", SDL_GetError());   
  124.         return -1;  
  125.     }   
  126.   
  127.     int screen_w=0,screen_h=0;  
  128.     SDL_Surface *screen;   
  129.     screen_w = pCodecCtx->width;  
  130.     screen_h = pCodecCtx->height;  
  131.     screen = SDL_SetVideoMode(screen_w, screen_h, 0,0);  
  132.   
  133.     if(!screen) {    
  134.         printf("SDL: could not set video mode - exiting:%s\n",SDL_GetError());    
  135.         return -1;  
  136.     }  
  137.     SDL_Overlay *bmp;   
  138.     bmp = SDL_CreateYUVOverlay(pCodecCtx->width, pCodecCtx->height,SDL_YV12_OVERLAY, screen);   
  139.     SDL_Rect rect;  
  140.   
  141.     int ret, got_picture;  
  142.   
  143.     AVPacket *packet=(AVPacket *)av_malloc(sizeof(AVPacket));  
  144.     //输出一下信息-----------------------------  
  145.     printf("File Information(文件信息)---------------------\n");  
  146.     av_dump_format(pFormatCtx,0,filepath,0);  
  147.     printf("-------------------------------------------------\n");  
  148.       
  149.     struct SwsContext *img_convert_ctx;  
  150.     img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);   
  151.     //--------------  
  152.     SDL_Thread *video_tid = SDL_CreateThread(sfp_refresh_thread,NULL);  
  153.     //  
  154.     SDL_WM_SetCaption("Simple FFmpeg Player (SDL Update)",NULL);  
  155.   
  156.     //Event Loop  
  157.     SDL_Event event;  
  158.     for (;;) {  
  159.         //Wait  
  160.         SDL_WaitEvent(&event);  
  161.         if(event.type==SFM_REFRESH_EVENT){  
  162.             //------------------------------  
  163.             if(av_read_frame(pFormatCtx, packet)>=0){  
  164.                 if(packet->stream_index==videoindex){  
  165.                     ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);  
  166.                     if(ret < 0){  
  167.                         printf("Decode Error.(解码错误)\n");  
  168.                         return -1;  
  169.                     }  
  170.                     if(got_picture){  
  171.                         sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);  
  172.   
  173.                         SDL_LockYUVOverlay(bmp);  
  174.                         bmp->pixels[0]=pFrameYUV->data[0];  
  175.                         bmp->pixels[2]=pFrameYUV->data[1];  
  176.                         bmp->pixels[1]=pFrameYUV->data[2];       
  177.                         bmp->pitches[0]=pFrameYUV->linesize[0];  
  178.                         bmp->pitches[2]=pFrameYUV->linesize[1];     
  179.                         bmp->pitches[1]=pFrameYUV->linesize[2];  
  180.                         SDL_UnlockYUVOverlay(bmp);   
  181.                         rect.x = 0;      
  182.                         rect.y = 0;      
  183.                         rect.w = screen_w;      
  184.                         rect.h = screen_h;    
  185.                         //测试自己填充数据----------------  
  186.                         SDL_DisplayYUVOverlay(bmp, &rect);   
  187.   
  188.                     }  
  189.                 }  
  190.                 av_free_packet(packet);  
  191.             }else{  
  192.                 //Exit Thread  
  193.                 thread_exit=1;  
  194.                 break;  
  195.             }  
  196.         }  
  197.   
  198.     }  
  199.       
  200.     SDL_Quit();  
  201.   
  202.     sws_freeContext(img_convert_ctx);  
  203.   
  204.     //--------------  
  205.     av_free(out_buffer);  
  206.     av_free(pFrameYUV);  
  207.     avcodec_close(pCodecCtx);  
  208.     avformat_close_input(&pFormatCtx);  
  209.   
  210.     return 0;  
  211. }  

simplest_ffmpeg_player_su(SU版)中将simplest_ffmpeg_player(标准版)中的循环做了更改。标准版中为播放视频的循环如下代码所示。

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. main(){  
  2.     //...  
  3.     while(av_read_frame(pFormatCtx, packet)>=0)  
  4.         {  
  5.             //Decode...  
  6.             SDL_Delay(40);  
  7.         }  
  8.     //...  
  9. }  

可以看出标准版中使用SDL_Delay(40)控制视频的播放速度。这样有一些问题在前文中已经叙述。SU版定义了一个函数专门用于发送“解码和显示”的Event。

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //自定义事件  
  2. //刷新画面  
  3. #define SFM_REFRESH_EVENT  (SDL_USEREVENT + 1)  
  4.   
  5. int thread_exit=0;  
  6. //Thread  
  7. int sfp_refresh_thread(void *opaque)  
  8. {  
  9.     while (thread_exit==0) {  
  10.         SDL_Event event;  
  11.         event.type = SFM_REFRESH_EVENT;  
  12.         SDL_PushEvent(&event);  
  13.         //Wait 40 ms  
  14.         SDL_Delay(40);  
  15.     }  
  16.     return 0;  
  17. }  

主函数形式如下。使用SDL_WaitEvent()等待Event进行解码和显示。

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. main(){  
  2.     //...  
  3.     SDL_Thread *video_tid = SDL_CreateThread(sfp_refresh_thread,NULL);  
  4.     //Event Loop  
  5.     SDL_Event event;  
  6.     for (;;) {  
  7.         //Wait  
  8.         SDL_WaitEvent(&event);  
  9.         if(event.type==SFM_REFRESH_EVENT){  
  10.             //Decode...  
  11.         }  
  12.   
  13.     }  
  14.     //...  
  15. }  



结果

软件运行截图:

完整工程下载地址:

http://download.csdn.net/detail/leixiaohua1020/5122959


更新(2014.5.10)==========================


完整工程(更新版)下载地址:

http://download.csdn.net/detail/leixiaohua1020/7319153

注1:类库版本2014.5.6,已经支持HEVC以及VP9的解码,附带了这两种视频编码的码流文件。此外修改了个别变更的API函数,并且提高了一些程序的效率。

注2:新版FFmpeg类库Release下出现错误的解决方法如下:
(注:此方法适用于所有近期发布的FFmpeg类库)
VC工程属性里,linker->Optimization->References 选项,改成No(/OPT:NOREF)即可。


更新(2014.8.25)==========================


版本升级至1.1,变为2个项目:

simplest_ffmpeg_player:标准版,FFmpeg学习的开始。
simplest_ffmpeg_player_su:SU(SDL Update)版,加入了简单的SDL的Event。

simplest_ffmpeg_player(标准版)增加了以下两个选项(当然,代码量超过了100行)

1.输出解码后的YUV420P像素数据文件

2.全屏播放

以上两项可以通过文件前面的宏进行控制:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #define SHOW_FULLSCREEN 0  
  2. #define OUTPUT_YUV420P 0  

另外修补了几个的函数,例如增加了SDL_Quit()等。

simplest_ffmpeg_player_su(SU版)具体情况在上文中已经说明。

1.1版下载地址:http://download.csdn.net/detail/leixiaohua1020/7814403

SourceForge上已经更新。


更新(2014.10.4)==========================

版本升级至1.2。

1.新版本在原版本的基础上增加了“flush_decoder”功能。当av_read_frame()循环退出的时候,实际上解码器中可能还包含剩余的几帧数据。因此需要通过“flush_decoder”将这几帧数据输出。“flush_decoder”功能简而言之即直接调用avcodec_decode_video2()获得AVFrame,而不再向解码器传递AVPacket。参考代码如下:

[cpp] view plaincopy
  1. //FIX: Flush Frames remained in Codec  
  2. while (1) {  
  3.     ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);  
  4.     if (ret < 0)  
  5.         break;  
  6.     if (!got_picture)  
  7.         break;  
  8.     sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);  
  9.     //处理...  
  10. }  

2.为了更好地适应Linux等其他操作系统,做到可以跨平台,去除掉了VC特有的一些函数。比如“#include "stdafx.h"”,“_tmain()”等等。

具体信息参见文章:avcodec_decode_video2()解码视频后丢帧的问题解决

1.2版下载地址:http://download.csdn.net/detail/leixiaohua1020/8001575

SourceForge上已经更新。

=========================================


Linux下代码下载地址:

http://download.csdn.net/detail/leixiaohua1020/7696879

这个是Linux下的代码,在Ubuntu下测试可以运行,前提是安装了FFmpeg和SDL(版本1.2)。
编译命令:

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. gcc simplest_ffmpeg_player.c -g -o smp.out -lSDLmain -lSDL -lavformat -lavcodec -lavutil -lswscale  
使用方法:

下列命令即可播放同一目录下的test.flv文件。

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. ./smp.out test.flv  

FFMPEG相关学习资料

SDL GUIDE 中文译本

http://download.csdn.net/detail/leixiaohua1020/6389841
ffdoc (FFMPEG的最完整教程)
http://download.csdn.net/detail/leixiaohua1020/6377803
如何用FFmpeg编写一个简单播放器
http://download.csdn.net/detail/leixiaohua1020/6373783


补充问题

补充1:旧版程序有一个小BUG,就是sws_getContext()之后,需要调用sws_freeContext()。否则长时间运行的话,会出现内存泄露的状况。更新版已经修复。

补充2:有人会疑惑,为什么解码后的pFrame不直接用于显示,而是调用swscale()转换之后进行显示?

如果不进行转换,而是直接调用SDL进行显示的话,会发现显示出来的图像是混乱的。关键问题在于解码后的pFrame的linesize里存储的不是图像的宽度,而是比宽度大一些的一个值。其原因目前还没有仔细调查(大概是出于性能的考虑)。例如分辨率为480x272的图像,解码后的视频的linesize[0]为512,而不是480。以第1行亮度像素(pFrame->data[0])为例,从0-480存储的是亮度数据,而从480-512则存储的是无效的数据。因此需要使用swscale()进行转换。转换后去除了无效数据,linesize[0]变为480。就可以正常显示了。





0 0