1 //==========================================================================
3 // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
4 // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
5 // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
8 // Copyright (c) 1999 - 2001 On2 Technologies Inc. All Rights Reserved.
10 //--------------------------------------------------------------------------
25 * ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage **
26 * ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage **
27 * ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage **
31 * The library contains color space conversion functions. The proper way to use this library is to
32 * call InitCCLib with a value of "SpecialProc" BEFORE attempting any color space conversions. DeInitCCLib
33 * should be called when you are done with the libary. It will preform any clean up that is necessary.
38 * ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage **
39 * ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage **
40 * ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage **
48 * This function MUST be called before attempting to use any of the functions in the library.
49 * This function will initilize all the function pointers to point to valid routines.
52 * Assumes that it is safe to write to the function pointers.
55 * CpuType - If CpuType type is set to "SpecialProc" the code will autodetect the CPU and initilize the function
56 * pointers appropiatly. If CpuType is set to any other value it will assume that that was the CPUType
57 * detected. NOTE: You should be careful when forcing the CPU to a specific type. If you force the
58 * CPU type to one that is not valid for your system you will most likely crash.
61 * Return Non-Zero value if there was a problem initilizing the function pointers
63 * Function pointers RGB32toYV12FuncPtr
67 * Initilized to point to the proper routines for this system
69 int InitCCLib( PROCTYPE CpuType
);
75 * You should call this function when you are done using the color conversion library.
78 * You are done with the color conversion library and would like it to clean up after itself
84 * No explicit return value
86 * color conversion library cleaned up
88 void DeInitCCLib( void );
91 * *** N O T E *** N O T E *** *** N O T E *** N O T E *** *** N O T E *** N O T E *** *** N O T E *** N O T E *** *** N O T E *** N O T E ***
94 * There are macros below to reduce the pain needed to use these functions
97 * *** N O T E *** N O T E *** *** N O T E *** N O T E *** *** N O T E *** N O T E *** *** N O T E *** N O T E *** *** N O T E *** N O T E ***
101 * **-RGB32toYV12FuncPtr
103 * This function pointer points to the fastest version of the function that will convert a RGB32 buffer to planer YV12 output
106 * InitCCLib MUST be called before using this function pointer or you will go off into the weeds.
109 * RGBABuffer - Pointer to buffer containing RGB data. We assume that data looks like
111 * +---+---+---+---+---+---+---+---+
112 * Memory Address | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
113 * +---+---+---+---+---+---+---+---+
114 * Contents | B | G | R | A | B | G | R | A |
115 * +---+---+---+---+---+---+---+---+
117 * ImageWidth - Width (in pixels) of the image to be processed
119 * ImageHeight - Height (in pixels) of the image to be processed
121 * YBuffer - Pointer to buffer where we should place the converted Y data. The caller needs to
122 * ensure that sufficent memory is allocated. We do not check.
124 * UBuffer - Pointer to buffer where we should place the converted U data. The caller needs to
125 * ensure that sufficent memory is allocated. We do not check.
127 * VBuffer - Pointer to buffer where we should place the converted U data. The caller needs to
128 * ensure that sufficent memory is allocated. We do not check.
131 * YBuffer - Buffer filled with RGB data converted to YV12 format
133 * UBuffer - Buffer filled with RGB data converted to YV12 format
135 * VBuffer - Buffer filled with RGB data converted to YV12 format
138 * Assumes that InitCCLib has been called to initilize this function pointer
140 * We assume that the width and height of the image passed in is even. If it is not
141 * the last line and column will get bad U and V values. This is due to us averging
142 * 4x4 block to get U and V values.
148 * Y = 0.257R + 0.504G + 0.098B + 16
149 * Cb = -0.148R - 0.291G + 0.439B + 128
150 * Cr = 0.439R - 0.368G - 0.071B + 128
152 * The formulas above were obtained from the book Video Demistyfied.
154 * The YV12 format drops every other U and V across and every other U, V vertical line.
155 * To calculate U and V we will average the 4 RGB values before we convert to U and V.
156 * This is slightly less accurate than converting the 4 RGB values to 4 U and V values
157 * and then averaging the U and V values. The plus side of averaging before is that
158 * we the coversion is about 10% faster than if we were to convert the values and then
161 * We process the image in 2x2 blocks. From left to right then from top to bottom.
162 * Given the following image we will process it in the following order
164 * 1) (0,0), (0,1), (1,0), (1,1)
165 * 2) (0,2), (0,3), (1,2), (1,3)
166 * 3) (2,0), (2,1), (2,2), (2,3)
167 * 4) (3,0), (3,1), (3,2), (3,3)
169 * +-----+-----+-----+-----+
170 * | 0,0 | 0,1 | 0,2 | 0,3 |
171 * +-----+-----+-----+-----+
172 * | 1,0 | 1,1 | 1,2 | 1,3 |
173 * +-----+-----+-----+-----+
174 * | 2,0 | 2,1 | 2,2 | 2,3 |
175 * +-----+-----+-----+-----+
176 * | 3,0 | 3,1 | 3,2 | 3,3 |
177 * +-----+-----+-----+-----+
179 * To try and avoid rounding errors we are going to scale the number and only
180 * convert when we write the number to memory.
182 * When we finally scale the numbers down we will round values with fractions
183 * greater than .5 up and less than .5 down. To achieve this we add in a round
184 * factor which is equal to half of the amount that we divide by.
186 * The values that this function generates for Y, Cr, Cb are very accurate.
187 * Utilizing double precision floating point will not generate more accurate
190 * When converting from the 32-bit Y, Cb, Cr to the 8-bit Y, Cb, Cr values we do
191 * not need to worry about over flowing the 8-bit value. Using the worst R, G, B
192 * values we get the following Min and Max values for Y, Cb, Cr.
194 * +=====+=====+=====++=====+=====+=====++=========+
195 * | R | G | B || Y | Cb | Cr || |
196 * +=====+=====+=====++=====+=====+=====++=========+
197 * | 255 | 255 | 0 || 210 | 16 | 146 || Min Cb |
198 * +-----+-----+-----++-----+-----+-----++---------+
199 * | 0 | 0 | 255 || 40 | 239 | 109 || Max Cb |
200 * +-----+-----+-----++-----+-----+-----++---------+
201 * | 0 | 255 | 255 || 169 | 165 | 16 || Min Cr |
202 * +-----+-----+-----++-----+-----+-----++---------+
203 * | 255 | 0 | 0 || 81 | 90 | 239 || Max Cr |
204 * +-----+-----+-----++-----+-----+-----++---------+
205 * | 0 | 0 | 0 || 16 | 128 | 128 || Min Y |
206 * +-----+-----+-----++-----+-----+-----++---------+
207 * | 255 | 255 | 255 || 235 | 128 | 128 || Max Y |
208 * +-----+-----+-----++-----+-----+-----++---------+
212 extern void (*RGB32toYV12FuncPtr
)( unsigned char *RGBABuffer
, int ImageWidth
, int ImageHeight
,
213 unsigned char *YBuffer
, unsigned char *UBuffer
, unsigned char *VBuffer
);
216 * **-RGB24toYV12FuncPtr
218 * This function is 99.99% the same as CC_RGB32toYV12 see comments for CC_RGB32toYV12 if you want to know how this
219 * function works. The only difference from CC_RGB32toYV12 is we assume that
220 * the input buffer is of the RGB 24 format given below.
222 * +---+---+---+---+---+---+---+---+
223 * Memory Address | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
224 * +---+---+---+---+---+---+---+---+
225 * Contents | B | G | R | B | G | R | B | G |
226 * +---+---+---+---+---+---+---+---+
229 extern void (*RGB24toYV12FuncPtr
)( unsigned char *RGBBuffer
, int ImageWidth
, int ImageHeight
,
230 unsigned char *YBuffer
, unsigned char *UBuffer
, unsigned char *VBuffer
);
233 * **-YVYUtoYV12FuncPtr
235 * This function pointer points to the fastest version of the following function that will run on
238 * InitCCLib MUST be called before trying to use this pointer. If you do not you will be in the
241 * The function will convert a YVYU (a.k.a. YUV 4:2:2) format YUV buffer to YV12 format buffer.
242 * The YVYU format has two lines of U and V data per two lines of Y data. The YV12 format only
243 * has one line of U, V data per two lines of Y data. To fit the extra U, V data into a single U, V
244 * line we will average the two U, V lines.
247 * +--------+--------+--------+--------+--------+--------+--------+--------+-----+
248 * | Y(0,0) | U(0,0) | Y(0,1) | V(0,0) | Y(0,2) | U(0,1) | Y(0,1) | V(0,1) | ... |
249 * +--------+--------+--------+--------+--------+--------+--------+--------+-----+
250 * | Y(1,0) | U(1,0) | Y(1,1) | V(1,0) | Y(1,2) | U(1,1) | Y(1,1) | V(1,1) | ... |
251 * +--------+--------+--------+--------+--------+--------+--------+--------+-----+
252 * | Y(2,0) | U(2,0) | Y(2,1) | V(2,0) | Y(2,2) | U(2,1) | Y(2,1) | V(2,1) | ... |
253 * +--------+--------+--------+--------+--------+--------+--------+--------+-----+
254 * | Y(3,0) | U(3,0) | Y(3,1) | V(3,0) | Y(3,2) | U(3,1) | Y(3,1) | V(3,1) | ... |
255 * +--------+--------+--------+--------+--------+--------+--------+--------+-----+
256 * | ... | ... | ... | ... | ... | ... | ... | ... | ... |
257 * +--------+--------+--------+--------+--------+--------+--------+--------+-----+
262 * +--------+--------+--------+--------+-----+
263 * | Y(0,0) | Y(0,1) | Y(0,2) | Y(0,1) | ... |
264 * +--------+--------+--------+--------+-----+
265 * | Y(1,0) | Y(1,1) | Y(1,2) | Y(1,1) | ... |
266 * +--------+--------+--------+--------+-----+
267 * | Y(2,0) | Y(2,1) | Y(2,2) | Y(2,1) | ... |
268 * +--------+--------+--------+--------+-----+
269 * | Y(3,0) | Y(3,1) | Y(3,2) | Y(3,1) | ... |
270 * +--------+--------+--------+--------+-----+
271 * | ... | ... | ... | ... | ... |
272 * +--------+--------+--------+--------+-----+
275 * +--------------------+--------------------+------+
276 * | AVG[U(0,0),U(1,0)] | AVG[U(0,1),U(1,1)] | ... |
277 * +--------------------+--------------------+------+
278 * | AVG[U(2,0),U(3,0)] | AVG[U(2,1),U(3,1)] | ... |
279 * +--------------------+--------------------+------+
280 * | ... | ... | ... |
281 * +--------------------+--------------------+------+
284 * +--------------------+--------------------+------+
285 * | AVG[V(0,0),U(1,0)] | AVG[V(0,1),U(1,1)] | ... |
286 * +--------------------+--------------------+------+
287 * | AVG[V(2,0),U(3,0)] | AVG[V(2,1),U(3,1)] | ... |
288 * +--------------------+--------------------+------+
289 * | ... | ... | ... |
290 * +--------------------+--------------------+------+
292 * A single pass of the core look will process two horizontal lines of the image at once.
293 * The makes it easier to average the U and V values.
297 * YVYUBuffer - Pointer to buffer containing YVYU data. We assume that the data looks like
299 * +---+---+---+---+---+---+---+---+
300 * Memory Address | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
301 * +---+---+---+---+---+---+---+---+
302 * Contents | Y | V | Y | U | Y | V | Y | U |
303 * +---+---+---+---+---+---+---+---+
305 * ImageWidth - Width (in pixels) of the image to be processed
307 * ImageHeight - Height (in pixels) of the image to be processed
309 * YBuffer - Pointer to buffer where we should place the converted Y data. The caller needs to
310 * ensure that sufficent memory is allocated. We do not check.
312 * UBuffer - Pointer to buffer where we should place the converted U data. The caller needs to
313 * ensure that sufficent memory is allocated. We do not check.
315 * VBuffer - Pointer to buffer where we should place the converted U data. The caller needs to
316 * ensure that sufficent memory is allocated. We do not check.
319 * YBuffer - Buffer filled with YVYU data converted to YV12 format
321 * UBuffer - Buffer filled with YVYU data converted to YV12 format
323 * VBuffer - Buffer filled with YVYU data converted to YV12 format
326 * Assumes that InitCCLib has been called to initilize this function pointer
328 * Height of the image that we are processing is assumed to be even. If
329 * the height is not even the last line of the image will be corrupted.
331 * For the C version the width of the image must be a multiple of two. For
332 * the assembly version the width of the image must be a multiple of 8.
335 extern void (*YVYUtoYV12FuncPtr
)( unsigned char *YVYUBuffer
, int ImageWidth
, int ImageHeight
,
336 unsigned char *YBuffer
, unsigned char *UBuffer
, unsigned char *VBuffer
);
340 * Macros to make it easier to call the needed functions
342 extern void (*RGB32toYV12
)( unsigned char *RGBABuffer
, int ImageWidth
, int ImageHeight
,
343 unsigned char *YBuffer
, unsigned char *UBuffer
, unsigned char *VBuffer
, int SrcPitch
,int DstPitch
);
345 extern void (*RGB24toYV12
)( unsigned char *RGBBuffer
, int ImageWidth
, int ImageHeight
,
346 unsigned char *YBuffer
, unsigned char *UBuffer
, unsigned char *VBuffer
, int SrcPitch
,int DstPitch
);
348 extern void (*UYVYtoYV12
)( unsigned char *UYVYBuffer
, int ImageWidth
, int ImageHeight
,
349 unsigned char *YBuffer
, unsigned char *UBuffer
, unsigned char *VBuffer
, int SrcPitch
,int DstPitch
);
351 extern void (*YUY2toYV12
)( unsigned char *UYVYBuffer
, int ImageWidth
, int ImageHeight
,
352 unsigned char *YBuffer
, unsigned char *UBuffer
, unsigned char *VBuffer
, int SrcPitch
,int DstPitch
);
354 extern void (*YVYUtoYV12
)( unsigned char *YVYUBuffer
, int ImageWidth
, int ImageHeight
,
355 unsigned char *YBuffer
, unsigned char *UBuffer
, unsigned char *VBuffer
, int SrcPitch
,int DstPitch
);
357 #define CC_RGB32toYV12( _RGBABuffer, _ImageWidth, _ImageHeight, _YBuffer, _UBuffer, _VBuffer ) \
358 (*RGB32toYV12)( _RGBABuffer, _ImageWidth, _ImageHeight, _YBuffer, _UBuffer, _VBuffer, _ImageWidth*4, _ImageWidth )
360 #define CC_RGB24toYV12( _RGBBuffer, _ImageWidth, _ImageHeight, _YBuffer, _UBuffer, _VBuffer ) \
361 (*RGB24toYV12)( _RGBBuffer, _ImageWidth, _ImageHeight, _YBuffer, _UBuffer, _VBuffer, _ImageWidth*3, _ImageWidth )
363 #define CC_UYVYtoYV12( _UYVYBuffer, _ImageWidth, _ImageHeight, _YBuffer, _UBuffer, _VBuffer ) \
364 (*UYVYtoYV12)( _UYVYBuffer, _ImageWidth, _ImageHeight, _YBuffer, _UBuffer, _VBuffer, _ImageWidth*2, _ImageWidth )
366 #define CC_YUY2toYV12( _YUY2Buffer, _ImageWidth, _ImageHeight, _YBuffer, _UBuffer, _VBuffer ) \
367 (*YUY2toYV12)( _YUY2Buffer, _ImageWidth, _ImageHeight, _YBuffer, _UBuffer, _VBuffer, _ImageWidth*2, _ImageWidth )
369 #define CC_YVYUtoYV12( _YVYUBuffer, _ImageWidth, _ImageHeight, _YBuffer, _UBuffer, _VBuffer ) \
370 (*YVYUtoYV12)( _YVYUBuffer, _ImageWidth, _ImageHeight, _YBuffer, _UBuffer, _VBuffer, _ImageWidth*2, _ImageWidth )
372 // super generic rgb to yuv color conversion can handle any rgb to yuv conversion
373 // provided r,g,b components are 1 byte apiece, and that the resulting y is 1 byte
374 extern void ConvertRGBtoYUV(
375 const unsigned char* const pucSourceR
, const unsigned char* const pucSourceG
, const unsigned char* const pucSourceB
,
376 int width
, int height
, int rgb_step
, int rgb_pitch
,
377 unsigned char* const pucDestY
, unsigned char* const pucDestU
, unsigned char* const pucDestV
,
378 int uv_width_shift
, int uv_height_shift
,
379 int y_step
, int y_pitch
,int uv_step
,int uv_pitch
);
383 void ConvertRGBtoYUV(
384 unsigned char *r_src,unsigned char *g_src,unsigned char *b_src,
385 int width, int height, int rgb_step, int rgb_pitch,
386 unsigned char *y_src, unsigned char *u_src, unsigned char *v_src,
387 int uv_width_shift, int uv_height_shift,
388 int y_step, int y_pitch,int uv_step,int uv_pitch
394 #endif /* _CCLIB_H */