📄 vconvert.cxx
字号:
return GreytoYUV420P(srcFrameBuffer, dstFrameBuffer, bytesReturned);
}
PSTANDARD_COLOUR_CONVERTER(RGB24,YUV420P)
{
return RGBtoYUV420P(srcFrameBuffer, dstFrameBuffer, bytesReturned, 3, 0, 2);
}
PSTANDARD_COLOUR_CONVERTER(BGR24,YUV420P)
{
return RGBtoYUV420P(srcFrameBuffer, dstFrameBuffer, bytesReturned, 3, 2, 0);
}
PSTANDARD_COLOUR_CONVERTER(RGB32,YUV420P)
{
return RGBtoYUV420P(srcFrameBuffer, dstFrameBuffer, bytesReturned, 4, 0, 2);
}
PSTANDARD_COLOUR_CONVERTER(BGR32,YUV420P)
{
return RGBtoYUV420P(srcFrameBuffer, dstFrameBuffer, bytesReturned, 4, 2, 0);
}
/*
* Format YUY2 or YUV422(non planar):
*
* off: 0 Y00 U00 Y01 V00 Y02 U01 Y03 V01
* off: 8 Y10 U10 Y11 V10 Y12 U11 Y13 V11
* off:16 Y20 U20 Y21 V20 Y22 U21 Y23 V21
* off:24 Y30 U30 Y31 V30 Y32 U31 Y33 V31
* length:32 bytes
*
* Format YUV420:
* off: 00 Y00 Y01 Y02 Y03
* off: 04 Y10 Y11 Y12 Y13
* off: 08 Y20 Y21 Y22 Y23
* off: 12 Y30 Y31 Y32 Y33
* off: 16 U00 U02 U20 U22
* off: 20 V00 V02 V20 V22
*
* So, we loose some bit of information when converting YUY2 to YUV420
*
* NOTE: This algorithm works only if the width and the height is pair.
*/
void PStandardColourConverter::YUY2toYUV420PSameSize(const BYTE *yuy2, BYTE *yuv420p) const
{
const BYTE *s;
BYTE *y, *u, *v;
unsigned int x, h;
int npixels = srcFrameWidth * srcFrameHeight;
s = yuy2;
y = yuv420p;
u = yuv420p + npixels;
v = u + npixels/4;
for (h=0; h<srcFrameHeight; h+=2) {
/* Copy the first line keeping all information */
for (x=0; x<srcFrameWidth; x+=2) {
*y++ = *s++;
*u++ = *s++;
*y++ = *s++;
*v++ = *s++;
}
/* Copy the second line discarding u and v information */
for (x=0; x<srcFrameWidth; x+=2) {
*y++ = *s++;
s++;
*y++ = *s++;
s++;
}
}
}
PSTANDARD_COLOUR_CONVERTER(YUY2,YUV420P)
{
const BYTE *yuy2 = srcFrameBuffer;
BYTE *yuv420p = dstFrameBuffer;
if ((srcFrameWidth | dstFrameWidth | srcFrameHeight | dstFrameHeight) & 1) {
PTRACE(2,"PColCnv\tError in YUY2 to YUV420P converter, All size need to be pair.");
return FALSE;
}
if ((srcFrameWidth == dstFrameWidth) || (srcFrameHeight == dstFrameHeight)) {
YUY2toYUV420PSameSize(yuy2, yuv420p);
} else {
/* not efficient (convert then resize) */
BYTE *intermed = intermediateFrameStore.GetPointer(srcFrameWidth*srcFrameHeight*3/2);
YUY2toYUV420PSameSize(yuy2, intermed);
ResizeYUV420P(intermed, yuv420p);
}
if (bytesReturned != NULL)
*bytesReturned = dstFrameBytes;
return TRUE;
}
// Consider a YUV422P image of 8x2 pixels.
//
// A plane of Y values A B C D E F G H
// I J K L M N O P
//
// A plane of U values 1 . 2 . 3 . 4 .
// 5 . 6 . 7 . 8 .
//
// A plane of V values 1 . 2 . 3 . 4 .
// 5 . 6 . 7 . 8 .
//
// YUV422 is stored as Y U Y V
// thus, a 4x4 image requires 32 bytes of storage.
//
// Image has two possible transformations.
// padded (src smaller than dst)
// subsampled and padded (src bigger than dst)
void PStandardColourConverter::ResizeYUV422(const BYTE * src, BYTE * dest) const
{
DWORD *result = (DWORD *)dest;
DWORD black = (DWORD)(BLACK_U<<24) + (BLACK_Y<<16) + (BLACK_U<<8) + BLACK_Y;
unsigned maxIndex = dstFrameWidth*dstFrameHeight/2;
for (unsigned i = 0; i < maxIndex; i++)
*result++ = black;
if ( (dstFrameWidth*dstFrameHeight) > (srcFrameWidth*srcFrameHeight) ) {
//dest is bigger than the source. No subsampling.
//Place the src in the middle of the destination.
unsigned yOffset = dstFrameHeight - srcFrameHeight;
unsigned xOffset = dstFrameWidth - srcFrameWidth;
BYTE *s_ptr,*d_ptr;
d_ptr = (yOffset * dstFrameWidth) + xOffset + dest;
s_ptr = (BYTE *)src;
for (unsigned y = 0; y < srcFrameHeight; y++) {
memcpy(d_ptr,s_ptr, srcFrameWidth*2);
d_ptr += 2*dstFrameWidth;
s_ptr += 2*srcFrameWidth;
}
} else {
// source is bigger than the destination.
//
unsigned subSample = 1 + (srcFrameHeight/dstFrameHeight) ;
unsigned yOffset = dstFrameHeight - (srcFrameHeight/subSample);
unsigned xOffset = dstFrameWidth - (srcFrameWidth/subSample);
unsigned subSample2 = subSample*2;
DWORD *s_ptr = (DWORD * )src;
DWORD *d_ptr = (DWORD *) dest + ((yOffset * dstFrameWidth) + xOffset)/4 ;
DWORD *sl_ptr, *dl_ptr;
for (unsigned y = 0; y < srcFrameHeight; y+= subSample) {
sl_ptr = s_ptr;
dl_ptr = d_ptr;
for (unsigned x = 0; x < srcFrameWidth; x+= subSample2) {
*dl_ptr++ = *sl_ptr;
sl_ptr += subSample;
}
d_ptr += dstFrameWidth/2;
s_ptr += srcFrameWidth*subSample/2;
}
}
}
PSTANDARD_COLOUR_CONVERTER(YUV422,YUV422)
{
if (bytesReturned != NULL)
*bytesReturned = dstFrameBytes;
if (srcFrameBuffer == dstFrameBuffer)
return TRUE;
if ((srcFrameWidth == dstFrameWidth) && (srcFrameHeight == dstFrameHeight))
memcpy(dstFrameBuffer,srcFrameBuffer,srcFrameWidth*srcFrameHeight*2);
else
ResizeYUV422(srcFrameBuffer, dstFrameBuffer);
return TRUE;
}
// Consider a YUV420P image of 4x4 pixels.
//
// A plane of Y values A B C D
// E F G H
// I J K L
// M N O P
//
// A plane of U values 1 . 2 .
// . . . .
// 3 . 4 .
// . . . .
//
// A plane of V values 1 . 2 .
// . . . .
// 3 . 4 .
// . . . .
//
// YUV420P is stored as all Y (w*h), then U (w*h/4), then V
// thus, a 4x4 image requires 24 bytes of storage.
//
// Image has two possible transformations.
// padded (src smaller than dst)
// subsampled and padded (src bigger than dst)
void PStandardColourConverter::ResizeYUV420P(const BYTE * src, BYTE * dest) const
{
unsigned int i, y, x, npixels;
BYTE *d;
const BYTE *s;
npixels = dstFrameWidth * dstFrameHeight;
if ( (dstFrameWidth*dstFrameHeight) > (srcFrameWidth*srcFrameHeight) ) {
// dest is bigger than the source. No subsampling.
// Place the src in the middle of the destination.
unsigned int yOffset = (dstFrameHeight - srcFrameHeight)/2;
unsigned int xOffset = (dstFrameWidth - srcFrameWidth)/2;
d = dest;
for (i=0; i < npixels; i++)
*d++ = BLACK_Y;
for (i=0; i < npixels/4; i++)
*d++ = BLACK_U;
for (i=0; i < npixels/4; i++)
*d++ = BLACK_V;
// Copy plane Y
d = dest + yOffset * dstFrameWidth + xOffset;
s = src;
for (y = 0; y < srcFrameHeight; y++) {
memcpy(d, s, srcFrameWidth);
s += srcFrameWidth;
d += dstFrameWidth;
}
// Copy plane U
d = dest + npixels + (yOffset*dstFrameWidth/4) + xOffset/2;
for (y = 0; y < srcFrameHeight/2; y++) {
memcpy(d, s, srcFrameWidth/2);
s += srcFrameWidth/2;
d += dstFrameWidth/2;
}
// Copy plane V
d = dest + npixels + npixels/4 + (yOffset*dstFrameWidth/4) + xOffset/2;
for (y = 0; y < srcFrameHeight/2; y++) {
memcpy(d, s, srcFrameWidth/2);
s += srcFrameWidth/2;
d += dstFrameWidth/2;
}
} else {
// source is bigger than the destination.
//
#define FIX_FLOAT 16
unsigned int dx = (srcFrameWidth<<FIX_FLOAT)/dstFrameWidth;
unsigned int dy = (srcFrameHeight<<FIX_FLOAT)/dstFrameHeight;
unsigned int fy, fx;
s = src;
d = dest;
/* Copy Plane Y */
for (fy=0, y=0; y<dstFrameHeight; y++, fy+=dy) {
s = src + (fy>>FIX_FLOAT) * srcFrameWidth;
for (fx=0, x=0; x<dstFrameWidth; x++, fx+=dx) {
*d++ = s[fx>>FIX_FLOAT];
}
}
/* Copy Plane U */
src += srcFrameWidth*srcFrameHeight;
for (fy=0, y=0; y<dstFrameHeight/2; y++, fy+=dy) {
s = src + (fy>>FIX_FLOAT) * srcFrameWidth/2;
for (fx=0, x=0; x<dstFrameWidth/2; x++, fx+=dx) {
*d++ = s[fx>>FIX_FLOAT];
}
}
/* Copy Plane V */
src += srcFrameWidth*srcFrameHeight/4;
for (fy=0, y=0; y<dstFrameHeight/2; y++, fy+=dy) {
s = src + (fy>>FIX_FLOAT) * srcFrameWidth/2;
for (fx=0, x=0; x<dstFrameWidth/2; x++, fx+=dx) {
*d++ = s[fx>>FIX_FLOAT];
}
}
}
}
PSTANDARD_COLOUR_CONVERTER(YUV420P,YUV420P)
{
if (bytesReturned != NULL)
*bytesReturned = dstFrameBytes;
if (srcFrameBuffer == dstFrameBuffer)
return TRUE;
if ((srcFrameWidth == dstFrameWidth) && (srcFrameHeight == dstFrameHeight))
memcpy(dstFrameBuffer,srcFrameBuffer,srcFrameWidth*srcFrameHeight*3/2);
else
ResizeYUV420P(srcFrameBuffer, dstFrameBuffer);
return TRUE;
}
///No resize here.
//Colour format change only, YUV422 is turned into YUV420P.
static void Yuv422ToYuv420P(unsigned dstFrameWidth, unsigned dstFrameHeight,
const BYTE * srcFrame, BYTE * dstFrame)
{
unsigned a,b;
BYTE *u,*v;
const BYTE * s = srcFrame;
BYTE * y = dstFrame;
u = y + (dstFrameWidth * dstFrameHeight);
v = u + (dstFrameWidth * dstFrameHeight / 4);
for (a = 0; a < dstFrameHeight; a+=2) {
for (b = 0; b < dstFrameWidth; b+=2) {
*(y++) = *(s++);
*(u++) = *(s++);
*(y++) = *(s++);
*(v++) = *(s++);
}
for (b = 0; b < dstFrameWidth; b+=2) {
*(y++) = *(s++);
s++;
*(y++) = *(s++);
s++;
}
}
}
PSTANDARD_COLOUR_CONVERTER(YUV422,YUV420P)
{
if (srcFrameBuffer == dstFrameBuffer)
return FALSE;
if ((srcFrameWidth==dstFrameWidth) && (srcFrameHeight==dstFrameHeight))
Yuv422ToYuv420P(srcFrameWidth, srcFrameHeight, srcFrameBuffer, dstFrameBuffer);
else {
//do a resize. then convert to yuv420p.
BYTE * intermed = intermediateFrameStore.GetPointer(dstFrameWidth*dstFrameHeight*2);
ResizeYUV422(srcFrameBuffer, intermed);
Yuv422ToYuv420P(dstFrameWidth, dstFrameHeight, intermed, dstFrameBuffer);
}
if (bytesReturned != NULL)
*bytesReturned = dstFrameBytes;
return TRUE;
}
#define LIMIT(x) (unsigned char) ((x > 255) ? 255 : ((x < 0) ? 0 : x ))
static inline int clip(int a, int limit) {
return a<limit?a:limit;
}
BOOL PStandardColourConverter::SBGGR8toYUV420P(const BYTE * src, BYTE * dst, PINDEX * bytesReturned) const
{
#define USE_SBGGR8_NATIVE 1 // set to 0 to use the double conversion algorithm (Bayer->RGB->YUV420P)
#if USE_SBGGR8_NATIVE
// kernels for Y conversion, normalised by 2^16
const int kR[]={1802,9667,1802,9667,19661,9667,1802,9667,1802};
const int kG1[]={7733,9830,7733,3604,7733,3604,7733,9830,7733};
const int kG2[]={7733,3604,7733,9830,7733,9830,7733,3604,7733};
const int kB[]={4915,9667,4915,9667,7209,9667,4915,9667,4915};
// const int kID[]={0,0,0,0,65536,0,0,0,0}; identity kernel, use to test
int B, G, G1, G2, R;
const int stride = srcFrameWidth;
unsigned const int hSize =srcFrameHeight/2;
unsigned const int vSize =srcFrameWidth/2;
unsigned const int lastRow=srcFrameHeight-1;
unsigned const int lastCol=srcFrameWidth-1;
unsigned int i,j;
const BYTE *sBayer = src;
// Y = round( 0.256788 * R + 0.504129 * G + 0.097906 * B) + 16;
// Y = round( 0.30 * R + 0.59 * G + 0.11 * B ) use this!
// U = round(-0.148223 * R - 0.290993 * G + 0.439216 * B) + 128;
// V = round( 0.439216 * R - 0.367788 * G - 0.071427 * B) + 128;
// Compute U and V planes using EXACT values, reading 2x2 pixels at a time
BYTE *dU = dst+srcFrameHeight*srcFrameWidth;
BYTE *dV = dU+hSize*vSize;
for (i=0; i<hSize; i++) {
for (j=0; j<vSize; j++) {
B=sBayer[0];
G1=sBayer[1];
G2=sBayer[stride];
R=sBayer[stride+1];
G=G1+G2;
*dU = (BYTE)( ( (-19428 * R -19071*G +57569 * B) >> 17) + 128 );
*dV = (BYTE)( ( ( 57569 * R -24103*G -9362 * B) >> 17) + 128 );
sBayer+=2;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -