Statistics
| Branch: | Revision:

ffmpeg / libswscale / swscale.c @ db46be01

History | View | Annotate | Download (72.1 KB)

1 fe8054c0 Michael Niedermayer
/*
2 d026b45e Diego Biurrun
 * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
3
 *
4 2912e87a Mans Rullgard
 * This file is part of Libav.
5 d026b45e Diego Biurrun
 *
6 2912e87a Mans Rullgard
 * Libav is free software; you can redistribute it and/or
7 819ee683 Diego Biurrun
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10 d026b45e Diego Biurrun
 *
11 2912e87a Mans Rullgard
 * Libav is distributed in the hope that it will be useful,
12 d026b45e Diego Biurrun
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 819ee683 Diego Biurrun
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15 d026b45e Diego Biurrun
 *
16 819ee683 Diego Biurrun
 * You should have received a copy of the GNU Lesser General Public
17 2912e87a Mans Rullgard
 * License along with Libav; if not, write to the Free Software
18 b19bcbaa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 d026b45e Diego Biurrun
 */
20 783e9cc9 Michael Niedermayer
21 28bf81c9 Michael Niedermayer
/*
22 9990e426 Michael Niedermayer
  supported Input formats: YV12, I420/IYUV, YUY2, UYVY, BGR32, BGR32_1, BGR24, BGR16, BGR15, RGB32, RGB32_1, RGB24, Y8/Y800, YVU9/IF09, PAL8
23 caeaabe7 Alex Beregszaszi
  supported output formats: YV12, I420/IYUV, YUY2, UYVY, {BGR,RGB}{1,4,8,15,16,24,32}, Y8/Y800, YVU9/IF09
24 e09d12f4 Michael Niedermayer
  {BGR,RGB}{1,4,8,15,16} support dithering
25 6a4970ab Diego Biurrun

26 e09d12f4 Michael Niedermayer
  unscaled special converters (YV12=I420=IYUV, Y800=Y8)
27 601949b8 Janusz Krzysztofik
  YV12 -> {BGR,RGB}{1,4,8,12,15,16,24,32}
28 e09d12f4 Michael Niedermayer
  x -> x
29
  YUV9 -> YV12
30
  YUV9/YV12 -> Y800
31
  Y800 -> YUV9/YV12
32 b935781b Michael Niedermayer
  BGR24 -> BGR32 & RGB24 -> RGB32
33
  BGR32 -> BGR24 & RGB32 -> RGB24
34 4bb3fa5e Michael Niedermayer
  BGR15 -> BGR16
35 b935781b Michael Niedermayer
*/
36
37 6a4970ab Diego Biurrun
/*
38 a6f6b237 Diego Biurrun
tested special converters (most are tested actually, but I did not write it down ...)
39 22e8222f Janusz Krzysztofik
 YV12 -> BGR12/BGR16
40 b935781b Michael Niedermayer
 YV12 -> YV12
41 4bb3fa5e Michael Niedermayer
 BGR15 -> BGR16
42 1e1c4fe9 Michael Niedermayer
 BGR16 -> BGR16
43 e09d12f4 Michael Niedermayer
 YVU9 -> YV12
44 b935781b Michael Niedermayer

45
untested special converters
46 f40c7dbb Diego Biurrun
  YV12/I420 -> BGR15/BGR24/BGR32 (it is the yuv2rgb stuff, so it should be OK)
47 1e1c4fe9 Michael Niedermayer
  YV12/I420 -> YV12/I420
48
  YUY2/BGR15/BGR24/BGR32/RGB24/RGB32 -> same format
49 b935781b Michael Niedermayer
  BGR24 -> BGR32 & RGB24 -> RGB32
50
  BGR32 -> BGR24 & RGB32 -> RGB24
51 ec22603f Michael Niedermayer
  BGR24 -> YV12
52 28bf81c9 Michael Niedermayer
*/
53
54 d3f41512 Michael Niedermayer
#include <inttypes.h>
55 dda87e9f Pierre Lombard
#include <string.h>
56 077ea8a7 Michael Niedermayer
#include <math.h>
57 c1b0bfb4 Michael Niedermayer
#include <stdio.h>
58 b2d374c9 Diego Biurrun
#include "config.h"
59 81b7c056 Michael Niedermayer
#include <assert.h>
60 d604bab9 Michael Niedermayer
#include "swscale.h"
61 5427e242 Michael Niedermayer
#include "swscale_internal.h"
62 37079906 Michael Niedermayer
#include "rgb2rgb.h"
63 52154148 Ramiro Polla
#include "libavutil/intreadwrite.h"
64 83da2c6f Diego Biurrun
#include "libavutil/x86_cpu.h"
65 7248797c Ramiro Polla
#include "libavutil/avutil.h"
66 4e74187d Stefano Sabatini
#include "libavutil/mathematics.h"
67 83da2c6f Diego Biurrun
#include "libavutil/bswap.h"
68 a9af75ae Stefano Sabatini
#include "libavutil/pixdesc.h"
69 0d9f3d85 Arpi
70 541c4eb9 Michael Niedermayer
#undef MOVNTQ
71 7d7f78b5 Michael Niedermayer
#undef PAVGB
72 d3f41512 Michael Niedermayer
73 783e9cc9 Michael Niedermayer
//#undef HAVE_MMX2
74 f4406ec1 Diego Biurrun
//#define HAVE_AMD3DNOW
75 d3f41512 Michael Niedermayer
//#undef HAVE_MMX
76 783e9cc9 Michael Niedermayer
//#undef ARCH_X86
77 d604bab9 Michael Niedermayer
#define DITHER1XBPP
78 d3f41512 Michael Niedermayer
79 f40c7dbb Diego Biurrun
#define FAST_BGR2YV12 // use 7 bit coefficients instead of 15 bit
80 ac6a2e45 Michael Niedermayer
81 9d9de37d Ivo van Poorten
#define isPacked(x)         (       \
82
           (x)==PIX_FMT_PAL8        \
83
        || (x)==PIX_FMT_YUYV422     \
84
        || (x)==PIX_FMT_UYVY422     \
85 df0743e0 Baptiste Coudurier
        || (x)==PIX_FMT_Y400A       \
86 60222557 Michael Niedermayer
        || isAnyRGB(x)              \
87 9d9de37d Ivo van Poorten
    )
88 6ff0ad6b Michael Niedermayer
89 6b79dbce Michael Niedermayer
#define RGB2YUV_SHIFT 15
90 7b5d7b9e Michael Niedermayer
#define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
91
#define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
92
#define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
93
#define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
94
#define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
95
#define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
96
#define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
97
#define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
98
#define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
99 6c7506de Michael Niedermayer
100 0f5d4aa8 Michael Niedermayer
static const double rgb2yuv_table[8][9]={
101 24911613 Stefano Sabatini
    {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5}, //ITU709
102
    {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5}, //ITU709
103
    {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
104
    {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
105 0f5d4aa8 Michael Niedermayer
    {0.59  , 0.11  , 0.30  , -0.331, 0.5, -0.169, -0.421, -0.079, 0.5}, //FCC
106 24911613 Stefano Sabatini
    {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
107
    {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
108 04f6ab1c Stefano Sabatini
    {0.701 , 0.087 , 0.212 , -0.384, 0.5, -0.116, -0.445, -0.055, 0.5}, //SMPTE 240M
109 0f5d4aa8 Michael Niedermayer
};
110
111 783e9cc9 Michael Niedermayer
/*
112
NOTES
113 d604bab9 Michael Niedermayer
Special versions: fast Y 1:1 scaling (no interpolation in y direction)
114 31190492 Arpi

115 783e9cc9 Michael Niedermayer
TODO
116 bd7c6fd5 Diego Biurrun
more intelligent misalignment avoidance for the horizontal scaler
117 c1b0bfb4 Michael Niedermayer
write special vertical cubic upscale version
118 f40c7dbb Diego Biurrun
optimize C code (YV12 / minmax)
119
add support for packed pixel YUV input & output
120 6ff0ad6b Michael Niedermayer
add support for Y8 output
121 f40c7dbb Diego Biurrun
optimize BGR24 & BGR32
122 ff7ba856 Michael Niedermayer
add BGR4 output support
123 1e621b18 Michael Niedermayer
write special BGR->BGR scaler
124 783e9cc9 Michael Niedermayer
*/
125 31190492 Arpi
126 819ee683 Diego Biurrun
#if ARCH_X86
127 d334c7c2 Reimar Döffinger
DECLARE_ASM_CONST(8, uint64_t, bF8)=       0xF8F8F8F8F8F8F8F8LL;
128
DECLARE_ASM_CONST(8, uint64_t, bFC)=       0xFCFCFCFCFCFCFCFCLL;
129
DECLARE_ASM_CONST(8, uint64_t, w10)=       0x0010001000100010LL;
130
DECLARE_ASM_CONST(8, uint64_t, w02)=       0x0002000200020002LL;
131
DECLARE_ASM_CONST(8, uint64_t, bm00001111)=0x00000000FFFFFFFFLL;
132
DECLARE_ASM_CONST(8, uint64_t, bm00000111)=0x0000000000FFFFFFLL;
133
DECLARE_ASM_CONST(8, uint64_t, bm11111000)=0xFFFFFFFFFF000000LL;
134
DECLARE_ASM_CONST(8, uint64_t, bm01010101)=0x00FF00FF00FF00FFLL;
135 d604bab9 Michael Niedermayer
136 4a888526 Måns Rullgård
const DECLARE_ALIGNED(8, uint64_t, ff_dither4)[2] = {
137 221b804f Diego Biurrun
        0x0103010301030103LL,
138
        0x0200020002000200LL,};
139 d8fa3c54 Michael Niedermayer
140 4a888526 Måns Rullgård
const DECLARE_ALIGNED(8, uint64_t, ff_dither8)[2] = {
141 221b804f Diego Biurrun
        0x0602060206020602LL,
142
        0x0004000400040004LL,};
143 d604bab9 Michael Niedermayer
144 d334c7c2 Reimar Döffinger
DECLARE_ASM_CONST(8, uint64_t, b16Mask)=   0x001F001F001F001FLL;
145
DECLARE_ASM_CONST(8, uint64_t, g16Mask)=   0x07E007E007E007E0LL;
146
DECLARE_ASM_CONST(8, uint64_t, r16Mask)=   0xF800F800F800F800LL;
147
DECLARE_ASM_CONST(8, uint64_t, b15Mask)=   0x001F001F001F001FLL;
148
DECLARE_ASM_CONST(8, uint64_t, g15Mask)=   0x03E003E003E003E0LL;
149
DECLARE_ASM_CONST(8, uint64_t, r15Mask)=   0x7C007C007C007C00LL;
150 d604bab9 Michael Niedermayer
151 5802683a Reimar Döffinger
DECLARE_ALIGNED(8, const uint64_t, ff_M24A)         = 0x00FF0000FF0000FFLL;
152
DECLARE_ALIGNED(8, const uint64_t, ff_M24B)         = 0xFF0000FF0000FF00LL;
153
DECLARE_ALIGNED(8, const uint64_t, ff_M24C)         = 0x0000FF0000FF0000LL;
154 99d2cb72 Michael Niedermayer
155 ac6a2e45 Michael Niedermayer
#ifdef FAST_BGR2YV12
156 5802683a Reimar Döffinger
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff)   = 0x000000210041000DULL;
157
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff)   = 0x0000FFEEFFDC0038ULL;
158
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff)   = 0x00000038FFD2FFF8ULL;
159 ac6a2e45 Michael Niedermayer
#else
160 5802683a Reimar Döffinger
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff)   = 0x000020E540830C8BULL;
161
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff)   = 0x0000ED0FDAC23831ULL;
162
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff)   = 0x00003831D0E6F6EAULL;
163 69796008 Diego Biurrun
#endif /* FAST_BGR2YV12 */
164 5802683a Reimar Döffinger
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YOffset)  = 0x1010101010101010ULL;
165
DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UVOffset) = 0x8080808080808080ULL;
166
DECLARE_ALIGNED(8, const uint64_t, ff_w1111)        = 0x0001000100010001ULL;
167 dfb09bd1 Michael Niedermayer
168 b5c44b1a Diego Pettenò
DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY1Coeff) = 0x0C88000040870C88ULL;
169
DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY2Coeff) = 0x20DE4087000020DEULL;
170
DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY1Coeff) = 0x20DE0000408720DEULL;
171
DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY2Coeff) = 0x0C88408700000C88ULL;
172
DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toYOffset) = 0x0008400000084000ULL;
173 dfb09bd1 Michael Niedermayer
174 4a888526 Måns Rullgård
DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUV)[2][4] = {
175 dfb09bd1 Michael Niedermayer
    {0x38380000DAC83838ULL, 0xECFFDAC80000ECFFULL, 0xF6E40000D0E3F6E4ULL, 0x3838D0E300003838ULL},
176
    {0xECFF0000DAC8ECFFULL, 0x3838DAC800003838ULL, 0x38380000D0E33838ULL, 0xF6E4D0E30000F6E4ULL},
177
};
178
179 b5c44b1a Diego Pettenò
DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUVOffset)= 0x0040400000404000ULL;
180 dfb09bd1 Michael Niedermayer
181 819ee683 Diego Biurrun
#endif /* ARCH_X86 */
182 783e9cc9 Michael Niedermayer
183 4a888526 Måns Rullgård
DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_4)[2][8]={
184 45e18be8 Michael Niedermayer
{  1,   3,   1,   3,   1,   3,   1,   3, },
185
{  2,   0,   2,   0,   2,   0,   2,   0, },
186
};
187
188 4a888526 Måns Rullgård
DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8)[2][8]={
189 45e18be8 Michael Niedermayer
{  6,   2,   6,   2,   6,   2,   6,   2, },
190
{  0,   4,   0,   4,   0,   4,   0,   4, },
191
};
192
193 601949b8 Janusz Krzysztofik
DECLARE_ALIGNED(8, const uint8_t, dither_4x4_16)[4][8]={
194
{  8,   4,  11,   7,   8,   4,  11,   7, },
195
{  2,  14,   1,  13,   2,  14,   1,  13, },
196
{ 10,   6,   9,   5,  10,   6,   9,   5, },
197
{  0,  12,   3,  15,   0,  12,   3,  15, },
198
};
199
200 4a888526 Måns Rullgård
DECLARE_ALIGNED(8, const uint8_t, dither_8x8_32)[8][8]={
201 45e18be8 Michael Niedermayer
{ 17,   9,  23,  15,  16,   8,  22,  14, },
202
{  5,  29,   3,  27,   4,  28,   2,  26, },
203
{ 21,  13,  19,  11,  20,  12,  18,  10, },
204
{  0,  24,   6,  30,   1,  25,   7,  31, },
205
{ 16,   8,  22,  14,  17,   9,  23,  15, },
206
{  4,  28,   2,  26,   5,  29,   3,  27, },
207
{ 20,  12,  18,  10,  21,  13,  19,  11, },
208
{  1,  25,   7,  31,   0,  24,   6,  30, },
209
};
210
211 4a888526 Måns Rullgård
DECLARE_ALIGNED(8, const uint8_t, dither_8x8_73)[8][8]={
212 45e18be8 Michael Niedermayer
{  0,  55,  14,  68,   3,  58,  17,  72, },
213
{ 37,  18,  50,  32,  40,  22,  54,  35, },
214
{  9,  64,   5,  59,  13,  67,   8,  63, },
215
{ 46,  27,  41,  23,  49,  31,  44,  26, },
216
{  2,  57,  16,  71,   1,  56,  15,  70, },
217
{ 39,  21,  52,  34,  38,  19,  51,  33, },
218
{ 11,  66,   7,  62,  10,  65,   6,  60, },
219
{ 48,  30,  43,  25,  47,  29,  42,  24, },
220
};
221
222
#if 1
223 4a888526 Måns Rullgård
DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
224 45e18be8 Michael Niedermayer
{117,  62, 158, 103, 113,  58, 155, 100, },
225
{ 34, 199,  21, 186,  31, 196,  17, 182, },
226
{144,  89, 131,  76, 141,  86, 127,  72, },
227
{  0, 165,  41, 206,  10, 175,  52, 217, },
228
{110,  55, 151,  96, 120,  65, 162, 107, },
229
{ 28, 193,  14, 179,  38, 203,  24, 189, },
230
{138,  83, 124,  69, 148,  93, 134,  79, },
231
{  7, 172,  48, 213,   3, 168,  45, 210, },
232
};
233
#elif 1
234
// tries to correct a gamma of 1.5
235 4a888526 Måns Rullgård
DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
236 45e18be8 Michael Niedermayer
{  0, 143,  18, 200,   2, 156,  25, 215, },
237
{ 78,  28, 125,  64,  89,  36, 138,  74, },
238
{ 10, 180,   3, 161,  16, 195,   8, 175, },
239
{109,  51,  93,  38, 121,  60, 105,  47, },
240
{  1, 152,  23, 210,   0, 147,  20, 205, },
241
{ 85,  33, 134,  71,  81,  30, 130,  67, },
242
{ 14, 190,   6, 171,  12, 185,   5, 166, },
243
{117,  57, 101,  44, 113,  54,  97,  41, },
244
};
245
#elif 1
246
// tries to correct a gamma of 2.0
247 4a888526 Måns Rullgård
DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
248 45e18be8 Michael Niedermayer
{  0, 124,   8, 193,   0, 140,  12, 213, },
249
{ 55,  14, 104,  42,  66,  19, 119,  52, },
250
{  3, 168,   1, 145,   6, 187,   3, 162, },
251
{ 86,  31,  70,  21,  99,  39,  82,  28, },
252
{  0, 134,  11, 206,   0, 129,   9, 200, },
253
{ 62,  17, 114,  48,  58,  16, 109,  45, },
254
{  5, 181,   2, 157,   4, 175,   1, 151, },
255
{ 95,  36,  78,  26,  90,  34,  74,  24, },
256
};
257
#else
258
// tries to correct a gamma of 2.5
259 4a888526 Måns Rullgård
DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
260 45e18be8 Michael Niedermayer
{  0, 107,   3, 187,   0, 125,   6, 212, },
261
{ 39,   7,  86,  28,  49,  11, 102,  36, },
262
{  1, 158,   0, 131,   3, 180,   1, 151, },
263
{ 68,  19,  52,  12,  81,  25,  64,  17, },
264
{  0, 119,   5, 203,   0, 113,   4, 195, },
265
{ 45,   9,  96,  33,  42,   8,  91,  30, },
266
{  2, 172,   1, 144,   2, 165,   0, 137, },
267
{ 77,  23,  60,  15,  72,  21,  56,  14, },
268
};
269
#endif
270 5cebb24b Michael Niedermayer
271 52154148 Ramiro Polla
static av_always_inline void yuv2yuvX16inC_template(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
272
                                                    const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
273
                                                    const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest,
274
                                                    int dstW, int chrDstW, int big_endian)
