goom.c 10.5 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * goom.c: based on libgoom (see http://ios.free.fr/?page=projet&quoi=1)
 *****************************************************************************
 * Copyright (C) 2003 VideoLAN
5
 * $Id$
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
 *
 * Authors: Laurent Aimar
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>                                      /* malloc(), free() */
#include <string.h>                                              /* strdup() */
#include <errno.h>

#include <vlc/vlc.h>
#include <vlc/input.h>
#include <vlc/aout.h>
#include <vlc/vout.h>
#include "aout_internal.h"

37
#include "goom_core.h"
38 39 40 41 42 43 44

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  Open         ( vlc_object_t * );
static void Close        ( vlc_object_t * );

45 46 47 48 49
#define WIDTH_TEXT N_("Goom display width")
#define HEIGHT_TEXT N_("Goom display height")
#define RES_LONGTEXT N_("Allows you to change the resolution of the " \
    "goom display (bigger resolution will be prettier but more CPU intensive)")

50 51 52
vlc_module_begin();
    set_description( _("goom effect") );
    set_capability( "audio filter", 0 );
53 54 55 56
    add_integer( "goom-width", 320, NULL,
                 WIDTH_TEXT, RES_LONGTEXT, VLC_FALSE );
    add_integer( "goom-height", 240, NULL,
                 HEIGHT_TEXT, RES_LONGTEXT, VLC_FALSE );
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
    set_callbacks( Open, Close );
    add_shortcut( "goom" );
vlc_module_end();

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/

typedef struct
{
    VLC_COMMON_MEMBERS
    vout_thread_t *p_vout;

    char          *psz_title;

    vlc_mutex_t   lock;
    vlc_cond_t    wait;

    mtime_t       i_pts;
    int           i_samples;
    int16_t       samples[2][512];
} goom_thread_t;

typedef struct aout_filter_sys_t
{
    goom_thread_t *p_thread;

} aout_filter_sys_t;

86 87
static void DoWork   ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
                       aout_buffer_t * );
88

89
static void Thread   ( vlc_object_t * );
90 91 92 93 94 95 96 97 98 99

static char *TitleGet( vlc_object_t * );

/*****************************************************************************
 * Open: open a scope effect plugin
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    aout_filter_t     *p_filter = (aout_filter_t *)p_this;
    aout_filter_sys_t *p_sys;
100 101
    goom_thread_t     *p_thread;
    vlc_value_t       width, height;
102 103 104 105 106

    if ( p_filter->input.i_format != VLC_FOURCC('f','l','3','2' )
         || p_filter->output.i_format != VLC_FOURCC('f','l','3','2') )
    {
        msg_Warn( p_filter, "Bad input or output format" );
107
        return VLC_EGENERIC;
108 109 110 111
    }
    if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
    {
        msg_Warn( p_filter, "input and output formats are not similar" );
112
        return VLC_EGENERIC;
113 114 115 116 117 118 119 120 121
    }

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

    /* Allocate structure */
    p_sys = p_filter->p_sys = malloc( sizeof( aout_filter_sys_t ) );

    /* Create goom thread */
