Statistics
| Branch: | Revision:

ffmpeg / libswscale / yuv2rgb.c @ 95c8bb03

History | View | Annotate | Download (26.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_4x4_16[4][8];
39
extern const uint8_t dither_8x8_32[8][8];
40
extern const uint8_t dither_8x8_73[8][8];
41
extern const uint8_t dither_8x8_220[8][8];
42

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

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

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

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

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

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

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

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

    
102
#define PUTBGR48(dst,src,i)             \
103
    Y = src[2*i];                       \
104
    dst[12*i+ 0] = dst[12*i+ 1] = b[Y]; \
105
    dst[12*i+ 2] = dst[12*i+ 3] = g[Y]; \
106
    dst[12*i+ 4] = dst[12*i+ 5] = r[Y]; \
107
    Y = src[2*i+1];                     \
108
    dst[12*i+ 6] = dst[12*i+ 7] = b[Y]; \
109
    dst[12*i+ 8] = dst[12*i+ 9] = g[Y]; \
110
    dst[12*i+10] = dst[12*i+11] = r[Y];
111

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

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

    
152
#define ENDYUV2RGBFUNC()\
153
        }\
154
    }\
155
    return srcSliceH;\
156
}
157

    
158
#define CLOSEYUV2RGBFUNC(dst_delta)\
159
    ENDYUV2RGBLINE(dst_delta)\
160
    ENDYUV2RGBFUNC()
161

    
162
YUV2RGBFUNC(yuv2rgb_c_48, uint8_t, 0)
163
    LOADCHROMA(0);
164
    PUTRGB48(dst_1,py_1,0);
165
    PUTRGB48(dst_2,py_2,0);
166

    
167
    LOADCHROMA(1);
168
    PUTRGB48(dst_2,py_2,1);
169
    PUTRGB48(dst_1,py_1,1);
170

    
171
    LOADCHROMA(2);
172
    PUTRGB48(dst_1,py_1,2);
173
    PUTRGB48(dst_2,py_2,2);
174

    
175
    LOADCHROMA(3);
176
    PUTRGB48(dst_2,py_2,3);
177
    PUTRGB48(dst_1,py_1,3);
178
ENDYUV2RGBLINE(48)
179
    LOADCHROMA(0);
180
    PUTRGB48(dst_1,py_1,0);
181
    PUTRGB48(dst_2,py_2,0);
182

    
183
    LOADCHROMA(1);
184
    PUTRGB48(dst_2,py_2,1);
185
    PUTRGB48(dst_1,py_1,1);
186
ENDYUV2RGBFUNC()
187

    
188
YUV2RGBFUNC(yuv2rgb_c_bgr48, uint8_t, 0)
189
    LOADCHROMA(0);
190
    PUTBGR48(dst_1,py_1,0);
191
    PUTBGR48(dst_2,py_2,0);
192

    
193
    LOADCHROMA(1);
194
    PUTBGR48(dst_2,py_2,1);
195
    PUTBGR48(dst_1,py_1,1);
196

    
197
    LOADCHROMA(2);
198
    PUTBGR48(dst_1,py_1,2);
199
    PUTBGR48(dst_2,py_2,2);
200

    
201
    LOADCHROMA(3);
202
    PUTBGR48(dst_2,py_2,3);
203
    PUTBGR48(dst_1,py_1,3);
204
ENDYUV2RGBLINE(48)
205
    LOADCHROMA(0);
206
    PUTBGR48(dst_1,py_1,0);
207
    PUTBGR48(dst_2,py_2,0);
208

    
209
    LOADCHROMA(1);
210
    PUTBGR48(dst_2,py_2,1);
211
    PUTBGR48(dst_1,py_1,1);
212
ENDYUV2RGBFUNC()
213

    
214
YUV2RGBFUNC(yuv2rgb_c_32, uint32_t, 0)
215
    LOADCHROMA(0);
216
    PUTRGB(dst_1,py_1,0);
217
    PUTRGB(dst_2,py_2,0);
218

    
219
    LOADCHROMA(1);
220
    PUTRGB(dst_2,py_2,1);
221
    PUTRGB(dst_1,py_1,1);
222

    
223
    LOADCHROMA(2);
224
    PUTRGB(dst_1,py_1,2);
225
    PUTRGB(dst_2,py_2,2);
226

    
227
    LOADCHROMA(3);
228
    PUTRGB(dst_2,py_2,3);
229
    PUTRGB(dst_1,py_1,3);
230
ENDYUV2RGBLINE(8)
231
    LOADCHROMA(0);
232
    PUTRGB(dst_1,py_1,0);
