yuv2rgb.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. /*
  2. * software YUV to RGB converter
  3. *
  4. * Copyright (C) 1999, Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
  5. *
  6. * Functions broken out from display_x11.c and several new modes
  7. * added by Håkan Hjort <d95hjort@dtek.chalmers.se>
  8. *
  9. * 15 & 16 bpp support by Franck Sicard <Franck.Sicard@solsoft.fr>
  10. *
  11. * MMX/MMX2 template stuff (needed for fast movntq support),
  12. * 1,4,8bpp support and context / deglobalize stuff
  13. * by Michael Niedermayer (michaelni@gmx.at)
  14. *
  15. * This file is part of mpeg2dec, a free MPEG-2 video decoder
  16. *
  17. * mpeg2dec is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2, or (at your option)
  20. * any later version.
  21. *
  22. * mpeg2dec is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with mpeg2dec; if not, write to the Free Software
  29. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  30. */
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <inttypes.h>
  34. #include <assert.h>
  35. #include "config.h"
  36. #include "rgb2rgb.h"
  37. #include "swscale.h"
  38. #include "swscale_internal.h"
  39. #define DITHER1XBPP // only for MMX
  40. extern const uint8_t dither_8x8_32[8][8];
  41. extern const uint8_t dither_8x8_73[8][8];
  42. extern const uint8_t dither_8x8_220[8][8];
  43. #if HAVE_MMX
  44. /* hope these constant values are cache line aligned */
  45. DECLARE_ASM_CONST(8, uint64_t, mmx_00ffw) = 0x00ff00ff00ff00ffULL;
  46. DECLARE_ASM_CONST(8, uint64_t, mmx_redmask) = 0xf8f8f8f8f8f8f8f8ULL;
  47. DECLARE_ASM_CONST(8, uint64_t, mmx_grnmask) = 0xfcfcfcfcfcfcfcfcULL;
  48. //MMX versions
  49. #undef RENAME
  50. #undef HAVE_MMX2
  51. #undef HAVE_AMD3DNOW
  52. #define HAVE_MMX2 0
  53. #define HAVE_AMD3DNOW 0
  54. #define RENAME(a) a ## _MMX
  55. #include "yuv2rgb_template.c"
  56. //MMX2 versions
  57. #undef RENAME
  58. #undef HAVE_MMX2
  59. #define HAVE_MMX2 1
  60. #define RENAME(a) a ## _MMX2
  61. #include "yuv2rgb_template.c"
  62. #endif /* HAVE_MMX */
  63. const int32_t Inverse_Table_6_9[8][4] = {
  64. {117504, 138453, 13954, 34903}, /* no sequence_display_extension */
  65. {117504, 138453, 13954, 34903}, /* ITU-R Rec. 709 (1990) */
  66. {104597, 132201, 25675, 53279}, /* unspecified */
  67. {104597, 132201, 25675, 53279}, /* reserved */
  68. {104448, 132798, 24759, 53109}, /* FCC */
  69. {104597, 132201, 25675, 53279}, /* ITU-R Rec. 624-4 System B, G */
  70. {104597, 132201, 25675, 53279}, /* SMPTE 170M */
  71. {117579, 136230, 16907, 35559} /* SMPTE 240M (1987) */
  72. };
  73. #define RGB(i) \
  74. U = pu[i]; \
  75. V = pv[i]; \
  76. r = (void *)c->table_rV[V]; \
  77. g = (void *)(c->table_gU[U] + c->table_gV[V]); \
  78. b = (void *)c->table_bU[U];
  79. #define DST1(i) \
  80. Y = py_1[2*i]; \
  81. dst_1[2*i] = r[Y] + g[Y] + b[Y]; \
  82. Y = py_1[2*i+1]; \
  83. dst_1[2*i+1] = r[Y] + g[Y] + b[Y];
  84. #define DST2(i) \
  85. Y = py_2[2*i]; \
  86. dst_2[2*i] = r[Y] + g[Y] + b[Y]; \
  87. Y = py_2[2*i+1]; \
  88. dst_2[2*i+1] = r[Y] + g[Y] + b[Y];
  89. #define DST1RGB(i) \
  90. Y = py_1[2*i]; \
  91. dst_1[6*i] = r[Y]; dst_1[6*i+1] = g[Y]; dst_1[6*i+2] = b[Y]; \
  92. Y = py_1[2*i+1]; \
  93. dst_1[6*i+3] = r[Y]; dst_1[6*i+4] = g[Y]; dst_1[6*i+5] = b[Y];
  94. #define DST2RGB(i) \
  95. Y = py_2[2*i]; \
  96. dst_2[6*i] = r[Y]; dst_2[6*i+1] = g[Y]; dst_2[6*i+2] = b[Y]; \
  97. Y = py_2[2*i+1]; \
  98. dst_2[6*i+3] = r[Y]; dst_2[6*i+4] = g[Y]; dst_2[6*i+5] = b[Y];
  99. #define DST1BGR(i) \
  100. Y = py_1[2*i]; \
  101. dst_1[6*i] = b[Y]; dst_1[6*i+1] = g[Y]; dst_1[6*i+2] = r[Y]; \
  102. Y = py_1[2*i+1]; \
  103. dst_1[6*i+3] = b[Y]; dst_1[6*i+4] = g[Y]; dst_1[6*i+5] = r[Y];
  104. #define DST2BGR(i) \
  105. Y = py_2[2*i]; \
  106. dst_2[6*i] = b[Y]; dst_2[6*i+1] = g[Y]; dst_2[6*i+2] = r[Y]; \
  107. Y = py_2[2*i+1]; \
  108. dst_2[6*i+3] = b[Y]; dst_2[6*i+4] = g[Y]; dst_2[6*i+5] = r[Y];
  109. #define PROLOG(func_name, dst_type) \
  110. static int func_name(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY, \
  111. int srcSliceH, uint8_t* dst[], int dstStride[]){\
  112. int y;\
  113. \
  114. if (c->srcFormat == PIX_FMT_YUV422P){\
  115. srcStride[1] *= 2;\
  116. srcStride[2] *= 2;\
  117. }\
  118. for (y=0; y<srcSliceH; y+=2){\
  119. dst_type *dst_1= (dst_type*)(dst[0] + (y+srcSliceY )*dstStride[0]);\
  120. dst_type *dst_2= (dst_type*)(dst[0] + (y+srcSliceY+1)*dstStride[0]);\
  121. dst_type av_unused *r, *b;\
  122. dst_type *g;\
  123. uint8_t *py_1= src[0] + y*srcStride[0];\
  124. uint8_t *py_2= py_1 + srcStride[0];\
  125. uint8_t *pu= src[1] + (y>>1)*srcStride[1];\
  126. uint8_t *pv= src[2] + (y>>1)*srcStride[2];\
  127. unsigned int h_size= c->dstW>>3;\
  128. while (h_size--) {\
  129. int av_unused U, V;\
  130. int Y;\
  131. #define EPILOG1(dst_delta)\
  132. pu += 4;\
  133. pv += 4;\
  134. py_1 += 8;\
  135. py_2 += 8;\
  136. dst_1 += dst_delta;\
  137. dst_2 += dst_delta;\
  138. }\
  139. if (c->dstW & 4) {\
  140. int av_unused Y, U, V;\
  141. #define EPILOG2()\
  142. }\
  143. }\
  144. return srcSliceH;\
  145. }
  146. #define EPILOG(dst_delta)\
  147. EPILOG1(dst_delta)\
  148. EPILOG2()
  149. PROLOG(yuv2rgb_c_32, uint32_t)
  150. RGB(0);
  151. DST1(0);
  152. DST2(0);
  153. RGB(1);
  154. DST2(1);
  155. DST1(1);
  156. RGB(2);
  157. DST1(2);
  158. DST2(2);
  159. RGB(3);
  160. DST2(3);
  161. DST1(3);
  162. EPILOG1(8)
  163. RGB(0);
  164. DST1(0);
  165. DST2(0);
  166. RGB(1);
  167. DST2(1);
  168. DST1(1);
  169. EPILOG2()
  170. PROLOG(yuv2rgb_c_24_rgb, uint8_t)
  171. RGB(0);
  172. DST1RGB(0);
  173. DST2RGB(0);
  174. RGB(1);
  175. DST2RGB(1);
  176. DST1RGB(1);
  177. RGB(2);
  178. DST1RGB(2);
  179. DST2RGB(2);
  180. RGB(3);
  181. DST2RGB(3);
  182. DST1RGB(3);
  183. EPILOG1(24)
  184. RGB(0);
  185. DST1RGB(0);
  186. DST2RGB(0);
  187. RGB(1);
  188. DST2RGB(1);
  189. DST1RGB(1);
  190. EPILOG2()
  191. // only trivial mods from yuv2rgb_c_24_rgb
  192. PROLOG(yuv2rgb_c_24_bgr, uint8_t)
  193. RGB(0);
  194. DST1BGR(0);
  195. DST2BGR(0);
  196. RGB(1);
  197. DST2BGR(1);
  198. DST1BGR(1);
  199. RGB(2);
  200. DST1BGR(2);
  201. DST2BGR(2);
  202. RGB(3);
  203. DST2BGR(3);
  204. DST1BGR(3);
  205. EPILOG1(24)
  206. RGB(0);
  207. DST1BGR(0);
  208. DST2BGR(0);
  209. RGB(1);
  210. DST2BGR(1);
  211. DST1BGR(1);
  212. EPILOG2()
  213. // This is exactly the same code as yuv2rgb_c_32 except for the types of
  214. // r, g, b, dst_1, dst_2
  215. PROLOG(yuv2rgb_c_16, uint16_t)
  216. RGB(0);
  217. DST1(0);
  218. DST2(0);
  219. RGB(1);
  220. DST2(1);
  221. DST1(1);
  222. RGB(2);
  223. DST1(2);
  224. DST2(2);
  225. RGB(3);
  226. DST2(3);
  227. DST1(3);
  228. EPILOG(8)
  229. // This is exactly the same code as yuv2rgb_c_32 except for the types of
  230. // r, g, b, dst_1, dst_2
  231. PROLOG(yuv2rgb_c_8, uint8_t)
  232. RGB(0);
  233. DST1(0);
  234. DST2(0);
  235. RGB(1);
  236. DST2(1);
  237. DST1(1);
  238. RGB(2);
  239. DST1(2);
  240. DST2(2);
  241. RGB(3);
  242. DST2(3);
  243. DST1(3);
  244. EPILOG(8)
  245. // r, g, b, dst_1, dst_2
  246. PROLOG(yuv2rgb_c_8_ordered_dither, uint8_t)
  247. const uint8_t *d32= dither_8x8_32[y&7];
  248. const uint8_t *d64= dither_8x8_73[y&7];
  249. #define DST1bpp8(i,o) \
  250. Y = py_1[2*i]; \
  251. dst_1[2*i] = r[Y+d32[0+o]] + g[Y+d32[0+o]] + b[Y+d64[0+o]]; \
  252. Y = py_1[2*i+1]; \
  253. dst_1[2*i+1] = r[Y+d32[1+o]] + g[Y+d32[1+o]] + b[Y+d64[1+o]];
  254. #define DST2bpp8(i,o) \
  255. Y = py_2[2*i]; \
  256. dst_2[2*i] = r[Y+d32[8+o]] + g[Y+d32[8+o]] + b[Y+d64[8+o]]; \
  257. Y = py_2[2*i+1]; \
  258. dst_2[2*i+1] = r[Y+d32[9+o]] + g[Y+d32[9+o]] + b[Y+d64[9+o]];
  259. RGB(0);
  260. DST1bpp8(0,0);
  261. DST2bpp8(0,0);
  262. RGB(1);
  263. DST2bpp8(1,2);
  264. DST1bpp8(1,2);
  265. RGB(2);
  266. DST1bpp8(2,4);
  267. DST2bpp8(2,4);
  268. RGB(3);
  269. DST2bpp8(3,6);
  270. DST1bpp8(3,6);
  271. EPILOG(8)
  272. // This is exactly the same code as yuv2rgb_c_32 except for the types of
  273. // r, g, b, dst_1, dst_2
  274. PROLOG(yuv2rgb_c_4, uint8_t)
  275. int acc;
  276. #define DST1_4(i) \
  277. Y = py_1[2*i]; \
  278. acc = r[Y] + g[Y] + b[Y]; \
  279. Y = py_1[2*i+1]; \
  280. acc |= (r[Y] + g[Y] + b[Y])<<4; \
  281. dst_1[i] = acc;
  282. #define DST2_4(i) \
  283. Y = py_2[2*i]; \
  284. acc = r[Y] + g[Y] + b[Y]; \
  285. Y = py_2[2*i+1]; \
  286. acc |= (r[Y] + g[Y] + b[Y])<<4; \
  287. dst_2[i] = acc;
  288. RGB(0);
  289. DST1_4(0);
  290. DST2_4(0);
  291. RGB(1);
  292. DST2_4(1);
  293. DST1_4(1);
  294. RGB(2);
  295. DST1_4(2);
  296. DST2_4(2);
  297. RGB(3);
  298. DST2_4(3);
  299. DST1_4(3);
  300. EPILOG(4)
  301. PROLOG(yuv2rgb_c_4_ordered_dither, uint8_t)
  302. const uint8_t *d64= dither_8x8_73[y&7];
  303. const uint8_t *d128=dither_8x8_220[y&7];
  304. int acc;
  305. #define DST1bpp4(i,o) \
  306. Y = py_1[2*i]; \
  307. acc = r[Y+d128[0+o]] + g[Y+d64[0+o]] + b[Y+d128[0+o]]; \
  308. Y = py_1[2*i+1]; \
  309. acc |= (r[Y+d128[1+o]] + g[Y+d64[1+o]] + b[Y+d128[1+o]])<<4; \
  310. dst_1[i]= acc;
  311. #define DST2bpp4(i,o) \
  312. Y = py_2[2*i]; \
  313. acc = r[Y+d128[8+o]] + g[Y+d64[8+o]] + b[Y+d128[8+o]]; \
  314. Y = py_2[2*i+1]; \
  315. acc |= (r[Y+d128[9+o]] + g[Y+d64[9+o]] + b[Y+d128[9+o]])<<4; \
  316. dst_2[i]= acc;
  317. RGB(0);
  318. DST1bpp4(0,0);
  319. DST2bpp4(0,0);
  320. RGB(1);
  321. DST2bpp4(1,2);
  322. DST1bpp4(1,2);
  323. RGB(2);
  324. DST1bpp4(2,4);
  325. DST2bpp4(2,4);
  326. RGB(3);
  327. DST2bpp4(3,6);
  328. DST1bpp4(3,6);
  329. EPILOG(4)
  330. // This is exactly the same code as yuv2rgb_c_32 except for the types of
  331. // r, g, b, dst_1, dst_2
  332. PROLOG(yuv2rgb_c_4b, uint8_t)
  333. RGB(0);
  334. DST1(0);
  335. DST2(0);
  336. RGB(1);
  337. DST2(1);
  338. DST1(1);
  339. RGB(2);
  340. DST1(2);
  341. DST2(2);
  342. RGB(3);
  343. DST2(3);
  344. DST1(3);
  345. EPILOG(8)
  346. PROLOG(yuv2rgb_c_4b_ordered_dither, uint8_t)
  347. const uint8_t *d64= dither_8x8_73[y&7];
  348. const uint8_t *d128=dither_8x8_220[y&7];
  349. #define DST1bpp4b(i,o) \
  350. Y = py_1[2*i]; \
  351. dst_1[2*i] = r[Y+d128[0+o]] + g[Y+d64[0+o]] + b[Y+d128[0+o]]; \
  352. Y = py_1[2*i+1]; \
  353. dst_1[2*i+1] = r[Y+d128[1+o]] + g[Y+d64[1+o]] + b[Y+d128[1+o]];
  354. #define DST2bpp4b(i,o) \
  355. Y = py_2[2*i]; \
  356. dst_2[2*i] = r[Y+d128[8+o]] + g[Y+d64[8+o]] + b[Y+d128[8+o]]; \
  357. Y = py_2[2*i+1]; \
  358. dst_2[2*i+1] = r[Y+d128[9+o]] + g[Y+d64[9+o]] + b[Y+d128[9+o]];
  359. RGB(0);
  360. DST1bpp4b(0,0);
  361. DST2bpp4b(0,0);
  362. RGB(1);
  363. DST2bpp4b(1,2);
  364. DST1bpp4b(1,2);
  365. RGB(2);
  366. DST1bpp4b(2,4);
  367. DST2bpp4b(2,4);
  368. RGB(3);
  369. DST2bpp4b(3,6);
  370. DST1bpp4b(3,6);
  371. EPILOG(8)
  372. PROLOG(yuv2rgb_c_1_ordered_dither, uint8_t)
  373. const uint8_t *d128=dither_8x8_220[y&7];
  374. char out_1=0, out_2=0;
  375. g= c->table_gU[128] + c->table_gV[128];
  376. #define DST1bpp1(i,o) \
  377. Y = py_1[2*i]; \
  378. out_1+= out_1 + g[Y+d128[0+o]]; \
  379. Y = py_1[2*i+1]; \
  380. out_1+= out_1 + g[Y+d128[1+o]];
  381. #define DST2bpp1(i,o) \
  382. Y = py_2[2*i]; \
  383. out_2+= out_2 + g[Y+d128[8+o]]; \
  384. Y = py_2[2*i+1]; \
  385. out_2+= out_2 + g[Y+d128[9+o]];
  386. DST1bpp1(0,0);
  387. DST2bpp1(0,0);
  388. DST2bpp1(1,2);
  389. DST1bpp1(1,2);
  390. DST1bpp1(2,4);
  391. DST2bpp1(2,4);
  392. DST2bpp1(3,6);
  393. DST1bpp1(3,6);
  394. dst_1[0]= out_1;
  395. dst_2[0]= out_2;
  396. EPILOG(1)
  397. SwsFunc yuv2rgb_get_func_ptr (SwsContext *c)
  398. {
  399. #if HAVE_MMX2 || HAVE_MMX
  400. if (c->flags & SWS_CPU_CAPS_MMX2){
  401. switch(c->dstFormat){
  402. case PIX_FMT_RGB32: return yuv420_rgb32_MMX2;
  403. case PIX_FMT_BGR24: return yuv420_rgb24_MMX2;
  404. case PIX_FMT_RGB565: return yuv420_rgb16_MMX2;
  405. case PIX_FMT_RGB555: return yuv420_rgb15_MMX2;
  406. }
  407. }
  408. if (c->flags & SWS_CPU_CAPS_MMX){
  409. switch(c->dstFormat){
  410. case PIX_FMT_RGB32: return yuv420_rgb32_MMX;
  411. case PIX_FMT_BGR24: return yuv420_rgb24_MMX;
  412. case PIX_FMT_RGB565: return yuv420_rgb16_MMX;
  413. case PIX_FMT_RGB555: return yuv420_rgb15_MMX;
  414. }
  415. }
  416. #endif
  417. #if HAVE_VIS
  418. {
  419. SwsFunc t= yuv2rgb_init_vis(c);
  420. if (t) return t;
  421. }
  422. #endif
  423. #if CONFIG_MLIB
  424. {
  425. SwsFunc t= yuv2rgb_init_mlib(c);
  426. if (t) return t;
  427. }
  428. #endif
  429. #if HAVE_ALTIVEC
  430. if (c->flags & SWS_CPU_CAPS_ALTIVEC)
  431. {
  432. SwsFunc t = yuv2rgb_init_altivec(c);
  433. if (t) return t;
  434. }
  435. #endif
  436. #if ARCH_BFIN
  437. if (c->flags & SWS_CPU_CAPS_BFIN)
  438. {
  439. SwsFunc t = ff_bfin_yuv2rgb_get_func_ptr (c);
  440. if (t) return t;
  441. }
  442. #endif
  443. av_log(c, AV_LOG_WARNING, "No accelerated colorspace conversion found.\n");
  444. switch(c->dstFormat){
  445. case PIX_FMT_BGR32_1:
  446. case PIX_FMT_RGB32_1:
  447. case PIX_FMT_BGR32:
  448. case PIX_FMT_RGB32: return yuv2rgb_c_32;
  449. case PIX_FMT_RGB24: return yuv2rgb_c_24_rgb;
  450. case PIX_FMT_BGR24: return yuv2rgb_c_24_bgr;
  451. case PIX_FMT_RGB565:
  452. case PIX_FMT_BGR565:
  453. case PIX_FMT_RGB555:
  454. case PIX_FMT_BGR555: return yuv2rgb_c_16;
  455. case PIX_FMT_RGB8:
  456. case PIX_FMT_BGR8: return yuv2rgb_c_8_ordered_dither;
  457. case PIX_FMT_RGB4:
  458. case PIX_FMT_BGR4: return yuv2rgb_c_4_ordered_dither;
  459. case PIX_FMT_RGB4_BYTE:
  460. case PIX_FMT_BGR4_BYTE: return yuv2rgb_c_4b_ordered_dither;
  461. case PIX_FMT_MONOBLACK: return yuv2rgb_c_1_ordered_dither;
  462. default:
  463. assert(0);
  464. }
  465. return NULL;
  466. }
  467. static int div_round (int dividend, int divisor)
  468. {
  469. if (dividend > 0)
  470. return (dividend + (divisor>>1)) / divisor;
  471. else
  472. return -((-dividend + (divisor>>1)) / divisor);
  473. }
  474. int yuv2rgb_c_init_tables (SwsContext *c, const int inv_table[4], int fullRange, int brightness, int contrast, int saturation)
  475. {
  476. const int isRgb = c->dstFormat==PIX_FMT_RGB32
  477. || c->dstFormat==PIX_FMT_RGB32_1
  478. || c->dstFormat==PIX_FMT_BGR24
  479. || c->dstFormat==PIX_FMT_RGB565
  480. || c->dstFormat==PIX_FMT_RGB555
  481. || c->dstFormat==PIX_FMT_RGB8
  482. || c->dstFormat==PIX_FMT_RGB4
  483. || c->dstFormat==PIX_FMT_RGB4_BYTE
  484. || c->dstFormat==PIX_FMT_MONOBLACK;
  485. const int bpp = fmt_depth(c->dstFormat);
  486. int i, base;
  487. uint8_t table_Y[1024];
  488. uint32_t *table_32 = 0;
  489. uint16_t *table_16 = 0;
  490. uint8_t *table_8 = 0;
  491. uint8_t *table_332 = 0;
  492. uint8_t *table_121 = 0;
  493. uint8_t *table_1 = 0;
  494. int entry_size = 0;
  495. void *table_r = 0, *table_g = 0, *table_b = 0;
  496. void *table_start;
  497. int64_t crv = inv_table[0];
  498. int64_t cbu = inv_table[1];
  499. int64_t cgu = -inv_table[2];
  500. int64_t cgv = -inv_table[3];
  501. int64_t cy = 1<<16;
  502. int64_t oy = 0;
  503. //printf("%lld %lld %lld %lld %lld\n", cy, crv, cbu, cgu, cgv);
  504. if (!fullRange){
  505. cy= (cy*255) / 219;
  506. oy= 16<<16;
  507. }else{
  508. crv= (crv*224) / 255;
  509. cbu= (cbu*224) / 255;
  510. cgu= (cgu*224) / 255;
  511. cgv= (cgv*224) / 255;
  512. }
  513. cy = (cy *contrast )>>16;
  514. crv= (crv*contrast * saturation)>>32;
  515. cbu= (cbu*contrast * saturation)>>32;
  516. cgu= (cgu*contrast * saturation)>>32;
  517. cgv= (cgv*contrast * saturation)>>32;
  518. //printf("%lld %lld %lld %lld %lld\n", cy, crv, cbu, cgu, cgv);
  519. oy -= 256*brightness;
  520. for (i = 0; i < 1024; i++) {
  521. int j;
  522. j= (cy*(((i - 384)<<16) - oy) + (1<<31))>>32;
  523. j = (j < 0) ? 0 : ((j > 255) ? 255 : j);
  524. table_Y[i] = j;
  525. }
  526. switch (bpp) {
  527. case 32:
  528. table_start= table_32 = av_malloc ((197 + 2*682 + 256 + 132) * sizeof (uint32_t));
  529. base= (c->dstFormat == PIX_FMT_RGB32_1 || c->dstFormat == PIX_FMT_BGR32_1) ? 8 : 0;
  530. entry_size = sizeof (uint32_t);
  531. table_r = table_32 + 197;
  532. table_b = table_32 + 197 + 685;
  533. table_g = table_32 + 197 + 2*682;
  534. for (i = -197; i < 256+197; i++)
  535. ((uint32_t *)table_r)[i] = table_Y[i+384] << ((isRgb ? 16 : 0) + base);
  536. for (i = -132; i < 256+132; i++)
  537. ((uint32_t *)table_g)[i] = table_Y[i+384] << (8 + base);
  538. for (i = -232; i < 256+232; i++)
  539. ((uint32_t *)table_b)[i] = table_Y[i+384] << ((isRgb ? 0 : 16) + base);
  540. break;
  541. case 24:
  542. table_start= table_8 = av_malloc ((256 + 2*232) * sizeof (uint8_t));
  543. entry_size = sizeof (uint8_t);
  544. table_r = table_g = table_b = table_8 + 232;
  545. for (i = -232; i < 256+232; i++)
  546. ((uint8_t * )table_b)[i] = table_Y[i+384];
  547. break;
  548. case 15:
  549. case 16:
  550. table_start= table_16 = av_malloc ((197 + 2*682 + 256 + 132) * sizeof (uint16_t));
  551. entry_size = sizeof (uint16_t);
  552. table_r = table_16 + 197;
  553. table_b = table_16 + 197 + 685;
  554. table_g = table_16 + 197 + 2*682;
  555. for (i = -197; i < 256+197; i++) {
  556. int j = table_Y[i+384] >> 3;
  557. if (isRgb)
  558. j <<= ((bpp==16) ? 11 : 10);
  559. ((uint16_t *)table_r)[i] = j;
  560. }
  561. for (i = -132; i < 256+132; i++) {
  562. int j = table_Y[i+384] >> ((bpp==16) ? 2 : 3);
  563. ((uint16_t *)table_g)[i] = j << 5;
  564. }
  565. for (i = -232; i < 256+232; i++) {
  566. int j = table_Y[i+384] >> 3;
  567. if (!isRgb)
  568. j <<= ((bpp==16) ? 11 : 10);
  569. ((uint16_t *)table_b)[i] = j;
  570. }
  571. break;
  572. case 8:
  573. table_start= table_332 = av_malloc ((197 + 2*682 + 256 + 132) * sizeof (uint8_t));
  574. entry_size = sizeof (uint8_t);
  575. table_r = table_332 + 197;
  576. table_b = table_332 + 197 + 685;
  577. table_g = table_332 + 197 + 2*682;
  578. for (i = -197; i < 256+197; i++) {
  579. int j = (table_Y[i+384 - 16] + 18)/36;
  580. if (isRgb)
  581. j <<= 5;
  582. ((uint8_t *)table_r)[i] = j;
  583. }
  584. for (i = -132; i < 256+132; i++) {
  585. int j = (table_Y[i+384 - 16] + 18)/36;
  586. if (!isRgb)
  587. j <<= 1;
  588. ((uint8_t *)table_g)[i] = j << 2;
  589. }
  590. for (i = -232; i < 256+232; i++) {
  591. int j = (table_Y[i+384 - 37] + 43)/85;
  592. if (!isRgb)
  593. j <<= 6;
  594. ((uint8_t *)table_b)[i] = j;
  595. }
  596. break;
  597. case 4:
  598. case 4|128:
  599. table_start= table_121 = av_malloc ((197 + 2*682 + 256 + 132) * sizeof (uint8_t));
  600. entry_size = sizeof (uint8_t);
  601. table_r = table_121 + 197;
  602. table_b = table_121 + 197 + 685;
  603. table_g = table_121 + 197 + 2*682;
  604. for (i = -197; i < 256+197; i++) {
  605. int j = table_Y[i+384 - 110] >> 7;
  606. if (isRgb)
  607. j <<= 3;
  608. ((uint8_t *)table_r)[i] = j;
  609. }
  610. for (i = -132; i < 256+132; i++) {
  611. int j = (table_Y[i+384 - 37]+ 43)/85;
  612. ((uint8_t *)table_g)[i] = j << 1;
  613. }
  614. for (i = -232; i < 256+232; i++) {
  615. int j =table_Y[i+384 - 110] >> 7;
  616. if (!isRgb)
  617. j <<= 3;
  618. ((uint8_t *)table_b)[i] = j;
  619. }
  620. break;
  621. case 1:
  622. table_start= table_1 = av_malloc (256*2 * sizeof (uint8_t));
  623. entry_size = sizeof (uint8_t);
  624. table_g = table_1;
  625. table_r = table_b = NULL;
  626. for (i = 0; i < 256+256; i++) {
  627. int j = table_Y[i + 384 - 110]>>7;
  628. ((uint8_t *)table_g)[i] = j;
  629. }
  630. break;
  631. default:
  632. table_start= NULL;
  633. av_log(c, AV_LOG_ERROR, "%ibpp not supported by yuv2rgb\n", bpp);
  634. //free mem?
  635. return -1;
  636. }
  637. for (i = 0; i < 256; i++) {
  638. c->table_rV[i] = (uint8_t *)table_r + entry_size * div_round (crv * (i-128), cy);
  639. c->table_gU[i] = (uint8_t *)table_g + entry_size * div_round (cgu * (i-128), cy);
  640. c->table_gV[i] = entry_size * div_round (cgv * (i-128), cy);
  641. c->table_bU[i] = (uint8_t *)table_b + entry_size * div_round (cbu * (i-128), cy);
  642. }
  643. av_free(c->yuvTable);
  644. c->yuvTable= table_start;
  645. return 0;
  646. }