FFmpeg解码H264及swscale缩放详解

本文转载自:http://blog.csdn.net/gubenpeiyuan/article/details/19548019?utm_source=tuicool


本文概要:

        本文介绍著名开源音视频编解码库ffmpeg如何解码h264码流,比较详细阐述了其h264码流输入过程,解码原理,解码过程。同时,大部分应用环境下,以原始码流视频大小展示并不是最佳方式,因此,开发者不仅仅需要对视频流解码,并且需要缩放图像以展示于不同窗体下。

        综上,本文除介绍ffmpeg解码h264,同时阐述如何使用swscale缩放视频流。       

        文章使用的开发环境Ubuntu12.04.。交流邮箱:leoluopy@gmail.com。转载请注明出处 CSDN--固本培元。

ffmpeg介绍:

          FFmpeg是一个开源免费跨平台的视频和音频流方案,属于自由软件,采用LGPL或GPL许可证(依据你选择的组件)。它提供了录制、转换以及流化音视频的完整解决方案。它包含了非常先进的音频/视频编解码库libavcodec,为了保证高可移植性和编解码质量,libavcodec里很多codec都是从头开发的。

    

开始解码

         好了,不多说了。直接上工程和代码吧。(注意在链接工程时,引用库有连接顺序,因为他们有相互依赖关系,如果缺少将不能通过编译。)

        

        需要连接的库: VS代码如下

  1. #pragma comment (lib,"..\\FFMPEG_lib\\avformat.lib")  
  2. #pragma comment (lib,"..\\FFMPEG_lib\\avutil.lib")  
  3. #pragma comment (lib,"..\\FFMPEG_lib\\swscale.lib")  
  4. #pragma comment (lib,"..\\FFMPEG_lib\\avcodec.lib")  
  5. #pragma comment (lib,"..\\FFMPEG_lib\\avdevice.lib")  
  6. #pragma comment (lib,"..\\FFMPEG_lib\\avfilter.lib")  

          需要的头文件:
  1. #include "libavcodec\\avcodec.h"  
  2. #include "libswscale/swscale.h"  

          环境初始化代码:(参考了api-example.c)ubuntu上使用的ffmpeg版本是0.6
  1.    avcodec_init(); //首先,main函数中一开始会去调用avcodec_init()函数,该函数的作用是初始化libavcodec,而我们在使用avcodec编解码库时,该函数必须被调用。  
  2.    avcodec_register_all();//注册所有的编解码器(codecs),解析器(parsers)以及码流过滤器(bitstream filters)。当然我们也可以使用个别的注册函数来注册我们所要支持的格式。  
  3.   
  4.    AVCodec *codec;  
  5.    AVCodecContext *c= NULL;  
  6.    int frame, size, got_picture, len;  
  7.    FILE *fin, *fout;  
  8.    AVFrame *picture,*dst_picture;  
  9.    uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE], *inbuf_ptr;  
  10.    char buf[1024];  
  11.   
  12.   
  13.    /* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */  
  14.    memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);  
  15.   
  16.    printf("Video decoding\n");  
  17.   
  18.    /* find the mpeg1 video decoder */  
  19.    codec = avcodec_find_decoder(CODEC_ID_H264);  
  20.    if (!codec){  
  21.        fprintf(stderr, "codec not found\n");  
  22.        exit(1);  
  23.    }  
  24.   
  25.    c= avcodec_alloc_context();  
  26.    picture= avcodec_alloc_frame();  
  27.   
  28.    if(codec->capabilities&CODEC_CAP_TRUNCATED){  
  29.        c->flags|= CODEC_FLAG_TRUNCATED; /* we dont send complete frames */  
  30. }  
  31.   
  32.   
  33.    /* for some codecs, such as msmpeg4 and mpeg4, width and height  
  34.       MUST be initialized there because these info are not available  
  35.       in the bitstream */  
  36.   
  37.   
  38.    /* open it */  
  39.    if (avcodec_open(c, codec) < 0){  
  40.        fprintf(stderr, "could not open codec\n");  
  41.        exit(1);  
  42.    }  

          avcodec_init和avcodec_register_all初始化了相关的解码器,申请了解码需要的空间等。

          其他解码需要具备的是AVcontext、AVCodec、以及AVFrame。

          AVContext是解码需要的环境,其中存储了比如长宽,编码器算法,位图格式等信息。

         AVCondec就是你所选择的的编解码器了,使用枚举来索引,申请空间后与解码函数配合使用。

         AVFrame与AVPicture比较像,都存储解码后的位图信息。