233
    PUTRGB(dst_2,py_2,0);
234

    
235
    LOADCHROMA(1);
236
    PUTRGB(dst_2,py_2,1);
237
    PUTRGB(dst_1,py_1,1);
238
ENDYUV2RGBFUNC()
239

    
240
YUV2RGBFUNC(yuva2rgba_c, uint32_t, 1)
241
    LOADCHROMA(0);
242
    PUTRGBA(dst_1,py_1,pa_1,0,24);
243
    PUTRGBA(dst_2,py_2,pa_2,0,24);
244

    
245
    LOADCHROMA(1);
246
    PUTRGBA(dst_2,py_2,pa_1,1,24);
247
    PUTRGBA(dst_1,py_1,pa_2,1,24);
248

    
249
    LOADCHROMA(2);
250
    PUTRGBA(dst_1,py_1,pa_1,2,24);
251
    PUTRGBA(dst_2,py_2,pa_2,2,24);
252

    
253
    LOADCHROMA(3);
254
    PUTRGBA(dst_2,py_2,pa_1,3,24);
255
    PUTRGBA(dst_1,py_1,pa_2,3,24);
256
    pa_1 += 8;\
257
    pa_2 += 8;\
258
ENDYUV2RGBLINE(8)
259
    LOADCHROMA(0);
260
    PUTRGBA(dst_1,py_1,pa_1,0,24);
261
    PUTRGBA(dst_2,py_2,pa_2,0,24);
262

    
263
    LOADCHROMA(1);
264
    PUTRGBA(dst_2,py_2,pa_1,1,24);
265
    PUTRGBA(dst_1,py_1,pa_2,1,24);
266
ENDYUV2RGBFUNC()
267

    
268
YUV2RGBFUNC(yuva2argb_c, uint32_t, 1)
269
    LOADCHROMA(0);
270
    PUTRGBA(dst_1,py_1,pa_1,0,0);
271
    PUTRGBA(dst_2,py_2,pa_2,0,0);
272

    
273
    LOADCHROMA(1);
274
    PUTRGBA(dst_2,py_2,pa_2,1,0);
275
    PUTRGBA(dst_1,py_1,pa_1,1,0);
276

    
277
    LOADCHROMA(2);
278
    PUTRGBA(dst_1,py_1,pa_1,2,0);
279
    PUTRGBA(dst_2,py_2,pa_2,2,0);
280

    
281
    LOADCHROMA(3);
282
    PUTRGBA(dst_2,py_2,pa_2,3,0);
283
    PUTRGBA(dst_1,py_1,pa_1,3,0);
284
    pa_1 += 8;\
285
    pa_2 += 8;\
286
ENDYUV2RGBLINE(8)
287
    LOADCHROMA(0);
288
    PUTRGBA(dst_1,py_1,pa_1,0,0);
289
    PUTRGBA(dst_2,py_2,pa_2,0,0);
290

    
291
    LOADCHROMA(1);
292
    PUTRGBA(dst_2,py_2,pa_2,1,0);
293
    PUTRGBA(dst_1,py_1,pa_1,1,0);
294
ENDYUV2RGBFUNC()
295

    
296
YUV2RGBFUNC(yuv2rgb_c_24_rgb, uint8_t, 0)
297
    LOADCHROMA(0);
298
    PUTRGB24(dst_1,py_1,0);
299
    PUTRGB24(dst_2,py_2,0);
300

    
301
    LOADCHROMA(1);
302
    PUTRGB24(dst_2,py_2,1);
303
    PUTRGB24(dst_1,py_1,1);
304

    
305
    LOADCHROMA(2);
306
    PUTRGB24(dst_1,py_1,2);
307
    PUTRGB24(dst_2,py_2,2);
308

    
309
    LOADCHROMA(3);
310
    PUTRGB24(dst_2,py_2,3);
311
    PUTRGB24(dst_1,py_1,3);
312
ENDYUV2RGBLINE(24)
313
    LOADCHROMA(0);
314
    PUTRGB24(dst_1,py_1,0);
315
    PUTRGB24(dst_2,py_2,0);
316

    
317
    LOADCHROMA(1);
318
    PUTRGB24(dst_2,py_2,1);
319
    PUTRGB24(dst_1,py_1,1);
320
ENDYUV2RGBFUNC()
321

    
322
// only trivial mods from yuv2rgb_c_24_rgb
323
YUV2RGBFUNC(yuv2rgb_c_24_bgr, uint8_t, 0)
324
    LOADCHROMA(0);
325
    PUTBGR24(dst_1,py_1,0);
