FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libswscale/graph.c
Date: 2025-10-10 03:51:19
Exec Total Coverage
Lines: 321 453 70.9%
Functions: 25 29 86.2%
Branches: 148 254 58.3%

Line Branch Exec Source
1 /*
2 * Copyright (C) 2024 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/error.h"
23 #include "libavutil/imgutils.h"
24 #include "libavutil/macros.h"
25 #include "libavutil/mem.h"
26 #include "libavutil/opt.h"
27 #include "libavutil/pixdesc.h"
28 #include "libavutil/slicethread.h"
29
30 #include "libswscale/swscale.h"
31 #include "libswscale/format.h"
32
33 #include "cms.h"
34 #include "lut3d.h"
35 #include "swscale_internal.h"
36 #include "graph.h"
37 #include "ops.h"
38
39 6336 static int pass_alloc_output(SwsPass *pass)
40 {
41
3/4
✓ Branch 0 taken 59 times.
✓ Branch 1 taken 6277 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 59 times.
6336 if (!pass || pass->output.fmt != AV_PIX_FMT_NONE)
42 6277 return 0;
43 59 pass->output.fmt = pass->format;
44 59 return av_image_alloc(pass->output.data, pass->output.linesize, pass->width,
45 59 pass->num_slices * pass->slice_h, pass->format, 64);
46 }
47
48 6336 SwsPass *ff_sws_graph_add_pass(SwsGraph *graph, enum AVPixelFormat fmt,
49 int width, int height, SwsPass *input,
50 int align, void *priv, sws_filter_run_t run)
51 {
52 int ret;
53 6336 SwsPass *pass = av_mallocz(sizeof(*pass));
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6336 times.
6336 if (!pass)
55 return NULL;
56
57 6336 pass->graph = graph;
58 6336 pass->run = run;
59 6336 pass->priv = priv;
60 6336 pass->format = fmt;
61 6336 pass->width = width;
62 6336 pass->height = height;
63 6336 pass->input = input;
64 6336 pass->output.fmt = AV_PIX_FMT_NONE;
65
66 6336 ret = pass_alloc_output(input);
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6336 times.
6336 if (ret < 0) {
68 av_free(pass);
69 return NULL;
70 }
71
72
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 6325 times.
6336 if (!align) {
73 11 pass->slice_h = pass->height;
74 11 pass->num_slices = 1;
75 } else {
76 6325 pass->slice_h = (pass->height + graph->num_threads - 1) / graph->num_threads;
77 6325 pass->slice_h = FFALIGN(pass->slice_h, align);
78 6325 pass->num_slices = (pass->height + pass->slice_h - 1) / pass->slice_h;
79 }
80
81 6336 ret = av_dynarray_add_nofree(&graph->passes, &graph->num_passes, pass);
82
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6336 times.
6336 if (ret < 0)
83 av_freep(&pass);
84 6336 return pass;
85 }
86
87 /* Wrapper around ff_sws_graph_add_pass() that chains a pass "in-place" */
88 58 static int pass_append(SwsGraph *graph, enum AVPixelFormat fmt, int w, int h,
89 SwsPass **pass, int align, void *priv, sws_filter_run_t run)
90 {
91 58 SwsPass *new = ff_sws_graph_add_pass(graph, fmt, w, h, *pass, align, priv, run);
92
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (!new)
93 return AVERROR(ENOMEM);
94 58 *pass = new;
95 58 return 0;
96 }
97
98 static void run_copy(const SwsImg *out_base, const SwsImg *in_base,
99 int y, int h, const SwsPass *pass)
100 {
101 SwsImg in = ff_sws_img_shift(in_base, y);
102 SwsImg out = ff_sws_img_shift(out_base, y);
103
104 for (int i = 0; i < FF_ARRAY_ELEMS(out.data) && out.data[i]; i++) {
105 const int lines = h >> ff_fmt_vshift(in.fmt, i);
106 av_assert1(in.data[i]);
107
108 if (in.linesize[i] == out.linesize[i]) {
109 memcpy(out.data[i], in.data[i], lines * out.linesize[i]);
110 } else {
111 const int linesize = FFMIN(out.linesize[i], in.linesize[i]);
112 for (int j = 0; j < lines; j++) {
113 memcpy(out.data[i], in.data[i], linesize);
114 in.data[i] += in.linesize[i];
115 out.data[i] += out.linesize[i];
116 }
117 }
118 }
119 }
120
121 54 static void run_rgb0(const SwsImg *out, const SwsImg *in, int y, int h,
122 const SwsPass *pass)
123 {
124 54 SwsInternal *c = pass->priv;
125 54 const int x0 = c->src0Alpha - 1;
126 54 const int w4 = 4 * pass->width;
127 54 const int src_stride = in->linesize[0];
128 54 const int dst_stride = out->linesize[0];
129 54 const uint8_t *src = in->data[0] + y * src_stride;
130 54 uint8_t *dst = out->data[0] + y * dst_stride;
131
132
2/2
✓ Branch 0 taken 4662 times.
✓ Branch 1 taken 54 times.
4716 for (int y = 0; y < h; y++) {
133 4662 memcpy(dst, src, w4 * sizeof(*dst));
134
2/2
✓ Branch 0 taken 2869202 times.
✓ Branch 1 taken 4662 times.
2873864 for (int x = x0; x < w4; x += 4)
135 2869202 dst[x] = 0xFF;
136
137 4662 src += src_stride;
138 4662 dst += dst_stride;
139 }
140 54 }
141
142 2579 static void run_xyz2rgb(const SwsImg *out, const SwsImg *in, int y, int h,
143 const SwsPass *pass)
144 {
145 2579 ff_xyz12Torgb48(pass->priv, out->data[0] + y * out->linesize[0], out->linesize[0],
146 2579 in->data[0] + y * in->linesize[0], in->linesize[0],
147 2579 pass->width, h);
148 2579 }
149
150 2644 static void run_rgb2xyz(const SwsImg *out, const SwsImg *in, int y, int h,
151 const SwsPass *pass)
152 {
153 2644 ff_rgb48Toxyz12(pass->priv, out->data[0] + y * out->linesize[0], out->linesize[0],
154 2644 in->data[0] + y * in->linesize[0], in->linesize[0],
155 2644 pass->width, h);
156 2644 }
157
158 /***********************************************************************
159 * Internal ff_swscale() wrapper. This reuses the legacy scaling API. *
160 * This is considered fully deprecated, and will be replaced by a full *
161 * reimplementation ASAP. *
162 ***********************************************************************/
163
164 6278 static void free_legacy_swscale(void *priv)
165 {
166 6278 SwsContext *sws = priv;
167 6278 sws_free_context(&sws);
168 6278 }
169
170 103081 static void setup_legacy_swscale(const SwsImg *out, const SwsImg *in,
171 const SwsPass *pass)
172 {
173 103081 const SwsGraph *graph = pass->graph;
174 103081 const SwsImg *in_orig = &graph->exec.input;
175 103081 SwsContext *sws = pass->priv;
176 103081 SwsInternal *c = sws_internal(sws);
177
5/6
✓ Branch 0 taken 95837 times.
✓ Branch 1 taken 7244 times.
✓ Branch 2 taken 875 times.
✓ Branch 3 taken 94962 times.
✓ Branch 4 taken 875 times.
✗ Branch 5 not taken.
103081 if (sws->flags & SWS_BITEXACT && sws->dither == SWS_DITHER_ED && c->dither_error[0]) {
178
2/2
✓ Branch 0 taken 3500 times.
✓ Branch 1 taken 875 times.
4375 for (int i = 0; i < 4; i++)
179 3500 memset(c->dither_error[i], 0, sizeof(c->dither_error[0][0]) * (sws->dst_w + 2));
180 }
181
182
2/2
✓ Branch 1 taken 4721 times.
✓ Branch 2 taken 98360 times.
103081 if (usePal(sws->src_format))
183 4721 ff_update_palette(c, (const uint32_t *) in_orig->data[1]);
184 103081 }
185
186 624778 static inline SwsContext *slice_ctx(const SwsPass *pass, int y)
187 {
188 624778 SwsContext *sws = pass->priv;
189 624778 SwsInternal *parent = sws_internal(sws);
190
2/2
✓ Branch 0 taken 37733 times.
✓ Branch 1 taken 587045 times.
624778 if (pass->num_slices == 1)
191 37733 return sws;
192
193 av_assert1(parent->nb_slice_ctx == pass->num_slices);
194 587045 sws = parent->slice_ctx[y / pass->slice_h];
195
196
2/2
✓ Branch 1 taken 35135 times.
✓ Branch 2 taken 551910 times.
587045 if (usePal(sws->src_format)) {
197 35135 SwsInternal *sub = sws_internal(sws);
198 35135 memcpy(sub->pal_yuv, parent->pal_yuv, sizeof(sub->pal_yuv));
199 35135 memcpy(sub->pal_rgb, parent->pal_rgb, sizeof(sub->pal_rgb));
200 }
201
202 587045 return sws;
203 }
204
205 88093 static void run_legacy_unscaled(const SwsImg *out, const SwsImg *in_base,
206 int y, int h, const SwsPass *pass)
207 {
208 88093 SwsContext *sws = slice_ctx(pass, y);
209 88093 SwsInternal *c = sws_internal(sws);
210 88093 const SwsImg in = ff_sws_img_shift(in_base, y);
211
212 88093 c->convert_unscaled(c, (const uint8_t *const *) in.data, in.linesize, y, h,
213 88093 out->data, out->linesize);
214 88093 }
215
216 536685 static void run_legacy_swscale(const SwsImg *out_base, const SwsImg *in,
217 int y, int h, const SwsPass *pass)
218 {
219 536685 SwsContext *sws = slice_ctx(pass, y);
220 536685 SwsInternal *c = sws_internal(sws);
221 536685 const SwsImg out = ff_sws_img_shift(out_base, y);
222
223 536685 ff_swscale(c, (const uint8_t *const *) in->data, in->linesize, 0,
224 sws->src_h, out.data, out.linesize, y, h);
225 536685 }
226
227 12554 static void get_chroma_pos(SwsGraph *graph, int *h_chr_pos, int *v_chr_pos,
228 const SwsFormat *fmt)
229 {
230 12554 enum AVChromaLocation chroma_loc = fmt->loc;
231 12554 const int sub_x = fmt->desc->log2_chroma_w;
232 12554 const int sub_y = fmt->desc->log2_chroma_h;
233 int x_pos, y_pos;
234
235 /* Explicitly default to center siting for compatibility with swscale */
236
2/2
✓ Branch 0 taken 12553 times.
✓ Branch 1 taken 1 times.
12554 if (chroma_loc == AVCHROMA_LOC_UNSPECIFIED) {
237 12553 chroma_loc = AVCHROMA_LOC_CENTER;
238
4/4
✓ Branch 0 taken 6489 times.
✓ Branch 1 taken 6064 times.
✓ Branch 2 taken 240 times.
✓ Branch 3 taken 6249 times.
12553 graph->incomplete |= sub_x || sub_y;
239 }
240
241 /* av_chroma_location_enum_to_pos() always gives us values in the range from
242 * 0 to 256, but we need to adjust this to the true value range of the
243 * subsampling grid, which may be larger for h/v_sub > 1 */
244 12554 av_chroma_location_enum_to_pos(&x_pos, &y_pos, chroma_loc);
245 12554 x_pos *= (1 << sub_x) - 1;
246 12554 y_pos *= (1 << sub_y) - 1;
247
248 /* Fix vertical chroma position for interlaced frames */
249
3/4
✓ Branch 0 taken 4886 times.
✓ Branch 1 taken 7668 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4886 times.
12554 if (sub_y && fmt->interlaced) {
250 /* When vertically subsampling, chroma samples are effectively only
251 * placed next to even rows. To access them from the odd field, we need
252 * to account for this shift by offsetting the distance of one luma row.
253 *
254 * For 4x vertical subsampling (v_sub == 2), they are only placed
255 * next to every *other* even row, so we need to shift by three luma
256 * rows to get to the chroma sample. */
257 if (graph->field == FIELD_BOTTOM)
258 y_pos += (256 << sub_y) - 256;
259
260 /* Luma row distance is doubled for fields, so halve offsets */
261 y_pos >>= 1;
262 }
263
264 /* Explicitly strip chroma offsets when not subsampling, because it
265 * interferes with the operation of flags like SWS_FULL_CHR_H_INP */
266
2/2
✓ Branch 0 taken 6065 times.
✓ Branch 1 taken 6489 times.
12554 *h_chr_pos = sub_x ? x_pos : -513;
267
2/2
✓ Branch 0 taken 4886 times.
✓ Branch 1 taken 7668 times.
12554 *v_chr_pos = sub_y ? y_pos : -513;
268 12554 }
269
270 25108 static void legacy_chr_pos(SwsGraph *graph, int *chr_pos, int override, int *warned)
271 {
272
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 25108 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
25108 if (override == -513 || override == *chr_pos)
273 25108 return;
274
275 if (!*warned) {
276 av_log(NULL, AV_LOG_WARNING,
277 "Setting chroma position directly is deprecated, make sure "
278 "the frame is tagged with the correct chroma location.\n");
279 *warned = 1;
280 }
281
282 *chr_pos = override;
283 }
284
285 /* Takes over ownership of `sws` */
286 6279 static int init_legacy_subpass(SwsGraph *graph, SwsContext *sws,
287 SwsPass *input, SwsPass **output)
288 {
289 6279 SwsInternal *c = sws_internal(sws);
290 6279 const int src_w = sws->src_w, src_h = sws->src_h;
291 6279 const int dst_w = sws->dst_w, dst_h = sws->dst_h;
292
4/4
✓ Branch 0 taken 5825 times.
✓ Branch 1 taken 454 times.
✓ Branch 2 taken 5815 times.
✓ Branch 3 taken 10 times.
6279 const int unscaled = src_w == dst_w && src_h == dst_h;
293 6279 int align = c->dst_slice_align;
294 6279 SwsPass *pass = NULL;
295 int ret;
296
297
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6278 times.
6279 if (c->cascaded_context[0]) {
298
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 const int num_cascaded = c->cascaded_context[2] ? 3 : 2;
299
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (int i = 0; i < num_cascaded; i++) {
300 2 const int is_last = i + 1 == num_cascaded;
301
302 /* Steal cascaded context, so we can manage its lifetime independently */
303 2 SwsContext *sub = c->cascaded_context[i];
304 2 c->cascaded_context[i] = NULL;
305
306
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 ret = init_legacy_subpass(graph, sub, input, is_last ? output : &input);
307
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
308 break;
309 }
310
311 1 sws_free_context(&sws);
312 1 return ret;
313 }
314
315
3/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 6267 times.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
6278 if (sws->dither == SWS_DITHER_ED && !c->convert_unscaled)
316 11 align = 0; /* disable slice threading */
317
318
6/6
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 6253 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 4 times.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 15 times.
6278 if (c->src0Alpha && !c->dst0Alpha && isALPHA(sws->dst_format)) {
319 6 ret = pass_append(graph, AV_PIX_FMT_RGBA, src_w, src_h, &input, 1, c, run_rgb0);
320
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0) {
321 sws_free_context(&sws);
322 return ret;
323 }
324 }
325
326
5/6
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 6264 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
6278 if (c->srcXYZ && !(c->dstXYZ && unscaled)) {
327 14 ret = pass_append(graph, AV_PIX_FMT_RGB48, src_w, src_h, &input, 1, c, run_xyz2rgb);
328
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (ret < 0) {
329 sws_free_context(&sws);
330 return ret;
331 }
332 }
333
334 6278 pass = ff_sws_graph_add_pass(graph, sws->dst_format, dst_w, dst_h, input, align, sws,
335
2/2
✓ Branch 0 taken 647 times.
✓ Branch 1 taken 5631 times.
6278 c->convert_unscaled ? run_legacy_unscaled : run_legacy_swscale);
336
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6278 times.
6278 if (!pass) {
337 sws_free_context(&sws);
338 return AVERROR(ENOMEM);
339 }
340 6278 pass->setup = setup_legacy_swscale;
341 6278 pass->free = free_legacy_swscale;
342
343 /**
344 * For slice threading, we need to create sub contexts, similar to how
345 * swscale normally handles it internally. The most important difference
346 * is that we handle cascaded contexts before threaded contexts; whereas
347 * context_init_threaded() does it the other way around.
348 */
349
350
2/2
✓ Branch 0 taken 2486 times.
✓ Branch 1 taken 3792 times.
6278 if (pass->num_slices > 1) {
351 2486 c->slice_ctx = av_calloc(pass->num_slices, sizeof(*c->slice_ctx));
352
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2486 times.
2486 if (!c->slice_ctx)
353 return AVERROR(ENOMEM);
354
355
2/2
✓ Branch 0 taken 22281 times.
✓ Branch 1 taken 2486 times.
24767 for (int i = 0; i < pass->num_slices; i++) {
356 SwsContext *slice;
357 SwsInternal *c2;
358 22281 slice = c->slice_ctx[i] = sws_alloc_context();
359
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22281 times.
22281 if (!slice)
360 return AVERROR(ENOMEM);
361 22281 c->nb_slice_ctx++;
362
363 22281 c2 = sws_internal(slice);
364 22281 c2->parent = sws;
365
366 22281 ret = av_opt_copy(slice, sws);
367
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22281 times.
22281 if (ret < 0)
368 return ret;
369
370 22281 ret = ff_sws_init_single_context(slice, NULL, NULL);
371
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22281 times.
22281 if (ret < 0)
372 return ret;
373
374 22281 sws_setColorspaceDetails(slice, c->srcColorspaceTable,
375 22281 slice->src_range, c->dstColorspaceTable,
376 slice->dst_range, c->brightness, c->contrast,
377 c->saturation);
378
379
2/2
✓ Branch 0 taken 89124 times.
✓ Branch 1 taken 22281 times.
111405 for (int i = 0; i < FF_ARRAY_ELEMS(c->srcColorspaceTable); i++) {
380 89124 c2->srcColorspaceTable[i] = c->srcColorspaceTable[i];
381 89124 c2->dstColorspaceTable[i] = c->dstColorspaceTable[i];
382 }
383 }
384 }
385
386
5/6
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 6240 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 36 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
6278 if (c->dstXYZ && !(c->srcXYZ && unscaled)) {
387 38 ret = pass_append(graph, AV_PIX_FMT_RGB48, dst_w, dst_h, &pass, 1, c, run_rgb2xyz);
388
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 if (ret < 0)
389 return ret;
390 }
391
392 6278 *output = pass;
393 6278 return 0;
394 }
395
396 6277 static int add_legacy_sws_pass(SwsGraph *graph, SwsFormat src, SwsFormat dst,
397 SwsPass *input, SwsPass **output)
398 {
399 6277 int ret, warned = 0;
400 6277 SwsContext *const ctx = graph->ctx;
401 6277 SwsContext *sws = sws_alloc_context();
402
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6277 times.
6277 if (!sws)
403 return AVERROR(ENOMEM);
404
405 6277 sws->flags = ctx->flags;
406 6277 sws->dither = ctx->dither;
407 6277 sws->alpha_blend = ctx->alpha_blend;
408 6277 sws->gamma_flag = ctx->gamma_flag;
409
410 6277 sws->src_w = src.width;
411 6277 sws->src_h = src.height;
412 6277 sws->src_format = src.format;
413 6277 sws->src_range = src.range == AVCOL_RANGE_JPEG;
414
415 6277 sws->dst_w = dst.width;
416 6277 sws->dst_h = dst.height;
417 6277 sws->dst_format = dst.format;
418 6277 sws->dst_range = dst.range == AVCOL_RANGE_JPEG;
419 6277 get_chroma_pos(graph, &sws->src_h_chr_pos, &sws->src_v_chr_pos, &src);
420 6277 get_chroma_pos(graph, &sws->dst_h_chr_pos, &sws->dst_v_chr_pos, &dst);
421
422 6277 graph->incomplete |= src.range == AVCOL_RANGE_UNSPECIFIED;
423 6277 graph->incomplete |= dst.range == AVCOL_RANGE_UNSPECIFIED;
424
425 /* Allow overriding chroma position with the legacy API */
426 6277 legacy_chr_pos(graph, &sws->src_h_chr_pos, ctx->src_h_chr_pos, &warned);
427 6277 legacy_chr_pos(graph, &sws->src_v_chr_pos, ctx->src_v_chr_pos, &warned);
428 6277 legacy_chr_pos(graph, &sws->dst_h_chr_pos, ctx->dst_h_chr_pos, &warned);
429 6277 legacy_chr_pos(graph, &sws->dst_v_chr_pos, ctx->dst_v_chr_pos, &warned);
430
431 6277 sws->scaler_params[0] = ctx->scaler_params[0];
432 6277 sws->scaler_params[1] = ctx->scaler_params[1];
433
434 6277 ret = sws_init_context(sws, NULL, NULL);
435
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6277 times.
6277 if (ret < 0) {
436 sws_free_context(&sws);
437 return ret;
438 }
439
440 /* Set correct color matrices */
441 {
442 int in_full, out_full, brightness, contrast, saturation;
443 const int *inv_table, *table;
444 6277 sws_getColorspaceDetails(sws, (int **)&inv_table, &in_full,
445 (int **)&table, &out_full,
446 &brightness, &contrast, &saturation);
447
448 6277 inv_table = sws_getCoefficients(src.csp);
449 6277 table = sws_getCoefficients(dst.csp);
450
451
2/2
✓ Branch 0 taken 2165 times.
✓ Branch 1 taken 4112 times.
8442 graph->incomplete |= src.csp != dst.csp &&
452
2/2
✓ Branch 0 taken 629 times.
✓ Branch 1 taken 1536 times.
2165 (src.csp == AVCOL_SPC_UNSPECIFIED ||
453
2/2
✓ Branch 0 taken 619 times.
✓ Branch 1 taken 10 times.
629 dst.csp == AVCOL_SPC_UNSPECIFIED);
454
455 6277 sws_setColorspaceDetails(sws, inv_table, in_full, table, out_full,
456 brightness, contrast, saturation);
457 }
458
459 6277 return init_legacy_subpass(graph, sws, input, output);
460 }
461
462 /*********************
463 * Format conversion *
464 *********************/
465
466 #if CONFIG_UNSTABLE
467 6277 static int add_convert_pass(SwsGraph *graph, SwsFormat src, SwsFormat dst,
468 SwsPass *input, SwsPass **output)
469 {
470 6277 const SwsPixelType type = SWS_PIXEL_F32;
471
472 6277 SwsContext *ctx = graph->ctx;
473 6277 SwsOpList *ops = NULL;
474 6277 int ret = AVERROR(ENOTSUP);
475
476 /* Mark the entire new ops infrastructure as experimental for now */
477
1/2
✓ Branch 0 taken 6277 times.
✗ Branch 1 not taken.
6277 if (!(ctx->flags & SWS_UNSTABLE))
478 6277 goto fail;
479
480 /* The new format conversion layer cannot scale for now */
481 if (src.width != dst.width || src.height != dst.height ||
482 src.desc->log2_chroma_h || src.desc->log2_chroma_w ||
483 dst.desc->log2_chroma_h || dst.desc->log2_chroma_w)
484 goto fail;
485
486 /* The new code does not yet support alpha blending */
487 if (src.desc->flags & AV_PIX_FMT_FLAG_ALPHA &&
488 ctx->alpha_blend != SWS_ALPHA_BLEND_NONE)
489 goto fail;
490
491 ops = ff_sws_op_list_alloc();
492 if (!ops)
493 return AVERROR(ENOMEM);
494 ops->src = src;
495 ops->dst = dst;
496
497 ret = ff_sws_decode_pixfmt(ops, src.format);
498 if (ret < 0)
499 goto fail;
500 ret = ff_sws_decode_colors(ctx, type, ops, src, &graph->incomplete);
501 if (ret < 0)
502 goto fail;
503 ret = ff_sws_encode_colors(ctx, type, ops, dst, &graph->incomplete);
504 if (ret < 0)
505 goto fail;
506 ret = ff_sws_encode_pixfmt(ops, dst.format);
507 if (ret < 0)
508 goto fail;
509
510 av_log(ctx, AV_LOG_VERBOSE, "Conversion pass for %s -> %s:\n",
511 av_get_pix_fmt_name(src.format), av_get_pix_fmt_name(dst.format));
512
513 av_log(ctx, AV_LOG_DEBUG, "Unoptimized operation list:\n");
514 ff_sws_op_list_print(ctx, AV_LOG_DEBUG, ops);
515 av_log(ctx, AV_LOG_DEBUG, "Optimized operation list:\n");
516
517 ff_sws_op_list_optimize(ops);
518 if (ops->num_ops == 0) {
519 av_log(ctx, AV_LOG_VERBOSE, " optimized into memcpy\n");
520 ff_sws_op_list_free(&ops);
521 *output = input;
522 return 0;
523 }
524
525 ff_sws_op_list_print(ctx, AV_LOG_VERBOSE, ops);
526
527 ret = ff_sws_compile_pass(graph, ops, 0, dst, input, output);
528 if (ret < 0)
529 goto fail;
530
531 ret = 0;
532 /* fall through */
533
534 6277 fail:
535 6277 ff_sws_op_list_free(&ops);
536
1/2
✓ Branch 0 taken 6277 times.
✗ Branch 1 not taken.
6277 if (ret == AVERROR(ENOTSUP))
537 6277 return add_legacy_sws_pass(graph, src, dst, input, output);
538 return ret;
539 }
540 #else
541 #define add_convert_pass add_legacy_sws_pass
542 #endif
543
544
545 /**************************
546 * Gamut and tone mapping *
547 **************************/
548
549 static void free_lut3d(void *priv)
550 {
551 SwsLut3D *lut = priv;
552 ff_sws_lut3d_free(&lut);
553 }
554
555 static void setup_lut3d(const SwsImg *out, const SwsImg *in, const SwsPass *pass)
556 {
557 SwsLut3D *lut = pass->priv;
558
559 /* Update dynamic frame metadata from the original source frame */
560 ff_sws_lut3d_update(lut, &pass->graph->src.color);
561 }
562
563 static void run_lut3d(const SwsImg *out_base, const SwsImg *in_base,
564 int y, int h, const SwsPass *pass)
565 {
566 SwsLut3D *lut = pass->priv;
567 const SwsImg in = ff_sws_img_shift(in_base, y);
568 const SwsImg out = ff_sws_img_shift(out_base, y);
569
570 ff_sws_lut3d_apply(lut, in.data[0], in.linesize[0], out.data[0],
571 out.linesize[0], pass->width, h);
572 }
573
574 6277 static int adapt_colors(SwsGraph *graph, SwsFormat src, SwsFormat dst,
575 SwsPass *input, SwsPass **output)
576 {
577 enum AVPixelFormat fmt_in, fmt_out;
578 6277 SwsColorMap map = {0};
579 SwsLut3D *lut;
580 SwsPass *pass;
581 int ret;
582
583 /**
584 * Grayspace does not really have primaries, so just force the use of
585 * the equivalent other primary set to avoid a conversion. Technically,
586 * this does affect the weights used for the Grayscale conversion, but
587 * in practise, that should give the expected results more often than not.
588 */
589
2/2
✓ Branch 1 taken 342 times.
✓ Branch 2 taken 5935 times.
6277 if (isGray(dst.format)) {
590 342 dst.color = src.color;
591
2/2
✓ Branch 1 taken 95 times.
✓ Branch 2 taken 5840 times.
5935 } else if (isGray(src.format)) {
592 95 src.color = dst.color;
593 }
594
595 /* Fully infer color spaces before color mapping logic */
596 6277 graph->incomplete |= ff_infer_colors(&src.color, &dst.color);
597
598 6277 map.intent = graph->ctx->intent;
599 6277 map.src = src.color;
600 6277 map.dst = dst.color;
601
602
1/2
✓ Branch 1 taken 6277 times.
✗ Branch 2 not taken.
6277 if (ff_sws_color_map_noop(&map))
603 6277 return 0;
604
605 lut = ff_sws_lut3d_alloc();
606 if (!lut)
607 return AVERROR(ENOMEM);
608
609 fmt_in = ff_sws_lut3d_pick_pixfmt(src, 0);
610 fmt_out = ff_sws_lut3d_pick_pixfmt(dst, 1);
611 if (fmt_in != src.format) {
612 SwsFormat tmp = src;
613 tmp.format = fmt_in;
614 ret = add_convert_pass(graph, src, tmp, input, &input);
615 if (ret < 0)
616 return ret;
617 }
618
619 ret = ff_sws_lut3d_generate(lut, fmt_in, fmt_out, &map);
620 if (ret < 0) {
621 ff_sws_lut3d_free(&lut);
622 return ret;
623 }
624
625 pass = ff_sws_graph_add_pass(graph, fmt_out, src.width, src.height,
626 input, 1, lut, run_lut3d);
627 if (!pass) {
628 ff_sws_lut3d_free(&lut);
629 return AVERROR(ENOMEM);
630 }
631 pass->setup = setup_lut3d;
632 pass->free = free_lut3d;
633
634 *output = pass;
635 return 0;
636 }
637
638 /***************************************
639 * Main filter graph construction code *
640 ***************************************/
641
642 6277 static int init_passes(SwsGraph *graph)
643 {
644 6277 SwsFormat src = graph->src;
645 6277 SwsFormat dst = graph->dst;
646 6277 SwsPass *pass = NULL; /* read from main input image */
647 int ret;
648
649 6277 ret = adapt_colors(graph, src, dst, pass, &pass);
650
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6277 times.
6277 if (ret < 0)
651 return ret;
652
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6277 times.
6277 src.format = pass ? pass->format : src.format;
653 6277 src.color = dst.color;
654
655
1/2
✓ Branch 1 taken 6277 times.
✗ Branch 2 not taken.
6277 if (!ff_fmt_equal(&src, &dst)) {
656 6277 ret = add_convert_pass(graph, src, dst, pass, &pass);
657
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6277 times.
6277 if (ret < 0)
658 return ret;
659 }
660
661
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6277 times.
6277 if (!pass) {
662 /* No passes were added, so no operations were necessary */
663 graph->noop = 1;
664
665 /* Add threaded memcpy pass */
666 pass = ff_sws_graph_add_pass(graph, dst.format, dst.width, dst.height,
667 pass, 1, NULL, run_copy);
668 if (!pass)
669 return AVERROR(ENOMEM);
670 }
671
672 6277 return 0;
673 }
674
675 630055 static void sws_graph_worker(void *priv, int jobnr, int threadnr, int nb_jobs,
676 int nb_threads)
677 {
678 630055 SwsGraph *graph = priv;
679 630055 const SwsPass *pass = graph->exec.pass;
680
2/2
✓ Branch 0 taken 5295 times.
✓ Branch 1 taken 624760 times.
630055 const SwsImg *input = pass->input ? &pass->input->output : &graph->exec.input;
681
2/2
✓ Branch 0 taken 5295 times.
✓ Branch 1 taken 624760 times.
630055 const SwsImg *output = pass->output.fmt != AV_PIX_FMT_NONE ? &pass->output : &graph->exec.output;
682 630055 const int slice_y = jobnr * pass->slice_h;
683 630055 const int slice_h = FFMIN(pass->slice_h, pass->height - slice_y);
684
685 630055 pass->run(output, input, slice_y, slice_h, pass);
686 630055 }
687
688 6277 int ff_sws_graph_create(SwsContext *ctx, const SwsFormat *dst, const SwsFormat *src,
689 int field, SwsGraph **out_graph)
690 {
691 int ret;
692 6277 SwsGraph *graph = av_mallocz(sizeof(*graph));
693
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6277 times.
6277 if (!graph)
694 return AVERROR(ENOMEM);
695
696 6277 graph->ctx = ctx;
697 6277 graph->src = *src;
698 6277 graph->dst = *dst;
699 6277 graph->field = field;
700 6277 graph->opts_copy = *ctx;
701
702 6277 graph->exec.input.fmt = src->format;
703 6277 graph->exec.output.fmt = dst->format;
704
705 6277 ret = avpriv_slicethread_create(&graph->slicethread, (void *) graph,
706 sws_graph_worker, NULL, ctx->threads);
707
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6277 times.
6277 if (ret == AVERROR(ENOSYS))
708 graph->num_threads = 1;
709
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6277 times.
6277 else if (ret < 0)
710 goto error;
711 else
712 6277 graph->num_threads = ret;
713
714 6277 ret = init_passes(graph);
715
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6277 times.
6277 if (ret < 0)
716 goto error;
717
718 6277 *out_graph = graph;
719 6277 return 0;
720
721 error:
722 ff_sws_graph_free(&graph);
723 return ret;
724 }
725
726 208594 void ff_sws_graph_free(SwsGraph **pgraph)
727 {
728 208594 SwsGraph *graph = *pgraph;
729
2/2
✓ Branch 0 taken 202317 times.
✓ Branch 1 taken 6277 times.
208594 if (!graph)
730 202317 return;
731
732 6277 avpriv_slicethread_free(&graph->slicethread);
733
734
2/2
✓ Branch 0 taken 6336 times.
✓ Branch 1 taken 6277 times.
12613 for (int i = 0; i < graph->num_passes; i++) {
735 6336 SwsPass *pass = graph->passes[i];
736
2/2
✓ Branch 0 taken 6278 times.
✓ Branch 1 taken 58 times.
6336 if (pass->free)
737 6278 pass->free(pass->priv);
738
2/2
✓ Branch 0 taken 59 times.
✓ Branch 1 taken 6277 times.
6336 if (pass->output.fmt != AV_PIX_FMT_NONE)
739 59 av_free(pass->output.data[0]);
740 6336 av_free(pass);
741 }
742 6277 av_free(graph->passes);
743
744 6277 av_free(graph);
745 6277 *pgraph = NULL;
746 }
747
748 /* Tests only options relevant to SwsGraph */
749 96802 static int opts_equal(const SwsContext *c1, const SwsContext *c2)
750 {
751 193604 return c1->flags == c2->flags &&
752
1/2
✓ Branch 0 taken 96802 times.
✗ Branch 1 not taken.
96802 c1->threads == c2->threads &&
753
1/2
✓ Branch 0 taken 96802 times.
✗ Branch 1 not taken.
96802 c1->dither == c2->dither &&
754
1/2
✓ Branch 0 taken 96802 times.
✗ Branch 1 not taken.
96802 c1->alpha_blend == c2->alpha_blend &&
755
1/2
✓ Branch 0 taken 96802 times.
✗ Branch 1 not taken.
96802 c1->gamma_flag == c2->gamma_flag &&
756
1/2
✓ Branch 0 taken 96802 times.
✗ Branch 1 not taken.
96802 c1->src_h_chr_pos == c2->src_h_chr_pos &&
757
1/2
✓ Branch 0 taken 96802 times.
✗ Branch 1 not taken.
96802 c1->src_v_chr_pos == c2->src_v_chr_pos &&
758
1/2
✓ Branch 0 taken 96802 times.
✗ Branch 1 not taken.
96802 c1->dst_h_chr_pos == c2->dst_h_chr_pos &&
759
1/2
✓ Branch 0 taken 96802 times.
✗ Branch 1 not taken.
96802 c1->dst_v_chr_pos == c2->dst_v_chr_pos &&
760
2/4
✓ Branch 0 taken 96802 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 96802 times.
✗ Branch 3 not taken.
290406 c1->intent == c2->intent &&
761
1/2
✓ Branch 0 taken 96802 times.
✗ Branch 1 not taken.
96802 !memcmp(c1->scaler_params, c2->scaler_params, sizeof(c1->scaler_params));
762
763 }
764
765 103079 int ff_sws_graph_reinit(SwsContext *ctx, const SwsFormat *dst, const SwsFormat *src,
766 int field, SwsGraph **out_graph)
767 {
768 103079 SwsGraph *graph = *out_graph;
769
4/6
✓ Branch 0 taken 96802 times.
✓ Branch 1 taken 6277 times.
✓ Branch 3 taken 96802 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 96802 times.
✗ Branch 6 not taken.
199881 if (graph && ff_fmt_equal(&graph->src, src) &&
770
1/2
✓ Branch 1 taken 96802 times.
✗ Branch 2 not taken.
193604 ff_fmt_equal(&graph->dst, dst) &&
771 96802 opts_equal(ctx, &graph->opts_copy))
772 {
773 96802 ff_sws_graph_update_metadata(graph, &src->color);
774 96802 return 0;
775 }
776
777 6277 ff_sws_graph_free(out_graph);
778 6277 return ff_sws_graph_create(ctx, dst, src, field, out_graph);
779 }
780
781 96802 void ff_sws_graph_update_metadata(SwsGraph *graph, const SwsColor *color)
782 {
783
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 96802 times.
96802 if (!color)
784 return;
785
786 96802 ff_color_update_dynamic(&graph->src.color, color);
787 }
788
789 103079 void ff_sws_graph_run(SwsGraph *graph, uint8_t *const out_data[4],
790 const int out_linesize[4],
791 const uint8_t *const in_data[4],
792 const int in_linesize[4])
793 {
794 103079 SwsImg *out = &graph->exec.output;
795 103079 SwsImg *in = &graph->exec.input;
796 103079 memcpy(out->data, out_data, sizeof(out->data));
797 103079 memcpy(out->linesize, out_linesize, sizeof(out->linesize));
798 103079 memcpy(in->data, in_data, sizeof(in->data));
799 103079 memcpy(in->linesize, in_linesize, sizeof(in->linesize));
800
801
2/2
✓ Branch 0 taken 103734 times.
✓ Branch 1 taken 103079 times.
206813 for (int i = 0; i < graph->num_passes; i++) {
802 103734 const SwsPass *pass = graph->passes[i];
803 103734 graph->exec.pass = pass;
804
2/2
✓ Branch 0 taken 103081 times.
✓ Branch 1 taken 653 times.
103734 if (pass->setup) {
805
2/2
✓ Branch 0 taken 358 times.
✓ Branch 1 taken 102723 times.
103081 pass->setup(pass->output.fmt != AV_PIX_FMT_NONE ? &pass->output : out,
806
2/2
✓ Branch 0 taken 299 times.
✓ Branch 1 taken 102782 times.
103081 pass->input ? &pass->input->output : in, pass);
807 }
808 103734 avpriv_slicethread_execute(graph->slicethread, pass->num_slices, 0);
809 }
810 103079 }
811