Statistics
| Branch: | Revision:

ffmpeg / libswscale / yuv2rgb.c @ 1379b584

History | View | Annotate | Download (22.4 KB)

1
/*
2
 * software YUV to RGB converter
3
 *
4
 * Copyright (C) 2009 Konstantin Shishkov
5
 *
6
 * 1,4,8bpp support and context / deglobalize stuff
7
 * by Michael Niedermayer (michaelni@gmx.at)
8
 *
9
 * This file is part of FFmpeg.
10
 *
11
 * FFmpeg is free software; you can redistribute it and/or
12
 * modify it under the terms of the GNU Lesser General Public
13
 * License as published by the Free Software Foundation; either
14
 * version 2.1 of the License, or (at your option) any later version.
15
 *
16
 * FFmpeg is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19
 * Lesser General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Lesser General Public
22
 * License along with FFmpeg; if not, write to the Free Software
23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24
 */
25

    
26
#include <stdio.h>
27
#include <stdlib.h>
28
#include <inttypes.h>
29
#include <assert.h>
30

    
31
#include "config.h"
32
#include "rgb2rgb.h"
33
#include "swscale.h"
34
#include "swscale_internal.h"
35
#include "libavutil/x86_cpu.h"
36
#include "libavutil/bswap.h"
37

    
38
extern const uint8_t dither_8x8_32[8][8];
39
extern const uint8_t dither_8x8_73[8][8];
40
extern const uint8_t dither_8x8_220[8][8];
41

    
42
const int32_t ff_yuv2rgb_coeffs[8][4] = {
43
    {117504, 138453, 13954, 34903}, /* no sequence_display_extension */
44
    {117504, 138453, 13954, 34903}, /* ITU-R Rec. 709 (1990) */
45
    {104597, 132201, 25675, 53279}, /* unspecified */
46
    {104597, 132201, 25675, 53279}, /* reserved */
47
    {104448, 132798, 24759, 53109}, /* FCC */
48
    {104597, 132201, 25675, 53279}, /* ITU-R Rec. 624-4 System B, G */
49
    {104597, 132201, 25675, 53279}, /* SMPTE 170M */
50
    {117579, 136230, 16907, 35559}  /* SMPTE 240M (1987) */
51
};
52

    
53
const int *sws_getCoefficients(int colorspace)
54
{
55
    if (colorspace > 7 || colorspace < 0)
56
        colorspace = SWS_CS_DEFAULT;
57
    return ff_yuv2rgb_coeffs[colorspace];
58
}
59

    
60
#define LOADCHROMA(i)                               \
61
    U = pu[i];                                      \
62
    V = pv[i];                                      \
63
    r = (void *)c->table_rV[V];                     \
64
    g = (void *)(c->table_gU[U] + c->table_gV[V]);  \
65
    b = (void *)c->table_bU[U];
66

    
67
#define PUTRGB(dst,src,i)            \
68
    Y = src[2*i];                    \
69
    dst[2*i  ] = r[Y] + g[Y] + b[Y]; \
70
    Y = src[2*i+1];                  \
71
    dst[2*i+1] = r[Y] + g[Y] + b[Y];
72

    
73
#define PUTRGB24(dst,src,i)                                  \
74
    Y = src[2*i];                                            \
75
    dst[6*i+0] = r[Y]; dst[6*i+1] = g[Y]; dst[6*i+2] = b[Y]; \
76
    Y = src[2*i+1];                                          \
77
    dst[6*i+3] = r[Y]; dst[6*i+4] = g[Y]; dst[6*i+5] = b[Y];
78

    
79
#define PUTBGR24(dst,src,i)                                  \
80
    Y = src[2*i];                                            \
81
    dst[6*i+0] = b[Y]; dst[6*i+1] = g[Y]; dst[6*i+2] = r[Y]; \
82
    Y = src[2*i+1];                                          \
83
    dst[6*i+3] = b[Y]; dst[6*i+4] = g[Y]; dst[6*i+5] = r[Y];
84

    
85
#define PUTRGBA(dst,ysrc,asrc,i,s)                      \
86
    Y = ysrc[2*i];                                      \
87
    dst[2*i  ] = r[Y] + g[Y] + b[Y] + (asrc[2*i  ]<<s); \
88
    Y = ysrc[2*i+1];                                    \
89
    dst[2*i+1] = r[Y] + g[Y] + b[Y] + (asrc[2*i+1]<<s);
90

    
91
#define PUTRGB48(dst,src,i)             \
92
    Y = src[2*i];                       \
93
    dst[12*i+ 0] = dst[12*i+ 1] = r[Y]; \
94
    dst[12*i+ 2] = dst[12*i+ 3] = g[Y]; \
95
    dst[12*i+ 4] = dst[12*i+ 5] = b[Y]; \
96
    Y = src[2*i+1];                     \
97
    dst[12*i+ 6] = dst[12*i+ 7] = r[Y]; \
98
    dst[12*i+ 8] = dst[12*i+ 9] = g[Y]; \
99
    dst[12*i+10] = dst[12*i+11] = b[Y];
100

    
101
#define YUV2RGBFUNC(func_name, dst_type, alpha) \
102
static int func_name(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY, \
103
                     int srcSliceH, uint8_t* dst[], int dstStride[]) \
104
{\
105
    int y;\
106
\
107
    if (!alpha && c->srcFormat == PIX_FMT_YUV422P) {\
108
        srcStride[1] *= 2;\
109
        srcStride[2] *= 2;\
110
    }\
111
    for (y=0; y<srcSliceH; y+=2) {\
112
        dst_type *dst_1 = (dst_type*)(dst[0] + (y+srcSliceY  )*dstStride[0]);\
113
        dst_type *dst_2 = (dst_type*)(dst[0] + (y+srcSliceY+1)*dstStride[0]);\
114
        dst_type av_unused *r, *b;\
115
        dst_type *g;\
116
        const uint8_t *py_1 = src[0] + y*srcStride[0];\
117
        const uint8_t *py_2 = py_1 + srcStride[0];\
118
        const uint8_t *pu = src[1] + (y>>1)*srcStride[1];\
119
        const uint8_t *pv = src[2] + (y>>1)*srcStride[2];\
120
        const uint8_t av_unused *pa_1, *pa_2;\
121
        unsigned int h_size = c->dstW>>3;\
122
        if (alpha) {\
123
            pa_1 = src[3] + y*srcStride[3];\
124
            pa_2 = pa_1 + srcStride[3];\
125
        }\
126
        while (h_size--) {\
127
            int av_unused U, V;\
128
            int Y;\
129

    
130
#define ENDYUV2RGBLINE(dst_delta)\
131
            pu += 4;\
132
            pv += 4;\
133
            py_1 += 8;\
134
            py_2 += 8;\
135
            dst_1 += dst_delta;\
136
            dst_2 += dst_delta;\
137
        }\
138
        if (c->dstW & 4) {\
139
            int av_unused Y, U, V;\
140

    
141
#define ENDYUV2RGBFUNC()\
142
        }\
143
    }\
144
    return srcSliceH;\
145
}
146

    
147
#define CLOSEYUV2RGBFUNC(dst_delta)\
148
    ENDYUV2RGBLINE(dst_delta)\
149
    ENDYUV2RGBFUNC()
150

    
151
YUV2RGBFUNC(yuv2rgb_c_48, uint8_t, 0)
152
    LOADCHROMA(0);
153
    PUTRGB48(dst_1,py_1,0);
154
    PUTRGB48(dst_2,py_2,0);
155

    
156
    LOADCHROMA(1);
157
    PUTRGB48(dst_2,py_2,1);
158
    PUTRGB48(dst_1,py_1,1);
159

    
160
    LOADCHROMA(2);
161
    PUTRGB48(dst_1,py_1,2);
162
    PUTRGB48(dst_2,py_2,2);
163

    
164
    LOADCHROMA(3);
165
    PUTRGB48(dst_2,py_2,3);
166
    PUTRGB48(dst_1,py_1,3);
167
ENDYUV2RGBLINE(48)
168
    LOADCHROMA(0);
169
    PUTRGB48(dst_1,py_1,0);
170
    PUTRGB48(dst_2,py_2,0);
171

    
172
    LOADCHROMA(1);
173
    PUTRGB48(dst_2,py_2,1);
174
    PUTRGB48(dst_1,py_1,1);
175
ENDYUV2RGBFUNC()
176

    
177
YUV2RGBFUNC(yuv2rgb_c_32, uint32_t, 0)
178
    LOADCHROMA(0);
179
    PUTRGB(dst_1,py_1,0);
180
    PUTRGB(dst_2,py_2,0);
181

    
182
    LOADCHROMA(1);
183
    PUTRGB(dst_2,py_2,1);
184
    PUTRGB(dst_1,py_1,1);
185

    
186
    LOADCHROMA(2);
187
    PUTRGB(dst_1,py_1,2);
188
    PUTRGB(dst_2,py_2,2);
189

    
190
    LOADCHROMA(3);
191
    PUTRGB(dst_2,py_2,3);
192
    PUTRGB(dst_1,py_1,3);
193
ENDYUV2RGBLINE(8)
194
    LOADCHROMA(0);