326
    PUTBGR24(dst_2,py_2,0);
327

    
328
    LOADCHROMA(1);
329
    PUTBGR24(dst_2,py_2,1);
330
    PUTBGR24(dst_1,py_1,1);
331

    
332
    LOADCHROMA(2);
333
    PUTBGR24(dst_1,py_1,2);
334
    PUTBGR24(dst_2,py_2,2);
335

    
336
    LOADCHROMA(3);
337
    PUTBGR24(dst_2,py_2,3);
338
    PUTBGR24(dst_1,py_1,3);
339
ENDYUV2RGBLINE(24)
340
    LOADCHROMA(0);
341
    PUTBGR24(dst_1,py_1,0);
342
    PUTBGR24(dst_2,py_2,0);
343

    
344
    LOADCHROMA(1);
345
    PUTBGR24(dst_2,py_2,1);
346
    PUTBGR24(dst_1,py_1,1);
347
ENDYUV2RGBFUNC()
348

    
349
// This is exactly the same code as yuv2rgb_c_32 except for the types of
350
// r, g, b, dst_1, dst_2
351
YUV2RGBFUNC(yuv2rgb_c_16, uint16_t, 0)
352
    LOADCHROMA(0);
353
    PUTRGB(dst_1,py_1,0);
354
    PUTRGB(dst_2,py_2,0);
355

    
356
    LOADCHROMA(1);
357
    PUTRGB(dst_2,py_2,1);
358
    PUTRGB(dst_1,py_1,1);
359

    
360
    LOADCHROMA(2);
361
    PUTRGB(dst_1,py_1,2);
362
    PUTRGB(dst_2,py_2,2);
363

    
364
    LOADCHROMA(3);
365
    PUTRGB(dst_2,py_2,3);
366
    PUTRGB(dst_1,py_1,3);
367
CLOSEYUV2RGBFUNC(8)
368

    
369
#if 0 // Currently unused
370
// This is exactly the same code as yuv2rgb_c_32 except for the types of
371
// r, g, b, dst_1, dst_2
372
YUV2RGBFUNC(yuv2rgb_c_8, uint8_t, 0)
373
    LOADCHROMA(0);
374
    PUTRGB(dst_1,py_1,0);
375
    PUTRGB(dst_2,py_2,0);
376

377
    LOADCHROMA(1);
378
    PUTRGB(dst_2,py_2,1);
379
    PUTRGB(dst_1,py_1,1);
380

381
    LOADCHROMA(2);
382
    PUTRGB(dst_1,py_1,2);
383
    PUTRGB(dst_2,py_2,2);
384

385
    LOADCHROMA(3);
386
    PUTRGB(dst_2,py_2,3);
387
    PUTRGB(dst_1,py_1,3);
388
CLOSEYUV2RGBFUNC(8)
389
#endif
390

    
391
// r, g, b, dst_1, dst_2
392
YUV2RGBFUNC(yuv2rgb_c_12_ordered_dither, uint16_t, 0)
393
    const uint8_t *d16 = dither_4x4_16[y&3];
394
#define PUTRGB12(dst,src,i,o)                                   \
395
    Y = src[2*i];                                               \
396
    dst[2*i]   = r[Y+d16[0+o]] + g[Y+d16[0+o]] + b[Y+d16[0+o]]; \
397
    Y = src[2*i+1];                                             \
398
    dst[2*i+1] = r[Y+d16[1+o]] + g[Y+d16[1+o]] + b[Y+d16[1+o]];
399

    
400
    LOADCHROMA(0);
401
    PUTRGB12(dst_1,py_1,0,0);
402
    PUTRGB12(dst_2,py_2,0,0+8);
403

    
404
    LOADCHROMA(1);
405
    PUTRGB12(dst_2,py_2,1,2+8);
406
    PUTRGB12(dst_1,py_1,1,2);
407

    
408
    LOADCHROMA(2);
409
    PUTRGB12(dst_1,py_1,2,4);
410
    PUTRGB12(dst_2,py_2,2,4+8);
411

    
412
    LOADCHROMA(3);
413
    PUTRGB12(dst_2,py_2,3,6+8);
414
    PUTRGB12(dst_1,py_1,3,6);
415
CLOSEYUV2RGBFUNC(8)
416

    
417
// r, g, b, dst_1, dst_2
418
YUV2RGBFUNC(yuv2rgb_c_8_ordered_dither, uint8_t, 0)
419
    const uint8_t *d32 = dither_8x8_32[y&7];
420
    const uint8_t *d64 = dither_8x8_73[y&7];
421
#define PUTRGB8(dst,src,i,o)                                    \
422
    Y = src[2*i];                                               \
