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.

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