]> git.sesse.net Git - vlc/blobdiff - modules/audio_filter/resampler/bandlimited.c
Improvements to preferences
[vlc] / modules / audio_filter / resampler / bandlimited.c
index e700c0f5fbec658b7a623fffae38a964c79348f8..c31a3b8246fea484df8cd2d9f8bf0ded753a9952 100644 (file)
@@ -1,8 +1,8 @@
 /*****************************************************************************
- * bandlimited.c : bandlimited interpolation resampler
+ * bandlimited.c : band-limited interpolation resampler
  *****************************************************************************
  * Copyright (C) 2002 VideoLAN
- * $Id: bandlimited.c,v 1.2 2003/03/04 19:28:39 gbazin Exp $
+ * $Id$
  *
  * Authors: Gildas Bazin <gbazin@netcourrier.com>
  *
@@ -24,7 +24,7 @@
 /*****************************************************************************
  * Preamble:
  *
- * This implementation of the bandlimited interpolationis based on the
+ * This implementation of the band-limited interpolationis based on the
  * following paper:
  * http://ccrma-www.stanford.edu/~jos/resample/resample.html
  *
@@ -79,7 +79,9 @@ struct aout_filter_sys_t
  * Module descriptor
  *****************************************************************************/
 vlc_module_begin();
-    set_description( _("audio filter for bandlimited interpolation resampling") );
+    set_category( CAT_AUDIO );
+    set_subcategory( SUBCAT_AUDIO_MISC );
+    set_description( _("audio filter for band-limited interpolation resampling") );
     set_capability( "audio filter", 20 );
     set_callbacks( Create, Close );
 vlc_module_end();
@@ -104,6 +106,13 @@ static int Create( vlc_object_t *p_this )
         return VLC_EGENERIC;
     }
 
+#if !defined( SYS_DARWIN )
+    if( !config_GetInt( p_this, "hq-resampling" ) )
+    {
+        return VLC_EGENERIC;
+    }
+#endif
+
     /* Allocate the memory needed to store the module's structure */
     p_filter->p_sys = malloc( sizeof(struct aout_filter_sys_t) );
     if( p_filter->p_sys == NULL )
@@ -128,6 +137,7 @@ static int Create( vlc_object_t *p_this )
         return VLC_ENOMEM;
     }
 
+    p_filter->p_sys->i_old_wing = 0;
     p_filter->pf_do_work = DoWork;
 
     /* We don't want a new buffer to be created because we're not sure we'll
@@ -167,36 +177,34 @@ static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
     /* Check if we really need to run the resampler */
     if( p_aout->mixer.mixer.i_rate == p_filter->input.i_rate )
     {
-        if( p_filter->b_continuity &&
+        if( //p_filter->b_continuity && /* What difference does it make ? :) */
+            p_filter->p_sys->i_old_wing &&
             p_in_buf->i_size >=
-              p_in_buf->i_nb_bytes + sizeof(float) * i_nb_channels )
+              p_in_buf->i_nb_bytes + p_filter->p_sys->i_old_wing *
+              p_filter->input.i_bytes_per_frame )
         {
-            if( p_filter->p_sys->i_old_wing )
-            {
-                /* output the whole thing with the samples from last time */
-                memmove( ((float *)(p_in_buf->p_buffer)) +
-                         i_nb_channels * p_filter->p_sys->i_old_wing,
-                         p_in_buf->p_buffer, p_in_buf->i_nb_bytes );
-                memcpy( p_in_buf->p_buffer, p_filter->p_sys->p_buf +
-                        i_nb_channels * p_filter->p_sys->i_old_wing,
-                        i_nb_channels * p_filter->p_sys->i_old_wing *
-                        p_filter->input.i_bytes_per_frame );
-
-                p_out_buf->i_nb_samples = p_in_buf->i_nb_samples +
-                    p_filter->p_sys->i_old_wing;
-
-                aout_DateSet( &p_filter->p_sys->end_date,
-                              p_in_buf->start_date );
-
-                p_out_buf->end_date =
-                    aout_DateIncrement( &p_filter->p_sys->end_date,
-                                        p_out_buf->i_nb_samples );
-
-                p_out_buf->i_nb_bytes = p_out_buf->i_nb_samples *
-                    i_nb_channels * sizeof(int32_t);
-            }
+            /* output the whole thing with the samples from last time */
+            memmove( ((float *)(p_in_buf->p_buffer)) +
+                     i_nb_channels * p_filter->p_sys->i_old_wing,
+                     p_in_buf->p_buffer, p_in_buf->i_nb_bytes );
+            memcpy( p_in_buf->p_buffer, p_filter->p_sys->p_buf +
+                    i_nb_channels * p_filter->p_sys->i_old_wing,
+                    p_filter->p_sys->i_old_wing *
+                    p_filter->input.i_bytes_per_frame );
+
+            p_out_buf->i_nb_samples = p_in_buf->i_nb_samples +
+                p_filter->p_sys->i_old_wing;
+
+            p_out_buf->start_date = aout_DateGet( &p_filter->p_sys->end_date );
+            p_out_buf->end_date =
+                aout_DateIncrement( &p_filter->p_sys->end_date,
+                                    p_out_buf->i_nb_samples );
+
+            p_out_buf->i_nb_bytes = p_out_buf->i_nb_samples *
+                p_filter->input.i_bytes_per_frame;
         }
         p_filter->b_continuity = VLC_FALSE;
+        p_filter->p_sys->i_old_wing = 0;
         return;
     }
 
