FFmpeg coverage


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