FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libswscale/swscale.c
Date: 2026-04-24 19:58:39
Exec Total Coverage
Lines: 622 882 70.5%
Functions: 32 40 80.0%
Branches: 322 599 53.8%

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 #include <stdint.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 #include "libavutil/avassert.h"
26 #include "libavutil/bswap.h"
27 #include "libavutil/common.h"
28 #include "libavutil/cpu.h"
29 #include "libavutil/emms.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/mem.h"
32 #include "libavutil/mem_internal.h"
33 #include "libavutil/pixdesc.h"
34 #include "libavutil/hwcontext.h"
35 #include "config.h"
36 #include "swscale_internal.h"
37 #include "swscale.h"
38 #if CONFIG_VULKAN
39 #include "vulkan/ops.h"
40 #endif
41
42 DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_128)[9][8] = {
43 { 36, 68, 60, 92, 34, 66, 58, 90, },
44 { 100, 4, 124, 28, 98, 2, 122, 26, },
45 { 52, 84, 44, 76, 50, 82, 42, 74, },
46 { 116, 20, 108, 12, 114, 18, 106, 10, },
47 { 32, 64, 56, 88, 38, 70, 62, 94, },
48 { 96, 0, 120, 24, 102, 6, 126, 30, },
49 { 48, 80, 40, 72, 54, 86, 46, 78, },
50 { 112, 16, 104, 8, 118, 22, 110, 14, },
51 { 36, 68, 60, 92, 34, 66, 58, 90, },
52 };
53
54 DECLARE_ALIGNED(8, static const uint8_t, sws_pb_64)[8] = {
55 64, 64, 64, 64, 64, 64, 64, 64
56 };
57
58 15407 static av_always_inline void fillPlane(uint8_t *plane, int stride, int width,
59 int height, int y, uint8_t val)
60 {
61 int i;
62 15407 uint8_t *ptr = plane + stride * y;
63
2/2
✓ Branch 0 taken 1094452 times.
✓ Branch 1 taken 15407 times.
1109859 for (i = 0; i < height; i++) {
64 1094452 memset(ptr, val, width);
65 1094452 ptr += stride;
66 }
67 15407 }
68
69 7900255 static void hScale16To19_c(SwsInternal *c, int16_t *_dst, int dstW,
70 const uint8_t *_src, const int16_t *filter,
71 const int32_t *filterPos, int filterSize)
72 {
73 7900255 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->opts.src_format);
74 int i;
75 7900255 int32_t *dst = (int32_t *) _dst;
76 7900255 const uint16_t *src = (const uint16_t *) _src;
77 7900255 int bits = desc->comp[0].depth - 1;
78 7900255 int sh = bits - 4;
79
80
5/6
✓ Branch 1 taken 4890548 times.
✓ Branch 2 taken 3009707 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4890548 times.
✓ Branch 5 taken 2015038 times.
✓ Branch 6 taken 994669 times.
7900255 if ((isAnyRGB(c->opts.src_format) || c->opts.src_format==AV_PIX_FMT_PAL8) && desc->comp[0].depth<16) {
81 2015038 sh = 9;
82
2/2
✓ Branch 0 taken 32221 times.
✓ Branch 1 taken 5852996 times.
5885217 } else if (desc->flags & AV_PIX_FMT_FLAG_FLOAT) { /* float input are process like uint 16bpc */
83 32221 sh = 16 - 1 - 4;
84 }
85
86
2/2
✓ Branch 0 taken 2378860283 times.
✓ Branch 1 taken 7900255 times.
2386760538 for (i = 0; i < dstW; i++) {
87 int j;
88 2378860283 int srcPos = filterPos[i];
89 2378860283 int val = 0;
90
91
2/2
✓ Branch 0 taken 4587793467 times.
✓ Branch 1 taken 2378860283 times.
6966653750 for (j = 0; j < filterSize; j++) {
92 4587793467 val += src[srcPos + j] * filter[filterSize * i + j];
93 }
94 // filter=14 bit, input=16 bit, output=30 bit, >> 11 makes 19 bit
95 2378860283 dst[i] = FFMIN(val >> sh, (1 << 19) - 1);
96 }
97 7900255 }
98
99 36693228 static void hScale16To15_c(SwsInternal *c, int16_t *dst, int dstW,
100 const uint8_t *_src, const int16_t *filter,
101 const int32_t *filterPos, int filterSize)
102 {
103 36693228 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->opts.src_format);
104 int i;
105 36693228 const uint16_t *src = (const uint16_t *) _src;
106 36693228 int sh = desc->comp[0].depth - 1;
107
108
2/2
✓ Branch 0 taken 30220780 times.
✓ Branch 1 taken 6472448 times.
36693228 if (sh<15) {
109
4/4
✓ Branch 1 taken 17115634 times.
✓ Branch 2 taken 13105146 times.
✓ Branch 3 taken 16574515 times.
✓ Branch 4 taken 541119 times.
30220780 sh = isAnyRGB(c->opts.src_format) || c->opts.src_format==AV_PIX_FMT_PAL8 ? 13 : (desc->comp[0].depth - 1);
110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6472448 times.
6472448 } else if (desc->flags & AV_PIX_FMT_FLAG_FLOAT) { /* float input are process like uint 16bpc */
111 sh = 16 - 1;
112 }
113
114
2/2
✓ Branch 0 taken 11262116525 times.
✓ Branch 1 taken 36693228 times.
11298809753 for (i = 0; i < dstW; i++) {
115 int j;
116 11262116525 int srcPos = filterPos[i];
117 11262116525 int val = 0;
118
119
2/2
✓ Branch 0 taken 20728057837 times.
✓ Branch 1 taken 11262116525 times.
31990174362 for (j = 0; j < filterSize; j++) {
120 20728057837 val += src[srcPos + j] * filter[filterSize * i + j];
121 }
122 // filter=14 bit, input=16 bit, output=30 bit, >> 15 makes 15 bit
123 11262116525 dst[i] = FFMIN(val >> sh, (1 << 15) - 1);
124 }
125 36693228 }
126
127 // bilinear / bicubic scaling
128 22396606 static void hScale8To15_c(SwsInternal *c, int16_t *dst, int dstW,
129 const uint8_t *src, const int16_t *filter,
130 const int32_t *filterPos, int filterSize)
131 {
132 int i;
133
2/2
✓ Branch 0 taken 6829052512 times.
✓ Branch 1 taken 22396606 times.
6851449118 for (i = 0; i < dstW; i++) {
134 int j;
135 6829052512 int srcPos = filterPos[i];
136 6829052512 int val = 0;
137
2/2
✓ Branch 0 taken 17193771146 times.
✓ Branch 1 taken 6829052512 times.
24022823658 for (j = 0; j < filterSize; j++) {
138 17193771146 val += ((int)src[srcPos + j]) * filter[filterSize * i + j];
139 }
140 6829052512 dst[i] = FFMIN(val >> 7, (1 << 15) - 1); // the cubic equation does overflow ...
141 }
142 22396606 }
143
144 2624544 static void hScale8To19_c(SwsInternal *c, int16_t *_dst, int dstW,
145 const uint8_t *src, const int16_t *filter,
146 const int32_t *filterPos, int filterSize)
147 {
148 int i;
149 2624544 int32_t *dst = (int32_t *) _dst;
150
2/2
✓ Branch 0 taken 792103612 times.
✓ Branch 1 taken 2624544 times.
794728156 for (i = 0; i < dstW; i++) {
151 int j;
152 792103612 int srcPos = filterPos[i];
153 792103612 int val = 0;
154
2/2
✓ Branch 0 taken 1537538828 times.
✓ Branch 1 taken 792103612 times.
2329642440 for (j = 0; j < filterSize; j++) {
155 1537538828 val += ((int)src[srcPos + j]) * filter[filterSize * i + j];
156 }
157 792103612 dst[i] = FFMIN(val >> 3, (1 << 19) - 1); // the cubic equation does overflow ...
158 }
159 2624544 }
160
161 // FIXME all pal and rgb srcFormats could do this conversion as well
162 // FIXME all scalers more complex than bilinear could do half of this transform
163 603832 static void chrRangeToJpeg_c(int16_t *dstU, int16_t *dstV, int width,
164 uint32_t _coeff, int64_t _offset)
165 {
166 603832 uint16_t coeff = _coeff;
167 603832 int32_t offset = _offset;
168 int i;
169
2/2
✓ Branch 0 taken 146603892 times.
✓ Branch 1 taken 603832 times.
147207724 for (i = 0; i < width; i++) {
170 146603892 int U = (dstU[i] * coeff + offset) >> 14;
171 146603892 int V = (dstV[i] * coeff + offset) >> 14;
172 146603892 dstU[i] = FFMIN(U, (1 << 15) - 1);
173 146603892 dstV[i] = FFMIN(V, (1 << 15) - 1);
174 }
175 603832 }
176
177 361954 static void chrRangeFromJpeg_c(int16_t *dstU, int16_t *dstV, int width,
178 uint32_t _coeff, int64_t _offset)
179 {
180 361954 uint16_t coeff = _coeff;
181 361954 int32_t offset = _offset;
182 int i;
183
2/2
✓ Branch 0 taken 89830698 times.
✓ Branch 1 taken 361954 times.
90192652 for (i = 0; i < width; i++) {
184 89830698 dstU[i] = (dstU[i] * coeff + offset) >> 14;
185 89830698 dstV[i] = (dstV[i] * coeff + offset) >> 14;
186 }
187 361954 }
188
189 1566286 static void lumRangeToJpeg_c(int16_t *dst, int width,
190 uint32_t _coeff, int64_t _offset)
191 {
192 1566286 uint16_t coeff = _coeff;
193 1566286 int32_t offset = _offset;
194 int i;
195
2/2
✓ Branch 0 taken 542415408 times.
✓ Branch 1 taken 1566286 times.
543981694 for (i = 0; i < width; i++) {
196 542415408 int Y = (dst[i] * coeff + offset) >> 14;
197 542415408 dst[i] = FFMIN(Y, (1 << 15) - 1);
198 }
199 1566286 }
200
201 900698 static void lumRangeFromJpeg_c(int16_t *dst, int width,
202 uint32_t _coeff, int64_t _offset)
203 {
204 900698 uint16_t coeff = _coeff;
205 900698 int32_t offset = _offset;
206 int i;
207
2/2
✓ Branch 0 taken 314235568 times.
✓ Branch 1 taken 900698 times.
315136266 for (i = 0; i < width; i++)
208 314235568 dst[i] = (dst[i] * coeff + offset) >> 14;
209 900698 }
210
211 6 static void chrRangeToJpeg16_c(int16_t *_dstU, int16_t *_dstV, int width,
212 uint32_t coeff, int64_t offset)
213 {
214 int i;
215 6 int32_t *dstU = (int32_t *) _dstU;
216 6 int32_t *dstV = (int32_t *) _dstV;
217
2/2
✓ Branch 0 taken 5784 times.
✓ Branch 1 taken 6 times.
5790 for (i = 0; i < width; i++) {
218 5784 int U = ((int64_t) dstU[i] * coeff + offset) >> 18;
219 5784 int V = ((int64_t) dstV[i] * coeff + offset) >> 18;
220 5784 dstU[i] = FFMIN(U, (1 << 19) - 1);
221 5784 dstV[i] = FFMIN(V, (1 << 19) - 1);
222 }
223 6 }
224
225 6 static void chrRangeFromJpeg16_c(int16_t *_dstU, int16_t *_dstV, int width,
226 uint32_t coeff, int64_t offset)
227 {
228 int i;
229 6 int32_t *dstU = (int32_t *) _dstU;
230 6 int32_t *dstV = (int32_t *) _dstV;
231
2/2
✓ Branch 0 taken 5784 times.
✓ Branch 1 taken 6 times.
5790 for (i = 0; i < width; i++) {
232 5784 dstU[i] = ((int64_t) dstU[i] * coeff + offset) >> 18;
233 5784 dstV[i] = ((int64_t) dstV[i] * coeff + offset) >> 18;
234 }
235 6 }
236
237 286637 static void lumRangeToJpeg16_c(int16_t *_dst, int width,
238 uint32_t coeff, int64_t offset)
239 {
240 int i;
241 286637 int32_t *dst = (int32_t *) _dst;
242
2/2
✓ Branch 0 taken 100828496 times.
✓ Branch 1 taken 286637 times.
101115133 for (i = 0; i < width; i++) {
243 100828496 int Y = ((int64_t) dst[i] * coeff + offset) >> 18;
244 100828496 dst[i] = FFMIN(Y, (1 << 19) - 1);
245 }
246 286637 }
247
248 33182 static void lumRangeFromJpeg16_c(int16_t *_dst, int width,
249 uint32_t coeff, int64_t offset)
250 {
251 int i;
252 33182 int32_t *dst = (int32_t *) _dst;
253
2/2
✓ Branch 0 taken 11683736 times.
✓ Branch 1 taken 33182 times.
11716918 for (i = 0; i < width; i++)
254 11683736 dst[i] = ((int64_t) dst[i] * coeff + offset) >> 18;
255 33182 }
256
257
258 #define DEBUG_SWSCALE_BUFFERS 0
259 #define DEBUG_BUFFERS(...) \
260 if (DEBUG_SWSCALE_BUFFERS) \
261 av_log(c, AV_LOG_DEBUG, __VA_ARGS__)
262
263 568144 int ff_swscale(SwsInternal *c, const uint8_t *const src[], const int srcStride[],
264 int srcSliceY, int srcSliceH, uint8_t *const dst[],
265 const int dstStride[], int dstSliceY, int dstSliceH)
266 {
267
4/4
✓ Branch 0 taken 110191 times.
✓ Branch 1 taken 457953 times.
✓ Branch 2 taken 57367 times.
✓ Branch 3 taken 52824 times.
568144 const int scale_dst = dstSliceY > 0 || dstSliceH < c->opts.dst_h;
268
269 /* load a few things into local vars to make the code more readable?
270 * and faster */
271 568144 const int dstW = c->opts.dst_w;
272 568144 int dstH = c->opts.dst_h;
273
274 568144 const enum AVPixelFormat dstFormat = c->opts.dst_format;
275 568144 const int flags = c->opts.flags;
276 568144 int32_t *vLumFilterPos = c->vLumFilterPos;
277 568144 int32_t *vChrFilterPos = c->vChrFilterPos;
278
279 568144 const int vLumFilterSize = c->vLumFilterSize;
280 568144 const int vChrFilterSize = c->vChrFilterSize;
281
282 568144 yuv2planar1_fn yuv2plane1 = c->yuv2plane1;
283 568144 yuv2planarX_fn yuv2planeX = c->yuv2planeX;
284 568144 yuv2interleavedX_fn yuv2nv12cX = c->yuv2nv12cX;
285 568144 yuv2packed1_fn yuv2packed1 = c->yuv2packed1;
286 568144 yuv2packed2_fn yuv2packed2 = c->yuv2packed2;
287 568144 yuv2packedX_fn yuv2packedX = c->yuv2packedX;
288 568144 yuv2anyX_fn yuv2anyX = c->yuv2anyX;
289 568144 const int chrSrcSliceY = srcSliceY >> c->chrSrcVSubSample;
290 568144 const int chrSrcSliceH = AV_CEIL_RSHIFT(srcSliceH, c->chrSrcVSubSample);
291
4/4
✓ Branch 1 taken 318673 times.
✓ Branch 2 taken 249471 times.
✓ Branch 3 taken 99851 times.
✓ Branch 4 taken 218822 times.
886817 int should_dither = isNBPS(c->opts.src_format) ||
292 318673 is16BPS(c->opts.src_format);
293 int lastDstY;
294
295 /* vars which will change and which we need to store back in the context */
296 568144 int dstY = c->dstY;
297 568144 int lastInLumBuf = c->lastInLumBuf;
298 568144 int lastInChrBuf = c->lastInChrBuf;
299
300 568144 int lumStart = 0;
301 568144 int lumEnd = c->descIndex[0];
302 568144 int chrStart = lumEnd;
303 568144 int chrEnd = c->descIndex[1];
304 568144 int vStart = chrEnd;
305 568144 int vEnd = c->numDesc;
306 568144 SwsSlice *src_slice = &c->slice[lumStart];
307 568144 SwsSlice *hout_slice = &c->slice[c->numSlice-2];
308 568144 SwsSlice *vout_slice = &c->slice[c->numSlice-1];
309 568144 SwsFilterDescriptor *desc = c->desc;
310
311 568144 int needAlpha = c->needAlpha;
312
313 568144 int hasLumHoles = 1;
314 568144 int hasChrHoles = 1;
315
316 const uint8_t *src2[4];
317 int srcStride2[4];
318
319
2/2
✓ Branch 1 taken 124015 times.
✓ Branch 2 taken 444129 times.
568144 if (isPacked(c->opts.src_format)) {
320 124015 src2[0] =
321 124015 src2[1] =
322 124015 src2[2] =
323 124015 src2[3] = src[0];
324 124015 srcStride2[0] =
325 124015 srcStride2[1] =
326 124015 srcStride2[2] =
327 124015 srcStride2[3] = srcStride[0];
328 } else {
329 444129 memcpy(src2, src, sizeof(src2));
330 444129 memcpy(srcStride2, srcStride, sizeof(srcStride2));
331 }
332
333 568144 srcStride2[1] *= 1 << c->vChrDrop;
334 568144 srcStride2[2] *= 1 << c->vChrDrop;
335
336 DEBUG_BUFFERS("swscale() %p[%d] %p[%d] %p[%d] %p[%d] -> %p[%d] %p[%d] %p[%d] %p[%d]\n",
337 src2[0], srcStride2[0], src2[1], srcStride2[1],
338 src2[2], srcStride2[2], src2[3], srcStride2[3],
339 dst[0], dstStride[0], dst[1], dstStride[1],
340 dst[2], dstStride[2], dst[3], dstStride[3]);
341 DEBUG_BUFFERS("srcSliceY: %d srcSliceH: %d dstY: %d dstH: %d\n",
342 srcSliceY, srcSliceH, dstY, dstH);
343 DEBUG_BUFFERS("vLumFilterSize: %d vChrFilterSize: %d\n",
344 vLumFilterSize, vChrFilterSize);
345
346
4/4
✓ Branch 0 taken 562775 times.
✓ Branch 1 taken 5369 times.
✓ Branch 2 taken 551372 times.
✓ Branch 3 taken 11403 times.
568144 if (dstStride[0]&15 || dstStride[1]&15 ||
347
2/4
✓ Branch 0 taken 551372 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 551372 times.
551372 dstStride[2]&15 || dstStride[3]&15) {
348
2/2
✓ Branch 0 taken 10159 times.
✓ Branch 1 taken 6613 times.
16772 SwsInternal *const ctx = c->parent ? sws_internal(c->parent) : c;
349
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16772 times.
16772 if (flags & SWS_PRINT_INFO &&
350 !atomic_exchange_explicit(&ctx->stride_unaligned_warned, 1, memory_order_relaxed)) {
351 av_log(c, AV_LOG_WARNING,
352 "Warning: dstStride is not aligned!\n"
353 " ->cannot do aligned memory accesses anymore\n");
354 }
355 }
356
357 #if ARCH_X86
358
5/6
✓ Branch 0 taken 565634 times.
✓ Branch 1 taken 2510 times.
✓ Branch 2 taken 563944 times.
✓ Branch 3 taken 1690 times.
✓ Branch 4 taken 563944 times.
✗ Branch 5 not taken.
568144 if ( (uintptr_t) dst[0]&15 || (uintptr_t) dst[1]&15 || (uintptr_t) dst[2]&15
359
5/6
✓ Branch 0 taken 563926 times.
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 560854 times.
✓ Branch 3 taken 3072 times.
✓ Branch 4 taken 560854 times.
✗ Branch 5 not taken.
563944 || (uintptr_t)src2[0]&15 || (uintptr_t)src2[1]&15 || (uintptr_t)src2[2]&15
360
6/8
✓ Branch 0 taken 560448 times.
✓ Branch 1 taken 406 times.
✓ Branch 2 taken 555291 times.
✓ Branch 3 taken 5157 times.
✓ Branch 4 taken 555291 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 555291 times.
560854 || srcStride2[0]&15 || srcStride2[1]&15 || srcStride2[2]&15 || srcStride2[3]&15
361 ) {
362
2/2
✓ Branch 0 taken 9452 times.
✓ Branch 1 taken 3401 times.
12853 SwsInternal *const ctx = c->parent ? sws_internal(c->parent) : c;
363 12853 int cpu_flags = av_get_cpu_flags();
364
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 12853 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
12853 if (flags & SWS_PRINT_INFO && HAVE_MMXEXT && (cpu_flags & AV_CPU_FLAG_SSE2) &&
365 !atomic_exchange_explicit(&ctx->stride_unaligned_warned,1, memory_order_relaxed)) {
366 av_log(c, AV_LOG_WARNING, "Warning: data is not aligned! This can lead to a speed loss\n");
367 }
368 }
369 #endif
370
371
2/2
✓ Branch 0 taken 515320 times.
✓ Branch 1 taken 52824 times.
568144 if (scale_dst) {
372 515320 dstY = dstSliceY;
373 515320 dstH = dstY + dstSliceH;
374 515320 lastInLumBuf = -1;
375 515320 lastInChrBuf = -1;
376
2/2
✓ Branch 0 taken 52769 times.
✓ Branch 1 taken 55 times.
52824 } else if (srcSliceY == 0) {
377 /* Note the user might start scaling the picture in the middle so this
378 * will not get executed. This is not really intended but works
379 * currently, so people might do it. */
380 52769 dstY = 0;
381 52769 lastInLumBuf = -1;
382 52769 lastInChrBuf = -1;
383 }
384
385
2/2
✓ Branch 0 taken 218822 times.
✓ Branch 1 taken 349322 times.
568144 if (!should_dither) {
386 218822 c->chrDither8 = c->lumDither8 = sws_pb_64;
387 }
388 568144 lastDstY = dstY;
389
390 568144 ff_init_vscale_pfn(c, yuv2plane1, yuv2planeX, yuv2nv12cX,
391 yuv2packed1, yuv2packed2, yuv2packedX, yuv2anyX, c->use_mmx_vfilter);
392
393 568144 ff_init_slice_from_src(src_slice, (uint8_t**)src2, srcStride2, c->opts.src_w,
394 srcSliceY, srcSliceH, chrSrcSliceY, chrSrcSliceH, 1);
395
396 568144 ff_init_slice_from_src(vout_slice, (uint8_t**)dst, dstStride, c->opts.dst_w,
397 568144 dstY, dstSliceH, dstY >> c->chrDstVSubSample,
398 568144 AV_CEIL_RSHIFT(dstSliceH, c->chrDstVSubSample), scale_dst);
399
2/2
✓ Branch 0 taken 568089 times.
✓ Branch 1 taken 55 times.
568144 if (srcSliceY == 0) {
400 568089 hout_slice->plane[0].sliceY = lastInLumBuf + 1;
401 568089 hout_slice->plane[1].sliceY = lastInChrBuf + 1;
402 568089 hout_slice->plane[2].sliceY = lastInChrBuf + 1;
403 568089 hout_slice->plane[3].sliceY = lastInLumBuf + 1;
404
405 568089 hout_slice->plane[0].sliceH =
406 568089 hout_slice->plane[1].sliceH =
407 568089 hout_slice->plane[2].sliceH =
408 568089 hout_slice->plane[3].sliceH = 0;
409 568089 hout_slice->width = dstW;
410 }
411
412
2/2
✓ Branch 0 taken 29640371 times.
✓ Branch 1 taken 568089 times.
30208460 for (; dstY < dstH; dstY++) {
413 29640371 const int chrDstY = dstY >> c->chrDstVSubSample;
414 29640371 int use_mmx_vfilter= c->use_mmx_vfilter;
415
416 // First line needed as input
417 29640371 const int firstLumSrcY = FFMAX(1 - vLumFilterSize, vLumFilterPos[dstY]);
418
2/2
✓ Branch 0 taken 100 times.
✓ Branch 1 taken 29640271 times.
29640371 const int firstLumSrcY2 = FFMAX(1 - vLumFilterSize, vLumFilterPos[FFMIN(dstY | ((1 << c->chrDstVSubSample) - 1), c->opts.dst_h - 1)]);
419 // First line needed as input
420 29640371 const int firstChrSrcY = FFMAX(1 - vChrFilterSize, vChrFilterPos[chrDstY]);
421
422 // Last line needed as input
423 29640371 int lastLumSrcY = FFMIN(c->opts.src_h, firstLumSrcY + vLumFilterSize) - 1;
424 29640371 int lastLumSrcY2 = FFMIN(c->opts.src_h, firstLumSrcY2 + vLumFilterSize) - 1;
425 29640371 int lastChrSrcY = FFMIN(c->chrSrcH, firstChrSrcY + vChrFilterSize) - 1;
426 int enough_lines;
427
428 int i;
429 int posY, cPosY, firstPosY, lastPosY, firstCPosY, lastCPosY;
430
431 // handle holes (FAST_BILINEAR & weird filters)
432
2/2
✓ Branch 0 taken 25730278 times.
✓ Branch 1 taken 3910093 times.
29640371 if (firstLumSrcY > lastInLumBuf) {
433
434 25730278 hasLumHoles = lastInLumBuf != firstLumSrcY - 1;
435
2/2
✓ Branch 0 taken 642053 times.
✓ Branch 1 taken 25088225 times.
25730278 if (hasLumHoles) {
436 642053 hout_slice->plane[0].sliceY = firstLumSrcY;
437 642053 hout_slice->plane[3].sliceY = firstLumSrcY;
438 642053 hout_slice->plane[0].sliceH =
439 642053 hout_slice->plane[3].sliceH = 0;
440 }
441
442 25730278 lastInLumBuf = firstLumSrcY - 1;
443 }
444
2/2
✓ Branch 0 taken 14653427 times.
✓ Branch 1 taken 14986944 times.
29640371 if (firstChrSrcY > lastInChrBuf) {
445
446 14653427 hasChrHoles = lastInChrBuf != firstChrSrcY - 1;
447
2/2
✓ Branch 0 taken 793853 times.
✓ Branch 1 taken 13859574 times.
14653427 if (hasChrHoles) {
448 793853 hout_slice->plane[1].sliceY = firstChrSrcY;
449 793853 hout_slice->plane[2].sliceY = firstChrSrcY;
450 793853 hout_slice->plane[1].sliceH =
451 793853 hout_slice->plane[2].sliceH = 0;
452 }
453
454 14653427 lastInChrBuf = firstChrSrcY - 1;
455 }
456
457 DEBUG_BUFFERS("dstY: %d\n", dstY);
458 DEBUG_BUFFERS("\tfirstLumSrcY: %d lastLumSrcY: %d lastInLumBuf: %d\n",
459 firstLumSrcY, lastLumSrcY, lastInLumBuf);
460 DEBUG_BUFFERS("\tfirstChrSrcY: %d lastChrSrcY: %d lastInChrBuf: %d\n",
461 firstChrSrcY, lastChrSrcY, lastInChrBuf);
462
463 // Do we have enough lines in this slice to output the dstY line
464
2/2
✓ Branch 0 taken 29640320 times.
✓ Branch 1 taken 51 times.
59280691 enough_lines = lastLumSrcY2 < srcSliceY + srcSliceH &&
465
2/2
✓ Branch 0 taken 29640316 times.
✓ Branch 1 taken 4 times.
29640320 lastChrSrcY < AV_CEIL_RSHIFT(srcSliceY + srcSliceH, c->chrSrcVSubSample);
466
467
2/2
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 29640316 times.
29640371 if (!enough_lines) {
468 55 lastLumSrcY = srcSliceY + srcSliceH - 1;
469 55 lastChrSrcY = chrSrcSliceY + chrSrcSliceH - 1;
470 DEBUG_BUFFERS("buffering slice: lastLumSrcY %d lastChrSrcY %d\n",
471 lastLumSrcY, lastChrSrcY);
472 }
473
474
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29640371 times.
29640371 av_assert0((lastLumSrcY - firstLumSrcY + 1) <= hout_slice->plane[0].available_lines);
475
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29640371 times.
29640371 av_assert0((lastChrSrcY - firstChrSrcY + 1) <= hout_slice->plane[1].available_lines);
476
477
478 29640371 posY = hout_slice->plane[0].sliceY + hout_slice->plane[0].sliceH;
479
4/4
✓ Branch 0 taken 6032869 times.
✓ Branch 1 taken 23607502 times.
✓ Branch 2 taken 5112645 times.
✓ Branch 3 taken 920224 times.
29640371 if (posY <= lastLumSrcY && !hasLumHoles) {
480 5112645 firstPosY = FFMAX(firstLumSrcY, posY);
481
2/2
✓ Branch 0 taken 92678 times.
✓ Branch 1 taken 5019967 times.
5112645 lastPosY = FFMIN(firstLumSrcY + hout_slice->plane[0].available_lines - 1, srcSliceY + srcSliceH - 1);
482 } else {
483 24527726 firstPosY = posY;
484 24527726 lastPosY = lastLumSrcY;
485 }
486
487 29640371 cPosY = hout_slice->plane[1].sliceY + hout_slice->plane[1].sliceH;
488
4/4
✓ Branch 0 taken 6704546 times.
✓ Branch 1 taken 22935825 times.
✓ Branch 2 taken 3911612 times.
✓ Branch 3 taken 2792934 times.
29640371 if (cPosY <= lastChrSrcY && !hasChrHoles) {
489 3911612 firstCPosY = FFMAX(firstChrSrcY, cPosY);
490 3911612 lastCPosY = FFMIN(firstChrSrcY + hout_slice->plane[1].available_lines - 1, AV_CEIL_RSHIFT(srcSliceY + srcSliceH, c->chrSrcVSubSample) - 1);
491 } else {
492 25728759 firstCPosY = cPosY;
493 25728759 lastCPosY = lastChrSrcY;
494 }
495
496 29640371 ff_rotate_slice(hout_slice, lastPosY, lastCPosY);
497
498
2/2
✓ Branch 0 taken 6032869 times.
✓ Branch 1 taken 23607502 times.
29640371 if (posY < lastLumSrcY + 1) {
499
2/2
✓ Branch 0 taken 8956575 times.
✓ Branch 1 taken 6032869 times.
14989444 for (i = lumStart; i < lumEnd; ++i)
500 8956575 desc[i].process(c, &desc[i], firstPosY, lastPosY - firstPosY + 1);
501 }
502
503 29640371 lastInLumBuf = lastLumSrcY;
504
505
2/2
✓ Branch 0 taken 6704546 times.
✓ Branch 1 taken 22935825 times.
29640371 if (cPosY < lastChrSrcY + 1) {
506
2/2
✓ Branch 0 taken 10578759 times.
✓ Branch 1 taken 6704546 times.
17283305 for (i = chrStart; i < chrEnd; ++i)
507 10578759 desc[i].process(c, &desc[i], firstCPosY, lastCPosY - firstCPosY + 1);
508 }
509
510 29640371 lastInChrBuf = lastChrSrcY;
511
512
2/2
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 29640316 times.
29640371 if (!enough_lines)
513 55 break; // we can't output a dstY line so let's try with the next slice
514
515 #if HAVE_MMX_INLINE
516 29640316 ff_updateMMXDitherTables(c, dstY);
517 29640316 c->dstW_mmx = c->opts.dst_w;
518 #endif
519
2/2
✓ Branch 0 taken 14550381 times.
✓ Branch 1 taken 15089935 times.
29640316 if (should_dither) {
520 14550381 c->chrDither8 = ff_dither_8x8_128[chrDstY & 7];
521 14550381 c->lumDither8 = ff_dither_8x8_128[dstY & 7];
522 }
523
2/2
✓ Branch 0 taken 220272 times.
✓ Branch 1 taken 29420044 times.
29640316 if (dstY >= c->opts.dst_h - 2) {
524 /* hmm looks like we can't use MMX here without overwriting
525 * this array's tail */
526 220272 ff_sws_init_output_funcs(c, &yuv2plane1, &yuv2planeX, &yuv2nv12cX,
527 &yuv2packed1, &yuv2packed2, &yuv2packedX, &yuv2anyX);
528 220272 use_mmx_vfilter= 0;
529 220272 ff_init_vscale_pfn(c, yuv2plane1, yuv2planeX, yuv2nv12cX,
530 yuv2packed1, yuv2packed2, yuv2packedX, yuv2anyX, use_mmx_vfilter);
531 }
532
533
2/2
✓ Branch 0 taken 49288632 times.
✓ Branch 1 taken 29640316 times.
78928948 for (i = vStart; i < vEnd; ++i)
534 49288632 desc[i].process(c, &desc[i], dstY, 1);
535 }
536
6/6
✓ Branch 1 taken 422844 times.
✓ Branch 2 taken 145300 times.
✓ Branch 4 taken 22728 times.
✓ Branch 5 taken 400116 times.
✓ Branch 6 taken 16804 times.
✓ Branch 7 taken 5924 times.
568144 if (isPlanar(dstFormat) && isALPHA(dstFormat) && !needAlpha) {
537 16804 int offset = lastDstY - dstSliceY;
538 16804 int length = dstW;
539 16804 int height = dstY - lastDstY;
540
541
4/4
✓ Branch 1 taken 16331 times.
✓ Branch 2 taken 473 times.
✓ Branch 4 taken 784 times.
✓ Branch 5 taken 15547 times.
18061 if (is16BPS(dstFormat) || isNBPS(dstFormat)) {
542 1257 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(dstFormat);
543 1257 fillPlane16(dst[3], dstStride[3], length, height, offset,
544 1257 1, desc->comp[3].depth,
545 isBE(dstFormat));
546
2/2
✓ Branch 1 taken 140 times.
✓ Branch 2 taken 15407 times.
15547 } else if (is32BPS(dstFormat)) {
547 140 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(dstFormat);
548 280 fillPlane32(dst[3], dstStride[3], length, height, offset,
549 140 1, desc->comp[3].depth,
550 140 isBE(dstFormat), desc->flags & AV_PIX_FMT_FLAG_FLOAT);
551 } else
552 15407 fillPlane(dst[3], dstStride[3], length, height, offset, 255);
553 }
554
555 #if HAVE_MMXEXT_INLINE
556
2/2
✓ Branch 1 taken 16362 times.
✓ Branch 2 taken 551782 times.
568144 if (av_get_cpu_flags() & AV_CPU_FLAG_MMXEXT)
557 16362 __asm__ volatile ("sfence" ::: "memory");
558 #endif
559 568144 emms_c();
560
561 /* store changed local vars back in the context */
562 568144 c->dstY = dstY;
563 568144 c->lastInLumBuf = lastInLumBuf;
564 568144 c->lastInChrBuf = lastInChrBuf;
565
566 568144 return dstY - lastDstY;
567 }
568
569 /*
570 * Solve for coeff and offset:
571 * dst = ((src << src_shift) * coeff + offset) >> (mult_shift + src_shift)
572 *
573 * If SwsInternal->dstBpc is > 14, coeff is uint16_t and offset is int32_t,
574 * otherwise (SwsInternal->dstBpc is <= 14) coeff is uint32_t and offset is
575 * int64_t.
576 */
577 19486 static void solve_range_convert(uint16_t src_min, uint16_t src_max,
578 uint16_t dst_min, uint16_t dst_max,
579 int src_bits, int src_shift, int mult_shift,
580 uint32_t *coeff, int64_t *offset)
581 {
582 19486 uint16_t src_range = src_max - src_min;
583 19486 uint16_t dst_range = dst_max - dst_min;
584 19486 int total_shift = mult_shift + src_shift;
585 19486 *coeff = AV_CEIL_RSHIFT(((uint64_t) dst_range << total_shift) / src_range, src_shift);
586 19486 *offset = ((int64_t) dst_max << total_shift) -
587 19486 ((int64_t) src_max << src_shift) * *coeff +
588 19486 (1U << (mult_shift - 1));
589 19486 }
590
591 9743 static void init_range_convert_constants(SwsInternal *c)
592 {
593
2/2
✓ Branch 0 taken 5045 times.
✓ Branch 1 taken 4698 times.
9743 const int bit_depth = c->dstBpc ? FFMIN(c->dstBpc, 16) : 8;
594
2/2
✓ Branch 0 taken 9229 times.
✓ Branch 1 taken 514 times.
9743 const int src_bits = bit_depth <= 14 ? 15 : 19;
595 9743 const int src_shift = src_bits - bit_depth;
596
2/2
✓ Branch 0 taken 9229 times.
✓ Branch 1 taken 514 times.
9743 const int mult_shift = bit_depth <= 14 ? 14 : 18;
597 9743 const uint16_t mpeg_min = 16U << (bit_depth - 8);
598 9743 const uint16_t mpeg_max_lum = 235U << (bit_depth - 8);
599 9743 const uint16_t mpeg_max_chr = 240U << (bit_depth - 8);
600 9743 const uint16_t jpeg_max = (1U << bit_depth) - 1;
601 uint16_t src_min, src_max_lum, src_max_chr;
602 uint16_t dst_min, dst_max_lum, dst_max_chr;
603
2/2
✓ Branch 0 taken 5806 times.
✓ Branch 1 taken 3937 times.
9743 if (c->opts.src_range) {
604 5806 src_min = 0;
605 5806 src_max_lum = jpeg_max;
606 5806 src_max_chr = jpeg_max;
607 5806 dst_min = mpeg_min;
608 5806 dst_max_lum = mpeg_max_lum;
609 5806 dst_max_chr = mpeg_max_chr;
610 } else {
611 3937 src_min = mpeg_min;
612 3937 src_max_lum = mpeg_max_lum;
613 3937 src_max_chr = mpeg_max_chr;
614 3937 dst_min = 0;
615 3937 dst_max_lum = jpeg_max;
616 3937 dst_max_chr = jpeg_max;
617 }
618 9743 solve_range_convert(src_min, src_max_lum, dst_min, dst_max_lum,
619 src_bits, src_shift, mult_shift,
620 &c->lumConvertRange_coeff, &c->lumConvertRange_offset);
621 9743 solve_range_convert(src_min, src_max_chr, dst_min, dst_max_chr,
622 src_bits, src_shift, mult_shift,
623 &c->chrConvertRange_coeff, &c->chrConvertRange_offset);
624 9743 }
625
626 110023 av_cold void ff_sws_init_range_convert(SwsInternal *c)
627 {
628 110023 c->lumConvertRange = NULL;
629 110023 c->chrConvertRange = NULL;
630
5/6
✓ Branch 0 taken 11079 times.
✓ Branch 1 taken 98944 times.
✓ Branch 3 taken 9743 times.
✓ Branch 4 taken 1336 times.
✓ Branch 5 taken 9743 times.
✗ Branch 6 not taken.
110023 if (c->opts.src_range != c->opts.dst_range && !isAnyRGB(c->opts.dst_format) && c->dstBpc < 32) {
631 9743 init_range_convert_constants(c);
632
2/2
✓ Branch 0 taken 9229 times.
✓ Branch 1 taken 514 times.
9743 if (c->dstBpc <= 14) {
633
2/2
✓ Branch 0 taken 5708 times.
✓ Branch 1 taken 3521 times.
9229 if (c->opts.src_range) {
634 5708 c->lumConvertRange = lumRangeFromJpeg_c;
635 5708 c->chrConvertRange = chrRangeFromJpeg_c;
636 } else {
637 3521 c->lumConvertRange = lumRangeToJpeg_c;
638 3521 c->chrConvertRange = chrRangeToJpeg_c;
639 }
640 } else {
641
2/2
✓ Branch 0 taken 98 times.
✓ Branch 1 taken 416 times.
514 if (c->opts.src_range) {
642 98 c->lumConvertRange = lumRangeFromJpeg16_c;
643 98 c->chrConvertRange = chrRangeFromJpeg16_c;
644 } else {
645 416 c->lumConvertRange = lumRangeToJpeg16_c;
646 416 c->chrConvertRange = chrRangeToJpeg16_c;
647 }
648 }
649
650 #if ARCH_AARCH64
651 ff_sws_init_range_convert_aarch64(c);
652 #elif ARCH_LOONGARCH64
653 ff_sws_init_range_convert_loongarch(c);
654 #elif ARCH_RISCV
655 ff_sws_init_range_convert_riscv(c);
656 #elif ARCH_X86
657 9743 ff_sws_init_range_convert_x86(c);
658 #endif
659 }
660 110023 }
661
662 57173 static av_cold void sws_init_swscale(SwsInternal *c)
663 {
664 57173 enum AVPixelFormat srcFormat = c->opts.src_format;
665
666 57173 ff_sws_init_xyzdsp(c);
667
668 57173 ff_sws_init_output_funcs(c, &c->yuv2plane1, &c->yuv2planeX,
669 &c->yuv2nv12cX, &c->yuv2packed1,
670 &c->yuv2packed2, &c->yuv2packedX, &c->yuv2anyX);
671
672 57173 ff_sws_init_input_funcs(c, &c->lumToYV12, &c->alpToYV12, &c->chrToYV12,
673 &c->readLumPlanar, &c->readAlpPlanar, &c->readChrPlanar);
674
675
2/2
✓ Branch 0 taken 24389 times.
✓ Branch 1 taken 32784 times.
57173 if (c->srcBpc == 8) {
676
2/2
✓ Branch 0 taken 22198 times.
✓ Branch 1 taken 2191 times.
24389 if (c->dstBpc <= 14) {
677 22198 c->hyScale = c->hcScale = hScale8To15_c;
678
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22198 times.
22198 if (c->opts.flags & SWS_FAST_BILINEAR) {
679 c->hyscale_fast = ff_hyscale_fast_c;
680 c->hcscale_fast = ff_hcscale_fast_c;
681 }
682 } else {
683 2191 c->hyScale = c->hcScale = hScale8To19_c;
684 }
685 } else {
686 32784 c->hyScale = c->hcScale = c->dstBpc > 14 ? hScale16To19_c
687
2/2
✓ Branch 0 taken 4595 times.
✓ Branch 1 taken 28189 times.
32784 : hScale16To15_c;
688 }
689
690 57173 ff_sws_init_range_convert(c);
691
692
8/8
✓ Branch 1 taken 54008 times.
✓ Branch 2 taken 3165 times.
✓ Branch 4 taken 52709 times.
✓ Branch 5 taken 1299 times.
✓ Branch 6 taken 52457 times.
✓ Branch 7 taken 252 times.
✓ Branch 8 taken 52201 times.
✓ Branch 9 taken 256 times.
57173 if (!(isGray(srcFormat) || isGray(c->opts.dst_format) ||
693 srcFormat == AV_PIX_FMT_MONOBLACK || srcFormat == AV_PIX_FMT_MONOWHITE))
694 52201 c->needs_hcscale = 1;
695 57173 }
696
697 57173 void ff_sws_init_scale(SwsInternal *c)
698 {
699 57173 sws_init_swscale(c);
700
701 #if ARCH_PPC
702 ff_sws_init_swscale_ppc(c);
703 #elif ARCH_X86
704 57173 ff_sws_init_swscale_x86(c);
705 #elif ARCH_AARCH64
706 ff_sws_init_swscale_aarch64(c);
707 #elif ARCH_ARM
708 ff_sws_init_swscale_arm(c);
709 #elif ARCH_LOONGARCH64
710 ff_sws_init_swscale_loongarch(c);
711 #elif ARCH_RISCV
712 ff_sws_init_swscale_riscv(c);
713 #endif
714 57173 }
715
716 290 static void reset_ptr(const uint8_t *src[], enum AVPixelFormat format)
717 {
718
2/2
✓ Branch 1 taken 255 times.
✓ Branch 2 taken 35 times.
290 if (!isALPHA(format))
719 255 src[3] = NULL;
720
2/2
✓ Branch 1 taken 105 times.
✓ Branch 2 taken 185 times.
290 if (!isPlanar(format)) {
721 105 src[3] = src[2] = NULL;
722
723
1/2
✓ Branch 1 taken 105 times.
✗ Branch 2 not taken.
105 if (!usePal(format))
724 105 src[1] = NULL;
725 }
726 290 }
727
728 290 static int check_image_pointers(const uint8_t * const data[4], enum AVPixelFormat pix_fmt,
729 const int linesizes[4])
730 {
731 290 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
732 int i;
733
734 av_assert2(desc);
735
736
2/2
✓ Branch 0 taken 1160 times.
✓ Branch 1 taken 290 times.
1450 for (i = 0; i < 4; i++) {
737 1160 int plane = desc->comp[i].plane;
738
2/4
✓ Branch 0 taken 1160 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1160 times.
1160 if (!data[plane] || !linesizes[plane])
739 return 0;
740 }
741
742 290 return 1;
743 }
744
745 3247 static void xyz12Torgb48_c(const SwsInternal *c, uint8_t *dst, int dst_stride,
746 const uint8_t *src, int src_stride, int w, int h)
747 {
748 3247 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->opts.src_format);
749
750
2/2
✓ Branch 0 taken 133724 times.
✓ Branch 1 taken 3247 times.
136971 for (int yp = 0; yp < h; yp++) {
751 133724 const uint16_t *src16 = (const uint16_t *) src;
752 133724 uint16_t *dst16 = (uint16_t *) dst;
753
754
2/2
✓ Branch 0 taken 34337012 times.
✓ Branch 1 taken 133724 times.
34470736 for (int xp = 0; xp < 3 * w; xp += 3) {
755 int x, y, z, r, g, b;
756
757
2/2
✓ Branch 0 taken 2580480 times.
✓ Branch 1 taken 31756532 times.
34337012 if (desc->flags & AV_PIX_FMT_FLAG_BE) {
758 2580480 x = AV_RB16(src16 + xp + 0);
759 2580480 y = AV_RB16(src16 + xp + 1);
760 2580480 z = AV_RB16(src16 + xp + 2);
761 } else {
762 31756532 x = AV_RL16(src16 + xp + 0);
763 31756532 y = AV_RL16(src16 + xp + 1);
764 31756532 z = AV_RL16(src16 + xp + 2);
765 }
766
767 34337012 x = c->xyz2rgb.gamma.in[x >> 4];
768 34337012 y = c->xyz2rgb.gamma.in[y >> 4];
769 34337012 z = c->xyz2rgb.gamma.in[z >> 4];
770
771 // convert from XYZlinear to sRGBlinear
772 34337012 r = c->xyz2rgb.mat[0][0] * x +
773 34337012 c->xyz2rgb.mat[0][1] * y +
774 34337012 c->xyz2rgb.mat[0][2] * z >> 12;
775 34337012 g = c->xyz2rgb.mat[1][0] * x +
776 34337012 c->xyz2rgb.mat[1][1] * y +
777 34337012 c->xyz2rgb.mat[1][2] * z >> 12;
778 34337012 b = c->xyz2rgb.mat[2][0] * x +
779 34337012 c->xyz2rgb.mat[2][1] * y +
780 34337012 c->xyz2rgb.mat[2][2] * z >> 12;
781
782 // limit values to 16-bit depth
783 34337012 r = av_clip_uint16(r);
784 34337012 g = av_clip_uint16(g);
785 34337012 b = av_clip_uint16(b);
786
787 // convert from sRGBlinear to RGB and scale from 12bit to 16bit
788
2/2
✓ Branch 0 taken 2580480 times.
✓ Branch 1 taken 31756532 times.
34337012 if (desc->flags & AV_PIX_FMT_FLAG_BE) {
789 2580480 AV_WB16(dst16 + xp + 0, c->xyz2rgb.gamma.out[r] << 4);
790 2580480 AV_WB16(dst16 + xp + 1, c->xyz2rgb.gamma.out[g] << 4);
791 2580480 AV_WB16(dst16 + xp + 2, c->xyz2rgb.gamma.out[b] << 4);
792 } else {
793 31756532 AV_WL16(dst16 + xp + 0, c->xyz2rgb.gamma.out[r] << 4);
794 31756532 AV_WL16(dst16 + xp + 1, c->xyz2rgb.gamma.out[g] << 4);
795 31756532 AV_WL16(dst16 + xp + 2, c->xyz2rgb.gamma.out[b] << 4);
796 }
797 }
798
799 133724 src += src_stride;
800 133724 dst += dst_stride;
801 }
802 3247 }
803
804 2902 static void rgb48Toxyz12_c(const SwsInternal *c, uint8_t *dst, int dst_stride,
805 const uint8_t *src, int src_stride, int w, int h)
806 {
807 2902 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->opts.dst_format);
808
809
2/2
✓ Branch 0 taken 126544 times.
✓ Branch 1 taken 2902 times.
129446 for (int yp = 0; yp < h; yp++) {
810 126544 uint16_t *src16 = (uint16_t *) src;
811 126544 uint16_t *dst16 = (uint16_t *) dst;
812
813
2/2
✓ Branch 0 taken 38142080 times.
✓ Branch 1 taken 126544 times.
38268624 for (int xp = 0; xp < 3 * w; xp += 3) {
814 int x, y, z, r, g, b;
815
816
2/2
✓ Branch 0 taken 4472896 times.
✓ Branch 1 taken 33669184 times.
38142080 if (desc->flags & AV_PIX_FMT_FLAG_BE) {
817 4472896 r = AV_RB16(src16 + xp + 0);
818 4472896 g = AV_RB16(src16 + xp + 1);
819 4472896 b = AV_RB16(src16 + xp + 2);
820 } else {
821 33669184 r = AV_RL16(src16 + xp + 0);
822 33669184 g = AV_RL16(src16 + xp + 1);
823 33669184 b = AV_RL16(src16 + xp + 2);
824 }
825
826 38142080 r = c->rgb2xyz.gamma.in[r >> 4];
827 38142080 g = c->rgb2xyz.gamma.in[g >> 4];
828 38142080 b = c->rgb2xyz.gamma.in[b >> 4];
829
830 // convert from sRGBlinear to XYZlinear
831 38142080 x = c->rgb2xyz.mat[0][0] * r +
832 38142080 c->rgb2xyz.mat[0][1] * g +
833 38142080 c->rgb2xyz.mat[0][2] * b >> 12;
834 38142080 y = c->rgb2xyz.mat[1][0] * r +
835 38142080 c->rgb2xyz.mat[1][1] * g +
836 38142080 c->rgb2xyz.mat[1][2] * b >> 12;
837 38142080 z = c->rgb2xyz.mat[2][0] * r +
838 38142080 c->rgb2xyz.mat[2][1] * g +
839 38142080 c->rgb2xyz.mat[2][2] * b >> 12;
840
841 // limit values to 16-bit depth
842 38142080 x = av_clip_uint16(x);
843 38142080 y = av_clip_uint16(y);
844 38142080 z = av_clip_uint16(z);
845
846 // convert from XYZlinear to X'Y'Z' and scale from 12bit to 16bit
847
2/2
✓ Branch 0 taken 4472896 times.
✓ Branch 1 taken 33669184 times.
38142080 if (desc->flags & AV_PIX_FMT_FLAG_BE) {
848 4472896 AV_WB16(dst16 + xp + 0, c->rgb2xyz.gamma.out[x] << 4);
849 4472896 AV_WB16(dst16 + xp + 1, c->rgb2xyz.gamma.out[y] << 4);
850 4472896 AV_WB16(dst16 + xp + 2, c->rgb2xyz.gamma.out[z] << 4);
851 } else {
852 33669184 AV_WL16(dst16 + xp + 0, c->rgb2xyz.gamma.out[x] << 4);
853 33669184 AV_WL16(dst16 + xp + 1, c->rgb2xyz.gamma.out[y] << 4);
854 33669184 AV_WL16(dst16 + xp + 2, c->rgb2xyz.gamma.out[z] << 4);
855 }
856 }
857
858 126544 src += src_stride;
859 126544 dst += dst_stride;
860 }
861 2902 }
862
863 104208 av_cold void ff_sws_init_xyzdsp(SwsInternal *c)
864 {
865 104208 c->xyz12Torgb48 = xyz12Torgb48_c;
866 104208 c->rgb48Toxyz12 = rgb48Toxyz12_c;
867
868 #if ARCH_AARCH64
869 ff_sws_init_xyzdsp_aarch64(c);
870 #endif
871 104208 }
872
873 5421 void ff_update_palette(SwsInternal *c, const uint32_t *pal)
874 {
875 5421 uint32_t *rgb2yuv = c->input_rgb2yuv_table;
876
877 5421 int32_t ry = rgb2yuv[RY_IDX], gy = rgb2yuv[GY_IDX], by = rgb2yuv[BY_IDX];
878 5421 int32_t ru = rgb2yuv[RU_IDX], gu = rgb2yuv[GU_IDX], bu = rgb2yuv[BU_IDX];
879 5421 int32_t rv = rgb2yuv[RV_IDX], gv = rgb2yuv[GV_IDX], bv = rgb2yuv[BV_IDX];
880
881
2/2
✓ Branch 0 taken 1387776 times.
✓ Branch 1 taken 5421 times.
1393197 for (int i = 0; i < 256; i++) {
882 1387776 int r, g, b, y, u, v, a = 0xff;
883
2/2
✓ Branch 0 taken 1119232 times.
✓ Branch 1 taken 268544 times.
1387776 if (c->opts.src_format == AV_PIX_FMT_PAL8) {
884 1119232 uint32_t p = pal[i];
885 1119232 a = (p >> 24) & 0xFF;
886 1119232 r = (p >> 16) & 0xFF;
887 1119232 g = (p >> 8) & 0xFF;
888 1119232 b = p & 0xFF;
889
2/2
✓ Branch 0 taken 36352 times.
✓ Branch 1 taken 232192 times.
268544 } else if (c->opts.src_format == AV_PIX_FMT_RGB8) {
890 36352 r = ( i >> 5 ) * 36;
891 36352 g = ((i >> 2) & 7) * 36;
892 36352 b = ( i & 3) * 85;
893
2/2
✓ Branch 0 taken 36352 times.
✓ Branch 1 taken 195840 times.
232192 } else if (c->opts.src_format == AV_PIX_FMT_BGR8) {
894 36352 b = ( i >> 6 ) * 85;
895 36352 g = ((i >> 3) & 7) * 36;
896 36352 r = ( i & 7) * 36;
897
2/2
✓ Branch 0 taken 36352 times.
✓ Branch 1 taken 159488 times.
195840 } else if (c->opts.src_format == AV_PIX_FMT_RGB4_BYTE) {
898 36352 r = ( i >> 3 ) * 255;
899 36352 g = ((i >> 1) & 3) * 85;
900 36352 b = ( i & 1) * 255;
901
3/4
✓ Branch 0 taken 36352 times.
✓ Branch 1 taken 123136 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 36352 times.
159488 } else if (c->opts.src_format == AV_PIX_FMT_GRAY8 || c->opts.src_format == AV_PIX_FMT_GRAY8A) {
902 123136 r = g = b = i;
903 } else {
904 av_assert1(c->opts.src_format == AV_PIX_FMT_BGR4_BYTE);
905 36352 b = ( i >> 3 ) * 255;
906 36352 g = ((i >> 1) & 3) * 85;
907 36352 r = ( i & 1) * 255;
908 }
909
910 1387776 y = av_clip_uint8((ry * r + gy * g + by * b + ( 33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
911 1387776 u = av_clip_uint8((ru * r + gu * g + bu * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
912 1387776 v = av_clip_uint8((rv * r + gv * g + bv * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
913
914 1387776 c->pal_yuv[i]= y + (u<<8) + (v<<16) + ((unsigned)a<<24);
915
916
3/5
✓ Branch 0 taken 855808 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6656 times.
✓ Branch 4 taken 525312 times.
1387776 switch (c->opts.dst_format) {
917 855808 case AV_PIX_FMT_BGR32:
918 #if !HAVE_BIGENDIAN
919 case AV_PIX_FMT_RGB24:
920 #endif
921 855808 c->pal_rgb[i]= r + (g<<8) + (b<<16) + ((unsigned)a<<24);
922 855808 break;
923 case AV_PIX_FMT_BGR32_1:
924 #if HAVE_BIGENDIAN
925 case AV_PIX_FMT_BGR24:
926 #endif
927 c->pal_rgb[i]= a + (r<<8) + (g<<16) + ((unsigned)b<<24);
928 break;
929 case AV_PIX_FMT_RGB32_1:
930 #if HAVE_BIGENDIAN
931 case AV_PIX_FMT_RGB24:
932 #endif
933 c->pal_rgb[i]= a + (b<<8) + (g<<16) + ((unsigned)r<<24);
934 break;
935 6656 case AV_PIX_FMT_GBRP:
936 case AV_PIX_FMT_GBRAP:
937 #if HAVE_BIGENDIAN
938 c->pal_rgb[i]= a + (r<<8) + (b<<16) + ((unsigned)g<<24);
939 #else
940 6656 c->pal_rgb[i]= g + (b<<8) + (r<<16) + ((unsigned)a<<24);
941 #endif
942 6656 break;
943 525312 case AV_PIX_FMT_RGB32:
944 #if !HAVE_BIGENDIAN
945 case AV_PIX_FMT_BGR24:
946 #endif
947 default:
948 525312 c->pal_rgb[i]= b + (g<<8) + (r<<16) + ((unsigned)a<<24);
949 }
950 }
951 5421 }
952
953 static int scale_internal(SwsContext *sws,
954 const uint8_t * const srcSlice[], const int srcStride[],
955 int srcSliceY, int srcSliceH,
956 uint8_t *const dstSlice[], const int dstStride[],
957 int dstSliceY, int dstSliceH);
958
959 static int scale_gamma(SwsInternal *c,
960 const uint8_t * const srcSlice[], const int srcStride[],
961 int srcSliceY, int srcSliceH,
962 uint8_t * const dstSlice[], const int dstStride[],
963 int dstSliceY, int dstSliceH)
964 {
965 int ret = scale_internal(c->cascaded_context[0],
966 srcSlice, srcStride, srcSliceY, srcSliceH,
967 c->cascaded_tmp[0], c->cascaded_tmpStride[0], 0, c->opts.src_h);
968
969 if (ret < 0)
970 return ret;
971
972 if (c->cascaded_context[2])
973 ret = scale_internal(c->cascaded_context[1], (const uint8_t * const *)c->cascaded_tmp[0],
974 c->cascaded_tmpStride[0], srcSliceY, srcSliceH,
975 c->cascaded_tmp[1], c->cascaded_tmpStride[1], 0, c->opts.dst_h);
976 else
977 ret = scale_internal(c->cascaded_context[1], (const uint8_t * const *)c->cascaded_tmp[0],
978 c->cascaded_tmpStride[0], srcSliceY, srcSliceH,
979 dstSlice, dstStride, dstSliceY, dstSliceH);
980
981 if (ret < 0)
982 return ret;
983
984 if (c->cascaded_context[2]) {
985 const int dstY1 = sws_internal(c->cascaded_context[1])->dstY;
986 ret = scale_internal(c->cascaded_context[2], (const uint8_t * const *)c->cascaded_tmp[1],
987 c->cascaded_tmpStride[1], dstY1 - ret, dstY1,
988 dstSlice, dstStride, dstSliceY, dstSliceH);
989 }
990 return ret;
991 }
992
993 static int scale_cascaded(SwsInternal *c,
994 const uint8_t * const srcSlice[], const int srcStride[],
995 int srcSliceY, int srcSliceH,
996 uint8_t * const dstSlice[], const int dstStride[],
997 int dstSliceY, int dstSliceH)
998 {
999 const int dstH0 = c->cascaded_context[0]->dst_h;
1000 int ret = scale_internal(c->cascaded_context[0],
1001 srcSlice, srcStride, srcSliceY, srcSliceH,
1002 c->cascaded_tmp[0], c->cascaded_tmpStride[0],
1003 0, dstH0);
1004 if (ret < 0)
1005 return ret;
1006 ret = scale_internal(c->cascaded_context[1],
1007 (const uint8_t * const * )c->cascaded_tmp[0], c->cascaded_tmpStride[0],
1008 0, dstH0, dstSlice, dstStride, dstSliceY, dstSliceH);
1009 return ret;
1010 }
1011
1012 145 static int scale_internal(SwsContext *sws,
1013 const uint8_t * const srcSlice[], const int srcStride[],
1014 int srcSliceY, int srcSliceH,
1015 uint8_t *const dstSlice[], const int dstStride[],
1016 int dstSliceY, int dstSliceH)
1017 {
1018 145 SwsInternal *c = sws_internal(sws);
1019
2/4
✓ Branch 0 taken 145 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 145 times.
145 const int scale_dst = dstSliceY > 0 || dstSliceH < sws->dst_h;
1020
3/4
✓ Branch 0 taken 145 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 90 times.
✓ Branch 3 taken 55 times.
145 const int frame_start = scale_dst || !c->sliceDir;
1021 int i, ret;
1022 const uint8_t *src2[4];
1023 uint8_t *dst2[4];
1024
1/2
✓ Branch 1 taken 145 times.
✗ Branch 2 not taken.
145 int macro_height_src = isBayer(sws->src_format) ? 2 : (1 << c->chrSrcVSubSample);
1025
1/2
✓ Branch 1 taken 145 times.
✗ Branch 2 not taken.
145 int macro_height_dst = isBayer(sws->dst_format) ? 2 : (1 << c->chrDstVSubSample);
1026 // copy strides, so they can safely be modified
1027 int srcStride2[4];
1028 int dstStride2[4];
1029 145 int srcSliceY_internal = srcSliceY;
1030
1031
4/8
✓ Branch 0 taken 145 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 145 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 145 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 145 times.
145 if (!srcStride || !dstStride || !dstSlice || !srcSlice) {
1032 av_log(c, AV_LOG_ERROR, "One of the input parameters to sws_scale() is NULL, please check the calling code\n");
1033 return AVERROR(EINVAL);
1034 }
1035
1036
1/2
✓ Branch 0 taken 145 times.
✗ Branch 1 not taken.
145 if ((srcSliceY & (macro_height_src - 1)) ||
1037
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 145 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
145 ((srcSliceH & (macro_height_src - 1)) && srcSliceY + srcSliceH != sws->src_h) ||
1038
2/4
✓ Branch 0 taken 145 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 145 times.
290 srcSliceY + srcSliceH > sws->src_h ||
1039
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
145 (isBayer(sws->src_format) && srcSliceH <= 1)) {
1040 av_log(c, AV_LOG_ERROR, "Slice parameters %d, %d are invalid\n", srcSliceY, srcSliceH);
1041 return AVERROR(EINVAL);
1042 }
1043
1044
1/2
✓ Branch 0 taken 145 times.
✗ Branch 1 not taken.
145 if ((dstSliceY & (macro_height_dst - 1)) ||
1045
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 145 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
145 ((dstSliceH & (macro_height_dst - 1)) && dstSliceY + dstSliceH != sws->dst_h) ||
1046
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 145 times.
145 dstSliceY + dstSliceH > sws->dst_h) {
1047 av_log(c, AV_LOG_ERROR, "Slice parameters %d, %d are invalid\n", dstSliceY, dstSliceH);
1048 return AVERROR(EINVAL);
1049 }
1050
1051
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 145 times.
145 if (!check_image_pointers(srcSlice, sws->src_format, srcStride)) {
1052 av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
1053 return AVERROR(EINVAL);
1054 }
1055
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 145 times.
145 if (!check_image_pointers((const uint8_t* const*)dstSlice, sws->dst_format, dstStride)) {
1056 av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
1057 return AVERROR(EINVAL);
1058 }
1059
1060 // do not mess up sliceDir if we have a "trailing" 0-size slice
1061
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 145 times.
145 if (srcSliceH == 0)
1062 return 0;
1063
1064
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 145 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
145 if (sws->gamma_flag && c->cascaded_context[0])
1065 return scale_gamma(c, srcSlice, srcStride, srcSliceY, srcSliceH,
1066 dstSlice, dstStride, dstSliceY, dstSliceH);
1067
1068
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 145 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
145 if (c->cascaded_context[0] && srcSliceY == 0 && srcSliceH == c->cascaded_context[0]->src_h)
1069 return scale_cascaded(c, srcSlice, srcStride, srcSliceY, srcSliceH,
1070 dstSlice, dstStride, dstSliceY, dstSliceH);
1071
1072
3/8
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 55 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 90 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
145 if (!srcSliceY && (sws->flags & SWS_BITEXACT) && sws->dither == SWS_DITHER_ED && c->dither_error[0])
1073 for (i = 0; i < 4; i++)
1074 memset(c->dither_error[i], 0, sizeof(c->dither_error[0][0]) * (sws->dst_w+2));
1075
1076
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 145 times.
145 if (usePal(sws->src_format))
1077 ff_update_palette(c, (const uint32_t *)srcSlice[1]);
1078
1079 145 memcpy(src2, srcSlice, sizeof(src2));
1080 145 memcpy(dst2, dstSlice, sizeof(dst2));
1081 145 memcpy(srcStride2, srcStride, sizeof(srcStride2));
1082 145 memcpy(dstStride2, dstStride, sizeof(dstStride2));
1083
1084
3/4
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 55 times.
✓ Branch 2 taken 90 times.
✗ Branch 3 not taken.
145 if (frame_start && !scale_dst) {
1085
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 90 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
90 if (srcSliceY != 0 && srcSliceY + srcSliceH != sws->src_h) {
1086 av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
1087 return AVERROR(EINVAL);
1088 }
1089
1090
1/2
✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
90 c->sliceDir = (srcSliceY == 0) ? 1 : -1;
1091
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
55 } else if (scale_dst)
1092 c->sliceDir = 1;
1093
1094
4/6
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 134 times.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 11 times.
145 if (c->src0Alpha && !c->dst0Alpha && isALPHA(sws->dst_format)) {
1095 uint8_t *base;
1096 int x,y;
1097
1098 av_fast_malloc(&c->rgb0_scratch, &c->rgb0_scratch_allocated,
1099 FFABS(srcStride[0]) * srcSliceH + 32);
1100 if (!c->rgb0_scratch)
1101 return AVERROR(ENOMEM);
1102
1103 base = srcStride[0] < 0 ? c->rgb0_scratch - srcStride[0] * (srcSliceH-1) :
1104 c->rgb0_scratch;
1105 for (y=0; y<srcSliceH; y++){
1106 memcpy(base + srcStride[0]*y, src2[0] + srcStride[0]*y, 4*sws->src_w);
1107 for (x=c->src0Alpha-1; x<4*sws->src_w; x+=4) {
1108 base[ srcStride[0]*y + x] = 0xFF;
1109 }
1110 }
1111 src2[0] = base;
1112 }
1113
1114
1/8
✗ Branch 0 not taken.
✓ Branch 1 taken 145 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
145 if (c->srcXYZ && !(c->dstXYZ && sws->src_w==sws->dst_w && sws->src_h==sws->dst_h)) {
1115 uint8_t *base;
1116
1117 av_fast_malloc(&c->xyz_scratch, &c->xyz_scratch_allocated,
1118 FFABS(srcStride[0]) * srcSliceH + 32);
1119 if (!c->xyz_scratch)
1120 return AVERROR(ENOMEM);
1121
1122 base = srcStride[0] < 0 ? c->xyz_scratch - srcStride[0] * (srcSliceH-1) :
1123 c->xyz_scratch;
1124
1125 c->xyz12Torgb48(c, base, srcStride[0], src2[0], srcStride[0], sws->src_w, srcSliceH);
1126 src2[0] = base;
1127 }
1128
1129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 145 times.
145 if (c->sliceDir != 1) {
1130 // slices go from bottom to top => we flip the image internally
1131 for (i=0; i<4; i++) {
1132 srcStride2[i] *= -1;
1133 dstStride2[i] *= -1;
1134 }
1135
1136 src2[0] += (srcSliceH - 1) * srcStride[0];
1137 if (!usePal(sws->src_format))
1138 src2[1] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[1];
1139 src2[2] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[2];
1140 src2[3] += (srcSliceH - 1) * srcStride[3];
1141 dst2[0] += ( sws->dst_h - 1) * dstStride[0];
1142 dst2[1] += ((sws->dst_h >> c->chrDstVSubSample) - 1) * dstStride[1];
1143 dst2[2] += ((sws->dst_h >> c->chrDstVSubSample) - 1) * dstStride[2];
1144 dst2[3] += ( sws->dst_h - 1) * dstStride[3];
1145
1146 srcSliceY_internal = sws->src_h-srcSliceY-srcSliceH;
1147 }
1148 145 reset_ptr(src2, sws->src_format);
1149 145 reset_ptr((void*)dst2, sws->dst_format);
1150
1151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 145 times.
145 if (c->convert_unscaled) {
1152 int offset = srcSliceY_internal;
1153 int slice_h = srcSliceH;
1154
1155 // for dst slice scaling, offset the pointers to match the unscaled API
1156 if (scale_dst) {
1157 av_assert0(offset == 0);
1158 for (i = 0; i < 4 && src2[i]; i++) {
1159 if (!src2[i] || (i > 0 && usePal(sws->src_format)))
1160 break;
1161 src2[i] += (dstSliceY >> ((i == 1 || i == 2) ? c->chrSrcVSubSample : 0)) * srcStride2[i];
1162 }
1163
1164 for (i = 0; i < 4 && dst2[i]; i++) {
1165 if (!dst2[i] || (i > 0 && usePal(sws->dst_format)))
1166 break;
1167 dst2[i] -= (dstSliceY >> ((i == 1 || i == 2) ? c->chrDstVSubSample : 0)) * dstStride2[i];
1168 }
1169 offset = dstSliceY;
1170 slice_h = dstSliceH;
1171 }
1172
1173 ret = c->convert_unscaled(c, src2, srcStride2, offset, slice_h,
1174 dst2, dstStride2);
1175 if (scale_dst)
1176 dst2[0] += dstSliceY * dstStride2[0];
1177 } else {
1178 145 ret = ff_swscale(c, src2, srcStride2, srcSliceY_internal, srcSliceH,
1179 dst2, dstStride2, dstSliceY, dstSliceH);
1180 }
1181
1182
1/8
✗ Branch 0 not taken.
✓ Branch 1 taken 145 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
145 if (c->dstXYZ && !(c->srcXYZ && sws->src_w==sws->dst_w && sws->src_h==sws->dst_h)) {
1183 uint8_t *dst;
1184
1185 if (scale_dst) {
1186 dst = dst2[0];
1187 } else {
1188 int dstY = c->dstY ? c->dstY : srcSliceY + srcSliceH;
1189
1190 av_assert0(dstY >= ret);
1191 av_assert0(ret >= 0);
1192 av_assert0(sws->dst_h >= dstY);
1193 dst = dst2[0] + (dstY - ret) * dstStride2[0];
1194 }
1195
1196 /* replace on the same data */
1197 c->rgb48Toxyz12(c, dst, dstStride2[0], dst, dstStride2[0], sws->dst_w, ret);
1198 }
1199
1200 /* reset slice direction at end of frame */
1201
3/4
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 90 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 55 times.
145 if ((srcSliceY_internal + srcSliceH == sws->src_h) || scale_dst)
1202 90 c->sliceDir = 0;
1203
1204 145 return ret;
1205 }
1206
1207 void sws_frame_end(SwsContext *sws)
1208 {
1209 SwsInternal *c = sws_internal(sws);
1210 if (!c->is_legacy_init)
1211 return;
1212 av_frame_unref(c->frame_src);
1213 av_frame_unref(c->frame_dst);
1214 c->src_ranges.nb_ranges = 0;
1215 }
1216
1217 33670 static int frame_alloc_buffers(SwsContext *sws, AVFrame *frame)
1218 {
1219 33670 SwsInternal *c = sws_internal(sws);
1220 33670 FFFramePool *pool = &c->frame_pool;
1221
1222
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33670 times.
33670 av_assert0(!frame->hw_frames_ctx);
1223 33670 const int nb_planes = av_pix_fmt_count_planes(frame->format);
1224
2/2
✓ Branch 0 taken 101400 times.
✓ Branch 1 taken 33670 times.
135070 for (int i = 0; i < nb_planes; i++) {
1225 101400 frame->linesize[i] = pool->linesize[i];
1226 101400 frame->buf[i] = av_buffer_pool_get(pool->pools[i]);
1227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101400 times.
101400 if (!frame->buf[i])
1228 return AVERROR(ENOMEM);
1229 101400 frame->data[i] = frame->buf[i]->data;
1230 }
1231
1232 33670 return 0;
1233 }
1234
1235 int sws_frame_start(SwsContext *sws, AVFrame *dst, const AVFrame *src)
1236 {
1237 SwsInternal *c = sws_internal(sws);
1238 int ret, allocated = 0;
1239 if (!c->is_legacy_init)
1240 return AVERROR(EINVAL);
1241
1242 ret = av_frame_ref(c->frame_src, src);
1243 if (ret < 0)
1244 return ret;
1245
1246 if (!dst->buf[0]) {
1247 dst->width = sws->dst_w;
1248 dst->height = sws->dst_h;
1249 dst->format = sws->dst_format;
1250
1251 ret = av_frame_get_buffer(dst, 0);
1252 if (ret < 0)
1253 return ret;
1254 allocated = 1;
1255 }
1256
1257 ret = av_frame_ref(c->frame_dst, dst);
1258 if (ret < 0) {
1259 if (allocated)
1260 av_frame_unref(dst);
1261
1262 return ret;
1263 }
1264
1265 return 0;
1266 }
1267
1268 int sws_send_slice(SwsContext *sws, unsigned int slice_start,
1269 unsigned int slice_height)
1270 {
1271 SwsInternal *c = sws_internal(sws);
1272 int ret;
1273 if (!c->is_legacy_init)
1274 return AVERROR(EINVAL);
1275
1276 ret = ff_range_add(&c->src_ranges, slice_start, slice_height);
1277 if (ret < 0)
1278 return ret;
1279
1280 return 0;
1281 }
1282
1283 unsigned int sws_receive_slice_alignment(const SwsContext *sws)
1284 {
1285 SwsInternal *c = sws_internal(sws);
1286 if (c->slice_ctx)
1287 return sws_internal(c->slice_ctx[0])->dst_slice_align;
1288
1289 return c->dst_slice_align;
1290 }
1291
1292 int sws_receive_slice(SwsContext *sws, unsigned int slice_start,
1293 unsigned int slice_height)
1294 {
1295 SwsInternal *c = sws_internal(sws);
1296 unsigned int align = sws_receive_slice_alignment(sws);
1297 uint8_t *dst[4];
1298 if (!c->is_legacy_init)
1299 return AVERROR(EINVAL);
1300
1301 /* wait until complete input has been received */
1302 if (!(c->src_ranges.nb_ranges == 1 &&
1303 c->src_ranges.ranges[0].start == 0 &&
1304 c->src_ranges.ranges[0].len == sws->src_h))
1305 return AVERROR(EAGAIN);
1306
1307 if ((slice_start > 0 || slice_height < sws->dst_h) &&
1308 (slice_start % align || slice_height % align)) {
1309 av_log(c, AV_LOG_ERROR,
1310 "Incorrectly aligned output: %u/%u not multiples of %u\n",
1311 slice_start, slice_height, align);
1312 return AVERROR(EINVAL);
1313 }
1314
1315 if (c->slicethread) {
1316 int nb_jobs = c->nb_slice_ctx;
1317 int ret = 0;
1318
1319 if (c->slice_ctx[0]->dither == SWS_DITHER_ED)
1320 nb_jobs = 1;
1321
1322 c->dst_slice_start = slice_start;
1323 c->dst_slice_height = slice_height;
1324
1325 avpriv_slicethread_execute(c->slicethread, nb_jobs, 0);
1326
1327 for (int i = 0; i < c->nb_slice_ctx; i++) {
1328 if (c->slice_err[i] < 0) {
1329 ret = c->slice_err[i];
1330 break;
1331 }
1332 }
1333
1334 memset(c->slice_err, 0, c->nb_slice_ctx * sizeof(*c->slice_err));
1335
1336 return ret;
1337 }
1338
1339 for (int i = 0; i < FF_ARRAY_ELEMS(dst); i++) {
1340 ptrdiff_t offset = c->frame_dst->linesize[i] * (ptrdiff_t)(slice_start >> c->chrDstVSubSample);
1341 dst[i] = FF_PTR_ADD(c->frame_dst->data[i], offset);
1342 }
1343
1344 return scale_internal(sws, (const uint8_t * const *)c->frame_src->data,
1345 c->frame_src->linesize, 0, sws->src_h,
1346 dst, c->frame_dst->linesize, slice_start, slice_height);
1347 }
1348
1349 /* Subset of av_frame_ref() that only references (video) data buffers */
1350 261 static int frame_ref(AVFrame *dst, const AVFrame *src)
1351 {
1352 /* ref the buffers */
1353
1/2
✓ Branch 0 taken 1049 times.
✗ Branch 1 not taken.
1049 for (int i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) {
1354
2/2
✓ Branch 0 taken 261 times.
✓ Branch 1 taken 788 times.
1049 if (!src->buf[i])
1355 261 break;
1356 788 dst->buf[i] = av_buffer_ref(src->buf[i]);
1357
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 788 times.
788 if (!dst->buf[i])
1358 return AVERROR(ENOMEM);
1359 }
1360
1361 261 memcpy(dst->data, src->data, sizeof(src->data));
1362 261 memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
1363 261 return 0;
1364 }
1365
1366 139442 int sws_scale_frame(SwsContext *sws, AVFrame *dst, const AVFrame *src)
1367 {
1368 139442 int ret, allocated = 0;
1369 139442 SwsInternal *c = sws_internal(sws);
1370
2/4
✓ Branch 0 taken 139442 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 139442 times.
139442 if (!src || !dst)
1371 return AVERROR(EINVAL);
1372
1373
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 139442 times.
139442 if (c->is_legacy_init) {
1374 /* Context has been initialized with explicit values, fall back to
1375 * legacy API behavior. */
1376 ret = sws_frame_start(sws, dst, src);
1377 if (ret < 0)
1378 return ret;
1379
1380 ret = sws_send_slice(sws, 0, src->height);
1381 if (ret >= 0)
1382 ret = sws_receive_slice(sws, 0, dst->height);
1383
1384 sws_frame_end(sws);
1385
1386 return ret;
1387 }
1388
1389 139442 ret = sws_frame_setup(sws, dst, src);
1390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 139442 times.
139442 if (ret < 0)
1391 return ret;
1392
1393
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 139442 times.
139442 if (!src->data[0])
1394 return 0;
1395
1396 139442 const SwsGraph *top = c->graph[FIELD_TOP];
1397 139442 const SwsGraph *bot = c->graph[FIELD_BOTTOM];
1398
2/2
✓ Branch 0 taken 105511 times.
✓ Branch 1 taken 33931 times.
139442 if (dst->data[0]) /* user-provided buffers */
1399 105511 goto process_frame;
1400
1401 /* Sanity */
1402 33931 memset(dst->buf, 0, sizeof(dst->buf));
1403 33931 memset(dst->data, 0, sizeof(dst->data));
1404 33931 memset(dst->linesize, 0, sizeof(dst->linesize));
1405 33931 dst->extended_data = dst->data;
1406
1407
4/8
✓ Branch 0 taken 33931 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 261 times.
✓ Branch 3 taken 33670 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 261 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
33931 if (src->buf[0] && top->noop && (!bot || bot->noop))
1408 261 return frame_ref(dst, src);
1409
1410 33670 ret = frame_alloc_buffers(sws, dst);
1411
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33670 times.
33670 if (ret < 0)
1412 return ret;
1413 33670 allocated = 1;
1414
1415 139181 process_frame:
1416
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 278362 times.
✓ Branch 2 taken 139181 times.
✓ Branch 3 taken 139181 times.
278362 for (int field = 0; field < (bot ? 2 : 1); field++) {
1417 139181 ret = ff_sws_graph_run(c->graph[field], dst, src);
1418
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 139181 times.
139181 if (ret < 0) {
1419 if (allocated)
1420 av_frame_unref(dst);
1421 return ret;
1422 }
1423 }
1424
1425 139181 return 0;
1426 }
1427
1428 156342 static int validate_params(SwsContext *ctx)
1429 {
1430 #define VALIDATE(field, min, max) \
1431 if (ctx->field < min || ctx->field > max) { \
1432 av_log(ctx, AV_LOG_ERROR, "'%s' (%d) out of range [%d, %d]\n", \
1433 #field, (int) ctx->field, min, max); \
1434 return AVERROR(EINVAL); \
1435 }
1436
1437
2/4
✓ Branch 0 taken 156342 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 156342 times.
156342 VALIDATE(threads, 0, SWS_MAX_THREADS);
1438
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156342 times.
156342 VALIDATE(dither, 0, SWS_DITHER_NB - 1)
1439
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156342 times.
156342 VALIDATE(alpha_blend, 0, SWS_ALPHA_BLEND_NB - 1)
1440 156342 return 0;
1441 }
1442
1443 156342 int sws_frame_setup(SwsContext *ctx, const AVFrame *dst, const AVFrame *src)
1444 {
1445 156342 SwsInternal *s = sws_internal(ctx);
1446 const char *err_msg;
1447 int ret;
1448
1449
2/4
✓ Branch 0 taken 156342 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 156342 times.
156342 if (!src || !dst)
1450 return AVERROR(EINVAL);
1451
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 156342 times.
156342 if ((ret = validate_params(ctx)) < 0)
1452 return ret;
1453
1454 /* For now, if a single frame has a context, then both need a context */
1455
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156342 times.
156342 if (!!src->hw_frames_ctx != !!dst->hw_frames_ctx) {
1456 return AVERROR(ENOTSUP);
1457
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156342 times.
156342 } else if (!!src->hw_frames_ctx) {
1458 /* Both hardware frames must already be allocated */
1459 if (!src->data[0] || !dst->data[0])
1460 return AVERROR(EINVAL);
1461
1462 AVHWFramesContext *src_hwfc, *dst_hwfc;
1463 src_hwfc = (AVHWFramesContext *)src->hw_frames_ctx->data;
1464 dst_hwfc = (AVHWFramesContext *)dst->hw_frames_ctx->data;
1465
1466 /* Both frames must live on the same device */
1467 if (src_hwfc->device_ref->data != dst_hwfc->device_ref->data)
1468 return AVERROR(EINVAL);
1469
1470 /* Only Vulkan devices are supported */
1471 AVHWDeviceContext *dev_ctx;
1472 dev_ctx = (AVHWDeviceContext *)src_hwfc->device_ref->data;
1473 if (dev_ctx->type != AV_HWDEVICE_TYPE_VULKAN)
1474 return AVERROR(ENOTSUP);
1475
1476 #if CONFIG_UNSTABLE && CONFIG_VULKAN
1477 ret = ff_sws_vk_init(ctx, src_hwfc->device_ref);
1478 if (ret < 0)
1479 return ret;
1480 #endif
1481 }
1482
1483 156342 int dst_width = dst->width;
1484
1/2
✓ Branch 0 taken 156342 times.
✗ Branch 1 not taken.
156342 for (int field = 0; field < 2; field++) {
1485 156342 SwsFormat src_fmt = ff_fmt_from_frame(src, field);
1486 156342 SwsFormat dst_fmt = ff_fmt_from_frame(dst, field);
1487 int src_ok, dst_ok;
1488
1489
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156342 times.
156342 if ((src->flags ^ dst->flags) & AV_FRAME_FLAG_INTERLACED) {
1490 err_msg = "Cannot convert interlaced to progressive frames or vice versa.\n";
1491 ret = AVERROR(EINVAL);
1492 goto fail;
1493 }
1494
1495 156342 src_ok = ff_test_fmt(&src_fmt, 0);
1496 156342 dst_ok = ff_test_fmt(&dst_fmt, 1);
1497
2/6
✓ Branch 0 taken 156342 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 156342 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
156342 if ((!src_ok || !dst_ok) && !ff_props_equal(&src_fmt, &dst_fmt)) {
1498 err_msg = src_ok ? "Unsupported output" : "Unsupported input";
1499 ret = AVERROR(ENOTSUP);
1500 goto fail;
1501 }
1502
1503 156342 ret = ff_sws_graph_reinit(ctx, &dst_fmt, &src_fmt, field, &s->graph[field]);
1504
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156342 times.
156342 if (ret < 0) {
1505 err_msg = "Failed initializing scaling graph";
1506 goto fail;
1507 }
1508
1509 156342 const SwsGraph *graph = s->graph[field];
1510
3/4
✓ Branch 0 taken 156328 times.
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 156328 times.
156342 if (graph->incomplete && ctx->flags & SWS_STRICT) {
1511 err_msg = "Incomplete scaling graph";
1512 ret = AVERROR(EINVAL);
1513 goto fail;
1514 }
1515
1516
2/2
✓ Branch 0 taken 155951 times.
✓ Branch 1 taken 391 times.
156342 if (!graph->noop) {
1517
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 155951 times.
155951 av_assert0(graph->num_passes);
1518 155951 const SwsPass *last_pass = graph->passes[graph->num_passes - 1];
1519 155951 const int aligned_w = ff_sws_pass_aligned_width(last_pass, dst->width);
1520 155951 dst_width = FFMAX(dst_width, aligned_w);
1521 }
1522
1523
1/2
✓ Branch 0 taken 156342 times.
✗ Branch 1 not taken.
156342 if (!src_fmt.interlaced) {
1524 156342 ff_sws_graph_free(&s->graph[FIELD_BOTTOM]);
1525 156342 break;
1526 }
1527
1528 continue;
1529
1530 fail:
1531 av_log(ctx, AV_LOG_ERROR, "%s (%s): fmt:%s csp:%s prim:%s trc:%s ->"
1532 " fmt:%s csp:%s prim:%s trc:%s\n",
1533 err_msg, av_err2str(ret),
1534 av_get_pix_fmt_name(src_fmt.format), av_color_space_name(src_fmt.csp),
1535 av_color_primaries_name(src_fmt.color.prim), av_color_transfer_name(src_fmt.color.trc),
1536 av_get_pix_fmt_name(dst_fmt.format), av_color_space_name(dst_fmt.csp),
1537 av_color_primaries_name(dst_fmt.color.prim), av_color_transfer_name(dst_fmt.color.trc));
1538
1539 for (int i = 0; i < FF_ARRAY_ELEMS(s->graph); i++)
1540 ff_sws_graph_free(&s->graph[i]);
1541
1542 return ret;
1543 }
1544
1545
1/2
✓ Branch 0 taken 156342 times.
✗ Branch 1 not taken.
156342 if (!dst->hw_frames_ctx) {
1546 156342 ret = ff_frame_pool_video_reinit(&s->frame_pool, dst_width, dst->height,
1547 156342 dst->format, av_cpu_max_align());
1548
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156342 times.
156342 if (ret < 0)
1549 return ret;
1550 }
1551
1552 156342 return 0;
1553 }
1554
1555 /**
1556 * swscale wrapper, so we don't need to export the SwsContext.
1557 * Assumes planar YUV to be in YUV order instead of YVU.
1558 */
1559 145 int attribute_align_arg sws_scale(SwsContext *sws,
1560 const uint8_t * const srcSlice[],
1561 const int srcStride[], int srcSliceY,
1562 int srcSliceH, uint8_t *const dst[],
1563 const int dstStride[])
1564 {
1565 145 SwsInternal *c = sws_internal(sws);
1566
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 145 times.
145 if (!c->is_legacy_init)
1567 return AVERROR(EINVAL);
1568
1569
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 145 times.
145 if (c->nb_slice_ctx) {
1570 sws = c->slice_ctx[0];
1571 c = sws_internal(sws);
1572 }
1573
1574 145 return scale_internal(sws, srcSlice, srcStride, srcSliceY, srcSliceH,
1575 dst, dstStride, 0, sws->dst_h);
1576 }
1577
1578 void ff_sws_slice_worker(void *priv, int jobnr, int threadnr,
1579 int nb_jobs, int nb_threads)
1580 {
1581 SwsInternal *parent = priv;
1582 SwsContext *sws = parent->slice_ctx[threadnr];
1583 SwsInternal *c = sws_internal(sws);
1584
1585 const int slice_height = FFALIGN(FFMAX((parent->dst_slice_height + nb_jobs - 1) / nb_jobs, 1),
1586 c->dst_slice_align);
1587 const int slice_start = jobnr * slice_height;
1588 const int slice_end = FFMIN((jobnr + 1) * slice_height, parent->dst_slice_height);
1589 int err = 0;
1590
1591 if (slice_end > slice_start) {
1592 uint8_t *dst[4] = { NULL };
1593
1594 for (int i = 0; i < FF_ARRAY_ELEMS(dst) && parent->frame_dst->data[i]; i++) {
1595 const int vshift = (i == 1 || i == 2) ? c->chrDstVSubSample : 0;
1596 const ptrdiff_t offset = parent->frame_dst->linesize[i] *
1597 (ptrdiff_t)((slice_start + parent->dst_slice_start) >> vshift);
1598
1599 dst[i] = parent->frame_dst->data[i] + offset;
1600 }
1601
1602 err = scale_internal(sws, (const uint8_t * const *)parent->frame_src->data,
1603 parent->frame_src->linesize, 0, sws->src_h,
1604 dst, parent->frame_dst->linesize,
1605 parent->dst_slice_start + slice_start, slice_end - slice_start);
1606 }
1607
1608 parent->slice_err[threadnr] = err;
1609 }
1610