【发布时间】:2015-04-21 21:41:34
【问题描述】:
我正在尝试使用 ffmpeg 将 JPG 帧转换为 RGBA 帧。原始图像大小为 250x250,像素格式为 AV_PIX_FMT_YUV420P。源线步幅数组包含 [256, 128, 128](用于 3 个平面)。我的目标帧大小是 250x250。以下是代码。
AVFrame* GetBGRAFrame(AVFrame* pFrame)
{
const int kiImageWidth = pFrame->width ;
const int kiImageHeight = pFrame->height ;
int iDestWidth = kiImageWidth;
int iDestHeight = kiImageHeight;
AVPixelFormat enPixelFormat = (AVPixelFormat)pFrame->format ;
SwsContext* pImageConvertContext =
sws_getContext(kiImageWidth, kiImageHeight, enPixelFormat,
iDestWidth, iDestHeight, PIX_FMT_BGRA, 0, NULL, NULL, NULL);
// Allocate destination frame
int iNumBytes = avpicture_get_size(AV_PIX_FMT_BGRA, iDestWidth, iDestHeight);
AVFrame* pFrame2 = av_frame_alloc();
uint8_t* pFrameBuffer = (uint8_t*)av_malloc(iNumBytes * sizeof(uint8_t));
avpicture_fill((AVPicture*)pFrame2, pFrameBuffer, AV_PIX_FMT_BGRA, iDestWidth, iDestHeight);
sws_scale(pImageConvertContext, pFrame->data, pFrame->linesize, 0, kiImageHeight, pFrame2->data, pFrame2->linesize);
sws_freeContext(pImageConvertContext);
return pFrame2;
}
转换后,我的目标框架右侧有 2 个透明列。
如果我使用命令行工具,我可以获得正确的图像: ffmpeg -i image.jpg 输出.png
那么,我的代码有什么问题?
目的地有对齐要求吗?如果是,我应该如何更改我的代码以获得 250x250 BGRA 帧?
谢谢
【问题讨论】:
-
什么版本的 FFmpeg(或 libswscale)?
-
尝试将
SWS_POINT而不是0传递到sws_getContext。 -
@Mark:我尝试过 SWS_POINT、SWS_AREA、SWS_BILINEAR、SWS_BICUBIC 和 SWS_LANCZOS。问题仍然存在。