557b0ae52200edff58c70d8eba218de90193ec88
[platform/framework/web/crosswalk.git] / src / third_party / ffmpeg / libswscale / swscale_internal.h
1 /*
2  * Copyright (C) 2001-2011 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * 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  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #ifndef SWSCALE_SWSCALE_INTERNAL_H
22 #define SWSCALE_SWSCALE_INTERNAL_H
23
24 #include "config.h"
25
26 #if HAVE_ALTIVEC_H
27 #include <altivec.h>
28 #endif
29
30 #include "version.h"
31
32 #include "libavutil/avassert.h"
33 #include "libavutil/avutil.h"
34 #include "libavutil/common.h"
35 #include "libavutil/intreadwrite.h"
36 #include "libavutil/log.h"
37 #include "libavutil/pixfmt.h"
38 #include "libavutil/pixdesc.h"
39
40 #define STR(s) AV_TOSTRING(s) // AV_STRINGIFY is too long
41
42 #define YUVRGB_TABLE_HEADROOM 128
43
44 #define MAX_FILTER_SIZE SWS_MAX_FILTER_SIZE
45
46 #define DITHER1XBPP
47
48 #if HAVE_BIGENDIAN
49 #define ALT32_CORR (-1)
50 #else
51 #define ALT32_CORR   1
52 #endif
53
54 #if ARCH_X86_64
55 #   define APCK_PTR2  8
56 #   define APCK_COEF 16
57 #   define APCK_SIZE 24
58 #else
59 #   define APCK_PTR2  4
60 #   define APCK_COEF  8
61 #   define APCK_SIZE 16
62 #endif
63
64 struct SwsContext;
65
66 typedef enum SwsDither {
67     SWS_DITHER_NONE = 0,
68     SWS_DITHER_AUTO,
69     SWS_DITHER_BAYER,
70     SWS_DITHER_ED,
71     SWS_DITHER_A_DITHER,
72     SWS_DITHER_X_DITHER,
73     NB_SWS_DITHER,
74 } SwsDither;
75
76 typedef int (*SwsFunc)(struct SwsContext *context, const uint8_t *src[],
77                        int srcStride[], int srcSliceY, int srcSliceH,
78                        uint8_t *dst[], int dstStride[]);
79
80 /**
81  * Write one line of horizontally scaled data to planar output
82  * without any additional vertical scaling (or point-scaling).
83  *
84  * @param src     scaled source data, 15bit for 8-10bit output,
85  *                19-bit for 16bit output (in int32_t)
86  * @param dest    pointer to the output plane. For >8bit
87  *                output, this is in uint16_t
88  * @param dstW    width of destination in pixels
89  * @param dither  ordered dither array of type int16_t and size 8
90  * @param offset  Dither offset
91  */
92 typedef void (*yuv2planar1_fn)(const int16_t *src, uint8_t *dest, int dstW,
93                                const uint8_t *dither, int offset);
94
95 /**
96  * Write one line of horizontally scaled data to planar output
97  * with multi-point vertical scaling between input pixels.
98  *
99  * @param filter        vertical luma/alpha scaling coefficients, 12bit [0,4096]
100  * @param src           scaled luma (Y) or alpha (A) source data, 15bit for 8-10bit output,
101  *                      19-bit for 16bit output (in int32_t)
102  * @param filterSize    number of vertical input lines to scale
103  * @param dest          pointer to output plane. For >8bit
104  *                      output, this is in uint16_t
105  * @param dstW          width of destination pixels
106  * @param offset        Dither offset
107  */
108 typedef void (*yuv2planarX_fn)(const int16_t *filter, int filterSize,
109                                const int16_t **src, uint8_t *dest, int dstW,
110                                const uint8_t *dither, int offset);
111
112 /**
113  * Write one line of horizontally scaled chroma to interleaved output
114  * with multi-point vertical scaling between input pixels.
115  *
116  * @param c             SWS scaling context
117  * @param chrFilter     vertical chroma scaling coefficients, 12bit [0,4096]
118  * @param chrUSrc       scaled chroma (U) source data, 15bit for 8-10bit output,
119  *                      19-bit for 16bit output (in int32_t)
120  * @param chrVSrc       scaled chroma (V) source data, 15bit for 8-10bit output,
121  *                      19-bit for 16bit output (in int32_t)
122  * @param chrFilterSize number of vertical chroma input lines to scale
123  * @param dest          pointer to the output plane. For >8bit
124  *                      output, this is in uint16_t
125  * @param dstW          width of chroma planes
126  */
127 typedef void (*yuv2interleavedX_fn)(struct SwsContext *c,
128                                     const int16_t *chrFilter,
129                                     int chrFilterSize,
130                                     const int16_t **chrUSrc,
131                                     const int16_t **chrVSrc,
132                                     uint8_t *dest, int dstW);
133
134 /**
135  * Write one line of horizontally scaled Y/U/V/A to packed-pixel YUV/RGB
136  * output without any additional vertical scaling (or point-scaling). Note
137  * that this function may do chroma scaling, see the "uvalpha" argument.
138  *
139  * @param c       SWS scaling context
140  * @param lumSrc  scaled luma (Y) source data, 15bit for 8-10bit output,
141  *                19-bit for 16bit output (in int32_t)
142  * @param chrUSrc scaled chroma (U) source data, 15bit for 8-10bit output,
143  *                19-bit for 16bit output (in int32_t)
144  * @param chrVSrc scaled chroma (V) source data, 15bit for 8-10bit output,
145  *                19-bit for 16bit output (in int32_t)
146  * @param alpSrc  scaled alpha (A) source data, 15bit for 8-10bit output,
147  *                19-bit for 16bit output (in int32_t)
148  * @param dest    pointer to the output plane. For 16bit output, this is
149  *                uint16_t
150  * @param dstW    width of lumSrc and alpSrc in pixels, number of pixels
151  *                to write into dest[]
152  * @param uvalpha chroma scaling coefficient for the second line of chroma
153  *                pixels, either 2048 or 0. If 0, one chroma input is used
154  *                for 2 output pixels (or if the SWS_FLAG_FULL_CHR_INT flag
155  *                is set, it generates 1 output pixel). If 2048, two chroma
156  *                input pixels should be averaged for 2 output pixels (this
157  *                only happens if SWS_FLAG_FULL_CHR_INT is not set)
158  * @param y       vertical line number for this output. This does not need
159  *                to be used to calculate the offset in the destination,
160  *                but can be used to generate comfort noise using dithering
161  *                for some output formats.
162  */
163 typedef void (*yuv2packed1_fn)(struct SwsContext *c, const int16_t *lumSrc,
164                                const int16_t *chrUSrc[2],
165                                const int16_t *chrVSrc[2],
166                                const int16_t *alpSrc, uint8_t *dest,
167                                int dstW, int uvalpha, int y);
168 /**
169  * Write one line of horizontally scaled Y/U/V/A to packed-pixel YUV/RGB
170  * output by doing bilinear scaling between two input lines.
171  *
172  * @param c       SWS scaling context
173  * @param lumSrc  scaled luma (Y) source data, 15bit for 8-10bit output,
174  *                19-bit for 16bit output (in int32_t)
175  * @param chrUSrc scaled chroma (U) source data, 15bit for 8-10bit output,
176  *                19-bit for 16bit output (in int32_t)
177  * @param chrVSrc scaled chroma (V) source data, 15bit for 8-10bit output,
178  *                19-bit for 16bit output (in int32_t)
179  * @param alpSrc  scaled alpha (A) source data, 15bit for 8-10bit output,
180  *                19-bit for 16bit output (in int32_t)
181  * @param dest    pointer to the output plane. For 16bit output, this is
182  *                uint16_t
183  * @param dstW    width of lumSrc and alpSrc in pixels, number of pixels
184  *                to write into dest[]
185  * @param yalpha  luma/alpha scaling coefficients for the second input line.
186  *                The first line's coefficients can be calculated by using
187  *                4096 - yalpha
188  * @param uvalpha chroma scaling coefficient for the second input line. The
189  *                first line's coefficients can be calculated by using
190  *                4096 - uvalpha
191  * @param y       vertical line number for this output. This does not need
192  *                to be used to calculate the offset in the destination,
193  *                but can be used to generate comfort noise using dithering
194  *                for some output formats.
195  */
196 typedef void (*yuv2packed2_fn)(struct SwsContext *c, const int16_t *lumSrc[2],
197                                const int16_t *chrUSrc[2],
198                                const int16_t *chrVSrc[2],
199                                const int16_t *alpSrc[2],
200                                uint8_t *dest,
201                                int dstW, int yalpha, int uvalpha, int y);
202 /**
203  * Write one line of horizontally scaled Y/U/V/A to packed-pixel YUV/RGB
204  * output by doing multi-point vertical scaling between input pixels.
205  *
206  * @param c             SWS scaling context
207  * @param lumFilter     vertical luma/alpha scaling coefficients, 12bit [0,4096]
208  * @param lumSrc        scaled luma (Y) source data, 15bit for 8-10bit output,
209  *                      19-bit for 16bit output (in int32_t)
210  * @param lumFilterSize number of vertical luma/alpha input lines to scale
211  * @param chrFilter     vertical chroma scaling coefficients, 12bit [0,4096]
212  * @param chrUSrc       scaled chroma (U) source data, 15bit for 8-10bit output,
213  *                      19-bit for 16bit output (in int32_t)
214  * @param chrVSrc       scaled chroma (V) source data, 15bit for 8-10bit output,
215  *                      19-bit for 16bit output (in int32_t)
216  * @param chrFilterSize number of vertical chroma input lines to scale
217  * @param alpSrc        scaled alpha (A) source data, 15bit for 8-10bit output,
218  *                      19-bit for 16bit output (in int32_t)
219  * @param dest          pointer to the output plane. For 16bit output, this is
220  *                      uint16_t
221  * @param dstW          width of lumSrc and alpSrc in pixels, number of pixels
222  *                      to write into dest[]
223  * @param y             vertical line number for this output. This does not need
224  *                      to be used to calculate the offset in the destination,
225  *                      but can be used to generate comfort noise using dithering
226  *                      or some output formats.
227  */
228 typedef void (*yuv2packedX_fn)(struct SwsContext *c, const int16_t *lumFilter,
229                                const int16_t **lumSrc, int lumFilterSize,
230                                const int16_t *chrFilter,
231                                const int16_t **chrUSrc,
232                                const int16_t **chrVSrc, int chrFilterSize,
233                                const int16_t **alpSrc, uint8_t *dest,
234                                int dstW, int y);
235
236 /**
237  * Write one line of horizontally scaled Y/U/V/A to YUV/RGB
238  * output by doing multi-point vertical scaling between input pixels.
239  *
240  * @param c             SWS scaling context
241  * @param lumFilter     vertical luma/alpha scaling coefficients, 12bit [0,4096]
242  * @param lumSrc        scaled luma (Y) source data, 15bit for 8-10bit output,
243  *                      19-bit for 16bit output (in int32_t)
244  * @param lumFilterSize number of vertical luma/alpha input lines to scale
245  * @param chrFilter     vertical chroma scaling coefficients, 12bit [0,4096]
246  * @param chrUSrc       scaled chroma (U) source data, 15bit for 8-10bit output,
247  *                      19-bit for 16bit output (in int32_t)
248  * @param chrVSrc       scaled chroma (V) source data, 15bit for 8-10bit output,
249  *                      19-bit for 16bit output (in int32_t)
250  * @param chrFilterSize number of vertical chroma input lines to scale
251  * @param alpSrc        scaled alpha (A) source data, 15bit for 8-10bit output,
252  *                      19-bit for 16bit output (in int32_t)
253  * @param dest          pointer to the output planes. For 16bit output, this is
254  *                      uint16_t
255  * @param dstW          width of lumSrc and alpSrc in pixels, number of pixels
256  *                      to write into dest[]
257  * @param y             vertical line number for this output. This does not need
258  *                      to be used to calculate the offset in the destination,
259  *                      but can be used to generate comfort noise using dithering
260  *                      or some output formats.
261  */
262 typedef void (*yuv2anyX_fn)(struct SwsContext *c, const int16_t *lumFilter,
263                             const int16_t **lumSrc, int lumFilterSize,
264                             const int16_t *chrFilter,
265                             const int16_t **chrUSrc,
266                             const int16_t **chrVSrc, int chrFilterSize,
267                             const int16_t **alpSrc, uint8_t **dest,
268                             int dstW, int y);
269
270 /* This struct should be aligned on at least a 32-byte boundary. */
271 typedef struct SwsContext {
272     /**
273      * info on struct for av_log
274      */
275     const AVClass *av_class;
276
277     /**
278      * Note that src, dst, srcStride, dstStride will be copied in the
279      * sws_scale() wrapper so they can be freely modified here.
280      */
281     SwsFunc swscale;
282     int srcW;                     ///< Width  of source      luma/alpha planes.
283     int srcH;                     ///< Height of source      luma/alpha planes.
284     int dstH;                     ///< Height of destination luma/alpha planes.
285     int chrSrcW;                  ///< Width  of source      chroma     planes.
286     int chrSrcH;                  ///< Height of source      chroma     planes.
287     int chrDstW;                  ///< Width  of destination chroma     planes.
288     int chrDstH;                  ///< Height of destination chroma     planes.
289     int lumXInc, chrXInc;
290     int lumYInc, chrYInc;
291     enum AVPixelFormat dstFormat; ///< Destination pixel format.
292     enum AVPixelFormat srcFormat; ///< Source      pixel format.
293     int dstFormatBpp;             ///< Number of bits per pixel of the destination pixel format.
294     int srcFormatBpp;             ///< Number of bits per pixel of the source      pixel format.
295     int dstBpc, srcBpc;
296     int chrSrcHSubSample;         ///< Binary logarithm of horizontal subsampling factor between luma/alpha and chroma planes in source      image.
297     int chrSrcVSubSample;         ///< Binary logarithm of vertical   subsampling factor between luma/alpha and chroma planes in source      image.
298     int chrDstHSubSample;         ///< Binary logarithm of horizontal subsampling factor between luma/alpha and chroma planes in destination image.
299     int chrDstVSubSample;         ///< Binary logarithm of vertical   subsampling factor between luma/alpha and chroma planes in destination image.
300     int vChrDrop;                 ///< Binary logarithm of extra vertical subsampling factor in source image chroma planes specified by user.
301     int sliceDir;                 ///< Direction that slices are fed to the scaler (1 = top-to-bottom, -1 = bottom-to-top).
302     double param[2];              ///< Input parameters for scaling algorithms that need them.
303
304     uint32_t pal_yuv[256];
305     uint32_t pal_rgb[256];
306
307     /**
308      * @name Scaled horizontal lines ring buffer.
309      * The horizontal scaler keeps just enough scaled lines in a ring buffer
310      * so they may be passed to the vertical scaler. The pointers to the
311      * allocated buffers for each line are duplicated in sequence in the ring
312      * buffer to simplify indexing and avoid wrapping around between lines
313      * inside the vertical scaler code. The wrapping is done before the
314      * vertical scaler is called.
315      */
316     //@{
317     int16_t **lumPixBuf;          ///< Ring buffer for scaled horizontal luma   plane lines to be fed to the vertical scaler.
318     int16_t **chrUPixBuf;         ///< Ring buffer for scaled horizontal chroma plane lines to be fed to the vertical scaler.
319     int16_t **chrVPixBuf;         ///< Ring buffer for scaled horizontal chroma plane lines to be fed to the vertical scaler.
320     int16_t **alpPixBuf;          ///< Ring buffer for scaled horizontal alpha  plane lines to be fed to the vertical scaler.
321     int vLumBufSize;              ///< Number of vertical luma/alpha lines allocated in the ring buffer.
322     int vChrBufSize;              ///< Number of vertical chroma     lines allocated in the ring buffer.
323     int lastInLumBuf;             ///< Last scaled horizontal luma/alpha line from source in the ring buffer.
324     int lastInChrBuf;             ///< Last scaled horizontal chroma     line from source in the ring buffer.
325     int lumBufIndex;              ///< Index in ring buffer of the last scaled horizontal luma/alpha line from source.
326     int chrBufIndex;              ///< Index in ring buffer of the last scaled horizontal chroma     line from source.
327     //@}
328
329     uint8_t *formatConvBuffer;
330
331     /**
332      * @name Horizontal and vertical filters.
333      * To better understand the following fields, here is a pseudo-code of
334      * their usage in filtering a horizontal line:
335      * @code
336      * for (i = 0; i < width; i++) {
337      *     dst[i] = 0;
338      *     for (j = 0; j < filterSize; j++)
339      *         dst[i] += src[ filterPos[i] + j ] * filter[ filterSize * i + j ];
340      *     dst[i] >>= FRAC_BITS; // The actual implementation is fixed-point.
341      * }
342      * @endcode
343      */
344     //@{
345     int16_t *hLumFilter;          ///< Array of horizontal filter coefficients for luma/alpha planes.
346     int16_t *hChrFilter;          ///< Array of horizontal filter coefficients for chroma     planes.
347     int16_t *vLumFilter;          ///< Array of vertical   filter coefficients for luma/alpha planes.
348     int16_t *vChrFilter;          ///< Array of vertical   filter coefficients for chroma     planes.
349     int32_t *hLumFilterPos;       ///< Array of horizontal filter starting positions for each dst[i] for luma/alpha planes.
350     int32_t *hChrFilterPos;       ///< Array of horizontal filter starting positions for each dst[i] for chroma     planes.
351     int32_t *vLumFilterPos;       ///< Array of vertical   filter starting positions for each dst[i] for luma/alpha planes.
352     int32_t *vChrFilterPos;       ///< Array of vertical   filter starting positions for each dst[i] for chroma     planes.
353     int hLumFilterSize;           ///< Horizontal filter size for luma/alpha pixels.
354     int hChrFilterSize;           ///< Horizontal filter size for chroma     pixels.
355     int vLumFilterSize;           ///< Vertical   filter size for luma/alpha pixels.
356     int vChrFilterSize;           ///< Vertical   filter size for chroma     pixels.
357     //@}
358
359     int lumMmxextFilterCodeSize;  ///< Runtime-generated MMXEXT horizontal fast bilinear scaler code size for luma/alpha planes.
360     int chrMmxextFilterCodeSize;  ///< Runtime-generated MMXEXT horizontal fast bilinear scaler code size for chroma planes.
361     uint8_t *lumMmxextFilterCode; ///< Runtime-generated MMXEXT horizontal fast bilinear scaler code for luma/alpha planes.
362     uint8_t *chrMmxextFilterCode; ///< Runtime-generated MMXEXT horizontal fast bilinear scaler code for chroma planes.
363
364     int canMMXEXTBeUsed;
365
366     int dstY;                     ///< Last destination vertical line output from last slice.
367     int flags;                    ///< Flags passed by the user to select scaler algorithm, optimizations, subsampling, etc...
368     void *yuvTable;             // pointer to the yuv->rgb table start so it can be freed()
369     // alignment ensures the offset can be added in a single
370     // instruction on e.g. ARM
371     DECLARE_ALIGNED(16, int, table_gV)[256 + 2*YUVRGB_TABLE_HEADROOM];
372     uint8_t *table_rV[256 + 2*YUVRGB_TABLE_HEADROOM];
373     uint8_t *table_gU[256 + 2*YUVRGB_TABLE_HEADROOM];
374     uint8_t *table_bU[256 + 2*YUVRGB_TABLE_HEADROOM];
375     DECLARE_ALIGNED(16, int32_t, input_rgb2yuv_table)[16+40*4]; // This table can contain both C and SIMD formatted values, the C vales are always at the XY_IDX points
376 #define RY_IDX 0
377 #define GY_IDX 1
378 #define BY_IDX 2
379 #define RU_IDX 3
380 #define GU_IDX 4
381 #define BU_IDX 5
382 #define RV_IDX 6
383 #define GV_IDX 7
384 #define BV_IDX 8
385 #define RGB2YUV_SHIFT 15
386
387     int *dither_error[4];
388
389     //Colorspace stuff
390     int contrast, brightness, saturation;    // for sws_getColorspaceDetails
391     int srcColorspaceTable[4];
392     int dstColorspaceTable[4];
393     int srcRange;                 ///< 0 = MPG YUV range, 1 = JPG YUV range (source      image).
394     int dstRange;                 ///< 0 = MPG YUV range, 1 = JPG YUV range (destination image).
395     int src0Alpha;
396     int dst0Alpha;
397     int srcXYZ;
398     int dstXYZ;
399     int src_h_chr_pos;
400     int dst_h_chr_pos;
401     int src_v_chr_pos;
402     int dst_v_chr_pos;
403     int yuv2rgb_y_offset;
404     int yuv2rgb_y_coeff;
405     int yuv2rgb_v2r_coeff;
406     int yuv2rgb_v2g_coeff;
407     int yuv2rgb_u2g_coeff;
408     int yuv2rgb_u2b_coeff;
409
410 #define RED_DITHER            "0*8"
411 #define GREEN_DITHER          "1*8"
412 #define BLUE_DITHER           "2*8"
413 #define Y_COEFF               "3*8"
414 #define VR_COEFF              "4*8"
415 #define UB_COEFF              "5*8"
416 #define VG_COEFF              "6*8"
417 #define UG_COEFF              "7*8"
418 #define Y_OFFSET              "8*8"
419 #define U_OFFSET              "9*8"
420 #define V_OFFSET              "10*8"
421 #define LUM_MMX_FILTER_OFFSET "11*8"
422 #define CHR_MMX_FILTER_OFFSET "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)
423 #define DSTW_OFFSET           "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2"
424 #define ESP_OFFSET            "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+8"
425 #define VROUNDER_OFFSET       "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+16"
426 #define U_TEMP                "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+24"
427 #define V_TEMP                "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+32"
428 #define Y_TEMP                "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+40"
429 #define ALP_MMX_FILTER_OFFSET "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+48"
430 #define UV_OFF_PX             "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*3+48"
431 #define UV_OFF_BYTE           "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*3+56"
432 #define DITHER16              "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*3+64"
433 #define DITHER32              "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*3+80"
434 #define DITHER32_INT          (11*8+4*4*MAX_FILTER_SIZE*3+80) // value equal to above, used for checking that the struct hasn't been changed by mistake
435
436     DECLARE_ALIGNED(8, uint64_t, redDither);
437     DECLARE_ALIGNED(8, uint64_t, greenDither);
438     DECLARE_ALIGNED(8, uint64_t, blueDither);
439
440     DECLARE_ALIGNED(8, uint64_t, yCoeff);
441     DECLARE_ALIGNED(8, uint64_t, vrCoeff);
442     DECLARE_ALIGNED(8, uint64_t, ubCoeff);
443     DECLARE_ALIGNED(8, uint64_t, vgCoeff);
444     DECLARE_ALIGNED(8, uint64_t, ugCoeff);
445     DECLARE_ALIGNED(8, uint64_t, yOffset);
446     DECLARE_ALIGNED(8, uint64_t, uOffset);
447     DECLARE_ALIGNED(8, uint64_t, vOffset);
448     int32_t lumMmxFilter[4 * MAX_FILTER_SIZE];
449     int32_t chrMmxFilter[4 * MAX_FILTER_SIZE];
450     int dstW;                     ///< Width  of destination luma/alpha planes.
451     DECLARE_ALIGNED(8, uint64_t, esp);
452     DECLARE_ALIGNED(8, uint64_t, vRounder);
453     DECLARE_ALIGNED(8, uint64_t, u_temp);
454     DECLARE_ALIGNED(8, uint64_t, v_temp);
455     DECLARE_ALIGNED(8, uint64_t, y_temp);
456     int32_t alpMmxFilter[4 * MAX_FILTER_SIZE];
457     // alignment of these values is not necessary, but merely here
458     // to maintain the same offset across x8632 and x86-64. Once we
459     // use proper offset macros in the asm, they can be removed.
460     DECLARE_ALIGNED(8, ptrdiff_t, uv_off); ///< offset (in pixels) between u and v planes
461     DECLARE_ALIGNED(8, ptrdiff_t, uv_offx2); ///< offset (in bytes) between u and v planes
462     DECLARE_ALIGNED(8, uint16_t, dither16)[8];
463     DECLARE_ALIGNED(8, uint32_t, dither32)[8];
464
465     const uint8_t *chrDither8, *lumDither8;
466
467 #if HAVE_ALTIVEC
468     vector signed short   CY;
469     vector signed short   CRV;
470     vector signed short   CBU;
471     vector signed short   CGU;
472     vector signed short   CGV;
473     vector signed short   OY;
474     vector unsigned short CSHIFT;
475     vector signed short  *vYCoeffsBank, *vCCoeffsBank;
476 #endif
477
478     int use_mmx_vfilter;
479
480 /* pre defined color-spaces gamma */
481 #define XYZ_GAMMA (2.6f)
482 #define RGB_GAMMA (2.2f)
483     int16_t *xyzgamma;
484     int16_t *rgbgamma;
485     int16_t *xyzgammainv;
486     int16_t *rgbgammainv;
487     int16_t xyz2rgb_matrix[3][4];
488     int16_t rgb2xyz_matrix[3][4];
489
490     /* function pointers for swscale() */
491     yuv2planar1_fn yuv2plane1;
492     yuv2planarX_fn yuv2planeX;
493     yuv2interleavedX_fn yuv2nv12cX;
494     yuv2packed1_fn yuv2packed1;
495     yuv2packed2_fn yuv2packed2;
496     yuv2packedX_fn yuv2packedX;
497     yuv2anyX_fn yuv2anyX;
498
499     /// Unscaled conversion of luma plane to YV12 for horizontal scaler.
500     void (*lumToYV12)(uint8_t *dst, const uint8_t *src, const uint8_t *src2, const uint8_t *src3,
501                       int width, uint32_t *pal);
502     /// Unscaled conversion of alpha plane to YV12 for horizontal scaler.
503     void (*alpToYV12)(uint8_t *dst, const uint8_t *src, const uint8_t *src2, const uint8_t *src3,
504                       int width, uint32_t *pal);
505     /// Unscaled conversion of chroma planes to YV12 for horizontal scaler.
506     void (*chrToYV12)(uint8_t *dstU, uint8_t *dstV,
507                       const uint8_t *src1, const uint8_t *src2, const uint8_t *src3,
508                       int width, uint32_t *pal);
509
510     /**
511      * Functions to read planar input, such as planar RGB, and convert
512      * internally to Y/UV/A.
513      */
514     /** @{ */
515     void (*readLumPlanar)(uint8_t *dst, const uint8_t *src[4], int width, int32_t *rgb2yuv);
516     void (*readChrPlanar)(uint8_t *dstU, uint8_t *dstV, const uint8_t *src[4],
517                           int width, int32_t *rgb2yuv);
518     void (*readAlpPlanar)(uint8_t *dst, const uint8_t *src[4], int width, int32_t *rgb2yuv);
519     /** @} */
520
521     /**
522      * Scale one horizontal line of input data using a bilinear filter
523      * to produce one line of output data. Compared to SwsContext->hScale(),
524      * please take note of the following caveats when using these:
525      * - Scaling is done using only 7bit instead of 14bit coefficients.
526      * - You can use no more than 5 input pixels to produce 4 output
527      *   pixels. Therefore, this filter should not be used for downscaling
528      *   by more than ~20% in width (because that equals more than 5/4th
529      *   downscaling and thus more than 5 pixels input per 4 pixels output).
530      * - In general, bilinear filters create artifacts during downscaling
531      *   (even when <20%), because one output pixel will span more than one
532      *   input pixel, and thus some pixels will need edges of both neighbor
533      *   pixels to interpolate the output pixel. Since you can use at most
534      *   two input pixels per output pixel in bilinear scaling, this is
535      *   impossible and thus downscaling by any size will create artifacts.
536      * To enable this type of scaling, set SWS_FLAG_FAST_BILINEAR
537      * in SwsContext->flags.
538      */
539     /** @{ */
540     void (*hyscale_fast)(struct SwsContext *c,
541                          int16_t *dst, int dstWidth,
542                          const uint8_t *src, int srcW, int xInc);
543     void (*hcscale_fast)(struct SwsContext *c,
544                          int16_t *dst1, int16_t *dst2, int dstWidth,
545                          const uint8_t *src1, const uint8_t *src2,
546                          int srcW, int xInc);
547     /** @} */
548
549     /**
550      * Scale one horizontal line of input data using a filter over the input
551      * lines, to produce one (differently sized) line of output data.
552      *
553      * @param dst        pointer to destination buffer for horizontally scaled
554      *                   data. If the number of bits per component of one
555      *                   destination pixel (SwsContext->dstBpc) is <= 10, data
556      *                   will be 15bpc in 16bits (int16_t) width. Else (i.e.
557      *                   SwsContext->dstBpc == 16), data will be 19bpc in
558      *                   32bits (int32_t) width.
559      * @param dstW       width of destination image
560      * @param src        pointer to source data to be scaled. If the number of
561      *                   bits per component of a source pixel (SwsContext->srcBpc)
562      *                   is 8, this is 8bpc in 8bits (uint8_t) width. Else
563      *                   (i.e. SwsContext->dstBpc > 8), this is native depth
564      *                   in 16bits (uint16_t) width. In other words, for 9-bit
565      *                   YUV input, this is 9bpc, for 10-bit YUV input, this is
566      *                   10bpc, and for 16-bit RGB or YUV, this is 16bpc.
567      * @param filter     filter coefficients to be used per output pixel for
568      *                   scaling. This contains 14bpp filtering coefficients.
569      *                   Guaranteed to contain dstW * filterSize entries.
570      * @param filterPos  position of the first input pixel to be used for
571      *                   each output pixel during scaling. Guaranteed to
572      *                   contain dstW entries.
573      * @param filterSize the number of input coefficients to be used (and
574      *                   thus the number of input pixels to be used) for
575      *                   creating a single output pixel. Is aligned to 4
576      *                   (and input coefficients thus padded with zeroes)
577      *                   to simplify creating SIMD code.
578      */
579     /** @{ */
580     void (*hyScale)(struct SwsContext *c, int16_t *dst, int dstW,
581                     const uint8_t *src, const int16_t *filter,
582                     const int32_t *filterPos, int filterSize);
583     void (*hcScale)(struct SwsContext *c, int16_t *dst, int dstW,
584                     const uint8_t *src, const int16_t *filter,
585                     const int32_t *filterPos, int filterSize);
586     /** @} */
587
588     /// Color range conversion function for luma plane if needed.
589     void (*lumConvertRange)(int16_t *dst, int width);
590     /// Color range conversion function for chroma planes if needed.
591     void (*chrConvertRange)(int16_t *dst1, int16_t *dst2, int width);
592
593     int needs_hcscale; ///< Set if there are chroma planes to be converted.
594
595     SwsDither dither;
596 } SwsContext;
597 //FIXME check init (where 0)
598
599 SwsFunc ff_yuv2rgb_get_func_ptr(SwsContext *c);
600 int ff_yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4],
601                              int fullRange, int brightness,
602                              int contrast, int saturation);
603 void ff_yuv2rgb_init_tables_ppc(SwsContext *c, const int inv_table[4],
604                                 int brightness, int contrast, int saturation);
605
606 void updateMMXDitherTables(SwsContext *c, int dstY, int lumBufIndex, int chrBufIndex,
607                            int lastInLumBuf, int lastInChrBuf);
608
609 av_cold void ff_sws_init_range_convert(SwsContext *c);
610
611 SwsFunc ff_yuv2rgb_init_x86(SwsContext *c);
612 SwsFunc ff_yuv2rgb_init_ppc(SwsContext *c);
613
614 #if FF_API_SWS_FORMAT_NAME
615 /**
616  * @deprecated Use av_get_pix_fmt_name() instead.
617  */
618 attribute_deprecated
619 const char *sws_format_name(enum AVPixelFormat format);
620 #endif
621
622 static av_always_inline int is16BPS(enum AVPixelFormat pix_fmt)
623 {
624     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
625     av_assert0(desc);
626     return desc->comp[0].depth_minus1 == 15;
627 }
628
629 static av_always_inline int is9_OR_10BPS(enum AVPixelFormat pix_fmt)
630 {
631     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
632     av_assert0(desc);
633     return desc->comp[0].depth_minus1 >= 8 && desc->comp[0].depth_minus1 <= 13;
634 }
635
636 #define isNBPS(x) is9_OR_10BPS(x)
637
638 static av_always_inline int isBE(enum AVPixelFormat pix_fmt)
639 {
640     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
641     av_assert0(desc);
642     return desc->flags & AV_PIX_FMT_FLAG_BE;
643 }
644
645 static av_always_inline int isYUV(enum AVPixelFormat pix_fmt)
646 {
647     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
648     av_assert0(desc);
649     return !(desc->flags & AV_PIX_FMT_FLAG_RGB) && desc->nb_components >= 2;
650 }
651
652 static av_always_inline int isPlanarYUV(enum AVPixelFormat pix_fmt)
653 {
654     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
655     av_assert0(desc);
656     return ((desc->flags & AV_PIX_FMT_FLAG_PLANAR) && isYUV(pix_fmt));
657 }
658
659 static av_always_inline int isRGB(enum AVPixelFormat pix_fmt)
660 {
661     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
662     av_assert0(desc);
663     return (desc->flags & AV_PIX_FMT_FLAG_RGB);
664 }
665
666 #if 0 // FIXME
667 #define isGray(x) \
668     (!(av_pix_fmt_desc_get(x)->flags & AV_PIX_FMT_FLAG_PAL) && \
669      av_pix_fmt_desc_get(x)->nb_components <= 2)
670 #else
671 #define isGray(x)                      \
672     ((x) == AV_PIX_FMT_GRAY8       ||  \
673      (x) == AV_PIX_FMT_Y400A       ||  \
674      (x) == AV_PIX_FMT_GRAY16BE    ||  \
675      (x) == AV_PIX_FMT_GRAY16LE)
676 #endif
677
678 #define isRGBinInt(x) \
679     (           \
680      (x) == AV_PIX_FMT_RGB48BE     ||  \
681      (x) == AV_PIX_FMT_RGB48LE     ||  \
682      (x) == AV_PIX_FMT_RGB32       ||  \
683      (x) == AV_PIX_FMT_RGB32_1     ||  \
684      (x) == AV_PIX_FMT_RGB24       ||  \
685      (x) == AV_PIX_FMT_RGB565BE    ||  \
686      (x) == AV_PIX_FMT_RGB565LE    ||  \
687      (x) == AV_PIX_FMT_RGB555BE    ||  \
688      (x) == AV_PIX_FMT_RGB555LE    ||  \
689      (x) == AV_PIX_FMT_RGB444BE    ||  \
690      (x) == AV_PIX_FMT_RGB444LE    ||  \
691      (x) == AV_PIX_FMT_RGB8        ||  \
692      (x) == AV_PIX_FMT_RGB4        ||  \
693      (x) == AV_PIX_FMT_RGB4_BYTE   ||  \
694      (x) == AV_PIX_FMT_RGBA64BE    ||  \
695      (x) == AV_PIX_FMT_RGBA64LE    ||  \
696      (x) == AV_PIX_FMT_MONOBLACK   ||  \
697      (x) == AV_PIX_FMT_MONOWHITE   \
698     )
699 #define isBGRinInt(x) \
700     (           \
701      (x) == AV_PIX_FMT_BGR48BE     ||  \
702      (x) == AV_PIX_FMT_BGR48LE     ||  \
703      (x) == AV_PIX_FMT_BGR32       ||  \
704      (x) == AV_PIX_FMT_BGR32_1     ||  \
705      (x) == AV_PIX_FMT_BGR24       ||  \
706      (x) == AV_PIX_FMT_BGR565BE    ||  \
707      (x) == AV_PIX_FMT_BGR565LE    ||  \
708      (x) == AV_PIX_FMT_BGR555BE    ||  \
709      (x) == AV_PIX_FMT_BGR555LE    ||  \
710      (x) == AV_PIX_FMT_BGR444BE    ||  \
711      (x) == AV_PIX_FMT_BGR444LE    ||  \
712      (x) == AV_PIX_FMT_BGR8        ||  \
713      (x) == AV_PIX_FMT_BGR4        ||  \
714      (x) == AV_PIX_FMT_BGR4_BYTE   ||  \
715      (x) == AV_PIX_FMT_BGRA64BE    ||  \
716      (x) == AV_PIX_FMT_BGRA64LE    ||  \
717      (x) == AV_PIX_FMT_MONOBLACK   ||  \
718      (x) == AV_PIX_FMT_MONOWHITE   \
719     )
720
721 #define isRGBinBytes(x) (           \
722            (x) == AV_PIX_FMT_RGB48BE     \
723         || (x) == AV_PIX_FMT_RGB48LE     \
724         || (x) == AV_PIX_FMT_RGBA64BE    \
725         || (x) == AV_PIX_FMT_RGBA64LE    \
726         || (x) == AV_PIX_FMT_RGBA        \
727         || (x) == AV_PIX_FMT_ARGB        \
728         || (x) == AV_PIX_FMT_RGB24       \
729     )
730 #define isBGRinBytes(x) (           \
731            (x) == AV_PIX_FMT_BGR48BE     \
732         || (x) == AV_PIX_FMT_BGR48LE     \
733         || (x) == AV_PIX_FMT_BGRA64BE    \
734         || (x) == AV_PIX_FMT_BGRA64LE    \
735         || (x) == AV_PIX_FMT_BGRA        \
736         || (x) == AV_PIX_FMT_ABGR        \
737         || (x) == AV_PIX_FMT_BGR24       \
738     )
739
740 #define isBayer(x) ( \
741            (x)==AV_PIX_FMT_BAYER_BGGR8    \
742         || (x)==AV_PIX_FMT_BAYER_BGGR16LE \
743         || (x)==AV_PIX_FMT_BAYER_BGGR16BE \
744         || (x)==AV_PIX_FMT_BAYER_RGGB8    \
745         || (x)==AV_PIX_FMT_BAYER_RGGB16LE \
746         || (x)==AV_PIX_FMT_BAYER_RGGB16BE \
747         || (x)==AV_PIX_FMT_BAYER_GBRG8    \
748         || (x)==AV_PIX_FMT_BAYER_GBRG16LE \
749         || (x)==AV_PIX_FMT_BAYER_GBRG16BE \
750         || (x)==AV_PIX_FMT_BAYER_GRBG8    \
751         || (x)==AV_PIX_FMT_BAYER_GRBG16LE \
752         || (x)==AV_PIX_FMT_BAYER_GRBG16BE \
753     )
754
755 #define isAnyRGB(x) \
756     (           \
757           isBayer(x)          ||    \
758           isRGBinInt(x)       ||    \
759           isBGRinInt(x)       ||    \
760           isRGB(x)      \
761     )
762
763 static av_always_inline int isALPHA(enum AVPixelFormat pix_fmt)
764 {
765     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
766     av_assert0(desc);
767     if (pix_fmt == AV_PIX_FMT_PAL8)
768         return 1;
769     return desc->flags & AV_PIX_FMT_FLAG_ALPHA;
770 }
771
772 #if 1
773 #define isPacked(x)         (       \
774            (x)==AV_PIX_FMT_PAL8        \
775         || (x)==AV_PIX_FMT_YUYV422     \
776         || (x)==AV_PIX_FMT_YVYU422     \
777         || (x)==AV_PIX_FMT_UYVY422     \
778         || (x)==AV_PIX_FMT_Y400A       \
779         ||  isRGBinInt(x)           \
780         ||  isBGRinInt(x)           \
781     )
782 #else
783 static av_always_inline int isPacked(enum AVPixelFormat pix_fmt)
784 {
785     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
786     av_assert0(desc);
787     return ((desc->nb_components >= 2 && !(desc->flags & AV_PIX_FMT_FLAG_PLANAR)) ||
788             pix_fmt == AV_PIX_FMT_PAL8);
789 }
790
791 #endif
792 static av_always_inline int isPlanar(enum AVPixelFormat pix_fmt)
793 {
794     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
795     av_assert0(desc);
796     return (desc->nb_components >= 2 && (desc->flags & AV_PIX_FMT_FLAG_PLANAR));
797 }
798
799 static av_always_inline int isPackedRGB(enum AVPixelFormat pix_fmt)
800 {
801     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
802     av_assert0(desc);
803     return ((desc->flags & (AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB)) == AV_PIX_FMT_FLAG_RGB);
804 }
805
806 static av_always_inline int isPlanarRGB(enum AVPixelFormat pix_fmt)
807 {
808     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
809     av_assert0(desc);
810     return ((desc->flags & (AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB)) ==
811             (AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB));
812 }
813
814 static av_always_inline int usePal(enum AVPixelFormat pix_fmt)
815 {
816     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
817     av_assert0(desc);
818     return (desc->flags & AV_PIX_FMT_FLAG_PAL) || (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL);
819 }
820
821 extern const uint64_t ff_dither4[2];
822 extern const uint64_t ff_dither8[2];
823
824 extern const uint8_t ff_dither_2x2_4[3][8];
825 extern const uint8_t ff_dither_2x2_8[3][8];
826 extern const uint8_t ff_dither_4x4_16[5][8];
827 extern const uint8_t ff_dither_8x8_32[9][8];
828 extern const uint8_t ff_dither_8x8_73[9][8];
829 extern const uint8_t ff_dither_8x8_128[9][8];
830 extern const uint8_t ff_dither_8x8_220[9][8];
831
832 extern const int32_t ff_yuv2rgb_coeffs[8][4];
833
834 extern const AVClass sws_context_class;
835
836 /**
837  * Set c->swscale to an unscaled converter if one exists for the specific
838  * source and destination formats, bit depths, flags, etc.
839  */
840 void ff_get_unscaled_swscale(SwsContext *c);
841 void ff_get_unscaled_swscale_ppc(SwsContext *c);
842 void ff_get_unscaled_swscale_arm(SwsContext *c);
843
844 /**
845  * Return function pointer to fastest main scaler path function depending
846  * on architecture and available optimizations.
847  */
848 SwsFunc ff_getSwsFunc(SwsContext *c);
849
850 void ff_sws_init_input_funcs(SwsContext *c);
851 void ff_sws_init_output_funcs(SwsContext *c,
852                               yuv2planar1_fn *yuv2plane1,
853                               yuv2planarX_fn *yuv2planeX,
854                               yuv2interleavedX_fn *yuv2nv12cX,
855                               yuv2packed1_fn *yuv2packed1,
856                               yuv2packed2_fn *yuv2packed2,
857                               yuv2packedX_fn *yuv2packedX,
858                               yuv2anyX_fn *yuv2anyX);
859 void ff_sws_init_swscale_ppc(SwsContext *c);
860 void ff_sws_init_swscale_x86(SwsContext *c);
861
862 void ff_hyscale_fast_c(SwsContext *c, int16_t *dst, int dstWidth,
863                        const uint8_t *src, int srcW, int xInc);
864 void ff_hcscale_fast_c(SwsContext *c, int16_t *dst1, int16_t *dst2,
865                        int dstWidth, const uint8_t *src1,
866                        const uint8_t *src2, int srcW, int xInc);
867 int ff_init_hscaler_mmxext(int dstW, int xInc, uint8_t *filterCode,
868                            int16_t *filter, int32_t *filterPos,
869                            int numSplits);
870 void ff_hyscale_fast_mmxext(SwsContext *c, int16_t *dst,
871                             int dstWidth, const uint8_t *src,
872                             int srcW, int xInc);
873 void ff_hcscale_fast_mmxext(SwsContext *c, int16_t *dst1, int16_t *dst2,
874                             int dstWidth, const uint8_t *src1,
875                             const uint8_t *src2, int srcW, int xInc);
876
877 static inline void fillPlane16(uint8_t *plane, int stride, int width, int height, int y,
878                                int alpha, int bits, const int big_endian)
879 {
880     int i, j;
881     uint8_t *ptr = plane + stride * y;
882     int v = alpha ? 0xFFFF>>(15-bits) : (1<<bits);
883     for (i = 0; i < height; i++) {
884 #define FILL(wfunc) \
885         for (j = 0; j < width; j++) {\
886             wfunc(ptr+2*j, v);\
887         }
888         if (big_endian) {
889             FILL(AV_WB16);
890         } else {
891             FILL(AV_WL16);
892         }
893         ptr += stride;
894     }
895 }
896
897 #endif /* SWSCALE_SWSCALE_INTERNAL_H */