Coverage Report

Created: 2026-10-28 07:09

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/cpu.h"
23
#include "libavutil/error.h"
24
#include "libavutil/hwcontext.h"
25
#include "libavutil/imgutils.h"
26
#include "libavutil/macros.h"
27
#include "libavutil/mem.h"
28
#include "libavutil/opt.h"
29
#include "libavutil/pixdesc.h"
30
#include "libavutil/refstruct.h"
31
#include "libavutil/slicethread.h"
32
33
#include "libswscale/swscale.h"
34
#include "libswscale/format.h"
35
36
#include "cms.h"
37
#include "lut3d.h"
38
#include "swscale_internal.h"
39
#include "graph.h"
40
#include "ops.h"
41
#include "ops_dispatch.h"
42
#if CONFIG_SWSCALE_VULKAN
43
#include "vulkan/ops.h"
44
#endif
45
46
int ff_sws_pass_aligned_width(const SwsPass *pass, int width)
47
0
{
48
0
    if (!pass)
49
0
        return width;
50
51
0
    size_t aligned_w = width;
52
0
    aligned_w = FFALIGN(aligned_w, pass->output->width_align);
53
0
    aligned_w += pass->output->width_pad;
54
0
    return aligned_w <= INT_MAX ? aligned_w : width;
55
0
}
56
57
/* Allocates (or refs) one buffer per plane */
58
static int frame_alloc_planes_ref(AVFrame *dst, const AVFrame *src,
59
                                  const int plane_copy[4])
60
0
{
61
0
    int ret = av_image_check_size2(dst->width, dst->height, INT64_MAX,
62
0
                                   dst->format, 0, NULL);
63
0
    if (ret < 0)
64
0
        return ret;
65
66
0
    const int align = av_cpu_max_align();
67
0
    const int aligned_w = FFALIGN(dst->width + 1, align); /* add space for over-write */
68
0
    ret = av_image_fill_linesizes(dst->linesize, dst->format, aligned_w);
69
0
    if (ret < 0)
70
0
        return ret;
71
72
0
    ptrdiff_t linesize1[4];
73
0
    for (int i = 0; i < 4; i++)
74
0
        linesize1[i] = dst->linesize[i] = FFALIGN(dst->linesize[i], align);
75
76
0
    size_t sizes[4];
77
0
    ret = av_image_fill_plane_sizes(sizes, dst->format, dst->height, linesize1);
78
0
    if (ret < 0)
79
0
        return ret;
80
81
0
    for (int i = 0; i < 4; i++) {
82
0
        if (!sizes[i])
83
0
            break;
84
0
        int src_idx = plane_copy[i];
85
0
        if (src_idx >= 0 && src) {
86
            /* Ref the source plane instead of allocating a new buffer */
87
0
            dst->buf[i] = av_buffer_ref(src->buf[src_idx]);
88
0
            if (!dst->buf[i])
89
0
                return AVERROR(ENOMEM);
90
0
            dst->data[i]     = src->data[src_idx];
91
0
            dst->linesize[i] = src->linesize[src_idx];
92
0
            continue;
93
0
        }
94
95
0
        AVBufferRef *buf = av_buffer_alloc(sizes[i]);
96
0
        if (!buf)
97
0
            return AVERROR(ENOMEM);
98
0
        dst->data[i] = buf->data;
99
0
        dst->buf[i] = buf;
100
0
    }
101
102
0
    return 0;
103
0
}
104
105
#if CONFIG_SWSCALE_VULKAN
106
static int pass_alloc_output_hw(SwsPass *pass, AVFrame *avframe,
107
                                AVBufferRef *dev_ref)
108
{
109
    SwsPassBuffer *buffer = pass->output;
110
    AVBufferRef *frames_ref = av_hwframe_ctx_alloc(dev_ref);
111
    if (!frames_ref)
112
        return AVERROR(ENOMEM);
113
114
    AVHWFramesContext *hwfc = (AVHWFramesContext *)frames_ref->data;
115
    hwfc->format    = AV_PIX_FMT_VULKAN;
116
    hwfc->sw_format = pass->format;
117
    hwfc->width     = buffer->width;
118
    hwfc->height    = buffer->height;
119
120
    int ret = av_hwframe_ctx_init(frames_ref);
121
    if (ret >= 0) {
122
        avframe->format = AV_PIX_FMT_VULKAN;
123
        ret = av_hwframe_get_buffer(frames_ref, avframe, 0);
124
    }
125
    av_buffer_unref(&frames_ref);
126
    return ret;
127
}
128
#endif
129
130
static int pass_alloc_output(SwsPass *pass)
131
0
{
132
0
    if (!pass || pass->output->avframe)
133
0
        return 0;
134
135
0
    SwsPassBuffer *buffer = pass->output;
136
0
    AVFrame *avframe = av_frame_alloc();
137
0
    if (!avframe)
138
0
        return AVERROR(ENOMEM);
139
0
    avframe->width  = buffer->width;
140
0
    avframe->height = buffer->height;
141
142
0
    int ret;
143
144
#if CONFIG_SWSCALE_VULKAN
145
    const SwsGraph *graph = pass->graph;
146
    if (graph->src.hw_format == AV_PIX_FMT_VULKAN &&
147
        graph->dst.hw_format == AV_PIX_FMT_VULKAN) {
148
        AVBufferRef *dev_ref = ff_sws_vk_device_ref(graph->ctx);
149
        if (dev_ref) {
150
            ret = pass_alloc_output_hw(pass, avframe, dev_ref);
151
            if (ret >= 0)
152
                goto done;
153
            av_frame_unref(avframe);
154
        }
155
    }
156
#endif
157
158
0
    const AVFrame *src = NULL;
159
0
    if (pass->input)
160
0
        src = pass->input->output->avframe;
161
162
0
    avframe->format = pass->format;
163
0
    ret = frame_alloc_planes_ref(avframe, src, buffer->plane_copy);
164
0
    if (ret < 0) {
165
0
        av_frame_free(&avframe);
166
0
        return ret;
167
0
    }
168
169
#if CONFIG_SWSCALE_VULKAN
170
done:
171
#endif
172
0
    buffer->avframe = avframe;
173
0
    ff_sws_frame_from_avframe(&buffer->frame, avframe);
174
0
    return 0;
175
0
}
176
177
static void free_buffer(AVRefStructOpaque opaque, void *obj)
178
0
{
179
0
    SwsPassBuffer *buffer = obj;
180
0
    av_frame_free(&buffer->avframe);
181
0
}
182
183
static void pass_free(SwsPass *pass)
184
0
{
185
0
    if (pass->free)
186
0
        pass->free(pass->priv);
187
0
    av_refstruct_unref(&pass->output);
188
0
    av_free(pass);
189
0
}
190
191
int ff_sws_graph_add_pass(SwsGraph *graph, enum AVPixelFormat fmt,
192
                          int width, int height, SwsPass *input,
193
                          int lines, int align,
194
                          SwsPassFunc run, SwsPassSetup setup,
195
                          void *priv, void (*free_cb)(void *priv),
196
                          SwsPass **out_pass)
