]> git.sesse.net Git - vlc/commitdiff
visual: save a huge number of allocation/deallocation.
authorRémi Duraffort <ivoire@videolan.org>
Thu, 19 Nov 2009 09:04:42 +0000 (10:04 +0100)
committerRémi Duraffort <ivoire@videolan.org>
Thu, 19 Nov 2009 09:04:42 +0000 (10:04 +0100)
modules/visualization/visual/effects.c
modules/visualization/visual/visual.c
modules/visualization/visual/visual.h

index f2043e6fb67047570248ee4041296777e5b98d46..23c9e47804e1c4898389c9c65582df6d4e91b5cb 100644 (file)
@@ -100,25 +100,7 @@ int spectrum_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;
-
-    p_buffs = p_s16_buff;
-    i_80_bands = config_GetInt ( p_aout, "visual-80-bands" );
-    i_peak     = config_GetInt ( p_aout, "visual-peaks" );
-
-    if( i_80_bands != 0)
-    {
-        xscale = xscale2;
-        i_nb_bands = 80;
-    }
-    else
-    {
-        xscale = xscale1;
-        i_nb_bands = 20;
-    }
-
+    /* Create p_data if needed */
     if( !p_data )
     {
         p_effect->p_data = p_data = malloc( sizeof( spectrum_data ) );
@@ -130,15 +112,43 @@ int spectrum_Run(visual_effect_t * p_effect, vlc_object_t *p_aout,
 
         p_data->peaks = calloc( 80, sizeof(int) );
         p_data->prev_heights = calloc( 80, sizeof(int) );
+
+        p_data->i_prev_nb_samples = 0;
+        p_data->p_prev_s16_buff = NULL;
     }
     peaks = (int *)p_data->peaks;
     prev_heights = (int *)p_data->prev_heights;
 
+    /* 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;
+
+    i_80_bands = config_GetInt ( p_aout, "visual-80-bands" );
+    i_peak     = config_GetInt ( p_aout, "visual-peaks" );
+
+    if( i_80_bands != 0)
+    {
+        xscale = xscale2;
+        i_nb_bands = 80;
+    }
+    else
+    {
+        xscale = xscale1;
+        i_nb_bands = 20;
+    }
 
     height = malloc( i_nb_bands * sizeof(int) );
     if( !height )
     {
-        free( p_s16_buff );
         return -1;
     }
     /* Convert the buffer to int16_t  */
@@ -157,7 +167,6 @@ int spectrum_Run(visual_effect_t * p_effect, vlc_object_t *p_aout,
     if( !p_state)
     {
         free( height );
-        free( p_s16_buff );
         msg_Err(p_aout,"unable to initialize FFT transform");
         return -1;
     }
@@ -321,7 +330,6 @@ int spectrum_Run(visual_effect_t * p_effect, vlc_object_t *p_aout,
 
     fft_close( p_state );
 
-    free( p_s16_buff );
     free( height );
 
     return 0;
index 2b8ca4d9f7a8286c8e131c65cb5c3f6e85467ab4..212c4de66fdeeb2baec89ac9b9b7a63772e9862b 100644 (file)
@@ -397,6 +397,7 @@ static void Close( vlc_object_t *p_this )
         {
             free( ( ( spectrum_data * )p_effect->p_data )->peaks );
             free( ( ( spectrum_data * )p_effect->p_data )->prev_heights );
+            free( ( ( spectrum_data * )p_effect->p_data )->p_prev_s16_buff );
         }
         free( p_effect->p_data );
         free( p_effect->psz_args );
index aadb7a84ed2544fad7a2287a33bfb8ad41ea1783..f811385876478a62bf99d762872dc11447362470 100644 (file)
@@ -42,6 +42,9 @@ typedef struct spectrum_data
 {
     int *peaks;
     int *prev_heights;
+
+    unsigned i_prev_nb_samples;
+    int16_t *p_prev_s16_buff;
 } spectrum_data;