2017-04-26 29 views
2

NV12 yuvをRGBに変換するコードを以下に書きますが、色が正しくありません。 yuv2rgb.rsアンドロイド - NV12 yuvをRGBに変換するレンダスクリプト

#pragma version(1) 
#pragma rs java_package_name(com.example.myexam) 
#pragma rs_fp_relaxed 

rs_allocation gYUV; 
uint32_t gW; 
uint32_t gH; 

uchar4 __attribute__((kernel)) YUV2RGB(uint32_t x,uint32_t y) 
{ 
    uchar yps = rsGetElementAt_uchar(gYUV, x, y); 
    uchar u = rsGetElementAt_uchar(gYUV,(x & ~1),gH + (y>>1)); 
    uchar v = rsGetElementAt_uchar(gYUV,(x & ~1)+1,gH + (y>>1)); 
    uchar4 rgb = rsYuvToRGBA_uchar4(yps, u, v); 
    return rgb; 
} 

Javaコード:私は(x、y)を推測

public Bitmap NV12_toRGB(byte[] yuv,int W,int H) { 
    RenderScript rs = RenderScript.create(this); 
    Type.Builder yuvBlder = new Type.Builder(rs, Element.U8(rs)) 
      .setX(W).setY(H*3/2); 
    Allocation allocIn = Allocation.createTyped(rs,yuvBlder.create(),Allocation.USAGE_SCRIPT); 
    Type rgbType = Type.createXY(rs, Element.RGBA_8888(rs), W, H); 
    Allocation allocOut = Allocation.createTyped(rs,rgbType,Allocation.USAGE_SCRIPT); 

    ScriptC_yuv2rgb scriptC_yuv2rgb = new ScriptC_yuv2rgb(rs); 
    scriptC_yuv2rgb.set_gW(W); 
    scriptC_yuv2rgb.set_gH(H); 
    allocIn.copyFrom(yuv); 
    scriptC_yuv2rgb.set_gYUV(allocIn); 
    scriptC_yuv2rgb.forEach_YUV2RGB(allocOut); 

    Bitmap bmp = Bitmap.createBitmap(W, H, Bitmap.Config.ARGB_8888); 
    allocOut.copyTo(bmp); 

    allocIn.destroy(); 
    scriptC_yuv2rgb.destroy(); 
    return bmp; 
} 

yが(x、y)にあるべきであるので、行列Uは(であるべきで、調整され(x/2)* 2、H + y/2)、vはuの次になければならない((x/2)* 2 + 1、H + y/2)。この論理のような音は間違っています!

答えて

2

2つのエラーが修正される必要があります。

  1. -1そうでも値を保つのx & -1 xにちょうど等しいので、〜1にする必要がありますが、X &〜1は、最後のビットをマスクします。
  2. yuv行列のサイズが正しくありません。 uvベクトルはyデータの最後に格納されるので、合計マトリックスサイズはW * H * 3/2でなければなりません。

これらの2つの変更を適用した後、正常に動作します。 のjava:

public Bitmap YUV_toRGB(byte[] yuv,int W,int H) { 
     RenderScript rs = RenderScript.create(this); 
     Type.Builder yuvBlder = new Type.Builder(rs, Element.U8(rs)) 
       .setX(W).setY(H*3/2); 
     Allocation allocIn = Allocation.createTyped(rs,yuvBlder.create(),Allocation.USAGE_SCRIPT); 
     Type rgbType = Type.createXY(rs, Element.RGBA_8888(rs), W, H); 
     Allocation allocOut = Allocation.createTyped(rs,rgbType,Allocation.USAGE_SCRIPT); 

     ScriptC_yuv2rgb scriptC_yuv2rgb = new ScriptC_yuv2rgb(rs); 
     allocIn.copyFrom(yuv); 
     scriptC_yuv2rgb.set_gW(W); 
     scriptC_yuv2rgb.set_gH(H); 
     scriptC_yuv2rgb.set_gYUV(allocIn); 
     scriptC_yuv2rgb.forEach_YUV2RGB(allocOut); 

     Bitmap bmp = Bitmap.createBitmap(W, H, Bitmap.Config.ARGB_8888); 
     allocOut.copyTo(bmp); 

     allocIn.destroy(); 
     scriptC_yuv2rgb.destroy(); 
     return bmp; 
    } 

yuv2rgb.rs

#pragma version(1) 
#pragma rs java_package_name(com.example.myexam) 
#pragma rs_fp_relaxed 

rs_allocation gYUV; 
uint32_t gW; 
uint32_t gH; 

uchar4 __attribute__((kernel)) YUV2RGB(uint32_t x,uint32_t y) 
{ 
    uchar yps = rsGetElementAt_uchar(gYUV, x, y); 
    uchar u = rsGetElementAt_uchar(gYUV,(x & ~1),gH + (y>>1)); 
    uchar v = rsGetElementAt_uchar(gYUV,(x & ~1)+1,gH + (y>>1)); 
    uchar4 rgb = rsYuvToRGBA_uchar4(yps, u, v); 
    return rgb; 
} 
+0

おっと、-1は0xFFFFFFFFを意味するので、&-1等しい何もしないこと。 – lucky1928

関連する問題