122 123 124 125 126 127 128 129 130 131 132 133 134 135
    p_sys->p_thread = p_thread =
        vlc_object_create( p_filter, sizeof( goom_thread_t ) );
    vlc_object_attach( p_thread, p_this );

    var_Create( p_thread, "goom-width", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
    var_Get( p_thread, "goom-width", &width );
    var_Create( p_thread, "goom-height", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
    var_Get( p_thread, "goom-height", &height );

    p_thread->p_vout =
        vout_Request( p_filter, NULL, width.i_int, height.i_int,
                      VLC_FOURCC('R','V','3','2'),
                      VOUT_ASPECT_FACTOR * width.i_int/height.i_int );
    if( p_thread->p_vout == NULL )
136 137
    {
        msg_Err( p_filter, "no suitable vout module" );
138 139 140
        vlc_object_detach( p_thread );
        vlc_object_destroy( p_thread );
        free( p_sys );
141 142
        return VLC_EGENERIC;
    }
143 144
    vlc_mutex_init( p_filter, &p_thread->lock );
    vlc_cond_init( p_filter, &p_thread->wait );
145

146 147 148
    p_thread->i_samples = 0;
    memset( &p_thread->samples, 0, 512*2*2 );
    p_thread->psz_title = TitleGet( VLC_OBJECT( p_filter ) );
149

150
    if( vlc_thread_create( p_thread, "Goom Update Thread", Thread,
151 152 153
                           VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
    {
        msg_Err( p_filter, "cannot lauch goom thread" );
154 155 156 157 158 159 160
        vout_Destroy( p_thread->p_vout );
        vlc_mutex_destroy( &p_thread->lock );
        vlc_cond_destroy( &p_thread->wait );
        if( p_thread->psz_title ) free( p_thread->psz_title );
        vlc_object_detach( p_thread );
        vlc_object_destroy( p_thread );
        free( p_sys );
161 162 163
        return VLC_EGENERIC;
    }

164
    return VLC_SUCCESS;
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
}

/*****************************************************************************
 * Play: play a sound samples buffer
 *****************************************************************************
 * This function writes a buffer of i_length bytes in the socket
 *****************************************************************************/
static inline int16_t FloatToInt16( float f )
{
    if( f >= 1.0 )
        return 32767;
    else if( f < -1.0 )
        return -32768;
    else
        return (int16_t)( f * 32768.0 );
}

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 )
{
    aout_filter_sys_t *p_sys = p_filter->p_sys;

    int16_t *p_isample;
    float   *p_fsample = (float*)p_in_buf->p_buffer;
    int     i_samples = 0;
    int     i_channels = aout_FormatNbChannels( &p_filter->input );

    p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
    p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes;

    /* copy samples */
    vlc_mutex_lock( &p_sys->p_thread->lock );
    p_sys->p_thread->i_pts = p_in_buf->start_date;
    if( p_sys->p_thread->i_samples >= 512 )
    {
        p_sys->p_thread->i_samples = 0;
    }
    p_isample = &p_sys->p_thread->samples[0][p_sys->p_thread->i_samples];

    i_samples = __MIN( 512 - p_sys->p_thread->i_samples,
                       (int)p_out_buf->i_nb_samples );
    p_sys->p_thread->i_samples += i_samples;

    while( i_samples > 0 )
    {
        p_isample[0] = FloatToInt16( p_fsample[0] );
        if( i_channels > 1 )
        {
            p_isample[512] = FloatToInt16( p_fsample[1] );
        }

        p_isample++;
        p_fsample += i_channels;

        i_samples--;
    }
    if( p_sys->p_thread->i_samples == 512 )
    {
        vlc_cond_signal( &p_sys->p_thread->wait );
    }
    vlc_mutex_unlock( &p_sys->p_thread->lock );

}
/*****************************************************************************
 * Thread:
 *****************************************************************************/
static void Thread( vlc_object_t *p_this )
{
    goom_thread_t *p_thread = (goom_thread_t*)p_this;
234
    vlc_value_t width, height;
235

236 237 238 239
    var_Get( p_this, "goom-width", &width );
    var_Get( p_this, "goom-height", &height );

    goom_init( width.i_int, height.i_int, 0 );
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
    goom_set_font( NULL, NULL, NULL );

    while( !p_thread->b_die )
    {
        mtime_t   i_pts;
        int16_t   data[2][512];
        uint32_t  *plane;
        picture_t *p_pic;

        /* goom_update is damn slow, so just copy data and release the lock */
        vlc_mutex_lock( &p_thread->lock );
        vlc_cond_wait( &p_thread->wait, &p_thread->lock );
        i_pts = p_thread->i_pts;
        memcpy( data, p_thread->samples, 512 * 2 * 2 );
        vlc_mutex_unlock( &p_thread->lock );

        plane = goom_update( data, 0, 0.0, p_thread->psz_title, NULL );

        if( p_thread->psz_title )
        {
            free( p_thread->psz_title );
            p_thread->psz_title = NULL;
        }
263
        while( !( p_pic = vout_CreatePicture( p_thread->p_vout, 0, 0, 0 ) ) &&
264 265 266 267 268
               !p_thread->b_die )
        {
            msleep( VOUT_OUTMEM_SLEEP );
        }

269
        if( p_pic == NULL )
270 271 272 273
        {
            break;
        }

274
        memcpy( p_pic->p[0].p_pixels, plane, width.i_int * height.i_int * 4 );
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
        vout_DatePicture( p_thread->p_vout, p_pic, i_pts );
        vout_DisplayPicture( p_thread->p_vout, p_pic );

    }

    goom_close();
}


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

    /* Stop Goom Thread */
    p_sys->p_thread->b_die = VLC_TRUE;

    vlc_mutex_lock( &p_sys->p_thread->lock );
    vlc_cond_signal( &p_sys->p_thread->wait );
    vlc_mutex_unlock( &p_sys->p_thread->lock );

    vlc_thread_join( p_sys->p_thread );

    /* Free data */
    vout_Request( p_filter, p_sys->p_thread->p_vout, 0, 0, 0, 0 );
    vlc_mutex_destroy( &p_sys->p_thread->lock );
    vlc_cond_destroy( &p_sys->p_thread->wait );
305
    vlc_object_detach( p_sys->p_thread );
306 307 308 309 310 311 312 313
    vlc_object_destroy( p_sys->p_thread );

    free( p_sys );
}


static char *TitleGet( vlc_object_t *p_this )
{
314 315 316
    char *psz_title = NULL;
    input_thread_t *p_input =
        vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338

    if( p_input )
    {
        char *psz = strrchr( p_input->psz_source, '/' );

        if( psz )
        {
            psz++;
        }
        else
        {
            psz = p_input->psz_source;
        }
        if( psz && *psz )
        {
            psz_title = strdup( psz );
        }
        vlc_object_release( p_input );
    }

    return psz_title;
}