1 /*****************************************************************************
2 * visual.c : Visualisation system
3 *****************************************************************************
4 * Copyright (C) 2002-2009 VLC authors and VideoLAN
7 * Authors: Clément Stenac <zorglub@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
38 #include <vlc_filter.h>
42 #include "window_presets.h"
44 /*****************************************************************************
46 *****************************************************************************/
47 #define ELIST_TEXT N_( "Effects list" )
48 #define ELIST_LONGTEXT N_( \
49 "A list of visual effect, separated by commas.\n" \
50 "Current effects include: dummy, scope, spectrum, "\
51 "spectrometer and vuMeter." )
53 #define WIDTH_TEXT N_( "Video width" )
54 #define WIDTH_LONGTEXT N_( \
55 "The width of the effects video window, in pixels." )
57 #define HEIGHT_TEXT N_( "Video height" )
58 #define HEIGHT_LONGTEXT N_( \
59 "The height of the effects video window, in pixels." )
61 #define FFT_WINDOW_TEXT N_( "FFT window" )
62 #define FFT_WINDOW_LONGTEXT N_( \
63 "The type of FFT window to use for spectrum-based visualizations." )
65 #define KAISER_PARAMETER_TEXT N_( "Kaiser window parameter" )
66 #define KAISER_PARAMETER_LONGTEXT N_( \
67 "The parameter alpha for the Kaiser window. Increasing alpha " \
68 "increases the main-lobe width and decreases the side-lobe amplitude. " )
70 #define NBBANDS_TEXT N_( "Show 80 bands instead of 20" )
71 #define SPNBBANDS_LONGTEXT N_( \
72 "More bands for the spectrometer : 80 if enabled else 20." )
74 #define SEPAR_TEXT N_( "Number of blank pixels between bands.")
76 #define AMP_TEXT N_( "Amplification" )
77 #define AMP_LONGTEXT N_( \
78 "This is a coefficient that modifies the height of the bands.")
80 #define PEAKS_TEXT N_( "Draw peaks in the analyzer" )
82 #define ORIG_TEXT N_( "Enable original graphic spectrum" )
83 #define ORIG_LONGTEXT N_( \
84 "Enable the \"flat\" spectrum analyzer in the spectrometer." )
86 #define BANDS_TEXT N_( "Draw bands in the spectrometer" )
88 #define BASE_TEXT N_( "Draw the base of the bands" )
90 #define RADIUS_TEXT N_( "Base pixel radius" )
91 #define RADIUS_LONGTEXT N_( \
92 "Defines radius size in pixels, of base of bands(beginning)." )
94 #define SSECT_TEXT N_( "Spectral sections" )
95 #define SSECT_LONGTEXT N_( \
96 "Determines how many sections of spectrum will exist." )
98 #define PEAK_HEIGHT_TEXT N_( "Peak height" )
99 #define PEAK_HEIGHT_LONGTEXT N_( \
100 "Total pixel height of the peak items." )
102 #define PEAK_WIDTH_TEXT N_( "Peak extra width" )
103 #define PEAK_WIDTH_LONGTEXT N_( \
104 "Additions or subtractions of pixels on the peak width." )
106 #define COLOR1_TEXT N_( "V-plane color" )
107 #define COLOR1_LONGTEXT N_( \
108 "YUV-Color cube shifting across the V-plane ( 0 - 127 )." )
110 /* Default vout size */
111 #define VOUT_WIDTH 800
112 #define VOUT_HEIGHT 500
114 static int Open ( vlc_object_t * );
115 static void Close ( vlc_object_t * );
118 set_shortname( N_("Visualizer"))
119 set_category( CAT_AUDIO )
120 set_subcategory( SUBCAT_AUDIO_VISUAL )
121 set_description( N_("Visualizer filter") )
122 set_section( N_( "General") , NULL )
123 add_string("effect-list", "spectrum",
124 ELIST_TEXT, ELIST_LONGTEXT, true )
125 add_integer("effect-width",VOUT_WIDTH,
126 WIDTH_TEXT, WIDTH_LONGTEXT, false )
127 add_integer("effect-height" , VOUT_HEIGHT ,
128 HEIGHT_TEXT, HEIGHT_LONGTEXT, false )
129 add_string("effect-fft-window", "flat",
130 FFT_WINDOW_TEXT, FFT_WINDOW_LONGTEXT, true )
131 change_string_list( window_list, window_list_text )
132 add_float("effect-kaiser-param", 3.0f,
133 KAISER_PARAMETER_TEXT, KAISER_PARAMETER_LONGTEXT, true )
134 set_section( N_("Spectrum analyser") , NULL )
135 add_obsolete_integer( "visual-nbbands" ) /* Since 1.0.0 */
136 add_bool("visual-80-bands", true,
137 NBBANDS_TEXT, NBBANDS_TEXT, true );
138 add_obsolete_integer( "visual-separ" ) /* Since 1.0.0 */
139 add_obsolete_integer( "visual-amp" ) /* Since 1.0.0 */
140 add_bool("visual-peaks", true,
141 PEAKS_TEXT, PEAKS_TEXT, true )
142 set_section( N_("Spectrometer") , NULL )
143 add_bool("spect-show-original", false,
144 ORIG_TEXT, ORIG_LONGTEXT, true )
145 add_bool("spect-show-base", true,
146 BASE_TEXT, BASE_TEXT, true )
147 add_integer("spect-radius", 42,
148 RADIUS_TEXT, RADIUS_LONGTEXT, true )
149 add_integer_with_range("spect-sections", 3, 1, INT_MAX,
150 SSECT_TEXT, SSECT_LONGTEXT, true )
151 add_integer("spect-color", 80,
152 COLOR1_TEXT, COLOR1_LONGTEXT, true )
153 add_bool("spect-show-bands", true,
154 BANDS_TEXT, BANDS_TEXT, true );
155 add_obsolete_integer( "spect-nbbands" ) /* Since 1.0.0 */
156 add_bool("spect-80-bands", true,
157 NBBANDS_TEXT, NBBANDS_TEXT, true )
158 add_integer("spect-separ", 1,
159 SEPAR_TEXT, SEPAR_TEXT, true )
160 add_integer("spect-amp", 8,
161 AMP_TEXT, AMP_LONGTEXT, true )
162 add_bool("spect-show-peaks", true,
163 PEAKS_TEXT, PEAKS_TEXT, true )
164 add_integer("spect-peak-width", 61,
165 PEAK_WIDTH_TEXT, PEAK_WIDTH_LONGTEXT, true )
166 add_integer("spect-peak-height", 1,
167 PEAK_HEIGHT_TEXT, PEAK_HEIGHT_LONGTEXT, true )
168 set_capability( "visualization", 0 )
169 set_callbacks( Open, Close )
170 add_shortcut( "visualizer")
174 /*****************************************************************************
176 *****************************************************************************/
177 static block_t *DoWork( filter_t *, block_t * );
178 static void *Thread( void *);
183 vout_thread_t *p_vout;
184 visual_effect_t **effect;
189 /*****************************************************************************
190 * Open: open the visualizer
191 *****************************************************************************/
192 static int Open( vlc_object_t *p_this )
194 filter_t *p_filter = (filter_t *)p_this;
197 char *psz_effects, *psz_parser;
199 p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
200 if( unlikely (p_sys == NULL ) )
203 int width = var_InheritInteger( p_filter , "effect-width");
204 int height = var_InheritInteger( p_filter , "effect-width");
205 /* No resolution under 400x532 and no odd dimension */
214 p_sys->effect = NULL;
216 /* Parse the effect list */
217 psz_parser = psz_effects = var_CreateGetString( p_filter, "effect-list" );
219 while( psz_parser && *psz_parser != '\0' )
221 visual_effect_t *p_effect;
223 p_effect = malloc( sizeof( visual_effect_t ) );
226 p_effect->i_width = width;
227 p_effect->i_height = height;
228 p_effect->i_nb_chans = aout_FormatNbChannels( &p_filter->fmt_in.audio);
229 p_effect->i_idx_left = 0;
230 p_effect->i_idx_right = __MIN( 1, p_effect->i_nb_chans-1 );
232 p_effect->p_data = NULL;
233 p_effect->pf_run = NULL;
235 for( unsigned i = 0; i < effectc; i++ )
237 if( !strncasecmp( psz_parser, effectv[i].name,
238 strlen( effectv[i].name ) ) )
240 p_effect->pf_run = effectv[i].run_cb;
241 p_effect->pf_free = effectv[i].free_cb;
242 psz_parser += strlen( effectv[i].name );
247 if( p_effect->pf_run != NULL )
249 if( *psz_parser == '{' )
255 if( ( psz_eoa = strchr( psz_parser, '}') ) == NULL )
257 msg_Err( p_filter, "unable to parse effect list. Aborting");
262 TAB_APPEND( p_sys->i_effect, p_sys->effect, p_effect );
266 msg_Err( p_filter, "unknown visual effect: %s", psz_parser );
270 if( strchr( psz_parser, ',' ) )
272 psz_parser = strchr( psz_parser, ',' ) + 1;
274 else if( strchr( psz_parser, ':' ) )
276 psz_parser = strchr( psz_parser, ':' ) + 1;
286 if( !p_sys->i_effect )
288 msg_Err( p_filter, "no effects found" );
292 /* Open the video output */
293 video_format_t fmt = {
294 .i_chroma = VLC_CODEC_I420,
297 .i_visible_width = width,
298 .i_visible_height = height,
302 p_sys->p_vout = aout_filter_RequestVout( p_filter, NULL, &fmt );
303 if( p_sys->p_vout == NULL )
305 msg_Err( p_filter, "no suitable vout module" );
309 p_sys->fifo = block_FifoNew();
310 if( unlikely( p_sys->fifo == NULL ) )
312 aout_filter_RequestVout( p_filter, p_sys->p_vout, NULL );
316 if( vlc_clone( &p_sys->thread, Thread, p_filter,
317 VLC_THREAD_PRIORITY_VIDEO ) )
319 block_FifoRelease( p_sys->fifo );
320 aout_filter_RequestVout( p_filter, p_sys->p_vout, NULL );
324 p_filter->fmt_in.audio.i_format = VLC_CODEC_FL32;
325 p_filter->fmt_out.audio = p_filter->fmt_in.audio;
326 p_filter->pf_audio_filter = DoWork;
330 for( int i = 0; i < p_sys->i_effect; i++ )
331 free( p_sys->effect[i] );
332 free( p_sys->effect );
337 static block_t *DoRealWork( filter_t *p_filter, block_t *p_in_buf )
339 filter_sys_t *p_sys = p_filter->p_sys;
342 /* First, get a new picture */
343 while( ( p_outpic = vout_GetPicture( p_sys->p_vout ) ) == NULL )
344 msleep( VOUT_OUTMEM_SLEEP );
346 /* Blank the picture */
347 for( int i = 0 ; i < p_outpic->i_planes ; i++ )
349 memset( p_outpic->p[i].p_pixels, i > 0 ? 0x80 : 0x00,
350 p_outpic->p[i].i_visible_lines * p_outpic->p[i].i_pitch );
353 /* We can now call our visualization effects */
354 for( int i = 0; i < p_sys->i_effect; i++ )
356 #define p_effect p_sys->effect[i]
357 if( p_effect->pf_run )
359 p_effect->pf_run( p_effect, VLC_OBJECT(p_filter),
360 p_in_buf, p_outpic );
365 p_outpic->date = p_in_buf->i_pts + (p_in_buf->i_length / 2);
367 vout_PutPicture( p_sys->p_vout, p_outpic );
371 static void *Thread( void *data )
373 filter_t *p_filter = data;
374 filter_sys_t *sys = p_filter->p_sys;
378 block_t *block = block_FifoGet( sys->fifo );
380 int canc = vlc_savecancel( );
381 block_Release( DoRealWork( p_filter, block ) );
382 vlc_restorecancel( canc );
387 static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
389 block_t *block = block_Duplicate( p_in_buf );
390 if( likely(block != NULL) )
391 block_FifoPut( p_filter->p_sys->fifo, block );
395 /*****************************************************************************
396 * Close: close the plugin
397 *****************************************************************************/
398 static void Close( vlc_object_t *p_this )
400 filter_t * p_filter = (filter_t *)p_this;
401 filter_sys_t *p_sys = p_filter->p_sys;
403 vlc_cancel( p_sys->thread );
404 vlc_join( p_sys->thread, NULL );
405 block_FifoRelease( p_sys->fifo );
406 aout_filter_RequestVout( p_filter, p_filter->p_sys->p_vout, NULL );
409 for( int i = 0; i < p_sys->i_effect; i++ )
411 #define p_effect (p_sys->effect[i])
412 p_effect->pf_free( p_effect->p_data );
417 free( p_sys->effect );