visual.c 14.3 KB
Newer Older
1
2
3
/*****************************************************************************
 * visual.c : Visualisation system
 *****************************************************************************
4
 * Copyright (C) 2002-2006 the VideoLAN team
zorglub's avatar
zorglub committed
5
 * $Id$
6
 *
7
 * Authors: Clément Stenac <zorglub@via.ecp.fr>
8
9
10
11
12
 *
 * 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.
Laurent Aimar's avatar
Laurent Aimar committed
13
 *
14
15
16
17
18
19
20
 * 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
dionoea's avatar
dionoea committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22
23
24
25
26
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
27
28
29
30
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

31
#include <vlc_common.h>
32
#include <vlc_plugin.h>
zorglub's avatar
zorglub committed
33
34
#include <vlc_vout.h>
#include <vlc_aout.h>
35

Laurent Aimar's avatar
Laurent Aimar committed
36
#include "visual.h"
37
38
39
40
41
42

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
#define ELIST_TEXT N_( "Effects list" )
#define ELIST_LONGTEXT N_( \
43
      "A list of visual effect, separated by commas.\n"  \
Christophe Mutricy's avatar
Christophe Mutricy committed
44
      "Current effects include: dummy, scope, spectrum." )
45
46
47
48
49

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

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

zorglub's avatar
zorglub committed
54
55
#define NBBANDS_TEXT N_( "Number of bands" )
#define NBBANDS_LONGTEXT N_( \
gbazin's avatar
   
gbazin committed
56
      "Number of bands used by spectrum analyzer, should be 20 or 80." )
57
#define SPNBBANDS_LONGTEXT N_( \
Felix Paul Kühne's avatar
Felix Paul Kühne committed
58
      "Number of bands used by the spectrometer, from 20 to 80." )
zorglub's avatar
zorglub committed
59
60
61

#define SEPAR_TEXT N_( "Band separator" )
#define SEPAR_LONGTEXT N_( \
gbazin's avatar
   
gbazin committed
62
        "Number of blank pixels between bands.")
Laurent Aimar's avatar
Laurent Aimar committed
63

zorglub's avatar
zorglub committed
64
65
#define AMP_TEXT N_( "Amplification" )
#define AMP_LONGTEXT N_( \
gbazin's avatar
   
gbazin committed
66
        "This is a coefficient that modifies the height of the bands.")
Laurent Aimar's avatar
Laurent Aimar committed
67

zorglub's avatar
zorglub committed
68
69
#define PEAKS_TEXT N_( "Enable peaks" )
#define PEAKS_LONGTEXT N_( \
70
        "Draw \"peaks\" in the spectrum analyzer." )
zorglub's avatar
zorglub committed
71

72
73
#define ORIG_TEXT N_( "Enable original graphic spectrum" )
#define ORIG_LONGTEXT N_( \
74
        "Enable the \"flat\" spectrum analyzer in the spectrometer." )
75
76
77

#define BANDS_TEXT N_( "Enable bands" )
#define BANDS_LONGTEXT N_( \
78
        "Draw bands in the spectrometer." )
79
80
81
82
83
84
85
86
87

#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)." )

88
89
#define SSECT_TEXT N_( "Spectral sections" )
#define SSECT_LONGTEXT N_( \
90
91
92
93
        "Determines how many sections of spectrum will exist." )

#define PEAK_HEIGHT_TEXT N_( "Peak height" )
#define PEAK_HEIGHT_LONGTEXT N_( \
94
        "Total pixel height of the peak items." )
95
96
97
98
99
100
101
102
103

#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 )." )

zorglub's avatar
zorglub committed
104
105
#define STARS_TEXT N_( "Number of stars" )
#define STARS_LONGTEXT N_( \
106
        "Number of stars to draw with random effect." )
zorglub's avatar
zorglub committed
107

Laurent Aimar's avatar
Laurent Aimar committed
108
109
110
static int  Open         ( vlc_object_t * );
static void Close        ( vlc_object_t * );

111
vlc_module_begin();
112
    set_shortname( N_("Visualizer"));
zorglub's avatar
zorglub committed
113
114
    set_category( CAT_AUDIO );
    set_subcategory( SUBCAT_AUDIO_VISUAL );
115
    set_description( N_("Visualizer filter") );
zorglub's avatar
zorglub committed
116
    set_section( N_( "General") , NULL );
zorglub's avatar
zorglub committed
117
    add_string("effect-list", "spectrum", NULL,
118
            ELIST_TEXT, ELIST_LONGTEXT, true );
119
    add_integer("effect-width",VOUT_WIDTH,NULL,
120
             WIDTH_TEXT, WIDTH_LONGTEXT, false );
121
    add_integer("effect-height" , VOUT_HEIGHT , NULL,
122
             HEIGHT_TEXT, HEIGHT_LONGTEXT, false );
zorglub's avatar
zorglub committed
123
    set_section( N_("Spectrum analyser") , NULL );
zorglub's avatar
zorglub committed
124
    add_integer("visual-nbbands", 80, NULL,
125
             NBBANDS_TEXT, NBBANDS_LONGTEXT, true );
126
    add_integer("visual-separ", 1, NULL,
127
             SEPAR_TEXT, SEPAR_LONGTEXT, true );
128
    add_integer("visual-amp", 3, NULL,
129
130
131
             AMP_TEXT, AMP_LONGTEXT, true );
    add_bool("visual-peaks", true, NULL,
             PEAKS_TEXT, PEAKS_LONGTEXT, true );
132
    set_section( N_("Spectrometer") , NULL );
133
134
135
136
    add_bool("spect-show-original", false, NULL,
             ORIG_TEXT, ORIG_LONGTEXT, true );
    add_bool("spect-show-base", true, NULL,
             BASE_TEXT, BASE_LONGTEXT, true );
137
    add_integer("spect-radius", 42, NULL,
138
             RADIUS_TEXT, RADIUS_LONGTEXT, true );
139
    add_integer("spect-sections", 3, NULL,
140
             SSECT_TEXT, SSECT_LONGTEXT, true );
141
    add_integer("spect-color", 80, NULL,
142
143
144
             COLOR1_TEXT, COLOR1_LONGTEXT, true );
    add_bool("spect-show-bands", true, NULL,
             BANDS_TEXT, BANDS_LONGTEXT, true );
145
    add_integer("spect-nbbands", 32, NULL,
146
             NBBANDS_TEXT, SPNBBANDS_LONGTEXT, true );
147
    add_integer("spect-separ", 1, NULL,
148
             SEPAR_TEXT, SEPAR_LONGTEXT, true );
149
    add_integer("spect-amp", 8, NULL,
150
151
152
             AMP_TEXT, AMP_LONGTEXT, true );
    add_bool("spect-show-peaks", true, NULL,
             PEAKS_TEXT, PEAKS_LONGTEXT, true );
153
    add_integer("spect-peak-width", 61, NULL,
154
             PEAK_WIDTH_TEXT, PEAK_WIDTH_LONGTEXT, true );
155
    add_integer("spect-peak-height", 1, NULL,
156
             PEAK_HEIGHT_TEXT, PEAK_HEIGHT_LONGTEXT, true );
zorglub's avatar
zorglub committed
157
    set_capability( "visualization", 0 );
158
159
160
161
    set_callbacks( Open, Close );
    add_shortcut( "visualizer");
vlc_module_end();

Laurent Aimar's avatar
Laurent Aimar committed
162
163
164
165
166
167

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static void DoWork( aout_instance_t *, aout_filter_t *,
                    aout_buffer_t *, aout_buffer_t * );
168
169
static int FilterCallback( vlc_object_t *, char const *,
                           vlc_value_t, vlc_value_t, void * );
170
static const struct
Laurent Aimar's avatar
Laurent Aimar committed
171
{
Laurent Aimar's avatar
Laurent Aimar committed
172
    const char *psz_name;
Laurent Aimar's avatar
Laurent Aimar committed
173
174
175
176
177
    int  (*pf_run)( visual_effect_t *, aout_instance_t *,
                    aout_buffer_t *, picture_t *);
} pf_effect_run[]=
{
    { "scope",      scope_Run },
Adrien Maglo's avatar
Adrien Maglo committed
178
    { "vuMeter",    vuMeter_Run },
Laurent Aimar's avatar
Laurent Aimar committed
179
    { "spectrum",   spectrum_Run },
180
    { "spectrometer",   spectrometer_Run },
Laurent Aimar's avatar
Laurent Aimar committed
181
182
183
184
    { "dummy",      dummy_Run},
    { NULL,         dummy_Run}
};

185
186
187
188
189
/*****************************************************************************
 * Open: open the visualizer
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
Laurent Aimar's avatar
Laurent Aimar committed
190
191
    aout_filter_t     *p_filter = (aout_filter_t *)p_this;
    aout_filter_sys_t *p_sys;
192
    vlc_value_t        val;
Laurent Aimar's avatar
Laurent Aimar committed
193
194

    char *psz_effects, *psz_parser;
195
196
    video_format_t fmt;

Laurent Aimar's avatar
Laurent Aimar committed
197
198
199

    if( ( p_filter->input.i_format != VLC_FOURCC('f','l','3','2') &&
          p_filter->input.i_format != VLC_FOURCC('f','i','3','2') ) )
200
    {
Laurent Aimar's avatar
Laurent Aimar committed
201
        return VLC_EGENERIC;
202
203
    }

Laurent Aimar's avatar
Laurent Aimar committed
204
205
    p_sys = p_filter->p_sys = malloc( sizeof( aout_filter_sys_t ) );
    if( p_sys == NULL )
206
207
    {
        msg_Err( p_filter, "out of memory" );
Laurent Aimar's avatar
Laurent Aimar committed
208
        return VLC_EGENERIC;
209
210
    }

Laurent Aimar's avatar
Laurent Aimar committed
211
212
213
214
215
216
217
218
219
220
221
    p_sys->i_height = config_GetInt( p_filter , "effect-height");
    p_sys->i_width  = config_GetInt( p_filter , "effect-width");

    if( p_sys->i_height < 20 ) p_sys->i_height =  20;
    if( p_sys->i_width  < 20 ) p_sys->i_width  =  20;
    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;

222
    /* Parse the effect list */
