简体   繁体   English

FFMpeg RGB32 到 NV12 使用 SWScale

[英]FFMpeg RGB32 to NV12 using SWScale

I'm trying to convert RGB32 frames to NV12 Frames to feed into an encoder.我正在尝试将 RGB32 帧转换为 NV12 帧以输入编码器。

m_iWidthIn = 1920;
m_iHeightIn = 1080;
m_iWidthOut = (((iWidthIn  + 31) >> 5) << 5) //32bit align
m_heightOut = (((iHeightIn + 31) >> 5) << 5) //32bit align
m_outputPixelFormat = AV_PIX_FMT_NV12;

// allocate and fill buffers

m_sws = ::sws_getContext(m_iWidthIn, m_iHeightIn, AV_PIX_FMT_RGB32, m_iWidthOut, m_iHeightOut, m_outputPixelFormat, SWS_FAST_BILINEAR, nullptr, nullptr, nullptr);
AVFrame* frameOut = av_frame_alloc();
frameOut->height = m_iHeightOut;
frameOut->width = m_iWidthOut;
frameOut->format = m_outputPixelFormat;
av_frame_get_buffer(frameOut, 32);
int linesize[1] = { m_iWidthIn * 4 };
uint8_t * data[1] = { m_inputBuffer  };
if (m_bFlip)
{
    data[0] += linesize[0] * (m_iHeightIn - 1);
    linesize[0] = -linesize[0];
}
::sws_scale(m_sws, data, linesize, 0, m_iHeightIn, frameOut->data, frameOut->linesize);
::av_image_copy_to_buffer(pOutputBuffer, lDataLen, frameOut->data, frameOut->linesize, m_outputPixelFormat, m_iWidthOut, m_iHeightOut, 32);

If I make m_outputPixelFormat AV_PIX_FMT_RGB32 and use a DMO colorspace converter, the video comes out correctly.如果我制作 m_outputPixelFormat AV_PIX_FMT_RGB32 并使用 DMO 色彩空间转换器,则视频会正确显示。 However if I change it to NV12, I end up with a slanted video with missing data at the bottom.但是,如果我将其更改为 NV12,我最终会得到一个倾斜的视频,底部缺少数据。 I know this is caused by me copying the data incorrectly out of the buffer, but I'm unsure what I'm doing incorrectly.我知道这是由于我从缓冲区中错误地复制数据引起的,但我不确定我做错了什么。

Your problem is here:你的问题在这里:

m_heightOut = (((iHeightIn + 31) >> 5) << 5) //32bit align

You don't need to align height.您不需要对齐高度。 So frameOut->data has m_iHeightIn height.所以frameOut->datam_iHeightIn高度。 The correct line is:正确的行是:

m_heightOut = iHeightIn;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM