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.

503 lines
12KB

  1. /*
  2. * yuv2rgb.c, Software YUV to RGB coverter
  3. *
  4. * Copyright (C) 1999, Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
  5. * All Rights Reserved.
  6. *
  7. * Functions broken out from display_x11.c and several new modes
  8. * added by HÃ¥kan Hjort <d95hjort@dtek.chalmers.se>
  9. *
  10. * 15 & 16 bpp support by Franck Sicard <Franck.Sicard@solsoft.fr>
  11. *
  12. * This file is part of mpeg2dec, a free MPEG-2 video decoder
  13. *
  14. * mpeg2dec is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2, or (at your option)
  17. * any later version.
  18. *
  19. * mpeg2dec is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with GNU Make; see the file COPYING. If not, write to
  26. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  27. *
  28. * MMX/MMX2 Template stuff from Michael Niedermayer (michaelni@gmx.at) (needed for fast movntq support)
  29. */
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <inttypes.h>
  33. #include "config.h"
  34. //#include "video_out.h"
  35. #include "rgb2rgb.h"
  36. #include "../cpudetect.h"
  37. #include "../mangle.h"
  38. #ifdef HAVE_MLIB
  39. #include "yuv2rgb_mlib.c"
  40. #endif
  41. #define DITHER1XBPP // only for mmx
  42. #ifdef ARCH_X86
  43. #define CAN_COMPILE_X86_ASM
  44. #endif
  45. #ifdef CAN_COMPILE_X86_ASM
  46. /* hope these constant values are cache line aligned */
  47. uint64_t __attribute__((aligned(8))) mmx_80w = 0x0080008000800080;
  48. uint64_t __attribute__((aligned(8))) mmx_10w = 0x1010101010101010;
  49. uint64_t __attribute__((aligned(8))) mmx_00ffw = 0x00ff00ff00ff00ff;
  50. uint64_t __attribute__((aligned(8))) mmx_Y_coeff = 0x253f253f253f253f;
  51. /* hope these constant values are cache line aligned */
  52. uint64_t __attribute__((aligned(8))) mmx_U_green = 0xf37df37df37df37d;
  53. uint64_t __attribute__((aligned(8))) mmx_U_blue = 0x4093409340934093;
  54. uint64_t __attribute__((aligned(8))) mmx_V_red = 0x3312331233123312;
  55. uint64_t __attribute__((aligned(8))) mmx_V_green = 0xe5fce5fce5fce5fc;
  56. /* hope these constant values are cache line aligned */
  57. uint64_t __attribute__((aligned(8))) mmx_redmask = 0xf8f8f8f8f8f8f8f8;
  58. uint64_t __attribute__((aligned(8))) mmx_grnmask = 0xfcfcfcfcfcfcfcfc;
  59. uint64_t __attribute__((aligned(8))) M24A= 0x00FF0000FF0000FFLL;
  60. uint64_t __attribute__((aligned(8))) M24B= 0xFF0000FF0000FF00LL;
  61. uint64_t __attribute__((aligned(8))) M24C= 0x0000FF0000FF0000LL;
  62. // the volatile is required because gcc otherwise optimizes some writes away not knowing that these
  63. // are read in the asm block
  64. volatile uint64_t __attribute__((aligned(8))) b5Dither;
  65. volatile uint64_t __attribute__((aligned(8))) g5Dither;
  66. volatile uint64_t __attribute__((aligned(8))) g6Dither;
  67. volatile uint64_t __attribute__((aligned(8))) r5Dither;
  68. uint64_t __attribute__((aligned(8))) dither4[2]={
  69. 0x0103010301030103LL,
  70. 0x0200020002000200LL,};
  71. uint64_t __attribute__((aligned(8))) dither8[2]={
  72. 0x0602060206020602LL,
  73. 0x0004000400040004LL,};
  74. #undef HAVE_MMX
  75. #undef ARCH_X86
  76. //MMX versions
  77. #undef RENAME
  78. #define HAVE_MMX
  79. #undef HAVE_MMX2
  80. #undef HAVE_3DNOW
  81. #define ARCH_X86
  82. #define RENAME(a) a ## _MMX
  83. #include "yuv2rgb_template.c"
  84. //MMX2 versions
  85. #undef RENAME
  86. #define HAVE_MMX
  87. #define HAVE_MMX2
  88. #undef HAVE_3DNOW
  89. #define ARCH_X86
  90. #define RENAME(a) a ## _MMX2
  91. #include "yuv2rgb_template.c"
  92. #endif // CAN_COMPILE_X86_ASM
  93. uint32_t matrix_coefficients = 6;
  94. const int32_t Inverse_Table_6_9[8][4] = {
  95. {117504, 138453, 13954, 34903}, /* no sequence_display_extension */
  96. {117504, 138453, 13954, 34903}, /* ITU-R Rec. 709 (1990) */
  97. {104597, 132201, 25675, 53279}, /* unspecified */
  98. {104597, 132201, 25675, 53279}, /* reserved */
  99. {104448, 132798, 24759, 53109}, /* FCC */
  100. {104597, 132201, 25675, 53279}, /* ITU-R Rec. 624-4 System B, G */
  101. {104597, 132201, 25675, 53279}, /* SMPTE 170M */
  102. {117579, 136230, 16907, 35559} /* SMPTE 240M (1987) */
  103. };
  104. static void yuv2rgb_c_init (int bpp, int mode);
  105. yuv2rgb_fun yuv2rgb;
  106. static void (* yuv2rgb_c_internal) (uint8_t *, uint8_t *,
  107. uint8_t *, uint8_t *,
  108. void *, void *, int);
  109. static void yuv2rgb_c (void * dst, uint8_t * py,
  110. uint8_t * pu, uint8_t * pv,
  111. int h_size, int v_size,
  112. int rgb_stride, int y_stride, int uv_stride)
  113. {
  114. v_size >>= 1;
  115. while (v_size--) {
  116. yuv2rgb_c_internal (py, py + y_stride, pu, pv, dst, dst + rgb_stride,
  117. h_size);
  118. py += 2 * y_stride;
  119. pu += uv_stride;
  120. pv += uv_stride;
  121. dst += 2 * rgb_stride;
  122. }
  123. }
  124. void yuv2rgb_init (int bpp, int mode)
  125. {
  126. yuv2rgb = NULL;
  127. #ifdef CAN_COMPILE_X86_ASM
  128. if(gCpuCaps.hasMMX2)
  129. {
  130. if (yuv2rgb == NULL /*&& (config.flags & VO_MMX_ENABLE)*/) {
  131. yuv2rgb = yuv2rgb_init_MMX2 (bpp, mode);
  132. if (yuv2rgb != NULL)
  133. printf ("Using MMX2 for colorspace transform\n");
  134. else
  135. printf ("Cannot init MMX2 colorspace transform\n");
  136. }
  137. }
  138. else if(gCpuCaps.hasMMX)
  139. {
  140. if (yuv2rgb == NULL /*&& (config.flags & VO_MMX_ENABLE)*/) {
  141. yuv2rgb = yuv2rgb_init_MMX (bpp, mode);
  142. if (yuv2rgb != NULL)
  143. printf ("Using MMX for colorspace transform\n");
  144. else
  145. printf ("Cannot init MMX colorspace transform\n");
  146. }
  147. }
  148. #endif
  149. #ifdef HAVE_MLIB
  150. if (yuv2rgb == NULL /*&& (config.flags & VO_MLIB_ENABLE)*/) {
  151. yuv2rgb = yuv2rgb_init_mlib (bpp, mode);
  152. if (yuv2rgb != NULL)
  153. printf ("Using mlib for colorspace transform\n");
  154. }
  155. #endif
  156. if (yuv2rgb == NULL) {
  157. printf ("No accelerated colorspace conversion found\n");
  158. yuv2rgb_c_init (bpp, mode);
  159. yuv2rgb = (yuv2rgb_fun)yuv2rgb_c;
  160. }
  161. }
  162. void * table_rV[256];
  163. void * table_gU[256];
  164. int table_gV[256];
  165. void * table_bU[256];
  166. #define RGB(i) \
  167. U = pu[i]; \
  168. V = pv[i]; \
  169. r = table_rV[V]; \
  170. g = table_gU[U] + table_gV[V]; \
  171. b = table_bU[U];
  172. #define DST1(i) \
  173. Y = py_1[2*i]; \
  174. dst_1[2*i] = r[Y] + g[Y] + b[Y]; \
  175. Y = py_1[2*i+1]; \
  176. dst_1[2*i+1] = r[Y] + g[Y] + b[Y];
  177. #define DST2(i) \
  178. Y = py_2[2*i]; \
  179. dst_2[2*i] = r[Y] + g[Y] + b[Y]; \
  180. Y = py_2[2*i+1]; \
  181. dst_2[2*i+1] = r[Y] + g[Y] + b[Y];
  182. #define DST1RGB(i) \
  183. Y = py_1[2*i]; \
  184. dst_1[6*i] = r[Y]; dst_1[6*i+1] = g[Y]; dst_1[6*i+2] = b[Y]; \
  185. Y = py_1[2*i+1]; \
  186. dst_1[6*i+3] = r[Y]; dst_1[6*i+4] = g[Y]; dst_1[6*i+5] = b[Y];
  187. #define DST2RGB(i) \
  188. Y = py_2[2*i]; \
  189. dst_2[6*i] = r[Y]; dst_2[6*i+1] = g[Y]; dst_2[6*i+2] = b[Y]; \
  190. Y = py_2[2*i+1]; \
  191. dst_2[6*i+3] = r[Y]; dst_2[6*i+4] = g[Y]; dst_2[6*i+5] = b[Y];
  192. #define DST1BGR(i) \
  193. Y = py_1[2*i]; \
  194. dst_1[6*i] = b[Y]; dst_1[6*i+1] = g[Y]; dst_1[6*i+2] = r[Y]; \
  195. Y = py_1[2*i+1]; \
  196. dst_1[6*i+3] = b[Y]; dst_1[6*i+4] = g[Y]; dst_1[6*i+5] = r[Y];
  197. #define DST2BGR(i) \
  198. Y = py_2[2*i]; \
  199. dst_2[6*i] = b[Y]; dst_2[6*i+1] = g[Y]; dst_2[6*i+2] = r[Y]; \
  200. Y = py_2[2*i+1]; \
  201. dst_2[6*i+3] = b[Y]; dst_2[6*i+4] = g[Y]; dst_2[6*i+5] = r[Y];
  202. static void yuv2rgb_c_32 (uint8_t * py_1, uint8_t * py_2,
  203. uint8_t * pu, uint8_t * pv,
  204. void * _dst_1, void * _dst_2, int h_size)
  205. {
  206. int U, V, Y;
  207. uint32_t * r, * g, * b;
  208. uint32_t * dst_1, * dst_2;
  209. h_size >>= 3;
  210. dst_1 = _dst_1;
  211. dst_2 = _dst_2;
  212. while (h_size--) {
  213. RGB(0);
  214. DST1(0);
  215. DST2(0);
  216. RGB(1);
  217. DST2(1);
  218. DST1(1);
  219. RGB(2);
  220. DST1(2);
  221. DST2(2);
  222. RGB(3);
  223. DST2(3);
  224. DST1(3);
  225. pu += 4;
  226. pv += 4;
  227. py_1 += 8;
  228. py_2 += 8;
  229. dst_1 += 8;
  230. dst_2 += 8;
  231. }
  232. }
  233. // This is very near from the yuv2rgb_c_32 code
  234. static void yuv2rgb_c_24_rgb (uint8_t * py_1, uint8_t * py_2,
  235. uint8_t * pu, uint8_t * pv,
  236. void * _dst_1, void * _dst_2, int h_size)
  237. {
  238. int U, V, Y;
  239. uint8_t * r, * g, * b;
  240. uint8_t * dst_1, * dst_2;
  241. h_size >>= 3;
  242. dst_1 = _dst_1;
  243. dst_2 = _dst_2;
  244. while (h_size--) {
  245. RGB(0);
  246. DST1RGB(0);
  247. DST2RGB(0);
  248. RGB(1);
  249. DST2RGB(1);
  250. DST1RGB(1);
  251. RGB(2);
  252. DST1RGB(2);
  253. DST2RGB(2);
  254. RGB(3);
  255. DST2RGB(3);
  256. DST1RGB(3);
  257. pu += 4;
  258. pv += 4;
  259. py_1 += 8;
  260. py_2 += 8;
  261. dst_1 += 24;
  262. dst_2 += 24;
  263. }
  264. }
  265. // only trivial mods from yuv2rgb_c_24_rgb
  266. static void yuv2rgb_c_24_bgr (uint8_t * py_1, uint8_t * py_2,
  267. uint8_t * pu, uint8_t * pv,
  268. void * _dst_1, void * _dst_2, int h_size)
  269. {
  270. int U, V, Y;
  271. uint8_t * r, * g, * b;
  272. uint8_t * dst_1, * dst_2;
  273. h_size >>= 3;
  274. dst_1 = _dst_1;
  275. dst_2 = _dst_2;
  276. while (h_size--) {
  277. RGB(0);
  278. DST1BGR(0);
  279. DST2BGR(0);
  280. RGB(1);
  281. DST2BGR(1);
  282. DST1BGR(1);
  283. RGB(2);
  284. DST1BGR(2);
  285. DST2BGR(2);
  286. RGB(3);
  287. DST2BGR(3);
  288. DST1BGR(3);
  289. pu += 4;
  290. pv += 4;
  291. py_1 += 8;
  292. py_2 += 8;
  293. dst_1 += 24;
  294. dst_2 += 24;
  295. }
  296. }
  297. // This is exactly the same code as yuv2rgb_c_32 except for the types of
  298. // r, g, b, dst_1, dst_2
  299. static void yuv2rgb_c_16 (uint8_t * py_1, uint8_t * py_2,
  300. uint8_t * pu, uint8_t * pv,
  301. void * _dst_1, void * _dst_2, int h_size)
  302. {
  303. int U, V, Y;
  304. uint16_t * r, * g, * b;
  305. uint16_t * dst_1, * dst_2;
  306. h_size >>= 3;
  307. dst_1 = _dst_1;
  308. dst_2 = _dst_2;
  309. while (h_size--) {
  310. RGB(0);
  311. DST1(0);
  312. DST2(0);
  313. RGB(1);
  314. DST2(1);
  315. DST1(1);
  316. RGB(2);
  317. DST1(2);
  318. DST2(2);
  319. RGB(3);
  320. DST2(3);
  321. DST1(3);
  322. pu += 4;
  323. pv += 4;
  324. py_1 += 8;
  325. py_2 += 8;
  326. dst_1 += 8;
  327. dst_2 += 8;
  328. }
  329. }
  330. static int div_round (int dividend, int divisor)
  331. {
  332. if (dividend > 0)
  333. return (dividend + (divisor>>1)) / divisor;
  334. else
  335. return -((-dividend + (divisor>>1)) / divisor);
  336. }
  337. static void yuv2rgb_c_init (int bpp, int mode)
  338. {
  339. int i;
  340. uint8_t table_Y[1024];
  341. uint32_t *table_32 = 0;
  342. uint16_t *table_16 = 0;
  343. uint8_t *table_8 = 0;
  344. uint32_t entry_size = 0;
  345. void *table_r = 0, *table_g = 0, *table_b = 0;
  346. int crv = Inverse_Table_6_9[matrix_coefficients][0];
  347. int cbu = Inverse_Table_6_9[matrix_coefficients][1];
  348. int cgu = -Inverse_Table_6_9[matrix_coefficients][2];
  349. int cgv = -Inverse_Table_6_9[matrix_coefficients][3];
  350. for (i = 0; i < 1024; i++) {
  351. int j;
  352. j = (76309 * (i - 384 - 16) + 32768) >> 16;
  353. j = (j < 0) ? 0 : ((j > 255) ? 255 : j);
  354. table_Y[i] = j;
  355. }
  356. switch (bpp) {
  357. case 32:
  358. yuv2rgb_c_internal = yuv2rgb_c_32;
  359. table_32 = malloc ((197 + 2*682 + 256 + 132) * sizeof (uint32_t));
  360. entry_size = sizeof (uint32_t);
  361. table_r = table_32 + 197;
  362. table_b = table_32 + 197 + 685;
  363. table_g = table_32 + 197 + 2*682;
  364. for (i = -197; i < 256+197; i++)
  365. ((uint32_t *)table_r)[i] = table_Y[i+384] << ((mode==MODE_RGB) ? 16 : 0);
  366. for (i = -132; i < 256+132; i++)
  367. ((uint32_t *)table_g)[i] = table_Y[i+384] << 8;
  368. for (i = -232; i < 256+232; i++)
  369. ((uint32_t *)table_b)[i] = table_Y[i+384] << ((mode==MODE_RGB) ? 0 : 16);
  370. break;
  371. case 24:
  372. // yuv2rgb_c_internal = (mode==MODE_RGB) ? yuv2rgb_c_24_rgb : yuv2rgb_c_24_bgr;
  373. yuv2rgb_c_internal = (mode!=MODE_RGB) ? yuv2rgb_c_24_rgb : yuv2rgb_c_24_bgr;
  374. table_8 = malloc ((256 + 2*232) * sizeof (uint8_t));
  375. entry_size = sizeof (uint8_t);
  376. table_r = table_g = table_b = table_8 + 232;
  377. for (i = -232; i < 256+232; i++)
  378. ((uint8_t * )table_b)[i] = table_Y[i+384];
  379. break;
  380. case 15:
  381. case 16:
  382. yuv2rgb_c_internal = yuv2rgb_c_16;
  383. table_16 = malloc ((197 + 2*682 + 256 + 132) * sizeof (uint16_t));
  384. entry_size = sizeof (uint16_t);
  385. table_r = table_16 + 197;
  386. table_b = table_16 + 197 + 685;
  387. table_g = table_16 + 197 + 2*682;
  388. for (i = -197; i < 256+197; i++) {
  389. int j = table_Y[i+384] >> 3;
  390. if (mode == MODE_RGB)
  391. j <<= ((bpp==16) ? 11 : 10);
  392. ((uint16_t *)table_r)[i] = j;
  393. }
  394. for (i = -132; i < 256+132; i++) {
  395. int j = table_Y[i+384] >> ((bpp==16) ? 2 : 3);
  396. ((uint16_t *)table_g)[i] = j << 5;
  397. }
  398. for (i = -232; i < 256+232; i++) {
  399. int j = table_Y[i+384] >> 3;
  400. if (mode == MODE_BGR)
  401. j <<= ((bpp==16) ? 11 : 10);
  402. ((uint16_t *)table_b)[i] = j;
  403. }
  404. break;
  405. default:
  406. printf ("%ibpp not supported by yuv2rgb\n", bpp);
  407. //exit (1);
  408. }
  409. for (i = 0; i < 256; i++) {
  410. table_rV[i] = table_r + entry_size * div_round (crv * (i-128), 76309);
  411. table_gU[i] = table_g + entry_size * div_round (cgu * (i-128), 76309);
  412. table_gV[i] = entry_size * div_round (cgv * (i-128), 76309);
  413. table_bU[i] = table_b + entry_size * div_round (cbu * (i-128), 76309);
  414. }
  415. }