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

转自:http://blog.csdn.net/leixiaohua1020/article/details/8652605

版权声明:本文为博主原创文章,未经博主允许不得转载。

目录(?)[-]

  1. 简介
  2. 流程图
  3. simplest_ffmpeg_player标准版代码
  4. simplest_ffmpeg_player_suSU版代码
  5. 结果
  6. FFMPEG相关学习资料
  7. 补充问题

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

最简单的基于FFmpeg的视频播放器系列文章列表:

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

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

最简单的基于FFmpeg的解码器-纯净版(不包含libavformat)

最简单的基于FFMPEG+SDL的视频播放器:拆分-解码器和播放器

最简单的基于FFMPEG的Helloworld程序

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

简介

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):http://blog.csdn.net/leixiaohua1020/article/details/38868499

流程图

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

  1. /**
  2. * 最简单的基于FFmpeg的视频播放器
  3. * Simplest FFmpeg Player
  4. *
  5. * 雷霄骅 Lei Xiaohua
  6. * [email protected]
  7. * 中国传媒大学/数字电视技术
  8. * Communication University of China / Digital TV Technology
  9. * http://blog.csdn.net/leixiaohua1020
  10. *
  11. * 本程序实现了视频文件的解码和显示(支持HEVC,H.264,MPEG2等)。
  12. * 是最简单的FFmpeg视频解码方面的教程。
  13. * 通过学习本例子可以了解FFmpeg的解码流程。
  14. * This software is a simplest video player based on FFmpeg.
  15. * Suitable for beginner of FFmpeg.
  16. */
  17. #include <stdio.h>
  18. #define __STDC_CONSTANT_MACROS
  19. #ifdef _WIN32
  20. //Windows
  21. extern "C"
  22. {
  23. #include "libavcodec/avcodec.h"
  24. #include "libavformat/avformat.h"
  25. #include "libswscale/swscale.h"
  26. #include "SDL/SDL.h"
  27. };
  28. #else
  29. //Linux...
  30. #ifdef __cplusplus
  31. extern "C"
  32. {
  33. #endif
  34. #include <libavcodec/avcodec.h>
  35. #include <libavformat/avformat.h>
  36. #include <libswscale/swscale.h>
  37. #include <SDL/SDL.h>
  38. #ifdef __cplusplus
  39. };
  40. #endif
  41. #endif
  42. //Full Screen
  43. #define SHOW_FULLSCREEN 0
  44. //Output YUV420P
  45. #define OUTPUT_YUV420P 0
  46. int main(int argc, char* argv[])
  47. {
  48. //FFmpeg
  49. AVFormatContext *pFormatCtx;
  50. int             i, videoindex;
  51. AVCodecContext  *pCodecCtx;
  52. AVCodec         *pCodec;
  53. AVFrame *pFrame,*pFrameYUV;
  54. AVPacket *packet;
  55. struct SwsContext *img_convert_ctx;
  56. //SDL
  57. int screen_w,screen_h;
  58. SDL_Surface *screen;
  59. SDL_VideoInfo *vi;
  60. SDL_Overlay *bmp;
  61. SDL_Rect rect;
  62. FILE *fp_yuv;
  63. int ret, got_picture;
  64. char filepath[]="bigbuckbunny_480x272.h265";
  65. av_register_all();
  66. avformat_network_init();
  67. pFormatCtx = avformat_alloc_context();
  68. if(avformat_open_input(&pFormatCtx,filepath,NULL,NULL)!=0){
  69. printf("Couldn‘t open input stream.\n");
  70. return -1;
  71. }
  72. if(avformat_find_stream_info(pFormatCtx,NULL)<0){
  73. printf("Couldn‘t find stream information.\n");
  74. return -1;
  75. }
  76. videoindex=-1;
  77. for(i=0; i<pFormatCtx->nb_streams; i++)
  78. if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO){
  79. videoindex=i;
  80. break;
  81. }
  82. if(videoindex==-1){
  83. printf("Didn‘t find a video stream.\n");
  84. return -1;
  85. }
  86. pCodecCtx=pFormatCtx->streams[videoindex]->codec;
  87. pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
  88. if(pCodec==NULL){
  89. printf("Codec not found.\n");
  90. return -1;
  91. }
  92. if(avcodec_open2(pCodecCtx, pCodec,NULL)<0){
  93. printf("Could not open codec.\n");
  94. return -1;
  95. }
  96. pFrame=av_frame_alloc();
  97. pFrameYUV=av_frame_alloc();
  98. //uint8_t *out_buffer=(uint8_t *)av_malloc(avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height));
  99. //avpicture_fill((AVPicture *)pFrameYUV, out_buffer, PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
  100. //SDL----------------------------
  101. if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
  102. printf( "Could not initialize SDL - %s\n", SDL_GetError());
  103. return -1;
  104. }
  105. #if SHOW_FULLSCREEN
  106. vi = SDL_GetVideoInfo();
  107. screen_w = vi->current_w;
  108. screen_h = vi->current_h;
  109. screen = SDL_SetVideoMode(screen_w, screen_h, 0,SDL_FULLSCREEN);
  110. #else
  111. screen_w = pCodecCtx->width;
  112. screen_h = pCodecCtx->height;
  113. screen = SDL_SetVideoMode(screen_w, screen_h, 0,0);
  114. #endif
  115. if(!screen) {
  116. printf("SDL: could not set video mode - exiting:%s\n",SDL_GetError());
  117. return -1;
  118. }
  119. bmp = SDL_CreateYUVOverlay(pCodecCtx->width, pCodecCtx->height,SDL_YV12_OVERLAY, screen);
  120. rect.x = 0;
  121. rect.y = 0;
  122. rect.w = screen_w;
  123. rect.h = screen_h;
  124. //SDL End------------------------
  125. packet=(AVPacket *)av_malloc(sizeof(AVPacket));
  126. //Output Information-----------------------------
  127. printf("------------- File Information ------------------\n");
  128. av_dump_format(pFormatCtx,0,filepath,0);
  129. printf("-------------------------------------------------\n");
  130. #if OUTPUT_YUV420P
  131. fp_yuv=fopen("output.yuv","wb+");
  132. #endif
  133. SDL_WM_SetCaption("Simplest FFmpeg Player",NULL);
  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. if(packet->stream_index==videoindex){
  138. //Decode
  139. ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
  140. if(ret < 0){
  141. printf("Decode Error.\n");
  142. return -1;
  143. }
  144. if(got_picture){
  145. SDL_LockYUVOverlay(bmp);
  146. pFrameYUV->data[0]=bmp->pixels[0];
  147. pFrameYUV->data[1]=bmp->pixels[2];
  148. pFrameYUV->data[2]=bmp->pixels[1];
  149. pFrameYUV->linesize[0]=bmp->pitches[0];
  150. pFrameYUV->linesize[1]=bmp->pitches[2];
  151. pFrameYUV->linesize[2]=bmp->pitches[1];
  152. sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0,
  153. pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);
  154. #if OUTPUT_YUV420P
  155. int y_size=pCodecCtx->width*pCodecCtx->height;
  156. fwrite(pFrameYUV->data[0],1,y_size,fp_yuv);    //Y
  157. fwrite(pFrameYUV->data[1],1,y_size/4,fp_yuv);  //U
  158. fwrite(pFrameYUV->data[2],1,y_size/4,fp_yuv);  //V
  159. #endif
  160. SDL_UnlockYUVOverlay(bmp);
  161. SDL_DisplayYUVOverlay(bmp, &rect);
  162. //Delay 40ms
  163. SDL_Delay(40);
  164. }
  165. }
  166. av_free_packet(packet);
  167. }
  168. //FIX: Flush Frames remained in Codec
  169. while (1) {
  170. ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
  171. if (ret < 0)
  172. break;
  173. if (!got_picture)
  174. break;
  175. sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);
  176. SDL_LockYUVOverlay(bmp);
  177. pFrameYUV->data[0]=bmp->pixels[0];
  178. pFrameYUV->data[1]=bmp->pixels[2];
  179. pFrameYUV->data[2]=bmp->pixels[1];
  180. pFrameYUV->linesize[0]=bmp->pitches[0];
  181. pFrameYUV->linesize[1]=bmp->pitches[2];
  182. pFrameYUV->linesize[2]=bmp->pitches[1];
  183. #if OUTPUT_YUV420P
  184. int y_size=pCodecCtx->width*pCodecCtx->height;
  185. fwrite(pFrameYUV->data[0],1,y_size,fp_yuv);    //Y
  186. fwrite(pFrameYUV->data[1],1,y_size/4,fp_yuv);  //U
  187. fwrite(pFrameYUV->data[2],1,y_size/4,fp_yuv);  //V
  188. #endif
  189. SDL_UnlockYUVOverlay(bmp);
  190. SDL_DisplayYUVOverlay(bmp, &rect);
  191. //Delay 40ms
  192. SDL_Delay(40);
  193. }
  194. sws_freeContext(img_convert_ctx);
  195. #if OUTPUT_YUV420P
  196. fclose(fp_yuv);
  197. #endif
  198. SDL_Quit();
  199. //av_free(out_buffer);
  200. av_free(pFrameYUV);
  201. avcodec_close(pCodecCtx);
  202. avformat_close_input(&pFormatCtx);
  203. return 0;
  204. }

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

  1. /**
  2. * 最简单的基于FFmpeg的视频播放器SU(SDL升级版)
  3. * Simplest FFmpeg Player (SDL Update)
  4. *
  5. * 雷霄骅 Lei Xiaohua
  6. * [email protected]
  7. * 中国传媒大学/数字电视技术
  8. * Communication University of China / Digital TV Technology
  9. * http://blog.csdn.net/leixiaohua1020
  10. *
  11. * 本程序实现了视频文件的解码和显示(支持HEVC,H.264,MPEG2等)。
  12. * 是最简单的FFmpeg视频解码方面的教程。
  13. * 通过学习本例子可以了解FFmpeg的解码流程。
  14. * 本版本中使用SDL消息机制刷新视频画面。
  15. * This software is a simplest video player based on FFmpeg.
  16. * Suitable for beginner of FFmpeg.
  17. *
  18. * Version:1.2
  19. *
  20. * 备注:
  21. * 标准版在播放视频的时候,画面显示使用延时40ms的方式。这么做有两个后果:
  22. * (1)SDL弹出的窗口无法移动,一直显示是忙碌状态
  23. * (2)画面显示并不是严格的40ms一帧,因为还没有考虑解码的时间。
  24. * SU(SDL Update)版在视频解码的过程中,不再使用延时40ms的方式,而是创建了
  25. * 一个线程,每隔40ms发送一个自定义的消息,告知主函数进行解码显示。这样做之后:
  26. * (1)SDL弹出的窗口可以移动了
  27. * (2)画面显示是严格的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. #include <stdio.h>
  38. #define __STDC_CONSTANT_MACROS
  39. #ifdef _WIN32
  40. //Windows
  41. extern "C"
  42. {
  43. #include "libavcodec/avcodec.h"
  44. #include "libavformat/avformat.h"
  45. #include "libswscale/swscale.h"
  46. #include "SDL/SDL.h"
  47. };
  48. #else
  49. //Linux...
  50. #ifdef __cplusplus
  51. extern "C"
  52. {
  53. #endif
  54. #include <libavcodec/avcodec.h>
  55. #include <libavformat/avformat.h>
  56. #include <libswscale/swscale.h>
  57. #include <SDL/SDL.h>
  58. #ifdef __cplusplus
  59. };
  60. #endif
  61. #endif
  62. //Refresh
  63. #define SFM_REFRESH_EVENT  (SDL_USEREVENT + 1)
  64. int thread_exit=0;
  65. //Thread
  66. int sfp_refresh_thread(void *opaque)
  67. {
  68. SDL_Event event;
  69. while (thread_exit==0) {
  70. event.type = SFM_REFRESH_EVENT;
  71. SDL_PushEvent(&event);
  72. //Wait 40 ms
  73. SDL_Delay(40);
  74. }
  75. return 0;
  76. }
  77. int main(int argc, char* argv[])
  78. {
  79. AVFormatContext *pFormatCtx;
  80. int             i, videoindex;
  81. AVCodecContext  *pCodecCtx;
  82. AVCodec         *pCodec;
  83. AVFrame *pFrame,*pFrameYUV;
  84. AVPacket *packet;
  85. struct SwsContext *img_convert_ctx;
  86. //SDL
  87. int ret, got_picture;
  88. int screen_w=0,screen_h=0;
  89. SDL_Surface *screen;
  90. SDL_Overlay *bmp;
  91. SDL_Rect rect;
  92. SDL_Thread *video_tid;
  93. SDL_Event event;
  94. char filepath[]="bigbuckbunny_480x272.h265";
  95. av_register_all();
  96. avformat_network_init();
  97. pFormatCtx = avformat_alloc_context();
  98. if(avformat_open_input(&pFormatCtx,filepath,NULL,NULL)!=0){
  99. printf("Couldn‘t open input stream.\n");
  100. return -1;
  101. }
  102. if(avformat_find_stream_info(pFormatCtx,NULL)<0){
  103. printf("Couldn‘t find stream information.\n");
  104. return -1;
  105. }
  106. videoindex=-1;
  107. for(i=0; i<pFormatCtx->nb_streams; i++)
  108. if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO){
  109. videoindex=i;
  110. break;
  111. }
  112. if(videoindex==-1){
  113. printf("Didn‘t find a video stream.\n");
  114. return -1;
  115. }
  116. pCodecCtx=pFormatCtx->streams[videoindex]->codec;
  117. pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
  118. if(pCodec==NULL)
  119. {
  120. printf("Codec not found.\n");
  121. return -1;
  122. }
  123. if(avcodec_open2(pCodecCtx, pCodec,NULL)<0)
  124. {
  125. printf("Could not open codec.\n");
  126. return -1;
  127. }
  128. pFrame=av_frame_alloc();
  129. pFrameYUV=av_frame_alloc();
  130. //uint8_t *out_buffer=(uint8_t *)av_malloc(avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height));
  131. //avpicture_fill((AVPicture *)pFrameYUV, out_buffer, PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
  132. //------------SDL----------------
  133. if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
  134. printf( "Could not initialize SDL - %s\n", SDL_GetError());
  135. return -1;
  136. }
  137. screen_w = pCodecCtx->width;
  138. screen_h = pCodecCtx->height;
  139. screen = SDL_SetVideoMode(screen_w, screen_h, 0,0);
  140. if(!screen) {
  141. printf("SDL: could not set video mode - exiting:%s\n",SDL_GetError());
  142. return -1;
  143. }
  144. bmp = SDL_CreateYUVOverlay(pCodecCtx->width, pCodecCtx->height,SDL_YV12_OVERLAY, screen);
  145. rect.x = 0;
  146. rect.y = 0;
  147. rect.w = screen_w;
  148. rect.h = screen_h;
  149. packet=(AVPacket *)av_malloc(sizeof(AVPacket));
  150. printf("---------------File Information------------------\n");
  151. av_dump_format(pFormatCtx,0,filepath,0);
  152. printf("-------------------------------------------------\n");
  153. img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
  154. //--------------
  155. video_tid = SDL_CreateThread(sfp_refresh_thread,NULL);
  156. //
  157. SDL_WM_SetCaption("Simple FFmpeg Player (SDL Update)",NULL);
  158. //Event Loop
  159. for (;;) {
  160. //Wait
  161. SDL_WaitEvent(&event);
  162. if(event.type==SFM_REFRESH_EVENT){
  163. //------------------------------
  164. if(av_read_frame(pFormatCtx, packet)>=0){
  165. if(packet->stream_index==videoindex){
  166. ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
  167. if(ret < 0){
  168. printf("Decode Error.\n");
  169. return -1;
  170. }
  171. if(got_picture){
  172. SDL_LockYUVOverlay(bmp);
  173. pFrameYUV->data[0]=bmp->pixels[0];
  174. pFrameYUV->data[1]=bmp->pixels[2];
  175. pFrameYUV->data[2]=bmp->pixels[1];
  176. pFrameYUV->linesize[0]=bmp->pitches[0];
  177. pFrameYUV->linesize[1]=bmp->pitches[2];
  178. pFrameYUV->linesize[2]=bmp->pitches[1];
  179. sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);
  180. SDL_UnlockYUVOverlay(bmp);
  181. SDL_DisplayYUVOverlay(bmp, &rect);
  182. }
  183. }
  184. av_free_packet(packet);
  185. }else{
  186. //Exit Thread
  187. thread_exit=1;
  188. break;
  189. }
  190. }
  191. }
  192. SDL_Quit();
  193. sws_freeContext(img_convert_ctx);
  194. //--------------
  195. //av_free(out_buffer);
  196. av_free(pFrameYUV);
  197. avcodec_close(pCodecCtx);
  198. avformat_close_input(&pFormatCtx);
  199. return 0;
  200. }

simplest_ffmpeg_player_su(SU版)中将simplest_ffmpeg_player(标准版)中的循环做了更改。标准版中为播放视频的循环如下代码所示。

[cpp] view plain copy

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

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

[cpp] view plain copy

  1. //自定义事件
  2. //刷新画面
  3. #define SFM_REFRESH_EVENT  (SDL_USEREVENT + 1)
  4. int thread_exit=0;
  5. //Thread
  6. int sfp_refresh_thread(void *opaque)
  7. {
  8. while (thread_exit==0) {
  9. SDL_Event event;
  10. event.type = SFM_REFRESH_EVENT;
  11. SDL_PushEvent(&event);
  12. //Wait 40 ms
  13. SDL_Delay(40);
  14. }
  15. return 0;
  16. }

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

[cpp] view plain copy

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

结果

软件运行截图:

完整工程下载地址:

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)==========================

simplest ffmpeg player 1.1

版本升级至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 plain copy

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

另外修补了几个的函数,例如增加了SDL_Quit()等。

simplest_ffmpeg_player_su(SU版)具体情况在上文中已经说明。

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

SourceForge上已经更新。

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

simplest ffmpeg player 1.2

版本升级至1.2。

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

[cpp] view plain copy

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

具体信息参见文章:avcodec_decode_video2()解码视频后丢帧的问题解决

2.为了更好地适应Linux等其他操作系统,做到可以跨平台,去除掉了VC特有的一些函数。比如“#include "stdafx.h"”,“_tmain()”等等。

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

SourceForge上已经更新。

Linux版本=================================

Linux下代码下载地址:

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

这个是Linux下的代码,在Ubuntu下测试可以运行,前提是安装了FFmpeg和SDL(版本1.2)。
编译命令:

[plain] view plain copy

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

使用方法:

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

[plain] view plain copy

  1. ./smp.out test.flv

 

更新-最终版(2015.2.12)==========================

simplest ffmpeg player 1 final

这是该播放器源代码的最后一次更新,以后会把更新的重点集中在基于FFmpeg和SDL2.0的视频播放器。这次考虑到了跨平台的要求,源代码的调整幅度比较大。经过这次调整之后,源代码可以在以下平台编译通过:

VC++:打开sln文件即可编译,无需配置。

cl.exe:打开compile_cl.bat即可命令行下使用cl.exe进行编译,注意可能需要按照VC的安装路径调整脚本里面的参数。编译命令如下。

[plain] view plain copy

  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 SDL.lib SDLmain.lib avcodec.lib ^
  9. avformat.lib avutil.lib avdevice.lib avfilter.lib postproc.lib swresample.lib swscale.lib ^
  10. /SUBSYSTEM:WINDOWS /OPT:NOREF
  11. exit

MinGW:MinGW命令行下运行compile_mingw.sh即可使用MinGW的g++进行编译。编译命令如下。

[plain] view plain copy

  1. g++ simplest_ffmpeg_player.cpp -g -o simplest_ffmpeg_player.exe \
  2. -I /usr/local/include -L /usr/local/lib \
  3. -lmingw32 -lSDLmain -lSDL -lavformat -lavcodec -lavutil -lswscale

GCC(Linux):Linux命令行下运行compile_gcc.sh即可使用GCC进行编译。编译命令如下。

[plain] view plain copy

  1. gcc simplest_ffmpeg_player.cpp -g -o simplest_ffmpeg_player.out \
  2. -I /usr/local/include -L /usr/local/lib -lSDLmain -lSDL -lavformat -lavcodec -lavutil -lswscale

GCC(MacOS):Mac终端下运行compile_gcc_mac.sh即可使用Mac 的GCC进行编译,Mac的GCC和Linux的GCC差别不大,但是使用SDL1.2的时候,必须加上“-framework Cocoa”参数,否则编译无法通过。编译命令如下。

[plain] view plain copy

  1. gcc simplest_ffmpeg_player.cpp -g -o simplest_ffmpeg_player.out \
  2. -framework Cocoa -I /usr/local/include -L /usr/local/lib \
  3. -lSDLmain -lSDL -lavformat -lavcodec -lavutil -lswscale

PS:相关的编译命令已经保存到了工程文件夹中

此外,该版本修正了在某些系统下(例如部分Ubuntu)SDL绿屏显示的问题,经过测试已经不再有绿屏现象。

CSDN下载地址:http://download.csdn.net/detail/leixiaohua1020/8443253

SourceForge上已经更新。

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

50

2
时间: 2024-07-30 13:48:16

100行代码实现最简单的基于FFMPEG+SDL的视频播放器(SDL1.x)【转】的相关文章

用JavaCV改写“100行代码实现最简单的基于FFMPEG+SDL的视频播放器 ”

FFMPEG的文档少,JavaCV的文档就更少了.从网上找到这篇100行代码实现最简单的基于FFMPEG+SDL的视频播放器.地址是http://blog.csdn.net/leixiaohua1020/article/details/8652605. 用JavaCV重新实现并使用opencv_highgui进行显示. 1 import com.googlecode.javacpp.IntPointer; 2 import com.googlecode.javacpp.Pointer; 3 im

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

===================================================== 最简单的基于FFmpeg的视频播放器系列文章列表: 100行代码实现最简单的基于FFMPEG+SDL的视频播放器(SDL1.x) 最简单的基于FFMPEG+SDL的视频播放器 ver2 (採用SDL2.0) 最简单的基于FFmpeg的解码器-纯净版(不包括libavformat) 最简单的基于FFMPEG+SDL的视频播放器:拆分-解码器和播放器 最简单的基于FFMPEG的Hellowor

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

简介 之前做过一个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

基于&lt;最简单的基于FFMPEG+SDL的视频播放器 ver2 (采用SDL2.0)&gt;的一些个人总结

最近因为项目接近收尾阶段,所以变的没有之前那么忙了,所以最近重新拿起了之前的一些FFMPEG和SDL的相关流媒体播放器的例子在看. 同时自己也用FFMPEG2.01,SDL2.01结合MFC以及网上罗列的一些资料,打算打造一款自己的简易播放器. 最先开始是阅读了<An ffmpeg and SDL Tutorial>以及来源与(http://blog.csdn.net/love4mario/article/details/17652355)中的中文资料,同时认真对tutorial01-08中的

最简单的基于FFMPEG+SDL的视频播放器:拆分-解码器和播放器

本文补充记录<最简单的基于FFMPEG+SDL的视频播放器>中的两个例子:FFmpeg视频解码器和SDL像素数据播放器.这两个部分是从视频播放器中拆分出来的两个例子.FFmpeg视频解码器实现了视频数据到YUV数据的解码,而SDL像素数据播放器实现了YUV数据的显示.简而言之,原先的FFmpeg+SDL视频播放器实现了: 视频数据->YUV->显示器 FFmpeg视频解码器实现了: 视频数据->YUV SDL像素数据播放器实现了: YUV->显示器 FFmpeg视频解码

最简单的基于FFMPEG+SDL的视频播放器附件:Helloworld

本文记录一个基于FFmpeg的HelloWorld程序.该程序可以打印出FFmpeg类库的基本信息.使用该程序通常可以验证FFmpeg是否正确的安装配置. 源代码 /** * 最简单的FFmpeg Helloworld程序 * Simplest FFmpeg HelloWorld * * 雷霄骅 Lei Xiaohua * [email protected] * 中国传媒大学/数字电视技术 * Communication University of China / Digital TV Tech

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

简介 之前做过一个简单的音频播放器:<最简单的基于FFMPEG+SDL的音频播放器>,采用的是SDL1.2.前两天刚把原先做的<最简单的基于FFMPEG+SDL的视频播放器>更新采用了SDL2.0,于是顺手也把音频播放器更新成为SDL2.0. SourceForge项目主页:https://sourceforge.net/projects/simplestffmpegaudioplayer/ 完整工程下载地址:http://download.csdn.net/detail/leix

不到100行代码实现一个简单的推荐系统

似乎咱的产品七,八年前就想做个推荐系统的,就是类似根据用户的喜好,自动的找到用户喜欢的电影或者节目,给用户做推荐.可是这么多年过去了,不知道是领导忘记了还是怎么了,连个影子还没见到. 而市场上各种产品的都有了推荐系统了.比如常见的各种购物网站京东,亚马逊,淘宝之类的商品推荐,视频网站优酷的的类似影片推荐,豆瓣音乐的音乐推荐...... 一个好的推荐系统推荐的精度必然很高,能够真的发现用户的潜在需求或喜好,提高购物网詀的销量,让视频网站发现用户喜欢的收费电影... 可是要实现一个高精度的推荐系统不

最简单的基于FFMPEG+SDL的音频播放器:拆分-解码器和播放器

本文补充记录<最简单的基于FFMPEG+SDL的音频播放器>中的两个例子:FFmpeg音频解码器和SDL音频采样数据播放器.这两个部分是从音频播放器中拆分出来的两个例子.FFmpeg音频解码器实现了视频数据到PCM采样数据的解码,而SDL音频采样数据播放器实现了PCM数据到音频设备的播放.简而言之,原先的FFmpeg+SDL音频播放器实现了: 音频数据->PCM->音频设备 FFmpeg音频解码器实现了: 音频数据->PCM SDL音频采样数据播放器实现了: PCM->