195
    PUTRGB(dst_1,py_1,0);
196
    PUTRGB(dst_2,py_2,0);
197

    
198
    LOADCHROMA(1);
199
    PUTRGB(dst_2,py_2,1);
200
    PUTRGB(dst_1,py_1,1);
201
ENDYUV2RGBFUNC()
202

    
203
YUV2RGBFUNC(yuva2rgba_c, uint32_t, 1)
204
    LOADCHROMA(0);
205
    PUTRGBA(dst_1,py_1,pa_1,0,24);
206
    PUTRGBA(dst_2,py_2,pa_2,0,24);
207

    
208
    LOADCHROMA(1);
209
    PUTRGBA(dst_2,py_2,pa_1,1,24);
210
    PUTRGBA(dst_1,py_1,pa_2,1,24);
211

    
212
    LOADCHROMA(2);
213
    PUTRGBA(dst_1,py_1,pa_1,2,24);
214
    PUTRGBA(dst_2,py_2,pa_2,2,24);
215

    
216
    LOADCHROMA(3);
217
    PUTRGBA(dst_2,py_2,pa_1,3,24);
218
    PUTRGBA(dst_1,py_1,pa_2,3,24);
219
    pa_1 += 8;\
220
    pa_2 += 8;\
221
ENDYUV2RGBLINE(8)
222
    LOADCHROMA(0);
223
    PUTRGBA(dst_1,py_1,pa_1,0,24);
224
    PUTRGBA(dst_2,py_2,pa_2,0,24);
225

    
226
    LOADCHROMA(1);
227
    PUTRGBA(dst_2,py_2,pa_1,1,24);
228
    PUTRGBA(dst_1,py_1,pa_2,1,24);
229
ENDYUV2RGBFUNC()
230

    
231
YUV2RGBFUNC(yuva2argb_c, uint32_t, 1)
232
    LOADCHROMA(0);
233
    PUTRGBA(dst_1,py_1,pa_1,0,0);
234
    PUTRGBA(dst_2,py_2,pa_2,0,0);
235

    
236
    LOADCHROMA(1);
237
    PUTRGBA(dst_2,py_2,pa_2,1,0);
238
    PUTRGBA(dst_1,py_1,pa_1,1,0);
239

    
240
    LOADCHROMA(2);
241
    PUTRGBA(dst_1,py_1,pa_1,2,0);
242
    PUTRGBA(dst_2,py_2,pa_2,2,0);
243

    
244
    LOADCHROMA(3);
245
    PUTRGBA(dst_2,py_2,pa_2,3,0);
246
    PUTRGBA(dst_1,py_1,pa_1,3,0);
247
    pa_1 += 8;\
248
    pa_2 += 8;\
249
ENDYUV2RGBLINE(8)
250
    LOADCHROMA(0);
251
    PUTRGBA(dst_1,py_1,pa_1,0,0);
252
    PUTRGBA(dst_2,py_2,pa_2,0,0);
253

    
254
    LOADCHROMA(1);
255
    PUTRGBA(dst_2,py_2,pa_2,1,0);
256
    PUTRGBA(dst_1,py_1,pa_1,1,0);
257
ENDYUV2RGBFUNC()
258

    
259
YUV2RGBFUNC(yuv2rgb_c_24_rgb, uint8_t, 0)
260
    LOADCHROMA(0);
261
    PUTRGB24(dst_1,py_1,0);
262
    PUTRGB24(dst_2,py_2,0);
263

    
264
    LOADCHROMA(1);
265
    PUTRGB24(dst_2,py_2,1);
266
    PUTRGB24(dst_1,py_1,1);
267

    
268
    LOADCHROMA(2);
269
    PUTRGB24(dst_1,py_1,2);
270
    PUTRGB24(dst_2,py_2,2);
271

    
272
    LOADCHROMA(3);
273
    PUTRGB24(dst_2,py_2,3);
274
    PUTRGB24(dst_1,py_1,3);
275
ENDYUV2RGBLINE(24)
276
    LOADCHROMA(0);
277
    PUTRGB24(dst_1,py_1,0);
278
    PUTRGB24(dst_2,py_2,0);
279

    
280
    LOADCHROMA(1);
281
    PUTRGB24(dst_2,py_2,1);
282
    PUTRGB24(dst_1,py_1,1);
283
ENDYUV2RGBFUNC()
284

    
285
// only trivial mods from yuv2rgb_c_24_rgb
286
YUV2RGBFUNC(yuv2rgb_c_24_bgr, uint8_t, 0)
287
    LOADCHROMA(0);
288
    PUTBGR24(dst_1,py_1,0);
289
    PUTBGR24(dst_2,py_2,0);