275
{
276
    //FIXME Optimize (just quickly written not optimized..)
277
    int i;
278
279
    for (i = 0; i < dstW; i++) {
280
        int val = 1 << 10;
281
        int j;
282
283
        for (j = 0; j < lumFilterSize; j++)
284
            val += lumSrc[j][i] * lumFilter[j];
285
286
        if (big_endian) {
287
            AV_WB16(&dest[i], av_clip_uint16(val >> 11));
288
        } else {
289
            AV_WL16(&dest[i], av_clip_uint16(val >> 11));
290
        }
291
    }
292
293
    if (uDest) {
294
        for (i = 0; i < chrDstW; i++) {
295
            int u = 1 << 10;
296
            int v = 1 << 10;
297
            int j;
298
299
            for (j = 0; j < chrFilterSize; j++) {
300
                u += chrSrc[j][i       ] * chrFilter[j];
301
                v += chrSrc[j][i + VOFW] * chrFilter[j];
302
            }
303
304
            if (big_endian) {
305
                AV_WB16(&uDest[i], av_clip_uint16(u >> 11));
306
                AV_WB16(&vDest[i], av_clip_uint16(v >> 11));
307
            } else {
308
                AV_WL16(&uDest[i], av_clip_uint16(u >> 11));
309
                AV_WL16(&vDest[i], av_clip_uint16(v >> 11));
310
            }
311
        }
312
    }
313
314
    if (CONFIG_SWSCALE_ALPHA && aDest) {
315
        for (i = 0; i < dstW; i++) {
316
            int val = 1 << 10;
317
            int j;
318
319
            for (j = 0; j < lumFilterSize; j++)
320
                val += alpSrc[j][i] * lumFilter[j];
321
322
            if (big_endian) {
323
                AV_WB16(&aDest[i], av_clip_uint16(val >> 11));
324
            } else {
325
                AV_WL16(&aDest[i], av_clip_uint16(val >> 11));
326
            }
327
        }
328
    }
329
}
330
331
static inline void yuv2yuvX16inC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
332
                                 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
333
                                 const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest, int dstW, int chrDstW,
334
                                 enum PixelFormat dstFormat)
335
{
336
    if (isBE(dstFormat)) {
337
        yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
338
                               chrFilter, chrSrc, chrFilterSize,
339
                               alpSrc,
340
                               dest, uDest, vDest, aDest,
341
                               dstW, chrDstW, 1);
342
    } else {
343
        yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
344
                               chrFilter, chrSrc, chrFilterSize,
345
                               alpSrc,
346
                               dest, uDest, vDest, aDest,
347
                               dstW, chrDstW, 0);
348
    }
349
}
350
351 7ac40327 Ramiro Polla
static inline void yuv2yuvXinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
352
                               const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
353
                               const int16_t **alpSrc, uint8_t *dest, uint8_t *uDest, uint8_t *vDest, uint8_t *aDest, int dstW, int chrDstW)
354 e3d2500f Michael Niedermayer
{
355 f40c7dbb Diego Biurrun
    //FIXME Optimize (just quickly written not optimized..)
356 221b804f Diego Biurrun
    int i;
357 dd68318c Ramiro Polla
    for (i=0; i<dstW; i++) {
358 221b804f Diego Biurrun
        int val=1<<18;
359
        int j;
360
        for (j=0; j<lumFilterSize; j++)
361
            val += lumSrc[j][i] * lumFilter[j];
362
363
        dest[i]= av_clip_uint8(val>>19);
364
    }
365
366 1b0a4572 Benoit Fouet
    if (uDest)
367 dd68318c Ramiro Polla
        for (i=0; i<chrDstW; i++) {
368 221b804f Diego Biurrun
            int u=1<<18;
369
            int v=1<<18;
370
            int j;
371 dd68318c Ramiro Polla
            for (j=0; j<chrFilterSize; j++) {
372 221b804f Diego Biurrun
                u += chrSrc[j][i] * chrFilter[j];
373 8b2fce0d Michael Niedermayer
                v += chrSrc[j][i + VOFW] * chrFilter[j];
374 221b804f Diego Biurrun
            }
375
376
            uDest[i]= av_clip_uint8(u>>19);
377
            vDest[i]= av_clip_uint8(v>>19);
378
        }
379 6858492e Cédric Schieli
380
    if (CONFIG_SWSCALE_ALPHA && aDest)
381 dd68318c Ramiro Polla
        for (i=0; i<dstW; i++) {
382 6858492e Cédric Schieli
            int val=1<<18;
383
            int j;
384
            for (j=0; j<lumFilterSize; j++)
385
                val += alpSrc[j][i] * lumFilter[j];
386
387
            aDest[i]= av_clip_uint8(val>>19);
388
        }
389
390 e3d2500f Michael Niedermayer
}
391
392 7ac40327 Ramiro Polla
static inline void yuv2nv12XinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
393
                                const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
394 221b804f Diego Biurrun
                                uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
395 6118e52e Ville Syrjälä
{
396 f40c7dbb Diego Biurrun
    //FIXME Optimize (just quickly written not optimized..)
397 221b804f Diego Biurrun
    int i;
398 dd68318c Ramiro Polla
    for (i=0; i<dstW; i++) {
399 221b804f Diego Biurrun
        int val=1<<18;
400
        int j;
401
        for (j=0; j<lumFilterSize; j++)
402
            val += lumSrc[j][i] * lumFilter[j];
403
404
        dest[i]= av_clip_uint8(val>>19);
405
    }
406
407 1b0a4572 Benoit Fouet
    if (!uDest)
408 221b804f Diego Biurrun
        return;
409
410
    if (dstFormat == PIX_FMT_NV12)
411 dd68318c Ramiro Polla
        for (i=0; i<chrDstW; i++) {
412 221b804f Diego Biurrun
            int u=1<<18;
413
            int v=1<<18;
414
            int j;
415 dd68318c Ramiro Polla
            for (j=0; j<chrFilterSize; j++) {
416 221b804f Diego Biurrun
                u += chrSrc[j][i] * chrFilter[j];
417 8b2fce0d Michael Niedermayer
                v += chrSrc[j][i + VOFW] * chrFilter[j];
418 221b804f Diego Biurrun
            }
419
420
            uDest[2*i]= av_clip_uint8(u>>19);
421
            uDest[2*i+1]= av_clip_uint8(v>>19);
422
        }
423
    else
424 dd68318c Ramiro Polla
        for (i=0; i<chrDstW; i++) {
425 221b804f Diego Biurrun
            int u=1<<18;
426
            int v=1<<18;
427
            int j;
428 dd68318c Ramiro Polla
            for (j=0; j<chrFilterSize; j++) {
429 221b804f Diego Biurrun
                u += chrSrc[j][i] * chrFilter[j];
430 8b2fce0d Michael Niedermayer
                v += chrSrc[j][i + VOFW] * chrFilter[j];
431 221b804f Diego Biurrun
            }
432
433
            uDest[2*i]= av_clip_uint8(v>>19);
434
            uDest[2*i+1]= av_clip_uint8(u>>19);
435
        }
436 6118e52e Ville Syrjälä
}
437 46de8b73 Michael Niedermayer
438 6858492e Cédric Schieli
#define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha) \
439 dd68318c Ramiro Polla
    for (i=0; i<(dstW>>1); i++) {\
440 221b804f Diego Biurrun
        int j;\
441
        int Y1 = 1<<18;\
442
        int Y2 = 1<<18;\
443
        int U  = 1<<18;\
444
        int V  = 1<<18;\
445 6858492e Cédric Schieli
        int av_unused A1, A2;\
446 2db27aad Carl Eugen Hoyos
        type av_unused *r, *b, *g;\
447 221b804f Diego Biurrun
        const int i2= 2*i;\
448
        \
449 dd68318c Ramiro Polla
        for (j=0; j<lumFilterSize; j++) {\
450 221b804f Diego Biurrun
            Y1 += lumSrc[j][i2] * lumFilter[j];\
451
            Y2 += lumSrc[j][i2+1] * lumFilter[j];\
452
        }\
453 dd68318c Ramiro Polla
        for (j=0; j<chrFilterSize; j++) {\
454 221b804f Diego Biurrun
            U += chrSrc[j][i] * chrFilter[j];\
455 8b2fce0d Michael Niedermayer
            V += chrSrc[j][i+VOFW] * chrFilter[j];\
456 221b804f Diego Biurrun
        }\
457
        Y1>>=19;\
458
        Y2>>=19;\
459
        U >>=19;\
460
        V >>=19;\
461 dd68318c Ramiro Polla
        if (alpha) {\
462 6858492e Cédric Schieli
            A1 = 1<<18;\
463
            A2 = 1<<18;\
464 dd68318c Ramiro Polla
            for (j=0; j<lumFilterSize; j++) {\
465 6858492e Cédric Schieli
                A1 += alpSrc[j][i2  ] * lumFilter[j];\
466
                A2 += alpSrc[j][i2+1] * lumFilter[j];\
467
            }\
468
            A1>>=19;\
469
            A2>>=19;\
470 c36a4294 Stefano Sabatini
        }
471 bdf397ba Michael Niedermayer
472 6858492e Cédric Schieli
#define YSCALE_YUV_2_PACKEDX_C(type,alpha) \
473
        YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha)\
474 dd68318c Ramiro Polla
        if ((Y1|Y2|U|V)&256) {\
475 221b804f Diego Biurrun
            if (Y1>255)   Y1=255; \
476
            else if (Y1<0)Y1=0;   \
477
            if (Y2>255)   Y2=255; \
478
            else if (Y2<0)Y2=0;   \
479
            if (U>255)    U=255;  \
480
            else if (U<0) U=0;    \
481
            if (V>255)    V=255;  \
482
            else if (V<0) V=0;    \
483 6858492e Cédric Schieli
        }\
