]> git.sesse.net Git - vlc/blob - modules/visualization/visual/visual.c
c82621ab15feb029a85a65c499e368ba759ab137
[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 #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, 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 #define SPNBBANDS_LONGTEXT N_( \
55       "Number of bands used by the spectrometer, from 20 to 80." )
56
57 #define SEPAR_TEXT N_( "Band separator" )
58 #define SEPAR_LONGTEXT N_( \
59         "Number of blank pixels between bands.")
60
61 #define AMP_TEXT N_( "Amplification" )
62 #define AMP_LONGTEXT N_( \
63         "This is a coefficient that modifies the height of the bands.")
64
65 #define PEAKS_TEXT N_( "Enable peaks" )
66 #define PEAKS_LONGTEXT N_( \
67         "Draw \"peaks\" in the spectrum analyzer." )
68
69 #define ORIG_TEXT N_( "Enable original graphic spectrum" )
70 #define ORIG_LONGTEXT N_( \
71         "Enable the \"flat\" spectrum analyzer in the spectrometer." )
72
73 #define BANDS_TEXT N_( "Enable bands" )
74 #define BANDS_LONGTEXT N_( \
75         "Draw bands in the spectrometer." )
76
77 #define BASE_TEXT N_( "Enable base" )
78 #define BASE_LONGTEXT N_( \
79         "Defines whether to draw the base of the bands." )
80
81 #define RADIUS_TEXT N_( "Base pixel radius" )
82 #define RADIUS_LONGTEXT N_( \
83         "Defines radius size in pixels, of base of bands(beginning)." )
84
85 #define SSECT_TEXT N_( "Spectral sections" )
86 #define SSECT_LONGTEXT N_( \
87         "Determines how many sections of spectrum will exist." )
88
89 #define PEAK_HEIGHT_TEXT N_( "Peak height" )
90 #define PEAK_HEIGHT_LONGTEXT N_( \
91         "Total pixel height of the peak items." )
92
93 #define PEAK_WIDTH_TEXT N_( "Peak extra width" )
94 #define PEAK_WIDTH_LONGTEXT N_( \
95         "Additions or subtractions of pixels on the peak width." )
96
97 #define COLOR1_TEXT N_( "V-plane color" )
98 #define COLOR1_LONGTEXT N_( \
99         "YUV-Color cube shifting across the V-plane ( 0 - 127 )." )
100
101 #define STARS_TEXT N_( "Number of stars" )
102 #define STARS_LONGTEXT N_( \
103         "Number of stars to draw with random effect." )
104
105 static int  Open         ( vlc_object_t * );
106 static void Close        ( vlc_object_t * );
107
108 vlc_module_begin();
109     set_shortname( _("Visualizer"));
110     set_category( CAT_AUDIO );
111     set_subcategory( SUBCAT_AUDIO_VISUAL );
112     set_description( _("Visualizer filter") );
113     set_section( N_( "General") , NULL );
114     add_string("effect-list", "spectrum", NULL,
115             ELIST_TEXT, ELIST_LONGTEXT, VLC_TRUE );
116     add_integer("effect-width",VOUT_WIDTH,NULL,
117              WIDTH_TEXT, WIDTH_LONGTEXT, VLC_FALSE );
118     add_integer("effect-height" , VOUT_HEIGHT , NULL,
119              HEIGHT_TEXT, HEIGHT_LONGTEXT, VLC_FALSE );
120     set_section( N_("Spectrum analyser") , NULL );
121     add_integer("visual-nbbands", 80, NULL,
122              NBBANDS_TEXT, NBBANDS_LONGTEXT, VLC_TRUE );
123     add_integer("visual-separ", 1, NULL,
124              SEPAR_TEXT, SEPAR_LONGTEXT, VLC_TRUE );
125     add_integer("visual-amp", 3, NULL,
126              AMP_TEXT, AMP_LONGTEXT, VLC_TRUE );
127     add_bool("visual-peaks", VLC_TRUE, NULL,
128              PEAKS_TEXT, PEAKS_LONGTEXT, VLC_TRUE );
129     set_section( N_("Spectrometer") , NULL );
130     add_bool("spect-show-original", VLC_FALSE, NULL,
131              ORIG_TEXT, ORIG_LONGTEXT, VLC_TRUE );
132     add_bool("spect-show-base", VLC_TRUE, NULL,
133              BASE_TEXT, BASE_LONGTEXT, VLC_TRUE );
134     add_integer("spect-radius", 42, NULL,
135              RADIUS_TEXT, RADIUS_LONGTEXT, VLC_TRUE );
136     add_integer("spect-sections", 3, NULL,
137              SSECT_TEXT, SSECT_LONGTEXT, VLC_TRUE );
138     add_integer("spect-color", 80, NULL,
139              COLOR1_TEXT, COLOR1_LONGTEXT, VLC_TRUE );
140     add_bool("spect-show-bands", VLC_TRUE, NULL,
141              BANDS_TEXT, BANDS_LONGTEXT, VLC_TRUE );
142     add_integer("spect-nbbands", 32, NULL,
143              NBBANDS_TEXT, SPNBBANDS_LONGTEXT, VLC_TRUE );
144     add_integer("spect-separ", 1, NULL,
145              SEPAR_TEXT, SEPAR_LONGTEXT, VLC_TRUE );
146     add_integer("spect-amp", 8, NULL,
147              AMP_TEXT, AMP_LONGTEXT, VLC_TRUE );
148     add_bool("spect-show-peaks", VLC_TRUE, NULL,
149              PEAKS_TEXT, PEAKS_LONGTEXT, VLC_TRUE );
150     add_integer("spect-peak-width", 61, NULL,
151              PEAK_WIDTH_TEXT, PEAK_WIDTH_LONGTEXT, VLC_TRUE );
152     add_integer("spect-peak-height", 1, NULL,
153              PEAK_HEIGHT_TEXT, PEAK_HEIGHT_LONGTEXT, VLC_TRUE );
154     set_capability( "visualization", 0 );
155     set_callbacks( Open, Close );
156     add_shortcut( "visualizer");
157 vlc_module_end();
158
159
160 /*****************************************************************************
161  * Local prototypes
162  *****************************************************************************/
163 static void DoWork( aout_instance_t *, aout_filter_t *,
164                     aout_buffer_t *, aout_buffer_t * );
165 static int FilterCallback( vlc_object_t *, char const *,
166                            vlc_value_t, vlc_value_t, void * );
167 static struct
168 {
169     char *psz_name;
170     int  (*pf_run)( visual_effect_t *, aout_instance_t *,
171                     aout_buffer_t *, picture_t *);
172 } pf_effect_run[]=
173 {
174     { "scope",      scope_Run },
175     { "spectrum",   spectrum_Run },
176     { "spectrometer",   spectrometer_Run },
177     { "dummy",      dummy_Run},
178     { NULL,         dummy_Run}
179 };
180
181 /*****************************************************************************
182  * Open: open the visualizer
183  *****************************************************************************/
184 static int Open( vlc_object_t *p_this )
185 {
186     aout_filter_t     *p_filter = (aout_filter_t *)p_this;
187     aout_filter_sys_t *p_sys;
188     vlc_value_t        val;
189
190     char *psz_effects, *psz_parser;
191     video_format_t fmt = {0};
192
193     if( ( p_filter->input.i_format != VLC_FOURCC('f','l','3','2') &&
194           p_filter->input.i_format != VLC_FOURCC('f','i','3','2') ) )
195     {
196         return VLC_EGENERIC;
197     }
198
199     p_sys = p_filter->p_sys = malloc( sizeof( aout_filter_sys_t ) );
200     if( p_sys == NULL )
201     {
202         msg_Err( p_filter, "out of memory" );
203         return VLC_EGENERIC;
204     }
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 < 20 ) p_sys->i_height =  20;
210     if( p_sys->i_width  < 20 ) p_sys->i_width  =  20;
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     var_Create( p_filter, "effect-list", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
219     var_Get( p_filter, "effect-list", &val);
220     psz_parser = psz_effects = strdup( val.psz_string );
221     free( val.psz_string );
222
223     var_AddCallback( p_filter, "effect-list", FilterCallback, NULL );
224
225     while( psz_parser && *psz_parser != '\0' )
226     {
227         visual_effect_t *p_effect;
228         int  i;
229
230         p_effect = malloc( sizeof( visual_effect_t ) );
231         p_effect->i_width = p_sys->i_width;
232         p_effect->i_height= p_sys->i_height;
233         p_effect->i_nb_chans = aout_FormatNbChannels( &p_filter->input);
234         p_effect->psz_args = NULL;
235         p_effect->p_data = NULL;
236
237         p_effect->pf_run = NULL;
238         p_effect->psz_name = NULL;
239
240         for( i = 0; pf_effect_run[i].psz_name != NULL; i++ )
241         {
242             if( !strncasecmp( psz_parser,
243                               pf_effect_run[i].psz_name,
244                               strlen( pf_effect_run[i].psz_name ) ) )
245             {
246                 p_effect->pf_run = pf_effect_run[i].pf_run;
247                 p_effect->psz_name = strdup( pf_effect_run[i].psz_name );
248                 break;
249             }
250         }
251
252         if( p_effect->psz_name )
253         {
254             psz_parser += strlen( p_effect->psz_name );
255
256             if( *psz_parser == '{' )
257             {
258                 char *psz_eoa;
259
260                 psz_parser++;
261
262                 if( ( psz_eoa = strchr( psz_parser, '}') ) == NULL )
263                 {
264                    msg_Err( p_filter, "unable to parse effect list. Aborting");
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     if( psz_effects )
293     {
294         free( psz_effects );
295     }
296
297     if( !p_sys->i_effect )
298     {
299         msg_Err( p_filter, "no effects found" );
300         free( p_sys );
301         return VLC_EGENERIC;
302     }
303
304     /* Open the video output */
305     fmt.i_width = fmt.i_visible_width = p_sys->i_width;
306     fmt.i_height = fmt.i_visible_height = p_sys->i_height;
307     fmt.i_chroma = VLC_FOURCC('I','4','2','0');
308     fmt.i_aspect = VOUT_ASPECT_FACTOR * p_sys->i_width/p_sys->i_height;
309     fmt.i_sar_num = fmt.i_sar_den = 1;
310
311     p_sys->p_vout = vout_Request( p_filter, NULL, &fmt );
312     if( p_sys->p_vout == NULL )
313     {
314         msg_Err( p_filter, "no suitable vout module" );
315         free( p_sys );
316         return VLC_EGENERIC;
317     }
318
319     p_filter->pf_do_work = DoWork;
320     p_filter->b_in_place= 1;
321
322     return VLC_SUCCESS;
323 }
324
325 /*****************************************************************************
326  * DoWork: convert a buffer
327  *****************************************************************************
328  * Audio part pasted from trivial.c
329  ****************************************************************************/
330 static void DoWork( aout_instance_t *p_aout, aout_filter_t *p_filter,
331                     aout_buffer_t *p_in_buf, aout_buffer_t *p_out_buf )
332 {
333     aout_filter_sys_t *p_sys = p_filter->p_sys;
334     picture_t *p_outpic;
335     int i;
336
337     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
338     p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes *
339                             aout_FormatNbChannels( &p_filter->output ) /
340                             aout_FormatNbChannels( &p_filter->input );
341
342     /* First, get a new picture */
343     while( ( p_outpic = vout_CreatePicture( p_sys->p_vout, 0, 0, 3 ) ) == NULL)
344     {
345         if( p_aout->b_die )
346         {
347             return;
348         }
349         msleep( VOUT_OUTMEM_SLEEP );
350     }
351
352     /* Blank the picture */
353     for( i = 0 ; i < p_outpic->i_planes ; i++ )
354     {
355         memset( p_outpic->p[i].p_pixels, i > 0 ? 0x80 : 0x00,
356                 p_outpic->p[i].i_visible_lines * p_outpic->p[i].i_pitch );
357     }
358
359     /* We can now call our visualization effects */
360     for( i = 0; i < p_sys->i_effect; i++ )
361     {
362 #define p_effect p_sys->effect[i]
363         if( p_effect->pf_run )
364         {
365             p_effect->pf_run( p_effect, p_aout, p_out_buf, p_outpic );
366         }
367 #undef p_effect
368     }
369
370     vout_DatePicture( p_sys->p_vout, p_outpic,
371                       ( p_in_buf->start_date + p_in_buf->end_date ) / 2 );
372
373     vout_DisplayPicture( p_sys->p_vout, p_outpic );
374 }
375
376 /*****************************************************************************
377  * Close: close the plugin
378  *****************************************************************************/
379 static void Close( vlc_object_t *p_this )
380 {
381     aout_filter_t * p_filter = (aout_filter_t *)p_this;
382     aout_filter_sys_t *p_sys = p_filter->p_sys;
383
384     int i;
385
386     if( p_filter->p_sys->p_vout )
387     {
388         vout_Request( p_filter, p_filter->p_sys->p_vout, 0 );
389     }
390
391     /* Free the list */
392     for( i = 0; i < p_sys->i_effect; i++ )
393     {
394 #define p_effect p_sys->effect[i]
395         if( p_effect->psz_name )
396         {
397             free( p_effect->psz_name );
398         }
399         if( p_effect->psz_args )
400         {
401             free( p_effect->psz_args );
402         }
403         free( p_effect );
404 #undef p_effect
405     }
406
407     if( p_sys->effect )
408     {
409         free( p_sys->effect );
410     }
411
412     free( p_filter->p_sys );
413 }
414
415 /*****************************************************************************
416  * FilterCallback: called when changing the deinterlace method on the fly.
417  *****************************************************************************/
418 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
419                            vlc_value_t oldval, vlc_value_t newval,
420                            void *p_data )
421 {
422     aout_filter_t     *p_filter = (aout_filter_t *)p_this;
423     /* restart this baby */
424     msg_Dbg( p_filter, "we should restart the visual filter" );
425     return VLC_SUCCESS;
426 }
427