Coverage Report

Created: 2025-11-16 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libswscale/graph.c
Line
Count
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
static int pass_alloc_output(SwsPass *pass)
40
0
{
41
0
    if (!pass || pass->output.fmt != AV_PIX_FMT_NONE)
42
0
        return 0;
43
0
    pass->output.fmt = pass->format;
44
0
    return av_image_alloc(pass->output.data, pass->output.linesize, pass->width,
45
0
                          pass->num_slices * pass->slice_h, pass->format, 64);
46
0
}
47
48
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
0
{
52
0
    int ret;
53
0
    SwsPass *pass = av_mallocz(sizeof(*pass));
54
0
    if (!pass)
55
0
        return NULL;
56
57
0
    pass->graph  = graph;
58
0
    pass->run    = run;
59
0
    pass->priv   = priv;
60
0
    pass->format = fmt;
61
0
    pass->width  = width;
62
0
    pass->height = height;
63
0
    pass->input  = input;
64
0
    pass->output.fmt = AV_PIX_FMT_NONE;
65
66
0
    ret = pass_alloc_output(input);
67
0
    if (ret < 0) {
68
0
        av_free(pass);
69
0
        return NULL;
70
0
    }
71
72
0
    if (!align) {
73
0
        pass->slice_h = pass->height;
74
0
        pass->num_slices = 1;
75
0
    } else {
76
0
        pass->slice_h = (pass->height + graph->num_threads - 1) / graph->num_threads;
77
0
        pass->slice_h = FFALIGN(pass->slice_h, align);
78
0
        pass->num_slices = (pass->height + pass->slice_h - 1) / pass->slice_h;
79
0
    }
80
81
0
    ret = av_dynarray_add_nofree(&graph->passes, &graph->num_passes, pass);
82
0
    if (ret < 0)
83
0
        av_freep(&pass);
84
0
    return pass;
85
0
}
86
87
/* Wrapper around ff_sws_graph_add_pass() that chains a pass "in-place" */
88
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
0
{
91
0
    SwsPass *new = ff_sws_graph_add_pass(graph, fmt, w, h, *pass, align, priv, run);
92
0
    if (!new)
93
0
        return AVERROR(ENOMEM);
94
0
    *pass = new;
95
0
    return 0;
96
0
}
97
98
static void run_copy(const SwsImg *out_base, const SwsImg *in_base,
99
                     int y, int h, const SwsPass *pass)
100
0
{
101
0
    SwsImg in  = ff_sws_img_shift(in_base,  y);
102
0
    SwsImg out = ff_sws_img_shift(out_base, y);
103
104
0
    for (int i = 0; i < FF_ARRAY_ELEMS(out.data) && out.data[i]; i++) {
105
0
        const int lines = h >> ff_fmt_vshift(in.fmt, i);
106
0
        av_assert1(in.data[i]);
107
108
0
        if (in.linesize[i] == out.linesize[i]) {
109
0
            memcpy(out.data[i], in.data[i], lines * out.linesize[i]);
110
0
        } else {
111
0
            const int linesize = FFMIN(out.linesize[i], in.linesize[i]);
112
0
            for (int j = 0; j < lines; j++) {
113
0
                memcpy(out.data[i], in.data[i], linesize);
114
0
                in.data[i]  += in.linesize[i];
115
0
                out.data[i] += out.linesize[i];
116
0
            }
117
0
        }
118
0
    }
119
0
}
120
121
static void run_rgb0(const SwsImg *out, const SwsImg *in, int y, int h,
122
                     const SwsPass *pass)
123
0
{
124
0
    SwsInternal *c = pass->priv;
125
0
    const int x0 = c->src0Alpha - 1;
126
0
    const int w4 = 4 * pass->width;
127
0
    const int src_stride = in->linesize[0];
128
0
    const int dst_stride = out->linesize[0];
129
0
    const uint8_t *src = in->data[0] + y * src_stride;
130
0
    uint8_t *dst = out->data[0] + y * dst_stride;
131
132
0
    for (int y = 0; y < h; y++) {
133
0
        memcpy(dst, src, w4 * sizeof(*dst));
134
0
        for (int x = x0; x < w4; x += 4)
135
0
            dst[x] = 0xFF;
136
137
0
        src += src_stride;
138
0
        dst += dst_stride;
139
0
    }
140
0
}
141
142
static void run_xyz2rgb(const SwsImg *out, const SwsImg *in, int y, int h,
143
                        const SwsPass *pass)
