最简单的基于FFMPEG+SDL的视频播放器 ver2 (采用SDL2.0)

来源:互联网 发布:淘宝企业店铺出租 编辑:程序博客网 时间:2024/04/20 09:46
转载自: http://blog.csdn.net/leixiaohua1020/article/details/38868499

简介

之前做过一个FFMPEG+SDL的简单播放器:《100行代码实现最简单的基于FFMPEG+SDL的视频播放器》。该播放器采用SDL1.2显示视频。最近有不少人反映SDL已经升级到2.0版本了,甚至官网的Wiki上都只有SDL2.0的文档了,因此下载了SDL 2.0 并且进行了简单的研究。随后对此前的播放器进行了修改,将SDL1.2换成了SDL2.0。

注:《100行代码实现最简单的基于FFMPEG+SDL的视频播放器》文章中提到的很多知识这里不再重复记录。本文重点记录SDL1.2与SDL2.0的不同。


平台使用了VC2010,FFmpeg类库使用了最近的版本,SDL使用2.0版本。


项目主页

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

Github:https://github.com/leixiaohua1020/simplest_ffmpeg_player

开源中国:http://git.oschina.net/leixiaohua1020/simplest_ffmpeg_player


流程图


FFmpeg解码一个视频流程如下图所示:


SDL2.0显示YUV的流程图:


对比SDL1.2的流程图,发现变化还是很大的。几乎所有的API都发生了变化。但是函数和变量有一定的对应关系:

SDL_SetVideoMode()————SDL_CreateWindow()

SDL_Surface————SDL_Window

SDL_CreateYUVOverlay()————SDL_CreateTexture()

SDL_Overlay————SDL_Texture

不再一一例举。

下图为SDL1.x显示YUV的流程图。


简单解释各个变量的作用:

SDL_Window就是使用SDL的时候弹出的那个窗口。在SDL1.x版本中,只可以创建一个一个窗口。在SDL2.0版本中,可以创建多个窗口。
SDL_Texture用于显示YUV数据。一个SDL_Texture对应一帧YUV数据。
SDL_Renderer用于渲染SDL_Texture至SDL_Window。
SDL_Rect用于确定SDL_Texture显示的位置。注意:一个SDL_Texture可以指定多个不同的SDL_Rect,这样就可以在SDL_Window不同位置显示相同的内容(使用SDL_RenderCopy()函数)。
它们的关系如下图所示:


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




simplest_ffmpeg_player(标准版)代码