223
224
225
226
    var_Create( p_filter, "effect-list", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
    var_Get( p_filter, "effect-list", &val);
    psz_parser = psz_effects = strdup( val.psz_string );
    free( val.psz_string );
227

228
    var_AddCallback( p_filter, "effect-list", FilterCallback, NULL );
229

Laurent Aimar's avatar
Laurent Aimar committed
230
    while( psz_parser && *psz_parser != '\0' )
231
    {
Laurent Aimar's avatar
Laurent Aimar committed
232
233
234
235
        visual_effect_t *p_effect;
        int  i;

        p_effect = malloc( sizeof( visual_effect_t ) );
Rémi Duraffort's avatar
Rémi Duraffort committed
236
237
        if( !p_effect )
            break;
Laurent Aimar's avatar
Laurent Aimar committed
238
239
240
241
242
243
244
245
246
247
        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->input);
        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++ )
248
        {
Laurent Aimar's avatar
Laurent Aimar committed
249
250
251
252
253
254
255
256
            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 = strdup( pf_effect_run[i].psz_name );
                break;
            }
257
        }
Laurent Aimar's avatar
Laurent Aimar committed
258
259

        if( p_effect->psz_name )
260
        {
Laurent Aimar's avatar
Laurent Aimar committed
261
262
263
264
265
266
267
268
269
270
            psz_parser += strlen( p_effect->psz_name );

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

                psz_parser++;

                if( ( psz_eoa = strchr( psz_parser, '}') ) == NULL )
                {
Christophe Mutricy's avatar
Christophe Mutricy committed
271
                   msg_Err( p_filter, "unable to parse effect list. Aborting");
Laurent Aimar's avatar
Laurent Aimar committed
272
273
                   break;
                }
274
275
                p_effect->psz_args =
                    strndup( psz_parser, psz_eoa - psz_parser);
Laurent Aimar's avatar
Laurent Aimar committed
276
            }
