You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

792 lines
21KB

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