144
0
{
145
0
    ff_xyz12Torgb48(pass->priv, out->data[0] + y * out->linesize[0], out->linesize[0],
146
0
                    in->data[0] + y * in->linesize[0], in->linesize[0],
147
0
                    pass->width, h);
148
0
}
149
150
static void run_rgb2xyz(const SwsImg *out, const SwsImg *in, int y, int h,
151
                        const SwsPass *pass)
152
0
{
153
0
    ff_rgb48Toxyz12(pass->priv, out->data[0] + y * out->linesize[0], out->linesize[0],
154
0
                    in->data[0] + y * in->linesize[0], in->linesize[0],
155
0
                    pass->width, h);
156
0
}
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
static void free_legacy_swscale(void *priv)
165
0
{
166
0
    SwsContext *sws = priv;
167
0
    sws_free_context(&sws);
168
0
}
169
170
static void setup_legacy_swscale(const SwsImg *out, const SwsImg *in,
171
                                 const SwsPass *pass)
172
0
{
173
0
    const SwsGraph *graph = pass->graph;
174
0
    const SwsImg *in_orig = &graph->exec.input;
175
0
    SwsContext *sws = pass->priv;
176
0
    SwsInternal *c = sws_internal(sws);
177
0
    if (sws->flags & SWS_BITEXACT && sws->dither == SWS_DITHER_ED && c->dither_error[0]) {
178
0
        for (int i = 0; i < 4; i++)
179
0
            memset(c->dither_error[i], 0, sizeof(c->dither_error[0][0]) * (sws->dst_w + 2));
180
0
    }
181
182
0
    if (usePal(sws->src_format))
183
0
        ff_update_palette(c, (const uint32_t *) in_orig->data[1]);
184
0
}
185
186
static inline SwsContext *slice_ctx(const SwsPass *pass, int y)
187
0
{
188
0
    SwsContext *sws = pass->priv;
189
0
    SwsInternal *parent = sws_internal(sws);
190
0
    if (pass->num_slices == 1)
191
0
        return sws;
192
193
0
    av_assert1(parent->nb_slice_ctx == pass->num_slices);
194
0
    sws = parent->slice_ctx[y / pass->slice_h];
195
196
0
    if (usePal(sws->src_format)) {
197
0
        SwsInternal *sub = sws_internal(sws);
198
0
        memcpy(sub->pal_yuv, parent->pal_yuv, sizeof(sub->pal_yuv));
199
0
        memcpy(sub->pal_rgb, parent->pal_rgb, sizeof(sub->pal_rgb));
200
0
    }
201
202
0
    return sws;
203
0
}
204
205
static void run_legacy_unscaled(const SwsImg *out, const SwsImg *in_base,
206
                                int y, int h, const SwsPass *pass)
207
0
{
208
0
    SwsContext *sws = slice_ctx(pass, y);
209
0
    SwsInternal *c = sws_internal(sws);
210
0
    const SwsImg in = ff_sws_img_shift(in_base, y);
211
212
0
    c->convert_unscaled(c, (const uint8_t *const *) in.data, in.linesize, y, h,
213
0
                        out->data, out->linesize);
214
0
}
215
216
static void run_legacy_swscale(const SwsImg *out_base, const SwsImg *in,
217
                               int y, int h, const SwsPass *pass)
218
0
{
219
0
    SwsContext *sws = slice_ctx(pass, y);
220
0
    SwsInternal *c = sws_internal(sws);
221
0
    const SwsImg out = ff_sws_img_shift(out_base, y);
222
223
0
    ff_swscale(c, (const uint8_t *const *) in->data, in->linesize, 0,
224
0
               sws->src_h, out.data, out.linesize, y, h);
225
0
}
226
227
static void get_chroma_pos(SwsGraph *graph, int *h_chr_pos, int *v_chr_pos,
228
                           const SwsFormat *fmt)
