Statistics
| Branch: | Revision:

ffmpeg / libswscale / yuv2rgb.c @ 3164d25e

History | View | Annotate | Download (22.5 KB)

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

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

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

    
38
#define DITHER1XBPP // only for MMX
39

    
40
extern const uint8_t dither_8x8_32[8][8];
41
extern const uint8_t dither_8x8_73[8][8];
42
extern const uint8_t dither_8x8_220[8][8];
43

    
44
#if HAVE_MMX && CONFIG_GPL
45

    
46
/* hope these constant values are cache line aligned */
47
DECLARE_ASM_CONST(8, uint64_t, mmx_00ffw)   = 0x00ff00ff00ff00ffULL;
48
DECLARE_ASM_CONST(8, uint64_t, mmx_redmask) = 0xf8f8f8f8f8f8f8f8ULL;
49
DECLARE_ASM_CONST(8, uint64_t, mmx_grnmask) = 0xfcfcfcfcfcfcfcfcULL;
50

    
51
//MMX versions
52
#undef RENAME
53
#undef HAVE_MMX2
54
#undef HAVE_AMD3DNOW
55
#define HAVE_MMX2 0
56
#define HAVE_AMD3DNOW 0
57
#define RENAME(a) a ## _MMX
58
#include "yuv2rgb_template.c"
59

    
60
//MMX2 versions
61
#undef RENAME
62
#undef HAVE_MMX2
63
#define HAVE_MMX2 1
64
#define RENAME(a) a ## _MMX2
65
#include "yuv2rgb_template.c"
66

    
67
#endif /* HAVE_MMX && CONFIG_GPL */
68

    
69
const int32_t ff_yuv2rgb_coeffs[8][4] = {
70
    {117504, 138453, 13954, 34903}, /* no sequence_display_extension */
71
    {117504, 138453, 13954, 34903}, /* ITU-R Rec. 709 (1990) */
72
    {104597, 132201, 25675, 53279}, /* unspecified */
73
    {104597, 132201, 25675, 53279}, /* reserved */
74
    {104448, 132798, 24759, 53109}, /* FCC */
75
    {104597, 132201, 25675, 53279}, /* ITU-R Rec. 624-4 System B, G */
76
    {104597, 132201, 25675, 53279}, /* SMPTE 170M */
77
    {117579, 136230, 16907, 35559}  /* SMPTE 240M (1987) */
78
};
79

    
80
#define LOADCHROMA(i)                               \
81
    U = pu[i];                                      \
82
    V = pv[i];                                      \
83
    r = (void *)c->table_rV[V];                     \
84
    g = (void *)(c->table_gU[U] + c->table_gV[V]);  \
85
    b = (void *)c->table_bU[U];
86

    
87
#define PUTRGB(dst,src,i,o)          \
88
    Y = src[2*i+o];                  \
89
    dst[2*i  ] = r[Y] + g[Y] + b[Y]; \
90
    Y = src[2*i+1-o];                \
91
    dst[2*i+1] = r[Y] + g[Y] + b[Y];
92

    
93
#define PUTRGB24(dst,src,i)                                  \
94
    Y = src[2*i];                                            \
95
    dst[6*i+0] = r[Y]; dst[6*i+1] = g[Y]; dst[6*i+2] = b[Y]; \
96
    Y = src[2*i+1];                                          \
97
    dst[6*i+3] = r[Y]; dst[6*i+4] = g[Y]; dst[6*i+5] = b[Y];
98

    
99
#define PUTBGR24(dst,src,i)                                  \
100
    Y = src[2*i];                                            \
101
    dst[6*i+0] = b[Y]; dst[6*i+1] = g[Y]; dst[6*i+2] = r[Y]; \
102
    Y = src[2*i+1];                                          \
103
    dst[6*i+3] = b[Y]; dst[6*i+4] = g[Y]; dst[6*i+5] = r[Y];
104

    
105
#define PUTRGBA(dst,ysrc,asrc,i,o,s)                    \
106
    Y = ysrc[2*i+o];                                    \
