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>
35 #include <vlc_filter.h>
39 /*****************************************************************************
41 *****************************************************************************/
42 #define ELIST_TEXT N_( "Effects list" )
43 #define ELIST_LONGTEXT N_( \
44 "A list of visual effect, separated by commas.\n" \
45 "Current effects include: dummy, scope, spectrum, "\
46 "spectrometer and vuMeter." )
48 #define WIDTH_TEXT N_( "Video width" )
49 #define WIDTH_LONGTEXT N_( \
50 "The width of the effects video window, in pixels." )
52 #define HEIGHT_TEXT N_( "Video height" )
53 #define HEIGHT_LONGTEXT N_( \
54 "The height of the effects video window, in pixels." )
56 #define NBBANDS_TEXT N_( "Show 80 bands instead of 20" )
57 #define SPNBBANDS_LONGTEXT N_( \
58 "More bands for the spectrometer : 80 if enabled else 20." )
60 #define SEPAR_TEXT N_( "Number of blank pixels between bands.")
62 #define AMP_TEXT N_( "Amplification" )
63 #define AMP_LONGTEXT N_( \
64 "This is a coefficient that modifies the height of the bands.")
66 #define PEAKS_TEXT N_( "Draw peaks in the analyzer" )
68 #define ORIG_TEXT N_( "Enable original graphic spectrum" )
69 #define ORIG_LONGTEXT N_( \
70 "Enable the \"flat\" spectrum analyzer in the spectrometer." )
72 #define BANDS_TEXT N_( "Draw bands in the spectrometer" )
74 #define BASE_TEXT N_( "Draw the base of the bands" )
76 #define RADIUS_TEXT N_( "Base pixel radius" )
77 #define RADIUS_LONGTEXT N_( \
78 "Defines radius size in pixels, of base of bands(beginning)." )
80 #define SSECT_TEXT N_( "Spectral sections" )
81 #define SSECT_LONGTEXT N_( \
82 "Determines how many sections of spectrum will exist." )
84 #define PEAK_HEIGHT_TEXT N_( "Peak height" )
85 #define PEAK_HEIGHT_LONGTEXT N_( \
86 "Total pixel height of the peak items." )
88 #define PEAK_WIDTH_TEXT N_( "Peak extra width" )
89 #define PEAK_WIDTH_LONGTEXT N_( \
90 "Additions or subtractions of pixels on the peak width." )
92 #define COLOR1_TEXT N_( "V-plane color" )
93 #define COLOR1_LONGTEXT N_( \
94 "YUV-Color cube shifting across the V-plane ( 0 - 127 )." )
96 static int Open ( vlc_object_t * );
97 static void Close ( vlc_object_t * );
100 set_shortname( N_("Visualizer"))
101 set_category( CAT_AUDIO )
102 set_subcategory( SUBCAT_AUDIO_VISUAL )
103 set_description( N_("Visualizer filter") )
104 set_section( N_( "General") , NULL )
105 add_string("effect-list", "spectrum",
106 ELIST_TEXT, ELIST_LONGTEXT, true )
107 add_integer("effect-width",VOUT_WIDTH,
108 WIDTH_TEXT, WIDTH_LONGTEXT, false )
109 add_integer("effect-height" , VOUT_HEIGHT ,
110 HEIGHT_TEXT, HEIGHT_LONGTEXT, false )
111 set_section( N_("Spectrum analyser") , NULL )
112 add_obsolete_integer( "visual-nbbands" ) /* Since 1.0.0 */
113 add_bool("visual-80-bands", true,
114 NBBANDS_TEXT, NBBANDS_TEXT, true );
115 add_obsolete_integer( "visual-separ" ) /* Since 1.0.0 */
116 add_obsolete_integer( "visual-amp" ) /* Since 1.0.0 */
117 add_bool("visual-peaks", true,
118 PEAKS_TEXT, PEAKS_TEXT, true )
119 set_section( N_("Spectrometer") , NULL )
120 add_bool("spect-show-original", false,
121 ORIG_TEXT, ORIG_LONGTEXT, true )
122 add_bool("spect-show-base", true,
123 BASE_TEXT, BASE_TEXT, true )
124 add_integer("spect-radius", 42,
125 RADIUS_TEXT, RADIUS_LONGTEXT, true )
126 add_integer("spect-sections", 3,
127 SSECT_TEXT, SSECT_LONGTEXT, true )
128 add_integer("spect-color", 80,
129 COLOR1_TEXT, COLOR1_LONGTEXT, true )
130 add_bool("spect-show-bands", true,
131 BANDS_TEXT, BANDS_TEXT, true );
132 add_obsolete_integer( "spect-nbbands" ) /* Since 1.0.0 */
133 add_bool("spect-80-bands", true,
134 NBBANDS_TEXT, NBBANDS_TEXT, true )
135 add_integer("spect-separ", 1,
136 SEPAR_TEXT, SEPAR_TEXT, true )
137 add_integer("spect-amp", 8,
138 AMP_TEXT, AMP_LONGTEXT, true )
139 add_bool("spect-show-peaks", true,
140 PEAKS_TEXT, PEAKS_TEXT, true )
141 add_integer("spect-peak-width", 61,
142 PEAK_WIDTH_TEXT, PEAK_WIDTH_LONGTEXT, true )
143 add_integer("spect-peak-height", 1,
144 PEAK_HEIGHT_TEXT, PEAK_HEIGHT_LONGTEXT, true )
145 set_capability( "visualization2", 0 )
146 set_callbacks( Open, Close )
147 add_shortcut( "visualizer")
151 /*****************************************************************************
153 *****************************************************************************/
154 static block_t *DoWork( filter_t *, block_t * );
157 const char *psz_name;
158 int (*pf_run)( visual_effect_t *, vlc_object_t *,
159 const block_t *, picture_t *);
162 { "scope", scope_Run },
163 { "vuMeter", vuMeter_Run },
164 { "spectrum", spectrum_Run },
165 { "spectrometer", spectrometer_Run },
166 { "dummy", dummy_Run},
170 /*****************************************************************************
171 * Open: open the visualizer
172 *****************************************************************************/
173 static int Open( vlc_object_t *p_this )
175 filter_t *p_filter = (filter_t *)p_this;
178 char *psz_effects, *psz_parser;
181 if( p_filter->fmt_in.audio.i_format != VLC_CODEC_FL32 )
186 p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
187 if( unlikely (p_sys == NULL ) )
190 p_sys->i_height = var_InheritInteger( p_filter , "effect-height");
191 p_sys->i_width = var_InheritInteger( p_filter , "effect-width");
193 /* No resolution under 400x532 */
194 if( p_sys->i_height < 400 ) p_sys->i_height = 400;
195 if( p_sys->i_width < 532 ) p_sys->i_width = 532;
196 /* Work on even dimensions */
197 if( (p_sys->i_height % 2 ) != 0 ) p_sys->i_height--;
198 if( (p_sys->i_width % 2 ) != 0 ) p_sys->i_width--;
200 p_sys->b_close = false;
202 p_sys->effect = NULL;
204 /* Parse the effect list */
205 psz_parser = psz_effects = var_CreateGetString( p_filter, "effect-list" );
207 while( psz_parser && *psz_parser != '\0' )
209 visual_effect_t *p_effect;
211 p_effect = malloc( sizeof( visual_effect_t ) );
214 p_effect->i_width = p_sys->i_width;
215 p_effect->i_height = p_sys->i_height;
216 p_effect->i_nb_chans = aout_FormatNbChannels( &p_filter->fmt_in.audio);
217 p_effect->i_idx_left = 0;
218 p_effect->i_idx_right = __MIN( 1, p_effect->i_nb_chans-1 );
220 p_effect->psz_args = NULL;
221 p_effect->p_data = NULL;
223 p_effect->pf_run = NULL;
224 p_effect->psz_name = NULL;
226 for( int i = 0; pf_effect_run[i].psz_name != NULL; i++ )
228 if( !strncasecmp( psz_parser,
229 pf_effect_run[i].psz_name,
230 strlen( pf_effect_run[i].psz_name ) ) )
232 p_effect->pf_run = pf_effect_run[i].pf_run;
233 p_effect->psz_name = pf_effect_run[i].psz_name;
238 if( p_effect->psz_name )
240 psz_parser += strlen( p_effect->psz_name );
242 if( *psz_parser == '{' )
248 if( ( psz_eoa = strchr( psz_parser, '}') ) == NULL )
250 msg_Err( p_filter, "unable to parse effect list. Aborting");
255 strndup( psz_parser, psz_eoa - psz_parser);
257 TAB_APPEND( p_sys->i_effect, p_sys->effect, p_effect );
261 msg_Err( p_filter, "unknown visual effect: %s", psz_parser );
265 if( strchr( psz_parser, ',' ) )
267 psz_parser = strchr( psz_parser, ',' ) + 1;
269 else if( strchr( psz_parser, ':' ) )
271 psz_parser = strchr( psz_parser, ':' ) + 1;
281 if( !p_sys->i_effect )
283 msg_Err( p_filter, "no effects found" );
288 /* Open the video output */
289 memset( &fmt, 0, sizeof(video_format_t) );
291 fmt.i_width = fmt.i_visible_width = p_sys->i_width;
292 fmt.i_height = fmt.i_visible_height = p_sys->i_height;
293 fmt.i_chroma = VLC_CODEC_I420;
294 fmt.i_sar_num = fmt.i_sar_den = 1;
296 p_sys->p_vout = aout_filter_RequestVout( p_filter, NULL, &fmt );
297 if( p_sys->p_vout == NULL )
299 msg_Err( p_filter, "no suitable vout module" );
300 for( int i = 0; i < p_sys->i_effect; i++ )
302 free( p_sys->effect[i]->psz_args );
303 free( p_sys->effect[i] );
305 free( p_sys->effect );
310 p_filter->pf_audio_filter = DoWork;
315 /*****************************************************************************
316 * DoWork: convert a buffer
317 *****************************************************************************
318 * Audio part pasted from trivial.c
319 ****************************************************************************/
320 static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
322 filter_sys_t *p_sys = p_filter->p_sys;
325 /* First, get a new picture */
330 msleep( VOUT_OUTMEM_SLEEP );
332 while( ( p_outpic = vout_GetPicture( p_sys->p_vout ) ) == NULL);
334 /* Blank the picture */
335 for( int i = 0 ; i < p_outpic->i_planes ; i++ )
337 memset( p_outpic->p[i].p_pixels, i > 0 ? 0x80 : 0x00,
338 p_outpic->p[i].i_visible_lines * p_outpic->p[i].i_pitch );
341 /* We can now call our visualization effects */
342 for( int i = 0; i < p_sys->i_effect; i++ )
344 #define p_effect p_sys->effect[i]
345 if( p_effect->pf_run )
347 p_effect->pf_run( p_effect, VLC_OBJECT(p_filter),
348 p_in_buf, p_outpic );
353 p_outpic->date = p_in_buf->i_pts + (p_in_buf->i_length / 2);
355 vout_PutPicture( p_sys->p_vout, p_outpic );
359 /*****************************************************************************
360 * Close: close the plugin
361 *****************************************************************************/
362 static void Close( vlc_object_t *p_this )
364 filter_t * p_filter = (filter_t *)p_this;
365 filter_sys_t *p_sys = p_filter->p_sys;
367 p_sys->b_close = true;
369 if( p_filter->p_sys->p_vout )
371 aout_filter_RequestVout( p_filter, p_filter->p_sys->p_vout, 0 );
375 for( int i = 0; i < p_sys->i_effect; i++ )
377 #define p_effect p_sys->effect[i]
378 if( p_effect->p_data != NULL )
380 if( !strncmp( p_effect->psz_name, "spectrum", strlen( "spectrum" ) ) )
382 spectrum_data* p_data = p_effect->p_data;
383 free( p_data->peaks );
384 free( p_data->prev_heights );
385 free( p_data->p_prev_s16_buff );
387 if( !strncmp( p_effect->psz_name, "spectrometer", strlen( "spectrometer" ) ) )
389 spectrometer_data* p_data = p_effect->p_data;
390 free( p_data->peaks );
391 free( p_data->p_prev_s16_buff );
393 free( p_effect->p_data );
395 free( p_effect->psz_args );
400 free( p_sys->effect );
401 free( p_filter->p_sys );