最簡單的基於FFMPEG+SDL的視頻播放器 ver2 (采用SDL2.0)


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

最簡單的基於FFmpeg的視頻播放器系列文章列表:

100行代碼實現最簡單的基於FFMPEG+SDL的視頻播放器(SDL1.x)

最簡單的基於FFMPEG+SDL的視頻播放器 ver2 (采用SDL2.0)

最簡單的基於FFmpeg的解碼器-純凈版(不包含libavformat)

最簡單的基於FFMPEG+SDL的視頻播放器:拆分-解碼器和播放器

最簡單的基於FFMPEG的Helloworld程序

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


簡介

之前做過一個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版本。


Simplest FFmpeg Player 2


項目主頁

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 plain  copy
  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.  */  
  21.   
  22.   
  23.   
  24. #include <stdio.h>  
  25.   
  26. #define __STDC_CONSTANT_MACROS  
  27.   
  28. #ifdef _WIN32  
  29. //Windows  
  30. extern "C"  
  31. {  
  32. #include "libavcodec/avcodec.h"  
  33. #include "libavformat/avformat.h"  
  34. #include "libswscale/swscale.h"  
  35. #include "libavutil/imgutils.h"  
  36. #include "SDL2/SDL.h"  
  37. };  
  38. #else  
  39. //Linux...  
  40. #ifdef __cplusplus  
  41. extern "C"  
  42. {  
  43. #endif  
  44. #include <libavcodec/avcodec.h>  
  45. #include <libavformat/avformat.h>  
  46. #include <libswscale/swscale.h>  
  47. #include <SDL2/SDL.h>  
  48. #include <libavutil/imgutils.h>  
  49. #ifdef __cplusplus  
  50. };  
  51. #endif  
  52. #endif  
  53.   
  54. //Output YUV420P data as a file   
  55. #define OUTPUT_YUV420P 0  
  56.   
  57. int main(int argc, char* argv[])  
  58. {  
  59.     AVFormatContext *pFormatCtx;  
  60.     int             i, videoindex;  
  61.     AVCodecContext  *pCodecCtx;  
  62.     AVCodec         *pCodec;  
  63.     AVFrame *pFrame,*pFrameYUV;  
  64.     unsigned char *out_buffer;  
  65.     AVPacket *packet;  
  66.     int y_size;  
  67.     int ret, got_picture;  
  68.     struct SwsContext *img_convert_ctx;  
  69.   
  70.     char filepath[]="bigbuckbunny_480x272.h265";  
  71.     //SDL---------------------------  
  72.     int screen_w=0,screen_h=0;  
  73.     SDL_Window *screen;   
  74.     SDL_Renderer* sdlRenderer;  
  75.     SDL_Texture* sdlTexture;  
  76.     SDL_Rect sdlRect;  
  77.   
  78.     FILE *fp_yuv;  
  79.   
  80.     av_register_all();  
  81.     avformat_network_init();  
  82.     pFormatCtx = avformat_alloc_context();  
  83.   
  84.     if(avformat_open_input(&pFormatCtx,filepath,NULL,NULL)!=0){  
  85.         printf("Couldn't open input stream.\n");  
  86.         return -1;  
  87.     }  
  88.     if(avformat_find_stream_info(pFormatCtx,NULL)<0){  
  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.             videoindex=i;  
  96.             break;  
  97.         }  
  98.     if(videoindex==-1){  
  99.         printf("Didn't find a video stream.\n");  
  100.         return -1;  
  101.     }  
  102.   
  103.     pCodecCtx=pFormatCtx->streams[videoindex]->codec;  
  104.     pCodec=avcodec_find_decoder(pCodecCtx->codec_id);  
  105.     if(pCodec==NULL){  
  106.         printf("Codec not found.\n");  
  107.         return -1;  
  108.     }  
  109.     if(avcodec_open2(pCodecCtx, pCodec,NULL)<0){  
  110.         printf("Could not open codec.\n");  
  111.         return -1;  
  112.     }  
  113.       
  114.     pFrame=av_frame_alloc();  
  115.     pFrameYUV=av_frame_alloc();  
  116.     out_buffer=(unsigned char *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P,  pCodecCtx->width, pCodecCtx->height,1));  
  117.     av_image_fill_arrays(pFrameYUV->data, pFrameYUV->linesize,out_buffer,  
  118.         AV_PIX_FMT_YUV420P,pCodecCtx->width, pCodecCtx->height,1);  
  119.       
  120.     packet=(AVPacket *)av_malloc(sizeof(AVPacket));  
  121.     //Output Info-----------------------------  
  122.     printf("--------------- File Information ----------------\n");  
  123.     av_dump_format(pFormatCtx,0,filepath,0);  
  124.     printf("-------------------------------------------------\n");  
  125.     img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,   
  126.         pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);   
  127.   
  128. #if OUTPUT_YUV420P   
  129.     fp_yuv=fopen("output.yuv","wb+");    
  130. #endif    
  131.       
  132.     if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {    
  133.         printf( "Could not initialize SDL - %s\n", SDL_GetError());   
  134.         return -1;  
  135.     }   
  136.   
  137.     screen_w = pCodecCtx->width;  
  138.     screen_h = pCodecCtx->height;  
  139.     //SDL 2.0 Support for multiple windows  
  140.     screen = SDL_CreateWindow("Simplest ffmpeg player's Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,  
  141.         screen_w, screen_h,  
  142.         SDL_WINDOW_OPENGL);  
  143.   
  144.     if(!screen) {    
  145.         printf("SDL: could not create window - exiting:%s\n",SDL_GetError());    
  146.         return -1;  
  147.     }  
  148.   
  149.     sdlRenderer = SDL_CreateRenderer(screen, -1, 0);    
  150.     //IYUV: Y + U + V  (3 planes)  
  151.     //YV12: Y + V + U  (3 planes)  
  152.     sdlTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING,pCodecCtx->width,pCodecCtx->height);    
  153.   
  154.     sdlRect.x=0;  
  155.     sdlRect.y=0;  
  156.     sdlRect.w=screen_w;  
  157.     sdlRect.h=screen_h;  
  158.   
  159.     //SDL End----------------------  
  160.     while(av_read_frame(pFormatCtx, packet)>=0){  
  161.         if(packet->stream_index==videoindex){  
  162.             ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);  
  163.             if(ret < 0){  
  164.                 printf("Decode Error.\n");  
  165.                 return -1;  
  166.             }  
  167.             if(got_picture){  
  168.                 sws_scale(img_convert_ctx, (const unsigned charconst*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height,   
  169.                     pFrameYUV->data, pFrameYUV->linesize);  
  170.                   
  171. #if OUTPUT_YUV420P  
  172.                 y_size=pCodecCtx->width*pCodecCtx->height;    
  173.                 fwrite(pFrameYUV->data[0],1,y_size,fp_yuv);    //Y   
  174.                 fwrite(pFrameYUV->data[1],1,y_size/4,fp_yuv);  //U  
  175.                 fwrite(pFrameYUV->data[2],1,y_size/4,fp_yuv);  //V  
  176. #endif  
  177.                 //SDL---------------------------  
  178. #if 0  
  179.                 SDL_UpdateTexture( sdlTexture, NULL, pFrameYUV->data[0], pFrameYUV->linesize[0] );    
  180. #else  
  181.                 SDL_UpdateYUVTexture(sdlTexture, &sdlRect,  
  182.                 pFrameYUV->data[0], pFrameYUV->linesize[0],  
  183.                 pFrameYUV->data[1], pFrameYUV->linesize[1],  
  184.                 pFrameYUV->data[2], pFrameYUV->linesize[2]);  
  185. #endif    
  186.                   
  187.                 SDL_RenderClear( sdlRenderer );    
  188.                 SDL_RenderCopy( sdlRenderer, sdlTexture,  NULL, &sdlRect);    
  189.                 SDL_RenderPresent( sdlRenderer );    
  190.                 //SDL End-----------------------  
  191.                 //Delay 40ms  
  192.                 SDL_Delay(40);  
  193.             }  
  194.         }  
  195.         av_free_packet(packet);  
  196.     }  
  197.     //flush decoder  
  198.     //FIX: Flush Frames remained in Codec  
  199.     while (1) {  
  200.         ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);  
  201.         if (ret < 0)  
  202.             break;  
  203.         if (!got_picture)  
  204.             break;  
  205.         sws_scale(img_convert_ctx, (const unsigned charconst*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height,   
  206.             pFrameYUV->data, pFrameYUV->linesize);  
  207. #if OUTPUT_YUV420P  
  208.         int y_size=pCodecCtx->width*pCodecCtx->height;    
  209.         fwrite(pFrameYUV->data[0],1,y_size,fp_yuv);    //Y   
  210.         fwrite(pFrameYUV->data[1],1,y_size/4,fp_yuv);  //U  
  211.         fwrite(pFrameYUV->data[2],1,y_size/4,fp_yuv);  //V  
  212. #endif  
  213.         //SDL---------------------------  
  214.         SDL_UpdateTexture( sdlTexture, &sdlRect, pFrameYUV->data[0], pFrameYUV->linesize[0] );    
  215.         SDL_RenderClear( sdlRenderer );    
  216.         SDL_RenderCopy( sdlRenderer, sdlTexture,  NULL, &sdlRect);    
  217.         SDL_RenderPresent( sdlRenderer );    
  218.         //SDL End-----------------------  
  219.         //Delay 40ms  
  220.         SDL_Delay(40);  
  221.     }  
  222.   
  223.     sws_freeContext(img_convert_ctx);  
  224.   
  225. #if OUTPUT_YUV420P   
  226.     fclose(fp_yuv);  
  227. #endif   
  228.   
  229.     SDL_Quit();  
  230.   
  231.     av_frame_free(&pFrameYUV);  
  232.     av_frame_free(&pFrame);  
  233.     avcodec_close(pCodecCtx);  
  234.     avformat_close_input(&pFormatCtx);  
  235.   
  236.     return 0;  
  237. }  



