FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_mix.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 0 182 0.0%
Branches: 0 134 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2017 Paul B Mahol
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 "config_components.h"
22
23 #include "libavutil/avstring.h"
24 #include "libavutil/imgutils.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/opt.h"
27 #include "libavutil/pixdesc.h"
28
29 #include "avfilter.h"
30 #include "formats.h"
31 #include "internal.h"
32 #include "framesync.h"
33 #include "video.h"
34
35 typedef struct MixContext {
36 const AVClass *class;
37 const AVPixFmtDescriptor *desc;
38 char *weights_str;
39 int nb_inputs;
40 int nb_threads;
41 int duration;
42 float *weights;
43 float scale;
44 float wfactor;
45
46 int tmix;
47 int nb_frames;
48
49 int depth;
50 int max;
51 int planes;
52 int nb_planes;
53 int linesizes[4];
54 int height[4];
55
56 uint8_t **data;
57 int *linesize;
58
59 AVFrame **frames;
60 FFFrameSync fs;
61 } MixContext;
62
63 static int query_formats(AVFilterContext *ctx)
64 {
65 unsigned reject_flags = AV_PIX_FMT_FLAG_BITSTREAM |
66 AV_PIX_FMT_FLAG_HWACCEL |
67 AV_PIX_FMT_FLAG_PAL;
68 unsigned accept_flags = 0;
69
70 if (!HAVE_BIGENDIAN)
71 reject_flags |= AV_PIX_FMT_FLAG_BE;
72 else
73 accept_flags |= AV_PIX_FMT_FLAG_BE;
74
75 return ff_set_common_formats(ctx, ff_formats_pixdesc_filter(accept_flags, reject_flags));
76 }
77
78 static int parse_weights(AVFilterContext *ctx)
79 {
80 MixContext *s = ctx->priv;
81 char *p, *arg, *saveptr = NULL;
82 int i, last = 0;
83
84 s->wfactor = 0.f;
85 p = s->weights_str;
86 for (i = 0; i < s->nb_inputs; i++) {
87 if (!(arg = av_strtok(p, " |", &saveptr)))
88 break;
89
90 p = NULL;
91 if (av_sscanf(arg, "%f", &s->weights[i]) != 1) {
92 av_log(ctx, AV_LOG_ERROR, "Invalid syntax for weights[%d].\n", i);
93 return AVERROR(EINVAL);
94 }
95 s->wfactor += s->weights[i];
96 last = i;
97 }
98
99 for (; i < s->nb_inputs; i++) {
100 s->weights[i] = s->weights[last];
101 s->wfactor += s->weights[i];
102 }
103 if (s->scale == 0) {
104 s->wfactor = 1 / s->wfactor;
105 } else {
106 s->wfactor = s->scale;
107 }
108
109 return 0;
110 }
111
112 static av_cold int init(AVFilterContext *ctx)
113 {
114 MixContext *s = ctx->priv;
115 int ret;
116
117 s->tmix = !strcmp(ctx->filter->name, "tmix");
118
119 s->frames = av_calloc(s->nb_inputs, sizeof(*s->frames));
120 if (!s->frames)
121 return AVERROR(ENOMEM);
122
123 s->weights = av_calloc(s->nb_inputs, sizeof(*s->weights));
124 if (!s->weights)
125 return AVERROR(ENOMEM);
126
127 if (!s->tmix) {
128 for (int i = 0; i < s->nb_inputs; i++) {
129 AVFilterPad pad = { 0 };
130
131 pad.type = AVMEDIA_TYPE_VIDEO;
132 pad.name = av_asprintf("input%d", i);
133 if (!pad.name)
134 return AVERROR(ENOMEM);
135
136 if ((ret = ff_append_inpad_free_name(ctx, &pad)) < 0)
137 return ret;
138 }
139 }
140
141 return parse_weights(ctx);
142 }
143
144 typedef struct ThreadData {
145 AVFrame **in, *out;
146 } ThreadData;
147
148 #define MIX_SLICE(type, fun, clip) \
149 for (int p = 0; p < s->nb_planes; p++) { \
150 const int slice_start = (s->height[p] * jobnr) / nb_jobs; \
151 const int slice_end = (s->height[p] * (jobnr+1)) / nb_jobs; \
152 const int width = s->linesizes[p] / sizeof(type); \
153 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
154 ptrdiff_t dst_linesize = out->linesize[p] / sizeof(type); \
155 \
156 if (!((1 << p) & s->planes)) { \
157 av_image_copy_plane((uint8_t *)dst, out->linesize[p], \
158 in[0]->data[p] + slice_start * in[0]->linesize[p], \
159 in[0]->linesize[p], \
160 s->linesizes[p], slice_end - slice_start); \
161 continue; \
162 } \
163 \
164 for (int i = 0; i < s->nb_inputs; i++) \
165 linesize[i] = in[i]->linesize[p]; \
166 \
167 for (int i = 0; i < s->nb_inputs; i++) \
168 srcf[i] = in[i]->data[p] + slice_start * linesize[i]; \
169 \
170 for (int y = slice_start; y < slice_end; y++) { \
171 for (int x = 0; x < width; x++) { \
172 float val = 0.f; \
173 \
174 for (int i = 0; i < s->nb_inputs; i++) { \
175 float src = *(type *)(srcf[i] + x * sizeof(type)); \
176 \
177 val += src * weights[i]; \
178 } \
179 \
180 dst[x] = clip(fun(val * s->wfactor), 0, s->max); \
181 } \
182 \
183 dst += dst_linesize; \
184 for (int i = 0; i < s->nb_inputs; i++) \
185 srcf[i] += linesize[i]; \
186 } \
187 }
188
189 #define CLIP8(x, min, max) av_clip_uint8(x)
190 #define CLIP16(x, min, max) av_clip(x, min, max)
191 #define CLIPF(x, min, max) (x)
192 #define NOP(x) (x)
193
194 static int mix_frames(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
195 {
196 MixContext *s = ctx->priv;
197 ThreadData *td = arg;
198 AVFrame **in = td->in;
199 AVFrame *out = td->out;
200 const float *weights = s->weights;
201 uint8_t **srcf = s->data + jobnr * s->nb_inputs;
202 int *linesize = s->linesize + jobnr * s->nb_inputs;
203
204 if (s->depth <= 8) {
205 MIX_SLICE(uint8_t, lrintf, CLIP8)
206 } else if (s->depth <= 16) {
207 MIX_SLICE(uint16_t, lrintf, CLIP16)
208 } else {
209 MIX_SLICE(float, NOP, CLIPF)
210 }
211
212 return 0;
213 }
214
215 static int process_frame(FFFrameSync *fs)
216 {
217 AVFilterContext *ctx = fs->parent;
218 AVFilterLink *outlink = ctx->outputs[0];
219 MixContext *s = fs->opaque;
220 AVFrame **in = s->frames;
221 AVFrame *out;
222 ThreadData td;
223 int i, ret;
224
225 for (i = 0; i < s->nb_inputs; i++) {
226 if ((ret = ff_framesync_get_frame(&s->fs, i, &in[i], 0)) < 0)
227 return ret;
228 }
229
230 if (ctx->is_disabled) {
231 out = av_frame_clone(s->frames[0]);
232 if (!out)
233 return AVERROR(ENOMEM);
234 out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
235 return ff_filter_frame(outlink, out);
236 }
237
238 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
239 if (!out)
240 return AVERROR(ENOMEM);
241 out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
242
243 td.in = in;
244 td.out = out;
245 ff_filter_execute(ctx, mix_frames, &td, NULL,
246 FFMIN(s->height[1], s->nb_threads));
247
248 return ff_filter_frame(outlink, out);
249 }
250
251 static int config_output(AVFilterLink *outlink)
252 {
253 AVFilterContext *ctx = outlink->src;
254 MixContext *s = ctx->priv;
255 AVRational frame_rate = ctx->inputs[0]->frame_rate;
256 AVRational sar = ctx->inputs[0]->sample_aspect_ratio;
257 AVFilterLink *inlink = ctx->inputs[0];
258 int height = ctx->inputs[0]->h;
259 int width = ctx->inputs[0]->w;
260 FFFrameSyncIn *in;
261 int i, ret;
262
263 if (!s->tmix) {
264 for (i = 1; i < s->nb_inputs; i++) {
265 if (ctx->inputs[i]->h != height || ctx->inputs[i]->w != width) {
266 av_log(ctx, AV_LOG_ERROR, "Input %d size (%dx%d) does not match input %d size (%dx%d).\n", i, ctx->inputs[i]->w, ctx->inputs[i]->h, 0, width, height);
267 return AVERROR(EINVAL);
268 }
269 }
270 }
271
272 s->nb_threads = ff_filter_get_nb_threads(ctx);
273 s->desc = av_pix_fmt_desc_get(outlink->format);
274 if (!s->desc)
275 return AVERROR_BUG;
276 s->nb_planes = av_pix_fmt_count_planes(outlink->format);
277 s->depth = s->desc->comp[0].depth;
278 s->max = (1 << s->depth) - 1;
279
280 if ((ret = av_image_fill_linesizes(s->linesizes, inlink->format, inlink->w)) < 0)
281 return ret;
282
283 s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
284 s->height[0] = s->height[3] = inlink->h;
285
286 s->data = av_calloc(s->nb_threads * s->nb_inputs, sizeof(*s->data));
287 if (!s->data)
288 return AVERROR(ENOMEM);
289
290 s->linesize = av_calloc(s->nb_threads * s->nb_inputs, sizeof(*s->linesize));
291 if (!s->linesize)
292 return AVERROR(ENOMEM);
293
294 if (s->tmix)
295 return 0;
296
297 outlink->w = width;
298 outlink->h = height;
299 outlink->frame_rate = frame_rate;
300 outlink->sample_aspect_ratio = sar;
301
302 if ((ret = ff_framesync_init(&s->fs, ctx, s->nb_inputs)) < 0)
303 return ret;
304
305 in = s->fs.in;
306 s->fs.opaque = s;
307 s->fs.on_event = process_frame;
308
309 for (i = 0; i < s->nb_inputs; i++) {
310 AVFilterLink *inlink = ctx->inputs[i];
311
312 in[i].time_base = inlink->time_base;
313 in[i].sync = 1;
314 in[i].before = EXT_STOP;
315 in[i].after = (s->duration == 1 || (s->duration == 2 && i == 0)) ? EXT_STOP : EXT_INFINITY;
316 }
317
318 ret = ff_framesync_configure(&s->fs);
319 outlink->time_base = s->fs.time_base;
320
321 return ret;
322 }
323
324 static av_cold void uninit(AVFilterContext *ctx)
325 {
326 MixContext *s = ctx->priv;
327 int i;
328
329 ff_framesync_uninit(&s->fs);
330 av_freep(&s->weights);
331 av_freep(&s->data);
332 av_freep(&s->linesize);
333
334 if (s->tmix) {
335 for (i = 0; i < s->nb_frames && s->frames; i++)
336 av_frame_free(&s->frames[i]);
337 }
338 av_freep(&s->frames);
339 }
340
341 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
342 char *res, int res_len, int flags)
343 {
344 int ret;
345
346 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
347 if (ret < 0)
348 return ret;
349
350 return parse_weights(ctx);
351 }
352
353 static int activate(AVFilterContext *ctx)
354 {
355 MixContext *s = ctx->priv;
356 return ff_framesync_activate(&s->fs);
357 }
358
359 #define OFFSET(x) offsetof(MixContext, x)
360 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
361 #define TFLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM
362
363 static const AVOption mix_options[] = {
364 { "inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=2}, 2, INT16_MAX, .flags = FLAGS },
365 { "weights", "set weight for each input", OFFSET(weights_str), AV_OPT_TYPE_STRING, {.str="1 1"}, 0, 0, .flags = TFLAGS },
366 { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, INT16_MAX, .flags = TFLAGS },
367 { "planes", "set what planes to filter", OFFSET(planes), AV_OPT_TYPE_FLAGS, {.i64=15}, 0, 15, .flags = TFLAGS },
368 { "duration", "how to determine end of stream", OFFSET(duration), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, .flags = FLAGS, "duration" },
369 { "longest", "Duration of longest input", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "duration" },
370 { "shortest", "Duration of shortest input", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "duration" },
371 { "first", "Duration of first input", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "duration" },
372 { NULL },
373 };
374
375 static const AVFilterPad outputs[] = {
376 {
377 .name = "default",
378 .type = AVMEDIA_TYPE_VIDEO,
379 .config_props = config_output,
380 },
381 };
382
383 #if CONFIG_MIX_FILTER
384 AVFILTER_DEFINE_CLASS(mix);
385
386 const AVFilter ff_vf_mix = {
387 .name = "mix",
388 .description = NULL_IF_CONFIG_SMALL("Mix video inputs."),
389 .priv_size = sizeof(MixContext),
390 .priv_class = &mix_class,
391 FILTER_OUTPUTS(outputs),
392 FILTER_QUERY_FUNC(query_formats),
393 .init = init,
394 .uninit = uninit,
395 .activate = activate,
396 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_SLICE_THREADS |
397 AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
398 .process_command = process_command,
399 };
400
401 #endif /* CONFIG_MIX_FILTER */
402
403 #if CONFIG_TMIX_FILTER
404 static int tmix_filter_frame(AVFilterLink *inlink, AVFrame *in)
405 {
406 AVFilterContext *ctx = inlink->dst;
407 AVFilterLink *outlink = ctx->outputs[0];
408 MixContext *s = ctx->priv;
409 ThreadData td;
410 AVFrame *out;
411
412 if (s->nb_inputs == 1)
413 return ff_filter_frame(outlink, in);
414
415 if (s->nb_frames < s->nb_inputs) {
416 s->frames[s->nb_frames] = in;
417 s->nb_frames++;
418 while (s->nb_frames < s->nb_inputs) {
419 s->frames[s->nb_frames] = av_frame_clone(s->frames[s->nb_frames - 1]);
420 if (!s->frames[s->nb_frames])
421 return AVERROR(ENOMEM);
422 s->nb_frames++;
423 }
424 } else {
425 av_frame_free(&s->frames[0]);
426 memmove(&s->frames[0], &s->frames[1], sizeof(*s->frames) * (s->nb_inputs - 1));
427 s->frames[s->nb_inputs - 1] = in;
428 }
429
430 if (ctx->is_disabled) {
431 out = av_frame_clone(s->frames[0]);
432 if (!out)
433 return AVERROR(ENOMEM);
434 return ff_filter_frame(outlink, out);
435 }
436
437 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
438 if (!out)
439 return AVERROR(ENOMEM);
440 out->pts = s->frames[s->nb_frames - 1]->pts;
441
442 td.out = out;
443 td.in = s->frames;
444 ff_filter_execute(ctx, mix_frames, &td, NULL,
445 FFMIN(s->height[1], s->nb_threads));
446
447 return ff_filter_frame(outlink, out);
448 }
449
450 static const AVOption tmix_options[] = {
451 { "frames", "set number of successive frames to mix", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=3}, 1, 1024, .flags = FLAGS },
452 { "weights", "set weight for each frame", OFFSET(weights_str), AV_OPT_TYPE_STRING, {.str="1 1 1"}, 0, 0, .flags = TFLAGS },
453 { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, INT16_MAX, .flags = TFLAGS },
454 { "planes", "set what planes to filter", OFFSET(planes), AV_OPT_TYPE_FLAGS, {.i64=15}, 0, 15, .flags = TFLAGS },
455 { NULL },
456 };
457
458 static const AVFilterPad inputs[] = {
459 {
460 .name = "default",
461 .type = AVMEDIA_TYPE_VIDEO,
462 .filter_frame = tmix_filter_frame,
463 },
464 };
465
466 AVFILTER_DEFINE_CLASS(tmix);
467
468 const AVFilter ff_vf_tmix = {
469 .name = "tmix",
470 .description = NULL_IF_CONFIG_SMALL("Mix successive video frames."),
471 .priv_size = sizeof(MixContext),
472 .priv_class = &tmix_class,
473 FILTER_OUTPUTS(outputs),
474 FILTER_INPUTS(inputs),
475 FILTER_QUERY_FUNC(query_formats),
476 .init = init,
477 .uninit = uninit,
478 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
479 .process_command = process_command,
480 };
481
482 #endif /* CONFIG_TMIX_FILTER */
483