290

    
291
    LOADCHROMA(1);
292
    PUTBGR24(dst_2,py_2,1);
293
    PUTBGR24(dst_1,py_1,1);
294

    
295
    LOADCHROMA(2);
296
    PUTBGR24(dst_1,py_1,2);
297
    PUTBGR24(dst_2,py_2,2);
298

    
299
    LOADCHROMA(3);
300
    PUTBGR24(dst_2,py_2,3);
301
    PUTBGR24(dst_1,py_1,3);
302
ENDYUV2RGBLINE(24)
303
    LOADCHROMA(0);
304
    PUTBGR24(dst_1,py_1,0);
305
    PUTBGR24(dst_2,py_2,0);
306

    
307
    LOADCHROMA(1);
308
    PUTBGR24(dst_2,py_2,1);
309
    PUTBGR24(dst_1,py_1,1);
310
ENDYUV2RGBFUNC()
311

    
312
// This is exactly the same code as yuv2rgb_c_32 except for the types of
313
// r, g, b, dst_1, dst_2
314
YUV2RGBFUNC(yuv2rgb_c_16, uint16_t, 0)
315
    LOADCHROMA(0);
316
    PUTRGB(dst_1,py_1,0);
317
    PUTRGB(dst_2,py_2,0);
318

    
319
    LOADCHROMA(1);
320
    PUTRGB(dst_2,py_2,1);
321
    PUTRGB(dst_1,py_1,1);
322

    
323
    LOADCHROMA(2);
324
    PUTRGB(dst_1,py_1,2);
325
    PUTRGB(dst_2,py_2,2);
326

    
327
    LOADCHROMA(3);
328
    PUTRGB(dst_2,py_2,3);
329
    PUTRGB(dst_1,py_1,3);
330
CLOSEYUV2RGBFUNC(8)
331

    
332
#if 0 // Currently unused
333
// This is exactly the same code as yuv2rgb_c_32 except for the types of
334
// r, g, b, dst_1, dst_2
335
YUV2RGBFUNC(yuv2rgb_c_8, uint8_t, 0)
336
    LOADCHROMA(0);
337
    PUTRGB(dst_1,py_1,0);
338
    PUTRGB(dst_2,py_2,0);
339

340
    LOADCHROMA(1);
341
    PUTRGB(dst_2,py_2,1);
342
    PUTRGB(dst_1,py_1,1);
343

344
    LOADCHROMA(2);
345
    PUTRGB(dst_1,py_1,2);
346
    PUTRGB(dst_2,py_2,2);
347

348
    LOADCHROMA(3);
349
    PUTRGB(dst_2,py_2,3);
350
    PUTRGB(dst_1,py_1,3);
351
CLOSEYUV2RGBFUNC(8)
352
#endif
353

    
354
// r, g, b, dst_1, dst_2
355
YUV2RGBFUNC(yuv2rgb_c_8_ordered_dither, uint8_t, 0)
356
    const uint8_t *d32 = dither_8x8_32[y&7];
357
    const uint8_t *d64 = dither_8x8_73[y&7];
358
#define PUTRGB8(dst,src,i,o)                                    \
359
    Y = src[2*i];                                               \
360
    dst[2*i]   = r[Y+d32[0+o]] + g[Y+d32[0+o]] + b[Y+d64[0+o]]; \
361
    Y = src[2*i+1];                                             \
362
    dst[2*i+1] = r[Y+d32[1+o]] + g[Y+d32[1+o]] + b[Y+d64[1+o]];
363

    
364
    LOADCHROMA(0);
365
    PUTRGB8(dst_1,py_1,0,0);
366
    PUTRGB8(dst_2,py_2,0,0+8);
367

    
368
    LOADCHROMA(1);
369
    PUTRGB8(dst_2,py_2,1,2+8);
370
    PUTRGB8(dst_1,py_1,1,2);
371

    
372
    LOADCHROMA(2);
373
    PUTRGB8(dst_1,py_1,2,4);
374
    PUTRGB8(dst_2,py_2,2,4+8);
375

    
376
    LOADCHROMA(3);
377
    PUTRGB8(dst_2,py_2,3,6+8);
378
    PUTRGB8(dst_1,py_1,3,6);
379
CLOSEYUV2RGBFUNC(8)
380

    
381
#if 0 // Currently unused
382
// This is exactly the same code as yuv2rgb_c_32 except for the types of
383
// r, g, b, dst_1, dst_2
384
YUV2RGBFUNC(yuv2rgb_c_4, uint8_t, 0)
385
    int acc;
386
#define PUTRGB4(dst,src,i)          \
387
    Y = src[2*i];                   \
388
    acc = r[Y] + g[Y] + b[Y];       \
