Android-android ColorConverter 中 TI NV12 是什么样的颜色格式

Android-android ColorConverter 中 TI NV12 是什么样的颜色格式

偏爱自由 发布于 2017-05-06 字数 4104 浏览 1316 回复 1

android stagefright下的ColorConverter.cpp是native进行颜色转换的代码,android 4.0 中有这么一种颜色格式OMX_TI_COLOR_FormatYUV420PackedSemiPlanar ,是TI 的NV12。
现在项目中会用这种颜色格式,可是弄了半天都没弄懂是什么颜色格式,也没能找到TI 官方的说明。请问各位这种格式是怎样的.贴上源代码供参考。尤其是u分量的计算是我费解的地方

status_t ColorConverter::convertTIYUV420PackedSemiPlanar(
const BitmapParams &src, const BitmapParams &dst)
{

/*
The TIYUV420PackedSemiPlanar format is same as YUV420PackedSemiPlanar but with
additional padding as shown in the diagram below. The padded width and padded
height is different for different compression formats and it is read from the
codec. In this color conversion routine, the padded resolution is obtained from
src bitmap.





| |
| |
| ------------------------- |
| | | |
| | | |
| | Y DATA | |
| | | |
| | | |
| | | |
| ------------------------- |
| |
| ------------ |
| | | |
| | | |
| | UV DATA | |
| | | |
| | | |
| | | |
| ------------ |
| |
| |




*/

LOGV("src.mCropLeft = %d src.mCropTop =%d src.mWidth = %d src.mHeight = %d"
" dst.mWidth = %d dst.mHeight = %d", src.mCropLeft , src.mCropTop,
src.mWidth, src.mHeight, dst.mWidth, dst.mHeight);

size_t offset = (src.mWidth * src.mCropTop) + src.mCropLeft;

uint8_t *kAdjustedClip = initClip();

uint32_t *dst_ptr = (uint32_t *)dst.mBits;
const uint8_t *src_y = (const uint8_t *)src.mBits;
const uint8_t *src_u = (const uint8_t *)(src_y-offset) + (src.mWidth * src.mHeight);
src_u += ( ( src.mWidth * (src.mCropTop/2) ) + src.mCropLeft );
const uint8_t *src_v = src_u + 1;

for (size_t y = 0; y < dst.mHeight; ++y) {
for (size_t x = 0; x < dst.mWidth; x += 2) {

signed y1 = (signed)src_y[x] - 16; //Y pixel
signed y2 = (signed)src_y[x + 1] - 16; //2nd Y pixel

signed u = (signed)src_u[x & ~1] - 128; //U component
signed v = (signed)src_u[(x & ~1) + 1] - 128; //V component

signed u_b = u * 517;
signed u_g = -u * 100;
signed v_g = -v * 208;
signed v_r = v * 409;

signed tmp1 = y1 * 298;
signed b1 = (tmp1 + u_b) / 256;
signed g1 = (tmp1 + v_g + u_g) / 256;
signed r1 = (tmp1 + v_r) / 256;

signed tmp2 = y2 * 298;
signed b2 = (tmp2 + u_b) / 256;
signed g2 = (tmp2 + v_g + u_g) / 256;
signed r2 = (tmp2 + v_r) / 256;

uint32_t rgb1 =
((kAdjustedClip[r1] >> 3) << 11)
| ((kAdjustedClip[g1] >> 2) << 5)
| (kAdjustedClip[b1] >> 3);

uint32_t rgb2 =
((kAdjustedClip[r2] >> 3) << 11)
| ((kAdjustedClip[g1] >> 2) << 5)
| (kAdjustedClip[b1] >> 3);

dst_ptr[x / 2] = (rgb2 << 16) | rgb1;
}

src_y += src.mWidth; //increment Y-pixel line
if (y&1) {
src_u += src.mWidth; //increment U-V line
}

dst_ptr += dst.mWidth / 2;
}
return OK;
}

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜无邪 2017-09-04 1 楼

调QQ视频吧,以前遇到过,qq视频时色彩不对,红的变蓝的。
后来在camera加了个转换算法就行了。
NV12的格式:
yy yy yy yy
yy yy yy yy
...

UV UV UV UV,一对UV对应4个Y.
TI确实很坑爹~~~~~~~~~~