simplest_ffmpeg_player_su(SU版)代碼

標准版的基礎之上引入了SDL的Event。

效果如下:

(1)SDL彈出的窗口可以移動了
(2)畫面顯示是嚴格的40ms一幀

[cpp]  view plain  copy
  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.  * 備注: 
  22.  * 標准版在播放視頻的時候,畫面顯示使用延時40ms的方式。這么做有兩個后果: 
  23.  * (1)SDL彈出的窗口無法移動,一直顯示是忙碌狀態 
  24.  * (2)畫面顯示並不是嚴格的40ms一幀,因為還沒有考慮解碼的時間。 
  25.  * SU(SDL Update)版在視頻解碼的過程中,不再使用延時40ms的方式,而是創建了 
  26.  * 一個線程,每隔40ms發送一個自定義的消息,告知主函數進行解碼顯示。這樣做之后: 
  27.  * (1)SDL彈出的窗口可以移動了 
  28.  * (2)畫面顯示是嚴格的40ms一幀 
  29.  * Remark: 
  30.  * Standard Version use's SDL_Delay() to control video's frame rate, it has 2 
  31.  * disadvantages: 
  32.  * (1)SDL's Screen can't be moved and always "Busy". 
  33.  * (2)Frame rate can't be accurate because it doesn't consider the time consumed  
  34.  * by avcodec_decode_video2() 
  35.  * SU(SDL Update)Version solved 2 problems above. It create a thread to send SDL  
  36.  * Event every 40ms to tell the main loop to decode and show video frames. 
  37.  */  
  38.   
  39. #include <stdio.h>  
  40.   
  41. #define __STDC_CONSTANT_MACROS  
  42.   
  43. #ifdef _WIN32  
  44. //Windows  
  45. extern "C"  
  46. {  
  47. #include "libavcodec/avcodec.h"  
  48. #include "libavformat/avformat.h"  
  49. #include "libswscale/swscale.h"  
  50. #include "libavutil/imgutils.h"  
  51. #include "SDL2/SDL.h"  
  52. };  
  53. #else  
  54. //Linux...  
  55. #ifdef __cplusplus  
  56. extern "C"  
  57. {  
  58. #endif  
  59. #include <libavcodec/avcodec.h>  
  60. #include <libavformat/avformat.h>  
  61. #include <libswscale/swscale.h>  
  62. #include <libavutil/imgutils.h>  
  63. #include <SDL2/SDL.h>  
  64. #ifdef __cplusplus  
  65. };  
  66. #endif  
  67. #endif  
  68.   
  69. //Refresh Event  
  70. #define SFM_REFRESH_EVENT  (SDL_USEREVENT + 1)  
  71.   
  72. #define SFM_BREAK_EVENT  (SDL_USEREVENT + 2)  
  73.   
  74. int thread_exit=0;  
  75. int thread_pause=0;  
  76.   
  77. int sfp_refresh_thread(void *opaque){  
  78.     thread_exit=0;  
  79.     thread_pause=0;  
  80.   
  81.     while (!thread_exit) {  
  82.         if(!thread_pause){  
  83.             SDL_Event event;  
  84.             event.type = SFM_REFRESH_EVENT;  
  85.             SDL_PushEvent(&event);  
  86.         }  
  87.         SDL_Delay(40);  
  88.     }  
  89.     thread_exit=0;  
  90.     thread_pause=0;  
  91.     //Break  
  92.     SDL_Event event;  
  93.     event.type = SFM_BREAK_EVENT;  
  94.     SDL_PushEvent(&event);  
  95.   
  96.     return 0;  
  97. }  
  98.   
  99.   
  100. int main(int argc, char* argv[])  
  101. {  
  102.   
  103.     AVFormatContext *pFormatCtx;  
  104.     int             i, videoindex;  
  105.     AVCodecContext  *pCodecCtx;  
  106.     AVCodec         *pCodec;  
  107.     AVFrame *pFrame,*pFrameYUV;  
  108.     unsigned char *out_buffer;  
  109.     AVPacket *packet;  
  110.     int ret, got_picture;  
  111.   
  112.     //------------SDL----------------  
  113.     int screen_w,screen_h;  
  114.     SDL_Window *screen;   
  115.     SDL_Renderer* sdlRenderer;  
  116.     SDL_Texture* sdlTexture;  
  117.     SDL_Rect sdlRect;  
  118.     SDL_Thread *video_tid;  
  119.     SDL_Event event;  
  120.   
  121.     struct SwsContext *img_convert_ctx;  
  122.   
  123.     //char filepath[]="bigbuckbunny_480x272.h265";  
  124.     char filepath[]="Titanic.ts";  
  125.   
  126.     av_register_all();  
  127.     avformat_network_init();  
  128.     pFormatCtx = avformat_alloc_context();  
  129.   
  130.     if(avformat_open_input(&pFormatCtx,filepath,NULL,NULL)!=0){  
  131.         printf("Couldn't open input stream.\n");  
  132.         return -1;  
  133.     }  
  134.     if(avformat_find_stream_info(pFormatCtx,NULL)<0){  
  135.         printf("Couldn't find stream information.\n");  
  136.         return -1;  
  137.     }  
  138.     videoindex=-1;  
  139.     for(i=0; i<pFormatCtx->nb_streams; i++)   
  140.         if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO){  
  141.             videoindex=i;  
  142.             break;  
  143.         }  
  144.     if(videoindex==-1){  
  145.         printf("Didn't find a video stream.\n");  
  146.         return -1;  
  147.     }  
  148.     pCodecCtx=pFormatCtx->streams[videoindex]->codec;  
  149.     pCodec=avcodec_find_decoder(pCodecCtx->codec_id);  
  150.     if(pCodec==NULL){  
  151.         printf("Codec not found.\n");  
  152.         return -1;  
  153.     }  
  154.     if(avcodec_open2(pCodecCtx, pCodec,NULL)<0){  
  155.         printf("Could not open codec.\n");  
  156.         return -1;  
  157.     }  
  158.     pFrame=av_frame_alloc();  
  159.     pFrameYUV=av_frame_alloc();  
  160.   
  161.     out_buffer=(unsigned char *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P,  pCodecCtx->width, pCodecCtx->height,1));  
  162.     av_image_fill_arrays(pFrameYUV->data, pFrameYUV->linesize,out_buffer,  
  163.         AV_PIX_FMT_YUV420P,pCodecCtx->width, pCodecCtx->height,1);  
  164.   
  165.     //Output Info-----------------------------  
  166.     printf("---------------- File Information ---------------\n");  
  167.     av_dump_format(pFormatCtx,0,filepath,0);  
  168.     printf("-------------------------------------------------\n");  
  169.       
  170.     img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,   
  171.         pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);   
  172.       
  173.   
  174.     if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {    
  175.         printf( "Could not initialize SDL - %s\n", SDL_GetError());   
  176.         return -1;  
  177.     }   
  178.     //SDL 2.0 Support for multiple windows  
  179.     screen_w = pCodecCtx->width;  
  180.     screen_h = pCodecCtx->height;  
  181.     screen = SDL_CreateWindow("Simplest ffmpeg player's Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,  
  182.         screen_w, screen_h,SDL_WINDOW_OPENGL);  
  183.   
  184.     if(!screen) {    
  185.         printf("SDL: could not create window - exiting:%s\n",SDL_GetError());    
  186.         return -1;  
  187.     }  
  188.     sdlRenderer = SDL_CreateRenderer(screen, -1, 0);    
  189.     //IYUV: Y + U + V  (3 planes)  
  190.     //YV12: Y + V + U  (3 planes)  
  191.     sdlTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING,pCodecCtx->width,pCodecCtx->height);    
  192.   
  193.     sdlRect.x=0;  
  194.     sdlRect.y=0;  
  195.     sdlRect.w=screen_w;  
  196.     sdlRect.h=screen_h;  
  197.   
  198.     packet=(AVPacket *)av_malloc(sizeof(AVPacket));  
  199.   
  200.     video_tid = SDL_CreateThread(sfp_refresh_thread,NULL,NULL);  
  201.     //------------SDL End------------  
  202.     //Event Loop  
  203.       
  204.     for (;;) {  
  205.         //Wait  
  206.         SDL_WaitEvent(&event);  
  207.         if(event.type==SFM_REFRESH_EVENT){  
  208.             while(1){  
  209.                 if(av_read_frame(pFormatCtx, packet)<0)  
  210.                     thread_exit=1;  
  211.   
  212.                 if(packet->stream_index==videoindex)  
  213.                     break;  
  214.             }  
  215.             ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);  
  216.             if(ret < 0){  
  217.                 printf("Decode Error.\n");  
  218.                 return -1;  
  219.             }  
  220.             if(got_picture){  
  221.                 sws_scale(img_convert_ctx, (const unsigned charconst*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);  
  222.                 //SDL---------------------------  
  223.                 SDL_UpdateTexture( sdlTexture, NULL, pFrameYUV->data[0], pFrameYUV->linesize[0] );    
  224.                 SDL_RenderClear( sdlRenderer );    
  225.                 //SDL_RenderCopy( sdlRenderer, sdlTexture, &sdlRect, &sdlRect );    
  226.                 SDL_RenderCopy( sdlRenderer, sdlTexture, NULL, NULL);    
  227.                 SDL_RenderPresent( sdlRenderer );    
  228.                 //SDL End-----------------------  
  229.             }  
  230.             av_free_packet(packet);  
  231.         }else if(event.type==SDL_KEYDOWN){  
  232.             //Pause  
  233.             if(event.key.keysym.sym==SDLK_SPACE)  
  234.                 thread_pause=!thread_pause;  
  235.         }else if(event.type==SDL_QUIT){  
  236.             thread_exit=1;  
  237.         }else if(event.type==SFM_BREAK_EVENT){  
  238.             break;  
  239.         }  
  240.   
  241.     }  
  242.   
  243.     sws_freeContext(img_convert_ctx);  
  244.   
  245.     SDL_Quit();  
  246.     //--------------  
  247.     av_frame_free(&pFrameYUV);  
  248.     av_frame_free(&pFrame);  
  249.     avcodec_close(pCodecCtx);  
  250.     avformat_close_input(&pFormatCtx);  
  251.   
  252.     return 0;  
  253. }  



