File: visual.c

package info (click to toggle)
vlc 1.1.3-1squeeze6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 134,496 kB
  • ctags: 53,762
  • sloc: ansic: 341,893; cpp: 80,372; objc: 23,171; sh: 12,102; makefile: 5,035; xml: 1,351; asm: 524; python: 257; perl: 76; sed: 16
file content (428 lines) | stat: -rw-r--r-- 14,759 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*****************************************************************************
 * visual.c : Visualisation system
 *****************************************************************************
 * Copyright (C) 2002-2009 the VideoLAN team
 * $Id: cf3c00ff5255b30f057c687ff3cdfe4e8eb2d390 $
 *
 * Authors: Clément Stenac <zorglub@via.ecp.fr>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_vout.h>
#include <vlc_aout.h>
#include <vlc_filter.h>

#include "visual.h"

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
#define ELIST_TEXT N_( "Effects list" )
#define ELIST_LONGTEXT N_( \
      "A list of visual effect, separated by commas.\n"  \
      "Current effects include: dummy, scope, spectrum, "\
      "spectrometer and vuMeter." )

#define WIDTH_TEXT N_( "Video width" )
#define WIDTH_LONGTEXT N_( \
      "The width of the effects video window, in pixels." )

#define HEIGHT_TEXT N_( "Video height" )
#define HEIGHT_LONGTEXT N_( \
      "The height of the effects video window, in pixels." )

#define NBBANDS_TEXT N_( "More bands : 80 / 20" )
#define NBBANDS_LONGTEXT N_( \
      "More bands for the spectrum analyzer : 80 if enabled else 20." )
#define SPNBBANDS_LONGTEXT N_( \
      "More bands for the spectrometer : 80 if enabled else 20." )

#define SEPAR_TEXT N_( "Band separator" )
#define SEPAR_LONGTEXT N_( \
        "Number of blank pixels between bands.")

#define AMP_TEXT N_( "Amplification" )
#define AMP_LONGTEXT N_( \
        "This is a coefficient that modifies the height of the bands.")

#define PEAKS_TEXT N_( "Enable peaks" )
#define PEAKS_LONGTEXT N_( \
        "Draw \"peaks\" in the spectrum analyzer." )

#define ORIG_TEXT N_( "Enable original graphic spectrum" )
#define ORIG_LONGTEXT N_( \
        "Enable the \"flat\" spectrum analyzer in the spectrometer." )

#define BANDS_TEXT N_( "Enable bands" )
#define BANDS_LONGTEXT N_( \
        "Draw bands in the spectrometer." )

#define BASE_TEXT N_( "Enable base" )
#define BASE_LONGTEXT N_( \
        "Defines whether to draw the base of the bands." )

#define RADIUS_TEXT N_( "Base pixel radius" )
#define RADIUS_LONGTEXT N_( \
        "Defines radius size in pixels, of base of bands(beginning)." )

#define SSECT_TEXT N_( "Spectral sections" )
#define SSECT_LONGTEXT N_( \
        "Determines how many sections of spectrum will exist." )

#define PEAK_HEIGHT_TEXT N_( "Peak height" )
#define PEAK_HEIGHT_LONGTEXT N_( \
        "Total pixel height of the peak items." )

#define PEAK_WIDTH_TEXT N_( "Peak extra width" )
#define PEAK_WIDTH_LONGTEXT N_( \
        "Additions or subtractions of pixels on the peak width." )

#define COLOR1_TEXT N_( "V-plane color" )
#define COLOR1_LONGTEXT N_( \
        "YUV-Color cube shifting across the V-plane ( 0 - 127 )." )

static int  Open         ( vlc_object_t * );
static void Close        ( vlc_object_t * );