@@ -207,7 +215,7 @@ static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
         p_filter->b_continuity = VLC_TRUE;
         p_filter->p_sys->i_remainder = 0;
         aout_DateInit( &p_filter->p_sys->end_date, p_filter->output.i_rate );
-
+        aout_DateSet( &p_filter->p_sys->end_date, p_in_buf->start_date );
         p_filter->p_sys->i_old_rate   = p_filter->input.i_rate;
         p_filter->p_sys->d_old_factor = 1;
         p_filter->p_sys->i_old_wing   = 0;
@@ -258,15 +266,15 @@ static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
     d_scale_factor = SMALL_FILTER_SCALE * d_factor + 0.5;
 
     /* Apply the old rate until we have enough samples for the new one */
-    /* TODO: Check we have enough samples */
     i_in = p_filter->p_sys->i_old_wing;
     p_in += p_filter->p_sys->i_old_wing * i_nb_channels;
-    for( ; i_in < i_filter_wing; i_in++ )
+    for( ; i_in < i_filter_wing &&
+           (i_in + p_filter->p_sys->i_old_wing) < i_in_nb; i_in++ )
     {
         if( p_filter->p_sys->d_old_factor == 1 )
         {
             /* Just copy the samples */
-            memcpy( p_out_buf->p_buffer, p_in, 
+            memcpy( p_out, p_in, 
                     p_filter->input.i_bytes_per_frame );          
             p_in += i_nb_channels;
             p_out += i_nb_channels;
@@ -302,6 +310,16 @@ static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
                     *(p_out+i) *= d_old_scale_factor;
                 }
 #endif
+
+                /* Sanity check */
+                if( p_out_buf->i_size/p_filter->input.i_bytes_per_frame
+                    <= (unsigned int)i_out+1 )
+                {
+                    p_out += i_nb_channels;
+                    i_out++;
+                    p_filter->p_sys->i_remainder += p_filter->input.i_rate;
+                    break;
+                }
             }
             else
             {
@@ -331,7 +349,6 @@ static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
     }
 
     /* Apply the new rate for the rest of the samples */
-    /* TODO: Check we have enough future samples for the new rate */
     if( i_in < i_in_nb - i_filter_wing )
     {
         p_filter->p_sys->i_old_rate   = p_filter->input.i_rate;
@@ -369,6 +386,15 @@ static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
                     *(p_out+i) *= d_old_scale_factor;
                 }
 #endif
+                /* Sanity check */
+                if( p_out_buf->i_size/p_filter->input.i_bytes_per_frame
+                    <= (unsigned int)i_out+1 )
+                {
+                    p_out += i_nb_channels;
+                    i_out++;
+                    p_filter->p_sys->i_remainder += p_filter->input.i_rate;
+                    break;
+                }
             }
             else
             {
@@ -418,14 +444,7 @@ static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
 
     /* Finalize aout buffer */
     p_out_buf->i_nb_samples = i_out;
-    p_out_buf->start_date = p_in_buf->start_date;
-
-    if( p_in_buf->start_date !=
-        aout_DateGet( &p_filter->p_sys->end_date ) )
-    {
-        aout_DateSet( &p_filter->p_sys->end_date, p_in_buf->start_date );
-    }
-
+    p_out_buf->start_date = aout_DateGet( &p_filter->p_sys->end_date );
     p_out_buf->end_date = aout_DateIncrement( &p_filter->p_sys->end_date,
                                               p_out_buf->i_nb_samples );