423
    dst[2*i]   = r[Y+d32[0+o]] + g[Y+d32[0+o]] + b[Y+d64[0+o]]; \
424
    Y = src[2*i+1];                                             \
425
    dst[2*i+1] = r[Y+d32[1+o]] + g[Y+d32[1+o]] + b[Y+d64[1+o]];
426

    
427
    LOADCHROMA(0);
428
    PUTRGB8(dst_1,py_1,0,0);
429
    PUTRGB8(dst_2,py_2,0,0+8);
430

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

    
435
    LOADCHROMA(2);
436
    PUTRGB8(dst_1,py_1,2,4);
437
    PUTRGB8(dst_2,py_2,2,4+8);
438

    
439
    LOADCHROMA(3);
440
    PUTRGB8(dst_2,py_2,3,6+8);
441
    PUTRGB8(dst_1,py_1,3,6);
442
CLOSEYUV2RGBFUNC(8)
443

    
444
#if 0 // Currently unused
445
// This is exactly the same code as yuv2rgb_c_32 except for the types of
446
// r, g, b, dst_1, dst_2
447
YUV2RGBFUNC(yuv2rgb_c_4, uint8_t, 0)
448
    int acc;
449
#define PUTRGB4(dst,src,i)          \
450
    Y = src[2*i];                   \
451
    acc = r[Y] + g[Y] + b[Y];       \
452
    Y = src[2*i+1];                 \
453
    acc |= (r[Y] + g[Y] + b[Y])<<4; \
454
    dst[i] = acc;
455

456
    LOADCHROMA(0);
457
    PUTRGB4(dst_1,py_1,0);
458
    PUTRGB4(dst_2,py_2,0);
459

460
    LOADCHROMA(1);
461
    PUTRGB4(dst_2,py_2,1);
462
    PUTRGB4(dst_1,py_1,1);
463

464
    LOADCHROMA(2);
465
    PUTRGB4(dst_1,py_1,2);
466
    PUTRGB4(dst_2,py_2,2);
467

468
    LOADCHROMA(3);
469
    PUTRGB4(dst_2,py_2,3);
470
    PUTRGB4(dst_1,py_1,3);
471
CLOSEYUV2RGBFUNC(4)
472
#endif
473

    
474
YUV2RGBFUNC(yuv2rgb_c_4_ordered_dither, uint8_t, 0)
475
    const uint8_t *d64 =  dither_8x8_73[y&7];
476
    const uint8_t *d128 = dither_8x8_220[y&7];
477
    int acc;
478

    
479
#define PUTRGB4D(dst,src,i,o)                                     \
480
    Y = src[2*i];                                                 \
481
    acc = r[Y+d128[0+o]] + g[Y+d64[0+o]] + b[Y+d128[0+o]];        \
482
    Y = src[2*i+1];                                               \
483
    acc |= (r[Y+d128[1+o]] + g[Y+d64[1+o]] + b[Y+d128[1+o]])<<4;  \
484
    dst[i]= acc;
485

    
486
    LOADCHROMA(0);
487
    PUTRGB4D(dst_1,py_1,0,0);
488
    PUTRGB4D(dst_2,py_2,0,0+8);
489

    
490
    LOADCHROMA(1);
491
    PUTRGB4D(dst_2,py_2,1,2+8);
492
    PUTRGB4D(dst_1,py_1,1,2);
493

    
494
    LOADCHROMA(2);
495
    PUTRGB4D(dst_1,py_1,2,4);
496
    PUTRGB4D(dst_2,py_2,2,4+8);
497

    
498
    LOADCHROMA(3);
499
    PUTRGB4D(dst_2,py_2,3,6+8);
500
    PUTRGB4D(dst_1,py_1,3,6);
501
CLOSEYUV2RGBFUNC(4)
502

    
503
#if 0 // Currently unused
504
// This is exactly the same code as yuv2rgb_c_32 except for the types of
505
// r, g, b, dst_1, dst_2
506
YUV2RGBFUNC(yuv2rgb_c_4b, uint8_t, 0)
507
    LOADCHROMA(0);
508
    PUTRGB(dst_1,py_1,0);
509
    PUTRGB(dst_2,py_2,0);
510

511
    LOADCHROMA(1);
512
    PUTRGB(dst_2,py_2,1);
513
    PUTRGB(dst_1,py_1,1);
514

515
    LOADCHROMA(2);
516
    PUTRGB(dst_1,py_1,2);
517
    PUTRGB(dst_2,py_2,2);
518

519
    LOADCHROMA(3);