484 dd68318c Ramiro Polla
        if (alpha && ((A1|A2)&256)) {\
485 6858492e Cédric Schieli
            A1=av_clip_uint8(A1);\
486
            A2=av_clip_uint8(A2);\
487 221b804f Diego Biurrun
        }
488 6a4970ab Diego Biurrun
489 6858492e Cédric Schieli
#define YSCALE_YUV_2_PACKEDX_FULL_C(rnd,alpha) \
490 dd68318c Ramiro Polla
    for (i=0; i<dstW; i++) {\
491 f0faee4c Michael Niedermayer
        int j;\
492
        int Y = 0;\
493
        int U = -128<<19;\
494
        int V = -128<<19;\
495 6858492e Cédric Schieli
        int av_unused A;\
496 f0faee4c Michael Niedermayer
        int R,G,B;\
497
        \
498 dd68318c Ramiro Polla
        for (j=0; j<lumFilterSize; j++) {\
499 f0faee4c Michael Niedermayer
            Y += lumSrc[j][i     ] * lumFilter[j];\
500
        }\
501 dd68318c Ramiro Polla
        for (j=0; j<chrFilterSize; j++) {\
502 f0faee4c Michael Niedermayer
            U += chrSrc[j][i     ] * chrFilter[j];\
503
            V += chrSrc[j][i+VOFW] * chrFilter[j];\
504
        }\
505
        Y >>=10;\
506
        U >>=10;\
507
        V >>=10;\
508 dd68318c Ramiro Polla
        if (alpha) {\
509 6858492e Cédric Schieli
            A = rnd;\
510
            for (j=0; j<lumFilterSize; j++)\
511
                A += alpSrc[j][i     ] * lumFilter[j];\
512
            A >>=19;\
513
            if (A&256)\
514
                A = av_clip_uint8(A);\
515 c36a4294 Stefano Sabatini
        }
516 f0faee4c Michael Niedermayer
517 6858492e Cédric Schieli
#define YSCALE_YUV_2_RGBX_FULL_C(rnd,alpha) \
518
    YSCALE_YUV_2_PACKEDX_FULL_C(rnd>>3,alpha)\
519 43c16478 Michael Niedermayer
        Y-= c->yuv2rgb_y_offset;\
520
        Y*= c->yuv2rgb_y_coeff;\
521 f0faee4c Michael Niedermayer
        Y+= rnd;\
522 43c16478 Michael Niedermayer
        R= Y + V*c->yuv2rgb_v2r_coeff;\
523
        G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
524
        B= Y +                          U*c->yuv2rgb_u2b_coeff;\
525 dd68318c Ramiro Polla
        if ((R|G|B)&(0xC0000000)) {\
526 f0faee4c Michael Niedermayer
            if (R>=(256<<22))   R=(256<<22)-1; \
527
            else if (R<0)R=0;   \
528
            if (G>=(256<<22))   G=(256<<22)-1; \
529
            else if (G<0)G=0;   \
530
            if (B>=(256<<22))   B=(256<<22)-1; \
531
            else if (B<0)B=0;   \
532 c36a4294 Stefano Sabatini
        }
533 f0faee4c Michael Niedermayer
534 e69bd294 Michael Niedermayer
#define YSCALE_YUV_2_GRAY16_C \
535 dd68318c Ramiro Polla
    for (i=0; i<(dstW>>1); i++) {\
536 b0880d5d Michael Niedermayer
        int j;\
537
        int Y1 = 1<<18;\
538
        int Y2 = 1<<18;\
539
        int U  = 1<<18;\
540
        int V  = 1<<18;\
541 e69bd294 Michael Niedermayer
        \
542 b0880d5d Michael Niedermayer
        const int i2= 2*i;\
543
        \
544 dd68318c Ramiro Polla
        for (j=0; j<lumFilterSize; j++) {\
545 b0880d5d Michael Niedermayer
            Y1 += lumSrc[j][i2] * lumFilter[j];\
546
            Y2 += lumSrc[j][i2+1] * lumFilter[j];\
547
        }\
548
        Y1>>=11;\
549
        Y2>>=11;\
550 dd68318c Ramiro Polla
        if ((Y1|Y2|U|V)&65536) {\
551 b0880d5d Michael Niedermayer
            if (Y1>65535)   Y1=65535; \
552
            else if (Y1<0)Y1=0;   \
553
            if (Y2>65535)   Y2=65535; \
554
            else if (Y2<0)Y2=0;   \
555
        }
556
557 6858492e Cédric Schieli
#define YSCALE_YUV_2_RGBX_C(type,alpha) \
558
    YSCALE_YUV_2_PACKEDX_C(type,alpha)  /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
559 221b804f Diego Biurrun
    r = (type *)c->table_rV[V];   \
560
    g = (type *)(c->table_gU[U] + c->table_gV[V]); \
561 c36a4294 Stefano Sabatini
    b = (type *)c->table_bU[U];
562 221b804f Diego Biurrun
563 6858492e Cédric Schieli
#define YSCALE_YUV_2_PACKED2_C(type,alpha)   \
564 dd68318c Ramiro Polla
    for (i=0; i<(dstW>>1); i++) { \
565 221b804f Diego Biurrun
        const int i2= 2*i;       \
566
        int Y1= (buf0[i2  ]*yalpha1+buf1[i2  ]*yalpha)>>19;           \
567
        int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19;           \
568
        int U= (uvbuf0[i     ]*uvalpha1+uvbuf1[i     ]*uvalpha)>>19;  \
569 8b2fce0d Michael Niedermayer
        int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19;  \
570 6858492e Cédric Schieli
        type av_unused *r, *b, *g;                                    \
571
        int av_unused A1, A2;                                         \
572 dd68318c Ramiro Polla
        if (alpha) {\
573 6858492e Cédric Schieli
            A1= (abuf0[i2  ]*yalpha1+abuf1[i2  ]*yalpha)>>19;         \
574
            A2= (abuf0[i2+1]*yalpha1+abuf1[i2+1]*yalpha)>>19;         \
575 c36a4294 Stefano Sabatini
        }
576 46de8b73 Michael Niedermayer
577 b0880d5d Michael Niedermayer
#define YSCALE_YUV_2_GRAY16_2_C   \
578 dd68318c Ramiro Polla
    for (i=0; i<(dstW>>1); i++) { \
579 b0880d5d Michael Niedermayer
        const int i2= 2*i;       \
580
        int Y1= (buf0[i2  ]*yalpha1+buf1[i2  ]*yalpha)>>11;           \
581 c36a4294 Stefano Sabatini
        int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11;
582 b0880d5d Michael Niedermayer
583 6858492e Cédric Schieli
#define YSCALE_YUV_2_RGB2_C(type,alpha) \
584
    YSCALE_YUV_2_PACKED2_C(type,alpha)\
585 221b804f Diego Biurrun
    r = (type *)c->table_rV[V];\
586
    g = (type *)(c->table_gU[U] + c->table_gV[V]);\
587 c36a4294 Stefano Sabatini
    b = (type *)c->table_bU[U];
588 cf7d1c1a Michael Niedermayer
589 6858492e Cédric Schieli
#define YSCALE_YUV_2_PACKED1_C(type,alpha) \
590 dd68318c Ramiro Polla
    for (i=0; i<(dstW>>1); i++) {\
591 221b804f Diego Biurrun
        const int i2= 2*i;\
592
        int Y1= buf0[i2  ]>>7;\
593
        int Y2= buf0[i2+1]>>7;\
594
        int U= (uvbuf1[i     ])>>7;\
595 8b2fce0d Michael Niedermayer
        int V= (uvbuf1[i+VOFW])>>7;\
596 6858492e Cédric Schieli
        type av_unused *r, *b, *g;\
597
        int av_unused A1, A2;\
598 dd68318c Ramiro Polla
        if (alpha) {\
599 6858492e Cédric Schieli
            A1= abuf0[i2  ]>>7;\
600
            A2= abuf0[i2+1]>>7;\
601 c36a4294 Stefano Sabatini
        }
602 46de8b73 Michael Niedermayer
603 b0880d5d Michael Niedermayer
#define YSCALE_YUV_2_GRAY16_1_C \
604 dd68318c Ramiro Polla
    for (i=0; i<(dstW>>1); i++) {\
605 b0880d5d Michael Niedermayer
        const int i2= 2*i;\
606
        int Y1= buf0[i2  ]<<1;\
607 c36a4294 Stefano Sabatini
        int Y2= buf0[i2+1]<<1;
608 b0880d5d Michael Niedermayer
609 6858492e Cédric Schieli
#define YSCALE_YUV_2_RGB1_C(type,alpha) \
610
    YSCALE_YUV_2_PACKED1_C(type,alpha)\
611 221b804f Diego Biurrun
    r = (type *)c->table_rV[V];\
612
    g = (type *)(c->table_gU[U] + c->table_gV[V]);\
613 c36a4294 Stefano Sabatini
    b = (type *)c->table_bU[U];
614 cf7d1c1a Michael Niedermayer
615 6858492e Cédric Schieli
#define YSCALE_YUV_2_PACKED1B_C(type,alpha) \
616 dd68318c Ramiro Polla
    for (i=0; i<(dstW>>1); i++) {\
617 221b804f Diego Biurrun
        const int i2= 2*i;\
618
        int Y1= buf0[i2  ]>>7;\
619
        int Y2= buf0[i2+1]>>7;\
620
        int U= (uvbuf0[i     ] + uvbuf1[i     ])>>8;\
621 8b2fce0d Michael Niedermayer
        int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
622 6858492e Cédric Schieli
        type av_unused *r, *b, *g;\
623
        int av_unused A1, A2;\
624 dd68318c Ramiro Polla
        if (alpha) {\
625 6858492e Cédric Schieli
            A1= abuf0[i2  ]>>7;\
626
            A2= abuf0[i2+1]>>7;\
627 c36a4294 Stefano Sabatini
        }
628 46de8b73 Michael Niedermayer
629 6858492e Cédric Schieli
#define YSCALE_YUV_2_RGB1B_C(type,alpha) \
630
    YSCALE_YUV_2_PACKED1B_C(type,alpha)\
631 221b804f Diego Biurrun
    r = (type *)c->table_rV[V];\
632
    g = (type *)(c->table_gU[U] + c->table_gV[V]);\
633 c36a4294 Stefano Sabatini
    b = (type *)c->table_bU[U];
634 cf7d1c1a Michael Niedermayer
635 ec1bca2a Michael Niedermayer
#define YSCALE_YUV_2_MONO2_C \
636 e69bd294 Michael Niedermayer
    const uint8_t * const d128=dither_8x8_220[y&7];\
637
    uint8_t *g= c->table_gU[128] + c->table_gV[128];\
638 dd68318c Ramiro Polla
    for (i=0; i<dstW-7; i+=8) {\
639 e69bd294 Michael Niedermayer
        int acc;\
640
        acc =       g[((buf0[i  ]*yalpha1+buf1[i  ]*yalpha)>>19) + d128[0]];\
641
        acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
642
        acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
643
        acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
644
        acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
645
        acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
646
        acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
647
        acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
648 ec1bca2a Michael Niedermayer
        ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
649 e69bd294 Michael Niedermayer
        dest++;\
650 c36a4294 Stefano Sabatini
    }
651 e69bd294 Michael Niedermayer
652 ec1bca2a Michael Niedermayer
#define YSCALE_YUV_2_MONOX_C \
653 e69bd294 Michael Niedermayer
    const uint8_t * const d128=dither_8x8_220[y&7];\
654
    uint8_t *g= c->table_gU[128] + c->table_gV[128];\
655
    int acc=0;\
656 dd68318c Ramiro Polla
    for (i=0; i<dstW-1; i+=2) {\
657 e69bd294 Michael Niedermayer
        int j;\
658
        int Y1=1<<18;\
659
        int Y2=1<<18;\
660
\
661 dd68318c Ramiro Polla
        for (j=0; j<lumFilterSize; j++) {\
662 e69bd294 Michael Niedermayer
            Y1 += lumSrc[j][i] * lumFilter[j];\
663
            Y2 += lumSrc[j][i+1] * lumFilter[j];\
664
        }\
665
        Y1>>=19;\
666
        Y2>>=19;\
667 dd68318c Ramiro Polla
        if ((Y1|Y2)&256) {\
668 e69bd294 Michael Niedermayer
            if (Y1>255)   Y1=255;\
669
            else if (Y1<0)Y1=0;\
670
            if (Y2>255)   Y2=255;\
671
            else if (Y2<0)Y2=0;\
672
        }\
673
        acc+= acc + g[Y1+d128[(i+0)&7]];\
674
        acc+= acc + g[Y2+d128[(i+1)&7]];\
675 dd68318c Ramiro Polla
        if ((i&7)==6) {\
676 ec1bca2a Michael Niedermayer
            ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
677 e69bd294 Michael Niedermayer
            dest++;\
678
        }\
679
    }
680
681
#define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
682 dd68318c Ramiro Polla
    switch(c->dstFormat) {\
683 68e7f482 Kostya Shishkov
    case PIX_FMT_RGB48BE:\
684
    case PIX_FMT_RGB48LE:\
685
        func(uint8_t,0)\
686
            ((uint8_t*)dest)[ 0]= r[Y1];\
687
            ((uint8_t*)dest)[ 1]= r[Y1];\
688
            ((uint8_t*)dest)[ 2]= g[Y1];\
689
            ((uint8_t*)dest)[ 3]= g[Y1];\
690
            ((uint8_t*)dest)[ 4]= b[Y1];\
691
            ((uint8_t*)dest)[ 5]= b[Y1];\
692
            ((uint8_t*)dest)[ 6]= r[Y2];\
693
            ((uint8_t*)dest)[ 7]= r[Y2];\
694
            ((uint8_t*)dest)[ 8]= g[Y2];\
695
            ((uint8_t*)dest)[ 9]= g[Y2];\
696
            ((uint8_t*)dest)[10]= b[Y2];\
697
            ((uint8_t*)dest)[11]= b[Y2];\
698
            dest+=12;\
699
        }\
700
        break;\
701 6858492e Cédric Schieli
    case PIX_FMT_RGBA:\
702
    case PIX_FMT_BGRA:\
703 dd68318c Ramiro Polla
        if (CONFIG_SMALL) {\
704 6858492e Cédric Schieli
            int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
705
            func(uint32_t,needAlpha)\
706
                ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? (A1<<24) : 0);\
707
                ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? (A2<<24) : 0);\
708
            }\
709 dd68318c Ramiro Polla
        } else {\
710
            if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
711 6858492e Cédric Schieli
                func(uint32_t,1)\
712
                    ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (A1<<24);\
713
                    ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (A2<<24);\
714
                }\
715 dd68318c Ramiro Polla
            } else {\
716 6858492e Cédric Schieli
                func(uint32_t,0)\
717
                    ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
718
                    ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
719
                }\
720
            }\
721
        }\
722
        break;\
723
    case PIX_FMT_ARGB:\
724
    case PIX_FMT_ABGR:\
725 dd68318c Ramiro Polla
        if (CONFIG_SMALL) {\
726 6858492e Cédric Schieli
            int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
727
            func(uint32_t,needAlpha)\
728
                ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? A1 : 0);\
729
                ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? A2 : 0);\
