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