]> git.sesse.net Git - vlc/blobdiff - modules/audio_filter/scaletempo.c
Make aout_buffer_t an alias for block_t
[vlc] / modules / audio_filter / scaletempo.c
index 9f7f6018779cbf47b6d286824f3e23db3f0e6e0c..5eb18505b1e8f65202377f2b32801445a96db401 100644 (file)
@@ -43,22 +43,22 @@ static void Close( vlc_object_t * );
 static void DoWork( aout_instance_t *, aout_filter_t *,
                     aout_buffer_t *, aout_buffer_t * );
 
-vlc_module_begin();
-    set_description( N_("Scale audio tempo in sync with playback rate") );
-    set_shortname( N_("Scaletempo") );
-    set_capability( "audio filter", 0 );
-    set_category( CAT_AUDIO );
-    set_subcategory( SUBCAT_AUDIO_AFILTER );
+vlc_module_begin ()
+    set_description( N_("Audio tempo scaler synched with rate") )
+    set_shortname( N_("Scaletempo") )
+    set_capability( "audio filter", 0 )
+    set_category( CAT_AUDIO )
+    set_subcategory( SUBCAT_AUDIO_AFILTER )
 
     add_integer_with_range( "scaletempo-stride", 30, 1, 2000, NULL,
-        N_("Stride Length"), N_("Length in milliseconds to output each stride"), true );
+        N_("Stride Length"), N_("Length in milliseconds to output each stride"), true )
     add_float_with_range( "scaletempo-overlap", .20, 0.0, 1.0, NULL,
-        N_("Overlap Length"), N_("Percentage of stride to overlap"), true );
+        N_("Overlap Length"), N_("Percentage of stride to overlap"), true )
     add_integer_with_range( "scaletempo-search", 14, 0, 200, NULL,
-        N_("Search Length"), N_("Length in milliseconds to search for best overlap position"), true );
+        N_("Search Length"), N_("Length in milliseconds to search for best overlap position"), true )
 
-    set_callbacks( Open, Close );
-vlc_module_end();
+    set_callbacks( Open, Close )
+vlc_module_end ()
 
 /*
  * Scaletempo works by producing audio in constant sized chunks (a "stride") but
@@ -76,7 +76,7 @@ vlc_module_end();
  * frame: a single set of samples, one for each channel
  * VLC uses these terms differently
  */
-typedef struct aout_filter_sys_t
+struct aout_filter_sys_t
 {
     /* Filter static config */
     double    scale;
@@ -114,7 +114,7 @@ typedef struct aout_filter_sys_t
     /* for "audio filter" only, manage own buffers */
     int       i_buf;
     uint8_t  *p_buffers[2];
-} aout_filter_sys_t;
+};
 
 /*****************************************************************************
  * best_overlap_offset: calculate best offset for overlap
@@ -288,12 +288,15 @@ static int reinit_buffers( aout_filter_t *p_filter )
 
     /* overlap */
     unsigned frames_overlap = frames_stride * p->percent_overlap;
