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.

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