197
0
{
198
0
    int ret;
199
0
    SwsPass *pass = av_mallocz(sizeof(*pass));
200
0
    if (!pass) {
201
0
        if (free_cb)
202
0
            free_cb(priv);
203
0
        return AVERROR(ENOMEM);
204
0
    }
205
206
0
    if (!lines)
207
0
        lines = height;
208
209
0
    pass->graph  = graph;
210
0
    pass->run    = run;
211
0
    pass->setup  = setup;
212
0
    pass->priv   = priv;
213
0
    pass->free   = free_cb;
214
0
    pass->format = fmt;
215
0
    pass->lines  = lines;
216
0
    pass->input  = input;
217
0
    pass->output = av_refstruct_alloc_ext(sizeof(*pass->output), 0, NULL, free_buffer);
218
0
    if (!pass->output) {
219
0
        ret = AVERROR(ENOMEM);
220
0
        goto fail;
221
0
    }
222
223
0
    pass->output->height = height;
224
0
    pass->output->width  = width;
225
0
    pass->output->width_align = 1;
226
0
    memset(pass->output->plane_copy, -1, sizeof(pass->output->plane_copy));
227
228
0
    if (!align) {
229
0
        pass->slice_h = pass->lines;
230
0
        pass->num_slices = 1;
231
0
    } else {
232
0
        pass->slice_h = (pass->lines + graph->num_threads - 1) / graph->num_threads;
233
0
        pass->slice_h = FFALIGN(pass->slice_h, align);
234
0
        pass->num_slices = (pass->lines + pass->slice_h - 1) / pass->slice_h;
235
0
    }
236
237
0
    ret = av_dynarray_add_nofree(&graph->passes, &graph->num_passes, pass);
238
0
    if (ret < 0)
239
0
        goto fail;
240
241
0
    *out_pass = pass;
242
0
    return 0;
243
244
0
fail:
245
0
    pass_free(pass);
246
0
    return ret;
247
0
}
248
249
void ff_sws_pass_link_output(SwsPass *dst, const SwsPass *src)
250
0
{
251
0
    if (!dst || !src || dst == src)
252
0
        return;
253
254
0
    av_assert0(dst->format == src->format);
255
0
    SwsPassBuffer *keep = src->output, *drop = dst->output;
256
257
0
    av_assert1(keep->width  == drop->width);
258
0
    av_assert1(keep->height == drop->height);
259
0
    keep->width_align = FFMAX(keep->width_align, drop->width_align);
260
0
    keep->width_pad   = FFMAX(keep->width_pad,   drop->width_pad);
261
262
0
    for (int i = 0; i < FF_ARRAY_ELEMS(keep->plane_copy); i++) {
263
0
        if (keep->plane_copy[i] < 0)
264
0
            keep->plane_copy[i] = drop->plane_copy[i];
265
0
        else if (drop->plane_copy[i] >= 0)
266
0
            av_assert1(keep->plane_copy[i] == drop->plane_copy[i]);
267
0
    }
268
269
0
    av_refstruct_replace(&dst->output, src->output);
270
0
}
271
272
static void frame_shift(const SwsFrame *f, const int y, uint8_t *data[4])
273
0
{
274
0
    for (int i = 0; i < 4; i++) {
275
0
        if (f->data[i])
276
0
            data[i] = f->data[i] + (y >> ff_fmt_vshift(f->format, i)) * f->linesize[i];
277
0
        else
278
0
            data[i] = NULL;
279
0
    }
280
0
}
281
282
static void run_copy(const SwsFrame *out, const SwsFrame *in, int y, int h,
283
                     const SwsPass *pass)
284
0
{
285
0
    uint8_t *in_data[4], *out_data[4];
286
0
    frame_shift(in,  y, in_data);
287
0
    frame_shift(out, y, out_data);
288
289
0
    for (int i = 0; i < 4 && out_data[i]; i++) {
290
0
        const int lines = h >> ff_fmt_vshift(in->format, i);
291
0
        av_assert1(in_data[i]);
292
293
0
        if (i == 1 && av_pix_fmt_desc_get(in->format)->flags & AV_PIX_FMT_FLAG_PAL) {
294
0
            if (!y && in->data[1] != out->data[1])
295
0
                memcpy(out->data[1], in->data[1], AVPALETTE_SIZE);
296
0
            break;
297
0
        }
298
299
0
        if (in_data[i] == out_data[i]) {
300
0
            av_assert0(in->linesize[i] == out->linesize[i]);
301
0
        } else {
302
0
            ff_copyPlane(in_data[i], in->linesize[i], 0, lines,
303
0
                         av_image_get_linesize(out->format, out->width, i),
304
0
                         out_data[i], out->linesize[i]);
305
0
        }
306
0
    }
307
0
}
308
309
static void run_rgb0(const SwsFrame *out, const SwsFrame *in, int y, int h,
310
                     const SwsPass *pass)
