1 /*****************************************************************************
2 * visual.c : Visualisation system
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
7 * Authors: Clément Stenac <zorglub@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
30 #include "audio_output.h"
31 #include "aout_internal.h"
35 /*****************************************************************************
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" )
43 #define WIDTH_TEXT N_( "Video width" )
44 #define WIDTH_LONGTEXT N_( \
45 "The width of the effects video window, in pixels." )
47 #define HEIGHT_TEXT N_( "Video height" )
48 #define HEIGHT_LONGTEXT N_( \
49 "The height of the effects video window, in pixels." )
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." )
55 #define SEPAR_TEXT N_( "Band separator" )
56 #define SEPAR_LONGTEXT N_( \
57 "Number of blank pixels between bands.")
59 #define AMP_TEXT N_( "Amplification" )
60 #define AMP_LONGTEXT N_( \
61 "This is a coefficient that modifies the height of the bands.")
63 #define PEAKS_TEXT N_( "Enable peaks" )
64 #define PEAKS_LONGTEXT N_( \
65 "Defines whether to draw peaks." )
67 #define STARS_TEXT N_( "Number of stars" )
68 #define STARS_LONGTEXT N_( \
69 "Defines the number of stars to draw with random effect." )
71 static int Open ( vlc_object_t * );
72 static void Close ( vlc_object_t * );
75 set_category( CAT_AUDIO );
76 set_subcategory( SUBCAT_AUDIO_VISUAL );
77 set_description( _("Visualizer filter") );
78 set_section( N_( "General") , NULL );
79 add_string("effect-list", "spectrum", NULL,
80 ELIST_TEXT, ELIST_LONGTEXT, VLC_TRUE );
81 add_integer("effect-width",VOUT_WIDTH,NULL,
82 WIDTH_TEXT, WIDTH_LONGTEXT, VLC_FALSE );
83 add_integer("effect-height" , VOUT_HEIGHT , NULL,
84 HEIGHT_TEXT, HEIGHT_LONGTEXT, VLC_FALSE );
85 set_section( N_("Spectrum analyser") , NULL );
86 add_integer("visual-nbbands", 80, NULL,
87 NBBANDS_TEXT, NBBANDS_LONGTEXT, VLC_FALSE );
88 add_integer("visual-separ", 1, NULL,
89 SEPAR_TEXT, SEPAR_LONGTEXT, VLC_FALSE );
90 add_integer("visual-amp", 3, NULL,
91 AMP_TEXT, AMP_LONGTEXT, VLC_FALSE );
92 add_bool("visual-peaks", VLC_TRUE, NULL,
93 PEAKS_TEXT, PEAKS_LONGTEXT, VLC_FALSE );
94 set_section( N_( "Random effect") , NULL );
95 add_integer("visual-stars", 200, NULL,
96 STARS_TEXT, STARS_LONGTEXT, VLC_FALSE );
97 set_capability( "visualization", 0 );
98 set_callbacks( Open, Close );
99 add_shortcut( "visualizer");
103 /*****************************************************************************
105 *****************************************************************************/
106 static void DoWork( aout_instance_t *, aout_filter_t *,
107 aout_buffer_t *, aout_buffer_t * );
108 static int FilterCallback( vlc_object_t *, char const *,
109 vlc_value_t, vlc_value_t, void * );
113 int (*pf_run)( visual_effect_t *, aout_instance_t *,
114 aout_buffer_t *, picture_t *);
117 { "scope", scope_Run },
118 { "spectrum", spectrum_Run },
119 { "random", random_Run},
120 { "dummy", dummy_Run},
124 /*****************************************************************************
125 * Open: open the visualizer
126 *****************************************************************************/
127 static int Open( vlc_object_t *p_this )
129 aout_filter_t *p_filter = (aout_filter_t *)p_this;
130 aout_filter_sys_t *p_sys;
133 char *psz_effects, *psz_parser;
135 if( ( p_filter->input.i_format != VLC_FOURCC('f','l','3','2') &&
136 p_filter->input.i_format != VLC_FOURCC('f','i','3','2') ) )
141 p_sys = p_filter->p_sys = malloc( sizeof( aout_filter_sys_t ) );
144 msg_Err( p_filter, "out of memory" );
148 p_sys->i_height = config_GetInt( p_filter , "effect-height");
149 p_sys->i_width = config_GetInt( p_filter , "effect-width");
151 if( p_sys->i_height < 20 ) p_sys->i_height = 20;
152 if( p_sys->i_width < 20 ) p_sys->i_width = 20;
153 if( (p_sys->i_height % 2 ) != 0 ) p_sys->i_height--;
154 if( (p_sys->i_width % 2 ) != 0 ) p_sys->i_width--;
157 p_sys->effect = NULL;
159 /* Parse the effect list */
160 var_Create( p_filter, "effect-list", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
161 var_Get( p_filter, "effect-list", &val);
162 psz_parser = psz_effects = strdup( val.psz_string );
163 free( val.psz_string );
164 msg_Dbg( p_filter , "Building list of effects" );
166 var_AddCallback( p_filter, "effect-list", FilterCallback, NULL );
168 while( psz_parser && *psz_parser != '\0' )
170 visual_effect_t *p_effect;
173 p_effect = malloc( sizeof( visual_effect_t ) );
174 p_effect->i_width = p_sys->i_width;
175 p_effect->i_height= p_sys->i_height;
176 p_effect->i_nb_chans = aout_FormatNbChannels( &p_filter->input);
177 p_effect->psz_args = NULL;
178 p_effect->p_data = NULL;
180 p_effect->pf_run = NULL;
181 p_effect->psz_name = NULL;
183 for( i = 0; pf_effect_run[i].psz_name != NULL; i++ )
185 if( !strncasecmp( psz_parser,
186 pf_effect_run[i].psz_name,
187 strlen( pf_effect_run[i].psz_name ) ) )
189 p_effect->pf_run = pf_effect_run[i].pf_run;
190 p_effect->psz_name = strdup( pf_effect_run[i].psz_name );
195 if( p_effect->psz_name )
197 psz_parser += strlen( p_effect->psz_name );
199 if( *psz_parser == '{' )
205 if( ( psz_eoa = strchr( psz_parser, '}') ) == NULL )
207 msg_Err( p_filter, "Unable to parse effect list. Aborting");
211 strndup( psz_parser, psz_eoa - psz_parser);
213 TAB_APPEND( p_sys->i_effect, p_sys->effect, p_effect );
217 msg_Err( p_filter, "unknown visual effect: %s", psz_parser );
221 if( strchr( psz_parser, ',' ) )
223 psz_parser = strchr( psz_parser, ',' ) + 1;
225 else if( strchr( psz_parser, ':' ) )
227 psz_parser = strchr( psz_parser, ':' ) + 1;
240 if( !p_sys->i_effect )
242 msg_Err( p_filter, "no effects found" );
247 /* Open the video output */
249 vout_Request( p_filter, NULL,
250 p_sys->i_width, p_sys->i_height,
251 VLC_FOURCC('I','4','2','0'),
252 VOUT_ASPECT_FACTOR * p_sys->i_width/p_sys->i_height );
254 if( p_sys->p_vout == NULL )
256 msg_Err( p_filter, "no suitable vout module" );
261 p_filter->pf_do_work = DoWork;
262 p_filter->b_in_place= 1;
264 msg_Dbg( p_filter,"Visualizer initialized");
268 /*****************************************************************************
269 * DoWork: convert a buffer
270 *****************************************************************************
271 * Audio part pasted from trivial.c
272 ****************************************************************************/
273 static void DoWork( aout_instance_t *p_aout, aout_filter_t *p_filter,
274 aout_buffer_t *p_in_buf, aout_buffer_t *p_out_buf )
276 aout_filter_sys_t *p_sys = p_filter->p_sys;
280 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
281 p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes *
282 aout_FormatNbChannels( &p_filter->output ) /
283 aout_FormatNbChannels( &p_filter->input );
285 /* First, get a new picture */
286 while( ( p_outpic = vout_CreatePicture( p_sys->p_vout, 0, 0, 3 ) ) == NULL)
292 msleep( VOUT_OUTMEM_SLEEP );
295 /* Blank the picture */
296 for( i = 0 ; i < p_outpic->i_planes ; i++ )
298 memset( p_outpic->p[i].p_pixels, i > 0 ? 0x80 : 0x00,
299 p_outpic->p[i].i_visible_lines * p_outpic->p[i].i_pitch );
302 /* We can now call our visualization effects */
303 for( i = 0; i < p_sys->i_effect; i++ )
305 #define p_effect p_sys->effect[i]
306 if( p_effect->pf_run )
308 p_effect->pf_run( p_effect, p_aout, p_out_buf, p_outpic );
313 vout_DatePicture( p_sys->p_vout, p_outpic,
314 ( p_in_buf->start_date + p_in_buf->end_date ) / 2 );
316 vout_DisplayPicture( p_sys->p_vout, p_outpic );
319 /*****************************************************************************
320 * Close: close the plugin
321 *****************************************************************************/
322 static void Close( vlc_object_t *p_this )
324 aout_filter_t * p_filter = (aout_filter_t *)p_this;
325 aout_filter_sys_t *p_sys = p_filter->p_sys;
329 if( p_filter->p_sys->p_vout )
331 vout_Request( p_filter, p_filter->p_sys->p_vout, 0, 0, 0, 0 );
335 for( i = 0; i < p_sys->i_effect; i++ )
337 #define p_effect p_sys->effect[i]
338 if( p_effect->psz_name )
340 free( p_effect->psz_name );
342 if( p_effect->psz_args )
344 free( p_effect->psz_args );
352 free( p_sys->effect );
355 free( p_filter->p_sys );
358 /*****************************************************************************
359 * FilterCallback: called when changing the deinterlace method on the fly.
360 *****************************************************************************/
361 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
362 vlc_value_t oldval, vlc_value_t newval,
365 aout_filter_t *p_filter = (aout_filter_t *)p_this;
366 /* restart this baby */
367 msg_Dbg( p_filter, "We should restart the visual filter" );