]> git.sesse.net Git - vlc/commitdiff
visual: save again some allocation/deallocation cycles.
authorRémi Duraffort <ivoire@videolan.org>
Fri, 20 Nov 2009 09:04:03 +0000 (10:04 +0100)
committerRémi Duraffort <ivoire@videolan.org>
Fri, 20 Nov 2009 09:04:03 +0000 (10:04 +0100)
modules/visualization/visual/effects.c
modules/visualization/visual/visual.c
modules/visualization/visual/visual.h

index 473cd4148d6f11e2abd57aa0012959b60ac11591..7a6440f6396c2fd5758b19d097c724ef233a1383 100644 (file)
@@ -396,11 +396,38 @@ int spectrometer_Run(visual_effect_t * p_effect, vlc_object_t *p_aout,
     int16_t  *p_buffs;                    /* int16_t converted buffer */
     int16_t  *p_s16_buff;                /* int16_t converted buffer */
 
-    p_s16_buff = malloc( p_buffer->i_nb_samples * p_effect->i_nb_chans * sizeof(int16_t) );
-    if( !p_s16_buff )
-        return -1;
+    /* Create the data struct if needed */
+    spectrometer_data *p_data = p_effect->p_data;
+    if( !p_data )
+    {
+        p_data = malloc( sizeof(spectrometer_data) );
+        if( !p_data )
+            return -1;
+        p_data->peaks = calloc( 80, sizeof(int) );
+        if( !p_data->peaks )
+        {
+            free( p_data );
+            return -1;
+        }
+        p_data->i_prev_nb_samples = 0;
+        p_data->p_prev_s16_buff = NULL;
+        p_effect->p_data = (void*)p_data;
+    }
+    peaks = p_data->peaks;
+
+    /* Allocate the buffer only if the number of samples change */
+    if( p_buffer->i_nb_samples != p_data->i_prev_nb_samples )
+    {
+        free( p_data->p_prev_s16_buff );
+        p_data->p_prev_s16_buff = malloc( p_buffer->i_nb_samples *
+                                          p_effect->i_nb_chans *
+                                          sizeof(int16_t));
+        p_data->i_prev_nb_samples = p_buffer->i_nb_samples;
+        if( !p_data->p_prev_s16_buff )
+            return -1;
+    }
+    p_buffs = p_s16_buff = p_data->p_prev_s16_buff;
 
-    p_buffs = p_s16_buff;
     i_original     = config_GetInt ( p_aout, "spect-show-original" );
     i_80_bands     = config_GetInt ( p_aout, "spect-80-bands" );
     i_separ        = config_GetInt ( p_aout, "spect-separ" );
@@ -425,23 +452,9 @@ int spectrometer_Run(visual_effect_t * p_effect, vlc_object_t *p_aout,
         i_nb_bands = 20;
     }
 
-    if( !p_effect->p_data )
-    {
-        p_effect->p_data = calloc( 80, sizeof(int) );
-        if( !p_effect->p_data )
-        {
-            free( p_s16_buff );
-            return -1;
-        }
-    }
-    peaks =(int *)p_effect->p_data;
-
     height = malloc( i_nb_bands * sizeof(int) );
     if( !height)
-    {
-        free( p_s16_buff );
         return -1;
-    }
 
     /* Convert the buffer to int16_t  */
     /* Pasted from float32tos16.c */
@@ -460,7 +473,6 @@ int spectrometer_Run(visual_effect_t * p_effect, vlc_object_t *p_aout,
     {
         msg_Err(p_aout,"unable to initialize FFT transform");
         free( height );
-        free( p_s16_buff );
         return -1;
     }
     p_buffs = p_s16_buff;
@@ -772,7 +784,6 @@ int spectrometer_Run(visual_effect_t * p_effect, vlc_object_t *p_aout,
 
     fft_close( p_state );
 
-    free( p_s16_buff );
     free( height );
 
     return 0;
index 212c4de66fdeeb2baec89ac9b9b7a63772e9862b..ee69dee2349d3eb91fd3620e19e52e673852ac42 100644 (file)
@@ -399,6 +399,11 @@ static void Close( vlc_object_t *p_this )
             free( ( ( spectrum_data * )p_effect->p_data )->prev_heights );
             free( ( ( spectrum_data * )p_effect->p_data )->p_prev_s16_buff );
         }
+        if( !strncmp( p_effect->psz_name, "spectrometer", strlen( "spectrometer" ) ) )
+        {
+            free( ((spectrometer_data*)p_effect->p_data)->peaks );
+            free( ((spectrometer_data*)p_effect->p_data)->p_prev_s16_buff );
+        }
         free( p_effect->p_data );
         free( p_effect->psz_args );
         free( p_effect );
index f811385876478a62bf99d762872dc11447362470..d1b20c42bfcb32365d4fa00438306f98cbb6ce88 100644 (file)
@@ -47,6 +47,13 @@ typedef struct spectrum_data
     int16_t *p_prev_s16_buff;
 } spectrum_data;
 
+typedef struct
+{
+    int *peaks;
+
+    unsigned i_prev_nb_samples;
+    int16_t *p_prev_s16_buff;
+} spectrometer_data;
 
 /*****************************************************************************
  * aout_filter_sys_t: visualizer audio filter method descriptor