520
    PUTRGB(dst_2,py_2,3);
521
    PUTRGB(dst_1,py_1,3);
522
CLOSEYUV2RGBFUNC(8)
523
#endif
524

    
525
YUV2RGBFUNC(yuv2rgb_c_4b_ordered_dither, uint8_t, 0)
526
    const uint8_t *d64 =  dither_8x8_73[y&7];
527
    const uint8_t *d128 = dither_8x8_220[y&7];
528

    
529
#define PUTRGB4DB(dst,src,i,o)                                    \
530
    Y = src[2*i];                                                 \
531
    dst[2*i]   = r[Y+d128[0+o]] + g[Y+d64[0+o]] + b[Y+d128[0+o]]; \
532
    Y = src[2*i+1];                                               \
533
    dst[2*i+1] = r[Y+d128[1+o]] + g[Y+d64[1+o]] + b[Y+d128[1+o]];
534

    
535
    LOADCHROMA(0);
536
    PUTRGB4DB(dst_1,py_1,0,0);
537
    PUTRGB4DB(dst_2,py_2,0,0+8);
538

    
539
    LOADCHROMA(1);
540
    PUTRGB4DB(dst_2,py_2,1,2+8);
541
    PUTRGB4DB(dst_1,py_1,1,2);
542

    
543
    LOADCHROMA(2);
544
    PUTRGB4DB(dst_1,py_1,2,4);
545
    PUTRGB4DB(dst_2,py_2,2,4+8);
546

    
547
    LOADCHROMA(3);
548
    PUTRGB4DB(dst_2,py_2,3,6+8);
549
    PUTRGB4DB(dst_1,py_1,3,6);
550
CLOSEYUV2RGBFUNC(8)
551

    
552
YUV2RGBFUNC(yuv2rgb_c_1_ordered_dither, uint8_t, 0)
553
        const uint8_t *d128 = dither_8x8_220[y&7];
554
        char out_1 = 0, out_2 = 0;
555
        g= c->table_gU[128] + c->table_gV[128];
556

    
557
#define PUTRGB1(out,src,i,o)    \
558
    Y = src[2*i];               \
559
    out+= out + g[Y+d128[0+o]]; \
560
    Y = src[2*i+1];             \
561
    out+= out + g[Y+d128[1+o]];
562

    
563
    PUTRGB1(out_1,py_1,0,0);
564
    PUTRGB1(out_2,py_2,0,0+8);
565

    
566
    PUTRGB1(out_2,py_2,1,2+8);
567
    PUTRGB1(out_1,py_1,1,2);
568

    
569
    PUTRGB1(out_1,py_1,2,4);
570
    PUTRGB1(out_2,py_2,2,4+8);
571

    
572
    PUTRGB1(out_2,py_2,3,6+8);
573
    PUTRGB1(out_1,py_1,3,6);
574

    
575
    dst_1[0]= out_1;
576
    dst_2[0]= out_2;
577
CLOSEYUV2RGBFUNC(1)
578

    
579
SwsFunc ff_yuv2rgb_get_func_ptr(SwsContext *c)
580
{
581
    SwsFunc t = NULL;
582
#if HAVE_MMX
583
     t = ff_yuv2rgb_init_mmx(c);
584
#endif
585
#if HAVE_VIS
586
    t = ff_yuv2rgb_init_vis(c);
587
#endif
588
#if CONFIG_MLIB
589
    t = ff_yuv2rgb_init_mlib(c);
590
#endif
591
#if HAVE_ALTIVEC
592
    if (c->flags & SWS_CPU_CAPS_ALTIVEC)
593
        t = ff_yuv2rgb_init_altivec(c);
594
#endif
595

    
596
#if ARCH_BFIN
597
    if (c->flags & SWS_CPU_CAPS_BFIN)
598
        t = ff_yuv2rgb_get_func_ptr_bfin(c);
599
#endif
600

    
601
    if (t)
602
        return t;
603

    
604
    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));
