RGB与YUV的转换
文章信息
兼容于
平台: S60 3rd Edition, S60 3rd Edition FP 1, S60 3rd Edition FP 2, S60 5th Edition
文章
翻译:
由 huwell
最后由 hamishwillee
在 09 Aug 2012 编辑
- 详细描述
从S60第三版FP2开始,MDF DevVideoRecord API作为SDK API Plug-in的一部分出现了。DevVideoRecord允许对视频解码和预处理进行直接和底层的访问。
传输到解码器的必须是特殊的颜色格式,YUV422或更常见的YUV420(planar),YUV420每个像素有8位luminance (Y),和减少数量的chrominance(U,V)采样,每个都覆盖一个2x2像素区域。在plane模式下,Y,U和V值在内存中被分组为[Y0Y1Y2Y3Y4Y5Y6Y7....U0U1....V0V1],这样增强了图片的压缩率.
- 解决方案
下列代码演示了如何将一帧原始RGB(8 bits/channel)转化为YUV420格式。
// Definitions that help access each colour component in source bitmap
#define sR ((TInt32)(s[2]))
#define sG ((TInt32)(s[1]))
#define sB ((TInt32)(s[0]))
#define KImageWidth 176 // QCIF resolution
#define KImageHeigth 144
const TInt KImageNumPixels = KImageWidth * KImageHeigth;
// Lock source bitmap (CFbsBitmap)
iSourceBitmap->LockHeap(EFalse);
TUint8* s = (TUint8*)iSourceBitmap->DataAddress();
TInt i = 0;
TInt ui = KImageNumPixels;
TInt vi = KImageNumPixels + KImageNumPixels/4;
// iYuv is an array of TUint8 values, length (KImageNumPixels*3/2)
for(TInt j=0; j < KImageHeigth; j++)
for(TInt k=0; k < KImageWidth; k++)
{
// Y value is generated for each pixel
iYuv[i] = (TUint8)( ( 66*sR + 129*sG + 25*sB + 128) >> 8 ) + 16;
// U, V values are generated for every other pixel on every other scanline
if(0 == j%2 && 0 == k%2)
{
iYuv[ui++] = (TUint8)( (-38*sR - 74*sG + 112*sB + 128) >> 8 ) + 128;
iYuv[vi++] = (TUint8)( (112*sR - 94*sG - 18*sB + 128) >> 8 ) + 128;
}
i++;
s+=iBytesPerPixel; // Number of bytes representing one pixel in source
// bitmap e.g. if bitmap display mode == EColor16M
// (24bits/pixel), then iBytesPerPixel == 3
}
iSourceBitmap->UnlockHeap(EFalse);
// iYuv now contains the source frame converted to YUV420p format
注意:最新S60设备有多个DevVideoRecord解码用来区分H263和MPEG-4(VSP)编码能力,是硬件加速还是基于软件的编码。一些编码也支持直接从相机捕捉。编码器及能力以及支持的输入格式可以通过编程列出,并选择一个适当的编码器。


(no comments yet)