免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
100行代碼實(shí)現(xiàn)最簡(jiǎn)單的基于FFMPEG+SDL的視頻播放器(SDL1.x)

簡(jiǎn)介

FFMPEG工程浩大,可以參考的書籍又不是很多,因此很多剛學(xué)習(xí)FFMPEG的人常常感覺到無從下手。我剛接觸FFMPEG的時(shí)候也感覺不知從何學(xué)起。

因此我把自己做項(xiàng)目過程中實(shí)現(xiàn)的一個(gè)非常簡(jiǎn)單的視頻播放器(大約100行代碼)源代碼傳上來,以作備忘,同時(shí)方便新手學(xué)習(xí)FFMPEG。

該播放器雖然簡(jiǎn)單,但是幾乎包含了使用FFMPEG播放一個(gè)視頻所有必備的API,并且使用SDL顯示解碼出來的視頻。

并且支持流媒體等多種視頻輸入,處于簡(jiǎn)單考慮,沒有音頻部分,同時(shí)視頻播放采用直接延時(shí)40ms的方式

平臺(tái)使用VC2010,使用了新版的FFMPEG類庫(kù)

該工程已經(jīng)傳到SourceForge上(該工程會(huì)時(shí)刻更新):

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

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

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


流程圖

沒想到這篇文章中介紹的播放器挺受FFMPEG初學(xué)者的歡迎,因此再次更新兩張流程圖,方便大家學(xué)習(xí)。此外在源代碼上添加了注釋,方便理解。

該播放器解碼的流程用圖的方式可以表示稱如下形式:


SDL顯示YUV圖像的流程圖:


簡(jiǎn)單解釋幾句:

SDL_Surface就是使用SDL的時(shí)候彈出的那個(gè)窗口。在SDL1.x版本中,只可以創(chuàng)建一個(gè)SDL_Surface。

SDL_Overlay用于顯示YUV數(shù)據(jù)。一個(gè)SDL_Overlay對(duì)應(yīng)一幀YUV數(shù)據(jù)。

SDL_Rect用于確定SDL_Overlay顯示的位置。注意:一個(gè)SDL_Overlay可以指定多個(gè)不同的SDL_Rect,這樣就可以在SDL_Surface不同位置顯示相同的內(nèi)容。

它們的關(guān)系如下圖所示:



下圖舉了個(gè)例子,指定了4個(gè)SDL_Rect,可以實(shí)現(xiàn)4分屏的顯示。



