diff libaf/af.c @ 24888:b2402b4f0afa

libaf: change filter input/output ratio calculations Change the audio filters to use a double instead of rationals for the ratio of output to input size. The rationals could overflow when calculating the overall ratio of a filter chain and gave no real advantage compared to doubles.
author uau
date Thu, 01 Nov 2007 06:52:01 +0000
parents 484b8eaaf28f
children 4055d43fc406
line wrap: on
line diff
--- a/libaf/af.c	Thu Nov 01 06:51:57 2007 +0000
+++ b/libaf/af.c	Thu Nov 01 06:52:01 2007 +0000
@@ -516,12 +516,14 @@
   return data;
 }
 
-/* Helper function used to calculate the exact buffer length needed
-   when buffers are resized. The returned length is >= than what is
-   needed */
-inline int af_lencalc(frac_t mul, af_data_t* d){
-  register int t = d->bps*d->nch;
-  return t*(((d->len/t)*mul.n)/mul.d + 1);
+/* Calculate the minimum output buffer size for given input data d
+ * when using the RESIZE_LOCAL_BUFFER macro. The +t+1 part ensures the
+ * value is >= len*mul rounded upwards to whole samples even if the
+ * double 'mul' is inexact. */
+int af_lencalc(double mul, af_data_t* d)
+{
+  int t = d->bps * d->nch;
+  return d->len * mul + t + 1;
 }
 
 /* Calculate how long the input IN to the filters should be to produce
@@ -537,34 +539,21 @@
 {
   int t   = s->input.bps*s->input.nch;
   int in  = 0;
-  int out = 0;
   af_instance_t* af=s->first; 
-  frac_t mul = {1,1};
+  double mul = 1;
   // Iterate through all filters and calculate total multiplication factor
   do{
-    af_frac_mul(&mul, &af->mul);
-    af=af->next;
+      mul *= af->mul;
+      af=af->next;
   }while(af);
-  // Sanity check 
-  if(!mul.n || !mul.d) 
-    return -1;
 
-  in = t * (((len/t) * mul.d - 1)/mul.n);
-  
+  if (len > max_outsize)
+      len = max_outsize;
+
+  in = len / t / mul * t;
+
   if(in>max_insize) in=t*(max_insize/t);
 
-  // Try to meet constraint nr 3. 
-  while((out=t * (((in/t+1)*mul.n - 1)/mul.d)) <= max_outsize && in<=max_insize){
-    if( (t * (((in/t)*mul.n))/mul.d) >= len) return in;
-    in+=t;
-  }
-  
-  // Could no meet constraint nr 3.
-  while(out > max_outsize || in > max_insize){
-    in-=t;
-    if(in<t) return -1; // Input parameters are probably incorrect
-    out = t * (((in/t)*mul.n + 1)/mul.d);
-  }
   return in;
 }