運行結果

程序運行后,會在命令行窗口打印一些視頻信息,同時會彈出一個窗口播放視頻內容。



下載

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 plain  copy
  在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


更新-2.3(2015.1.03)==============================

新增一個工程:最簡單的基於FFmpeg的解碼器-純凈版(不包含libavformat)

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


更新-2.4(2015.2.13)==============================

這次考慮到了跨平台的要求,調整了源代碼。經過這次調整之后,源代碼可以在以下平台編譯通過:

VC++:打開sln文件即可編譯,無需配置。

cl.exe:打開compile_cl.bat即可命令行下使用cl.exe進行編譯,注意可能需要按照VC的安裝路徑調整腳本里面的參數。編譯命令如下。

[plain]  view plain  copy
  在CODE上查看代碼片 派生到我的代碼片
  1. ::VS2010 Environment  
  2. call "D:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"  
  3. ::include  
  4. @set INCLUDE=include;%INCLUDE%  
  5. ::lib  
  6. @set LIB=lib;%LIB%  
  7. ::compile and link  
  8. cl simplest_ffmpeg_player.cpp /MD /link SDL2.lib SDL2main.lib avcodec.lib ^  
  9. avformat.lib avutil.lib avdevice.lib avfilter.lib postproc.lib swresample.lib swscale.lib ^  
  10. /SUBSYSTEM:WINDOWS /OPT:NOREF  

