YUV顏色編碼解析

轉自http://www.lxweimin.com/p/a91502c00fb0

YUV

YUV是一種顏色空間,基于YUV的顏色編碼是流媒體的常用編碼方式。Y表示流明,U、V表示色度、濃度,這種表達方式起初是為了彩色電視與黑白電視之間的信號兼容。 對于圖像每一點,Y確定其亮度,UV確認其彩度。

Y'CbCr也稱為YUV,是YUV的壓縮版本,不同之處在于Y'CbCr用于數字圖像領域,YUV用于模擬信號領域,MPEG、DVD、攝像機中常說的YUV其實是Y'CbCr,二者轉換為RGBA的轉換矩陣是不同的。Y'為亮度,Cb、Cr分量代表當前顏色對藍色和紅色的偏移程度。

Y'=0.5時,Cb、Cr構成的顏色平面

如果輸出Y'CbCr三個分量的值,那么會是這樣的。

由上到下依次為Y'、Cb、Cr

為了方便,以下文中YUV特指Y'CbCr。

YUV顏色編碼的作用

YUV編碼是image/video pipeline的重要組成。比如常用的I420相對于RGB24(RGB三個分量各8個字節)的編碼格式,只需要一半的存儲容量。在流數據傳輸時降低了帶寬壓力。

YUV顏色編碼在video pipeline中的運用

YUV顏色編碼格式

YUV色彩編碼格式由其色度抽樣方式和存儲方式決定。

色度抽樣方式

色度抽樣方式用J:A:B表示

J:最小水平抽樣的的寬度,一般為4

A:最小水平抽樣區域第一行的色度抽樣

B:最小水平抽樣區域第二行的色度抽樣

注意4:2:0并不是只抽樣第一行的色度,是第一行和第二行輪番抽樣的:4:2:0 -->? 4:0:2 --> 4:2:0 ……

可以看到,不管是哪種抽樣方式,亮度都是全抽樣的,不同之處在于U、V分量的抽樣率。可以看到常用的4:2:0的U、V都是半抽樣,所以抽樣后的數據量是RGB24一半。(RGB24相當于全抽樣)

YUV存儲方式

YUV存儲方式主要分為兩種:Packeted 和 Planar。

Packeted方式類似RGB的存儲方式,以像素矩陣為存儲方式。

Planar方式將YUV分量分別存儲到矩陣,每一個分量矩陣稱為一個平面。

YUV420即以平面方式存儲,色度抽樣為4:2:0的色彩編碼格式。其中YUV420P為三平面存儲,YUV420SP為兩平面存儲。

常用的I420(YUV420P),NV12(YUV420SP),YV12(YUV420P),NV21(YUV420SP)等都是屬于YUV420,NV12是一種兩平面存儲方式,Y為一個平面,交錯的UV為另一個平面。

由此,I420就是存儲方式為Planar,抽樣方式為4:2:0,數據組成為YYYYYYYYUUVV的一種色彩編碼格式。

除此之外,NV12的數據組成:YYYYYYYYUVUV 。YV12的數據組成:YYYYYYYYVVUU。NV21的數據組成:YYYYYYYYVUVU。

通常,用來遠程傳輸的是I420數據,而本地攝像頭采集的是NV12數據。(iOS)

I420多用于傳輸

攝像頭采集得到的數據是NV12

YUV與RGB之間的轉換

在渲染時,不管是OpenGL還是iOS,都不支持直接渲染YUV數據,底層都是轉為RGB。

//RGB --> YUV

Y = 0.299 R + 0.587 G + 0.114 B

U = - 0.1687 R - 0.3313 G + 0.5 B + 128

V = 0.5 R - 0.4187 G - 0.0813 B + 128

//YUV --> RGB

//由于U、V可能出現負數,單存儲為了方便就用一個字節表示:0-255,讀取時要-128回歸原值。

R = Y + 1.402 (Cr-128)

G = Y - 0.34414 (Cb-128) - 0.71414 (Cr-128)

B = Y + 1.772 (Cb-128)

YUV數據渲染

以NV12為例:

void convertNv12ToRgb(unsigned char *rgbout, unsigned char *pdata,int DataWidth,int DataHeight)

