使用ffmpeg进行图像格式转换以及图像缩放/sws_scale/linux/c++/c/rgb-yuv420

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

      struct SwsContext *sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat,
                               int dstW, int dstH, enum AVPixelFormat dstFormat,
                               int flags, SwsFilter *srcFilter,
                               SwsFilter *dstFilter, const double *param);

      int sws_scale(struct SwsContext *c, const uint8_t *const srcSlice[],

                     const int srcStride[], int srcSliceY, int srcSliceH,
                   uint8_t *const dst[], const int dstStride[]);

      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函数返回的值;

  示例代码:rgb24_2_rgb24/1600*1200—352*288

//.h

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #ifndef INT64_C  
  2. #define INT64_C  
  3. #define UINT64_C  
  4. #endif  
  5.   
  6. extern "C"  
  7. {  
  8. #include "libswscale/swscale.h"  
  9. }  
  10. struct ImgInfo  
  11. {  
  12.     unsigned int height;  
  13.     unsigned int width;  
  14.     unsigned long bufferSize;  
  15.     unsigned char *bufferPtr;  
  16. }rawDate;  

//.cpp

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. int nSrcH = rawData.height;  //1200  
  2. int nSrcW = rawData.width;   //1600  
  3.   
  4. int nDstH = RESIZED_HIGHT;   //258  
  5. int nDstW = RESIZED_WIDTH;   //352  
  6.   
  7. uint8_t *pSrcBuff[3] = {rawData.bufferPtr, rawData.bufferPtr  + nSrcW * nSrcH, rawData.bufferPtr  + nSrcW * nSrcH * 2};  
  8. uint8_t *pDstBuff[3] = {pResizedData->bufferPtr, pResizedData->bufferPtr + nDstW * nDstH, pResizedData->bufferPtr + nDstW * nDstH * 2};  
  9.      
  10. int nSrcStride[3];  
  11. int nDstStride[3];  
  12.   
  13. for (int i=0; i<3; i++)  
  14. {  
  15.     nSrcStride[i] = nSrcW * 3;  
  16.     nDstStride[i] = nDstW * 3;  
  17. }  
  18.   
  19. SwsContext* m_pSwsContext;  
  20.   
  21. m_pSwsContext = sws_getContext(nSrcW, nSrcH, AV_PIX_FMT_RGB24,  
  22.                               nDstW, nDstH, AV_PIX_FMT_RGB24,  
  23.                               SWS_SINC,   
  24.                               NULL, NULL, NULL);  
  25.                                 
  26. if (NULL == m_pSwsContext)   
  27. {  
  28.     printf("ffmpeg get context error!\n");  
  29.     return false;  
  30. }  
  31.   
  32. sws_scale(m_pSwsContext, pSrcBuff,  
  33.           nSrcStride, 0, nSrcH,  
  34.           pDstBuff, nDstStride);  
  35.             
  36. sws_freeContext(m_pSwsContext);  

示例代码:rgb24_2_yuv420/1600*1200—352*288

//.cpp

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. int nSrcH = rawData->height;   //1200  
  2. int nSrcW = rawData->width;    //1600  
  3.   
  4. int nDstH = RESIZED_HIGHT;    //288  
  5. int nDstW = RESIZED_WIDTH;    //352  
  6.   
  7. uint8_t *pSrcBuff[3] = {rawData->bufferPtr, rawData->bufferPtr  + nSrcW * nSrcH, rawData->bufferPtr  + nSrcW * nSrcH * 2};  
  8. uint8_t *pDstBuff[3] = {resizedYuvData->bufferPtr, resizedYuvData->bufferPtr + nDstW * nDstH, resizedYuvData->bufferPtr + nDstW * nDstH * 5 / 4};  
  9.      
  10. int nSrcStride[3];  
  11. int nDstStride[3];  
  12.   
  13. for (int i=0; i<3; i++)  
  14. {  
  15.     nSrcStride[i] = nSrcW * 3;  
  16. }  
  17.   
  18. nDstStride[0] = nDstW;  
  19. nDstStride[1] = nDstW / 2;  
  20. nDstStride[2] = nDstW / 2;  
  21.   
  22. SwsContext* m_pSwsContext;  
  23.   
  24. m_pSwsContext = sws_getContext(nSrcW, nSrcH, AV_PIX_FMT_RGB24,  
  25.                               nDstW, nDstH, AV_PIX_FMT_YUV420P,  
  26.                               SWS_SINC,   
  27.                               NULL, NULL, NULL);  
  28.                                 
  29. if (NULL == m_pSwsContext)   
  30. {  
  31.     printf("ffmpeg get context error!\n");  
  32.     return false;  
  33. }  
  34.   
  35. sws_scale(m_pSwsContext, pSrcBuff,  
  36.           nSrcStride, 0, nSrcH,  
  37.           pDstBuff, nDstStride);  
  38.             
  39. sws_freeContext(m_pSwsContext);   


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值