[FFmpeg-devel] [PATCH] swr: move compensation_distance handling to swri_resample caller.

Ronald S. Bultje rsbultje at gmail.com
Tue May 27 14:39:12 CEST 2014


I think there's an off-by-one in terms of the switchpoint where we
switch from dst_incr to ideal_dst_incr, I don't think that's a massive
issue, but just be aware of that. It's probably trivial to prevent but
I don't care.
---
 libswresample/resample_template.c | 17 ++---------------
 libswresample/swresample.c        | 22 ++++++++++++++++++++--
 2 files changed, 22 insertions(+), 17 deletions(-)

diff --git a/libswresample/resample_template.c b/libswresample/resample_template.c
index 3fc8315..ac480d1 100644
--- a/libswresample/resample_template.c
+++ b/libswresample/resample_template.c
@@ -112,12 +112,11 @@ int RENAME(swri_resample)(ResampleContext *c, DELEM *dst, const DELEM *src, int
     int frac= c->frac;
     int dst_incr_frac= c->dst_incr % c->src_incr;
     int dst_incr=      c->dst_incr / c->src_incr;
-    int compensation_distance= c->compensation_distance;
 
     av_assert1(c->filter_shift == FILTER_SHIFT);
     av_assert1(c->felem_size == sizeof(FELEM));
 
-    if(compensation_distance == 0 && c->filter_length == 1 && c->phase_shift==0){
+    if (c->filter_length == 1 && c->phase_shift == 0) {
         int64_t index2= (1LL<<32)*c->frac/c->src_incr + (1LL<<32)*index;
         int64_t incr= (1LL<<32) * c->dst_incr / c->src_incr;
         int new_size = (src_size * (int64_t)c->src_incr - frac + c->dst_incr - 1) / c->dst_incr;
@@ -134,7 +133,7 @@ int RENAME(swri_resample)(ResampleContext *c, DELEM *dst, const DELEM *src, int
         av_assert2(index >= 0);
         *consumed= index;
         index = 0;
-    } else if (compensation_distance == 0 && index >= 0) {
+    } else if (index >= 0) {
         int64_t end_index = (1 + src_size - c->filter_length) << c->phase_shift;
         int64_t delta_frac = (end_index - index) * c->src_incr - c->frac;
         int delta_n = (delta_frac + c->dst_incr - 1) / c->dst_incr;
@@ -241,27 +240,15 @@ int RENAME(swri_resample)(ResampleContext *c, DELEM *dst, const DELEM *src, int
                 frac -= c->src_incr;
                 index++;
             }
-
-            if(dst_index + 1 == compensation_distance){
-                compensation_distance= 0;
-                dst_incr_frac= c->ideal_dst_incr % c->src_incr;
-                dst_incr=      c->ideal_dst_incr / c->src_incr;
-            }
         }
         *consumed= FFMAX(sample_index, 0);
         index += FFMIN(sample_index, 0) << c->phase_shift;
-
-        if(compensation_distance){
-            compensation_distance -= dst_index;
-            av_assert1(compensation_distance > 0);
-        }
     }
 
     if(update_ctx){
         c->frac= frac;
         c->index= index;
         c->dst_incr= dst_incr_frac + c->src_incr*dst_incr;
-        c->compensation_distance= compensation_distance;
     }
 
     return dst_index;
diff --git a/libswresample/swresample.c b/libswresample/swresample.c
index 7076650..add59aa 100644
--- a/libswresample/swresample.c
+++ b/libswresample/swresample.c
@@ -544,9 +544,18 @@ static int resample(SwrContext *s, AudioData *out_param, int out_count,
     do{
         int ret, size, consumed;
         if(!s->resample_in_constraint && s->in_buffer_count){
+            int out_count_2 = out_count;
+            if (s->resample->compensation_distance != 0)
+                out_count_2 = FFMAX(out_count_2, s->resample->compensation_distance);
             buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
-            ret= s->resampler->multiple_resample(s->resample, &out, out_count, &tmp, s->in_buffer_count, &consumed);
+            ret= s->resampler->multiple_resample(s->resample, &out, out_count_2, &tmp, s->in_buffer_count, &consumed);
             out_count -= ret;
+            if (s->resample->compensation_distance != 0) {
+                s->resample->compensation_distance -= ret;
+                if (!s->resample->compensation_distance)
+                    s->resample->dst_incr =
+                        s->resample->ideal_dst_incr / s->resample->src_incr;
+            }
             ret_sum += ret;
             buf_set(&out, &out, ret);
             s->in_buffer_count -= consumed;
@@ -564,9 +573,18 @@ static int resample(SwrContext *s, AudioData *out_param, int out_count,
         }
 
         if((s->flushed || in_count > padless) && !s->in_buffer_count){
+            int out_count_2 = out_count;
+            if (s->resample->compensation_distance != 0)
+                out_count_2 = FFMAX(out_count_2, s->resample->compensation_distance);
             s->in_buffer_index=0;
-            ret= s->resampler->multiple_resample(s->resample, &out, out_count, &in, FFMAX(in_count-padless, 0), &consumed);
+            ret= s->resampler->multiple_resample(s->resample, &out, out_count_2, &in, FFMAX(in_count-padless, 0), &consumed);
             out_count -= ret;
+            if (s->resample->compensation_distance != 0) {
+                s->resample->compensation_distance -= ret;
+                if (!s->resample->compensation_distance)
+                    s->resample->dst_incr =
+                        s->resample->ideal_dst_incr / s->resample->src_incr;
+            }
             ret_sum += ret;
             buf_set(&out, &out, ret);
             in_count -= consumed;
-- 
1.8.5.5



More information about the ffmpeg-devel mailing list