直接使用ffmpeg中的sws_scale虽然已 经不太复杂,但每次的手动初始化等操作,毕竟不够方便。我对这部分代码进行了封装。平时常见的需要处理的图像格式无非就是YUV420和RGB24、 RGB32等格式,我的封装仅仅对它们进行。blog好像不能上传附件,因此我没法把ffmpeg的scale相关的静态库lib(静态库携带比动态库方 便^_^)等上传了(发了一份给自己的qq邮箱留作备份了,发现这儿可以上传附件,全部源码也上传一份好了)。
下面的代码,可能需要ffmpeg的相关头文件支持,我对它们进行了一些精简,只需要一个头文件"swscale.h"即可。
FFScale.h源码如下:
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #pragma once
-
-
-
- enum SwsAlogrithm
- {
- SWS_SA_FAST_BILINEAR = 0x1,
- SWS_SA_BILINEAR = 0x2,
- SWS_SA_BICUBIC = 0x4,
- SWS_SA_X = 0x8,
- SWS_SA_POINT = 0x10,
- SWS_SA_AREA = 0x20,
- SWS_SA_BICUBLIN = 0x40,
- SWS_SA_GAUSS = 0x80,
- SWS_SA_SINC = 0x100,
- SWS_SA_LANCZOS = 0x200,
- SWS_SA_SPLINE = 0x400,
- };
-
-
- enum PicFormat
- {
- SWS_PF_NONE = PIX_FMT_NONE,
- SWS_PF_YUV420P = PIX_FMT_YUV420P,
- SWS_PF_RGB24 = PIX_FMT_RGB24,
- SWS_PF_BGR24 = PIX_FMT_BGR24,
- SWS_PF_ARGB = PIX_FMT_ARGB,
- SWS_PF_RGBA = PIX_FMT_RGBA,
- SWS_PF_ABGR = PIX_FMT_ABGR,
- SWS_PF_BGRA = PIX_FMT_BGRA,
- };
-
-
- class CFFScale
- {
- public:
- CFFScale(void);
- ~CFFScale(void);
-
-
- void SetAttribute(PicFormat srcFormat, PicFormat dstFormat
- , SwsAlogrithm enAlogrithm = SWS_SA_FAST_BILINEAR);
-
-
- BOOL Scale(
- byte *pSrc, int nSrcW, int nSrcH, int nSrcPicth,
- byte *pDst, int nDstW, int nDstH, int nDstPicth
- );
-
- private:
-
-
- BOOL Init();
-
-
- void DeInit();
-
- SwsContext* m_pSwsContext;
- PicFormat m_srcFormat;
- PicFormat m_dstFormat;
- SwsAlogrithm m_enAlogrithm;
-
- int m_nSrcW, m_nSrcH;
- int m_nSrcPicth;
- int m_nSrcSlice[4];
- int m_nSrcStride[4];
-
- int m_nDstW, m_nDstH;
- int m_nDstPicth;
- int m_nDstSlice[4];
- int m_nDstStride[4];
-
- };
FFScale.cpp源码如下:
- #include "StdAfx.h"
- #include "FFScale.h"
-
- #pragma comment(lib, "Scale/libavutil.lib")
- #pragma comment(lib, "Scale/libswscale.lib")
- #pragma comment(lib, "Scale/libgcc.lib")
- #pragma comment(lib, "Scale/libmingwex.lib")
-
-
- CFFScale::CFFScale(void)
- {
- m_pSwsContext = NULL;
- m_srcFormat = SWS_PF_NONE;
- m_dstFormat = SWS_PF_NONE;
- m_enAlogrithm = SWS_SA_FAST_BILINEAR;
-
- m_nSrcW = m_nSrcH = 0;
- m_nSrcPicth = 0;
- m_nDstW = m_nDstH = 0;
- m_nDstPicth = 0;
- for (int i=0; i<4; i++)
- {
- m_nSrcSlice[i] = -1;
- m_nSrcStride[i] = 0;
- m_nDstSlice[i] = -1;
- m_nDstStride[i] = 0;
- }
- }
-
-
- CFFScale::~CFFScale(void)
- {
- DeInit();
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- void CFFScale::SetAttribute(PicFormat srcFormat, PicFormat dstFormat
- , SwsAlogrithm enAlogrithm)
- {
- m_srcFormat = srcFormat;
- m_dstFormat = dstFormat;
- m_enAlogrithm = enAlogrithm;
- DeInit();
- }
-
-
-
-
-
-
-
-
-
-
- BOOL CFFScale::Init()
- {
-
- if (SWS_PF_NONE == m_srcFormat || SWS_PF_NONE == m_dstFormat)
- {
- return FALSE;
- }
-
-
- DeInit();
-
-
- m_pSwsContext = sws_getContext(
- m_nSrcW,
- m_nSrcH,
- (PixelFormat)m_srcFormat,
- m_nDstW,
- m_nDstH,
- (PixelFormat)m_dstFormat,
- (int)m_enAlogrithm,
- NULL,
- NULL,
- NULL);
- if (NULL == m_pSwsContext)
- {
- return FALSE;
- }
-
-
- if (m_srcFormat == SWS_PF_YUV420P)
- {
- m_nSrcSlice[0] = 0;
- m_nSrcSlice[1] = m_nSrcW * m_nSrcH;
- m_nSrcSlice[2] = m_nSrcW * m_nSrcH * 5 / 4;
- m_nSrcSlice[3] = -1;
-
- m_nSrcStride[0] = m_nSrcW;
- m_nSrcStride[1] = m_nSrcW / 2;
- m_nSrcStride[2] = m_nSrcW / 2;
- m_nSrcStride[3] = 0;
-
- }
- else
- {
- m_nSrcSlice[0] = 0;
- m_nSrcSlice[1] = -1;
- m_nSrcSlice[2] = -1;
- m_nSrcSlice[3] = -1;
-
- m_nSrcStride[0] = m_nSrcPicth;
- m_nSrcStride[1] = 0;
- m_nSrcStride[2] = 0;
- m_nSrcStride[3] = 0;
- }
-
-
- if (m_dstFormat == SWS_PF_YUV420P)
- {
- m_nDstSlice[0] = 0;
- m_nDstSlice[1] = m_nDstW * m_nDstH;
- m_nDstSlice[2] = m_nDstW * m_nDstH * 5 / 4;
- m_nDstSlice[3] = -1;
-
- m_nDstStride[0] = m_nDstW;
- m_nDstStride[1] = m_nDstW / 2;
- m_nDstStride[2] = m_nDstW / 2;
- m_nDstStride[3] = 0;
-
- }
- else
- {
- m_nDstSlice[0] = 0;
- m_nDstSlice[1] = -1;
- m_nDstSlice[2] = -1;
- m_nDstSlice[3] = -1;
-
- m_nDstStride[0] = m_nDstPicth;
- m_nDstStride[1] = 0;
- m_nDstStride[2] = 0;
- m_nDstStride[3] = 0;
- }
- return TRUE;
- }
-
-
-
-
-
-
-
-
-
-
- void CFFScale::DeInit()
- {
- if (NULL != m_pSwsContext)
- {
- sws_freeContext(m_pSwsContext);
- }
- m_pSwsContext = NULL;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- BOOL CFFScale::Scale(byte *pSrc, int nSrcW, int nSrcH, int nSrcPicth
- , byte *pDst, int nDstW, int nDstH, int nDstPicth)
- {
-
- if (nSrcW != m_nSrcW || nSrcH != m_nSrcH || m_nSrcPicth != m_nSrcPicth
- || nDstW != m_nDstW || nDstH != m_nDstH || m_nDstPicth != m_nDstPicth)
- {
- m_nSrcW = nSrcW;
- m_nSrcH = nSrcH;
- m_nSrcPicth = nSrcPicth;
- m_nDstW = nDstW;
- m_nDstH = nDstH;
- m_nDstPicth = nDstPicth;
- DeInit();
- }
-
-
- if (NULL == m_pSwsContext && !Init())
- {
- return FALSE;
- }
-
-
- byte *srcSlice[4], *dstSlice[4];
- for (int i=0; i<4; i++)
- {
- srcSlice[i] = m_nSrcSlice[i] < 0 ? NULL : (pSrc + m_nSrcSlice[i]);
- dstSlice[i] = m_nDstSlice[i] < 0 ? NULL : (pDst + m_nDstSlice[i]);
- }
- return sws_scale
- (
- m_pSwsContext,
- srcSlice,
- m_nSrcStride,
- 0,
- m_nSrcH,
- dstSlice,
- m_nDstStride
- ) == m_nSrcH;
- }
(acloud) |