需要从RGB帧创建webm视频

时间:2011-01-22 00:57:02

标签: rgb video-encoding yuv webm libvpx

我有一个应用程序可以生成一堆jpgs,我需要将其变成webm视频。我正在尝试将jggs中的rgb数据转换为vpxenc示例。我可以在输出视频中看到原始jpgs的基本形状,但是所有内容都是绿色的(即使是应该是黑色的像素大约是绿色的一半),而且每个其他扫描线都有一些垃圾。

我正在尝试提供VPX_IMG_FMT_YV12数据,我假设其结构如此:

每帧        8位Y数据        每个2x2 V块的8位平均值        每个2x2 U块的8位平均值

以下是源图片和即将发布的视频的屏幕截图:

Images

我完全有可能错误地进行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;

..然后为我写入vpxenc的U和V产生2x2滤波值,我只做(a + b + c + d)/ 4,其中a,b,c,d是U或每个2x2像素块的V值。

所以我想知道:

  1. 是否有更简单的方法(在代码中)获取RGB数据并将其提供给vpx_codec_encode以获得精彩的网络视频?

  2. 我的RGB-> YV12转换错误了吗?

  3. 非常感谢任何帮助。

2 个答案:

答案 0 :(得分:5)

freefallr:当然。这是代码。请注意,它正在转换RGB-> YUV以及将YV12输出放入pFullYPlane / 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;
        }
    }
}

答案 1 :(得分:0)

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

相关问题