389
    Y = src[2*i+1];                 \
390
    acc |= (r[Y] + g[Y] + b[Y])<<4; \
391
    dst[i] = acc;
392

393
    LOADCHROMA(0);
394
    PUTRGB4(dst_1,py_1,0);
395
    PUTRGB4(dst_2,py_2,0);
396

397
    LOADCHROMA(1);
398
    PUTRGB4(dst_2,py_2,1);
399
    PUTRGB4(dst_1,py_1,1);
400

401
    LOADCHROMA(2);
402
    PUTRGB4(dst_1,py_1,2);
403
    PUTRGB4(dst_2,py_2,2);
404

405
    LOADCHROMA(3);
406
    PUTRGB4(dst_2,py_2,3);
407
    PUTRGB4(dst_1,py_1,3);
408
CLOSEYUV2RGBFUNC(4)
409
#endif
410

    
411
YUV2RGBFUNC(yuv2rgb_c_4_ordered_dither, uint8_t, 0)
412
    const uint8_t *d64 =  dither_8x8_73[y&7];
413
    const uint8_t *d128 = dither_8x8_220[y&7];
414
    int acc;
415

    
416
#define PUTRGB4D(dst,src,i,o)                                     \
417
    Y = src[2*i];                                                 \
418
    acc = r[Y+d128[0+o]] + g[Y+d64[0+o]] + b[Y+d128[0+o]];        \
419
    Y = src[2*i+1];                                               \
420
    acc |= (r[Y+d128[1+o]] + g[Y+d64[1+o]] + b[Y+d128[1+o]])<<4;  \
421
    dst[i]= acc;
422

    
423
    LOADCHROMA(0);
424
    PUTRGB4D(dst_1,py_1,0,0);
425
    PUTRGB4D(dst_2,py_2,0,0+8);
426

    
427
    LOADCHROMA(1);
428
    PUTRGB4D(dst_2,py_2,1,2+8);
429
    PUTRGB4D(dst_1,py_1,1,2);
430

    
431
    LOADCHROMA(2);
432
    PUTRGB4D(dst_1,py_1,2,4);
433
    PUTRGB4D(dst_2,py_2,2,4+8);
434

    
435
    LOADCHROMA(3);
436
    PUTRGB4D(dst_2,py_2,3,6+8);
437
    PUTRGB4D(dst_1,py_1,3,6);
438
CLOSEYUV2RGBFUNC(4)
439

    
440
#if 0 // Currently unused
441
// This is exactly the same code as yuv2rgb_c_32 except for the types of
442
// r, g, b, dst_1, dst_2
443
YUV2RGBFUNC(yuv2rgb_c_4b, uint8_t, 0)
444
    LOADCHROMA(0);
445
    PUTRGB(dst_1,py_1,0);
446
    PUTRGB(dst_2,py_2,0);
447

448
    LOADCHROMA(1);
449
    PUTRGB(dst_2,py_2,1);
450
    PUTRGB(dst_1,py_1,1);
451

452
    LOADCHROMA(2);
453
    PUTRGB(dst_1,py_1,2);
454
    PUTRGB(dst_2,py_2,2);
455

456
    LOADCHROMA(3);
457
    PUTRGB(dst_2,py_2,3);
458
    PUTRGB(dst_1,py_1,3);
459
CLOSEYUV2RGBFUNC(8)
460
#endif
461

    
462
YUV2RGBFUNC(yuv2rgb_c_4b_ordered_dither, uint8_t, 0)
463
    const uint8_t *d64 =  dither_8x8_73[y&7];
464
    const uint8_t *d128 = dither_8x8_220[y&7];
465

    
466
#define PUTRGB4DB(dst,src,i,o)                                    \
467
    Y = src[2*i];                                                 \
468
    dst[2*i]   = r[Y+d128[0+o]] + g[Y+d64[0+o]] + b[Y+d128[0+o]]; \
469
    Y = src[2*i+1];                                               \
470
    dst[2*i+1] = r[Y+d128[1+o]] + g[Y+d64[1+o]] + b[Y+d128[1+o]];
471

    
472
    LOADCHROMA(0);
473
    PUTRGB4DB(dst_1,py_1,0,0);
474
    PUTRGB4DB(dst_2,py_2,0,0+8);
475

    
476
    LOADCHROMA(1);
477
    PUTRGB4DB(dst_2,py_2,1,2+8);
478
    PUTRGB4DB(dst_1,py_1,1,2);
479

    
480
    LOADCHROMA(2);
481
    PUTRGB4DB(dst_1,py_1,2,4);
482
    PUTRGB4DB(dst_2,py_2,2,4+8);
