void Rgb24ToYuv420(int nSrcW,int nSrcH,BYTE* pRgbData,int nDstW, int nDstH,BYTE* pYuvData)
{
int nDstStride[3];
nDstStride[0] = nDstW;
nDstStride[1] = nDstW / 2;
nDstStride[2] = nDstW / 2;
int nSrcStride[3] = {3*nSrcW,0,0};
uint8_t *pSrcBuff[3] = {pRgbData,NULL,NULL};
uint8_t *pDstBuff[3] = {pYuvData, pYuvData + nDstW * nDstH, pYuvData + nDstW * nDstH * 5 / 4};
//翻转
pDstBuff[0] += nDstStride[0]*(nDstH -1);
nDstStride[0] *= -1;
pDstBuff[1] += nDstStride[1]*(nDstH/2 - 1);
nDstStride[1] *= -1;
pDstBuff[2] += nDstStride[2]*(nDstH/2 - 1);
nDstStride[2] *= -1;
SwsContext* m_pSwsContext;
m_pSwsContext = sws_getContext(nSrcW, nSrcH, AV_PIX_FMT_BGR24,
nDstW, nDstH, AV_PIX_FMT_YUV420P,
SWS_SA_BILINEAR,
NULL, NULL, NULL);
//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)
{
return;
}
sws_scale(m_pSwsContext, pSrcBuff,
nSrcStride, 0, nSrcH,
pDstBuff, nDstStride);
sws_freeContext(m_pSwsContext);
}
我也用过ffmpeg以前的库也是会有这个问题,1440*896的rgb24的格式缩放成1320*768的yuv420p也会有这个问题
解决方案:
通过改变nSrcStride/pSrcBuff来实现翻转
void Rgb24ToYuv420(int nSrcW,int nSrcH,BYTE* pRgbData,int nDstW, int nDstH,BYTE* pYuvData)
{
int nDstStride[3];
nDstStride[0] = nDstW;
nDstStride[1] = nDstW / 2;
nDstStride[2] = nDstW / 2;
int nSrcStride[3] = {-3*nSrcW,0,0};
uint8_t *pSrcBuff[3] = {pRgbData+3*nSrcW*( nSrcH - 1),NULL,NULL};
uint8_t *pDstBuff[3] = {pYuvData, pYuvData + nDstW * nDstH, pYuvData + nDstW * nDstH * 5 / 4};
SwsContext* m_pSwsContext;
m_pSwsContext = sws_getContext(nSrcW, nSrcH, AV_PIX_FMT_BGR24,
nDstW, nDstH, AV_PIX_FMT_YUV420P,
SWS_SA_BILINEAR,
NULL, NULL, NULL);
//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)
{
return;
}
sws_scale(m_pSwsContext, pSrcBuff,
nSrcStride, 0, nSrcH,
pDstBuff, nDstStride);
sws_freeContext(m_pSwsContext);
}