311
0
{
312
0
    SwsInternal *c = pass->priv;
313
0
    const int x0 = c->src0Alpha - 1;
314
0
    const int w4 = 4 * out->width;
315
0
    const int src_stride = in->linesize[0];
316
0
    const int dst_stride = out->linesize[0];
317
0
    const uint8_t *src = in->data[0] + y * src_stride;
318
0
    uint8_t *dst = out->data[0] + y * dst_stride;
319
320
0
    for (int y = 0; y < h; y++) {
321
0
        memcpy(dst, src, w4 * sizeof(*dst));
322
0
        for (int x = x0; x < w4; x += 4)
323
0
            dst[x] = 0xFF;
324
325
0
        src += src_stride;
326
0
        dst += dst_stride;
327
0
    }
328
0
}
329
330
static void run_xyz2rgb(const SwsFrame *out, const SwsFrame *in, int y, int h,
331
                        const SwsPass *pass)
332
0
{
333
0
    const SwsInternal *c = pass->priv;
334
0
    c->xyz12Torgb48(c, out->data[0] + y * out->linesize[0], out->linesize[0],
335
0
                    in->data[0] + y * in->linesize[0], in->linesize[0],
336
0
                    out->width, h);
337
0
}
338
339
static void run_rgb2xyz(const SwsFrame *out, const SwsFrame *in, int y, int h,
340
                        const SwsPass *pass)
341
0
{
342
0
    const SwsInternal *c = pass->priv;
343
0
    c->rgb48Toxyz12(c, out->data[0] + y * out->linesize[0], out->linesize[0],
344
0
                    in->data[0] + y * in->linesize[0], in->linesize[0],
345
0
                    out->width, h);
346
0
}
347
348
/***********************************************************************
349
 * Internal ff_swscale() wrapper. This reuses the legacy scaling API. *
350
 * This is considered fully deprecated, and will be replaced by a full *
351
 * reimplementation ASAP.                                              *
352
 ***********************************************************************/
353
354
static void free_legacy_swscale(void *priv)
355
0
{
356
0
    SwsContext *sws = priv;
357
0
    sws_free_context(&sws);
358
0
}
359
360
static int setup_legacy_swscale(const SwsFrame *out, const SwsFrame *in,
361
                                const SwsPass *pass)
362
0
{
363
0
    SwsContext *sws = pass->priv;
364
0
    SwsInternal *c = sws_internal(sws);
365
0
    if (sws->flags & SWS_BITEXACT && sws->dither == SWS_DITHER_ED && c->dither_error[0]) {
366
0
        for (int i = 0; i < 4; i++)
367
0
            memset(c->dither_error[i], 0, sizeof(c->dither_error[0][0]) * (sws->dst_w + 2));
368
0
    }
369
370
0
    if (usePal(sws->src_format))
371
0
        ff_update_palette(c, (const uint32_t *) in->data[1]);
372
373
0
    return 0;
374
0
}
375
376
static inline SwsContext *slice_ctx(const SwsPass *pass, int y)
377
0
{
378
0
    SwsContext *sws = pass->priv;
379
0
    SwsInternal *parent = sws_internal(sws);
380
0
    if (pass->num_slices == 1)
381
0
        return sws;
382
383
0
    av_assert1(parent->nb_slice_ctx == pass->num_slices);
384
0
    sws = parent->slice_ctx[y / pass->slice_h];
385
386
0
    if (usePal(sws->src_format)) {
387
0
        SwsInternal *sub = sws_internal(sws);
388
0
        memcpy(sub->pal_yuv, parent->pal_yuv, sizeof(sub->pal_yuv));
389
0
        memcpy(sub->pal_rgb, parent->pal_rgb, sizeof(sub->pal_rgb));
390
0
    }
391
392
0
    return sws;
393
0
}
394
395
static void run_legacy_unscaled(const SwsFrame *out, const SwsFrame *in,
396
                                int y, int h, const SwsPass *pass)
397
0
{
398
0
    SwsContext *sws = slice_ctx(pass, y);
399
0
    SwsInternal *c = sws_internal(sws);
400
0
    uint8_t *in_data[4];
401
0
    frame_shift(in, y, in_data);
402
403
0
    c->convert_unscaled(c, (const uint8_t *const *) in_data, in->linesize, y, h,
404
0
                        out->data, out->linesize);
405
0
}
406
407
static void run_legacy_swscale(const SwsFrame *out, const SwsFrame *in,
408
                               int y, int h, const SwsPass *pass)
409
0
{
410
0
    SwsContext *sws = slice_ctx(pass, y);
411
0
    SwsInternal *c = sws_internal(sws);
412
0
    uint8_t *out_data[4];
413
0
    frame_shift(out, y, out_data);
414
415
0
    ff_swscale(c, (const uint8_t *const *) in->data, in->linesize, 0,
416
0
               sws->src_h, out_data, out->linesize, y, h);
417
0
}
418
419
static void run_legacy_lut3d(const SwsFrame *out, const SwsFrame *in,
420
                             int y, int h, const SwsPass *pass)