483

    
484
    LOADCHROMA(3);
485
    PUTRGB4DB(dst_2,py_2,3,6+8);
486
    PUTRGB4DB(dst_1,py_1,3,6);
487
CLOSEYUV2RGBFUNC(8)
488

    
489
YUV2RGBFUNC(yuv2rgb_c_1_ordered_dither, uint8_t, 0)
490
        const uint8_t *d128 = dither_8x8_220[y&7];
491
        char out_1 = 0, out_2 = 0;
492
        g= c->table_gU[128] + c->table_gV[128];
493

    
494
#define PUTRGB1(out,src,i,o)    \
495
    Y = src[2*i];               \
496
    out+= out + g[Y+d128[0+o]]; \
497
    Y = src[2*i+1];             \
498
    out+= out + g[Y+d128[1+o]];
499

    
500
    PUTRGB1(out_1,py_1,0,0);
501
    PUTRGB1(out_2,py_2,0,0+8);
502

    
503
    PUTRGB1(out_2,py_2,1,2+8);
504
    PUTRGB1(out_1,py_1,1,2);
505

    
506
    PUTRGB1(out_1,py_1,2,4);
507
    PUTRGB1(out_2,py_2,2,4+8);
508

    
509
    PUTRGB1(out_2,py_2,3,6+8);
510
    PUTRGB1(out_1,py_1,3,6);
511

    
512
    dst_1[0]= out_1;
513
    dst_2[0]= out_2;
514
CLOSEYUV2RGBFUNC(1)
515

    
516
SwsFunc ff_yuv2rgb_get_func_ptr(SwsContext *c)
517
{
518
    SwsFunc t = NULL;
519
#if (HAVE_MMX2 || HAVE_MMX) && CONFIG_GPL
520
     t = ff_yuv2rgb_init_mmx(c);
521
#endif
522
#if HAVE_VIS
523
    t = ff_yuv2rgb_init_vis(c);
524
#endif
525
#if CONFIG_MLIB
526
    t = ff_yuv2rgb_init_mlib(c);
527
#endif
528
#if HAVE_ALTIVEC
529
    if (c->flags & SWS_CPU_CAPS_ALTIVEC)
530
        t = ff_yuv2rgb_init_altivec(c);
531
#endif
532

    
533
#if ARCH_BFIN
534
    if (c->flags & SWS_CPU_CAPS_BFIN)
535
        t = ff_yuv2rgb_get_func_ptr_bfin(c);
536
#endif
537

    
538
    if (t)
539
        return t;
540

    
541
    av_log(c, AV_LOG_WARNING, "No accelerated colorspace conversion found from %s to %s.\n", sws_format_name(c->srcFormat), sws_format_name(c->dstFormat));
542

    
543
    switch (c->dstFormat) {
544
    case PIX_FMT_RGB48BE:
545
    case PIX_FMT_RGB48LE:    return yuv2rgb_c_48;
546
    case PIX_FMT_ARGB:
547
    case PIX_FMT_ABGR:       if (CONFIG_SWSCALE_ALPHA && c->srcFormat == PIX_FMT_YUVA420P) return yuva2argb_c;
548
    case PIX_FMT_RGBA:
549
    case PIX_FMT_BGRA:       return (CONFIG_SWSCALE_ALPHA && c->srcFormat == PIX_FMT_YUVA420P) ? yuva2rgba_c : yuv2rgb_c_32;
550
    case PIX_FMT_RGB24:      return yuv2rgb_c_24_rgb;
551
    case PIX_FMT_BGR24:      return yuv2rgb_c_24_bgr;
552
    case PIX_FMT_RGB565:
553
    case PIX_FMT_BGR565:
554
    case PIX_FMT_RGB555:
555
    case PIX_FMT_BGR555:     return yuv2rgb_c_16;
556
    case PIX_FMT_RGB8:
557
    case PIX_FMT_BGR8:       return yuv2rgb_c_8_ordered_dither;
558
    case PIX_FMT_RGB4:
559
    case PIX_FMT_BGR4:       return yuv2rgb_c_4_ordered_dither;
560
    case PIX_FMT_RGB4_BYTE:
561
    case PIX_FMT_BGR4_BYTE:  return yuv2rgb_c_4b_ordered_dither;
562
    case PIX_FMT_MONOBLACK:  return yuv2rgb_c_1_ordered_dither;
563
    default:
564
        assert(0);
565
    }
566
    return NULL;
567
}
568

    
569
static void fill_table(uint8_t* table[256], const int elemsize, const int inc, uint8_t *y_table)
570
{
571
    int i;
572
    int64_t cb = 0;
573

    
574
    y_table -= elemsize * (inc >> 9);
575

    
576
    for (i = 0; i < 256; i++) {
577
        table[i] = y_table + elemsize * (cb >> 16);
578
        cb += inc;
579
    }
580
}
581

    
582
static void fill_gv_table(int table[256], const int elemsize, const int inc)
583
{
584
    int i;
585
    int64_t cb = 0;
586
    int off = -(inc >> 9);
587

    
588
    for (i = 0; i < 256; i++) {
589
        table[i] = elemsize * (off + (cb >> 16));
590
        cb += inc;
591
    }
592
}
593

    
594
av_cold int ff_yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4], int fullRange,
595
                                     int brightness, int contrast, int saturation)