605

    
606
    switch (c->dstFormat) {
607
    case PIX_FMT_BGR48BE:
608
    case PIX_FMT_BGR48LE:    return yuv2rgb_c_bgr48;
609
    case PIX_FMT_RGB48BE:
610
    case PIX_FMT_RGB48LE:    return yuv2rgb_c_48;
611
    case PIX_FMT_ARGB:
612
    case PIX_FMT_ABGR:       if (CONFIG_SWSCALE_ALPHA && c->srcFormat == PIX_FMT_YUVA420P) return yuva2argb_c;
613
    case PIX_FMT_RGBA:
614
    case PIX_FMT_BGRA:       return (CONFIG_SWSCALE_ALPHA && c->srcFormat == PIX_FMT_YUVA420P) ? yuva2rgba_c : yuv2rgb_c_32;
615
    case PIX_FMT_RGB24:      return yuv2rgb_c_24_rgb;
616
    case PIX_FMT_BGR24:      return yuv2rgb_c_24_bgr;
617
    case PIX_FMT_RGB565:
618
    case PIX_FMT_BGR565:
619
    case PIX_FMT_RGB555:
620
    case PIX_FMT_BGR555:     return yuv2rgb_c_16;
621
    case PIX_FMT_RGB444:
622
    case PIX_FMT_BGR444:     return yuv2rgb_c_12_ordered_dither;
623
    case PIX_FMT_RGB8:
624
    case PIX_FMT_BGR8:       return yuv2rgb_c_8_ordered_dither;
625
    case PIX_FMT_RGB4:
626
    case PIX_FMT_BGR4:       return yuv2rgb_c_4_ordered_dither;
627
    case PIX_FMT_RGB4_BYTE:
628
    case PIX_FMT_BGR4_BYTE:  return yuv2rgb_c_4b_ordered_dither;
629
    case PIX_FMT_MONOBLACK:  return yuv2rgb_c_1_ordered_dither;
630
    default:
631
        assert(0);
632
    }
633
    return NULL;
634
}
635

    
636
static void fill_table(uint8_t* table[256], const int elemsize, const int inc, void *y_tab)
637
{
638
    int i;
639
    int64_t cb = 0;
640
    uint8_t *y_table = y_tab;
641

    
642
    y_table -= elemsize * (inc >> 9);
643

    
644
    for (i = 0; i < 256; i++) {
645
        table[i] = y_table + elemsize * (cb >> 16);
646
        cb += inc;
647
    }
648
}
649

    
650
static void fill_gv_table(int table[256], const int elemsize, const int inc)
651
{
652
    int i;
653
    int64_t cb = 0;
654
    int off = -(inc >> 9);
655

    
656
    for (i = 0; i < 256; i++) {
657
        table[i] = elemsize * (off + (cb >> 16));
658
        cb += inc;
659
    }
660
}
661

    
662
static uint16_t roundToInt16(int64_t f)
663
{
664
    int r= (f + (1<<15))>>16;
665
         if (r<-0x7FFF) return 0x8000;
666
    else if (r> 0x7FFF) return 0x7FFF;
667
    else                return r;
668
}
669

    
670
av_cold int ff_yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4], int fullRange,
671
                                     int brightness, int contrast, int saturation)
672
{
673
    const int isRgb =      c->dstFormat==PIX_FMT_RGB32
674
                        || c->dstFormat==PIX_FMT_RGB32_1
675
                        || c->dstFormat==PIX_FMT_BGR24
676
                        || c->dstFormat==PIX_FMT_RGB565BE
677
                        || c->dstFormat==PIX_FMT_RGB565LE
678
                        || c->dstFormat==PIX_FMT_RGB555BE
679
                        || c->dstFormat==PIX_FMT_RGB555LE
680
                        || c->dstFormat==PIX_FMT_RGB444BE
681
                        || c->dstFormat==PIX_FMT_RGB444LE
682
                        || c->dstFormat==PIX_FMT_RGB8
683
                        || c->dstFormat==PIX_FMT_RGB4
684
                        || c->dstFormat==PIX_FMT_RGB4_BYTE
685
                        || c->dstFormat==PIX_FMT_MONOBLACK;
686
    const int isNotNe =    c->dstFormat==PIX_FMT_NE(RGB565LE,RGB565BE)
687
                        || c->dstFormat==PIX_FMT_NE(RGB555LE,RGB555BE)
688
                        || c->dstFormat==PIX_FMT_NE(RGB444LE,RGB444BE)
689
                        || c->dstFormat==PIX_FMT_NE(BGR565LE,BGR565BE)
690
                        || c->dstFormat==PIX_FMT_NE(BGR555LE,BGR555BE)
691
                        || c->dstFormat==PIX_FMT_NE(BGR444LE,BGR444BE);
692
    const int bpp = c->dstFormatBpp;
693
    uint8_t *y_table;
694
    uint16_t *y_table16;
695
    uint32_t *y_table32;
696
    int i, base, rbase, gbase, bbase, abase, needAlpha;
697
    const int yoffs = fullRange ? 384 : 326;
698

    
699
    int64_t crv =  inv_table[0];
700
    int64_t cbu =  inv_table[1];
701
    int64_t cgu = -inv_table[2];
702
    int64_t cgv = -inv_table[3];
703
    int64_t cy  = 1<<16;
704
    int64_t oy  = 0;
705

    
706
    int64_t yb = 0;
707

    
708
    if (!fullRange) {
709
        cy = (cy*255) / 219;
710
        oy = 16<<16;
711
    } else {
712
        crv = (crv*224) / 255;
713
        cbu = (cbu*224) / 255;
714
        cgu = (cgu*224) / 255;
715
        cgv = (cgv*224) / 255;
716
    }
717

    
718
    cy  = (cy *contrast             ) >> 16;
719
    crv = (crv*contrast * saturation) >> 32;
720
    cbu = (cbu*contrast * saturation) >> 32;
721
    cgu = (cgu*contrast * saturation) >> 32;
722
    cgv = (cgv*contrast * saturation) >> 32;
723
    oy -= 256*brightness;
724

    
725
    c->uOffset=   0x0400040004000400LL;
726
    c->vOffset=   0x0400040004000400LL;
727
    c->yCoeff=    roundToInt16(cy *8192) * 0x0001000100010001ULL;
728
    c->vrCoeff=   roundToInt16(crv*8192) * 0x0001000100010001ULL;
729
    c->ubCoeff=   roundToInt16(cbu*8192) * 0x0001000100010001ULL;
730
    c->vgCoeff=   roundToInt16(cgv*8192) * 0x0001000100010001ULL;
731
    c->ugCoeff=   roundToInt16(cgu*8192) * 0x0001000100010001ULL;
732
    c->yOffset=   roundToInt16(oy *   8) * 0x0001000100010001ULL;
733

    
734
    c->yuv2rgb_y_coeff  = (int16_t)roundToInt16(cy <<13);
735
    c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy << 9);