277
            TAB_APPEND( p_sys->i_effect, p_sys->effect, p_effect );
Laurent Aimar's avatar
Laurent Aimar committed
278
279
280
        }
        else
        {
281
282
            msg_Err( p_filter, "unknown visual effect: %s", psz_parser );
            free( p_effect );
283
284
        }

Laurent Aimar's avatar
Laurent Aimar committed
285
        if( strchr( psz_parser, ',' ) )
zorglub's avatar
zorglub committed
286
        {
Laurent Aimar's avatar
Laurent Aimar committed
287
            psz_parser = strchr( psz_parser, ',' ) + 1;
zorglub's avatar
zorglub committed
288
        }
Laurent Aimar's avatar
Laurent Aimar committed
289
        else if( strchr( psz_parser, ':' ) )
zorglub's avatar
zorglub committed
290
        {
Laurent Aimar's avatar
Laurent Aimar committed
291
            psz_parser = strchr( psz_parser, ':' ) + 1;
zorglub's avatar
zorglub committed
292
        }
Laurent Aimar's avatar
Laurent Aimar committed
293
294
        else
        {
295
            break;
Laurent Aimar's avatar
Laurent Aimar committed
296
297
298
299
300
301
        }
    }

    if( psz_effects )
    {
        free( psz_effects );
302
    }
