FFmpeg coverage


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