Re: [FFmpeg-devel] [PATCH v8] avfilter/avf_aphasemeter: Add out of phase and mono detection

2020-02-27 Thread Romane Lafon
Le mar. 24 déc. 2019 à 17:36, Romane Lafon  a écrit :

> I rewrote the patch I submitted a few months ago.
> This patch extends aphasemeter to detect out of phase or mono sequences in
> stereo streams.
>
> Regards,
> Romane
>


Ping for review.

Thanks,
Romane
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v8] avfilter/avf_aphasemeter: Add out of phase and mono detection

2019-12-24 Thread Romane Lafon
I rewrote the patch I submitted a few months ago.
This patch extends aphasemeter to detect out of phase or mono sequences in
stereo streams.

Regards,
Romane
From a44e9caf6c654030884c03c9e802975e72f1f2dd Mon Sep 17 00:00:00 2001
From: Romane Lafon 
Date: Tue, 24 Dec 2019 17:06:58 +0100
Subject: [PATCH] avfilter/avf_aphasemeter: Add out of phase and mono detection

Signed-off-by: Romane Lafon 
---
 doc/filters.texi  |  33 ++
 libavfilter/avf_aphasemeter.c | 118 +-
 2 files changed, 150 insertions(+), 1 deletion(-)

diff --git a/doc/filters.texi b/doc/filters.texi
index 8c5d3a5760..b6fb5bed85 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -22033,6 +22033,39 @@ Set color which will be used for drawing median phase. If color is
 Enable video output. Default is enabled.
 @end table
 
+@subsection phasing detection
+
+The filter also detects out of phase and mono sequences in stereo streams.
+It logs the sequence start, end and duration when it lasts longer or as long as the minimum set.
+
+The filter accepts the following options for this detection:
+
+@table @option
+@item phasing
+Enable mono and out of phase detection. Default is disabled.
+
+@item tolerance, t
+Set phase tolerance for mono detection, in amplitude ratio. Default is @code{0}.
+Allowed range is @code{[0, 1]}.
+
+@item angle, a
+Set angle threshold for out of phase detection, in degree. Default is @code{170}.
+Allowed range is @code{[90, 180]}.
+
+@item duration, d
+Set mono or out of phase duration until notification, expressed in seconds. Default is @code{2}.
+@end table
+
+@subsection Examples
+
+@itemize
+@item
+Complete example with @command{ffmpeg} to detect 1 second of mono with 0.001 phase tolerance:
+@example
+ffmpeg -i stereo.wav -af aphasemeter=video=0:phasing=1:duration=1:tolerance=0.001 -f null -
+@end example
+@end itemize
+
 @section avectorscope
 
 Convert input audio to a video output, representing the audio vector
diff --git a/libavfilter/avf_aphasemeter.c b/libavfilter/avf_aphasemeter.c
index f497bc9969..bdf0a7e8c1 100644
--- a/libavfilter/avf_aphasemeter.c
+++ b/libavfilter/avf_aphasemeter.c
@@ -28,26 +28,43 @@
 #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 "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;
+AVRational time_base;
+int64_t duration;
+int64_t frame_end;
+int64_t mono_idx[2];
+int64_t out_phase_idx[2];
 } AudioPhaseMeterContext;
 
+#define MAX_DURATION (24*60*60*100LL)
 #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 +76,13 @@ 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 },
+{ "t", "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 },
+{ "a", "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_DURATION, {.i64=200}, 0, MAX_DURATION, FLAGS },
+{ "d","set minimum mono or out-of-phase duration in seconds", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=200}, 0, MAX_DURATION, FLAGS },
 { NULL }
 };
 
@@ -104,6 +128,7 @@ static int config_input(AVFilterLink *inlink)
 AVFilterContext *ctx = inlink->dst;
 AudioPhaseMeterContext *s = ctx->priv;
 int nb_samples;
+s->duration = av_resc