229
0
{
230
0
    enum AVChromaLocation chroma_loc = fmt->loc;
231
0
    const int sub_x = fmt->desc->log2_chroma_w;
232
0
    const int sub_y = fmt->desc->log2_chroma_h;
233
0
    int x_pos, y_pos;
234
235
    /* Explicitly default to center siting for compatibility with swscale */
236
0
    if (chroma_loc == AVCHROMA_LOC_UNSPECIFIED) {
237
0
        chroma_loc = AVCHROMA_LOC_CENTER;
238
0
        graph->incomplete |= sub_x || sub_y;
239
0
    }
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
0
    av_chroma_location_enum_to_pos(&x_pos, &y_pos, chroma_loc);
245
0
    x_pos *= (1 << sub_x) - 1;
246
0
    y_pos *= (1 << sub_y) - 1;
247
248
    /* Fix vertical chroma position for interlaced frames */
249
0
    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
0
        if (graph->field == FIELD_BOTTOM)
258
0
            y_pos += (256 << sub_y) - 256;
259
260
        /* Luma row distance is doubled for fields, so halve offsets */
261
0
        y_pos >>= 1;
262
0
    }
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
0
    *h_chr_pos = sub_x ? x_pos : -513;
267
0
    *v_chr_pos = sub_y ? y_pos : -513;
268
0
}
269
270
static void legacy_chr_pos(SwsGraph *graph, int *chr_pos, int override, int *warned)
271
0
{
272
0
    if (override == -513 || override == *chr_pos)
273
0
        return;
274
275
0
    if (!*warned) {
276
0
        av_log(NULL, AV_LOG_WARNING,
277
0
               "Setting chroma position directly is deprecated, make sure "
278
0
               "the frame is tagged with the correct chroma location.\n");
279
0
        *warned = 1;
280
0
    }
281
282
0
    *chr_pos = override;
283
0
}
284
285
/* Takes over ownership of `sws` */
286
static int init_legacy_subpass(SwsGraph *graph, SwsContext *sws,
287
                               SwsPass *input, SwsPass **output)