736
    c->yuv2rgb_v2r_coeff= (int16_t)roundToInt16(crv<<13);
737
    c->yuv2rgb_v2g_coeff= (int16_t)roundToInt16(cgv<<13);
738
    c->yuv2rgb_u2g_coeff= (int16_t)roundToInt16(cgu<<13);
739
    c->yuv2rgb_u2b_coeff= (int16_t)roundToInt16(cbu<<13);
740

    
741
    //scale coefficients by cy
742
    crv = ((crv << 16) + 0x8000) / cy;
743
    cbu = ((cbu << 16) + 0x8000) / cy;
744
    cgu = ((cgu << 16) + 0x8000) / cy;
745
    cgv = ((cgv << 16) + 0x8000) / cy;
746

    
747
    av_free(c->yuvTable);
748

    
749
    switch (bpp) {
750
    case 1:
751
        c->yuvTable = av_malloc(1024);
752
        y_table = c->yuvTable;
753
        yb = -(384<<16) - oy;
754
        for (i = 0; i < 1024-110; i++) {
755
            y_table[i+110] = av_clip_uint8((yb + 0x8000) >> 16) >> 7;
756
            yb += cy;
757
        }
758
        fill_table(c->table_gU, 1, cgu, y_table + yoffs);
759
        fill_gv_table(c->table_gV, 1, cgv);
760
        break;
761
    case 4:
762
    case 4|128:
763
        rbase = isRgb ? 3 : 0;
764
        gbase = 1;
765
        bbase = isRgb ? 0 : 3;
766
        c->yuvTable = av_malloc(1024*3);
767
        y_table = c->yuvTable;
768
        yb = -(384<<16) - oy;
769
        for (i = 0; i < 1024-110; i++) {
770
            int yval = av_clip_uint8((yb + 0x8000) >> 16);
771
            y_table[i+110     ] =  (yval >> 7)       << rbase;
772
            y_table[i+ 37+1024] = ((yval + 43) / 85) << gbase;
773
            y_table[i+110+2048] =  (yval >> 7)       << bbase;
774
            yb += cy;
775
        }
776
        fill_table(c->table_rV, 1, crv, y_table + yoffs);
777
        fill_table(c->table_gU, 1, cgu, y_table + yoffs + 1024);
778
        fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2048);
779
        fill_gv_table(c->table_gV, 1, cgv);
780
        break;
781
    case 8:
782
        rbase = isRgb ? 5 : 0;
783
        gbase = isRgb ? 2 : 3;
784
        bbase = isRgb ? 0 : 6;
785
        c->yuvTable = av_malloc(1024*3);
786
        y_table = c->yuvTable;
787
        yb = -(384<<16) - oy;