730
            }\
731 dd68318c Ramiro Polla
        } else {\
732
            if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
733 6858492e Cédric Schieli
                func(uint32_t,1)\
734
                    ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + A1;\
735
                    ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + A2;\
736
                }\
737 dd68318c Ramiro Polla
            } else {\
738 6858492e Cédric Schieli
                func(uint32_t,0)\
739
                    ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
740
                    ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
741
                }\
742
            }\
743 221b804f Diego Biurrun
        }                \
744
        break;\
745
    case PIX_FMT_RGB24:\
746 6858492e Cédric Schieli
        func(uint8_t,0)\
747 221b804f Diego Biurrun
            ((uint8_t*)dest)[0]= r[Y1];\
748
            ((uint8_t*)dest)[1]= g[Y1];\
749
            ((uint8_t*)dest)[2]= b[Y1];\
750
            ((uint8_t*)dest)[3]= r[Y2];\
751
            ((uint8_t*)dest)[4]= g[Y2];\
752
            ((uint8_t*)dest)[5]= b[Y2];\
753
            dest+=6;\
754
        }\
755
        break;\
756
    case PIX_FMT_BGR24:\
757 6858492e Cédric Schieli
        func(uint8_t,0)\
758 221b804f Diego Biurrun
            ((uint8_t*)dest)[0]= b[Y1];\
759
            ((uint8_t*)dest)[1]= g[Y1];\
760
            ((uint8_t*)dest)[2]= r[Y1];\
761
            ((uint8_t*)dest)[3]= b[Y2];\
762
            ((uint8_t*)dest)[4]= g[Y2];\
763
            ((uint8_t*)dest)[5]= r[Y2];\
764
            dest+=6;\
765
        }\
766
        break;\
767 1379b584 Alexis Ballier
    case PIX_FMT_RGB565BE:\
768
    case PIX_FMT_RGB565LE:\
769
    case PIX_FMT_BGR565BE:\
770
    case PIX_FMT_BGR565LE:\
771 221b804f Diego Biurrun
        {\
772
            const int dr1= dither_2x2_8[y&1    ][0];\
773
            const int dg1= dither_2x2_4[y&1    ][0];\
774
            const int db1= dither_2x2_8[(y&1)^1][0];\
775
            const int dr2= dither_2x2_8[y&1    ][1];\
776
            const int dg2= dither_2x2_4[y&1    ][1];\
777
            const int db2= dither_2x2_8[(y&1)^1][1];\
778 6858492e Cédric Schieli
            func(uint16_t,0)\
779 221b804f Diego Biurrun
                ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
780
                ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
781
            }\
782
        }\
783
        break;\
784 1379b584 Alexis Ballier
    case PIX_FMT_RGB555BE:\
785
    case PIX_FMT_RGB555LE:\
786
    case PIX_FMT_BGR555BE:\
787
    case PIX_FMT_BGR555LE:\
788 221b804f Diego Biurrun
        {\
789
            const int dr1= dither_2x2_8[y&1    ][0];\
790
            const int dg1= dither_2x2_8[y&1    ][1];\
791
            const int db1= dither_2x2_8[(y&1)^1][0];\
792
            const int dr2= dither_2x2_8[y&1    ][1];\
793
            const int dg2= dither_2x2_8[y&1    ][0];\
794
            const int db2= dither_2x2_8[(y&1)^1][1];\
795 6858492e Cédric Schieli
            func(uint16_t,0)\
796 221b804f Diego Biurrun
                ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
797
                ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
798
            }\
799
        }\
800
        break;\
801 22e8222f Janusz Krzysztofik
    case PIX_FMT_RGB444BE:\
802
    case PIX_FMT_RGB444LE:\
803
    case PIX_FMT_BGR444BE:\
804
    case PIX_FMT_BGR444LE:\
805
        {\
806
            const int dr1= dither_4x4_16[y&3    ][0];\
807
            const int dg1= dither_4x4_16[y&3    ][1];\
808
            const int db1= dither_4x4_16[(y&3)^3][0];\
809
            const int dr2= dither_4x4_16[y&3    ][1];\
810
            const int dg2= dither_4x4_16[y&3    ][0];\
811
            const int db2= dither_4x4_16[(y&3)^3][1];\
812
            func(uint16_t,0)\
813
                ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
814
                ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
815
            }\
816
        }\
817
        break;\
818 221b804f Diego Biurrun
    case PIX_FMT_RGB8:\
819
    case PIX_FMT_BGR8:\
820
        {\
821
            const uint8_t * const d64= dither_8x8_73[y&7];\
822
            const uint8_t * const d32= dither_8x8_32[y&7];\
823 6858492e Cédric Schieli
            func(uint8_t,0)\
824 221b804f Diego Biurrun
                ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
825
                ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
826
            }\
827
        }\
828
        break;\
829
    case PIX_FMT_RGB4:\
830
    case PIX_FMT_BGR4:\
831
        {\
832
            const uint8_t * const d64= dither_8x8_73 [y&7];\
833
            const uint8_t * const d128=dither_8x8_220[y&7];\
834 6858492e Cédric Schieli
            func(uint8_t,0)\
835 221b804f Diego Biurrun
                ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
836
                                 + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
837
            }\
838
        }\
839
        break;\
840
    case PIX_FMT_RGB4_BYTE:\
841
    case PIX_FMT_BGR4_BYTE:\
842
        {\
843
            const uint8_t * const d64= dither_8x8_73 [y&7];\
844
            const uint8_t * const d128=dither_8x8_220[y&7];\
845 6858492e Cédric Schieli
            func(uint8_t,0)\
846 221b804f Diego Biurrun
                ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
847
                ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
848
            }\
849
        }\
850
        break;\
851
    case PIX_FMT_MONOBLACK:\
852 ec1bca2a Michael Niedermayer
    case PIX_FMT_MONOWHITE:\
853 221b804f Diego Biurrun
        {\
854 e69bd294 Michael Niedermayer
            func_monoblack\
855 221b804f Diego Biurrun
        }\
856
        break;\
857
    case PIX_FMT_YUYV422:\
858
        func2\
859
            ((uint8_t*)dest)[2*i2+0]= Y1;\
860
            ((uint8_t*)dest)[2*i2+1]= U;\
861
            ((uint8_t*)dest)[2*i2+2]= Y2;\
862
            ((uint8_t*)dest)[2*i2+3]= V;\
863
        }                \
864
        break;\
865
    case PIX_FMT_UYVY422:\
866
        func2\
867
            ((uint8_t*)dest)[2*i2+0]= U;\
868
            ((uint8_t*)dest)[2*i2+1]= Y1;\
869
            ((uint8_t*)dest)[2*i2+2]= V;\
870
            ((uint8_t*)dest)[2*i2+3]= Y2;\
871
        }                \
872
        break;\
873 b0880d5d Michael Niedermayer
    case PIX_FMT_GRAY16BE:\
874
        func_g16\
875
            ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
876
            ((uint8_t*)dest)[2*i2+1]= Y1;\
877
            ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
878
            ((uint8_t*)dest)[2*i2+3]= Y2;\
879
        }                \
880
        break;\
881
    case PIX_FMT_GRAY16LE:\
882
        func_g16\
883
            ((uint8_t*)dest)[2*i2+0]= Y1;\
884
            ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
885
            ((uint8_t*)dest)[2*i2+2]= Y2;\
886
            ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
887
        }                \
888
        break;\
889 c36a4294 Stefano Sabatini
    }
890 cf7d1c1a Michael Niedermayer
891 7ac40327 Ramiro Polla
static inline void yuv2packedXinC(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
892
                                  const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
893
                                  const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
894 e3d2500f Michael Niedermayer
{
895 221b804f Diego Biurrun
    int i;
896 6858492e Cédric Schieli
    YSCALE_YUV_2_ANYRGB_C(YSCALE_YUV_2_RGBX_C, YSCALE_YUV_2_PACKEDX_C(void,0), YSCALE_YUV_2_GRAY16_C, YSCALE_YUV_2_MONOX_C)
897 e3d2500f Michael Niedermayer
}
898
899 7ac40327 Ramiro Polla
static inline void yuv2rgbXinC_full(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
900
                                    const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
901
                                    const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
902 f0faee4c Michael Niedermayer
{
903
    int i;
904 30b61475 Stefano Sabatini
    int step= c->dstFormatBpp/8;
905 d616c8ae Michael Niedermayer
    int aidx= 3;
906 f0faee4c Michael Niedermayer
907 dd68318c Ramiro Polla
    switch(c->dstFormat) {
908 f0faee4c Michael Niedermayer
    case PIX_FMT_ARGB:
909
        dest++;
910 a3398feb Cédric Schieli
        aidx= 0;
911 f0faee4c Michael Niedermayer
    case PIX_FMT_RGB24:
912 d616c8ae Michael Niedermayer
        aidx--;
913 f0faee4c Michael Niedermayer
    case PIX_FMT_RGBA:
914 dd68318c Ramiro Polla
        if (CONFIG_SMALL) {
915 6858492e Cédric Schieli
            int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
916
            YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
917
                dest[aidx]= needAlpha ? A : 255;
918
                dest[0]= R>>22;
919
                dest[1]= G>>22;
920
                dest[2]= B>>22;
921
                dest+= step;
922
            }
923 dd68318c Ramiro Polla
        } else {
924
            if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
925 6858492e Cédric Schieli
                YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
926
                    dest[aidx]= A;
927
                    dest[0]= R>>22;
928
                    dest[1]= G>>22;
929
                    dest[2]= B>>22;
930
                    dest+= step;
931
                }
932 dd68318c Ramiro Polla
            } else {
933 6858492e Cédric Schieli
                YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
934
                    dest[aidx]= 255;
935
                    dest[0]= R>>22;
936
                    dest[1]= G>>22;
937
                    dest[2]= B>>22;
938
                    dest+= step;
939
                }
940
            }
941 f0faee4c Michael Niedermayer
        }
942
        break;
943
    case PIX_FMT_ABGR:
944
        dest++;
945 a3398feb Cédric Schieli
        aidx= 0;
946 f0faee4c Michael Niedermayer
    case PIX_FMT_BGR24:
947 d616c8ae Michael Niedermayer
        aidx--;
948 f0faee4c Michael Niedermayer
    case PIX_FMT_BGRA:
949 dd68318c Ramiro Polla
        if (CONFIG_SMALL) {
950 6858492e Cédric Schieli
            int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
951
            YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
952
                dest[aidx]= needAlpha ? A : 255;
953
                dest[0]= B>>22;
954
                dest[1]= G>>22;
955
                dest[2]= R>>22;
956
                dest+= step;
957
            }
958 dd68318c Ramiro Polla
        } else {
959
            if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
960 6858492e Cédric Schieli
                YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
961
                    dest[aidx]= A;
962
                    dest[0]= B>>22;
963
                    dest[1]= G>>22;
964
                    dest[2]= R>>22;
965
                    dest+= step;
966
                }
967 dd68318c Ramiro Polla
            } else {
968 6858492e Cédric Schieli
                YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
969
                    dest[aidx]= 255;
970
                    dest[0]= B>>22;
971
                    dest[1]= G>>22;
972
                    dest[2]= R>>22;
973
                    dest+= step;
974
                }
975
            }
976 f0faee4c Michael Niedermayer
        }
977
        break;
978
    default:
979
        assert(0);
980
    }
981
}
982 e3d2500f Michael Niedermayer
983 dd68318c Ramiro Polla
static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val)
984
{
985 d4da3e47 Cédric Schieli
    int i;
986
    uint8_t *ptr = plane + stride*y;
987 dd68318c Ramiro Polla
    for (i=0; i<height; i++) {
988 d4da3e47 Cédric Schieli
        memset(ptr, val, width);
989
        ptr += stride;
990
    }
991
}
992
993 3483d877 Eli Friedman
static inline void rgb48ToY(uint8_t *dst, const uint8_t *src, long width,
994 a3e35e28 Ramiro Polla
                            uint32_t *unused)
995 e8417235 Kostya Shishkov
{
996
    int i;
997
    for (i = 0; i < width; i++) {
998
        int r = src[i*6+0];
999
        int g = src[i*6+2];
1000
        int b = src[i*6+4];
1001
1002
        dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1003
    }
1004
}
1005
1006
static inline void rgb48ToUV(uint8_t *dstU, uint8_t *dstV,
1007 efc034cc Ramiro Polla
                             const uint8_t *src1, const uint8_t *src2,
1008 3483d877 Eli Friedman
                             long width, uint32_t *unused)
1009 e8417235 Kostya Shishkov
{
1010
    int i;
1011
    assert(src1==src2);
1012
    for (i = 0; i < width; i++) {
1013
        int r = src1[6*i + 0];
1014
        int g = src1[6*i + 2];
1015
        int b = src1[6*i + 4];
1016
1017
        dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1018
        dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1019
    }
1020
}
1021
1022
static inline void rgb48ToUV_half(uint8_t *dstU, uint8_t *dstV,
1023 efc034cc Ramiro Polla
                                  const uint8_t *src1, const uint8_t *src2,
1024 3483d877 Eli Friedman
                                  long width, uint32_t *unused)
