FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libswscale/swscale_internal.h
Date: 2026-10-29 23:19:09
Exec Total Coverage
Lines: 168 174 96.6%
Functions: 26 27 96.3%
Branches: 180 208 86.5%

Line Branch Exec Source
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 <stdatomic.h>
25 #include <assert.h>
26
27 #include "config.h"
28 #include "swscale.h"
29 #include "graph.h"
30
31 #include "libavutil/avassert.h"
32 #include "libavutil/common.h"
33 #include "libavutil/frame.h"
34 #include "libavutil/framepool.h"
35 #include "libavutil/intreadwrite.h"
36 #include "libavutil/log.h"
37 #include "libavutil/mem_internal.h"
38 #include "libavutil/pixfmt.h"
39 #include "libavutil/pixdesc.h"
40 #include "libavutil/slicethread.h"
41 #include "libavutil/half2float.h"
42
43 #if HAVE_ALTIVEC
44 #define SWSINTERNAL_ADDITIONAL_ASM_SIZE (7*16 + 2*8 + /* alignment */ 16)
45 #endif
46 #ifndef SWSINTERNAL_ADDITIONAL_ASM_SIZE
47 #define SWSINTERNAL_ADDITIONAL_ASM_SIZE 0
48 #endif
49
50 #define STR(s) AV_TOSTRING(s) // AV_STRINGIFY is too long
51
52 #define YUVRGB_TABLE_HEADROOM 512
53 #define YUVRGB_TABLE_LUMA_HEADROOM 512
54
55 #define MAX_FILTER_SIZE SWS_MAX_FILTER_SIZE
56
57 #define SWS_MAX_THREADS 8192 /* sanity clamp */
58
59 #if HAVE_BIGENDIAN
60 #define ALT32_CORR (-1)
61 #else
62 #define ALT32_CORR 1
63 #endif
64
65 #if ARCH_X86_64
66 # define APCK_PTR2 8
67 # define APCK_COEF 16
68 # define APCK_SIZE 24
69 #else
70 # define APCK_PTR2 4
71 # define APCK_COEF 8
72 # define APCK_SIZE 16
73 #endif
74
75 #define RETCODE_USE_CASCADE -12345
76
77 typedef struct SwsInternal SwsInternal;
78
79 5077786 static inline SwsInternal *sws_internal(const SwsContext *sws)
80 {
81 5077786 return (SwsInternal *) sws;
82 }
83
84 SwsBackend ff_sws_enabled_backends(const SwsContext *ctx);
85
86 typedef struct Range {
87 unsigned int start;
88 unsigned int len;
89 } Range;
90
91 typedef struct RangeList {
92 Range *ranges;
93 unsigned int nb_ranges;
94 int ranges_allocated;
95 } RangeList;
96
97 int ff_range_add(RangeList *r, unsigned int start, unsigned int len);
98
99 typedef int (*SwsFunc)(SwsInternal *c, const uint8_t *const src[],
100 const int srcStride[], int srcSliceY, int srcSliceH,
101 uint8_t *const dst[], const int dstStride[]);
102
103 typedef void (*SwsColorFunc)(const SwsInternal *c, uint8_t *dst, int dst_stride,
104 const uint8_t *src, int src_stride, int w, int h);
105
106 typedef struct SwsLuts {
107 uint16_t *in;
108 uint16_t *out;
109 } SwsLuts;
110
111 typedef struct SwsColorXform {
112 SwsLuts gamma;
113 int16_t mat[3][3];
114 } SwsColorXform;
115
116 /**
117 * Write one line of horizontally scaled data to planar output
118 * without any additional vertical scaling (or point-scaling).
119 *
120 * @param src scaled source data, 15 bits for 8-10-bit output,
121 * 19 bits for 16-bit output (in int32_t)
122 * @param dest pointer to the output plane. For >8-bit
123 * output, this is in uint16_t
124 * @param dstW width of destination in pixels
125 * @param dither ordered dither array of type int16_t and size 8
126 * @param offset Dither offset
127 */
128 typedef void (*yuv2planar1_fn)(const int16_t *src, uint8_t *dest, int dstW,
129 const uint8_t *dither, int offset);
130
131 /**
132 * Write one line of horizontally scaled data to planar output
133 * with multi-point vertical scaling between input pixels.
134 *
135 * @param filter vertical luma/alpha scaling coefficients, 12 bits [0,4096]
136 * @param src scaled luma (Y) or alpha (A) source data, 15 bits for
137 * 8-10-bit output, 19 bits for 16-bit output (in int32_t)
138 * @param filterSize number of vertical input lines to scale
139 * @param dest pointer to output plane. For >8-bit
140 * output, this is in uint16_t
141 * @param dstW width of destination pixels
142 * @param offset Dither offset
143 */
144 typedef void (*yuv2planarX_fn)(const int16_t *filter, int filterSize,
145 const int16_t **src, uint8_t *dest, int dstW,
146 const uint8_t *dither, int offset);
147
148 /**
149 * Write one line of horizontally scaled chroma to interleaved output
150 * with multi-point vertical scaling between input pixels.
151 *
152 * @param dstFormat destination pixel format
153 * @param chrDither ordered dither array of type uint8_t and size 8
154 * @param chrFilter vertical chroma scaling coefficients, 12 bits [0,4096]
155 * @param chrUSrc scaled chroma (U) source data, 15 bits for 8-10-bit
156 * output, 19 bits for 16-bit output (in int32_t)
157 * @param chrVSrc scaled chroma (V) source data, 15 bits for 8-10-bit
158 * output, 19 bits for 16-bit output (in int32_t)
159 * @param chrFilterSize number of vertical chroma input lines to scale
160 * @param dest pointer to the output plane. For >8-bit
161 * output, this is in uint16_t
162 * @param dstW width of chroma planes
163 */
164 typedef void (*yuv2interleavedX_fn)(enum AVPixelFormat dstFormat,
165 const uint8_t *chrDither,
166 const int16_t *chrFilter,
167 int chrFilterSize,
168 const int16_t **chrUSrc,
169 const int16_t **chrVSrc,
170 uint8_t *dest, int dstW);
171
172 /**
173 * Write one line of horizontally scaled Y/U/V/A to packed-pixel YUV/RGB
174 * output without any additional vertical scaling (or point-scaling). Note
175 * that this function may do chroma scaling, see the "uvalpha" argument.
176 *
177 * @param c SWS scaling context
178 * @param lumSrc scaled luma (Y) source data, 15 bits for 8-10-bit output,
179 * 19 bits for 16-bit output (in int32_t)
180 * @param chrUSrc scaled chroma (U) source data, 15 bits for 8-10-bit output,
181 * 19 bits for 16-bit output (in int32_t)
182 * @param chrVSrc scaled chroma (V) source data, 15 bits for 8-10-bit output,
183 * 19 bits for 16-bit output (in int32_t)
184 * @param alpSrc scaled alpha (A) source data, 15 bits for 8-10-bit output,
185 * 19 bits for 16-bit output (in int32_t)
186 * @param dest pointer to the output plane. For 16-bit output, this is
187 * uint16_t
188 * @param dstW width of lumSrc and alpSrc in pixels, number of pixels
189 * to write into dest[]
190 * @param uvalpha chroma scaling coefficient for the second line of chroma
191 * pixels, either 2048 or 0. If 0, one chroma input is used
192 * for 2 output pixels (or if the SWS_FULL_CHR_H_INT flag
193 * is set, it generates 1 output pixel). If 2048, two chroma
194 * input pixels should be averaged for 2 output pixels (this
195 * only happens if SWS_FULL_CHR_H_INT is not set)
196 * @param y vertical line number for this output. This does not need
197 * to be used to calculate the offset in the destination,
198 * but can be used to generate comfort noise using dithering
199 * for some output formats.
200 */
201 typedef void (*yuv2packed1_fn)(SwsInternal *c, const int16_t *lumSrc,
202 const int16_t *chrUSrc[2],
203 const int16_t *chrVSrc[2],
204 const int16_t *alpSrc, uint8_t *dest,
205 int dstW, int uvalpha, int y);
206 /**
207 * Write one line of horizontally scaled Y/U/V/A to packed-pixel YUV/RGB
208 * output by doing bilinear scaling between two input lines.
209 *
210 * @param c SWS scaling context
211 * @param lumSrc scaled luma (Y) source data, 15 bits for 8-10-bit output,
212 * 19 bits for 16-bit output (in int32_t)
213 * @param chrUSrc scaled chroma (U) source data, 15 bits for 8-10-bit output,
214 * 19 bits for 16-bit output (in int32_t)
215 * @param chrVSrc scaled chroma (V) source data, 15 bits for 8-10-bit output,
216 * 19 bits for 16-bit output (in int32_t)
217 * @param alpSrc scaled alpha (A) source data, 15 bits for 8-10-bit output,
218 * 19 bits for 16-bit output (in int32_t)
219 * @param dest pointer to the output plane. For 16-bit 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 yalpha luma/alpha scaling coefficients for the second input line.
224 * The first line's coefficients can be calculated by using
225 * 4096 - yalpha
226 * @param uvalpha chroma scaling coefficient for the second input line. The
227 * first line's coefficients can be calculated by using
228 * 4096 - uvalpha
229 * @param y vertical line number for this output. This does not need
230 * to be used to calculate the offset in the destination,
231 * but can be used to generate comfort noise using dithering
232 * for some output formats.
233 */
234 typedef void (*yuv2packed2_fn)(SwsInternal *c, const int16_t *lumSrc[2],
235 const int16_t *chrUSrc[2],
236 const int16_t *chrVSrc[2],
237 const int16_t *alpSrc[2],
238 uint8_t *dest,
239 int dstW, int yalpha, int uvalpha, int y);
240 /**
241 * Write one line of horizontally scaled Y/U/V/A to packed-pixel YUV/RGB
242 * output by doing multi-point vertical scaling between input pixels.
243 *
244 * @param c SWS scaling context
245 * @param lumFilter vertical luma/alpha scaling coefficients, 12 bits [0,4096]
246 * @param lumSrc scaled luma (Y) source data, 15 bits for 8-10-bit output,
247 * 19 bits for 16-bit output (in int32_t)
248 * @param lumFilterSize number of vertical luma/alpha input lines to scale
249 * @param chrFilter vertical chroma scaling coefficients, 12 bits [0,4096]
250 * @param chrUSrc scaled chroma (U) source data, 15 bits for 8-10-bit output,
251 * 19 bits for 16-bit output (in int32_t)
252 * @param chrVSrc scaled chroma (V) source data, 15 bits for 8-10-bit output,
253 * 19 bits for 16-bit output (in int32_t)
254 * @param chrFilterSize number of vertical chroma input lines to scale
255 * @param alpSrc scaled alpha (A) source data, 15 bits for 8-10-bit output,
256 * 19 bits for 16-bit output (in int32_t)
257 * @param dest pointer to the output plane. For 16-bit output, this is
258 * uint16_t
259 * @param dstW width of lumSrc and alpSrc in pixels, number of pixels
260 * to write into dest[]
261 * @param y vertical line number for this output. This does not need
262 * to be used to calculate the offset in the destination,
263 * but can be used to generate comfort noise using dithering
264 * or some output formats.
265 */
266 typedef void (*yuv2packedX_fn)(SwsInternal *c, const int16_t *lumFilter,
267 const int16_t **lumSrc, int lumFilterSize,
268 const int16_t *chrFilter,
269 const int16_t **chrUSrc,
270 const int16_t **chrVSrc, int chrFilterSize,
271 const int16_t **alpSrc, uint8_t *dest,
272 int dstW, int y);
273
274 /**
275 * Write one line of horizontally scaled Y/U/V/A to YUV/RGB
276 * output by doing multi-point vertical scaling between input pixels.
277 *
278 * @param c SWS scaling context
279 * @param lumFilter vertical luma/alpha scaling coefficients, 12 bits [0,4096]
280 * @param lumSrc scaled luma (Y) source data, 15 bits for 8-10-bit output,
281 * 19 bits for 16-bit output (in int32_t)
282 * @param lumFilterSize number of vertical luma/alpha input lines to scale
283 * @param chrFilter vertical chroma scaling coefficients, 12 bits [0,4096]
284 * @param chrUSrc scaled chroma (U) source data, 15 bits for 8-10-bit output,
285 * 19 bits for 16-bit output (in int32_t)
286 * @param chrVSrc scaled chroma (V) source data, 15 bits for 8-10-bit output,
287 * 19 bits for 16-bit output (in int32_t)
288 * @param chrFilterSize number of vertical chroma input lines to scale
289 * @param alpSrc scaled alpha (A) source data, 15 bits for 8-10-bit output,
290 * 19 bits for 16-bit output (in int32_t)
291 * @param dest pointer to the output planes. For 16-bit output, this is
292 * uint16_t
293 * @param dstW width of lumSrc and alpSrc in pixels, number of pixels
294 * to write into dest[]
295 * @param y vertical line number for this output. This does not need
296 * to be used to calculate the offset in the destination,
297 * but can be used to generate comfort noise using dithering
298 * or some output formats.
299 */
300 typedef void (*yuv2anyX_fn)(SwsInternal *c, const int16_t *lumFilter,
301 const int16_t **lumSrc, int lumFilterSize,
302 const int16_t *chrFilter,
303 const int16_t **chrUSrc,
304 const int16_t **chrVSrc, int chrFilterSize,
305 const int16_t **alpSrc, uint8_t **dest,
306 int dstW, int y);
307
308 /**
309 * Unscaled conversion of luma/alpha plane to YV12 for horizontal scaler.
310 */
311 typedef void (*planar1_YV12_fn)(uint8_t *dst, const uint8_t *src, const uint8_t *src2,
312 const uint8_t *src3, int width, uint32_t *pal,
313 void *opaque);
314
315 /**
316 * Unscaled conversion of chroma plane to YV12 for horizontal scaler.
317 */
318 typedef void (*planar2_YV12_fn)(uint8_t *dst, uint8_t *dst2, const uint8_t *src,
319 const uint8_t *src2, const uint8_t *src3,
320 int width, uint32_t *pal, void *opaque);
321
322 /**
323 * Unscaled conversion of arbitrary planar data (e.g. RGBA) to YV12, through
324 * conversion using the given color matrix.
325 */
326 typedef void (*planarX_YV12_fn)(uint8_t *dst, const uint8_t *src[4], int width,
327 int32_t *rgb2yuv, void *opaque);
328
329 typedef void (*planarX2_YV12_fn)(uint8_t *dst, uint8_t *dst2,
330 const uint8_t *src[4], int width,
331 int32_t *rgb2yuv, void *opaque);
332
333 struct SwsSlice;
334 struct SwsFilterDescriptor;
335
336 /* This struct should be aligned on at least a 32-byte boundary. */
337 struct SwsInternal {
338 /* Currently active user-facing options. Also contains AVClass */
339 SwsContext opts;
340
341 /* Parent context (for slice contexts) */
342 SwsContext *parent;
343
344 AVSliceThread *slicethread;
345 SwsContext **slice_ctx;
346 int nb_slice_ctx;
347
348 /* Scaling graph, reinitialized dynamically as needed. */
349 SwsGraph *graph[2]; /* top, bottom fields */
350
351 // values passed to current sws_receive_slice() call
352 int dst_slice_start;
353 int dst_slice_height;
354
355 /**
356 * Note that src, dst, srcStride, dstStride will be copied in the
357 * sws_scale() wrapper so they can be freely modified here.
358 */
359 SwsFunc convert_unscaled;
360 int chrSrcW; ///< Width of source chroma planes.
361 int chrSrcH; ///< Height of source chroma planes.
362 int chrDstW; ///< Width of destination chroma planes.
363 int chrDstH; ///< Height of destination chroma planes.
364 int lumXInc, chrXInc;
365 int lumYInc, chrYInc;
366 int dstFormatBpp; ///< Number of bits per pixel of the destination pixel format.
367 int srcFormatBpp; ///< Number of bits per pixel of the source pixel format.
368 int dstBpc, srcBpc;
369 int chrSrcHSubSample; ///< Binary logarithm of horizontal subsampling factor between luma/alpha and chroma planes in source image.
370 int chrSrcVSubSample; ///< Binary logarithm of vertical subsampling factor between luma/alpha and chroma planes in source image.
371 int chrDstHSubSample; ///< Binary logarithm of horizontal subsampling factor between luma/alpha and chroma planes in destination image.
372 int chrDstVSubSample; ///< Binary logarithm of vertical subsampling factor between luma/alpha and chroma planes in destination image.
373 int vChrDrop; ///< Binary logarithm of extra vertical subsampling factor in source image chroma planes specified by user.
374 int sliceDir; ///< Direction that slices are fed to the scaler (1 = top-to-bottom, -1 = bottom-to-top).
375
376 AVFrame *frame_src;
377 AVFrame *frame_dst;
378
379 RangeList src_ranges;
380
381 /* The cascaded_* fields allow splitting a scaler task into multiple
382 * sequential steps, this is for example used to limit the maximum
383 * downscaling factor that needs to be supported in one scaler.
384 */
385 SwsContext *cascaded_context[3];
386 int cascaded_tmpStride[2][4];
387 uint8_t *cascaded_tmp[2][4];
388 int cascaded_mainindex;
389
390 double gamma_value;
391 int is_internal_gamma;
392 uint16_t *gamma;
393 uint16_t *inv_gamma;
394
395 int numDesc;
396 int descIndex[2];
397 int numSlice;
398 struct SwsSlice *slice;
399 struct SwsFilterDescriptor *desc;
400
401 uint32_t pal_yuv[256];
402 uint32_t pal_rgb[256];
403
404 float uint2float_lut[256];
405
406 /**
407 * @name Scaled horizontal lines ring buffer.
408 * The horizontal scaler keeps just enough scaled lines in a ring buffer
409 * so they may be passed to the vertical scaler. The pointers to the
410 * allocated buffers for each line are duplicated in sequence in the ring
411 * buffer to simplify indexing and avoid wrapping around between lines
412 * inside the vertical scaler code. The wrapping is done before the
413 * vertical scaler is called.
414 */
415 //@{
416 int lastInLumBuf; ///< Last scaled horizontal luma/alpha line from source in the ring buffer.
417 int lastInChrBuf; ///< Last scaled horizontal chroma line from source in the ring buffer.
418 //@}
419
420 uint8_t *formatConvBuffer;
421 int needAlpha;
422
423 /**
424 * @name Horizontal and vertical filters.
425 * To better understand the following fields, here is a pseudo-code of
426 * their usage in filtering a horizontal line:
427 * @code
428 * for (i = 0; i < width; i++) {
429 * dst[i] = 0;
430 * for (j = 0; j < filterSize; j++)
431 * dst[i] += src[ filterPos[i] + j ] * filter[ filterSize * i + j ];
432 * dst[i] >>= FRAC_BITS; // The actual implementation is fixed-point.
433 * }
434 * @endcode
435 */
436 //@{
437 int16_t *hLumFilter; ///< Array of horizontal filter coefficients for luma/alpha planes.
438 int16_t *hChrFilter; ///< Array of horizontal filter coefficients for chroma planes.
439 int16_t *vLumFilter; ///< Array of vertical filter coefficients for luma/alpha planes.
440 int16_t *vChrFilter; ///< Array of vertical filter coefficients for chroma planes.
441 int32_t *hLumFilterPos; ///< Array of horizontal filter starting positions for each dst[i] for luma/alpha planes.
442 int32_t *hChrFilterPos; ///< Array of horizontal filter starting positions for each dst[i] for chroma planes.
443 int32_t *vLumFilterPos; ///< Array of vertical filter starting positions for each dst[i] for luma/alpha planes.
444 int32_t *vChrFilterPos; ///< Array of vertical filter starting positions for each dst[i] for chroma planes.
445 int hLumFilterSize; ///< Horizontal filter size for luma/alpha pixels.
446 int hChrFilterSize; ///< Horizontal filter size for chroma pixels.
447 int vLumFilterSize; ///< Vertical filter size for luma/alpha pixels.
448 int vChrFilterSize; ///< Vertical filter size for chroma pixels.
449 //@}
450
451 int lumMmxextFilterCodeSize; ///< Runtime-generated MMXEXT horizontal fast bilinear scaler code size for luma/alpha planes.
452 int chrMmxextFilterCodeSize; ///< Runtime-generated MMXEXT horizontal fast bilinear scaler code size for chroma planes.
453 uint8_t *lumMmxextFilterCode; ///< Runtime-generated MMXEXT horizontal fast bilinear scaler code for luma/alpha planes.
454 uint8_t *chrMmxextFilterCode; ///< Runtime-generated MMXEXT horizontal fast bilinear scaler code for chroma planes.
455
456 int canMMXEXTBeUsed;
457 int warned_unuseable_bilinear;
458
459 int dstY; ///< Last destination vertical line output from last slice.
460 void *yuvTable; // pointer to the yuv->rgb table start so it can be freed()
461 // alignment ensures the offset can be added in a single
462 // instruction on e.g. ARM
463 DECLARE_ALIGNED(16, int, table_gV)[256 + 2*YUVRGB_TABLE_HEADROOM];
464 uint8_t *table_rV[256 + 2*YUVRGB_TABLE_HEADROOM];
465 uint8_t *table_gU[256 + 2*YUVRGB_TABLE_HEADROOM];
466 uint8_t *table_bU[256 + 2*YUVRGB_TABLE_HEADROOM];
467 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
468 #define RY_IDX 0
469 #define GY_IDX 1
470 #define BY_IDX 2
471 #define RU_IDX 3
472 #define GU_IDX 4
473 #define BU_IDX 5
474 #define RV_IDX 6
475 #define GV_IDX 7
476 #define BV_IDX 8
477 #define RGB2YUV_SHIFT 15
478
479 int *dither_error[4];
480
481 //Colorspace stuff
482 int contrast, brightness, saturation; // for sws_getColorspaceDetails
483 int srcColorspaceTable[4];
484 int dstColorspaceTable[4];
485 int src0Alpha;
486 int dst0Alpha;
487 int srcXYZ;
488 int dstXYZ;
489 int yuv2rgb_y_offset;
490 int yuv2rgb_y_coeff;
491 int yuv2rgb_v2r_coeff;
492 int yuv2rgb_v2g_coeff;
493 int yuv2rgb_u2g_coeff;
494 int yuv2rgb_u2b_coeff;
495
496 #define RED_DITHER "0*8"
497 #define GREEN_DITHER "1*8"
498 #define BLUE_DITHER "2*8"
499 #define Y_COEFF "3*8"
500 #define VR_COEFF "4*8"
501 #define UB_COEFF "5*8"
502 #define VG_COEFF "6*8"
503 #define UG_COEFF "7*8"
504 #define Y_OFFSET "8*8"
505 #define U_OFFSET "9*8"
506 #define V_OFFSET "10*8"
507 #define LUM_MMX_FILTER_OFFSET "11*8"
508 #define CHR_MMX_FILTER_OFFSET "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)
509 #define DSTW_OFFSET "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2"
510 #define ESP_OFFSET "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+8"
511 #define VROUNDER_OFFSET "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+16"
512 #define U_TEMP "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+24"
513 #define V_TEMP "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+32"
514 #define Y_TEMP "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+40"
515 #define ALP_MMX_FILTER_OFFSET "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+48"
516 #define UV_OFF_PX "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*3+48"
517 #define UV_OFF_BYTE "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*3+56"
518 #define DITHER16 "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*3+64"
519 #define DITHER32 "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*3+80"
520 #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
521
522 DECLARE_ALIGNED(8, uint64_t, redDither);
523 DECLARE_ALIGNED(8, uint64_t, greenDither);
524 DECLARE_ALIGNED(8, uint64_t, blueDither);
525
526 DECLARE_ALIGNED(8, uint64_t, yCoeff);
527 DECLARE_ALIGNED(8, uint64_t, vrCoeff);
528 DECLARE_ALIGNED(8, uint64_t, ubCoeff);
529 DECLARE_ALIGNED(8, uint64_t, vgCoeff);
530 DECLARE_ALIGNED(8, uint64_t, ugCoeff);
531 DECLARE_ALIGNED(8, uint64_t, yOffset);
532 DECLARE_ALIGNED(8, uint64_t, uOffset);
533 DECLARE_ALIGNED(8, uint64_t, vOffset);
534 int32_t lumMmxFilter[4 * MAX_FILTER_SIZE];
535 int32_t chrMmxFilter[4 * MAX_FILTER_SIZE];
536 int dstW_mmx;
537 DECLARE_ALIGNED(8, uint64_t, esp);
538 DECLARE_ALIGNED(8, uint64_t, vRounder);
539 DECLARE_ALIGNED(8, uint64_t, u_temp);
540 DECLARE_ALIGNED(8, uint64_t, v_temp);
541 DECLARE_ALIGNED(8, uint64_t, y_temp);
542 int32_t alpMmxFilter[4 * MAX_FILTER_SIZE];
543 // alignment of these values is not necessary, but merely here
544 // to maintain the same offset across x8632 and x86-64. Once we
545 // use proper offset macros in the asm, they can be removed.
546 DECLARE_ALIGNED(8, ptrdiff_t, uv_off); ///< offset (in pixels) between u and v planes
547 DECLARE_ALIGNED(8, ptrdiff_t, uv_offx2); ///< offset (in bytes) between u and v planes
548 DECLARE_ALIGNED(8, uint16_t, dither16)[8];
549 DECLARE_ALIGNED(8, uint32_t, dither32)[8];
550
551 const uint8_t *chrDither8, *lumDither8;
552
553 int use_mmx_vfilter;
554
555 SwsColorFunc xyz12Torgb48;
556 SwsColorFunc rgb48Toxyz12;
557 SwsColorXform xyz2rgb;
558 SwsColorXform rgb2xyz;
559
560 /* function pointers for swscale() */
561 yuv2planar1_fn yuv2plane1;
562 yuv2planarX_fn yuv2planeX;
563 yuv2interleavedX_fn yuv2nv12cX;
564 yuv2packed1_fn yuv2packed1;
565 yuv2packed2_fn yuv2packed2;
566 yuv2packedX_fn yuv2packedX;
567 yuv2anyX_fn yuv2anyX;
568
569 /// Opaque data pointer passed to all input functions.
570 void *input_opaque;
571
572 planar1_YV12_fn lumToYV12;
573 planar1_YV12_fn alpToYV12;
574 planar2_YV12_fn chrToYV12;
575
576 /**
577 * Functions to read planar input, such as planar RGB, and convert
578 * internally to Y/UV/A.
579 */
580 /** @{ */
581 planarX_YV12_fn readLumPlanar;
582 planarX_YV12_fn readAlpPlanar;
583 planarX2_YV12_fn readChrPlanar;
584 /** @} */
585
586 /**
587 * Scale one horizontal line of input data using a bilinear filter
588 * to produce one line of output data. Compared to SwsInternal->hScale(),
589 * please take note of the following caveats when using these:
590 * - Scaling is done using only 7 bits instead of 14-bit coefficients.
591 * - You can use no more than 5 input pixels to produce 4 output
592 * pixels. Therefore, this filter should not be used for downscaling
593 * by more than ~20% in width (because that equals more than 5/4th
594 * downscaling and thus more than 5 pixels input per 4 pixels output).
595 * - In general, bilinear filters create artifacts during downscaling
596 * (even when <20%), because one output pixel will span more than one
597 * input pixel, and thus some pixels will need edges of both neighbor
598 * pixels to interpolate the output pixel. Since you can use at most
599 * two input pixels per output pixel in bilinear scaling, this is
600 * impossible and thus downscaling by any size will create artifacts.
601 * To enable this type of scaling, set SWS_FAST_BILINEAR
602 * in SwsInternal->flags.
603 */
604 /** @{ */
605 void (*hyscale_fast)(SwsInternal *c,
606 int16_t *dst, int dstWidth,
607 const uint8_t *src, int srcW, int xInc);
608 void (*hcscale_fast)(SwsInternal *c,
609 int16_t *dst1, int16_t *dst2, int dstWidth,
610 const uint8_t *src1, const uint8_t *src2,
611 int srcW, int xInc);
612 /** @} */
613
614 /**
615 * Scale one horizontal line of input data using a filter over the input
616 * lines, to produce one (differently sized) line of output data.
617 *
618 * @param dst pointer to destination buffer for horizontally scaled
619 * data. If the number of bits per component of one
620 * destination pixel (SwsInternal->dstBpc) is <= 10, data
621 * will be 15 bpc in 16 bits (int16_t) width. Else (i.e.
622 * SwsInternal->dstBpc == 16), data will be 19bpc in
623 * 32 bits (int32_t) width.
624 * @param dstW width of destination image
625 * @param src pointer to source data to be scaled. If the number of
626 * bits per component of a source pixel (SwsInternal->srcBpc)
627 * is 8, this is 8bpc in 8 bits (uint8_t) width. Else
628 * (i.e. SwsInternal->dstBpc > 8), this is native depth
629 * in 16 bits (uint16_t) width. In other words, for 9-bit
630 * YUV input, this is 9bpc, for 10-bit YUV input, this is
631 * 10bpc, and for 16-bit RGB or YUV, this is 16bpc.
632 * @param filter filter coefficients to be used per output pixel for
633 * scaling. This contains 14bpp filtering coefficients.
634 * Guaranteed to contain dstW * filterSize entries.
635 * @param filterPos position of the first input pixel to be used for
636 * each output pixel during scaling. Guaranteed to
637 * contain dstW entries.
638 * @param filterSize the number of input coefficients to be used (and
639 * thus the number of input pixels to be used) for
640 * creating a single output pixel. Is aligned to 4
641 * (and input coefficients thus padded with zeroes)
642 * to simplify creating SIMD code.
643 */
644 /** @{ */
645 void (*hyScale)(SwsInternal *c, int16_t *dst, int dstW,
646 const uint8_t *src, const int16_t *filter,
647 const int32_t *filterPos, int filterSize);
648 void (*hcScale)(SwsInternal *c, int16_t *dst, int dstW,
649 const uint8_t *src, const int16_t *filter,
650 const int32_t *filterPos, int filterSize);
651 /** @} */
652
653 /**
654 * Color range conversion functions if needed.
655 * If SwsInternal->dstBpc is > 14:
656 * - int16_t *dst (data is 15 bpc)
657 * - uint16_t coeff
658 * - int32_t offset
659 * Otherwise (SwsInternal->dstBpc is <= 14):
660 * - int32_t *dst (data is 19 bpc)
661 * - uint32_t coeff
662 * - int64_t offset
663 */
664 /** @{ */
665 void (*lumConvertRange)(int16_t *dst, int width,
666 uint32_t coeff, int64_t offset);
667 void (*chrConvertRange)(int16_t *dst1, int16_t *dst2, int width,
668 uint32_t coeff, int64_t offset);
669 /** @} */
670
671 uint32_t lumConvertRange_coeff;
672 uint32_t chrConvertRange_coeff;
673 int64_t lumConvertRange_offset;
674 int64_t chrConvertRange_offset;
675
676 int needs_hcscale; ///< Set if there are chroma planes to be converted.
677
678 // scratch buffer for converting packed rgb0 sources
679 // filled with a copy of the input frame + fully opaque alpha,
680 // then passed as input to further conversion
681 uint8_t *rgb0_scratch;
682 unsigned int rgb0_scratch_allocated;
683
684 // scratch buffer for converting XYZ sources
685 // filled with the input converted to rgb48
686 // then passed as input to further conversion
687 uint8_t *xyz_scratch;
688 unsigned int xyz_scratch_allocated;
689
690 unsigned int dst_slice_align;
691 atomic_int stride_unaligned_warned;
692 atomic_int data_unaligned_warned;
693 int color_conversion_warned;
694
695 Half2FloatTables *h2f_tables;
696
697 // Hardware specific private data
698 void *hw_priv; /* refstruct */
699
700 int is_legacy_init;
701
702 FFFramePool frame_pool; /* for sws_scale_frame() data allocations */
703 };
704 //FIXME check init (where 0)
705
706 static_assert(offsetof(SwsInternal, redDither) + DITHER32_INT == offsetof(SwsInternal, dither32),
707 "dither32 must be at the same offset as redDither + DITHER32_INT");
708
709 #if ARCH_X86_64
710 /* x86 yuv2gbrp uses the SwsInternal for yuv coefficients
711 if struct offsets change the asm needs to be updated too */
712 static_assert(offsetof(SwsInternal, yuv2rgb_y_offset) == 40348,
713 "yuv2rgb_y_offset must be updated in x86 asm");
714 #endif
715
716 SwsFunc ff_yuv2rgb_get_func_ptr(SwsInternal *c);
717 int ff_yuv2rgb_c_init_tables(SwsInternal *c, const int inv_table[4],
718 int fullRange, int brightness,
719 int contrast, int saturation);
720 void ff_yuv2rgb_init_tables_ppc(SwsInternal *c, const int inv_table[4],
721 int brightness, int contrast, int saturation);
722
723 void ff_updateMMXDitherTables(SwsInternal *c, int dstY);
724
725 void ff_update_palette(SwsInternal *c, const uint32_t *pal);
726
727 av_cold void ff_sws_init_range_convert(SwsInternal *c);
728 av_cold void ff_sws_init_range_convert_aarch64(SwsInternal *c);
729 av_cold void ff_sws_init_range_convert_loongarch(SwsInternal *c);
730 av_cold void ff_sws_init_range_convert_riscv(SwsInternal *c);
731 av_cold void ff_sws_init_range_convert_x86(SwsInternal *c);
732
733 av_cold void ff_sws_init_xyzdsp(SwsInternal *c);
734 av_cold void ff_sws_init_xyzdsp_aarch64(SwsInternal *c);
735
736 av_cold int ff_sws_fill_st428_tables(SwsInternal *c);
737
738 SwsFunc ff_yuv2rgb_init_x86(SwsInternal *c);
739 SwsFunc ff_yuv2rgb_init_ppc(SwsInternal *c);
740 SwsFunc ff_yuv2rgb_init_loongarch(SwsInternal *c);
741 SwsFunc ff_yuv2rgb_init_aarch64(SwsInternal *c);
742
743 1291604 static av_always_inline int is16BPS(enum AVPixelFormat pix_fmt)
744 {
745 1291604 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
746
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1291604 times.
1291604 av_assert0(desc);
747 1291604 return desc->comp[0].depth == 16;
748 }
749
750 75079 static av_always_inline int is32BPS(enum AVPixelFormat pix_fmt)
751 {
752 75079 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
753
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 75079 times.
75079 av_assert0(desc);
754 75079 return desc->comp[0].depth == 32;
755 }
756
757 1619818 static av_always_inline int isNBPS(enum AVPixelFormat pix_fmt)
758 {
759 1619818 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
760
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1619818 times.
1619818 av_assert0(desc);
761
4/4
✓ Branch 0 taken 591193 times.
✓ Branch 1 taken 1028625 times.
✓ Branch 2 taken 439305 times.
✓ Branch 3 taken 151888 times.
1619818 return desc->comp[0].depth >= 9 && desc->comp[0].depth <= 14;
762 }
763
764 6809353 static av_always_inline int isBE(enum AVPixelFormat pix_fmt)
765 {
766 6809353 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
767
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6809353 times.
6809353 av_assert0(desc);
768 6809353 return desc->flags & AV_PIX_FMT_FLAG_BE;
769 }
770
771 6814718 static av_always_inline int isYUV(enum AVPixelFormat pix_fmt)
772 {
773 6814718 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
774
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6814718 times.
6814718 av_assert0(desc);
775
4/4
✓ Branch 0 taken 6177685 times.
✓ Branch 1 taken 637033 times.
✓ Branch 2 taken 6115846 times.
✓ Branch 3 taken 61839 times.
6814718 return !(desc->flags & AV_PIX_FMT_FLAG_RGB) && desc->nb_components >= 2;
776 }
777
778 5403321 static av_always_inline int isPlanarYUV(enum AVPixelFormat pix_fmt)
779 {
780 5403321 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
781
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5403321 times.
5403321 av_assert0(desc);
782
4/4
✓ Branch 0 taken 4705391 times.
✓ Branch 1 taken 697930 times.
✓ Branch 3 taken 4449169 times.
✓ Branch 4 taken 256222 times.
5403321 return ((desc->flags & AV_PIX_FMT_FLAG_PLANAR) && isYUV(pix_fmt));
783 }
784
785 /*
786 * Identity semi-planar YUV formats. Specifically, those are YUV formats
787 * where the second and third components (U & V) are on the same plane.
788 */
789 2685359 static av_always_inline int isSemiPlanarYUV(enum AVPixelFormat pix_fmt)
790 {
791 2685359 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
792
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2685359 times.
2685359 av_assert0(desc);
793
4/4
✓ Branch 1 taken 2312084 times.
✓ Branch 2 taken 373275 times.
✓ Branch 3 taken 931904 times.
✓ Branch 4 taken 1380180 times.
2685359 return (isPlanarYUV(pix_fmt) && desc->comp[1].plane == desc->comp[2].plane);
794 }
795
796 270 static av_always_inline int isRGB(enum AVPixelFormat pix_fmt)
797 {
798 270 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
799
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 270 times.
270 av_assert0(desc);
800 270 return (desc->flags & AV_PIX_FMT_FLAG_RGB);
801 }
802
803 3265370 static av_always_inline int isGray(enum AVPixelFormat pix_fmt)
804 {
805 3265370 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
806
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3265370 times.
3265370 av_assert0(desc);
807 6527997 return !(desc->flags & AV_PIX_FMT_FLAG_PAL) &&
808
2/2
✓ Branch 0 taken 3262610 times.
✓ Branch 1 taken 17 times.
3262627 !(desc->flags & AV_PIX_FMT_FLAG_HWACCEL) &&
809
4/4
✓ Branch 0 taken 249391 times.
✓ Branch 1 taken 3013219 times.
✓ Branch 2 taken 233710 times.
✓ Branch 3 taken 15681 times.
3262610 desc->nb_components <= 2 &&
810
4/4
✓ Branch 0 taken 3262627 times.
✓ Branch 1 taken 2743 times.
✓ Branch 2 taken 217255 times.
✓ Branch 3 taken 16455 times.
6527997 pix_fmt != AV_PIX_FMT_MONOBLACK &&
811 pix_fmt != AV_PIX_FMT_MONOWHITE;
812 }
813
814 42440 static av_always_inline int isRGBinInt(enum AVPixelFormat pix_fmt)
815 {
816
2/2
✓ Branch 0 taken 41934 times.
✓ Branch 1 taken 493 times.
42427 return pix_fmt == AV_PIX_FMT_RGB48BE ||
817
2/2
✓ Branch 0 taken 37084 times.
✓ Branch 1 taken 4850 times.
41934 pix_fmt == AV_PIX_FMT_RGB48LE ||
818
2/2
✓ Branch 0 taken 37079 times.
✓ Branch 1 taken 5 times.
37084 pix_fmt == AV_PIX_FMT_RGB32 ||
819
2/2
✓ Branch 0 taken 18038 times.
✓ Branch 1 taken 19041 times.
37079 pix_fmt == AV_PIX_FMT_RGB32_1 ||
820
2/2
✓ Branch 0 taken 18037 times.
✓ Branch 1 taken 1 times.
18038 pix_fmt == AV_PIX_FMT_RGB24 ||
821
2/2
✓ Branch 0 taken 15962 times.
✓ Branch 1 taken 2075 times.
18037 pix_fmt == AV_PIX_FMT_RGB565BE ||
822
2/2
✓ Branch 0 taken 15961 times.
✓ Branch 1 taken 1 times.
15962 pix_fmt == AV_PIX_FMT_RGB565LE ||
823
2/2
✓ Branch 0 taken 6967 times.
✓ Branch 1 taken 8994 times.
15961 pix_fmt == AV_PIX_FMT_RGB555BE ||
824
2/2
✓ Branch 0 taken 6966 times.
✓ Branch 1 taken 1 times.
6967 pix_fmt == AV_PIX_FMT_RGB555LE ||
825
2/2
✓ Branch 0 taken 6965 times.
✓ Branch 1 taken 1 times.
6966 pix_fmt == AV_PIX_FMT_RGB444BE ||
826
2/2
✓ Branch 0 taken 6962 times.
✓ Branch 1 taken 3 times.
6965 pix_fmt == AV_PIX_FMT_RGB444LE ||
827
2/2
✓ Branch 0 taken 6961 times.
✓ Branch 1 taken 1 times.
6962 pix_fmt == AV_PIX_FMT_RGB8 ||
828
2/2
✓ Branch 0 taken 6959 times.
✓ Branch 1 taken 2 times.
6961 pix_fmt == AV_PIX_FMT_RGB4 ||
829
2/2
✓ Branch 0 taken 6946 times.
✓ Branch 1 taken 13 times.
6959 pix_fmt == AV_PIX_FMT_RGB4_BYTE ||
830
2/2
✓ Branch 0 taken 6943 times.
✓ Branch 1 taken 3 times.
6946 pix_fmt == AV_PIX_FMT_RGBA64BE ||
831
2/2
✓ Branch 0 taken 6842 times.
✓ Branch 1 taken 101 times.
6943 pix_fmt == AV_PIX_FMT_RGBA64LE ||
832
4/4
✓ Branch 0 taken 42427 times.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 101 times.
✓ Branch 3 taken 6741 times.
84867 pix_fmt == AV_PIX_FMT_MONOBLACK ||
833 pix_fmt == AV_PIX_FMT_MONOWHITE;
834 }
835
836 28909 static av_always_inline int isBGRinInt(enum AVPixelFormat pix_fmt)
837 {
838
2/2
✓ Branch 0 taken 28905 times.
✓ Branch 1 taken 3 times.
28908 return pix_fmt == AV_PIX_FMT_BGR48BE ||
839
2/2
✓ Branch 0 taken 28573 times.
✓ Branch 1 taken 332 times.
28905 pix_fmt == AV_PIX_FMT_BGR48LE ||
840
2/2
✓ Branch 0 taken 28511 times.
✓ Branch 1 taken 62 times.
28573 pix_fmt == AV_PIX_FMT_BGR32 ||
841
2/2
✓ Branch 0 taken 26972 times.
✓ Branch 1 taken 1539 times.
28511 pix_fmt == AV_PIX_FMT_BGR32_1 ||
842
2/2
✓ Branch 0 taken 26971 times.
✓ Branch 1 taken 1 times.
26972 pix_fmt == AV_PIX_FMT_BGR24 ||
843
2/2
✓ Branch 0 taken 26970 times.
✓ Branch 1 taken 1 times.
26971 pix_fmt == AV_PIX_FMT_BGR565BE ||
844
2/2
✓ Branch 0 taken 26969 times.
✓ Branch 1 taken 1 times.
26970 pix_fmt == AV_PIX_FMT_BGR565LE ||
845
2/2
✓ Branch 0 taken 23778 times.
✓ Branch 1 taken 3191 times.
26969 pix_fmt == AV_PIX_FMT_BGR555BE ||
846
2/2
✓ Branch 0 taken 23777 times.
✓ Branch 1 taken 1 times.
23778 pix_fmt == AV_PIX_FMT_BGR555LE ||
847
2/2
✓ Branch 0 taken 23776 times.
✓ Branch 1 taken 1 times.
23777 pix_fmt == AV_PIX_FMT_BGR444BE ||
848
2/2
✓ Branch 0 taken 23773 times.
✓ Branch 1 taken 3 times.
23776 pix_fmt == AV_PIX_FMT_BGR444LE ||
849
2/2
✓ Branch 0 taken 23772 times.
✓ Branch 1 taken 1 times.
23773 pix_fmt == AV_PIX_FMT_BGR8 ||
850
2/2
✓ Branch 0 taken 23770 times.
✓ Branch 1 taken 2 times.
23772 pix_fmt == AV_PIX_FMT_BGR4 ||
851
2/2
✓ Branch 0 taken 23769 times.
✓ Branch 1 taken 1 times.
23770 pix_fmt == AV_PIX_FMT_BGR4_BYTE ||
852
2/2
✓ Branch 0 taken 23766 times.
✓ Branch 1 taken 3 times.
23769 pix_fmt == AV_PIX_FMT_BGRA64BE ||
853
2/2
✓ Branch 0 taken 23685 times.
✓ Branch 1 taken 81 times.
23766 pix_fmt == AV_PIX_FMT_BGRA64LE ||
854
4/4
✓ Branch 0 taken 28908 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 81 times.
✓ Branch 3 taken 23604 times.
57817 pix_fmt == AV_PIX_FMT_MONOBLACK ||
855 pix_fmt == AV_PIX_FMT_MONOWHITE;
856 }
857
858 280419 static av_always_inline int isBayer(enum AVPixelFormat pix_fmt)
859 {
860 280419 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
861
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 280419 times.
280419 av_assert0(desc);
862 280419 return !!(desc->flags & AV_PIX_FMT_FLAG_BAYER);
863 }
864
865 ✗ static av_always_inline int isBayer16BPS(enum AVPixelFormat pix_fmt)
866 {
867 ✗ const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
868 ✗ av_assert0(desc);
869 ✗ return desc->comp[1].depth == 8;
870 }
871
872 39497074 static av_always_inline int isAnyRGB(enum AVPixelFormat pix_fmt)
873 {
874 39497074 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
875
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39497074 times.
39497074 av_assert0(desc);
876
2/2
✓ Branch 0 taken 22843278 times.
✓ Branch 1 taken 101556 times.
22944834 return (desc->flags & AV_PIX_FMT_FLAG_RGB) ||
877
4/4
✓ Branch 0 taken 22944834 times.
✓ Branch 1 taken 16552240 times.
✓ Branch 2 taken 112307 times.
✓ Branch 3 taken 22730971 times.
62441908 pix_fmt == AV_PIX_FMT_MONOBLACK || pix_fmt == AV_PIX_FMT_MONOWHITE;
878 }
879
880 1000490 static av_always_inline int isFloat(enum AVPixelFormat pix_fmt)
881 {
882 1000490 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
883
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1000490 times.
1000490 av_assert0(desc);
884 1000490 return desc->flags & AV_PIX_FMT_FLAG_FLOAT;
885 }
886
887 317006 static av_always_inline int isFloat16(enum AVPixelFormat pix_fmt)
888 {
889 317006 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
890
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 317006 times.
317006 av_assert0(desc);
891
4/4
✓ Branch 0 taken 1313 times.
✓ Branch 1 taken 315693 times.
✓ Branch 2 taken 822 times.
✓ Branch 3 taken 491 times.
317006 return (desc->flags & AV_PIX_FMT_FLAG_FLOAT) && desc->comp[0].depth == 16;
892 }
893
894 1449126 static av_always_inline int isALPHA(enum AVPixelFormat pix_fmt)
895 {
896 1449126 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
897
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1449126 times.
1449126 av_assert0(desc);
898
2/2
✓ Branch 0 taken 1079 times.
✓ Branch 1 taken 1448047 times.
1449126 if (pix_fmt == AV_PIX_FMT_PAL8)
899 1079 return 1;
900 1448047 return desc->flags & AV_PIX_FMT_FLAG_ALPHA;
901 }
902
903 689426 static av_always_inline int isPacked(enum AVPixelFormat pix_fmt)
904 {
905 689426 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
906
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 689426 times.
689426 av_assert0(desc);
907
4/4
✓ Branch 0 taken 490225 times.
✓ Branch 1 taken 159937 times.
✓ Branch 2 taken 528547 times.
✓ Branch 3 taken 942 times.
689426 return (desc->nb_components >= 2 && !(desc->flags & AV_PIX_FMT_FLAG_PLANAR)) ||
908
2/2
✓ Branch 0 taken 524904 times.
✓ Branch 1 taken 3643 times.
528547 pix_fmt == AV_PIX_FMT_PAL8 ||
909
4/4
✓ Branch 0 taken 650162 times.
✓ Branch 1 taken 39264 times.
✓ Branch 2 taken 3754 times.
✓ Branch 3 taken 521150 times.
1378852 pix_fmt == AV_PIX_FMT_MONOBLACK || pix_fmt == AV_PIX_FMT_MONOWHITE;
910 }
911
912 700010 static av_always_inline int isPlanar(enum AVPixelFormat pix_fmt)
913 {
914 700010 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
915
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 700010 times.
700010 av_assert0(desc);
916
4/4
✓ Branch 0 taken 670874 times.
✓ Branch 1 taken 29136 times.
✓ Branch 2 taken 530830 times.
✓ Branch 3 taken 140044 times.
700010 return (desc->nb_components >= 2 && (desc->flags & AV_PIX_FMT_FLAG_PLANAR));
917 }
918
919 89696 static av_always_inline int isPackedRGB(enum AVPixelFormat pix_fmt)
920 {
921 89696 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
922
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 89696 times.
89696 av_assert0(desc);
923 89696 return ((desc->flags & (AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB)) == AV_PIX_FMT_FLAG_RGB);
924 }
925
926 223602 static av_always_inline int isPlanarRGB(enum AVPixelFormat pix_fmt)
927 {
928 223602 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
929
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 223602 times.
223602 av_assert0(desc);
930 223602 return ((desc->flags & (AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB)) ==
931 (AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB));
932 }
933
934 1125768 static av_always_inline int usePal(enum AVPixelFormat pix_fmt)
935 {
936
2/2
✓ Branch 0 taken 85121 times.
✓ Branch 1 taken 1040647 times.
1125768 switch (pix_fmt) {
937 85121 case AV_PIX_FMT_PAL8:
938 case AV_PIX_FMT_BGR4_BYTE:
939 case AV_PIX_FMT_BGR8:
940 case AV_PIX_FMT_GRAY8:
941 case AV_PIX_FMT_RGB4_BYTE:
942 case AV_PIX_FMT_RGB8:
943 85121 return 1;
944 1040647 default:
945 1040647 return 0;
946 }
947 }
948
949 /*
950 * Identity formats where the data is in the high bits, and the low bits are shifted away.
951 */
952 554219 static av_always_inline int isDataInHighBits(enum AVPixelFormat pix_fmt)
953 {
954 int i;
955 554219 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
956
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 554219 times.
554219 av_assert0(desc);
957
2/2
✓ Branch 0 taken 2815 times.
✓ Branch 1 taken 551404 times.
554219 if (desc->flags & (AV_PIX_FMT_FLAG_BITSTREAM | AV_PIX_FMT_FLAG_HWACCEL))
958 2815 return 0;
959
2/2
✓ Branch 0 taken 607890 times.
✓ Branch 1 taken 25216 times.
633106 for (i = 0; i < desc->nb_components; i++) {
960
2/2
✓ Branch 0 taken 510578 times.
✓ Branch 1 taken 97312 times.
607890 if (!desc->comp[i].shift)
961 510578 return 0;
962
2/2
✓ Branch 0 taken 15610 times.
✓ Branch 1 taken 81702 times.
97312 if ((desc->comp[i].shift + desc->comp[i].depth) & 0x7)
963 15610 return 0;
964 }
965 25216 return 1;
966 }
967
968 /*
969 * Identity formats where the chroma planes are swapped (CrCb order).
970 */
971 876488 static av_always_inline int isSwappedChroma(enum AVPixelFormat pix_fmt)
972 {
973 876488 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
974
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 876488 times.
876488 av_assert0(desc);
975
2/2
✓ Branch 1 taken 135 times.
✓ Branch 2 taken 876353 times.
876488 if (!isYUV(pix_fmt))
976 135 return 0;
977
4/4
✓ Branch 0 taken 9583 times.
✓ Branch 1 taken 866770 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 9576 times.
876353 if ((desc->flags & AV_PIX_FMT_FLAG_ALPHA) && desc->nb_components < 4)
978 7 return 0;
979
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 876346 times.
876346 if (desc->nb_components < 3)
980 ✗ return 0;
981
4/4
✓ Branch 1 taken 876319 times.
✓ Branch 2 taken 27 times.
✓ Branch 4 taken 859155 times.
✓ Branch 5 taken 17164 times.
876346 if (!isPlanarYUV(pix_fmt) || isSemiPlanarYUV(pix_fmt))
982 859182 return desc->comp[1].offset > desc->comp[2].offset;
983 else
984 17164 return desc->comp[1].plane > desc->comp[2].plane;
985 }
986
987 extern const uint64_t ff_dither4[2];
988 extern const uint64_t ff_dither8[2];
989
990 extern const uint8_t ff_dither_2x2_4[3][8];
991 extern const uint8_t ff_dither_2x2_8[3][8];
992 extern const uint8_t ff_dither_4x4_16[5][8];
993 extern const uint8_t ff_dither_8x8_32[9][8];
994 extern const uint8_t ff_dither_8x8_73[9][8];
995 extern const uint8_t ff_dither_8x8_128[9][8];
996 extern const uint8_t ff_dither_8x8_220[9][8];
997
998 extern const int32_t ff_yuv2rgb_coeffs[11][4];
999
1000 extern const AVClass ff_sws_context_class;
1001
1002 int ff_sws_init_single_context(SwsContext *sws, SwsFilter *srcFilter,
1003 SwsFilter *dstFilter);
1004
1005 /**
1006 * Set c->convert_unscaled to an unscaled converter if one exists for the
1007 * specific source and destination formats, bit depths, flags, etc.
1008 */
1009 void ff_get_unscaled_swscale(SwsInternal *c);
1010 void ff_get_unscaled_swscale_ppc(SwsInternal *c);
1011 void ff_get_unscaled_swscale_arm(SwsInternal *c);
1012 void ff_get_unscaled_swscale_aarch64(SwsInternal *c);
1013 void ff_get_unscaled_swscale_loongarch(SwsInternal *c);
1014
1015 void ff_sws_init_scale(SwsInternal *c);
1016
1017 void ff_sws_init_input_funcs(SwsInternal *c,
1018 planar1_YV12_fn *lumToYV12,
1019 planar1_YV12_fn *alpToYV12,
1020 planar2_YV12_fn *chrToYV12,
1021 planarX_YV12_fn *readLumPlanar,
1022 planarX_YV12_fn *readAlpPlanar,
1023 planarX2_YV12_fn *readChrPlanar);
1024 void ff_sws_init_output_funcs(SwsInternal *c,
1025 yuv2planar1_fn *yuv2plane1,
1026 yuv2planarX_fn *yuv2planeX,
1027 yuv2interleavedX_fn *yuv2nv12cX,
1028 yuv2packed1_fn *yuv2packed1,
1029 yuv2packed2_fn *yuv2packed2,
1030 yuv2packedX_fn *yuv2packedX,
1031 yuv2anyX_fn *yuv2anyX);
1032 void ff_sws_init_swscale_ppc(SwsInternal *c);
1033 void ff_sws_init_swscale_vsx(SwsInternal *c);
1034 void ff_sws_init_swscale_x86(SwsInternal *c);
1035 void ff_sws_init_swscale_aarch64(SwsInternal *c);
1036 void ff_sws_init_swscale_arm(SwsInternal *c);
1037 void ff_sws_init_swscale_loongarch(SwsInternal *c);
1038 void ff_sws_init_swscale_riscv(SwsInternal *c);
1039
1040 int ff_sws_init_altivec_bufs(SwsInternal *c);
1041 void ff_sws_free_altivec_bufs(SwsInternal *c);
1042
1043 void ff_hyscale_fast_c(SwsInternal *c, int16_t *dst, int dstWidth,
1044 const uint8_t *src, int srcW, int xInc);
1045 void ff_hcscale_fast_c(SwsInternal *c, int16_t *dst1, int16_t *dst2,
1046 int dstWidth, const uint8_t *src1,
1047 const uint8_t *src2, int srcW, int xInc);
1048 int ff_init_hscaler_mmxext(int dstW, int xInc, uint8_t *filterCode,
1049 int16_t *filter, int32_t *filterPos,
1050 int numSplits);
1051 void ff_hyscale_fast_mmxext(SwsInternal *c, int16_t *dst,
1052 int dstWidth, const uint8_t *src,
1053 int srcW, int xInc);
1054 void ff_hcscale_fast_mmxext(SwsInternal *c, int16_t *dst1, int16_t *dst2,
1055 int dstWidth, const uint8_t *src1,
1056 const uint8_t *src2, int srcW, int xInc);
1057
1058 int ff_sws_alphablendaway(SwsInternal *c, const uint8_t *const src[],
1059 const int srcStride[], int srcSliceY, int srcSliceH,
1060 uint8_t *const dst[], const int dstStride[]);
1061
1062 void ff_copyPlane(const uint8_t *src, int srcStride,
1063 int srcSliceY, int srcSliceH, int width,
1064 uint8_t *dst, int dstStride);
1065
1066 1371 static inline void fillPlane16(uint8_t *plane, int stride, int width, int height, int y,
1067 int alpha, int bits, const int big_endian)
1068 {
1069 1371 uint8_t *ptr = plane + stride * y;
1070
1/2
✓ Branch 0 taken 1371 times.
✗ Branch 1 not taken.
1371 int v = alpha ? 0xFFFF>>(16-bits) : (1<<(bits-1));
1071
2/2
✓ Branch 0 taken 514 times.
✓ Branch 1 taken 857 times.
1371 if (big_endian != HAVE_BIGENDIAN)
1072 514 v = av_bswap16(v);
1073
2/2
✓ Branch 0 taken 360644 times.
✓ Branch 1 taken 1371 times.
362015 for (int i = 0; i < height; i++) {
1074
2/2
✓ Branch 0 taken 126086600 times.
✓ Branch 1 taken 360644 times.
126447244 for (int j = 0; j < width; j++)
1075 126086600 AV_WN16(ptr + 2 * j, v);
1076 360644 ptr += stride;
1077 }
1078 1371 }
1079
1080 120 static inline void fillPlane32(uint8_t *plane, int stride, int width, int height, int y,
1081 int alpha, int bits, const int big_endian, int is_float)
1082 {
1083 120 uint8_t *ptr = plane + stride * y;
1084 uint32_t v;
1085 120 uint32_t onef32 = 0x3f800000;
1086
1/2
✓ Branch 0 taken 120 times.
✗ Branch 1 not taken.
120 if (is_float)
1087
1/2
✓ Branch 0 taken 120 times.
✗ Branch 1 not taken.
120 v = alpha ? onef32 : 0;
1088 else
1089 ✗ v = alpha ? 0xFFFFFFFF>>(32-bits) : (1<<(bits-1));
1090
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 88 times.
120 if (big_endian != HAVE_BIGENDIAN)
1091 32 v = av_bswap32(v);
1092
1093
2/2
✓ Branch 0 taken 34560 times.
✓ Branch 1 taken 120 times.
34680 for (int i = 0; i < height; i++) {
1094
2/2
✓ Branch 0 taken 12165120 times.
✓ Branch 1 taken 34560 times.
12199680 for (int j = 0; j < width; j++)
1095 12165120 AV_WN32(ptr + 4 * j, v);
1096 34560 ptr += stride;
1097 }
1098 120 }
1099
1100
1101 #define MAX_SLICE_PLANES 4
1102
1103 /// Slice plane
1104 typedef struct SwsPlane
1105 {
1106 int available_lines; ///< max number of lines that can be hold by this plane
1107 int sliceY; ///< index of first line
1108 int sliceH; ///< number of lines
1109 uint8_t **line; ///< line buffer
1110 uint8_t **tmp; ///< Tmp line buffer used by mmx code
1111 } SwsPlane;
1112
1113 /**
1114 * Struct which defines a slice of an image to be scaled or an output for
1115 * a scaled slice.
1116 * A slice can also be used as intermediate ring buffer for scaling steps.
1117 */
1118 typedef struct SwsSlice
1119 {
1120 int width; ///< Slice line width
1121 int h_chr_sub_sample; ///< horizontal chroma subsampling factor
1122 int v_chr_sub_sample; ///< vertical chroma subsampling factor
1123 int is_ring; ///< flag to identify if this slice is a ring buffer
1124 int should_free_lines; ///< flag to identify if there are dynamic allocated lines
1125 enum AVPixelFormat fmt; ///< planes pixel format
1126 SwsPlane plane[MAX_SLICE_PLANES]; ///< color planes
1127 } SwsSlice;
1128
1129 /**
1130 * Struct which holds all necessary data for processing a slice.
1131 * A processing step can be a color conversion or horizontal/vertical scaling.
1132 */
1133 typedef struct SwsFilterDescriptor
1134 {
1135 SwsSlice *src; ///< Source slice
1136 SwsSlice *dst; ///< Output slice
1137
1138 int alpha; ///< Flag for processing alpha channel
1139 void *instance; ///< Filter instance data
1140
1141 /// Function for processing input slice sliceH lines starting from line sliceY
1142 int (*process)(SwsInternal *c, struct SwsFilterDescriptor *desc, int sliceY, int sliceH);
1143 } SwsFilterDescriptor;
1144
1145 // warp input lines in the form (src + width*i + j) to slice format (line[i][j])
1146 // relative=true means first line src[x][0] otherwise first line is src[x][lum/crh Y]
1147 int ff_init_slice_from_src(SwsSlice * s, uint8_t *const src[4], const int stride[4],
1148 int srcW, int lumY, int lumH, int chrY, int chrH, int relative);
1149
1150 // Initialize scaler filter descriptor chain
1151 int ff_init_filters(SwsInternal *c);
1152
1153 // Free all filter data
1154 int ff_free_filters(SwsInternal *c);
1155
1156 /*
1157 function for applying ring buffer logic into slice s
1158 It checks if the slice can hold more @lum lines, if yes
1159 do nothing otherwise remove @lum least used lines.
1160 It applies the same procedure for @chr lines.
1161 */
1162 int ff_rotate_slice(SwsSlice *s, int lum, int chr);
1163
1164 /// initializes gamma conversion descriptor
1165 int ff_init_gamma_convert(SwsFilterDescriptor *desc, SwsSlice * src, uint16_t *table);
1166
1167 /// initializes lum pixel format conversion descriptor
1168 int ff_init_desc_fmt_convert(SwsFilterDescriptor *desc, SwsSlice * src, SwsSlice *dst, uint32_t *pal);
1169
1170 /// initializes lum horizontal scaling descriptor
1171 int ff_init_desc_hscale(SwsFilterDescriptor *desc, SwsSlice *src, SwsSlice *dst, uint16_t *filter, int * filter_pos, int filter_size, int xInc);
1172
1173 /// initializes chr pixel format conversion descriptor
1174 int ff_init_desc_cfmt_convert(SwsFilterDescriptor *desc, SwsSlice * src, SwsSlice *dst, uint32_t *pal);
1175
1176 /// initializes chr horizontal scaling descriptor
1177 int ff_init_desc_chscale(SwsFilterDescriptor *desc, SwsSlice *src, SwsSlice *dst, uint16_t *filter, int * filter_pos, int filter_size, int xInc);
1178
1179 int ff_init_desc_no_chr(SwsFilterDescriptor *desc, SwsSlice * src, SwsSlice *dst);
1180
1181 /// initializes vertical scaling descriptors
1182 int ff_init_vscale(SwsInternal *c, SwsFilterDescriptor *desc, SwsSlice *src, SwsSlice *dst);
1183
1184 /// setup vertical scaler functions
1185 void ff_init_vscale_pfn(SwsInternal *c, yuv2planar1_fn yuv2plane1, yuv2planarX_fn yuv2planeX,
1186 yuv2interleavedX_fn yuv2nv12cX, yuv2packed1_fn yuv2packed1, yuv2packed2_fn yuv2packed2,
1187 yuv2packedX_fn yuv2packedX, yuv2anyX_fn yuv2anyX, int use_mmx);
1188
1189 int ff_sws_slice_worker(void *priv, int jobnr, int threadnr,
1190 int nb_jobs, int nb_threads);
1191
1192 int ff_swscale(SwsInternal *c, const uint8_t *const src[], const int srcStride[],
1193 int srcSliceY, int srcSliceH, uint8_t *const dst[],
1194 const int dstStride[], int dstSliceY, int dstSliceH);
1195
1196 /**
1197 * Helper for dispatching a single function across multiple threads. This is
1198 * a wrapper around avpriv_slicethread_create2() + avpriv_slicethread_execute2(),
1199 * falling back to direct invocation if threading is not available.
1200 */
1201 int ff_sws_thread_exec(void *priv,
1202 int (*func)(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads),
1203 int nb_threads, int nb_jobs);
1204
1205 //number of extra lines to process
1206 #define MAX_LINES_AHEAD 4
1207
1208 //shuffle filter and filterPos for hyScale and hcScale filters in avx2
1209 int ff_shuffle_filter_coefficients(SwsInternal *c, int* filterPos, int filterSize, int16_t *filter, int dstW);
1210 #endif /* SWSCALE_SWSCALE_INTERNAL_H */
1211