1 /*****************************************************************************
2 * visual.c : Visualisation system
3 *****************************************************************************
4 * Copyright (C) 2002-2009 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
38 /*****************************************************************************
40 *****************************************************************************/
41 #define ELIST_TEXT N_( "Effects list" )
42 #define ELIST_LONGTEXT N_( \
43 "A list of visual effect, separated by commas.\n" \
44 "Current effects include: dummy, scope, spectrum, "\
45 "spectrometer and vuMeter." )
47 #define WIDTH_TEXT N_( "Video width" )
48 #define WIDTH_LONGTEXT N_( \
49 "The width of the effects video window, in pixels." )
51 #define HEIGHT_TEXT N_( "Video height" )
52 #define HEIGHT_LONGTEXT N_( \
53 "The height of the effects video window, in pixels." )
55 #define NBBANDS_TEXT N_( "More bands : 80 / 20" )
56 #define NBBANDS_LONGTEXT N_( \
57 "More bands for the spectrum analyzer : 80 if enabled else 20." )
58 #define SPNBBANDS_LONGTEXT N_( \
59 "More bands for the spectrometer : 80 if enabled else 20." )
61 #define SEPAR_TEXT N_( "Band separator" )
62 #define SEPAR_LONGTEXT N_( \
63 "Number of blank pixels between bands.")
65 #define AMP_TEXT N_( "Amplification" )
66 #define AMP_LONGTEXT N_( \
67 "This is a coefficient that modifies the height of the bands.")
69 #define PEAKS_TEXT N_( "Enable peaks" )
70 #define PEAKS_LONGTEXT N_( \
71 "Draw \"peaks\" in the spectrum analyzer." )
73 #define ORIG_TEXT N_( "Enable original graphic spectrum" )
74 #define ORIG_LONGTEXT N_( \
75 "Enable the \"flat\" spectrum analyzer in the spectrometer." )
77 #define BANDS_TEXT N_( "Enable bands" )
78 #define BANDS_LONGTEXT N_( \
79 "Draw bands in the spectrometer." )
81 #define BASE_TEXT N_( "Enable base" )
82 #define BASE_LONGTEXT N_( \
83 "Defines whether to draw the base of the bands." )
85 #define RADIUS_TEXT N_( "Base pixel radius" )
86 #define RADIUS_LONGTEXT N_( \
87 "Defines radius size in pixels, of base of bands(beginning)." )
89 #define SSECT_TEXT N_( "Spectral sections" )
90 #define SSECT_LONGTEXT N_( \
91 "Determines how many sections of spectrum will exist." )
93 #define PEAK_HEIGHT_TEXT N_( "Peak height" )
94 #define PEAK_HEIGHT_LONGTEXT N_( \
95 "Total pixel height of the peak items." )
97 #define PEAK_WIDTH_TEXT N_( "Peak extra width" )
98 #define PEAK_WIDTH_LONGTEXT N_( \
99 "Additions or subtractions of pixels on the peak width." )
101 #define COLOR1_TEXT N_( "V-plane color" )
102 #define COLOR1_LONGTEXT N_( \
103 "YUV-Color cube shifting across the V-plane ( 0 - 127 )." )
105 #define STARS_TEXT N_( "Number of stars" )
106 #define STARS_LONGTEXT N_( \
107 "Number of stars to draw with random effect." )
109 static int Open ( vlc_object_t * );
110 static void Close ( vlc_object_t * );
113 set_shortname( N_("Visualizer"))
114 set_category( CAT_AUDIO )
115 set_subcategory( SUBCAT_AUDIO_VISUAL )
116 set_description( N_("Visualizer filter") )
117 set_section( N_( "General") , NULL )
118 add_string("effect-list", "spectrum", NULL,
119 ELIST_TEXT, ELIST_LONGTEXT, true )
120 add_integer("effect-width",VOUT_WIDTH,NULL,
121 WIDTH_TEXT, WIDTH_LONGTEXT, false )
122 add_integer("effect-height" , VOUT_HEIGHT , NULL,
123 HEIGHT_TEXT, HEIGHT_LONGTEXT, false )
124 set_section( N_("Spectrum analyser") , NULL )
125 add_obsolete_integer( "visual-nbbands" ) /* Since 1.0.0 */
126 add_bool("visual-80-bands", 1, NULL,
127 NBBANDS_TEXT, NBBANDS_LONGTEXT, true );
128 add_obsolete_integer( "visual-separ" ) /* Since 1.0.0 */
129 add_obsolete_integer( "visual-amp" ) /* Since 1.0.0 */
130 add_bool("visual-peaks", true, NULL,
131 PEAKS_TEXT, PEAKS_LONGTEXT, true )
132 set_section( N_("Spectrometer") , NULL )
133 add_bool("spect-show-original", false, NULL,
134 ORIG_TEXT, ORIG_LONGTEXT, true )
135 add_bool("spect-show-base", true, NULL,
136 BASE_TEXT, BASE_LONGTEXT, true )
137 add_integer("spect-radius", 42, NULL,
138 RADIUS_TEXT, RADIUS_LONGTEXT, true )
139 add_integer("spect-sections", 3, NULL,
140 SSECT_TEXT, SSECT_LONGTEXT, true )
141 add_integer("spect-color", 80, NULL,
142 COLOR1_TEXT, COLOR1_LONGTEXT, true )
143 add_bool("spect-show-bands", true, NULL,
144 BANDS_TEXT, BANDS_LONGTEXT, true );
145 add_obsolete_integer( "spect-nbbands" ) /* Since 1.0.0 */
146 add_bool("spect-80-bands", 1, NULL,
147 NBBANDS_TEXT, SPNBBANDS_LONGTEXT, true )
148 add_integer("spect-separ", 1, NULL,
149 SEPAR_TEXT, SEPAR_LONGTEXT, true )
150 add_integer("spect-amp", 8, NULL,
151 AMP_TEXT, AMP_LONGTEXT, true )
152 add_bool("spect-show-peaks", true, NULL,
153 PEAKS_TEXT, PEAKS_LONGTEXT, true )
154 add_integer("spect-peak-width", 61, NULL,
155 PEAK_WIDTH_TEXT, PEAK_WIDTH_LONGTEXT, true )
156 add_integer("spect-peak-height", 1, NULL,
157 PEAK_HEIGHT_TEXT, PEAK_HEIGHT_LONGTEXT, true )
158 set_capability( "visualization", 0 )
159 set_callbacks( Open, Close )
160 add_shortcut( "visualizer")
164 /*****************************************************************************
166 *****************************************************************************/
167 static void DoWork( aout_instance_t *, aout_filter_t *,
168 aout_buffer_t *, aout_buffer_t * );
169 static int FilterCallback( vlc_object_t *, char const *,
170 vlc_value_t, vlc_value_t, void * );
173 const char *psz_name;
174 int (*pf_run)( visual_effect_t *, aout_instance_t *,
175 aout_buffer_t *, picture_t *);
178 { "scope", scope_Run },
179 { "vuMeter", vuMeter_Run },
180 { "spectrum", spectrum_Run },
181 { "spectrometer", spectrometer_Run },
182 { "dummy", dummy_Run},
186 /*****************************************************************************
187 * Open: open the visualizer
188 *****************************************************************************/
189 static int Open( vlc_object_t *p_this )
191 aout_filter_t *p_filter = (aout_filter_t *)p_this;
192 aout_filter_sys_t *p_sys;
194 char *psz_effects, *psz_parser;
197 if( ( p_filter->input.i_format != VLC_CODEC_FL32 &&
198 p_filter->input.i_format != VLC_CODEC_FI32 ) )
203 p_sys = p_filter->p_sys = malloc( sizeof( aout_filter_sys_t ) );
207 p_sys->i_height = config_GetInt( p_filter , "effect-height");
208 p_sys->i_width = config_GetInt( p_filter , "effect-width");
210 if( p_sys->i_height < 400 ) p_sys->i_height = 400;
211 if( p_sys->i_width < 532 ) p_sys->i_width = 532;
212 if( (p_sys->i_height % 2 ) != 0 ) p_sys->i_height--;
213 if( (p_sys->i_width % 2 ) != 0 ) p_sys->i_width--;
216 p_sys->effect = NULL;
218 /* Parse the effect list */
219 psz_parser = psz_effects = var_CreateGetString( p_filter, "effect-list" );
220 var_AddCallback( p_filter, "effect-list", FilterCallback, NULL );
222 while( psz_parser && *psz_parser != '\0' )
224 visual_effect_t *p_effect;
227 p_effect = malloc( sizeof( visual_effect_t ) );
230 p_effect->i_width = p_sys->i_width;
231 p_effect->i_height= p_sys->i_height;
232 p_effect->i_nb_chans = aout_FormatNbChannels( &p_filter->input);
233 p_effect->i_idx_left = 0;
234 p_effect->i_idx_right = __MIN( 1, p_effect->i_nb_chans-1 );
236 p_effect->psz_args = NULL;
237 p_effect->p_data = NULL;
239 p_effect->pf_run = NULL;
240 p_effect->psz_name = NULL;
242 for( i = 0; pf_effect_run[i].psz_name != NULL; i++ )
244 if( !strncasecmp( psz_parser,
245 pf_effect_run[i].psz_name,
246 strlen( pf_effect_run[i].psz_name ) ) )
248 p_effect->pf_run = pf_effect_run[i].pf_run;
249 p_effect->psz_name = pf_effect_run[i].psz_name;
254 if( p_effect->psz_name )
256 psz_parser += strlen( p_effect->psz_name );
258 if( *psz_parser == '{' )
264 if( ( psz_eoa = strchr( psz_parser, '}') ) == NULL )
266 msg_Err( p_filter, "unable to parse effect list. Aborting");
271 strndup( psz_parser, psz_eoa - psz_parser);
273 TAB_APPEND( p_sys->i_effect, p_sys->effect, p_effect );
277 msg_Err( p_filter, "unknown visual effect: %s", psz_parser );
281 if( strchr( psz_parser, ',' ) )
283 psz_parser = strchr( psz_parser, ',' ) + 1;
285 else if( strchr( psz_parser, ':' ) )
287 psz_parser = strchr( psz_parser, ':' ) + 1;
297 if( !p_sys->i_effect )
299 msg_Err( p_filter, "no effects found" );
304 /* Open the video output */
305 memset( &fmt, 0, sizeof(video_format_t) );
307 fmt.i_width = fmt.i_visible_width = p_sys->i_width;
308 fmt.i_height = fmt.i_visible_height = p_sys->i_height;
309 fmt.i_chroma = VLC_CODEC_I420;
310 fmt.i_aspect = VOUT_ASPECT_FACTOR * p_sys->i_width/p_sys->i_height;
311 fmt.i_sar_num = fmt.i_sar_den = 1;
313 p_sys->p_vout = aout_filter_RequestVout( p_filter, NULL, &fmt );
314 if( p_sys->p_vout == NULL )
316 msg_Err( p_filter, "no suitable vout module" );
317 for( int i = 0; i < p_sys->i_effect; i++ )
319 free( p_sys->effect[i]->psz_args );
320 free( p_sys->effect[i] );
322 free( p_sys->effect );
327 p_filter->pf_do_work = DoWork;
328 p_filter->b_in_place= 1;
333 /*****************************************************************************
334 * DoWork: convert a buffer
335 *****************************************************************************
336 * Audio part pasted from trivial.c
337 ****************************************************************************/
338 static void DoWork( aout_instance_t *p_aout, aout_filter_t *p_filter,
339 aout_buffer_t *p_in_buf, aout_buffer_t *p_out_buf )
341 aout_filter_sys_t *p_sys = p_filter->p_sys;
345 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
346 p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes *
347 aout_FormatNbChannels( &p_filter->output ) /
348 aout_FormatNbChannels( &p_filter->input );
350 /* First, get a new picture */
351 while( ( p_outpic = vout_CreatePicture( p_sys->p_vout, 0, 0, 3 ) ) == NULL)
353 if( !vlc_object_alive (p_aout) )
357 msleep( VOUT_OUTMEM_SLEEP );
360 /* Blank the picture */
361 for( i = 0 ; i < p_outpic->i_planes ; i++ )
363 memset( p_outpic->p[i].p_pixels, i > 0 ? 0x80 : 0x00,
364 p_outpic->p[i].i_visible_lines * p_outpic->p[i].i_pitch );
367 /* We can now call our visualization effects */
368 for( i = 0; i < p_sys->i_effect; i++ )
370 #define p_effect p_sys->effect[i]
371 if( p_effect->pf_run )
373 p_effect->pf_run( p_effect, p_aout, p_out_buf, p_outpic );
378 p_outpic->date = ( p_in_buf->start_date + p_in_buf->end_date ) / 2;
380 vout_DisplayPicture( p_sys->p_vout, p_outpic );
383 /*****************************************************************************
384 * Close: close the plugin
385 *****************************************************************************/
386 static void Close( vlc_object_t *p_this )
388 aout_filter_t * p_filter = (aout_filter_t *)p_this;
389 aout_filter_sys_t *p_sys = p_filter->p_sys;
393 if( p_filter->p_sys->p_vout )
395 aout_filter_RequestVout( p_filter, p_filter->p_sys->p_vout, 0 );
399 for( i = 0; i < p_sys->i_effect; i++ )
401 #define p_effect p_sys->effect[i]
402 if( !strncmp( p_effect->psz_name, "spectrum", strlen( "spectrum" ) ) )
404 free( ( ( spectrum_data * )p_effect->p_data )->peaks );
405 free( ( ( spectrum_data * )p_effect->p_data )->prev_heights );
407 free( p_effect->p_data );
408 free( p_effect->psz_args );
413 free( p_sys->effect );
414 free( p_filter->p_sys );
417 /*****************************************************************************
418 * FilterCallback: called when changing the deinterlace method on the fly.
419 *****************************************************************************/
420 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
421 vlc_value_t oldval, vlc_value_t newval,
424 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
425 VLC_UNUSED(p_data); VLC_UNUSED(newval);
426 aout_filter_t *p_filter = (aout_filter_t *)p_this;
427 /* restart this baby */
428 msg_Dbg( p_filter, "we should restart the visual filter" );