1025 e8417235 Kostya Shishkov
{
1026
    int i;
1027
    assert(src1==src2);
1028
    for (i = 0; i < width; i++) {
1029
        int r= src1[12*i + 0] + src1[12*i + 6];
1030
        int g= src1[12*i + 2] + src1[12*i + 8];
1031
        int b= src1[12*i + 4] + src1[12*i + 10];
1032
1033
        dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1034
        dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1035
    }
1036
}
1037
1038 80704c47 Kostya Shishkov
#define BGR2Y(type, name, shr, shg, shb, maskr, maskg, maskb, RY, GY, BY, S)\
1039 c3ab0004 Ramiro Polla
static inline void name(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)\
1040 80704c47 Kostya Shishkov
{\
1041
    int i;\
1042 dd68318c Ramiro Polla
    for (i=0; i<width; i++) {\
1043 80704c47 Kostya Shishkov
        int b= (((const type*)src)[i]>>shb)&maskb;\
1044
        int g= (((const type*)src)[i]>>shg)&maskg;\
1045
        int r= (((const type*)src)[i]>>shr)&maskr;\
1046
\
1047
        dst[i]= (((RY)*r + (GY)*g + (BY)*b + (33<<((S)-1)))>>(S));\
1048
    }\
1049
}
1050
1051
BGR2Y(uint32_t, bgr32ToY,16, 0, 0, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY   , BY<< 8, RGB2YUV_SHIFT+8)
1052 66842fe8 Måns Rullgård
BGR2Y(uint32_t,bgr321ToY,16,16, 0, 0xFF00, 0x00FF, 0xFF00, RY    , GY<<8, BY    , RGB2YUV_SHIFT+8)
1053 80704c47 Kostya Shishkov
BGR2Y(uint32_t, rgb32ToY, 0, 0,16, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY   , BY<< 8, RGB2YUV_SHIFT+8)
1054 66842fe8 Måns Rullgård
BGR2Y(uint32_t,rgb321ToY, 0,16,16, 0xFF00, 0x00FF, 0xFF00, RY    , GY<<8, BY    , RGB2YUV_SHIFT+8)
1055 80704c47 Kostya Shishkov
BGR2Y(uint16_t, bgr16ToY, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RY<<11, GY<<5, BY    , RGB2YUV_SHIFT+8)
1056
BGR2Y(uint16_t, bgr15ToY, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RY<<10, GY<<5, BY    , RGB2YUV_SHIFT+7)
1057
BGR2Y(uint16_t, rgb16ToY, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RY    , GY<<5, BY<<11, RGB2YUV_SHIFT+8)
1058
BGR2Y(uint16_t, rgb15ToY, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RY    , GY<<5, BY<<10, RGB2YUV_SHIFT+7)
1059
1060 c3ab0004 Ramiro Polla
static inline void abgrToA(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1061 dd68318c Ramiro Polla
{
1062 80704c47 Kostya Shishkov
    int i;
1063 dd68318c Ramiro Polla
    for (i=0; i<width; i++) {
1064 80704c47 Kostya Shishkov
        dst[i]= src[4*i];
1065
    }
1066
}
1067
1068 66842fe8 Måns Rullgård
#define BGR2UV(type, name, shr, shg, shb, shp, maskr, maskg, maskb, RU, GU, BU, RV, GV, BV, S) \
1069 c3ab0004 Ramiro Polla
static inline void name(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1070 80704c47 Kostya Shishkov
{\
1071
    int i;\
1072 dd68318c Ramiro Polla
    for (i=0; i<width; i++) {\
1073 a1899e0a Ramiro Polla
        int b= ((((const type*)src)[i]>>shp)&maskb)>>shb;\
1074
        int g= ((((const type*)src)[i]>>shp)&maskg)>>shg;\
1075
        int r= ((((const type*)src)[i]>>shp)&maskr)>>shr;\
1076 80704c47 Kostya Shishkov
\
1077
        dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<((S)-1)))>>(S);\
1078
        dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<((S)-1)))>>(S);\
1079
    }\
1080
}\
1081 c3ab0004 Ramiro Polla
static inline void name ## _half(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1082 80704c47 Kostya Shishkov
{\
1083
    int i;\
1084 dd68318c Ramiro Polla
    for (i=0; i<width; i++) {\
1085 66842fe8 Måns Rullgård
        int pix0= ((const type*)src)[2*i+0]>>shp;\
1086
        int pix1= ((const type*)src)[2*i+1]>>shp;\
1087 80704c47 Kostya Shishkov
        int g= (pix0&~(maskr|maskb))+(pix1&~(maskr|maskb));\
1088
        int b= ((pix0+pix1-g)&(maskb|(2*maskb)))>>shb;\
1089
        int r= ((pix0+pix1-g)&(maskr|(2*maskr)))>>shr;\
1090
        g&= maskg|(2*maskg);\
1091
\
1092
        g>>=shg;\
1093
\
1094
        dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<(S)))>>((S)+1);\
1095
        dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<(S)))>>((S)+1);\
1096
    }\
1097
}
1098
1099 66842fe8 Måns Rullgård
BGR2UV(uint32_t, bgr32ToUV,16, 0, 0, 0, 0xFF0000, 0xFF00,   0x00FF, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1100
BGR2UV(uint32_t,bgr321ToUV,16, 0, 0, 8, 0xFF0000, 0xFF00,   0x00FF, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1101
BGR2UV(uint32_t, rgb32ToUV, 0, 0,16, 0,   0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1102
BGR2UV(uint32_t,rgb321ToUV, 0, 0,16, 8,   0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1103
BGR2UV(uint16_t, bgr16ToUV, 0, 0, 0, 0,   0x001F, 0x07E0,   0xF800, RU<<11, GU<<5, BU    , RV<<11, GV<<5, BV    , RGB2YUV_SHIFT+8)
1104
BGR2UV(uint16_t, bgr15ToUV, 0, 0, 0, 0,   0x001F, 0x03E0,   0x7C00, RU<<10, GU<<5, BU    , RV<<10, GV<<5, BV    , RGB2YUV_SHIFT+7)
1105
BGR2UV(uint16_t, rgb16ToUV, 0, 0, 0, 0,   0xF800, 0x07E0,   0x001F, RU    , GU<<5, BU<<11, RV    , GV<<5, BV<<11, RGB2YUV_SHIFT+8)
1106
BGR2UV(uint16_t, rgb15ToUV, 0, 0, 0, 0,   0x7C00, 0x03E0,   0x001F, RU    , GU<<5, BU<<10, RV    , GV<<5, BV<<10, RGB2YUV_SHIFT+7)
1107 80704c47 Kostya Shishkov
1108 c3ab0004 Ramiro Polla
static inline void palToY(uint8_t *dst, const uint8_t *src, long width, uint32_t *pal)
1109 80704c47 Kostya Shishkov
{
1110
    int i;
1111 dd68318c Ramiro Polla
    for (i=0; i<width; i++) {
1112 80704c47 Kostya Shishkov
        int d= src[i];
1113
1114
        dst[i]= pal[d] & 0xFF;
1115
    }
1116
}
1117
1118
static inline void palToUV(uint8_t *dstU, uint8_t *dstV,
1119
                           const uint8_t *src1, const uint8_t *src2,
1120 c3ab0004 Ramiro Polla
                           long width, uint32_t *pal)
1121 80704c47 Kostya Shishkov
{
1122
    int i;
1123
    assert(src1 == src2);
1124 dd68318c Ramiro Polla
    for (i=0; i<width; i++) {
1125 80704c47 Kostya Shishkov
        int p= pal[src1[i]];
1126
1127
        dstU[i]= p>>8;
1128
        dstV[i]= p>>16;
1129
    }
1130
}
1131
1132 c3ab0004 Ramiro Polla
static inline void monowhite2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1133 80704c47 Kostya Shishkov
{
1134
    int i, j;
1135 dd68318c Ramiro Polla
    for (i=0; i<width/8; i++) {
1136 80704c47 Kostya Shishkov
        int d= ~src[i];
1137
        for(j=0; j<8; j++)
1138
            dst[8*i+j]= ((d>>(7-j))&1)*255;
1139
    }
1140
}
1141
1142 c3ab0004 Ramiro Polla
static inline void monoblack2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1143 80704c47 Kostya Shishkov
{
1144
    int i, j;
1145 dd68318c Ramiro Polla
    for (i=0; i<width/8; i++) {
1146 80704c47 Kostya Shishkov
        int d= src[i];
1147
        for(j=0; j<8; j++)
1148
            dst[8*i+j]= ((d>>(7-j))&1)*255;
1149
    }
1150
}
1151
1152 dd409025 Ramiro Polla
//Note: we have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
1153 7630f2e0 Michael Niedermayer
//Plain C versions
1154 0fc29f27 Ramiro Polla
#if CONFIG_RUNTIME_CPUDETECT
1155
#  define COMPILE_C 1
1156
#  if   ARCH_X86
1157
#    define COMPILE_MMX     HAVE_MMX
1158
#    define COMPILE_MMX2    HAVE_MMX2
1159
#    define COMPILE_3DNOW   HAVE_AMD3DNOW
1160
#  elif ARCH_PPC
1161
#    define COMPILE_ALTIVEC HAVE_ALTIVEC
1162
#  endif
1163
#else /* CONFIG_RUNTIME_CPUDETECT */
1164
#  if   ARCH_X86
1165
#    if   HAVE_MMX2
1166
#      define COMPILE_MMX2  1
1167
#    elif HAVE_AMD3DNOW
1168
#      define COMPILE_3DNOW 1
1169
#    elif HAVE_MMX
1170
#      define COMPILE_MMX   1
1171
#    else
1172
#      define COMPILE_C     1
1173
#    endif
1174
#  elif ARCH_PPC && HAVE_ALTIVEC
1175
#    define COMPILE_ALTIVEC 1
1176
#  else
1177
#    define COMPILE_C       1
1178
#  endif
1179 726a959a Michael Niedermayer
#endif
1180
1181 8f028616 Ramiro Polla
#ifndef COMPILE_C
1182
#  define COMPILE_C 0
1183
#endif
1184
#ifndef COMPILE_MMX
1185
#  define COMPILE_MMX 0
1186
#endif
1187
#ifndef COMPILE_MMX2
1188
#  define COMPILE_MMX2 0
1189
#endif
1190
#ifndef COMPILE_3DNOW
1191
#  define COMPILE_3DNOW 0
1192
#endif
1193
#ifndef COMPILE_ALTIVEC
1194
#  define COMPILE_ALTIVEC 0
1195
#endif
1196
1197 94daf2e9 Ramiro Polla
#define COMPILE_TEMPLATE_MMX 0
1198
#define COMPILE_TEMPLATE_MMX2 0
1199
#define COMPILE_TEMPLATE_AMD3DNOW 0
1200
#define COMPILE_TEMPLATE_ALTIVEC 0
1201 726a959a Michael Niedermayer
1202 8f028616 Ramiro Polla
#if COMPILE_C
1203 7630f2e0 Michael Niedermayer
#define RENAME(a) a ## _C
1204
#include "swscale_template.c"
1205 726a959a Michael Niedermayer
#endif
1206 397c035e Michael Niedermayer
1207 8f028616 Ramiro Polla
#if COMPILE_ALTIVEC
1208 a2faa401 Romain Dolbeau
#undef RENAME
1209 94daf2e9 Ramiro Polla
#undef COMPILE_TEMPLATE_ALTIVEC
1210
#define COMPILE_TEMPLATE_ALTIVEC 1
1211 a2faa401 Romain Dolbeau
#define RENAME(a) a ## _altivec
1212
#include "swscale_template.c"
1213
#endif
1214
1215 b63f641e Aurelien Jacobs
#if ARCH_X86
1216 397c035e Michael Niedermayer
1217 7630f2e0 Michael Niedermayer
//MMX versions
1218 8f028616 Ramiro Polla
#if COMPILE_MMX
1219 7630f2e0 Michael Niedermayer
#undef RENAME
1220 94daf2e9 Ramiro Polla
#undef COMPILE_TEMPLATE_MMX
1221
#undef COMPILE_TEMPLATE_MMX2
1222
#undef COMPILE_TEMPLATE_AMD3DNOW
1223
#define COMPILE_TEMPLATE_MMX 1
1224
#define COMPILE_TEMPLATE_MMX2 0
1225
#define COMPILE_TEMPLATE_AMD3DNOW 0
1226 7630f2e0 Michael Niedermayer
#define RENAME(a) a ## _MMX
1227
#include "swscale_template.c"
1228 726a959a Michael Niedermayer
#endif
1229 7630f2e0 Michael Niedermayer
1230
//MMX2 versions
1231 8f028616 Ramiro Polla
#if COMPILE_MMX2
1232 7630f2e0 Michael Niedermayer
#undef RENAME
1233 94daf2e9 Ramiro Polla
#undef COMPILE_TEMPLATE_MMX
1234
#undef COMPILE_TEMPLATE_MMX2
1235
#undef COMPILE_TEMPLATE_AMD3DNOW
1236
#define COMPILE_TEMPLATE_MMX 1
1237
#define COMPILE_TEMPLATE_MMX2 1
1238
#define COMPILE_TEMPLATE_AMD3DNOW 0
1239 7630f2e0 Michael Niedermayer
#define RENAME(a) a ## _MMX2
1240
#include "swscale_template.c"
1241 726a959a Michael Niedermayer
#endif
1242 7630f2e0 Michael Niedermayer
1243
//3DNOW versions
1244 8f028616 Ramiro Polla
#if COMPILE_3DNOW
1245 7630f2e0 Michael Niedermayer
#undef RENAME
1246 94daf2e9 Ramiro Polla
#undef COMPILE_TEMPLATE_MMX
1247
#undef COMPILE_TEMPLATE_MMX2
1248
#undef COMPILE_TEMPLATE_AMD3DNOW
1249
#define COMPILE_TEMPLATE_MMX 1
1250
#define COMPILE_TEMPLATE_MMX2 0
1251
#define COMPILE_TEMPLATE_AMD3DNOW 1
1252 7630f2e0 Michael Niedermayer
#define RENAME(a) a ## _3DNow
1253
#include "swscale_template.c"
1254 726a959a Michael Niedermayer
#endif
1255 7630f2e0 Michael Niedermayer
1256 7a24ec50 Diego Biurrun
#endif //ARCH_X86
1257 7630f2e0 Michael Niedermayer
1258 a4388ebd Ramiro Polla
SwsFunc ff_getSwsFunc(SwsContext *c)
1259 14d5c18c Ramiro Polla
{
1260 8b1a6441 Diego Biurrun
#if CONFIG_RUNTIME_CPUDETECT
1261 40fa5140 Ramiro Polla
    int flags = c->flags;
1262 6a4970ab Diego Biurrun
1263 819ee683 Diego Biurrun
#if ARCH_X86
1264 c14731d8 Reimar Döffinger
    // ordered per speed fastest first
1265 14d5c18c Ramiro Polla
    if (flags & SWS_CPU_CAPS_MMX2) {
1266 40fa5140 Ramiro Polla
        sws_init_swScale_MMX2(c);
1267 221b804f Diego Biurrun
        return swScale_MMX2;
1268 14d5c18c Ramiro Polla
    } else if (flags & SWS_CPU_CAPS_3DNOW) {
1269 40fa5140 Ramiro Polla
        sws_init_swScale_3DNow(c);
1270 221b804f Diego Biurrun
        return swScale_3DNow;
1271 14d5c18c Ramiro Polla
    } else if (flags & SWS_CPU_CAPS_MMX) {
1272 40fa5140 Ramiro Polla
        sws_init_swScale_MMX(c);
1273 221b804f Diego Biurrun
        return swScale_MMX;
1274 730cb9da Stefano Sabatini
    } else {
1275
        sws_init_swScale_C(c);
1276
        return swScale_C;
1277 14d5c18c Ramiro Polla
    }
1278 730cb9da Stefano Sabatini
1279 28bf81c9 Michael Niedermayer
#else
1280 8f028616 Ramiro Polla
#if COMPILE_ALTIVEC
1281 14d5c18c Ramiro Polla
    if (flags & SWS_CPU_CAPS_ALTIVEC) {
1282 40fa5140 Ramiro Polla
        sws_init_swScale_altivec(c);
1283 221b804f Diego Biurrun
        return swScale_altivec;
1284 730cb9da Stefano Sabatini
    } else {
1285
        sws_init_swScale_C(c);
1286
        return swScale_C;
1287 14d5c18c Ramiro Polla
    }
1288 a2faa401 Romain Dolbeau
#endif
1289 730cb9da Stefano Sabatini
    sws_init_swScale_C(c);
1290
    return swScale_C;
1291 819ee683 Diego Biurrun
#endif /* ARCH_X86 */
1292 10a7d216 Ramiro Polla
#else //CONFIG_RUNTIME_CPUDETECT
1293 94daf2e9 Ramiro Polla
#if   COMPILE_TEMPLATE_MMX2
1294 40fa5140 Ramiro Polla
    sws_init_swScale_MMX2(c);
1295 221b804f Diego Biurrun
    return swScale_MMX2;
1296 94daf2e9 Ramiro Polla
#elif COMPILE_TEMPLATE_AMD3DNOW
1297 40fa5140 Ramiro Polla
    sws_init_swScale_3DNow(c);
1298 221b804f Diego Biurrun
    return swScale_3DNow;
1299 94daf2e9 Ramiro Polla
#elif COMPILE_TEMPLATE_MMX
1300 40fa5140 Ramiro Polla
    sws_init_swScale_MMX(c);
1301 221b804f Diego Biurrun
    return swScale_MMX;
1302 94daf2e9 Ramiro Polla
#elif COMPILE_TEMPLATE_ALTIVEC
1303 40fa5140 Ramiro Polla
    sws_init_swScale_altivec(c);
1304 221b804f Diego Biurrun
    return swScale_altivec;
1305 730cb9da Stefano Sabatini
#else
1306 80853cfc Stefano Sabatini
    sws_init_swScale_C(c);
1307
    return swScale_C;
1308 730cb9da Stefano Sabatini
#endif
1309
#endif //!CONFIG_RUNTIME_CPUDETECT
1310 31190492 Arpi
}
1311 7630f2e0 Michael Niedermayer
1312 cf1cd520 Ramiro Polla
static void copyPlane(const uint8_t *src, int srcStride,
1313
                      int srcSliceY, int srcSliceH, int width,
1314
                      uint8_t *dst, int dstStride)
1315 dd68318c Ramiro Polla
{
1316 cf1cd520 Ramiro Polla
    dst += dstStride * srcSliceY;
1317
    if (dstStride == srcStride && srcStride > 0) {
1318
        memcpy(dst, src, srcSliceH * dstStride);
1319
    } else {
1320 221b804f Diego Biurrun
        int i;
1321 dd68318c Ramiro Polla
        for (i=0; i<srcSliceH; i++) {
1322 5a901737 Ramiro Polla
            memcpy(dst, src, width);
1323
            src += srcStride;
1324
            dst += dstStride;
1325 221b804f Diego Biurrun
        }
1326
    }
1327 cf1cd520 Ramiro Polla
}
1328
1329
static int planarToNv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1330
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1331
{
1332
    uint8_t *dst = dstParam[1] + dstStride[1]*srcSliceY/2;
1333
1334
    copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
1335
              dstParam[0], dstStride[0]);
1336
1337 221b804f Diego Biurrun
    if (c->dstFormat == PIX_FMT_NV12)
1338 30c48a0a Benoit Fouet
        interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
1339 221b804f Diego Biurrun
    else
1340 30c48a0a Benoit Fouet
        interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
1341 221b804f Diego Biurrun
1342
    return srcSliceH;
1343 0d9f3d85 Arpi
}
1344
1345 3ece3e52 Stefano Sabatini
static int planarToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1346 dd68318c Ramiro Polla
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1347
{
1348 221b804f Diego Biurrun
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1349 0d9f3d85 Arpi
1350 30c48a0a Benoit Fouet
    yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
1351 fccb9b2b Michael Niedermayer
1352 221b804f Diego Biurrun
    return srcSliceH;
1353 0d9f3d85 Arpi
}
1354
1355 3ece3e52 Stefano Sabatini
static int planarToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1356 dd68318c Ramiro Polla
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1357
{
1358 221b804f Diego Biurrun
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1359 caeaabe7 Alex Beregszaszi
1360 30c48a0a Benoit Fouet
    yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
1361 caeaabe7 Alex Beregszaszi
1362 221b804f Diego Biurrun
    return srcSliceH;
1363 caeaabe7 Alex Beregszaszi
}
1364
1365 3ece3e52 Stefano Sabatini
static int yuv422pToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1366 dd68318c Ramiro Polla
                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
1367
{
1368 a6100f39 Baptiste Coudurier
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1369
1370
    yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1371
1372
    return srcSliceH;
1373
}
1374
1375 3ece3e52 Stefano Sabatini
static int yuv422pToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1376 dd68318c Ramiro Polla
                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
1377
{
1378 a6100f39 Baptiste Coudurier
    uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1379
1380
    yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1381
1382
    return srcSliceH;
1383
}
1384
1385 3ece3e52 Stefano Sabatini
static int yuyvToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1386 dd68318c Ramiro Polla
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1387
{
1388 0411072e Michael Niedermayer
    uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1389
    uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1390
    uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1391
1392
    yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1393
1394 4626ee1a Cédric Schieli
    if (dstParam[3])
1395
        fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1396
1397 0411072e Michael Niedermayer
    return srcSliceH;
1398
}
1399
1400 3ece3e52 Stefano Sabatini
static int yuyvToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1401 dd68318c Ramiro Polla
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1402
{
1403 0411072e Michael Niedermayer
    uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1404 72ef3dd7 Michael Niedermayer
    uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1405
    uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1406 0411072e Michael Niedermayer
1407
    yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1408
1409
    return srcSliceH;
1410
}
1411
1412 3ece3e52 Stefano Sabatini
static int uyvyToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1413 dd68318c Ramiro Polla
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1414
{
1415 0411072e Michael Niedermayer
    uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1416
    uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1417
    uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1418
1419
    uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1420
1421 4626ee1a Cédric Schieli
    if (dstParam[3])
1422
        fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1423
1424 0411072e Michael Niedermayer
    return srcSliceH;
1425
}
1426
1427 3ece3e52 Stefano Sabatini
static int uyvyToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1428 dd68318c Ramiro Polla
                               int srcSliceH, uint8_t* dstParam[], int dstStride[])
1429
{
1430 0411072e Michael Niedermayer
    uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1431 72ef3dd7 Michael Niedermayer
    uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1432
    uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1433 0411072e Michael Niedermayer
1434
    uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1435
1436
    return srcSliceH;
1437
}
1438
1439 df0743e0 Baptiste Coudurier
static void gray8aToPacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
1440
{
1441
    long i;
1442
    for (i=0; i<num_pixels; i++)
1443
        ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | (src[(i<<1)+1] << 24);
1444
}
1445
1446
static void gray8aToPacked32_1(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
1447
{
1448
    long i;
1449
1450
    for (i=0; i<num_pixels; i++)
1451
        ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | src[(i<<1)+1];
1452
}
1453
1454
static void gray8aToPacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
1455
{
1456
    long i;
1457
1458
    for (i=0; i<num_pixels; i++) {
1459
        //FIXME slow?
1460
        dst[0]= palette[src[i<<1]*4+0];
1461
        dst[1]= palette[src[i<<1]*4+1];
1462
        dst[2]= palette[src[i<<1]*4+2];
1463
        dst+= 3;
1464
    }
1465
}
1466
1467 3ece3e52 Stefano Sabatini
static int palToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1468 909f6594 Stefano Sabatini
                           int srcSliceH, uint8_t* dst[], int dstStride[])
1469 dd68318c Ramiro Polla
{
1470 f5a2c981 Carl Eugen Hoyos
    const enum PixelFormat srcFormat= c->srcFormat;
1471
    const enum PixelFormat dstFormat= c->dstFormat;
1472 49004617 Vitor Sessak
    void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
1473
                 const uint8_t *palette)=NULL;
1474
    int i;
1475
    uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1476 a959e247 Zuxy Meng
    const uint8_t *srcPtr= src[0];
1477 49004617 Vitor Sessak
1478 df0743e0 Baptiste Coudurier
    if (srcFormat == PIX_FMT_Y400A) {
1479
        switch (dstFormat) {
1480
        case PIX_FMT_RGB32  : conv = gray8aToPacked32; break;
1481
        case PIX_FMT_BGR32  : conv = gray8aToPacked32; break;
1482
        case PIX_FMT_BGR32_1: conv = gray8aToPacked32_1; break;
1483
        case PIX_FMT_RGB32_1: conv = gray8aToPacked32_1; break;
1484
        case PIX_FMT_RGB24  : conv = gray8aToPacked24; break;
1485
        case PIX_FMT_BGR24  : conv = gray8aToPacked24; break;
1486
        }
1487
    } else if (usePal(srcFormat)) {
1488 e36a545d Stefano Sabatini
        switch (dstFormat) {
1489 2b991422 Reinhard Tartler
        case PIX_FMT_RGB32  : conv = sws_convertPalette8ToPacked32; break;
1490
        case PIX_FMT_BGR32  : conv = sws_convertPalette8ToPacked32; break;
1491
        case PIX_FMT_BGR32_1: conv = sws_convertPalette8ToPacked32; break;
1492
        case PIX_FMT_RGB32_1: conv = sws_convertPalette8ToPacked32; break;
1493
        case PIX_FMT_RGB24  : conv = sws_convertPalette8ToPacked24; break;
1494
        case PIX_FMT_BGR24  : conv = sws_convertPalette8ToPacked24; break;
1495 e36a545d Stefano Sabatini
        }
1496 c0d1dc1c Stefano Sabatini
    }
1497 49004617 Vitor Sessak
1498 398c193d Stefano Sabatini
    if (!conv)
1499
        av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1500
               sws_format_name(srcFormat), sws_format_name(dstFormat));
1501
    else {
1502 e36a545d Stefano Sabatini
        for (i=0; i<srcSliceH; i++) {
1503
            conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
1504
            srcPtr+= srcStride[0];
1505
            dstPtr+= dstStride[0];
1506
        }
1507 398c193d Stefano Sabatini
    }
1508 49004617 Vitor Sessak
1509
    return srcSliceH;
1510
}
1511
1512 82e5f86b Stefano Sabatini
#define isRGBA32(x) (            \
1513
           (x) == PIX_FMT_ARGB   \
1514
        || (x) == PIX_FMT_RGBA   \
1515
        || (x) == PIX_FMT_BGRA   \
1516
        || (x) == PIX_FMT_ABGR   \
1517
        )
1518
1519 9990e426 Michael Niedermayer
/* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
1520 3ece3e52 Stefano Sabatini
static int rgbToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1521 909f6594 Stefano Sabatini
                           int srcSliceH, uint8_t* dst[], int dstStride[])
1522 dd68318c Ramiro Polla
{
1523 58e4b706 Carl Eugen Hoyos
    const enum PixelFormat srcFormat= c->srcFormat;
1524
    const enum PixelFormat dstFormat= c->dstFormat;
1525 30b61475 Stefano Sabatini
    const int srcBpp= (c->srcFormatBpp + 7) >> 3;
1526
    const int dstBpp= (c->dstFormatBpp + 7) >> 3;
1527
    const int srcId= c->srcFormatBpp >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
1528
    const int dstId= c->dstFormatBpp >> 2;
1529 221b804f Diego Biurrun
    void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
1530
1531 82e5f86b Stefano Sabatini
#define CONV_IS(src, dst) (srcFormat == PIX_FMT_##src && dstFormat == PIX_FMT_##dst)
1532
1533
    if (isRGBA32(srcFormat) && isRGBA32(dstFormat)) {
1534
        if (     CONV_IS(ABGR, RGBA)
1535
              || CONV_IS(ARGB, BGRA)
1536
              || CONV_IS(BGRA, ARGB)
1537
              || CONV_IS(RGBA, ABGR)) conv = shuffle_bytes_3210;
1538
        else if (CONV_IS(ABGR, ARGB)
1539
              || CONV_IS(ARGB, ABGR)) conv = shuffle_bytes_0321;
1540
        else if (CONV_IS(ABGR, BGRA)
1541
              || CONV_IS(ARGB, RGBA)) conv = shuffle_bytes_1230;
1542
        else if (CONV_IS(BGRA, RGBA)
1543
              || CONV_IS(RGBA, BGRA)) conv = shuffle_bytes_2103;
1544
        else if (CONV_IS(BGRA, ABGR)
1545
              || CONV_IS(RGBA, ARGB)) conv = shuffle_bytes_3012;
1546
    } else
1547 221b804f Diego Biurrun
    /* BGR -> BGR */
1548 3b1ee703 Michael Niedermayer
    if (  (isBGRinInt(srcFormat) && isBGRinInt(dstFormat))
1549
       || (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) {
1550 dd68318c Ramiro Polla
        switch(srcId | (dstId<<4)) {
1551 221b804f Diego Biurrun
        case 0x34: conv= rgb16to15; break;
1552
        case 0x36: conv= rgb24to15; break;
1553
        case 0x38: conv= rgb32to15; break;
1554
        case 0x43: conv= rgb15to16; break;
1555
        case 0x46: conv= rgb24to16; break;
1556
        case 0x48: conv= rgb32to16; break;
1557
        case 0x63: conv= rgb15to24; break;
1558
        case 0x64: conv= rgb16to24; break;
1559
        case 0x68: conv= rgb32to24; break;
1560
        case 0x83: conv= rgb15to32; break;
1561
        case 0x84: conv= rgb16to32; break;
1562
        case 0x86: conv= rgb24to32; break;
1563
        }
1564 3b1ee703 Michael Niedermayer
    } else if (  (isBGRinInt(srcFormat) && isRGBinInt(dstFormat))
1565
             || (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) {
1566 dd68318c Ramiro Polla
        switch(srcId | (dstId<<4)) {
1567 221b804f Diego Biurrun
        case 0x33: conv= rgb15tobgr15; break;
1568
        case 0x34: conv= rgb16tobgr15; break;
1569
        case 0x36: conv= rgb24tobgr15; break;
1570
        case 0x38: conv= rgb32tobgr15; break;
1571
        case 0x43: conv= rgb15tobgr16; break;
1572
        case 0x44: conv= rgb16tobgr16; break;
1573
        case 0x46: conv= rgb24tobgr16; break;
1574
        case 0x48: conv= rgb32tobgr16; break;
1575
        case 0x63: conv= rgb15tobgr24; break;
1576
        case 0x64: conv= rgb16tobgr24; break;
1577
        case 0x66: conv= rgb24tobgr24; break;
1578
        case 0x68: conv= rgb32tobgr24; break;
1579
        case 0x83: conv= rgb15tobgr32; break;
1580
        case 0x84: conv= rgb16tobgr32; break;
1581
        case 0x86: conv= rgb24tobgr32; break;
1582
        }
1583
    }
1584
1585 9a180390 Stefano Sabatini
    if (!conv) {
1586
        av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1587
               sws_format_name(srcFormat), sws_format_name(dstFormat));
1588
    } else {
1589 a959e247 Zuxy Meng
        const uint8_t *srcPtr= src[0];
1590 82e5f86b Stefano Sabatini
              uint8_t *dstPtr= dst[0];
1591
        if ((srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1) && !isRGBA32(dstFormat))
1592 9990e426 Michael Niedermayer
            srcPtr += ALT32_CORR;
1593
1594 82e5f86b Stefano Sabatini
        if ((dstFormat == PIX_FMT_RGB32_1 || dstFormat == PIX_FMT_BGR32_1) && !isRGBA32(srcFormat))
1595
            dstPtr += ALT32_CORR;
1596
1597 5efaf000 Peter Schlaile
        if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
1598 82e5f86b Stefano Sabatini
            conv(srcPtr, dstPtr + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1599 dd68318c Ramiro Polla
        else {
1600 c4ca31d0 Benoit Fouet
            int i;
1601 82e5f86b Stefano Sabatini
            dstPtr += dstStride[0]*srcSliceY;
1602 c4ca31d0 Benoit Fouet
1603 dd68318c Ramiro Polla
            for (i=0; i<srcSliceH; i++) {
1604 c4ca31d0 Benoit Fouet
                conv(srcPtr, dstPtr, c->srcW*srcBpp);
1605
                srcPtr+= srcStride[0];
1606
                dstPtr+= dstStride[0];
1607
            }
1608 221b804f Diego Biurrun
        }
1609
    }
1610
    return srcSliceH;
1611 0d9f3d85 Arpi
}
1612
1613 3ece3e52 Stefano Sabatini
static int bgr24ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1614 dd68318c Ramiro Polla
                              int srcSliceH, uint8_t* dst[], int dstStride[])
1615
{
1616 221b804f Diego Biurrun
    rgb24toyv12(
1617
        src[0],
1618
        dst[0]+ srcSliceY    *dstStride[0],
1619
        dst[1]+(srcSliceY>>1)*dstStride[1],
1620
        dst[2]+(srcSliceY>>1)*dstStride[2],
1621
        c->srcW, srcSliceH,
1622
        dstStride[0], dstStride[1], srcStride[0]);
1623 08218e6d Cédric Schieli
    if (dst[3])
1624
        fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1625 221b804f Diego Biurrun
    return srcSliceH;
1626 ec22603f Michael Niedermayer
}
1627
1628 3ece3e52 Stefano Sabatini
static int yvu9ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1629 dd68318c Ramiro Polla
                             int srcSliceH, uint8_t* dst[], int dstStride[])
1630
{
1631 cf1cd520 Ramiro Polla
    copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
1632
              dst[0], dstStride[0]);
1633 221b804f Diego Biurrun
1634 0132ab80 Ramiro Polla
    planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
1635
             srcSliceH >> 2, srcStride[1], dstStride[1]);
1636
    planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
1637
             srcSliceH >> 2, srcStride[2], dstStride[2]);
1638 08218e6d Cédric Schieli
    if (dst[3])
1639
        fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1640 221b804f Diego Biurrun
    return srcSliceH;
1641 b241cbf2 Michael Niedermayer
}
1642
1643 b6654a54 Michael Niedermayer
/* unscaled copy like stuff (assumes nearly identical formats) */
1644 3ece3e52 Stefano Sabatini
static int packedCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1645 909f6594 Stefano Sabatini
                             int srcSliceH, uint8_t* dst[], int dstStride[])
1646 bc5a0444 Luca Barbato
{
1647
    if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1648
        memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
1649 dd68318c Ramiro Polla
    else {
1650 bc5a0444 Luca Barbato
        int i;
1651 a959e247 Zuxy Meng
        const uint8_t *srcPtr= src[0];
1652 bc5a0444 Luca Barbato
        uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1653
        int length=0;
1654 221b804f Diego Biurrun
1655 bc5a0444 Luca Barbato
        /* universal length finder */
1656
        while(length+c->srcW <= FFABS(dstStride[0])
1657
           && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
1658 fcc402b1 Luca Barbato
        assert(length!=0);
1659 2d35ae56 Luca Barbato
1660 dd68318c Ramiro Polla
        for (i=0; i<srcSliceH; i++) {
1661 bc5a0444 Luca Barbato
            memcpy(dstPtr, srcPtr, length);
1662
            srcPtr+= srcStride[0];
1663
            dstPtr+= dstStride[0];
1664 221b804f Diego Biurrun
        }
1665 bc5a0444 Luca Barbato
    }
1666 2d35ae56 Luca Barbato
    return srcSliceH;
1667
}
1668 bc5a0444 Luca Barbato
1669 3ece3e52 Stefano Sabatini
static int planarCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1670 909f6594 Stefano Sabatini
                             int srcSliceH, uint8_t* dst[], int dstStride[])
1671 2d35ae56 Luca Barbato
{
1672 61a3f379 Michael Niedermayer
    int plane, i, j;
1673 dd68318c Ramiro Polla
    for (plane=0; plane<4; plane++) {
1674 6268f55b Cédric Schieli
        int length= (plane==0 || plane==3) ? c->srcW  : -((-c->srcW  )>>c->chrDstHSubSample);
1675
        int y=      (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
1676
        int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
1677 a959e247 Zuxy Meng
        const uint8_t *srcPtr= src[plane];
1678 61a3f379 Michael Niedermayer
        uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
1679 2d35ae56 Luca Barbato
1680 20ddf5a8 Reimar Döffinger
        if (!dst[plane]) continue;
1681 6f348086 Reimar Döffinger
        // ignore palette for GRAY8
1682
        if (plane == 1 && !dst[2]) continue;
1683 dd68318c Ramiro Polla
        if (!src[plane] || (plane == 1 && !src[2])) {
1684 61a3f379 Michael Niedermayer
            if(is16BPS(c->dstFormat))
1685
                length*=2;
1686 6268f55b Cédric Schieli
            fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
1687 dd68318c Ramiro Polla
        } else {
1688
            if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)) {
1689 61a3f379 Michael Niedermayer
                if (!isBE(c->srcFormat)) srcPtr++;
1690 dd68318c Ramiro Polla
                for (i=0; i<height; i++) {
1691 61a3f379 Michael Niedermayer
                    for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
1692
                    srcPtr+= srcStride[plane];
1693
                    dstPtr+= dstStride[plane];
1694
                }
1695 dd68318c Ramiro Polla
            } else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)) {
1696
                for (i=0; i<height; i++) {
1697
                    for (j=0; j<length; j++) {
1698 61a3f379 Michael Niedermayer
                        dstPtr[ j<<1   ] = srcPtr[j];
1699
                        dstPtr[(j<<1)+1] = srcPtr[j];
1700
                    }
1701
                    srcPtr+= srcStride[plane];
1702
                    dstPtr+= dstStride[plane];
1703
                }
1704 dd68318c Ramiro Polla
            } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
1705
                  && isBE(c->srcFormat) != isBE(c->dstFormat)) {
1706 61a3f379 Michael Niedermayer
1707 dd68318c Ramiro Polla
                for (i=0; i<height; i++) {
1708 61a3f379 Michael Niedermayer
                    for (j=0; j<length; j++)
1709 6a01eab7 Måns Rullgård
                        ((uint16_t*)dstPtr)[j] = av_bswap16(((const uint16_t*)srcPtr)[j]);
1710 61a3f379 Michael Niedermayer
                    srcPtr+= srcStride[plane];
1711
                    dstPtr+= dstStride[plane];
1712
                }
1713 e593826b Martin Storsjö
            } else if (dstStride[plane] == srcStride[plane] &&
1714
                       srcStride[plane] > 0 && srcStride[plane] == length) {
1715
                memcpy(dst[plane] + dstStride[plane]*y, src[plane],
1716
                       height*dstStride[plane]);
1717 c4e2b826 Martin Storsjö
            } else {
1718 61a3f379 Michael Niedermayer
                if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
1719
                    length*=2;
1720 dd68318c Ramiro Polla
                for (i=0; i<height; i++) {
1721 bc5a0444 Luca Barbato
                    memcpy(dstPtr, srcPtr, length);
1722
                    srcPtr+= srcStride[plane];
1723
                    dstPtr+= dstStride[plane];
1724 221b804f Diego Biurrun
                }
1725
            }
1726
        }
1727 bc5a0444 Luca Barbato
    }
1728 221b804f Diego Biurrun
    return srcSliceH;
1729 37079906 Michael Niedermayer
}
1730 28bf81c9 Michael Niedermayer
1731 a4388ebd Ramiro Polla
int ff_hardcodedcpuflags(void)
1732 dd68318c Ramiro Polla
{
1733 a4388ebd Ramiro Polla
    int flags = 0;
1734 94daf2e9 Ramiro Polla
#if   COMPILE_TEMPLATE_MMX2
1735 221b804f Diego Biurrun
    flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
1736 94daf2e9 Ramiro Polla
#elif COMPILE_TEMPLATE_AMD3DNOW
1737 221b804f Diego Biurrun
    flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
1738 94daf2e9 Ramiro Polla
#elif COMPILE_TEMPLATE_MMX
1739 221b804f Diego Biurrun
    flags |= SWS_CPU_CAPS_MMX;
1740 94daf2e9 Ramiro Polla
#elif COMPILE_TEMPLATE_ALTIVEC
1741 221b804f Diego Biurrun
    flags |= SWS_CPU_CAPS_ALTIVEC;
1742 b63f641e Aurelien Jacobs
#elif ARCH_BFIN
1743 d3f3eea9 Marc Hoffman
    flags |= SWS_CPU_CAPS_BFIN;
1744 516b1f82 Michael Niedermayer
#endif
1745 a4388ebd Ramiro Polla
    return flags;
1746
}
1747
1748
void ff_get_unscaled_swscale(SwsContext *c)
1749
{
1750
    const enum PixelFormat srcFormat = c->srcFormat;
1751
    const enum PixelFormat dstFormat = c->dstFormat;
1752
    const int flags = c->flags;
1753
    const int dstH = c->dstH;
1754
    int needsDither;
1755 221b804f Diego Biurrun
1756 60222557 Michael Niedermayer
    needsDither= isAnyRGB(dstFormat)
1757 aa2ead82 Stefano Sabatini
        &&  c->dstFormatBpp < 24
1758 60222557 Michael Niedermayer
        && (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
1759 221b804f Diego Biurrun
1760 59398ea9 Ramiro Polla
    /* yv12_to_nv12 */
1761
    if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
1762 3ece3e52 Stefano Sabatini
        c->swScale= planarToNv12Wrapper;
1763 59398ea9 Ramiro Polla
    }
1764
    /* yuv2bgr */
1765 60222557 Michael Niedermayer
    if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && isAnyRGB(dstFormat)
1766 59398ea9 Ramiro Polla
        && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
1767
        c->swScale= ff_yuv2rgb_get_func_ptr(c);
1768
    }
1769 6a4970ab Diego Biurrun
1770 59398ea9 Ramiro Polla
    if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
1771 3ece3e52 Stefano Sabatini
        c->swScale= yvu9ToYv12Wrapper;
1772 59398ea9 Ramiro Polla
    }
1773
1774
    /* bgr24toYV12 */
1775
    if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
1776 3ece3e52 Stefano Sabatini
        c->swScale= bgr24ToYv12Wrapper;
1777 59398ea9 Ramiro Polla
1778
    /* RGB/BGR -> RGB/BGR (no dither needed forms) */
