]> git.sesse.net Git - vlc/blob - modules/visualization/visual/visual.c
* all: clean up.
[vlc] / modules / visualization / visual / visual.c
1 /*****************************************************************************
2  * visual.c : Visualisation system
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: visual.c,v 1.9 2003/09/20 00:37:53 fenrir Exp $
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 HEIGHT_LONGTEXT N_( \
45       "The width of the effects video window, in pixels." )
46
47 #define HEIGHT_TEXT N_( "Video width" )
48 #define WIDTH_LONGTEXT N_( \
49       "The width 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 analizer, 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     add_category_hint( N_("visualizer") , NULL , VLC_FALSE);
76     set_description( _("visualizer filter") );
77     add_string("effect-list", "dummy", NULL,
78             ELIST_TEXT, ELIST_LONGTEXT, VLC_TRUE );
79     add_integer("effect-width",VOUT_WIDTH,NULL,
80              WIDTH_TEXT, WIDTH_LONGTEXT, VLC_FALSE );
81     add_integer("effect-height" , VOUT_HEIGHT , NULL,
82              HEIGHT_TEXT, HEIGHT_LONGTEXT, VLC_FALSE );
83     add_integer("visual-nbbands", 80, NULL,
84              NBBANDS_TEXT, NBBANDS_LONGTEXT, VLC_FALSE );
85     add_integer("visual-separ", 1, NULL,
86              SEPAR_TEXT, SEPAR_LONGTEXT, VLC_FALSE );
87     add_integer("visual-amp", 3, NULL,
88              AMP_TEXT, AMP_LONGTEXT, VLC_FALSE );
89     add_bool("visual-peaks", VLC_TRUE, NULL,
90              PEAKS_TEXT, PEAKS_LONGTEXT, VLC_FALSE );
91     add_integer("visual-stars", 200, NULL,
92              STARS_TEXT, STARS_LONGTEXT, VLC_FALSE );
93     set_capability( "audio filter", 0 );
94     set_callbacks( Open, Close );
95     add_shortcut( "visualizer");
96 vlc_module_end();
97
98
99 /*****************************************************************************
100  * Local prototypes
101  *****************************************************************************/
102 static void DoWork( aout_instance_t *, aout_filter_t *,
103                     aout_buffer_t *, aout_buffer_t * );
104 static struct
105 {
106     char *psz_name;
107     int  (*pf_run)( visual_effect_t *, aout_instance_t *,
108                     aout_buffer_t *, picture_t *);
109 } pf_effect_run[]=
110 {
111     { "scope",      scope_Run },
112     { "spectrum",   spectrum_Run },
113     { "random",     random_Run},
114     { "dummy",      dummy_Run},
115     { NULL,         dummy_Run}
116 };
117
118 /*****************************************************************************
119  * Open: open the visualizer
120  *****************************************************************************/
121 static int Open( vlc_object_t *p_this )
122 {
123     aout_filter_t     *p_filter = (aout_filter_t *)p_this;
124     aout_filter_sys_t *p_sys;
125
126     char *psz_effects, *psz_parser;
127
128     if( ( p_filter->input.i_format != VLC_FOURCC('f','l','3','2') &&
129           p_filter->input.i_format != VLC_FOURCC('f','i','3','2') ) )
130     {
131         return VLC_EGENERIC;
132     }
133
134     p_sys = p_filter->p_sys = malloc( sizeof( aout_filter_sys_t ) );
135     if( p_sys == NULL )
136     {
137         msg_Err( p_filter, "out of memory" );
138         return VLC_EGENERIC;
139     }
140
141     p_sys->i_height = config_GetInt( p_filter , "effect-height");
142     p_sys->i_width  = config_GetInt( p_filter , "effect-width");
143
144     if( p_sys->i_height < 20 ) p_sys->i_height =  20;
145     if( p_sys->i_width  < 20 ) p_sys->i_width  =  20;
146     if( (p_sys->i_height % 2 ) != 0 ) p_sys->i_height--;
147     if( (p_sys->i_width % 2 )  != 0 ) p_sys->i_width--;
148
149     p_sys->i_effect = 0;
150     p_sys->effect   = NULL;
151
152     /* Parse the effect list */
153     psz_parser = psz_effects = config_GetPsz( p_filter, "effect-list" );
154     msg_Dbg( p_filter , "Building list of effects" );
155
156     while( psz_parser && *psz_parser != '\0' )
157     {
158         visual_effect_t *p_effect;
159         int  i;
160
161         p_effect = malloc( sizeof( visual_effect_t ) );
162         p_effect->i_width = p_sys->i_width;
163         p_effect->i_height= p_sys->i_height;
164         p_effect->i_nb_chans = aout_FormatNbChannels( &p_filter->input);
165         p_effect->psz_args = NULL;
166         p_effect->p_data = NULL;
167
168         p_effect->pf_run = NULL;
169         p_effect->psz_name = NULL;
170
171         for( i = 0; pf_effect_run[i].psz_name != NULL; i++ )
172         {
173             if( !strncasecmp( psz_parser,
174                               pf_effect_run[i].psz_name,
175                               strlen( pf_effect_run[i].psz_name ) ) )
176             {
177                 p_effect->pf_run = pf_effect_run[i].pf_run;
178                 p_effect->psz_name = strdup( pf_effect_run[i].psz_name );
179                 break;
180             }
181         }
182
183         if( p_effect->psz_name )
184         {
185             psz_parser += strlen( p_effect->psz_name );
186
187             if( *psz_parser == '{' )
188             {
189                 char *psz_eoa;
190
191                 psz_parser++;
192
193                 if( ( psz_eoa = strchr( psz_parser, '}') ) == NULL )
194                 {
195                    msg_Err( p_filter, "Unable to parse effect list. Aborting");
196                    break;
197                 }
198                 p_effect->psz_args = strndup( psz_parser, psz_eoa - psz_parser);
199             }
200         }
201         else
202         {
203             msg_Err( p_filter, "unknown visual effect" );
204         }
205
206         TAB_APPEND( p_sys->i_effect, p_sys->effect, p_effect );
207
208         if( strchr( psz_parser, ',' ) )
209         {
210             psz_parser = strchr( psz_parser, ',' ) + 1;
211         }
212         else if( strchr( psz_parser, ':' ) )
213         {
214             psz_parser = strchr( psz_parser, ':' ) + 1;
215         }
216         else
217         {
218             break;
219         }
220     }
221
222     if( psz_effects )
223     {
224         free( psz_effects );
225     }
226
227
228     /* Open the video output */
229     p_sys->p_vout =
230          vout_Request( p_filter, NULL,
231                        p_sys->i_width, p_sys->i_height,
232                        VLC_FOURCC('I','4','2','0'),
233                        VOUT_ASPECT_FACTOR * p_sys->i_width/p_sys->i_height );
234
235     if( p_sys->p_vout == NULL )
236     {
237         msg_Err( p_filter, "no suitable vout module" );
238         free( p_sys );
239         return VLC_EGENERIC;
240     }
241
242     p_filter->pf_do_work = DoWork;
243     p_filter->b_in_place= 1;
244
245     msg_Dbg( p_filter,"Visualizer initialized");
246     return VLC_SUCCESS;
247 }
248
249 /*****************************************************************************
250  * DoWork: convert a buffer
251  *****************************************************************************
252  * Audio part pasted from trivial.c
253  ****************************************************************************/
254 static void DoWork( aout_instance_t *p_aout, aout_filter_t *p_filter,
255                     aout_buffer_t *p_in_buf, aout_buffer_t *p_out_buf )
256 {
257     aout_filter_sys_t *p_sys = p_filter->p_sys;
258     picture_t *p_outpic;
259     int i;
260
261     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
262     p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes *
263                             aout_FormatNbChannels( &p_filter->output ) /
264                             aout_FormatNbChannels( &p_filter->input );
265
266     /* First, get a new picture */
267     while( ( p_outpic = vout_CreatePicture( p_sys->p_vout, 0, 0, 3 ) ) == NULL)
268     {
269         if( p_aout->b_die )
270         {
271             return;
272         }
273         msleep( VOUT_OUTMEM_SLEEP );
274     }
275
276     /* Blank the picture */
277     for( i = 0 ; i < p_outpic->i_planes ; i++ )
278     {
279         memset( p_outpic->p[i].p_pixels, i > 0 ? 0x80 : 0x00,
280                 p_outpic->p[i].i_lines * p_outpic->p[i].i_pitch );
281     }
282
283     /* We can now call our visualization effects */
284     for( i = 0; i < p_sys->i_effect; i++ )
285     {
286 #define p_effect p_sys->effect[i]
287         if( p_effect->pf_run )
288         {
289             p_effect->pf_run( p_effect, p_aout, p_out_buf, p_outpic );
290         }
291 #undef p_effect
292     }
293
294     vout_DatePicture( p_sys->p_vout, p_outpic,
295                       ( p_in_buf->start_date + p_in_buf->end_date ) / 2 );
296
297     vout_DisplayPicture( p_sys->p_vout, p_outpic );
298 }
299
300 /*****************************************************************************
301  * Close: close the plugin
302  *****************************************************************************/
303 static void Close( vlc_object_t *p_this )
304 {
305     aout_filter_t * p_filter = (aout_filter_t *)p_this;
306     aout_filter_sys_t *p_sys = p_filter->p_sys;
307
308     int i;
309
310     if( p_filter->p_sys->p_vout )
311     {
312         vout_Request( p_filter, p_filter->p_sys->p_vout, 0, 0, 0, 0 );
313     }
314
315     /* Free the list */
316     for( i = 0; i < p_sys->i_effect; i++ )
317     {
318 #define p_effect p_sys->effect[i]
319         if( p_effect->psz_name )
320         {
321             free( p_effect->psz_name );
322         }
323         if( p_effect->psz_args )
324         {
325             free( p_effect->psz_args );
326         }
327         free( p_effect );
328 #undef p_effect
329     }
330
331     if( p_sys->effect )
332     {
333         free( p_sys->effect );
334     }
335
336     free( p_filter->p_sys );
337 }