Coverage Report

Created: 2026-03-12 07:14

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