288
0
{
289
0
    SwsInternal *c = sws_internal(sws);
290
0
    const int src_w = sws->src_w, src_h = sws->src_h;
291
0
    const int dst_w = sws->dst_w, dst_h = sws->dst_h;
292
0
    const int unscaled = src_w == dst_w && src_h == dst_h;
293
0
    int align = c->dst_slice_align;
294
0
    SwsPass *pass = NULL;
295
0
    int ret;
296
297
0
    if (c->cascaded_context[0]) {
298
0
        const int num_cascaded = c->cascaded_context[2] ? 3 : 2;
299
0
        for (int i = 0; i < num_cascaded; i++) {
300
0
            const int is_last = i + 1 == num_cascaded;
301
302
            /* Steal cascaded context, so we can manage its lifetime independently */
303
0
            SwsContext *sub = c->cascaded_context[i];
304
0
            c->cascaded_context[i] = NULL;
305
306
0
            ret = init_legacy_subpass(graph, sub, input, is_last ? output : &input);
307
0
            if (ret < 0)
308
0
                break;
309
0
        }
310
311
0
        sws_free_context(&sws);
312
0
        return ret;
313
0
    }
314
315
0
    if (sws->dither == SWS_DITHER_ED && !c->convert_unscaled)
316
0
        align = 0; /* disable slice threading */
317
318
0
    if (c->src0Alpha && !c->dst0Alpha && isALPHA(sws->dst_format)) {
319
0
        ret = pass_append(graph, AV_PIX_FMT_RGBA, src_w, src_h, &input, 1, c, run_rgb0);
320
0
        if (ret < 0) {
321
0
            sws_free_context(&sws);
322
0
            return ret;
323
0
        }
324
0
    }
325
326
0
    if (c->srcXYZ && !(c->dstXYZ && unscaled)) {
327
0
        ret = pass_append(graph, AV_PIX_FMT_RGB48, src_w, src_h, &input, 1, c, run_xyz2rgb);
328
0
        if (ret < 0) {
329
0
            sws_free_context(&sws);
330
0
            return ret;
331
0
        }
332
0
    }
333
334
0
    pass = ff_sws_graph_add_pass(graph, sws->dst_format, dst_w, dst_h, input, align, sws,
335
0
                                 c->convert_unscaled ? run_legacy_unscaled : run_legacy_swscale);
336
0
    if (!pass) {
337
0
        sws_free_context(&sws);
338
0
        return AVERROR(ENOMEM);
339
0
    }
340
0
    pass->setup = setup_legacy_swscale;
341
0
    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
0
    if (pass->num_slices > 1) {
351
0
        c->slice_ctx = av_calloc(pass->num_slices, sizeof(*c->slice_ctx));
352
0
        if (!c->slice_ctx)
353
0
            return AVERROR(ENOMEM);
354
355
0
        for (int i = 0; i < pass->num_slices; i++) {
356
0
            SwsContext *slice;
357
0
            SwsInternal *c2;
358
0
            slice = c->slice_ctx[i] = sws_alloc_context();
359
0
            if (!slice)
360
0
                return AVERROR(ENOMEM);
361
0
            c->nb_slice_ctx++;
362
363
0
            c2 = sws_internal(slice);
364
0
            c2->parent = sws;
365
366
0
            ret = av_opt_copy(slice, sws);
367
0
            if (ret < 0)
368
0
                return ret;
369
370
0
            ret = ff_sws_init_single_context(slice, NULL, NULL);
371
0
            if (ret < 0)
372
0
                return ret;
373
374
0
            sws_setColorspaceDetails(slice, c->srcColorspaceTable,
375
0
                                     slice->src_range, c->dstColorspaceTable,
376
0
                                     slice->dst_range, c->brightness, c->contrast,
377
0
                                     c->saturation);
378
379
0
            for (int i = 0; i < FF_ARRAY_ELEMS(c->srcColorspaceTable); i++) {
380
0
                c2->srcColorspaceTable[i] = c->srcColorspaceTable[i];
381
0
                c2->dstColorspaceTable[i] = c->dstColorspaceTable[i];
382
0
            }
383
0
        }
384
0
    }
385
386
0
    if (c->dstXYZ && !(c->srcXYZ && unscaled)) {
387
0
        ret = pass_append(graph, AV_PIX_FMT_RGB48, dst_w, dst_h, &pass, 1, c, run_rgb2xyz);
388
0
        if (ret < 0)
389
0
            return ret;
390
0
    }
391
392
0
    *output = pass;
393
0
    return 0;
394
0
}
395
396
static int add_legacy_sws_pass(SwsGraph *graph, SwsFormat src, SwsFormat dst,
397
                               SwsPass *input, SwsPass **output)
