FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libswscale/graph.c
Date: 2025-04-25 22:50:00
Exec Total Coverage
Lines: 317 408 77.7%
Functions: 26 30 86.7%
Branches: 151 226 66.8%

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
38 6185 static int pass_alloc_output(SwsPass *pass)
39 {
40
3/4
✓ Branch 0 taken 59 times.
✓ Branch 1 taken 6126 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 59 times.
6185 if (!pass || pass->output.fmt != AV_PIX_FMT_NONE)
41 6126 return 0;
42 59 pass->output.fmt = pass->format;
43 59 return av_image_alloc(pass->output.data, pass->output.linesize, pass->width,
44 59 pass->num_slices * pass->slice_h, pass->format, 64);
45 }
46
47 /* slice_align should be a power of two, or 0 to disable slice threading */
48 6185 static SwsPass *pass_add(SwsGraph *graph, void *priv, enum AVPixelFormat fmt,
49 int w, int h, SwsPass *input, int slice_align,
50 sws_filter_run_t run)
51 {
52 int ret;
53 6185 SwsPass *pass = av_mallocz(sizeof(*pass));
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6185 times.
6185 if (!pass)
55 return NULL;
56
57 6185 pass->graph = graph;
58 6185 pass->run = run;
59 6185 pass->priv = priv;
60 6185 pass->format = fmt;
61 6185 pass->width = w;
62 6185 pass->height = h;
63 6185 pass->input = input;
64 6185 pass->output.fmt = AV_PIX_FMT_NONE;
65
66 6185 ret = pass_alloc_output(input);
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6185 times.
6185 if (ret < 0) {
68 av_free(pass);
69 return NULL;
70 }
71
72
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 6174 times.
6185 if (!slice_align) {
73 11 pass->slice_h = pass->height;
74 11 pass->num_slices = 1;
75 } else {
76 6174 pass->slice_h = (pass->height + graph->num_threads - 1) / graph->num_threads;
77 6174 pass->slice_h = FFALIGN(pass->slice_h, slice_align);
78 6174 pass->num_slices = (pass->height + pass->slice_h - 1) / pass->slice_h;
79 }
80
81 6185 ret = av_dynarray_add_nofree(&graph->passes, &graph->num_passes, pass);
82
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6185 times.
6185 if (ret < 0)
83 av_freep(&pass);
84 6185 return pass;
85 }
86
87 /* Wrapper around pass_add that chains a pass "in-place" */
88 58 static int pass_append(SwsGraph *graph, void *priv, enum AVPixelFormat fmt,
89 int w, int h, SwsPass **pass, int slice_align,
90 sws_filter_run_t run)
91 {
92 58 SwsPass *new = pass_add(graph, priv, fmt, w, h, *pass, slice_align, run);
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (!new)
94 return AVERROR(ENOMEM);
95 58 *pass = new;
96 58 return 0;
97 }
98
99 1359383 static int vshift(enum AVPixelFormat fmt, int plane)
100 {
101 1359383 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
102
4/4
✓ Branch 0 taken 910550 times.
✓ Branch 1 taken 448833 times.
✓ Branch 2 taken 289837 times.
✓ Branch 3 taken 620713 times.
1359383 return (plane == 1 || plane == 2) ? desc->log2_chroma_h : 0;
103 }
104
105 /* Shift an image vertically by y lines */
106 612119 static SwsImg shift_img(const SwsImg *img_base, int y)
107 {
108 612119 SwsImg img = *img_base;
109
4/4
✓ Branch 0 taken 1962908 times.
✓ Branch 1 taken 8594 times.
✓ Branch 2 taken 1359383 times.
✓ Branch 3 taken 603525 times.
1971502 for (int i = 0; i < 4 && img.data[i]; i++)
110 1359383 img.data[i] += (y >> vshift(img.fmt, i)) * img.linesize[i];
111 612119 return img;
112 }
113
114 static void run_copy(const SwsImg *out_base, const SwsImg *in_base,
115 int y, int h, const SwsPass *pass)
116 {
117 SwsImg in = shift_img(in_base, y);
118 SwsImg out = shift_img(out_base, y);
119
120 for (int i = 0; i < FF_ARRAY_ELEMS(in.data) && in.data[i]; i++) {
121 const int lines = h >> vshift(in.fmt, i);
122 if (in.linesize[i] == out.linesize[i]) {
123 memcpy(out.data[i], in.data[i], lines * out.linesize[i]);
124 } else {
125 const int linesize = FFMIN(out.linesize[i], in.linesize[i]);
126 for (int j = 0; j < lines; j++) {
127 memcpy(out.data[i], in.data[i], linesize);
128 in.data[i] += in.linesize[i];
129 out.data[i] += out.linesize[i];
130 }
131 }
132 }
133 }
134
135 54 static void run_rgb0(const SwsImg *out, const SwsImg *in, int y, int h,
136 const SwsPass *pass)
137 {
138 54 SwsInternal *c = pass->priv;
139 54 const int x0 = c->src0Alpha - 1;
140 54 const int w4 = 4 * pass->width;
141 54 const int src_stride = in->linesize[0];
142 54 const int dst_stride = out->linesize[0];
143 54 const uint8_t *src = in->data[0] + y * src_stride;
144 54 uint8_t *dst = out->data[0] + y * dst_stride;
145
146
2/2
✓ Branch 0 taken 4662 times.
✓ Branch 1 taken 54 times.
4716 for (int y = 0; y < h; y++) {
147 4662 memcpy(dst, src, w4 * sizeof(*dst));
148
2/2
✓ Branch 0 taken 2869202 times.
✓ Branch 1 taken 4662 times.
2873864 for (int x = x0; x < w4; x += 4)
149 2869202 dst[x] = 0xFF;
150
151 4662 src += src_stride;
152 4662 dst += dst_stride;
153 }
154 54 }
155
156 2577 static void run_xyz2rgb(const SwsImg *out, const SwsImg *in, int y, int h,
157 const SwsPass *pass)
158 {
159 2577 ff_xyz12Torgb48(pass->priv, out->data[0] + y * out->linesize[0], out->linesize[0],
160 2577 in->data[0] + y * in->linesize[0], in->linesize[0],
161 2577 pass->width, h);
162 2577 }
163
164 2618 static void run_rgb2xyz(const SwsImg *out, const SwsImg *in, int y, int h,
165 const SwsPass *pass)
166 {
167 2618 ff_rgb48Toxyz12(pass->priv, out->data[0] + y * out->linesize[0], out->linesize[0],
168 2618 in->data[0] + y * in->linesize[0], in->linesize[0],
169 2618 pass->width, h);
170 2618 }
171
172 /***********************************************************************
173 * Internal ff_swscale() wrapper. This re-uses the legacy scaling API. *
174 * This is considered fully deprecated, and will be replaced by a full *
175 * reimplementation ASAP. *
176 ***********************************************************************/
177
178 6127 static void free_legacy_swscale(void *priv)
179 {
180 6127 SwsContext *sws = priv;
181 6127 sws_free_context(&sws);
182 6127 }
183
184 98785 static void setup_legacy_swscale(const SwsImg *out, const SwsImg *in,
185 const SwsPass *pass)
186 {
187 98785 SwsContext *sws = pass->priv;
188 98785 SwsInternal *c = sws_internal(sws);
189
5/6
✓ Branch 0 taken 91545 times.
✓ Branch 1 taken 7240 times.
✓ Branch 2 taken 870 times.
✓ Branch 3 taken 90675 times.
✓ Branch 4 taken 870 times.
✗ Branch 5 not taken.
98785 if (sws->flags & SWS_BITEXACT && sws->dither == SWS_DITHER_ED && c->dither_error[0]) {
190
2/2
✓ Branch 0 taken 3480 times.
✓ Branch 1 taken 870 times.
4350 for (int i = 0; i < 4; i++)
191 3480 memset(c->dither_error[i], 0, sizeof(c->dither_error[0][0]) * (sws->dst_w + 2));
192 }
193
194
2/2
✓ Branch 1 taken 4713 times.
✓ Branch 2 taken 94072 times.
98785 if (usePal(sws->src_format))
195 4713 ff_update_palette(c, (const uint32_t *) in->data[1]);
196 98785 }
197
198 612119 static inline SwsContext *slice_ctx(const SwsPass *pass, int y)
199 {
200 612119 SwsContext *sws = pass->priv;
201 612119 SwsInternal *parent = sws_internal(sws);
202
2/2
✓ Branch 0 taken 34491 times.
✓ Branch 1 taken 577628 times.
612119 if (pass->num_slices == 1)
203 34491 return sws;
204
205 av_assert1(parent->nb_slice_ctx == pass->num_slices);
206 577628 sws = parent->slice_ctx[y / pass->slice_h];
207
208
2/2
✓ Branch 1 taken 35117 times.
✓ Branch 2 taken 542511 times.
577628 if (usePal(sws->src_format)) {
209 35117 SwsInternal *sub = sws_internal(sws);
210 35117 memcpy(sub->pal_yuv, parent->pal_yuv, sizeof(sub->pal_yuv));
211 35117 memcpy(sub->pal_rgb, parent->pal_rgb, sizeof(sub->pal_rgb));
212 }
213
214 577628 return sws;
215 }
216
217 87075 static void run_legacy_unscaled(const SwsImg *out, const SwsImg *in_base,
218 int y, int h, const SwsPass *pass)
219 {
220 87075 SwsContext *sws = slice_ctx(pass, y);
221 87075 SwsInternal *c = sws_internal(sws);
222 87075 const SwsImg in = shift_img(in_base, y);
223
224 87075 c->convert_unscaled(c, (const uint8_t *const *) in.data, in.linesize, y, h,
225 87075 out->data, out->linesize);
226 87075 }
227
228 525044 static void run_legacy_swscale(const SwsImg *out_base, const SwsImg *in,
229 int y, int h, const SwsPass *pass)
230 {
231 525044 SwsContext *sws = slice_ctx(pass, y);
232 525044 SwsInternal *c = sws_internal(sws);
233 525044 const SwsImg out = shift_img(out_base, y);
234
235 525044 ff_swscale(c, (const uint8_t *const *) in->data, in->linesize, 0,
236 sws->src_h, out.data, out.linesize, y, h);
237 525044 }
238
239 12252 static void get_chroma_pos(SwsGraph *graph, int *h_chr_pos, int *v_chr_pos,
240 const SwsFormat *fmt)
241 {
242 12252 enum AVChromaLocation chroma_loc = fmt->loc;
243 12252 const int sub_x = fmt->desc->log2_chroma_w;
244 12252 const int sub_y = fmt->desc->log2_chroma_h;
245 int x_pos, y_pos;
246
247 /* Explicitly default to center siting for compatibility with swscale */
248
2/2
✓ Branch 0 taken 12251 times.
✓ Branch 1 taken 1 times.
12252 if (chroma_loc == AVCHROMA_LOC_UNSPECIFIED) {
249 12251 chroma_loc = AVCHROMA_LOC_CENTER;
250
4/4
✓ Branch 0 taken 6312 times.
✓ Branch 1 taken 5939 times.
✓ Branch 2 taken 240 times.
✓ Branch 3 taken 6072 times.
12251 graph->incomplete |= sub_x || sub_y;
251 }
252
253 /* av_chroma_location_enum_to_pos() always gives us values in the range from
254 * 0 to 256, but we need to adjust this to the true value range of the
255 * subsampling grid, which may be larger for h/v_sub > 1 */
256 12252 av_chroma_location_enum_to_pos(&x_pos, &y_pos, chroma_loc);
257 12252 x_pos *= (1 << sub_x) - 1;
258 12252 y_pos *= (1 << sub_y) - 1;
259
260 /* Fix vertical chroma position for interlaced frames */
261
3/4
✓ Branch 0 taken 4771 times.
✓ Branch 1 taken 7481 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4771 times.
12252 if (sub_y && fmt->interlaced) {
262 /* When vertically subsampling, chroma samples are effectively only
263 * placed next to even rows. To access them from the odd field, we need
264 * to account for this shift by offsetting the distance of one luma row.
265 *
266 * For 4x vertical subsampling (v_sub == 2), they are only placed
267 * next to every *other* even row, so we need to shift by three luma
268 * rows to get to the chroma sample. */
269 if (graph->field == FIELD_BOTTOM)
270 y_pos += (256 << sub_y) - 256;
271
272 /* Luma row distance is doubled for fields, so halve offsets */
273 y_pos >>= 1;
274 }
275
276 /* Explicitly strip chroma offsets when not subsampling, because it
277 * interferes with the operation of flags like SWS_FULL_CHR_H_INP */
278
2/2
✓ Branch 0 taken 5940 times.
✓ Branch 1 taken 6312 times.
12252 *h_chr_pos = sub_x ? x_pos : -513;
279
2/2
✓ Branch 0 taken 4771 times.
✓ Branch 1 taken 7481 times.
12252 *v_chr_pos = sub_y ? y_pos : -513;
280 12252 }
281
282 24504 static void legacy_chr_pos(SwsGraph *graph, int *chr_pos, int override, int *warned)
283 {
284
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24504 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24504 if (override == -513 || override == *chr_pos)
285 24504 return;
286
287 if (!*warned) {
288 av_log(NULL, AV_LOG_WARNING,
289 "Setting chroma position directly is deprecated, make sure "
290 "the frame is tagged with the correct chroma location.\n");
291 *warned = 1;
292 }
293
294 *chr_pos = override;
295 }
296
297 6128 static int init_legacy_subpass(SwsGraph *graph, SwsContext *sws,
298 SwsPass *input, SwsPass **output)
299 {
300 6128 SwsInternal *c = sws_internal(sws);
301 6128 const int src_w = sws->src_w, src_h = sws->src_h;
302 6128 const int dst_w = sws->dst_w, dst_h = sws->dst_h;
303
4/4
✓ Branch 0 taken 5682 times.
✓ Branch 1 taken 446 times.
✓ Branch 2 taken 5672 times.
✓ Branch 3 taken 10 times.
6128 const int unscaled = src_w == dst_w && src_h == dst_h;
304 6128 int align = c->dst_slice_align;
305 6128 SwsPass *pass = NULL;
306 int ret;
307
308
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6127 times.
6128 if (c->cascaded_context[0]) {
309
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 const int num_cascaded = c->cascaded_context[2] ? 3 : 2;
310
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (int i = 0; i < num_cascaded; i++) {
311 2 SwsContext *sub = c->cascaded_context[i];
312 2 const int is_last = i + 1 == num_cascaded;
313
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 ret = init_legacy_subpass(graph, sub, input, is_last ? output : &input);
314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
315 return ret;
316 /* Steal cascaded context, so we can free the parent */
317 2 c->cascaded_context[i] = NULL;
318 }
319
320 1 sws_free_context(&sws);
321 1 return 0;
322 }
323
324
3/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 6116 times.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
6127 if (sws->dither == SWS_DITHER_ED && !c->convert_unscaled)
325 11 align = 0; /* disable slice threading */
326
327
6/6
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 6102 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 4 times.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 15 times.
6127 if (c->src0Alpha && !c->dst0Alpha && isALPHA(sws->dst_format)) {
328 6 ret = pass_append(graph, c, AV_PIX_FMT_RGBA, src_w, src_h, &input, 1, run_rgb0);
329
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
330 return ret;
331 }
332
333
5/6
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 6113 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
6127 if (c->srcXYZ && !(c->dstXYZ && unscaled)) {
334 14 ret = pass_append(graph, c, AV_PIX_FMT_RGB48, src_w, src_h, &input, 1, run_xyz2rgb);
335
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (ret < 0)
336 return ret;
337 }
338
339 6127 pass = pass_add(graph, sws, sws->dst_format, dst_w, dst_h, input, align,
340
2/2
✓ Branch 0 taken 643 times.
✓ Branch 1 taken 5484 times.
6127 c->convert_unscaled ? run_legacy_unscaled : run_legacy_swscale);
341
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6127 times.
6127 if (!pass)
342 return AVERROR(ENOMEM);
343 6127 pass->setup = setup_legacy_swscale;
344 6127 pass->free = free_legacy_swscale;
345
346 /**
347 * For slice threading, we need to create sub contexts, similar to how
348 * swscale normally handles it internally. The most important difference
349 * is that we handle cascaded contexts before threaded contexts; whereas
350 * context_init_threaded() does it the other way around.
351 */
352
353
2/2
✓ Branch 0 taken 2446 times.
✓ Branch 1 taken 3681 times.
6127 if (pass->num_slices > 1) {
354 2446 c->slice_ctx = av_calloc(pass->num_slices, sizeof(*c->slice_ctx));
355
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2446 times.
2446 if (!c->slice_ctx)
356 return AVERROR(ENOMEM);
357
358
2/2
✓ Branch 0 taken 21930 times.
✓ Branch 1 taken 2446 times.
24376 for (int i = 0; i < pass->num_slices; i++) {
359 SwsContext *slice;
360 SwsInternal *c2;
361 21930 slice = c->slice_ctx[i] = sws_alloc_context();
362
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21930 times.
21930 if (!slice)
363 return AVERROR(ENOMEM);
364 21930 c->nb_slice_ctx++;
365
366 21930 c2 = sws_internal(slice);
367 21930 c2->parent = sws;
368
369 21930 ret = av_opt_copy(slice, sws);
370
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21930 times.
21930 if (ret < 0)
371 return ret;
372
373 21930 ret = ff_sws_init_single_context(slice, NULL, NULL);
374
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21930 times.
21930 if (ret < 0)
375 return ret;
376
377 21930 sws_setColorspaceDetails(slice, c->srcColorspaceTable,
378 21930 slice->src_range, c->dstColorspaceTable,
379 slice->dst_range, c->brightness, c->contrast,
380 c->saturation);
381
382
2/2
✓ Branch 0 taken 87720 times.
✓ Branch 1 taken 21930 times.
109650 for (int i = 0; i < FF_ARRAY_ELEMS(c->srcColorspaceTable); i++) {
383 87720 c2->srcColorspaceTable[i] = c->srcColorspaceTable[i];
384 87720 c2->dstColorspaceTable[i] = c->dstColorspaceTable[i];
385 }
386 }
387 }
388
389
5/6
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 6089 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 36 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
6127 if (c->dstXYZ && !(c->srcXYZ && unscaled)) {
390 38 ret = pass_append(graph, c, AV_PIX_FMT_RGB48, dst_w, dst_h, &pass, 1, run_rgb2xyz);
391
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 if (ret < 0)
392 return ret;
393 }
394
395 6127 *output = pass;
396 6127 return 0;
397 }
398
399 6126 static int add_legacy_sws_pass(SwsGraph *graph, SwsFormat src, SwsFormat dst,
400 SwsPass *input, SwsPass **output)
401 {
402 6126 int ret, warned = 0;
403 6126 SwsContext *const ctx = graph->ctx;
404 6126 SwsContext *sws = sws_alloc_context();
405
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6126 times.
6126 if (!sws)
406 return AVERROR(ENOMEM);
407
408 6126 sws->flags = ctx->flags;
409 6126 sws->dither = ctx->dither;
410 6126 sws->alpha_blend = ctx->alpha_blend;
411 6126 sws->gamma_flag = ctx->gamma_flag;
412
413 6126 sws->src_w = src.width;
414 6126 sws->src_h = src.height;
415 6126 sws->src_format = src.format;
416 6126 sws->src_range = src.range == AVCOL_RANGE_JPEG;
417
418 6126 sws->dst_w = dst.width;
419 6126 sws->dst_h = dst.height;
420 6126 sws->dst_format = dst.format;
421 6126 sws->dst_range = dst.range == AVCOL_RANGE_JPEG;
422 6126 get_chroma_pos(graph, &sws->src_h_chr_pos, &sws->src_v_chr_pos, &src);
423 6126 get_chroma_pos(graph, &sws->dst_h_chr_pos, &sws->dst_v_chr_pos, &dst);
424
425 6126 graph->incomplete |= src.range == AVCOL_RANGE_UNSPECIFIED;
426 6126 graph->incomplete |= dst.range == AVCOL_RANGE_UNSPECIFIED;
427
428 /* Allow overriding chroma position with the legacy API */
429 6126 legacy_chr_pos(graph, &sws->src_h_chr_pos, ctx->src_h_chr_pos, &warned);
430 6126 legacy_chr_pos(graph, &sws->src_v_chr_pos, ctx->src_v_chr_pos, &warned);
431 6126 legacy_chr_pos(graph, &sws->dst_h_chr_pos, ctx->dst_h_chr_pos, &warned);
432 6126 legacy_chr_pos(graph, &sws->dst_v_chr_pos, ctx->dst_v_chr_pos, &warned);
433
434 6126 sws->scaler_params[0] = ctx->scaler_params[0];
435 6126 sws->scaler_params[1] = ctx->scaler_params[1];
436
437 6126 ret = sws_init_context(sws, NULL, NULL);
438
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6126 times.
6126 if (ret < 0) {
439 sws_free_context(&sws);
440 return ret;
441 }
442
443 /* Set correct color matrices */
444 {
445 int in_full, out_full, brightness, contrast, saturation;
446 const int *inv_table, *table;
447 6126 sws_getColorspaceDetails(sws, (int **)&inv_table, &in_full,
448 (int **)&table, &out_full,
449 &brightness, &contrast, &saturation);
450
451 6126 inv_table = sws_getCoefficients(src.csp);
452 6126 table = sws_getCoefficients(dst.csp);
453
454
2/2
✓ Branch 0 taken 2108 times.
✓ Branch 1 taken 4018 times.
8234 graph->incomplete |= src.csp != dst.csp &&
455
2/2
✓ Branch 0 taken 625 times.
✓ Branch 1 taken 1483 times.
2108 (src.csp == AVCOL_SPC_UNSPECIFIED ||
456
2/2
✓ Branch 0 taken 616 times.
✓ Branch 1 taken 9 times.
625 dst.csp == AVCOL_SPC_UNSPECIFIED);
457
458 6126 sws_setColorspaceDetails(sws, inv_table, in_full, table, out_full,
459 brightness, contrast, saturation);
460 }
461
462 6126 ret = init_legacy_subpass(graph, sws, input, output);
463
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6126 times.
6126 if (ret < 0) {
464 sws_free_context(&sws);
465 return ret;
466 }
467
468 6126 return 0;
469 }
470
471 /**************************
472 * Gamut and tone mapping *
473 **************************/
474
475 static void free_lut3d(void *priv)
476 {
477 SwsLut3D *lut = priv;
478 ff_sws_lut3d_free(&lut);
479 }
480
481 static void setup_lut3d(const SwsImg *out, const SwsImg *in, const SwsPass *pass)
482 {
483 SwsLut3D *lut = pass->priv;
484
485 /* Update dynamic frame metadata from the original source frame */
486 ff_sws_lut3d_update(lut, &pass->graph->src.color);
487 }
488
489 static void run_lut3d(const SwsImg *out_base, const SwsImg *in_base,
490 int y, int h, const SwsPass *pass)
491 {
492 SwsLut3D *lut = pass->priv;
493 const SwsImg in = shift_img(in_base, y);
494 const SwsImg out = shift_img(out_base, y);
495
496 ff_sws_lut3d_apply(lut, in.data[0], in.linesize[0], out.data[0],
497 out.linesize[0], pass->width, h);
498 }
499
500 6126 static int adapt_colors(SwsGraph *graph, SwsFormat src, SwsFormat dst,
501 SwsPass *input, SwsPass **output)
502 {
503 enum AVPixelFormat fmt_in, fmt_out;
504 6126 SwsColorMap map = {0};
505 SwsLut3D *lut;
506 SwsPass *pass;
507 int ret;
508
509 /**
510 * Grayspace does not really have primaries, so just force the use of
511 * the equivalent other primary set to avoid a conversion. Technically,
512 * this does affect the weights used for the Grayscale conversion, but
513 * in practise, that should give the expected results more often than not.
514 */
515
2/2
✓ Branch 1 taken 340 times.
✓ Branch 2 taken 5786 times.
6126 if (isGray(dst.format)) {
516 340 dst.color = src.color;
517
2/2
✓ Branch 1 taken 93 times.
✓ Branch 2 taken 5693 times.
5786 } else if (isGray(src.format)) {
518 93 src.color = dst.color;
519 }
520
521 /* Fully infer color spaces before color mapping logic */
522 6126 graph->incomplete |= ff_infer_colors(&src.color, &dst.color);
523
524 6126 map.intent = graph->ctx->intent;
525 6126 map.src = src.color;
526 6126 map.dst = dst.color;
527
528
1/2
✓ Branch 1 taken 6126 times.
✗ Branch 2 not taken.
6126 if (ff_sws_color_map_noop(&map))
529 6126 return 0;
530
531 lut = ff_sws_lut3d_alloc();
532 if (!lut)
533 return AVERROR(ENOMEM);
534
535 fmt_in = ff_sws_lut3d_pick_pixfmt(src, 0);
536 fmt_out = ff_sws_lut3d_pick_pixfmt(dst, 1);
537 if (fmt_in != src.format) {
538 SwsFormat tmp = src;
539 tmp.format = fmt_in;
540 ret = add_legacy_sws_pass(graph, src, tmp, input, &input);
541 if (ret < 0)
542 return ret;
543 }
544
545 ret = ff_sws_lut3d_generate(lut, fmt_in, fmt_out, &map);
546 if (ret < 0) {
547 ff_sws_lut3d_free(&lut);
548 return ret;
549 }
550
551 pass = pass_add(graph, lut, fmt_out, src.width, src.height,
552 input, 1, run_lut3d);
553 if (!pass) {
554 ff_sws_lut3d_free(&lut);
555 return AVERROR(ENOMEM);
556 }
557 pass->setup = setup_lut3d;
558 pass->free = free_lut3d;
559
560 *output = pass;
561 return 0;
562 }
563
564 /***************************************
565 * Main filter graph construction code *
566 ***************************************/
567
568 6126 static int init_passes(SwsGraph *graph)
569 {
570 6126 SwsFormat src = graph->src;
571 6126 SwsFormat dst = graph->dst;
572 6126 SwsPass *pass = NULL; /* read from main input image */
573 int ret;
574
575 6126 ret = adapt_colors(graph, src, dst, pass, &pass);
576
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6126 times.
6126 if (ret < 0)
577 return ret;
578
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6126 times.
6126 src.format = pass ? pass->format : src.format;
579 6126 src.color = dst.color;
580
581
1/2
✓ Branch 1 taken 6126 times.
✗ Branch 2 not taken.
6126 if (!ff_fmt_equal(&src, &dst)) {
582 6126 ret = add_legacy_sws_pass(graph, src, dst, pass, &pass);
583
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6126 times.
6126 if (ret < 0)
584 return ret;
585 }
586
587
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6126 times.
6126 if (!pass) {
588 /* No passes were added, so no operations were necessary */
589 graph->noop = 1;
590
591 /* Add threaded memcpy pass */
592 pass = pass_add(graph, NULL, dst.format, dst.width, dst.height, pass, 1, run_copy);
593 if (!pass)
594 return AVERROR(ENOMEM);
595 }
596
597 6126 return 0;
598 }
599
600 617368 static void sws_graph_worker(void *priv, int jobnr, int threadnr, int nb_jobs,
601 int nb_threads)
602 {
603 617368 SwsGraph *graph = priv;
604 617368 const SwsPass *pass = graph->exec.pass;
605
2/2
✓ Branch 0 taken 5258 times.
✓ Branch 1 taken 612110 times.
617368 const SwsImg *input = pass->input ? &pass->input->output : &graph->exec.input;
606
2/2
✓ Branch 0 taken 5258 times.
✓ Branch 1 taken 612110 times.
617368 const SwsImg *output = pass->output.fmt != AV_PIX_FMT_NONE ? &pass->output : &graph->exec.output;
607 617368 const int slice_y = jobnr * pass->slice_h;
608 617368 const int slice_h = FFMIN(pass->slice_h, pass->height - slice_y);
609
610 617368 pass->run(output, input, slice_y, slice_h, pass);
611 617368 }
612
613 6126 int ff_sws_graph_create(SwsContext *ctx, const SwsFormat *dst, const SwsFormat *src,
614 int field, SwsGraph **out_graph)
615 {
616 int ret;
617 6126 SwsGraph *graph = av_mallocz(sizeof(*graph));
618
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6126 times.
6126 if (!graph)
619 return AVERROR(ENOMEM);
620
621 6126 graph->ctx = ctx;
622 6126 graph->src = *src;
623 6126 graph->dst = *dst;
624 6126 graph->field = field;
625 6126 graph->opts_copy = *ctx;
626
627 6126 graph->exec.input.fmt = src->format;
628 6126 graph->exec.output.fmt = dst->format;
629
630 6126 ret = avpriv_slicethread_create(&graph->slicethread, (void *) graph,
631 sws_graph_worker, NULL, ctx->threads);
632
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6126 times.
6126 if (ret == AVERROR(ENOSYS))
633 graph->num_threads = 1;
634
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6126 times.
6126 else if (ret < 0)
635 goto error;
636 else
637 6126 graph->num_threads = ret;
638
639 6126 ret = init_passes(graph);
640
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6126 times.
6126 if (ret < 0)
641 goto error;
642
643 6126 *out_graph = graph;
644 6126 return 0;
645
646 error:
647 ff_sws_graph_free(&graph);
648 return ret;
649 }
650
651 189280 void ff_sws_graph_free(SwsGraph **pgraph)
652 {
653 189280 SwsGraph *graph = *pgraph;
654
2/2
✓ Branch 0 taken 183154 times.
✓ Branch 1 taken 6126 times.
189280 if (!graph)
655 183154 return;
656
657 6126 avpriv_slicethread_free(&graph->slicethread);
658
659
2/2
✓ Branch 0 taken 6185 times.
✓ Branch 1 taken 6126 times.
12311 for (int i = 0; i < graph->num_passes; i++) {
660 6185 SwsPass *pass = graph->passes[i];
661
2/2
✓ Branch 0 taken 6127 times.
✓ Branch 1 taken 58 times.
6185 if (pass->free)
662 6127 pass->free(pass->priv);
663
2/2
✓ Branch 0 taken 59 times.
✓ Branch 1 taken 6126 times.
6185 if (pass->output.fmt != AV_PIX_FMT_NONE)
664 59 av_free(pass->output.data[0]);
665 6185 av_free(pass);
666 }
667 6126 av_free(graph->passes);
668
669 6126 av_free(graph);
670 6126 *pgraph = NULL;
671 }
672
673 /* Tests only options relevant to SwsGraph */
674 92658 static int opts_equal(const SwsContext *c1, const SwsContext *c2)
675 {
676 185316 return c1->flags == c2->flags &&
677
1/2
✓ Branch 0 taken 92658 times.
✗ Branch 1 not taken.
92658 c1->threads == c2->threads &&
678
1/2
✓ Branch 0 taken 92658 times.
✗ Branch 1 not taken.
92658 c1->dither == c2->dither &&
679
1/2
✓ Branch 0 taken 92658 times.
✗ Branch 1 not taken.
92658 c1->alpha_blend == c2->alpha_blend &&
680
1/2
✓ Branch 0 taken 92658 times.
✗ Branch 1 not taken.
92658 c1->gamma_flag == c2->gamma_flag &&
681
1/2
✓ Branch 0 taken 92658 times.
✗ Branch 1 not taken.
92658 c1->src_h_chr_pos == c2->src_h_chr_pos &&
682
1/2
✓ Branch 0 taken 92658 times.
✗ Branch 1 not taken.
92658 c1->src_v_chr_pos == c2->src_v_chr_pos &&
683
1/2
✓ Branch 0 taken 92658 times.
✗ Branch 1 not taken.
92658 c1->dst_h_chr_pos == c2->dst_h_chr_pos &&
684
1/2
✓ Branch 0 taken 92658 times.
✗ Branch 1 not taken.
92658 c1->dst_v_chr_pos == c2->dst_v_chr_pos &&
685
2/4
✓ Branch 0 taken 92658 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 92658 times.
✗ Branch 3 not taken.
277974 c1->intent == c2->intent &&
686
1/2
✓ Branch 0 taken 92658 times.
✗ Branch 1 not taken.
92658 !memcmp(c1->scaler_params, c2->scaler_params, sizeof(c1->scaler_params));
687
688 }
689
690 98784 int ff_sws_graph_reinit(SwsContext *ctx, const SwsFormat *dst, const SwsFormat *src,
691 int field, SwsGraph **out_graph)
692 {
693 98784 SwsGraph *graph = *out_graph;
694
4/6
✓ Branch 0 taken 92658 times.
✓ Branch 1 taken 6126 times.
✓ Branch 3 taken 92658 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 92658 times.
✗ Branch 6 not taken.
191442 if (graph && ff_fmt_equal(&graph->src, src) &&
695
1/2
✓ Branch 1 taken 92658 times.
✗ Branch 2 not taken.
185316 ff_fmt_equal(&graph->dst, dst) &&
696 92658 opts_equal(ctx, &graph->opts_copy))
697 {
698 92658 ff_sws_graph_update_metadata(graph, &src->color);
699 92658 return 0;
700 }
701
702 6126 ff_sws_graph_free(out_graph);
703 6126 return ff_sws_graph_create(ctx, dst, src, field, out_graph);
704 }
705
706 92658 void ff_sws_graph_update_metadata(SwsGraph *graph, const SwsColor *color)
707 {
708
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 92658 times.
92658 if (!color)
709 return;
710
711 92658 ff_color_update_dynamic(&graph->src.color, color);
712 }
713
714 98784 void ff_sws_graph_run(SwsGraph *graph, uint8_t *const out_data[4],
715 const int out_linesize[4],
716 const uint8_t *const in_data[4],
717 const int in_linesize[4])
718 {
719 98784 SwsImg *out = &graph->exec.output;
720 98784 SwsImg *in = &graph->exec.input;
721 98784 memcpy(out->data, out_data, sizeof(out->data));
722 98784 memcpy(out->linesize, out_linesize, sizeof(out->linesize));
723 98784 memcpy(in->data, in_data, sizeof(in->data));
724 98784 memcpy(in->linesize, in_linesize, sizeof(in->linesize));
725
726
2/2
✓ Branch 0 taken 99410 times.
✓ Branch 1 taken 98784 times.
198194 for (int i = 0; i < graph->num_passes; i++) {
727 99410 const SwsPass *pass = graph->passes[i];
728 99410 graph->exec.pass = pass;
729
2/2
✓ Branch 0 taken 98785 times.
✓ Branch 1 taken 625 times.
99410 if (pass->setup)
730 98785 pass->setup(out, in, pass);
731 99410 avpriv_slicethread_execute(graph->slicethread, pass->num_slices, 0);
732 }
733 98784 }
734