History

avfilter/avf_aphasemeter: add out of phase and mono detection

Extend aphasemeter to detect out of phase or mono sequences in stereo streams.

Romane Lafon committed 6 years ago

Commit d60927b1 parent 67071522 files · +150 −1Browse files

Unified · Split

2 files
  1. doc/filters.texi +33 −0
  2. libavfilter/avf_aphasemeter.c +117 −1
doc/filters.texi+33−0
@@ -23356,6 +23356,39 @@ Set color which will be used for drawing median phase. If color is · 23355 unmodified lines
2335623356 Enable video output. Default is enabled.
2335723357 @end table
2335823358
23359+@subsection phasing detection
23360+
23361+The filter also detects out of phase and mono sequences in stereo streams.
23362+It logs the sequence start, end and duration when it lasts longer or as long as the minimum set.
23363+
23364+The filter accepts the following options for this detection:
23365+
23366+@table @option
23367+@item phasing
23368+Enable mono and out of phase detection. Default is disabled.
23369+
23370+@item tolerance, t
23371+Set phase tolerance for mono detection, in amplitude ratio. Default is @code{0}.
23372+Allowed range is @code{[0, 1]}.
23373+
23374+@item angle, a
23375+Set angle threshold for out of phase detection, in degree. Default is @code{170}.
23376+Allowed range is @code{[90, 180]}.
23377+
23378+@item duration, d
23379+Set mono or out of phase duration until notification, expressed in seconds. Default is @code{2}.
23380+@end table
23381+
23382+@subsection Examples
23383+
23384+@itemize
23385+@item
23386+Complete example with @command{ffmpeg} to detect 1 second of mono with 0.001 phase tolerance:
23387+@example
23388+ffmpeg -i stereo.wav -af aphasemeter=video=0:phasing=1:duration=1:tolerance=0.001 -f null -
23389+@end example
23390+@end itemize
23391+
2335923392 @section avectorscope
2336023393
2336123394 Convert input audio to a video output, representing the audio vector
libavfilter/avf_aphasemeter.c+117−1
@@ -28,26 +28,43 @@ · 27 unmodified lines
2828 #include "libavutil/intreadwrite.h"
2929 #include "libavutil/opt.h"
3030 #include "libavutil/parseutils.h"
31+#include "libavutil/timestamp.h"
3132 #include "avfilter.h"
3233 #include "formats.h"
3334 #include "audio.h"
3435 #include "video.h"
3536 #include "internal.h"
37+#include "float.h"
3638
3739 typedef struct AudioPhaseMeterContext {
3840 const AVClass *class;
3941 AVFrame *out;
4042 int do_video;
43+ int do_phasing_detection;
4144 int w, h;
4245 AVRational frame_rate;
4346 int contrast[4];
4447 uint8_t *mpc_str;
4548 uint8_t mpc[4];
4649 int draw_median_phase;
50+ int is_mono;
51+ int is_out_phase;
52+ int start_mono_presence;
53+ int start_out_phase_presence;
54+ float tolerance;
55+ float angle;
56+ float phase;
57+ AVRational time_base;
58+ int64_t duration;
59+ int64_t frame_end;
60+ int64_t mono_idx[2];
61+ int64_t out_phase_idx[2];
4762 } AudioPhaseMeterContext;
4863
64+#define MAX_DURATION (24*60*60*1000000LL)
4965 #define OFFSET(x) offsetof(AudioPhaseMeterContext, x)
5066 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
67+#define get_duration(index) (index[1] - index[0])
5168
5269 static const AVOption aphasemeter_options[] = {
5370 { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
@@ -59,6 +76,13 @@ static const AVOption aphasemeter_options[] = { · 5 unmodified lines
5976 { "bc", "set blue contrast", OFFSET(contrast[2]), AV_OPT_TYPE_INT, {.i64=1}, 0, 255, FLAGS },
6077 { "mpc", "set median phase color", OFFSET(mpc_str), AV_OPT_TYPE_STRING, {.str = "none"}, 0, 0, FLAGS },
6178 { "video", "set video output", OFFSET(do_video), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS },
79+ { "phasing", "set mono and out-of-phase detection output", OFFSET(do_phasing_detection), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
80+ { "tolerance", "set phase tolerance for mono detection", OFFSET(tolerance), AV_OPT_TYPE_FLOAT, {.dbl = 0.}, 0, 1, FLAGS },
81+ { "t", "set phase tolerance for mono detection", OFFSET(tolerance), AV_OPT_TYPE_FLOAT, {.dbl = 0.}, 0, 1, FLAGS },
82+ { "angle", "set angle threshold for out-of-phase detection", OFFSET(angle), AV_OPT_TYPE_FLOAT, {.dbl = 170.}, 90, 180, FLAGS },
83+ { "a", "set angle threshold for out-of-phase detection", OFFSET(angle), AV_OPT_TYPE_FLOAT, {.dbl = 170.}, 90, 180, FLAGS },
84+ { "duration", "set minimum mono or out-of-phase duration in seconds", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=2000000}, 0, MAX_DURATION, FLAGS },
85+ { "d", "set minimum mono or out-of-phase duration in seconds", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=2000000}, 0, MAX_DURATION, FLAGS },
6286 { NULL }
6387 };
6488
@@ -104,6 +128,7 @@ static int config_input(AVFilterLink *inlink) · 39 unmodified lines
104128 AVFilterContext *ctx = inlink->dst;
105129 AudioPhaseMeterContext *s = ctx->priv;
106130 int nb_samples;
131+ s->duration = av_rescale(s->duration, inlink->sample_rate, AV_TIME_BASE);
107132
108133 if (s->do_video) {
109134 nb_samples = FFMAX(1, av_rescale(inlink->sample_rate, s->frame_rate.den, s->frame_rate.num));
@@ -140,6 +165,76 @@ static inline int get_x(float phase, int w) · 30 unmodified lines
140165 return (phase + 1.) / 2. * (w - 1);
141166 }
142167
168+static inline void add_metadata(AVFrame *insamples, const char *key, char *value)
169+{
170+ char buf[128];
171+
172+ snprintf(buf, sizeof(buf), "lavfi.aphasemeter.%s", key);
173+ av_dict_set(&insamples->metadata, buf, value, 0);
174+}
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+
143238 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
144239 {
145240 AVFilterContext *ctx = inlink->dst;
@@ -154,6 +249,10 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 8 unmodified lines
154249 AVFrame *out;
155250 uint8_t *dst;
156251 int i;
252+ int mono_measurement;
253+ int out_phase_measurement;
254+ float tolerance = 1.0f - s->tolerance;
255+ float angle = cosf(s->angle/180.0f*M_PI);
157256
158257 if (s->do_video && (!s->out || s->out->width != outlink->w ||
159258 s->out->height != outlink->h)) {
@@ -193,6 +292,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 33 unmodified lines
193292 fphase += phase;
194293 }
195294 fphase /= in->nb_samples;
295+ s->phase = fphase;
196296
197297 if (s->do_video) {
198298 if (s->draw_median_phase) {
@@ -209,7 +309,19 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 10 unmodified lines
209309 uint8_t value[128];
210310
211311 snprintf(value, sizeof(value), "%f", fphase);
212− av_dict_set(metadata, "lavfi.aphasemeter.phase", value, 0);
312+ add_metadata(in, "phase", value);
313+ }
314+
315+ if (s->do_phasing_detection) {
316+ s->time_base = inlink->time_base;
317+ s->frame_end = in->pts + av_rescale_q(in->nb_samples,
318+ (AVRational){ 1, in->sample_rate }, inlink->time_base);
319+
320+ mono_measurement = (tolerance - fphase) < FLT_EPSILON;
321+ out_phase_measurement = (angle - fphase) > FLT_EPSILON;
322+
323+ update_mono_detection(s, in, mono_measurement);
324+ update_out_phase_detection(s, in, out_phase_measurement);
213325 }
214326
215327 if (s->do_video) {
@@ -228,6 +340,10 @@ static av_cold void uninit(AVFilterContext *ctx) · 12 unmodified lines
228340 {
229341 AudioPhaseMeterContext *s = ctx->priv;
230342
343+ if (s->do_phasing_detection) {
344+ update_mono_detection(s, NULL, 0);
345+ update_out_phase_detection(s, NULL, 0);
346+ }
231347 av_frame_free(&s->out);
232348 }
233349

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t