{

unsigned long? idx=0;

unsigned char *ybase,*ubase;

unsigned char y,u,v;

ybase = pdata; //獲取Y平面地址

ubase = pdata+DataWidth * DataHeight; //獲取U平面地址,由于NV12中U、V是交錯存儲在一個平民的,v是u+1

for(int j=0;j

{

idx=(DataHeight-j-1)*DataWidth*3;//該值保證所生成的rgb數據逆序存放在rgbbuf中,位圖是底朝上的

for(int i=0;i

{

unsigned char r,g,b;

y=ybase[i + j? * DataWidth];//一個像素對應一個y

u=ubase[j/2 * DataWidth+(i/2)*2];// 每四個y對應一個uv

v=ubase[j/2 * DataWidth+(i/2)*2+1];? //一定要注意是u+1

b=(unsigned char)(y+1.779*(u- 128));

g=(unsigned char)(y-0.7169*(v - 128)-0.3455*(u - 128));

r=(unsigned char)(y+ 1.4075*(v - 128));

rgbout[idx++]=b;

rgbout[idx++]=g;

rgbout[idx++]=r;

}

}

}

有時不同的YUV格式需要互相轉換

unsigned char* convertNV12ToI420(unsigned char *data , int dataWidth, int dataHeight){

unsigned char *ybase,*ubase;

ybase = data;

ubase = data + dataWidth*dataHeight;

unsigned char* tmpData = (unsigned char*)malloc(dataWidth*dataHeight * 1.5);

int offsetOfU = dataWidth*dataHeight;

int offsetOfV = dataWidth*dataHeight* 5/4;

memcpy(tmpData, ybase, dataWidth*dataHeight);

for (int i = 0; i < dataWidth*dataHeight/2; i++) {

if (i % 2 == 0) {

tmpData[offsetOfU] = ubase[i];

offsetOfU++;

}else{

tmpData[offsetOfV] = ubase[i];

offsetOfV++;

}

}

free(data);

return tmpData;

}

或者需要旋轉獲得的數據

void rotate90NV12(unsigned char *dst, const unsigned char *src, int srcWidth, int srcHeight)

{

int wh = srcWidth * srcHeight;

int uvHeight = srcHeight / 2;

int uvWidth = srcWidth / 2;

//旋轉Y

int i = 0, j = 0;

int srcPos = 0, nPos = 0;

for(i = 0; i < srcHeight; i++) {

nPos = srcHeight - 1 - i;

for(j = 0; j < srcWidth; j++) {

dst[j * srcHeight + nPos] = src[srcPos++];

}

}

srcPos = wh;

for(i = 0; i < uvHeight; i++) {

nPos = (uvHeight - 1 - i) * 2;

for(j = 0; j < uvWidth; j++) {

dst[wh + j * srcHeight + nPos] = src[srcPos++];

dst[wh + j * srcHeight + nPos + 1] = src[srcPos++];

}

}

}

void rotate270YUV420sp(unsigned char *dst, const unsigned char *src, int srcWidth, int srcHeight)

{

int nWidth = 0, nHeight = 0;

int wh = 0;

int uvHeight = 0;

if(srcWidth != nWidth || srcHeight != nHeight)

{

nWidth = srcWidth;

nHeight = srcHeight;

wh = srcWidth * srcHeight;

uvHeight = srcHeight >> 1;//uvHeight = height / 2

}

//旋轉Y

int k = 0;

for(int i = 0; i < srcWidth; i++){

int nPos = srcWidth - 1;

for(int j = 0; j < srcHeight; j++)

{

dst[k] = src[nPos - i];

k++;

nPos += srcWidth;

}

}

for(int i = 0; i < srcWidth; i+=2){

int nPos = wh + srcWidth - 1;

for(int j = 0; j < uvHeight; j++) {

dst[k] = src[nPos - i - 1];

dst[k + 1] = src[nPos - i];

k += 2;

nPos += srcWidth;

}

}

}

在iOS中,可以使用core graphics將RGB數據畫成UIImage。

- (UIImage *) convertBitmapRGBA8ToUIImage:(unsigned char *) buffer

withWidth:(int) width

withHeight:(int) height {

//轉為RGBA32

char* rgba = (char*)malloc(width*height*4);

for(int i=0; i < width*height; ++i) {

rgba[4*i] = buffer[3*i];

rgba[4*i+1] = buffer[3*i+1];

rgba[4*i+2] = buffer[3*i+2];

rgba[4*i+3] = 255;

}

size_t bufferLength = width * height * 4;

CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, rgba, bufferLength, NULL);

size_t bitsPerComponent = 8;

size_t bitsPerPixel = 32;

size_t bytesPerRow = 4 * width;

CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();

if(colorSpaceRef == NULL) {

NSLog(@"Error allocating color space");

CGDataProviderRelease(provider);

return nil;

}

CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast;

CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

CGImageRef iref = CGImageCreate(width,

height,

bitsPerComponent,

bitsPerPixel,

bytesPerRow,

colorSpaceRef,

bitmapInfo,

provider,? // data provider

NULL,? ? ? // decode

YES,? ? ? ? ? ? // should interpolate

renderingIntent);

uint32_t* pixels = (uint32_t*)malloc(bufferLength);

if(pixels == NULL) {

NSLog(@"Error: Memory not allocated for bitmap");

CGDataProviderRelease(provider);

CGColorSpaceRelease(colorSpaceRef);

CGImageRelease(iref);

return nil;

}

CGContextRef context = CGBitmapContextCreate(pixels,

width,

height,

bitsPerComponent,

bytesPerRow,

colorSpaceRef,

bitmapInfo);

if(context == NULL) {

NSLog(@"Error context not created");

free(pixels);

}

UIImage *image = nil;

if(context) {

CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, width, height), iref);

CGImageRef imageRef = CGBitmapContextCreateImage(context);

image = [UIImage imageWithCGImage:imageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];

if([UIImage respondsToSelector:@selector(imageWithCGImage:scale:orientation:)]) {

image = [UIImage imageWithCGImage:imageRef scale:1.0 orientation:UIImageOrientationUp];

} else {

image = [UIImage imageWithCGImage:imageRef];

}

CGImageRelease(imageRef);

CGContextRelease(context);

}

CGColorSpaceRelease(colorSpaceRef);

CGImageRelease(iref);

CGDataProviderRelease(provider);

if(pixels) {

free(pixels);

}

return image;

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容