Statistics
| Branch: | Revision:

ffmpeg / libswscale / yuv2rgb.c @ db46be01

History | View | Annotate | Download (25.4 KB)

1 e76709d8 Kostya Shishkov
/*
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 2912e87a Mans Rullgard
 * This file is part of Libav.
10 e76709d8 Kostya Shishkov
 *
11 2912e87a Mans Rullgard
 * Libav is free software; you can redistribute it and/or
12 e76709d8 Kostya Shishkov
 * 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 2912e87a Mans Rullgard
 * Libav is distributed in the hope that it will be useful,
17 e76709d8 Kostya Shishkov
 * 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 2912e87a Mans Rullgard
 * License along with Libav; if not, write to the Free Software
23 e76709d8 Kostya Shishkov
 * 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 d0ce212a Ramiro Polla
#include "libavutil/x86_cpu.h"
36 1379b584 Alexis Ballier
#include "libavutil/bswap.h"
37 e76709d8 Kostya Shishkov
38 601949b8 Janusz Krzysztofik
extern const uint8_t dither_4x4_16[4][8];
39 e76709d8 Kostya Shishkov
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 8ebed38e Stefano Sabatini
const int *sws_getCoefficients(int colorspace)
55 3f3dc76c David Conrad
{
56
    if (colorspace > 7 || colorspace < 0)
57
        colorspace = SWS_CS_DEFAULT;
58
    return ff_yuv2rgb_coeffs[colorspace];
59
}
60
61 e76709d8 Kostya Shishkov
#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 df226e63 Kostya Shishkov
#define PUTRGB(dst,src,i)            \
69
    Y = src[2*i];                    \
70 e76709d8 Kostya Shishkov
    dst[2*i  ] = r[Y] + g[Y] + b[Y]; \
71 df226e63 Kostya Shishkov
    Y = src[2*i+1];                  \
72 e76709d8 Kostya Shishkov
    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 df226e63 Kostya Shishkov
#define PUTRGBA(dst,ysrc,asrc,i,s)                      \
87
    Y = ysrc[2*i];                                      \
88 3acd545f Cédric Schieli
    dst[2*i  ] = r[Y] + g[Y] + b[Y] + (asrc[2*i  ]<<s); \
89 df226e63 Kostya Shishkov
    Y = ysrc[2*i+1];                                    \
90 3acd545f Cédric Schieli
    dst[2*i+1] = r[Y] + g[Y] + b[Y] + (asrc[2*i+1]<<s);
91
92 68e7f482 Kostya Shishkov
#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 9a10a076 Cédric Schieli
#define YUV2RGBFUNC(func_name, dst_type, alpha) \
103 a959e247 Zuxy Meng
static int func_name(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY, \
104 dd68318c Ramiro Polla
                     int srcSliceH, uint8_t* dst[], int dstStride[]) \
105
{\
106 e76709d8 Kostya Shishkov
    int y;\
107
\
108 3acd545f Cédric Schieli
    if (!alpha && c->srcFormat == PIX_FMT_YUV422P) {\
109 e76709d8 Kostya Shishkov
        srcStride[1] *= 2;\
110
        srcStride[2] *= 2;\
111
    }\
112
    for (y=0; y<srcSliceH; y+=2) {\
113
        dst_type *dst_1 = (dst_type*)(dst[0] + (y+srcSliceY  )*dstStride[0]);\
114
        dst_type *dst_2 = (dst_type*)(dst[0] + (y+srcSliceY+1)*dstStride[0]);\
115
        dst_type av_unused *r, *b;\
116
        dst_type *g;\
117 a959e247 Zuxy Meng
        const uint8_t *py_1 = src[0] + y*srcStride[0];\
118
        const uint8_t *py_2 = py_1 + srcStride[0];\
119
        const uint8_t *pu = src[1] + (y>>1)*srcStride[1];\
120
        const uint8_t *pv = src[2] + (y>>1)*srcStride[2];\
121
        const uint8_t av_unused *pa_1, *pa_2;\
122 e76709d8 Kostya Shishkov
        unsigned int h_size = c->dstW>>3;\
123 dd68318c Ramiro Polla
        if (alpha) {\
124 3acd545f Cédric Schieli
            pa_1 = src[3] + y*srcStride[3];\
125
            pa_2 = pa_1 + srcStride[3];\
126
        }\
127 e76709d8 Kostya Shishkov
        while (h_size--) {\
128
            int av_unused U, V;\
129
            int Y;\
130
131
#define ENDYUV2RGBLINE(dst_delta)\
132
            pu += 4;\
133
            pv += 4;\
134
            py_1 += 8;\
135
            py_2 += 8;\
136
            dst_1 += dst_delta;\
137
            dst_2 += dst_delta;\
138
        }\
139
        if (c->dstW & 4) {\
140
            int av_unused Y, U, V;\
141
142
#define ENDYUV2RGBFUNC()\
143
        }\
144
    }\
145
    return srcSliceH;\
146
}
147
148
#define CLOSEYUV2RGBFUNC(dst_delta)\
149
    ENDYUV2RGBLINE(dst_delta)\
150
    ENDYUV2RGBFUNC()
151
152 68e7f482 Kostya Shishkov
YUV2RGBFUNC(yuv2rgb_c_48, uint8_t, 0)
153
    LOADCHROMA(0);
154
    PUTRGB48(dst_1,py_1,0);
155
    PUTRGB48(dst_2,py_2,0);
156
157
    LOADCHROMA(1);
158
    PUTRGB48(dst_2,py_2,1);
159
    PUTRGB48(dst_1,py_1,1);
160
161
    LOADCHROMA(2);
162
    PUTRGB48(dst_1,py_1,2);
163
    PUTRGB48(dst_2,py_2,2);
164
165
    LOADCHROMA(3);
166
    PUTRGB48(dst_2,py_2,3);
167
    PUTRGB48(dst_1,py_1,3);
168
ENDYUV2RGBLINE(48)
169
    LOADCHROMA(0);
170
    PUTRGB48(dst_1,py_1,0);
171
    PUTRGB48(dst_2,py_2,0);
172
173
    LOADCHROMA(1);
174
    PUTRGB48(dst_2,py_2,1);
175
    PUTRGB48(dst_1,py_1,1);
176
ENDYUV2RGBFUNC()
177
178 9a10a076 Cédric Schieli
YUV2RGBFUNC(yuv2rgb_c_32, uint32_t, 0)
179 e76709d8 Kostya Shishkov
    LOADCHROMA(0);
180 df226e63 Kostya Shishkov
    PUTRGB(dst_1,py_1,0);
181
    PUTRGB(dst_2,py_2,0);
182 e76709d8 Kostya Shishkov
183
    LOADCHROMA(1);
184 df226e63 Kostya Shishkov
    PUTRGB(dst_2,py_2,1);
185
    PUTRGB(dst_1,py_1,1);
186 e76709d8 Kostya Shishkov
187
    LOADCHROMA(2);
188 df226e63 Kostya Shishkov
    PUTRGB(dst_1,py_1,2);
189
    PUTRGB(dst_2,py_2,2);
190 e76709d8 Kostya Shishkov
191
    LOADCHROMA(3);
192 df226e63 Kostya Shishkov
    PUTRGB(dst_2,py_2,3);
193
    PUTRGB(dst_1,py_1,3);
194 e76709d8 Kostya Shishkov
ENDYUV2RGBLINE(8)
195
    LOADCHROMA(0);
196 df226e63 Kostya Shishkov
    PUTRGB(dst_1,py_1,0);
197
    PUTRGB(dst_2,py_2,0);
198 e76709d8 Kostya Shishkov
199
    LOADCHROMA(1);
200 df226e63 Kostya Shishkov
    PUTRGB(dst_2,py_2,1);
201
    PUTRGB(dst_1,py_1,1);
202 e76709d8 Kostya Shishkov
ENDYUV2RGBFUNC()
203
204 3acd545f Cédric Schieli
YUV2RGBFUNC(yuva2rgba_c, uint32_t, 1)
205
    LOADCHROMA(0);
206 df226e63 Kostya Shishkov
    PUTRGBA(dst_1,py_1,pa_1,0,24);
207
    PUTRGBA(dst_2,py_2,pa_2,0,24);
208 3acd545f Cédric Schieli
209
    LOADCHROMA(1);
210 df226e63 Kostya Shishkov
    PUTRGBA(dst_2,py_2,pa_1,1,24);
211
    PUTRGBA(dst_1,py_1,pa_2,1,24);
212 3acd545f Cédric Schieli
213
    LOADCHROMA(2);
214 df226e63 Kostya Shishkov
    PUTRGBA(dst_1,py_1,pa_1,2,24);
215
    PUTRGBA(dst_2,py_2,pa_2,2,24);
216 3acd545f Cédric Schieli
217
    LOADCHROMA(3);
218 df226e63 Kostya Shishkov
    PUTRGBA(dst_2,py_2,pa_1,3,24);
219
    PUTRGBA(dst_1,py_1,pa_2,3,24);
220 3acd545f Cédric Schieli
    pa_1 += 8;\
221
    pa_2 += 8;\
222
ENDYUV2RGBLINE(8)
223
    LOADCHROMA(0);
224 df226e63 Kostya Shishkov
    PUTRGBA(dst_1,py_1,pa_1,0,24);
225
    PUTRGBA(dst_2,py_2,pa_2,0,24);
226 3acd545f Cédric Schieli
227
    LOADCHROMA(1);
228 df226e63 Kostya Shishkov
    PUTRGBA(dst_2,py_2,pa_1,1,24);
229
    PUTRGBA(dst_1,py_1,pa_2,1,24);
230 3acd545f Cédric Schieli
ENDYUV2RGBFUNC()
231
232
YUV2RGBFUNC(yuva2argb_c, uint32_t, 1)
233
    LOADCHROMA(0);
234 df226e63 Kostya Shishkov
    PUTRGBA(dst_1,py_1,pa_1,0,0);
235
    PUTRGBA(dst_2,py_2,pa_2,0,0);
236 3acd545f Cédric Schieli
237
    LOADCHROMA(1);
238 df226e63 Kostya Shishkov
    PUTRGBA(dst_2,py_2,pa_2,1,0);
239
    PUTRGBA(dst_1,py_1,pa_1,1,0);
240 3acd545f Cédric Schieli
241
    LOADCHROMA(2);
242 df226e63 Kostya Shishkov
    PUTRGBA(dst_1,py_1,pa_1,2,0);
243
    PUTRGBA(dst_2,py_2,pa_2,2,0);
244 3acd545f Cédric Schieli
245
    LOADCHROMA(3);
246 df226e63 Kostya Shishkov
    PUTRGBA(dst_2,py_2,pa_2,3,0);
247
    PUTRGBA(dst_1,py_1,pa_1,3,0);
248 3acd545f Cédric Schieli
    pa_1 += 8;\
249
    pa_2 += 8;\
250
ENDYUV2RGBLINE(8)
251
    LOADCHROMA(0);
252 df226e63 Kostya Shishkov
    PUTRGBA(dst_1,py_1,pa_1,0,0);
253
    PUTRGBA(dst_2,py_2,pa_2,0,0);
254 3acd545f Cédric Schieli
255
    LOADCHROMA(1);
256 df226e63 Kostya Shishkov
    PUTRGBA(dst_2,py_2,pa_2,1,0);
257
    PUTRGBA(dst_1,py_1,pa_1,1,0);
258 3acd545f Cédric Schieli
ENDYUV2RGBFUNC()
259
260 9a10a076 Cédric Schieli
YUV2RGBFUNC(yuv2rgb_c_24_rgb, uint8_t, 0)
261 e76709d8 Kostya Shishkov
    LOADCHROMA(0);
262
    PUTRGB24(dst_1,py_1,0);
263
    PUTRGB24(dst_2,py_2,0);
264
265
    LOADCHROMA(1);
266
    PUTRGB24(dst_2,py_2,1);
267
    PUTRGB24(dst_1,py_1,1);
268
269
    LOADCHROMA(2);
270
    PUTRGB24(dst_1,py_1,2);
271
    PUTRGB24(dst_2,py_2,2);
272
273
    LOADCHROMA(3);
274
    PUTRGB24(dst_2,py_2,3);
275
    PUTRGB24(dst_1,py_1,3);
276
ENDYUV2RGBLINE(24)
277
    LOADCHROMA(0);
278
    PUTRGB24(dst_1,py_1,0);
279
    PUTRGB24(dst_2,py_2,0);
280
281
    LOADCHROMA(1);
282
    PUTRGB24(dst_2,py_2,1);
283
    PUTRGB24(dst_1,py_1,1);
284
ENDYUV2RGBFUNC()
285
286
// only trivial mods from yuv2rgb_c_24_rgb
287 9a10a076 Cédric Schieli
YUV2RGBFUNC(yuv2rgb_c_24_bgr, uint8_t, 0)
288 e76709d8 Kostya Shishkov
    LOADCHROMA(0);
289
    PUTBGR24(dst_1,py_1,0);
290
    PUTBGR24(dst_2,py_2,0);
291
292
    LOADCHROMA(1);
293
    PUTBGR24(dst_2,py_2,1);
294
    PUTBGR24(dst_1,py_1,1);
295
296
    LOADCHROMA(2);
297
    PUTBGR24(dst_1,py_1,2);
298
    PUTBGR24(dst_2,py_2,2);
299
300
    LOADCHROMA(3);
301
    PUTBGR24(dst_2,py_2,3);
302
    PUTBGR24(dst_1,py_1,3);
303
ENDYUV2RGBLINE(24)
304
    LOADCHROMA(0);
305
    PUTBGR24(dst_1,py_1,0);
306
    PUTBGR24(dst_2,py_2,0);
307
308
    LOADCHROMA(1);
309
    PUTBGR24(dst_2,py_2,1);
310
    PUTBGR24(dst_1,py_1,1);
311
ENDYUV2RGBFUNC()
312
313
// This is exactly the same code as yuv2rgb_c_32 except for the types of
314
// r, g, b, dst_1, dst_2
315 9a10a076 Cédric Schieli
YUV2RGBFUNC(yuv2rgb_c_16, uint16_t, 0)
316 e76709d8 Kostya Shishkov
    LOADCHROMA(0);
317 df226e63 Kostya Shishkov
    PUTRGB(dst_1,py_1,0);
318
    PUTRGB(dst_2,py_2,0);
319 e76709d8 Kostya Shishkov
320
    LOADCHROMA(1);
321 df226e63 Kostya Shishkov
    PUTRGB(dst_2,py_2,1);
322
    PUTRGB(dst_1,py_1,1);
323 e76709d8 Kostya Shishkov
324
    LOADCHROMA(2);
325 df226e63 Kostya Shishkov
    PUTRGB(dst_1,py_1,2);
326
    PUTRGB(dst_2,py_2,2);
327 e76709d8 Kostya Shishkov
328
    LOADCHROMA(3);
329 df226e63 Kostya Shishkov
    PUTRGB(dst_2,py_2,3);
330
    PUTRGB(dst_1,py_1,3);
331 e76709d8 Kostya Shishkov
CLOSEYUV2RGBFUNC(8)
332
333 3c52d484 Zuxy Meng
#if 0 // Currently unused
334 e76709d8 Kostya Shishkov
// This is exactly the same code as yuv2rgb_c_32 except for the types of
335
// r, g, b, dst_1, dst_2
336 9a10a076 Cédric Schieli
YUV2RGBFUNC(yuv2rgb_c_8, uint8_t, 0)
337 e76709d8 Kostya Shishkov
    LOADCHROMA(0);
338 df226e63 Kostya Shishkov
    PUTRGB(dst_1,py_1,0);
339
    PUTRGB(dst_2,py_2,0);
340 e76709d8 Kostya Shishkov

341
    LOADCHROMA(1);
342 df226e63 Kostya Shishkov
    PUTRGB(dst_2,py_2,1);
343
    PUTRGB(dst_1,py_1,1);
344 e76709d8 Kostya Shishkov

345
    LOADCHROMA(2);
346 df226e63 Kostya Shishkov
    PUTRGB(dst_1,py_1,2);
347
    PUTRGB(dst_2,py_2,2);
348 e76709d8 Kostya Shishkov

349
    LOADCHROMA(3);
350 df226e63 Kostya Shishkov
    PUTRGB(dst_2,py_2,3);
351
    PUTRGB(dst_1,py_1,3);
352 e76709d8 Kostya Shishkov
CLOSEYUV2RGBFUNC(8)
353 3c52d484 Zuxy Meng
#endif
354 e76709d8 Kostya Shishkov
355
// r, g, b, dst_1, dst_2
356 601949b8 Janusz Krzysztofik
YUV2RGBFUNC(yuv2rgb_c_12_ordered_dither, uint16_t, 0)
357
    const uint8_t *d16 = dither_4x4_16[y&3];
358
#define PUTRGB12(dst,src,i,o)                                   \
359
    Y = src[2*i];                                               \
360
    dst[2*i]   = r[Y+d16[0+o]] + g[Y+d16[0+o]] + b[Y+d16[0+o]]; \
361
    Y = src[2*i+1];                                             \
362
    dst[2*i+1] = r[Y+d16[1+o]] + g[Y+d16[1+o]] + b[Y+d16[1+o]];
363
364
    LOADCHROMA(0);
365
    PUTRGB12(dst_1,py_1,0,0);
366
    PUTRGB12(dst_2,py_2,0,0+8);
367
368
    LOADCHROMA(1);
369
    PUTRGB12(dst_2,py_2,1,2+8);
370
    PUTRGB12(dst_1,py_1,1,2);
371
372
    LOADCHROMA(2);
373
    PUTRGB12(dst_1,py_1,2,4);
374
    PUTRGB12(dst_2,py_2,2,4+8);
375
376
    LOADCHROMA(3);
377
    PUTRGB12(dst_2,py_2,3,6+8);
378
    PUTRGB12(dst_1,py_1,3,6);
379
CLOSEYUV2RGBFUNC(8)
380
381
// r, g, b, dst_1, dst_2
382 9a10a076 Cédric Schieli
YUV2RGBFUNC(yuv2rgb_c_8_ordered_dither, uint8_t, 0)
383 e76709d8 Kostya Shishkov
    const uint8_t *d32 = dither_8x8_32[y&7];
384
    const uint8_t *d64 = dither_8x8_73[y&7];
385
#define PUTRGB8(dst,src,i,o)                                    \
386
    Y = src[2*i];                                               \
387
    dst[2*i]   = r[Y+d32[0+o]] + g[Y+d32[0+o]] + b[Y+d64[0+o]]; \
388
    Y = src[2*i+1];                                             \
389
    dst[2*i+1] = r[Y+d32[1+o]] + g[Y+d32[1+o]] + b[Y+d64[1+o]];
390
391
    LOADCHROMA(0);
392
    PUTRGB8(dst_1,py_1,0,0);
393
    PUTRGB8(dst_2,py_2,0,0+8);
394
395
    LOADCHROMA(1);
396
    PUTRGB8(dst_2,py_2,1,2+8);
397
    PUTRGB8(dst_1,py_1,1,2);
398
399
    LOADCHROMA(2);
400
    PUTRGB8(dst_1,py_1,2,4);
401
    PUTRGB8(dst_2,py_2,2,4+8);
402
403
    LOADCHROMA(3);
404
    PUTRGB8(dst_2,py_2,3,6+8);
405
    PUTRGB8(dst_1,py_1,3,6);
406
CLOSEYUV2RGBFUNC(8)
407
408 3c52d484 Zuxy Meng
#if 0 // Currently unused
409 e76709d8 Kostya Shishkov
// This is exactly the same code as yuv2rgb_c_32 except for the types of
410
// r, g, b, dst_1, dst_2
411 9a10a076 Cédric Schieli
YUV2RGBFUNC(yuv2rgb_c_4, uint8_t, 0)
412 e76709d8 Kostya Shishkov
    int acc;
413
#define PUTRGB4(dst,src,i)          \
414
    Y = src[2*i];                   \
415
    acc = r[Y] + g[Y] + b[Y];       \
416
    Y = src[2*i+1];                 \
417
    acc |= (r[Y] + g[Y] + b[Y])<<4; \
418
    dst[i] = acc;
419

420
    LOADCHROMA(0);
421
    PUTRGB4(dst_1,py_1,0);
422
    PUTRGB4(dst_2,py_2,0);
423

424
    LOADCHROMA(1);
425
    PUTRGB4(dst_2,py_2,1);
426
    PUTRGB4(dst_1,py_1,1);
427

428
    LOADCHROMA(2);
429
    PUTRGB4(dst_1,py_1,2);
430
    PUTRGB4(dst_2,py_2,2);
431

432
    LOADCHROMA(3);
433
    PUTRGB4(dst_2,py_2,3);
434
    PUTRGB4(dst_1,py_1,3);
435
CLOSEYUV2RGBFUNC(4)
436 3c52d484 Zuxy Meng
#endif
437 e76709d8 Kostya Shishkov
438 9a10a076 Cédric Schieli
YUV2RGBFUNC(yuv2rgb_c_4_ordered_dither, uint8_t, 0)
439 e76709d8 Kostya Shishkov
    const uint8_t *d64 =  dither_8x8_73[y&7];
440
    const uint8_t *d128 = dither_8x8_220[y&7];
441
    int acc;
442
443
#define PUTRGB4D(dst,src,i,o)                                     \
444
    Y = src[2*i];                                                 \
445
    acc = r[Y+d128[0+o]] + g[Y+d64[0+o]] + b[Y+d128[0+o]];        \
446
    Y = src[2*i+1];                                               \
447
    acc |= (r[Y+d128[1+o]] + g[Y+d64[1+o]] + b[Y+d128[1+o]])<<4;  \
448
    dst[i]= acc;
449
450
    LOADCHROMA(0);
451
    PUTRGB4D(dst_1,py_1,0,0);
452
    PUTRGB4D(dst_2,py_2,0,0+8);
453
454
    LOADCHROMA(1);
455
    PUTRGB4D(dst_2,py_2,1,2+8);
456
    PUTRGB4D(dst_1,py_1,1,2);
457
458
    LOADCHROMA(2);
459
    PUTRGB4D(dst_1,py_1,2,4);
460
    PUTRGB4D(dst_2,py_2,2,4+8);
461
462
    LOADCHROMA(3);
463
    PUTRGB4D(dst_2,py_2,3,6+8);
464
    PUTRGB4D(dst_1,py_1,3,6);
465
CLOSEYUV2RGBFUNC(4)
466
467 3c52d484 Zuxy Meng
#if 0 // Currently unused
468 e76709d8 Kostya Shishkov
// This is exactly the same code as yuv2rgb_c_32 except for the types of
469
// r, g, b, dst_1, dst_2
470 9a10a076 Cédric Schieli
YUV2RGBFUNC(yuv2rgb_c_4b, uint8_t, 0)
471 e76709d8 Kostya Shishkov
    LOADCHROMA(0);
472 df226e63 Kostya Shishkov
    PUTRGB(dst_1,py_1,0);
473
    PUTRGB(dst_2,py_2,0);
474 e76709d8 Kostya Shishkov

475
    LOADCHROMA(1);
476 df226e63 Kostya Shishkov
    PUTRGB(dst_2,py_2,1);
477
    PUTRGB(dst_1,py_1,1);
478 e76709d8 Kostya Shishkov

479
    LOADCHROMA(2);
480 df226e63 Kostya Shishkov
    PUTRGB(dst_1,py_1,2);
481
    PUTRGB(dst_2,py_2,2);
482 e76709d8 Kostya Shishkov

483
    LOADCHROMA(3);
484 df226e63 Kostya Shishkov
    PUTRGB(dst_2,py_2,3);
485
    PUTRGB(dst_1,py_1,3);
486 e76709d8 Kostya Shishkov
CLOSEYUV2RGBFUNC(8)
487 3c52d484 Zuxy Meng
#endif
488 e76709d8 Kostya Shishkov
489 9a10a076 Cédric Schieli
YUV2RGBFUNC(yuv2rgb_c_4b_ordered_dither, uint8_t, 0)
490 e76709d8 Kostya Shishkov
    const uint8_t *d64 =  dither_8x8_73[y&7];
491
    const uint8_t *d128 = dither_8x8_220[y&7];
492
493
#define PUTRGB4DB(dst,src,i,o)                                    \
494
    Y = src[2*i];                                                 \
495
    dst[2*i]   = r[Y+d128[0+o]] + g[Y+d64[0+o]] + b[Y+d128[0+o]]; \
496
    Y = src[2*i+1];                                               \
497
    dst[2*i+1] = r[Y+d128[1+o]] + g[Y+d64[1+o]] + b[Y+d128[1+o]];
498
499
    LOADCHROMA(0);
500
    PUTRGB4DB(dst_1,py_1,0,0);
501
    PUTRGB4DB(dst_2,py_2,0,0+8);
502
503
    LOADCHROMA(1);
504
    PUTRGB4DB(dst_2,py_2,1,2+8);
505
    PUTRGB4DB(dst_1,py_1,1,2);
506
507
    LOADCHROMA(2);
508
    PUTRGB4DB(dst_1,py_1,2,4);
509
    PUTRGB4DB(dst_2,py_2,2,4+8);
510
511
    LOADCHROMA(3);
512
    PUTRGB4DB(dst_2,py_2,3,6+8);
513
    PUTRGB4DB(dst_1,py_1,3,6);
514
CLOSEYUV2RGBFUNC(8)
515
516 9a10a076 Cédric Schieli
YUV2RGBFUNC(yuv2rgb_c_1_ordered_dither, uint8_t, 0)
517 e76709d8 Kostya Shishkov
        const uint8_t *d128 = dither_8x8_220[y&7];
518
        char out_1 = 0, out_2 = 0;
519
        g= c->table_gU[128] + c->table_gV[128];
520
521
#define PUTRGB1(out,src,i,o)    \
522
    Y = src[2*i];               \
523
    out+= out + g[Y+d128[0+o]]; \
524
    Y = src[2*i+1];             \
525
    out+= out + g[Y+d128[1+o]];
526
527
    PUTRGB1(out_1,py_1,0,0);
528
    PUTRGB1(out_2,py_2,0,0+8);
529
530
    PUTRGB1(out_2,py_2,1,2+8);
531
    PUTRGB1(out_1,py_1,1,2);
532
533
    PUTRGB1(out_1,py_1,2,4);
534
    PUTRGB1(out_2,py_2,2,4+8);
535
536
    PUTRGB1(out_2,py_2,3,6+8);
537
    PUTRGB1(out_1,py_1,3,6);
538
539
    dst_1[0]= out_1;
540
    dst_2[0]= out_2;
541
CLOSEYUV2RGBFUNC(1)
542
543 780daf2b Diego Biurrun
SwsFunc ff_yuv2rgb_get_func_ptr(SwsContext *c)
544 e76709d8 Kostya Shishkov
{
545
    SwsFunc t = NULL;
546 f4ea7c89 Diego Biurrun
#if HAVE_MMX
547 befa8e66 Ramiro Polla
     t = ff_yuv2rgb_init_mmx(c);
548 e76709d8 Kostya Shishkov
#endif
549
#if HAVE_VIS
550 780daf2b Diego Biurrun
    t = ff_yuv2rgb_init_vis(c);
551 e76709d8 Kostya Shishkov
#endif
552
#if CONFIG_MLIB
553 780daf2b Diego Biurrun
    t = ff_yuv2rgb_init_mlib(c);
554 e76709d8 Kostya Shishkov
#endif
555 29ce0433 Diego Biurrun
#if HAVE_ALTIVEC
556 e76709d8 Kostya Shishkov
    if (c->flags & SWS_CPU_CAPS_ALTIVEC)
557 780daf2b Diego Biurrun
        t = ff_yuv2rgb_init_altivec(c);
558 e76709d8 Kostya Shishkov
#endif
559
560
#if ARCH_BFIN
561
    if (c->flags & SWS_CPU_CAPS_BFIN)
562 780daf2b Diego Biurrun
        t = ff_yuv2rgb_get_func_ptr_bfin(c);
563 e76709d8 Kostya Shishkov
#endif
564
565
    if (t)
566
        return t;
567
568 d5206d3d Zuxy Meng
    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));
569 e76709d8 Kostya Shishkov
570
    switch (c->dstFormat) {
571 68e7f482 Kostya Shishkov
    case PIX_FMT_RGB48BE:
572
    case PIX_FMT_RGB48LE:    return yuv2rgb_c_48;
573 3acd545f Cédric Schieli
    case PIX_FMT_ARGB:
574
    case PIX_FMT_ABGR:       if (CONFIG_SWSCALE_ALPHA && c->srcFormat == PIX_FMT_YUVA420P) return yuva2argb_c;
575
    case PIX_FMT_RGBA:
576
    case PIX_FMT_BGRA:       return (CONFIG_SWSCALE_ALPHA && c->srcFormat == PIX_FMT_YUVA420P) ? yuva2rgba_c : yuv2rgb_c_32;
577 e76709d8 Kostya Shishkov
    case PIX_FMT_RGB24:      return yuv2rgb_c_24_rgb;
578
    case PIX_FMT_BGR24:      return yuv2rgb_c_24_bgr;
579
    case PIX_FMT_RGB565:
580
    case PIX_FMT_BGR565:
581
    case PIX_FMT_RGB555:
582
    case PIX_FMT_BGR555:     return yuv2rgb_c_16;
583 601949b8 Janusz Krzysztofik
    case PIX_FMT_RGB444:
584
    case PIX_FMT_BGR444:     return yuv2rgb_c_12_ordered_dither;
585 e76709d8 Kostya Shishkov
    case PIX_FMT_RGB8:
586
    case PIX_FMT_BGR8:       return yuv2rgb_c_8_ordered_dither;
587
    case PIX_FMT_RGB4:
588
    case PIX_FMT_BGR4:       return yuv2rgb_c_4_ordered_dither;
589
    case PIX_FMT_RGB4_BYTE:
590
    case PIX_FMT_BGR4_BYTE:  return yuv2rgb_c_4b_ordered_dither;
591
    case PIX_FMT_MONOBLACK:  return yuv2rgb_c_1_ordered_dither;
592
    default:
593
        assert(0);
594
    }
595
    return NULL;
596
}
597
598 453d28af Benoit Fouet
static void fill_table(uint8_t* table[256], const int elemsize, const int inc, void *y_tab)
599 e76709d8 Kostya Shishkov
{
600
    int i;
601
    int64_t cb = 0;
602 453d28af Benoit Fouet
    uint8_t *y_table = y_tab;
603 e76709d8 Kostya Shishkov
604
    y_table -= elemsize * (inc >> 9);
605
606
    for (i = 0; i < 256; i++) {
607
        table[i] = y_table + elemsize * (cb >> 16);
608
        cb += inc;
609
    }
610
}
611
612
static void fill_gv_table(int table[256], const int elemsize, const int inc)
613
{
614
    int i;
615
    int64_t cb = 0;
616
    int off = -(inc >> 9);
617
618
    for (i = 0; i < 256; i++) {
619
        table[i] = elemsize * (off + (cb >> 16));
620
        cb += inc;
621
    }
622
}
623
624 0093ac5b Stefano Sabatini
static uint16_t roundToInt16(int64_t f)
625
{
626
    int r= (f + (1<<15))>>16;
627
         if (r<-0x7FFF) return 0x8000;
628
    else if (r> 0x7FFF) return 0x7FFF;
629
    else                return r;
630
}
631
632 780daf2b Diego Biurrun
av_cold int ff_yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4], int fullRange,
633
                                     int brightness, int contrast, int saturation)
634 e76709d8 Kostya Shishkov
{
635
    const int isRgb =      c->dstFormat==PIX_FMT_RGB32
636
                        || c->dstFormat==PIX_FMT_RGB32_1
637
                        || c->dstFormat==PIX_FMT_BGR24
638 1379b584 Alexis Ballier
                        || c->dstFormat==PIX_FMT_RGB565BE
639
                        || c->dstFormat==PIX_FMT_RGB565LE
640
                        || c->dstFormat==PIX_FMT_RGB555BE
641
                        || c->dstFormat==PIX_FMT_RGB555LE
642 a4e44130 Janusz Krzysztofik
                        || c->dstFormat==PIX_FMT_RGB444BE
643
                        || c->dstFormat==PIX_FMT_RGB444LE
644 e76709d8 Kostya Shishkov
                        || c->dstFormat==PIX_FMT_RGB8
645
                        || c->dstFormat==PIX_FMT_RGB4
646
                        || c->dstFormat==PIX_FMT_RGB4_BYTE
647
                        || c->dstFormat==PIX_FMT_MONOBLACK;
648 1379b584 Alexis Ballier
    const int isNotNe =    c->dstFormat==PIX_FMT_NE(RGB565LE,RGB565BE)
649
                        || c->dstFormat==PIX_FMT_NE(RGB555LE,RGB555BE)
650 a4e44130 Janusz Krzysztofik
                        || c->dstFormat==PIX_FMT_NE(RGB444LE,RGB444BE)
651 1379b584 Alexis Ballier
                        || c->dstFormat==PIX_FMT_NE(BGR565LE,BGR565BE)
652 a4e44130 Janusz Krzysztofik
                        || c->dstFormat==PIX_FMT_NE(BGR555LE,BGR555BE)
653
                        || c->dstFormat==PIX_FMT_NE(BGR444LE,BGR444BE);
654 30b61475 Stefano Sabatini
    const int bpp = c->dstFormatBpp;
655 e76709d8 Kostya Shishkov
    uint8_t *y_table;
656
    uint16_t *y_table16;
657
    uint32_t *y_table32;
658 3acd545f Cédric Schieli
    int i, base, rbase, gbase, bbase, abase, needAlpha;
659 e76709d8 Kostya Shishkov
    const int yoffs = fullRange ? 384 : 326;
660
661
    int64_t crv =  inv_table[0];
662
    int64_t cbu =  inv_table[1];
663
    int64_t cgu = -inv_table[2];
664
    int64_t cgv = -inv_table[3];
665
    int64_t cy  = 1<<16;
666
    int64_t oy  = 0;
667
668
    int64_t yb = 0;
669
670
    if (!fullRange) {
671
        cy = (cy*255) / 219;
672
        oy = 16<<16;
673
    } else {
674
        crv = (crv*224) / 255;
675
        cbu = (cbu*224) / 255;
676
        cgu = (cgu*224) / 255;
677
        cgv = (cgv*224) / 255;
678
    }
679
680
    cy  = (cy *contrast             ) >> 16;
681
    crv = (crv*contrast * saturation) >> 32;
682
    cbu = (cbu*contrast * saturation) >> 32;
683
    cgu = (cgu*contrast * saturation) >> 32;
684
    cgv = (cgv*contrast * saturation) >> 32;
685
    oy -= 256*brightness;
686
687 0093ac5b Stefano Sabatini
    c->uOffset=   0x0400040004000400LL;
688
    c->vOffset=   0x0400040004000400LL;
689
    c->yCoeff=    roundToInt16(cy *8192) * 0x0001000100010001ULL;
690
    c->vrCoeff=   roundToInt16(crv*8192) * 0x0001000100010001ULL;
691
    c->ubCoeff=   roundToInt16(cbu*8192) * 0x0001000100010001ULL;
692
    c->vgCoeff=   roundToInt16(cgv*8192) * 0x0001000100010001ULL;
693
    c->ugCoeff=   roundToInt16(cgu*8192) * 0x0001000100010001ULL;
694
    c->yOffset=   roundToInt16(oy *   8) * 0x0001000100010001ULL;
695
696
    c->yuv2rgb_y_coeff  = (int16_t)roundToInt16(cy <<13);
697
    c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy << 9);
698
    c->yuv2rgb_v2r_coeff= (int16_t)roundToInt16(crv<<13);
699
    c->yuv2rgb_v2g_coeff= (int16_t)roundToInt16(cgv<<13);
700
    c->yuv2rgb_u2g_coeff= (int16_t)roundToInt16(cgu<<13);
701
    c->yuv2rgb_u2b_coeff= (int16_t)roundToInt16(cbu<<13);
702
703 e76709d8 Kostya Shishkov
    //scale coefficients by cy
704
    crv = ((crv << 16) + 0x8000) / cy;
705
    cbu = ((cbu << 16) + 0x8000) / cy;
706
    cgu = ((cgu << 16) + 0x8000) / cy;
707
    cgv = ((cgv << 16) + 0x8000) / cy;
708
709
    av_free(c->yuvTable);
710
711
    switch (bpp) {
712
    case 1:
713
        c->yuvTable = av_malloc(1024);
714
        y_table = c->yuvTable;
715
        yb = -(384<<16) - oy;
716
        for (i = 0; i < 1024-110; i++) {
717
            y_table[i+110] = av_clip_uint8((yb + 0x8000) >> 16) >> 7;
718
            yb += cy;
719
        }
720
        fill_table(c->table_gU, 1, cgu, y_table + yoffs);
721
        fill_gv_table(c->table_gV, 1, cgv);
722
        break;
723
    case 4:
724
    case 4|128:
725
        rbase = isRgb ? 3 : 0;
726
        gbase = 1;
727
        bbase = isRgb ? 0 : 3;
728
        c->yuvTable = av_malloc(1024*3);
729
        y_table = c->yuvTable;
730
        yb = -(384<<16) - oy;
731
        for (i = 0; i < 1024-110; i++) {
732
            int yval = av_clip_uint8((yb + 0x8000) >> 16);
733
            y_table[i+110     ] =  (yval >> 7)       << rbase;
734
            y_table[i+ 37+1024] = ((yval + 43) / 85) << gbase;
735
            y_table[i+110+2048] =  (yval >> 7)       << bbase;
736
            yb += cy;
737
        }
738
        fill_table(c->table_rV, 1, crv, y_table + yoffs);
739
        fill_table(c->table_gU, 1, cgu, y_table + yoffs + 1024);
740
        fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2048);
741
        fill_gv_table(c->table_gV, 1, cgv);
742
        break;
743
    case 8:
744
        rbase = isRgb ? 5 : 0;
745
        gbase = isRgb ? 2 : 3;
746
        bbase = isRgb ? 0 : 6;
747
        c->yuvTable = av_malloc(1024*3);
748
        y_table = c->yuvTable;
749
        yb = -(384<<16) - oy;
750
        for (i = 0; i < 1024-38; i++) {
751
            int yval = av_clip_uint8((yb + 0x8000) >> 16);
752
            y_table[i+16     ] = ((yval + 18) / 36) << rbase;
753
            y_table[i+16+1024] = ((yval + 18) / 36) << gbase;
754
            y_table[i+37+2048] = ((yval + 43) / 85) << bbase;
755
            yb += cy;
756
        }
757
        fill_table(c->table_rV, 1, crv, y_table + yoffs);
758
        fill_table(c->table_gU, 1, cgu, y_table + yoffs + 1024);
759
        fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2048);
760
        fill_gv_table(c->table_gV, 1, cgv);
761
        break;
762 601949b8 Janusz Krzysztofik
    case 12:
763
        rbase = isRgb ? 8 : 0;
764
        gbase = 4;
765
        bbase = isRgb ? 0 : 8;
766
        c->yuvTable = av_malloc(1024*3*2);
767
        y_table16 = c->yuvTable;
768
        yb = -(384<<16) - oy;
769
        for (i = 0; i < 1024; i++) {
770
            uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
771 124cb451 Benoit Fouet
            y_table16[i     ] = (yval >> 4) << rbase;
772 601949b8 Janusz Krzysztofik
            y_table16[i+1024] = (yval >> 4) << gbase;
773 124cb451 Benoit Fouet
            y_table16[i+2048] = (yval >> 4) << bbase;
774 601949b8 Janusz Krzysztofik
            yb += cy;
775
        }
776 a4e44130 Janusz Krzysztofik
        if (isNotNe)
777
            for (i = 0; i < 1024*3; i++)
778 6a01eab7 Måns Rullgård
                y_table16[i] = av_bswap16(y_table16[i]);
779 601949b8 Janusz Krzysztofik
        fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
780
        fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + 1024);
781
        fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2048);
782
        fill_gv_table(c->table_gV, 2, cgv);
783
        break;
784 e76709d8 Kostya Shishkov
    case 15:
785
    case 16:
786
        rbase = isRgb ? bpp - 5 : 0;
787
        gbase = 5;
788
        bbase = isRgb ? 0 : (bpp - 5);
789
        c->yuvTable = av_malloc(1024*3*2);
790
        y_table16 = c->yuvTable;
791
        yb = -(384<<16) - oy;
792
        for (i = 0; i < 1024; i++) {
793
            uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
794
            y_table16[i     ] = (yval >> 3)          << rbase;
795
            y_table16[i+1024] = (yval >> (18 - bpp)) << gbase;
796
            y_table16[i+2048] = (yval >> 3)          << bbase;
797
            yb += cy;
798
        }
799 1379b584 Alexis Ballier
        if(isNotNe)
800
            for (i = 0; i < 1024*3; i++)
801 6a01eab7 Måns Rullgård
                y_table16[i] = av_bswap16(y_table16[i]);
802 e76709d8 Kostya Shishkov
        fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
803
        fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + 1024);
804
        fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2048);
805
        fill_gv_table(c->table_gV, 2, cgv);
806
        break;
807
    case 24:
808 68e7f482 Kostya Shishkov
    case 48:
809 e76709d8 Kostya Shishkov
        c->yuvTable = av_malloc(1024);
810
        y_table = c->yuvTable;
811
        yb = -(384<<16) - oy;
812
        for (i = 0; i < 1024; i++) {
813
            y_table[i] = av_clip_uint8((yb + 0x8000) >> 16);
814
            yb += cy;
815
        }
816
        fill_table(c->table_rV, 1, crv, y_table + yoffs);
817
        fill_table(c->table_gU, 1, cgu, y_table + yoffs);
818
        fill_table(c->table_bU, 1, cbu, y_table + yoffs);
819
        fill_gv_table(c->table_gV, 1, cgv);
820
        break;
821
    case 32:
822
        base = (c->dstFormat == PIX_FMT_RGB32_1 || c->dstFormat == PIX_FMT_BGR32_1) ? 8 : 0;
823
        rbase = base + (isRgb ? 16 : 0);
824
        gbase = base + 8;
825
        bbase = base + (isRgb ? 0 : 16);
826 3acd545f Cédric Schieli
        needAlpha = CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat);
827
        if (!needAlpha)
828
            abase = (base + 24) & 31;
829 e76709d8 Kostya Shishkov
        c->yuvTable = av_malloc(1024*3*4);
830
        y_table32 = c->yuvTable;
831
        yb = -(384<<16) - oy;
832
        for (i = 0; i < 1024; i++) {
833
            uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
834 3acd545f Cédric Schieli
            y_table32[i     ] = (yval << rbase) + (needAlpha ? 0 : (255 << abase));
835 e76709d8 Kostya Shishkov
            y_table32[i+1024] = yval << gbase;
836
            y_table32[i+2048] = yval << bbase;
837
            yb += cy;
838
        }
839
        fill_table(c->table_rV, 4, crv, y_table32 + yoffs);
840
        fill_table(c->table_gU, 4, cgu, y_table32 + yoffs + 1024);
841
        fill_table(c->table_bU, 4, cbu, y_table32 + yoffs + 2048);
842
        fill_gv_table(c->table_gV, 4, cgv);
843
        break;
844
    default:
845
        c->yuvTable = NULL;
846
        av_log(c, AV_LOG_ERROR, "%ibpp not supported by yuv2rgb\n", bpp);
847
        return -1;
848
    }
849
    return 0;
850
}