107
    dst[2*i  ] = r[Y] + g[Y] + b[Y] + (asrc[2*i  ]<<s); \
108
    Y = ysrc[2*i+1-o];                                  \
109
    dst[2*i+1] = r[Y] + g[Y] + b[Y] + (asrc[2*i+1]<<s);
110

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

    
139
#define ENDYUV2RGBLINE(dst_delta)\
140
            pu += 4;\
141
            pv += 4;\
142
            py_1 += 8;\
143
            py_2 += 8;\
144
            dst_1 += dst_delta;\
145
            dst_2 += dst_delta;\
146
        }\
147
        if (c->dstW & 4) {\
148
            int av_unused Y, U, V;\
149

    
150
#define ENDYUV2RGBFUNC()\
151
        }\
152
    }\
153
    return srcSliceH;\
154
}
155

    
156
#define CLOSEYUV2RGBFUNC(dst_delta)\
157
    ENDYUV2RGBLINE(dst_delta)\
158
    ENDYUV2RGBFUNC()
159

    
160
YUV2RGBFUNC(yuv2rgb_c_32, uint32_t, 0)
161
    LOADCHROMA(0);
162
    PUTRGB(dst_1,py_1,0,0);
163
    PUTRGB(dst_2,py_2,0,1);
164

    
165
    LOADCHROMA(1);
166
    PUTRGB(dst_2,py_2,1,1);
167
    PUTRGB(dst_1,py_1,1,0);
168
    LOADCHROMA(1);
169
    PUTRGB(dst_2,py_2,1,1);
170
    PUTRGB(dst_1,py_1,1,0);
171

    
172
    LOADCHROMA(2);
173
    PUTRGB(dst_1,py_1,2,0);
174
    PUTRGB(dst_2,py_2,2,1);
175

    
176
    LOADCHROMA(3);
177
    PUTRGB(dst_2,py_2,3,1);
178
    PUTRGB(dst_1,py_1,3,0);
179
ENDYUV2RGBLINE(8)
180
    LOADCHROMA(0);
181
    PUTRGB(dst_1,py_1,0,0);
182
    PUTRGB(dst_2,py_2,0,1);
183

    
184
    LOADCHROMA(1);
185
    PUTRGB(dst_2,py_2,1,1);
186
    PUTRGB(dst_1,py_1,1,0);
187
ENDYUV2RGBFUNC()
188

    
189
YUV2RGBFUNC(yuva2rgba_c, uint32_t, 1)
190
    LOADCHROMA(0);
191
    PUTRGBA(dst_1,py_1,pa_1,0,0,24);
192
    PUTRGBA(dst_2,py_2,pa_2,0,1,24);
193

    
194
    LOADCHROMA(1);
195
    PUTRGBA(dst_2,py_2,pa_1,1,1,24);
196
    PUTRGBA(dst_1,py_1,pa_2,1,0,24);
197
    LOADCHROMA(1);
198
    PUTRGBA(dst_2,py_2,pa_1,1,1,24);
199
    PUTRGBA(dst_1,py_1,pa_2,1,0,24);
200

    
201
    LOADCHROMA(2);
202
    PUTRGBA(dst_1,py_1,pa_1,2,0,24);
203
    PUTRGBA(dst_2,py_2,pa_2,2,1,24);
204

    
205
    LOADCHROMA(3);
206
    PUTRGBA(dst_2,py_2,pa_1,3,1,24);
207
    PUTRGBA(dst_1,py_1,pa_2,3,0,24);
208
    pa_1 += 8;\
209
    pa_2 += 8;\
210
ENDYUV2RGBLINE(8)
211
    LOADCHROMA(0);
212
    PUTRGBA(dst_1,py_1,pa_1,0,0,24);
213
    PUTRGBA(dst_2,py_2,pa_2,0,1,24);
214

    
215
    LOADCHROMA(1);
216
    PUTRGBA(dst_2,py_2,pa_1,1,1,24);
217
    PUTRGBA(dst_1,py_1,pa_2,1,0,24);
