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 | 5561 | static int pass_alloc_output(SwsPass *pass) | |
39 | { | ||
40 |
3/4✓ Branch 0 taken 53 times.
✓ Branch 1 taken 5508 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 53 times.
|
5561 | if (!pass || pass->output.fmt != AV_PIX_FMT_NONE) |
41 | 5508 | 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 | 5561 | 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 | 5561 | SwsPass *pass = av_mallocz(sizeof(*pass)); | |
54 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5561 times.
|
5561 | if (!pass) |
55 | ✗ | return NULL; | |
56 | |||
57 | 5561 | pass->graph = graph; | |
58 | 5561 | pass->run = run; | |
59 | 5561 | pass->priv = priv; | |
60 | 5561 | pass->format = fmt; | |
61 | 5561 | pass->width = w; | |
62 | 5561 | pass->height = h; | |
63 | 5561 | pass->input = input; | |
64 | 5561 | pass->output.fmt = AV_PIX_FMT_NONE; | |
65 | |||
66 | 5561 | ret = pass_alloc_output(input); | |
67 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5561 times.
|
5561 | if (ret < 0) { |
68 | ✗ | av_free(pass); | |
69 | ✗ | return NULL; | |
70 | } | ||
71 | |||
72 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 5550 times.
|
5561 | if (!slice_align) { |
73 | 11 | pass->slice_h = pass->height; | |
74 | 11 | pass->num_slices = 1; | |
75 | } else { | ||
76 | 5550 | pass->slice_h = (pass->height + graph->num_threads - 1) / graph->num_threads; | |
77 | 5550 | pass->slice_h = FFALIGN(pass->slice_h, slice_align); | |
78 | 5550 | pass->num_slices = (pass->height + pass->slice_h - 1) / pass->slice_h; | |
79 | } | ||
80 | |||
81 | 5561 | ret = av_dynarray_add_nofree(&graph->passes, &graph->num_passes, pass); | |
82 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5561 times.
|
5561 | if (ret < 0) |
83 | ✗ | av_freep(&pass); | |
84 | 5561 | 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 | 1101265 | static int vshift(enum AVPixelFormat fmt, int plane) | |
100 | { | ||
101 | 1101265 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt); | |
102 |
4/4✓ Branch 0 taken 758472 times.
✓ Branch 1 taken 342793 times.
✓ Branch 2 taken 266440 times.
✓ Branch 3 taken 492032 times.
|
1101265 | return (plane == 1 || plane == 2) ? desc->log2_chroma_h : 0; |
103 | } | ||
104 | |||
105 | /* Shift an image vertically by y lines */ | ||
106 | 483549 | static SwsImg shift_img(const SwsImg *img_base, int y) | |
107 | { | ||
108 | 483549 | SwsImg img = *img_base; | |
109 |
4/4✓ Branch 0 taken 1576331 times.
✓ Branch 1 taken 8483 times.
✓ Branch 2 taken 1101265 times.
✓ Branch 3 taken 475066 times.
|
1584814 | for (int i = 0; i < 4 && img.data[i]; i++) |
110 | 1101265 | img.data[i] += (y >> vshift(img.fmt, i)) * img.linesize[i]; | |
111 | 483549 | 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 | 5509 | static void free_legacy_swscale(void *priv) | |
179 | { | ||
180 | 5509 | SwsContext *sws = priv; | |
181 | 5509 | sws_free_context(&sws); | |
182 | 5509 | } | |
183 | |||
184 | 84282 | static void setup_legacy_swscale(const SwsImg *out, const SwsImg *in, | |
185 | const SwsPass *pass) | ||
186 | { | ||
187 | 84282 | SwsContext *sws = pass->priv; | |
188 | 84282 | SwsInternal *c = sws_internal(sws); | |
189 |
5/6✓ Branch 0 taken 77086 times.
✓ Branch 1 taken 7196 times.
✓ Branch 2 taken 870 times.
✓ Branch 3 taken 76216 times.
✓ Branch 4 taken 870 times.
✗ Branch 5 not taken.
|
84282 | 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 79697 times.
|
84282 | if (usePal(sws->src_format)) |
195 | 4585 | ff_update_palette(c, (const uint32_t *) in->data[1]); | |
196 | 84282 | } | |
197 | |||
198 | 483549 | static inline SwsContext *slice_ctx(const SwsPass *pass, int y) | |
199 | { | ||
200 | 483549 | SwsContext *sws = pass->priv; | |
201 | 483549 | SwsInternal *parent = sws_internal(sws); | |
202 |
2/2✓ Branch 0 taken 34350 times.
✓ Branch 1 taken 449199 times.
|
483549 | if (pass->num_slices == 1) |
203 | 34350 | return sws; | |
204 | |||
205 | av_assert1(parent->nb_slice_ctx == pass->num_slices); | ||
206 | 449199 | sws = parent->slice_ctx[y / pass->slice_h]; | |
207 | |||
208 |
2/2✓ Branch 1 taken 34415 times.
✓ Branch 2 taken 414784 times.
|
449199 | 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 | 449199 | 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 | 406302 | static void run_legacy_swscale(const SwsImg *out_base, const SwsImg *in, | |
229 | int y, int h, const SwsPass *pass) | ||
230 | { | ||
231 | 406302 | SwsContext *sws = slice_ctx(pass, y); | |
232 | 406302 | SwsInternal *c = sws_internal(sws); | |
233 | 406302 | const SwsImg out = shift_img(out_base, y); | |
234 | |||
235 | 406302 | ff_swscale(c, (const uint8_t *const *) in->data, in->linesize, 0, | |
236 | sws->src_h, out.data, out.linesize, y, h); | ||
237 | 406302 | } | |
238 | |||
239 | 11016 | static void get_chroma_pos(SwsGraph *graph, int *h_chr_pos, int *v_chr_pos, | |
240 | const SwsFormat *fmt) | ||
241 | { | ||
242 | 11016 | enum AVChromaLocation chroma_loc = fmt->loc; | |
243 | 11016 | const int sub_x = fmt->desc->log2_chroma_w; | |
244 | 11016 | 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 11015 times.
✓ Branch 1 taken 1 times.
|
11016 | if (chroma_loc == AVCHROMA_LOC_UNSPECIFIED) { |
249 | 11015 | chroma_loc = AVCHROMA_LOC_CENTER; | |
250 |
4/4✓ Branch 0 taken 5360 times.
✓ Branch 1 taken 5655 times.
✓ Branch 2 taken 204 times.
✓ Branch 3 taken 5156 times.
|
11015 | 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 | 11016 | av_chroma_location_enum_to_pos(&x_pos, &y_pos, chroma_loc); | |
257 | 11016 | x_pos *= (1 << sub_x) - 1; | |
258 | 11016 | 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 6406 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4610 times.
|
11016 | 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 5360 times.
|
11016 | *h_chr_pos = sub_x ? x_pos : -513; |
279 |
2/2✓ Branch 0 taken 4610 times.
✓ Branch 1 taken 6406 times.
|
11016 | *v_chr_pos = sub_y ? y_pos : -513; |
280 | 11016 | } | |
281 | |||
282 | 22032 | 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 22032 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
22032 | if (override == -513 || override == *chr_pos) |
285 | 22032 | 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 | 5510 | static int init_legacy_subpass(SwsGraph *graph, SwsContext *sws, | |
298 | SwsPass *input, SwsPass **output) | ||
299 | { | ||
300 | 5510 | SwsInternal *c = sws_internal(sws); | |
301 | 5510 | const int src_w = sws->src_w, src_h = sws->src_h; | |
302 | 5510 | const int dst_w = sws->dst_w, dst_h = sws->dst_h; | |
303 |
4/4✓ Branch 0 taken 5066 times.
✓ Branch 1 taken 444 times.
✓ Branch 2 taken 5056 times.
✓ Branch 3 taken 10 times.
|
5510 | const int unscaled = src_w == dst_w && src_h == dst_h; |
304 | 5510 | int align = c->dst_slice_align; | |
305 | 5510 | SwsPass *pass = NULL; | |
306 | int ret; | ||
307 | |||
308 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5509 times.
|
5510 | 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 5498 times.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
|
5509 | 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 5484 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 4 times.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 15 times.
|
5509 | 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 5498 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
5509 | 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 | 5509 | 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 4908 times.
|
5509 | c->convert_unscaled ? run_legacy_unscaled : run_legacy_swscale); |
341 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5509 times.
|
5509 | if (!pass) |
342 | ✗ | return AVERROR(ENOMEM); | |
343 | 5509 | pass->setup = setup_legacy_swscale; | |
344 | 5509 | 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 1855 times.
✓ Branch 1 taken 3654 times.
|
5509 | if (pass->num_slices > 1) { |
354 | 1855 | c->slice_ctx = av_calloc(pass->num_slices, sizeof(*c->slice_ctx)); | |
355 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1855 times.
|
1855 | if (!c->slice_ctx) |
356 | ✗ | return AVERROR(ENOMEM); | |
357 | |||
358 |
2/2✓ Branch 0 taken 16656 times.
✓ Branch 1 taken 1855 times.
|
18511 | for (int i = 0; i < pass->num_slices; i++) { |
359 | SwsContext *slice; | ||
360 | SwsInternal *c2; | ||
361 | 16656 | slice = c->slice_ctx[i] = sws_alloc_context(); | |
362 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16656 times.
|
16656 | if (!slice) |
363 | ✗ | return AVERROR(ENOMEM); | |
364 | 16656 | c->nb_slice_ctx++; | |
365 | |||
366 | 16656 | c2 = sws_internal(slice); | |
367 | 16656 | c2->parent = sws; | |
368 | |||
369 | 16656 | ret = av_opt_copy(slice, sws); | |
370 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16656 times.
|
16656 | if (ret < 0) |
371 | ✗ | return ret; | |
372 | |||
373 | 16656 | ret = ff_sws_init_single_context(slice, NULL, NULL); | |
374 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16656 times.
|
16656 | if (ret < 0) |
375 | ✗ | return ret; | |
376 | |||
377 | 16656 | sws_setColorspaceDetails(slice, c->srcColorspaceTable, | |
378 | 16656 | slice->src_range, c->dstColorspaceTable, | |
379 | slice->dst_range, c->brightness, c->contrast, | ||
380 | c->saturation); | ||
381 | |||
382 |
2/2✓ Branch 0 taken 66624 times.
✓ Branch 1 taken 16656 times.
|
83280 | for (int i = 0; i < FF_ARRAY_ELEMS(c->srcColorspaceTable); i++) { |
383 | 66624 | c2->srcColorspaceTable[i] = c->srcColorspaceTable[i]; | |
384 | 66624 | c2->dstColorspaceTable[i] = c->dstColorspaceTable[i]; | |
385 | } | ||
386 | } | ||
387 | } | ||
388 | |||
389 |
5/6✓ Branch 0 taken 35 times.
✓ Branch 1 taken 5474 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 33 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
5509 | 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 | 5509 | *output = pass; | |
396 | 5509 | return 0; | |
397 | } | ||
398 | |||
399 | 5508 | static int add_legacy_sws_pass(SwsGraph *graph, SwsFormat src, SwsFormat dst, | |
400 | SwsPass *input, SwsPass **output) | ||
401 | { | ||
402 | 5508 | int ret, warned = 0; | |
403 | 5508 | SwsContext *const ctx = graph->ctx; | |
404 | 5508 | SwsContext *sws = sws_alloc_context(); | |
405 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5508 times.
|
5508 | if (!sws) |
406 | ✗ | return AVERROR(ENOMEM); | |
407 | |||
408 | 5508 | sws->flags = ctx->flags; | |
409 | 5508 | sws->dither = ctx->dither; | |
410 | 5508 | sws->alpha_blend = ctx->alpha_blend; | |
411 | 5508 | sws->gamma_flag = ctx->gamma_flag; | |
412 | |||
413 | 5508 | sws->src_w = src.width; | |
414 | 5508 | sws->src_h = src.height; | |
415 | 5508 | sws->src_format = src.format; | |
416 | 5508 | sws->src_range = src.range == AVCOL_RANGE_JPEG; | |
417 | |||
418 | 5508 | sws->dst_w = dst.width; | |
419 | 5508 | sws->dst_h = dst.height; | |
420 | 5508 | sws->dst_format = dst.format; | |
421 | 5508 | sws->dst_range = dst.range == AVCOL_RANGE_JPEG; | |
422 | 5508 | get_chroma_pos(graph, &sws->src_h_chr_pos, &sws->src_v_chr_pos, &src); | |
423 | 5508 | get_chroma_pos(graph, &sws->dst_h_chr_pos, &sws->dst_v_chr_pos, &dst); | |
424 | |||
425 | 5508 | graph->incomplete |= src.range == AVCOL_RANGE_UNSPECIFIED; | |
426 | 5508 | graph->incomplete |= dst.range == AVCOL_RANGE_UNSPECIFIED; | |
427 | |||
428 | /* Allow overriding chroma position with the legacy API */ | ||
429 | 5508 | legacy_chr_pos(graph, &sws->src_h_chr_pos, ctx->src_h_chr_pos, &warned); | |
430 | 5508 | legacy_chr_pos(graph, &sws->src_v_chr_pos, ctx->src_v_chr_pos, &warned); | |
431 | 5508 | legacy_chr_pos(graph, &sws->dst_h_chr_pos, ctx->dst_h_chr_pos, &warned); | |
432 | 5508 | legacy_chr_pos(graph, &sws->dst_v_chr_pos, ctx->dst_v_chr_pos, &warned); | |
433 | |||
434 | 5508 | ret = sws_init_context(sws, NULL, NULL); | |
435 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5508 times.
|
5508 | if (ret < 0) { |
436 | ✗ | sws_free_context(&sws); | |
437 | ✗ | return ret; | |
438 | } | ||
439 | |||
440 | /* Set correct color matrices */ | ||
441 | { | ||
442 | int in_full, out_full, brightness, contrast, saturation; | ||
443 | const int *inv_table, *table; | ||
444 | 5508 | sws_getColorspaceDetails(sws, (int **)&inv_table, &in_full, | |
445 | (int **)&table, &out_full, | ||
446 | &brightness, &contrast, &saturation); | ||
447 | |||
448 | 5508 | inv_table = sws_getCoefficients(src.csp); | |
449 | 5508 | table = sws_getCoefficients(dst.csp); | |
450 | |||
451 |
2/2✓ Branch 0 taken 2021 times.
✓ Branch 1 taken 3487 times.
|
7529 | graph->incomplete |= src.csp != dst.csp && |
452 |
2/2✓ Branch 0 taken 582 times.
✓ Branch 1 taken 1439 times.
|
2021 | (src.csp == AVCOL_SPC_UNSPECIFIED || |
453 |
2/2✓ Branch 0 taken 573 times.
✓ Branch 1 taken 9 times.
|
582 | dst.csp == AVCOL_SPC_UNSPECIFIED); |
454 | |||
455 | 5508 | sws_setColorspaceDetails(sws, inv_table, in_full, table, out_full, | |
456 | brightness, contrast, saturation); | ||
457 | } | ||
458 | |||
459 | 5508 | ret = init_legacy_subpass(graph, sws, input, output); | |
460 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5508 times.
|
5508 | if (ret < 0) { |
461 | ✗ | sws_free_context(&sws); | |
462 | ✗ | return ret; | |
463 | } | ||
464 | |||
465 | 5508 | return 0; | |
466 | } | ||
467 | |||
468 | /************************** | ||
469 | * Gamut and tone mapping * | ||
470 | **************************/ | ||
471 | |||
472 | ✗ | static void free_lut3d(void *priv) | |
473 | { | ||
474 | ✗ | SwsLut3D *lut = priv; | |
475 | ✗ | ff_sws_lut3d_free(&lut); | |
476 | ✗ | } | |
477 | |||
478 | ✗ | static void setup_lut3d(const SwsImg *out, const SwsImg *in, const SwsPass *pass) | |
479 | { | ||
480 | ✗ | SwsLut3D *lut = pass->priv; | |
481 | |||
482 | /* Update dynamic frame metadata from the original source frame */ | ||
483 | ✗ | ff_sws_lut3d_update(lut, &pass->graph->src.color); | |
484 | ✗ | } | |
485 | |||
486 | ✗ | static void run_lut3d(const SwsImg *out_base, const SwsImg *in_base, | |
487 | int y, int h, const SwsPass *pass) | ||
488 | { | ||
489 | ✗ | SwsLut3D *lut = pass->priv; | |
490 | ✗ | const SwsImg in = shift_img(in_base, y); | |
491 | ✗ | const SwsImg out = shift_img(out_base, y); | |
492 | |||
493 | ✗ | ff_sws_lut3d_apply(lut, in.data[0], in.linesize[0], out.data[0], | |
494 | ✗ | out.linesize[0], pass->width, h); | |
495 | ✗ | } | |
496 | |||
497 | 5508 | static int adapt_colors(SwsGraph *graph, SwsFormat src, SwsFormat dst, | |
498 | SwsPass *input, SwsPass **output) | ||
499 | { | ||
500 | enum AVPixelFormat fmt_in, fmt_out; | ||
501 | 5508 | SwsColorMap map = {0}; | |
502 | SwsLut3D *lut; | ||
503 | SwsPass *pass; | ||
504 | int ret; | ||
505 | |||
506 | /** | ||
507 | * Grayspace does not really have primaries, so just force the use of | ||
508 | * the equivalent other primary set to avoid a conversion. Technically, | ||
509 | * this does affect the weights used for the Grayscale conversion, but | ||
510 | * in practise, that should give the expected results more often than not. | ||
511 | */ | ||
512 |
2/2✓ Branch 1 taken 316 times.
✓ Branch 2 taken 5192 times.
|
5508 | if (isGray(dst.format)) { |
513 | 316 | dst.color = src.color; | |
514 |
2/2✓ Branch 1 taken 70 times.
✓ Branch 2 taken 5122 times.
|
5192 | } else if (isGray(src.format)) { |
515 | 70 | src.color = dst.color; | |
516 | } | ||
517 | |||
518 | /* Fully infer color spaces before color mapping logic */ | ||
519 | 5508 | graph->incomplete |= ff_infer_colors(&src.color, &dst.color); | |
520 | |||
521 | 5508 | map.intent = graph->ctx->intent; | |
522 | 5508 | map.src = src.color; | |
523 | 5508 | map.dst = dst.color; | |
524 | |||
525 |
1/2✓ Branch 1 taken 5508 times.
✗ Branch 2 not taken.
|
5508 | if (ff_sws_color_map_noop(&map)) |
526 | 5508 | return 0; | |
527 | |||
528 | ✗ | lut = ff_sws_lut3d_alloc(); | |
529 | ✗ | if (!lut) | |
530 | ✗ | return AVERROR(ENOMEM); | |
531 | |||
532 | ✗ | fmt_in = ff_sws_lut3d_pick_pixfmt(src, 0); | |
533 | ✗ | fmt_out = ff_sws_lut3d_pick_pixfmt(dst, 1); | |
534 | ✗ | if (fmt_in != src.format) { | |
535 | ✗ | SwsFormat tmp = src; | |
536 | ✗ | tmp.format = fmt_in; | |
537 | ✗ | ret = add_legacy_sws_pass(graph, src, tmp, input, &input); | |
538 | ✗ | if (ret < 0) | |
539 | ✗ | return ret; | |
540 | } | ||
541 | |||
542 | ✗ | ret = ff_sws_lut3d_generate(lut, fmt_in, fmt_out, &map); | |
543 | ✗ | if (ret < 0) { | |
544 | ✗ | ff_sws_lut3d_free(&lut); | |
545 | ✗ | return ret; | |
546 | } | ||
547 | |||
548 | ✗ | pass = pass_add(graph, lut, fmt_out, src.width, src.height, | |
549 | input, 1, run_lut3d); | ||
550 | ✗ | if (!pass) { | |
551 | ✗ | ff_sws_lut3d_free(&lut); | |
552 | ✗ | return AVERROR(ENOMEM); | |
553 | } | ||
554 | ✗ | pass->setup = setup_lut3d; | |
555 | ✗ | pass->free = free_lut3d; | |
556 | |||
557 | ✗ | *output = pass; | |
558 | ✗ | return 0; | |
559 | } | ||
560 | |||
561 | /*************************************** | ||
562 | * Main filter graph construction code * | ||
563 | ***************************************/ | ||
564 | |||
565 | 5508 | static int init_passes(SwsGraph *graph) | |
566 | { | ||
567 | 5508 | SwsFormat src = graph->src; | |
568 | 5508 | SwsFormat dst = graph->dst; | |
569 | 5508 | SwsPass *pass = NULL; /* read from main input image */ | |
570 | int ret; | ||
571 | |||
572 | 5508 | ret = adapt_colors(graph, src, dst, pass, &pass); | |
573 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5508 times.
|
5508 | if (ret < 0) |
574 | ✗ | return ret; | |
575 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5508 times.
|
5508 | src.format = pass ? pass->format : src.format; |
576 | 5508 | src.color = dst.color; | |
577 | |||
578 |
1/2✓ Branch 1 taken 5508 times.
✗ Branch 2 not taken.
|
5508 | if (!ff_fmt_equal(&src, &dst)) { |
579 | 5508 | ret = add_legacy_sws_pass(graph, src, dst, pass, &pass); | |
580 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5508 times.
|
5508 | if (ret < 0) |
581 | ✗ | return ret; | |
582 | } | ||
583 | |||
584 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5508 times.
|
5508 | if (!pass) { |
585 | /* No passes were added, so no operations were necessary */ | ||
586 | ✗ | graph->noop = 1; | |
587 | |||
588 | /* Add threaded memcpy pass */ | ||
589 | ✗ | pass = pass_add(graph, NULL, dst.format, dst.width, dst.height, pass, 1, run_copy); | |
590 | ✗ | if (!pass) | |
591 | ✗ | return AVERROR(ENOMEM); | |
592 | } | ||
593 | |||
594 | 5508 | return 0; | |
595 | } | ||
596 | |||
597 | 487394 | static void sws_graph_worker(void *priv, int jobnr, int threadnr, int nb_jobs, | |
598 | int nb_threads) | ||
599 | { | ||
600 | 487394 | SwsGraph *graph = priv; | |
601 | 487394 | const SwsPass *pass = graph->exec.pass; | |
602 |
2/2✓ Branch 0 taken 3854 times.
✓ Branch 1 taken 483540 times.
|
487394 | const SwsImg *input = pass->input ? &pass->input->output : &graph->exec.input; |
603 |
2/2✓ Branch 0 taken 3854 times.
✓ Branch 1 taken 483540 times.
|
487394 | const SwsImg *output = pass->output.fmt != AV_PIX_FMT_NONE ? &pass->output : &graph->exec.output; |
604 | 487394 | const int slice_y = jobnr * pass->slice_h; | |
605 | 487394 | const int slice_h = FFMIN(pass->slice_h, pass->height - slice_y); | |
606 | |||
607 | 487394 | pass->run(output, input, slice_y, slice_h, pass); | |
608 | 487394 | } | |
609 | |||
610 | 5508 | int ff_sws_graph_create(SwsContext *ctx, const SwsFormat *dst, const SwsFormat *src, | |
611 | int field, SwsGraph **out_graph) | ||
612 | { | ||
613 | int ret; | ||
614 | 5508 | SwsGraph *graph = av_mallocz(sizeof(*graph)); | |
615 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5508 times.
|
5508 | if (!graph) |
616 | ✗ | return AVERROR(ENOMEM); | |
617 | |||
618 | 5508 | graph->ctx = ctx; | |
619 | 5508 | graph->src = *src; | |
620 | 5508 | graph->dst = *dst; | |
621 | 5508 | graph->field = field; | |
622 | 5508 | graph->opts_copy = *ctx; | |
623 | |||
624 | 5508 | graph->exec.input.fmt = src->format; | |
625 | 5508 | graph->exec.output.fmt = dst->format; | |
626 | |||
627 | 5508 | ret = avpriv_slicethread_create(&graph->slicethread, (void *) graph, | |
628 | sws_graph_worker, NULL, ctx->threads); | ||
629 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5508 times.
|
5508 | if (ret == AVERROR(ENOSYS)) |
630 | ✗ | graph->num_threads = 1; | |
631 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5508 times.
|
5508 | else if (ret < 0) |
632 | ✗ | goto error; | |
633 | else | ||
634 | 5508 | graph->num_threads = ret; | |
635 | |||
636 | 5508 | ret = init_passes(graph); | |
637 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5508 times.
|
5508 | if (ret < 0) |
638 | ✗ | goto error; | |
639 | |||
640 | 5508 | *out_graph = graph; | |
641 | 5508 | return 0; | |
642 | |||
643 | ✗ | error: | |
644 | ✗ | ff_sws_graph_free(&graph); | |
645 | ✗ | return ret; | |
646 | } | ||
647 | |||
648 | 160609 | void ff_sws_graph_free(SwsGraph **pgraph) | |
649 | { | ||
650 | 160609 | SwsGraph *graph = *pgraph; | |
651 |
2/2✓ Branch 0 taken 155101 times.
✓ Branch 1 taken 5508 times.
|
160609 | if (!graph) |
652 | 155101 | return; | |
653 | |||
654 | 5508 | avpriv_slicethread_free(&graph->slicethread); | |
655 | |||
656 |
2/2✓ Branch 0 taken 5561 times.
✓ Branch 1 taken 5508 times.
|
11069 | for (int i = 0; i < graph->num_passes; i++) { |
657 | 5561 | SwsPass *pass = graph->passes[i]; | |
658 |
2/2✓ Branch 0 taken 5509 times.
✓ Branch 1 taken 52 times.
|
5561 | if (pass->free) |
659 | 5509 | pass->free(pass->priv); | |
660 |
2/2✓ Branch 0 taken 53 times.
✓ Branch 1 taken 5508 times.
|
5561 | if (pass->output.fmt != AV_PIX_FMT_NONE) |
661 | 53 | av_free(pass->output.data[0]); | |
662 | 5561 | av_free(pass); | |
663 | } | ||
664 | 5508 | av_free(graph->passes); | |
665 | |||
666 | 5508 | av_free(graph); | |
667 | 5508 | *pgraph = NULL; | |
668 | } | ||
669 | |||
670 | /* Tests only options relevant to SwsGraph */ | ||
671 | 78773 | static int opts_equal(const SwsContext *c1, const SwsContext *c2) | |
672 | { | ||
673 | 157546 | return c1->flags == c2->flags && | |
674 |
1/2✓ Branch 0 taken 78773 times.
✗ Branch 1 not taken.
|
78773 | c1->threads == c2->threads && |
675 |
1/2✓ Branch 0 taken 78773 times.
✗ Branch 1 not taken.
|
78773 | c1->dither == c2->dither && |
676 |
1/2✓ Branch 0 taken 78773 times.
✗ Branch 1 not taken.
|
78773 | c1->alpha_blend == c2->alpha_blend && |
677 |
1/2✓ Branch 0 taken 78773 times.
✗ Branch 1 not taken.
|
78773 | c1->gamma_flag == c2->gamma_flag && |
678 |
1/2✓ Branch 0 taken 78773 times.
✗ Branch 1 not taken.
|
78773 | c1->src_h_chr_pos == c2->src_h_chr_pos && |
679 |
1/2✓ Branch 0 taken 78773 times.
✗ Branch 1 not taken.
|
78773 | c1->src_v_chr_pos == c2->src_v_chr_pos && |
680 |
1/2✓ Branch 0 taken 78773 times.
✗ Branch 1 not taken.
|
78773 | c1->dst_h_chr_pos == c2->dst_h_chr_pos && |
681 |
1/2✓ Branch 0 taken 78773 times.
✗ Branch 1 not taken.
|
78773 | c1->dst_v_chr_pos == c2->dst_v_chr_pos && |
682 |
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 && |
683 |
1/2✓ Branch 0 taken 78773 times.
✗ Branch 1 not taken.
|
78773 | !memcmp(c1->scaler_params, c2->scaler_params, sizeof(c1->scaler_params)); |
684 | |||
685 | } | ||
686 | |||
687 | 84281 | int ff_sws_graph_reinit(SwsContext *ctx, const SwsFormat *dst, const SwsFormat *src, | |
688 | int field, SwsGraph **out_graph) | ||
689 | { | ||
690 | 84281 | SwsGraph *graph = *out_graph; | |
691 |
4/6✓ Branch 0 taken 78773 times.
✓ Branch 1 taken 5508 times.
✓ Branch 3 taken 78773 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 78773 times.
✗ Branch 6 not taken.
|
163054 | if (graph && ff_fmt_equal(&graph->src, src) && |
692 |
1/2✓ Branch 1 taken 78773 times.
✗ Branch 2 not taken.
|
157546 | ff_fmt_equal(&graph->dst, dst) && |
693 | 78773 | opts_equal(ctx, &graph->opts_copy)) | |
694 | { | ||
695 | 78773 | ff_sws_graph_update_metadata(graph, &src->color); | |
696 | 78773 | return 0; | |
697 | } | ||
698 | |||
699 | 5508 | ff_sws_graph_free(out_graph); | |
700 | 5508 | return ff_sws_graph_create(ctx, dst, src, field, out_graph); | |
701 | } | ||
702 | |||
703 | 78773 | void ff_sws_graph_update_metadata(SwsGraph *graph, const SwsColor *color) | |
704 | { | ||
705 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 78773 times.
|
78773 | if (!color) |
706 | ✗ | return; | |
707 | |||
708 | 78773 | ff_color_update_dynamic(&graph->src.color, color); | |
709 | } | ||
710 | |||
711 | 84281 | void ff_sws_graph_run(SwsGraph *graph, uint8_t *const out_data[4], | |
712 | const int out_linesize[4], | ||
713 | const uint8_t *const in_data[4], | ||
714 | const int in_linesize[4]) | ||
715 | { | ||
716 | 84281 | SwsImg *out = &graph->exec.output; | |
717 | 84281 | SwsImg *in = &graph->exec.input; | |
718 | 84281 | memcpy(out->data, out_data, sizeof(out->data)); | |
719 | 84281 | memcpy(out->linesize, out_linesize, sizeof(out->linesize)); | |
720 | 84281 | memcpy(in->data, in_data, sizeof(in->data)); | |
721 | 84281 | memcpy(in->linesize, in_linesize, sizeof(in->linesize)); | |
722 | |||
723 |
2/2✓ Branch 0 taken 84751 times.
✓ Branch 1 taken 84281 times.
|
169032 | for (int i = 0; i < graph->num_passes; i++) { |
724 | 84751 | const SwsPass *pass = graph->passes[i]; | |
725 | 84751 | graph->exec.pass = pass; | |
726 |
2/2✓ Branch 0 taken 84282 times.
✓ Branch 1 taken 469 times.
|
84751 | if (pass->setup) |
727 | 84282 | pass->setup(out, in, pass); | |
728 | 84751 | avpriv_slicethread_execute(graph->slicethread, pass->num_slices, 0); | |
729 | } | ||
730 | 84281 | } | |
731 |