Laurent Aimar's avatar
Laurent Aimar committed
303

304
305
306
307
308
309
    if( !p_sys->i_effect )
    {
        msg_Err( p_filter, "no effects found" );
        free( p_sys );
        return VLC_EGENERIC;
    }
310
311

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

314
315
316
317
318
    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_FOURCC('I','4','2','0');
    fmt.i_aspect = VOUT_ASPECT_FACTOR * p_sys->i_width/p_sys->i_height;
    fmt.i_sar_num = fmt.i_sar_den = 1;
Laurent Aimar's avatar
Laurent Aimar committed
319

320
    p_sys->p_vout = vout_Request( p_filter, NULL, &fmt );
Laurent Aimar's avatar
Laurent Aimar committed
321
    if( p_sys->p_vout == NULL )
322
323
    {
        msg_Err( p_filter, "no suitable vout module" );
Laurent Aimar's avatar
Laurent Aimar committed
324
325
        free( p_sys );
        return VLC_EGENERIC;
326
    }
Laurent Aimar's avatar
Laurent Aimar committed
327
328
329
330
331

    p_filter->pf_do_work = DoWork;
    p_filter->b_in_place= 1;

    return VLC_SUCCESS;
332
333
334
335
336
337
338
339
340
341
}

/*****************************************************************************
 * DoWork: convert a buffer
 *****************************************************************************
 * Audio part pasted from trivial.c
 ****************************************************************************/
