FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libswscale/ops_dispatch.c
Date: 2026-09-15 03:49:06
Exec Total Coverage
Lines: 428 473 90.5%
Functions: 20 20 100.0%
Branches: 237 295 80.3%

Line Branch Exec Source
1 /**
2 * Copyright (C) 2025 Niklas Haas
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 "libavutil/avassert.h"
22 #include "libavutil/cpu.h"
23 #include "libavutil/mathematics.h"
24 #include "libavutil/mem.h"
25 #include "libavutil/mem_internal.h"
26 #include "libavutil/refstruct.h"
27
28 #include "ops.h"
29 #include "ops_internal.h"
30 #include "ops_dispatch.h"
31 #include "swscale_internal.h"
32
33 #define RET(x) \
34 do { \
35 if ((ret = (x)) < 0) \
36 goto fail; \
37 } while (0)
38
39 typedef struct SwsOpPass {
40 SwsCompiledOp comp;
41 SwsOpExec exec_base;
42 SwsOpExec exec_tail;
43 size_t num_blocks;
44 int tail_off_in;
45 int tail_off_out;
46 int tail_size_in;
47 int tail_size_out;
48 int planes_in;
49 int planes_out;
50 int pixel_bits_in;
51 int pixel_bits_out;
52 int idx_in[4];
53 int idx_out[4];
54 int palette_idx;
55 int *offsets_y;
56 int filter_size_h;
57 int filter_size_v;
58 bool memcpy_first;
59 bool memcpy_last;
60 bool memcpy_out;
61 size_t tail_blocks;
62 uint8_t *tail_buf; /* extra memory for fixing unpadded tails */
63 unsigned int tail_buf_size;
64 } SwsOpPass;
65
66 1629383 static int compile_backend(SwsContext *ctx, const SwsOpBackend *backend,
67 const SwsOpList *ops, SwsCompiledOp *out)
68 {
69 SwsOpList *copy;
70 1629383 SwsCompiledOp compiled = {0};
71 1629383 int ret = 0;
72
73 1629383 copy = ff_sws_op_list_duplicate(ops);
74
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1629383 times.
1629383 if (!copy)
75 return AVERROR(ENOMEM);
76
77 /* Ensure these are always set during compilation */
78 1629383 ff_sws_op_list_update_comps(copy);
79
80 1629383 ret = backend->compile(ctx, copy, &compiled);
81
2/2
✓ Branch 0 taken 587294 times.
✓ Branch 1 taken 1042089 times.
1629383 if (ret < 0) {
82
1/2
✓ Branch 0 taken 587294 times.
✗ Branch 1 not taken.
587294 int msg_lev = ret == AVERROR(ENOTSUP) ? AV_LOG_TRACE : AV_LOG_ERROR;
83 1174588 av_log(ctx, msg_lev, "Backend '%s' failed to compile operations: %s\n",
84 587294 backend->name, av_err2str(ret));
85 587294 goto fail;
86 }
87
88 1042089 compiled.backend = backend;
89 1042089 *out = compiled;
90
91 1042089 av_log(ctx, AV_LOG_VERBOSE, "Compiled using backend '%s': "
92 "block size = %d, over-read = {%d %d %d %d}, over-write = {%d %d %d %d}, "
93 1042089 "cpu flags = 0x%x\n", backend->name, out->block_size,
94 out->over_read[0], out->over_read[1],
95 out->over_read[2], out->over_read[3],
96 out->over_write[0], out->over_write[1],
97 out->over_write[2], out->over_write[3],
98 out->cpu_flags);
99
100 1042089 ff_sws_op_list_print(ctx, AV_LOG_VERBOSE, AV_LOG_TRACE, ops);
101
102 1629383 fail:
103 1629383 ff_sws_op_list_free(&copy);
104 1629383 return ret;
105 }
106
107 1476951 int ff_sws_ops_compile(SwsContext *ctx, const SwsOpBackend *backend,
108 const SwsOpList *ops, SwsCompiledOp *out)
109 {
110
2/2
✓ Branch 0 taken 1359063 times.
✓ Branch 1 taken 117888 times.
1476951 if (backend)
111 1359063 return compile_backend(ctx, backend, ops, out);
112
113 117888 const SwsBackend enabled = ff_sws_enabled_backends(ctx);
114
2/2
✓ Branch 0 taken 270320 times.
✓ Branch 1 taken 31998 times.
302318 for (int n = 0; ff_sws_op_backends[n]; n++) {
115 270320 const SwsOpBackend *backend = ff_sws_op_backends[n];
116
1/2
✓ Branch 0 taken 270320 times.
✗ Branch 1 not taken.
270320 if (ops->src.hw_format != backend->hw_format ||
117
1/2
✓ Branch 0 taken 270320 times.
✗ Branch 1 not taken.
270320 ops->dst.hw_format != backend->hw_format ||
118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 270320 times.
270320 !(enabled & backend->flags))
119 continue;
120
2/2
✓ Branch 1 taken 184430 times.
✓ Branch 2 taken 85890 times.
270320 if (compile_backend(ctx, backend, ops, out) < 0)
121 184430 continue;
122
123 85890 return 0;
124 }
125
126 31998 return AVERROR(ENOTSUP);
127 }
128
129 1487116 void ff_sws_compiled_op_unref(SwsCompiledOp *comp)
130 {
131
2/2
✓ Branch 0 taken 96055 times.
✓ Branch 1 taken 1391061 times.
1487116 if (comp->free)
132 96055 comp->free(comp->priv);
133
134 1487116 *comp = (SwsCompiledOp) {0};
135 1487116 }
136
137 1476951 static void op_pass_free(void *ptr)
138 {
139 1476951 SwsOpPass *p = ptr;
140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1476951 times.
1476951 if (!p)
141 return;
142
143 1476951 ff_sws_compiled_op_unref(&p->comp);
144 1476951 av_refstruct_unref(&p->offsets_y);
145 1476951 av_free(p->exec_base.in_bump_y);
146 1476951 av_free(p->exec_base.in_offset_x);
147 1476951 av_free(p->tail_buf);
148 1476951 av_free(p);
149 }
150
151 86470 static inline void get_row_data(const SwsOpPass *p, const int y_dst,
152 const uint8_t *in[4], uint8_t *out[4])
153 {
154 86470 const SwsOpExec *base = &p->exec_base;
155
2/2
✓ Branch 0 taken 18948 times.
✓ Branch 1 taken 67522 times.
86470 const int y_src = p->offsets_y ? p->offsets_y[y_dst] : y_dst;
156
2/2
✓ Branch 0 taken 168926 times.
✓ Branch 1 taken 86470 times.
255396 for (int i = 0; i < p->planes_in; i++)
157 168926 in[i] = base->in[i] + (y_src >> base->in_sub_y[i]) * base->in_stride[i];
158
2/2
✓ Branch 0 taken 188397 times.
✓ Branch 1 taken 86470 times.
274867 for (int i = 0; i < p->planes_out; i++)
159 188397 out[i] = base->out[i] + (y_dst >> base->out_sub_y[i]) * base->out_stride[i];
160 86470 }
161
162 16542 static inline int get_lines_in(const SwsOpPass *p, const int y, const int h,
163 const int plane)
164 {
165 16542 const SwsOpExec *base = &p->exec_base;
166
1/2
✓ Branch 0 taken 16542 times.
✗ Branch 1 not taken.
16542 if (!p->offsets_y)
167 16542 return h >> base->in_sub_y[plane];
168
169 const int y0 = p->offsets_y[y] >> base->in_sub_y[plane];
170 const int y1 = (p->offsets_y[y + h - 1] + p->filter_size_v - 1) >> base->in_sub_y[plane];
171 return y1 - y0 + 1;
172 }
173
174 98606 static inline size_t pixel_bytes(size_t pixels, int pixel_bits,
175 enum AVRounding rounding)
176 {
177 98606 const uint64_t bits = (uint64_t) pixels * pixel_bits;
178
2/3
✓ Branch 0 taken 14134 times.
✓ Branch 1 taken 84472 times.
✗ Branch 2 not taken.
98606 switch (rounding) {
179 14134 case AV_ROUND_ZERO:
180 case AV_ROUND_DOWN:
181 14134 return bits >> 3;
182 84472 case AV_ROUND_INF:
183 case AV_ROUND_UP:
184 84472 return (bits + 7) >> 3;
185 default:
186 av_unreachable("Invalid rounding mode");
187 return (size_t) -1;
188 }
189 }
190
191 357323 static size_t safe_bytes_pad(int linesize, int plane_pad)
192 {
193 av_assert1(linesize);
194 357323 int64_t safe_bytes = FFABS((int64_t) linesize) - plane_pad;
195 357323 return FFMAX(safe_bytes, 0);
196 }
197
198 52605 static size_t safe_blocks_offset(size_t num_blocks, unsigned block_size,
199 ptrdiff_t safe_offset,
200 const int32_t *offset_bytes)
201 {
202 52605 size_t safe_blocks = num_blocks;
203
3/4
✓ Branch 0 taken 64298 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11693 times.
✓ Branch 3 taken 52605 times.
64298 while (safe_blocks && offset_bytes[safe_blocks * block_size - 1] > safe_offset)
204 11693 safe_blocks--;
205 52605 return safe_blocks;
206 }
207
208 86470 static int op_pass_setup(const SwsFrame *out, const SwsFrame *in,
209 const SwsPass *pass)
210 {
211 86470 const AVPixFmtDescriptor *indesc = av_pix_fmt_desc_get(in->format);
212 86470 const bool float_in = indesc->flags & AV_PIX_FMT_FLAG_FLOAT;
213 86470 const int width = out->width;
214
215 86470 SwsOpPass *p = pass->priv;
216 86470 SwsOpExec *exec = &p->exec_base;
217 86470 const SwsCompiledOp *comp = &p->comp;
218
219 /* Set up main loop parameters */
220 86470 const unsigned block_size = comp->block_size;
221 86470 const size_t num_blocks = (width + block_size - 1) / block_size;
222 86470 const size_t aligned_w = num_blocks * block_size;
223
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 86470 times.
86470 if (aligned_w < width) /* overflow */
224 return AVERROR(EINVAL);
225 86470 p->num_blocks = num_blocks;
226 86470 p->memcpy_first = false;
227 86470 p->memcpy_last = false;
228 86470 p->memcpy_out = false;
229
230 86470 size_t safe_blocks = num_blocks;
231
2/2
✓ Branch 0 taken 168926 times.
✓ Branch 1 taken 86470 times.
255396 for (int i = 0; i < p->planes_in; i++) {
232 168926 const int idx = p->idx_in[i];
233 168926 size_t input_bytes = in->linesize[idx];
234
4/4
✓ Branch 0 taken 52605 times.
✓ Branch 1 taken 116321 times.
✓ Branch 2 taken 8310 times.
✓ Branch 3 taken 44295 times.
168926 if (p->filter_size_h && float_in) {
235 /* Floating point inputs may contain NaN / Infinity in the padding */
236 8310 const int plane_w = AV_CEIL_RSHIFT(in->width, exec->in_sub_x[i]);
237 8310 input_bytes = pixel_bytes(plane_w, p->pixel_bits_in, AV_ROUND_UP);
238 }
239
240 168926 size_t safe_bytes = safe_bytes_pad(input_bytes, comp->over_read[i]);
241 size_t safe_blocks_in;
242
2/2
✓ Branch 0 taken 52605 times.
✓ Branch 1 taken 116321 times.
168926 if (exec->in_offset_x) {
243 52605 size_t filter_size = pixel_bytes(p->filter_size_h, p->pixel_bits_in,
244 AV_ROUND_UP);
245 52605 safe_blocks_in = safe_blocks_offset(num_blocks, block_size,
246 52605 safe_bytes - filter_size,
247 52605 exec->in_offset_x);
248 } else {
249 116321 safe_blocks_in = safe_bytes / exec->block_size_in[i];
250 }
251
252
2/2
✓ Branch 0 taken 16542 times.
✓ Branch 1 taken 152384 times.
168926 if (safe_blocks_in < num_blocks) {
253 16542 p->memcpy_first |= in->linesize[idx] < 0;
254 16542 p->memcpy_last |= in->linesize[idx] > 0;
255 16542 safe_blocks = FFMIN(safe_blocks, safe_blocks_in);
256 }
257
258 168926 size_t loop_size = num_blocks * exec->block_size_in[i];
259 168926 exec->in[i] = in->data[idx];
260 168926 exec->in_stride[i] = in->linesize[idx];
261 168926 exec->in_bump[i] = in->linesize[idx] - loop_size;
262 }
263
264
2/2
✓ Branch 0 taken 188397 times.
✓ Branch 1 taken 86470 times.
274867 for (int i = 0; i < p->planes_out; i++) {
265 188397 const int idx = p->idx_out[i];
266 188397 size_t safe_bytes = safe_bytes_pad(out->linesize[idx], comp->over_write[i]);
267 188397 size_t safe_blocks_out = safe_bytes / exec->block_size_out[i];
268
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 188394 times.
188397 if (safe_blocks_out < num_blocks) {
269 3 p->memcpy_out = true;
270 3 safe_blocks = FFMIN(safe_blocks, safe_blocks_out);
271 }
272
273 188397 size_t loop_size = num_blocks * exec->block_size_out[i];
274 188397 exec->out[i] = out->data[idx];
275 188397 exec->out_stride[i] = out->linesize[idx];
276 188397 exec->out_bump[i] = out->linesize[idx] - loop_size;
277 }
278
279
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 86470 times.
86470 if (p->palette_idx >= 0) {
280 exec->in[1] = in->data[p->palette_idx];
281 exec->in_stride[1] = exec->in_bump[1] = 0;
282 }
283
284
3/4
✓ Branch 0 taken 86470 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9352 times.
✓ Branch 3 taken 77118 times.
86470 const bool memcpy_in = p->memcpy_first || p->memcpy_last;
285
3/4
✓ Branch 0 taken 77118 times.
✓ Branch 1 taken 9352 times.
✓ Branch 2 taken 77118 times.
✗ Branch 3 not taken.
86470 if (!memcpy_in && !p->memcpy_out) {
286
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 77118 times.
77118 av_assert0(safe_blocks == num_blocks);
287 77118 return 0;
288 }
289
290 /* Set-up tail section parameters and buffers */
291 9352 SwsOpExec *tail = &p->exec_tail;
292 9352 const int align = av_cpu_max_align();
293 9352 size_t alloc_size = 0;
294 9352 *tail = *exec;
295
296 9352 const size_t safe_width = safe_blocks * block_size;
297 9352 const size_t tail_size = width - safe_width;
298 9352 p->tail_off_out = pixel_bytes(safe_width, p->pixel_bits_out, AV_ROUND_DOWN);
299 9352 p->tail_size_out = pixel_bytes(tail_size, p->pixel_bits_out, AV_ROUND_UP);
300 9352 p->tail_blocks = num_blocks - safe_blocks;
301
302
2/2
✓ Branch 0 taken 4570 times.
✓ Branch 1 taken 4782 times.
9352 if (exec->in_offset_x) {
303 4570 p->tail_off_in = exec->in_offset_x[safe_width];
304 4570 p->tail_size_in = exec->in_offset_x[width - 1] - p->tail_off_in;
305 4570 p->tail_size_in += pixel_bytes(p->filter_size_h, p->pixel_bits_in, AV_ROUND_UP);
306 } else {
307 4782 p->tail_off_in = pixel_bytes(safe_width, p->pixel_bits_in, AV_ROUND_DOWN);
308 4782 p->tail_size_in = pixel_bytes(tail_size, p->pixel_bits_in, AV_ROUND_UP);
309 }
310
311 9352 const size_t alloc_width = aligned_w - safe_width;
312
3/4
✓ Branch 0 taken 25894 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16542 times.
✓ Branch 3 taken 9352 times.
25894 for (int i = 0; memcpy_in && i < p->planes_in; i++) {
313 size_t needed_size;
314
2/2
✓ Branch 0 taken 11693 times.
✓ Branch 1 taken 4849 times.
16542 if (exec->in_offset_x) {
315 /* The input offset map is already padded to multiples of the block
316 * size, and clamps the input offsets to the image boundaries; so
317 * we just need to compensate for the comp->over_read */
318 11693 needed_size = p->tail_size_in;
319 } else {
320 4849 needed_size = pixel_bytes(alloc_width, p->pixel_bits_in, AV_ROUND_UP);
321 }
322 16542 size_t loop_size = p->tail_blocks * exec->block_size_in[i];
323 16542 tail->in_stride[i] = FFALIGN(needed_size + comp->over_read[i], align);
324 16542 tail->in_bump[i] = tail->in_stride[i] - loop_size;
325 16542 alloc_size += tail->in_stride[i] * in->height;
326 }
327
328
4/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 9351 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 1 times.
9356 for (int i = 0; p->memcpy_out && i < p->planes_out; i++) {
329 4 size_t needed_size = pixel_bytes(alloc_width, p->pixel_bits_out, AV_ROUND_UP);
330 4 size_t loop_size = p->tail_blocks * exec->block_size_out[i];
331 4 tail->out_stride[i] = FFALIGN(needed_size + comp->over_write[i], align);
332 4 tail->out_bump[i] = tail->out_stride[i] - loop_size;
333 4 alloc_size += tail->out_stride[i] * out->height;
334 }
335
336
3/4
✓ Branch 0 taken 9352 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4570 times.
✓ Branch 3 taken 4782 times.
9352 if (memcpy_in && exec->in_offset_x) {
337 /* `in_offset_x` is indexed relative to the line start, not the start
338 * of the section being processed; so we need to over-allocate this
339 * array to the full width of the image, even though we will only
340 * partially fill in the offsets relevant to the tail region */
341 4570 alloc_size += aligned_w * sizeof(*exec->in_offset_x);
342 }
343
344 9352 av_fast_mallocz(&p->tail_buf, &p->tail_buf_size, alloc_size);
345
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9352 times.
9352 if (!p->tail_buf)
346 return AVERROR(ENOMEM);
347
348 9352 uint8_t *tail_buf = p->tail_buf;
349
3/4
✓ Branch 0 taken 25894 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16542 times.
✓ Branch 3 taken 9352 times.
25894 for (int i = 0; memcpy_in && i < p->planes_in; i++) {
350 16542 tail->in[i] = tail_buf;
351 16542 tail_buf += tail->in_stride[i] * in->height;
352 }
353
354
4/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 9351 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 1 times.
9356 for (int i = 0; p->memcpy_out && i < p->planes_out; i++) {
355 4 tail->out[i] = tail_buf;
356 4 tail_buf += tail->out_stride[i] * out->height;
357 }
358
359
3/4
✓ Branch 0 taken 9352 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4570 times.
✓ Branch 3 taken 4782 times.
9352 if (memcpy_in && exec->in_offset_x) {
360 4570 tail->in_offset_x = (int32_t *) tail_buf;
361
2/2
✓ Branch 0 taken 73120 times.
✓ Branch 1 taken 4570 times.
77690 for (int i = safe_width; i < aligned_w; i++)
362 73120 tail->in_offset_x[i] = exec->in_offset_x[i] - p->tail_off_in;
363 }
364
365 9352 return 0;
366 }
367
368 16546 static void copy_lines(uint8_t *dst, const ptrdiff_t dst_stride,
369 const uint8_t *src, const ptrdiff_t src_stride,
370 const int h, const size_t bytes)
371 {
372
2/2
✓ Branch 0 taken 1588448 times.
✓ Branch 1 taken 16546 times.
1604994 for (int y = 0; y < h; y++) {
373 1588448 memcpy(dst, src, bytes);
374 1588448 dst += dst_stride;
375 1588448 src += src_stride;
376 }
377 16546 }
378
379 86470 static void op_pass_run(const SwsFrame *out, const SwsFrame *in, const int y,
380 const int h, const SwsPass *pass)
381 {
382 86470 const SwsOpPass *p = pass->priv;
383 86470 const SwsCompiledOp *comp = &p->comp;
384
385 /* Fill exec metadata for this slice */
386 86470 DECLARE_ALIGNED_32(SwsOpExec, exec) = p->exec_base;
387 86470 exec.slice_y = y;
388 86470 exec.slice_h = h;
389
390 /**
391 * To ensure safety, we need to consider the following:
392 *
393 * 1. We can overread the input, unless this is the last line of an
394 * unpadded buffer. All defined operations can handle arbitrary pixel
395 * input, so overread of arbitrary data is fine. For flipped images,
396 * this condition is actually *inverted* to where the first line is
397 * the one at the end of the buffer.
398 *
399 * 2. We can overwrite the output, as long as we don't write more than the
400 * amount of pixels that fit into one linesize. So we always need to
401 * memcpy the last column on the output side if unpadded.
402 */
403
404
2/2
✓ Branch 0 taken 18948 times.
✓ Branch 1 taken 67522 times.
86470 const int y_in_first = p->offsets_y ? p->offsets_y[y] : y;
405 191888 const int y_in_last = p->offsets_y ? p->offsets_y[y + h - 1] + p->filter_size_v - 1
406
2/2
✓ Branch 0 taken 18948 times.
✓ Branch 1 taken 67522 times.
86470 : y + h - 1;
407
3/4
✓ Branch 0 taken 9352 times.
✓ Branch 1 taken 77118 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9352 times.
163588 const bool memcpy_in = p->memcpy_last && y_in_last == in->height - 1 ||
408
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 77118 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
77118 p->memcpy_first && y_in_first == 0;
409 86470 const bool memcpy_out = p->memcpy_out;
410 86470 const size_t num_blocks = p->num_blocks;
411 86470 const size_t tail_blocks = p->tail_blocks;
412
413 86470 get_row_data(p, y, exec.in, exec.out);
414
3/4
✓ Branch 0 taken 77118 times.
✓ Branch 1 taken 9352 times.
✓ Branch 2 taken 77118 times.
✗ Branch 3 not taken.
86470 if (!memcpy_in && !memcpy_out) {
415 /* Fast path (fully aligned/padded inputs and outputs) */
416 77118 comp->func(&exec, comp->priv, 0, y, num_blocks, y + h);
417 77118 return;
418 }
419
420 /* Non-aligned case (slow path); process main blocks as normal, and
421 * a separate tail (via memcpy into an appropriately padded buffer) */
422
1/2
✓ Branch 0 taken 9352 times.
✗ Branch 1 not taken.
9352 if (num_blocks > tail_blocks) {
423
2/2
✓ Branch 0 taken 37408 times.
✓ Branch 1 taken 9352 times.
46760 for (int i = 0; i < 4; i++) {
424 /* We process fewer blocks, so the in_bump needs to be increased
425 * to reflect that the plane pointers are left on the last block,
426 * not the end of the processed line, after each loop iteration */
427 37408 exec.in_bump[i] += exec.block_size_in[i] * tail_blocks;
428 37408 exec.out_bump[i] += exec.block_size_out[i] * tail_blocks;
429 }
430
431 9352 comp->func(&exec, comp->priv, 0, y, num_blocks - tail_blocks, y + h);
432 }
433
434 9352 DECLARE_ALIGNED_32(SwsOpExec, tail) = p->exec_tail;
435 9352 tail.slice_y = y;
436 9352 tail.slice_h = h;
437
438
2/2
✓ Branch 0 taken 16542 times.
✓ Branch 1 taken 9352 times.
25894 for (int i = 0; i < p->planes_in; i++) {
439 /* Input offsets are relative to the base pointer */
440
3/4
✓ Branch 0 taken 11693 times.
✓ Branch 1 taken 4849 times.
✓ Branch 2 taken 11693 times.
✗ Branch 3 not taken.
16542 if (!exec.in_offset_x || memcpy_in)
441 16542 exec.in[i] += p->tail_off_in;
442 16542 tail.in[i] += (y_in_first >> exec.in_sub_y[i]) * tail.in_stride[i];
443 }
444
2/2
✓ Branch 0 taken 24301 times.
✓ Branch 1 taken 9352 times.
33653 for (int i = 0; i < p->planes_out; i++) {
445 24301 exec.out[i] += p->tail_off_out;
446 24301 tail.out[i] += y * tail.out_stride[i];
447 }
448
449
2/2
✓ Branch 0 taken 16542 times.
✓ Branch 1 taken 9352 times.
25894 for (int i = 0; i < p->planes_in; i++) {
450
1/2
✓ Branch 0 taken 16542 times.
✗ Branch 1 not taken.
16542 if (memcpy_in) {
451 16542 const int lines = get_lines_in(p, y, h, i);
452 16542 copy_lines((uint8_t *) tail.in[i], tail.in_stride[i],
453 16542 exec.in[i], exec.in_stride[i], lines, p->tail_size_in);
454 } else {
455 /* Reuse input pointers directly */
456 const size_t loop_size = tail_blocks * exec.block_size_in[i];
457 tail.in[i] = exec.in[i];
458 tail.in_stride[i] = exec.in_stride[i];
459 tail.in_bump[i] = exec.in_stride[i] - loop_size;
460 }
461 }
462
463
4/4
✓ Branch 0 taken 33648 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 24297 times.
✓ Branch 3 taken 9351 times.
33649 for (int i = 0; !memcpy_out && i < p->planes_out; i++) {
464 /* Reuse output pointers directly */
465 24297 const size_t loop_size = tail_blocks * exec.block_size_out[i];
466 24297 tail.out[i] = exec.out[i];
467 24297 tail.out_stride[i] = exec.out_stride[i];
468 24297 tail.out_bump[i] = exec.out_stride[i] - loop_size;
469 }
470
471 /* Dispatch kernel over tail */
472 av_assert1(tail_blocks > 0);
473 9352 comp->func(&tail, comp->priv, num_blocks - tail_blocks, y, num_blocks, y + h);
474
475
4/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 9351 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 1 times.
9356 for (int i = 0; memcpy_out && i < p->planes_out; i++) {
476 4 const int lines = h >> tail.out_sub_y[i];
477 4 copy_lines(exec.out[i], exec.out_stride[i],
478 4 tail.out[i], tail.out_stride[i], lines, p->tail_size_out);
479 }
480 }
481
482 /* Updates the plane copy (no-op) map for this operation list */
483 85890 static void op_list_get_plane_copy(const SwsOpList *ops, SwsPass *pass)
484 {
485 85890 const SwsOp *write = ff_sws_op_list_output(ops);
486 85890 const SwsOp *read = ff_sws_op_list_input(ops);
487
5/6
✓ Branch 0 taken 85890 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 66685 times.
✓ Branch 3 taken 19205 times.
✓ Branch 4 taken 57745 times.
✓ Branch 5 taken 8940 times.
85890 if (!write || write->rw.mode != SWS_RW_PLANAR ||
488
2/2
✓ Branch 0 taken 39111 times.
✓ Branch 1 taken 18634 times.
57745 !read || read->rw.mode != SWS_RW_PLANAR ||
489
2/2
✓ Branch 0 taken 15156 times.
✓ Branch 1 taken 23955 times.
39111 read->type != write->type ||
490
2/2
✓ Branch 0 taken 392 times.
✓ Branch 1 taken 14764 times.
15156 read->rw.frac != write->rw.frac)
491 71126 return; /* only regular planes can be directly ref'd */
492
493 14764 const SwsOp *prev = &ops->ops[ops->num_ops - 2];
494 14764 int *plane_copy = pass->output->plane_copy;
495
2/2
✓ Branch 0 taken 38644 times.
✓ Branch 1 taken 14764 times.
53408 for (int i = 0; i < write->rw.elems; i++) {
496 38644 SwsCompFlags flags = prev->comps.flags[i];
497
2/2
✓ Branch 0 taken 38509 times.
✓ Branch 1 taken 135 times.
38644 if (!(flags & SWS_COMP_COPY))
498 38509 continue;
499
500 135 const int out_idx = ops->plane_dst[i];
501
3/5
✓ Branch 0 taken 97 times.
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
135 switch (prev->comps.dep_in[i]) {
502 97 case SWS_COMP(0): plane_copy[out_idx] = ops->plane_src[0]; break;
503 19 case SWS_COMP(1): plane_copy[out_idx] = ops->plane_src[1]; break;
504 19 case SWS_COMP(2): plane_copy[out_idx] = ops->plane_src[2]; break;
505 case SWS_COMP(3): plane_copy[out_idx] = ops->plane_src[3]; break;
506 }
507 }
508 }
509
510 162840 static int rw_data_planes(const SwsOp *op)
511 {
512 /* Exclude the palette plane from the plane count, since it does not need
513 * to be directly processed/adjusted by the dispatch layer */
514
1/2
✓ Branch 0 taken 162840 times.
✗ Branch 1 not taken.
162840 return op->rw.mode == SWS_RW_PALETTE ? 1 : ff_sws_rw_op_planes(op);
515 }
516
517 162840 static int rw_pixel_bits(const SwsOp *op)
518 {
519
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 162840 times.
162840 if (op->rw.mode == SWS_RW_PALETTE)
520 return 8; /* index size */
521
522 162840 int elems = 0;
523
2/3
✓ Branch 0 taken 120969 times.
✓ Branch 1 taken 41871 times.
✗ Branch 2 not taken.
162840 switch (op->rw.mode) {
524 120969 case SWS_RW_PLANAR: elems = 1; break;
525 41871 case SWS_RW_PACKED: elems = op->rw.elems; break;
526 }
527
528 162840 const int size = ff_sws_pixel_type_size(op->type);
529 162840 const int bits = 8 >> op->rw.frac;
530 av_assert1(bits >= 1);
531 162840 return elems * size * bits;
532 }
533
534 162840 static void align_pass(SwsPass *pass, int block_size, const int *over_rw,
535 int pixel_bits)
536 {
537
3/4
✓ Branch 0 taken 117880 times.
✓ Branch 1 taken 44960 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 117880 times.
162840 if (!pass || pixel_bits <= 0)
538 44960 return;
539
540 /* Add at least as many pixels as needed to cover the padding requirement */
541 117880 int pad_max = 0;
542
2/2
✓ Branch 0 taken 471520 times.
✓ Branch 1 taken 117880 times.
589400 for (int i = 0; i < 4; i++) {
543 471520 const int pad = (over_rw[i] * 8 + pixel_bits - 1) / pixel_bits;
544 471520 pad_max = FFMAX(pad_max, pad);
545 }
546
547 117880 SwsPassBuffer *buf = pass->output;
548 117880 buf->width_align = FFMAX(buf->width_align, block_size);
549 117880 buf->width_pad = FFMAX(buf->width_pad, pad_max);
550 }
551
552 /* Unchanging part of parameter list */
553 typedef struct CompileArgs {
554 const SwsOpBackend *backend;
555 SwsGraph *graph;
556 int flags;
557 } CompileArgs;
558
559 1476951 static int compile_single(const CompileArgs *args, const SwsOpList *ops,
560 SwsPass *link, SwsPass *input, SwsPass **output)
561 {
562 1476951 SwsGraph *graph = args->graph;
563 1476951 SwsContext *ctx = graph->ctx;
564 1476951 SwsOpPass *p = av_mallocz(sizeof(*p));
565
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1476951 times.
1476951 if (!p)
566 return AVERROR(ENOMEM);
567
568 1476951 int ret = ff_sws_ops_compile(ctx, args->backend, ops, &p->comp);
569
2/2
✓ Branch 0 taken 434862 times.
✓ Branch 1 taken 1042089 times.
1476951 if (ret < 0)
570 434862 goto fail;
571
2/2
✓ Branch 0 taken 956199 times.
✓ Branch 1 taken 85890 times.
1042089 else if (args->flags & SWS_OP_FLAG_DRY_RUN)
572 956199 goto fail; /* nothing to do, just return */
573
574 85890 const SwsCompiledOp *comp = &p->comp;
575 85890 const SwsFormat *src = &ops->src;
576 85890 const SwsFormat *dst = &ops->dst;
577
3/4
✓ Branch 0 taken 8948 times.
✓ Branch 1 taken 76942 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8948 times.
85890 av_assert0(!link || link->format == dst->format);
578
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 85890 times.
85890 if (p->comp.opaque) {
579 SwsCompiledOp c = *comp;
580 av_free(p);
581 ret = ff_sws_graph_add_pass(graph, dst->format, dst->width, dst->height,
582 input, 0, c.slice_align, c.func_opaque,
583 NULL, c.priv, c.free, output);
584 if (ret >= 0) {
585 (*output)->backend = c.backend->flags;
586 op_list_get_plane_copy(ops, *output);
587 ff_sws_pass_link_output(*output, link);
588 }
589 return ret;
590 }
591
592 85890 const AVPixFmtDescriptor *indesc = av_pix_fmt_desc_get(src->format);
593 85890 const AVPixFmtDescriptor *outdesc = av_pix_fmt_desc_get(dst->format);
594 85890 const SwsOp *write = ff_sws_op_list_output(ops);
595 85890 p->planes_out = rw_data_planes(write);
596 85890 p->pixel_bits_out = rw_pixel_bits(write);
597 85890 p->palette_idx = -1;
598 85890 p->exec_base = (SwsOpExec) {
599 85890 .width = dst->width,
600 85890 .height = dst->height,
601 };
602
603 85890 const SwsOp *read = ff_sws_op_list_input(ops);
604
2/2
✓ Branch 0 taken 76950 times.
✓ Branch 1 taken 8940 times.
85890 if (read) {
605 76950 p->planes_in = rw_data_planes(read);
606 76950 p->pixel_bits_in = rw_pixel_bits(read);
607
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 76950 times.
76950 if (read->rw.mode == SWS_RW_PALETTE)
608 p->palette_idx = ops->plane_src[1];
609 }
610
611 85890 const int64_t block_bits_in = (int64_t) comp->block_size * p->pixel_bits_in;
612 85890 const int64_t block_bits_out = (int64_t) comp->block_size * p->pixel_bits_out;
613
2/4
✓ Branch 0 taken 85890 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 85890 times.
85890 if (block_bits_in & 0x7 || block_bits_out & 0x7) {
614 av_log(ctx, AV_LOG_ERROR, "Block size must be byte-aligned.\n");
615 ret = AVERROR(EINVAL);
616 goto fail;
617 }
618
619
2/2
✓ Branch 0 taken 343560 times.
✓ Branch 1 taken 85890 times.
429450 for (int i = 0; i < 4; i++)
620 343560 p->idx_in[i] = p->idx_out[i] = -1;
621
622
2/2
✓ Branch 0 taken 167907 times.
✓ Branch 1 taken 85890 times.
253797 for (int i = 0; i < p->planes_in; i++) {
623 167907 const int idx = ops->plane_src[i];
624
4/4
✓ Branch 0 taken 124875 times.
✓ Branch 1 taken 43032 times.
✓ Branch 2 taken 41975 times.
✓ Branch 3 taken 82900 times.
167907 const int chroma = idx == 1 || idx == 2;
625
2/2
✓ Branch 0 taken 85007 times.
✓ Branch 1 taken 82900 times.
167907 const int sub_x = chroma ? indesc->log2_chroma_w : 0;
626
2/2
✓ Branch 0 taken 85007 times.
✓ Branch 1 taken 82900 times.
167907 const int sub_y = chroma ? indesc->log2_chroma_h : 0;
627 167907 p->exec_base.in_sub_x[i] = sub_x;
628 167907 p->exec_base.in_sub_y[i] = sub_y;
629 167907 p->exec_base.block_size_in[i] = block_bits_in >> 3;
630 167907 p->idx_in[i] = idx;
631 }
632
633
2/2
✓ Branch 0 taken 186923 times.
✓ Branch 1 taken 85890 times.
272813 for (int i = 0; i < p->planes_out; i++) {
634 186923 const int idx = ops->plane_dst[i];
635
4/4
✓ Branch 0 taken 138668 times.
✓ Branch 1 taken 48255 times.
✓ Branch 2 taken 47197 times.
✓ Branch 3 taken 91471 times.
186923 const int chroma = idx == 1 || idx == 2;
636
2/2
✓ Branch 0 taken 95452 times.
✓ Branch 1 taken 91471 times.
186923 const int sub_x = chroma ? outdesc->log2_chroma_w : 0;
637
2/2
✓ Branch 0 taken 95452 times.
✓ Branch 1 taken 91471 times.
186923 const int sub_y = chroma ? outdesc->log2_chroma_h : 0;
638 186923 p->exec_base.out_sub_x[i] = sub_x;
639 186923 p->exec_base.out_sub_y[i] = sub_y;
640 186923 p->exec_base.block_size_out[i] = block_bits_out >> 3;
641 186923 p->idx_out[i] = idx;
642 }
643
644
2/2
✓ Branch 0 taken 76950 times.
✓ Branch 1 taken 8940 times.
85890 const SwsFilterWeights *filter = read ? read->rw.filter.kernel : NULL;
645
4/4
✓ Branch 0 taken 76950 times.
✓ Branch 1 taken 8940 times.
✓ Branch 2 taken 18808 times.
✓ Branch 3 taken 58142 times.
85890 if (read && read->rw.filter.op == SWS_OP_FILTER_V) {
646 18808 p->offsets_y = av_refstruct_ref(filter->offsets);
647 18808 p->filter_size_v = filter->filter_size;
648
649 /* Compute relative pointer bumps for each output line */
650 18808 int32_t *bump = av_malloc_array(filter->dst_size, sizeof(*bump));
651
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18808 times.
18808 if (!bump) {
652 ret = AVERROR(ENOMEM);
653 goto fail;
654 }
655
656 18808 int line = filter->offsets[0];
657
2/2
✓ Branch 0 taken 1782248 times.
✓ Branch 1 taken 18808 times.
1801056 for (int y = 0; y < filter->dst_size - 1; y++) {
658 1782248 int next = filter->offsets[y + 1];
659 1782248 bump[y] = next - line - 1;
660 1782248 line = next;
661 }
662 18808 bump[filter->dst_size - 1] = 0;
663 18808 p->exec_base.in_bump_y = bump;
664
4/4
✓ Branch 0 taken 58142 times.
✓ Branch 1 taken 8940 times.
✓ Branch 2 taken 19126 times.
✓ Branch 3 taken 39016 times.
67082 } else if (read && read->rw.filter.op == SWS_OP_FILTER_H) {
665 /* Compute pixel offset map for each output line */
666 19126 const int pixels = FFALIGN(filter->dst_size, p->comp.block_size);
667 19126 int32_t *offset = av_malloc_array(pixels, sizeof(*offset));
668
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19126 times.
19126 if (!offset) {
669 ret = AVERROR(ENOMEM);
670 goto fail;
671 }
672 19126 p->exec_base.in_offset_x = offset;
673
674
2/2
✓ Branch 0 taken 1841568 times.
✓ Branch 1 taken 19126 times.
1860694 for (int x = 0; x < filter->dst_size; x++) {
675 /* Sanity check; if the tap would land on a half-pixel, we cannot
676 * reasonably expect the implementation to know about this. Just
677 * error out in such (theoretical) cases. */
678 1841568 int64_t bits = (int64_t) filter->offsets[x] * p->pixel_bits_in;
679
2/4
✓ Branch 0 taken 1841568 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1841568 times.
1841568 if ((bits & 0x7) || (bits >> 3) > INT32_MAX) {
680 ret = AVERROR(EINVAL);
681 goto fail;
682 }
683 1841568 offset[x] = bits >> 3;
684 }
685
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19126 times.
19126 for (int x = filter->dst_size; x < pixels; x++)
686 offset[x] = offset[filter->dst_size - 1];
687
2/2
✓ Branch 0 taken 76504 times.
✓ Branch 1 taken 19126 times.
95630 for (int i = 0; i < 4; i++)
688 76504 p->exec_base.block_size_in[i] = 0; /* ptr does not advance */
689 19126 p->filter_size_h = filter->filter_size;
690 }
691
692 85890 ret = ff_sws_graph_add_pass(graph, dst->format, dst->width, dst->height,
693 85890 input, 0, comp->slice_align, op_pass_run,
694 op_pass_setup, p, op_pass_free, output);
695
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 85890 times.
85890 if (ret < 0)
696 return ret;
697
698 85890 (*output)->backend = comp->backend->flags;
699 85890 op_list_get_plane_copy(ops, *output);
700 85890 ff_sws_pass_link_output(*output, link);
701 85890 align_pass(*output, comp->block_size, comp->over_write, p->pixel_bits_out);
702
2/2
✓ Branch 0 taken 76950 times.
✓ Branch 1 taken 8940 times.
85890 if (read)
703 76950 align_pass(input, comp->block_size, comp->over_read, p->pixel_bits_in);
704 85890 return 0;
705
706 1391061 fail:
707 1391061 op_pass_free(p);
708 1391061 return ret;
709 }
710
711 /* Return a mask of all planes matching any flag in `flags` */
712 1530878 static SwsCompMask plane_mask_flags(const SwsOp *op, SwsCompFlags flags)
713 {
714 1530878 SwsCompMask planes = 0;
715
2/2
✓ Branch 0 taken 6123512 times.
✓ Branch 1 taken 1530878 times.
7654390 for (int c = 0; c < 4; c++) {
716
2/2
✓ Branch 0 taken 299734 times.
✓ Branch 1 taken 5823778 times.
6123512 if (op->comps.flags[c] & flags)
717 299734 planes |= SWS_COMP(c);
718 }
719
720 1530878 return planes;
721 }
722
723 /* Takes over ownership of *pops, even on failure */
724 1530878 static int compile_subpass(const CompileArgs *args, SwsOpList **pops,
725 SwsPass *link, SwsPass *input, SwsPass **output)
726 {
727 int ret;
728 1530878 SwsContext *ctx = args->graph->ctx;
729 1530878 SwsOpList *ops = *pops;
730 1530878 SwsOpList *rest = NULL;
731 1530878 SwsPass *tmp = NULL;
732 1530878 *pops = NULL;
733
734
1/2
✓ Branch 0 taken 1530878 times.
✗ Branch 1 not taken.
1530878 if (args->flags & SWS_OP_FLAG_SPLIT_MEMCPY) {
735 /* Split off copied and constant planes into a separate subpass,
736 * since these are likely to be handled by the memcpy backend */
737
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1530878 times.
1530878 av_assert0(ops->num_ops >= 2);
738 1530878 const SwsOp *prev = &ops->ops[ops->num_ops - 2];
739 1530878 SwsCompMask planes = plane_mask_flags(prev, SWS_COMP_COPY | SWS_COMP_CONST);
740
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1530878 times.
1530878 RET(ff_sws_op_list_split_planes(ops, &rest, planes));
741
2/2
✓ Branch 0 taken 53927 times.
✓ Branch 1 taken 1476951 times.
1530878 if (rest) {
742 /* Parallel split: share input and link all outputs together */
743 53927 av_log(ctx, AV_LOG_DEBUG, "Splitting const/memcpy planes: %s\n",
744 53927 ff_sws_comp_mask_str(planes));
745
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 53927 times.
53927 RET(compile_subpass(args, &ops, link, input, &tmp));
746
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 53927 times.
53927 RET(compile_subpass(args, &rest, tmp, input, output));
747 53927 return 0;
748 }
749 }
750
751 1476951 ret = compile_single(args, ops, link, input, output);
752
2/2
✓ Branch 0 taken 1042089 times.
✓ Branch 1 taken 434862 times.
1476951 if (ret != AVERROR(ENOTSUP))
753 1042089 goto fail; /* either success or a hard error */
754
755 /* Find any unresolved filter */
756
2/2
✓ Branch 0 taken 752665 times.
✓ Branch 1 taken 4 times.
752669 for (int idx = 1; idx < ops->num_ops - 1; idx++) {
757 752665 const SwsOp *op = &ops->ops[idx];
758
4/4
✓ Branch 0 taken 551390 times.
✓ Branch 1 taken 201275 times.
✓ Branch 2 taken 233583 times.
✓ Branch 3 taken 317807 times.
752665 if (op->op == SWS_OP_FILTER_H || op->op == SWS_OP_FILTER_V) {
759
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 434858 times.
434858 RET(ff_sws_op_list_split_at(ops, &rest, idx));
760
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 434858 times.
434858 if (ff_sws_op_list_is_noop(ops)) {
761 /* Prevent infinite recursion by avoiding splitting in a way
762 * that does not meaningfully reduce the number of operations
763 * performed in the second part. */
764 FFSWAP(SwsOpList *, ops, rest);
765 break;
766 }
767 /* Serial split: feed first pass into second */
768
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 434858 times.
434858 RET(compile_subpass(args, &ops, NULL, input, &tmp));
769
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 434850 times.
434858 RET(compile_subpass(args, &rest, link, tmp, output));
770 434850 return 0;
771 }
772 }
773
774 /* If we didn't find any more operations to eliminate, then this ops list
775 * is simply unsupported by any of the available backends */
776 4 av_log(ctx, AV_LOG_WARNING, "No backend found for operations:\n");
777 4 ff_sws_op_list_print(ctx, AV_LOG_WARNING, AV_LOG_TRACE, ops);
778 4 ret = AVERROR(ENOTSUP);
779
780 1042101 fail:
781 1042101 ff_sws_op_list_free(&ops);
782 1042101 ff_sws_op_list_free(&rest);
783 1042101 return ret;
784 }
785
786 553969 int ff_sws_compile_pass(SwsGraph *graph, const SwsOpBackend *backend,
787 SwsOpList **pops, int flags, SwsPass *input,
788 SwsPass **output)
789 {
790 553969 const int passes_orig = graph->num_passes;
791 553969 SwsContext *ctx = graph->ctx;
792 553969 SwsOpList *ops = *pops;
793 553969 int ret = 0;
794
795 553969 const SwsOp *write = ff_sws_op_list_output(ops);
796
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 553969 times.
553969 if (!write) {
797 av_log(ctx, AV_LOG_ERROR, "Last operation must be SWS_OP_WRITE.\n");
798 ret = AVERROR(EINVAL);
799 goto out;
800 }
801
802
2/2
✓ Branch 0 taken 44973 times.
✓ Branch 1 taken 508996 times.
553969 if (flags & SWS_OP_FLAG_OPTIMIZE) {
803 44973 ret = ff_sws_op_list_optimize(ops);
804
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44973 times.
44973 if (ret < 0)
805 goto out;
806 44973 av_log(ctx, AV_LOG_DEBUG, "Operation list after optimizing:\n");
807 44973 ff_sws_op_list_print(ctx, AV_LOG_DEBUG, AV_LOG_TRACE, ops);
808 }
809
810 /* Check if the whole operation graph is an end-to-end no-op */
811
2/2
✓ Branch 1 taken 661 times.
✓ Branch 2 taken 553308 times.
553969 if (ff_sws_op_list_is_noop(ops)) {
812
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 640 times.
661 if (output)
813 21 *output = input;
814 661 goto out;
815 }
816
817 553308 const CompileArgs args = {
818 .backend = backend,
819 .graph = graph,
820 .flags = flags,
821 };
822
823 553308 ret = compile_subpass(&args, &ops, NULL, input, output);
824
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 553304 times.
553308 if (ret < 0)
825 4 goto out;
826
827 553304 const int num_passes = graph->num_passes - passes_orig;
828
2/2
✓ Branch 0 taken 526515 times.
✓ Branch 1 taken 26789 times.
553304 if (num_passes > 1)
829 26789 av_log(ctx, AV_LOG_VERBOSE, "Using %d separate passes.\n", num_passes);
830
831 526515 out:
832
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 553965 times.
553969 if (ret < 0)
833 4 ff_sws_graph_rollback(graph, passes_orig);
834 553969 ff_sws_op_list_free(&ops);
835 553969 *pops = NULL;
836 553969 return ret;
837 }
838