1779 60222557 Michael Niedermayer
    if (   isAnyRGB(srcFormat)
1780
        && isAnyRGB(dstFormat)
1781 59398ea9 Ramiro Polla
        && srcFormat != PIX_FMT_BGR8      && dstFormat != PIX_FMT_BGR8
1782
        && srcFormat != PIX_FMT_RGB8      && dstFormat != PIX_FMT_RGB8
1783
        && srcFormat != PIX_FMT_BGR4      && dstFormat != PIX_FMT_BGR4
1784
        && srcFormat != PIX_FMT_RGB4      && dstFormat != PIX_FMT_RGB4
1785
        && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
1786
        && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
1787
        && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
1788
        && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
1789
        && srcFormat != PIX_FMT_RGB48LE   && dstFormat != PIX_FMT_RGB48LE
1790
        && srcFormat != PIX_FMT_RGB48BE   && dstFormat != PIX_FMT_RGB48BE
1791
        && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
1792 3ece3e52 Stefano Sabatini
        c->swScale= rgbToRgbWrapper;
1793 59398ea9 Ramiro Polla
1794
    if ((usePal(srcFormat) && (
1795
        dstFormat == PIX_FMT_RGB32   ||
1796
        dstFormat == PIX_FMT_RGB32_1 ||
1797
        dstFormat == PIX_FMT_RGB24   ||
1798
        dstFormat == PIX_FMT_BGR32   ||
1799
        dstFormat == PIX_FMT_BGR32_1 ||
1800
        dstFormat == PIX_FMT_BGR24)))
1801 3ece3e52 Stefano Sabatini
        c->swScale= palToRgbWrapper;
1802 59398ea9 Ramiro Polla
1803
    if (srcFormat == PIX_FMT_YUV422P) {
1804
        if (dstFormat == PIX_FMT_YUYV422)
1805 3ece3e52 Stefano Sabatini
            c->swScale= yuv422pToYuy2Wrapper;
1806 59398ea9 Ramiro Polla
        else if (dstFormat == PIX_FMT_UYVY422)
1807 3ece3e52 Stefano Sabatini
            c->swScale= yuv422pToUyvyWrapper;
1808 59398ea9 Ramiro Polla
    }
1809 221b804f Diego Biurrun
1810 59398ea9 Ramiro Polla
    /* LQ converters if -sws 0 or -sws 4*/
1811
    if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
1812
        /* yv12_to_yuy2 */
1813
        if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
1814 a6100f39 Baptiste Coudurier
            if (dstFormat == PIX_FMT_YUYV422)
1815 3ece3e52 Stefano Sabatini
                c->swScale= planarToYuy2Wrapper;
1816 a6100f39 Baptiste Coudurier
            else if (dstFormat == PIX_FMT_UYVY422)
1817 3ece3e52 Stefano Sabatini
                c->swScale= planarToUyvyWrapper;
1818 a6100f39 Baptiste Coudurier
        }
1819 59398ea9 Ramiro Polla
    }
1820
    if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
1821 3ece3e52 Stefano Sabatini
        c->swScale= yuyvToYuv420Wrapper;
1822 59398ea9 Ramiro Polla
    if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
1823 3ece3e52 Stefano Sabatini
        c->swScale= uyvyToYuv420Wrapper;
1824 59398ea9 Ramiro Polla
    if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
1825 3ece3e52 Stefano Sabatini
        c->swScale= yuyvToYuv422Wrapper;
1826 59398ea9 Ramiro Polla
    if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
1827 3ece3e52 Stefano Sabatini
        c->swScale= uyvyToYuv422Wrapper;
1828 ec22603f Michael Niedermayer
1829 8f028616 Ramiro Polla
#if COMPILE_ALTIVEC
1830 59398ea9 Ramiro Polla
    if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
1831
        !(c->flags & SWS_BITEXACT) &&
1832
        srcFormat == PIX_FMT_YUV420P) {
1833
        // unscaled YV12 -> packed YUV, we want speed
1834
        if (dstFormat == PIX_FMT_YUYV422)
1835
            c->swScale= yv12toyuy2_unscaled_altivec;
1836
        else if (dstFormat == PIX_FMT_UYVY422)
1837
            c->swScale= yv12touyvy_unscaled_altivec;
1838
    }
1839 b71cf33c Romain Dolbeau
#endif
1840
1841 59398ea9 Ramiro Polla
    /* simple copy */
1842
    if (  srcFormat == dstFormat
1843
        || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
1844
        || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
1845
        || (isPlanarYUV(srcFormat) && isGray(dstFormat))
1846
        || (isPlanarYUV(dstFormat) && isGray(srcFormat))
1847
        || (isGray(dstFormat) && isGray(srcFormat))
1848
        || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
1849
            && c->chrDstHSubSample == c->chrSrcHSubSample
1850
            && c->chrDstVSubSample == c->chrSrcVSubSample
1851
            && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
1852
            && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
1853
    {
1854
        if (isPacked(c->srcFormat))
1855 3ece3e52 Stefano Sabatini
            c->swScale= packedCopyWrapper;
1856 59398ea9 Ramiro Polla
        else /* Planar YUV or gray */
1857 3ece3e52 Stefano Sabatini
            c->swScale= planarCopyWrapper;
1858 59398ea9 Ramiro Polla
    }
1859 b63f641e Aurelien Jacobs
#if ARCH_BFIN
1860 59398ea9 Ramiro Polla
    if (flags & SWS_CPU_CAPS_BFIN)
1861
        ff_bfin_get_unscaled_swscale (c);
1862 1ebbfe15 Marc Hoffman
#endif
1863 28bf81c9 Michael Niedermayer
}
1864
1865 a959e247 Zuxy Meng
static void reset_ptr(const uint8_t* src[], int format)
1866 dd68318c Ramiro Polla
{
1867 b87fae9f Michael Niedermayer
    if(!isALPHA(format))
1868
        src[3]=NULL;
1869 dd68318c Ramiro Polla
    if(!isPlanarYUV(format)) {
1870 b87fae9f Michael Niedermayer
        src[3]=src[2]=NULL;
1871 3e906f4c Stefano Sabatini
1872
        if (!usePal(format))
1873 b87fae9f Michael Niedermayer
            src[1]= NULL;
1874
    }
1875
}
1876
1877 a42c29fe Ramiro Polla
static int check_image_pointers(uint8_t *data[4], enum PixelFormat pix_fmt,
1878
                                const int linesizes[4])
1879
{
1880
    const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
1881
    int i;
1882
1883
    for (i = 0; i < 4; i++) {
1884
        int plane = desc->comp[i].plane;
1885
        if (!data[plane] || !linesizes[plane])
1886
            return 0;
1887
    }
1888
1889
    return 1;
1890
}
1891
1892 28bf81c9 Michael Niedermayer
/**
1893 56b69633 Diego Biurrun
 * swscale wrapper, so we don't need to export the SwsContext.
1894 f40c7dbb Diego Biurrun
 * Assumes planar YUV to be in YUV order instead of YVU.
1895 fccb9b2b Michael Niedermayer
 */
1896 01cc47dc David Conrad
int sws_scale(SwsContext *c, const uint8_t* const src[], const int srcStride[], int srcSliceY,
1897
              int srcSliceH, uint8_t* const dst[], const int dstStride[])
1898 dd68318c Ramiro Polla
{
1899 221b804f Diego Biurrun
    int i;
1900 a959e247 Zuxy Meng
    const uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
1901 b87fae9f Michael Niedermayer
    uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
1902 14623020 Michael Niedermayer
1903 1b168a42 Reimar Döffinger
    // do not mess up sliceDir if we have a "trailing" 0-size slice
1904
    if (srcSliceH == 0)
1905
        return 0;
1906
1907 a42c29fe Ramiro Polla
    if (!check_image_pointers(src, c->srcFormat, srcStride)) {
1908
        av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
1909
        return 0;
1910
    }
1911
    if (!check_image_pointers(dst, c->dstFormat, dstStride)) {
1912
        av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
1913
        return 0;
1914
    }
1915
1916 221b804f Diego Biurrun
    if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
1917 3f0bc115 Diego Biurrun
        av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
1918 221b804f Diego Biurrun
        return 0;
1919
    }
1920
    if (c->sliceDir == 0) {
1921
        if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
1922
    }
1923
1924 dd68318c Ramiro Polla
    if (usePal(c->srcFormat)) {
1925
        for (i=0; i<256; i++) {
1926 14623020 Michael Niedermayer
            int p, r, g, b,y,u,v;
1927 dd68318c Ramiro Polla
            if(c->srcFormat == PIX_FMT_PAL8) {
1928 e922e34c Stefano Sabatini
                p=((const uint32_t*)(src[1]))[i];
1929 14623020 Michael Niedermayer
                r= (p>>16)&0xFF;
1930
                g= (p>> 8)&0xFF;
1931
                b=  p     &0xFF;
1932 dd68318c Ramiro Polla
            } else if(c->srcFormat == PIX_FMT_RGB8) {
1933 14623020 Michael Niedermayer
                r= (i>>5    )*36;
1934
                g= ((i>>2)&7)*36;
1935
                b= (i&3     )*85;
1936 dd68318c Ramiro Polla
            } else if(c->srcFormat == PIX_FMT_BGR8) {
1937 14623020 Michael Niedermayer
                b= (i>>6    )*85;
1938
                g= ((i>>3)&7)*36;
1939
                r= (i&7     )*36;
1940 dd68318c Ramiro Polla
            } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
1941 14623020 Michael Niedermayer
                r= (i>>3    )*255;
1942
                g= ((i>>1)&3)*85;
1943
                b= (i&1     )*255;
1944 bd8154d0 Baptiste Coudurier
            } else if(c->srcFormat == PIX_FMT_GRAY8 || c->srcFormat == PIX_FMT_Y400A) {
1945 23bf3b2c Stefano Sabatini
                r = g = b = i;
1946 dd68318c Ramiro Polla
            } else {
1947 75add6fb Vitor Sessak
                assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
1948 14623020 Michael Niedermayer
                b= (i>>3    )*255;
1949
                g= ((i>>1)&3)*85;
1950
                r= (i&1     )*255;
1951
            }
1952 cbcb408f Michael Niedermayer
            y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
1953
            u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
1954
            v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
1955 49004617 Vitor Sessak
            c->pal_yuv[i]= y + (u<<8) + (v<<16);
1956 522ce957 Vitor Sessak
1957
            switch(c->dstFormat) {
1958
            case PIX_FMT_BGR32:
1959 a898cdc9 Måns Rullgård
#if !HAVE_BIGENDIAN
1960 522ce957 Vitor Sessak
            case PIX_FMT_RGB24:
1961
#endif
1962
                c->pal_rgb[i]=  r + (g<<8) + (b<<16);
1963
                break;
1964
            case PIX_FMT_BGR32_1:
1965 a898cdc9 Måns Rullgård
#if HAVE_BIGENDIAN
1966 522ce957 Vitor Sessak
            case PIX_FMT_BGR24:
1967
#endif
1968
                c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
1969
                break;
1970
            case PIX_FMT_RGB32_1:
1971 a898cdc9 Måns Rullgård
#if HAVE_BIGENDIAN
1972 522ce957 Vitor Sessak
            case PIX_FMT_RGB24:
1973
#endif
1974
                c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
1975
                break;
1976
            case PIX_FMT_RGB32:
1977 a898cdc9 Måns Rullgård
#if !HAVE_BIGENDIAN
1978 522ce957 Vitor Sessak
            case PIX_FMT_BGR24:
1979
#endif
1980
            default:
1981
                c->pal_rgb[i]=  b + (g<<8) + (r<<16);
1982
            }
1983 21c08a3f Michael Niedermayer
        }
1984 221b804f Diego Biurrun
    }
1985 21c08a3f Michael Niedermayer
1986 221b804f Diego Biurrun
    // copy strides, so they can safely be modified
1987
    if (c->sliceDir == 1) {
1988
        // slices go from top to bottom
1989 6208d676 Cédric Schieli
        int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
1990
        int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
1991 b87fae9f Michael Niedermayer
1992
        reset_ptr(src2, c->srcFormat);
1993 a959e247 Zuxy Meng
        reset_ptr((const uint8_t**)dst2, c->dstFormat);
1994 b87fae9f Michael Niedermayer
1995 749fac31 Ramiro Polla
        /* reset slice direction at end of frame */
1996
        if (srcSliceY + srcSliceH == c->srcH)
1997
            c->sliceDir = 0;
1998
1999 b87fae9f Michael Niedermayer
        return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
2000 221b804f Diego Biurrun
    } else {
2001
        // slices go from bottom to top => we flip the image internally
2002 6208d676 Cédric Schieli
        int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
2003
        int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
2004 221b804f Diego Biurrun
2005
        src2[0] += (srcSliceH-1)*srcStride[0];
2006 49004617 Vitor Sessak
        if (!usePal(c->srcFormat))
2007 221b804f Diego Biurrun
            src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
2008
        src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
2009 6208d676 Cédric Schieli
        src2[3] += (srcSliceH-1)*srcStride[3];
2010 b87fae9f Michael Niedermayer
        dst2[0] += ( c->dstH                      -1)*dstStride[0];
2011
        dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
2012
        dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
2013
        dst2[3] += ( c->dstH                      -1)*dstStride[3];
2014
2015
        reset_ptr(src2, c->srcFormat);
2016 a959e247 Zuxy Meng
        reset_ptr((const uint8_t**)dst2, c->dstFormat);
2017 221b804f Diego Biurrun
2018 749fac31 Ramiro Polla
        /* reset slice direction at end of frame */
2019
        if (!srcSliceY)
2020
            c->sliceDir = 0;
2021
2022 221b804f Diego Biurrun
        return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
2023
    }
2024 fccb9b2b Michael Niedermayer
}
2025
2026 c099766f Stefano Sabatini
#if LIBSWSCALE_VERSION_MAJOR < 1
2027 f28cc577 Alexis Ballier
int sws_scale_ordered(SwsContext *c, const uint8_t* const src[], int srcStride[], int srcSliceY,
2028 dd68318c Ramiro Polla
                      int srcSliceH, uint8_t* dst[], int dstStride[])
2029
{
2030 221b804f Diego Biurrun
    return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
2031 d4e24275 Michael Niedermayer
}
2032 c099766f Stefano Sabatini
#endif
2033 2b991422 Reinhard Tartler
2034
/* Convert the palette to the same packed 32-bit format as the palette */
2035
void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
2036
{
2037
    long i;
2038
2039
    for (i=0; i<num_pixels; i++)
2040
        ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i]];
2041
}
2042
2043
/* Palette format: ABCD -> dst format: ABC */
2044
void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
2045
{
2046
    long i;
2047
2048
    for (i=0; i<num_pixels; i++) {
2049
        //FIXME slow?
2050
        dst[0]= palette[src[i]*4+0];
2051
        dst[1]= palette[src[i]*4+1];
2052
        dst[2]= palette[src[i]*4+2];
2053
        dst+= 3;
2054
    }
2055
}