]> git.sesse.net Git - vlc/blob - modules/visualization/visual/visual.c
visual: do not pretend to support FI32
[vlc] / modules / visualization / visual / visual.c
1 /*****************************************************************************
2  * visual.c : Visualisation system
3  *****************************************************************************
4  * Copyright (C) 2002-2009 the VideoLAN team
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
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.
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 General Public License for more details.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_vout.h>
34 #include <vlc_aout.h>
35 #include <vlc_filter.h>
36
37 #include "visual.h"
38
39 /*****************************************************************************
40  * Module descriptor
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." )
47
48 #define WIDTH_TEXT N_( "Video width" )
49 #define WIDTH_LONGTEXT N_( \
50       "The width of the effects video window, in pixels." )
51
52 #define HEIGHT_TEXT N_( "Video height" )
53 #define HEIGHT_LONGTEXT N_( \
54       "The height of the effects video window, in pixels." )
55
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." )
59
60 #define SEPAR_TEXT N_( "Number of blank pixels between bands.")
61
62 #define AMP_TEXT N_( "Amplification" )
63 #define AMP_LONGTEXT N_( \
64         "This is a coefficient that modifies the height of the bands.")
65
66 #define PEAKS_TEXT N_( "Draw peaks in the analyzer" )
67
68 #define ORIG_TEXT N_( "Enable original graphic spectrum" )
69 #define ORIG_LONGTEXT N_( \
70         "Enable the \"flat\" spectrum analyzer in the spectrometer." )
71
72 #define BANDS_TEXT N_( "Draw bands in the spectrometer" )
73
74 #define BASE_TEXT N_( "Draw the base of the bands" )
75
76 #define RADIUS_TEXT N_( "Base pixel radius" )
77 #define RADIUS_LONGTEXT N_( \
78         "Defines radius size in pixels, of base of bands(beginning)." )
79
80 #define SSECT_TEXT N_( "Spectral sections" )
81 #define SSECT_LONGTEXT N_( \
82         "Determines how many sections of spectrum will exist." )
83
84 #define PEAK_HEIGHT_TEXT N_( "Peak height" )
85 #define PEAK_HEIGHT_LONGTEXT N_( \
86         "Total pixel height of the peak items." )
87
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." )
91
92 #define COLOR1_TEXT N_( "V-plane color" )
93 #define COLOR1_LONGTEXT N_( \
94         "YUV-Color cube shifting across the V-plane ( 0 - 127 )." )
95
96 static int  Open         ( vlc_object_t * );
97 static void Close        ( vlc_object_t * );
98
99 vlc_module_begin ()
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")
148 vlc_module_end ()
149
150
151 /*****************************************************************************
152  * Local prototypes
153  *****************************************************************************/
154 static block_t *DoWork( filter_t *, block_t * );
155 static const struct
156 {
157     const char *psz_name;
158     int  (*pf_run)( visual_effect_t *, vlc_object_t *,
159                     const block_t *, picture_t *);
160 } pf_effect_run[]=
161 {
162     { "scope",        scope_Run },
163     { "vuMeter",      vuMeter_Run },
164     { "spectrum",     spectrum_Run },
165     { "spectrometer", spectrometer_Run },
166     { "dummy",        dummy_Run},
167     { NULL,           dummy_Run}
168 };
169
170 /*****************************************************************************
171  * Open: open the visualizer
172  *****************************************************************************/
173 static int Open( vlc_object_t *p_this )
174 {
175     filter_t     *p_filter = (filter_t *)p_this;
176     filter_sys_t *p_sys;
177
178     char *psz_effects, *psz_parser;
179     video_format_t fmt;
180
181     if( p_filter->fmt_in.audio.i_format != VLC_CODEC_FL32 )
182     {
183         return VLC_EGENERIC;
184     }
185
186     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
187     if( unlikely (p_sys == NULL ) )
188         return VLC_EGENERIC;
189
190     p_sys->i_height = var_InheritInteger( p_filter , "effect-height");
191     p_sys->i_width  = var_InheritInteger( p_filter , "effect-width");
192
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--;
199
200     p_sys->b_close = false;
201     p_sys->i_effect = 0;
202     p_sys->effect   = NULL;
203
204     /* Parse the effect list */
205     psz_parser = psz_effects = var_CreateGetString( p_filter, "effect-list" );
206
207     while( psz_parser && *psz_parser != '\0' )
208     {
209         visual_effect_t *p_effect;
210
211         p_effect = malloc( sizeof( visual_effect_t ) );
212         if( !p_effect )
213             break;
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 );
219
220         p_effect->psz_args = NULL;
221         p_effect->p_data   = NULL;
222
223         p_effect->pf_run   = NULL;
224         p_effect->psz_name = NULL;
225
226         for( int i = 0; pf_effect_run[i].psz_name != NULL; i++ )
227         {
228             if( !strncasecmp( psz_parser,
229                               pf_effect_run[i].psz_name,
230                               strlen( pf_effect_run[i].psz_name ) ) )
231             {
232                 p_effect->pf_run = pf_effect_run[i].pf_run;
233                 p_effect->psz_name = pf_effect_run[i].psz_name;
234                 break;
235             }
236         }
237
238         if( p_effect->psz_name )
239         {
240             psz_parser += strlen( p_effect->psz_name );
241
242             if( *psz_parser == '{' )
243             {
244                 char *psz_eoa;
245
246                 psz_parser++;
247
248                 if( ( psz_eoa = strchr( psz_parser, '}') ) == NULL )
249                 {
250                    msg_Err( p_filter, "unable to parse effect list. Aborting");
251                    free( p_effect );
252                    break;
253                 }
254                 p_effect->psz_args =
255                     strndup( psz_parser, psz_eoa - psz_parser);
256             }
257             TAB_APPEND( p_sys->i_effect, p_sys->effect, p_effect );
258         }
259         else
260         {
261             msg_Err( p_filter, "unknown visual effect: %s", psz_parser );
262             free( p_effect );
263         }
264
265         if( strchr( psz_parser, ',' ) )
266         {
267             psz_parser = strchr( psz_parser, ',' ) + 1;
268         }
269         else if( strchr( psz_parser, ':' ) )
270         {
271             psz_parser = strchr( psz_parser, ':' ) + 1;
272         }
273         else
274         {
275             break;
276         }
277     }
278
279     free( psz_effects );
280
281     if( !p_sys->i_effect )
282     {
283         msg_Err( p_filter, "no effects found" );
284         free( p_sys );
285         return VLC_EGENERIC;
286     }
287
288     /* Open the video output */
289     memset( &fmt, 0, sizeof(video_format_t) );
290
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;
295
296     p_sys->p_vout = aout_filter_RequestVout( p_filter, NULL, &fmt );
297     if( p_sys->p_vout == NULL )
298     {
299         msg_Err( p_filter, "no suitable vout module" );
300         for( int i = 0; i < p_sys->i_effect; i++ )
301         {
302             free( p_sys->effect[i]->psz_args );
303             free( p_sys->effect[i] );
304         }
305         free( p_sys->effect );
306         free( p_sys );
307         return VLC_EGENERIC;
308     }
309
310     p_filter->pf_audio_filter = DoWork;
311
312     return VLC_SUCCESS;
313 }
314
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 )
321 {
322     filter_sys_t *p_sys = p_filter->p_sys;
323     picture_t *p_outpic;
324
325     /* First, get a new picture */
326     do
327     {
328         if( p_sys->b_close )
329             return NULL;
330         msleep( VOUT_OUTMEM_SLEEP );
331     }
332     while( ( p_outpic = vout_GetPicture( p_sys->p_vout ) ) == NULL);
333
334     /* Blank the picture */
335     for( int i = 0 ; i < p_outpic->i_planes ; i++ )
336     {
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 );
339     }
340
341     /* We can now call our visualization effects */
342     for( int i = 0; i < p_sys->i_effect; i++ )
343     {
344 #define p_effect p_sys->effect[i]
345         if( p_effect->pf_run )
346         {
347             p_effect->pf_run( p_effect, VLC_OBJECT(p_filter),
348                               p_in_buf, p_outpic );
349         }
350 #undef p_effect
351     }
352
353     p_outpic->date = p_in_buf->i_pts + (p_in_buf->i_length / 2);
354
355     vout_PutPicture( p_sys->p_vout, p_outpic );
356     return p_in_buf;
357 }
358
359 /*****************************************************************************
360  * Close: close the plugin
361  *****************************************************************************/
362 static void Close( vlc_object_t *p_this )
363 {
364     filter_t * p_filter = (filter_t *)p_this;
365     filter_sys_t *p_sys = p_filter->p_sys;
366
367     p_sys->b_close = true;
368
369     if( p_filter->p_sys->p_vout )
370     {
371         aout_filter_RequestVout( p_filter, p_filter->p_sys->p_vout, 0 );
372     }
373
374     /* Free the list */
375     for( int i = 0; i < p_sys->i_effect; i++ )
376     {
377 #define p_effect p_sys->effect[i]
378         if( p_effect->p_data != NULL )
379         {
380             if( !strncmp( p_effect->psz_name, "spectrum", strlen( "spectrum" ) ) )
381             {
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 );
386             }
387             if( !strncmp( p_effect->psz_name, "spectrometer", strlen( "spectrometer" ) ) )
388             {
389                 spectrometer_data* p_data = p_effect->p_data;
390                 free( p_data->peaks );
391                 free( p_data->p_prev_s16_buff );
392             }
393             free( p_effect->p_data );
394         }
395         free( p_effect->psz_args );
396         free( p_effect );
397 #undef p_effect
398     }
399
400     free( p_sys->effect );
401     free( p_filter->p_sys );
402 }