Coverage Report

Created: 2026-04-29 07:00

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