android - 将 NV12 yuv 转换为 RGB 的渲染脚本
android - Renderscript to convert NV12 yuv to RGB
我写了下面的代码来将 NV12 yuv 转换为 RGB,但是颜色不正确。
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;
}
java代码:
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;
}
我猜(x,y)是矩阵坐标,所以y应该在(x,y),u应该在((x/2)*2,H + y/2),v 应该紧挨着 u, ((x/2)*2+1,H + y/2).听起来这个逻辑是错误的!
两个错误需要修正:
- -1 应该是 ~1,因为 x & -1 刚好等于 x,但是 x & ~1 会屏蔽掉最后一位,所以保持值均匀。
- yuv 矩阵大小不正确。由于 uv 向量存储在 y 数据的末尾,因此总矩阵大小应为 W*H*3/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;
}
我写了下面的代码来将 NV12 yuv 转换为 RGB,但是颜色不正确。 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;
}
java代码:
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;
}
我猜(x,y)是矩阵坐标,所以y应该在(x,y),u应该在((x/2)*2,H + y/2),v 应该紧挨着 u, ((x/2)*2+1,H + y/2).听起来这个逻辑是错误的!
两个错误需要修正:
- -1 应该是 ~1,因为 x & -1 刚好等于 x,但是 x & ~1 会屏蔽掉最后一位,所以保持值均匀。
- yuv 矩阵大小不正确。由于 uv 向量存储在 y 数据的末尾,因此总矩阵大小应为 W*H*3/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;
}