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