最基础的版本,学习的开始。

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * 最简单的基于FFmpeg的视频播放器 2 
  3.  * Simplest FFmpeg Player 2 
  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.  * 第2版使用SDL2.0取代了第一版中的SDL1.2 
  12.  * Version 2 use SDL 2.0 instead of SDL 1.2 in version 1. 
  13.  * 
  14.  * 本程序实现了视频文件的解码和显示(支持HEVC,H.264,MPEG2等)。 
  15.  * 是最简单的FFmpeg视频解码方面的教程。 
  16.  * 通过学习本例子可以了解FFmpeg的解码流程。 
  17.  * This software is a simplest video player based on FFmpeg. 
  18.  * Suitable for beginner of FFmpeg. 
  19.  * 
  20.  * Version:2 
  21.  */  
  22.   
  23.   
  24. #include "stdafx.h"  
  25.   
  26. extern "C"  
  27. {  
  28. #include "libavcodec/avcodec.h"  
  29. #include "libavformat/avformat.h"  
  30. #include "libswscale/swscale.h"  
  31.     //SDL  
  32. #include "sdl/SDL.h"  
  33. #include "sdl/SDL_thread.h"  
  34.   
  35. };  
  36.   
  37. //Output YUV420P data as a file   
  38. #define OUTPUT_YUV420P 0  
  39.   
  40. int _tmain(int argc, _TCHAR* argv[])  
  41. {  
  42.   
  43.     AVFormatContext *pFormatCtx;  
  44.     int             i, videoindex;  
  45.     AVCodecContext  *pCodecCtx;  
  46.     AVCodec         *pCodec;  
  47.     char filepath[]="src01_480x272_22.h265";  
  48.     av_register_all();  
  49.     avformat_network_init();  
  50.     pFormatCtx = avformat_alloc_context();  
  51.       
  52.     if(avformat_open_input(&pFormatCtx,filepath,NULL,NULL)!=0){  
  53.         printf("Couldn't open input stream.(无法打开输入流)\n");  
  54.         return -1;  
  55.     }  
  56.     if(av_find_stream_info(pFormatCtx)<0)  
  57.     {  
  58.         printf("Couldn't find stream information.(无法获取流信息)\n");  
  59.         return -1;  
  60.     }  
  61.     videoindex=-1;  
  62.     for(i=0; i<pFormatCtx->nb_streams; i++)   
  63.         if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)  
  64.         {  
  65.             videoindex=i;  
  66.             break;  
  67.         }  
  68.     if(videoindex==-1)  
  69.     {  
  70.         printf("Didn't find a video stream.(没有找到视频流)\n");  
  71.         return -1;  
  72.     }  
  73.     pCodecCtx=pFormatCtx->streams[videoindex]->codec;  
  74.     pCodec=avcodec_find_decoder(pCodecCtx->codec_id);  
  75.     if(pCodec==NULL)  
  76.     {  
  77.         printf("Codec not found.(没有找到解码器)\n");  
  78.         return -1;  
  79.     }  
  80.     if(avcodec_open2(pCodecCtx, pCodec,NULL)<0)  
  81.     {  
  82.         printf("Could not open codec.(无法打开解码器)\n");  
  83.         return -1;  
  84.     }  
  85.     AVFrame *pFrame,*pFrameYUV;  
  86.     pFrame=avcodec_alloc_frame();  
  87.     pFrameYUV=avcodec_alloc_frame();  
  88.     uint8_t *out_buffer=(uint8_t *)av_malloc(avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height));  
  89.     avpicture_fill((AVPicture *)pFrameYUV, out_buffer, PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);  
  90.     //SDL---------------------------  
  91.     if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {    
  92.         printf( "Could not initialize SDL - %s\n", SDL_GetError());   
  93.         return -1;  
  94.     }   
  95.   
  96.     int screen_w=0,screen_h=0;  
  97.     //SDL 2.0 Support for multiple windows  
  98.     SDL_Window *screen;   
  99.     screen_w = pCodecCtx->width;  
  100.     screen_h = pCodecCtx->height;  
  101.     screen = SDL_CreateWindow("Simplest ffmpeg player's Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,  
  102.         screen_w, screen_h,  
  103.         SDL_WINDOW_OPENGL);  
  104.   
  105.   
  106.     if(!screen) {    
  107.         printf("SDL: could not create window - exiting:%s\n",SDL_GetError());    
  108.         return -1;  
  109.     }  
  110.   
  111.     SDL_Renderer* sdlRenderer = SDL_CreateRenderer(screen, -1, 0);    
  112.     //IYUV: Y + U + V  (3 planes)  
  113.     //YV12: Y + V + U  (3 planes)  
  114.     SDL_Texture* sdlTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING,pCodecCtx->width,pCodecCtx->height);    
  115.       
  116.     SDL_Rect sdlRect;    
  117.     sdlRect.x = 0;    
  118.     sdlRect.y = 0;    
  119.     sdlRect.w = screen_w;    
  120.     sdlRect.h = screen_h;    
  121.     //SDL End----------------------  
  122.     int ret, got_picture;  
  123.     AVPacket *packet=(AVPacket *)av_malloc(sizeof(AVPacket));  
  124.     //Output Info-----------------------------  
  125.     printf("File Information(文件信息)---------------------\n");  
  126.     av_dump_format(pFormatCtx,0,filepath,0);  
  127.     printf("-------------------------------------------------\n");  
  128.   
  129. #if OUTPUT_YUV420P   
  130.     FILE *fp_yuv=fopen("output.yuv","wb+");    
  131. #endif    
  132.   
  133.     struct SwsContext *img_convert_ctx;  
  134.     img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);   
  135.     //------------------------------  
  136.     while(av_read_frame(pFormatCtx, packet)>=0)  
  137.     {  
  138.         if(packet->stream_index==videoindex)  
  139.         {  
  140.             ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);  
  141.             if(ret < 0)  
  142.             {  
  143.                 printf("Decode Error.(解码错误)\n");  
  144.                 return -1;  
  145.             }  
  146.             if(got_picture)  
  147.             {  
  148.                 sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);  
  149.                   
  150. #if OUTPUT_YUV420P  
  151.                 int y_size=pCodecCtx->width*pCodecCtx->height;    
  152.                 fwrite(pFrameYUV->data[0],1,y_size,fp_yuv); //Y   
  153.                 fwrite(pFrameYUV->data[1],1,y_size/4,fp_yuv);  //U  
  154.                 fwrite(pFrameYUV->data[2],1,y_size/4,fp_yuv);  //V  
  155. #endif  
  156.                 //SDL---------------------------  
  157.                 SDL_UpdateTexture( sdlTexture, &sdlRect, pFrameYUV->data[0], pFrameYUV->linesize[0] );    
  158.                 SDL_RenderClear( sdlRenderer );    
  159.                 SDL_RenderCopy( sdlRenderer, sdlTexture, &sdlRect, &sdlRect );    
  160.                 SDL_RenderPresent( sdlRenderer );    
  161.                 //SDL End-----------------------  
  162.                 //Delay 40ms  
  163.                 SDL_Delay(40);  
  164.             }  
  165.         }  
  166.         av_free_packet(packet);  
  167.     }  
  168.     sws_freeContext(img_convert_ctx);  
  169.   
  170. #if OUTPUT_YUV420P   
  171.     fclose(fp_yuv);  
  172. #endif   
  173.   
  174.     SDL_Quit();  
  175.   
  176.     av_free(out_buffer);  
  177.     av_free(pFrameYUV);  
  178.     avcodec_close(pCodecCtx);  
  179.     avformat_close_input(&pFormatCtx);  
  180.   
  181.     return 0;  
  182. }  