398
0
{
399
0
    int ret, warned = 0;
400
0
    SwsContext *const ctx = graph->ctx;
401
0
    SwsContext *sws = sws_alloc_context();
402
0
    if (!sws)
403
0
        return AVERROR(ENOMEM);
404
405
0
    sws->flags       = ctx->flags;
406
0
    sws->dither      = ctx->dither;
407
0
    sws->alpha_blend = ctx->alpha_blend;
408
0
    sws->gamma_flag  = ctx->gamma_flag;
409
410
0
    sws->src_w       = src.width;
411
0
    sws->src_h       = src.height;
412
0
    sws->src_format  = src.format;
413
0
    sws->src_range   = src.range == AVCOL_RANGE_JPEG;
414
415
0
    sws->dst_w      = dst.width;
416
0
    sws->dst_h      = dst.height;
417
0
    sws->dst_format = dst.format;
418
0
    sws->dst_range  = dst.range == AVCOL_RANGE_JPEG;
419
0
    get_chroma_pos(graph, &sws->src_h_chr_pos, &sws->src_v_chr_pos, &src);
420
0
    get_chroma_pos(graph, &sws->dst_h_chr_pos, &sws->dst_v_chr_pos, &dst);
421
422
0
    graph->incomplete |= src.range == AVCOL_RANGE_UNSPECIFIED;
423
0
    graph->incomplete |= dst.range == AVCOL_RANGE_UNSPECIFIED;
424
425
    /* Allow overriding chroma position with the legacy API */
426
0
    legacy_chr_pos(graph, &sws->src_h_chr_pos, ctx->src_h_chr_pos, &warned);
427
0
    legacy_chr_pos(graph, &sws->src_v_chr_pos, ctx->src_v_chr_pos, &warned);
428
0
    legacy_chr_pos(graph, &sws->dst_h_chr_pos, ctx->dst_h_chr_pos, &warned);
429
0
    legacy_chr_pos(graph, &sws->dst_v_chr_pos, ctx->dst_v_chr_pos, &warned);
430
431
0
    sws->scaler_params[0] = ctx->scaler_params[0];
432
0
    sws->scaler_params[1] = ctx->scaler_params[1];
433
434
0
    ret = sws_init_context(sws, NULL, NULL);
435
0
    if (ret < 0) {
436
0
        sws_free_context(&sws);
437
0
        return ret;
438
0
    }
439
440
    /* Set correct color matrices */
441
0
    {
442
0
        int in_full, out_full, brightness, contrast, saturation;
443
0
        const int *inv_table, *table;
444
0
        sws_getColorspaceDetails(sws, (int **)&inv_table, &in_full,
445
0
                                (int **)&table, &out_full,
446
0
                                &brightness, &contrast, &saturation);
447
448
0
        inv_table = sws_getCoefficients(src.csp);
449
0
        table     = sws_getCoefficients(dst.csp);
450
451
0
        graph->incomplete |= src.csp != dst.csp &&
452
0
                            (src.csp == AVCOL_SPC_UNSPECIFIED ||
453
0
                             dst.csp == AVCOL_SPC_UNSPECIFIED);
454
455
0
        sws_setColorspaceDetails(sws, inv_table, in_full, table, out_full,
456
0
                                brightness, contrast, saturation);
457
0
    }
458
459
0
    return init_legacy_subpass(graph, sws, input, output);
460
0
}
461
462
/*********************
463
 * Format conversion *
464
 *********************/
465
466
#if CONFIG_UNSTABLE
467
static int add_convert_pass(SwsGraph *graph, SwsFormat src, SwsFormat dst,
468
                            SwsPass *input, SwsPass **output)
