]> git.sesse.net Git - vlc/blob - modules/visualization/visual/visual.c
4c4af5bea47f9df73c6929211a2e82306837cf18
[vlc] / modules / visualization / visual / visual.c
1 /*****************************************************************************
2  * visual.c : Visualisation system
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <vlc/vlc.h>
29 #include <vlc/vout.h>
30 #include "audio_output.h"
31 #include "aout_internal.h"
32
33 #include "visual.h"
34
35 /*****************************************************************************
36  * Module descriptor
37  *****************************************************************************/
38 #define ELIST_TEXT N_( "Effects list" )
39 #define ELIST_LONGTEXT N_( \
40       "A list of visual effect, separated by commas.\n"  \
41       "Current effects include: dummy, random, scope, spectrum" )
42
43 #define WIDTH_TEXT N_( "Video width" )
44 #define WIDTH_LONGTEXT N_( \
45       "The width of the effects video window, in pixels." )
46
47 #define HEIGHT_TEXT N_( "Video height" )
48 #define HEIGHT_LONGTEXT N_( \
49       "The height of the effects video window, in pixels." )
50
51 #define NBBANDS_TEXT N_( "Number of bands" )
52 #define NBBANDS_LONGTEXT N_( \
53       "Number of bands used by spectrum analyzer, should be 20 or 80." )
54
55 #define SEPAR_TEXT N_( "Band separator" )
56 #define SEPAR_LONGTEXT N_( \
57         "Number of blank pixels between bands.")
58
59 #define AMP_TEXT N_( "Amplification" )
60 #define AMP_LONGTEXT N_( \
61         "This is a coefficient that modifies the height of the bands.")
62
63 #define PEAKS_TEXT N_( "Enable peaks" )
64 #define PEAKS_LONGTEXT N_( \
65         "Defines whether to draw peaks." )
66
67 #define STARS_TEXT N_( "Number of stars" )
68 #define STARS_LONGTEXT N_( \
69         "Defines the number of stars to draw with random effect." )
70
71 static int  Open         ( vlc_object_t * );
72 static void Close        ( vlc_object_t * );
73
74 vlc_module_begin();
75     set_description( _("visualizer filter") );
76     add_string("effect-list", "spectrum", NULL,
77             ELIST_TEXT, ELIST_LONGTEXT, VLC_TRUE );
78     add_integer("effect-width",VOUT_WIDTH,NULL,
79              WIDTH_TEXT, WIDTH_LONGTEXT, VLC_FALSE );
80     add_integer("effect-height" , VOUT_HEIGHT , NULL,
81              HEIGHT_TEXT, HEIGHT_LONGTEXT, VLC_FALSE );
82     add_integer("visual-nbbands", 80, NULL,
83              NBBANDS_TEXT, NBBANDS_LONGTEXT, VLC_FALSE );
84     add_integer("visual-separ", 1, NULL,
85              SEPAR_TEXT, SEPAR_LONGTEXT, VLC_FALSE );
86     add_integer("visual-amp", 3, NULL,
87              AMP_TEXT, AMP_LONGTEXT, VLC_FALSE );
88     add_bool("visual-peaks", VLC_TRUE, NULL,
89              PEAKS_TEXT, PEAKS_LONGTEXT, VLC_FALSE );
90     add_integer("visual-stars", 200, NULL,
91              STARS_TEXT, STARS_LONGTEXT, VLC_FALSE );
92     set_capability( "audio filter", 0 );
93     set_callbacks( Open, Close );
94     add_shortcut( "visualizer");
95 vlc_module_end();
96
97
98 /*****************************************************************************
99  * Local prototypes
100  *****************************************************************************/
101 static void DoWork( aout_instance_t *, aout_filter_t *,
102                     aout_buffer_t *, aout_buffer_t * );
103 static int FilterCallback( vlc_object_t *, char const *,
104                            vlc_value_t, vlc_value_t, void * );
105 static struct
106 {
107     char *psz_name;
108     int  (*pf_run)( visual_effect_t *, aout_instance_t *,
109                     aout_buffer_t *, picture_t *);
110 } pf_effect_run[]=
111 {
112     { "scope",      scope_Run },
113     { "spectrum",   spectrum_Run },
114     { "random",     random_Run},
115     { "dummy",      dummy_Run},
116     { NULL,         dummy_Run}
117 };
118
119 /*****************************************************************************
120  * Open: open the visualizer
121  *****************************************************************************/
122 static int Open( vlc_object_t *p_this )
123 {
124     aout_filter_t     *p_filter = (aout_filter_t *)p_this;
125     aout_filter_sys_t *p_sys;
126     vlc_value_t        val;
127
128     char *psz_effects, *psz_parser;
129
130     if( ( p_filter->input.i_format != VLC_FOURCC('f','l','3','2') &&
131           p_filter->input.i_format != VLC_FOURCC('f','i','3','2') ) )
132     {
133         return VLC_EGENERIC;
134     }
135
136     p_sys = p_filter->p_sys = malloc( sizeof( aout_filter_sys_t ) );
137     if( p_sys == NULL )
138     {
139         msg_Err( p_filter, "out of memory" );
140         return VLC_EGENERIC;
141     }
142
143     p_sys->i_height = config_GetInt( p_filter , "effect-height");
144     p_sys->i_width  = config_GetInt( p_filter , "effect-width");
145
146     if( p_sys->i_height < 20 ) p_sys->i_height =  20;
147     if( p_sys->i_width  < 20 ) p_sys->i_width  =  20;
148     if( (p_sys->i_height % 2 ) != 0 ) p_sys->i_height--;
149     if( (p_sys->i_width % 2 )  != 0 ) p_sys->i_width--;
150
151     p_sys->i_effect = 0;
152     p_sys->effect   = NULL;
153
154     /* Parse the effect list */
155     var_Create( p_filter, "effect-list", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
156     var_Get( p_filter, "effect-list", &val);
157     psz_parser = psz_effects = strdup( val.psz_string );
158     free( val.psz_string );
159     msg_Dbg( p_filter , "Building list of effects" );
160     
161     var_AddCallback( p_filter, "effect-list", FilterCallback, NULL );
162
163     while( psz_parser && *psz_parser != '\0' )
164     {
165         visual_effect_t *p_effect;
166         int  i;
167
168         p_effect = malloc( sizeof( visual_effect_t ) );
169         p_effect->i_width = p_sys->i_width;
170         p_effect->i_height= p_sys->i_height;
171         p_effect->i_nb_chans = aout_FormatNbChannels( &p_filter->input);
172         p_effect->psz_args = NULL;
173         p_effect->p_data = NULL;
174
175         p_effect->pf_run = NULL;
176         p_effect->psz_name = NULL;
177
178         for( i = 0; pf_effect_run[i].psz_name != NULL; i++ )
179         {
180             if( !strncasecmp( psz_parser,
181                               pf_effect_run[i].psz_name,
182                               strlen( pf_effect_run[i].psz_name ) ) )
183             {
184                 p_effect->pf_run = pf_effect_run[i].pf_run;
185                 p_effect->psz_name = strdup( pf_effect_run[i].psz_name );
186                 break;
187             }
188         }
189
190         if( p_effect->psz_name )
191         {
192             psz_parser += strlen( p_effect->psz_name );
193
194             if( *psz_parser == '{' )
195             {
196                 char *psz_eoa;
197
198                 psz_parser++;
199
200                 if( ( psz_eoa = strchr( psz_parser, '}') ) == NULL )
201                 {
202                    msg_Err( p_filter, "Unable to parse effect list. Aborting");
203                    break;
204                 }
205                 p_effect->psz_args = strndup( psz_parser, psz_eoa - psz_parser);
206             }
207         }
208         else
209         {
210             msg_Err( p_filter, "unknown visual effect" );
211         }
212
213         TAB_APPEND( p_sys->i_effect, p_sys->effect, p_effect );
214
215         if( strchr( psz_parser, ',' ) )
216         {
217             psz_parser = strchr( psz_parser, ',' ) + 1;
218         }
219         else if( strchr( psz_parser, ':' ) )
220         {
221             psz_parser = strchr( psz_parser, ':' ) + 1;
222         }
223         else
224         {
225             break;
226         }
227     }
228
229     if( psz_effects )
230     {
231         free( psz_effects );
232     }
233
234
235     /* Open the video output */
236     p_sys->p_vout =
237          vout_Request( p_filter, NULL,
238                        p_sys->i_width, p_sys->i_height,
239                        VLC_FOURCC('I','4','2','0'),
240                        VOUT_ASPECT_FACTOR * p_sys->i_width/p_sys->i_height );
241
242     if( p_sys->p_vout == NULL )
243     {
244         msg_Err( p_filter, "no suitable vout module" );
245         free( p_sys );
246         return VLC_EGENERIC;
247     }
248
249     p_filter->pf_do_work = DoWork;
250     p_filter->b_in_place= 1;
251
252     msg_Dbg( p_filter,"Visualizer initialized");
253     return VLC_SUCCESS;
254 }
255
256 /*****************************************************************************
257  * DoWork: convert a buffer
258  *****************************************************************************
259  * Audio part pasted from trivial.c
260  ****************************************************************************/
261 static void DoWork( aout_instance_t *p_aout, aout_filter_t *p_filter,
262                     aout_buffer_t *p_in_buf, aout_buffer_t *p_out_buf )
263 {
264     aout_filter_sys_t *p_sys = p_filter->p_sys;
265     picture_t *p_outpic;
266     int i;
267
268     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
269     p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes *
270                             aout_FormatNbChannels( &p_filter->output ) /
271                             aout_FormatNbChannels( &p_filter->input );
272
273     /* First, get a new picture */
274     while( ( p_outpic = vout_CreatePicture( p_sys->p_vout, 0, 0, 3 ) ) == NULL)
275     {
276         if( p_aout->b_die )
277         {
278             return;
279         }
280         msleep( VOUT_OUTMEM_SLEEP );
281     }
282
283     /* Blank the picture */
284     for( i = 0 ; i < p_outpic->i_planes ; i++ )
285     {
286         memset( p_outpic->p[i].p_pixels, i > 0 ? 0x80 : 0x00,
287                 p_outpic->p[i].i_lines * p_outpic->p[i].i_pitch );
288     }
289
290     /* We can now call our visualization effects */
291     for( i = 0; i < p_sys->i_effect; i++ )
292     {
293 #define p_effect p_sys->effect[i]
294         if( p_effect->pf_run )
295         {
296             p_effect->pf_run( p_effect, p_aout, p_out_buf, p_outpic );
297         }
298 #undef p_effect
299     }
300
301     vout_DatePicture( p_sys->p_vout, p_outpic,
302                       ( p_in_buf->start_date + p_in_buf->end_date ) / 2 );
303
304     vout_DisplayPicture( p_sys->p_vout, p_outpic );
305 }
306
307 /*****************************************************************************
308  * Close: close the plugin
309  *****************************************************************************/
310 static void Close( vlc_object_t *p_this )
311 {
312     aout_filter_t * p_filter = (aout_filter_t *)p_this;
313     aout_filter_sys_t *p_sys = p_filter->p_sys;
314
315     int i;
316
317     if( p_filter->p_sys->p_vout )
318     {
319         vout_Request( p_filter, p_filter->p_sys->p_vout, 0, 0, 0, 0 );
320     }
321
322     /* Free the list */
323     for( i = 0; i < p_sys->i_effect; i++ )
324     {
325 #define p_effect p_sys->effect[i]
326         if( p_effect->psz_name )
327         {
328             free( p_effect->psz_name );
329         }
330         if( p_effect->psz_args )
331         {
332             free( p_effect->psz_args );
333         }
334         free( p_effect );
335 #undef p_effect
336     }
337
338     if( p_sys->effect )
339     {
340         free( p_sys->effect );
341     }
342
343     free( p_filter->p_sys );
344 }
345
346 /*****************************************************************************
347  * FilterCallback: called when changing the deinterlace method on the fly.
348  *****************************************************************************/
349 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
350                            vlc_value_t oldval, vlc_value_t newval,
351                            void *p_data )
352 {
353     aout_filter_t     *p_filter = (aout_filter_t *)p_this;
354     /* restart this baby */
355     msg_Dbg( p_filter, "We should restart the visual filter" );
356     return VLC_SUCCESS;
357 }
358