]> git.sesse.net Git - ffmpeg/blob - postproc/yuv2rgb.c
brightness / saturation / contrast / different yuv colorspace support for some yuv2rg...
[ffmpeg] / postproc / yuv2rgb.c
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  * 1,4,8bpp support by Michael Niedermayer (michaelni@gmx.at)
30  */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <inttypes.h>
35
36 #include "config.h"
37 //#include "video_out.h"
38 #include "rgb2rgb.h"
39 #include "../cpudetect.h"
40 #include "../mangle.h"
41 #include "../mp_msg.h"
42
43 #ifdef HAVE_MLIB
44 #include "yuv2rgb_mlib.c"
45 #endif
46
47 #define DITHER1XBPP // only for mmx
48
49 #ifdef ARCH_X86
50 #define CAN_COMPILE_X86_ASM
51 #endif
52
53 const uint8_t  __attribute__((aligned(8))) dither_2x2_4[2][8]={
54 {  1,   3,   1,   3,   1,   3,   1,   3, },
55 {  2,   0,   2,   0,   2,   0,   2,   0, },
56 };
57
58 const uint8_t  __attribute__((aligned(8))) dither_2x2_8[2][8]={
59 {  6,   2,   6,   2,   6,   2,   6,   2, },
60 {  0,   4,   0,   4,   0,   4,   0,   4, },
61 };
62
63 const uint8_t  __attribute__((aligned(8))) dither_8x8_32[8][8]={
64 { 17,   9,  23,  15,  16,   8,  22,  14, },
65 {  5,  29,   3,  27,   4,  28,   2,  26, },
66 { 21,  13,  19,  11,  20,  12,  18,  10, },
67 {  0,  24,   6,  30,   1,  25,   7,  31, },
68 { 16,   8,  22,  14,  17,   9,  23,  15, },
69 {  4,  28,   2,  26,   5,  29,   3,  27, },
70 { 20,  12,  18,  10,  21,  13,  19,  11, },
71 {  1,  25,   7,  31,   0,  24,   6,  30, },
72 };
73
74 #if 0
75 const uint8_t  __attribute__((aligned(8))) dither_8x8_64[8][8]={
76 {  0,  48,  12,  60,   3,  51,  15,  63, },
77 { 32,  16,  44,  28,  35,  19,  47,  31, },
78 {  8,  56,   4,  52,  11,  59,   7,  55, },
79 { 40,  24,  36,  20,  43,  27,  39,  23, },
80 {  2,  50,  14,  62,   1,  49,  13,  61, },
81 { 34,  18,  46,  30,  33,  17,  45,  29, },
82 { 10,  58,   6,  54,   9,  57,   5,  53, },
83 { 42,  26,  38,  22,  41,  25,  37,  21, },
84 };
85 #endif
86
87 const uint8_t  __attribute__((aligned(8))) dither_8x8_73[8][8]={
88 {  0,  55,  14,  68,   3,  58,  17,  72, },
89 { 37,  18,  50,  32,  40,  22,  54,  35, },
90 {  9,  64,   5,  59,  13,  67,   8,  63, },
91 { 46,  27,  41,  23,  49,  31,  44,  26, },
92 {  2,  57,  16,  71,   1,  56,  15,  70, },
93 { 39,  21,  52,  34,  38,  19,  51,  33, },
94 { 11,  66,   7,  62,  10,  65,   6,  60, },
95 { 48,  30,  43,  25,  47,  29,  42,  24, },
96 };
97
98 #if 0
99 const uint8_t  __attribute__((aligned(8))) dither_8x8_128[8][8]={
100 { 68,  36,  92,  60,  66,  34,  90,  58, },
101 { 20, 116,  12, 108,  18, 114,  10, 106, },
102 { 84,  52,  76,  44,  82,  50,  74,  42, },
103 {  0,  96,  24, 120,   6, 102,  30, 126, },
104 { 64,  32,  88,  56,  70,  38,  94,  62, },
105 { 16, 112,   8, 104,  22, 118,  14, 110, },
106 { 80,  48,  72,  40,  86,  54,  78,  46, },
107 {  4, 100,  28, 124,   2,  98,  26, 122, },
108 };
109 #endif
110
111 #if 1
112 const uint8_t  __attribute__((aligned(8))) dither_8x8_220[8][8]={
113 {117,  62, 158, 103, 113,  58, 155, 100, },
114 { 34, 199,  21, 186,  31, 196,  17, 182, },
115 {144,  89, 131,  76, 141,  86, 127,  72, },
116 {  0, 165,  41, 206,  10, 175,  52, 217, },
117 {110,  55, 151,  96, 120,  65, 162, 107, },
118 { 28, 193,  14, 179,  38, 203,  24, 189, },
119 {138,  83, 124,  69, 148,  93, 134,  79, },
120 {  7, 172,  48, 213,   3, 168,  45, 210, },
121 };
122 #elif 1
123 // tries to correct a gamma of 1.5
124 const uint8_t  __attribute__((aligned(8))) dither_8x8_220[8][8]={
125 {  0, 143,  18, 200,   2, 156,  25, 215, },
126 { 78,  28, 125,  64,  89,  36, 138,  74, },
127 { 10, 180,   3, 161,  16, 195,   8, 175, },
128 {109,  51,  93,  38, 121,  60, 105,  47, },
129 {  1, 152,  23, 210,   0, 147,  20, 205, },
130 { 85,  33, 134,  71,  81,  30, 130,  67, },
131 { 14, 190,   6, 171,  12, 185,   5, 166, },
132 {117,  57, 101,  44, 113,  54,  97,  41, },
133 };
134 #elif 1
135 // tries to correct a gamma of 2.0
136 const uint8_t  __attribute__((aligned(8))) dither_8x8_220[8][8]={
137 {  0, 124,   8, 193,   0, 140,  12, 213, },
138 { 55,  14, 104,  42,  66,  19, 119,  52, },
139 {  3, 168,   1, 145,   6, 187,   3, 162, },
140 { 86,  31,  70,  21,  99,  39,  82,  28, },
141 {  0, 134,  11, 206,   0, 129,   9, 200, },
142 { 62,  17, 114,  48,  58,  16, 109,  45, },
143 {  5, 181,   2, 157,   4, 175,   1, 151, },
144 { 95,  36,  78,  26,  90,  34,  74,  24, },
145 };
146 #else
147 // tries to correct a gamma of 2.5
148 const uint8_t  __attribute__((aligned(8))) dither_8x8_220[8][8]={
149 {  0, 107,   3, 187,   0, 125,   6, 212, },
150 { 39,   7,  86,  28,  49,  11, 102,  36, },
151 {  1, 158,   0, 131,   3, 180,   1, 151, },
152 { 68,  19,  52,  12,  81,  25,  64,  17, },
153 {  0, 119,   5, 203,   0, 113,   4, 195, },
154 { 45,   9,  96,  33,  42,   8,  91,  30, },
155 {  2, 172,   1, 144,   2, 165,   0, 137, },
156 { 77,  23,  60,  15,  72,  21,  56,  14, },
157 };
158 #endif
159
160 #ifdef CAN_COMPILE_X86_ASM
161
162 /* hope these constant values are cache line aligned */
163 uint64_t __attribute__((aligned(8))) mmx_80w = 0x0080008000800080;
164 uint64_t __attribute__((aligned(8))) mmx_10w = 0x1010101010101010;
165 uint64_t __attribute__((aligned(8))) mmx_00ffw = 0x00ff00ff00ff00ff;
166 uint64_t __attribute__((aligned(8))) mmx_Y_coeff = 0x253f253f253f253f;
167
168 /* hope these constant values are cache line aligned */
169 uint64_t __attribute__((aligned(8))) mmx_U_green = 0xf37df37df37df37d;
170 uint64_t __attribute__((aligned(8))) mmx_U_blue = 0x4093409340934093;
171 uint64_t __attribute__((aligned(8))) mmx_V_red = 0x3312331233123312;
172 uint64_t __attribute__((aligned(8))) mmx_V_green = 0xe5fce5fce5fce5fc;
173
174 /* hope these constant values are cache line aligned */
175 uint64_t __attribute__((aligned(8))) mmx_redmask = 0xf8f8f8f8f8f8f8f8;
176 uint64_t __attribute__((aligned(8))) mmx_grnmask = 0xfcfcfcfcfcfcfcfc;
177
178 uint64_t __attribute__((aligned(8))) M24A=   0x00FF0000FF0000FFLL;
179 uint64_t __attribute__((aligned(8))) M24B=   0xFF0000FF0000FF00LL;
180 uint64_t __attribute__((aligned(8))) M24C=   0x0000FF0000FF0000LL;
181
182 // the volatile is required because gcc otherwise optimizes some writes away not knowing that these
183 // are read in the asm block
184 volatile uint64_t __attribute__((aligned(8))) b5Dither;
185 volatile uint64_t __attribute__((aligned(8))) g5Dither;
186 volatile uint64_t __attribute__((aligned(8))) g6Dither;
187 volatile uint64_t __attribute__((aligned(8))) r5Dither;
188
189 uint64_t __attribute__((aligned(8))) dither4[2]={
190         0x0103010301030103LL,
191         0x0200020002000200LL,};
192
193 uint64_t __attribute__((aligned(8))) dither8[2]={
194         0x0602060206020602LL,
195         0x0004000400040004LL,};
196
197 #undef HAVE_MMX
198 #undef ARCH_X86
199
200 //MMX versions
201 #undef RENAME
202 #define HAVE_MMX
203 #undef HAVE_MMX2
204 #undef HAVE_3DNOW
205 #define ARCH_X86
206 #define RENAME(a) a ## _MMX
207 #include "yuv2rgb_template.c"
208
209 //MMX2 versions
210 #undef RENAME
211 #define HAVE_MMX
212 #define HAVE_MMX2
213 #undef HAVE_3DNOW
214 #define ARCH_X86
215 #define RENAME(a) a ## _MMX2
216 #include "yuv2rgb_template.c"
217
218 #endif // CAN_COMPILE_X86_ASM
219
220 uint32_t matrix_coefficients = 6;
221
222 const int32_t Inverse_Table_6_9[8][4] = {
223     {117504, 138453, 13954, 34903}, /* no sequence_display_extension */
224     {117504, 138453, 13954, 34903}, /* ITU-R Rec. 709 (1990) */
225     {104597, 132201, 25675, 53279}, /* unspecified */
226     {104597, 132201, 25675, 53279}, /* reserved */
227     {104448, 132798, 24759, 53109}, /* FCC */
228     {104597, 132201, 25675, 53279}, /* ITU-R Rec. 624-4 System B, G */
229     {104597, 132201, 25675, 53279}, /* SMPTE 170M */
230     {117579, 136230, 16907, 35559}  /* SMPTE 240M (1987) */
231 };
232
233 void *yuv2rgb_c_init (unsigned bpp, int mode, void *table_rV[256], void *table_gU[256], int table_gV[256], void *table_bU[256]);
234
235 yuv2rgb_fun yuv2rgb= NULL;
236
237 static void (* yuv2rgb_c_internal) (uint8_t *, uint8_t *,
238                                     uint8_t *, uint8_t *,
239                                     void *, void *, int, int);
240
241 static void yuv2rgb_c (void * dst, uint8_t * py,
242                        uint8_t * pu, uint8_t * pv,
243                        unsigned h_size, unsigned v_size,
244                        unsigned rgb_stride, unsigned y_stride, unsigned uv_stride)
245 {
246     v_size >>= 1;
247
248     while (v_size--) {
249         yuv2rgb_c_internal (py, py + y_stride, pu, pv, dst, dst + rgb_stride,
250                             h_size, v_size<<1);
251
252         py += 2 * y_stride;
253         pu += uv_stride;
254         pv += uv_stride;
255         dst += 2 * rgb_stride;
256     }
257 }
258
259 void * table_rV[256];
260 void * table_gU[256];
261 int table_gV[256];
262 void * table_bU[256];
263
264 void yuv2rgb_init (unsigned bpp, int mode)
265 {
266     if(yuv2rgb) return;
267 #ifdef CAN_COMPILE_X86_ASM
268     if(gCpuCaps.hasMMX2)
269     {
270         if (yuv2rgb == NULL /*&& (config.flags & VO_MMX_ENABLE)*/) {
271                 yuv2rgb = yuv2rgb_init_MMX2 (bpp, mode);
272                 if (yuv2rgb != NULL)
273                         mp_msg(MSGT_SWS,MSGL_INFO,"Using MMX2 for colorspace transform\n");
274                 else
275                         mp_msg(MSGT_SWS,MSGL_WARN,"Cannot init MMX2 colorspace transform\n");
276         }
277     }
278     else if(gCpuCaps.hasMMX)
279     {
280         if (yuv2rgb == NULL /*&& (config.flags & VO_MMX_ENABLE)*/) {
281                 yuv2rgb = yuv2rgb_init_MMX (bpp, mode);
282                 if (yuv2rgb != NULL)
283                         mp_msg(MSGT_SWS,MSGL_INFO,"Using MMX for colorspace transform\n");
284                 else
285                         mp_msg(MSGT_SWS,MSGL_WARN,"Cannot init MMX colorspace transform\n");
286         }
287     }
288 #endif
289 #ifdef HAVE_MLIB
290     if (yuv2rgb == NULL /*&& (config.flags & VO_MLIB_ENABLE)*/) {
291         yuv2rgb = yuv2rgb_init_mlib (bpp, mode);
292         if (yuv2rgb != NULL)
293             mp_msg(MSGT_SWS,MSGL_INFO,"Using mlib for colorspace transform\n");
294     }
295 #endif
296     if (yuv2rgb == NULL) {
297         mp_msg(MSGT_SWS,MSGL_INFO,"No accelerated colorspace conversion found\n");
298         yuv2rgb_c_init (bpp, mode, table_rV, table_gU, table_gV, table_bU);
299         yuv2rgb = (yuv2rgb_fun)yuv2rgb_c;
300     }
301 }
302
303 #define RGB(i)                                  \
304         U = pu[i];                              \
305         V = pv[i];                              \
306         r = table_rV[V];                        \
307         g = table_gU[U] + table_gV[V];          \
308         b = table_bU[U];
309
310 #define DST1(i)                                 \
311         Y = py_1[2*i];                          \
312         dst_1[2*i] = r[Y] + g[Y] + b[Y];        \
313         Y = py_1[2*i+1];                        \
314         dst_1[2*i+1] = r[Y] + g[Y] + b[Y];
315
316 #define DST2(i)                                 \
317         Y = py_2[2*i];                          \
318         dst_2[2*i] = r[Y] + g[Y] + b[Y];        \
319         Y = py_2[2*i+1];                        \
320         dst_2[2*i+1] = r[Y] + g[Y] + b[Y];
321
322 #define DST1RGB(i)                                                      \
323         Y = py_1[2*i];                                                  \
324         dst_1[6*i] = r[Y]; dst_1[6*i+1] = g[Y]; dst_1[6*i+2] = b[Y];    \
325         Y = py_1[2*i+1];                                                \
326         dst_1[6*i+3] = r[Y]; dst_1[6*i+4] = g[Y]; dst_1[6*i+5] = b[Y];
327
328 #define DST2RGB(i)                                                      \
329         Y = py_2[2*i];                                                  \
330         dst_2[6*i] = r[Y]; dst_2[6*i+1] = g[Y]; dst_2[6*i+2] = b[Y];    \
331         Y = py_2[2*i+1];                                                \
332         dst_2[6*i+3] = r[Y]; dst_2[6*i+4] = g[Y]; dst_2[6*i+5] = b[Y];
333
334 #define DST1BGR(i)                                                      \
335         Y = py_1[2*i];                                                  \
336         dst_1[6*i] = b[Y]; dst_1[6*i+1] = g[Y]; dst_1[6*i+2] = r[Y];    \
337         Y = py_1[2*i+1];                                                \
338         dst_1[6*i+3] = b[Y]; dst_1[6*i+4] = g[Y]; dst_1[6*i+5] = r[Y];
339
340 #define DST2BGR(i)                                                      \
341         Y = py_2[2*i];                                                  \
342         dst_2[6*i] = b[Y]; dst_2[6*i+1] = g[Y]; dst_2[6*i+2] = r[Y];    \
343         Y = py_2[2*i+1];                                                \
344         dst_2[6*i+3] = b[Y]; dst_2[6*i+4] = g[Y]; dst_2[6*i+5] = r[Y];
345
346 static void yuv2rgb_c_32 (uint8_t * py_1, uint8_t * py_2,
347                           uint8_t * pu, uint8_t * pv,
348                           void * _dst_1, void * _dst_2, int h_size, int v_pos)
349 {
350     int U, V, Y;
351     uint32_t * r, * g, * b;
352     uint32_t * dst_1, * dst_2;
353
354     h_size >>= 3;
355     dst_1 = _dst_1;
356     dst_2 = _dst_2;
357
358     while (h_size--) {
359         RGB(0);
360         DST1(0);
361         DST2(0);
362
363         RGB(1);
364         DST2(1);
365         DST1(1);
366
367         RGB(2);
368         DST1(2);
369         DST2(2);
370
371         RGB(3);
372         DST2(3);
373         DST1(3);
374
375         pu += 4;
376         pv += 4;
377         py_1 += 8;
378         py_2 += 8;
379         dst_1 += 8;
380         dst_2 += 8;
381     }
382 }
383
384 // This is very near from the yuv2rgb_c_32 code
385 static void yuv2rgb_c_24_rgb (uint8_t * py_1, uint8_t * py_2,
386                               uint8_t * pu, uint8_t * pv,
387                               void * _dst_1, void * _dst_2, int h_size, int v_pos)
388 {
389     int U, V, Y;
390     uint8_t * r, * g, * b;
391     uint8_t * dst_1, * dst_2;
392
393     h_size >>= 3;
394     dst_1 = _dst_1;
395     dst_2 = _dst_2;
396
397     while (h_size--) {
398         RGB(0);
399         DST1RGB(0);
400         DST2RGB(0);
401
402         RGB(1);
403         DST2RGB(1);
404         DST1RGB(1);
405
406         RGB(2);
407         DST1RGB(2);
408         DST2RGB(2);
409
410         RGB(3);
411         DST2RGB(3);
412         DST1RGB(3);
413
414         pu += 4;
415         pv += 4;
416         py_1 += 8;
417         py_2 += 8;
418         dst_1 += 24;
419         dst_2 += 24;
420     }
421 }
422
423 // only trivial mods from yuv2rgb_c_24_rgb
424 static void yuv2rgb_c_24_bgr (uint8_t * py_1, uint8_t * py_2,
425                               uint8_t * pu, uint8_t * pv,
426                               void * _dst_1, void * _dst_2, int h_size, int v_pos)
427 {
428     int U, V, Y;
429     uint8_t * r, * g, * b;
430     uint8_t * dst_1, * dst_2;
431
432     h_size >>= 3;
433     dst_1 = _dst_1;
434     dst_2 = _dst_2;
435
436     while (h_size--) {
437         RGB(0);
438         DST1BGR(0);
439         DST2BGR(0);
440
441         RGB(1);
442         DST2BGR(1);
443         DST1BGR(1);
444
445         RGB(2);
446         DST1BGR(2);
447         DST2BGR(2);
448
449         RGB(3);
450         DST2BGR(3);
451         DST1BGR(3);
452
453         pu += 4;
454         pv += 4;
455         py_1 += 8;
456         py_2 += 8;
457         dst_1 += 24;
458         dst_2 += 24;
459     }
460 }
461
462 // This is exactly the same code as yuv2rgb_c_32 except for the types of
463 // r, g, b, dst_1, dst_2
464 static void yuv2rgb_c_16 (uint8_t * py_1, uint8_t * py_2,
465                           uint8_t * pu, uint8_t * pv,
466                           void * _dst_1, void * _dst_2, int h_size, int v_pos)
467 {
468     int U, V, Y;
469     uint16_t * r, * g, * b;
470     uint16_t * dst_1, * dst_2;
471
472     h_size >>= 3;
473     dst_1 = _dst_1;
474     dst_2 = _dst_2;
475
476     while (h_size--) {
477         RGB(0);
478         DST1(0);
479         DST2(0);
480
481         RGB(1);
482         DST2(1);
483         DST1(1);
484
485         RGB(2);
486         DST1(2);
487         DST2(2);
488
489         RGB(3);
490         DST2(3);
491         DST1(3);
492
493         pu += 4;
494         pv += 4;
495         py_1 += 8;
496         py_2 += 8;
497         dst_1 += 8;
498         dst_2 += 8;
499     }
500 }
501
502 // This is exactly the same code as yuv2rgb_c_32 except for the types of
503 // r, g, b, dst_1, dst_2
504 static void yuv2rgb_c_8  (uint8_t * py_1, uint8_t * py_2,
505                           uint8_t * pu, uint8_t * pv,
506                           void * _dst_1, void * _dst_2, int h_size, int v_pos)
507 {
508     int U, V, Y;
509     uint8_t * r, * g, * b;
510     uint8_t * dst_1, * dst_2;
511
512     h_size >>= 3;
513     dst_1 = _dst_1;
514     dst_2 = _dst_2;
515
516     while (h_size--) {
517         RGB(0);
518         DST1(0);
519         DST2(0);
520
521         RGB(1);
522         DST2(1);
523         DST1(1);
524
525         RGB(2);
526         DST1(2);
527         DST2(2);
528
529         RGB(3);
530         DST2(3);
531         DST1(3);
532
533         pu += 4;
534         pv += 4;
535         py_1 += 8;
536         py_2 += 8;
537         dst_1 += 8;
538         dst_2 += 8;
539     }
540 }
541
542 // r, g, b, dst_1, dst_2
543 static void yuv2rgb_c_8_ordered_dither  (uint8_t * py_1, uint8_t * py_2,
544                           uint8_t * pu, uint8_t * pv,
545                           void * _dst_1, void * _dst_2, int h_size, int v_pos)
546 {
547     int U, V, Y;
548     uint8_t * r, * g, * b;
549     uint8_t * dst_1, * dst_2;
550
551     h_size >>= 3;
552     dst_1 = _dst_1;
553     dst_2 = _dst_2;
554
555     while (h_size--) {
556         const uint8_t *d32= dither_8x8_32[v_pos&7];
557         const uint8_t *d64= dither_8x8_73[v_pos&7];
558 #define DST1bpp8(i,o)                                   \
559         Y = py_1[2*i];                          \
560         dst_1[2*i] = r[Y+d32[0+o]] + g[Y+d32[0+o]] + b[Y+d64[0+o]];     \
561         Y = py_1[2*i+1];                        \
562         dst_1[2*i+1] = r[Y+d32[1+o]] + g[Y+d32[1+o]] + b[Y+d64[1+o]];
563
564 #define DST2bpp8(i,o)                                   \
565         Y = py_2[2*i];                          \
566         dst_2[2*i] =  r[Y+d32[8+o]] + g[Y+d32[8+o]] + b[Y+d64[8+o]];    \
567         Y = py_2[2*i+1];                        \
568         dst_2[2*i+1] =  r[Y+d32[9+o]] + g[Y+d32[9+o]] + b[Y+d64[9+o]];
569
570
571         RGB(0);
572         DST1bpp8(0,0);
573         DST2bpp8(0,0);
574
575         RGB(1);
576         DST2bpp8(1,2);
577         DST1bpp8(1,2);
578
579         RGB(2);
580         DST1bpp8(2,4);
581         DST2bpp8(2,4);
582
583         RGB(3);
584         DST2bpp8(3,6);
585         DST1bpp8(3,6);
586
587         pu += 4;
588         pv += 4;
589         py_1 += 8;
590         py_2 += 8;
591         dst_1 += 8;
592         dst_2 += 8;
593     }
594 }
595
596
597 // This is exactly the same code as yuv2rgb_c_32 except for the types of
598 // r, g, b, dst_1, dst_2
599 static void yuv2rgb_c_4  (uint8_t * py_1, uint8_t * py_2,
600                           uint8_t * pu, uint8_t * pv,
601                           void * _dst_1, void * _dst_2, int h_size, int v_pos)
602 {
603     int U, V, Y;
604     uint8_t * r, * g, * b;
605     uint8_t * dst_1, * dst_2;
606
607     h_size >>= 3;
608     dst_1 = _dst_1;
609     dst_2 = _dst_2;
610
611     while (h_size--) {
612         int acc;
613 #define DST1_4(i)                                       \
614         Y = py_1[2*i];                          \
615         acc = r[Y] + g[Y] + b[Y];       \
616         Y = py_1[2*i+1];                        \
617         acc |= (r[Y] + g[Y] + b[Y])<<4;\
618         dst_1[i] = acc; 
619
620 #define DST2_4(i)                                       \
621         Y = py_2[2*i];                          \
622         acc = r[Y] + g[Y] + b[Y];       \
623         Y = py_2[2*i+1];                        \
624         acc |= (r[Y] + g[Y] + b[Y])<<4;\
625         dst_2[i] = acc; 
626         
627         RGB(0);
628         DST1_4(0);
629         DST2_4(0);
630
631         RGB(1);
632         DST2_4(1);
633         DST1_4(1);
634
635         RGB(2);
636         DST1_4(2);
637         DST2_4(2);
638
639         RGB(3);
640         DST2_4(3);
641         DST1_4(3);
642
643         pu += 4;
644         pv += 4;
645         py_1 += 8;
646         py_2 += 8;
647         dst_1 += 4;
648         dst_2 += 4;
649     }
650 }
651
652 static void yuv2rgb_c_4_ordered_dither  (uint8_t * py_1, uint8_t * py_2,
653                           uint8_t * pu, uint8_t * pv,
654                           void * _dst_1, void * _dst_2, int h_size, int v_pos)
655 {
656     int U, V, Y;
657     uint8_t * r, * g, * b;
658     uint8_t * dst_1, * dst_2;
659
660     h_size >>= 3;
661     dst_1 = _dst_1;
662     dst_2 = _dst_2;
663
664     while (h_size--) {
665         const uint8_t *d64= dither_8x8_73[v_pos&7];
666         const uint8_t *d128=dither_8x8_220[v_pos&7];
667         int acc;
668
669 #define DST1bpp4(i,o)                                   \
670         Y = py_1[2*i];                          \
671         acc = r[Y+d128[0+o]] + g[Y+d64[0+o]] + b[Y+d128[0+o]];  \
672         Y = py_1[2*i+1];                        \
673         acc |= (r[Y+d128[1+o]] + g[Y+d64[1+o]] + b[Y+d128[1+o]])<<4;\
674         dst_1[i]= acc;
675
676 #define DST2bpp4(i,o)                                   \
677         Y = py_2[2*i];                          \
678         acc =  r[Y+d128[8+o]] + g[Y+d64[8+o]] + b[Y+d128[8+o]]; \
679         Y = py_2[2*i+1];                        \
680         acc |=  (r[Y+d128[9+o]] + g[Y+d64[9+o]] + b[Y+d128[9+o]])<<4;\
681         dst_2[i]= acc;
682
683
684         RGB(0);
685         DST1bpp4(0,0);
686         DST2bpp4(0,0);
687
688         RGB(1);
689         DST2bpp4(1,2);
690         DST1bpp4(1,2);
691
692         RGB(2);
693         DST1bpp4(2,4);
694         DST2bpp4(2,4);
695
696         RGB(3);
697         DST2bpp4(3,6);
698         DST1bpp4(3,6);
699
700         pu += 4;
701         pv += 4;
702         py_1 += 8;
703         py_2 += 8;
704         dst_1 += 4;
705         dst_2 += 4;
706     }
707 }
708
709 // This is exactly the same code as yuv2rgb_c_32 except for the types of
710 // r, g, b, dst_1, dst_2
711 static void yuv2rgb_c_4b  (uint8_t * py_1, uint8_t * py_2,
712                           uint8_t * pu, uint8_t * pv,
713                           void * _dst_1, void * _dst_2, int h_size, int v_pos)
714 {
715     int U, V, Y;
716     uint8_t * r, * g, * b;
717     uint8_t * dst_1, * dst_2;
718
719     h_size >>= 3;
720     dst_1 = _dst_1;
721     dst_2 = _dst_2;
722
723     while (h_size--) {
724         RGB(0);
725         DST1(0);
726         DST2(0);
727
728         RGB(1);
729         DST2(1);
730         DST1(1);
731
732         RGB(2);
733         DST1(2);
734         DST2(2);
735
736         RGB(3);
737         DST2(3);
738         DST1(3);
739
740         pu += 4;
741         pv += 4;
742         py_1 += 8;
743         py_2 += 8;
744         dst_1 += 8;
745         dst_2 += 8;
746     }
747 }
748
749 static void yuv2rgb_c_4b_ordered_dither  (uint8_t * py_1, uint8_t * py_2,
750                           uint8_t * pu, uint8_t * pv,
751                           void * _dst_1, void * _dst_2, int h_size, int v_pos)
752 {
753     int U, V, Y;
754     uint8_t * r, * g, * b;
755     uint8_t * dst_1, * dst_2;
756
757     h_size >>= 3;
758     dst_1 = _dst_1;
759     dst_2 = _dst_2;
760
761     while (h_size--) {
762         const uint8_t *d64= dither_8x8_73[v_pos&7];
763         const uint8_t *d128=dither_8x8_220[v_pos&7];
764
765 #define DST1bpp4b(i,o)                                  \
766         Y = py_1[2*i];                          \
767         dst_1[2*i] = r[Y+d128[0+o]] + g[Y+d64[0+o]] + b[Y+d128[0+o]];   \
768         Y = py_1[2*i+1];                        \
769         dst_1[2*i+1] = r[Y+d128[1+o]] + g[Y+d64[1+o]] + b[Y+d128[1+o]];
770
771 #define DST2bpp4b(i,o)                                  \
772         Y = py_2[2*i];                          \
773         dst_2[2*i] =  r[Y+d128[8+o]] + g[Y+d64[8+o]] + b[Y+d128[8+o]];  \
774         Y = py_2[2*i+1];                        \
775         dst_2[2*i+1] =  r[Y+d128[9+o]] + g[Y+d64[9+o]] + b[Y+d128[9+o]];
776
777
778         RGB(0);
779         DST1bpp4b(0,0);
780         DST2bpp4b(0,0);
781
782         RGB(1);
783         DST2bpp4b(1,2);
784         DST1bpp4b(1,2);
785
786         RGB(2);
787         DST1bpp4b(2,4);
788         DST2bpp4b(2,4);
789
790         RGB(3);
791         DST2bpp4b(3,6);
792         DST1bpp4b(3,6);
793
794         pu += 4;
795         pv += 4;
796         py_1 += 8;
797         py_2 += 8;
798         dst_1 += 8;
799         dst_2 += 8;
800     }
801 }
802
803 static void yuv2rgb_c_1_ordered_dither  (uint8_t * py_1, uint8_t * py_2,
804                           uint8_t * pu, uint8_t * pv,
805                           void * _dst_1, void * _dst_2, int h_size, int v_pos)
806 {
807     int Y;
808     uint8_t * g;
809     uint8_t * dst_1, * dst_2;
810
811     h_size >>= 3;
812     dst_1 = _dst_1;
813     dst_2 = _dst_2;
814     g= table_gU[128] + table_gV[128];
815
816     while (h_size--) {
817         const uint8_t *d128=dither_8x8_220[v_pos&7];
818         char out_1=0, out_2=0;
819
820 #define DST1bpp1(i,o)                                   \
821         Y = py_1[2*i];                          \
822         out_1+= out_1 + g[Y+d128[0+o]]; \
823         Y = py_1[2*i+1];                        \
824         out_1+= out_1 + g[Y+d128[1+o]];
825
826 #define DST2bpp1(i,o)                                   \
827         Y = py_2[2*i];                          \
828         out_2+= out_2 + g[Y+d128[8+o]]; \
829         Y = py_2[2*i+1];                        \
830         out_2+= out_2 + g[Y+d128[9+o]];
831
832         DST1bpp1(0,0);
833         DST2bpp1(0,0);
834
835         DST2bpp1(1,2);
836         DST1bpp1(1,2);
837
838         DST1bpp1(2,4);
839         DST2bpp1(2,4);
840
841         DST2bpp1(3,6);
842         DST1bpp1(3,6);
843         
844         dst_1[0]= out_1;
845         dst_2[0]= out_2;
846
847         pu += 4;
848         pv += 4;
849         py_1 += 8;
850         py_2 += 8;
851         dst_1 ++;
852         dst_2 ++;
853     }
854 }
855
856
857 static int div_round (int dividend, int divisor)
858 {
859     if (dividend > 0)
860         return (dividend + (divisor>>1)) / divisor;
861     else
862         return -((-dividend + (divisor>>1)) / divisor);
863 }
864
865 void *yuv2rgb_c_init (unsigned bpp, int mode, void *table_rV[256], void *table_gU[256], int table_gV[256], void *table_bU[256])
866 {  
867     int i;
868     uint8_t table_Y[1024];
869     uint32_t *table_32 = 0;
870     uint16_t *table_16 = 0;
871     uint8_t *table_8 = 0;
872     uint8_t *table_332 = 0;
873     uint8_t *table_121 = 0;
874     uint8_t *table_1 = 0;
875     int entry_size = 0;
876     void *table_r = 0, *table_g = 0, *table_b = 0;
877     void *table_start;
878
879     int crv = Inverse_Table_6_9[matrix_coefficients][0];
880     int cbu = Inverse_Table_6_9[matrix_coefficients][1];
881     int cgu = -Inverse_Table_6_9[matrix_coefficients][2];
882     int cgv = -Inverse_Table_6_9[matrix_coefficients][3];
883
884     for (i = 0; i < 1024; i++) {
885         int j;
886
887         j = (76309 * (i - 384 - 16) + 32768) >> 16;
888         j = (j < 0) ? 0 : ((j > 255) ? 255 : j);
889         table_Y[i] = j;
890     }
891
892     switch (bpp) {
893     case 32:
894         yuv2rgb_c_internal = yuv2rgb_c_32;
895
896         table_start= table_32 = malloc ((197 + 2*682 + 256 + 132) * sizeof (uint32_t));
897
898         entry_size = sizeof (uint32_t);
899         table_r = table_32 + 197;
900         table_b = table_32 + 197 + 685;
901         table_g = table_32 + 197 + 2*682;
902
903         for (i = -197; i < 256+197; i++)
904             ((uint32_t *)table_r)[i] = table_Y[i+384] << ((mode==MODE_RGB) ? 16 : 0);
905         for (i = -132; i < 256+132; i++)
906             ((uint32_t *)table_g)[i] = table_Y[i+384] << 8;
907         for (i = -232; i < 256+232; i++)
908             ((uint32_t *)table_b)[i] = table_Y[i+384] << ((mode==MODE_RGB) ? 0 : 16);
909         break;
910
911     case 24:
912 //      yuv2rgb_c_internal = (mode==MODE_RGB) ? yuv2rgb_c_24_rgb : yuv2rgb_c_24_bgr;
913         yuv2rgb_c_internal = (mode!=MODE_RGB) ? yuv2rgb_c_24_rgb : yuv2rgb_c_24_bgr;
914
915         table_start= table_8 = malloc ((256 + 2*232) * sizeof (uint8_t));
916
917         entry_size = sizeof (uint8_t);
918         table_r = table_g = table_b = table_8 + 232;
919
920         for (i = -232; i < 256+232; i++)
921             ((uint8_t * )table_b)[i] = table_Y[i+384];
922         break;
923
924     case 15:
925     case 16:
926         yuv2rgb_c_internal = yuv2rgb_c_16;
927
928         table_start= table_16 = malloc ((197 + 2*682 + 256 + 132) * sizeof (uint16_t));
929
930         entry_size = sizeof (uint16_t);
931         table_r = table_16 + 197;
932         table_b = table_16 + 197 + 685;
933         table_g = table_16 + 197 + 2*682;
934
935         for (i = -197; i < 256+197; i++) {
936             int j = table_Y[i+384] >> 3;
937
938             if (mode == MODE_RGB)
939                 j <<= ((bpp==16) ? 11 : 10);
940
941             ((uint16_t *)table_r)[i] = j;
942         }
943         for (i = -132; i < 256+132; i++) {
944             int j = table_Y[i+384] >> ((bpp==16) ? 2 : 3);
945
946             ((uint16_t *)table_g)[i] = j << 5;
947         }
948         for (i = -232; i < 256+232; i++) {
949             int j = table_Y[i+384] >> 3;
950
951             if (mode == MODE_BGR)
952                 j <<= ((bpp==16) ? 11 : 10);
953
954             ((uint16_t *)table_b)[i] = j;
955         }
956         break;
957
958     case 8:
959         yuv2rgb_c_internal = yuv2rgb_c_8_ordered_dither; //yuv2rgb_c_8;
960
961         table_start= table_332 = malloc ((197 + 2*682 + 256 + 132) * sizeof (uint8_t));
962
963         entry_size = sizeof (uint8_t);
964         table_r = table_332 + 197;
965         table_b = table_332 + 197 + 685;
966         table_g = table_332 + 197 + 2*682;
967
968         for (i = -197; i < 256+197; i++) {
969             int j = (table_Y[i+384 - 16] + 18)/36;
970
971             if (mode == MODE_RGB)
972                 j <<= 5;
973
974             ((uint8_t *)table_r)[i] = j;
975         }
976         for (i = -132; i < 256+132; i++) {
977             int j = (table_Y[i+384 - 16] + 18)/36;
978
979             if (mode == MODE_BGR)
980                 j <<= 1;
981
982             ((uint8_t *)table_g)[i] = j << 2;
983         }
984         for (i = -232; i < 256+232; i++) {
985             int j = (table_Y[i+384 - 37] + 43)/85;
986
987             if (mode == MODE_BGR)
988                 j <<= 6;
989
990             ((uint8_t *)table_b)[i] = j;
991         }
992         break;
993     case 4:
994     case 4|128:
995         if(bpp==4)
996             yuv2rgb_c_internal = yuv2rgb_c_4_ordered_dither; //yuv2rgb_c_4;
997         else
998             yuv2rgb_c_internal = yuv2rgb_c_4b_ordered_dither; //yuv2rgb_c_4;
999
1000         table_start= table_121 = malloc ((197 + 2*682 + 256 + 132) * sizeof (uint8_t));
1001
1002         entry_size = sizeof (uint8_t);
1003         table_r = table_121 + 197;
1004         table_b = table_121 + 197 + 685;
1005         table_g = table_121 + 197 + 2*682;
1006
1007         for (i = -197; i < 256+197; i++) {
1008             int j = table_Y[i+384 - 110] >> 7;
1009
1010             if (mode == MODE_RGB)
1011                 j <<= 3;
1012
1013             ((uint8_t *)table_r)[i] = j;
1014         }
1015         for (i = -132; i < 256+132; i++) {
1016             int j = (table_Y[i+384 - 37]+ 43)/85;
1017
1018             ((uint8_t *)table_g)[i] = j << 1;
1019         }
1020         for (i = -232; i < 256+232; i++) {
1021             int j =table_Y[i+384 - 110] >> 7;
1022
1023             if (mode == MODE_BGR)
1024                 j <<= 3;
1025
1026             ((uint8_t *)table_b)[i] = j;
1027         }
1028         break;
1029
1030     case 1:
1031         yuv2rgb_c_internal = yuv2rgb_c_1_ordered_dither;
1032
1033         table_start= table_1 = malloc (256*2 * sizeof (uint8_t));
1034
1035         entry_size = sizeof (uint8_t);
1036         table_g = table_1;
1037         table_r = table_b = NULL;
1038
1039         for (i = 0; i < 256+256; i++) {
1040             int j = table_Y[i + 384 - 110]>>7;
1041
1042             ((uint8_t *)table_g)[i] = j;
1043         }
1044         break;
1045
1046     default:
1047         table_start= NULL;
1048         mp_msg(MSGT_SWS,MSGL_ERR,"%ibpp not supported by yuv2rgb\n", bpp);
1049         //exit (1);
1050     }
1051
1052     for (i = 0; i < 256; i++) {
1053         table_rV[i] = table_r + entry_size * div_round (crv * (i-128), 76309);
1054         table_gU[i] = table_g + entry_size * div_round (cgu * (i-128), 76309);
1055         table_gV[i] = entry_size * div_round (cgv * (i-128), 76309);
1056         table_bU[i] = table_b + entry_size * div_round (cbu * (i-128), 76309);
1057     }
1058
1059     return table_start; 
1060 }