我有一个应用程序,该应用程序生成了一堆JPG,我需要将其转换为WebM视频。我正在尝试将我的RGB数据从JPEGs获取到VPXENC示例中。我可以在输出视频中看到原始JPG的基本形状,但是所有内容都是有色绿色的(甚至应该是黑色的像素,大约是绿色的一半),并且其他所有扫描线都有一些垃圾。

我正在尝试将其馈送为VPX_IMG_FMT_YV12数据,我假设它是这样的:

对于每个2x2 V块的8位平均8位平均8位平均每个2x2 U块的平均值8位平均值

这是源图像和视频的屏幕截图:

图片

我可能会错误地进行RGB-> YV12转换,但是即使我只编码8位Y数据并将U和V块设置为0,视频也看起来相同。我基本上通过此方程运行我的RGB数据:

// (R, G, and B are 0-255)
float y = 0.299f*R + 0.587f*G + 0.114f*B;
float v = (R-y)*0.713f;
float u = (B-v)*0.565f;

。每个2x2像素块。

所以我想知道:

  1. 是否有一种更简单的方法(在代码中)获取RGB数据并将其馈送到VPX_CODEC_ENCODE以获取不错的WebM视频?

  2. 我的RGB-> YV12转换在某个地方是否错误?

任何帮助将不胜感激。

有帮助吗?

解决方案

FreeFallr:当然。这是代码。请注意,它正在将RGB-> YUV转换为适当的位置,并将YV12输出放入plateplane/pdownsampleduplane/pdownsampledvplane中。当我修改其VPXENC示例以使用此数据时,该代码会产生漂亮的WebM视频。

void RGB_To_YV12( unsigned char *pRGBData, int nFrameWidth, int nFrameHeight, void *pFullYPlane, void *pDownsampledUPlane, void *pDownsampledVPlane )
{
    int nRGBBytes = nFrameWidth * nFrameHeight * 3;

    // Convert RGB -> YV12. We do this in-place to avoid allocating any more memory.
    unsigned char *pYPlaneOut = (unsigned char*)pFullYPlane;
    int nYPlaneOut = 0;

    for ( int i=0; i < nRGBBytes; i += 3 )
    {
        unsigned char B = pRGBData[i+0];
        unsigned char G = pRGBData[i+1];
        unsigned char R = pRGBData[i+2];

        float y = (float)( R*66 + G*129 + B*25 + 128 ) / 256 + 16;
        float u = (float)( R*-38 + G*-74 + B*112 + 128 ) / 256 + 128;
        float v = (float)( R*112 + G*-94 + B*-18 + 128 ) / 256 + 128;

        // NOTE: We're converting pRGBData to YUV in-place here as well as writing out YUV to pFullYPlane/pDownsampledUPlane/pDownsampledVPlane.
        pRGBData[i+0] = (unsigned char)y;
        pRGBData[i+1] = (unsigned char)u;
        pRGBData[i+2] = (unsigned char)v;

        // Write out the Y plane directly here rather than in another loop.
        pYPlaneOut[nYPlaneOut++] = pRGBData[i+0];
    }

    // Downsample to U and V.
    int halfHeight = nFrameHeight >> 1;
    int halfWidth = nFrameWidth >> 1;

    unsigned char *pVPlaneOut = (unsigned char*)pDownsampledVPlane;
    unsigned char *pUPlaneOut = (unsigned char*)pDownsampledUPlane;

    for ( int yPixel=0; yPixel < halfHeight; yPixel++ )
    {
        int iBaseSrc = ( (yPixel*2) * nFrameWidth * 3 );

        for ( int xPixel=0; xPixel < halfWidth; xPixel++ )
        {
            pVPlaneOut[yPixel * halfWidth + xPixel] = pRGBData[iBaseSrc + 2];
            pUPlaneOut[yPixel * halfWidth + xPixel] = pRGBData[iBaseSrc + 1];

            iBaseSrc += 6;
        }
    }
}

其他提示

没关系。我使用的方案是正确的,但是我在U/V下采样代码中有一个错误。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top