您的位置:首页 > 编程语言

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

2014-11-08 18:41 846 查看

转载地址: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):/article/1379090.html

流程图

没想到这篇文章中介绍的播放器挺受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





/**

* 最简单的基于FFmpeg的视频播放器

* Simplest FFmpeg Player

*

* 雷霄骅 Lei Xiaohua

* leixiaohua1020@126.com

* 中国传媒大学/数字电视技术

* Communication University of China / Digital TV Technology

* http://blog.csdn.net/leixiaohua1020
*

* 本程序实现了视频文件的解码和显示(支持HEVC,H.264,MPEG2等)。

* 是最简单的FFmpeg视频解码方面的教程。

* 通过学习本例子可以了解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"

//新版里的图像转换结构需要引入的头文件

#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.(解码错误)\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;

//测试自己填充数据----------------

SDL_DisplayYUVOverlay(bmp, &rect);

//延时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版之后,新添加了一个工程: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





/**

* 最简单的基于FFmpeg的视频播放器SU(SDL升级版)

* Simplest FFmpeg Player (SDL Update)

*

* 雷霄骅 Lei Xiaohua

* leixiaohua1020@126.com

* 中国传媒大学/数字电视技术

* Communication University of China / Digital TV Technology

* http://blog.csdn.net/leixiaohua1020
*

* 本程序实现了视频文件的解码和显示(支持HEVC,H.264,MPEG2等)。

* 是最简单的FFmpeg视频解码方面的教程。

* 通过学习本例子可以了解FFmpeg的解码流程。

* 本版本中使用SDL消息机制刷新视频画面。

* This software is a simplest video player based on FFmpeg.

* Suitable for beginner of FFmpeg.

*

* Version:1.1

*

* 备注:

* 标准版在播放视频的时候,画面显示使用延时40ms的方式。这么做有两个后果:

* (1)SDL弹出的窗口无法移动,一直显示是忙碌状态

* (2)画面显示并不是严格的40ms一帧,因为还没有考虑解码的时间。

* SU(SDL Update)版在视频解码的过程中,不再使用延时40ms的方式,而是创建了

* 一个线程,每隔40ms发送一个自定义的消息,告知主函数进行解码显示。这样做之后:

* (1)SDL弹出的窗口可以移动了

* (2)画面显示是严格的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"

//新版里的图像转换结构需要引入的头文件

#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.(解码错误)\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;

//测试自己填充数据----------------

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(标准版)中的循环做了更改。标准版中为播放视频的循环如下代码所示。

[cpp] view
plaincopy





main(){

//...

while(av_read_frame(pFormatCtx, packet)>=0)

{

//Decode...

SDL_Delay(40);

}

//...

}

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

[cpp] view
plaincopy





//自定义事件

//刷新画面

#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;

}

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

[cpp] view
plaincopy





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

}

}

//...

}

结果

软件运行截图:



完整工程下载地址:

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





#define SHOW_FULLSCREEN 0

#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

//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.为了更好地适应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





gcc simplest_ffmpeg_player.c -g -o smp.out -lSDLmain -lSDL -lavformat -lavcodec -lavutil -lswscale

使用方法:

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

[plain] view
plaincopy





./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。就可以正常显示了。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: