Statistics
| Branch: | Revision:

ffmpeg / libswscale / yuv2rgb.c @ 88e2a9ae

History | View | Annotate | Download (21 KB)

1
/*
2
 * yuv2rgb.c, Software YUV to RGB converter
3
 *
4
 *  Copyright (C) 1999, Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
5
 *
6
 *  Functions broken out from display_x11.c and several new modes
7
 *  added by HÃ¥kan Hjort <d95hjort@dtek.chalmers.se>
8
 *
9
 *  15 & 16 bpp support by Franck Sicard <Franck.Sicard@solsoft.fr>
10
 *
11
 *  MMX/MMX2 template stuff (needed for fast movntq support),
12
 *  1,4,8bpp support and context / deglobalize stuff
13
 *  by Michael Niedermayer (michaelni@gmx.at)
14
 *
15
 *  This file is part of mpeg2dec, a free MPEG-2 video decoder
16
 *
17
 *  mpeg2dec is free software; you can redistribute it and/or modify
18
 *  it under the terms of the GNU General Public License as published by
19
 *  the Free Software Foundation; either version 2, or (at your option)
20
 *  any later version.
21
 *
22
 *  mpeg2dec is distributed in the hope that it will be useful,
23
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
 *  GNU General Public License for more details.
26
 *
27
 *  You should have received a copy of the GNU General Public License
28
 *  along with mpeg2dec; if not, write to the Free Software
29
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30
 */
31

    
32
#include <stdio.h>
33
#include <stdlib.h>
34
#include <inttypes.h>
35
#include <assert.h>
36

    
37
#include "config.h"
38
#include "rgb2rgb.h"
39
#include "swscale.h"
40
#include "swscale_internal.h"
41

    
42
#define DITHER1XBPP // only for MMX
43

    
44
extern const uint8_t dither_2x2_4[2][8];
45
extern const uint8_t dither_2x2_8[2][8];
46
extern const uint8_t dither_8x8_32[8][8];
47
extern const uint8_t dither_8x8_73[8][8];
48
extern const uint8_t dither_8x8_220[8][8];
49

    
50
#ifdef HAVE_MMX
51

    
52
/* hope these constant values are cache line aligned */
53
DECLARE_ASM_CONST(8, uint64_t, mmx_00ffw)   = 0x00ff00ff00ff00ffULL;
54
DECLARE_ASM_CONST(8, uint64_t, mmx_redmask) = 0xf8f8f8f8f8f8f8f8ULL;
55
DECLARE_ASM_CONST(8, uint64_t, mmx_grnmask) = 0xfcfcfcfcfcfcfcfcULL;
56

    
57
#undef HAVE_MMX
58

    
59
//MMX versions
60
#undef RENAME
61
#define HAVE_MMX
62
#undef HAVE_MMX2
63
#undef HAVE_3DNOW
64
#define RENAME(a) a ## _MMX
65
#include "yuv2rgb_template.c"
66

    
67
//MMX2 versions
68
#undef RENAME
69
#define HAVE_MMX
70
#define HAVE_MMX2
71
#undef HAVE_3DNOW
72
#define RENAME(a) a ## _MMX2
73
#include "yuv2rgb_template.c"
74

    
75
#endif /* HAVE_MMX */
76

    
77
const int32_t Inverse_Table_6_9[8][4] = {
78
    {117504, 138453, 13954, 34903}, /* no sequence_display_extension */
79
    {117504, 138453, 13954, 34903}, /* ITU-R Rec. 709 (1990) */
80
    {104597, 132201, 25675, 53279}, /* unspecified */
81
    {104597, 132201, 25675, 53279}, /* reserved */
82
    {104448, 132798, 24759, 53109}, /* FCC */
83
    {104597, 132201, 25675, 53279}, /* ITU-R Rec. 624-4 System B, G */
84
    {104597, 132201, 25675, 53279}, /* SMPTE 170M */
85
    {117579, 136230, 16907, 35559}  /* SMPTE 240M (1987) */
86
};
87

    
88
#define RGB(i)                                      \
89
    U = pu[i];                                      \
90
    V = pv[i];                                      \
91
    r = (void *)c->table_rV[V];                     \
92
    g = (void *)(c->table_gU[U] + c->table_gV[V]);  \
93
    b = (void *)c->table_bU[U];
94

    
95
#define DST1(i)                         \
96
    Y = py_1[2*i];                      \
97
    dst_1[2*i] = r[Y] + g[Y] + b[Y];    \
98
    Y = py_1[2*i+1];                    \
99
    dst_1[2*i+1] = r[Y] + g[Y] + b[Y];
100

    
101
#define DST2(i)                         \
102
    Y = py_2[2*i];                      \
103
    dst_2[2*i] = r[Y] + g[Y] + b[Y];    \
104
    Y = py_2[2*i+1];                    \
105
    dst_2[2*i+1] = r[Y] + g[Y] + b[Y];
106

    
107
#define DST1RGB(i)                                                \
108
    Y = py_1[2*i];                                                \
109
    dst_1[6*i] = r[Y]; dst_1[6*i+1] = g[Y]; dst_1[6*i+2] = b[Y];  \
110
    Y = py_1[2*i+1];                                              \
111
    dst_1[6*i+3] = r[Y]; dst_1[6*i+4] = g[Y]; dst_1[6*i+5] = b[Y];
112

    
113
#define DST2RGB(i)                                                \
114
    Y = py_2[2*i];                                                \
115
    dst_2[6*i] = r[Y]; dst_2[6*i+1] = g[Y]; dst_2[6*i+2] = b[Y];  \
116
    Y = py_2[2*i+1];                                              \
117
    dst_2[6*i+3] = r[Y]; dst_2[6*i+4] = g[Y]; dst_2[6*i+5] = b[Y];
118

    
119
#define DST1BGR(i)                                                \
120
    Y = py_1[2*i];                                                \
121
    dst_1[6*i] = b[Y]; dst_1[6*i+1] = g[Y]; dst_1[6*i+2] = r[Y];  \
122
    Y = py_1[2*i+1];                                              \
123
    dst_1[6*i+3] = b[Y]; dst_1[6*i+4] = g[Y]; dst_1[6*i+5] = r[Y];
124

    
125
#define DST2BGR(i)                                                \
126
    Y = py_2[2*i];                                                \
127
    dst_2[6*i] = b[Y]; dst_2[6*i+1] = g[Y]; dst_2[6*i+2] = r[Y];  \
128
    Y = py_2[2*i+1];                                              \
129
    dst_2[6*i+3] = b[Y]; dst_2[6*i+4] = g[Y]; dst_2[6*i+5] = r[Y];
130

    
131
#define PROLOG(func_name, dst_type) \
132
static int func_name(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY, \
133
                     int srcSliceH, uint8_t* dst[], int dstStride[]){\
134
    int y;\
135
\
136
    if (c->srcFormat == PIX_FMT_YUV422P){\
137
        srcStride[1] *= 2;\
138
        srcStride[2] *= 2;\
139
    }\
140
    for (y=0; y<srcSliceH; y+=2){\
141
        dst_type *dst_1= (dst_type*)(dst[0] + (y+srcSliceY  )*dstStride[0]);\
142
        dst_type *dst_2= (dst_type*)(dst[0] + (y+srcSliceY+1)*dstStride[0]);\
143
        dst_type av_unused *r, *b;\
144
        dst_type *g;\
145
        uint8_t *py_1= src[0] + y*srcStride[0];\
146
        uint8_t *py_2= py_1 + srcStride[0];\
147
        uint8_t *pu= src[1] + (y>>1)*srcStride[1];\
148
        uint8_t *pv= src[2] + (y>>1)*srcStride[2];\
149
        unsigned int h_size= c->dstW>>3;\
150
        while (h_size--) {\
151
            int av_unused U, V;\
152
            int Y;\
153

    
154
#define EPILOG1(dst_delta)\
155
            pu += 4;\
156
            pv += 4;\
157
            py_1 += 8;\
158
            py_2 += 8;\
159
            dst_1 += dst_delta;\
160
            dst_2 += dst_delta;\
161
        }\
162
        if (c->dstW & 4) {\
163
            int av_unused Y, U, V;\
164

    
165
#define EPILOG2()\
166
        }\
167
    }\
168
    return srcSliceH;\
169
}
170

    
171
#define EPILOG(dst_delta)\
172
    EPILOG1(dst_delta)\
