FFmpeg
Loading...
Searching...
No Matches
graph.c
Go to the documentation of this file.
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"
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
47{
48 if (!pass)
49 return width;
50
51 size_t aligned_w = width;
52 aligned_w = FFALIGN(aligned_w, pass->output->width_align);
53 aligned_w += pass->output->width_pad;
54 return aligned_w <= INT_MAX ? aligned_w : width;
55}
56
57/* Allocates (or refs) one buffer per plane */
58static int frame_alloc_planes_ref(AVFrame *dst, int padding[4],
59 const AVFrame *src, const int plane_copy[4])
60{
61 int ret = av_image_check_size2(dst->width, dst->height, INT64_MAX,
62 dst->format, 0, NULL);
63 if (ret < 0)
64 return ret;
65
66 const int align = av_cpu_max_align();
67 const int aligned_w = FFALIGN(dst->width + 1, align); /* add space for over-write */
68 ret = av_image_fill_linesizes(dst->linesize, dst->format, aligned_w);
69 if (ret < 0)
70 return ret;
71
72 ptrdiff_t linesize1[4];
73 for (int i = 0; i < 4; i++)
74 linesize1[i] = dst->linesize[i] = FFALIGN(dst->linesize[i], align);
75
76 size_t sizes[4];
77 ret = av_image_fill_plane_sizes(sizes, dst->format, dst->height, linesize1);
78 if (ret < 0)
79 return ret;
80
81 for (int i = 0; i < 4; i++) {
82 if (!sizes[i])
83 break;
84 int src_idx = plane_copy[i];
85 if (src_idx >= 0 && src) {
86 /* Ref the source plane instead of allocating a new buffer */
87 dst->buf[i] = av_buffer_ref(src->buf[src_idx]);
88 if (!dst->buf[i])
89 return AVERROR(ENOMEM);
90 dst->data[i] = src->data[src_idx];
91 dst->linesize[i] = src->linesize[src_idx];
92 continue;
93 }
94
96 if (!buf)
97 return AVERROR(ENOMEM);
98 dst->data[i] = buf->data;
99 dst->buf[i] = buf;
100 }
101
102 for (int i = 0; i < 4 && dst->data[i]; i++) {
103 padding[i] = dst->linesize[i] -
104 av_image_get_linesize(dst->format, dst->width, i);
105 }
106
107 return 0;
108}
109
110#if CONFIG_SWSCALE_VULKAN
111static int pass_alloc_output_hw(SwsPass *pass, AVFrame *avframe,
112 AVBufferRef *dev_ref)
113{
114 SwsPassBuffer *buffer = pass->output;
115 AVBufferRef *frames_ref = av_hwframe_ctx_alloc(dev_ref);
116 if (!frames_ref)
117 return AVERROR(ENOMEM);
118
119 AVHWFramesContext *hwfc = (AVHWFramesContext *)frames_ref->data;
120 hwfc->format = AV_PIX_FMT_VULKAN;
121 hwfc->sw_format = pass->format;
122 hwfc->width = buffer->width;
123 hwfc->height = buffer->height;
124
125 int ret = av_hwframe_ctx_init(frames_ref);
126 if (ret >= 0) {
127 avframe->format = AV_PIX_FMT_VULKAN;
128 ret = av_hwframe_get_buffer(frames_ref, avframe, 0);
129 }
130 av_buffer_unref(&frames_ref);
131 return ret;
132}
133#endif
134
135static int pass_alloc_output(SwsPass *pass)
136{
137 if (!pass || pass->output->avframe)
138 return 0;
139
140 SwsPassBuffer *buffer = pass->output;
141 AVFrame *avframe = av_frame_alloc();
142 if (!avframe)
143 return AVERROR(ENOMEM);
144 avframe->width = buffer->width;
145 avframe->height = buffer->height;
146
147 int ret;
148 int padding[4] = {0};
149
150#if CONFIG_SWSCALE_VULKAN
151 const SwsGraph *graph = pass->graph;
152 if (graph->src.hw_format == AV_PIX_FMT_VULKAN &&
153 graph->dst.hw_format == AV_PIX_FMT_VULKAN) {
154 AVBufferRef *dev_ref = ff_sws_vk_device_ref(graph->ctx);
155 if (dev_ref) {
156 ret = pass_alloc_output_hw(pass, avframe, dev_ref);
157 if (ret >= 0)
158 goto done;
159 av_frame_unref(avframe);
160 }
161 }
162#endif
163
164 const AVFrame *src = NULL;
165 if (pass->input)
166 src = pass->input->output->avframe;
167
168 avframe->format = pass->format;
169 ret = frame_alloc_planes_ref(avframe, padding, src, buffer->plane_copy);
170 if (ret < 0) {
171 av_frame_free(&avframe);
172 return ret;
173 }
174
175#if CONFIG_SWSCALE_VULKAN
176done:
177#endif
178 buffer->avframe = avframe;
179 ff_sws_frame_from_avframe(&buffer->frame, avframe);
180 memcpy(buffer->frame.padding, padding, sizeof(padding));
181 return 0;
182}
183
184static void free_buffer(AVRefStructOpaque opaque, void *obj)
185{
187 av_frame_free(&buffer->avframe);
188}
189
190static void pass_free(SwsPass *pass)
191{
192 if (pass->free)
193 pass->free(pass->priv);
195 av_free(pass);
196}
197
199 int width, int height, SwsPass *input,
200 int lines, int align,
202 void *priv, void (*free_cb)(void *priv),
203 SwsPass **out_pass)
204{
205 int ret;
206 SwsPass *pass = av_mallocz(sizeof(*pass));
207 if (!pass) {
208 if (free_cb)
209 free_cb(priv);
210 return AVERROR(ENOMEM);
211 }
212
213 if (!lines)
214 lines = height;
215
216 pass->graph = graph;
217 pass->run = run;
218 pass->setup = setup;
219 pass->priv = priv;
220 pass->free = free_cb;
221 pass->format = fmt;
222 pass->lines = lines;
223 pass->input = input;
224 pass->output = av_refstruct_alloc_ext(sizeof(*pass->output), 0, NULL, free_buffer);
225 if (!pass->output) {
226 ret = AVERROR(ENOMEM);
227 goto fail;
228 }
229
230 pass->output->height = height;
231 pass->output->width = width;
232 pass->output->width_align = 1;
233 memset(pass->output->plane_copy, -1, sizeof(pass->output->plane_copy));
234
235 if (!align) {
236 pass->slice_h = pass->lines;
237 pass->num_slices = 1;
238 } else {
239 pass->slice_h = (pass->lines + graph->num_threads - 1) / graph->num_threads;
240 pass->slice_h = FFALIGN(pass->slice_h, align);
241 pass->num_slices = (pass->lines + pass->slice_h - 1) / pass->slice_h;
242 }
243
244 ret = av_dynarray_add_nofree(&graph->passes, &graph->num_passes, pass);
245 if (ret < 0)
246 goto fail;
247
248 *out_pass = pass;
249 return 0;
250
251fail:
252 pass_free(pass);
253 return ret;
254}
255
257{
258 if (!dst || !src || dst == src)
259 return;
260
261 av_assert0(dst->format == src->format);
262 SwsPassBuffer *keep = src->output, *drop = dst->output;
263
264 av_assert1(keep->width == drop->width);
265 av_assert1(keep->height == drop->height);
266 keep->width_align = FFMAX(keep->width_align, drop->width_align);
267 keep->width_pad = FFMAX(keep->width_pad, drop->width_pad);
268
269 for (int i = 0; i < FF_ARRAY_ELEMS(keep->plane_copy); i++) {
270 if (keep->plane_copy[i] < 0)
271 keep->plane_copy[i] = drop->plane_copy[i];
272 else if (drop->plane_copy[i] >= 0)
273 av_assert1(keep->plane_copy[i] == drop->plane_copy[i]);
274 }
275
276 av_refstruct_replace(&dst->output, src->output);
277}
278
279static void frame_shift(const SwsFrame *f, const int y, uint8_t *data[4])
280{
281 for (int i = 0; i < 4; i++) {
282 if (f->data[i])
283 data[i] = f->data[i] + (y >> ff_fmt_vshift(f->format, i)) * f->linesize[i];
284 else
285 data[i] = NULL;
286 }
287}
288
289static void run_copy(const SwsFrame *out, const SwsFrame *in, int y, int h,
290 const SwsPass *pass)
291{
292 uint8_t *in_data[4], *out_data[4];
293 frame_shift(in, y, in_data);
294 frame_shift(out, y, out_data);
295
296 for (int i = 0; i < 4 && out_data[i]; i++) {
297 const int lines = h >> ff_fmt_vshift(in->format, i);
298 av_assert1(in_data[i]);
299
301 if (!y && in->data[1] != out->data[1])
302 memcpy(out->data[1], in->data[1], AVPALETTE_SIZE);
303 break;
304 }
305
306 if (in_data[i] == out_data[i]) {
307 av_assert0(in->linesize[i] == out->linesize[i]);
308 } else {
309 ff_copyPlane(in_data[i], in->linesize[i], 0, lines,
310 av_image_get_linesize(out->format, out->width, i),
311 out_data[i], out->linesize[i]);
312 }
313 }
314}
315
316static void run_rgb0(const SwsFrame *out, const SwsFrame *in, int y, int h,
317 const SwsPass *pass)
318{
319 SwsInternal *c = pass->priv;
320 const int x0 = c->src0Alpha - 1;
321 const int w4 = 4 * out->width;
322 const int src_stride = in->linesize[0];
323 const int dst_stride = out->linesize[0];
324 const uint8_t *src = in->data[0] + y * src_stride;
325 uint8_t *dst = out->data[0] + y * dst_stride;
326
327 for (int y = 0; y < h; y++) {
328 memcpy(dst, src, w4 * sizeof(*dst));
329 for (int x = x0; x < w4; x += 4)
330 dst[x] = 0xFF;
331
332 src += src_stride;
333 dst += dst_stride;
334 }
335}
336
337static void run_xyz2rgb(const SwsFrame *out, const SwsFrame *in, int y, int h,
338 const SwsPass *pass)
339{
340 const SwsInternal *c = pass->priv;
341 c->xyz12Torgb48(c, out->data[0] + y * out->linesize[0], out->linesize[0],
342 in->data[0] + y * in->linesize[0], in->linesize[0],
343 out->width, h);
344}
345
346static void run_rgb2xyz(const SwsFrame *out, const SwsFrame *in, int y, int h,
347 const SwsPass *pass)
348{
349 const SwsInternal *c = pass->priv;
350 c->rgb48Toxyz12(c, out->data[0] + y * out->linesize[0], out->linesize[0],
351 in->data[0] + y * in->linesize[0], in->linesize[0],
352 out->width, h);
353}
354
355/***********************************************************************
356 * Internal ff_swscale() wrapper. This reuses the legacy scaling API. *
357 * This is considered fully deprecated, and will be replaced by a full *
358 * reimplementation ASAP. *
359 ***********************************************************************/
360
361static void free_legacy_swscale(void *priv)
362{
363 SwsContext *sws = priv;
364 sws_free_context(&sws);
365}
366
367static int setup_legacy_swscale(const SwsFrame *out, const SwsFrame *in,
368 const SwsPass *pass)
369{
370 SwsContext *sws = pass->priv;
371 SwsInternal *c = sws_internal(sws);
372 if (sws->flags & SWS_BITEXACT && sws->dither == SWS_DITHER_ED && c->dither_error[0]) {
373 for (int i = 0; i < 4; i++)
374 memset(c->dither_error[i], 0, sizeof(c->dither_error[0][0]) * (sws->dst_w + 2));
375 }
376
377 if (usePal(sws->src_format))
378 ff_update_palette(c, (const uint32_t *) in->data[1]);
379
380 return 0;
381}
382
383static inline SwsContext *slice_ctx(const SwsPass *pass, int y)
384{
385 SwsContext *sws = pass->priv;
386 SwsInternal *parent = sws_internal(sws);
387 if (pass->num_slices == 1)
388 return sws;
389
390 av_assert1(parent->nb_slice_ctx == pass->num_slices);
391 sws = parent->slice_ctx[y / pass->slice_h];
392
393 if (usePal(sws->src_format)) {
394 SwsInternal *sub = sws_internal(sws);
395 memcpy(sub->pal_yuv, parent->pal_yuv, sizeof(sub->pal_yuv));
396 memcpy(sub->pal_rgb, parent->pal_rgb, sizeof(sub->pal_rgb));
397 }
398
399 return sws;
400}
401
402static void run_legacy_unscaled(const SwsFrame *out, const SwsFrame *in,
403 int y, int h, const SwsPass *pass)
404{
405 SwsContext *sws = slice_ctx(pass, y);
406 SwsInternal *c = sws_internal(sws);
407 uint8_t *in_data[4];
408 frame_shift(in, y, in_data);
409
410 c->convert_unscaled(c, (const uint8_t *const *) in_data, in->linesize, y, h,
411 out->data, out->linesize);
412}
413
414static void run_legacy_swscale(const SwsFrame *out, const SwsFrame *in,
415 int y, int h, const SwsPass *pass)
416{
417 SwsContext *sws = slice_ctx(pass, y);
418 SwsInternal *c = sws_internal(sws);
419 uint8_t *out_data[4];
420 frame_shift(out, y, out_data);
421
422 ff_swscale(c, (const uint8_t *const *) in->data, in->linesize, 0,
423 sws->src_h, out_data, out->linesize, y, h);
424}
425
426static void run_legacy_lut3d(const SwsFrame *out, const SwsFrame *in,
427 int y, int h, const SwsPass *pass)
428{
429 const SwsLut3D *lut = pass->graph->lut3d;
430 uint8_t *in_data[4], *out_data[4];
431 frame_shift(in, y, in_data);
432 frame_shift(out, y, out_data);
433
434 ff_sws_lut3d_apply_rgba64(lut, in_data[0], in->linesize[0], out_data[0],
435 out->linesize[0], out->width, h);
436}
437
438static void legacy_chr_pos(SwsGraph *graph, int *chr_pos, int override, int *warned)
439{
440 if (override == -513 || override == *chr_pos)
441 return;
442
443 if (!*warned) {
445 "Setting chroma position directly is deprecated, make sure "
446 "the frame is tagged with the correct chroma location.\n");
447 *warned = 1;
448 }
449
450 *chr_pos = override;
451}
452
453/* Takes over ownership of `sws` */
455 SwsPass *input, SwsPass **output)
456{
457 SwsInternal *c = sws_internal(sws);
458 const int src_w = sws->src_w, src_h = sws->src_h;
459 const int dst_w = sws->dst_w, dst_h = sws->dst_h;
460 int align = c->dst_slice_align;
461 SwsPass *pass = NULL;
462 int ret;
463
464 if (c->cascaded_context[0]) {
465 const int num_cascaded = c->cascaded_context[2] ? 3 : 2;
466 for (int i = 0; i < num_cascaded; i++) {
467 const int is_last = i + 1 == num_cascaded;
468
469 /* Steal cascaded context, so we can manage its lifetime independently */
470 SwsContext *sub = c->cascaded_context[i];
471 c->cascaded_context[i] = NULL;
472
473 ret = init_legacy_subpass(graph, sub, input, is_last ? output : &input);
474 if (ret < 0)
475 break;
476 }
477
478 sws_free_context(&sws);
479 return ret;
480 }
481
482 if (sws->dither == SWS_DITHER_ED && !c->convert_unscaled)
483 align = 0; /* disable slice threading */
484
485 if (c->src0Alpha && !c->dst0Alpha && isALPHA(sws->dst_format)) {
486 ret = ff_sws_graph_add_pass(graph, AV_PIX_FMT_RGBA, src_w, src_h, input,
487 0, 1, run_rgb0, NULL, c, NULL, &input);
488 if (ret < 0) {
489 sws_free_context(&sws);
490 return ret;
491 }
492 }
493
494 if (c->srcXYZ && !c->dstXYZ) {
495 ret = ff_sws_graph_add_pass(graph, AV_PIX_FMT_RGB48, src_w, src_h, input,
496 0, 1, run_xyz2rgb, NULL, c, NULL, &input);
497 if (ret < 0) {
498 sws_free_context(&sws);
499 return ret;
500 }
501 }
502
503 ret = ff_sws_graph_add_pass(graph, sws->dst_format, dst_w, dst_h, input, 0, align,
504 c->convert_unscaled ? run_legacy_unscaled : run_legacy_swscale,
506 if (ret < 0)
507 return ret;
509
510 /**
511 * For slice threading, we need to create sub contexts, similar to how
512 * swscale normally handles it internally. The most important difference
513 * is that we handle cascaded contexts before threaded contexts; whereas
514 * context_init_threaded() does it the other way around.
515 */
516
517 if (pass->num_slices > 1) {
518 c->slice_ctx = av_calloc(pass->num_slices, sizeof(*c->slice_ctx));
519 if (!c->slice_ctx)
520 return AVERROR(ENOMEM);
521
522 for (int i = 0; i < pass->num_slices; i++) {
523 SwsContext *slice;
525 slice = c->slice_ctx[i] = sws_alloc_context();
526 if (!slice)
527 return AVERROR(ENOMEM);
528 c->nb_slice_ctx++;
529
530 c2 = sws_internal(slice);
531 c2->parent = sws;
532
533 ret = av_opt_copy(slice, sws);
534 if (ret < 0)
535 return ret;
536
538 if (ret < 0)
539 return ret;
540
541 sws_setColorspaceDetails(slice, c->srcColorspaceTable,
542 slice->src_range, c->dstColorspaceTable,
543 slice->dst_range, c->brightness, c->contrast,
544 c->saturation);
545
546 for (int i = 0; i < FF_ARRAY_ELEMS(c->srcColorspaceTable); i++) {
547 c2->srcColorspaceTable[i] = c->srcColorspaceTable[i];
548 c2->dstColorspaceTable[i] = c->dstColorspaceTable[i];
549 }
550
551 c2->srcXYZ = c->srcXYZ;
552 c2->dstXYZ = c->dstXYZ;
553 }
554 }
555
556 if (c->dstXYZ && !c->srcXYZ) {
557 ret = ff_sws_graph_add_pass(graph, AV_PIX_FMT_RGB48, dst_w, dst_h, pass,
558 0, 1, run_rgb2xyz, NULL, c, NULL, &pass);
559 if (ret < 0)
560 return ret;
561 }
562
563 *output = pass;
564 return 0;
565}
566
567static int add_legacy_3dlut_pass(SwsGraph *graph, const SwsFormat *src,
568 SwsPass *input, SwsPass **output);
569
570static int add_legacy_sws_pass(SwsGraph *graph, const SwsFormat *src,
571 const SwsFormat *dst, const SwsLut3D *lut3d,
572 SwsPass *input, SwsPass **output)
573{
574 int ret, warned = 0;
575 SwsContext *const ctx = graph->ctx;
576 const SwsBackend backend = ff_sws_enabled_backends(ctx);
577 if (!(backend & SWS_BACKEND_LEGACY))
578 return AVERROR(ENOTSUP);
579 if (src->hw_format != AV_PIX_FMT_NONE || dst->hw_format != AV_PIX_FMT_NONE)
580 return AVERROR(ENOTSUP);
581
582 /* Re-check this here because this might not be excluded if the caller was
583 * testing against multiple backends */
584 if (!sws_isSupportedInput(src->format) || !sws_isSupportedOutput(dst->format))
585 return AVERROR(ENOTSUP);
586
587 /* If we need to apply a 3D LUT, add it as an explicit input prepass */
588 if (lut3d) {
589 ret = add_legacy_3dlut_pass(graph, src, input, &input);
590 if (ret < 0)
591 return ret;
592
593 SwsFormat tmp = *src;
594 tmp.format = input->format;
595 tmp.color = lut3d->map.dst;
596 return add_legacy_sws_pass(graph, &tmp, dst, NULL, input, output);
597 }
598
600 if (!sws)
601 return AVERROR(ENOMEM);
602
603 sws->flags = ctx->flags;
604 sws->dither = ctx->dither;
605 sws->alpha_blend = ctx->alpha_blend;
606 sws->gamma_flag = ctx->gamma_flag;
607 sws->scaler = ctx->scaler;
608 sws->scaler_sub = ctx->scaler_sub;
609
610 sws->src_w = src->width;
611 sws->src_h = src->height;
612 sws->src_format = src->format;
613 sws->src_range = src->range == AVCOL_RANGE_JPEG;
614
615 sws->dst_w = dst->width;
616 sws->dst_h = dst->height;
617 sws->dst_format = dst->format;
618 sws->dst_range = dst->range == AVCOL_RANGE_JPEG;
621
622 if (!isFloat(src->format))
623 graph->incomplete |= src->range == AVCOL_RANGE_UNSPECIFIED;
624 if (!isFloat(dst->format))
625 graph->incomplete |= dst->range == AVCOL_RANGE_UNSPECIFIED;
626
627 /* Allow overriding chroma position with the legacy API */
628 legacy_chr_pos(graph, &sws->src_h_chr_pos, ctx->src_h_chr_pos, &warned);
629 legacy_chr_pos(graph, &sws->src_v_chr_pos, ctx->src_v_chr_pos, &warned);
630 legacy_chr_pos(graph, &sws->dst_h_chr_pos, ctx->dst_h_chr_pos, &warned);
631 legacy_chr_pos(graph, &sws->dst_v_chr_pos, ctx->dst_v_chr_pos, &warned);
632
633 /* Explicitly strip chroma offsets when not subsampling, because it
634 * interferes with the operation of flags like SWS_FULL_CHR_H_INP */
635 if (!src->desc->log2_chroma_w)
636 sws->src_h_chr_pos = -513;
637 if (!src->desc->log2_chroma_h)
638 sws->src_v_chr_pos = -513;
639 if (!dst->desc->log2_chroma_w)
640 sws->dst_h_chr_pos = -513;
641 if (!dst->desc->log2_chroma_h)
642 sws->dst_v_chr_pos = -513;
643
644 for (int i = 0; i < SWS_NUM_SCALER_PARAMS; i++)
645 sws->scaler_params[i] = ctx->scaler_params[i];
646
647 ret = sws_init_context(sws, NULL, NULL);
648 if (ret < 0) {
649 sws_free_context(&sws);
650 return ret;
651 }
652
653 /* Set correct color matrices */
654 {
655 int in_full, out_full, brightness, contrast, saturation;
656 const int *inv_table, *table;
657 sws_getColorspaceDetails(sws, (int **)&inv_table, &in_full,
658 (int **)&table, &out_full,
659 &brightness, &contrast, &saturation);
660
661 inv_table = sws_getCoefficients(src->csp);
663
664 graph->incomplete |= src->csp != dst->csp &&
665 (src->csp == AVCOL_SPC_UNSPECIFIED ||
666 dst->csp == AVCOL_SPC_UNSPECIFIED);
667
668 sws_setColorspaceDetails(sws, inv_table, in_full, table, out_full,
669 brightness, contrast, saturation);
670 }
671
672 return init_legacy_subpass(graph, sws, input, output);
673}
674
675static int add_legacy_3dlut_pass(SwsGraph *graph, const SwsFormat *src,
676 SwsPass *input, SwsPass **output)
677{
678 int ret;
679
680 const SwsLut3D *lut3d = graph->lut3d;
681 if (!lut3d)
682 return 0;
683
684 const enum AVPixelFormat fmt = AV_PIX_FMT_RGBA64;
685 if (src->format != fmt) {
686 SwsFormat tmp = *src;
687 tmp.format = fmt;
688 ret = add_legacy_sws_pass(graph, src, &tmp, NULL, input, &input);
689 if (ret < 0)
690 return ret;
691 }
692
693 ret = ff_sws_graph_add_pass(graph, fmt, src->width, src->height,
694 input, 0, 1, run_legacy_lut3d, NULL, NULL, NULL,
695 output);
696 if (ret < 0)
697 return ret;
698
699 return 0;
700}
701
702/*********************************
703 * Format conversion and scaling *
704 *********************************/
705
706static int add_ops_convert_pass(SwsGraph *graph, const SwsFormat *src,
707 const SwsFormat *dst, const SwsLut3D *lut3d,
708 SwsPass *input, SwsPass **output)
709{
710 SwsContext *ctx = graph->ctx;
711
712 /* Preemptively skip the ops list generation if the backend was
713 * constrained to the legacy implementation only. This would
714 * normally also fail in ff_sws_compile_pass() with the same
715 * error, but this way saves a bit of unnecessary overhead */
718 return AVERROR(ENOTSUP);
719
720 SwsOpList *ops;
721 int ret = ff_sws_op_list_generate(ctx, src, dst, lut3d, &ops, &graph->incomplete);
722 if (ret < 0)
723 return ret;
724
725 av_log(ctx, AV_LOG_VERBOSE, "Conversion pass for %s -> %s:\n",
727
728 av_log(ctx, AV_LOG_DEBUG, "Unoptimized operation list:\n");
731
733 return ff_sws_compile_pass(graph, NULL, &ops, flags, input, output);
734}
735
737{
738 if (ctx->flags & SWS_UNSTABLE)
739 return true;
740 if (isFloat(src->format) || isFloat(dst->format))
741 return true; /* ops backend has better support for float formats */
742 return false; /* default to legacy for stability reasons */
743}
744
745static int add_convert_pass(SwsGraph *graph, const SwsFormat *src,
746 const SwsFormat *dst, const SwsLut3D *lut3d,
747 SwsPass *input, SwsPass **output)
748{
749 SwsContext *ctx = graph->ctx;
750 int ret;
751
752 if (prefer_ops_backend(ctx, src, dst)) {
753 ret = add_ops_convert_pass(graph, src, dst, lut3d, input, output);
754 if (ret == AVERROR(ENOTSUP))
755 ret = add_legacy_sws_pass(graph, src, dst, lut3d, input, output);
756 } else {
757 ret = add_legacy_sws_pass(graph, src, dst, lut3d, input, output);
758 if (ret == AVERROR(ENOTSUP))
759 ret = add_ops_convert_pass(graph, src, dst, lut3d, input, output);
760 }
761
762 return ret;
763}
764
765/**************************
766 * Gamut and tone mapping *
767 **************************/
768
770{
771 SwsColorMap map = {0};
772
773 /**
774 * Grayspace does not really have primaries, so just force the use of
775 * the equivalent other primary set to avoid a conversion. Technically,
776 * this does affect the weights used for the Grayscale conversion, but
777 * in practise, that should give the expected results more often than not.
778 */
779 if (isGray(dst->format)) {
780 dst->color = src->color;
781 } else if (isGray(src->format)) {
782 src->color = dst->color;
783 }
784
785 /* Fully infer color spaces before color mapping logic */
786 graph->incomplete |= ff_infer_colors(&src->color, &dst->color);
787
788 map.intent = graph->ctx->intent;
789 map.src = src->color;
790 map.dst = dst->color;
791
793 return 0;
794
795 if (src->hw_format != AV_PIX_FMT_NONE || dst->hw_format != AV_PIX_FMT_NONE)
796 return AVERROR(ENOTSUP);
797
798 graph->lut3d = ff_sws_lut3d_alloc();
799 if (!graph->lut3d)
800 return AVERROR(ENOMEM);
801
802 return ff_sws_lut3d_generate(graph->lut3d, &map);
803}
804
805/***************************************
806 * Main filter graph construction code *
807 ***************************************/
808
809static int init_passes(SwsGraph *graph)
810{
811 SwsFormat src = graph->src;
812 SwsFormat dst = graph->dst;
813 SwsPass *pass = NULL; /* read from main input image */
814 int ret;
815
817
818 ret = generate_3dlut(graph, &src, &dst);
819 if (ret < 0)
820 return ret;
821
822 if (!ff_fmt_equal(&src, &dst) || graph->lut3d) {
823 ret = add_convert_pass(graph, &src, &dst, graph->lut3d, pass, &pass);
824 if (ret < 0)
825 return ret;
826 }
827
828 if (!pass) {
829 /* Plane passthrough is not implemented for hardware frames */
830 if (src.hw_format != AV_PIX_FMT_NONE || dst.hw_format != AV_PIX_FMT_NONE)
831 return AVERROR(ENOTSUP);
832
833 /* No passes were added, so no operations were necessary */
834 graph->noop = 1;
835
836 const int nb_planes = av_pix_fmt_count_planes(dst.format);
837 for (int i = 0; i < nb_planes; i++)
838 graph->plane_copy[i] = i;
839
840 /* Add threaded memcpy pass */
841 return ff_sws_graph_add_pass(graph, dst.format, dst.width, dst.height,
842 pass, 0, 1, run_copy, NULL, NULL, NULL, &pass);
843 }
844
845 /* Compute end-to-end plane copy map */
846 for (int n = 0; n < graph->num_passes; n++) {
847 const SwsPass *pass = graph->passes[n];
848 /* This pass writes to an output buffer other than the image
849 * output, or copies from the output of a different pass */
850 if (pass->output->avframe || pass->input)
851 continue;
852 for (int i = 0; i < FF_ARRAY_ELEMS(graph->plane_copy); i++) {
853 const int idx = pass->output->plane_copy[i];
854 if (idx < 0)
855 continue;
856 if (graph->plane_copy[i] < 0) {
857 graph->plane_copy[i] = idx;
858 av_log(graph->ctx, AV_LOG_DEBUG, "Plane %d passthrough from "
859 "plane %d\n", i, idx);
860 } else {
861 av_assert0(graph->plane_copy[i] == idx);
862 }
863 }
864 }
865
866 return 0;
867}
868
869static int sws_graph_worker(void *priv, int jobnr, int threadnr, int nb_jobs,
870 int nb_threads)
871{
872 SwsGraph *graph = priv;
873 const SwsPass *pass = graph->exec.pass;
874 const int slice_y = jobnr * pass->slice_h;
875 const int slice_h = FFMIN(pass->slice_h, pass->lines - slice_y);
876
877 pass->run(graph->exec.output, graph->exec.input, slice_y, slice_h, pass);
878 return 0;
879}
880
882{
883 return av_mallocz(sizeof(SwsGraph));
884}
885
886static void graph_uninit(SwsGraph *graph)
887{
889
890 for (int i = 0; i < graph->num_passes; i++)
891 pass_free(graph->passes[i]);
892 av_free(graph->passes);
893
894 av_refstruct_unref(&graph->lut3d);
895
896 memset(graph, 0, sizeof(*graph));
897}
898
900 const SwsFormat *src)
901{
902 int ret;
903 if (graph->ctx) {
904 av_log(ctx, AV_LOG_ERROR, "Graph is already initialized\n");
905 return AVERROR(EINVAL);
906 }
907
908 graph->ctx = ctx;
909 graph->src = *src;
910 graph->dst = *dst;
911 graph->opts_copy = *ctx;
912 av_assert0(src->interlaced == dst->interlaced);
913 av_assert0(src->field == dst->field);
914 memset(graph->plane_copy, -1, sizeof(graph->plane_copy));
915
916 if (ctx->threads == 1) {
917 graph->num_threads = 1;
918 } else {
919 ret = avpriv_slicethread_create2(&graph->slicethread, (void *) graph,
920 sws_graph_worker, NULL, ctx->threads);
921 if (ret == AVERROR(ENOSYS)) {
922 /* Fall back to single threaded operation */
923 graph->num_threads = 1;
924 } else if (ret < 0) {
925 goto error;
926 } else {
927 graph->num_threads = ret;
928 }
929 }
930
931 ret = init_passes(graph);
932 if (ret < 0)
933 goto error;
934
935 /* Resolve output buffers for all intermediate passes */
936 for (int i = 0; i < graph->num_passes; i++) {
937 graph->backend |= graph->passes[i]->backend;
938 ret = pass_alloc_output(graph->passes[i]->input);
939 if (ret < 0)
940 goto error;
941 }
942
943 return 0;
944
945error:
946 graph_uninit(graph);
947 return ret;
948}
949
950void ff_sws_graph_rollback(SwsGraph *graph, int since_idx)
951{
952 for (int i = since_idx; i < graph->num_passes; i++)
953 pass_free(graph->passes[i]);
954 graph->num_passes = since_idx;
955}
956
958{
959 SwsGraph *graph = *pgraph;
960 if (!graph)
961 return;
962
963 graph_uninit(graph);
964 av_free(graph);
965 *pgraph = NULL;
966}
967
968/* Tests only options relevant to SwsGraph */
969static int opts_equal(const SwsContext *c1, const SwsContext *c2)
970{
971 return c1->flags == c2->flags &&
972 c1->threads == c2->threads &&
973 c1->dither == c2->dither &&
974 c1->alpha_blend == c2->alpha_blend &&
975 c1->gamma_flag == c2->gamma_flag &&
976 c1->src_h_chr_pos == c2->src_h_chr_pos &&
977 c1->src_v_chr_pos == c2->src_v_chr_pos &&
978 c1->dst_h_chr_pos == c2->dst_h_chr_pos &&
979 c1->dst_v_chr_pos == c2->dst_v_chr_pos &&
980 c1->intent == c2->intent &&
981 c1->scaler == c2->scaler &&
982 c1->scaler_sub == c2->scaler_sub &&
983 c1->backends == c2->backends &&
984 !memcmp(c1->scaler_params, c2->scaler_params, sizeof(c1->scaler_params));
985
986}
987
989 const SwsFormat *src)
990{
991 if (ff_fmt_equal(&graph->src, src) && ff_fmt_equal(&graph->dst, dst) &&
992 opts_equal(ctx, &graph->opts_copy))
993 {
994 ff_sws_graph_update_metadata(graph, &src->color);
995 return 0;
996 }
997
998 graph_uninit(graph);
999 return ff_sws_graph_init(graph, ctx, dst, src);
1000}
1001
1003{
1004 if (!color)
1005 return;
1006
1008
1009 if (graph->lut3d)
1010 ff_sws_lut3d_update(graph->lut3d, &graph->src.color);
1011}
1012
1013static void get_field(SwsGraph *graph, const SwsFormat *fmt,
1014 const AVFrame *avframe, SwsFrame *frame)
1015{
1017
1018 if (!(avframe->flags & AV_FRAME_FLAG_INTERLACED)) {
1019 av_assert1(!fmt->field);
1020 return;
1021 }
1022
1023 if (fmt->field == FIELD_BOTTOM) {
1024 /* Odd rows, offset by one line */
1026 for (int i = 0; i < 4; i++) {
1027 if (frame->data[i])
1028 frame->data[i] += frame->linesize[i];
1029 if (desc->flags & AV_PIX_FMT_FLAG_PAL)
1030 break;
1031 }
1032 }
1033
1034 /* Take only every second line */
1035 for (int i = 0; i < 4; i++)
1036 frame->linesize[i] *= 2;
1037
1038 frame->height = (frame->height + (fmt->field == FIELD_TOP)) >> 1;
1039}
1040
1041int ff_sws_graph_run(SwsGraph *graph, const AVFrame *dst, const AVFrame *src)
1042{
1043 av_assert0(dst->format == graph->dst.hw_format || dst->format == graph->dst.format);
1044 av_assert0(src->format == graph->src.hw_format || src->format == graph->src.format);
1045
1046 SwsFrame src_field, dst_field;
1047 get_field(graph, &graph->dst, dst, &dst_field);
1048 get_field(graph, &graph->src, src, &src_field);
1049
1050 /* AVFrame.data requires 16 bytes of padding after the planes for reading */
1051 for (int i = 0; i < 4; i++)
1052 src_field.padding[i] = 16;
1053
1054 for (int i = 0; i < graph->num_passes; i++) {
1055 const SwsPass *pass = graph->passes[i];
1056 graph->exec.pass = pass;
1057 graph->exec.input = pass->input ? &pass->input->output->frame : &src_field;
1058 graph->exec.output = pass->output->avframe ? &pass->output->frame : &dst_field;
1059 if (pass->setup) {
1060 int ret = pass->setup(graph->exec.output, graph->exec.input, pass);
1061 if (ret < 0)
1062 return ret;
1063 }
1064
1065 if (pass->num_slices == 1) {
1066 pass->run(graph->exec.output, graph->exec.input, 0, pass->lines, pass);
1067 } else {
1069 }
1070 }
1071
1072 return 0;
1073}
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
static FILE * out
static AVFormatContext * ctx
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
static const uint8_t *BS_FUNC align(BSCTX *bc)
Skip bits to a byte boundary.
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
bool ff_sws_color_map_noop(const SwsColorMap *map)
Returns true if the given color map is a semantic no-op - that is, the overall RGB end to end transfo...
Definition cms.c:34
static IPT saturation(const CmsCtx *ctx, IPT ipt)
Definition cms.c:559
#define NULL
Definition coverity.c:32
static AVFrame * frame
static void free_buffer(void *data, size_t length)
error code definitions
int ret
Definition ffplay.c:391
SDL_atomic_t done
Definition ffplay.c:392
@ FIELD_TOP
Definition format.h:56
@ FIELD_BOTTOM
Definition format.h:57
static int ff_fmt_equal(const SwsFormat *fmt1, const SwsFormat *fmt2)
Definition format.h:129
static void ff_color_update_dynamic(SwsColor *dst, const SwsColor *src)
Definition format.h:74
void ff_sws_graph_update_metadata(SwsGraph *graph, const SwsColor *color)
Update dynamic per-frame HDR metadata without requiring a full reinit.
Definition graph.c:1002
static void run_rgb2xyz(const SwsFrame *out, const SwsFrame *in, int y, int h, const SwsPass *pass)
Definition graph.c:346
int ff_sws_graph_init(SwsGraph *graph, SwsContext *ctx, const SwsFormat *dst, const SwsFormat *src)
Initialize the filter graph for a given pair of formats.
Definition graph.c:899
int ff_sws_graph_add_pass(SwsGraph *graph, enum AVPixelFormat fmt, int width, int height, SwsPass *input, int lines, int align, SwsPassFunc run, SwsPassSetup setup, void *priv, void(*free_cb)(void *priv), SwsPass **out_pass)
Allocate and add a new pass to the filter graph.
Definition graph.c:198
static bool prefer_ops_backend(SwsContext *ctx, const SwsFormat *src, const SwsFormat *dst)
Definition graph.c:736
static void legacy_chr_pos(SwsGraph *graph, int *chr_pos, int override, int *warned)
Definition graph.c:438
static int add_ops_convert_pass(SwsGraph *graph, const SwsFormat *src, const SwsFormat *dst, const SwsLut3D *lut3d, SwsPass *input, SwsPass **output)
Definition graph.c:706
static int sws_graph_worker(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads)
Definition graph.c:869
static void run_rgb0(const SwsFrame *out, const SwsFrame *in, int y, int h, const SwsPass *pass)
Definition graph.c:316
void ff_sws_pass_link_output(SwsPass *dst, const SwsPass *src)
Link the output buffers to a different pass, rather than allocating new image buffers.
Definition graph.c:256
void ff_sws_graph_rollback(SwsGraph *graph, int since_idx)
Remove all passes added since the given index.
Definition graph.c:950
static int add_legacy_3dlut_pass(SwsGraph *graph, const SwsFormat *src, SwsPass *input, SwsPass **output)
Definition graph.c:675
static int add_convert_pass(SwsGraph *graph, const SwsFormat *src, const SwsFormat *dst, const SwsLut3D *lut3d, SwsPass *input, SwsPass **output)
Definition graph.c:745
static void graph_uninit(SwsGraph *graph)
Definition graph.c:886
static int opts_equal(const SwsContext *c1, const SwsContext *c2)
Definition graph.c:969
static void run_legacy_unscaled(const SwsFrame *out, const SwsFrame *in, int y, int h, const SwsPass *pass)
Definition graph.c:402
static SwsContext * slice_ctx(const SwsPass *pass, int y)
Definition graph.c:383
static void free_buffer(AVRefStructOpaque opaque, void *obj)
Definition graph.c:184
int ff_sws_graph_run(SwsGraph *graph, const AVFrame *dst, const AVFrame *src)
Dispatch the filter graph on a single field of the given frames.
Definition graph.c:1041
static int frame_alloc_planes_ref(AVFrame *dst, int padding[4], const AVFrame *src, const int plane_copy[4])
Definition graph.c:58
static void run_legacy_swscale(const SwsFrame *out, const SwsFrame *in, int y, int h, const SwsPass *pass)
Definition graph.c:414
static int pass_alloc_output(SwsPass *pass)
Definition graph.c:135
static void run_legacy_lut3d(const SwsFrame *out, const SwsFrame *in, int y, int h, const SwsPass *pass)
Definition graph.c:426
static int add_legacy_sws_pass(SwsGraph *graph, const SwsFormat *src, const SwsFormat *dst, const SwsLut3D *lut3d, SwsPass *input, SwsPass **output)
Definition graph.c:570
SwsGraph * ff_sws_graph_alloc(void)
Allocate an empty SwsGraph.
Definition graph.c:881
int ff_sws_pass_aligned_width(const SwsPass *pass, int width)
Align width to the optimal size for pass.
Definition graph.c:46
static void run_copy(const SwsFrame *out, const SwsFrame *in, int y, int h, const SwsPass *pass)
Definition graph.c:289
int ff_sws_graph_reinit(SwsGraph *graph, SwsContext *ctx, const SwsFormat *dst, const SwsFormat *src)
Wrapper around ff_sws_graph_init() that reuses the existing graph if the format is compatible.
Definition graph.c:988
static void frame_shift(const SwsFrame *f, const int y, uint8_t *data[4])
Definition graph.c:279
static int init_legacy_subpass(SwsGraph *graph, SwsContext *sws, SwsPass *input, SwsPass **output)
Definition graph.c:454
void ff_sws_graph_free(SwsGraph **pgraph)
Uninitialize any state associate with this filter graph and free it.
Definition graph.c:957
static void run_xyz2rgb(const SwsFrame *out, const SwsFrame *in, int y, int h, const SwsPass *pass)
Definition graph.c:337
static void free_legacy_swscale(void *priv)
Definition graph.c:361
static int generate_3dlut(SwsGraph *graph, SwsFormat *src, SwsFormat *dst)
Definition graph.c:769
static void get_field(SwsGraph *graph, const SwsFormat *fmt, const AVFrame *avframe, SwsFrame *frame)
Definition graph.c:1013
static void pass_free(SwsPass *pass)
Definition graph.c:190
static int setup_legacy_swscale(const SwsFrame *out, const SwsFrame *in, const SwsPass *pass)
Definition graph.c:367
static int init_passes(SwsGraph *graph)
Definition graph.c:809
void(* SwsPassFunc)(const SwsFrame *out, const SwsFrame *in, int y, int h, const SwsPass *pass)
Output h lines of filtered data.
Definition graph.h:46
int(* SwsPassSetup)(const SwsFrame *out, const SwsFrame *in, const SwsPass *pass)
Function to run from the main thread before processing any lines.
Definition graph.h:52
static av_always_inline av_const int ff_fmt_vshift(enum AVPixelFormat fmt, int plane)
Definition graph.h:33
#define fail
Definition test.h:479
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition buffer.c:140
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition buffer.c:104
AVBufferRef * av_buffer_alloc(size_t size)
Allocate an AVBuffer of the given size using av_malloc().
Definition buffer.c:78
#define AVERROR(e)
Definition error.h:45
#define AV_FRAME_FLAG_INTERLACED
A flag to mark frames whose content is interlaced.
Definition frame.h:702
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition frame.c:499
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:65
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:53
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition log.h:236
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem)
Add an element to a dynamic array.
Definition mem.c:419
int av_image_fill_plane_sizes(size_t sizes[4], enum AVPixelFormat pix_fmt, int height, const ptrdiff_t linesizes[4])
Fill plane sizes for an image with pixel format pix_fmt and height height.
Definition imgutils.c:111
int av_image_get_linesize(enum AVPixelFormat pix_fmt, int width, int plane)
Compute the size of an image line with format pix_fmt and width width for the plane plane.
Definition imgutils.c:76
int av_image_check_size2(unsigned int w, unsigned int h, int64_t max_pixels, enum AVPixelFormat pix_fmt, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of a plane of an image with...
Definition imgutils.c:289
int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
Fill plane linesizes for an image with pixel format pix_fmt and width width.
Definition imgutils.c:89
int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation)
Definition utils.c:1057
av_warn_unused_result int sws_init_context(SwsContext *sws_context, SwsFilter *srcFilter, SwsFilter *dstFilter)
Initialize the swscaler context sws_context.
Definition utils.c:1972
SwsContext * sws_alloc_context(void)
Allocate an empty SwsContext and set its fields to default values.
Definition utils.c:1082
void sws_free_context(SwsContext **ctx)
Free the context and everything associated with it, and write NULL to the provided pointer.
Definition utils.c:2409
const int * sws_getCoefficients(int colorspace)
Return a pointer to yuv<->rgb coefficients for the given colorspace suitable for sws_setColorspaceDet...
Definition yuv2rgb.c:61
SwsBackend
Definition swscale.h:110
int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation)
Definition utils.c:893
@ SWS_DITHER_ED
Definition swscale.h:81
@ SWS_BACKEND_LEGACY
Legacy bespoke format-specific code.
Definition swscale.h:112
@ SWS_BITEXACT
Definition swscale.h:178
@ SWS_UNSTABLE
Allow/prefer using experimental new code paths.
Definition swscale.h:185
int av_opt_copy(void *dst, const void *src)
Copy options from src object into dest object.
Definition opt.c:2239
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
Definition hwcontext.c:347
AVBufferRef * av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
Allocate an AVHWFramesContext tied to a given device context.
Definition hwcontext.c:267
int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
Allocate a new frame attached to the given AVHWFramesContext.
Definition hwcontext.c:564
const VDPAUPixFmtMap * map
static const int sizes[][2]
Definition img2dec.c:63
misc image utilities
size_t av_cpu_max_align(void)
Get the maximum data alignment that may be required by FFmpeg.
Definition cpu.c:289
const char * desc
Definition libsvtav1.c:83
bool ff_infer_colors(SwsColor *src, SwsColor *dst)
Definition format.c:540
void ff_sws_chroma_pos(const SwsFormat *fmt, bool *incomplete, int *out_x_pos, int *out_y_pos)
Wrapper around av_chroma_location_enum_to_pos() that accounts for the per-field offset introduced by ...
Definition format.c:578
int ff_sws_op_list_generate(SwsContext *ctx, const SwsFormat *src, const SwsFormat *dst, const SwsLut3D *lut3d, SwsOpList **out_ops, bool *incomplete)
Generate an SwsOpList defining a conversion from src to dst, with an optional 3DLUT for converting be...
Definition format.c:1825
bool ff_infer_chroma_loc(SwsFormat *src, SwsFormat *dst)
Infers an unspecified chroma location of a subsampled format from the other format.
Definition format.c:568
void ff_sws_frame_from_avframe(SwsFrame *dst, const AVFrame *src)
Initialize a SwsFrame from an AVFrame.
Definition format.c:732
SwsLut3D * ff_sws_lut3d_alloc(void)
Allocates a refstruct.
Definition lut3d.c:33
void ff_sws_lut3d_update(SwsLut3D *lut3d, const SwsColor *new_src)
Update the tone mapping state.
Definition lut3d.c:222
void ff_sws_lut3d_apply_rgba64(const SwsLut3D *lut3d, const uint8_t *in, int in_stride, uint8_t *out, int out_stride, int w, int h)
Applies a color transformation to a plane in RGBA64 format.
Definition lut3d.c:234
int ff_sws_lut3d_generate(SwsLut3D *lut3d, const SwsColorMap *map)
Recalculate the (static) 3DLUT state with new settings.
Definition lut3d.c:198
Utility Preprocessor macros.
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
#define FFALIGN(x, a)
Definition macros.h:78
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:370
Memory handling functions.
static const char * obj
Definition mscl.c:57
static const uint64_t c2
Definition murmur3.c:53
static const uint64_t c1
Definition murmur3.c:52
const char data[16]
Definition mxf.c:155
void ff_sws_op_list_update(SwsOpList *ops)
Infer + propagate known information about components.
Definition ops.c:677
void ff_sws_op_list_print(void *log, int lev, int lev_extra, const SwsOpList *ops)
Print out the contents of an operation list.
Definition ops.c:1041
int ff_sws_compile_pass(SwsGraph *graph, const SwsOpBackend *backend, SwsOpList **pops, int flags, SwsPass *input, SwsPass **output)
Resolves an operation list to a graph pass.
@ SWS_OP_FLAG_OPTIMIZE
@ SWS_OP_FLAG_SPLIT_MEMCPY
AVOptions.
#define sws_isSupportedOutput(x)
#define sws_isSupportedInput(x)
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3524
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition pixdesc.c:3404
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3484
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition pixdesc.h:120
@ AVCOL_RANGE_UNSPECIFIED
Definition pixfmt.h:758
@ AVCOL_RANGE_JPEG
Full range content.
Definition pixfmt.h:792
#define AV_PIX_FMT_RGBA64
Definition pixfmt.h:538
#define AV_PIX_FMT_RGB48
Definition pixfmt.h:534
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_VULKAN
Vulkan hardware images.
Definition pixfmt.h:379
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition pixfmt.h:100
#define AVPALETTE_SIZE
Definition pixfmt.h:32
@ AVCOL_SPC_UNSPECIFIED
Definition pixfmt.h:714
static const uint16_t table[]
Definition prosumer.c:203
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition refstruct.c:121
void av_refstruct_replace(void *dstp, const void *src)
Ensure *dstp refers to the same object as src.
Definition refstruct.c:161
static void * av_refstruct_alloc_ext(size_t size, unsigned flags, void *opaque, void(*free_cb)(AVRefStructOpaque opaque, void *obj))
A wrapper around av_refstruct_alloc_ext_c() for the common case of a non-const qualified opaque.
Definition refstruct.h:94
#define FF_ARRAY_ELEMS(a)
void avpriv_slicethread_free(AVSliceThread **pctx)
Destroy slice threading context.
int avpriv_slicethread_execute2(AVSliceThread *ctx, int nb_jobs, int execute_main)
Execute slice threading.
int avpriv_slicethread_create2(AVSliceThread **pctx, void *priv, int(*worker_func)(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads), int(*main_func)(void *priv), int nb_threads)
Create slice threading context.
A reference to a data buffer.
Definition buffer.h:82
uint8_t * data
The data buffer.
Definition buffer.h:90
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
int width
Definition frame.h:551
int height
Definition frame.h:551
int flags
Frame flags, a combination of AV_FRAME_FLAGS.
Definition frame.h:723
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition frame.h:566
This struct describes a set or pool of "hardware" frames (i.e.
Definition hwcontext.h:118
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
uint64_t flags
Combination of AV_PIX_FMT_FLAG_... flags.
Definition pixdesc.h:94
SwsColor dst
Definition cms.h:62
Main external API structure.
Definition swscale.h:227
int src_h
Width and height of the source frame.
Definition swscale.h:272
int dst_format
Destination pixel format.
Definition swscale.h:275
int intent
Desired ICC intent for color space conversions.
Definition swscale.h:286
int gamma_flag
Use gamma correct scaling.
Definition swscale.h:264
SwsScaler scaler
Scaling filter.
Definition swscale.h:294
int dst_h
Width and height of the destination frame.
Definition swscale.h:273
SwsAlphaBlend alpha_blend
Alpha blending mode.
Definition swscale.h:259
double scaler_params[SWS_NUM_SCALER_PARAMS]
Definition swscale.h:244
int dst_w
Definition swscale.h:273
int dst_h_chr_pos
Destination horizontal chroma position.
Definition swscale.h:281
SwsScaler scaler_sub
Scaler used specifically for up/downsampling subsampled (chroma) planes.
Definition swscale.h:302
int src_w
Deprecated frame property overrides, for the legacy API only.
Definition swscale.h:272
int src_format
Source pixel format.
Definition swscale.h:274
int src_v_chr_pos
Source vertical chroma position in luma grid / 256.
Definition swscale.h:278
int dst_v_chr_pos
Destination vertical chroma position.
Definition swscale.h:280
SwsDither dither
Dither mode.
Definition swscale.h:254
int dst_range
Destination is full range.
Definition swscale.h:277
unsigned flags
Bitmask of SWS_*.
Definition swscale.h:238
int src_range
Source is full range.
Definition swscale.h:276
int src_h_chr_pos
Source horizontal chroma position.
Definition swscale.h:279
SwsColor color
Definition format.h:91
enum AVPixelFormat format
Definition format.h:85
int field
Definition format.h:84
enum AVPixelFormat hw_format
Definition format.h:86
Represents a view into a single field of frame data.
Definition format.h:206
uint8_t * data[4]
Definition format.h:208
int linesize[4]
Definition format.h:209
int padding[4]
Number of bytes after the last pixel of each line that may be read (input frames) or written (output ...
Definition format.h:221
enum AVPixelFormat format
Definition format.h:215
Filter graph, which represents a 'baked' pixel format conversion.
Definition graph.h:133
bool noop
Definition graph.h:138
const SwsFrame * output
Definition graph.h:180
SwsPass ** passes
Sorted sequence of filter passes to apply.
Definition graph.h:154
bool incomplete
Definition graph.h:137
SwsFormat src
Currently active format and processing parameters.
Definition graph.h:166
int num_passes
Definition graph.h:155
const SwsPass * pass
Definition graph.h:178
SwsContext opts_copy
Cached copy of the public options that were used to construct this SwsGraph.
Definition graph.h:161
SwsLut3D * lut3d
3DLUT state used for gamut/tone mapping.
Definition graph.h:171
SwsFormat dst
Definition graph.h:166
SwsContext * ctx
Definition graph.h:134
int plane_copy[4]
Map of planes which directly copied from the input.
Definition graph.h:151
int num_threads
Definition graph.h:136
AVSliceThread * slicethread
Definition graph.h:135
SwsBackend backend
Definition graph.h:139
struct SwsGraph::@252226332054334034101247363262236031041275151254 exec
Temporary execution state inside ff_sws_graph_run(); used to pass data to worker threads.
const SwsFrame * input
Definition graph.h:179
uint32_t pal_rgb[256]
uint32_t pal_yuv[256]
SwsContext ** slice_ctx
SwsColorMap map
Definition lut3d.h:51
Helper struct for representing a list of operations.
Definition ops.h:318
Represents an output buffer for a filter pass.
Definition graph.h:60
int width
Definition graph.h:63
int height
Definition graph.h:63
int width_pad
Definition graph.h:68
SwsFrame frame
Definition graph.h:61
AVFrame * avframe
Definition graph.h:64
int width_align
Definition graph.h:67
int plane_copy[4]
Map of planes which are directly copied from the pass input.
Definition graph.h:77
Represents a single filter pass in the scaling graph.
Definition graph.h:85
int num_slices
Definition graph.h:99
SwsPass * input
Filter input.
Definition graph.h:105
void * priv
Definition graph.h:122
enum AVPixelFormat format
Definition graph.h:96
int lines
Definition graph.h:97
const SwsGraph * graph
Definition graph.h:86
SwsPassFunc run
Filter main execution function.
Definition graph.h:93
SwsPassBuffer * output
Filter output buffer.
Definition graph.h:110
void(* free)(void *priv)
Optional private state and associated free() function.
Definition graph.h:121
SwsBackend backend
Definition graph.h:94
SwsPassSetup setup
Called once from the main thread before running the filter.
Definition graph.h:116
int slice_h
Definition graph.h:98
uint8_t run
Definition svq3.c:207
int ff_swscale(SwsInternal *c, const uint8_t *const src[], const int srcStride[], int srcSliceY, int srcSliceH, uint8_t *const dst[], const int dstStride[], int dstSliceY, int dstSliceH)
Definition swscale.c:280
void ff_update_palette(SwsInternal *c, const uint32_t *pal)
Definition swscale.c:899
external API header
#define SWS_NUM_SCALER_PARAMS
Extra parameters for fine-tuning certain scalers.
Definition swscale.h:243
int ff_sws_init_single_context(SwsContext *sws, SwsFilter *srcFilter, SwsFilter *dstFilter)
Definition utils.c:1187
static av_always_inline int isFloat(enum AVPixelFormat pix_fmt)
static av_always_inline int usePal(enum AVPixelFormat pix_fmt)
SwsBackend ff_sws_enabled_backends(const SwsContext *ctx)
Definition utils.c:60
static av_always_inline int isGray(enum AVPixelFormat pix_fmt)
static SwsInternal * sws_internal(const SwsContext *sws)
void ff_copyPlane(const uint8_t *src, int srcStride, int srcSliceY, int srcSliceH, int width, uint8_t *dst, int dstStride)
static av_always_inline int isALPHA(enum AVPixelFormat pix_fmt)
#define av_free(p)
#define av_mallocz(s)
#define av_log(a,...)
static void error(const char *err)
static uint8_t tmp[40]
Definition aes_ctr.c:52
#define src
Definition vp8dsp.c:248
static char buffer[20]
Definition seek.c:33
#define height
Definition dsp.h:99
#define width
Definition dsp.h:99
RefStruct is an API for creating reference-counted objects with minimal overhead.
Definition refstruct.h:58
static const Backend backends[]
static double c[64]
AVBufferRef * ff_sws_vk_device_ref(SwsContext *sws)
Returns the Vulkan device reference associated with sws, or NULL if Vulkan has not been initialized f...
Definition ops.c:73