首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将YUV422转换为YUV420

如何将YUV422转换为YUV420
EN

Stack Overflow用户
提问于 2015-07-15 16:17:01
回答 2查看 4K关注 0票数 3

我想在H.264格式的相机预览中获得数据,普通相机isYv12,这样我就可以使用:

代码语言:javascript
复制
 public byte[] YV12toYUV420PackedSemiPlanar(final byte[] input, final byte[] output, final int width, final int height) 
{
    this.halfWidth = width / 2;
    this.halfHeight = height / 2;
    this.frameRate = framerate;

    this.yStride = (int) Math.ceil(width/16.0f) * 16;
    this.cStride = (int) Math.ceil(width/32.0f)  * 16;
    this.ySize = yStride * height;
    this.cSize = cStride * height / 2;

    for (int i=0; i<height; i++)
        System.arraycopy(input, yStride*i, output, yStride*i, yStride); // Y

    for (int i=0; i<halfHeight; i++) 
    {
        for (int j=0; j<halfWidth; j++)
        {
            output[ySize + (i*halfWidth + j)*2] = input[ySize + cSize + i*cStride + j]; // Cb (U)
            output[ySize + (i*halfWidth + j)*2 + 1] = input[ySize + i*cStride + j]; // Cr (V)
        }
    }
    return output;
}

但是现在我的相机预览版是yuv422,所以如何用java把YUV422转换成YUV420,我找了一些c语言的方法和理论,但是我不能成功地转换它。

我用过这个:我说得对吗?

代码语言:javascript
复制
static void yuyv_to_yuv420PackedSemiPlanar( byte[] yuyv, byte[] yuv, int w, int h ) {

    int expected_length;
    int actual_length;

    actual_length = yuyv.length;
    expected_length = PixelFormat.V4L2_PIX_FMT_YUYV.frameSize( w, h );
    if ( actual_length < expected_length ) {
        throw new IllegalArgumentException( "invalid input buffer length " + actual_length + " for yuyv image of size ( " + w + ", " + h + " ). Should be " + expected_length  );
    }

    actual_length = yuv.length;
    expected_length = PixelFormat.V4L2_PIX_FMT_RGB24.frameSize( w, h );
    if ( actual_length < expected_length ) {
        throw new IllegalArgumentException( "invalid output buffer length " + actual_length + " for yuv image of size ( " + w + ", " + h + " ). Should be " + expected_length );
    }

    for( int i = 0, j = 0; i < w * h * 3; i += 6, j += 4 ) {
        yuv[ i + 0 ] = yuyv[ j + 0 ];
        yuv[ i + 1 ] = yuyv[ j + 1 ];
        yuv[ i + 2 ] = yuyv[ j + 3 ];
        yuv[ i + 3 ] = yuyv[ j + 2 ];
        yuv[ i + 4 ] = yuyv[ j + 1 ];
        yuv[ i + 5 ] = yuyv[ j + 3 ];
    }
}
EN

回答 2

Stack Overflow用户

发布于 2015-11-04 11:13:40

这是一个从YUV422 (yuyv,V4L2_PIX_FMT_YUYV,交错格式)到YUV420SP (NV21,V4L2_PIX_FMT_NV21,半平面格式)转换的代码示例。

代码语言:javascript
复制
struct pixel_yvuv {
    uint8_t Y0;
    uint8_t V;
    uint8_t Y1;
    uint8_t U;
};

struct pixel_NV21_y_plane{
    uint8_t Y0;
    uint8_t Y1;
    uint8_t Y2;
    uint8_t Y3;
};


struct pixel_NV21_uv_plane{
    uint8_t U;
    uint8_t V;
};

/*
SRC
-------
Y0V0Y1U1
Y2V1Y3U1
....
....
....
....

DST
-------
Y0Y1Y2Y3
...
...
U0V0
...
*/

void
yuyv422_to_yuv420sp(unsigned char *bufsrc, unsigned char *bufdest, int     width, int height, bool flipuv)
{
    int i,j;

    volatile struct pixel_yvuv* src;
    volatile struct pixel_NV21_y_plane* dst_p1;
    volatile struct pixel_NV21_uv_plane* dst_p2;

    src = (struct pixel_yvuv*)bufsrc;
    /* plane 1 */
    dst_p1 = (struct pixel_NV21_y_plane*) bufdest;
    /* plane 2 */
    dst_p2 = (struct pixel_NV21_uv_plane*) (bufdest + (height*width)); /* offset to UV plane */


    for(i=1; i<=height; i++)
    {
            for(j=1; j<=width/2; j++)
            {
                    if(j%2) {
                            dst_p1->Y0 = src->Y0;
                            dst_p1->Y1 = src->Y1;
                    } else {
                            dst_p1->Y2 = src->Y0;
                            dst_p1->Y3 = src->Y1;
                            dst_p1++;


                    }
                    /* vertical subsampling for U and V plane */
                    if(i%2) {
                            /* U and V  Plane */
                            if(flipuv) {
                                    dst_p2->U  = src->U;
                                    dst_p2->V  = src->V;
                            } else {
                                    dst_p2->U  = src->V;
                                    dst_p2->V  = src->U;
                            }
                            dst_p2++;
                    }
                    src++;
            }
    }
}
票数 1
EN

Stack Overflow用户

发布于 2015-07-15 16:51:18

你的代码是错误的。

  • 请务必了解YUV422、YUV420和YUV420SP(NV12)等YUV数据的排列方式。注意stride的含义。

YUV format

  • 了解YUV映射到颜色这是调试帮助。

yuv_rgb

  • 找到一个工具来显示YUV原始数据。将数据写入二进制文件,然后进行检查。

yuv-player

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31425033

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档