簡(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)版)代碼
- /**
- * 最簡(jiǎn)單的基于FFmpeg的視頻播放器
- * Simplest FFmpeg Player
- *
- * 雷霄驊 Lei Xiaohua
- * leixiaohua1020@126.com
- * 中國(guó)傳媒大學(xué)/數(shù)字電視技術(shù)
- * Communication University of China / Digital TV Technology
- * http://blog.csdn.net/leixiaohua1020
- *
- * 本程序?qū)崿F(xiàn)了視頻文件的解碼和顯示(支持HEVC,H.264,MPEG2等)。
- * 是最簡(jiǎn)單的FFmpeg視頻解碼方面的教程。
- * 通過學(xué)習(xí)本例子可以了解FFmpeg的解碼流程。
- * This software is a simplest video player based on FFmpeg.
- * Suitable for beginner of FFmpeg.
- *
- * Version:1.0
- */
-
-
- #include "stdafx.h"
-
- extern "C"
- {
- #include "libavcodec/avcodec.h"
- #include "libavformat/avformat.h"
- //新版里的圖像轉(zhuǎn)換結(jié)構(gòu)需要引入的頭文件
- #include "libswscale/swscale.h"
- //SDL
- #include "sdl/SDL.h"
- #include "sdl/SDL_thread.h"
- };
-
- //Full Screen
- #define SHOW_FULLSCREEN 0
- //Output YUV420P
- #define OUTPUT_YUV420P 0
-
- int _tmain(int argc, _TCHAR* argv[])
- {
-
- AVFormatContext *pFormatCtx;
- int i, videoindex;
- AVCodecContext *pCodecCtx;
- AVCodec *pCodec;
- char filepath[]="src01_480x272_22.hm10";
- av_register_all();
- avformat_network_init();
- pFormatCtx = avformat_alloc_context();
-
- if(avformat_open_input(&pFormatCtx,filepath,NULL,NULL)!=0){
- printf("Couldn't open input stream.(無法打開輸入流)\n");
- return -1;
- }
- if(av_find_stream_info(pFormatCtx)<0)
- {
- printf("Couldn't find stream information.(無法獲取流信息)\n");
- return -1;
- }
- videoindex=-1;
- for(i=0; i<pFormatCtx->nb_streams; i++)
- if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)
- {
- videoindex=i;
- break;
- }
- if(videoindex==-1)
- {
- printf("Didn't find a video stream.(沒有找到視頻流)\n");
- return -1;
- }
- pCodecCtx=pFormatCtx->streams[videoindex]->codec;
- pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
- if(pCodec==NULL)
- {
- printf("Codec not found.(沒有找到解碼器)\n");
- return -1;
- }
- if(avcodec_open2(pCodecCtx, pCodec,NULL)<0)
- {
- printf("Could not open codec.(無法打開解碼器)\n");
- return -1;
- }
- AVFrame *pFrame,*pFrameYUV;
- pFrame=avcodec_alloc_frame();
- pFrameYUV=avcodec_alloc_frame();
- uint8_t *out_buffer=(uint8_t *)av_malloc(avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height));
- avpicture_fill((AVPicture *)pFrameYUV, out_buffer, PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
- //------------SDL----------------
- if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
- printf( "Could not initialize SDL - %s\n", SDL_GetError());
- return -1;
- }
-
- int screen_w=0,screen_h=0;
- SDL_Surface *screen;
- #if SHOW_FULLSCREEN
- const SDL_VideoInfo *vi = SDL_GetVideoInfo();
- screen_w = vi->current_w;
- screen_h = vi->current_h;
- screen = SDL_SetVideoMode(screen_w, screen_h, 0,SDL_FULLSCREEN);
- #else
- screen_w = pCodecCtx->width;
- screen_h = pCodecCtx->height;
- screen = SDL_SetVideoMode(screen_w, screen_h, 0,0);
- #endif
-
- if(!screen) {
- printf("SDL: could not set video mode - exiting:%s\n",SDL_GetError());
- return -1;
- }
- SDL_Overlay *bmp;
- bmp = SDL_CreateYUVOverlay(pCodecCtx->width, pCodecCtx->height,SDL_YV12_OVERLAY, screen);
- SDL_Rect rect;
-
- int ret, got_picture;
-
- AVPacket *packet=(AVPacket *)av_malloc(sizeof(AVPacket));
- //輸出一下信息-----------------------------
- printf("File Information(文件信息)---------------------\n");
- av_dump_format(pFormatCtx,0,filepath,0);
- printf("-------------------------------------------------\n");
-
- #if OUTPUT_YUV420P
- FILE *fp_yuv=fopen("output.yuv","wb+");
- #endif
-
-
- struct SwsContext *img_convert_ctx;
- img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
- //------------------------------
- while(av_read_frame(pFormatCtx, packet)>=0)
- {
- if(packet->stream_index==videoindex)
- {
- ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
- if(ret < 0)
- {
- printf("Decode Error.(解碼錯(cuò)誤)\n");
- return -1;
- }
- if(got_picture)
- {
- sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);
-
- #if OUTPUT_YUV420P
- int y_size=pCodecCtx->width*pCodecCtx->height;
- fwrite(pFrameYUV->data[0],1,y_size,fp_yuv); //Y
- fwrite(pFrameYUV->data[1],1,y_size/4,fp_yuv); //U
- fwrite(pFrameYUV->data[2],1,y_size/4,fp_yuv); //V
- #endif
- SDL_LockYUVOverlay(bmp);
- bmp->pixels[0]=pFrameYUV->data[0];
- bmp->pixels[2]=pFrameYUV->data[1];
- bmp->pixels[1]=pFrameYUV->data[2];
- bmp->pitches[0]=pFrameYUV->linesize[0];
- bmp->pitches[2]=pFrameYUV->linesize[1];
- bmp->pitches[1]=pFrameYUV->linesize[2];
- SDL_UnlockYUVOverlay(bmp);
- rect.x = 0;
- rect.y = 0;
- rect.w = screen_w;
- rect.h = screen_h;
- //測(cè)試自己填充數(shù)據(jù)----------------
- SDL_DisplayYUVOverlay(bmp, &rect);
- //延時(shí)40ms
- SDL_Delay(40);
- }
- }
- av_free_packet(packet);
- }
- sws_freeContext(img_convert_ctx);
-
- #if OUTPUT_YUV420P
- fclose(fp_yuv);
- #endif
-
- SDL_Quit();
-
- av_free(out_buffer);
- av_free(pFrameYUV);
- avcodec_close(pCodecCtx);
- avformat_close_input(&pFormatCtx);
-
- return 0;
- }
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版)代碼
- /**
- * 最簡(jiǎn)單的基于FFmpeg的視頻播放器SU(SDL升級(jí)版)
- * Simplest FFmpeg Player (SDL Update)
- *
- * 雷霄驊 Lei Xiaohua
- * leixiaohua1020@126.com
- * 中國(guó)傳媒大學(xué)/數(shù)字電視技術(shù)
- * Communication University of China / Digital TV Technology
- * http://blog.csdn.net/leixiaohua1020
- *
- * 本程序?qū)崿F(xiàn)了視頻文件的解碼和顯示(支持HEVC,H.264,MPEG2等)。
- * 是最簡(jiǎn)單的FFmpeg視頻解碼方面的教程。
- * 通過學(xué)習(xí)本例子可以了解FFmpeg的解碼流程。
- * 本版本中使用SDL消息機(jī)制刷新視頻畫面。
- * This software is a simplest video player based on FFmpeg.
- * Suitable for beginner of FFmpeg.
- *
- * Version:1.1
- *
- * 備注:
- * 標(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一幀
- * Remark:
- * Standard Version use's SDL_Delay() to control video's frame rate, it has 2
- * disadvantages:
- * (1)SDL's Screen can't be moved and always "Busy".
- * (2)Frame rate can't be accurate because it doesn't consider the time consumed
- * by avcodec_decode_video2()
- * SU(SDL Update)Version solved 2 problems above. It create a thread to send SDL
- * Event every 40ms to tell the main loop to decode and show video frames.
- */
-
-
- #include "stdafx.h"
-
- extern "C"
- {
- #include "libavcodec/avcodec.h"
- #include "libavformat/avformat.h"
- //新版里的圖像轉(zhuǎn)換結(jié)構(gòu)需要引入的頭文件
- #include "libswscale/swscale.h"
- //SDL
- #include "sdl/SDL.h"
- #include "sdl/SDL_thread.h"
-
- };
-
- //自定義事件
- //刷新畫面
- #define SFM_REFRESH_EVENT (SDL_USEREVENT + 1)
-
- int thread_exit=0;
- //Thread
- int sfp_refresh_thread(void *opaque)
- {
- while (thread_exit==0) {
- SDL_Event event;
- event.type = SFM_REFRESH_EVENT;
- SDL_PushEvent(&event);
- //Wait 40 ms
- SDL_Delay(40);
- }
- return 0;
- }
-
-
- int _tmain(int argc, _TCHAR* argv[])
- {
- AVFormatContext *pFormatCtx;
- int i, videoindex;
- AVCodecContext *pCodecCtx;
- AVCodec *pCodec;
- char filepath[]="src01_480x272_22.hm10";
- av_register_all();
- avformat_network_init();
- pFormatCtx = avformat_alloc_context();
-
- if(avformat_open_input(&pFormatCtx,filepath,NULL,NULL)!=0){
- printf("Couldn't open input stream.(無法打開輸入流)\n");
- return -1;
- }
- if(av_find_stream_info(pFormatCtx)<0)
- {
- printf("Couldn't find stream information.(無法獲取流信息)\n");
- return -1;
- }
- videoindex=-1;
- for(i=0; i<pFormatCtx->nb_streams; i++)
- if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)
- {
- videoindex=i;
- break;
- }
- if(videoindex==-1)
- {
- printf("Didn't find a video stream.(沒有找到視頻流)\n");
- return -1;
- }
- pCodecCtx=pFormatCtx->streams[videoindex]->codec;
- pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
- if(pCodec==NULL)
- {
- printf("Codec not found.(沒有找到解碼器)\n");
- return -1;
- }
- if(avcodec_open2(pCodecCtx, pCodec,NULL)<0)
- {
- printf("Could not open codec.(無法打開解碼器)\n");
- return -1;
- }
- AVFrame *pFrame,*pFrameYUV;
- pFrame=avcodec_alloc_frame();
- pFrameYUV=avcodec_alloc_frame();
- uint8_t *out_buffer=(uint8_t *)av_malloc(avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height));
- avpicture_fill((AVPicture *)pFrameYUV, out_buffer, PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
- //------------SDL----------------
- if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
- printf( "Could not initialize SDL - %s\n", SDL_GetError());
- return -1;
- }
-
- int screen_w=0,screen_h=0;
- SDL_Surface *screen;
- screen_w = pCodecCtx->width;
- screen_h = pCodecCtx->height;
- screen = SDL_SetVideoMode(screen_w, screen_h, 0,0);
-
- if(!screen) {
- printf("SDL: could not set video mode - exiting:%s\n",SDL_GetError());
- return -1;
- }
- SDL_Overlay *bmp;
- bmp = SDL_CreateYUVOverlay(pCodecCtx->width, pCodecCtx->height,SDL_YV12_OVERLAY, screen);
- SDL_Rect rect;
-
- int ret, got_picture;
-
- AVPacket *packet=(AVPacket *)av_malloc(sizeof(AVPacket));
- //輸出一下信息-----------------------------
- printf("File Information(文件信息)---------------------\n");
- av_dump_format(pFormatCtx,0,filepath,0);
- printf("-------------------------------------------------\n");
-
- struct SwsContext *img_convert_ctx;
- img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
- //--------------
- SDL_Thread *video_tid = SDL_CreateThread(sfp_refresh_thread,NULL);
- //
- SDL_WM_SetCaption("Simple FFmpeg Player (SDL Update)",NULL);
-
- //Event Loop
- SDL_Event event;
- for (;;) {
- //Wait
- SDL_WaitEvent(&event);
- if(event.type==SFM_REFRESH_EVENT){
- //------------------------------
- if(av_read_frame(pFormatCtx, packet)>=0){
- if(packet->stream_index==videoindex){
- ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
- if(ret < 0){
- printf("Decode Error.(解碼錯(cuò)誤)\n");
- return -1;
- }
- if(got_picture){
- sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);
-
- SDL_LockYUVOverlay(bmp);
- bmp->pixels[0]=pFrameYUV->data[0];
- bmp->pixels[2]=pFrameYUV->data[1];
- bmp->pixels[1]=pFrameYUV->data[2];
- bmp->pitches[0]=pFrameYUV->linesize[0];
- bmp->pitches[2]=pFrameYUV->linesize[1];
- bmp->pitches[1]=pFrameYUV->linesize[2];
- SDL_UnlockYUVOverlay(bmp);
- rect.x = 0;
- rect.y = 0;
- rect.w = screen_w;
- rect.h = screen_h;
- //測(cè)試自己填充數(shù)據(jù)----------------
- SDL_DisplayYUVOverlay(bmp, &rect);
-
- }
- }
- av_free_packet(packet);
- }else{
- //Exit Thread
- thread_exit=1;
- break;
- }
- }
-
- }
-
- SDL_Quit();
-
- sws_freeContext(img_convert_ctx);
-
- //--------------
- av_free(out_buffer);
- av_free(pFrameYUV);
- avcodec_close(pCodecCtx);
- avformat_close_input(&pFormatCtx);
-
- return 0;
- }
simplest_ffmpeg_player_su(SU版)中將simplest_ffmpeg_player(標(biāo)準(zhǔn)版)中的循環(huán)做了更改。標(biāo)準(zhǔn)版中為播放視頻的循環(huán)如下代碼所示。
- main(){
- //...
- while(av_read_frame(pFormatCtx, packet)>=0)
- {
- //Decode...
- SDL_Delay(40);
- }
- //...
- }
可以看出標(biāo)準(zhǔn)版中使用SDL_Delay(40)控制視頻的播放速度。這樣有一些問題在前文中已經(jīng)敘述。SU版定義了一個(gè)函數(shù)專門用于發(fā)送“解碼和顯示”的Event。
- //自定義事件
- //刷新畫面
- #define SFM_REFRESH_EVENT (SDL_USEREVENT + 1)
-
- int thread_exit=0;
- //Thread
- int sfp_refresh_thread(void *opaque)
- {
- while (thread_exit==0) {
- SDL_Event event;
- event.type = SFM_REFRESH_EVENT;
- SDL_PushEvent(&event);
- //Wait 40 ms
- SDL_Delay(40);
- }
- return 0;
- }
主函數(shù)形式如下。使用SDL_WaitEvent()等待Event進(jìn)行解碼和顯示。
- main(){
- //...
- SDL_Thread *video_tid = SDL_CreateThread(sfp_refresh_thread,NULL);
- //Event Loop
- SDL_Event event;
- for (;;) {
- //Wait
- SDL_WaitEvent(&event);
- if(event.type==SFM_REFRESH_EVENT){
- //Decode...
- }
-
- }
- //...
- }
結(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)行控制:
- #define SHOW_FULLSCREEN 0
- #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。參考代碼如下:
- //FIX: Flush Frames remained in Codec
- while (1) {
- ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
- if (ret < 0)
- break;
- if (!got_picture)
- break;
- sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);
- //處理...
- }
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)。
編譯命令:
- gcc simplest_ffmpeg_player.c -g -o smp.out -lSDLmain -lSDL -lavformat -lavcodec -lavutil -lswscale
使用方法:
下列命令即可播放同一目錄下的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。就可以正常顯示了。