Extend aphasemeter to detect out of phase or mono sequences in stereo streams.
Showing the changes to libavfilter (1 of 2 files). Show all
Unified · Split
| @@ -28,26 +28,43 @@ · 27 unmodified lines | ||
| 28 | 28 | #include "libavutil/intreadwrite.h" |
| 29 | 29 | #include "libavutil/opt.h" |
| 30 | 30 | #include "libavutil/parseutils.h" |
| 31 | +#include "libavutil/timestamp.h" | |
| 31 | 32 | #include "avfilter.h" |
| 32 | 33 | #include "formats.h" |
| 33 | 34 | #include "audio.h" |
| 34 | 35 | #include "video.h" |
| 35 | 36 | #include "internal.h" |
| 37 | +#include "float.h" | |
| 36 | 38 | |
| 37 | 39 | typedef struct AudioPhaseMeterContext { |
| 38 | 40 | const AVClass *class; |
| 39 | 41 | AVFrame *out; |
| 40 | 42 | int do_video; |
| 43 | + int do_phasing_detection; | |
| 41 | 44 | int w, h; |
| 42 | 45 | AVRational frame_rate; |
| 43 | 46 | int contrast[4]; |
| 44 | 47 | uint8_t *mpc_str; |
| 45 | 48 | uint8_t mpc[4]; |
| 46 | 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 | 62 | } AudioPhaseMeterContext; |
| 48 | 63 | |
| 64 | +#define MAX_DURATION (24*60*60*1000000LL) | |
| 49 | 65 | #define OFFSET(x) offsetof(AudioPhaseMeterContext, x) |
| 50 | 66 | #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM |
| 67 | +#define get_duration(index) (index[1] - index[0]) | |
| 51 | 68 | |
| 52 | 69 | static const AVOption aphasemeter_options[] = { |
| 53 | 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 | 76 | { "bc", "set blue contrast", OFFSET(contrast[2]), AV_OPT_TYPE_INT, {.i64=1}, 0, 255, FLAGS }, |
| 60 | 77 | { "mpc", "set median phase color", OFFSET(mpc_str), AV_OPT_TYPE_STRING, {.str = "none"}, 0, 0, FLAGS }, |
| 61 | 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 | 86 | { NULL } |
| 63 | 87 | }; |
| 64 | 88 | |
| @@ -104,6 +128,7 @@ static int config_input(AVFilterLink *inlink) · 39 unmodified lines | ||
| 104 | 128 | AVFilterContext *ctx = inlink->dst; |
| 105 | 129 | AudioPhaseMeterContext *s = ctx->priv; |
| 106 | 130 | int nb_samples; |
| 131 | + s->duration = av_rescale(s->duration, inlink->sample_rate, AV_TIME_BASE); | |
| 107 | 132 | |
| 108 | 133 | if (s->do_video) { |
| 109 | 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 | 165 | return (phase + 1.) / 2. * (w - 1); |
| 141 | 166 | } |
| 142 | 167 | |
| 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 | + | |
| 143 | 238 | static int filter_frame(AVFilterLink *inlink, AVFrame *in) |
| 144 | 239 | { |
| 145 | 240 | AVFilterContext *ctx = inlink->dst; |
| @@ -154,6 +249,10 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 8 unmodified lines | ||
| 154 | 249 | AVFrame *out; |
| 155 | 250 | uint8_t *dst; |
| 156 | 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 | 257 | if (s->do_video && (!s->out || s->out->width != outlink->w || |
| 159 | 258 | s->out->height != outlink->h)) { |
| @@ -193,6 +292,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 33 unmodified lines | ||
| 193 | 292 | fphase += phase; |
| 194 | 293 | } |
| 195 | 294 | fphase /= in->nb_samples; |
| 295 | + s->phase = fphase; | |
| 196 | 296 | |
| 197 | 297 | if (s->do_video) { |
| 198 | 298 | if (s->draw_median_phase) { |
| @@ -209,7 +309,19 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 10 unmodified lines | ||
| 209 | 309 | uint8_t value[128]; |
| 210 | 310 | |
| 211 | 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 | 327 | if (s->do_video) { |
| @@ -228,6 +340,10 @@ static av_cold void uninit(AVFilterContext *ctx) · 12 unmodified lines | ||
| 228 | 340 | { |
| 229 | 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 | 347 | av_frame_free(&s->out); |
| 232 | 348 | } |
| 233 | 349 | |