421
0
{
422
0
    const SwsLut3D *lut = pass->graph->lut3d;
423
0
    uint8_t *in_data[4], *out_data[4];
424
0
    frame_shift(in,  y, in_data);
425
0
    frame_shift(out, y, out_data);
426
427
0
    ff_sws_lut3d_apply_rgba64(lut, in_data[0], in->linesize[0], out_data[0],
428
0
                              out->linesize[0], out->width, h);
429
0
}
430
431
static void legacy_chr_pos(SwsGraph *graph, int *chr_pos, int override, int *warned)
432
0
{
433
0
    if (override == -513 || override == *chr_pos)
434
0
        return;
435
436
0
    if (!*warned) {
437
0
        av_log(NULL, AV_LOG_WARNING,
438
0
               "Setting chroma position directly is deprecated, make sure "
439
0
               "the frame is tagged with the correct chroma location.\n");
440
0
        *warned = 1;
441
0
    }
442
443
0
    *chr_pos = override;
444
0
}
445
446
/* Takes over ownership of `sws` */
447
static int init_legacy_subpass(SwsGraph *graph, SwsContext *sws,
448
                               SwsPass *input, SwsPass **output)
449
0
{
450
0
    SwsInternal *c = sws_internal(sws);
451
0
    const int src_w = sws->src_w, src_h = sws->src_h;
452
0
    const int dst_w = sws->dst_w, dst_h = sws->dst_h;
453
0
    const int unscaled = src_w == dst_w && src_h == dst_h;
454
0
    int align = c->dst_slice_align;
455
0
    SwsPass *pass = NULL;
456
0
    int ret;
457
458
0
    if (c->cascaded_context[0]) {
459
0
        const int num_cascaded = c->cascaded_context[2] ? 3 : 2;
460
0
        for (int i = 0; i < num_cascaded; i++) {
461
0
            const int is_last = i + 1 == num_cascaded;
462
463
            /* Steal cascaded context, so we can manage its lifetime independently */
464
0
            SwsContext *sub = c->cascaded_context[i];
465
0
            c->cascaded_context[i] = NULL;
466
467
0
            ret = init_legacy_subpass(graph, sub, input, is_last ? output : &input);
468
0
            if (ret < 0)
469
0
                break;
470
0
        }
471
472
0
        sws_free_context(&sws);
473
0
        return ret;
474
0
    }
475
476
0
    if (sws->dither == SWS_DITHER_ED && !c->convert_unscaled)
477
0
        align = 0; /* disable slice threading */
478
479
0
    if (c->src0Alpha && !c->dst0Alpha && isALPHA(sws->dst_format)) {
480
0
        ret = ff_sws_graph_add_pass(graph, AV_PIX_FMT_RGBA, src_w, src_h, input,
481
0
                                    0, 1, run_rgb0, NULL, c, NULL, &input);
482
0
        if (ret < 0) {
483
0
            sws_free_context(&sws);
484
0
            return ret;
485
0
        }
486
0
    }
487
488
0
    if (c->srcXYZ && !(c->dstXYZ && unscaled)) {
489
0
        ret = ff_sws_graph_add_pass(graph, AV_PIX_FMT_RGB48, src_w, src_h, input,
490
0
                                    0, 1, run_xyz2rgb, NULL, c, NULL, &input);
491
0
        if (ret < 0) {
492
0
            sws_free_context(&sws);
493
0
            return ret;
494
0
        }
495
0
    }
496
497
0
    ret = ff_sws_graph_add_pass(graph, sws->dst_format, dst_w, dst_h, input, 0, align,
498
0
                                c->convert_unscaled ? run_legacy_unscaled : run_legacy_swscale,
499
0
                                setup_legacy_swscale, sws, free_legacy_swscale, &pass);
500
0
    if (ret < 0)
501
0
        return ret;
502
0
    pass->backend = SWS_BACKEND_LEGACY;
503
504
    /**
505
     * For slice threading, we need to create sub contexts, similar to how
506
     * swscale normally handles it internally. The most important difference
507
     * is that we handle cascaded contexts before threaded contexts; whereas
508
     * context_init_threaded() does it the other way around.
509
     */
510
511
0
    if (pass->num_slices > 1) {
512
0
        c->slice_ctx = av_calloc(pass->num_slices, sizeof(*c->slice_ctx));
513
0
        if (!c->slice_ctx)
514
0
            return AVERROR(ENOMEM);
515
516
0
        for (int i = 0; i < pass->num_slices; i++) {
517
0
            SwsContext *slice;
518
0
            SwsInternal *c2;
519
0
            slice = c->slice_ctx[i] = sws_alloc_context();
520
0
            if (!slice)
521
0
                return AVERROR(ENOMEM);
522
0
            c->nb_slice_ctx++;
523
524
0
            c2 = sws_internal(slice);
525
0
            c2->parent = sws;
526
527
0
            ret = av_opt_copy(slice, sws);
528
0
            if (ret < 0)
529
0
                return ret;
530
531
0
            ret = ff_sws_init_single_context(slice, NULL, NULL);
532
0
            if (ret < 0)
533
0
                return ret;
534
535
0
            sws_setColorspaceDetails(slice, c->srcColorspaceTable,
536
0
                                     slice->src_range, c->dstColorspaceTable,
537
0
                                     slice->dst_range, c->brightness, c->contrast,
538
0
                                     c->saturation);
539
540
0
            for (int i = 0; i < FF_ARRAY_ELEMS(c->srcColorspaceTable); i++) {
541
0
                c2->srcColorspaceTable[i] = c->srcColorspaceTable[i];
542
0
                c2->dstColorspaceTable[i] = c->dstColorspaceTable[i];
543
0
            }
544
0
        }
545
0
    }
546
547
0
    if (c->dstXYZ && !(c->srcXYZ && unscaled)) {
548
0
        ret = ff_sws_graph_add_pass(graph, AV_PIX_FMT_RGB48, dst_w, dst_h, pass,
549
0
                                    0, 1, run_rgb2xyz, NULL, c, NULL, &pass);
550
0
        if (ret < 0)
551
0
            return ret;
552
0
    }
553
554
0
    *output = pass;
555
0
    return 0;
556
0
}
557
558
static int add_legacy_3dlut_pass(SwsGraph *graph, const SwsFormat *src,
559
                                 SwsPass *input, SwsPass **output);