218
ENDYUV2RGBFUNC()
219

    
220
YUV2RGBFUNC(yuva2argb_c, uint32_t, 1)
221
    LOADCHROMA(0);
222
    PUTRGBA(dst_1,py_1,pa_1,0,0,0);
223
    PUTRGBA(dst_2,py_2,pa_2,0,1,0);
224

    
225
    LOADCHROMA(1);
226
    PUTRGBA(dst_2,py_2,pa_2,1,1,0);
227
    PUTRGBA(dst_1,py_1,pa_1,1,0,0);
228
    LOADCHROMA(1);
229
    PUTRGBA(dst_2,py_2,pa_2,1,1,0);
230
    PUTRGBA(dst_1,py_1,pa_1,1,0,0);
231

    
232
    LOADCHROMA(2);
233
    PUTRGBA(dst_1,py_1,pa_1,2,0,0);
234
    PUTRGBA(dst_2,py_2,pa_2,2,1,0);
235

    
236
    LOADCHROMA(3);
237
    PUTRGBA(dst_2,py_2,pa_2,3,1,0);
238
    PUTRGBA(dst_1,py_1,pa_1,3,0,0);
239
    pa_1 += 8;\
240
    pa_2 += 8;\
241
ENDYUV2RGBLINE(8)
242
    LOADCHROMA(0);
243
    PUTRGBA(dst_1,py_1,pa_1,0,0,0);
244
    PUTRGBA(dst_2,py_2,pa_2,0,1,0);
245

    
246
    LOADCHROMA(1);
247
    PUTRGBA(dst_2,py_2,pa_2,1,1,0);
248
    PUTRGBA(dst_1,py_1,pa_1,1,0,0);
249
ENDYUV2RGBFUNC()
250

    
251
YUV2RGBFUNC(yuv2rgb_c_24_rgb, uint8_t, 0)
252
    LOADCHROMA(0);
253
    PUTRGB24(dst_1,py_1,0);
254
    PUTRGB24(dst_2,py_2,0);
255

    
256
    LOADCHROMA(1);
257
    PUTRGB24(dst_2,py_2,1);
258
    PUTRGB24(dst_1,py_1,1);
259

    
260
    LOADCHROMA(2);
261
    PUTRGB24(dst_1,py_1,2);
262
    PUTRGB24(dst_2,py_2,2);
263

    
264
    LOADCHROMA(3);
265
    PUTRGB24(dst_2,py_2,3);
266
    PUTRGB24(dst_1,py_1,3);
267
ENDYUV2RGBLINE(24)
268
    LOADCHROMA(0);
269
    PUTRGB24(dst_1,py_1,0);
270
    PUTRGB24(dst_2,py_2,0);
271

    
272
    LOADCHROMA(1);
273
    PUTRGB24(dst_2,py_2,1);
274
    PUTRGB24(dst_1,py_1,1);
275
ENDYUV2RGBFUNC()
276

    
277
// only trivial mods from yuv2rgb_c_24_rgb
278
YUV2RGBFUNC(yuv2rgb_c_24_bgr, uint8_t, 0)
279
    LOADCHROMA(0);
280
    PUTBGR24(dst_1,py_1,0);
281
    PUTBGR24(dst_2,py_2,0);
282

    
283
    LOADCHROMA(1);
284
    PUTBGR24(dst_2,py_2,1);
285
    PUTBGR24(dst_1,py_1,1);
286

    
287
    LOADCHROMA(2);
288
    PUTBGR24(dst_1,py_1,2);
289
    PUTBGR24(dst_2,py_2,2);
290

    
291
    LOADCHROMA(3);
292
    PUTBGR24(dst_2,py_2,3);
293
    PUTBGR24(dst_1,py_1,3);
294
ENDYUV2RGBLINE(24)
295
    LOADCHROMA(0);
296
    PUTBGR24(dst_1,py_1,0);
297
    PUTBGR24(dst_2,py_2,0);
298

    
299
    LOADCHROMA(1);
300
    PUTBGR24(dst_2,py_2,1);
301
    PUTBGR24(dst_1,py_1,1);
