From 4def5d2b64d78a6f15c6c8869c28ece1f823626e Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <michaelni@gmx.at>
Date: Fri, 18 May 2012 23:02:10 +0200
Subject: [PATCH] swr: add swr_get_delay() to find the exact delay the
 swresampler introduces.

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
---
 libswresample/resample.c   | 15 +++++++++++++++
 libswresample/swresample.h | 20 +++++++++++++++++++-
 2 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/libswresample/resample.c b/libswresample/resample.c
index 868f3eb949b..558401c4595 100644
--- a/libswresample/resample.c
+++ b/libswresample/resample.c
@@ -367,3 +367,18 @@ int swri_multiple_resample(ResampleContext *c, AudioData *dst, int dst_size, Aud
 
     return ret;
 }
+
+int64_t swr_get_delay(struct SwrContext *s, int64_t base){
+    ResampleContext *c = s->resample;
+    if(c){
+        int64_t num = s->in_buffer_count - (c->filter_length-1)/2;
+        num <<= c->phase_shift;
+        num -= c->index;
+        num *= c->src_incr;
+        num -= c->frac;
+
+        return av_rescale(num, base, s->in_sample_rate*(int64_t)c->src_incr << c->phase_shift);
+    }else{
+        return (s->in_buffer_count*base + (s->in_sample_rate>>1))/ s->in_sample_rate;
+    }
+}
diff --git a/libswresample/swresample.h b/libswresample/swresample.h
index 137517d6d29..43112f9c17b 100644
--- a/libswresample/swresample.h
+++ b/libswresample/swresample.h
@@ -30,7 +30,7 @@
 #include "libavutil/samplefmt.h"
 
 #define LIBSWRESAMPLE_VERSION_MAJOR 0
-#define LIBSWRESAMPLE_VERSION_MINOR 11
+#define LIBSWRESAMPLE_VERSION_MINOR 12
 #define LIBSWRESAMPLE_VERSION_MICRO 100
 
 #define LIBSWRESAMPLE_VERSION_INT  AV_VERSION_INT(LIBSWRESAMPLE_VERSION_MAJOR, \
@@ -158,6 +158,24 @@ int swr_set_channel_mapping(struct SwrContext *s, const int *channel_map);
  */
 int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride);
 
+/**
+ * Gets the delay the next input sample will experience relative to the next output sample.
+ *
+ * Swresample can buffer data if more input has been provided than available
+ * output space, also converting between sample rates needs a delay.
+ * This function returns the sum of all such delays.
+ *
+ * @param s     swr context
+ * @param base  timebase in which the returned delay will be
+ *              if its set to 1 the returned delay is in seconds
+ *              if its set to 1000 the returned delay is in milli seconds
+ *              if its set to the input sample rate then the returned delay is in input samples
+ *              if its set to the output sample rate then the returned delay is in output samples
+ *              an exact rounding free delay can be found by using LCM(in_sample_rate, out_sample_rate)
+ * @returns     the delay in 1/base units.
+ */
+int64_t swr_get_delay(struct SwrContext *s, int64_t base);
+
 /**
  * Return the LIBSWRESAMPLE_VERSION_INT constant.
  */
-- 
GitLab