]> git.sesse.net Git - vlc/blob - modules/visualization/visual/visual.c
Another check for malloc.
[vlc] / modules / visualization / visual / visual.c
1 /*****************************************************************************
2  * visual.c : Visualisation system
3  *****************************************************************************
4  * Copyright (C) 2002-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc/vlc.h>
32 #include <vlc_vout.h>
33 #include <vlc_aout.h>
34
35 #include "visual.h"
36
37 /*****************************************************************************
38  * Module descriptor
39  *****************************************************************************/
40 #define ELIST_TEXT N_( "Effects list" )
41 #define ELIST_LONGTEXT N_( \
42       "A list of visual effect, separated by commas.\n"  \
43       "Current effects include: dummy, scope, spectrum." )
44
45 #define WIDTH_TEXT N_( "Video width" )
46 #define WIDTH_LONGTEXT N_( \
47       "The width of the effects video window, in pixels." )
48
49 #define HEIGHT_TEXT N_( "Video height" )
50 #define HEIGHT_LONGTEXT N_( \
51       "The height of the effects video window, in pixels." )
52
53 #define NBBANDS_TEXT N_( "Number of bands" )
54 #define NBBANDS_LONGTEXT N_( \
55       "Number of bands used by spectrum analyzer, should be 20 or 80." )
56 #define SPNBBANDS_LONGTEXT N_( \
57       "Number of bands used by the spectrometer, from 20 to 80." )
58
59 #define SEPAR_TEXT N_( "Band separator" )
60 #define SEPAR_LONGTEXT N_( \
61         "Number of blank pixels between bands.")
62
63 #define AMP_TEXT N_( "Amplification" )
64 #define AMP_LONGTEXT N_( \
65         "This is a coefficient that modifies the height of the bands.")
66
67 #define PEAKS_TEXT N_( "Enable peaks" )
68 #define PEAKS_LONGTEXT N_( \
69         "Draw \"peaks\" in the spectrum analyzer." )
70
71 #define ORIG_TEXT N_( "Enable original graphic spectrum" )
72 #define ORIG_LONGTEXT N_( \
73         "Enable the \"flat\" spectrum analyzer in the spectrometer." )
74
75 #define BANDS_TEXT N_( "Enable bands" )
76 #define BANDS_LONGTEXT N_( \
77         "Draw bands in the spectrometer." )
78
79 #define BASE_TEXT N_( "Enable base" )
80 #define BASE_LONGTEXT N_( \
81         "Defines whether to draw the base of the bands." )
82
83 #define RADIUS_TEXT N_( "Base pixel radius" )
84 #define RADIUS_LONGTEXT N_( \
85         "Defines radius size in pixels, of base of bands(beginning)." )
86
87 #define SSECT_TEXT N_( "Spectral sections" )
88 #define SSECT_LONGTEXT N_( \
89         "Determines how many sections of spectrum will exist." )
90
91 #define PEAK_HEIGHT_TEXT N_( "Peak height" )
92 #define PEAK_HEIGHT_LONGTEXT N_( \
93         "Total pixel height of the peak items." )
94
95 #define PEAK_WIDTH_TEXT N_( "Peak extra width" )
96 #define PEAK_WIDTH_LONGTEXT N_( \
97         "Additions or subtractions of pixels on the peak width." )
98
99 #define COLOR1_TEXT N_( "V-plane color" )
100 #define COLOR1_LONGTEXT N_( \
101         "YUV-Color cube shifting across the V-plane ( 0 - 127 )." )
102
103 #define STARS_TEXT N_( "Number of stars" )
104 #define STARS_LONGTEXT N_( \
105         "Number of stars to draw with random effect." )
106
107 static int  Open         ( vlc_object_t * );
108 static void Close        ( vlc_object_t * );
109
110 vlc_module_begin();
111     set_shortname( _("Visualizer"));
112     set_category( CAT_AUDIO );
113     set_subcategory( SUBCAT_AUDIO_VISUAL );
114     set_description( _("Visualizer filter") );
115     set_section( N_( "General") , NULL );
116     add_string("effect-list", "spectrum", NULL,
117             ELIST_TEXT, ELIST_LONGTEXT, VLC_TRUE );
118     add_integer("effect-width",VOUT_WIDTH,NULL,
119              WIDTH_TEXT, WIDTH_LONGTEXT, VLC_FALSE );
120     add_integer("effect-height" , VOUT_HEIGHT , NULL,
121              HEIGHT_TEXT, HEIGHT_LONGTEXT, VLC_FALSE );
122     set_section( N_("Spectrum analyser") , NULL );
123     add_integer("visual-nbbands", 80, NULL,
124              NBBANDS_TEXT, NBBANDS_LONGTEXT, VLC_TRUE );
125     add_integer("visual-separ", 1, NULL,
126              SEPAR_TEXT, SEPAR_LONGTEXT, VLC_TRUE );
127     add_integer("visual-amp", 3, NULL,
128              AMP_TEXT, AMP_LONGTEXT, VLC_TRUE );
129     add_bool("visual-peaks", VLC_TRUE, NULL,
130              PEAKS_TEXT, PEAKS_LONGTEXT, VLC_TRUE );
131     set_section( N_("Spectrometer") , NULL );
132     add_bool("spect-show-original", VLC_FALSE, NULL,
133              ORIG_TEXT, ORIG_LONGTEXT, VLC_TRUE );
134     add_bool("spect-show-base", VLC_TRUE, NULL,
135              BASE_TEXT, BASE_LONGTEXT, VLC_TRUE );
136     add_integer("spect-radius", 42, NULL,
137              RADIUS_TEXT, RADIUS_LONGTEXT, VLC_TRUE );
138     add_integer("spect-sections", 3, NULL,
139              SSECT_TEXT, SSECT_LONGTEXT, VLC_TRUE );
140     add_integer("spect-color", 80, NULL,
141              COLOR1_TEXT, COLOR1_LONGTEXT, VLC_TRUE );
142     add_bool("spect-show-bands", VLC_TRUE, NULL,
143              BANDS_TEXT, BANDS_LONGTEXT, VLC_TRUE );
144     add_integer("spect-nbbands", 32, NULL,
145              NBBANDS_TEXT, SPNBBANDS_LONGTEXT, VLC_TRUE );
146     add_integer("spect-separ", 1, NULL,
147              SEPAR_TEXT, SEPAR_LONGTEXT, VLC_TRUE );
148     add_integer("spect-amp", 8, NULL,
149              AMP_TEXT, AMP_LONGTEXT, VLC_TRUE );
150     add_bool("spect-show-peaks", VLC_TRUE, NULL,
151              PEAKS_TEXT, PEAKS_LONGTEXT, VLC_TRUE );
152     add_integer("spect-peak-width", 61, NULL,
153              PEAK_WIDTH_TEXT, PEAK_WIDTH_LONGTEXT, VLC_TRUE );
154     add_integer("spect-peak-height", 1, NULL,
155              PEAK_HEIGHT_TEXT, PEAK_HEIGHT_LONGTEXT, VLC_TRUE );
156     set_capability( "visualization", 0 );
157     set_callbacks( Open, Close );
158     add_shortcut( "visualizer");
159 vlc_module_end();
160
161
162 /*****************************************************************************
163  * Local prototypes
164  *****************************************************************************/
165 static void DoWork( aout_instance_t *, aout_filter_t *,
166                     aout_buffer_t *, aout_buffer_t * );
167 static int FilterCallback( vlc_object_t *, char const *,
168                            vlc_value_t, vlc_value_t, void * );
169 static struct
170 {
171     const char *psz_name;
172     int  (*pf_run)( visual_effect_t *, aout_instance_t *,
173                     aout_buffer_t *, picture_t *);
174 } pf_effect_run[]=
175 {
176     { "scope",      scope_Run },
177     { "spectrum",   spectrum_Run },
178     { "spectrometer",   spectrometer_Run },
179     { "dummy",      dummy_Run},
180     { NULL,         dummy_Run}
181 };
182
183 /*****************************************************************************
184  * Open: open the visualizer
185  *****************************************************************************/
186 static int Open( vlc_object_t *p_this )
187 {
188     aout_filter_t     *p_filter = (aout_filter_t *)p_this;
189     aout_filter_sys_t *p_sys;
190     vlc_value_t        val;
191
192     char *psz_effects, *psz_parser;
193     video_format_t fmt;
194
195
196     if( ( p_filter->input.i_format != VLC_FOURCC('f','l','3','2') &&
197           p_filter->input.i_format != VLC_FOURCC('f','i','3','2') ) )
198     {
199         return VLC_EGENERIC;
200     }
201
202     p_sys = p_filter->p_sys = malloc( sizeof( aout_filter_sys_t ) );
203     if( p_sys == NULL )
204     {
205         msg_Err( p_filter, "out of memory" );
206         return VLC_EGENERIC;
207     }
208
209     p_sys->i_height = config_GetInt( p_filter , "effect-height");
210     p_sys->i_width  = config_GetInt( p_filter , "effect-width");
211
212     if( p_sys->i_height < 20 ) p_sys->i_height =  20;
213     if( p_sys->i_width  < 20 ) p_sys->i_width  =  20;
214     if( (p_sys->i_height % 2 ) != 0 ) p_sys->i_height--;
215     if( (p_sys->i_width % 2 )  != 0 ) p_sys->i_width--;
216
217     p_sys->i_effect = 0;
218     p_sys->effect   = NULL;
219
220     /* Parse the effect list */
221     var_Create( p_filter, "effect-list", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
222     var_Get( p_filter, "effect-list", &val);
223     psz_parser = psz_effects = strdup( val.psz_string );
224     free( val.psz_string );
225
226     var_AddCallback( p_filter, "effect-list", FilterCallback, NULL );
227
228     while( psz_parser && *psz_parser != '\0' )
229     {
230         visual_effect_t *p_effect;
231         int  i;
232
233         p_effect = malloc( sizeof( visual_effect_t ) );
234         if( !p_effect )
235             break;
236         p_effect->i_width = p_sys->i_width;
237         p_effect->i_height= p_sys->i_height;
238         p_effect->i_nb_chans = aout_FormatNbChannels( &p_filter->input);
239         p_effect->psz_args = NULL;
240         p_effect->p_data = NULL;
241
242         p_effect->pf_run = NULL;
243         p_effect->psz_name = NULL;
244
245         for( i = 0; pf_effect_run[i].psz_name != NULL; i++ )
246         {
247             if( !strncasecmp( psz_parser,
248                               pf_effect_run[i].psz_name,
249                               strlen( pf_effect_run[i].psz_name ) ) )
250             {
251                 p_effect->pf_run = pf_effect_run[i].pf_run;
252                 p_effect->psz_name = strdup( pf_effect_run[i].psz_name );
253                 break;
254             }
255         }
256
257         if( p_effect->psz_name )
258         {
259             psz_parser += strlen( p_effect->psz_name );
260
261             if( *psz_parser == '{' )
262             {
263                 char *psz_eoa;
264
265                 psz_parser++;
266
267                 if( ( psz_eoa = strchr( psz_parser, '}') ) == NULL )
268                 {
269                    msg_Err( p_filter, "unable to parse effect list. Aborting");
270                    break;
271                 }
272                 p_effect->psz_args =
273                     strndup( psz_parser, psz_eoa - psz_parser);
274             }
275             TAB_APPEND( p_sys->i_effect, p_sys->effect, p_effect );
276         }
277         else
278         {
279             msg_Err( p_filter, "unknown visual effect: %s", psz_parser );
280             free( p_effect );
281         }
282
283         if( strchr( psz_parser, ',' ) )
284         {
285             psz_parser = strchr( psz_parser, ',' ) + 1;
286         }
287         else if( strchr( psz_parser, ':' ) )
288         {
289             psz_parser = strchr( psz_parser, ':' ) + 1;
290         }
291         else
292         {
293             break;
294         }
295     }
296
297     if( psz_effects )
298     {
299         free( psz_effects );
300     }
301
302     if( !p_sys->i_effect )
303     {
304         msg_Err( p_filter, "no effects found" );
305         free( p_sys );
306         return VLC_EGENERIC;
307     }
308
309     /* Open the video output */
310     memset( &fmt, 0, sizeof(video_format_t) );
311
312     fmt.i_width = fmt.i_visible_width = p_sys->i_width;
313     fmt.i_height = fmt.i_visible_height = p_sys->i_height;
314     fmt.i_chroma = VLC_FOURCC('I','4','2','0');
315     fmt.i_aspect = VOUT_ASPECT_FACTOR * p_sys->i_width/p_sys->i_height;
316     fmt.i_sar_num = fmt.i_sar_den = 1;
317
318     p_sys->p_vout = vout_Request( p_filter, NULL, &fmt );
319     if( p_sys->p_vout == NULL )
320     {
321         msg_Err( p_filter, "no suitable vout module" );
322         free( p_sys );
323         return VLC_EGENERIC;
324     }
325
326     p_filter->pf_do_work = DoWork;
327     p_filter->b_in_place= 1;
328
329     return VLC_SUCCESS;
330 }
331
332 /*****************************************************************************
333  * DoWork: convert a buffer
334  *****************************************************************************
335  * Audio part pasted from trivial.c
336  ****************************************************************************/
337 static void DoWork( aout_instance_t *p_aout, aout_filter_t *p_filter,
338                     aout_buffer_t *p_in_buf, aout_buffer_t *p_out_buf )
339 {
340     aout_filter_sys_t *p_sys = p_filter->p_sys;
341     picture_t *p_outpic;
342     int i;
343
344     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
345     p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes *
346                             aout_FormatNbChannels( &p_filter->output ) /
347                             aout_FormatNbChannels( &p_filter->input );
348
349     /* First, get a new picture */
350     while( ( p_outpic = vout_CreatePicture( p_sys->p_vout, 0, 0, 3 ) ) == NULL)
351     {
352         if( p_aout->b_die )
353         {
354             return;
355         }
356         msleep( VOUT_OUTMEM_SLEEP );
357     }
358
359     /* Blank the picture */
360     for( i = 0 ; i < p_outpic->i_planes ; i++ )
361     {
362         memset( p_outpic->p[i].p_pixels, i > 0 ? 0x80 : 0x00,
363                 p_outpic->p[i].i_visible_lines * p_outpic->p[i].i_pitch );
364     }
365
366     /* We can now call our visualization effects */
367     for( i = 0; i < p_sys->i_effect; i++ )
368     {
369 #define p_effect p_sys->effect[i]
370         if( p_effect->pf_run )
371         {
372             p_effect->pf_run( p_effect, p_aout, p_out_buf, p_outpic );
373         }
374 #undef p_effect
375     }
376
377     vout_DatePicture( p_sys->p_vout, p_outpic,
378                       ( p_in_buf->start_date + p_in_buf->end_date ) / 2 );
379
380     vout_DisplayPicture( p_sys->p_vout, p_outpic );
381 }
382
383 /*****************************************************************************
384  * Close: close the plugin
385  *****************************************************************************/
386 static void Close( vlc_object_t *p_this )
387 {
388     aout_filter_t * p_filter = (aout_filter_t *)p_this;
389     aout_filter_sys_t *p_sys = p_filter->p_sys;
390
391     int i;
392
393     if( p_filter->p_sys->p_vout )
394     {
395         vout_Request( p_filter, p_filter->p_sys->p_vout, 0 );
396     }
397
398     /* Free the list */
399     for( i = 0; i < p_sys->i_effect; i++ )
400     {
401 #define p_effect p_sys->effect[i]
402         if( p_effect->psz_name )
403         {
404             free( p_effect->psz_name );
405         }
406         if( p_effect->psz_args )
407         {
408             free( p_effect->psz_args );
409         }
410         free( p_effect );
411 #undef p_effect
412     }
413
414     if( p_sys->effect )
415     {
416         free( p_sys->effect );
417     }
418
419     free( p_filter->p_sys );
420 }
421
422 /*****************************************************************************
423  * FilterCallback: called when changing the deinterlace method on the fly.
424  *****************************************************************************/
425 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
426                            vlc_value_t oldval, vlc_value_t newval,
427                            void *p_data )
428 {
429     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
430     VLC_UNUSED(p_data); VLC_UNUSED(newval);
431     aout_filter_t     *p_filter = (aout_filter_t *)p_this;
432     /* restart this baby */
433     msg_Dbg( p_filter, "we should restart the visual filter" );
434     return VLC_SUCCESS;
435 }
436