解码:

         avcodec_decode_video需要输入参数,AVContext,AVFrame,数据首地址以及数据长度。同时传入一个int指针用于记录解码返回的解码成功帧数。

         len记录本次解码消耗的字节数。

  1. len = avcodec_decode_video(c, picture, &got_picture,  
  2.                            inbuf_ptr, size);  
         注意:在解码过程中不要清理contxt环境,以及解码器,如果有必要字节流空间有保存意义,因为,264传输过程中,有PTS以及DTS之分,播放时间以及解码时间如果不一致,可能导致,先到数据需要存储后到达他解码时间时解码。

          同时,h264码流分IPB帧,只有I帧是比较全面的图像信息。如果在解码I帧完成后,清空解码环境context,后续解码将持续返回错误信息,直至下一个I帧出现。作者亲测,望看到此文的朋友在做解码时不会再走这条弯路。

          自此,解码部分阐述完毕。

缩放:

           

利用ffmpeg进行图像数据格式的转换以及图片的缩放应用中,主要用到了swscale.h文件中的三个函数,分别是:

    

  1. struct SwsContext *sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat,  
  2.                              int dstW, int dstH, enum AVPixelFormat dstFormat,  
  3.                              int flags, SwsFilter *srcFilter,  
  4.                              SwsFilter *dstFilter, const double *param);  
  5.     int sws_scale(struct SwsContext *c, const uint8_t *const srcSlice[],  
  6.                    const int srcStride[], int srcSliceY, int srcSliceH,  
  7.                  uint8_t *const dst[], const int dstStride[]);  
  8.     void sws_freeContext(struct SwsContext *swsContext);  

  sws_getContext函数可以看做是初始化函数,它的参数定义分别为:

      int srcW,int srcH 为原始图像数据的高和宽;

      int dstW,int dstH 为输出图像数据的高和宽;

      enum AVPixelFormat srcFormat 为输入和输出图片数据的类型;eg:AV_PIX_FMT_YUV420、PAV_PIX_FMT_RGB24;

      int flags 为scale算法种类;eg:SWS_BICUBIC、SWS_BICUBLIN、SWS_POINT、SWS_SINC;

      SwsFilter *srcFilter ,SwsFilter *dstFilter,const double *param 可以不用管,全为NULL即可;

  sws_scale函数则为执行函数,它的参数定义分别为:

      struct SwsContext *c 为sws_getContext函数返回的值;

      const uint8_t *const srcSlice[],uint8_t *const dst[] 为输入输出图像数据各颜色通道的buffer指针数组;

      const int srcStride[],const int dstStride[] 为输入输出图像数据各颜色通道每行存储的字节数数组;     

      int srcSliceY 为从输入图像数据的第多少列开始逐行扫描,通常设为0;

      int srcSliceH 为需要扫描多少行,通常为输入图像数据的高度;

  sws_freeContext函数为结束函数,它的参数即为sws_getContext函数返回的值;


         做一个实际缩放YUV420函数打包实例如下:


  1. int ScaleImg(AVCodecContext *pCodecCtx,AVFrame *src_picture,AVFrame *dst_picture,int nDstH ,int nDstW )  
  2. {  
  3. int i ;  
  4. int nSrcStride[3];  
  5. int nDstStride[3];  
  6. int nSrcH = pCodecCtx->height;  
  7. int nSrcW = pCodecCtx->width;  
  8. struct SwsContext* m_pSwsContext;  
  9.   
  10.   
  11. uint8_t *pSrcBuff[3] = {src_picture->data[0],src_picture->data[1], src_picture->data[2]};  
  12.   
  13.   
  14. nSrcStride[0] = nSrcW ;  
  15. nSrcStride[1] = nSrcW/2 ;  
  16. nSrcStride[2] = nSrcW/2 ;  
  17.   
  18.   
  19.   
  20.   
  21. dst_picture->linesize[0] = nDstW;  
  22. dst_picture->linesize[1] = nDstW / 2;  
  23. dst_picture->linesize[2] = nDstW / 2;  
  24.   
  25.   
  26. printf("nSrcW%d\n",nSrcW);  
  27.   
  28.   
  29. m_pSwsContext = sws_getContext(nSrcW, nSrcH, PIX_FMT_YUV420P,  
  30. nDstW, nDstH, PIX_FMT_YUV420P,  
  31. SWS_BICUBIC,  
  32. NULL, NULL, NULL);  
  33.   
  34.   
  35. if (NULL == m_pSwsContext)  
  36. {  
  37. printf("ffmpeg get context error!\n");  
  38. exit (-1);  
  39. }  
  40.   
  41.   
  42. sws_scale(m_pSwsContext, src_picture->data,src_picture->linesize, 0, pCodecCtx->height,dst_picture->data,dst_picture->linesize);  
  43.   
  44.   
  45. printf("line0:%d line1:%d line2:%d\n",dst_picture->linesize[0] ,dst_picture->linesize[1] ,dst_picture->linesize[2]);  
  46. sws_freeContext(m_pSwsContext);  
  47.   
  48.   
  49. return 1 ;  
  50. }  

        函数很简单,申请环境初始指针,后缩放即可。读到此文的朋友,这个函数可以直接拷贝使用哟。如果有疑问可以留言或者邮件:leoluopy@gmail.com

