]> git.sesse.net Git - vlc/blob - modules/visualization/visual/visual.c
628fd7038564d293cff5edecc6b33050d6fb5744
[vlc] / modules / visualization / visual / visual.c
1 /*****************************************************************************
2  * visual.c : Visualisation system
3  *****************************************************************************
4  * Copyright (C) 2002-2009 VLC authors and VideoLAN
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 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.
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 Lesser General Public License for more details.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <assert.h>
32 #include <limits.h>
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_vout.h>
37 #include <vlc_aout.h>
38 #include <vlc_filter.h>
39
40 #include "visual.h"
41
42 #include "window_presets.h"
43
44 /*****************************************************************************
45  * Module descriptor
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." )
52
53 #define WIDTH_TEXT N_( "Video width" )
54 #define WIDTH_LONGTEXT N_( \
55       "The width of the effects video window, in pixels." )
56
57 #define HEIGHT_TEXT N_( "Video height" )
58 #define HEIGHT_LONGTEXT N_( \
59       "The height of the effects video window, in pixels." )
60
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." )
64
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. " )
69
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." )
73
74 #define SEPAR_TEXT N_( "Number of blank pixels between bands.")
75
76 #define AMP_TEXT N_( "Amplification" )
77 #define AMP_LONGTEXT N_( \
78         "This is a coefficient that modifies the height of the bands.")
79
80 #define PEAKS_TEXT N_( "Draw peaks in the analyzer" )
81
82 #define ORIG_TEXT N_( "Enable original graphic spectrum" )
83 #define ORIG_LONGTEXT N_( \
84         "Enable the \"flat\" spectrum analyzer in the spectrometer." )
85
86 #define BANDS_TEXT N_( "Draw bands in the spectrometer" )
87
88 #define BASE_TEXT N_( "Draw the base of the bands" )
89
90 #define RADIUS_TEXT N_( "Base pixel radius" )
91 #define RADIUS_LONGTEXT N_( \
92         "Defines radius size in pixels, of base of bands(beginning)." )
93
94 #define SSECT_TEXT N_( "Spectral sections" )
95 #define SSECT_LONGTEXT N_( \
96         "Determines how many sections of spectrum will exist." )
97
98 #define PEAK_HEIGHT_TEXT N_( "Peak height" )
99 #define PEAK_HEIGHT_LONGTEXT N_( \
100         "Total pixel height of the peak items." )
101
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." )
105
106 #define COLOR1_TEXT N_( "V-plane color" )
107 #define COLOR1_LONGTEXT N_( \
108         "YUV-Color cube shifting across the V-plane ( 0 - 127 )." )
109
110 /* Default vout size */
111 #define VOUT_WIDTH  800
112 #define VOUT_HEIGHT 500
113
114 static int  Open         ( vlc_object_t * );
115 static void Close        ( vlc_object_t * );
116
117 vlc_module_begin ()
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")
171 vlc_module_end ()
172
173
174 /*****************************************************************************
175  * Local prototypes
176  *****************************************************************************/
177 static block_t *DoWork( filter_t *, block_t * );
178 static void *Thread( void *);
179
180 struct filter_sys_t
181 {
182     block_fifo_t    *fifo;
183     vout_thread_t   *p_vout;
184     visual_effect_t **effect;
185     int             i_effect;
186     vlc_thread_t    thread;
187 };
188
189 /*****************************************************************************
190  * Open: open the visualizer
191  *****************************************************************************/
192 static int Open( vlc_object_t *p_this )
193 {
194     filter_t     *p_filter = (filter_t *)p_this;
195     filter_sys_t *p_sys;
196
197     char *psz_effects, *psz_parser;
198
199     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
200     if( unlikely (p_sys == NULL ) )
201         return VLC_EGENERIC;
202
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 */
206     if( width < 532 )
207         width  = 532;
208     width &= ~1;
209     if( height < 400 )
210         height = 400;
211     height &= ~1;
212
213     p_sys->i_effect = 0;
214     p_sys->effect   = NULL;
215
216     /* Parse the effect list */
217     psz_parser = psz_effects = var_CreateGetString( p_filter, "effect-list" );
218
219     while( psz_parser && *psz_parser != '\0' )
220     {
221         visual_effect_t *p_effect;
222
223         p_effect = malloc( sizeof( visual_effect_t ) );
224         if( !p_effect )
225             break;
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 );
231
232         p_effect->p_data   = NULL;
233         p_effect->pf_run   = NULL;
234
235         for( unsigned i = 0; i < effectc; i++ )
236         {
237             if( !strncasecmp( psz_parser, effectv[i].name,
238                               strlen( effectv[i].name ) ) )
239             {
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 );
243                 break;
244             }
245         }
246
247         if( p_effect->pf_run != NULL )
248         {
249             if( *psz_parser == '{' )
250             {
251                 char *psz_eoa;
252
253                 psz_parser++;
254
255                 if( ( psz_eoa = strchr( psz_parser, '}') ) == NULL )
256                 {
257                    msg_Err( p_filter, "unable to parse effect list. Aborting");
258                    free( p_effect );
259                    break;
260                 }
261             }
262             TAB_APPEND( p_sys->i_effect, p_sys->effect, p_effect );
263         }
264         else
265         {
266             msg_Err( p_filter, "unknown visual effect: %s", psz_parser );
267             free( p_effect );
268         }
269
270         if( strchr( psz_parser, ',' ) )
271         {
272             psz_parser = strchr( psz_parser, ',' ) + 1;
273         }
274         else if( strchr( psz_parser, ':' ) )
275         {
276             psz_parser = strchr( psz_parser, ':' ) + 1;
277         }
278         else
279         {
280             break;
281         }
282     }
283
284     free( psz_effects );
285
286     if( !p_sys->i_effect )
287     {
288         msg_Err( p_filter, "no effects found" );
289         goto error;
290     }
291
292     /* Open the video output */
293     video_format_t fmt = {
294         .i_chroma = VLC_CODEC_I420,
295         .i_width = width,
296         .i_height = height,
297         .i_visible_width = width,
298         .i_visible_height = height,
299         .i_sar_num = 1,
300         .i_sar_den = 1,
301     };
302     p_sys->p_vout = aout_filter_RequestVout( p_filter, NULL, &fmt );
303     if( p_sys->p_vout == NULL )
304     {
305         msg_Err( p_filter, "no suitable vout module" );
306         goto error;
307     }
308
309     p_sys->fifo = block_FifoNew();
310     if( unlikely( p_sys->fifo == NULL ) )
311     {
312         aout_filter_RequestVout( p_filter, p_sys->p_vout, NULL );
313         goto error;
314     }
315
316     if( vlc_clone( &p_sys->thread, Thread, p_filter,
317                    VLC_THREAD_PRIORITY_VIDEO ) )
318     {
319         block_FifoRelease( p_sys->fifo );
320         aout_filter_RequestVout( p_filter, p_sys->p_vout, NULL );
321         goto error;
322     }
323
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;
327     return VLC_SUCCESS;
328
329 error:
330     for( int i = 0; i < p_sys->i_effect; i++ )
331         free( p_sys->effect[i] );
332     free( p_sys->effect );
333     free( p_sys );
334     return VLC_EGENERIC;
335 }
336
337 static block_t *DoRealWork( filter_t *p_filter, block_t *p_in_buf )
338 {
339     filter_sys_t *p_sys = p_filter->p_sys;
340     picture_t *p_outpic;
341
342     /* First, get a new picture */
343     while( ( p_outpic = vout_GetPicture( p_sys->p_vout ) ) == NULL )
344         msleep( VOUT_OUTMEM_SLEEP );
345
346     /* Blank the picture */
347     for( int i = 0 ; i < p_outpic->i_planes ; i++ )
348     {
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 );
351     }
352
353     /* We can now call our visualization effects */
354     for( int i = 0; i < p_sys->i_effect; i++ )
355     {
356 #define p_effect p_sys->effect[i]
357         if( p_effect->pf_run )
358         {
359             p_effect->pf_run( p_effect, VLC_OBJECT(p_filter),
360                               p_in_buf, p_outpic );
361         }
362 #undef p_effect
363     }
364
365     p_outpic->date = p_in_buf->i_pts + (p_in_buf->i_length / 2);
366
367     vout_PutPicture( p_sys->p_vout, p_outpic );
368     return p_in_buf;
369 }
370
371 static void *Thread( void *data )
372 {
373     filter_t *p_filter = data;
374     filter_sys_t *sys = p_filter->p_sys;
375
376     for (;;)
377     {
378         block_t *block = block_FifoGet( sys->fifo );
379
380         int canc = vlc_savecancel( );
381         block_Release( DoRealWork( p_filter, block ) );
382         vlc_restorecancel( canc );
383     }
384     assert(0);
385 }
386
387 static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
388 {
389     block_t *block = block_Duplicate( p_in_buf );
390     if( likely(block != NULL) )
391         block_FifoPut( p_filter->p_sys->fifo, block );
392     return p_in_buf;
393 }
394
395 /*****************************************************************************
396  * Close: close the plugin
397  *****************************************************************************/
398 static void Close( vlc_object_t *p_this )
399 {
400     filter_t * p_filter = (filter_t *)p_this;
401     filter_sys_t *p_sys = p_filter->p_sys;
402
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 );
407
408     /* Free the list */
409     for( int i = 0; i < p_sys->i_effect; i++ )
410     {
411 #define p_effect (p_sys->effect[i])
412         p_effect->pf_free( p_effect->p_data );
413         free( p_effect );
414 #undef p_effect
415     }
416
417     free( p_sys->effect );
418     free( p_sys );
419 }