302
ENDYUV2RGBFUNC()
303

    
304
// This is exactly the same code as yuv2rgb_c_32 except for the types of
305
// r, g, b, dst_1, dst_2
306
YUV2RGBFUNC(yuv2rgb_c_16, uint16_t, 0)
307
    LOADCHROMA(0);
308
    PUTRGB(dst_1,py_1,0,0);
309
    PUTRGB(dst_2,py_2,0,1);
310

    
311
    LOADCHROMA(1);
312
    PUTRGB(dst_2,py_2,1,1);
313
    PUTRGB(dst_1,py_1,1,0);
314

    
315
    LOADCHROMA(2);
316
    PUTRGB(dst_1,py_1,2,0);
317
    PUTRGB(dst_2,py_2,2,1);
318

    
319
    LOADCHROMA(3);
320
    PUTRGB(dst_2,py_2,3,1);
321
    PUTRGB(dst_1,py_1,3,0);
322
CLOSEYUV2RGBFUNC(8)
323

    
324
// This is exactly the same code as yuv2rgb_c_32 except for the types of
325
// r, g, b, dst_1, dst_2
326
YUV2RGBFUNC(yuv2rgb_c_8, uint8_t, 0)
327
    LOADCHROMA(0);
328
    PUTRGB(dst_1,py_1,0,0);
329
    PUTRGB(dst_2,py_2,0,1);
330

    
331
    LOADCHROMA(1);
332
    PUTRGB(dst_2,py_2,1,1);
333
    PUTRGB(dst_1,py_1,1,0);
334

    
335
    LOADCHROMA(2);
336
    PUTRGB(dst_1,py_1,2,0);
337
    PUTRGB(dst_2,py_2,2,1);
338

    
339
    LOADCHROMA(3);
340
    PUTRGB(dst_2,py_2,3,1);
341
    PUTRGB(dst_1,py_1,3,0);
342
CLOSEYUV2RGBFUNC(8)
343

    
344
// r, g, b, dst_1, dst_2
345
YUV2RGBFUNC(yuv2rgb_c_8_ordered_dither, uint8_t, 0)
346
    const uint8_t *d32 = dither_8x8_32[y&7];
347
    const uint8_t *d64 = dither_8x8_73[y&7];
348
#define PUTRGB8(dst,src,i,o)                                    \
349
    Y = src[2*i];                                               \
350
    dst[2*i]   = r[Y+d32[0+o]] + g[Y+d32[0+o]] + b[Y+d64[0+o]]; \
351
    Y = src[2*i+1];                                             \
352
    dst[2*i+1] = r[Y+d32[1+o]] + g[Y+d32[1+o]] + b[Y+d64[1+o]];
353

    
354
    LOADCHROMA(0);
355
    PUTRGB8(dst_1,py_1,0,0);
356
    PUTRGB8(dst_2,py_2,0,0+8);
357

    
358
    LOADCHROMA(1);
359
    PUTRGB8(dst_2,py_2,1,2+8);
360
    PUTRGB8(dst_1,py_1,1,2);
361

    
362
    LOADCHROMA(2);
363
    PUTRGB8(dst_1,py_1,2,4);
364
    PUTRGB8(dst_2,py_2,2,4+8);
365

    
366
    LOADCHROMA(3);
367
    PUTRGB8(dst_2,py_2,3,6+8);
368
    PUTRGB8(dst_1,py_1,3,6);
369
CLOSEYUV2RGBFUNC(8)
370

    
371

    
372
// This is exactly the same code as yuv2rgb_c_32 except for the types of
373
// r, g, b, dst_1, dst_2
374
YUV2RGBFUNC(yuv2rgb_c_4, uint8_t, 0)
375
    int acc;
376
#define PUTRGB4(dst,src,i)          \
377
    Y = src[2*i];                   \
378
    acc = r[Y] + g[Y] + b[Y];       \
379
    Y = src[2*i+1];                 \
380
    acc |= (r[Y] + g[Y] + b[Y])<<4; \
381
    dst[i] = acc;
382

    
383
    LOADCHROMA(0);
384
    PUTRGB4(dst_1,py_1,0);