469
0
{
470
0
    const SwsPixelType type = SWS_PIXEL_F32;
471
472
0
    SwsContext *ctx = graph->ctx;
473
0
    SwsOpList *ops = NULL;
474
0
    int ret = AVERROR(ENOTSUP);
475
476
    /* Mark the entire new ops infrastructure as experimental for now */
477
0
    if (!(ctx->flags & SWS_UNSTABLE))
478
0
        goto fail;
479
480
    /* The new format conversion layer cannot scale for now */
481
0
    if (src.width != dst.width || src.height != dst.height ||
482
0
        src.desc->log2_chroma_h || src.desc->log2_chroma_w ||
483
0
        dst.desc->log2_chroma_h || dst.desc->log2_chroma_w)
484
0
        goto fail;
485
486
    /* The new code does not yet support alpha blending */
487
0
    if (src.desc->flags & AV_PIX_FMT_FLAG_ALPHA &&
488
0
        ctx->alpha_blend != SWS_ALPHA_BLEND_NONE)
489
0
        goto fail;
490
491
0
    ops = ff_sws_op_list_alloc();
492
0
    if (!ops)
493
0
        return AVERROR(ENOMEM);
494
0
    ops->src = src;
495
0
    ops->dst = dst;
496
497
0
    ret = ff_sws_decode_pixfmt(ops, src.format);
498
0
    if (ret < 0)
499
0
        goto fail;
500
0
    ret = ff_sws_decode_colors(ctx, type, ops, src, &graph->incomplete);
501
0
    if (ret < 0)
502
0
        goto fail;
503
0
    ret = ff_sws_encode_colors(ctx, type, ops, dst, &graph->incomplete);
504
0
    if (ret < 0)
505
0
        goto fail;
506
0
    ret = ff_sws_encode_pixfmt(ops, dst.format);
507
0
    if (ret < 0)
508
0
        goto fail;
509
510
0
    av_log(ctx, AV_LOG_VERBOSE, "Conversion pass for %s -> %s:\n",
511
0
           av_get_pix_fmt_name(src.format), av_get_pix_fmt_name(dst.format));
512
513
0
    av_log(ctx, AV_LOG_DEBUG, "Unoptimized operation list:\n");
514
0
    ff_sws_op_list_print(ctx, AV_LOG_DEBUG, ops);
515
0
    av_log(ctx, AV_LOG_DEBUG, "Optimized operation list:\n");
516
517
0
    ff_sws_op_list_optimize(ops);
518
0
    if (ops->num_ops == 0) {
519
0
        av_log(ctx, AV_LOG_VERBOSE, "  optimized into memcpy\n");
520
0
        ff_sws_op_list_free(&ops);
521
0
        *output = input;
522
0
        return 0;
523
0
    }
524
525
0
    ff_sws_op_list_print(ctx, AV_LOG_VERBOSE, ops);
526
527
0
    ret = ff_sws_compile_pass(graph, ops, 0, dst, input, output);
528
0
    if (ret < 0)
529
0
        goto fail;
530
531
0
    ret = 0;
532
    /* fall through */
533
534
0
fail:
535
0
    ff_sws_op_list_free(&ops);
536
0
    if (ret == AVERROR(ENOTSUP))
537
0
        return add_legacy_sws_pass(graph, src, dst, input, output);
538
0
    return ret;
539
0
}
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
0
{
551
0
    SwsLut3D *lut = priv;
552
0
    ff_sws_lut3d_free(&lut);
553
0
}
554
555
static void setup_lut3d(const SwsImg *out, const SwsImg *in, const SwsPass *pass)
556
0
{
557
0
    SwsLut3D *lut = pass->priv;
558
559
    /* Update dynamic frame metadata from the original source frame */
560
0
    ff_sws_lut3d_update(lut, &pass->graph->src.color);
561
0
}
562
563
static void run_lut3d(const SwsImg *out_base, const SwsImg *in_base,
564
                      int y, int h, const SwsPass *pass)
565
0
{
566
0
    SwsLut3D *lut = pass->priv;
567
0
    const SwsImg in  = ff_sws_img_shift(in_base,  y);
568
0
    const SwsImg out = ff_sws_img_shift(out_base, y);
569
570
0
    ff_sws_lut3d_apply(lut, in.data[0], in.linesize[0], out.data[0],
571
0
                       out.linesize[0], pass->width, h);
572
0
}
573
574
static int adapt_colors(SwsGraph *graph, SwsFormat src, SwsFormat dst,
575
                        SwsPass *input, SwsPass **output)
576
0
{
577
0
    enum AVPixelFormat fmt_in, fmt_out;
578
0
    SwsColorMap map = {0};
579
0
    SwsLut3D *lut;
580
0
    SwsPass *pass;
581
0
    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
0
    if (isGray(dst.format)) {
590
0
        dst.color = src.color;
591
0
    } else if (isGray(src.format)) {
592
0
        src.color = dst.color;
593
0
    }
594
595
    /* Fully infer color spaces before color mapping logic */
596
0
    graph->incomplete |= ff_infer_colors(&src.color, &dst.color);
597
598
0
    map.intent = graph->ctx->intent;
599
0
    map.src    = src.color;
600
0
    map.dst    = dst.color;
601
602
0
    if (ff_sws_color_map_noop(&map))
603
0
        return 0;
604
605
0
    lut = ff_sws_lut3d_alloc();
606
0
    if (!lut)
607
0
        return AVERROR(ENOMEM);
608
609
0
    fmt_in  = ff_sws_lut3d_pick_pixfmt(src, 0);
610
0
    fmt_out = ff_sws_lut3d_pick_pixfmt(dst, 1);
611
0
    if (fmt_in != src.format) {
612
0
        SwsFormat tmp = src;
613
0
        tmp.format = fmt_in;
614
0
        ret = add_convert_pass(graph, src, tmp, input, &input);
615
0
        if (ret < 0)
616
0
            return ret;
617
0
    }
618
619
0
    ret = ff_sws_lut3d_generate(lut, fmt_in, fmt_out, &map);
620
0
    if (ret < 0) {
621
0
        ff_sws_lut3d_free(&lut);
622
0
        return ret;
623
0
    }
624
625
0
    pass = ff_sws_graph_add_pass(graph, fmt_out, src.width, src.height,
626
0
                                 input, 1, lut, run_lut3d);
627
0
    if (!pass) {
628
0
        ff_sws_lut3d_free(&lut);
629
0
        return AVERROR(ENOMEM);
630
0
    }
631
0
    pass->setup = setup_lut3d;
632
0
    pass->free = free_lut3d;
633
634
0
    *output = pass;
635
0
    return 0;
636
0
}
637
638
/***************************************
639
 * Main filter graph construction code *
640
 ***************************************/
