various resampling fixes
[ffmpeg.git] / libavcodec / resample2.c
index 7ea623e..47eeb0a 100644 (file)
@@ -93,7 +93,7 @@ void av_build_filter(int16_t *filter, double factor, int tap_count, int phase_co
                 break;
             case 2:
                 w = 2.0*x / (factor*tap_count*M_PI);
-                y *= bessel(16*sqrt(FFMAX(1-w*w, 0))) / bessel(16);
+                y *= bessel(16*sqrt(FFMAX(1-w*w, 0)));
                 break;
             }
 
@@ -123,8 +123,8 @@ AVResampleContext *av_resample_init(int out_rate, int in_rate){
     c->filter_length= ceil(16.0/factor);
     c->filter_bank= av_mallocz(c->filter_length*(PHASE_COUNT+1)*sizeof(short));
     av_build_filter(c->filter_bank, factor, c->filter_length, PHASE_COUNT, 1<<FILTER_SHIFT, 1);
-    c->filter_bank[c->filter_length*PHASE_COUNT + (c->filter_length-1) + 1]= (1<<FILTER_SHIFT)-1;
-    c->filter_bank[c->filter_length*PHASE_COUNT + (c->filter_length-1) + 2]= 1;
+    c->filter_bank[c->filter_length*PHASE_COUNT + (c->filter_length-1)/2 + 1]= (1<<FILTER_SHIFT)-1;
+    c->filter_bank[c->filter_length*PHASE_COUNT + (c->filter_length-1)/2 + 2]= 1;
 
     c->src_incr= out_rate;
     c->ideal_dst_incr= c->dst_incr= in_rate * PHASE_COUNT;
@@ -139,10 +139,9 @@ void av_resample_close(AVResampleContext *c){
 }
 
 void av_resample_compensate(AVResampleContext *c, int sample_delta, int compensation_distance){
-    assert(!c->compensation_distance); //FIXME
-
+//    sample_delta += (c->ideal_dst_incr - c->dst_incr)*(int64_t)c->compensation_distance / c->ideal_dst_incr;
     c->compensation_distance= compensation_distance;
-    c->dst_incr-= c->ideal_dst_incr * sample_delta / compensation_distance;
+    c->dst_incr = c->ideal_dst_incr - c->ideal_dst_incr * (int64_t)sample_delta / compensation_distance;
 }
 
 /**
@@ -171,7 +170,7 @@ int av_resample(AVResampleContext *c, short *dst, short *src, int *consumed, int
         
         if(sample_index < 0){
             for(i=0; i<c->filter_length; i++)
-                val += src[ABS(sample_index + i)] * filter[i];
+                val += src[ABS(sample_index + i) % src_size] * filter[i];
         }else if(sample_index + c->filter_length > src_size){
             break;
         }else{
@@ -200,15 +199,25 @@ int av_resample(AVResampleContext *c, short *dst, short *src, int *consumed, int
             index++;
         }
     }
+    *consumed= FFMAX(index, 0) >> PHASE_SHIFT;
+    index= FFMIN(index, 0);
+
     if(update_ctx){
         if(c->compensation_distance){
-            c->compensation_distance -= index;
+            c->compensation_distance -= dst_index;
             if(!c->compensation_distance)
                 c->dst_incr= c->ideal_dst_incr;
         }
         c->frac= frac;
-        c->index=0;
+        c->index= index;
+    }
+#if 0    
+    if(update_ctx && !c->compensation_distance){
+#undef rand
+        av_resample_compensate(c, rand() % (8000*2) - 8000, 8000*2);
+av_log(NULL, AV_LOG_DEBUG, "%d %d %d\n", c->dst_incr, c->ideal_dst_incr, c->compensation_distance);
     }
-    *consumed= index >> PHASE_SHIFT;
+#endif
+    
     return dst_index;
 }