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
23356Enable video output. Default is enabled.23356Enable video output. Default is enabled.
23357@end table23357@end table
23358 23358
23359@subsection phasing detection
23360
23361The filter also detects out of phase and mono sequences in stereo streams.
23362It logs the sequence start, end and duration when it lasts longer or as long as the minimum set.
23363
23364The filter accepts the following options for this detection:
23365
23366@table @option
23367@item phasing
23368Enable mono and out of phase detection. Default is disabled.
23369
23370@item tolerance, t
23371Set phase tolerance for mono detection, in amplitude ratio. Default is @code{0}.
23372Allowed range is @code{[0, 1]}.
23373
23374@item angle, a
23375Set angle threshold for out of phase detection, in degree. Default is @code{170}.
23376Allowed range is @code{[90, 180]}.
23377
23378@item duration, d
23379Set 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
23386Complete example with @command{ffmpeg} to detect 1 second of mono with 0.001 phase tolerance:
23387@example
23388ffmpeg -i stereo.wav -af aphasemeter=video=0:phasing=1:duration=1:tolerance=0.001 -f null -
23389@end example
23390@end itemize
23391
23359@section avectorscope23392@section avectorscope
23360 23393
23361Convert input audio to a video output, representing the audio vector23394Convert input audio to a video output, representing the audio vector
libavfilter/avf_aphasemeter.c+117−1
@@ -28,26 +28,43 @@ · 27 unmodified lines
28#include "libavutil/intreadwrite.h"28#include "libavutil/intreadwrite.h"
29#include "libavutil/opt.h"29#include "libavutil/opt.h"
30#include "libavutil/parseutils.h"30#include "libavutil/parseutils.h"
31#include "libavutil/timestamp.h"
31#include "avfilter.h"32#include "avfilter.h"
32#include "formats.h"33#include "formats.h"
33#include "audio.h"34#include "audio.h"
34#include "video.h"35#include "video.h"
35#include "internal.h"36#include "internal.h"
37#include "float.h"
36 38
37typedef struct AudioPhaseMeterContext {39typedef struct AudioPhaseMeterContext {
38 const AVClass *class;40 const AVClass *class;
39 AVFrame *out;41 AVFrame *out;
40 int do_video;42 int do_video;
43 int do_phasing_detection;
41 int w, h;44 int w, h;
42 AVRational frame_rate;45 AVRational frame_rate;
43 int contrast[4];46 int contrast[4];
44 uint8_t *mpc_str;47 uint8_t *mpc_str;
45 uint8_t mpc[4];48 uint8_t mpc[4];
46 int draw_median_phase;49 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];
47} AudioPhaseMeterContext;62} AudioPhaseMeterContext;
48 63
64#define MAX_DURATION (24*60*60*1000000LL)
49#define OFFSET(x) offsetof(AudioPhaseMeterContext, x)65#define OFFSET(x) offsetof(AudioPhaseMeterContext, x)
50#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM66#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
67#define get_duration(index) (index[1] - index[0])
51 68
52static const AVOption aphasemeter_options[] = {69static const AVOption aphasemeter_options[] = {
53 { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },70 { "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
59 { "bc", "set blue contrast", OFFSET(contrast[2]), AV_OPT_TYPE_INT, {.i64=1}, 0, 255, FLAGS },76 { "bc", "set blue contrast", OFFSET(contrast[2]), AV_OPT_TYPE_INT, {.i64=1}, 0, 255, FLAGS },
60 { "mpc", "set median phase color", OFFSET(mpc_str), AV_OPT_TYPE_STRING, {.str = "none"}, 0, 0, FLAGS },77 { "mpc", "set median phase color", OFFSET(mpc_str), AV_OPT_TYPE_STRING, {.str = "none"}, 0, 0, FLAGS },
61 { "video", "set video output", OFFSET(do_video), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS },78 { "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 },
62 { NULL }86 { NULL }
63};87};
64 88
@@ -104,6 +128,7 @@ static int config_input(AVFilterLink *inlink) · 39 unmodified lines
104 AVFilterContext *ctx = inlink->dst;128 AVFilterContext *ctx = inlink->dst;
105 AudioPhaseMeterContext *s = ctx->priv;129 AudioPhaseMeterContext *s = ctx->priv;
106 int nb_samples;130 int nb_samples;
131 s->duration = av_rescale(s->duration, inlink->sample_rate, AV_TIME_BASE);
107 132
108 if (s->do_video) {133 if (s->do_video) {
109 nb_samples = FFMAX(1, av_rescale(inlink->sample_rate, s->frame_rate.den, s->frame_rate.num));134 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
140 return (phase + 1.) / 2. * (w - 1);165 return (phase + 1.) / 2. * (w - 1);
141}166}
142 167
168static 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
176static 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
207static 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
143static int filter_frame(AVFilterLink *inlink, AVFrame *in)238static int filter_frame(AVFilterLink *inlink, AVFrame *in)
144{239{
145 AVFilterContext *ctx = inlink->dst;240 AVFilterContext *ctx = inlink->dst;
@@ -154,6 +249,10 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 8 unmodified lines
154 AVFrame *out;249 AVFrame *out;
155 uint8_t *dst;250 uint8_t *dst;
156 int i;251 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);
157 256
158 if (s->do_video && (!s->out || s->out->width != outlink->w ||257 if (s->do_video && (!s->out || s->out->width != outlink->w ||
159 s->out->height != outlink->h)) {258 s->out->height != outlink->h)) {
@@ -193,6 +292,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 33 unmodified lines
193 fphase += phase;292 fphase += phase;
194 }293 }
195 fphase /= in->nb_samples;294 fphase /= in->nb_samples;
295 s->phase = fphase;
196 296
197 if (s->do_video) {297 if (s->do_video) {
198 if (s->draw_median_phase) {298 if (s->draw_median_phase) {
@@ -209,7 +309,19 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 10 unmodified lines
209 uint8_t value[128];309 uint8_t value[128];
210 310
211 snprintf(value, sizeof(value), "%f", fphase);311 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);
213 }325 }
214 326
215 if (s->do_video) {327 if (s->do_video) {
@@ -228,6 +340,10 @@ static av_cold void uninit(AVFilterContext *ctx) · 12 unmodified lines
228{340{
229 AudioPhaseMeterContext *s = ctx->priv;341 AudioPhaseMeterContext *s = ctx->priv;
230 342
343 if (s->do_phasing_detection) {
344 update_mono_detection(s, NULL, 0);
345 update_out_phase_detection(s, NULL, 0);
346 }
231 av_frame_free(&s->out);347 av_frame_free(&s->out);
232}348}
233 349

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