788
        for (i = 0; i < 1024-38; i++) {
789
            int yval = av_clip_uint8((yb + 0x8000) >> 16);
790
            y_table[i+16     ] = ((yval + 18) / 36) << rbase;
791
            y_table[i+16+1024] = ((yval + 18) / 36) << gbase;
792
            y_table[i+37+2048] = ((yval + 43) / 85) << bbase;
793
            yb += cy;
794
        }
795
        fill_table(c->table_rV, 1, crv, y_table + yoffs);
796
        fill_table(c->table_gU, 1, cgu, y_table + yoffs + 1024);
797
        fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2048);
798
        fill_gv_table(c->table_gV, 1, cgv);
799
        break;
800
    case 12:
801
        rbase = isRgb ? 8 : 0;
802
        gbase = 4;
803
        bbase = isRgb ? 0 : 8;
804
        c->yuvTable = av_malloc(1024*3*2);
805
        y_table16 = c->yuvTable;
806
        yb = -(384<<16) - oy;
807
        for (i = 0; i < 1024; i++) {
808
            uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
809
            y_table16[i     ] = (yval >> 4) << rbase;
810
            y_table16[i+1024] = (yval >> 4) << gbase;
811
            y_table16[i+2048] = (yval >> 4) << bbase;
812
            yb += cy;
813
        }
814
        if (isNotNe)
815
            for (i = 0; i < 1024*3; i++)
816
                y_table16[i] = av_bswap16(y_table16[i]);
817
        fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
818
        fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + 1024);
819
        fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2048);
820
        fill_gv_table(c->table_gV, 2, cgv);
821
        break;
822
    case 15:
823
    case 16:
824
        rbase = isRgb ? bpp - 5 : 0;
825
        gbase = 5;
826
        bbase = isRgb ? 0 : (bpp - 5);
827
        c->yuvTable = av_malloc(1024*3*2);
828
        y_table16 = c->yuvTable;
829
        yb = -(384<<16) - oy;
830
        for (i = 0; i < 1024; i++) {
831
            uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
832
            y_table16[i     ] = (yval >> 3)          << rbase;
833
            y_table16[i+1024] = (yval >> (18 - bpp)) << gbase;
834
            y_table16[i+2048] = (yval >> 3)          << bbase;
835
            yb += cy;
836
        }
837
        if(isNotNe)
838
            for (i = 0; i < 1024*3; i++)
839
                y_table16[i] = av_bswap16(y_table16[i]);
840
        fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
841
        fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + 1024);
842
        fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2048);
843
        fill_gv_table(c->table_gV, 2, cgv);
844
        break;
845
    case 24:
846
    case 48:
847
        c->yuvTable = av_malloc(1024);
848
        y_table = c->yuvTable;
849
        yb = -(384<<16) - oy;
850
        for (i = 0; i < 1024; i++) {
851
            y_table[i] = av_clip_uint8((yb + 0x8000) >> 16);
852
            yb += cy;
853
        }
854
        fill_table(c->table_rV, 1, crv, y_table + yoffs);
855
        fill_table(c->table_gU, 1, cgu, y_table + yoffs);
856
        fill_table(c->table_bU, 1, cbu, y_table + yoffs);
857
        fill_gv_table(c->table_gV, 1, cgv);
858
        break;
859
    case 32:
860
        base = (c->dstFormat == PIX_FMT_RGB32_1 || c->dstFormat == PIX_FMT_BGR32_1) ? 8 : 0;
861
        rbase = base + (isRgb ? 16 : 0);
862
        gbase = base + 8;
863
        bbase = base + (isRgb ? 0 : 16);
864
        needAlpha = CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat);
865
        if (!needAlpha)
866
            abase = (base + 24) & 31;
867
        c->yuvTable = av_malloc(1024*3*4);
868
        y_table32 = c->yuvTable;
869
        yb = -(384<<16) - oy;
870
        for (i = 0; i < 1024; i++) {
871
            uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
872
            y_table32[i     ] = (yval << rbase) + (needAlpha ? 0 : (255 << abase));
873
            y_table32[i+1024] = yval << gbase;
874
            y_table32[i+2048] = yval << bbase;
875
            yb += cy;
876
        }
877
        fill_table(c->table_rV, 4, crv, y_table32 + yoffs);
878
        fill_table(c->table_gU, 4, cgu, y_table32 + yoffs + 1024);
879
        fill_table(c->table_bU, 4, cbu, y_table32 + yoffs + 2048);
880
        fill_gv_table(c->table_gV, 4, cgv);
881
        break;
882
    default:
883
        c->yuvTable = NULL;
884
        av_log(c, AV_LOG_ERROR, "%ibpp not supported by yuv2rgb\n", bpp);
885
        return -1;
886
    }
887
    return 0;
888
}