使用ffmpeg进行图像格式转换以及图像缩放/sws_scale/linux/c++/c/rgb-yuv420
2014-12-15 14:48
1136 查看
转自:http://blog.csdn.net/skys_broyal/article/details/10337147
利用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
plaincopy
#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
[cpp] view
plaincopy
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
[cpp] view
plaincopy
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进行图像数据格式的转换以及图片的缩放应用中,主要用到了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
plaincopy
#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
[cpp] view
plaincopy
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
[cpp] view
plaincopy
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_scale/linux/c++/c/rgb-yuv420
- 使用ffmpeg进行图像格式转换以及图像缩放/sws_scale/linux/c++/c/rgb-yuv420
- 使用ffmpeg进行图像格式转换以及图像缩放/sws_scale/linux/c++/c/rgb-yuv420
- Linux下使用RecordMyDesktop进行屏幕录像 输出图像格式转换
- Linux下使用convert对图片缩放和使用ffmpeg对视频进行转换
- 使用ffmpeg进行图片格式转换和图片缩放—jpg to yuv420/.mp4 to yuv420
- 轻松使用 ffmpeg sdk 实现各种格式的rgb以及yuv raw
- RGB与YUV图像视频格式的相互转换
- FFMPEG 实现 YUV,RGB各种图像原始数据之间的转换(swscale)
- RGB与YUV图像视频格式的相互转换
- RGB与YUV图像视频格式的相互转换(转)
- FFMPEG 实现 YUV,RGB各种图像原始数据之间的转换(swscale)
- YUY2(YUV) 与 RGB 格式图片的相互转换 以及 基于YUY2(YUV)的blending
- YUV色彩模型 & YUV与RGB的转换公式 & YUV420的存储格式
- YUY2(YUV) 与 RGB 格式图片的相互转换 以及 基于YUY2(YUV)的blending
- RGB与YUV图像视频格式的相互转换
- 使用C#进行图片转换格式,缩放,自动旋转,保留exif(转载)
- 轻松使用 ffmpeg sdk 实现各种格式的rgb以及yuv raw
- (原创)用verilog实现RGB格式图像到YCbCr或YUV格式的转换及其验证方法 (RGB2YCrCb)(RGB2YUV)
- RGB与YUV图像视频格式的相互转换