173
    EPILOG2()
174

    
175
PROLOG(yuv2rgb_c_32, uint32_t)
176
    RGB(0);
177
    DST1(0);
178
    DST2(0);
179

    
180
    RGB(1);
181
    DST2(1);
182
    DST1(1);
183

    
184
    RGB(2);
185
    DST1(2);
186
    DST2(2);
187

    
188
    RGB(3);
189
    DST2(3);
190
    DST1(3);
191
EPILOG1(8)
192
    RGB(0);
193
    DST1(0);
194
    DST2(0);
195

    
196
    RGB(1);
197
    DST2(1);
198
    DST1(1);
199
EPILOG2()
200

    
201
PROLOG(yuv2rgb_c_24_rgb, uint8_t)
202
    RGB(0);
203
    DST1RGB(0);
204
    DST2RGB(0);
205

    
206
    RGB(1);
207
    DST2RGB(1);
208
    DST1RGB(1);
209

    
210
    RGB(2);
211
    DST1RGB(2);
212
    DST2RGB(2);
213

    
214
    RGB(3);
215
    DST2RGB(3);
216
    DST1RGB(3);
217
EPILOG1(24)
218
    RGB(0);
219
    DST1RGB(0);
220
    DST2RGB(0);
221

    
222
    RGB(1);
223
    DST2RGB(1);
224
    DST1RGB(1);
225
EPILOG2()
226

    
227
// only trivial mods from yuv2rgb_c_24_rgb
228
PROLOG(yuv2rgb_c_24_bgr, uint8_t)
229
    RGB(0);
230
    DST1BGR(0);
231
    DST2BGR(0);
232

    
233
    RGB(1);
234
    DST2BGR(1);
235
    DST1BGR(1);
236

    
237
    RGB(2);
238
    DST1BGR(2);
239
    DST2BGR(2);
240

    
241
    RGB(3);
242
    DST2BGR(3);
243
    DST1BGR(3);
244
EPILOG1(24)
245
    RGB(0);
246
    DST1BGR(0);
247
    DST2BGR(0);
248

    
249
    RGB(1);
250
    DST2BGR(1);
251
    DST1BGR(1);
252
EPILOG2()
253

    
254
// This is exactly the same code as yuv2rgb_c_32 except for the types of
255
// r, g, b, dst_1, dst_2
256
PROLOG(yuv2rgb_c_16, uint16_t)
257
    RGB(0);
258
    DST1(0);
259
    DST2(0);
260

    
261
    RGB(1);
262
    DST2(1);
263
    DST1(1);
264

    
265
    RGB(2);
266
    DST1(2);
267
    DST2(2);
268

    
269
    RGB(3);
270
    DST2(3);
271
    DST1(3);
272
EPILOG(8)
273

    
274
// This is exactly the same code as yuv2rgb_c_32 except for the types of
275
// r, g, b, dst_1, dst_2
276
PROLOG(yuv2rgb_c_8, uint8_t)
277
    RGB(0);
278
    DST1(0);
279
    DST2(0);
280

    
281
    RGB(1);
282
    DST2(1);
283
    DST1(1);
284

    
285
    RGB(2);
286
    DST1(2);
287
    DST2(2);
288

    
289
    RGB(3);
290
    DST2(3);
291
    DST1(3);
292
EPILOG(8)
293

    
294
// r, g, b, dst_1, dst_2
295
PROLOG(yuv2rgb_c_8_ordered_dither, uint8_t)
296
    const uint8_t *d32= dither_8x8_32[y&7];
297
    const uint8_t *d64= dither_8x8_73[y&7];
298
#define DST1bpp8(i,o)                                               \
299
    Y = py_1[2*i];                                                  \
300
    dst_1[2*i]   = r[Y+d32[0+o]] + g[Y+d32[0+o]] + b[Y+d64[0+o]];   \
301
    Y = py_1[2*i+1];                                                \