RGB的缩放可以参考下面:

  1. int ScaleYUVImgToRGB(int nSrcW,int nSrcH ,uint8_t* src_data,int *linesize,int nDstW ,int nDstH )    
  2. {    
  3.     int i ; int ret ;  FILE *nRGB_file ;  
  4.   
  5.     AVFrame *nDst_picture ;  
  6.     struct SwsContext* m_pSwsContext;    
  7.   
  8.     nDst_picture = avcodec_alloc_frame();  
  9.     if (!nDst_picture){  
  10.         printf("nDst_picture avcodec_alloc_frame failed\n");  
  11.         exit(1);  
  12.     }  
  13.     if(avpicture_alloc((AVPicture *)nDst_picture,PIX_FMT_RGB24,nDstW, nDstH)<0){  
  14.         printf("dst_picture avpicture_alloc failed\n");  
  15.         exit(1);  
  16.     }  
  17.     m_pSwsContext = sws_getContext(nSrcW, nSrcH, PIX_FMT_YUV420P,    
  18.         nDstW, nDstH, PIX_FMT_RGB24,    
  19.         SWS_BICUBIC,    
  20.         NULL, NULL, NULL);    
  21.   
  22.     if (NULL == m_pSwsContext)    
  23.     {    
  24.         printf("ffmpeg get context error!\n");    
  25.         exit (-1);    
  26.     }     
  27.   
  28.     ret = sws_scale(m_pSwsContext, src_data,linesize, 0,nSrcH,nDst_picture->data,nDst_picture->linesize);    
  29.   
  30.     nRGB_file = fopen("..\\YUV_STREAM\\RGBFile.rgb","ab+");  
  31.     fwrite(nDst_picture->data[0],nDstW*nDstH*3,1,nRGB_file);  
  32.     fclose(nRGB_file);  
  33.   
  34.     sws_freeContext(m_pSwsContext);    
  35.     avpicture_free((AVPicture *)nDst_picture);  
  36.   
  37.     return 0;    
  38. }  

参数data  和 linesize 参考yuv平面指针即可。

 同时如果不想使用AVPicture结构的话,可以参考下面的:(注意不同图像类型,linesize一定要写对)

  1. char* H264Decoder_c::ScaleYUVImgToRGB(int nSrcW,int nSrcH ,uint8_t** src_data,int *linesize,int nDstW ,int nDstH )  
  2. {  
  3.     int i ; int ret ;  FILE *nRGB_file ;  
  4.   
  5.     struct SwsContext* m_pSwsContext;  
  6.     char*  out_Img[3];  
  7.     int out_linesize[3];  
  8.     out_linesize[0] = 2*nDstW ; //out_linesize[1] = nDstW ;out_linesize[2] = nDstW ;  
  9.     out_Img[0] = g_RGBImg ;  
  10.   
  11.     m_pSwsContext = sws_getContext(nSrcW, nSrcH, PIX_FMT_YUV420P,  
  12.         nDstW, nDstH, PIX_FMT_RGB565,  
  13.         SWS_BICUBIC,  
  14.         NULL, NULL, NULL);  
  15.   
  16.     if (NULL == m_pSwsContext)  
  17.     {  
  18.         printf("ffmpeg get context error!\n");  
  19.         exit (-1);  
  20.     }  
  21.   
  22.     ret = sws_scale(m_pSwsContext, src_data,linesize, 0,nSrcH,(uint8_t**)out_Img,out_linesize);  
  23. #if 0  
  24.     nRGB_file = fopen("./RGBFile.rgb","ab+");  
  25.     fwrite(out_Img[0],nDstW*nDstH*2,1,nRGB_file);  
  26.     fclose(nRGB_file);  
  27.   
  28. #endif  
  29.   
  30.     sws_freeContext(m_pSwsContext);  
  31.   
  32.     return out_Img[0];  
  33. }  



        下面有一个缩放图像的效果图:



