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.

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