-    if( frames_overlap < 1 ) { /* if no overlap */
+    if( frames_overlap < 1 )
+    { /* if no overlap */
         p->bytes_overlap    = 0;
         p->bytes_standing   = p->bytes_stride;
         p->samples_standing = p->bytes_standing / p->bytes_per_sample;
         p->output_overlap   = NULL;
-    } else {
+    }
+    else
+    {
         unsigned prev_overlap   = p->bytes_overlap;
         p->bytes_overlap    = frames_overlap * p->bytes_per_frame;
         p->samples_overlap  = frames_overlap * p->samples_per_frame;
@@ -301,50 +304,55 @@ static int reinit_buffers( aout_filter_t *p_filter )
         p->samples_standing = p->bytes_standing / p->bytes_per_sample;
         p->buf_overlap      = malloc( p->bytes_overlap );
         p->table_blend      = malloc( p->samples_overlap * 4 ); /* sizeof (int32|float) */
-        if( ! p->buf_overlap || ! p->table_blend ) {
+        if( !p->buf_overlap || !p->table_blend )
             return VLC_ENOMEM;
-        }
-        if( p->bytes_overlap > prev_overlap ) {
+        if( p->bytes_overlap > prev_overlap )
             memset( (uint8_t *)p->buf_overlap + prev_overlap, 0, p->bytes_overlap - prev_overlap );
-        }
+
         float *pb = p->table_blend;
         float t = (float)frames_overlap;
-        for( i = 0; i<frames_overlap; i++ ) {
+        for( i = 0; i<frames_overlap; i++ )
+        {
             float v = i / t;
-            for( j = 0; j < p->samples_per_frame; j++ ) {
+            for( j = 0; j < p->samples_per_frame; j++ )
                 *pb++ = v;
-            }
         }
         p->output_overlap = output_overlap_float;
     }
 
     /* best overlap */
     p->frames_search = ( frames_overlap <= 1 ) ? 0 : p->ms_search * p->sample_rate / 1000.0;
-    if( p->frames_search < 1 ) { /* if no search */
+    if( p->frames_search < 1 )
+    { /* if no search */
         p->best_overlap_offset = NULL;
-    } else {
+    }
+    else
+    {
         unsigned bytes_pre_corr = ( p->samples_overlap - p->samples_per_frame ) * 4; /* sizeof (int32|float) */
         p->buf_pre_corr = malloc( bytes_pre_corr );
         p->table_window = malloc( bytes_pre_corr );
-        if( ! p->buf_pre_corr || ! p->table_window ) {
+        if( ! p->buf_pre_corr || ! p->table_window )
             return VLC_ENOMEM;
-        }
         float *pw = p->table_window;
-        for( i = 1; i<frames_overlap; i++ ) {
+        for( i = 1; i<frames_overlap; i++ )
+        {
             float v = i * ( frames_overlap - i );
-            for( j = 0; j < p->samples_per_frame; j++ ) {
+            for( j = 0; j < p->samples_per_frame; j++ )
                 *pw++ = v;
-            }
         }
         p->best_overlap_offset = best_overlap_offset_float;
     }
 
     unsigned new_size = ( p->frames_search + frames_stride + frames_overlap ) * p->bytes_per_frame;
-    if( p->bytes_queued > new_size ) {
-        if( p->bytes_to_slide > p->bytes_queued ) {
+    if( p->bytes_queued > new_size )
+    {
+        if( p->bytes_to_slide > p->bytes_queued )
+        {
           p->bytes_to_slide -= p->bytes_queued;
           p->bytes_queued    = 0;
-        } else {
+        }
+        else
+        {
             unsigned new_queued = __MIN( p->bytes_queued - p->bytes_to_slide, new_size );
             memmove( p->buf_queue,
                      p->buf_queue + p->bytes_queued - new_queued,
@@ -355,9 +363,8 @@ static int reinit_buffers( aout_filter_t *p_filter )
     }
     p->bytes_queue_max = new_size;
     p->buf_queue = malloc( p->bytes_queue_max );
-    if( ! p->buf_queue ) {
+    if( ! p->buf_queue )
         return VLC_ENOMEM;
-    }
 
     p->bytes_stride_scaled  = p->bytes_stride * p->scale;
     p->frames_stride_scaled = p->bytes_stride_scaled / p->bytes_per_frame;
@@ -385,11 +392,11 @@ static int Open( vlc_object_t *p_this )
     aout_filter_sys_t *p_sys;
     bool b_fit = true;
 
-    if( p_filter->input.i_format != VLC_FOURCC('f','l','3','2' ) ||
-        p_filter->output.i_format != VLC_FOURCC('f','l','3','2') )
+    if( p_filter->input.i_format != VLC_CODEC_FL32 ||
+        p_filter->output.i_format != VLC_CODEC_FL32 )
     {
         b_fit = false;
-        p_filter->input.i_format = p_filter->output.i_format = VLC_FOURCC('f','l','3','2');
+        p_filter->input.i_format = p_filter->output.i_format = VLC_CODEC_FL32;
         msg_Warn( p_filter, "bad input or output format" );
     }
     if( ! AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
@@ -400,9 +407,7 @@ static int Open( vlc_object_t *p_this )
     }
 
     if( ! b_fit )
-    {
         return VLC_EGENERIC;
-    }
 
     p_filter->pf_do_work = DoWork;
     p_filter->b_in_place = false;
@@ -410,9 +415,7 @@ static int Open( vlc_object_t *p_this )
     /* Allocate structure */
     p_sys = p_filter->p_sys = malloc( sizeof(aout_filter_sys_t) );
     if( ! p_sys )
-    {
         return VLC_ENOMEM;
-    }
 
     p_sys->scale             = 1.0;
     p_sys->sample_rate       = p_filter->input.i_rate;
@@ -446,7 +449,13 @@ static int Open( vlc_object_t *p_this )
     p_sys->bytes_queued   = 0;
     p_sys->bytes_to_slide = 0;
     p_sys->frames_stride_error = 0;
-    return reinit_buffers( p_filter );
+
+    if( reinit_buffers( p_filter ) != VLC_SUCCESS )
+    {
+        Close( p_this );
+        return VLC_EGENERIC;
+    }
+    return VLC_SUCCESS;
 }
 
 static void Close( vlc_object_t *p_this )
@@ -473,8 +482,8 @@ static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
     aout_filter_sys_t *p = p_filter->p_sys;
 
     if( p_filter->input.i_rate == p->sample_rate ) {
-      memcpy( p_out_buf->p_buffer, p_in_buf->p_buffer, p_in_buf->i_nb_bytes );
-      p_out_buf->i_nb_bytes   = p_in_buf->i_nb_bytes;
+      memcpy( p_out_buf->p_buffer, p_in_buf->p_buffer, p_in_buf->i_buffer );
+      p_out_buf->i_buffer   = p_in_buf->i_buffer;
       p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
       return;
     }
@@ -491,8 +500,14 @@ static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
                (int)( p->bytes_stride / p->bytes_per_frame ) );
     }
 
-    size_t i_outsize = calculate_output_buffer_size ( p_filter, p_in_buf->i_nb_bytes );
-    if( i_outsize > p_out_buf->i_size ) {
+    size_t i_outsize = calculate_output_buffer_size ( p_filter, p_in_buf->i_buffer );
+    if( i_outsize > p_out_buf->i_buffer ) {
+#if 0   /* FIXME: This requires audio filter2 to work */
+        p_out_buf = block_Realloc( p_out_buf, i_outsize, 0 );
+        if( p_out_buf == NULL )
+            abort();
+#else   /* This fails horribly if we have more than two buffers in the
+         * pipeline, or if the buffer is passed to another thread... XXX */
         void *temp = realloc( p->p_buffers[ p->i_buf ], i_outsize );
         if( temp == NULL )
         {
@@ -501,12 +516,13 @@ static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
         p->p_buffers[ p->i_buf ] = temp;
         p_out_buf->p_buffer = p->p_buffers[ p->i_buf ];
         p->i_buf = ! p->i_buf;
+#endif
     }
 
     size_t bytes_out = transform_buffer( p_filter,
-        p_in_buf->p_buffer, p_in_buf->i_nb_bytes,
+        p_in_buf->p_buffer, p_in_buf->i_buffer,
         p_out_buf->p_buffer );
 
-    p_out_buf->i_nb_bytes   = bytes_out;
+    p_out_buf->i_buffer   = bytes_out;
     p_out_buf->i_nb_samples = bytes_out / p->bytes_per_frame;
 }