596
{
597
    const int isRgb =      c->dstFormat==PIX_FMT_RGB32
598
                        || c->dstFormat==PIX_FMT_RGB32_1
599
                        || c->dstFormat==PIX_FMT_BGR24
600
                        || c->dstFormat==PIX_FMT_RGB565BE
601
                        || c->dstFormat==PIX_FMT_RGB565LE
602
                        || c->dstFormat==PIX_FMT_RGB555BE
603
                        || c->dstFormat==PIX_FMT_RGB555LE
604
                        || c->dstFormat==PIX_FMT_RGB8
605
                        || c->dstFormat==PIX_FMT_RGB4
606
                        || c->dstFormat==PIX_FMT_RGB4_BYTE
607
                        || c->dstFormat==PIX_FMT_MONOBLACK;
608
    const int isNotNe =    c->dstFormat==PIX_FMT_NE(RGB565LE,RGB565BE)
609
                        || c->dstFormat==PIX_FMT_NE(RGB555LE,RGB555BE)
610
                        || c->dstFormat==PIX_FMT_NE(BGR565LE,BGR565BE)
611
                        || c->dstFormat==PIX_FMT_NE(BGR555LE,BGR555BE);
612
    const int bpp = c->dstFormatBpp;
613
    uint8_t *y_table;
614
    uint16_t *y_table16;
615
    uint32_t *y_table32;
616
    int i, base, rbase, gbase, bbase, abase, needAlpha;
617
    const int yoffs = fullRange ? 384 : 326;
618

    
619
    int64_t crv =  inv_table[0];
620
    int64_t cbu =  inv_table[1];
621
    int64_t cgu = -inv_table[2];
622
    int64_t cgv = -inv_table[3];
623
    int64_t cy  = 1<<16;
624
    int64_t oy  = 0;
625

    
626
    int64_t yb = 0;
627

    
628
    if (!fullRange) {
629
        cy = (cy*255) / 219;
630
        oy = 16<<16;
631
    } else {
632
        crv = (crv*224) / 255;
633
        cbu = (cbu*224) / 255;
634
        cgu = (cgu*224) / 255;
635
        cgv = (cgv*224) / 255;
636
    }
637

    
638
    cy  = (cy *contrast             ) >> 16;
639
    crv = (crv*contrast * saturation) >> 32;
640
    cbu = (cbu*contrast * saturation) >> 32;
641
    cgu = (cgu*contrast * saturation) >> 32;
642
    cgv = (cgv*contrast * saturation) >> 32;
643
    oy -= 256*brightness;
644

    
645
    //scale coefficients by cy
646
    crv = ((crv << 16) + 0x8000) / cy;
647
    cbu = ((cbu << 16) + 0x8000) / cy;
648
    cgu = ((cgu << 16) + 0x8000) / cy;
649
    cgv = ((cgv << 16) + 0x8000) / cy;
650

    
651
    av_free(c->yuvTable);
652

    
653
    switch (bpp) {
654
    case 1:
655
        c->yuvTable = av_malloc(1024);
656
        y_table = c->yuvTable;
657
        yb = -(384<<16) - oy;
658
        for (i = 0; i < 1024-110; i++) {
659
            y_table[i+110] = av_clip_uint8((yb + 0x8000) >> 16) >> 7;
660
            yb += cy;
661
        }
662
        fill_table(c->table_gU, 1, cgu, y_table + yoffs);
663
        fill_gv_table(c->table_gV, 1, cgv);
664
        break;
665
    case 4:
666
    case 4|128:
667
        rbase = isRgb ? 3 : 0;
668
        gbase = 1;
669
        bbase = isRgb ? 0 : 3;
670
        c->yuvTable = av_malloc(1024*3);
671
        y_table = c->yuvTable;
672
        yb = -(384<<16) - oy;
673
        for (i = 0; i < 1024-110; i++) {
674
            int yval = av_clip_uint8((yb + 0x8000) >> 16);
675
            y_table[i+110     ] =  (yval >> 7)       << rbase;
676
            y_table[i+ 37+1024] = ((yval + 43) / 85) << gbase;
677
            y_table[i+110+2048] =  (yval >> 7)       << bbase;
678
            yb += cy;
679
        }
680
        fill_table(c->table_rV, 1, crv, y_table + yoffs);
681
        fill_table(c->table_gU, 1, cgu, y_table + yoffs + 1024);
682
        fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2048);
683
        fill_gv_table(c->table_gV, 1, cgv);
684
        break;
685
    case 8:
686
        rbase = isRgb ? 5 : 0;
687
        gbase = isRgb ? 2 : 3;
688
        bbase = isRgb ? 0 : 6;
689
        c->yuvTable = av_malloc(1024*3);
690
        y_table = c->yuvTable;
691
        yb = -(384<<16) - oy;
692
        for (i = 0; i < 1024-38; i++) {
693
            int yval = av_clip_uint8((yb + 0x8000) >> 16);
694
            y_table[i+16     ] = ((yval + 18) / 36) << rbase;
695
            y_table[i+16+1024] = ((yval + 18) / 36) << gbase;
696
            y_table[i+37+2048] = ((yval + 43) / 85) << bbase;
697
            yb += cy;
698
        }
699
        fill_table(c->table_rV, 1, crv, y_table + yoffs);
700
        fill_table(c->table_gU, 1, cgu, y_table + yoffs + 1024);
701
        fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2048);
