cclib.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. //==========================================================================
  2. //
  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
  6. // PURPOSE.
  7. //
  8. // Copyright (c) 1999 - 2001 On2 Technologies Inc. All Rights Reserved.
  9. //
  10. //--------------------------------------------------------------------------
  11. #ifndef _CCLIB_H
  12. #define _CCLIB_H
  13. #include "cpuidlib.h"
  14. #ifdef __cplusplus
  15. extern "C"
  16. {
  17. #endif
  18. /*
  19. * **-CCLIB.H
  20. *
  21. * ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage **
  22. * ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage **
  23. * ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage **
  24. *
  25. *
  26. *
  27. * The library contains color space conversion functions. The proper way to use this library is to
  28. * call InitCCLib with a value of "SpecialProc" BEFORE attempting any color space conversions. DeInitCCLib
  29. * should be called when you are done with the libary. It will preform any clean up that is necessary.
  30. *
  31. *
  32. *
  33. *
  34. * ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage **
  35. * ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage **
  36. * ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage ** Usage **
  37. */
  38. /*
  39. * **-InitCCLib
  40. *
  41. * This function MUST be called before attempting to use any of the functions in the library.
  42. * This function will initilize all the function pointers to point to valid routines.
  43. *
  44. * Assumptions:
  45. * Assumes that it is safe to write to the function pointers.
  46. *
  47. * Input:
  48. * CpuType - If CpuType type is set to "SpecialProc" the code will autodetect the CPU and initilize the function
  49. * pointers appropiatly. If CpuType is set to any other value it will assume that that was the CPUType
  50. * detected. NOTE: You should be careful when forcing the CPU to a specific type. If you force the
  51. * CPU type to one that is not valid for your system you will most likely crash.
  52. *
  53. * Output:
  54. * Return Non-Zero value if there was a problem initilizing the function pointers
  55. *
  56. * Function pointers RGB32toYV12FuncPtr
  57. * RGB24toYV12FuncPtr
  58. * YVYUtoYV12FuncPtr
  59. *
  60. * Initilized to point to the proper routines for this system
  61. */
  62. int InitCCLib( PROCTYPE CpuType );
  63. /*
  64. * **-DeInitCCLib
  65. *
  66. * You should call this function when you are done using the color conversion library.
  67. *
  68. * Assumptions:
  69. * You are done with the color conversion library and would like it to clean up after itself
  70. *
  71. * Input:
  72. * None
  73. *
  74. * Output:
  75. * No explicit return value
  76. *
  77. * color conversion library cleaned up
  78. */
  79. void DeInitCCLib( void );
  80. /*
  81. * *** 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 ***
  82. *
  83. *
  84. * There are macros below to reduce the pain needed to use these functions
  85. *
  86. *
  87. * *** 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 ***
  88. */
  89. /*
  90. * **-RGB32toYV12FuncPtr
  91. *
  92. * This function pointer points to the fastest version of the function that will convert a RGB32 buffer to planer YV12 output
  93. * Alpha is ignored.
  94. *
  95. * InitCCLib MUST be called before using this function pointer or you will go off into the weeds.
  96. *
  97. * Inputs:
  98. * RGBABuffer - Pointer to buffer containing RGB data. We assume that data looks like
  99. *
  100. * +---+---+---+---+---+---+---+---+
  101. * Memory Address | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
  102. * +---+---+---+---+---+---+---+---+
  103. * Contents | B | G | R | A | B | G | R | A |
  104. * +---+---+---+---+---+---+---+---+
  105. *
  106. * ImageWidth - Width (in pixels) of the image to be processed
  107. *
  108. * ImageHeight - Height (in pixels) of the image to be processed
  109. *
  110. * YBuffer - Pointer to buffer where we should place the converted Y data. The caller needs to
  111. * ensure that sufficent memory is allocated. We do not check.
  112. *
  113. * UBuffer - Pointer to buffer where we should place the converted U data. The caller needs to
  114. * ensure that sufficent memory is allocated. We do not check.
  115. *
  116. * VBuffer - Pointer to buffer where we should place the converted U data. The caller needs to
  117. * ensure that sufficent memory is allocated. We do not check.
  118. *
  119. * Outputs:
  120. * YBuffer - Buffer filled with RGB data converted to YV12 format
  121. *
  122. * UBuffer - Buffer filled with RGB data converted to YV12 format
  123. *
  124. * VBuffer - Buffer filled with RGB data converted to YV12 format
  125. *
  126. * Assumptions:
  127. * Assumes that InitCCLib has been called to initilize this function pointer
  128. *
  129. * We assume that the width and height of the image passed in is even. If it is not
  130. * the last line and column will get bad U and V values. This is due to us averging
  131. * 4x4 block to get U and V values.
  132. *
  133. * Formulas:
  134. * Cb = U
  135. * Cr = V
  136. *
  137. * Y = 0.257R + 0.504G + 0.098B + 16
  138. * Cb = -0.148R - 0.291G + 0.439B + 128
  139. * Cr = 0.439R - 0.368G - 0.071B + 128
  140. *
  141. * The formulas above were obtained from the book Video Demistyfied.
  142. *
  143. * The YV12 format drops every other U and V across and every other U, V vertical line.
  144. * To calculate U and V we will average the 4 RGB values before we convert to U and V.
  145. * This is slightly less accurate than converting the 4 RGB values to 4 U and V values
  146. * and then averaging the U and V values. The plus side of averaging before is that
  147. * we the coversion is about 10% faster than if we were to convert the values and then
  148. * average.
  149. *
  150. * We process the image in 2x2 blocks. From left to right then from top to bottom.
  151. * Given the following image we will process it in the following order
  152. *
  153. * 1) (0,0), (0,1), (1,0), (1,1)
  154. * 2) (0,2), (0,3), (1,2), (1,3)
  155. * 3) (2,0), (2,1), (2,2), (2,3)
  156. * 4) (3,0), (3,1), (3,2), (3,3)
  157. *
  158. * +-----+-----+-----+-----+
  159. * | 0,0 | 0,1 | 0,2 | 0,3 |
  160. * +-----+-----+-----+-----+
  161. * | 1,0 | 1,1 | 1,2 | 1,3 |
  162. * +-----+-----+-----+-----+
  163. * | 2,0 | 2,1 | 2,2 | 2,3 |
  164. * +-----+-----+-----+-----+
  165. * | 3,0 | 3,1 | 3,2 | 3,3 |
  166. * +-----+-----+-----+-----+
  167. *
  168. * To try and avoid rounding errors we are going to scale the number and only
  169. * convert when we write the number to memory.
  170. *
  171. * When we finally scale the numbers down we will round values with fractions
  172. * greater than .5 up and less than .5 down. To achieve this we add in a round
  173. * factor which is equal to half of the amount that we divide by.
  174. *
  175. * The values that this function generates for Y, Cr, Cb are very accurate.
  176. * Utilizing double precision floating point will not generate more accurate
  177. * results.
  178. *
  179. * When converting from the 32-bit Y, Cb, Cr to the 8-bit Y, Cb, Cr values we do
  180. * not need to worry about over flowing the 8-bit value. Using the worst R, G, B
  181. * values we get the following Min and Max values for Y, Cb, Cr.
  182. *
  183. * +=====+=====+=====++=====+=====+=====++=========+
  184. * | R | G | B || Y | Cb | Cr || |
  185. * +=====+=====+=====++=====+=====+=====++=========+
  186. * | 255 | 255 | 0 || 210 | 16 | 146 || Min Cb |
  187. * +-----+-----+-----++-----+-----+-----++---------+
  188. * | 0 | 0 | 255 || 40 | 239 | 109 || Max Cb |
  189. * +-----+-----+-----++-----+-----+-----++---------+
  190. * | 0 | 255 | 255 || 169 | 165 | 16 || Min Cr |
  191. * +-----+-----+-----++-----+-----+-----++---------+
  192. * | 255 | 0 | 0 || 81 | 90 | 239 || Max Cr |
  193. * +-----+-----+-----++-----+-----+-----++---------+
  194. * | 0 | 0 | 0 || 16 | 128 | 128 || Min Y |
  195. * +-----+-----+-----++-----+-----+-----++---------+
  196. * | 255 | 255 | 255 || 235 | 128 | 128 || Max Y |
  197. * +-----+-----+-----++-----+-----+-----++---------+
  198. *
  199. *
  200. */
  201. extern void (*RGB32toYV12FuncPtr)( unsigned char *RGBABuffer, int ImageWidth, int ImageHeight,
  202. unsigned char *YBuffer, unsigned char *UBuffer, unsigned char *VBuffer );
  203. /*
  204. * **-RGB24toYV12FuncPtr
  205. *
  206. * This function is 99.99% the same as CC_RGB32toYV12 see comments for CC_RGB32toYV12 if you want to know how this
  207. * function works. The only difference from CC_RGB32toYV12 is we assume that
  208. * the input buffer is of the RGB 24 format given below.
  209. *
  210. * +---+---+---+---+---+---+---+---+
  211. * Memory Address | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
  212. * +---+---+---+---+---+---+---+---+
  213. * Contents | B | G | R | B | G | R | B | G |
  214. * +---+---+---+---+---+---+---+---+
  215. *
  216. */
  217. extern void (*RGB24toYV12FuncPtr)( unsigned char *RGBBuffer, int ImageWidth, int ImageHeight,
  218. unsigned char *YBuffer, unsigned char *UBuffer, unsigned char *VBuffer );
  219. /*
  220. * **-YVYUtoYV12FuncPtr
  221. *
  222. * This function pointer points to the fastest version of the following function that will run on
  223. * this system.
  224. *
  225. * InitCCLib MUST be called before trying to use this pointer. If you do not you will be in the
  226. * weeds
  227. *
  228. * The function will convert a YVYU (a.k.a. YUV 4:2:2) format YUV buffer to YV12 format buffer.
  229. * The YVYU format has two lines of U and V data per two lines of Y data. The YV12 format only
  230. * 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
  231. * line we will average the two U, V lines.
  232. *
  233. * Example:
  234. * +--------+--------+--------+--------+--------+--------+--------+--------+-----+
  235. * | Y(0,0) | U(0,0) | Y(0,1) | V(0,0) | Y(0,2) | U(0,1) | Y(0,1) | V(0,1) | ... |
  236. * +--------+--------+--------+--------+--------+--------+--------+--------+-----+
  237. * | Y(1,0) | U(1,0) | Y(1,1) | V(1,0) | Y(1,2) | U(1,1) | Y(1,1) | V(1,1) | ... |
  238. * +--------+--------+--------+--------+--------+--------+--------+--------+-----+
  239. * | Y(2,0) | U(2,0) | Y(2,1) | V(2,0) | Y(2,2) | U(2,1) | Y(2,1) | V(2,1) | ... |
  240. * +--------+--------+--------+--------+--------+--------+--------+--------+-----+
  241. * | Y(3,0) | U(3,0) | Y(3,1) | V(3,0) | Y(3,2) | U(3,1) | Y(3,1) | V(3,1) | ... |
  242. * +--------+--------+--------+--------+--------+--------+--------+--------+-----+
  243. * | ... | ... | ... | ... | ... | ... | ... | ... | ... |
  244. * +--------+--------+--------+--------+--------+--------+--------+--------+-----+
  245. *
  246. *
  247. * ==
  248. *
  249. * +--------+--------+--------+--------+-----+
  250. * | Y(0,0) | Y(0,1) | Y(0,2) | Y(0,1) | ... |
  251. * +--------+--------+--------+--------+-----+
  252. * | Y(1,0) | Y(1,1) | Y(1,2) | Y(1,1) | ... |
  253. * +--------+--------+--------+--------+-----+
  254. * | Y(2,0) | Y(2,1) | Y(2,2) | Y(2,1) | ... |
  255. * +--------+--------+--------+--------+-----+
  256. * | Y(3,0) | Y(3,1) | Y(3,2) | Y(3,1) | ... |
  257. * +--------+--------+--------+--------+-----+
  258. * | ... | ... | ... | ... | ... |
  259. * +--------+--------+--------+--------+-----+
  260. *
  261. *
  262. * +--------------------+--------------------+------+
  263. * | AVG[U(0,0),U(1,0)] | AVG[U(0,1),U(1,1)] | ... |
  264. * +--------------------+--------------------+------+
  265. * | AVG[U(2,0),U(3,0)] | AVG[U(2,1),U(3,1)] | ... |
  266. * +--------------------+--------------------+------+
  267. * | ... | ... | ... |
  268. * +--------------------+--------------------+------+
  269. *
  270. *
  271. * +--------------------+--------------------+------+
  272. * | AVG[V(0,0),U(1,0)] | AVG[V(0,1),U(1,1)] | ... |
  273. * +--------------------+--------------------+------+
  274. * | AVG[V(2,0),U(3,0)] | AVG[V(2,1),U(3,1)] | ... |
  275. * +--------------------+--------------------+------+
  276. * | ... | ... | ... |
  277. * +--------------------+--------------------+------+
  278. *
  279. * A single pass of the core look will process two horizontal lines of the image at once.
  280. * The makes it easier to average the U and V values.
  281. *
  282. *
  283. * Inputs:
  284. * YVYUBuffer - Pointer to buffer containing YVYU data. We assume that the data looks like
  285. *
  286. * +---+---+---+---+---+---+---+---+
  287. * Memory Address | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
  288. * +---+---+---+---+---+---+---+---+
  289. * Contents | Y | V | Y | U | Y | V | Y | U |
  290. * +---+---+---+---+---+---+---+---+
  291. *
  292. * ImageWidth - Width (in pixels) of the image to be processed
  293. *
  294. * ImageHeight - Height (in pixels) of the image to be processed
  295. *
  296. * YBuffer - Pointer to buffer where we should place the converted Y data. The caller needs to
  297. * ensure that sufficent memory is allocated. We do not check.
  298. *
  299. * UBuffer - Pointer to buffer where we should place the converted U data. The caller needs to
  300. * ensure that sufficent memory is allocated. We do not check.
  301. *
  302. * VBuffer - Pointer to buffer where we should place the converted U data. The caller needs to
  303. * ensure that sufficent memory is allocated. We do not check.
  304. *
  305. * Outputs:
  306. * YBuffer - Buffer filled with YVYU data converted to YV12 format
  307. *
  308. * UBuffer - Buffer filled with YVYU data converted to YV12 format
  309. *
  310. * VBuffer - Buffer filled with YVYU data converted to YV12 format
  311. *
  312. * Assumptions:
  313. * Assumes that InitCCLib has been called to initilize this function pointer
  314. *
  315. * Height of the image that we are processing is assumed to be even. If
  316. * the height is not even the last line of the image will be corrupted.
  317. *
  318. * For the C version the width of the image must be a multiple of two. For
  319. * the assembly version the width of the image must be a multiple of 8.
  320. *
  321. */
  322. extern void (*YVYUtoYV12FuncPtr)( unsigned char *YVYUBuffer, int ImageWidth, int ImageHeight,
  323. unsigned char *YBuffer, unsigned char *UBuffer, unsigned char *VBuffer );
  324. /*
  325. * Macros to make it easier to call the needed functions
  326. */
  327. #define CC_RGB32toYV12( _RGBABuffer, _ImageWidth, _ImageHeight, _YBuffer, _UBuffer, _VBuffer ) \
  328. (*RGB32toYV12FuncPtr)( _RGBABuffer, _ImageWidth, _ImageHeight, _YBuffer, _UBuffer, _VBuffer )
  329. #define CC_RGB24toYV12( _RGBBuffer, _ImageWidth, _ImageHeight, _YBuffer, _UBuffer, _VBuffer ) \
  330. (*RGB24toYV12FuncPtr)( _RGBBuffer, _ImageWidth, _ImageHeight, _YBuffer, _UBuffer, _VBuffer )
  331. #define CC_YVYUtoYV12( _YVYUBuffer, _ImageWidth, _ImageHeight, _YBuffer, _UBuffer, _VBuffer ) \
  332. (*YVYUtoYV12FuncPtr)( _YVYUBuffer, _ImageWidth, _ImageHeight, _YBuffer, _UBuffer, _VBuffer )
  333. void ConvertRGBtoYUV(
  334. unsigned char *r_src,unsigned char *g_src,unsigned char *b_src,
  335. int width, int height, int rgb_step, int rgb_pitch,
  336. unsigned char *y_src, unsigned char *u_src, unsigned char *v_src,
  337. int uv_width_shift, int uv_height_shift,
  338. int y_step, int y_pitch,int uv_step,int uv_pitch
  339. );
  340. #ifdef __cplusplus
  341. }
  342. #endif
  343. #endif /* _CCLIB_H */