]> git.sesse.net Git - vlc/blob - modules/visualization/visual/visual.c
projectm: add missing return.
[vlc] / modules / visualization / visual / visual.c
1 /*****************************************************************************
2  * visual.c : Visualisation system
3  *****************************************************************************
4  * Copyright (C) 2002-2009 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 #include <vlc_filter.h>
36
37 #include "visual.h"
38
39 /*****************************************************************************
40  * Module descriptor
41  *****************************************************************************/
42 #define ELIST_TEXT N_( "Effects list" )
43 #define ELIST_LONGTEXT N_( \
44       "A list of visual effect, separated by commas.\n"  \
45       "Current effects include: dummy, scope, spectrum, "\
46       "spectrometer and vuMeter." )
47
48 #define WIDTH_TEXT N_( "Video width" )
49 #define WIDTH_LONGTEXT N_( \
50       "The width of the effects video window, in pixels." )
51
52 #define HEIGHT_TEXT N_( "Video height" )
53 #define HEIGHT_LONGTEXT N_( \
54       "The height of the effects video window, in pixels." )
55
56 #define NBBANDS_TEXT N_( "More bands : 80 / 20" )
57 #define NBBANDS_LONGTEXT N_( \
58       "More bands for the spectrum analyzer : 80 if enabled else 20." )
59 #define SPNBBANDS_LONGTEXT N_( \
60       "More bands for the spectrometer : 80 if enabled else 20." )
61
62 #define SEPAR_TEXT N_( "Band separator" )
63 #define SEPAR_LONGTEXT N_( \
64         "Number of blank pixels between bands.")
65
66 #define AMP_TEXT N_( "Amplification" )
67 #define AMP_LONGTEXT N_( \
68         "This is a coefficient that modifies the height of the bands.")
69
70 #define PEAKS_TEXT N_( "Enable peaks" )
71 #define PEAKS_LONGTEXT N_( \
72         "Draw \"peaks\" in the spectrum analyzer." )
73
74 #define ORIG_TEXT N_( "Enable original graphic spectrum" )
75 #define ORIG_LONGTEXT N_( \
76         "Enable the \"flat\" spectrum analyzer in the spectrometer." )
77
78 #define BANDS_TEXT N_( "Enable bands" )
79 #define BANDS_LONGTEXT N_( \
80         "Draw bands in the spectrometer." )
81
82 #define BASE_TEXT N_( "Enable base" )
83 #define BASE_LONGTEXT N_( \
84         "Defines whether to draw the base of the bands." )
85
86 #define RADIUS_TEXT N_( "Base pixel radius" )
87 #define RADIUS_LONGTEXT N_( \
88         "Defines radius size in pixels, of base of bands(beginning)." )
89
90 #define SSECT_TEXT N_( "Spectral sections" )
91 #define SSECT_LONGTEXT N_( \
92         "Determines how many sections of spectrum will exist." )
93
94 #define PEAK_HEIGHT_TEXT N_( "Peak height" )
95 #define PEAK_HEIGHT_LONGTEXT N_( \
96         "Total pixel height of the peak items." )
97
98 #define PEAK_WIDTH_TEXT N_( "Peak extra width" )
99 #define PEAK_WIDTH_LONGTEXT N_( \
100         "Additions or subtractions of pixels on the peak width." )
101
102 #define COLOR1_TEXT N_( "V-plane color" )
103 #define COLOR1_LONGTEXT N_( \
104         "YUV-Color cube shifting across the V-plane ( 0 - 127 )." )
105
106 static int  Open         ( vlc_object_t * );
107 static void Close        ( vlc_object_t * );
108
109 vlc_module_begin ()
110     set_shortname( N_("Visualizer"))
111     set_category( CAT_AUDIO )
112     set_subcategory( SUBCAT_AUDIO_VISUAL )
113     set_description( N_("Visualizer filter") )
114     set_section( N_( "General") , NULL )
115     add_string("effect-list", "spectrum", NULL,
116             ELIST_TEXT, ELIST_LONGTEXT, true )
117     add_integer("effect-width",VOUT_WIDTH,NULL,
118              WIDTH_TEXT, WIDTH_LONGTEXT, false )
119     add_integer("effect-height" , VOUT_HEIGHT , NULL,
120              HEIGHT_TEXT, HEIGHT_LONGTEXT, false )
121     set_section( N_("Spectrum analyser") , NULL )
122     add_obsolete_integer( "visual-nbbands" ) /* Since 1.0.0 */
123     add_bool("visual-80-bands", true, NULL,
124              NBBANDS_TEXT, NBBANDS_LONGTEXT, true );
125     add_obsolete_integer( "visual-separ" ) /* Since 1.0.0 */
126     add_obsolete_integer( "visual-amp" ) /* Since 1.0.0 */
127     add_bool("visual-peaks", true, NULL,
128              PEAKS_TEXT, PEAKS_LONGTEXT, true )
129     set_section( N_("Spectrometer") , NULL )
130     add_bool("spect-show-original", false, NULL,
131              ORIG_TEXT, ORIG_LONGTEXT, true )
132     add_bool("spect-show-base", true, NULL,
133              BASE_TEXT, BASE_LONGTEXT, true )
134     add_integer("spect-radius", 42, NULL,
135              RADIUS_TEXT, RADIUS_LONGTEXT, true )
136     add_integer("spect-sections", 3, NULL,
137              SSECT_TEXT, SSECT_LONGTEXT, true )
138     add_integer("spect-color", 80, NULL,
139              COLOR1_TEXT, COLOR1_LONGTEXT, true )
140     add_bool("spect-show-bands", true, NULL,
141              BANDS_TEXT, BANDS_LONGTEXT, true );
142     add_obsolete_integer( "spect-nbbands" ) /* Since 1.0.0 */
143     add_bool("spect-80-bands", true, NULL,
144              NBBANDS_TEXT, SPNBBANDS_LONGTEXT, true )
145     add_integer("spect-separ", 1, NULL,
146              SEPAR_TEXT, SEPAR_LONGTEXT, true )
147     add_integer("spect-amp", 8, NULL,
148              AMP_TEXT, AMP_LONGTEXT, true )
149     add_bool("spect-show-peaks", true, NULL,
150              PEAKS_TEXT, PEAKS_LONGTEXT, true )
151     add_integer("spect-peak-width", 61, NULL,
152              PEAK_WIDTH_TEXT, PEAK_WIDTH_LONGTEXT, true )
153     add_integer("spect-peak-height", 1, NULL,
154              PEAK_HEIGHT_TEXT, PEAK_HEIGHT_LONGTEXT, true )
155     set_capability( "visualization2", 0 )
156     set_callbacks( Open, Close )
157     add_shortcut( "visualizer")
158 vlc_module_end ()
159
160
161 /*****************************************************************************
162  * Local prototypes
163  *****************************************************************************/
164 static block_t *DoWork( filter_t *, block_t * );
165 static int FilterCallback( vlc_object_t *, char const *,
166                            vlc_value_t, vlc_value_t, void * );
167 static const struct
168 {
169     const char *psz_name;
170     int  (*pf_run)( visual_effect_t *, vlc_object_t *,
171                     const block_t *, picture_t *);
172 } pf_effect_run[]=
173 {
174     { "scope",        scope_Run },
175     { "vuMeter",      vuMeter_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     filter_t     *p_filter = (filter_t *)p_this;
188     filter_sys_t *p_sys;
189
190     char *psz_effects, *psz_parser;
191     video_format_t fmt;
192
193     if( ( p_filter->fmt_in.audio.i_format != VLC_CODEC_FL32 &&
194           p_filter->fmt_in.audio.i_format != VLC_CODEC_FI32 ) )
195     {
196         return VLC_EGENERIC;
197     }
198
199     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
200     if( p_sys == NULL )
201         return VLC_EGENERIC;
202
203     p_sys->i_height = var_InheritInteger( p_filter , "effect-height");
204     p_sys->i_width  = var_InheritInteger( p_filter , "effect-width");
205
206     if( p_sys->i_height < 400 ) p_sys->i_height = 400;
207     if( p_sys->i_width  < 532 ) p_sys->i_width  = 532;
208     if( (p_sys->i_height % 2 ) != 0 ) p_sys->i_height--;
209     if( (p_sys->i_width % 2 )  != 0 ) p_sys->i_width--;
210
211     p_sys->i_effect = 0;
212     p_sys->effect   = NULL;
213
214     /* Parse the effect list */
215     psz_parser = psz_effects = var_CreateGetString( p_filter, "effect-list" );
216     var_AddCallback( p_filter, "effect-list", FilterCallback, NULL );
217
218     while( psz_parser && *psz_parser != '\0' )
219     {
220         visual_effect_t *p_effect;
221         int  i;
222
223         p_effect = malloc( sizeof( visual_effect_t ) );
224         if( !p_effect )
225             break;
226         p_effect->i_width = p_sys->i_width;
227         p_effect->i_height= p_sys->i_height;
228         p_effect->i_nb_chans = aout_FormatNbChannels( &p_filter->fmt_in.audio);
229         p_effect->i_idx_left  = 0;
230         p_effect->i_idx_right = __MIN( 1, p_effect->i_nb_chans-1 );
231
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 = 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 );
264                    break;
265                 }
266                 p_effect->psz_args =
267                     strndup( psz_parser, psz_eoa - psz_parser);
268             }
269             TAB_APPEND( p_sys->i_effect, p_sys->effect, p_effect );
270         }
271         else
272         {
273             msg_Err( p_filter, "unknown visual effect: %s", psz_parser );
274             free( p_effect );
275         }
276
277         if( strchr( psz_parser, ',' ) )
278         {
279             psz_parser = strchr( psz_parser, ',' ) + 1;
280         }
281         else if( strchr( psz_parser, ':' ) )
282         {
283             psz_parser = strchr( psz_parser, ':' ) + 1;
284         }
285         else
286         {
287             break;
288         }
289     }
290
291     free( psz_effects );
292
293     if( !p_sys->i_effect )
294     {
295         msg_Err( p_filter, "no effects found" );
296         free( p_sys );
297         return VLC_EGENERIC;
298     }
299
300     /* Open the video output */
301     memset( &fmt, 0, sizeof(video_format_t) );
302
303     fmt.i_width = fmt.i_visible_width = p_sys->i_width;
304     fmt.i_height = fmt.i_visible_height = p_sys->i_height;
305     fmt.i_chroma = VLC_CODEC_I420;
306     fmt.i_sar_num = fmt.i_sar_den = 1;
307
308     p_sys->p_vout = aout_filter_RequestVout( p_filter, NULL, &fmt );
309     if( p_sys->p_vout == NULL )
310     {
311         msg_Err( p_filter, "no suitable vout module" );
312         for( int i = 0; i < p_sys->i_effect; i++ )
313         {
314             free( p_sys->effect[i]->psz_args );
315             free( p_sys->effect[i] );
316         }
317         free( p_sys->effect );
318         free( p_sys );
319         return VLC_EGENERIC;
320     }
321
322     p_filter->pf_audio_filter = DoWork;
323
324     return VLC_SUCCESS;
325 }
326
327 /*****************************************************************************
328  * DoWork: convert a buffer
329  *****************************************************************************
330  * Audio part pasted from trivial.c
331  ****************************************************************************/
332 static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
333 {
334     filter_sys_t *p_sys = p_filter->p_sys;
335     picture_t *p_outpic;
336     int i;
337
338     /* First, get a new picture */
339     while( ( p_outpic = vout_CreatePicture( p_sys->p_vout, 0, 0, 3 ) ) == NULL)
340     {   /* XXX: This looks like a bad idea. Don't run to me for sympathy if it
341          * dead locks... */
342         if( !vlc_object_alive (p_sys->p_vout) )
343             return NULL;
344         msleep( VOUT_OUTMEM_SLEEP );
345     }
346
347     /* Blank the picture */
348     for( i = 0 ; i < p_outpic->i_planes ; i++ )
349     {
350         memset( p_outpic->p[i].p_pixels, i > 0 ? 0x80 : 0x00,
351                 p_outpic->p[i].i_visible_lines * p_outpic->p[i].i_pitch );
352     }
353
354     /* We can now call our visualization effects */
355     for( i = 0; i < p_sys->i_effect; i++ )
356     {
357 #define p_effect p_sys->effect[i]
358         if( p_effect->pf_run )
359         {
360             p_effect->pf_run( p_effect, VLC_OBJECT(p_filter),
361                               p_in_buf, p_outpic );
362         }
363 #undef p_effect
364     }
365
366     p_outpic->date = p_in_buf->i_pts + (p_in_buf->i_length / 2);
367
368     vout_DisplayPicture( p_sys->p_vout, p_outpic );
369     return p_in_buf;
370 }
371
372 /*****************************************************************************
373  * Close: close the plugin
374  *****************************************************************************/
375 static void Close( vlc_object_t *p_this )
376 {
377     filter_t * p_filter = (filter_t *)p_this;
378     filter_sys_t *p_sys = p_filter->p_sys;
379
380     int i;
381
382     if( p_filter->p_sys->p_vout )
383     {
384         aout_filter_RequestVout( p_filter, p_filter->p_sys->p_vout, 0 );
385     }
386
387     /* Free the list */
388     for( i = 0; i < p_sys->i_effect; i++ )
389     {
390 #define p_effect p_sys->effect[i]
391         if( !strncmp( p_effect->psz_name, "spectrum", strlen( "spectrum" ) ) )
392         {
393             spectrum_data *p_data = p_effect->p_data;
394             free( p_data->peaks );
395             free( p_data->prev_heights );
396             free( p_data->p_prev_s16_buff );
397         }
398         if( !strncmp( p_effect->psz_name, "spectrometer", strlen( "spectrometer" ) ) )
399         {
400             spectrometer_data *p_data = p_effect->p_data;
401             free( p_data->peaks );
402             free( p_data->p_prev_s16_buff );
403         }
404         free( p_effect->p_data );
405         free( p_effect->psz_args );
406         free( p_effect );
407 #undef p_effect
408     }
409
410     free( p_sys->effect );
411     free( p_filter->p_sys );
412 }
413
414 /*****************************************************************************
415  * FilterCallback: called when changing the deinterlace method on the fly.
416  *****************************************************************************/
417 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
418                            vlc_value_t oldval, vlc_value_t newval,
419                            void *p_data )
420 {
421     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
422     VLC_UNUSED(p_data); VLC_UNUSED(newval);
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