New version of the patch that extends aphasemeter filter.
It allows to get metadata for out-of-phase or mono sequences of stereo
streams.
It displays start, end and duration as for silencedetect filter.
From 8eacd044bb4c69553e25fbd08c3be07c00af5be0 Mon Sep 17 00:00:00 2001
From: Romane Lafon
Date: Mon, 17 Jun 2019 14:31:14 +0200
Subject: [PATCH] avfilter/avf_aphasemeter: Add out of phase and mono detection
Signed-off-by: Romane Lafon
---
libavfilter/avf_aphasemeter.c | 127 --
1 file changed, 123 insertions(+), 4 deletions(-)
diff --git a/libavfilter/avf_aphasemeter.c b/libavfilter/avf_aphasemeter.c
index f497bc9969..77701e5cde 100644
--- a/libavfilter/avf_aphasemeter.c
+++ b/libavfilter/avf_aphasemeter.c
@@ -28,26 +28,41 @@
#include "libavutil/intreadwrite.h"
#include "libavutil/opt.h"
#include "libavutil/parseutils.h"
+#include "libavutil/timestamp.h"
#include "avfilter.h"
#include "formats.h"
#include "audio.h"
#include "video.h"
#include "internal.h"
+#include "stdbool.h"
+#include "float.h"
typedef struct AudioPhaseMeterContext {
const AVClass *class;
AVFrame *out;
int do_video;
+int do_phasing_detection;
int w, h;
AVRational frame_rate;
int contrast[4];
uint8_t *mpc_str;
uint8_t mpc[4];
int draw_median_phase;
+int is_mono;
+int is_out_phase;
+int start_mono_presence;
+int start_out_phase_presence;
+float tolerance;
+float angle;
+float phase;
+float mono_idx[2];
+float out_phase_idx[2];
+double duration;
} AudioPhaseMeterContext;
#define OFFSET(x) offsetof(AudioPhaseMeterContext, x)
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+#define get_duration(index) (index[1] - index[0])
static const AVOption aphasemeter_options[] = {
{ "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
@@ -59,6 +74,10 @@ static const AVOption aphasemeter_options[] = {
{ "bc", "set blue contrast", OFFSET(contrast[2]), AV_OPT_TYPE_INT, {.i64=1}, 0, 255, FLAGS },
{ "mpc", "set median phase color", OFFSET(mpc_str), AV_OPT_TYPE_STRING, {.str = "none"}, 0, 0, FLAGS },
{ "video", "set video output", OFFSET(do_video), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS },
+{ "phasing", "set mono and out-of-phase detection output", OFFSET(do_phasing_detection), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
+{ "tolerance", "set phase tolerance for mono detection", OFFSET(tolerance), AV_OPT_TYPE_FLOAT, {.dbl = 0.}, 0, 1, FLAGS },
+{ "angle", "set angle threshold for out-of-phase detection", OFFSET(angle), AV_OPT_TYPE_FLOAT, {.dbl = 170.}, 90, 180, FLAGS },
+{ "duration", "set minimum mono or out-of-phase duration in seconds", OFFSET(duration), AV_OPT_TYPE_DOUBLE, {.dbl=2.}, 0, 24*60*60, FLAGS },
{ NULL }
};
@@ -140,6 +159,22 @@ static inline int get_x(float phase, int w)
return (phase + 1.) / 2. * (w - 1);
}
+static inline float get_index(AVFilterLink *inlink, AVFrame *in)
+{
+char *index_str = av_ts2timestr(in->pts, &inlink->time_base);
+return atof(index_str);
+}
+
+static inline void add_metadata(AVFrame *insamples, const char *key, float value)
+{
+char buf[128];
+char str[128];
+
+snprintf(str, sizeof(str), "%f", value);
+snprintf(buf, sizeof(buf), "lavfi.aphasemeter.%s", key);
+av_dict_set(&insamples->metadata, buf, str, 0);
+}
+
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
{
AVFilterContext *ctx = inlink->dst;
@@ -154,6 +189,10 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
AVFrame *out;
uint8_t *dst;
int i;
+int mono_measurement;
+int out_phase_measurement;
+float tolerance = 1.0f - s->tolerance;
+float angle = cosf(s->angle/180.0f*M_PI);
if (s->do_video && (!s->out || s->out->width != outlink->w ||
s->out->height != outlink->h)) {
@@ -193,7 +232,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
fphase += phase;
}
fphase /= in->nb_samples;
-
+s->phase = fphase;
if (s->do_video) {
if (s->draw_median_phase) {
dst = out->data[0] + get_x(fphase, s->w) * 4;
@@ -206,10 +245,64 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
metadata = &in->metadata;
if (metadata) {
-uint8_t value[128];
+add_metadata(in, "phase", fphase);
+}
-snprintf(value, sizeof(value), "%f", fphase);
-av_dict_set(metadata, "lavfi.aphasemeter.phase", value, 0);
+if (s->do_phasing_detection) {
+mono_measurement = (tolerance - fphase) < FLT_EPSILON;
+out_phase_measurement = (angle - fphase) > FLT_EPSILON;
+if (!s->is_mono && mono_measurement) {
+s->is_mono = 1;
+s->start_mono_presence = 1;
+s->mono_idx[0] = get_index(inlink, in);
+}
+if (s->is_mono && mono_measuremen