FFmpeg
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/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 */
43 {
44  int ret = av_image_check_size2(dst->width, dst->height, INT64_MAX,
45  dst->format, 0, NULL);
46  if (ret < 0)
47  return ret;
48 
49  const int align = av_cpu_max_align();
50  const int aligned_w = FFALIGN(dst->width, align);
51  ret = av_image_fill_linesizes(dst->linesize, dst->format, aligned_w);
52  if (ret < 0)
53  return ret;
54 
55  ptrdiff_t linesize1[4];
56  for (int i = 0; i < 4; i++)
57  linesize1[i] = dst->linesize[i] = FFALIGN(dst->linesize[i], align);
58 
59  size_t sizes[4];
60  ret = av_image_fill_plane_sizes(sizes, dst->format, dst->height, linesize1);
61  if (ret < 0)
62  return ret;
63 
64  for (int i = 0; i < 4; i++) {
65  if (!sizes[i])
66  break;
68  if (!buf)
69  return AVERROR(ENOMEM);
70  dst->data[i] = buf->data;
71  dst->buf[i] = buf;
72  }
73 
74  return 0;
75 }
76 
77 static int pass_alloc_output(SwsPass *pass)
78 {
79  if (!pass || pass->output->avframe)
80  return 0;
81 
82  SwsPassBuffer *buffer = pass->output;
83  AVFrame *avframe = av_frame_alloc();
84  if (!avframe)
85  return AVERROR(ENOMEM);
86  avframe->format = pass->format;
87  avframe->width = buffer->width;
88  avframe->height = buffer->height;
89 
90  int ret = frame_alloc_planes(avframe);
91  if (ret < 0) {
92  av_frame_free(&avframe);
93  return ret;
94  }
95 
96  buffer->avframe = avframe;
97  ff_sws_frame_from_avframe(&buffer->frame, avframe);
98  return 0;
99 }
100 
101 static void free_buffer(AVRefStructOpaque opaque, void *obj)
102 {
103  SwsPassBuffer *buffer = obj;
104  av_frame_free(&buffer->avframe);
105 }
106 
107 static void pass_free(SwsPass *pass)
108 {
109  if (pass->free)
110  pass->free(pass->priv);
111  av_refstruct_unref(&pass->output);
112  av_free(pass);
113 }
114 
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 {
121  int ret;
122  SwsPass *pass = av_mallocz(sizeof(*pass));
123  if (!pass) {
124  if (free_cb)
125  free_cb(priv);
126  return AVERROR(ENOMEM);
127  }
128 
129  pass->graph = graph;
130  pass->run = run;
131  pass->setup = setup;
132  pass->priv = priv;
133  pass->free = free_cb;
134  pass->format = fmt;
135  pass->width = width;
136  pass->height = height;
137  pass->input = input;
138  pass->output = av_refstruct_alloc_ext(sizeof(*pass->output), 0, NULL, free_buffer);
139  if (!pass->output) {
140  ret = AVERROR(ENOMEM);
141  goto fail;
142  }
143 
145  if (ret < 0)
146  goto fail;
147 
148  if (!align) {
149  pass->slice_h = pass->height;
150  pass->num_slices = 1;
151  } else {
152  pass->slice_h = (pass->height + graph->num_threads - 1) / graph->num_threads;
153  pass->slice_h = FFALIGN(pass->slice_h, align);
154  pass->num_slices = (pass->height + pass->slice_h - 1) / pass->slice_h;
155  }
156 
157  /* Align output buffer to include extra slice padding */
158  pass->output->width = pass->width;
159  pass->output->height = pass->slice_h * pass->num_slices;
160 
161  ret = av_dynarray_add_nofree(&graph->passes, &graph->num_passes, pass);
162  if (ret < 0)
163  goto fail;
164 
165  *out_pass = pass;
166  return 0;
167 
168 fail:
169  pass_free(pass);
170  return ret;
171 }
172 
173 static void frame_shift(const SwsFrame *f, const int y, uint8_t *data[4])
174 {
175  for (int i = 0; i < 4; i++) {
176  if (f->data[i])
177  data[i] = f->data[i] + (y >> ff_fmt_vshift(f->format, i)) * f->linesize[i];
178  else
179  data[i] = NULL;
180  }
181 }
182 
183 static void run_copy(const SwsFrame *out, const SwsFrame *in, int y, int h,
184  const SwsPass *pass)
185 {
186  uint8_t *in_data[4], *out_data[4];
187  frame_shift(in, y, in_data);
188  frame_shift(out, y, out_data);
189 
190  for (int i = 0; i < 4 && out_data[i]; i++) {
191  const int lines = h >> ff_fmt_vshift(in->format, i);
192  av_assert1(in_data[i]);
193 
194  if (in_data[i] == out_data[i]) {
195  av_assert0(in->linesize[i] == out->linesize[i]);
196  } else if (in->linesize[i] == out->linesize[i]) {
197  memcpy(out_data[i], in_data[i], lines * out->linesize[i]);
198  } else {
199  const int linesize = FFMIN(out->linesize[i], in->linesize[i]);
200  for (int j = 0; j < lines; j++) {
201  memcpy(out_data[i], in_data[i], linesize);
202  in_data[i] += in->linesize[i];
203  out_data[i] += out->linesize[i];
204  }
205  }
206  }
207 }
208 
209 static void run_rgb0(const SwsFrame *out, const SwsFrame *in, int y, int h,
210  const SwsPass *pass)
211 {
212  SwsInternal *c = pass->priv;
213  const int x0 = c->src0Alpha - 1;
214  const int w4 = 4 * pass->width;
215  const int src_stride = in->linesize[0];
216  const int dst_stride = out->linesize[0];
217  const uint8_t *src = in->data[0] + y * src_stride;
218  uint8_t *dst = out->data[0] + y * dst_stride;
219 
220  for (int y = 0; y < h; y++) {
221  memcpy(dst, src, w4 * sizeof(*dst));
222  for (int x = x0; x < w4; x += 4)
223  dst[x] = 0xFF;
224 
225  src += src_stride;
226  dst += dst_stride;
227  }
228 }
229 
230 static void run_xyz2rgb(const SwsFrame *out, const SwsFrame *in, int y, int h,
231  const SwsPass *pass)
232 {
233  const SwsInternal *c = pass->priv;
234  c->xyz12Torgb48(c, out->data[0] + y * out->linesize[0], out->linesize[0],
235  in->data[0] + y * in->linesize[0], in->linesize[0],
236  pass->width, h);
237 }
238 
239 static void run_rgb2xyz(const SwsFrame *out, const SwsFrame *in, int y, int h,
240  const SwsPass *pass)
241 {
242  const SwsInternal *c = pass->priv;
243  c->rgb48Toxyz12(c, out->data[0] + y * out->linesize[0], out->linesize[0],
244  in->data[0] + y * in->linesize[0], in->linesize[0],
245  pass->width, h);
246 }
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 {
256  SwsContext *sws = priv;
257  sws_free_context(&sws);
258 }
259 
260 static void setup_legacy_swscale(const SwsFrame *out, const SwsFrame *in,
261  const SwsPass *pass)
262 {
263  SwsContext *sws = pass->priv;
264  SwsInternal *c = sws_internal(sws);
265  if (sws->flags & SWS_BITEXACT && sws->dither == SWS_DITHER_ED && c->dither_error[0]) {
266  for (int i = 0; i < 4; i++)
267  memset(c->dither_error[i], 0, sizeof(c->dither_error[0][0]) * (sws->dst_w + 2));
268  }
269 
270  if (usePal(sws->src_format))
271  ff_update_palette(c, (const uint32_t *) in->data[1]);
272 }
273 
274 static inline SwsContext *slice_ctx(const SwsPass *pass, int y)
275 {
276  SwsContext *sws = pass->priv;
277  SwsInternal *parent = sws_internal(sws);
278  if (pass->num_slices == 1)
279  return sws;
280 
281  av_assert1(parent->nb_slice_ctx == pass->num_slices);
282  sws = parent->slice_ctx[y / pass->slice_h];
283 
284  if (usePal(sws->src_format)) {
285  SwsInternal *sub = sws_internal(sws);
286  memcpy(sub->pal_yuv, parent->pal_yuv, sizeof(sub->pal_yuv));
287  memcpy(sub->pal_rgb, parent->pal_rgb, sizeof(sub->pal_rgb));
288  }
289 
290  return sws;
291 }
292 
293 static void run_legacy_unscaled(const SwsFrame *out, const SwsFrame *in,
294  int y, int h, const SwsPass *pass)
295 {
296  SwsContext *sws = slice_ctx(pass, y);
297  SwsInternal *c = sws_internal(sws);
298  uint8_t *in_data[4];
299  frame_shift(in, y, in_data);
300 
301  c->convert_unscaled(c, (const uint8_t *const *) in_data, in->linesize, y, h,
302  out->data, out->linesize);
303 }
304 
305 static void run_legacy_swscale(const SwsFrame *out, const SwsFrame *in,
306  int y, int h, const SwsPass *pass)
307 {
308  SwsContext *sws = slice_ctx(pass, y);
309  SwsInternal *c = sws_internal(sws);
310  uint8_t *out_data[4];
311  frame_shift(out, y, out_data);
312 
313  ff_swscale(c, (const uint8_t *const *) in->data, in->linesize, 0,
314  sws->src_h, out_data, out->linesize, y, h);
315 }
316 
317 static void get_chroma_pos(SwsGraph *graph, int *h_chr_pos, int *v_chr_pos,
318  const SwsFormat *fmt)
319 {
320  enum AVChromaLocation chroma_loc = fmt->loc;
321  const int sub_x = fmt->desc->log2_chroma_w;
322  const int sub_y = fmt->desc->log2_chroma_h;
323  int x_pos, y_pos;
324 
325  /* Explicitly default to center siting for compatibility with swscale */
326  if (chroma_loc == AVCHROMA_LOC_UNSPECIFIED) {
327  chroma_loc = AVCHROMA_LOC_CENTER;
328  graph->incomplete |= sub_x || sub_y;
329  }
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  av_chroma_location_enum_to_pos(&x_pos, &y_pos, chroma_loc);
335  x_pos *= (1 << sub_x) - 1;
336  y_pos *= (1 << sub_y) - 1;
337 
338  /* Fix vertical chroma position for interlaced frames */
339  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  if (graph->field == FIELD_BOTTOM)
348  y_pos += (256 << sub_y) - 256;
349 
350  /* Luma row distance is doubled for fields, so halve offsets */
351  y_pos >>= 1;
352  }
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  *h_chr_pos = sub_x ? x_pos : -513;
357  *v_chr_pos = sub_y ? y_pos : -513;
358 }
359 
360 static void legacy_chr_pos(SwsGraph *graph, int *chr_pos, int override, int *warned)
361 {
362  if (override == -513 || override == *chr_pos)
363  return;
364 
365  if (!*warned) {
367  "Setting chroma position directly is deprecated, make sure "
368  "the frame is tagged with the correct chroma location.\n");
369  *warned = 1;
370  }
371 
372  *chr_pos = override;
373 }
374 
375 /* Takes over ownership of `sws` */
376 static int init_legacy_subpass(SwsGraph *graph, SwsContext *sws,
378 {
379  SwsInternal *c = sws_internal(sws);
380  const int src_w = sws->src_w, src_h = sws->src_h;
381  const int dst_w = sws->dst_w, dst_h = sws->dst_h;
382  const int unscaled = src_w == dst_w && src_h == dst_h;
383  int align = c->dst_slice_align;
384  SwsPass *pass = NULL;
385  int ret;
386 
387  if (c->cascaded_context[0]) {
388  const int num_cascaded = c->cascaded_context[2] ? 3 : 2;
389  for (int i = 0; i < num_cascaded; i++) {
390  const int is_last = i + 1 == num_cascaded;
391 
392  /* Steal cascaded context, so we can manage its lifetime independently */
393  SwsContext *sub = c->cascaded_context[i];
394  c->cascaded_context[i] = NULL;
395 
396  ret = init_legacy_subpass(graph, sub, input, is_last ? output : &input);
397  if (ret < 0)
398  break;
399  }
400 
401  sws_free_context(&sws);
402  return ret;
403  }
404 
405  if (sws->dither == SWS_DITHER_ED && !c->convert_unscaled)
406  align = 0; /* disable slice threading */
407 
408  if (c->src0Alpha && !c->dst0Alpha && isALPHA(sws->dst_format)) {
409  ret = ff_sws_graph_add_pass(graph, AV_PIX_FMT_RGBA, src_w, src_h, input,
410  1, run_rgb0, NULL, c, NULL, &input);
411  if (ret < 0) {
412  sws_free_context(&sws);
413  return ret;
414  }
415  }
416 
417  if (c->srcXYZ && !(c->dstXYZ && unscaled)) {
418  ret = ff_sws_graph_add_pass(graph, AV_PIX_FMT_RGB48, src_w, src_h, input,
419  1, run_xyz2rgb, NULL, c, NULL, &input);
420  if (ret < 0) {
421  sws_free_context(&sws);
422  return ret;
423  }
424  }
425 
426  ret = ff_sws_graph_add_pass(graph, sws->dst_format, dst_w, dst_h, input, align,
427  c->convert_unscaled ? run_legacy_unscaled : run_legacy_swscale,
429  if (ret < 0)
430  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  if (pass->num_slices > 1) {
440  c->slice_ctx = av_calloc(pass->num_slices, sizeof(*c->slice_ctx));
441  if (!c->slice_ctx)
442  return AVERROR(ENOMEM);
443 
444  for (int i = 0; i < pass->num_slices; i++) {
445  SwsContext *slice;
446  SwsInternal *c2;
447  slice = c->slice_ctx[i] = sws_alloc_context();
448  if (!slice)
449  return AVERROR(ENOMEM);
450  c->nb_slice_ctx++;
451 
452  c2 = sws_internal(slice);
453  c2->parent = sws;
454 
455  ret = av_opt_copy(slice, sws);
456  if (ret < 0)
457  return ret;
458 
460  if (ret < 0)
461  return ret;
462 
463  sws_setColorspaceDetails(slice, c->srcColorspaceTable,
464  slice->src_range, c->dstColorspaceTable,
465  slice->dst_range, c->brightness, c->contrast,
466  c->saturation);
467 
468  for (int i = 0; i < FF_ARRAY_ELEMS(c->srcColorspaceTable); i++) {
469  c2->srcColorspaceTable[i] = c->srcColorspaceTable[i];
470  c2->dstColorspaceTable[i] = c->dstColorspaceTable[i];
471  }
472  }
473  }
474 
475  if (c->dstXYZ && !(c->srcXYZ && unscaled)) {
476  ret = ff_sws_graph_add_pass(graph, AV_PIX_FMT_RGB48, dst_w, dst_h, pass,
477  1, run_rgb2xyz, NULL, c, NULL, &pass);
478  if (ret < 0)
479  return ret;
480  }
481 
482  *output = pass;
483  return 0;
484 }
485 
486 static int add_legacy_sws_pass(SwsGraph *graph, const SwsFormat *src,
487  const SwsFormat *dst, SwsPass *input,
488  SwsPass **output)
489 {
490  int ret, warned = 0;
491  SwsContext *const ctx = graph->ctx;
492  if (src->hw_format != AV_PIX_FMT_NONE || dst->hw_format != AV_PIX_FMT_NONE)
493  return AVERROR(ENOTSUP);
494 
495  SwsContext *sws = sws_alloc_context();
496  if (!sws)
497  return AVERROR(ENOMEM);
498 
499  sws->flags = ctx->flags;
500  sws->dither = ctx->dither;
501  sws->alpha_blend = ctx->alpha_blend;
502  sws->gamma_flag = ctx->gamma_flag;
503 
504  sws->src_w = src->width;
505  sws->src_h = src->height;
506  sws->src_format = src->format;
507  sws->src_range = src->range == AVCOL_RANGE_JPEG;
508 
509  sws->dst_w = dst->width;
510  sws->dst_h = dst->height;
511  sws->dst_format = dst->format;
512  sws->dst_range = dst->range == AVCOL_RANGE_JPEG;
513  get_chroma_pos(graph, &sws->src_h_chr_pos, &sws->src_v_chr_pos, src);
514  get_chroma_pos(graph, &sws->dst_h_chr_pos, &sws->dst_v_chr_pos, dst);
515 
516  graph->incomplete |= src->range == AVCOL_RANGE_UNSPECIFIED;
517  graph->incomplete |= dst->range == AVCOL_RANGE_UNSPECIFIED;
518 
519  /* Allow overriding chroma position with the legacy API */
520  legacy_chr_pos(graph, &sws->src_h_chr_pos, ctx->src_h_chr_pos, &warned);
521  legacy_chr_pos(graph, &sws->src_v_chr_pos, ctx->src_v_chr_pos, &warned);
522  legacy_chr_pos(graph, &sws->dst_h_chr_pos, ctx->dst_h_chr_pos, &warned);
523  legacy_chr_pos(graph, &sws->dst_v_chr_pos, ctx->dst_v_chr_pos, &warned);
524 
525  sws->scaler_params[0] = ctx->scaler_params[0];
526  sws->scaler_params[1] = ctx->scaler_params[1];
527 
528  ret = sws_init_context(sws, NULL, NULL);
529  if (ret < 0) {
530  sws_free_context(&sws);
531  return ret;
532  }
533 
534  /* Set correct color matrices */
535  {
536  int in_full, out_full, brightness, contrast, saturation;
537  const int *inv_table, *table;
538  sws_getColorspaceDetails(sws, (int **)&inv_table, &in_full,
539  (int **)&table, &out_full,
540  &brightness, &contrast, &saturation);
541 
542  inv_table = sws_getCoefficients(src->csp);
543  table = sws_getCoefficients(dst->csp);
544 
545  graph->incomplete |= src->csp != dst->csp &&
546  (src->csp == AVCOL_SPC_UNSPECIFIED ||
547  dst->csp == AVCOL_SPC_UNSPECIFIED);
548 
549  sws_setColorspaceDetails(sws, inv_table, in_full, table, out_full,
550  brightness, contrast, saturation);
551  }
552 
553  return init_legacy_subpass(graph, sws, input, output);
554 }
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 {
566 
567  SwsContext *ctx = graph->ctx;
568  SwsOpList *ops = NULL;
569  int ret = AVERROR(ENOTSUP);
570 
571  /* Mark the entire new ops infrastructure as experimental for now */
572  if (!(ctx->flags & SWS_UNSTABLE))
573  goto fail;
574 
575  /* The new format conversion layer cannot scale for now */
576  if (src->width != dst->width || src->height != dst->height ||
577  src->desc->log2_chroma_h || src->desc->log2_chroma_w ||
578  dst->desc->log2_chroma_h || dst->desc->log2_chroma_w)
579  goto fail;
580 
581  /* The new code does not yet support alpha blending */
582  if (src->desc->flags & AV_PIX_FMT_FLAG_ALPHA &&
583  ctx->alpha_blend != SWS_ALPHA_BLEND_NONE)
584  goto fail;
585 
586  ops = ff_sws_op_list_alloc();
587  if (!ops)
588  return AVERROR(ENOMEM);
589  ops->src = *src;
590  ops->dst = *dst;
591 
592  ret = ff_sws_decode_pixfmt(ops, src->format);
593  if (ret < 0)
594  goto fail;
595  ret = ff_sws_decode_colors(ctx, type, ops, src, &graph->incomplete);
596  if (ret < 0)
597  goto fail;
598  ret = ff_sws_encode_colors(ctx, type, ops, src, dst, &graph->incomplete);
599  if (ret < 0)
600  goto fail;
601  ret = ff_sws_encode_pixfmt(ops, dst->format);
602  if (ret < 0)
603  goto fail;
604 
605  av_log(ctx, AV_LOG_VERBOSE, "Conversion pass for %s -> %s:\n",
606  av_get_pix_fmt_name(src->format), av_get_pix_fmt_name(dst->format));
607 
608  av_log(ctx, AV_LOG_DEBUG, "Unoptimized operation list:\n");
610 
612  if (ret < 0)
613  goto fail;
614 
615  ret = 0;
616  /* fall through */
617 
618 fail:
619  ff_sws_op_list_free(&ops);
620  if (ret == AVERROR(ENOTSUP))
621  return add_legacy_sws_pass(graph, src, dst, input, output);
622  return ret;
623 }
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 {
635  SwsLut3D *lut = priv;
636  ff_sws_lut3d_free(&lut);
637 }
638 
639 static void setup_lut3d(const SwsFrame *out, const SwsFrame *in, const SwsPass *pass)
640 {
641  SwsLut3D *lut = pass->priv;
642 
643  /* Update dynamic frame metadata from the original source frame */
644  ff_sws_lut3d_update(lut, &pass->graph->src.color);
645 }
646 
647 static void run_lut3d(const SwsFrame *out, const SwsFrame *in, int y, int h,
648  const SwsPass *pass)
649 {
650  SwsLut3D *lut = pass->priv;
651  uint8_t *in_data[4], *out_data[4];
652  frame_shift(in, y, in_data);
653  frame_shift(out, y, out_data);
654 
655  ff_sws_lut3d_apply(lut, in_data[0], in->linesize[0], out_data[0],
656  out->linesize[0], pass->width, h);
657 }
658 
661 {
662  enum AVPixelFormat fmt_in, fmt_out;
663  SwsColorMap map = {0};
664  SwsLut3D *lut;
665  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  if (isGray(dst.format)) {
674  dst.color = src.color;
675  } else if (isGray(src.format)) {
676  src.color = dst.color;
677  }
678 
679  /* Fully infer color spaces before color mapping logic */
680  graph->incomplete |= ff_infer_colors(&src.color, &dst.color);
681 
682  map.intent = graph->ctx->intent;
683  map.src = src.color;
684  map.dst = dst.color;
685 
687  return 0;
688 
689  if (src.hw_format != AV_PIX_FMT_NONE || dst.hw_format != AV_PIX_FMT_NONE)
690  return AVERROR(ENOTSUP);
691 
692  lut = ff_sws_lut3d_alloc();
693  if (!lut)
694  return AVERROR(ENOMEM);
695 
696  fmt_in = ff_sws_lut3d_pick_pixfmt(src, 0);
697  fmt_out = ff_sws_lut3d_pick_pixfmt(dst, 1);
698  if (fmt_in != src.format) {
699  SwsFormat tmp = src;
700  tmp.format = fmt_in;
701  ret = add_convert_pass(graph, &src, &tmp, input, &input);
702  if (ret < 0)
703  return ret;
704  }
705 
706  ret = ff_sws_lut3d_generate(lut, fmt_in, fmt_out, &map);
707  if (ret < 0) {
708  ff_sws_lut3d_free(&lut);
709  return ret;
710  }
711 
712  return ff_sws_graph_add_pass(graph, fmt_out, src.width, src.height,
713  input, 1, run_lut3d, setup_lut3d, lut,
714  free_lut3d, output);
715 }
716 
717 /***************************************
718  * Main filter graph construction code *
719  ***************************************/
720 
721 static int init_passes(SwsGraph *graph)
722 {
723  SwsFormat src = graph->src;
724  SwsFormat dst = graph->dst;
725  SwsPass *pass = NULL; /* read from main input image */
726  int ret;
727 
728  ret = adapt_colors(graph, src, dst, pass, &pass);
729  if (ret < 0)
730  return ret;
731  src.format = pass ? pass->format : src.format;
732  src.color = dst.color;
733 
734  if (!ff_fmt_equal(&src, &dst)) {
735  ret = add_convert_pass(graph, &src, &dst, pass, &pass);
736  if (ret < 0)
737  return ret;
738  }
739 
740  if (pass)
741  return 0;
742 
743  /* No passes were added, so no operations were necessary */
744  graph->noop = 1;
745 
746  /* Add threaded memcpy pass */
747  return ff_sws_graph_add_pass(graph, dst.format, dst.width, dst.height,
748  pass, 1, run_copy, NULL, NULL, NULL, &pass);
749 }
750 
751 static void sws_graph_worker(void *priv, int jobnr, int threadnr, int nb_jobs,
752  int nb_threads)
753 {
754  SwsGraph *graph = priv;
755  const SwsPass *pass = graph->exec.pass;
756  const int slice_y = jobnr * pass->slice_h;
757  const int slice_h = FFMIN(pass->slice_h, pass->height - slice_y);
758 
759  pass->run(graph->exec.output, graph->exec.input, slice_y, slice_h, pass);
760 }
761 
763  int field, SwsGraph **out_graph)
764 {
765  int ret;
766  SwsGraph *graph = av_mallocz(sizeof(*graph));
767  if (!graph)
768  return AVERROR(ENOMEM);
769 
770  graph->ctx = ctx;
771  graph->src = *src;
772  graph->dst = *dst;
773  graph->field = field;
774  graph->opts_copy = *ctx;
775 
776  if (ctx->threads == 1) {
777  graph->num_threads = 1;
778  } else {
779  ret = avpriv_slicethread_create(&graph->slicethread, (void *) graph,
780  sws_graph_worker, NULL, ctx->threads);
781  if (ret == AVERROR(ENOSYS)) {
782  /* Fall back to single threaded operation */
783  graph->num_threads = 1;
784  } else if (ret < 0) {
785  goto error;
786  } else {
787  graph->num_threads = ret;
788  }
789  }
790 
791  ret = init_passes(graph);
792  if (ret < 0)
793  goto error;
794 
795  *out_graph = graph;
796  return 0;
797 
798 error:
799  ff_sws_graph_free(&graph);
800  return ret;
801 }
802 
804 {
805  SwsGraph *graph = *pgraph;
806  if (!graph)
807  return;
808 
810 
811  for (int i = 0; i < graph->num_passes; i++)
812  pass_free(graph->passes[i]);
813  av_free(graph->passes);
814 
815  av_free(graph);
816  *pgraph = NULL;
817 }
818 
819 /* Tests only options relevant to SwsGraph */
820 static int opts_equal(const SwsContext *c1, const SwsContext *c2)
821 {
822  return c1->flags == c2->flags &&
823  c1->threads == c2->threads &&
824  c1->dither == c2->dither &&
825  c1->alpha_blend == c2->alpha_blend &&
826  c1->gamma_flag == c2->gamma_flag &&
827  c1->src_h_chr_pos == c2->src_h_chr_pos &&
828  c1->src_v_chr_pos == c2->src_v_chr_pos &&
829  c1->dst_h_chr_pos == c2->dst_h_chr_pos &&
830  c1->dst_v_chr_pos == c2->dst_v_chr_pos &&
831  c1->intent == c2->intent &&
832  !memcmp(c1->scaler_params, c2->scaler_params, sizeof(c1->scaler_params));
833 
834 }
835 
837  int field, SwsGraph **out_graph)
838 {
839  SwsGraph *graph = *out_graph;
840  if (graph && ff_fmt_equal(&graph->src, src) &&
841  ff_fmt_equal(&graph->dst, dst) &&
842  opts_equal(ctx, &graph->opts_copy))
843  {
844  ff_sws_graph_update_metadata(graph, &src->color);
845  return 0;
846  }
847 
848  ff_sws_graph_free(out_graph);
849  return ff_sws_graph_create(ctx, dst, src, field, out_graph);
850 }
851 
853 {
854  if (!color)
855  return;
856 
858 }
859 
860 static void get_field(SwsGraph *graph, const AVFrame *avframe, SwsFrame *frame)
861 {
863 
864  if (!(avframe->flags & AV_FRAME_FLAG_INTERLACED)) {
865  av_assert1(!graph->field);
866  return;
867  }
868 
869  if (graph->field == FIELD_BOTTOM) {
870  /* Odd rows, offset by one line */
872  for (int i = 0; i < 4; i++) {
873  if (frame->data[i])
874  frame->data[i] += frame->linesize[i];
875  if (desc->flags & AV_PIX_FMT_FLAG_PAL)
876  break;
877  }
878  }
879 
880  /* Take only every second line */
881  for (int i = 0; i < 4; i++)
882  frame->linesize[i] <<= 1;
883 
884  frame->height = (frame->height + (graph->field == FIELD_TOP)) >> 1;
885 }
886 
887 void ff_sws_graph_run(SwsGraph *graph, const AVFrame *dst, const AVFrame *src)
888 {
889  av_assert0(dst->format == graph->dst.hw_format || dst->format == graph->dst.format);
890  av_assert0(src->format == graph->src.hw_format || src->format == graph->src.format);
891 
892  SwsFrame src_field, dst_field;
893  get_field(graph, dst, &dst_field);
894  get_field(graph, src, &src_field);
895 
896  for (int i = 0; i < graph->num_passes; i++) {
897  const SwsPass *pass = graph->passes[i];
898  graph->exec.pass = pass;
899  graph->exec.input = pass->input ? &pass->input->output->frame : &src_field;
900  graph->exec.output = pass->output->avframe ? &pass->output->frame : &dst_field;
901  if (pass->setup)
902  pass->setup(graph->exec.output, graph->exec.input, pass);
903 
904  if (pass->num_slices == 1) {
905  pass->run(graph->exec.output, graph->exec.input, 0, pass->height, pass);
906  } else {
908  }
909  }
910 }
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:32
sws_setColorspaceDetails
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:850
ff_sws_op_list_free
void ff_sws_op_list_free(SwsOpList **p_ops)
Definition: ops.c:527
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:216
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
add_legacy_sws_pass
static int add_legacy_sws_pass(SwsGraph *graph, const SwsFormat *src, const SwsFormat *dst, SwsPass *input, SwsPass **output)
Definition: graph.c:486
SwsGraph::slicethread
AVSliceThread * slicethread
Definition: graph.h:111
SwsGraph::ctx
SwsContext * ctx
Definition: graph.h:110
SwsPass
Represents a single filter pass in the scaling graph.
Definition: graph.h:69
ff_sws_op_list_alloc
SwsOpList * ff_sws_op_list_alloc(void)
Definition: ops.c:515
SwsGraph::pass
const SwsPass * pass
Definition: graph.h:139
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
SwsGraph::passes
SwsPass ** passes
Sorted sequence of filter passes to apply.
Definition: graph.h:119
adapt_colors
static int adapt_colors(SwsGraph *graph, SwsFormat src, SwsFormat dst, SwsPass *input, SwsPass **output)
Definition: graph.c:659
out
static FILE * out
Definition: movenc.c:55
setup_legacy_swscale
static void setup_legacy_swscale(const SwsFrame *out, const SwsFrame *in, const SwsPass *pass)
Definition: graph.c:260
color
Definition: vf_paletteuse.c:513
init_passes
static int init_passes(SwsGraph *graph)
Definition: graph.c:721
SwsFormat::interlaced
int interlaced
Definition: format.h:79
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3456
SwsContext::src_w
int src_w
Deprecated frame property overrides, for the legacy API only.
Definition: swscale.h:237
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
ff_sws_graph_reinit
int ff_sws_graph_reinit(SwsContext *ctx, const SwsFormat *dst, const SwsFormat *src, int field, SwsGraph **out_graph)
Wrapper around ff_sws_graph_create() that reuses the existing graph if the format is compatible.
Definition: graph.c:836
AVRefStructOpaque
RefStruct is an API for creating reference-counted objects with minimal overhead.
Definition: refstruct.h:58
SwsPass::format
enum AVPixelFormat format
Definition: graph.h:78
saturation
static IPT saturation(const CmsCtx *ctx, IPT ipt)
Definition: cms.c:559
run_rgb0
static void run_rgb0(const SwsFrame *out, const SwsFrame *in, int y, int h, const SwsPass *pass)
Definition: graph.c:209
output
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
Definition: filter_design.txt:226
FIELD_TOP
@ FIELD_TOP
Definition: format.h:56
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:64
ff_sws_decode_colors
int ff_sws_decode_colors(SwsContext *ctx, SwsPixelType type, SwsOpList *ops, const SwsFormat *fmt, bool *incomplete)
Append a set of operations for transforming decoded pixel values to/from normalized RGB in the specif...
SwsGraph::src
SwsFormat src
Currently active format and processing parameters.
Definition: graph.h:131
avpriv_slicethread_execute
void avpriv_slicethread_execute(AVSliceThread *ctx, int nb_jobs, int execute_main)
Execute slice threading.
Definition: slicethread.c:270
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:427
pixdesc.h
frame_shift
static void frame_shift(const SwsFrame *f, const int y, uint8_t *data[4])
Definition: graph.c:173
ops.h
AVFrame::width
int width
Definition: frame.h:499
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:777
isGray
static av_always_inline int isGray(enum AVPixelFormat pix_fmt)
Definition: swscale_internal.h:802
SwsGraph::output
const SwsFrame * output
Definition: graph.h:141
run_copy
static void run_copy(const SwsFrame *out, const SwsFrame *in, int y, int h, const SwsPass *pass)
Definition: graph.c:183
pass_free
static void pass_free(SwsPass *pass)
Definition: graph.c:107
SWS_BITEXACT
@ SWS_BITEXACT
Definition: swscale.h:158
SwsPass::setup
SwsPassSetup setup
Called once from the main thread before running the filter.
Definition: graph.h:97
table
static const uint16_t table[]
Definition: prosumer.c:203
data
const char data[16]
Definition: mxf.c:149
SwsContext::flags
unsigned flags
Bitmask of SWS_*.
Definition: swscale.h:204
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:226
ff_sws_lut3d_pick_pixfmt
enum AVPixelFormat ff_sws_lut3d_pick_pixfmt(SwsFormat fmt, int output)
Pick the best compatible pixfmt for a given SwsFormat.
Definition: lut3d.c:52
AVFrame::flags
int flags
Frame flags, a combination of AV_FRAME_FLAGS.
Definition: frame.h:671
SwsPass::free
void(* free)(void *priv)
Optional private state and associated free() function.
Definition: graph.h:102
c1
static const uint64_t c1
Definition: murmur3.c:52
format.h
SWS_ALPHA_BLEND_NONE
@ SWS_ALPHA_BLEND_NONE
Definition: swscale.h:89
ff_sws_init_single_context
int ff_sws_init_single_context(SwsContext *sws, SwsFilter *srcFilter, SwsFilter *dstFilter)
Definition: utils.c:1122
SwsColorMap
Definition: cms.h:60
SwsPixelType
SwsPixelType
Copyright (C) 2025 Niklas Haas.
Definition: ops.h:30
SwsPass::width
int width
Definition: graph.h:79
ff_sws_op_list_print
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:754
ff_color_update_dynamic
static void ff_color_update_dynamic(SwsColor *dst, const SwsColor *src)
Definition: format.h:70
init_legacy_subpass
static int init_legacy_subpass(SwsGraph *graph, SwsContext *sws, SwsPass *input, SwsPass **output)
Definition: graph.c:376
SWS_PIXEL_F32
@ SWS_PIXEL_F32
Definition: ops.h:35
ff_sws_graph_run
void 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:887
SwsFrame::data
uint8_t * data[4]
Definition: format.h:190
avpriv_slicethread_create
int avpriv_slicethread_create(AVSliceThread **pctx, void *priv, void(*worker_func)(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads), void(*main_func)(void *priv), int nb_threads)
Create slice threading context.
Definition: slicethread.c:261
macros.h
fail
#define fail()
Definition: checkasm.h:220
SwsContext::src_v_chr_pos
int src_v_chr_pos
Source vertical chroma position in luma grid / 256.
Definition: swscale.h:243
slice_ctx
static SwsContext * slice_ctx(const SwsPass *pass, int y)
Definition: graph.c:274
sws_init_context
av_warn_unused_result int sws_init_context(SwsContext *sws_context, SwsFilter *srcFilter, SwsFilter *dstFilter)
Initialize the swscaler context sws_context.
Definition: utils.c:1897
SwsGraph::opts_copy
SwsContext opts_copy
Cached copy of the public options that were used to construct this SwsGraph.
Definition: graph.h:126
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
SwsPassBuffer::frame
SwsFrame frame
Definition: graph.h:58
ff_sws_compile_pass
int ff_sws_compile_pass(SwsGraph *graph, SwsOpList *ops, int flags, const SwsFormat *dst, SwsPass *input, SwsPass **output)
Resolves an operation list to a graph pass.
Definition: ops_dispatch.c:364
refstruct.h
av_image_check_size2
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
ff_sws_graph_create
int ff_sws_graph_create(SwsContext *ctx, const SwsFormat *dst, const SwsFormat *src, int field, SwsGraph **out_graph)
Allocate and initialize the filter graph.
Definition: graph.c:762
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:52
SwsFrame
Represents a view into a single field of frame data.
Definition: format.h:188
avassert.h
AV_LOG_TRACE
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:236
SwsInternal::pal_rgb
uint32_t pal_rgb[256]
Definition: swscale_internal.h:400
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
legacy_chr_pos
static void legacy_chr_pos(SwsGraph *graph, int *chr_pos, int override, int *warned)
Definition: graph.c:360
SwsFrame::format
enum AVPixelFormat format
Definition: format.h:197
SwsContext::dither
SwsDither dither
Dither mode.
Definition: swscale.h:219
SwsPass::priv
void * priv
Definition: graph.h:103
run_xyz2rgb
static void run_xyz2rgb(const SwsFrame *out, const SwsFrame *in, int y, int h, const SwsPass *pass)
Definition: graph.c:230
SwsInternal::nb_slice_ctx
int nb_slice_ctx
Definition: swscale_internal.h:344
av_image_fill_linesizes
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
SwsInternal::slice_ctx
SwsContext ** slice_ctx
Definition: swscale_internal.h:342
av_chroma_location_enum_to_pos
int av_chroma_location_enum_to_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
Converts AVChromaLocation to swscale x/y chroma position.
Definition: pixdesc.c:3898
ff_update_palette
void ff_update_palette(SwsInternal *c, const uint32_t *pal)
Definition: swscale.c:874
AVFormatContext::flags
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1414
ff_sws_lut3d_alloc
SwsLut3D * ff_sws_lut3d_alloc(void)
Definition: lut3d.c:32
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:42
SwsContext::intent
int intent
Desired ICC intent for color space conversions.
Definition: swscale.h:251
av_refstruct_alloc_ext
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
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:231
SwsGraph::num_passes
int num_passes
Definition: graph.h:120
AV_PIX_FMT_FLAG_ALPHA
#define AV_PIX_FMT_FLAG_ALPHA
The pixel format has an alpha channel.
Definition: pixdesc.h:147
ctx
static AVFormatContext * ctx
Definition: movenc.c:49
ff_sws_lut3d_update
void ff_sws_lut3d_update(SwsLut3D *lut3d, const SwsColor *new_src)
Update the tone mapping state.
Definition: lut3d.c:239
field
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this field
Definition: writing_filters.txt:78
AVPixFmtDescriptor::log2_chroma_w
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:80
SwsPass::run
SwsPassFunc run
Filter main execution function.
Definition: graph.h:77
free_buffer
static void free_buffer(AVRefStructOpaque opaque, void *obj)
Definition: graph.c:101
av_mallocz
#define av_mallocz(s)
Definition: tableprint_vlc.h:31
tmp
static uint8_t tmp[40]
Definition: aes_ctr.c:52
AV_PIX_FMT_RGBA
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:100
SwsGraph::field
int field
Definition: graph.h:132
ff_sws_lut3d_free
void ff_sws_lut3d_free(SwsLut3D **plut3d)
Definition: lut3d.c:42
NULL
#define NULL
Definition: coverity.c:32
sizes
static const int sizes[][2]
Definition: img2dec.c:61
run
uint8_t run
Definition: svq3.c:207
run_legacy_swscale
static void run_legacy_swscale(const SwsFrame *out, const SwsFrame *in, int y, int h, const SwsPass *pass)
Definition: graph.c:305
SwsContext::gamma_flag
int gamma_flag
Use gamma correct scaling.
Definition: swscale.h:229
av_image_fill_plane_sizes
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
ff_infer_colors
bool ff_infer_colors(SwsColor *src, SwsColor *dst)
Definition: format.c:526
ff_sws_lut3d_generate
int ff_sws_lut3d_generate(SwsLut3D *lut3d, enum AVPixelFormat fmt_in, enum AVPixelFormat fmt_out, const SwsColorMap *map)
Recalculate the (static) 3DLUT state with new settings.
Definition: lut3d.c:211
SwsContext::src_range
int src_range
Source is full range.
Definition: swscale.h:241
av_cpu_max_align
size_t av_cpu_max_align(void)
Get the maximum data alignment that may be required by FFmpeg.
Definition: cpu.c:287
SwsPass::graph
const SwsGraph * graph
Definition: graph.h:70
SwsGraph::exec
struct SwsGraph::@542 exec
Temporary execution state inside ff_sws_graph_run(); used to pass data to worker threads.
AVCOL_RANGE_UNSPECIFIED
@ AVCOL_RANGE_UNSPECIFIED
Definition: pixfmt.h:743
SwsContext::dst_h_chr_pos
int dst_h_chr_pos
Destination horizontal chroma position.
Definition: swscale.h:246
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
SwsPassBuffer::avframe
AVFrame * avframe
Definition: graph.h:61
error.h
av_opt_copy
int av_opt_copy(void *dst, const void *src)
Copy options from src object into dest object.
Definition: opt.c:2155
ff_sws_graph_free
void ff_sws_graph_free(SwsGraph **pgraph)
Uninitialize any state associate with this filter graph and free it.
Definition: graph.c:803
SwsPass::height
int height
Definition: graph.h:79
f
f
Definition: af_crystalizer.c:122
lut3d.h
free_lut3d
static void free_lut3d(void *priv)
Definition: graph.c:633
height
#define height
Definition: dsp.h:89
frame_alloc_planes
static int frame_alloc_planes(AVFrame *dst)
Definition: graph.c:42
sws_alloc_context
SwsContext * sws_alloc_context(void)
Allocate an empty SwsContext and set its fields to default values.
Definition: utils.c:1033
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:87
usePal
static av_always_inline int usePal(enum AVPixelFormat pix_fmt)
Definition: swscale_internal.h:933
cpu.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h264.c:63
ff_sws_lut3d_apply
void ff_sws_lut3d_apply(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.
Definition: lut3d.c:250
AV_PIX_FMT_RGB48
#define AV_PIX_FMT_RGB48
Definition: pixfmt.h:525
run_legacy_unscaled
static void run_legacy_unscaled(const SwsFrame *out, const SwsFrame *in, int y, int h, const SwsPass *pass)
Definition: graph.c:293
SwsContext::alpha_blend
SwsAlphaBlend alpha_blend
Alpha blending mode.
Definition: swscale.h:224
SwsPassBuffer::height
int height
Definition: graph.h:60
SwsOpList::src
SwsFormat src
Definition: ops.h:229
SwsContext::src_h
int src_h
Width and height of the source frame.
Definition: swscale.h:237
AVCHROMA_LOC_UNSPECIFIED
@ AVCHROMA_LOC_UNSPECIFIED
Definition: pixfmt.h:797
SwsFormat
Definition: format.h:77
AVFrame::format
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:514
sws_getColorspaceDetails
int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation)
Definition: utils.c:1008
align
static const uint8_t *BS_FUNC() align(BSCTX *bc)
Skip bits to a byte boundary.
Definition: bitstream_template.h:419
SwsFormat::loc
enum AVChromaLocation loc
Definition: format.h:84
SwsColor
Definition: format.h:60
SwsPass::output
SwsPassBuffer * output
Filter output buffer.
Definition: graph.h:92
av_buffer_alloc
AVBufferRef * av_buffer_alloc(size_t size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:77
SwsContext::dst_format
int dst_format
Destination pixel format.
Definition: swscale.h:240
SWS_OP_FLAG_OPTIMIZE
@ SWS_OP_FLAG_OPTIMIZE
Definition: ops.h:308
run_lut3d
static void run_lut3d(const SwsFrame *out, const SwsFrame *in, int y, int h, const SwsPass *pass)
Definition: graph.c:647
input
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some input
Definition: filter_design.txt:172
slicethread.h
SwsGraph::input
const SwsFrame * input
Definition: graph.h:140
AVChromaLocation
AVChromaLocation
Location of chroma samples.
Definition: pixfmt.h:796
av_refstruct_unref
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:120
setup_lut3d
static void setup_lut3d(const SwsFrame *out, const SwsFrame *in, const SwsPass *pass)
Definition: graph.c:639
free_legacy_swscale
static void free_legacy_swscale(void *priv)
Definition: graph.c:254
SwsLut3D
Definition: lut3d.h:50
SwsGraph::dst
SwsFormat dst
Definition: graph.h:131
ff_fmt_vshift
static av_always_inline av_const int ff_fmt_vshift(enum AVPixelFormat fmt, int plane)
Definition: graph.h:32
SwsFormat::format
enum AVPixelFormat format
Definition: format.h:80
SwsPass::slice_h
int slice_h
Definition: graph.h:80
SwsGraph::num_threads
int num_threads
Definition: graph.h:112
opts_equal
static int opts_equal(const SwsContext *c1, const SwsContext *c2)
Definition: graph.c:820
SwsFormat::desc
const AVPixFmtDescriptor * desc
Definition: format.h:85
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:58
swscale_internal.h
graph.h
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
ff_sws_encode_colors
int ff_sws_encode_colors(SwsContext *ctx, SwsPixelType type, SwsOpList *ops, const SwsFormat *src, const SwsFormat *dst, bool *incomplete)
SwsContext::dst_h
int dst_h
Width and height of the destination frame.
Definition: swscale.h:238
AVCOL_SPC_UNSPECIFIED
@ AVCOL_SPC_UNSPECIFIED
Definition: pixfmt.h:703
ff_sws_decode_pixfmt
int ff_sws_decode_pixfmt(SwsOpList *ops, enum AVPixelFormat fmt)
Append a set of operations for decoding/encoding raw pixels.
AV_FRAME_FLAG_INTERLACED
#define AV_FRAME_FLAG_INTERLACED
A flag to mark frames whose content is interlaced.
Definition: frame.h:650
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
get_chroma_pos
static void get_chroma_pos(SwsGraph *graph, int *h_chr_pos, int *v_chr_pos, const SwsFormat *fmt)
Definition: graph.c:317
ff_sws_graph_add_pass
int ff_sws_graph_add_pass(SwsGraph *graph, enum AVPixelFormat fmt, int width, int height, SwsPass *input, 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:115
SWS_DITHER_ED
@ SWS_DITHER_ED
Definition: swscale.h:83
FIELD_BOTTOM
@ FIELD_BOTTOM
Definition: format.h:57
SwsInternal
Definition: swscale_internal.h:334
ret
ret
Definition: filter_design.txt:187
SwsPassFunc
void(* SwsPassFunc)(const SwsFrame *out, const SwsFrame *in, int y, int h, const SwsPass *pass)
Output h lines of filtered data.
Definition: graph.h:45
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:265
SwsOpList::dst
SwsFormat dst
Definition: ops.h:229
ff_fmt_equal
static int ff_fmt_equal(const SwsFormat *fmt1, const SwsFormat *fmt2)
Definition: format.h:130
SwsPassSetup
void(* SwsPassSetup)(const SwsFrame *out, const SwsFrame *in, const SwsPass *pass)
Function to run from the main thread before processing any lines.
Definition: graph.h:51
SwsGraph::noop
bool noop
Definition: graph.h:114
av_dynarray_add_nofree
int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem)
Add an element to a dynamic array.
Definition: mem.c:315
AVFrame::height
int height
Definition: frame.h:499
c2
static const uint64_t c2
Definition: murmur3.c:53
SwsPassBuffer::width
int width
Definition: graph.h:60
SwsContext::scaler_params
double scaler_params[2]
Extra parameters for fine-tuning certain scalers.
Definition: swscale.h:209
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
AVCHROMA_LOC_CENTER
@ AVCHROMA_LOC_CENTER
MPEG-1 4:2:0, JPEG 4:2:0, H.263 4:2:0.
Definition: pixfmt.h:799
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
SwsFormat::hw_format
enum AVPixelFormat hw_format
Definition: format.h:81
SwsFormat::color
SwsColor color
Definition: format.h:86
get_field
static void get_field(SwsGraph *graph, const AVFrame *avframe, SwsFrame *frame)
Definition: graph.c:860
ff_sws_color_map_noop
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
ff_swscale
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:263
cms.h
add_convert_pass
#define add_convert_pass
Definition: graph.c:625
desc
const char * desc
Definition: libsvtav1.c:82
SwsInternal::pal_yuv
uint32_t pal_yuv[256]
Definition: swscale_internal.h:399
SwsGraph::incomplete
bool incomplete
Definition: graph.h:113
mem.h
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
sws_getCoefficients
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
SwsContext::dst_w
int dst_w
Definition: swscale.h:238
SwsGraph
Filter graph, which represents a 'baked' pixel format conversion.
Definition: graph.h:109
SwsContext::src_format
int src_format
Source pixel format.
Definition: swscale.h:239
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
map
const VDPAUPixFmtMap * map
Definition: hwcontext_vdpau.c:71
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
run_rgb2xyz
static void run_rgb2xyz(const SwsFrame *out, const SwsFrame *in, int y, int h, const SwsPass *pass)
Definition: graph.c:239
SwsContext::dst_range
int dst_range
Destination is full range.
Definition: swscale.h:242
pass_alloc_output
static int pass_alloc_output(SwsPass *pass)
Definition: graph.c:77
sws_free_context
void sws_free_context(SwsContext **ctx)
Free the context and everything associated with it, and write NULL to the provided pointer.
Definition: utils.c:2346
imgutils.h
avpriv_slicethread_free
void avpriv_slicethread_free(AVSliceThread **pctx)
Destroy slice threading context.
Definition: slicethread.c:275
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
SwsContext::src_h_chr_pos
int src_h_chr_pos
Source horizontal chroma position.
Definition: swscale.h:244
sws_internal
static SwsInternal * sws_internal(const SwsContext *sws)
Definition: swscale_internal.h:78
SwsPass::input
const SwsPass * input
Filter input.
Definition: graph.h:87
SwsPassBuffer
Represents an allocated output buffer for a filter pass.
Definition: graph.h:57
h
h
Definition: vp9dsp_template.c:2070
SwsPass::num_slices
int num_slices
Definition: graph.h:81
width
#define width
Definition: dsp.h:89
SwsOpList
Helper struct for representing a list of operations.
Definition: ops.h:224
SwsContext::dst_v_chr_pos
int dst_v_chr_pos
Destination vertical chroma position.
Definition: swscale.h:245
SwsContext
Main external API structure.
Definition: swscale.h:191
AV_PIX_FMT_FLAG_PAL
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition: pixdesc.h:120
SwsFrame::linesize
int linesize[4]
Definition: format.h:191
sws_graph_worker
static void sws_graph_worker(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads)
Definition: graph.c:751
ff_sws_graph_update_metadata
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:852
SWS_UNSTABLE
@ SWS_UNSTABLE
Allow using experimental new code paths.
Definition: swscale.h:165
AVPixFmtDescriptor::log2_chroma_h
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:89
src
#define src
Definition: vp8dsp.c:248
swscale.h
ff_sws_encode_pixfmt
int ff_sws_encode_pixfmt(SwsOpList *ops, enum AVPixelFormat fmt)
av_get_pix_fmt_name
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:3376
isALPHA
static av_always_inline int isALPHA(enum AVPixelFormat pix_fmt)
Definition: swscale_internal.h:893
ff_sws_frame_from_avframe
void ff_sws_frame_from_avframe(SwsFrame *dst, const AVFrame *src)
Initialize a SwsFrame from an AVFrame.
Definition: format.c:639