Line | Branch | Exec | 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/error.h" | ||
23 | #include "libavutil/imgutils.h" | ||
24 | #include "libavutil/macros.h" | ||
25 | #include "libavutil/mem.h" | ||
26 | #include "libavutil/opt.h" | ||
27 | #include "libavutil/pixdesc.h" | ||
28 | #include "libavutil/slicethread.h" | ||
29 | |||
30 | #include "libswscale/swscale.h" | ||
31 | #include "libswscale/utils.h" | ||
32 | |||
33 | #include "cms.h" | ||
34 | #include "lut3d.h" | ||
35 | #include "swscale_internal.h" | ||
36 | #include "graph.h" | ||
37 | |||
38 | 5605 | static int pass_alloc_output(SwsPass *pass) | |
39 | { | ||
40 |
3/4✓ Branch 0 taken 53 times.
✓ Branch 1 taken 5552 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 53 times.
|
5605 | if (!pass || pass->output.fmt != AV_PIX_FMT_NONE) |
41 | 5552 | return 0; | |
42 | 53 | pass->output.fmt = pass->format; | |
43 | 53 | return av_image_alloc(pass->output.data, pass->output.linesize, pass->width, | |
44 | 53 | pass->num_slices * pass->slice_h, pass->format, 64); | |
45 | } | ||
46 | |||
47 | /* slice_align should be a power of two, or 0 to disable slice threading */ | ||
48 | 5605 | static SwsPass *pass_add(SwsGraph *graph, void *priv, enum AVPixelFormat fmt, | |
49 | int w, int h, SwsPass *input, int slice_align, | ||
50 | sws_filter_run_t run) | ||
51 | { | ||
52 | int ret; | ||
53 | 5605 | SwsPass *pass = av_mallocz(sizeof(*pass)); | |
54 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5605 times.
|
5605 | if (!pass) |
55 | ✗ | return NULL; | |
56 | |||
57 | 5605 | pass->graph = graph; | |
58 | 5605 | pass->run = run; | |
59 | 5605 | pass->priv = priv; | |
60 | 5605 | pass->format = fmt; | |
61 | 5605 | pass->width = w; | |
62 | 5605 | pass->height = h; | |
63 | 5605 | pass->input = input; | |
64 | 5605 | pass->output.fmt = AV_PIX_FMT_NONE; | |
65 | |||
66 | 5605 | ret = pass_alloc_output(input); | |
67 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5605 times.
|
5605 | if (ret < 0) { |
68 | ✗ | av_free(pass); | |
69 | ✗ | return NULL; | |
70 | } | ||
71 | |||
72 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 5594 times.
|
5605 | if (!slice_align) { |
73 | 11 | pass->slice_h = pass->height; | |
74 | 11 | pass->num_slices = 1; | |
75 | } else { | ||
76 | 5594 | pass->slice_h = (pass->height + graph->num_threads - 1) / graph->num_threads; | |
77 | 5594 | pass->slice_h = FFALIGN(pass->slice_h, slice_align); | |
78 | 5594 | pass->num_slices = (pass->height + pass->slice_h - 1) / pass->slice_h; | |
79 | } | ||
80 | |||
81 | 5605 | ret = av_dynarray_add_nofree(&graph->passes, &graph->num_passes, pass); | |
82 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5605 times.
|
5605 | if (ret < 0) |
83 | ✗ | av_freep(&pass); | |
84 | 5605 | return pass; | |
85 | } | ||
86 | |||
87 | /* Wrapper around pass_add that chains a pass "in-place" */ | ||
88 | 52 | static int pass_append(SwsGraph *graph, void *priv, enum AVPixelFormat fmt, | |
89 | int w, int h, SwsPass **pass, int slice_align, | ||
90 | sws_filter_run_t run) | ||
91 | { | ||
92 | 52 | SwsPass *new = pass_add(graph, priv, fmt, w, h, *pass, slice_align, run); | |
93 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
|
52 | if (!new) |
94 | ✗ | return AVERROR(ENOMEM); | |
95 | 52 | *pass = new; | |
96 | 52 | return 0; | |
97 | } | ||
98 | |||
99 | 1102445 | static int vshift(enum AVPixelFormat fmt, int plane) | |
100 | { | ||
101 | 1102445 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt); | |
102 |
4/4✓ Branch 0 taken 759301 times.
✓ Branch 1 taken 343144 times.
✓ Branch 2 taken 266791 times.
✓ Branch 3 taken 492510 times.
|
1102445 | return (plane == 1 || plane == 2) ? desc->log2_chroma_h : 0; |
103 | } | ||
104 | |||
105 | /* Shift an image vertically by y lines */ | ||
106 | 483916 | static SwsImg shift_img(const SwsImg *img_base, int y) | |
107 | { | ||
108 | 483916 | SwsImg img = *img_base; | |
109 |
4/4✓ Branch 0 taken 1577767 times.
✓ Branch 1 taken 8594 times.
✓ Branch 2 taken 1102445 times.
✓ Branch 3 taken 475322 times.
|
1586361 | for (int i = 0; i < 4 && img.data[i]; i++) |
110 | 1102445 | img.data[i] += (y >> vshift(img.fmt, i)) * img.linesize[i]; | |
111 | 483916 | return img; | |
112 | } | ||
113 | |||
114 | ✗ | static void run_copy(const SwsImg *out_base, const SwsImg *in_base, | |
115 | int y, int h, const SwsPass *pass) | ||
116 | { | ||
117 | ✗ | SwsImg in = shift_img(in_base, y); | |
118 | ✗ | SwsImg out = shift_img(out_base, y); | |
119 | |||
120 | ✗ | for (int i = 0; i < FF_ARRAY_ELEMS(in.data) && in.data[i]; i++) { | |
121 | ✗ | const int lines = h >> vshift(in.fmt, i); | |
122 | ✗ | if (in.linesize[i] == out.linesize[i]) { | |
123 | ✗ | memcpy(out.data[i], in.data[i], lines * out.linesize[i]); | |
124 | } else { | ||
125 | ✗ | const int linesize = FFMIN(out.linesize[i], in.linesize[i]); | |
126 | ✗ | for (int j = 0; j < lines; j++) { | |
127 | ✗ | memcpy(out.data[i], in.data[i], linesize); | |
128 | ✗ | in.data[i] += in.linesize[i]; | |
129 | ✗ | out.data[i] += out.linesize[i]; | |
130 | } | ||
131 | } | ||
132 | } | ||
133 | ✗ | } | |
134 | |||
135 | 54 | static void run_rgb0(const SwsImg *out, const SwsImg *in, int y, int h, | |
136 | const SwsPass *pass) | ||
137 | { | ||
138 | 54 | SwsInternal *c = pass->priv; | |
139 | 54 | const int x0 = c->src0Alpha - 1; | |
140 | 54 | const int w4 = 4 * pass->width; | |
141 | 54 | const int src_stride = in->linesize[0]; | |
142 | 54 | const int dst_stride = out->linesize[0]; | |
143 | 54 | const uint8_t *src = in->data[0] + y * src_stride; | |
144 | 54 | uint8_t *dst = out->data[0] + y * dst_stride; | |
145 | |||
146 |
2/2✓ Branch 0 taken 4662 times.
✓ Branch 1 taken 54 times.
|
4716 | for (int y = 0; y < h; y++) { |
147 | 4662 | memcpy(dst, src, w4 * sizeof(*dst)); | |
148 |
2/2✓ Branch 0 taken 2869202 times.
✓ Branch 1 taken 4662 times.
|
2873864 | for (int x = x0; x < w4; x += 4) |
149 | 2869202 | dst[x] = 0xFF; | |
150 | |||
151 | 4662 | src += src_stride; | |
152 | 4662 | dst += dst_stride; | |
153 | } | ||
154 | 54 | } | |
155 | |||
156 | 1875 | static void run_xyz2rgb(const SwsImg *out, const SwsImg *in, int y, int h, | |
157 | const SwsPass *pass) | ||
158 | { | ||
159 | 1875 | ff_xyz12Torgb48(pass->priv, out->data[0] + y * out->linesize[0], out->linesize[0], | |
160 | 1875 | in->data[0] + y * in->linesize[0], in->linesize[0], | |
161 | 1875 | pass->width, h); | |
162 | 1875 | } | |
163 | |||
164 | 1916 | static void run_rgb2xyz(const SwsImg *out, const SwsImg *in, int y, int h, | |
165 | const SwsPass *pass) | ||
166 | { | ||
167 | 1916 | ff_rgb48Toxyz12(pass->priv, out->data[0] + y * out->linesize[0], out->linesize[0], | |
168 | 1916 | in->data[0] + y * in->linesize[0], in->linesize[0], | |
169 | 1916 | pass->width, h); | |
170 | 1916 | } | |
171 | |||
172 | /*********************************************************************** | ||
173 | * Internal ff_swscale() wrapper. This re-uses the legacy scaling API. * | ||
174 | * This is considered fully deprecated, and will be replaced by a full * | ||
175 | * reimplementation ASAP. * | ||
176 | ***********************************************************************/ | ||
177 | |||
178 | 5553 | static void free_legacy_swscale(void *priv) | |
179 | { | ||
180 | 5553 | SwsContext *sws = priv; | |
181 | 5553 | sws_free_context(&sws); | |
182 | 5553 | } | |
183 | |||
184 | 84326 | static void setup_legacy_swscale(const SwsImg *out, const SwsImg *in, | |
185 | const SwsPass *pass) | ||
186 | { | ||
187 | 84326 | SwsContext *sws = pass->priv; | |
188 | 84326 | SwsInternal *c = sws_internal(sws); | |
189 |
5/6✓ Branch 0 taken 77086 times.
✓ Branch 1 taken 7240 times.
✓ Branch 2 taken 870 times.
✓ Branch 3 taken 76216 times.
✓ Branch 4 taken 870 times.
✗ Branch 5 not taken.
|
84326 | if (sws->flags & SWS_BITEXACT && sws->dither == SWS_DITHER_ED && c->dither_error[0]) { |
190 |
2/2✓ Branch 0 taken 3480 times.
✓ Branch 1 taken 870 times.
|
4350 | for (int i = 0; i < 4; i++) |
191 | 3480 | memset(c->dither_error[i], 0, sizeof(c->dither_error[0][0]) * (sws->dst_w + 2)); | |
192 | } | ||
193 | |||
194 |
2/2✓ Branch 1 taken 4585 times.
✓ Branch 2 taken 79741 times.
|
84326 | if (usePal(sws->src_format)) |
195 | 4585 | ff_update_palette(c, (const uint32_t *) in->data[1]); | |
196 | 84326 | } | |
197 | |||
198 | 483916 | static inline SwsContext *slice_ctx(const SwsPass *pass, int y) | |
199 | { | ||
200 | 483916 | SwsContext *sws = pass->priv; | |
201 | 483916 | SwsInternal *parent = sws_internal(sws); | |
202 |
2/2✓ Branch 0 taken 34350 times.
✓ Branch 1 taken 449566 times.
|
483916 | if (pass->num_slices == 1) |
203 | 34350 | return sws; | |
204 | |||
205 | av_assert1(parent->nb_slice_ctx == pass->num_slices); | ||
206 | 449566 | sws = parent->slice_ctx[y / pass->slice_h]; | |
207 | |||
208 |
2/2✓ Branch 1 taken 34415 times.
✓ Branch 2 taken 415151 times.
|
449566 | if (usePal(sws->src_format)) { |
209 | 34415 | SwsInternal *sub = sws_internal(sws); | |
210 | 34415 | memcpy(sub->pal_yuv, parent->pal_yuv, sizeof(sub->pal_yuv)); | |
211 | 34415 | memcpy(sub->pal_rgb, parent->pal_rgb, sizeof(sub->pal_rgb)); | |
212 | } | ||
213 | |||
214 | 449566 | return sws; | |
215 | } | ||
216 | |||
217 | 77247 | static void run_legacy_unscaled(const SwsImg *out, const SwsImg *in_base, | |
218 | int y, int h, const SwsPass *pass) | ||
219 | { | ||
220 | 77247 | SwsContext *sws = slice_ctx(pass, y); | |
221 | 77247 | SwsInternal *c = sws_internal(sws); | |
222 | 77247 | const SwsImg in = shift_img(in_base, y); | |
223 | |||
224 | 77247 | c->convert_unscaled(c, (const uint8_t *const *) in.data, in.linesize, y, h, | |
225 | 77247 | out->data, out->linesize); | |
226 | 77247 | } | |
227 | |||
228 | 406669 | static void run_legacy_swscale(const SwsImg *out_base, const SwsImg *in, | |
229 | int y, int h, const SwsPass *pass) | ||
230 | { | ||
231 | 406669 | SwsContext *sws = slice_ctx(pass, y); | |
232 | 406669 | SwsInternal *c = sws_internal(sws); | |
233 | 406669 | const SwsImg out = shift_img(out_base, y); | |
234 | |||
235 | 406669 | ff_swscale(c, (const uint8_t *const *) in->data, in->linesize, 0, | |
236 | sws->src_h, out.data, out.linesize, y, h); | ||
237 | 406669 | } | |
238 | |||
239 | 11104 | static void get_chroma_pos(SwsGraph *graph, int *h_chr_pos, int *v_chr_pos, | |
240 | const SwsFormat *fmt) | ||
241 | { | ||
242 | 11104 | enum AVChromaLocation chroma_loc = fmt->loc; | |
243 | 11104 | const int sub_x = fmt->desc->log2_chroma_w; | |
244 | 11104 | const int sub_y = fmt->desc->log2_chroma_h; | |
245 | int x_pos, y_pos; | ||
246 | |||
247 | /* Explicitly default to center siting for compatibility with swscale */ | ||
248 |
2/2✓ Branch 0 taken 11103 times.
✓ Branch 1 taken 1 times.
|
11104 | if (chroma_loc == AVCHROMA_LOC_UNSPECIFIED) { |
249 | 11103 | chroma_loc = AVCHROMA_LOC_CENTER; | |
250 |
4/4✓ Branch 0 taken 5448 times.
✓ Branch 1 taken 5655 times.
✓ Branch 2 taken 204 times.
✓ Branch 3 taken 5244 times.
|
11103 | graph->incomplete |= sub_x || sub_y; |
251 | } | ||
252 | |||
253 | /* av_chroma_location_enum_to_pos() always gives us values in the range from | ||
254 | * 0 to 256, but we need to adjust this to the true value range of the | ||
255 | * subsampling grid, which may be larger for h/v_sub > 1 */ | ||
256 | 11104 | av_chroma_location_enum_to_pos(&x_pos, &y_pos, chroma_loc); | |
257 | 11104 | x_pos *= (1 << sub_x) - 1; | |
258 | 11104 | y_pos *= (1 << sub_y) - 1; | |
259 | |||
260 | /* Fix vertical chroma position for interlaced frames */ | ||
261 |
3/4✓ Branch 0 taken 4610 times.
✓ Branch 1 taken 6494 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4610 times.
|
11104 | if (sub_y && fmt->interlaced) { |
262 | /* When vertically subsampling, chroma samples are effectively only | ||
263 | * placed next to even rows. To access them from the odd field, we need | ||
264 | * to account for this shift by offsetting the distance of one luma row. | ||
265 | * | ||
266 | * For 4x vertical subsampling (v_sub == 2), they are only placed | ||
267 | * next to every *other* even row, so we need to shift by three luma | ||
268 | * rows to get to the chroma sample. */ | ||
269 | ✗ | if (graph->field == FIELD_BOTTOM) | |
270 | ✗ | y_pos += (256 << sub_y) - 256; | |
271 | |||
272 | /* Luma row distance is doubled for fields, so halve offsets */ | ||
273 | ✗ | y_pos >>= 1; | |
274 | } | ||
275 | |||
276 | /* Explicitly strip chroma offsets when not subsampling, because it | ||
277 | * interferes with the operation of flags like SWS_FULL_CHR_H_INP */ | ||
278 |
2/2✓ Branch 0 taken 5656 times.
✓ Branch 1 taken 5448 times.
|
11104 | *h_chr_pos = sub_x ? x_pos : -513; |
279 |
2/2✓ Branch 0 taken 4610 times.
✓ Branch 1 taken 6494 times.
|
11104 | *v_chr_pos = sub_y ? y_pos : -513; |
280 | 11104 | } | |
281 | |||
282 | 22208 | static void legacy_chr_pos(SwsGraph *graph, int *chr_pos, int override, int *warned) | |
283 | { | ||
284 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 22208 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
22208 | if (override == -513 || override == *chr_pos) |
285 | 22208 | return; | |
286 | |||
287 | ✗ | if (!*warned) { | |
288 | ✗ | av_log(NULL, AV_LOG_WARNING, | |
289 | "Setting chroma position directly is deprecated, make sure " | ||
290 | "the frame is tagged with the correct chroma location.\n"); | ||
291 | ✗ | *warned = 1; | |
292 | } | ||
293 | |||
294 | ✗ | *chr_pos = override; | |
295 | } | ||
296 | |||
297 | 5554 | static int init_legacy_subpass(SwsGraph *graph, SwsContext *sws, | |
298 | SwsPass *input, SwsPass **output) | ||
299 | { | ||
300 | 5554 | SwsInternal *c = sws_internal(sws); | |
301 | 5554 | const int src_w = sws->src_w, src_h = sws->src_h; | |
302 | 5554 | const int dst_w = sws->dst_w, dst_h = sws->dst_h; | |
303 |
4/4✓ Branch 0 taken 5110 times.
✓ Branch 1 taken 444 times.
✓ Branch 2 taken 5100 times.
✓ Branch 3 taken 10 times.
|
5554 | const int unscaled = src_w == dst_w && src_h == dst_h; |
304 | 5554 | int align = c->dst_slice_align; | |
305 | 5554 | SwsPass *pass = NULL; | |
306 | int ret; | ||
307 | |||
308 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5553 times.
|
5554 | if (c->cascaded_context[0]) { |
309 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | const int num_cascaded = c->cascaded_context[2] ? 3 : 2; |
310 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | for (int i = 0; i < num_cascaded; i++) { |
311 | 2 | SwsContext *sub = c->cascaded_context[i]; | |
312 | 2 | const int is_last = i + 1 == num_cascaded; | |
313 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | ret = init_legacy_subpass(graph, sub, input, is_last ? output : &input); |
314 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (ret < 0) |
315 | ✗ | return ret; | |
316 | /* Steal cascaded context, so we can free the parent */ | ||
317 | 2 | c->cascaded_context[i] = NULL; | |
318 | } | ||
319 | |||
320 | 1 | sws_free_context(&sws); | |
321 | 1 | return 0; | |
322 | } | ||
323 | |||
324 |
3/4✓ Branch 0 taken 11 times.
✓ Branch 1 taken 5542 times.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
|
5553 | if (sws->dither == SWS_DITHER_ED && !c->convert_unscaled) |
325 | 11 | align = 0; /* disable slice threading */ | |
326 | |||
327 |
6/6✓ Branch 0 taken 25 times.
✓ Branch 1 taken 5528 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 4 times.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 15 times.
|
5553 | if (c->src0Alpha && !c->dst0Alpha && isALPHA(sws->dst_format)) { |
328 | 6 | ret = pass_append(graph, c, AV_PIX_FMT_RGBA, src_w, src_h, &input, 1, run_rgb0); | |
329 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (ret < 0) |
330 | ✗ | return ret; | |
331 | } | ||
332 | |||
333 |
5/6✓ Branch 0 taken 11 times.
✓ Branch 1 taken 5542 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
5553 | if (c->srcXYZ && !(c->dstXYZ && unscaled)) { |
334 | 11 | ret = pass_append(graph, c, AV_PIX_FMT_RGB48, src_w, src_h, &input, 1, run_xyz2rgb); | |
335 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
|
11 | if (ret < 0) |
336 | ✗ | return ret; | |
337 | } | ||
338 | |||
339 | 5553 | pass = pass_add(graph, sws, sws->dst_format, dst_w, dst_h, input, align, | |
340 |
2/2✓ Branch 0 taken 601 times.
✓ Branch 1 taken 4952 times.
|
5553 | c->convert_unscaled ? run_legacy_unscaled : run_legacy_swscale); |
341 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5553 times.
|
5553 | if (!pass) |
342 | ✗ | return AVERROR(ENOMEM); | |
343 | 5553 | pass->setup = setup_legacy_swscale; | |
344 | 5553 | pass->free = free_legacy_swscale; | |
345 | |||
346 | /** | ||
347 | * For slice threading, we need to create sub contexts, similar to how | ||
348 | * swscale normally handles it internally. The most important difference | ||
349 | * is that we handle cascaded contexts before threaded contexts; whereas | ||
350 | * context_init_threaded() does it the other way around. | ||
351 | */ | ||
352 | |||
353 |
2/2✓ Branch 0 taken 1899 times.
✓ Branch 1 taken 3654 times.
|
5553 | if (pass->num_slices > 1) { |
354 | 1899 | c->slice_ctx = av_calloc(pass->num_slices, sizeof(*c->slice_ctx)); | |
355 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1899 times.
|
1899 | if (!c->slice_ctx) |
356 | ✗ | return AVERROR(ENOMEM); | |
357 | |||
358 |
2/2✓ Branch 0 taken 17023 times.
✓ Branch 1 taken 1899 times.
|
18922 | for (int i = 0; i < pass->num_slices; i++) { |
359 | SwsContext *slice; | ||
360 | SwsInternal *c2; | ||
361 | 17023 | slice = c->slice_ctx[i] = sws_alloc_context(); | |
362 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17023 times.
|
17023 | if (!slice) |
363 | ✗ | return AVERROR(ENOMEM); | |
364 | 17023 | c->nb_slice_ctx++; | |
365 | |||
366 | 17023 | c2 = sws_internal(slice); | |
367 | 17023 | c2->parent = sws; | |
368 | |||
369 | 17023 | ret = av_opt_copy(slice, sws); | |
370 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17023 times.
|
17023 | if (ret < 0) |
371 | ✗ | return ret; | |
372 | |||
373 | 17023 | ret = ff_sws_init_single_context(slice, NULL, NULL); | |
374 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17023 times.
|
17023 | if (ret < 0) |
375 | ✗ | return ret; | |
376 | |||
377 | 17023 | sws_setColorspaceDetails(slice, c->srcColorspaceTable, | |
378 | 17023 | slice->src_range, c->dstColorspaceTable, | |
379 | slice->dst_range, c->brightness, c->contrast, | ||
380 | c->saturation); | ||
381 | |||
382 |
2/2✓ Branch 0 taken 68092 times.
✓ Branch 1 taken 17023 times.
|
85115 | for (int i = 0; i < FF_ARRAY_ELEMS(c->srcColorspaceTable); i++) { |
383 | 68092 | c2->srcColorspaceTable[i] = c->srcColorspaceTable[i]; | |
384 | 68092 | c2->dstColorspaceTable[i] = c->dstColorspaceTable[i]; | |
385 | } | ||
386 | } | ||
387 | } | ||
388 | |||
389 |
5/6✓ Branch 0 taken 35 times.
✓ Branch 1 taken 5518 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 33 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
5553 | if (c->dstXYZ && !(c->srcXYZ && unscaled)) { |
390 | 35 | ret = pass_append(graph, c, AV_PIX_FMT_RGB48, dst_w, dst_h, &pass, 1, run_rgb2xyz); | |
391 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
|
35 | if (ret < 0) |
392 | ✗ | return ret; | |
393 | } | ||
394 | |||
395 | 5553 | *output = pass; | |
396 | 5553 | return 0; | |
397 | } | ||
398 | |||
399 | 5552 | static int add_legacy_sws_pass(SwsGraph *graph, SwsFormat src, SwsFormat dst, | |
400 | SwsPass *input, SwsPass **output) | ||
401 | { | ||
402 | 5552 | int ret, warned = 0; | |
403 | 5552 | SwsContext *const ctx = graph->ctx; | |
404 | 5552 | SwsContext *sws = sws_alloc_context(); | |
405 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5552 times.
|
5552 | if (!sws) |
406 | ✗ | return AVERROR(ENOMEM); | |
407 | |||
408 | 5552 | sws->flags = ctx->flags; | |
409 | 5552 | sws->dither = ctx->dither; | |
410 | 5552 | sws->alpha_blend = ctx->alpha_blend; | |
411 | 5552 | sws->gamma_flag = ctx->gamma_flag; | |
412 | |||
413 | 5552 | sws->src_w = src.width; | |
414 | 5552 | sws->src_h = src.height; | |
415 | 5552 | sws->src_format = src.format; | |
416 | 5552 | sws->src_range = src.range == AVCOL_RANGE_JPEG; | |
417 | |||
418 | 5552 | sws->dst_w = dst.width; | |
419 | 5552 | sws->dst_h = dst.height; | |
420 | 5552 | sws->dst_format = dst.format; | |
421 | 5552 | sws->dst_range = dst.range == AVCOL_RANGE_JPEG; | |
422 | 5552 | get_chroma_pos(graph, &sws->src_h_chr_pos, &sws->src_v_chr_pos, &src); | |
423 | 5552 | get_chroma_pos(graph, &sws->dst_h_chr_pos, &sws->dst_v_chr_pos, &dst); | |
424 | |||
425 | 5552 | graph->incomplete |= src.range == AVCOL_RANGE_UNSPECIFIED; | |
426 | 5552 | graph->incomplete |= dst.range == AVCOL_RANGE_UNSPECIFIED; | |
427 | |||
428 | /* Allow overriding chroma position with the legacy API */ | ||
429 | 5552 | legacy_chr_pos(graph, &sws->src_h_chr_pos, ctx->src_h_chr_pos, &warned); | |
430 | 5552 | legacy_chr_pos(graph, &sws->src_v_chr_pos, ctx->src_v_chr_pos, &warned); | |
431 | 5552 | legacy_chr_pos(graph, &sws->dst_h_chr_pos, ctx->dst_h_chr_pos, &warned); | |
432 | 5552 | legacy_chr_pos(graph, &sws->dst_v_chr_pos, ctx->dst_v_chr_pos, &warned); | |
433 | |||
434 | 5552 | sws->scaler_params[0] = ctx->scaler_params[0]; | |
435 | 5552 | sws->scaler_params[1] = ctx->scaler_params[1]; | |
436 | |||
437 | 5552 | ret = sws_init_context(sws, NULL, NULL); | |
438 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5552 times.
|
5552 | if (ret < 0) { |
439 | ✗ | sws_free_context(&sws); | |
440 | ✗ | return ret; | |
441 | } | ||
442 | |||
443 | /* Set correct color matrices */ | ||
444 | { | ||
445 | int in_full, out_full, brightness, contrast, saturation; | ||
446 | const int *inv_table, *table; | ||
447 | 5552 | sws_getColorspaceDetails(sws, (int **)&inv_table, &in_full, | |
448 | (int **)&table, &out_full, | ||
449 | &brightness, &contrast, &saturation); | ||
450 | |||
451 | 5552 | inv_table = sws_getCoefficients(src.csp); | |
452 | 5552 | table = sws_getCoefficients(dst.csp); | |
453 | |||
454 |
2/2✓ Branch 0 taken 2021 times.
✓ Branch 1 taken 3531 times.
|
7573 | graph->incomplete |= src.csp != dst.csp && |
455 |
2/2✓ Branch 0 taken 582 times.
✓ Branch 1 taken 1439 times.
|
2021 | (src.csp == AVCOL_SPC_UNSPECIFIED || |
456 |
2/2✓ Branch 0 taken 573 times.
✓ Branch 1 taken 9 times.
|
582 | dst.csp == AVCOL_SPC_UNSPECIFIED); |
457 | |||
458 | 5552 | sws_setColorspaceDetails(sws, inv_table, in_full, table, out_full, | |
459 | brightness, contrast, saturation); | ||
460 | } | ||
461 | |||
462 | 5552 | ret = init_legacy_subpass(graph, sws, input, output); | |
463 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5552 times.
|
5552 | if (ret < 0) { |
464 | ✗ | sws_free_context(&sws); | |
465 | ✗ | return ret; | |
466 | } | ||
467 | |||
468 | 5552 | return 0; | |
469 | } | ||
470 | |||
471 | /************************** | ||
472 | * Gamut and tone mapping * | ||
473 | **************************/ | ||
474 | |||
475 | ✗ | static void free_lut3d(void *priv) | |
476 | { | ||
477 | ✗ | SwsLut3D *lut = priv; | |
478 | ✗ | ff_sws_lut3d_free(&lut); | |
479 | ✗ | } | |
480 | |||
481 | ✗ | static void setup_lut3d(const SwsImg *out, const SwsImg *in, const SwsPass *pass) | |
482 | { | ||
483 | ✗ | SwsLut3D *lut = pass->priv; | |
484 | |||
485 | /* Update dynamic frame metadata from the original source frame */ | ||
486 | ✗ | ff_sws_lut3d_update(lut, &pass->graph->src.color); | |
487 | ✗ | } | |
488 | |||
489 | ✗ | static void run_lut3d(const SwsImg *out_base, const SwsImg *in_base, | |
490 | int y, int h, const SwsPass *pass) | ||
491 | { | ||
492 | ✗ | SwsLut3D *lut = pass->priv; | |
493 | ✗ | const SwsImg in = shift_img(in_base, y); | |
494 | ✗ | const SwsImg out = shift_img(out_base, y); | |
495 | |||
496 | ✗ | ff_sws_lut3d_apply(lut, in.data[0], in.linesize[0], out.data[0], | |
497 | ✗ | out.linesize[0], pass->width, h); | |
498 | ✗ | } | |
499 | |||
500 | 5552 | static int adapt_colors(SwsGraph *graph, SwsFormat src, SwsFormat dst, | |
501 | SwsPass *input, SwsPass **output) | ||
502 | { | ||
503 | enum AVPixelFormat fmt_in, fmt_out; | ||
504 | 5552 | SwsColorMap map = {0}; | |
505 | SwsLut3D *lut; | ||
506 | SwsPass *pass; | ||
507 | int ret; | ||
508 | |||
509 | /** | ||
510 | * Grayspace does not really have primaries, so just force the use of | ||
511 | * the equivalent other primary set to avoid a conversion. Technically, | ||
512 | * this does affect the weights used for the Grayscale conversion, but | ||
513 | * in practise, that should give the expected results more often than not. | ||
514 | */ | ||
515 |
2/2✓ Branch 1 taken 318 times.
✓ Branch 2 taken 5234 times.
|
5552 | if (isGray(dst.format)) { |
516 | 318 | dst.color = src.color; | |
517 |
2/2✓ Branch 1 taken 70 times.
✓ Branch 2 taken 5164 times.
|
5234 | } else if (isGray(src.format)) { |
518 | 70 | src.color = dst.color; | |
519 | } | ||
520 | |||
521 | /* Fully infer color spaces before color mapping logic */ | ||
522 | 5552 | graph->incomplete |= ff_infer_colors(&src.color, &dst.color); | |
523 | |||
524 | 5552 | map.intent = graph->ctx->intent; | |
525 | 5552 | map.src = src.color; | |
526 | 5552 | map.dst = dst.color; | |
527 | |||
528 |
1/2✓ Branch 1 taken 5552 times.
✗ Branch 2 not taken.
|
5552 | if (ff_sws_color_map_noop(&map)) |
529 | 5552 | return 0; | |
530 | |||
531 | ✗ | lut = ff_sws_lut3d_alloc(); | |
532 | ✗ | if (!lut) | |
533 | ✗ | return AVERROR(ENOMEM); | |
534 | |||
535 | ✗ | fmt_in = ff_sws_lut3d_pick_pixfmt(src, 0); | |
536 | ✗ | fmt_out = ff_sws_lut3d_pick_pixfmt(dst, 1); | |
537 | ✗ | if (fmt_in != src.format) { | |
538 | ✗ | SwsFormat tmp = src; | |
539 | ✗ | tmp.format = fmt_in; | |
540 | ✗ | ret = add_legacy_sws_pass(graph, src, tmp, input, &input); | |
541 | ✗ | if (ret < 0) | |
542 | ✗ | return ret; | |
543 | } | ||
544 | |||
545 | ✗ | ret = ff_sws_lut3d_generate(lut, fmt_in, fmt_out, &map); | |
546 | ✗ | if (ret < 0) { | |
547 | ✗ | ff_sws_lut3d_free(&lut); | |
548 | ✗ | return ret; | |
549 | } | ||
550 | |||
551 | ✗ | pass = pass_add(graph, lut, fmt_out, src.width, src.height, | |
552 | input, 1, run_lut3d); | ||
553 | ✗ | if (!pass) { | |
554 | ✗ | ff_sws_lut3d_free(&lut); | |
555 | ✗ | return AVERROR(ENOMEM); | |
556 | } | ||
557 | ✗ | pass->setup = setup_lut3d; | |
558 | ✗ | pass->free = free_lut3d; | |
559 | |||
560 | ✗ | *output = pass; | |
561 | ✗ | return 0; | |
562 | } | ||
563 | |||
564 | /*************************************** | ||
565 | * Main filter graph construction code * | ||
566 | ***************************************/ | ||
567 | |||
568 | 5552 | static int init_passes(SwsGraph *graph) | |
569 | { | ||
570 | 5552 | SwsFormat src = graph->src; | |
571 | 5552 | SwsFormat dst = graph->dst; | |
572 | 5552 | SwsPass *pass = NULL; /* read from main input image */ | |
573 | int ret; | ||
574 | |||
575 | 5552 | ret = adapt_colors(graph, src, dst, pass, &pass); | |
576 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5552 times.
|
5552 | if (ret < 0) |
577 | ✗ | return ret; | |
578 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5552 times.
|
5552 | src.format = pass ? pass->format : src.format; |
579 | 5552 | src.color = dst.color; | |
580 | |||
581 |
1/2✓ Branch 1 taken 5552 times.
✗ Branch 2 not taken.
|
5552 | if (!ff_fmt_equal(&src, &dst)) { |
582 | 5552 | ret = add_legacy_sws_pass(graph, src, dst, pass, &pass); | |
583 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5552 times.
|
5552 | if (ret < 0) |
584 | ✗ | return ret; | |
585 | } | ||
586 | |||
587 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5552 times.
|
5552 | if (!pass) { |
588 | /* No passes were added, so no operations were necessary */ | ||
589 | ✗ | graph->noop = 1; | |
590 | |||
591 | /* Add threaded memcpy pass */ | ||
592 | ✗ | pass = pass_add(graph, NULL, dst.format, dst.width, dst.height, pass, 1, run_copy); | |
593 | ✗ | if (!pass) | |
594 | ✗ | return AVERROR(ENOMEM); | |
595 | } | ||
596 | |||
597 | 5552 | return 0; | |
598 | } | ||
599 | |||
600 | 487761 | static void sws_graph_worker(void *priv, int jobnr, int threadnr, int nb_jobs, | |
601 | int nb_threads) | ||
602 | { | ||
603 | 487761 | SwsGraph *graph = priv; | |
604 | 487761 | const SwsPass *pass = graph->exec.pass; | |
605 |
2/2✓ Branch 0 taken 3854 times.
✓ Branch 1 taken 483907 times.
|
487761 | const SwsImg *input = pass->input ? &pass->input->output : &graph->exec.input; |
606 |
2/2✓ Branch 0 taken 3854 times.
✓ Branch 1 taken 483907 times.
|
487761 | const SwsImg *output = pass->output.fmt != AV_PIX_FMT_NONE ? &pass->output : &graph->exec.output; |
607 | 487761 | const int slice_y = jobnr * pass->slice_h; | |
608 | 487761 | const int slice_h = FFMIN(pass->slice_h, pass->height - slice_y); | |
609 | |||
610 | 487761 | pass->run(output, input, slice_y, slice_h, pass); | |
611 | 487761 | } | |
612 | |||
613 | 5552 | int ff_sws_graph_create(SwsContext *ctx, const SwsFormat *dst, const SwsFormat *src, | |
614 | int field, SwsGraph **out_graph) | ||
615 | { | ||
616 | int ret; | ||
617 | 5552 | SwsGraph *graph = av_mallocz(sizeof(*graph)); | |
618 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5552 times.
|
5552 | if (!graph) |
619 | ✗ | return AVERROR(ENOMEM); | |
620 | |||
621 | 5552 | graph->ctx = ctx; | |
622 | 5552 | graph->src = *src; | |
623 | 5552 | graph->dst = *dst; | |
624 | 5552 | graph->field = field; | |
625 | 5552 | graph->opts_copy = *ctx; | |
626 | |||
627 | 5552 | graph->exec.input.fmt = src->format; | |
628 | 5552 | graph->exec.output.fmt = dst->format; | |
629 | |||
630 | 5552 | ret = avpriv_slicethread_create(&graph->slicethread, (void *) graph, | |
631 | sws_graph_worker, NULL, ctx->threads); | ||
632 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5552 times.
|
5552 | if (ret == AVERROR(ENOSYS)) |
633 | ✗ | graph->num_threads = 1; | |
634 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5552 times.
|
5552 | else if (ret < 0) |
635 | ✗ | goto error; | |
636 | else | ||
637 | 5552 | graph->num_threads = ret; | |
638 | |||
639 | 5552 | ret = init_passes(graph); | |
640 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5552 times.
|
5552 | if (ret < 0) |
641 | ✗ | goto error; | |
642 | |||
643 | 5552 | *out_graph = graph; | |
644 | 5552 | return 0; | |
645 | |||
646 | ✗ | error: | |
647 | ✗ | ff_sws_graph_free(&graph); | |
648 | ✗ | return ret; | |
649 | } | ||
650 | |||
651 | 161519 | void ff_sws_graph_free(SwsGraph **pgraph) | |
652 | { | ||
653 | 161519 | SwsGraph *graph = *pgraph; | |
654 |
2/2✓ Branch 0 taken 155967 times.
✓ Branch 1 taken 5552 times.
|
161519 | if (!graph) |
655 | 155967 | return; | |
656 | |||
657 | 5552 | avpriv_slicethread_free(&graph->slicethread); | |
658 | |||
659 |
2/2✓ Branch 0 taken 5605 times.
✓ Branch 1 taken 5552 times.
|
11157 | for (int i = 0; i < graph->num_passes; i++) { |
660 | 5605 | SwsPass *pass = graph->passes[i]; | |
661 |
2/2✓ Branch 0 taken 5553 times.
✓ Branch 1 taken 52 times.
|
5605 | if (pass->free) |
662 | 5553 | pass->free(pass->priv); | |
663 |
2/2✓ Branch 0 taken 53 times.
✓ Branch 1 taken 5552 times.
|
5605 | if (pass->output.fmt != AV_PIX_FMT_NONE) |
664 | 53 | av_free(pass->output.data[0]); | |
665 | 5605 | av_free(pass); | |
666 | } | ||
667 | 5552 | av_free(graph->passes); | |
668 | |||
669 | 5552 | av_free(graph); | |
670 | 5552 | *pgraph = NULL; | |
671 | } | ||
672 | |||
673 | /* Tests only options relevant to SwsGraph */ | ||
674 | 78773 | static int opts_equal(const SwsContext *c1, const SwsContext *c2) | |
675 | { | ||
676 | 157546 | return c1->flags == c2->flags && | |
677 |
1/2✓ Branch 0 taken 78773 times.
✗ Branch 1 not taken.
|
78773 | c1->threads == c2->threads && |
678 |
1/2✓ Branch 0 taken 78773 times.
✗ Branch 1 not taken.
|
78773 | c1->dither == c2->dither && |
679 |
1/2✓ Branch 0 taken 78773 times.
✗ Branch 1 not taken.
|
78773 | c1->alpha_blend == c2->alpha_blend && |
680 |
1/2✓ Branch 0 taken 78773 times.
✗ Branch 1 not taken.
|
78773 | c1->gamma_flag == c2->gamma_flag && |
681 |
1/2✓ Branch 0 taken 78773 times.
✗ Branch 1 not taken.
|
78773 | c1->src_h_chr_pos == c2->src_h_chr_pos && |
682 |
1/2✓ Branch 0 taken 78773 times.
✗ Branch 1 not taken.
|
78773 | c1->src_v_chr_pos == c2->src_v_chr_pos && |
683 |
1/2✓ Branch 0 taken 78773 times.
✗ Branch 1 not taken.
|
78773 | c1->dst_h_chr_pos == c2->dst_h_chr_pos && |
684 |
1/2✓ Branch 0 taken 78773 times.
✗ Branch 1 not taken.
|
78773 | c1->dst_v_chr_pos == c2->dst_v_chr_pos && |
685 |
2/4✓ Branch 0 taken 78773 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 78773 times.
✗ Branch 3 not taken.
|
236319 | c1->intent == c2->intent && |
686 |
1/2✓ Branch 0 taken 78773 times.
✗ Branch 1 not taken.
|
78773 | !memcmp(c1->scaler_params, c2->scaler_params, sizeof(c1->scaler_params)); |
687 | |||
688 | } | ||
689 | |||
690 | 84325 | int ff_sws_graph_reinit(SwsContext *ctx, const SwsFormat *dst, const SwsFormat *src, | |
691 | int field, SwsGraph **out_graph) | ||
692 | { | ||
693 | 84325 | SwsGraph *graph = *out_graph; | |
694 |
4/6✓ Branch 0 taken 78773 times.
✓ Branch 1 taken 5552 times.
✓ Branch 3 taken 78773 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 78773 times.
✗ Branch 6 not taken.
|
163098 | if (graph && ff_fmt_equal(&graph->src, src) && |
695 |
1/2✓ Branch 1 taken 78773 times.
✗ Branch 2 not taken.
|
157546 | ff_fmt_equal(&graph->dst, dst) && |
696 | 78773 | opts_equal(ctx, &graph->opts_copy)) | |
697 | { | ||
698 | 78773 | ff_sws_graph_update_metadata(graph, &src->color); | |
699 | 78773 | return 0; | |
700 | } | ||
701 | |||
702 | 5552 | ff_sws_graph_free(out_graph); | |
703 | 5552 | return ff_sws_graph_create(ctx, dst, src, field, out_graph); | |
704 | } | ||
705 | |||
706 | 78773 | void ff_sws_graph_update_metadata(SwsGraph *graph, const SwsColor *color) | |
707 | { | ||
708 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 78773 times.
|
78773 | if (!color) |
709 | ✗ | return; | |
710 | |||
711 | 78773 | ff_color_update_dynamic(&graph->src.color, color); | |
712 | } | ||
713 | |||
714 | 84325 | void ff_sws_graph_run(SwsGraph *graph, uint8_t *const out_data[4], | |
715 | const int out_linesize[4], | ||
716 | const uint8_t *const in_data[4], | ||
717 | const int in_linesize[4]) | ||
718 | { | ||
719 | 84325 | SwsImg *out = &graph->exec.output; | |
720 | 84325 | SwsImg *in = &graph->exec.input; | |
721 | 84325 | memcpy(out->data, out_data, sizeof(out->data)); | |
722 | 84325 | memcpy(out->linesize, out_linesize, sizeof(out->linesize)); | |
723 | 84325 | memcpy(in->data, in_data, sizeof(in->data)); | |
724 | 84325 | memcpy(in->linesize, in_linesize, sizeof(in->linesize)); | |
725 | |||
726 |
2/2✓ Branch 0 taken 84795 times.
✓ Branch 1 taken 84325 times.
|
169120 | for (int i = 0; i < graph->num_passes; i++) { |
727 | 84795 | const SwsPass *pass = graph->passes[i]; | |
728 | 84795 | graph->exec.pass = pass; | |
729 |
2/2✓ Branch 0 taken 84326 times.
✓ Branch 1 taken 469 times.
|
84795 | if (pass->setup) |
730 | 84326 | pass->setup(out, in, pass); | |
731 | 84795 | avpriv_slicethread_execute(graph->slicethread, pass->num_slices, 0); | |
732 | } | ||
733 | 84325 | } | |
734 |