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.

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