385
    PUTRGB4(dst_2,py_2,0);
386

    
387
    LOADCHROMA(1);
388
    PUTRGB4(dst_2,py_2,1);
389
    PUTRGB4(dst_1,py_1,1);
390

    
391
    LOADCHROMA(2);
392
    PUTRGB4(dst_1,py_1,2);
393
    PUTRGB4(dst_2,py_2,2);
394

    
395
    LOADCHROMA(3);
396
    PUTRGB4(dst_2,py_2,3);
397
    PUTRGB4(dst_1,py_1,3);
398
CLOSEYUV2RGBFUNC(4)
399

    
400
YUV2RGBFUNC(yuv2rgb_c_4_ordered_dither, uint8_t, 0)
401
    const uint8_t *d64 =  dither_8x8_73[y&7];
402
    const uint8_t *d128 = dither_8x8_220[y&7];
403
    int acc;
404

    
405
#define PUTRGB4D(dst,src,i,o)                                     \
406
    Y = src[2*i];                                                 \
407
    acc = r[Y+d128[0+o]] + g[Y+d64[0+o]] + b[Y+d128[0+o]];        \
408
    Y = src[2*i+1];                                               \
409
    acc |= (r[Y+d128[1+o]] + g[Y+d64[1+o]] + b[Y+d128[1+o]])<<4;  \
410
    dst[i]= acc;
411

    
412
    LOADCHROMA(0);
413
    PUTRGB4D(dst_1,py_1,0,0);
414
    PUTRGB4D(dst_2,py_2,0,0+8);
415

    
416
    LOADCHROMA(1);
417
    PUTRGB4D(dst_2,py_2,1,2+8);
418
    PUTRGB4D(dst_1,py_1,1,2);
419

    
420
    LOADCHROMA(2);
421
    PUTRGB4D(dst_1,py_1,2,4);
422
    PUTRGB4D(dst_2,py_2,2,4+8);
423

    
424
    LOADCHROMA(3);
425
    PUTRGB4D(dst_2,py_2,3,6+8);
426
    PUTRGB4D(dst_1,py_1,3,6);
427
CLOSEYUV2RGBFUNC(4)
428

    
429
// This is exactly the same code as yuv2rgb_c_32 except for the types of
430
// r, g, b, dst_1, dst_2
431
YUV2RGBFUNC(yuv2rgb_c_4b, uint8_t, 0)
432
    LOADCHROMA(0);
433
    PUTRGB(dst_1,py_1,0,0);
434
    PUTRGB(dst_2,py_2,0,1);
435

    
436
    LOADCHROMA(1);
437
    PUTRGB(dst_2,py_2,1,1);
438
    PUTRGB(dst_1,py_1,1,0);
439

    
440
    LOADCHROMA(2);
441
    PUTRGB(dst_1,py_1,2,0);
442
    PUTRGB(dst_2,py_2,2,1);
443

    
444
    LOADCHROMA(3);
445
    PUTRGB(dst_2,py_2,3,1);
446
    PUTRGB(dst_1,py_1,3,0);
447
CLOSEYUV2RGBFUNC(8)
448

    
449
YUV2RGBFUNC(yuv2rgb_c_4b_ordered_dither, uint8_t, 0)
450
    const uint8_t *d64 =  dither_8x8_73[y&7];
451
    const uint8_t *d128 = dither_8x8_220[y&7];
452

    
453
#define PUTRGB4DB(dst,src,i,o)                                    \
454
    Y = src[2*i];                                                 \
455
    dst[2*i]   = r[Y+d128[0+o]] + g[Y+d64[0+o]] + b[Y+d128[0+o]]; \
456
    Y = src[2*i+1];                                               \
457
    dst[2*i+1] = r[Y+d128[1+o]] + g[Y+d64[1+o]] + b[Y+d128[1+o]];
458

    
459
    LOADCHROMA(0);
460
    PUTRGB4DB(dst_1,py_1,0,0);
461
    PUTRGB4DB(dst_2,py_2,0,0+8);
462

    
463
    LOADCHROMA(1);