641
642
static int init_passes(SwsGraph *graph)
643
0
{
644
0
    SwsFormat src = graph->src;
645
0
    SwsFormat dst = graph->dst;
646
0
    SwsPass *pass = NULL; /* read from main input image */
647
0
    int ret;
648
649
0
    ret = adapt_colors(graph, src, dst, pass, &pass);
650
0
    if (ret < 0)
651
0
        return ret;
652
0
    src.format = pass ? pass->format : src.format;
653
0
    src.color  = dst.color;
654
655
0
    if (!ff_fmt_equal(&src, &dst)) {
656
0
        ret = add_convert_pass(graph, src, dst, pass, &pass);
657
0
        if (ret < 0)
658
0
            return ret;
659
0
    }
660
661
0
    if (!pass) {
662
        /* No passes were added, so no operations were necessary */
663
0
        graph->noop = 1;
664
665
        /* Add threaded memcpy pass */
666
0
        pass = ff_sws_graph_add_pass(graph, dst.format, dst.width, dst.height,
667
0
                                     pass, 1, NULL, run_copy);
668
0
        if (!pass)
669
0
            return AVERROR(ENOMEM);
670
0
    }
671
672
0
    return 0;
673
0
}
674
675
static void sws_graph_worker(void *priv, int jobnr, int threadnr, int nb_jobs,
676
                             int nb_threads)
677
0
{
678
0
    SwsGraph *graph = priv;
679
0
    const SwsPass *pass = graph->exec.pass;
680
0
    const SwsImg *input  = pass->input ? &pass->input->output : &graph->exec.input;
681
0
    const SwsImg *output = pass->output.fmt != AV_PIX_FMT_NONE ? &pass->output : &graph->exec.output;
682
0
    const int slice_y = jobnr * pass->slice_h;
683
0
    const int slice_h = FFMIN(pass->slice_h, pass->height - slice_y);
684
685
0
    pass->run(output, input, slice_y, slice_h, pass);
686
0
}
687
688
int ff_sws_graph_create(SwsContext *ctx, const SwsFormat *dst, const SwsFormat *src,
689
                        int field, SwsGraph **out_graph)