560
561
static int add_legacy_sws_pass(SwsGraph *graph, const SwsFormat *src,
562
                               const SwsFormat *dst, const SwsLut3D *lut3d,
563
                               SwsPass *input, SwsPass **output)
564
0
{
565
0
    int ret, warned = 0;
566
0
    SwsContext *const ctx = graph->ctx;
567
0
    const SwsBackend backend = ff_sws_enabled_backends(ctx);
568
0
    if (!(backend & SWS_BACKEND_LEGACY))
569
0
        return AVERROR(ENOTSUP);
570
0
    if (src->hw_format != AV_PIX_FMT_NONE || dst->hw_format != AV_PIX_FMT_NONE)
571
0
        return AVERROR(ENOTSUP);
572
573
    /* Re-check this here because this might not be excluded if the caller was
574
     * testing against multiple backends */
575
0
    if (!sws_isSupportedInput(src->format) || !sws_isSupportedOutput(dst->format))
576
0
        return AVERROR(ENOTSUP);
577
578
    /* If we need to apply a 3D LUT, add it as an explicit input prepass */
579
0
    if (lut3d) {
580
0
        ret = add_legacy_3dlut_pass(graph, src, input, &input);
581
0
        if (ret < 0)
582
0
            return ret;
583
584
0
        SwsFormat tmp = *src;
585
0
        tmp.format = input->format;
586
0
        tmp.color  = lut3d->map.dst;
587
0
        return add_legacy_sws_pass(graph, &tmp, dst, NULL, input, output);
588
0
    }
589
590
0
    SwsContext *sws = sws_alloc_context();
591
0
    if (!sws)
592
0
        return AVERROR(ENOMEM);
593
594
0
    sws->flags       = ctx->flags;
595
0
    sws->dither      = ctx->dither;
596
0
    sws->alpha_blend = ctx->alpha_blend;
597
0
    sws->gamma_flag  = ctx->gamma_flag;
598
0
    sws->scaler      = ctx->scaler;
599
0
    sws->scaler_sub  = ctx->scaler_sub;
600
601
0
    sws->src_w       = src->width;
602
0
    sws->src_h       = src->height;
603
0
    sws->src_format  = src->format;
604
0
    sws->src_range   = src->range == AVCOL_RANGE_JPEG;
605
606
0
    sws->dst_w      = dst->width;
607
0
    sws->dst_h      = dst->height;
608
0
    sws->dst_format = dst->format;
609
0
    sws->dst_range  = dst->range == AVCOL_RANGE_JPEG;
610
0
    ff_sws_chroma_pos(src, &graph->incomplete, &sws->src_h_chr_pos, &sws->src_v_chr_pos);
611
0
    ff_sws_chroma_pos(dst, &graph->incomplete, &sws->dst_h_chr_pos, &sws->dst_v_chr_pos);
612
613
0
    graph->incomplete |= src->range == AVCOL_RANGE_UNSPECIFIED;
614
0
    graph->incomplete |= dst->range == AVCOL_RANGE_UNSPECIFIED;
615
616
    /* Allow overriding chroma position with the legacy API */
617
0
    legacy_chr_pos(graph, &sws->src_h_chr_pos, ctx->src_h_chr_pos, &warned);
618
0
    legacy_chr_pos(graph, &sws->src_v_chr_pos, ctx->src_v_chr_pos, &warned);
619
0
    legacy_chr_pos(graph, &sws->dst_h_chr_pos, ctx->dst_h_chr_pos, &warned);
620
0
    legacy_chr_pos(graph, &sws->dst_v_chr_pos, ctx->dst_v_chr_pos, &warned);
621
622
    /* Explicitly strip chroma offsets when not subsampling, because it
623
     * interferes with the operation of flags like SWS_FULL_CHR_H_INP */
624
0
    if (!src->desc->log2_chroma_w)
625
0
        sws->src_h_chr_pos = -513;
626
0
    if (!src->desc->log2_chroma_h)
627
0
        sws->src_v_chr_pos = -513;
628
0
    if (!dst->desc->log2_chroma_w)
629
0
        sws->dst_h_chr_pos = -513;
630
0
    if (!dst->desc->log2_chroma_h)
631
0
        sws->dst_v_chr_pos = -513;
632
633
0
    for (int i = 0; i < SWS_NUM_SCALER_PARAMS; i++)
634
0
        sws->scaler_params[i] = ctx->scaler_params[i];
635
636
0
    ret = sws_init_context(sws, NULL, NULL);
637
0
    if (ret < 0) {
638
0
        sws_free_context(&sws);
639
0
        return ret;
640
0
    }
641
642
    /* Set correct color matrices */
643
0
    {
644
0
        int in_full, out_full, brightness, contrast, saturation;
645
0
        const int *inv_table, *table;
646
0
        sws_getColorspaceDetails(sws, (int **)&inv_table, &in_full,
647
0
                                (int **)&table, &out_full,
648
0
                                &brightness, &contrast, &saturation);
649
650
0
        inv_table = sws_getCoefficients(src->csp);
651
0
        table     = sws_getCoefficients(dst->csp);
652
653
0
        graph->incomplete |= src->csp != dst->csp &&
654
0
                            (src->csp == AVCOL_SPC_UNSPECIFIED ||
655
0
                             dst->csp == AVCOL_SPC_UNSPECIFIED);
656
657
0
        sws_setColorspaceDetails(sws, inv_table, in_full, table, out_full,
658
0
                                brightness, contrast, saturation);
659
0
    }
660
661
0
    return init_legacy_subpass(graph, sws, input, output);
662
0
}
663
664
static int add_legacy_3dlut_pass(SwsGraph *graph, const SwsFormat *src,
665
                                 SwsPass *input, SwsPass **output)
666
0
{
667
0
    int ret;
668
669
0
    const SwsLut3D *lut3d = graph->lut3d;
670
0
    if (!lut3d)
671
0
        return 0;
672
673
0
    const enum AVPixelFormat fmt = AV_PIX_FMT_RGBA64;
674
0
    if (src->format != fmt) {
675
0
        SwsFormat tmp = *src;
676
0
        tmp.format = fmt;
677
0
        ret = add_legacy_sws_pass(graph, src, &tmp, NULL, input, &input);
678
0
        if (ret < 0)
679
0
            return ret;
680
0
    }
681
682
0
    ret = ff_sws_graph_add_pass(graph, fmt, src->width, src->height,
683
0
                                input, 0, 1, run_legacy_lut3d, NULL, NULL, NULL,
684
0
                                output);
685
0
    if (ret < 0)
686
0
        return ret;
687
688
0
    return 0;
689
0
}
690
691
/*********************************
692
 * Format conversion and scaling *
693
 *********************************/
694
695
static int add_ops_convert_pass(SwsGraph *graph, const SwsFormat *src,
696
                                const SwsFormat *dst, const SwsLut3D *lut3d,
697
                                SwsPass *input, SwsPass **output)
698
0
{
699
0
#if CONFIG_UNSTABLE
700
0
    SwsContext *ctx = graph->ctx;
701
702
    /* Preemptively skip the ops list generation if the backend was
703
     * constrained to the legacy implementation only. This would
704
     * normally also fail in ff_sws_compile_pass() with the same
705
     * error, but this way saves a bit of unnecessary overhead */
706
0
    const SwsBackend backends = ff_sws_enabled_backends(ctx);
707
0
    if (backends == SWS_BACKEND_LEGACY)
708
0
        return AVERROR(ENOTSUP);
709
710
0
    SwsOpList *ops;
711
0
    int ret = ff_sws_op_list_generate(ctx, src, dst, lut3d, &ops, &graph->incomplete);
712
0
    if (ret < 0)
713
0
        return ret;
714
715
0
    av_log(ctx, AV_LOG_VERBOSE, "Conversion pass for %s -> %s:\n",
716
0
           av_get_pix_fmt_name(src->format), av_get_pix_fmt_name(dst->format));
717
718
0
    av_log(ctx, AV_LOG_DEBUG, "Unoptimized operation list:\n");
719
0
    ff_sws_op_list_update(ops);
720
0
    ff_sws_op_list_print(ctx, AV_LOG_DEBUG, AV_LOG_TRACE, ops);
721
722
0
    const int flags = SWS_OP_FLAG_OPTIMIZE | SWS_OP_FLAG_SPLIT_MEMCPY;
723
0
    return ff_sws_compile_pass(graph, NULL, &ops, flags, input, output);
724
#else
725
    return AVERROR(ENOTSUP);
726
#endif
727
0
}
728
729
static bool prefer_ops_backend(SwsContext *ctx, const SwsFormat *src, const SwsFormat *dst)
730
0
{
731
0
    if (ctx->flags & SWS_UNSTABLE)
732
0
        return true;
733
0
    if (isFloat(src->format) || isFloat(dst->format))
734
0
        return true; /* ops backend has better support for float formats */
735
0
    return false; /* default to legacy for stability reasons */
736
0
}
737
738
static int add_convert_pass(SwsGraph *graph, const SwsFormat *src,
739
                            const SwsFormat *dst, const SwsLut3D *lut3d,
740
                            SwsPass *input, SwsPass **output)
741
0
{
742
0
    SwsContext *ctx = graph->ctx;
743
0
    int ret;
744
745
0
    if (prefer_ops_backend(ctx, src, dst)) {
746
0
        ret = add_ops_convert_pass(graph, src, dst, lut3d, input, output);
747
0
        if (ret == AVERROR(ENOTSUP))
748
0
            ret = add_legacy_sws_pass(graph, src, dst, lut3d, input, output);
749
0
    } else {
750
0
        ret = add_legacy_sws_pass(graph, src, dst, lut3d, input, output);
751
0
        if (ret == AVERROR(ENOTSUP))
752
0
            ret = add_ops_convert_pass(graph, src, dst, lut3d, input, output);
753
0
    }
754
755
0
    return ret;
756
0
}
757
758
/**************************
759
 * Gamut and tone mapping *
760
 **************************/
761
762
static int generate_3dlut(SwsGraph *graph, SwsFormat *src, SwsFormat *dst)
763
0
{
764
0
    SwsColorMap map = {0};
765
766
    /**
767
     * Grayspace does not really have primaries, so just force the use of
768
     * the equivalent other primary set to avoid a conversion. Technically,
769
     * this does affect the weights used for the Grayscale conversion, but
770
     * in practise, that should give the expected results more often than not.
771
     */
772
0
    if (isGray(dst->format)) {
773
0
        dst->color = src->color;
774
0
    } else if (isGray(src->format)) {
775
0
        src->color = dst->color;
776
0
    }
777
778
    /* Fully infer color spaces before color mapping logic */
779
0
    graph->incomplete |= ff_infer_colors(&src->color, &dst->color);
780
781
0
    map.intent = graph->ctx->intent;
782
0
    map.src    = src->color;
783
0
    map.dst    = dst->color;
784
785
0
    if (ff_sws_color_map_noop(&map))
786
0
        return 0;
787
788
0
    if (src->hw_format != AV_PIX_FMT_NONE || dst->hw_format != AV_PIX_FMT_NONE)
789
0
        return AVERROR(ENOTSUP);
790
791
0
    graph->lut3d = ff_sws_lut3d_alloc();
792
0
    if (!graph->lut3d)
793
0
        return AVERROR(ENOMEM);
794
795
0
    return ff_sws_lut3d_generate(graph->lut3d, &map);
796
0
}
797
798
/***************************************
799
 * Main filter graph construction code *
800
 ***************************************/
801
802
static int init_passes(SwsGraph *graph)
803
0
{
804
0
    SwsFormat src = graph->src;
805
0
    SwsFormat dst = graph->dst;
806
0
    SwsPass *pass = NULL; /* read from main input image */
807
0
    int ret;
808
809
0
    graph->incomplete |= ff_infer_chroma_loc(&src, &dst);
810
811
0
    ret = generate_3dlut(graph, &src, &dst);
812
0
    if (ret < 0)
813
0
        return ret;
814
815
0
    if (!ff_fmt_equal(&src, &dst) || graph->lut3d) {
816
0
        ret = add_convert_pass(graph, &src, &dst, graph->lut3d, pass, &pass);
817
0
        if (ret < 0)
818
0
            return ret;
819
0
    }
820
821
0
    if (!pass) {
822
        /* Plane passthrough is not implemented for hardware frames */
823
0
        if (src.hw_format != AV_PIX_FMT_NONE || dst.hw_format != AV_PIX_FMT_NONE)
824
0
            return AVERROR(ENOTSUP);
825
826
        /* No passes were added, so no operations were necessary */
827
0
        graph->noop = 1;
828
829
0
        const int nb_planes = av_pix_fmt_count_planes(dst.format);
830
0
        for (int i = 0; i < nb_planes; i++)
831
0
            graph->plane_copy[i] = i;
832
833
        /* Add threaded memcpy pass */
834
0
        return ff_sws_graph_add_pass(graph, dst.format, dst.width, dst.height,
835
0
                                    pass, 0, 1, run_copy, NULL, NULL, NULL, &pass);
836
0
    }
837
838
    /* Compute end-to-end plane copy map */
839
0
    for (int n = 0; n < graph->num_passes; n++) {
840
0
        const SwsPass *pass = graph->passes[n];
841
        /* This pass writes to an output buffer other than the image
842
         * output, or copies from the output of a different pass */
843
0
        if (pass->output->avframe || pass->input)
844
0
            continue;
845
0
        for (int i = 0; i < FF_ARRAY_ELEMS(graph->plane_copy); i++) {
846
0
            const int idx = pass->output->plane_copy[i];
847
0
            if (idx < 0)
848
0
                continue;
849
0
            if (graph->plane_copy[i] < 0) {
850
0
                graph->plane_copy[i] = idx;
851
0
                av_log(graph->ctx, AV_LOG_DEBUG, "Plane %d passthrough from "
852
0
                       "plane %d\n", i, idx);
853
0
            } else {
854
0
                av_assert0(graph->plane_copy[i] == idx);
855
0
            }
856
0
        }
857
0
    }
858
859
0
    return 0;
860
0
}
861
862
static int sws_graph_worker(void *priv, int jobnr, int threadnr, int nb_jobs,
863
                            int nb_threads)
864
0
{
865
0
    SwsGraph *graph = priv;
866
0
    const SwsPass *pass = graph->exec.pass;
867
0
    const int slice_y = jobnr * pass->slice_h;
868
0
    const int slice_h = FFMIN(pass->slice_h, pass->lines - slice_y);
869
870
0
    pass->run(graph->exec.output, graph->exec.input, slice_y, slice_h, pass);
871
0
    return 0;
872
0
}
873
874
SwsGraph *ff_sws_graph_alloc(void)
875
0
{
876
0
    return av_mallocz(sizeof(SwsGraph));
877
0
}
878
879
static void graph_uninit(SwsGraph *graph)
880
0
{
881
0
    avpriv_slicethread_free(&graph->slicethread);
882
883
0
    for (int i = 0; i < graph->num_passes; i++)
884
0
        pass_free(graph->passes[i]);
885
0
    av_free(graph->passes);
886
887
0
    av_refstruct_unref(&graph->lut3d);
888
889
0
    memset(graph, 0, sizeof(*graph));
890
0
}
891
892
int ff_sws_graph_init(SwsGraph *graph, SwsContext *ctx, const SwsFormat *dst,
893
                      const SwsFormat *src)