302
    dst_1[2*i+1] = r[Y+d32[1+o]] + g[Y+d32[1+o]] + b[Y+d64[1+o]];
303

    
304
#define DST2bpp8(i,o)                                               \
305
    Y = py_2[2*i];                                                  \
306
    dst_2[2*i]   =  r[Y+d32[8+o]] + g[Y+d32[8+o]] + b[Y+d64[8+o]];  \
307
    Y = py_2[2*i+1];                                                \
308
    dst_2[2*i+1] =  r[Y+d32[9+o]] + g[Y+d32[9+o]] + b[Y+d64[9+o]];
309

    
310

    
311
    RGB(0);
312
    DST1bpp8(0,0);
313
    DST2bpp8(0,0);
314

    
315
    RGB(1);
316
    DST2bpp8(1,2);
317
    DST1bpp8(1,2);
318

    
319
    RGB(2);
320
    DST1bpp8(2,4);
321
    DST2bpp8(2,4);
322

    
323
    RGB(3);
324
    DST2bpp8(3,6);
325
    DST1bpp8(3,6);
326
EPILOG(8)
327

    
328

    
329
// This is exactly the same code as yuv2rgb_c_32 except for the types of
330
// r, g, b, dst_1, dst_2
331
PROLOG(yuv2rgb_c_4, uint8_t)
332
    int acc;
333
#define DST1_4(i)                   \
334
    Y = py_1[2*i];                  \
335
    acc = r[Y] + g[Y] + b[Y];       \
336
    Y = py_1[2*i+1];                \
337
    acc |= (r[Y] + g[Y] + b[Y])<<4; \
338
    dst_1[i] = acc;
339

    
340
#define DST2_4(i)                   \
341
    Y = py_2[2*i];                  \
342
    acc = r[Y] + g[Y] + b[Y];       \
343
    Y = py_2[2*i+1];                \
344
    acc |= (r[Y] + g[Y] + b[Y])<<4; \
345
    dst_2[i] = acc;
346

    
347
    RGB(0);
348
    DST1_4(0);
349
    DST2_4(0);
350

    
351
    RGB(1);
352
    DST2_4(1);
353
    DST1_4(1);
354

    
355
    RGB(2);
356
    DST1_4(2);
357
    DST2_4(2);
358

    
359
    RGB(3);
360
    DST2_4(3);
361
    DST1_4(3);
362
EPILOG(4)
363

    
364
PROLOG(yuv2rgb_c_4_ordered_dither, uint8_t)
365
    const uint8_t *d64= dither_8x8_73[y&7];
366
    const uint8_t *d128=dither_8x8_220[y&7];
367
    int acc;
368

    
369
#define DST1bpp4(i,o)                                             \
370
    Y = py_1[2*i];                                                \
371
    acc = r[Y+d128[0+o]] + g[Y+d64[0+o]] + b[Y+d128[0+o]];        \
372
    Y = py_1[2*i+1];                                              \
373
    acc |= (r[Y+d128[1+o]] + g[Y+d64[1+o]] + b[Y+d128[1+o]])<<4;  \
374
    dst_1[i]= acc;
375

    
376
#define DST2bpp4(i,o)                                             \
377
    Y = py_2[2*i];                                                \
378
    acc =  r[Y+d128[8+o]] + g[Y+d64[8+o]] + b[Y+d128[8+o]];       \
379
    Y = py_2[2*i+1];                                              \
380
    acc |=  (r[Y+d128[9+o]] + g[Y+d64[9+o]] + b[Y+d128[9+o]])<<4; \
381
    dst_2[i]= acc;
382

    
383

    
384
    RGB(0);
385
    DST1bpp4(0,0);
386
    DST2bpp4(0,0);
387

    
388
    RGB(1);
389
    DST2bpp4(1,2);
390
    DST1bpp4(1,2);
391

    
392
    RGB(2);
393
    DST1bpp4(2,4);
394
    DST2bpp4(2,4);
395

    
396
    RGB(3);
397
    DST2bpp4(3,6);
398
    DST1bpp4(3,6);
