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

本文介绍如何使用FFmpeg的swscale库进行图像格式转换及缩放操作。重点介绍了sws_getContext、sws_scale和sws_freeContext三个关键函数,并通过两个示例代码详细展示了RGB到RGB、RGB到YUV420P的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  利用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

#ifndef INT64_C
#define INT64_C
#define UINT64_C
#endif

extern "C"
{
#include "libswscale/swscale.h"
}
struct ImgInfo
{
    unsigned int height;
    unsigned int width;
    unsigned long bufferSize;
    unsigned char *bufferPtr;
}rawDate;

//.cpp

    int nSrcH = rawData.height;  //1200
    int nSrcW = rawData.width;   //1600
		
    int nDstH = RESIZED_HIGHT;   //258
    int nDstW = RESIZED_WIDTH;   //352
		
    uint8_t *pSrcBuff[3] = {rawData.bufferPtr, rawData.bufferPtr  + nSrcW * nSrcH, rawData.bufferPtr  + nSrcW * nSrcH * 2};
    uint8_t *pDstBuff[3] = {pResizedData->bufferPtr, pResizedData->bufferPtr + nDstW * nDstH, pResizedData->bufferPtr + nDstW * nDstH * 2};
       
    int nSrcStride[3];
    int nDstStride[3];
    
    for (int i=0; i<3; i++)
    {
    	nSrcStride[i] = nSrcW * 3;
    	nDstStride[i] = nDstW * 3;
    }
    
    SwsContext*	m_pSwsContext;
    
    m_pSwsContext = sws_getContext(nSrcW, nSrcH, AV_PIX_FMT_RGB24,
                                  nDstW, nDstH, AV_PIX_FMT_RGB24,
                                  SWS_SINC, 
                                  NULL, NULL, NULL);
                                  
    if (NULL == m_pSwsContext) 
    {
    	printf("ffmpeg get context error!\n");
    	return false;
    }
    
    sws_scale(m_pSwsContext, pSrcBuff,
              nSrcStride, 0, nSrcH,
              pDstBuff, nDstStride);
              
    sws_freeContext(m_pSwsContext);

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

//.cpp

    int nSrcH = rawData->height;   //1200
    int nSrcW = rawData->width;    //1600
		
    int nDstH = RESIZED_HIGHT;    //288
    int nDstW = RESIZED_WIDTH;    //352
		
    uint8_t *pSrcBuff[3] = {rawData->bufferPtr, rawData->bufferPtr  + nSrcW * nSrcH, rawData->bufferPtr  + nSrcW * nSrcH * 2};
    uint8_t *pDstBuff[3] = {resizedYuvData->bufferPtr, resizedYuvData->bufferPtr + nDstW * nDstH, resizedYuvData->bufferPtr + nDstW * nDstH * 5 / 4};
       
    int nSrcStride[3];
    int nDstStride[3];
    
    for (int i=0; i<3; i++)
    {
    	nSrcStride[i] = nSrcW * 3;
    }
    
    nDstStride[0] = nDstW;
    nDstStride[1] = nDstW / 2;
    nDstStride[2] = nDstW / 2;
    
    SwsContext*	m_pSwsContext;
    
    m_pSwsContext = sws_getContext(nSrcW, nSrcH, AV_PIX_FMT_RGB24,
                                  nDstW, nDstH, AV_PIX_FMT_YUV420P,
                                  SWS_SINC, 
                                  NULL, NULL, NULL);
                                  
    if (NULL == m_pSwsContext) 
    {
    	printf("ffmpeg get context error!\n");
    	return false;
    }
    
    sws_scale(m_pSwsContext, pSrcBuff,
              nSrcStride, 0, nSrcH,
              pDstBuff, nDstStride);
              
    sws_freeContext(m_pSwsContext);	




FFmpeg中,`sws`(Simple_wayne_scale)是一个强大的工具,用于音频和视频信号的格式转换缩放操作,特别是处理像素级别的转换,包括但不限于颜色空间变换(如YUVRGB)、分辨率调整等。`sws`库提供了一套API,支持各种滤波算法(如最近邻插值、线性插值、bicubic等)。 如果你想要使用`sws`进行转码,通常会涉及到以下几个步骤: 1. **初始化上下文**:首先,你需要创建一个`sws_context`结构体,它表示一个转换上下文,可以用于执行多次转换。这通常需要提供源图像的输入格式、目标格式以及一些转换参数。 2. **配置转换**:设置`sws_context`的各种参数,如输入和输出的帧大小、色彩空间、过滤算法等。这对于精确控制转换质量和速度至关重要。 3. **执行转换**:使用`sws_scale`函数,传入上下文、源图像、目标缓冲区以及相应的宽度、高度等信息,这个函数会对源图像进行转换并写入目标缓冲区。 4. **释放资源**:转换完成后,记得关闭上下文以释放内存和其他资源。 下面是一个简单的C++示例,展示如何使用`sws`进行YUVRGB转换: ```cpp sws_ctx_t *ctx = sws_getContext(srcWidth, srcHeight, srcFormat, dstWidth, dstHeight, dstFormat, SWS_BICUBIC, NULL, NULL, NULL); if (!ctx) { // 处理错误 } // 执行转换 unsigned char *dstYuvData = new unsigned char[dstWidth * dstHeight * 3]; if (!sws_scale(ctx, srcYuvBuffer, srcStride, 0, srcHeight, dstYuvData, dstStride)) { // 处理错误 } // 现在 dstYuvData 包含了 RGB 数据 delete[] ctx; // 释放上下文 ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值