目的位图的空间申请:

       注意:上面的缩放函数如果直接使用而在没有解码成功或者没有申请目的位图空间时,将报段错误。

      原因:没有解码成功,位图源地址将是指向空的地址,目的位图地址同样。
  

      申请目的位图的方式:

  1. dst_picture = avcodec_alloc_frame();  
  2. if (!dst_picture){  
  3. return ;  
  4. }  
  5. if(avpicture_alloc((AVPicture *)dst_picture, c->pix_fmt,c->width*2, c->height*2)<0){  
  6. printf("dst_picture allocate failed\n");  
  7. exit(1);  
  8. }  


         初始化后即可以用于缩放了。


图像的内存Dump方法:

        上文已经比较详细的阐述了ffmpeg的解码以及缩放原理及流程,然而在实际运用环境中,无论是从实时播放或者是从历史回放来看,我们需要的是像素位图,而不是ffmpeg的结构体。那么怎么转换呢?下文介绍了相关的内容。

        作为实际运用环境中,像素位图格式,笔者使用的是比较常用的YUV格式。

        编解码图像格式

          承接上文继续,在大部分现场环境下,为了节省传送带宽以及提高系统工作效率,视频原始位图格式以及解码后展示格式一般使用YUV420。
          其中YV12以及IYUV是最常见的格式。
          简单的说,YUV格式又分为平面格式以及打包格式。他们各有利弊。本文使用的是YUV平面格式即三通道分别使用不同入口地址存储,这样的好处是,与ffmpeg解码接口方面,AVFrame中数据结构可以便捷拷贝图像信息。

         YV12:


     

         IYUV 




          这里做简单的叙述:详细可以参考:

          http://blog.csdn.net/searchsun/article/details/2443867


     如何Dump

           好了,对YUV格式又初步了解后,下面介绍如果导出这像素位图数据。ffmpeg将图像的亮度以及色差信息保存在AVFrame中的data[0]、data[1]、data[2]中。

          详细参考:

AVFrame和AVPicture对比:

http://yul100887.blog.163.com/blog/static/200336135201211143525930/


           所有操作均是针对这三个指针展开的,如下:


  1. pgm_save(picture->data[0], picture->linesize[0], //Y  
  2. c->width, c->height, outfilename);  
  3. pgm_save(picture->data[1], picture->linesize[1],  
  4. c->width/2, c->height/2, outfilename); //U  
  5. pgm_save(picture->data[2], picture->linesize[2],  
  6. c->width/2, c->height/2, outfilename);  //V  
  1. void pgm_save(unsigned char *buf,int wrap, int xsize,int ysize,char *filename)  
  2. {  
  3.     FILE *f;  
  4.     int i;  
  5.   
  6.   
  7. f=fopen(filename,"ab+");     
  8.   
  9.   
  10. for(i=0;i<ysize;i++)  
  11. {  
  12. fwrite(buf + i * wrap, 1, xsize, f );   
  13. }  
  14. fclose(f);  
  15. }  
                代码对YUV三个像素通道分开dump,读者可以根据自己的需要对函数进行包装。data[0] 是亮度信息入口指针,同时传入图像长宽,以及存储内存区域行宽。 data[1]  data[2] 类似。

                最后需要注意的是:linesize总是容易被遗忘,livesize[0]=height ,livesize[1]=height/2 ,livesize[2]=height /2,
                

               此文到此结束,感谢阅读。

                                                                        ---------------------leoluopy

参考文章:

使用ffmpeg进行图像格式转换及缩放

http://blog.csdn.net/skys_broyal/article/details/10337147

AVFrame和AVPicture对比:

http://yul100887.blog.163.com/blog/static/200336135201211143525930/

FFMPEG 实现 YUV,RGB各种图像原始数据之间的转换(swscale)

http://blog.csdn.net/leixiaohua1020/article/details/14215391

ffmpeg编解码H264

http://blog.csdn.net/chinabinlang/article/details/7088590


h264检测是I帧还是P帧

http://blog.csdn.net/zgyulongfei/article/details/7558031





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值