399
EPILOG(4)
400

    
401
// This is exactly the same code as yuv2rgb_c_32 except for the types of
402
// r, g, b, dst_1, dst_2
403
PROLOG(yuv2rgb_c_4b, uint8_t)
404
    RGB(0);
405
    DST1(0);
406
    DST2(0);
407

    
408
    RGB(1);
409
    DST2(1);
410
    DST1(1);
411

    
412
    RGB(2);
413
    DST1(2);
414
    DST2(2);
415

    
416
    RGB(3);
417
    DST2(3);
418
    DST1(3);
419
EPILOG(8)
420

    
421
PROLOG(yuv2rgb_c_4b_ordered_dither, uint8_t)
422
    const uint8_t *d64= dither_8x8_73[y&7];
423
    const uint8_t *d128=dither_8x8_220[y&7];
424

    
425
#define DST1bpp4b(i,o)                                                \
426
    Y = py_1[2*i];                                                    \
427
    dst_1[2*i]   = r[Y+d128[0+o]] + g[Y+d64[0+o]] + b[Y+d128[0+o]];   \
428
    Y = py_1[2*i+1];                                                  \
429
    dst_1[2*i+1] = r[Y+d128[1+o]] + g[Y+d64[1+o]] + b[Y+d128[1+o]];
430

    
431
#define DST2bpp4b(i,o)                                                \
432
    Y = py_2[2*i];                                                    \
433
    dst_2[2*i]   =  r[Y+d128[8+o]] + g[Y+d64[8+o]] + b[Y+d128[8+o]];  \
434
    Y = py_2[2*i+1];                                                  \
435
    dst_2[2*i+1] =  r[Y+d128[9+o]] + g[Y+d64[9+o]] + b[Y+d128[9+o]];
436

    
437

    
438
    RGB(0);
439
    DST1bpp4b(0,0);
440
    DST2bpp4b(0,0);
441

    
442
    RGB(1);
443
    DST2bpp4b(1,2);
444
    DST1bpp4b(1,2);
445

    
446
    RGB(2);
447
    DST1bpp4b(2,4);
448
    DST2bpp4b(2,4);
449

    
450
    RGB(3);
451
    DST2bpp4b(3,6);
452
    DST1bpp4b(3,6);
453
EPILOG(8)
454

    
455
PROLOG(yuv2rgb_c_1_ordered_dither, uint8_t)
456
        const uint8_t *d128=dither_8x8_220[y&7];
457
        char out_1=0, out_2=0;
458
        g= c->table_gU[128] + c->table_gV[128];
459

    
460
#define DST1bpp1(i,o)               \
461
    Y = py_1[2*i];                  \
462
    out_1+= out_1 + g[Y+d128[0+o]]; \
463
    Y = py_1[2*i+1];                \
464
    out_1+= out_1 + g[Y+d128[1+o]];
465

    
466
#define DST2bpp1(i,o)               \
467
    Y = py_2[2*i];                  \
468
    out_2+= out_2 + g[Y+d128[8+o]]; \
469
    Y = py_2[2*i+1];                \
470
    out_2+= out_2 + g[Y+d128[9+o]];
471

    
472
    DST1bpp1(0,0);
473
    DST2bpp1(0,0);
474

    
475
    DST2bpp1(1,2);
476
    DST1bpp1(1,2);
477

    
478
    DST1bpp1(2,4);
479
    DST2bpp1(2,4);
480

    
481
    DST2bpp1(3,6);
482
    DST1bpp1(3,6);
483

    
484
    dst_1[0]= out_1;
485
    dst_2[0]= out_2;