464
    PUTRGB4DB(dst_2,py_2,1,2+8);
465
    PUTRGB4DB(dst_1,py_1,1,2);
466

    
467
    LOADCHROMA(2);
468
    PUTRGB4DB(dst_1,py_1,2,4);
469
    PUTRGB4DB(dst_2,py_2,2,4+8);
470

    
471
    LOADCHROMA(3);
472
    PUTRGB4DB(dst_2,py_2,3,6+8);
473
    PUTRGB4DB(dst_1,py_1,3,6);
474
CLOSEYUV2RGBFUNC(8)
475

    
476
YUV2RGBFUNC(yuv2rgb_c_1_ordered_dither, uint8_t, 0)
477
        const uint8_t *d128 = dither_8x8_220[y&7];
478
        char out_1 = 0, out_2 = 0;
479
        g= c->table_gU[128] + c->table_gV[128];
480

    
481
#define PUTRGB1(out,src,i,o)    \
482
    Y = src[2*i];               \
483
    out+= out + g[Y+d128[0+o]]; \
484
    Y = src[2*i+1];             \
485
    out+= out + g[Y+d128[1+o]];
486

    
487
    PUTRGB1(out_1,py_1,0,0);
488
    PUTRGB1(out_2,py_2,0,0+8);
489

    
490
    PUTRGB1(out_2,py_2,1,2+8);
491
    PUTRGB1(out_1,py_1,1,2);
492

    
493
    PUTRGB1(out_1,py_1,2,4);
494
    PUTRGB1(out_2,py_2,2,4+8);
495

    
496
    PUTRGB1(out_2,py_2,3,6+8);
497
    PUTRGB1(out_1,py_1,3,6);
498

    
499
    dst_1[0]= out_1;
500
    dst_2[0]= out_2;
501
CLOSEYUV2RGBFUNC(1)
502

    
503
SwsFunc ff_yuv2rgb_get_func_ptr(SwsContext *c)
504
{
505
    SwsFunc t = NULL;
506
#if (HAVE_MMX2 || HAVE_MMX) && CONFIG_GPL
507
    if (c->flags & SWS_CPU_CAPS_MMX2) {
508
        switch (c->dstFormat) {
509
        case PIX_FMT_RGB32:
510
            if (CONFIG_SWSCALE_ALPHA && c->srcFormat == PIX_FMT_YUVA420P){
511
                if (HAVE_7REGS) return yuva420_rgb32_MMX2;
512
            }else return yuv420_rgb32_MMX2;
513
        case PIX_FMT_BGR24:  return yuv420_rgb24_MMX2;
514
        case PIX_FMT_RGB565: return yuv420_rgb16_MMX2;
515
        case PIX_FMT_RGB555: return yuv420_rgb15_MMX2;
516
        }
517
    }
518
    if (c->flags & SWS_CPU_CAPS_MMX) {
519
        switch (c->dstFormat) {
520
        case PIX_FMT_RGB32:
521
            if (CONFIG_SWSCALE_ALPHA && c->srcFormat == PIX_FMT_YUVA420P){
522
                if (HAVE_7REGS) return yuva420_rgb32_MMX;
523
            }else return yuv420_rgb32_MMX;
524
        case PIX_FMT_BGR24:  return yuv420_rgb24_MMX;
525
        case PIX_FMT_RGB565: return yuv420_rgb16_MMX;
526
        case PIX_FMT_RGB555: return yuv420_rgb15_MMX;
527
        }
528
    }
529
#endif
530
#if HAVE_VIS
531
    t = ff_yuv2rgb_init_vis(c);
532
#endif
533
#if CONFIG_MLIB
534
    t = ff_yuv2rgb_init_mlib(c);
535
#endif
536
#if HAVE_ALTIVEC && CONFIG_GPL
537
    if (c->flags & SWS_CPU_CAPS_ALTIVEC)
538
        t = ff_yuv2rgb_init_altivec(c);
539
#endif
540

    
541
#if ARCH_BFIN
542
    if (c->flags & SWS_CPU_CAPS_BFIN)
543
        t = ff_yuv2rgb_get_func_ptr_bfin(c);
544
#endif
545

    
546
    if (t)
547
        return t;
548

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

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

    
575
static void fill_table(uint8_t* table[256], const int elemsize, const int inc, uint8_t *y_table)
576
{
577
    int i;
578
    int64_t cb = 0;
579

    
580
    y_table -= elemsize * (inc >> 9);
581

    
582
    for (i = 0; i < 256; i++) {
583
        table[i] = y_table + elemsize * (cb >> 16);
584
        cb += inc;
585
    }
586
}
587

    
588
static void fill_gv_table(int table[256], const int elemsize, const int inc)
589
{
590
    int i;
591
    int64_t cb = 0;
592
    int off = -(inc >> 9);
593

    
594
    for (i = 0; i < 256; i++) {
595
        table[i] = elemsize * (off + (cb >> 16));
596
        cb += inc;
597
    }
598
}
599

    
600
av_cold int ff_yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4], int fullRange,
601
                                     int brightness, int contrast, int saturation)
