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