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.

450 lines
15KB

  1. /*
  2. * This is a (failed) attempt to re-implement the initialization
  3. * of the yuv2rgb conversion tables from scratch.
  4. * Unfortunately, it is still derived from yuv2rgb.c, so the license
  5. * header of that file applies:
  6. *
  7. * yuv2rgb.c, Software YUV to RGB coverter
  8. *
  9. * Copyright (C) 1999, Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
  10. * All Rights Reserved.
  11. *
  12. * Functions broken out from display_x11.c and several new modes
  13. * added by HÃ¥kan Hjort <d95hjort@dtek.chalmers.se>
  14. *
  15. * 15 & 16 bpp support by Franck Sicard <Franck.Sicard@solsoft.fr>
  16. *
  17. * MMX/MMX2 template stuff (needed for fast movntq support),
  18. * 1,4,8bpp support and context / deglobalize stuff
  19. * by Michael Niedermayer (michaelni@gmx.at)
  20. *
  21. * This file is part of mpeg2dec, a free MPEG-2 video decoder
  22. *
  23. * mpeg2dec is free software; you can redistribute it and/or modify
  24. * it under the terms of the GNU General Public License as published by
  25. * the Free Software Foundation; either version 2, or (at your option)
  26. * any later version.
  27. *
  28. * mpeg2dec is distributed in the hope that it will be useful,
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. * GNU General Public License for more details.
  32. *
  33. * You should have received a copy of the GNU General Public License
  34. * along with mpeg2dec; if not, write to the Free Software
  35. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  36. */
  37. #include "avutil.h"
  38. #include "swscale.h"
  39. #include "swscale_internal.h"
  40. #define YTABLE_MIN 384
  41. /**
  42. * YUV -> RGB conversion matrixes (inverse of table 6.9 in MPEG2 standard)
  43. *
  44. * An YUV -> RGB conversion matrix is in the form
  45. * | 1 0 Rv |
  46. * | 1 Gu Gv |
  47. * | 1 Bu 0 |
  48. *
  49. * Inverse_Table_6_9 stores | Rv Bu Gv Gu | * 255/224*2^16.
  50. * \arg Maximum Rv value: 117570
  51. * \arg Maximum Bu value: 138420
  52. * \arg Maximum Gv + Gu value: 25642 + 53281 = 78923
  53. *
  54. * These values are needed to allocate table_{r, g, b}. If you modify
  55. * this table, please update allocate_tables() accordingly
  56. */
  57. const int32_t Inverse_Table_6_9[8][4] = {
  58. {0, 0, 0, 0}, /* no sequence_display_extension */
  59. {117500, 138420, -13985, -34933}, /* ITU-R Rec. 709 (1990) */
  60. {0, 0, 0, 0}, /* unspecified */
  61. {0, 0, 0, 0}, /* reserved */
  62. {104480, 132820, -24811, -53150}, /* FCC */
  63. {104570, 132210, -25642, -53281}, /* ITU-R Rec. 624-4 System B, G */
  64. {104570, 132210, -25642, -53281}, /* SMPTE 170M */
  65. {117570, 136230, -16892, -35552} /* SMPTE 240M (1987) */
  66. };
  67. /**
  68. * Dithering matrixes (these are bayer ordered dither matrixes
  69. * with some manual changes by Michael)
  70. */
  71. const uint8_t __attribute__((aligned(8))) dither_2x2_4[2][8]={
  72. { 1, 3, 1, 3, 1, 3, 1, 3, },
  73. { 2, 0, 2, 0, 2, 0, 2, 0, },
  74. };
  75. const uint8_t __attribute__((aligned(8))) dither_2x2_8[2][8]={
  76. { 6, 2, 6, 2, 6, 2, 6, 2, },
  77. { 0, 4, 0, 4, 0, 4, 0, 4, },
  78. };
  79. const uint8_t __attribute__((aligned(8))) dither_8x8_32[8][8]={
  80. { 17, 9, 23, 15, 16, 8, 22, 14, },
  81. { 5, 29, 3, 27, 4, 28, 2, 26, },
  82. { 21, 13, 19, 11, 20, 12, 18, 10, },
  83. { 0, 24, 6, 30, 1, 25, 7, 31, },
  84. { 16, 8, 22, 14, 17, 9, 23, 15, },
  85. { 4, 28, 2, 26, 5, 29, 3, 27, },
  86. { 20, 12, 18, 10, 21, 13, 19, 11, },
  87. { 1, 25, 7, 31, 0, 24, 6, 30, },
  88. };
  89. #if 0
  90. const uint8_t __attribute__((aligned(8))) dither_8x8_64[8][8]={
  91. { 0, 48, 12, 60, 3, 51, 15, 63, },
  92. { 32, 16, 44, 28, 35, 19, 47, 31, },
  93. { 8, 56, 4, 52, 11, 59, 7, 55, },
  94. { 40, 24, 36, 20, 43, 27, 39, 23, },
  95. { 2, 50, 14, 62, 1, 49, 13, 61, },
  96. { 34, 18, 46, 30, 33, 17, 45, 29, },
  97. { 10, 58, 6, 54, 9, 57, 5, 53, },
  98. { 42, 26, 38, 22, 41, 25, 37, 21, },
  99. };
  100. #endif
  101. const uint8_t __attribute__((aligned(8))) dither_8x8_73[8][8]={
  102. { 0, 55, 14, 68, 3, 58, 17, 72, },
  103. { 37, 18, 50, 32, 40, 22, 54, 35, },
  104. { 9, 64, 5, 59, 13, 67, 8, 63, },
  105. { 46, 27, 41, 23, 49, 31, 44, 26, },
  106. { 2, 57, 16, 71, 1, 56, 15, 70, },
  107. { 39, 21, 52, 34, 38, 19, 51, 33, },
  108. { 11, 66, 7, 62, 10, 65, 6, 60, },
  109. { 48, 30, 43, 25, 47, 29, 42, 24, },
  110. };
  111. #if 0
  112. const uint8_t __attribute__((aligned(8))) dither_8x8_128[8][8]={
  113. { 68, 36, 92, 60, 66, 34, 90, 58, },
  114. { 20, 116, 12, 108, 18, 114, 10, 106, },
  115. { 84, 52, 76, 44, 82, 50, 74, 42, },
  116. { 0, 96, 24, 120, 6, 102, 30, 126, },
  117. { 64, 32, 88, 56, 70, 38, 94, 62, },
  118. { 16, 112, 8, 104, 22, 118, 14, 110, },
  119. { 80, 48, 72, 40, 86, 54, 78, 46, },
  120. { 4, 100, 28, 124, 2, 98, 26, 122, },
  121. };
  122. #endif
  123. #if 1
  124. const uint8_t __attribute__((aligned(8))) dither_8x8_220[8][8]={
  125. {117, 62, 158, 103, 113, 58, 155, 100, },
  126. { 34, 199, 21, 186, 31, 196, 17, 182, },
  127. {144, 89, 131, 76, 141, 86, 127, 72, },
  128. { 0, 165, 41, 206, 10, 175, 52, 217, },
  129. {110, 55, 151, 96, 120, 65, 162, 107, },
  130. { 28, 193, 14, 179, 38, 203, 24, 189, },
  131. {138, 83, 124, 69, 148, 93, 134, 79, },
  132. { 7, 172, 48, 213, 3, 168, 45, 210, },
  133. };
  134. #elif 1
  135. // tries to correct a gamma of 1.5
  136. const uint8_t __attribute__((aligned(8))) dither_8x8_220[8][8]={
  137. { 0, 143, 18, 200, 2, 156, 25, 215, },
  138. { 78, 28, 125, 64, 89, 36, 138, 74, },
  139. { 10, 180, 3, 161, 16, 195, 8, 175, },
  140. {109, 51, 93, 38, 121, 60, 105, 47, },
  141. { 1, 152, 23, 210, 0, 147, 20, 205, },
  142. { 85, 33, 134, 71, 81, 30, 130, 67, },
  143. { 14, 190, 6, 171, 12, 185, 5, 166, },
  144. {117, 57, 101, 44, 113, 54, 97, 41, },
  145. };
  146. #elif 1
  147. // tries to correct a gamma of 2.0
  148. const uint8_t __attribute__((aligned(8))) dither_8x8_220[8][8]={
  149. { 0, 124, 8, 193, 0, 140, 12, 213, },
  150. { 55, 14, 104, 42, 66, 19, 119, 52, },
  151. { 3, 168, 1, 145, 6, 187, 3, 162, },
  152. { 86, 31, 70, 21, 99, 39, 82, 28, },
  153. { 0, 134, 11, 206, 0, 129, 9, 200, },
  154. { 62, 17, 114, 48, 58, 16, 109, 45, },
  155. { 5, 181, 2, 157, 4, 175, 1, 151, },
  156. { 95, 36, 78, 26, 90, 34, 74, 24, },
  157. };
  158. #else
  159. // tries to correct a gamma of 2.5
  160. const uint8_t __attribute__((aligned(8))) dither_8x8_220[8][8]={
  161. { 0, 107, 3, 187, 0, 125, 6, 212, },
  162. { 39, 7, 86, 28, 49, 11, 102, 36, },
  163. { 1, 158, 0, 131, 3, 180, 1, 151, },
  164. { 68, 19, 52, 12, 81, 25, 64, 17, },
  165. { 0, 119, 5, 203, 0, 113, 4, 195, },
  166. { 45, 9, 96, 33, 42, 8, 91, 30, },
  167. { 2, 172, 1, 144, 2, 165, 0, 137, },
  168. { 77, 23, 60, 15, 72, 21, 56, 14, },
  169. };
  170. #endif
  171. static int get_entry_size(int bpp)
  172. {
  173. switch(bpp) {
  174. case 32:
  175. return 4;
  176. case 16:
  177. case 15:
  178. return 2;
  179. case 24:
  180. case 8:
  181. case 4:
  182. case 1:
  183. return 1;
  184. default:
  185. return -1;
  186. }
  187. }
  188. /**
  189. * Allocate table_r, table_g, and table_b
  190. *
  191. * For cache efficency reasons, these three tables are allocated
  192. * together, so that they are contiguous in memory
  193. *
  194. * table_r is indexed in the range
  195. * [-128 * 117570 / 76309, 255 + 127 * 117570 / 76309] =
  196. * [-197.21, 451.67] ---> [-198, 452]
  197. * table_b is indexed in the range
  198. * [-128 * 138420 / 76309, 255 + 127 * 138420 / 76309] =
  199. * [232.18, 485.37] ---> [-233, 486]
  200. * table_g is indexed in the range
  201. * [-128 * 78923 / 76309, 255 + 127 * 78923 / 76309] =
  202. * [-132.38, 386.35] ---> [-133, 387]
  203. *
  204. * Please look at the comments after Inverse_Table_6_9 to see where these
  205. * numbers are coming from.
  206. */
  207. static void *allocate_tables(uint8_t **table_r, uint8_t **table_g, uint8_t **table_b, int bpp)
  208. {
  209. uint8_t *table;
  210. int entry_size;
  211. entry_size = get_entry_size(bpp);
  212. /* First allocate the memory... */
  213. switch (bpp) {
  214. case 32:
  215. case 15:
  216. case 16:
  217. case 8:
  218. case 4:
  219. table = av_malloc((198 + 452 + 233 + 486 + 133 + 387) * entry_size);
  220. break;
  221. case 24:
  222. table = av_malloc(256 + 2 * 233);
  223. break;
  224. case 1:
  225. table = av_malloc (256 * 2);
  226. break;
  227. default:
  228. table = NULL;
  229. }
  230. if (table == NULL) {
  231. MSG_ERR("Cannot allocate memory for the YUV -> RGB tables!\n");
  232. return NULL;
  233. }
  234. /* ...and then, assign the table_* value */
  235. switch (bpp) {
  236. case 32:
  237. case 15:
  238. case 16:
  239. case 8:
  240. case 4:
  241. *table_r = table + 198 * entry_size;
  242. *table_b = table + (198 + 452 + 133 + 387 + 233) * entry_size;
  243. *table_g = table + (198 + 452 + 133) * entry_size;
  244. break;
  245. case 24:
  246. *table_r = *table_g = *table_b = table + 233;
  247. break;
  248. case 1:
  249. *table_g = table;
  250. *table_r = *table_b = NULL;
  251. break;
  252. }
  253. return table;
  254. }
  255. /**
  256. * Initialize the table_rV, table_gU[i], table_gV, and table_bU fields
  257. * in SwsContext
  258. *
  259. * @param inv_table the YUV -> RGB table (this is a line of Inverse_Table_6_9)
  260. * @param fullRange 0->MPEG YUV space 1->JPEG YUV space
  261. */
  262. int yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4], int fullRange, int brightness, int contrast, int saturation)
  263. {
  264. int i;
  265. static uint8_t ytable[1024];
  266. int64_t cy, oy;
  267. int64_t crv, cbu, cgu, cgv;
  268. int entry_size = 0;
  269. uint8_t *table_r, *table_g, *table_b;
  270. int value;
  271. if ((inv_table[0] == 0) || (inv_table[1] == 0) || (inv_table[2] == 0) || (inv_table[3] == 0)) {
  272. MSG_ERR("Invalid YUV ---> RGB table!\n");
  273. return -1;
  274. }
  275. crv = inv_table[0];
  276. cbu = inv_table[1];
  277. cgu = inv_table[2];
  278. cgv = inv_table[3];
  279. if (fullRange) {
  280. cy = 1 << 16;
  281. oy = 0;
  282. crv= (crv*224) / 255;
  283. cbu= (cbu*224) / 255;
  284. cgu= (cgu*224) / 255;
  285. cgv= (cgv*224) / 255;
  286. //FIXME maybe it is cleaner if the tables where based on full range (*244/255)
  287. } else {
  288. cy = ((1 << 16) * 255) / 219;
  289. oy= 16 << 16;
  290. }
  291. cy = (cy *contrast )>>16;
  292. crv= (crv*contrast * saturation)>>32;
  293. cbu= (cbu*contrast * saturation)>>32;
  294. cgu= (cgu*contrast * saturation)>>32;
  295. cgv= (cgv*contrast * saturation)>>32;
  296. oy -= 256*brightness;
  297. for (i = 0; i < 1024; i++) {
  298. value = (cy*(((i - YTABLE_MIN)<<16) - oy) + (1<<31))>>32;
  299. ytable[i] = av_clip_uint8(value);
  300. }
  301. entry_size = get_entry_size(fmt_depth(c->dstFormat));
  302. av_free(c->yuvTable);
  303. c->yuvTable = allocate_tables(&table_r, &table_g, &table_b, fmt_depth(c->dstFormat));
  304. if (c->yuvTable == NULL) {
  305. return -1;
  306. }
  307. switch (fmt_depth(c->dstFormat)) {
  308. case 32:
  309. for (i = -198; i < 256 + 197; i++) {
  310. value = ytable[i + YTABLE_MIN];
  311. if (isBGR(c->dstFormat)) {
  312. value <<= 16;
  313. }
  314. ((uint32_t *)table_r)[i] = value;
  315. }
  316. for (i = -133; i < 256 + 132; i++) {
  317. ((uint32_t *)table_g)[i] = ytable[i + YTABLE_MIN] << 8;
  318. }
  319. for (i = -233; i < 256 + 232; i++) {
  320. value = ytable[i + YTABLE_MIN];
  321. if (!isBGR(c->dstFormat)) {
  322. value <<= 16;
  323. }
  324. ((uint32_t *)table_b)[i] = value;
  325. }
  326. break;
  327. case 24:
  328. for (i = -233; i < 256 + 232; i++) {
  329. ((uint8_t * )table_b)[i] = ytable[i + YTABLE_MIN];
  330. }
  331. break;
  332. case 15:
  333. case 16:
  334. for (i = -198; i < 256 + 197; i++) {
  335. value = ytable[i + YTABLE_MIN] >> 3;
  336. if (isBGR(c->dstFormat)) {
  337. value <<= ((fmt_depth(c->dstFormat) == 16) ? 11 : 10);
  338. }
  339. ((uint16_t *)table_r)[i] = value;
  340. }
  341. for (i = -133; i < 256 + 132; i++) {
  342. value = ytable[i + YTABLE_MIN];
  343. value >>= ((fmt_depth(c->dstFormat) == 16) ? 2 : 3);
  344. ((uint16_t *)table_g)[i] = value << 5;
  345. }
  346. for (i = -233; i < 256 + 232; i++) {
  347. value = ytable[i + YTABLE_MIN] >> 3;
  348. if (!isBGR(c->dstFormat)) {
  349. value <<= ((fmt_depth(c->dstFormat) == 16) ? 11 : 10);
  350. }
  351. ((uint16_t *)table_b)[i] = value;
  352. }
  353. break;
  354. case 8:
  355. for (i = -198; i < 256 + 197; i++) {
  356. value = (ytable[i + YTABLE_MIN - 16] + 18) / 36;
  357. if (isBGR(c->dstFormat)) {
  358. value <<= 5;
  359. }
  360. ((uint8_t *)table_r)[i] = value;
  361. }
  362. for (i = -133; i < 256 + 132; i++) {
  363. value = (ytable[i + YTABLE_MIN - 16] + 18) / 36;
  364. if (!isBGR(c->dstFormat)) {
  365. value <<= 1;
  366. }
  367. ((uint8_t *)table_g)[i] = value << 2;
  368. }
  369. for (i = -233; i < 256 + 232; i++) {
  370. value = (ytable[i + YTABLE_MIN - 37] + 43) / 85;
  371. if (!isBGR(c->dstFormat)) {
  372. value <<= 6;
  373. }
  374. ((uint8_t *)table_b)[i] = value;
  375. }
  376. break;
  377. case 4:
  378. for (i = -198; i < 256 + 197; i++) {
  379. value = ytable[i + YTABLE_MIN - 110] >> 7;
  380. if (isBGR(c->dstFormat)) {
  381. value <<= 3;
  382. }
  383. ((uint8_t *)table_r)[i] = value;
  384. }
  385. for (i = -133; i < 256 + 132; i++) {
  386. value = (ytable[i + YTABLE_MIN - 37]+ 43) / 85;
  387. ((uint8_t *)table_g)[i] = value << 1;
  388. }
  389. for (i = -233; i < 256 + 232; i++) {
  390. value = ytable[i + YTABLE_MIN - 110] >> 7;
  391. if (!isBGR(c->dstFormat)) {
  392. value <<= 3;
  393. }
  394. ((uint8_t *)table_b)[i] = value;
  395. }
  396. break;
  397. case 1:
  398. for (i = 0; i < 256 + 256; i++) {
  399. value = ytable[i + YTABLE_MIN - 110] >> 7;
  400. ((uint8_t *)table_g)[i] = value;
  401. }
  402. break;
  403. default:
  404. MSG_ERR("%ibpp not supported by yuv2rgb\n", fmt_depth(c->dstFormat));
  405. av_free(c->yuvTable);
  406. c->yuvTable = NULL;
  407. return -1;
  408. }
  409. for (i = 0; i < 256; i++) {
  410. c->table_rV[i] = table_r +
  411. entry_size * ROUNDED_DIV(crv * (i - 128), 76309);
  412. c->table_gU[i] = table_g +
  413. entry_size * ROUNDED_DIV(cgu * (i - 128), 76309);
  414. c->table_gV[i] = entry_size * ROUNDED_DIV(cgv * (i - 128), 76309);
  415. c->table_bU[i] = table_b +
  416. entry_size * ROUNDED_DIV(cbu * (i - 128), 76309);
  417. }
  418. return 0;
  419. }