602
{
603
    const int isRgb =      c->dstFormat==PIX_FMT_RGB32
604
                        || c->dstFormat==PIX_FMT_RGB32_1
605
                        || c->dstFormat==PIX_FMT_BGR24
606
                        || c->dstFormat==PIX_FMT_RGB565
607
                        || c->dstFormat==PIX_FMT_RGB555
608
                        || c->dstFormat==PIX_FMT_RGB8
609
                        || c->dstFormat==PIX_FMT_RGB4
610
                        || c->dstFormat==PIX_FMT_RGB4_BYTE
611
                        || c->dstFormat==PIX_FMT_MONOBLACK;
612
    const int bpp = fmt_depth(c->dstFormat);
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
        fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
720
        fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + 1024);
721
        fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2048);
722
        fill_gv_table(c->table_gV, 2, cgv);
723
        break;
724
    case 24:
725
        c->yuvTable = av_malloc(1024);
726
        y_table = c->yuvTable;
727
        yb = -(384<<16) - oy;
728
        for (i = 0; i < 1024; i++) {
729
            y_table[i] = av_clip_uint8((yb + 0x8000) >> 16);
730
            yb += cy;
731
        }
732
        fill_table(c->table_rV, 1, crv, y_table + yoffs);
733
        fill_table(c->table_gU, 1, cgu, y_table + yoffs);
734
        fill_table(c->table_bU, 1, cbu, y_table + yoffs);
735
        fill_gv_table(c->table_gV, 1, cgv);
736
        break;
737
    case 32:
738
        base = (c->dstFormat == PIX_FMT_RGB32_1 || c->dstFormat == PIX_FMT_BGR32_1) ? 8 : 0;
739
        rbase = base + (isRgb ? 16 : 0);
740
        gbase = base + 8;
741
        bbase = base + (isRgb ? 0 : 16);
742
        needAlpha = CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat);
743
        if (!needAlpha)
744
            abase = (base + 24) & 31;
745
        c->yuvTable = av_malloc(1024*3*4);
746
        y_table32 = c->yuvTable;
747
        yb = -(384<<16) - oy;
748
        for (i = 0; i < 1024; i++) {
749
            uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
750
            y_table32[i     ] = (yval << rbase) + (needAlpha ? 0 : (255 << abase));
751
            y_table32[i+1024] = yval << gbase;
752
            y_table32[i+2048] = yval << bbase;
753
            yb += cy;
754
        }
755
        fill_table(c->table_rV, 4, crv, y_table32 + yoffs);
756
        fill_table(c->table_gU, 4, cgu, y_table32 + yoffs + 1024);
757
        fill_table(c->table_bU, 4, cbu, y_table32 + yoffs + 2048);
758
        fill_gv_table(c->table_gV, 4, cgv);
759
        break;
760
    default:
761
        c->yuvTable = NULL;
762
        av_log(c, AV_LOG_ERROR, "%ibpp not supported by yuv2rgb\n", bpp);
763
        return -1;
764
    }
765
    return 0;
766
}