894
0
{
895
0
    int ret;
896
0
    if (graph->ctx) {
897
0
        av_log(ctx, AV_LOG_ERROR, "Graph is already initialized\n");
898
0
        return AVERROR(EINVAL);
899
0
    }
900
901
0
    graph->ctx = ctx;
902
0
    graph->src = *src;
903
0
    graph->dst = *dst;
904
0
    graph->opts_copy = *ctx;
905
0
    av_assert0(src->interlaced == dst->interlaced);
906
0
    av_assert0(src->field      == dst->field);
907
0
    memset(graph->plane_copy, -1, sizeof(graph->plane_copy));
908
909
0
    if (ctx->threads == 1) {
910
0
        graph->num_threads = 1;
911
0
    } else {
912
0
        ret = avpriv_slicethread_create2(&graph->slicethread, (void *) graph,
913
0
                                         sws_graph_worker, NULL, ctx->threads);
914
0
        if (ret == AVERROR(ENOSYS)) {
915
            /* Fall back to single threaded operation */
916
0
            graph->num_threads = 1;
917
0
        } else if (ret < 0) {
918
0
            goto error;
919
0
        } else {
920
0
            graph->num_threads = ret;
921
0
        }
922
0
    }
923
924
0
    ret = init_passes(graph);
925
0
    if (ret < 0)
926
0
        goto error;
927
928
    /* Resolve output buffers for all intermediate passes */
929
0
    for (int i = 0; i < graph->num_passes; i++) {
930
0
        graph->backend |= graph->passes[i]->backend;
931
0
        ret = pass_alloc_output(graph->passes[i]->input);
932
0
        if (ret < 0)
933
0
            goto error;
934
0
    }
935
936
0
    return 0;
937
938
0
error:
939
0
    graph_uninit(graph);
940
0
    return ret;
941
0
}
942
943
void ff_sws_graph_rollback(SwsGraph *graph, int since_idx)
944
0
{
945
0
    for (int i = since_idx; i < graph->num_passes; i++)
946
0
        pass_free(graph->passes[i]);
947
0
    graph->num_passes = since_idx;
948
0
}
949
950
void ff_sws_graph_free(SwsGraph **pgraph)
951
0
{
952
0
    SwsGraph *graph = *pgraph;
953
0
    if (!graph)
954
0
        return;
955
956
0
    graph_uninit(graph);
957
0
    av_free(graph);
958
0
    *pgraph = NULL;
959
0
}
960
961
/* Tests only options relevant to SwsGraph */
962
static int opts_equal(const SwsContext *c1, const SwsContext *c2)
963
0
{
964
0
    return c1->flags         == c2->flags         &&
965
0
           c1->threads       == c2->threads       &&
966
0
           c1->dither        == c2->dither        &&
967
0
           c1->alpha_blend   == c2->alpha_blend   &&
968
0
           c1->gamma_flag    == c2->gamma_flag    &&
969
0
           c1->src_h_chr_pos == c2->src_h_chr_pos &&
970
0
           c1->src_v_chr_pos == c2->src_v_chr_pos &&
971
0
           c1->dst_h_chr_pos == c2->dst_h_chr_pos &&
972
0
           c1->dst_v_chr_pos == c2->dst_v_chr_pos &&
973
0
           c1->intent        == c2->intent        &&
974
0
           c1->scaler        == c2->scaler        &&
975
0
           c1->scaler_sub    == c2->scaler_sub    &&
976
0
           c1->backends      == c2->backends      &&
977
0
           !memcmp(c1->scaler_params, c2->scaler_params, sizeof(c1->scaler_params));
978
979
0
}
980
981
int ff_sws_graph_reinit(SwsGraph *graph, SwsContext *ctx, const SwsFormat *dst,
982
                        const SwsFormat *src)
983
0
{
984
0
    if (ff_fmt_equal(&graph->src, src) && ff_fmt_equal(&graph->dst, dst) &&
985
0
        opts_equal(ctx, &graph->opts_copy))
986
0
    {
987
0
        ff_sws_graph_update_metadata(graph, &src->color);
988
0
        return 0;
989
0
    }
990
991
0
    graph_uninit(graph);
992
0
    return ff_sws_graph_init(graph, ctx, dst, src);
993
0
}
994
995
void ff_sws_graph_update_metadata(SwsGraph *graph, const SwsColor *color)
996
0
{
997
0
    if (!color)
998
0
        return;
999
1000
0
    ff_color_update_dynamic(&graph->src.color, color);
1001
1002
0
    if (graph->lut3d)
1003
0
        ff_sws_lut3d_update(graph->lut3d, &graph->src.color);
1004
0
}
1005
1006
static void get_field(SwsGraph *graph, const SwsFormat *fmt,
1007
                      const AVFrame *avframe, SwsFrame *frame)
1008
0
{
1009
0
    ff_sws_frame_from_avframe(frame, avframe);
1010
1011
0
    if (!(avframe->flags & AV_FRAME_FLAG_INTERLACED)) {
1012
0
        av_assert1(!fmt->field);
1013
0
        return;
1014
0
    }
1015
1016
0
    if (fmt->field == FIELD_BOTTOM) {
1017
        /* Odd rows, offset by one line */
1018
0
        const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
1019
0
        for (int i = 0; i < 4; i++) {
1020
0
            if (frame->data[i])
1021
0
                frame->data[i] += frame->linesize[i];
1022
0
            if (desc->flags & AV_PIX_FMT_FLAG_PAL)
1023
0
                break;
1024
0
        }
1025
0
    }
1026
1027
    /* Take only every second line */
1028
0
    for (int i = 0; i < 4; i++)
1029
0
        frame->linesize[i] <<= 1;
1030
1031
0
    frame->height = (frame->height + (fmt->field == FIELD_TOP)) >> 1;
1032
0
}
1033
1034
int ff_sws_graph_run(SwsGraph *graph, const AVFrame *dst, const AVFrame *src)
1035
0
{
1036
0
    av_assert0(dst->format == graph->dst.hw_format || dst->format == graph->dst.format);
1037
0
    av_assert0(src->format == graph->src.hw_format || src->format == graph->src.format);
1038
1039
0
    SwsFrame src_field, dst_field;
1040
0
    get_field(graph, &graph->dst, dst, &dst_field);
1041
0
    get_field(graph, &graph->src, src, &src_field);
1042
1043
0
    for (int i = 0; i < graph->num_passes; i++) {
1044
0
        const SwsPass *pass = graph->passes[i];
1045
0
        graph->exec.pass   = pass;
1046
0
        graph->exec.input  = pass->input ? &pass->input->output->frame : &src_field;
1047
0
        graph->exec.output = pass->output->avframe ? &pass->output->frame : &dst_field;
1048
0
        if (pass->setup) {
1049
0
            int ret = pass->setup(graph->exec.output, graph->exec.input, pass);
1050
0
            if (ret < 0)
1051
0
                return ret;
1052
0
        }
1053
1054
0
        if (pass->num_slices == 1) {
1055
0
            pass->run(graph->exec.output, graph->exec.input, 0, pass->lines, pass);
1056
0
        } else {
1057
0
            avpriv_slicethread_execute2(graph->slicethread, pass->num_slices, 0);
1058
0
        }
1059
0
    }
1060
1061
0
    return 0;
1062
0
}