MinGW:MinGW命令行下運行compile_mingw.sh即可使用MinGW的g++進行編譯。編譯命令如下。

[plain]  view plain  copy
  在CODE上查看代碼片 派生到我的代碼片
  1. g++ simplest_ffmpeg_player.cpp -g -o simplest_ffmpeg_player.exe \  
  2. -I /usr/local/include -L /usr/local/lib \  
  3. -lmingw32 -lSDL2main -lSDL2 -lavformat -lavcodec -lavutil -lswscale  

GCC:Linux或者MacOS命令行下運行compile_gcc.sh即可使用GCC進行編譯。編譯命令如下。

[plain]  view plain  copy
  在CODE上查看代碼片 派生到我的代碼片
  1. gcc simplest_ffmpeg_player.cpp -g -o simplest_ffmpeg_player.out \  
  2. -I /usr/local/include -L /usr/local/lib -lSDL2main -lSDL2 -lavformat -lavcodec -lavutil -lswscale  
PS:相關的編譯命令已經保存到了工程文件夾中

SourceForge、Github等上面已經更新。

更新-2.5(2015.7.17)==============================
增加了下列工程:
simplest_ffmpeg_decoder:一個包含了封裝格式處理功能的解碼器。使用了libavcodec和libavformat。
simplest_video_play_sdl2:使用SDL2播放YUV的例子。
simplest_ffmpeg_helloworld:輸出FFmpeg類庫的信息。

SourceForge、Github等上面已經更新。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM