FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/avf_aphasemeter.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 87 216 40.3%
Functions: 8 11 72.7%
Branches: 37 150 24.7%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2015 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 /**
22 * @file
23 * audio to video multimedia aphasemeter filter
24 */
25
26 #include "libavutil/channel_layout.h"
27 #include "libavutil/intreadwrite.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/parseutils.h"
30 #include "libavutil/timestamp.h"
31 #include "avfilter.h"
32 #include "filters.h"
33 #include "formats.h"
34 #include "audio.h"
35 #include "video.h"
36 #include "internal.h"
37 #include "float.h"
38
39 typedef struct AudioPhaseMeterContext {
40 const AVClass *class;
41 AVFrame *out, *in;
42 int64_t last_pts;
43 int do_video;
44 int do_phasing_detection;
45 int w, h;
46 AVRational frame_rate;
47 int contrast[4];
48 uint8_t *mpc_str;
49 uint8_t mpc[4];
50 int draw_median_phase;
51 int is_mono;
52 int is_out_phase;
53 int start_mono_presence;
54 int start_out_phase_presence;
55 int nb_samples;
56 float tolerance;
57 float angle;
58 float phase;
59 AVRational time_base;
60 int64_t duration;
61 int64_t frame_end;
62 int64_t mono_idx[2];
63 int64_t out_phase_idx[2];
64 } AudioPhaseMeterContext;
65
66 #define MAX_DURATION (24*60*60*1000000LL)
67 #define OFFSET(x) offsetof(AudioPhaseMeterContext, x)
68 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
69 #define get_duration(index) (index[1] - index[0])
70
71 static const AVOption aphasemeter_options[] = {
72 { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
73 { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
74 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="800x400"}, 0, 0, FLAGS },
75 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="800x400"}, 0, 0, FLAGS },
76 { "rc", "set red contrast", OFFSET(contrast[0]), AV_OPT_TYPE_INT, {.i64=2}, 0, 255, FLAGS },
77 { "gc", "set green contrast", OFFSET(contrast[1]), AV_OPT_TYPE_INT, {.i64=7}, 0, 255, FLAGS },
78 { "bc", "set blue contrast", OFFSET(contrast[2]), AV_OPT_TYPE_INT, {.i64=1}, 0, 255, FLAGS },
79 { "mpc", "set median phase color", OFFSET(mpc_str), AV_OPT_TYPE_STRING, {.str = "none"}, 0, 0, FLAGS },
80 { "video", "set video output", OFFSET(do_video), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS },
81 { "phasing", "set mono and out-of-phase detection output", OFFSET(do_phasing_detection), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
82 { "tolerance", "set phase tolerance for mono detection", OFFSET(tolerance), AV_OPT_TYPE_FLOAT, {.dbl = 0.}, 0, 1, FLAGS },
83 { "t", "set phase tolerance for mono detection", OFFSET(tolerance), AV_OPT_TYPE_FLOAT, {.dbl = 0.}, 0, 1, FLAGS },
84 { "angle", "set angle threshold for out-of-phase detection", OFFSET(angle), AV_OPT_TYPE_FLOAT, {.dbl = 170.}, 90, 180, FLAGS },
85 { "a", "set angle threshold for out-of-phase detection", OFFSET(angle), AV_OPT_TYPE_FLOAT, {.dbl = 170.}, 90, 180, FLAGS },
86 { "duration", "set minimum mono or out-of-phase duration in seconds", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=2000000}, 0, MAX_DURATION, FLAGS },
87 { "d", "set minimum mono or out-of-phase duration in seconds", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=2000000}, 0, MAX_DURATION, FLAGS },
88 { NULL }
89 };
90
91 AVFILTER_DEFINE_CLASS(aphasemeter);
92
93 2 static int query_formats(AVFilterContext *ctx)
94 {
95 2 AudioPhaseMeterContext *s = ctx->priv;
96 2 AVFilterFormats *formats = NULL;
97 2 AVFilterChannelLayouts *layout = NULL;
98 2 AVFilterLink *inlink = ctx->inputs[0];
99 2 AVFilterLink *outlink = ctx->outputs[0];
100 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_NONE };
101 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
102 int ret;
103
104 2 formats = ff_make_format_list(sample_fmts);
105
2/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
4 if ((ret = ff_formats_ref (formats, &inlink->outcfg.formats )) < 0 ||
106
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 (ret = ff_formats_ref (formats, &outlink->incfg.formats )) < 0 ||
107
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 (ret = ff_add_channel_layout (&layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO )) < 0 ||
108
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
4 (ret = ff_channel_layouts_ref (layout , &inlink->outcfg.channel_layouts)) < 0 ||
109 2 (ret = ff_channel_layouts_ref (layout , &outlink->incfg.channel_layouts)) < 0)
110 return ret;
111
112 2 formats = ff_all_samplerates();
113
2/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
4 if ((ret = ff_formats_ref(formats, &inlink->outcfg.samplerates)) < 0 ||
114 2 (ret = ff_formats_ref(formats, &outlink->incfg.samplerates)) < 0)
115 return ret;
116
117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->do_video) {
118 AVFilterLink *outlink = ctx->outputs[1];
119
120 formats = ff_make_format_list(pix_fmts);
121 if ((ret = ff_formats_ref(formats, &outlink->incfg.formats)) < 0)
122 return ret;
123 }
124
125 2 return 0;
126 }
127
128 2 static int config_input(AVFilterLink *inlink)
129 {
130 2 AVFilterContext *ctx = inlink->dst;
131 2 AudioPhaseMeterContext *s = ctx->priv;
132 2 s->duration = av_rescale(s->duration, inlink->sample_rate, AV_TIME_BASE);
133
134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->do_video)
135 s->nb_samples = FFMAX(1, av_rescale(inlink->sample_rate, s->frame_rate.den, s->frame_rate.num));
136
137 2 return 0;
138 }
139
140 static int config_video_output(AVFilterLink *outlink)
141 {
142 AVFilterContext *ctx = outlink->src;
143 AudioPhaseMeterContext *s = ctx->priv;
144
145 s->last_pts = AV_NOPTS_VALUE;
146
147 outlink->w = s->w;
148 outlink->h = s->h;
149 outlink->sample_aspect_ratio = (AVRational){1,1};
150 outlink->frame_rate = s->frame_rate;
151 outlink->time_base = av_inv_q(outlink->frame_rate);
152
153 if (!strcmp(s->mpc_str, "none"))
154 s->draw_median_phase = 0;
155 else if (av_parse_color(s->mpc, s->mpc_str, -1, ctx) >= 0)
156 s->draw_median_phase = 1;
157 else
158 return AVERROR(EINVAL);
159
160 return 0;
161 }
162
163 96000 static inline int get_x(float phase, int w)
164 {
165 96000 return (phase + 1.f) / 2.f * (w - 1.f);
166 }
167
168 58 static inline void add_metadata(AVFrame *insamples, const char *key, char *value)
169 {
170 char buf[128];
171
172 58 snprintf(buf, sizeof(buf), "lavfi.aphasemeter.%s", key);
173 58 av_dict_set(&insamples->metadata, buf, value, 0);
174 58 }
175
176 static inline void update_mono_detection(AudioPhaseMeterContext *s, AVFrame *insamples, int mono_measurement)
177 {
178 int64_t mono_duration;
179 if (!s->is_mono && mono_measurement) {
180 s->is_mono = 1;
181 s->start_mono_presence = 1;
182 s->mono_idx[0] = insamples->pts;
183 }
184 if (s->is_mono && mono_measurement && s->start_mono_presence) {
185 s->mono_idx[1] = s->frame_end;
186 mono_duration = get_duration(s->mono_idx);
187 if (mono_duration >= s->duration) {
188 add_metadata(insamples, "mono_start", av_ts2timestr(s->mono_idx[0], &s->time_base));
189 av_log(s, AV_LOG_INFO, "mono_start: %s\n", av_ts2timestr(s->mono_idx[0], &s->time_base));
190 s->start_mono_presence = 0;
191 }
192 }
193 if (s->is_mono && !mono_measurement) {
194 s->mono_idx[1] = insamples ? insamples->pts : s->frame_end;
195 mono_duration = get_duration(s->mono_idx);
196 if (mono_duration >= s->duration) {
197 if (insamples) {
198 add_metadata(insamples, "mono_end", av_ts2timestr(s->mono_idx[1], &s->time_base));
199 add_metadata(insamples, "mono_duration", av_ts2timestr(mono_duration, &s->time_base));
200 }
201 av_log(s, AV_LOG_INFO, "mono_end: %s | mono_duration: %s\n", av_ts2timestr(s->mono_idx[1], &s->time_base), av_ts2timestr(mono_duration, &s->time_base));
202 }
203 s->is_mono = 0;
204 }
205 }
206
207 static inline void update_out_phase_detection(AudioPhaseMeterContext *s, AVFrame *insamples, int out_phase_measurement)
208 {
209 int64_t out_phase_duration;
210 if (!s->is_out_phase && out_phase_measurement) {
211 s->is_out_phase = 1;
212 s->start_out_phase_presence = 1;
213 s->out_phase_idx[0] = insamples->pts;
214 }
215 if (s->is_out_phase && out_phase_measurement && s->start_out_phase_presence) {
216 s->out_phase_idx[1] = s->frame_end;
217 out_phase_duration = get_duration(s->out_phase_idx);
218 if (out_phase_duration >= s->duration) {
219 add_metadata(insamples, "out_phase_start", av_ts2timestr(s->out_phase_idx[0], &s->time_base));
220 av_log(s, AV_LOG_INFO, "out_phase_start: %s\n", av_ts2timestr(s->out_phase_idx[0], &s->time_base));
221 s->start_out_phase_presence = 0;
222 }
223 }
224 if (s->is_out_phase && !out_phase_measurement) {
225 s->out_phase_idx[1] = insamples ? insamples->pts : s->frame_end;
226 out_phase_duration = get_duration(s->out_phase_idx);
227 if (out_phase_duration >= s->duration) {
228 if (insamples) {
229 add_metadata(insamples, "out_phase_end", av_ts2timestr(s->out_phase_idx[1], &s->time_base));
230 add_metadata(insamples, "out_phase_duration", av_ts2timestr(out_phase_duration, &s->time_base));
231 }
232 av_log(s, AV_LOG_INFO, "out_phase_end: %s | out_phase_duration: %s\n", av_ts2timestr(s->out_phase_idx[1], &s->time_base), av_ts2timestr(out_phase_duration, &s->time_base));
233 }
234 s->is_out_phase = 0;
235 }
236 }
237
238 58 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
239 {
240 58 AVFilterContext *ctx = inlink->dst;
241 58 AudioPhaseMeterContext *s = ctx->priv;
242
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 AVFilterLink *outlink = s->do_video ? ctx->outputs[1] : NULL;
243 58 AVFilterLink *aoutlink = ctx->outputs[0];
244 AVDictionary **metadata;
245 58 const int rc = s->contrast[0];
246 58 const int gc = s->contrast[1];
247 58 const int bc = s->contrast[2];
248 58 float fphase = 0;
249 AVFrame *out;
250 uint8_t *dst;
251 int i, ret;
252 int mono_measurement;
253 int out_phase_measurement;
254 58 float tolerance = 1.0f - s->tolerance;
255 58 float angle = cosf(s->angle/180.0f*M_PIf);
256 int64_t new_pts;
257
258
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
58 if (s->do_video && (!s->out || s->out->width != outlink->w ||
259 s->out->height != outlink->h)) {
260 av_frame_free(&s->out);
261 s->out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
262 if (!s->out) {
263 ret = AVERROR(ENOMEM);
264 goto fail;
265 }
266
267 out = s->out;
268 for (i = 0; i < outlink->h; i++)
269 memset(out->data[0] + i * out->linesize[0], 0, outlink->w * 4);
270
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 } else if (s->do_video) {
271 ret = ff_inlink_make_frame_writable(outlink, &s->out);
272 if (ret < 0)
273 goto fail;
274 out = s->out;
275 for (i = outlink->h - 1; i >= 10; i--)
276 memmove(out->data[0] + (i ) * out->linesize[0],
277 out->data[0] + (i-1) * out->linesize[0],
278 outlink->w * 4);
279 for (i = 0; i < outlink->w; i++)
280 AV_WL32(out->data[0] + i * 4, 0);
281 }
282
283
2/2
✓ Branch 0 taken 96000 times.
✓ Branch 1 taken 58 times.
96058 for (i = 0; i < in->nb_samples; i++) {
284 96000 const float *src = (float *)in->data[0] + i * 2;
285 96000 const float f = src[0] * src[1] / (src[0]*src[0] + src[1] * src[1]) * 2;
286
2/2
✓ Branch 0 taken 95999 times.
✓ Branch 1 taken 1 times.
96000 const float phase = isnan(f) ? 1 : f;
287 96000 const int x = get_x(phase, s->w);
288
289
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 96000 times.
96000 if (s->do_video) {
290 dst = out->data[0] + x * 4;
291 dst[0] = FFMIN(255, dst[0] + rc);
292 dst[1] = FFMIN(255, dst[1] + gc);
293 dst[2] = FFMIN(255, dst[2] + bc);
294 dst[3] = 255;
295 }
296 96000 fphase += phase;
297 }
298 58 fphase /= in->nb_samples;
299 58 s->phase = fphase;
300
301
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (s->do_video) {
302 if (s->draw_median_phase) {
303 dst = out->data[0] + get_x(fphase, s->w) * 4;
304 AV_WL32(dst, AV_RL32(s->mpc));
305 }
306
307 for (i = 1; i < 10 && i < outlink->h; i++)
308 memcpy(out->data[0] + i * out->linesize[0], out->data[0], outlink->w * 4);
309 }
310
311 58 metadata = &in->metadata;
312
1/2
✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
58 if (metadata) {
313 uint8_t value[128];
314
315 58 snprintf(value, sizeof(value), "%f", fphase);
316 58 add_metadata(in, "phase", value);
317 }
318
319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (s->do_phasing_detection) {
320 s->time_base = inlink->time_base;
321 s->frame_end = in->pts + av_rescale_q(in->nb_samples,
322 (AVRational){ 1, in->sample_rate }, inlink->time_base);
323
324 mono_measurement = (tolerance - fphase) < FLT_EPSILON;
325 out_phase_measurement = (angle - fphase) > FLT_EPSILON;
326
327 update_mono_detection(s, in, mono_measurement);
328 update_out_phase_detection(s, in, out_phase_measurement);
329 }
330
331
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (s->do_video)
332 new_pts = av_rescale_q(in->pts, inlink->time_base, outlink->time_base);
333
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
58 if (s->do_video && new_pts != s->last_pts) {
334 AVFrame *clone;
335
336 s->out->pts = s->last_pts = new_pts;
337 s->out->duration = 1;
338
339 clone = av_frame_clone(s->out);
340 if (!clone) {
341 ret = AVERROR(ENOMEM);
342 goto fail;
343 }
344 ret = ff_filter_frame(outlink, clone);
345 if (ret < 0)
346 goto fail;
347 }
348 58 s->in = NULL;
349 58 return ff_filter_frame(aoutlink, in);
350 fail:
351 av_frame_free(&in);
352 s->in = NULL;
353 return ret;
354 }
355
356 120 static int activate(AVFilterContext *ctx)
357 {
358 120 AVFilterLink *inlink = ctx->inputs[0];
359 120 AVFilterLink *outlink = ctx->outputs[0];
360 120 AudioPhaseMeterContext *s = ctx->priv;
361 int ret;
362
363
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 120 times.
120 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
364
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 120 times.
120 if (s->do_video)
365 FF_FILTER_FORWARD_STATUS_BACK(ctx->outputs[1], inlink);
366
367
1/2
✓ Branch 0 taken 120 times.
✗ Branch 1 not taken.
120 if (!s->in) {
368
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 120 times.
120 if (s->nb_samples > 0)
369 ret = ff_inlink_consume_samples(inlink, s->nb_samples, s->nb_samples, &s->in);
370 else
371 120 ret = ff_inlink_consume_frame(inlink, &s->in);
372
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 120 times.
120 if (ret < 0)
373 return ret;
374
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 62 times.
120 if (ret > 0)
375 58 return filter_frame(inlink, s->in);
376 }
377
378
4/4
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 60 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 2 times.
64 FF_FILTER_FORWARD_STATUS_ALL(inlink, ctx);
379
1/2
✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
60 FF_FILTER_FORWARD_WANTED(outlink, inlink);
380 if (s->do_video)
381 FF_FILTER_FORWARD_WANTED(ctx->outputs[1], inlink);
382
383 return FFERROR_NOT_READY;
384 }
385
386 2 static av_cold void uninit(AVFilterContext *ctx)
387 {
388 2 AudioPhaseMeterContext *s = ctx->priv;
389
390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->do_phasing_detection) {
391 update_mono_detection(s, NULL, 0);
392 update_out_phase_detection(s, NULL, 0);
393 }
394 2 av_frame_free(&s->out);
395 2 }
396
397 2 static av_cold int init(AVFilterContext *ctx)
398 {
399 2 AudioPhaseMeterContext *s = ctx->priv;
400 AVFilterPad pad;
401 int ret;
402
403 2 pad = (AVFilterPad){
404 .name = "out0",
405 .type = AVMEDIA_TYPE_AUDIO,
406 };
407 2 ret = ff_append_outpad(ctx, &pad);
408
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
409 return ret;
410
411
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->do_video) {
412 pad = (AVFilterPad){
413 .name = "out1",
414 .type = AVMEDIA_TYPE_VIDEO,
415 .config_props = config_video_output,
416 };
417 ret = ff_append_outpad(ctx, &pad);
418 if (ret < 0)
419 return ret;
420 }
421
422 2 return 0;
423 }
424
425 static const AVFilterPad inputs[] = {
426 {
427 .name = "default",
428 .type = AVMEDIA_TYPE_AUDIO,
429 .config_props = config_input,
430 },
431 };
432
433 const AVFilter ff_avf_aphasemeter = {
434 .name = "aphasemeter",
435 .description = NULL_IF_CONFIG_SMALL("Convert input audio to phase meter video output."),
436 .init = init,
437 .uninit = uninit,
438 .priv_size = sizeof(AudioPhaseMeterContext),
439 FILTER_INPUTS(inputs),
440 .activate = activate,
441 .outputs = NULL,
442 FILTER_QUERY_FUNC(query_formats),
443 .priv_class = &aphasemeter_class,
444 .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
445 };
446