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