702
        fill_gv_table(c->table_gV, 1, cgv);
703
        break;
704
    case 15:
705
    case 16:
706
        rbase = isRgb ? bpp - 5 : 0;
707
        gbase = 5;
708
        bbase = isRgb ? 0 : (bpp - 5);
709
        c->yuvTable = av_malloc(1024*3*2);
710
        y_table16 = c->yuvTable;
711
        yb = -(384<<16) - oy;
712
        for (i = 0; i < 1024; i++) {
713
            uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
714
            y_table16[i     ] = (yval >> 3)          << rbase;
715
            y_table16[i+1024] = (yval >> (18 - bpp)) << gbase;
716
            y_table16[i+2048] = (yval >> 3)          << bbase;
717
            yb += cy;
718
        }
719
        if(isNotNe)
720
            for (i = 0; i < 1024*3; i++)
721
                y_table16[i] = bswap_16(y_table16[i]);
722
        fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
723
        fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + 1024);
724
        fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2048);
725
        fill_gv_table(c->table_gV, 2, cgv);
726
        break;
727
    case 24:
728
    case 48:
729
        c->yuvTable = av_malloc(1024);
730
        y_table = c->yuvTable;
731
        yb = -(384<<16) - oy;
732
        for (i = 0; i < 1024; i++) {
733
            y_table[i] = av_clip_uint8((yb + 0x8000) >> 16);
734
            yb += cy;
735
        }
736
        fill_table(c->table_rV, 1, crv, y_table + yoffs);
737
        fill_table(c->table_gU, 1, cgu, y_table + yoffs);
738
        fill_table(c->table_bU, 1, cbu, y_table + yoffs);
739
        fill_gv_table(c->table_gV, 1, cgv);
740
        break;
741
    case 32:
742
        base = (c->dstFormat == PIX_FMT_RGB32_1 || c->dstFormat == PIX_FMT_BGR32_1) ? 8 : 0;
743
        rbase = base + (isRgb ? 16 : 0);
744
        gbase = base + 8;
745
        bbase = base + (isRgb ? 0 : 16);
746
        needAlpha = CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat);
747
        if (!needAlpha)
748
            abase = (base + 24) & 31;
749
        c->yuvTable = av_malloc(1024*3*4);
750
        y_table32 = c->yuvTable;
751
        yb = -(384<<16) - oy;
752
        for (i = 0; i < 1024; i++) {
753
            uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
754
            y_table32[i     ] = (yval << rbase) + (needAlpha ? 0 : (255 << abase));
755
            y_table32[i+1024] = yval << gbase;
756
            y_table32[i+2048] = yval << bbase;
757
            yb += cy;
758
        }
759
        fill_table(c->table_rV, 4, crv, y_table32 + yoffs);
760
        fill_table(c->table_gU, 4, cgu, y_table32 + yoffs + 1024);
761
        fill_table(c->table_bU, 4, cbu, y_table32 + yoffs + 2048);
762
        fill_gv_table(c->table_gV, 4, cgv);
763
        break;
764
    default:
765
        c->yuvTable = NULL;
766
        av_log(c, AV_LOG_ERROR, "%ibpp not supported by yuv2rgb\n", bpp);
767
        return -1;
768
    }
769
    return 0;
770
}