static void DoWork( aout_instance_t *p_aout, aout_filter_t *p_filter,
                    aout_buffer_t *p_in_buf, aout_buffer_t *p_out_buf )
{
Laurent Aimar's avatar
Laurent Aimar committed
342
    aout_filter_sys_t *p_sys = p_filter->p_sys;
343
    picture_t *p_outpic;
Laurent Aimar's avatar
Laurent Aimar committed
344
345
    int i;

346
    p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
Laurent Aimar's avatar
Laurent Aimar committed
347
348
349
    p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes *
                            aout_FormatNbChannels( &p_filter->output ) /
                            aout_FormatNbChannels( &p_filter->input );
350
351

    /* First, get a new picture */
Laurent Aimar's avatar
Laurent Aimar committed
352
    while( ( p_outpic = vout_CreatePicture( p_sys->p_vout, 0, 0, 3 ) ) == NULL)
353
    {
354
        if( !vlc_object_alive (p_aout) )
Laurent Aimar's avatar
Laurent Aimar committed
355
356
357
358
        {
            return;
        }
        msleep( VOUT_OUTMEM_SLEEP );
359
360
361
    }

    /* Blank the picture */
Laurent Aimar's avatar
Laurent Aimar committed
362
    for( i = 0 ; i < p_outpic->i_planes ; i++ )
363
    {
Laurent Aimar's avatar
Laurent Aimar committed
364
        memset( p_outpic->p[i].p_pixels, i > 0 ? 0x80 : 0x00,
365
                p_outpic->p[i].i_visible_lines * p_outpic->p[i].i_pitch );
366
367
368
    }

    /* We can now call our visualization effects */
Laurent Aimar's avatar
Laurent Aimar committed
369
    for( i = 0; i < p_sys->i_effect; i++ )
370
    {
Laurent Aimar's avatar
Laurent Aimar committed
371
372
#define p_effect p_sys->effect[i]
        if( p_effect->pf_run )
373
        {
Laurent Aimar's avatar
Laurent Aimar committed
374
            p_effect->pf_run( p_effect, p_aout, p_out_buf, p_outpic );
375
        }
Laurent Aimar's avatar
Laurent Aimar committed
376
#undef p_effect
377
    }
Laurent Aimar's avatar
Laurent Aimar committed
378
379
380
381
382

    vout_DatePicture( p_sys->p_vout, p_outpic,
                      ( p_in_buf->start_date + p_in_buf->end_date ) / 2 );

    vout_DisplayPicture( p_sys->p_vout, p_outpic );
383
384
385
386
387
388
389
390
}

/*****************************************************************************
 * Close: close the plugin
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
    aout_filter_t * p_filter = (aout_filter_t *)p_this;
Laurent Aimar's avatar
Laurent Aimar committed
391
392
393
    aout_filter_sys_t *p_sys = p_filter->p_sys;

    int i;
394

Laurent Aimar's avatar
Laurent Aimar committed
395
    if( p_filter->p_sys->p_vout )
396
    {
397
        vout_Request( p_filter, p_filter->p_sys->p_vout, 0 );
398
    }
Laurent Aimar's avatar
Laurent Aimar committed
399
400
401

    /* Free the list */
    for( i = 0; i < p_sys->i_effect; i++ )
zorglub's avatar
zorglub committed
402
    {
Laurent Aimar's avatar
Laurent Aimar committed
403
#define p_effect p_sys->effect[i]
ivoire's avatar
ivoire committed
404
405
406
        free( p_effect->p_data );
        free( p_effect->psz_name );
        free( p_effect->psz_args );
Laurent Aimar's avatar
Laurent Aimar committed
407
408
        free( p_effect );
#undef p_effect
zorglub's avatar
zorglub committed
409
    }
Laurent Aimar's avatar
Laurent Aimar committed
410

ivoire's avatar
ivoire committed
411
    free( p_sys->effect );
Laurent Aimar's avatar
Laurent Aimar committed
412
    free( p_filter->p_sys );
413
}
414
415
416
417
418
419
420
421

/*****************************************************************************
 * 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 )
{
422
423
    VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
    VLC_UNUSED(p_data); VLC_UNUSED(newval);
424
425
    aout_filter_t     *p_filter = (aout_filter_t *)p_this;
    /* restart this baby */
Christophe Mutricy's avatar
Christophe Mutricy committed
426
    msg_Dbg( p_filter, "we should restart the visual filter" );
427
428
429
    return VLC_SUCCESS;
}