2 * Copyright (C) 2001-2011 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of FFmpeg.
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.
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.
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
21 #ifndef SWSCALE_SWSCALE_INTERNAL_H
22 #define SWSCALE_SWSCALE_INTERNAL_H
27 #include "libavutil/avassert.h"
28 #include "libavutil/avutil.h"
29 #include "libavutil/common.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/log.h"
32 #include "libavutil/pixfmt.h"
33 #include "libavutil/pixdesc.h"
34 #include "libavutil/ppc/util_altivec.h"
36 #define STR(s) AV_TOSTRING(s) // AV_STRINGIFY is too long
38 #define YUVRGB_TABLE_HEADROOM 512
39 #define YUVRGB_TABLE_LUMA_HEADROOM 512
41 #define MAX_FILTER_SIZE SWS_MAX_FILTER_SIZE
46 #define ALT32_CORR (-1)
61 #define RETCODE_USE_CASCADE -12345
65 typedef enum SwsDither {
75 typedef enum SwsAlphaBlend {
76 SWS_ALPHA_BLEND_NONE = 0,
77 SWS_ALPHA_BLEND_UNIFORM,
78 SWS_ALPHA_BLEND_CHECKERBOARD,
82 typedef int (*SwsFunc)(struct SwsContext *context, const uint8_t *src[],
83 int srcStride[], int srcSliceY, int srcSliceH,
84 uint8_t *dst[], int dstStride[]);
87 * Write one line of horizontally scaled data to planar output
88 * without any additional vertical scaling (or point-scaling).
90 * @param src scaled source data, 15 bits for 8-10-bit output,
91 * 19 bits for 16-bit output (in int32_t)
92 * @param dest pointer to the output plane. For >8-bit
93 * output, this is in uint16_t
94 * @param dstW width of destination in pixels
95 * @param dither ordered dither array of type int16_t and size 8
96 * @param offset Dither offset
98 typedef void (*yuv2planar1_fn)(const int16_t *src, uint8_t *dest, int dstW,
99 const uint8_t *dither, int offset);
102 * Write one line of horizontally scaled data to planar output
103 * with multi-point vertical scaling between input pixels.
105 * @param filter vertical luma/alpha scaling coefficients, 12 bits [0,4096]
106 * @param src scaled luma (Y) or alpha (A) source data, 15 bits for
107 * 8-10-bit output, 19 bits for 16-bit output (in int32_t)
108 * @param filterSize number of vertical input lines to scale
109 * @param dest pointer to output plane. For >8-bit
110 * output, this is in uint16_t
111 * @param dstW width of destination pixels
112 * @param offset Dither offset
114 typedef void (*yuv2planarX_fn)(const int16_t *filter, int filterSize,
115 const int16_t **src, uint8_t *dest, int dstW,
116 const uint8_t *dither, int offset);
119 * Write one line of horizontally scaled chroma to interleaved output
120 * with multi-point vertical scaling between input pixels.
122 * @param dstFormat destination pixel format
123 * @param chrDither ordered dither array of type uint8_t and size 8
124 * @param chrFilter vertical chroma scaling coefficients, 12 bits [0,4096]
125 * @param chrUSrc scaled chroma (U) source data, 15 bits for 8-10-bit
126 * output, 19 bits for 16-bit output (in int32_t)
127 * @param chrVSrc scaled chroma (V) source data, 15 bits for 8-10-bit
128 * output, 19 bits for 16-bit output (in int32_t)
129 * @param chrFilterSize number of vertical chroma input lines to scale
130 * @param dest pointer to the output plane. For >8-bit
131 * output, this is in uint16_t
132 * @param dstW width of chroma planes
134 typedef void (*yuv2interleavedX_fn)(enum AVPixelFormat dstFormat, const uint8_t *chrDither,
135 const int16_t *chrFilter,
137 const int16_t **chrUSrc,
138 const int16_t **chrVSrc,
139 uint8_t *dest, int dstW);
142 * Write one line of horizontally scaled Y/U/V/A to packed-pixel YUV/RGB
143 * output without any additional vertical scaling (or point-scaling). Note
144 * that this function may do chroma scaling, see the "uvalpha" argument.
146 * @param c SWS scaling context
147 * @param lumSrc scaled luma (Y) source data, 15 bits for 8-10-bit output,
148 * 19 bits for 16-bit output (in int32_t)
149 * @param chrUSrc scaled chroma (U) source data, 15 bits for 8-10-bit output,
150 * 19 bits for 16-bit output (in int32_t)
151 * @param chrVSrc scaled chroma (V) source data, 15 bits for 8-10-bit output,
152 * 19 bits for 16-bit output (in int32_t)
153 * @param alpSrc scaled alpha (A) source data, 15 bits for 8-10-bit output,
154 * 19 bits for 16-bit output (in int32_t)
155 * @param dest pointer to the output plane. For 16-bit output, this is
157 * @param dstW width of lumSrc and alpSrc in pixels, number of pixels
158 * to write into dest[]
159 * @param uvalpha chroma scaling coefficient for the second line of chroma
160 * pixels, either 2048 or 0. If 0, one chroma input is used
161 * for 2 output pixels (or if the SWS_FLAG_FULL_CHR_INT flag
162 * is set, it generates 1 output pixel). If 2048, two chroma
163 * input pixels should be averaged for 2 output pixels (this
164 * only happens if SWS_FLAG_FULL_CHR_INT is not set)
165 * @param y vertical line number for this output. This does not need
166 * to be used to calculate the offset in the destination,
167 * but can be used to generate comfort noise using dithering
168 * for some output formats.
170 typedef void (*yuv2packed1_fn)(struct SwsContext *c, const int16_t *lumSrc,
171 const int16_t *chrUSrc[2],
172 const int16_t *chrVSrc[2],
173 const int16_t *alpSrc, uint8_t *dest,
174 int dstW, int uvalpha, int y);
176 * Write one line of horizontally scaled Y/U/V/A to packed-pixel YUV/RGB
177 * output by doing bilinear scaling between two input lines.
179 * @param c SWS scaling context
180 * @param lumSrc scaled luma (Y) source data, 15 bits for 8-10-bit output,
181 * 19 bits for 16-bit output (in int32_t)
182 * @param chrUSrc scaled chroma (U) source data, 15 bits for 8-10-bit output,
183 * 19 bits for 16-bit output (in int32_t)
184 * @param chrVSrc scaled chroma (V) source data, 15 bits for 8-10-bit output,
185 * 19 bits for 16-bit output (in int32_t)
186 * @param alpSrc scaled alpha (A) source data, 15 bits for 8-10-bit output,
187 * 19 bits for 16-bit output (in int32_t)
188 * @param dest pointer to the output plane. For 16-bit output, this is
190 * @param dstW width of lumSrc and alpSrc in pixels, number of pixels
191 * to write into dest[]
192 * @param yalpha luma/alpha scaling coefficients for the second input line.
193 * The first line's coefficients can be calculated by using
195 * @param uvalpha chroma scaling coefficient for the second input line. The
196 * first line's coefficients can be calculated by using
198 * @param y vertical line number for this output. This does not need
199 * to be used to calculate the offset in the destination,
200 * but can be used to generate comfort noise using dithering
201 * for some output formats.
203 typedef void (*yuv2packed2_fn)(struct SwsContext *c, const int16_t *lumSrc[2],
204 const int16_t *chrUSrc[2],
205 const int16_t *chrVSrc[2],
206 const int16_t *alpSrc[2],
208 int dstW, int yalpha, int uvalpha, int y);
210 * Write one line of horizontally scaled Y/U/V/A to packed-pixel YUV/RGB
211 * output by doing multi-point vertical scaling between input pixels.
213 * @param c SWS scaling context
214 * @param lumFilter vertical luma/alpha scaling coefficients, 12 bits [0,4096]
215 * @param lumSrc scaled luma (Y) source data, 15 bits for 8-10-bit output,
216 * 19 bits for 16-bit output (in int32_t)
217 * @param lumFilterSize number of vertical luma/alpha input lines to scale
218 * @param chrFilter vertical chroma scaling coefficients, 12 bits [0,4096]
219 * @param chrUSrc scaled chroma (U) source data, 15 bits for 8-10-bit output,
220 * 19 bits for 16-bit output (in int32_t)
221 * @param chrVSrc scaled chroma (V) source data, 15 bits for 8-10-bit output,
222 * 19 bits for 16-bit output (in int32_t)
223 * @param chrFilterSize number of vertical chroma input lines to scale
224 * @param alpSrc scaled alpha (A) source data, 15 bits for 8-10-bit output,
225 * 19 bits for 16-bit output (in int32_t)
226 * @param dest pointer to the output plane. For 16-bit output, this is
228 * @param dstW width of lumSrc and alpSrc in pixels, number of pixels
229 * to write into dest[]
230 * @param y vertical line number for this output. This does not need
231 * to be used to calculate the offset in the destination,
232 * but can be used to generate comfort noise using dithering
233 * or some output formats.
235 typedef void (*yuv2packedX_fn)(struct SwsContext *c, const int16_t *lumFilter,
236 const int16_t **lumSrc, int lumFilterSize,
237 const int16_t *chrFilter,
238 const int16_t **chrUSrc,
239 const int16_t **chrVSrc, int chrFilterSize,
240 const int16_t **alpSrc, uint8_t *dest,
244 * Write one line of horizontally scaled Y/U/V/A to YUV/RGB
245 * output by doing multi-point vertical scaling between input pixels.
247 * @param c SWS scaling context
248 * @param lumFilter vertical luma/alpha scaling coefficients, 12 bits [0,4096]
249 * @param lumSrc scaled luma (Y) source data, 15 bits for 8-10-bit output,
250 * 19 bits for 16-bit output (in int32_t)
251 * @param lumFilterSize number of vertical luma/alpha input lines to scale
252 * @param chrFilter vertical chroma scaling coefficients, 12 bits [0,4096]
253 * @param chrUSrc scaled chroma (U) source data, 15 bits for 8-10-bit output,
254 * 19 bits for 16-bit output (in int32_t)
255 * @param chrVSrc scaled chroma (V) source data, 15 bits for 8-10-bit output,
256 * 19 bits for 16-bit output (in int32_t)
257 * @param chrFilterSize number of vertical chroma input lines to scale
258 * @param alpSrc scaled alpha (A) source data, 15 bits for 8-10-bit output,
259 * 19 bits for 16-bit output (in int32_t)
260 * @param dest pointer to the output planes. For 16-bit output, this is
262 * @param dstW width of lumSrc and alpSrc in pixels, number of pixels
263 * to write into dest[]
264 * @param y vertical line number for this output. This does not need
265 * to be used to calculate the offset in the destination,
266 * but can be used to generate comfort noise using dithering
267 * or some output formats.
269 typedef void (*yuv2anyX_fn)(struct SwsContext *c, const int16_t *lumFilter,
270 const int16_t **lumSrc, int lumFilterSize,
271 const int16_t *chrFilter,
272 const int16_t **chrUSrc,
273 const int16_t **chrVSrc, int chrFilterSize,
274 const int16_t **alpSrc, uint8_t **dest,
278 struct SwsFilterDescriptor;
280 /* This struct should be aligned on at least a 32-byte boundary. */
281 typedef struct SwsContext {
283 * info on struct for av_log
285 const AVClass *av_class;
288 * Note that src, dst, srcStride, dstStride will be copied in the
289 * sws_scale() wrapper so they can be freely modified here.
292 int srcW; ///< Width of source luma/alpha planes.
293 int srcH; ///< Height of source luma/alpha planes.
294 int dstH; ///< Height of destination luma/alpha planes.
295 int chrSrcW; ///< Width of source chroma planes.
296 int chrSrcH; ///< Height of source chroma planes.
297 int chrDstW; ///< Width of destination chroma planes.
298 int chrDstH; ///< Height of destination chroma planes.
299 int lumXInc, chrXInc;
300 int lumYInc, chrYInc;
301 enum AVPixelFormat dstFormat; ///< Destination pixel format.
302 enum AVPixelFormat srcFormat; ///< Source pixel format.
303 int dstFormatBpp; ///< Number of bits per pixel of the destination pixel format.
304 int srcFormatBpp; ///< Number of bits per pixel of the source pixel format.
306 int chrSrcHSubSample; ///< Binary logarithm of horizontal subsampling factor between luma/alpha and chroma planes in source image.
307 int chrSrcVSubSample; ///< Binary logarithm of vertical subsampling factor between luma/alpha and chroma planes in source image.
308 int chrDstHSubSample; ///< Binary logarithm of horizontal subsampling factor between luma/alpha and chroma planes in destination image.
309 int chrDstVSubSample; ///< Binary logarithm of vertical subsampling factor between luma/alpha and chroma planes in destination image.
310 int vChrDrop; ///< Binary logarithm of extra vertical subsampling factor in source image chroma planes specified by user.
311 int sliceDir; ///< Direction that slices are fed to the scaler (1 = top-to-bottom, -1 = bottom-to-top).
312 double param[2]; ///< Input parameters for scaling algorithms that need them.
314 /* The cascaded_* fields allow spliting a scaler task into multiple
315 * sequential steps, this is for example used to limit the maximum
316 * downscaling factor that needs to be supported in one scaler.
318 struct SwsContext *cascaded_context[3];
319 int cascaded_tmpStride[4];
320 uint8_t *cascaded_tmp[4];
321 int cascaded1_tmpStride[4];
322 uint8_t *cascaded1_tmp[4];
323 int cascaded_mainindex;
327 int is_internal_gamma;
334 struct SwsSlice *slice;
335 struct SwsFilterDescriptor *desc;
337 uint32_t pal_yuv[256];
338 uint32_t pal_rgb[256];
340 float uint2float_lut[256];
343 * @name Scaled horizontal lines ring buffer.
344 * The horizontal scaler keeps just enough scaled lines in a ring buffer
345 * so they may be passed to the vertical scaler. The pointers to the
346 * allocated buffers for each line are duplicated in sequence in the ring
347 * buffer to simplify indexing and avoid wrapping around between lines
348 * inside the vertical scaler code. The wrapping is done before the
349 * vertical scaler is called.
352 int lastInLumBuf; ///< Last scaled horizontal luma/alpha line from source in the ring buffer.
353 int lastInChrBuf; ///< Last scaled horizontal chroma line from source in the ring buffer.
356 uint8_t *formatConvBuffer;
360 * @name Horizontal and vertical filters.
361 * To better understand the following fields, here is a pseudo-code of
362 * their usage in filtering a horizontal line:
364 * for (i = 0; i < width; i++) {
366 * for (j = 0; j < filterSize; j++)
367 * dst[i] += src[ filterPos[i] + j ] * filter[ filterSize * i + j ];
368 * dst[i] >>= FRAC_BITS; // The actual implementation is fixed-point.
373 int16_t *hLumFilter; ///< Array of horizontal filter coefficients for luma/alpha planes.
374 int16_t *hChrFilter; ///< Array of horizontal filter coefficients for chroma planes.
375 int16_t *vLumFilter; ///< Array of vertical filter coefficients for luma/alpha planes.
376 int16_t *vChrFilter; ///< Array of vertical filter coefficients for chroma planes.
377 int32_t *hLumFilterPos; ///< Array of horizontal filter starting positions for each dst[i] for luma/alpha planes.
378 int32_t *hChrFilterPos; ///< Array of horizontal filter starting positions for each dst[i] for chroma planes.
379 int32_t *vLumFilterPos; ///< Array of vertical filter starting positions for each dst[i] for luma/alpha planes.
380 int32_t *vChrFilterPos; ///< Array of vertical filter starting positions for each dst[i] for chroma planes.
381 int hLumFilterSize; ///< Horizontal filter size for luma/alpha pixels.
382 int hChrFilterSize; ///< Horizontal filter size for chroma pixels.
383 int vLumFilterSize; ///< Vertical filter size for luma/alpha pixels.
384 int vChrFilterSize; ///< Vertical filter size for chroma pixels.
387 int lumMmxextFilterCodeSize; ///< Runtime-generated MMXEXT horizontal fast bilinear scaler code size for luma/alpha planes.
388 int chrMmxextFilterCodeSize; ///< Runtime-generated MMXEXT horizontal fast bilinear scaler code size for chroma planes.
389 uint8_t *lumMmxextFilterCode; ///< Runtime-generated MMXEXT horizontal fast bilinear scaler code for luma/alpha planes.
390 uint8_t *chrMmxextFilterCode; ///< Runtime-generated MMXEXT horizontal fast bilinear scaler code for chroma planes.
393 int warned_unuseable_bilinear;
395 int dstY; ///< Last destination vertical line output from last slice.
396 int flags; ///< Flags passed by the user to select scaler algorithm, optimizations, subsampling, etc...
397 void *yuvTable; // pointer to the yuv->rgb table start so it can be freed()
398 // alignment ensures the offset can be added in a single
399 // instruction on e.g. ARM
400 DECLARE_ALIGNED(16, int, table_gV)[256 + 2*YUVRGB_TABLE_HEADROOM];
401 uint8_t *table_rV[256 + 2*YUVRGB_TABLE_HEADROOM];
402 uint8_t *table_gU[256 + 2*YUVRGB_TABLE_HEADROOM];
403 uint8_t *table_bU[256 + 2*YUVRGB_TABLE_HEADROOM];
404 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
414 #define RGB2YUV_SHIFT 15
416 int *dither_error[4];
419 int contrast, brightness, saturation; // for sws_getColorspaceDetails
420 int srcColorspaceTable[4];
421 int dstColorspaceTable[4];
422 int srcRange; ///< 0 = MPG YUV range, 1 = JPG YUV range (source image).
423 int dstRange; ///< 0 = MPG YUV range, 1 = JPG YUV range (destination image).
432 int yuv2rgb_y_offset;
434 int yuv2rgb_v2r_coeff;
435 int yuv2rgb_v2g_coeff;
436 int yuv2rgb_u2g_coeff;
437 int yuv2rgb_u2b_coeff;
439 #define RED_DITHER "0*8"
440 #define GREEN_DITHER "1*8"
441 #define BLUE_DITHER "2*8"
442 #define Y_COEFF "3*8"
443 #define VR_COEFF "4*8"
444 #define UB_COEFF "5*8"
445 #define VG_COEFF "6*8"
446 #define UG_COEFF "7*8"
447 #define Y_OFFSET "8*8"
448 #define U_OFFSET "9*8"
449 #define V_OFFSET "10*8"
450 #define LUM_MMX_FILTER_OFFSET "11*8"
451 #define CHR_MMX_FILTER_OFFSET "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)
452 #define DSTW_OFFSET "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2"
453 #define ESP_OFFSET "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+8"
454 #define VROUNDER_OFFSET "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+16"
455 #define U_TEMP "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+24"
456 #define V_TEMP "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+32"
457 #define Y_TEMP "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+40"
458 #define ALP_MMX_FILTER_OFFSET "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+48"
459 #define UV_OFF_PX "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*3+48"
460 #define UV_OFF_BYTE "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*3+56"
461 #define DITHER16 "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*3+64"
462 #define DITHER32 "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*3+80"
463 #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
465 DECLARE_ALIGNED(8, uint64_t, redDither);
466 DECLARE_ALIGNED(8, uint64_t, greenDither);
467 DECLARE_ALIGNED(8, uint64_t, blueDither);
469 DECLARE_ALIGNED(8, uint64_t, yCoeff);
470 DECLARE_ALIGNED(8, uint64_t, vrCoeff);
471 DECLARE_ALIGNED(8, uint64_t, ubCoeff);
472 DECLARE_ALIGNED(8, uint64_t, vgCoeff);
473 DECLARE_ALIGNED(8, uint64_t, ugCoeff);
474 DECLARE_ALIGNED(8, uint64_t, yOffset);
475 DECLARE_ALIGNED(8, uint64_t, uOffset);
476 DECLARE_ALIGNED(8, uint64_t, vOffset);
477 int32_t lumMmxFilter[4 * MAX_FILTER_SIZE];
478 int32_t chrMmxFilter[4 * MAX_FILTER_SIZE];
479 int dstW; ///< Width of destination luma/alpha planes.
480 DECLARE_ALIGNED(8, uint64_t, esp);
481 DECLARE_ALIGNED(8, uint64_t, vRounder);
482 DECLARE_ALIGNED(8, uint64_t, u_temp);
483 DECLARE_ALIGNED(8, uint64_t, v_temp);
484 DECLARE_ALIGNED(8, uint64_t, y_temp);
485 int32_t alpMmxFilter[4 * MAX_FILTER_SIZE];
486 // alignment of these values is not necessary, but merely here
487 // to maintain the same offset across x8632 and x86-64. Once we
488 // use proper offset macros in the asm, they can be removed.
489 DECLARE_ALIGNED(8, ptrdiff_t, uv_off); ///< offset (in pixels) between u and v planes
490 DECLARE_ALIGNED(8, ptrdiff_t, uv_offx2); ///< offset (in bytes) between u and v planes
491 DECLARE_ALIGNED(8, uint16_t, dither16)[8];
492 DECLARE_ALIGNED(8, uint32_t, dither32)[8];
494 const uint8_t *chrDither8, *lumDither8;
497 vector signed short CY;
498 vector signed short CRV;
499 vector signed short CBU;
500 vector signed short CGU;
501 vector signed short CGV;
502 vector signed short OY;
503 vector unsigned short CSHIFT;
504 vector signed short *vYCoeffsBank, *vCCoeffsBank;
509 /* pre defined color-spaces gamma */
510 #define XYZ_GAMMA (2.6f)
511 #define RGB_GAMMA (2.2f)
514 int16_t *xyzgammainv;
515 int16_t *rgbgammainv;
516 int16_t xyz2rgb_matrix[3][4];
517 int16_t rgb2xyz_matrix[3][4];
519 /* function pointers for swscale() */
520 yuv2planar1_fn yuv2plane1;
521 yuv2planarX_fn yuv2planeX;
522 yuv2interleavedX_fn yuv2nv12cX;
523 yuv2packed1_fn yuv2packed1;
524 yuv2packed2_fn yuv2packed2;
525 yuv2packedX_fn yuv2packedX;
526 yuv2anyX_fn yuv2anyX;
528 /// Unscaled conversion of luma plane to YV12 for horizontal scaler.
529 void (*lumToYV12)(uint8_t *dst, const uint8_t *src, const uint8_t *src2, const uint8_t *src3,
530 int width, uint32_t *pal);
531 /// Unscaled conversion of alpha plane to YV12 for horizontal scaler.
532 void (*alpToYV12)(uint8_t *dst, const uint8_t *src, const uint8_t *src2, const uint8_t *src3,
533 int width, uint32_t *pal);
534 /// Unscaled conversion of chroma planes to YV12 for horizontal scaler.
535 void (*chrToYV12)(uint8_t *dstU, uint8_t *dstV,
536 const uint8_t *src1, const uint8_t *src2, const uint8_t *src3,
537 int width, uint32_t *pal);
540 * Functions to read planar input, such as planar RGB, and convert
541 * internally to Y/UV/A.
544 void (*readLumPlanar)(uint8_t *dst, const uint8_t *src[4], int width, int32_t *rgb2yuv);
545 void (*readChrPlanar)(uint8_t *dstU, uint8_t *dstV, const uint8_t *src[4],
546 int width, int32_t *rgb2yuv);
547 void (*readAlpPlanar)(uint8_t *dst, const uint8_t *src[4], int width, int32_t *rgb2yuv);
551 * Scale one horizontal line of input data using a bilinear filter
552 * to produce one line of output data. Compared to SwsContext->hScale(),
553 * please take note of the following caveats when using these:
554 * - Scaling is done using only 7 bits instead of 14-bit coefficients.
555 * - You can use no more than 5 input pixels to produce 4 output
556 * pixels. Therefore, this filter should not be used for downscaling
557 * by more than ~20% in width (because that equals more than 5/4th
558 * downscaling and thus more than 5 pixels input per 4 pixels output).
559 * - In general, bilinear filters create artifacts during downscaling
560 * (even when <20%), because one output pixel will span more than one
561 * input pixel, and thus some pixels will need edges of both neighbor
562 * pixels to interpolate the output pixel. Since you can use at most
563 * two input pixels per output pixel in bilinear scaling, this is
564 * impossible and thus downscaling by any size will create artifacts.
565 * To enable this type of scaling, set SWS_FLAG_FAST_BILINEAR
566 * in SwsContext->flags.
569 void (*hyscale_fast)(struct SwsContext *c,
570 int16_t *dst, int dstWidth,
571 const uint8_t *src, int srcW, int xInc);
572 void (*hcscale_fast)(struct SwsContext *c,
573 int16_t *dst1, int16_t *dst2, int dstWidth,
574 const uint8_t *src1, const uint8_t *src2,
579 * Scale one horizontal line of input data using a filter over the input
580 * lines, to produce one (differently sized) line of output data.
582 * @param dst pointer to destination buffer for horizontally scaled
583 * data. If the number of bits per component of one
584 * destination pixel (SwsContext->dstBpc) is <= 10, data
585 * will be 15 bpc in 16 bits (int16_t) width. Else (i.e.
586 * SwsContext->dstBpc == 16), data will be 19bpc in
587 * 32 bits (int32_t) width.
588 * @param dstW width of destination image
589 * @param src pointer to source data to be scaled. If the number of
590 * bits per component of a source pixel (SwsContext->srcBpc)
591 * is 8, this is 8bpc in 8 bits (uint8_t) width. Else
592 * (i.e. SwsContext->dstBpc > 8), this is native depth
593 * in 16 bits (uint16_t) width. In other words, for 9-bit
594 * YUV input, this is 9bpc, for 10-bit YUV input, this is
595 * 10bpc, and for 16-bit RGB or YUV, this is 16bpc.
596 * @param filter filter coefficients to be used per output pixel for
597 * scaling. This contains 14bpp filtering coefficients.
598 * Guaranteed to contain dstW * filterSize entries.
599 * @param filterPos position of the first input pixel to be used for
600 * each output pixel during scaling. Guaranteed to
601 * contain dstW entries.
602 * @param filterSize the number of input coefficients to be used (and
603 * thus the number of input pixels to be used) for
604 * creating a single output pixel. Is aligned to 4
605 * (and input coefficients thus padded with zeroes)
606 * to simplify creating SIMD code.
609 void (*hyScale)(struct SwsContext *c, int16_t *dst, int dstW,
610 const uint8_t *src, const int16_t *filter,
611 const int32_t *filterPos, int filterSize);
612 void (*hcScale)(struct SwsContext *c, int16_t *dst, int dstW,
613 const uint8_t *src, const int16_t *filter,
614 const int32_t *filterPos, int filterSize);
617 /// Color range conversion function for luma plane if needed.
618 void (*lumConvertRange)(int16_t *dst, int width);
619 /// Color range conversion function for chroma planes if needed.
620 void (*chrConvertRange)(int16_t *dst1, int16_t *dst2, int width);
622 int needs_hcscale; ///< Set if there are chroma planes to be converted.
626 SwsAlphaBlend alphablend;
628 //FIXME check init (where 0)
630 SwsFunc ff_yuv2rgb_get_func_ptr(SwsContext *c);
631 int ff_yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4],
632 int fullRange, int brightness,
633 int contrast, int saturation);
634 void ff_yuv2rgb_init_tables_ppc(SwsContext *c, const int inv_table[4],
635 int brightness, int contrast, int saturation);
637 void ff_updateMMXDitherTables(SwsContext *c, int dstY);
639 av_cold void ff_sws_init_range_convert(SwsContext *c);
641 SwsFunc ff_yuv2rgb_init_x86(SwsContext *c);
642 SwsFunc ff_yuv2rgb_init_ppc(SwsContext *c);
644 static av_always_inline int is16BPS(enum AVPixelFormat pix_fmt)
646 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
648 return desc->comp[0].depth == 16;
651 static av_always_inline int is32BPS(enum AVPixelFormat pix_fmt)
653 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
655 return desc->comp[0].depth == 32;
658 static av_always_inline int isNBPS(enum AVPixelFormat pix_fmt)
660 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
662 return desc->comp[0].depth >= 9 && desc->comp[0].depth <= 14;
665 static av_always_inline int isBE(enum AVPixelFormat pix_fmt)
667 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
669 return desc->flags & AV_PIX_FMT_FLAG_BE;
672 static av_always_inline int isYUV(enum AVPixelFormat pix_fmt)
674 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
676 return !(desc->flags & AV_PIX_FMT_FLAG_RGB) && desc->nb_components >= 2;
679 static av_always_inline int isPlanarYUV(enum AVPixelFormat pix_fmt)
681 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
683 return ((desc->flags & AV_PIX_FMT_FLAG_PLANAR) && isYUV(pix_fmt));
687 * Identity semi-planar YUV formats. Specifically, those are YUV formats
688 * where the second and third components (U & V) are on the same plane.
690 static av_always_inline int isSemiPlanarYUV(enum AVPixelFormat pix_fmt)
692 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
694 return (isPlanarYUV(pix_fmt) && desc->comp[1].plane == desc->comp[2].plane);
697 static av_always_inline int isRGB(enum AVPixelFormat pix_fmt)
699 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
701 return (desc->flags & AV_PIX_FMT_FLAG_RGB);
704 static av_always_inline int isGray(enum AVPixelFormat pix_fmt)
706 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
708 return !(desc->flags & AV_PIX_FMT_FLAG_PAL) &&
709 !(desc->flags & AV_PIX_FMT_FLAG_HWACCEL) &&
710 desc->nb_components <= 2 &&
711 pix_fmt != AV_PIX_FMT_MONOBLACK &&
712 pix_fmt != AV_PIX_FMT_MONOWHITE;
715 static av_always_inline int isRGBinInt(enum AVPixelFormat pix_fmt)
717 return pix_fmt == AV_PIX_FMT_RGB48BE ||
718 pix_fmt == AV_PIX_FMT_RGB48LE ||
719 pix_fmt == AV_PIX_FMT_RGB32 ||
720 pix_fmt == AV_PIX_FMT_RGB32_1 ||
721 pix_fmt == AV_PIX_FMT_RGB24 ||
722 pix_fmt == AV_PIX_FMT_RGB565BE ||
723 pix_fmt == AV_PIX_FMT_RGB565LE ||
724 pix_fmt == AV_PIX_FMT_RGB555BE ||
725 pix_fmt == AV_PIX_FMT_RGB555LE ||
726 pix_fmt == AV_PIX_FMT_RGB444BE ||
727 pix_fmt == AV_PIX_FMT_RGB444LE ||
728 pix_fmt == AV_PIX_FMT_RGB8 ||
729 pix_fmt == AV_PIX_FMT_RGB4 ||
730 pix_fmt == AV_PIX_FMT_RGB4_BYTE ||
731 pix_fmt == AV_PIX_FMT_RGBA64BE ||
732 pix_fmt == AV_PIX_FMT_RGBA64LE ||
733 pix_fmt == AV_PIX_FMT_MONOBLACK ||
734 pix_fmt == AV_PIX_FMT_MONOWHITE;
737 static av_always_inline int isBGRinInt(enum AVPixelFormat pix_fmt)
739 return pix_fmt == AV_PIX_FMT_BGR48BE ||
740 pix_fmt == AV_PIX_FMT_BGR48LE ||
741 pix_fmt == AV_PIX_FMT_BGR32 ||
742 pix_fmt == AV_PIX_FMT_BGR32_1 ||
743 pix_fmt == AV_PIX_FMT_BGR24 ||
744 pix_fmt == AV_PIX_FMT_BGR565BE ||
745 pix_fmt == AV_PIX_FMT_BGR565LE ||
746 pix_fmt == AV_PIX_FMT_BGR555BE ||
747 pix_fmt == AV_PIX_FMT_BGR555LE ||
748 pix_fmt == AV_PIX_FMT_BGR444BE ||
749 pix_fmt == AV_PIX_FMT_BGR444LE ||
750 pix_fmt == AV_PIX_FMT_BGR8 ||
751 pix_fmt == AV_PIX_FMT_BGR4 ||
752 pix_fmt == AV_PIX_FMT_BGR4_BYTE ||
753 pix_fmt == AV_PIX_FMT_BGRA64BE ||
754 pix_fmt == AV_PIX_FMT_BGRA64LE ||
755 pix_fmt == AV_PIX_FMT_MONOBLACK ||
756 pix_fmt == AV_PIX_FMT_MONOWHITE;
759 static av_always_inline int isBayer(enum AVPixelFormat pix_fmt)
761 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
763 return !!(desc->flags & AV_PIX_FMT_FLAG_BAYER);
766 static av_always_inline int isAnyRGB(enum AVPixelFormat pix_fmt)
768 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
770 return (desc->flags & AV_PIX_FMT_FLAG_RGB) ||
771 pix_fmt == AV_PIX_FMT_MONOBLACK || pix_fmt == AV_PIX_FMT_MONOWHITE;
774 static av_always_inline int isFloat(enum AVPixelFormat pix_fmt)
776 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
778 return desc->flags & AV_PIX_FMT_FLAG_FLOAT;
781 static av_always_inline int isALPHA(enum AVPixelFormat pix_fmt)
783 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
785 if (pix_fmt == AV_PIX_FMT_PAL8)
787 return desc->flags & AV_PIX_FMT_FLAG_ALPHA;
790 static av_always_inline int isPacked(enum AVPixelFormat pix_fmt)
792 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
794 return (desc->nb_components >= 2 && !(desc->flags & AV_PIX_FMT_FLAG_PLANAR)) ||
795 pix_fmt == AV_PIX_FMT_PAL8 ||
796 pix_fmt == AV_PIX_FMT_MONOBLACK || pix_fmt == AV_PIX_FMT_MONOWHITE;
799 static av_always_inline int isPlanar(enum AVPixelFormat pix_fmt)
801 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
803 return (desc->nb_components >= 2 && (desc->flags & AV_PIX_FMT_FLAG_PLANAR));
806 static av_always_inline int isPackedRGB(enum AVPixelFormat pix_fmt)
808 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
810 return ((desc->flags & (AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB)) == AV_PIX_FMT_FLAG_RGB);
813 static av_always_inline int isPlanarRGB(enum AVPixelFormat pix_fmt)
815 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
817 return ((desc->flags & (AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB)) ==
818 (AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB));
821 static av_always_inline int usePal(enum AVPixelFormat pix_fmt)
824 case AV_PIX_FMT_PAL8:
825 case AV_PIX_FMT_BGR4_BYTE:
826 case AV_PIX_FMT_BGR8:
827 case AV_PIX_FMT_GRAY8:
828 case AV_PIX_FMT_RGB4_BYTE:
829 case AV_PIX_FMT_RGB8:
836 extern const uint64_t ff_dither4[2];
837 extern const uint64_t ff_dither8[2];
839 extern const uint8_t ff_dither_2x2_4[3][8];
840 extern const uint8_t ff_dither_2x2_8[3][8];
841 extern const uint8_t ff_dither_4x4_16[5][8];
842 extern const uint8_t ff_dither_8x8_32[9][8];
843 extern const uint8_t ff_dither_8x8_73[9][8];
844 extern const uint8_t ff_dither_8x8_128[9][8];
845 extern const uint8_t ff_dither_8x8_220[9][8];
847 extern const int32_t ff_yuv2rgb_coeffs[11][4];
849 extern const AVClass ff_sws_context_class;
852 * Set c->swscale to an unscaled converter if one exists for the specific
853 * source and destination formats, bit depths, flags, etc.
855 void ff_get_unscaled_swscale(SwsContext *c);
856 void ff_get_unscaled_swscale_ppc(SwsContext *c);
857 void ff_get_unscaled_swscale_arm(SwsContext *c);
858 void ff_get_unscaled_swscale_aarch64(SwsContext *c);
861 * Return function pointer to fastest main scaler path function depending
862 * on architecture and available optimizations.
864 SwsFunc ff_getSwsFunc(SwsContext *c);
866 void ff_sws_init_input_funcs(SwsContext *c);
867 void ff_sws_init_output_funcs(SwsContext *c,
868 yuv2planar1_fn *yuv2plane1,
869 yuv2planarX_fn *yuv2planeX,
870 yuv2interleavedX_fn *yuv2nv12cX,
871 yuv2packed1_fn *yuv2packed1,
872 yuv2packed2_fn *yuv2packed2,
873 yuv2packedX_fn *yuv2packedX,
874 yuv2anyX_fn *yuv2anyX);
875 void ff_sws_init_swscale_ppc(SwsContext *c);
876 void ff_sws_init_swscale_vsx(SwsContext *c);
877 void ff_sws_init_swscale_x86(SwsContext *c);
878 void ff_sws_init_swscale_aarch64(SwsContext *c);
879 void ff_sws_init_swscale_arm(SwsContext *c);
881 void ff_hyscale_fast_c(SwsContext *c, int16_t *dst, int dstWidth,
882 const uint8_t *src, int srcW, int xInc);
883 void ff_hcscale_fast_c(SwsContext *c, int16_t *dst1, int16_t *dst2,
884 int dstWidth, const uint8_t *src1,
885 const uint8_t *src2, int srcW, int xInc);
886 int ff_init_hscaler_mmxext(int dstW, int xInc, uint8_t *filterCode,
887 int16_t *filter, int32_t *filterPos,
889 void ff_hyscale_fast_mmxext(SwsContext *c, int16_t *dst,
890 int dstWidth, const uint8_t *src,
892 void ff_hcscale_fast_mmxext(SwsContext *c, int16_t *dst1, int16_t *dst2,
893 int dstWidth, const uint8_t *src1,
894 const uint8_t *src2, int srcW, int xInc);
897 * Allocate and return an SwsContext.
898 * This is like sws_getContext() but does not perform the init step, allowing
899 * the user to set additional AVOptions.
901 * @see sws_getContext()
903 struct SwsContext *sws_alloc_set_opts(int srcW, int srcH, enum AVPixelFormat srcFormat,
904 int dstW, int dstH, enum AVPixelFormat dstFormat,
905 int flags, const double *param);
907 int ff_sws_alphablendaway(SwsContext *c, const uint8_t *src[],
908 int srcStride[], int srcSliceY, int srcSliceH,
909 uint8_t *dst[], int dstStride[]);
911 static inline void fillPlane16(uint8_t *plane, int stride, int width, int height, int y,
912 int alpha, int bits, const int big_endian)
915 uint8_t *ptr = plane + stride * y;
916 int v = alpha ? 0xFFFF>>(16-bits) : (1<<(bits-1));
917 for (i = 0; i < height; i++) {
918 #define FILL(wfunc) \
919 for (j = 0; j < width; j++) {\
932 static inline void fillPlane32(uint8_t *plane, int stride, int width, int height, int y,
933 int alpha, int bits, const int big_endian, int is_float)
936 uint8_t *ptr = plane + stride * y;
938 uint32_t onef32 = 0x3f800000;
940 v = alpha ? onef32 : 0;
942 v = alpha ? 0xFFFFFFFF>>(32-bits) : (1<<(bits-1));
944 for (i = 0; i < height; i++) {
945 #define FILL(wfunc) \
946 for (j = 0; j < width; j++) {\
960 #define MAX_SLICE_PLANES 4
963 typedef struct SwsPlane
965 int available_lines; ///< max number of lines that can be hold by this plane
966 int sliceY; ///< index of first line
967 int sliceH; ///< number of lines
968 uint8_t **line; ///< line buffer
969 uint8_t **tmp; ///< Tmp line buffer used by mmx code
973 * Struct which defines a slice of an image to be scaled or an output for
975 * A slice can also be used as intermediate ring buffer for scaling steps.
977 typedef struct SwsSlice
979 int width; ///< Slice line width
980 int h_chr_sub_sample; ///< horizontal chroma subsampling factor
981 int v_chr_sub_sample; ///< vertical chroma subsampling factor
982 int is_ring; ///< flag to identify if this slice is a ring buffer
983 int should_free_lines; ///< flag to identify if there are dynamic allocated lines
984 enum AVPixelFormat fmt; ///< planes pixel format
985 SwsPlane plane[MAX_SLICE_PLANES]; ///< color planes
989 * Struct which holds all necessary data for processing a slice.
990 * A processing step can be a color conversion or horizontal/vertical scaling.
992 typedef struct SwsFilterDescriptor
994 SwsSlice *src; ///< Source slice
995 SwsSlice *dst; ///< Output slice
997 int alpha; ///< Flag for processing alpha channel
998 void *instance; ///< Filter instance data
1000 /// Function for processing input slice sliceH lines starting from line sliceY
1001 int (*process)(SwsContext *c, struct SwsFilterDescriptor *desc, int sliceY, int sliceH);
1002 } SwsFilterDescriptor;
1004 // warp input lines in the form (src + width*i + j) to slice format (line[i][j])
1005 // relative=true means first line src[x][0] otherwise first line is src[x][lum/crh Y]
1006 int ff_init_slice_from_src(SwsSlice * s, uint8_t *src[4], int stride[4], int srcW, int lumY, int lumH, int chrY, int chrH, int relative);
1008 // Initialize scaler filter descriptor chain
1009 int ff_init_filters(SwsContext *c);
1011 // Free all filter data
1012 int ff_free_filters(SwsContext *c);
1015 function for applying ring buffer logic into slice s
1016 It checks if the slice can hold more @lum lines, if yes
1017 do nothing otherwise remove @lum least used lines.
1018 It applies the same procedure for @chr lines.
1020 int ff_rotate_slice(SwsSlice *s, int lum, int chr);
1022 /// initializes gamma conversion descriptor
1023 int ff_init_gamma_convert(SwsFilterDescriptor *desc, SwsSlice * src, uint16_t *table);
1025 /// initializes lum pixel format conversion descriptor
1026 int ff_init_desc_fmt_convert(SwsFilterDescriptor *desc, SwsSlice * src, SwsSlice *dst, uint32_t *pal);
1028 /// initializes lum horizontal scaling descriptor
1029 int ff_init_desc_hscale(SwsFilterDescriptor *desc, SwsSlice *src, SwsSlice *dst, uint16_t *filter, int * filter_pos, int filter_size, int xInc);
1031 /// initializes chr pixel format conversion descriptor
1032 int ff_init_desc_cfmt_convert(SwsFilterDescriptor *desc, SwsSlice * src, SwsSlice *dst, uint32_t *pal);
1034 /// initializes chr horizontal scaling descriptor
1035 int ff_init_desc_chscale(SwsFilterDescriptor *desc, SwsSlice *src, SwsSlice *dst, uint16_t *filter, int * filter_pos, int filter_size, int xInc);
1037 int ff_init_desc_no_chr(SwsFilterDescriptor *desc, SwsSlice * src, SwsSlice *dst);
1039 /// initializes vertical scaling descriptors
1040 int ff_init_vscale(SwsContext *c, SwsFilterDescriptor *desc, SwsSlice *src, SwsSlice *dst);
1042 /// setup vertical scaler functions
1043 void ff_init_vscale_pfn(SwsContext *c, yuv2planar1_fn yuv2plane1, yuv2planarX_fn yuv2planeX,
1044 yuv2interleavedX_fn yuv2nv12cX, yuv2packed1_fn yuv2packed1, yuv2packed2_fn yuv2packed2,
1045 yuv2packedX_fn yuv2packedX, yuv2anyX_fn yuv2anyX, int use_mmx);
1047 //number of extra lines to process
1048 #define MAX_LINES_AHEAD 4
1050 #endif /* SWSCALE_SWSCALE_INTERNAL_H */