vlc_module_begin ()
    set_shortname( N_("Visualizer"))
    set_category( CAT_AUDIO )
    set_subcategory( SUBCAT_AUDIO_VISUAL )
    set_description( N_("Visualizer filter") )
    set_section( N_( "General") , NULL )
    add_string("effect-list", "spectrum", NULL,
            ELIST_TEXT, ELIST_LONGTEXT, true )
    add_integer("effect-width",VOUT_WIDTH,NULL,
             WIDTH_TEXT, WIDTH_LONGTEXT, false )
    add_integer("effect-height" , VOUT_HEIGHT , NULL,
             HEIGHT_TEXT, HEIGHT_LONGTEXT, false )
    set_section( N_("Spectrum analyser") , NULL )
    add_obsolete_integer( "visual-nbbands" ) /* Since 1.0.0 */
    add_bool("visual-80-bands", true, NULL,
             NBBANDS_TEXT, NBBANDS_LONGTEXT, true );
    add_obsolete_integer( "visual-separ" ) /* Since 1.0.0 */
    add_obsolete_integer( "visual-amp" ) /* Since 1.0.0 */
    add_bool("visual-peaks", true, NULL,
             PEAKS_TEXT, PEAKS_LONGTEXT, true )
    set_section( N_("Spectrometer") , NULL )
    add_bool("spect-show-original", false, NULL,
             ORIG_TEXT, ORIG_LONGTEXT, true )
    add_bool("spect-show-base", true, NULL,
             BASE_TEXT, BASE_LONGTEXT, true )
    add_integer("spect-radius", 42, NULL,
             RADIUS_TEXT, RADIUS_LONGTEXT, true )
    add_integer("spect-sections", 3, NULL,
             SSECT_TEXT, SSECT_LONGTEXT, true )
    add_integer("spect-color", 80, NULL,
             COLOR1_TEXT, COLOR1_LONGTEXT, true )
    add_bool("spect-show-bands", true, NULL,
             BANDS_TEXT, BANDS_LONGTEXT, true );
    add_obsolete_integer( "spect-nbbands" ) /* Since 1.0.0 */
    add_bool("spect-80-bands", true, NULL,
             NBBANDS_TEXT, SPNBBANDS_LONGTEXT, true )
    add_integer("spect-separ", 1, NULL,
             SEPAR_TEXT, SEPAR_LONGTEXT, true )
    add_integer("spect-amp", 8, NULL,
             AMP_TEXT, AMP_LONGTEXT, true )
    add_bool("spect-show-peaks", true, NULL,
             PEAKS_TEXT, PEAKS_LONGTEXT, true )
    add_integer("spect-peak-width", 61, NULL,
             PEAK_WIDTH_TEXT, PEAK_WIDTH_LONGTEXT, true )
    add_integer("spect-peak-height", 1, NULL,
             PEAK_HEIGHT_TEXT, PEAK_HEIGHT_LONGTEXT, true )
    set_capability( "visualization2", 0 )
    set_callbacks( Open, Close )
    add_shortcut( "visualizer")
vlc_module_end ()


/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static block_t *DoWork( filter_t *, block_t * );
static int FilterCallback( vlc_object_t *, char const *,
                           vlc_value_t, vlc_value_t, void * );
static const struct
{
    const char *psz_name;
    int  (*pf_run)( visual_effect_t *, vlc_object_t *,
                    const block_t *, picture_t *);
} pf_effect_run[]=
{
    { "scope",        scope_Run },
    { "vuMeter",      vuMeter_Run },
    { "spectrum",     spectrum_Run },
    { "spectrometer", spectrometer_Run },
    { "dummy",        dummy_Run},
    { NULL,           dummy_Run}
};

/*****************************************************************************
 * Open: open the visualizer
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    filter_t     *p_filter = (filter_t *)p_this;
    filter_sys_t *p_sys;

    char *psz_effects, *psz_parser;
    video_format_t fmt;

    if( ( p_filter->fmt_in.audio.i_format != VLC_CODEC_FL32 &&
          p_filter->fmt_in.audio.i_format != VLC_CODEC_FI32 ) )
    {
        return VLC_EGENERIC;
    }

    p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
    if( p_sys == NULL )
        return VLC_EGENERIC;

    p_sys->i_height = var_InheritInteger( p_filter , "effect-height");
    p_sys->i_width  = var_InheritInteger( p_filter , "effect-width");

    if( p_sys->i_height < 400 ) p_sys->i_height = 400;
    if( p_sys->i_width  < 532 ) p_sys->i_width  = 532;
    if( (p_sys->i_height % 2 ) != 0 ) p_sys->i_height--;
    if( (p_sys->i_width % 2 )  != 0 ) p_sys->i_width--;

    p_sys->i_effect = 0;
    p_sys->effect   = NULL;

    /* Parse the effect list */
    psz_parser = psz_effects = var_CreateGetString( p_filter, "effect-list" );
    var_AddCallback( p_filter, "effect-list", FilterCallback, NULL );

    while( psz_parser && *psz_parser != '\0' )
    {
        visual_effect_t *p_effect;
        int  i;

        p_effect = malloc( sizeof( visual_effect_t ) );
        if( !p_effect )
            break;
        p_effect->i_width = p_sys->i_width;
        p_effect->i_height= p_sys->i_height;
        p_effect->i_nb_chans = aout_FormatNbChannels( &p_filter->fmt_in.audio);
        p_effect->i_idx_left  = 0;
        p_effect->i_idx_right = __MIN( 1, p_effect->i_nb_chans-1 );

        p_effect->psz_args = NULL;
        p_effect->p_data = NULL;

        p_effect->pf_run = NULL;
        p_effect->psz_name = NULL;

        for( i = 0; pf_effect_run[i].psz_name != NULL; i++ )
        {
            if( !strncasecmp( psz_parser,
                              pf_effect_run[i].psz_name,
                              strlen( pf_effect_run[i].psz_name ) ) )
            {
                p_effect->pf_run = pf_effect_run[i].pf_run;
                p_effect->psz_name = pf_effect_run[i].psz_name;
                break;
            }
        }

        if( p_effect->psz_name )
        {
            psz_parser += strlen( p_effect->psz_name );

            if( *psz_parser == '{' )
            {
                char *psz_eoa;

                psz_parser++;

                if( ( psz_eoa = strchr( psz_parser, '}') ) == NULL )
                {
                   msg_Err( p_filter, "unable to parse effect list. Aborting");
                   free( p_effect );
                   break;
                }
                p_effect->psz_args =
                    strndup( psz_parser, psz_eoa - psz_parser);
            }
            TAB_APPEND( p_sys->i_effect, p_sys->effect, p_effect );
        }
        else
        {
            msg_Err( p_filter, "unknown visual effect: %s", psz_parser );
            free( p_effect );
        }

        if( strchr( psz_parser, ',' ) )
        {
            psz_parser = strchr( psz_parser, ',' ) + 1;
        }
        else if( strchr( psz_parser, ':' ) )
        {
            psz_parser = strchr( psz_parser, ':' ) + 1;
        }
        else
        {
            break;
        }
    }

    free( psz_effects );

    if( !p_sys->i_effect )
    {
        msg_Err( p_filter, "no effects found" );
        free( p_sys );
        return VLC_EGENERIC;
    }

    /* Open the video output */
    memset( &fmt, 0, sizeof(video_format_t) );

    fmt.i_width = fmt.i_visible_width = p_sys->i_width;
    fmt.i_height = fmt.i_visible_height = p_sys->i_height;
    fmt.i_chroma = VLC_CODEC_I420;
    fmt.i_sar_num = fmt.i_sar_den = 1;

    p_sys->p_vout = aout_filter_RequestVout( p_filter, NULL, &fmt );
    if( p_sys->p_vout == NULL )
    {
        msg_Err( p_filter, "no suitable vout module" );
        for( int i = 0; i < p_sys->i_effect; i++ )
        {
            free( p_sys->effect[i]->psz_args );
            free( p_sys->effect[i] );
        }
        free( p_sys->effect );
        free( p_sys );
        return VLC_EGENERIC;
    }

    p_filter->pf_audio_filter = DoWork;

    return VLC_SUCCESS;
}

