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