simplest_ffmpeg_player_su(SU版)代码

标准版的基础之上引入了SDL的Event。

效果如下:

(1)SDL弹出的窗口可以移动了
(2)画面显示是严格的40ms一帧

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * 最简单的基于FFmpeg的视频播放器2(SDL升级版) 
  3.  * Simplest FFmpeg Player 2(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.  * 第2版使用SDL2.0取代了第一版中的SDL1.2 
  12.  * Version 2 use SDL 2.0 instead of SDL 1.2 in version 1. 
  13.  * 
  14.  * 本程序实现了视频文件的解码和显示(支持HEVC,H.264,MPEG2等)。 
  15.  * 是最简单的FFmpeg视频解码方面的教程。 
  16.  * 通过学习本例子可以了解FFmpeg的解码流程。 
  17.  * 本版本中使用SDL消息机制刷新视频画面。 
  18.  * This software is a simplest video player based on FFmpeg. 
  19.  * Suitable for beginner of FFmpeg. 
  20.  *  
  21.  * Version: 2 
  22.  */  
  23.   
  24.   
  25. #include "stdafx.h"  
  26.   
  27. extern "C"  
  28. {  
  29. #include "libavcodec/avcodec.h"  
  30. #include "libavformat/avformat.h"  
  31. #include "libswscale/swscale.h"  
  32.     //SDL  
  33. #include "sdl/SDL.h"  
  34. #include "sdl/SDL_thread.h"  
  35.   
  36. };  
  37.   
  38.   
  39. //Refresh Event  
  40. #define SFM_REFRESH_EVENT  (SDL_USEREVENT + 1)  
  41.   
  42. int thread_exit=0;  
  43.   
  44. int sfp_refresh_thread(void *opaque)  
  45. {  
  46.     while (thread_exit==0) {  
  47.         SDL_Event event;  
  48.         event.type = SFM_REFRESH_EVENT;  
  49.         SDL_PushEvent(&event);  
  50.         SDL_Delay(40);  
  51.     }  
  52.     return 0;  
  53. }  
  54.   
  55.   
  56. int _tmain(int argc, _TCHAR* argv[])  
  57. {  
  58.   
  59.     AVFormatContext *pFormatCtx;  
  60.     int             i, videoindex;  
  61.     AVCodecContext  *pCodecCtx;  
  62.     AVCodec         *pCodec;  
  63.     char filepath[]="src01_480x272_22.h265";  
  64.   
  65.     av_register_all();  
  66.     avformat_network_init();  
  67.     pFormatCtx = avformat_alloc_context();  
  68.   
  69.     if(avformat_open_input(&pFormatCtx,filepath,NULL,NULL)!=0){  
  70.         printf("Couldn't open input stream.(无法打开输入流)\n");  
  71.         return -1;  
  72.     }  
  73.     if(av_find_stream_info(pFormatCtx)<0)  
  74.     {  
  75.         printf("Couldn't find stream information.(无法获取流信息)\n");  
  76.         return -1;  
  77.     }  
  78.     videoindex=-1;  
  79.     for(i=0; i<pFormatCtx->nb_streams; i++)   
  80.         if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)  
  81.         {  
  82.             videoindex=i;  
  83.             break;  
  84.         }  
  85.     if(videoindex==-1)  
  86.     {  
  87.         printf("Didn't find a video stream.(没有找到视频流)\n");  
  88.         return -1;  
  89.     }  
  90.     pCodecCtx=pFormatCtx->streams[videoindex]->codec;  
  91.     pCodec=avcodec_find_decoder(pCodecCtx->codec_id);  
  92.     if(pCodec==NULL)  
  93.     {  
  94.         printf("Codec not found.(没有找到解码器)\n");  
  95.         return -1;  
  96.     }  
  97.     if(avcodec_open2(pCodecCtx, pCodec,NULL)<0)  
  98.     {  
  99.         printf("Could not open codec.(无法打开解码器)\n");  
  100.         return -1;  
  101.     }  
  102.     AVFrame *pFrame,*pFrameYUV;  
  103.     pFrame=avcodec_alloc_frame();  
  104.     pFrameYUV=avcodec_alloc_frame();  
  105.     uint8_t *out_buffer=(uint8_t *)av_malloc(avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height));  
  106.     avpicture_fill((AVPicture *)pFrameYUV, out_buffer, PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);  
  107. //------------SDL----------------  
  108.     if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {    
  109.         printf( "Could not initialize SDL - %s\n", SDL_GetError());   
  110.         return -1;  
  111.     }   
  112.   
  113.     int screen_w=0,screen_h=0;  
  114.     SDL_Window *screen;   
  115.     //SDL 2.0 Support for multiple windows  
  116.     screen_w = pCodecCtx->width;  
  117.     screen_h = pCodecCtx->height;  
  118.     screen = SDL_CreateWindow("Simplest ffmpeg player's Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,  
  119.         screen_w, screen_h,  
  120.         SDL_WINDOW_OPENGL);  
  121.   
  122.     if(!screen) {    
  123.         printf("SDL: could not create window - exiting:%s\n",SDL_GetError());    
  124.         return -1;  
  125.     }  
  126.   
  127.   
  128.     SDL_Renderer* sdlRenderer = SDL_CreateRenderer(screen, -1, 0);    
  129.     //IYUV: Y + U + V  (3 planes)  
  130.     //YV12: Y + V + U  (3 planes)  
  131.     SDL_Texture* sdlTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING,pCodecCtx->width,pCodecCtx->height);    
  132.   
  133.     SDL_Rect sdlRect;    
  134.     sdlRect.x = 0;    
  135.     sdlRect.y = 0;    
  136.     sdlRect.w = screen_w;    
  137.     sdlRect.h = screen_h;    
  138.   
  139.     int ret, got_picture;  
  140.   
  141.     AVPacket *packet=(AVPacket *)av_malloc(sizeof(AVPacket));  
  142.     //Output Info-----------------------------  
  143.     printf("File Information(文件信息)---------------------\n");  
  144.     av_dump_format(pFormatCtx,0,filepath,0);  
  145.     printf("-------------------------------------------------\n");  
  146.       
  147.     struct SwsContext *img_convert_ctx;  
  148.     img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);   
  149.     //--------------  
  150.     SDL_Thread *video_tid = SDL_CreateThread(sfp_refresh_thread,NULL,NULL);  
  151.     //  
  152.     //Event Loop  
  153.     SDL_Event event;  
  154.     for (;;) {  
  155.         //Wait  
  156.         SDL_WaitEvent(&event);  
  157.         if(event.type==SFM_REFRESH_EVENT){  
  158.             //------------------------------  
  159.             if(av_read_frame(pFormatCtx, packet)>=0){  
  160.                 if(packet->stream_index==videoindex){  
  161.                     ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);  
  162.                     if(ret < 0){  
  163.                         printf("Decode Error.(解码错误)\n");  
  164.                         return -1;  
  165.                     }  
  166.                     if(got_picture){  
  167.                         sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);  
  168.                         //SDL---------------------------  
  169.                         SDL_UpdateTexture( sdlTexture, &sdlRect, pFrameYUV->data[0], pFrameYUV->linesize[0] );    
  170.                         SDL_RenderClear( sdlRenderer );    
  171.                         SDL_RenderCopy( sdlRenderer, sdlTexture, &sdlRect, &sdlRect );    
  172.                         SDL_RenderPresent( sdlRenderer );    
  173.                         //SDL End-----------------------  
  174.                     }  
  175.                 }  
  176.                 av_free_packet(packet);  
  177.             }else{  
  178.                 //Exit Thread  
  179.                 thread_exit=1;  
  180.                 break;  
  181.             }  
  182.         }  
  183.   
  184.     }  
  185.   
  186.     sws_freeContext(img_convert_ctx);  
  187.   
  188.     SDL_Quit();  
  189.     //--------------  
  190.     av_free(out_buffer);  
  191.     av_free(pFrameYUV);  
  192.     avcodec_close(pCodecCtx);  
  193.     avformat_close_input(&pFormatCtx);  
  194.   
  195.     return 0;  
  196. }  



运行结果




下载

CSDN完整工程下载地址:

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

更新(2014.10.5)==============================

版本升级至2.2。

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

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  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()解码视频后丢帧的问题解决

2.2版下载地址:http://download.csdn.net/detail/leixiaohua1020/8002337

更新(2015.1.3)==============================

版本升级至2.3。

新增一个工程:最简单的基于FFmpeg的解码器-纯净版(不包含libavformat)

2.3版下载地址:http://download.csdn.net/detail/leixiaohua1020/8322307



0 0
原创粉丝点击