/*****************************************************************************
 * DoWork: convert a buffer
 *****************************************************************************
 * Audio part pasted from trivial.c
 ****************************************************************************/
static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
{
    filter_sys_t *p_sys = p_filter->p_sys;
    picture_t *p_outpic;
    int i;

    /* First, get a new picture */
    while( ( p_outpic = vout_CreatePicture( p_sys->p_vout, 0, 0, 3 ) ) == NULL)
    {   /* XXX: This looks like a bad idea. Don't run to me for sympathy if it
         * dead locks... */
        if( !vlc_object_alive (p_sys->p_vout) )
            return NULL;
        msleep( VOUT_OUTMEM_SLEEP );
    }

    /* Blank the picture */
    for( i = 0 ; i < p_outpic->i_planes ; i++ )
    {
        memset( p_outpic->p[i].p_pixels, i > 0 ? 0x80 : 0x00,
                p_outpic->p[i].i_visible_lines * p_outpic->p[i].i_pitch );
    }

    /* We can now call our visualization effects */
    for( i = 0; i < p_sys->i_effect; i++ )
    {
#define p_effect p_sys->effect[i]
        if( p_effect->pf_run )
        {
            p_effect->pf_run( p_effect, VLC_OBJECT(p_filter),
                              p_in_buf, p_outpic );
        }
#undef p_effect
    }

    p_outpic->date = p_in_buf->i_pts + (p_in_buf->i_length / 2);

    vout_DisplayPicture( p_sys->p_vout, p_outpic );
    return p_in_buf;
}

/*****************************************************************************
 * Close: close the plugin
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
    filter_t * p_filter = (filter_t *)p_this;
    filter_sys_t *p_sys = p_filter->p_sys;

    int i;

    if( p_filter->p_sys->p_vout )
    {
        aout_filter_RequestVout( p_filter, p_filter->p_sys->p_vout, 0 );
    }

    /* Free the list */
    for( i = 0; i < p_sys->i_effect; i++ )
    {
#define p_effect p_sys->effect[i]
        if( !strncmp( p_effect->psz_name, "spectrum", strlen( "spectrum" ) ) )
        {
            spectrum_data *p_data = p_effect->p_data;
            free( p_data->peaks );
            free( p_data->prev_heights );
            free( p_data->p_prev_s16_buff );
        }
        if( !strncmp( p_effect->psz_name, "spectrometer", strlen( "spectrometer" ) ) )
        {
            spectrometer_data *p_data = p_effect->p_data;
            free( p_data->peaks );
            free( p_data->p_prev_s16_buff );
        }
        free( p_effect->p_data );
        free( p_effect->psz_args );
        free( p_effect );
#undef p_effect
    }

    free( p_sys->effect );
    free( p_filter->p_sys );
}

/*****************************************************************************
 * FilterCallback: called when changing the deinterlace method on the fly.
 *****************************************************************************/
static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
                           vlc_value_t oldval, vlc_value_t newval,
                           void *p_data )
{
    VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
    VLC_UNUSED(p_data); VLC_UNUSED(newval);
    aout_filter_t     *p_filter = (aout_filter_t *)p_this;
    /* restart this baby */
    msg_Dbg( p_filter, "we should restart the visual filter" );
    return VLC_SUCCESS;
}