690
0
{
691
0
    int ret;
692
0
    SwsGraph *graph = av_mallocz(sizeof(*graph));
693
0
    if (!graph)
694
0
        return AVERROR(ENOMEM);
695
696
0
    graph->ctx = ctx;
697
0
    graph->src = *src;
698
0
    graph->dst = *dst;
699
0
    graph->field = field;
700
0
    graph->opts_copy = *ctx;
701
702
0
    graph->exec.input.fmt  = src->format;
703
0
    graph->exec.output.fmt = dst->format;
704
705
0
    ret = avpriv_slicethread_create(&graph->slicethread, (void *) graph,
706
0
                                    sws_graph_worker, NULL, ctx->threads);
707
0
    if (ret == AVERROR(ENOSYS))
708
0
        graph->num_threads = 1;
709
0
    else if (ret < 0)
710
0
        goto error;
711
0
    else
712
0
        graph->num_threads = ret;
713
714
0
    ret = init_passes(graph);
715
0
    if (ret < 0)
716
0
        goto error;
717
718
0
    *out_graph = graph;
719
0
    return 0;
720
721
0
error:
722
0
    ff_sws_graph_free(&graph);
723
0
    return ret;
724
0
}
725
726
void ff_sws_graph_free(SwsGraph **pgraph)
727
30.6k
{
728
30.6k
    SwsGraph *graph = *pgraph;
729
30.6k
    if (!graph)
730
30.6k
        return;
731
732
0
    avpriv_slicethread_free(&graph->slicethread);
733
734
0
    for (int i = 0; i < graph->num_passes; i++) {
735
0
        SwsPass *pass = graph->passes[i];
736
0
        if (pass->free)
737
0
            pass->free(pass->priv);
738
0
        if (pass->output.fmt != AV_PIX_FMT_NONE)
739
0
            av_free(pass->output.data[0]);
740
0
        av_free(pass);
741
0
    }
742
0
    av_free(graph->passes);
743
744
0
    av_free(graph);
745
0
    *pgraph = NULL;
746
0
}
747
748
/* Tests only options relevant to SwsGraph */
749
static int opts_equal(const SwsContext *c1, const SwsContext *c2)
750
0
{
751
0
    return c1->flags         == c2->flags         &&
752
0
           c1->threads       == c2->threads       &&
753
0
           c1->dither        == c2->dither        &&
754
0
           c1->alpha_blend   == c2->alpha_blend   &&
755
0
           c1->gamma_flag    == c2->gamma_flag    &&
756
0
           c1->src_h_chr_pos == c2->src_h_chr_pos &&
757
0
           c1->src_v_chr_pos == c2->src_v_chr_pos &&
758
0
           c1->dst_h_chr_pos == c2->dst_h_chr_pos &&
759
0
           c1->dst_v_chr_pos == c2->dst_v_chr_pos &&
760
0
           c1->intent        == c2->intent        &&
761
0
           !memcmp(c1->scaler_params, c2->scaler_params, sizeof(c1->scaler_params));
762
763
0
}
764
765
int ff_sws_graph_reinit(SwsContext *ctx, const SwsFormat *dst, const SwsFormat *src,
766
                        int field, SwsGraph **out_graph)
767
0
{
768
0
    SwsGraph *graph = *out_graph;
769
0
    if (graph && ff_fmt_equal(&graph->src, src) &&
770
0
                 ff_fmt_equal(&graph->dst, dst) &&
771
0
                 opts_equal(ctx, &graph->opts_copy))
772
0
    {
773
0
        ff_sws_graph_update_metadata(graph, &src->color);
774
0
        return 0;
775
0
    }
776
777
0
    ff_sws_graph_free(out_graph);
778
0
    return ff_sws_graph_create(ctx, dst, src, field, out_graph);
779
0
}
780
781
void ff_sws_graph_update_metadata(SwsGraph *graph, const SwsColor *color)
782
0
{
783
0
    if (!color)
784
0
        return;
785
786
0
    ff_color_update_dynamic(&graph->src.color, color);
787
0
}
788
789
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
0
{
794
0
    SwsImg *out = &graph->exec.output;
795
0
    SwsImg *in  = &graph->exec.input;
796
0
    memcpy(out->data,     out_data,     sizeof(out->data));
797
0
    memcpy(out->linesize, out_linesize, sizeof(out->linesize));
798
0
    memcpy(in->data,      in_data,      sizeof(in->data));
799
0
    memcpy(in->linesize,  in_linesize,  sizeof(in->linesize));
800
801
0
    for (int i = 0; i < graph->num_passes; i++) {
802
0
        const SwsPass *pass = graph->passes[i];
803
0
        graph->exec.pass = pass;
804
0
        if (pass->setup) {
805
0
            pass->setup(pass->output.fmt != AV_PIX_FMT_NONE ? &pass->output : out,
806
0
                        pass->input ? &pass->input->output : in, pass);
807
0
        }
808
0
        avpriv_slicethread_execute(graph->slicethread, pass->num_slices, 0);
809
0
    }
810
0
}