486
EPILOG(1)
487

    
488
SwsFunc yuv2rgb_get_func_ptr (SwsContext *c)
489
{
490
#if defined(HAVE_MMX2) || defined(HAVE_MMX)
491
    if (c->flags & SWS_CPU_CAPS_MMX2){
492
        switch(c->dstFormat){
493
        case PIX_FMT_RGB32:  return yuv420_rgb32_MMX2;
494
        case PIX_FMT_BGR24:  return yuv420_rgb24_MMX2;
495
        case PIX_FMT_RGB565: return yuv420_rgb16_MMX2;
496
        case PIX_FMT_RGB555: return yuv420_rgb15_MMX2;
497
        }
498
    }
499
    if (c->flags & SWS_CPU_CAPS_MMX){
500
        switch(c->dstFormat){
501
        case PIX_FMT_RGB32:  return yuv420_rgb32_MMX;
502
        case PIX_FMT_BGR24:  return yuv420_rgb24_MMX;
503
        case PIX_FMT_RGB565: return yuv420_rgb16_MMX;
504
        case PIX_FMT_RGB555: return yuv420_rgb15_MMX;
505
        }
506
    }
507
#endif
508
#ifdef HAVE_VIS
509
    {
510
        SwsFunc t= yuv2rgb_init_vis(c);
511
        if (t) return t;
512
    }
513
#endif
514
#ifdef CONFIG_MLIB
515
    {
516
        SwsFunc t= yuv2rgb_init_mlib(c);
517
        if (t) return t;
518
    }
519
#endif
520
#ifdef HAVE_ALTIVEC
521
    if (c->flags & SWS_CPU_CAPS_ALTIVEC)
522
    {
523
        SwsFunc t = yuv2rgb_init_altivec(c);
524
        if (t) return t;
525
    }
526
#endif
527

    
528
#ifdef ARCH_BFIN
529
    if (c->flags & SWS_CPU_CAPS_BFIN)
530
    {
531
        SwsFunc t = ff_bfin_yuv2rgb_get_func_ptr (c);
532
        if (t) return t;
533
    }
534
#endif
535

    
536
    av_log(c, AV_LOG_WARNING, "No accelerated colorspace conversion found.\n");
537

    
538
    switch(c->dstFormat){
539
    case PIX_FMT_BGR32_1:
540
    case PIX_FMT_RGB32_1:
541
    case PIX_FMT_BGR32:
542
    case PIX_FMT_RGB32: return yuv2rgb_c_32;
543
    case PIX_FMT_RGB24: return yuv2rgb_c_24_rgb;
544
    case PIX_FMT_BGR24: return yuv2rgb_c_24_bgr;
545
    case PIX_FMT_RGB565:
546
    case PIX_FMT_BGR565:
547
    case PIX_FMT_RGB555:
548
    case PIX_FMT_BGR555: return yuv2rgb_c_16;
549
    case PIX_FMT_RGB8:
550
    case PIX_FMT_BGR8:  return yuv2rgb_c_8_ordered_dither;
551
    case PIX_FMT_RGB4:
552
    case PIX_FMT_BGR4:  return yuv2rgb_c_4_ordered_dither;
553
    case PIX_FMT_RGB4_BYTE:
554
    case PIX_FMT_BGR4_BYTE:  return yuv2rgb_c_4b_ordered_dither;
555
    case PIX_FMT_MONOBLACK:  return yuv2rgb_c_1_ordered_dither;
556
    default:
557
        assert(0);
558
    }
559
    return NULL;
560
}
561

    
562
static int div_round (int dividend, int divisor)
563
{
564
    if (dividend > 0)
565
        return (dividend + (divisor>>1)) / divisor;
566
    else
567
        return -((-dividend + (divisor>>1)) / divisor);
568
}
569

    
570
int yuv2rgb_c_init_tables (SwsContext *c, const int inv_table[4], int fullRange, int brightness, int contrast, int saturation)
571
{
572
    const int isRgb =      c->dstFormat==PIX_FMT_RGB32
573
                        || c->dstFormat==PIX_FMT_RGB32_1
574
                        || c->dstFormat==PIX_FMT_BGR24
575
                        || c->dstFormat==PIX_FMT_RGB565
576
                        || c->dstFormat==PIX_FMT_RGB555
577
                        || c->dstFormat==PIX_FMT_RGB8
578
                        || c->dstFormat==PIX_FMT_RGB4
579
                        || c->dstFormat==PIX_FMT_RGB4_BYTE
580
                        || c->dstFormat==PIX_FMT_MONOBLACK;
581
    const int bpp = fmt_depth(c->dstFormat);
582
    int i, base;
583
    uint8_t table_Y[1024];
584
    uint32_t *table_32 = 0;
585
    uint16_t *table_16 = 0;
586
    uint8_t *table_8 = 0;
587
    uint8_t *table_332 = 0;
588
    uint8_t *table_121 = 0;
589
    uint8_t *table_1 = 0;
590
    int entry_size = 0;
591
    void *table_r = 0, *table_g = 0, *table_b = 0;
592
    void *table_start;
593

    
594
    int64_t crv =  inv_table[0];
595
    int64_t cbu =  inv_table[1];
596
    int64_t cgu = -inv_table[2];
597
    int64_t cgv = -inv_table[3];
598
    int64_t cy  = 1<<16;
599
    int64_t oy  = 0;
600

    
601
//printf("%lld %lld %lld %lld %lld\n", cy, crv, cbu, cgu, cgv);
602
    if (!fullRange){
603
        cy= (cy*255) / 219;
604
        oy= 16<<16;
605
    }else{
606
        crv= (crv*224) / 255;
607
        cbu= (cbu*224) / 255;
608
        cgu= (cgu*224) / 255;
609
        cgv= (cgv*224) / 255;
610
    }
611

    
612
    cy = (cy *contrast             )>>16;
613
    crv= (crv*contrast * saturation)>>32;
614
    cbu= (cbu*contrast * saturation)>>32;
615
    cgu= (cgu*contrast * saturation)>>32;
616
    cgv= (cgv*contrast * saturation)>>32;
617
//printf("%lld %lld %lld %lld %lld\n", cy, crv, cbu, cgu, cgv);
618
    oy -= 256*brightness;
619

    
620
    for (i = 0; i < 1024; i++) {
621
        int j;
622

    
623
        j= (cy*(((i - 384)<<16) - oy) + (1<<31))>>32;
624
        j = (j < 0) ? 0 : ((j > 255) ? 255 : j);
625
        table_Y[i] = j;
626
    }
627

    
628
    switch (bpp) {
629
    case 32:
630
        table_start= table_32 = av_malloc ((197 + 2*682 + 256 + 132) * sizeof (uint32_t));
631
        base= (c->dstFormat == PIX_FMT_RGB32_1 || c->dstFormat == PIX_FMT_BGR32_1) ? 8 : 0;
632

    
633
        entry_size = sizeof (uint32_t);
634
        table_r = table_32 + 197;
635
        table_b = table_32 + 197 + 685;
636
        table_g = table_32 + 197 + 2*682;
637

    
638
        for (i = -197; i < 256+197; i++)
639
            ((uint32_t *)table_r)[i] = table_Y[i+384] << ((isRgb ? 16 : 0) + base);
640
        for (i = -132; i < 256+132; i++)
641
            ((uint32_t *)table_g)[i] = table_Y[i+384] << (8                + base);
642
        for (i = -232; i < 256+232; i++)
643
            ((uint32_t *)table_b)[i] = table_Y[i+384] << ((isRgb ? 0 : 16) + base);
644
        break;
645

    
646
    case 24:
647
        table_start= table_8 = av_malloc ((256 + 2*232) * sizeof (uint8_t));
648

    
649
        entry_size = sizeof (uint8_t);
650
        table_r = table_g = table_b = table_8 + 232;
651

    
652
        for (i = -232; i < 256+232; i++)
653
            ((uint8_t * )table_b)[i] = table_Y[i+384];
654
        break;
655

    
656
    case 15:
657
    case 16:
658
        table_start= table_16 = av_malloc ((197 + 2*682 + 256 + 132) * sizeof (uint16_t));
659

    
660
        entry_size = sizeof (uint16_t);
661
        table_r = table_16 + 197;
662
        table_b = table_16 + 197 + 685;
663
        table_g = table_16 + 197 + 2*682;
664

    
665
        for (i = -197; i < 256+197; i++) {
666
            int j = table_Y[i+384] >> 3;
667

    
668
            if (isRgb)
669
                j <<= ((bpp==16) ? 11 : 10);
670

    
671
            ((uint16_t *)table_r)[i] = j;
672
        }
673
        for (i = -132; i < 256+132; i++) {
674
            int j = table_Y[i+384] >> ((bpp==16) ? 2 : 3);
675

    
676
            ((uint16_t *)table_g)[i] = j << 5;
677
        }
678
        for (i = -232; i < 256+232; i++) {
679
            int j = table_Y[i+384] >> 3;
680

    
681
            if (!isRgb)
682
                j <<= ((bpp==16) ? 11 : 10);
683

    
684
            ((uint16_t *)table_b)[i] = j;
685
        }
686
        break;
687

    
688
    case 8:
689
        table_start= table_332 = av_malloc ((197 + 2*682 + 256 + 132) * sizeof (uint8_t));
690

    
691
        entry_size = sizeof (uint8_t);
692
        table_r = table_332 + 197;
693
        table_b = table_332 + 197 + 685;
694
        table_g = table_332 + 197 + 2*682;
695

    
696
        for (i = -197; i < 256+197; i++) {
697
            int j = (table_Y[i+384 - 16] + 18)/36;
698

    
699
            if (isRgb)
700
                j <<= 5;
701

    
702
            ((uint8_t *)table_r)[i] = j;
703
        }
704
        for (i = -132; i < 256+132; i++) {
705
            int j = (table_Y[i+384 - 16] + 18)/36;
706

    
707
            if (!isRgb)
708
                j <<= 1;
709

    
710
            ((uint8_t *)table_g)[i] = j << 2;
711
        }
712
        for (i = -232; i < 256+232; i++) {
713
            int j = (table_Y[i+384 - 37] + 43)/85;
714

    
715
            if (!isRgb)
716
                j <<= 6;
717

    
718
            ((uint8_t *)table_b)[i] = j;
719
        }
720
        break;
721
    case 4:
722
    case 4|128:
723
        table_start= table_121 = av_malloc ((197 + 2*682 + 256 + 132) * sizeof (uint8_t));
724

    
725
        entry_size = sizeof (uint8_t);
726
        table_r = table_121 + 197;
727
        table_b = table_121 + 197 + 685;
728
        table_g = table_121 + 197 + 2*682;
729

    
730
        for (i = -197; i < 256+197; i++) {
731
            int j = table_Y[i+384 - 110] >> 7;
732

    
733
            if (isRgb)
734
                j <<= 3;
735

    
736
            ((uint8_t *)table_r)[i] = j;
737
        }
738
        for (i = -132; i < 256+132; i++) {
739
            int j = (table_Y[i+384 - 37]+ 43)/85;
740

    
741
            ((uint8_t *)table_g)[i] = j << 1;
742
        }
743
        for (i = -232; i < 256+232; i++) {
744
            int j =table_Y[i+384 - 110] >> 7;
745

    
746
            if (!isRgb)
747
                j <<= 3;
748

    
749
            ((uint8_t *)table_b)[i] = j;
750
        }
751
        break;
752

    
753
    case 1:
754
        table_start= table_1 = av_malloc (256*2 * sizeof (uint8_t));
755

    
756
        entry_size = sizeof (uint8_t);
757
        table_g = table_1;
758
        table_r = table_b = NULL;
759

    
760
        for (i = 0; i < 256+256; i++) {
761
            int j = table_Y[i + 384 - 110]>>7;
762

    
763
            ((uint8_t *)table_g)[i] = j;
764
        }
765
        break;
766

    
767
    default:
768
        table_start= NULL;
769
        av_log(c, AV_LOG_ERROR, "%ibpp not supported by yuv2rgb\n", bpp);
770
        //free mem?
771
        return -1;
772
    }
773

    
774
    for (i = 0; i < 256; i++) {
775
        c->table_rV[i] = (uint8_t *)table_r + entry_size * div_round (crv * (i-128), cy);
776
        c->table_gU[i] = (uint8_t *)table_g + entry_size * div_round (cgu * (i-128), cy);
777
        c->table_gV[i] = entry_size * div_round (cgv * (i-128), cy);
778
        c->table_bU[i] = (uint8_t *)table_b + entry_size * div_round (cbu * (i-128), cy);
779
    }
780

    
781
    av_free(c->yuvTable);
782
    c->yuvTable= table_start;
783
    return 0;
784
}