Advertisement

VP8 Video Encoding Glitch (RGB->YUV) Picture Included

Started by April 09, 2014 07:03 PM
0 comments, last by FrankGalligan 10 years, 7 months ago

0bdLq4l.png

Hello. I'm trying to implement a VP8 Encoder, I'm working with the libvpx codec.

I'm taking a RGB 24bit bitmap, converting the bytes into YV12 format and then passing those along to the VP8 Encoder. My output file plays, but the image is quite distorted. To me it obviously looks like a one-off error, or something with the stride length being incorrect.

Here's the conversion code. I have two functions, both found easily online for RGB->YUV and both produce the same results...


#define rgbtoy(b, g, r, y)   \
y = (unsigned char)(((int)(30 * r) + (int)(59 * g) + (int)(11 * b)) / 100)

#define rgbtoyuv(b, g, r, y, u, v)   \
rgbtoy(b, g, r, y);  \
u = (unsigned char)(((int)(-17 * r) - (int)(33 * g) + (int)(50 * b) + 12800) / 100); \
v = (unsigned char)(((int)(50 * r) - (int)(42 * g) - (int)(8 * b) + 12800) / 100)

namespace VP8 
{
   public ref class Converter
   {
   public:
    static void RGBtoYUV420PSameSize(const unsigned char *rgb, vpx_image_t* yuvImage,unsigned int rgbIncrement, unsigned int srcFrameWidth, unsigned int srcFrameHeight)
    {
            unsigned int planeSize = srcFrameWidth * srcFrameHeight;
            unsigned int halfWidth = srcFrameWidth >> 1;

            // get pointers to the data
            unsigned char* yplane = yuvImage->planes[VPX_PLANE_Y];
            unsigned char* uplane = yuvImage->planes[VPX_PLANE_U];
            unsigned char* vplane = yuvImage->planes[VPX_PLANE_V];

            for (unsigned int y = 0; y < srcFrameHeight; y++)
            {
                    unsigned char* yline = yplane + (y * srcFrameWidth);
                    unsigned char* uline = uplane + ((y >> 1) * halfWidth);
                    unsigned char* vline = vplane + ((y >> 1) * halfWidth);

                    for (unsigned  int x = 0; x < srcFrameWidth; x += 2)
                    {
                            rgbtoyuv(rgb[2], rgb[1], rgb[0], *yline, *uline, *vline);
                            rgb += rgbIncrement;
                            yline++;

                            rgbtoyuv(rgb[2], rgb[1], rgb[0], *yline, *uline, *vline);
                            rgb += rgbIncrement;
                            yline++;
                            uline++;
                            vline++;
                    }
                }

    static void ConvertRGB24ToI420(unsigned char *rgbData, vpx_image_t *yuvImage, int srcFrameWidth, int srcFrameHeight)
    {
       int rgb24Stride = srcFrameWidth * 3;

       int rgb1 = 0;
       int yi1 = 0;
       int uvi = 0;

        for (int ypos = 0; ypos < srcFrameHeight; ypos += 2)
        {
            for (int xpos = 0; xpos < srcFrameWidth; xpos += 2)
            {
                    int rgb2 = rgb1 + 3;
                    int rgb3 = rgb1 + rgb24Stride;
                    int rgb4 = rgb2 + rgb24Stride;

                    int ro = 0;
                    int go = 1;
                    int bo = 2;

                    int r1 = rgbData[rgb1+ro];
                    int g1 = rgbData[rgb1+go];
                    int b1 = rgbData[rgb1+bo];

                    int r2 = rgbData[rgb2+ro];
                    int g2 = rgbData[rgb2+go];
                    int b2 = rgbData[rgb2+bo];

                    int r3 = rgbData[rgb3+ro];
                    int g3 = rgbData[rgb3+go];
                    int b3 = rgbData[rgb3+bo];

                    int r4 = rgbData[rgb4+ro];
                    int g4 = rgbData[rgb4+go];
                    int b4 = rgbData[rgb4+bo];

                    int yi2 = yi1 + 1;
                    int yi3 = yi1 + srcFrameWidth;
                    int yi4 = yi3 + 1;

                    int Y1 = getY(r1, g1, b1);
                    int Y2 = getY(r2, g2, b2);
                    int Y3 = getY(r3, g3, b3);
                    int Y4 = getY(r4, g4, b4);

                    yuvImage->planes[VPX_PLANE_Y][yi1] = (unsigned char)Y1;
                    yuvImage->planes[VPX_PLANE_Y][yi2] = (unsigned char)Y2;
                    yuvImage->planes[VPX_PLANE_Y][yi3] = (unsigned char)Y3;
                    yuvImage->planes[VPX_PLANE_Y][yi4] = (unsigned char)Y4;

                    int u1 = getU(r1, g1, b1, Y1);
                    int u2 = getU(r2, g2, b2, Y2);
                    int u3 = getU(r3, g3, b3, Y3);
                    int u4 = getU(r4, g4, b4, Y4);

                    int v1 = getV(r1, g1, b1, Y1);
                    int v2 = getV(r2, g2, b2, Y2);
                    int v3 = getV(r3, g3, b3, Y3);
                    int v4 = getV(r4, g4, b4, Y4);

                    yuvImage->planes[VPX_PLANE_U][uvi] = (unsigned char)((u1 + u2 + u3 + u4) / 4.0f);
                    yuvImage->planes[VPX_PLANE_V][uvi] = (unsigned char)((v1 + v2 + v3 + v4) / 4.0f);

                    rgb1 += 6;
                    yi1 += 2;
                    uvi += 1;
            }
            rgb1 += rgb24Stride;
            yi1 += srcFrameWidth;
       }
   }
private:
   static inline  int getR(int y, int u, int v)
   {
       return clamp(y + (int) (1.402f * (v - 128)));
   }    
   static inline int getG(int y, int u, int v)
   {
       return clamp(y - (int) (0.344f * (u - 128) + 0.714f * (v - 128)));
   }    
   static inline int getB(int y, int u, int v)
   {
       return clamp(y + (int) (1.772f * (u - 128)));
   }        
   static inline int getY(int r, int g, int b)
   {
       return (int) (0.299f * r + 0.587f * g + 0.114f * b);
   }
   static inline int getU(int r, int g, int b, int y)
   {
    //return (int)((b - y)*0.565f);
       return (int) (-0.169f * r - 0.331f * g + 0.499f * b + 128);
   }    
    static inline int getV(int r, int g, int b, int y)
   {
    //return (int)((r - y)*0.713f);
       return (int) (0.499f * r - 0.418f * g - 0.0813f * b + 128);
   }    
   static inline int clamp(int value)
   {
       return Math::Min(Math::Max(value, 0), 255);
   }
}

Try changing these lines to use the YUV image stride:

unsigned char* yline = yplane + (y * yuvImage->stride[VPX_PLANE_Y]);
unsigned char* uline = uplane + (y * yuvImage->stride[VPX_PLANE_U]);
unsigned char* vline = vplane + (y * yuvImage->stride[VPX_PLANE_V]);

This topic is closed to new replies.

Advertisement