simplest_ffmpeg_player(標(biāo)準(zhǔn)版)代碼

  1. /** 
  2.  * 最簡(jiǎn)單的基于FFmpeg的視頻播放器 
  3.  * Simplest FFmpeg Player 
  4.  * 
  5.  * 雷霄驊 Lei Xiaohua 
  6.  * leixiaohua1020@126.com 
  7.  * 中國(guó)傳媒大學(xué)/數(shù)字電視技術(shù) 
  8.  * Communication University of China / Digital TV Technology 
  9.  * http://blog.csdn.net/leixiaohua1020 
  10.  * 
  11.  * 本程序?qū)崿F(xiàn)了視頻文件的解碼和顯示(支持HEVC,H.264,MPEG2等)。 
  12.  * 是最簡(jiǎn)單的FFmpeg視頻解碼方面的教程。 
  13.  * 通過學(xué)習(xí)本例子可以了解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.     //新版里的圖像轉(zhuǎn)換結(jié)構(gòu)需要引入的頭文件  
  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.(解碼錯(cuò)誤)\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.                 //測(cè)試自己填充數(shù)據(jù)----------------  
  165.                 SDL_DisplayYUVOverlay(bmp, &rect);   
  166.                 //延時(shí)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版之后,新添加了一個(gè)工程:simplest_ffmpeg_player_su(SU版)。


標(biāo)準(zhǔn)版在播放視頻的時(shí)候,畫面顯示使用延時(shí)40ms的方式。這么做有兩個(gè)后果:
(1)SDL彈出的窗口無法移動(dòng),一直顯示是忙碌狀態(tài)
(2)畫面顯示并不是嚴(yán)格的40ms一幀,因?yàn)檫€沒有考慮解碼的時(shí)間。SU(SDL Update)版在視頻解碼的過程中,不再使用延時(shí)40ms的方式,而是創(chuàng)建了一個(gè)線程,每隔40ms發(fā)送一個(gè)自定義的消息,告知主函數(shù)進(jìn)行解碼顯示。這樣做之后:
(1)SDL彈出的窗口可以移動(dòng)了
(2)畫面顯示是嚴(yán)格的40ms一幀

simplest_ffmpeg_player_su(SU版)代碼

  1. /** 
  2.  * 最簡(jiǎn)單的基于FFmpeg的視頻播放器SU(SDL升級(jí)版) 
  3.  * Simplest FFmpeg Player (SDL Update) 
  4.  * 
  5.  * 雷霄驊 Lei Xiaohua 
  6.  * leixiaohua1020@126.com 
  7.  * 中國(guó)傳媒大學(xué)/數(shù)字電視技術(shù) 
  8.  * Communication University of China / Digital TV Technology 
  9.  * http://blog.csdn.net/leixiaohua1020 
  10.  * 
  11.  * 本程序?qū)崿F(xiàn)了視頻文件的解碼和顯示(支持HEVC,H.264,MPEG2等)。 
  12.  * 是最簡(jiǎn)單的FFmpeg視頻解碼方面的教程。 
  13.  * 通過學(xué)習(xí)本例子可以了解FFmpeg的解碼流程。 
  14.  * 本版本中使用SDL消息機(jī)制刷新視頻畫面。 
  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.  * 標(biāo)準(zhǔn)版在播放視頻的時(shí)候,畫面顯示使用延時(shí)40ms的方式。這么做有兩個(gè)后果: 
  22.  * (1)SDL彈出的窗口無法移動(dòng),一直顯示是忙碌狀態(tài) 
  23.  * (2)畫面顯示并不是嚴(yán)格的40ms一幀,因?yàn)檫€沒有考慮解碼的時(shí)間。 
  24.  * SU(SDL Update)版在視頻解碼的過程中,不再使用延時(shí)40ms的方式,而是創(chuàng)建了 
  25.  * 一個(gè)線程,每隔40ms發(fā)送一個(gè)自定義的消息,告知主函數(shù)進(jìn)行解碼顯示。這樣做之后: 
  26.  * (1)SDL彈出的窗口可以移動(dòng)了 
  27.  * (2)畫面顯示是嚴(yán)格的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.     //新版里的圖像轉(zhuǎn)換結(jié)構(gòu)需要引入的頭文件  
  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.(解碼錯(cuò)誤)\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.                         //測(cè)試自己填充數(shù)據(jù)----------------  
  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(標(biāo)準(zhǔn)版)中的循環(huán)做了更改。標(biāo)準(zhǔn)版中為播放視頻的循環(huán)如下代碼所示。

  1. main(){  
  2.     //...  
  3.     while(av_read_frame(pFormatCtx, packet)>=0)  
  4.         {  
  5.             //Decode...  
  6.             SDL_Delay(40);  
  7.         }  
  8.     //...  
  9. }  

可以看出標(biāo)準(zhǔn)版中使用SDL_Delay(40)控制視頻的播放速度。這樣有一些問題在前文中已經(jīng)敘述。SU版定義了一個(gè)函數(shù)專門用于發(fā)送“解碼和顯示”的Event。

  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. }  

主函數(shù)形式如下。使用SDL_WaitEvent()等待Event進(jìn)行解碼和顯示。

  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. }  



結(jié)果

軟件運(yùn)行截圖:

完整工程下載地址:

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


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


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

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

注1:類庫(kù)版本2014.5.6,已經(jīng)支持HEVC以及VP9的解碼,附帶了這兩種視頻編碼的碼流文件。此外修改了個(gè)別變更的API函數(shù),并且提高了一些程序的效率。

注2:新版FFmpeg類庫(kù)Release下出現(xiàn)錯(cuò)誤的解決方法如下:
(注:此方法適用于所有近期發(fā)布的FFmpeg類庫(kù))
VC工程屬性里,linker->Optimization->References 選項(xiàng),改成No(/OPT:NOREF)即可。


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


版本升級(jí)至1.1,變?yōu)?個(gè)項(xiàng)目:

simplest_ffmpeg_player:標(biāo)準(zhǔn)版,F(xiàn)Fmpeg學(xué)習(xí)的開始。
simplest_ffmpeg_player_su:SU(SDL Update)版,加入了簡(jiǎn)單的SDL的Event。

simplest_ffmpeg_player(標(biāo)準(zhǔn)版)增加了以下兩個(gè)選項(xiàng)(當(dāng)然,代碼量超過了100行)

1.輸出解碼后的YUV420P像素?cái)?shù)據(jù)文件

2.全屏播放

以上兩項(xiàng)可以通過文件前面的宏進(jìn)行控制:

  1. #define SHOW_FULLSCREEN 0  
  2. #define OUTPUT_YUV420P 0  

另外修補(bǔ)了幾個(gè)的函數(shù),例如增加了SDL_Quit()等。

simplest_ffmpeg_player_su(SU版)具體情況在上文中已經(jīng)說明。

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

SourceForge上已經(jīng)更新。


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

版本升級(jí)至1.2。

1.新版本在原版本的基礎(chǔ)上增加了“flush_decoder”功能。當(dāng)av_read_frame()循環(huán)退出的時(shí)候,實(shí)際上解碼器中可能還包含剩余的幾幀數(shù)據(jù)。因此需要通過“flush_decoder”將這幾幀數(shù)據(jù)輸出?!癴lush_decoder”功能簡(jiǎn)而言之即直接調(diào)用avcodec_decode_video2()獲得AVFrame,而不再向解碼器傳遞AVPacket。參考代碼如下:

  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.為了更好地適應(yīng)Linux等其他操作系統(tǒng),做到可以跨平臺(tái),去除掉了VC特有的一些函數(shù)。比如“#include "stdafx.h"”,“_tmain()”等等。

具體信息參見文章:avcodec_decode_video2()解碼視頻后丟幀的問題解決

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

SourceForge上已經(jīng)更新。

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


Linux下代碼下載地址:

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

這個(gè)是Linux下的代碼,在Ubuntu下測(cè)試可以運(yùn)行,前提是安裝了FFmpeg和SDL(版本1.2)。
編譯命令:

  1. gcc simplest_ffmpeg_player.c -g -o smp.out -lSDLmain -lSDL -lavformat -lavcodec -lavutil -lswscale  
使用方法:

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

  1. ./smp.out test.flv  

FFMPEG相關(guān)學(xué)習(xí)資料

SDL GUIDE 中文譯本

ffdoc (FFMPEG的最完整教程)
如何用FFmpeg編寫一個(gè)簡(jiǎn)單播放器


補(bǔ)充問題

補(bǔ)充1:舊版程序有一個(gè)小BUG,就是sws_getContext()之后,需要調(diào)用sws_freeContext()。否則長(zhǎng)時(shí)間運(yùn)行的話,會(huì)出現(xiàn)內(nèi)存泄露的狀況。更新版已經(jīng)修復(fù)。

補(bǔ)充2:有人會(huì)疑惑,為什么解碼后的pFrame不直接用于顯示,而是調(diào)用swscale()轉(zhuǎn)換之后進(jìn)行顯示?

如果不進(jìn)行轉(zhuǎn)換,而是直接調(diào)用SDL進(jìn)行顯示的話,會(huì)發(fā)現(xiàn)顯示出來的圖像是混亂的。關(guān)鍵問題在于解碼后的pFrame的linesize里存儲(chǔ)的不是圖像的寬度,而是比寬度大一些的一個(gè)值。其原因目前還沒有仔細(xì)調(diào)查(大概是出于性能的考慮)。例如分辨率為480x272的圖像,解碼后的視頻的linesize[0]為512,而不是480。以第1行亮度像素(pFrame->data[0])為例,從0-480存儲(chǔ)的是亮度數(shù)據(jù),而從480-512則存儲(chǔ)的是無效的數(shù)據(jù)。因此需要使用swscale()進(jìn)行轉(zhuǎn)換。轉(zhuǎn)換后去除了無效數(shù)據(jù),linesize[0]變?yōu)?80。就可以正常顯示了。









本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
最簡(jiǎn)單的基于FFmpeg的內(nèi)存讀寫的例子
FFmpeg學(xué)習(xí)教程
學(xué)習(xí)FFmpeg API – 解碼視頻
ffmpeg的快速音視頻開發(fā)方法[圖]
FFMpeg的解碼流程
關(guān)于ffmpeg如何提取視頻的關(guān)鍵幀的問題
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服