我的视频数据是YUV420p(I420)。我尝试使用C语言转换为RGBA,但我不知道我的代码的哪一部分是错误的,我无法获得正确的RGBA数据。
const void *data = [frameData bytes];
NSInteger numOfPixel = width * height;
NSInteger positionOfV = numOfPixel;
NSInteger positionOfU = numOfPixel * 5 / 4;
uint8_t *rgba = malloc(numOfPixel * 4);
for (uint32_t i = 0; i < height; i ++) {
NSInteger startY = i * width;
NSInteger step = (i/2)*(width/2);
NSInteger startU = positionOfU + step;
NSInteger startV = positionOfV + step;
for (uint32_t j = 0; j < width; j ++) {
NSInteger indexY = startY + j;
NSInteger indexU = startU + j/2;
NSInteger indexV = startV + j/2;
NSInteger index = indexY * 4;
uint8_t Y = *(uint8_t *)(&data[indexY]);
uint8_t U = *(uint8_t *)(&data[indexU]);
uint8_t V = *(uint8_t *)(&data[indexV]);
int r = (int)((Y&0xff) + 1.4075 * ((V&0xff) - 128));
int g = (int)((Y&0xff) - 0.3455 * ((U&0xff)-128) - 0.7169*((V&0xff)-128));
int b = (int)((Y&0xff) + 1.779 * ((U&0xff)-128));
int a = 0xff;
if (r<0) r=0; if (r>255) r=255;
if (g<0) g=0; if (g>255) g=255;
if (b<0) b=0; if (b>255) b=255;
rgba[index] = (uint8_t)r;
rgba[index + 1] = (uint8_t)g;
rgba[index + 2] = (uint8_t)b;
rgba[index + 3] = (uint8_t)a;
}
}