scope.c 11 KB
Newer Older
Sam Hocevar's avatar
 
Sam Hocevar committed
1 2 3 4
/*****************************************************************************
 * scope.c : Scope effect module
 *****************************************************************************
 * Copyright (C) 2002 VideoLAN
Sam Hocevar's avatar
 
Sam Hocevar committed
5
 * $Id: scope.c,v 1.4 2002/03/02 03:51:23 sam Exp $
Sam Hocevar's avatar
 
Sam Hocevar committed
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
 *
 * Authors: Samuel Hocevar <sam@zoy.org>
 *
 * 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 <videolan/vlc.h>

#include "video.h"
#include "video_output.h"

#include "audio_output.h"                                   /* aout_thread_t */

Sam Hocevar's avatar
 
Sam Hocevar committed
38 39
#define SCOPE_WIDTH 320
#define SCOPE_HEIGHT 240
Sam Hocevar's avatar
 
Sam Hocevar committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#define SCOPE_ASPECT (VOUT_ASPECT_FACTOR*SCOPE_WIDTH/SCOPE_HEIGHT)

/*****************************************************************************
 * Capabilities defined in the other files.
 *****************************************************************************/
static void aout_getfunctions( function_list_t * p_function_list );

/*****************************************************************************
 * aout_sys_t: scope audio output method descriptor
 *****************************************************************************
 * This structure is part of the audio output thread descriptor.
 * It describes some scope specific variables.
 *****************************************************************************/
typedef struct aout_sys_s
{
Sam Hocevar's avatar
 
Sam Hocevar committed
55 56 57
    struct aout_thread_s aout;
    struct aout_fifo_s *p_aout_fifo;

Sam Hocevar's avatar
 
Sam Hocevar committed
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
    struct vout_thread_s *p_vout;

} aout_sys_t;

/*****************************************************************************
 * Build configuration tree.
 *****************************************************************************/
MODULE_CONFIG_START
MODULE_CONFIG_STOP

MODULE_INIT_START
    SET_DESCRIPTION( "scope effect module" )
    ADD_CAPABILITY( AOUT, 0 )
    ADD_SHORTCUT( "scope" )
MODULE_INIT_STOP

MODULE_ACTIVATE_START
    aout_getfunctions( &p_module->p_functions->aout );
MODULE_ACTIVATE_STOP

MODULE_DEACTIVATE_START
MODULE_DEACTIVATE_STOP

/*****************************************************************************
 * Local prototypes.
 *****************************************************************************/
static int     aout_Open        ( aout_thread_t *p_aout );
static int     aout_SetFormat   ( aout_thread_t *p_aout );
static int     aout_GetBufInfo  ( aout_thread_t *p_aout, int i_buffer_info );
static void    aout_Play        ( aout_thread_t *p_aout,
                                  byte_t *buffer, int i_size );
static void    aout_Close       ( aout_thread_t *p_aout );

/*****************************************************************************
 * Functions exported as capabilities. They are declared as static so that
 * we don't pollute the namespace too much.
 *****************************************************************************/
static void aout_getfunctions( function_list_t * p_function_list )
{
    p_function_list->functions.aout.pf_open = aout_Open;
    p_function_list->functions.aout.pf_setformat = aout_SetFormat;
    p_function_list->functions.aout.pf_getbufinfo = aout_GetBufInfo;
    p_function_list->functions.aout.pf_play = aout_Play;
    p_function_list->functions.aout.pf_close = aout_Close;
}

/*****************************************************************************
 * aout_Open: open a scope effect plugin
 *****************************************************************************/
static int aout_Open( aout_thread_t *p_aout )
{
    /* Allocate structure */
    p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
    if( p_aout->p_sys == NULL )
    {
        intf_ErrMsg("error: %s", strerror(ENOMEM) );
Sam Hocevar's avatar
 
Sam Hocevar committed
114
        return -1;
Sam Hocevar's avatar
 
Sam Hocevar committed
115 116
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
117
    /* Open video output */
Sam Hocevar's avatar
 
Sam Hocevar committed
118 119 120 121
    p_aout->p_sys->p_vout =
        vout_CreateThread( NULL, SCOPE_WIDTH, SCOPE_HEIGHT,
                           FOURCC_I420, SCOPE_ASPECT );

Sam Hocevar's avatar
 
Sam Hocevar committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    /* Open audio output  */
    p_aout->p_sys->aout.i_format   = p_aout->i_format;
    p_aout->p_sys->aout.i_rate     = p_aout->i_rate;
    p_aout->p_sys->aout.i_channels = p_aout->i_channels;

    p_aout->p_sys->aout.p_module = module_Need( MODULE_CAPABILITY_AOUT, "",
                                    (void *)&p_aout->p_sys->aout );
    if( p_aout->p_sys->aout.p_module == NULL )
    {
        intf_ErrMsg( "aout error: no suitable aout module" );
        vout_DestroyThread( p_aout->p_sys->p_vout, NULL );
        free( p_aout->p_sys );
        return -1;
    }

#define aout_functions p_aout->p_sys->aout.p_module->p_functions->aout.functions.aout
    p_aout->p_sys->aout.pf_open       = aout_functions.pf_open;
    p_aout->p_sys->aout.pf_setformat  = aout_functions.pf_setformat;
    p_aout->p_sys->aout.pf_getbufinfo = aout_functions.pf_getbufinfo;
    p_aout->p_sys->aout.pf_play       = aout_functions.pf_play;
    p_aout->p_sys->aout.pf_close      = aout_functions.pf_close;
#undef aout_functions
    
Sam Hocevar's avatar
 
Sam Hocevar committed
145 146 147 148 149 150 151 152
    return( 0 );
}

/*****************************************************************************
 * aout_SetFormat: set the output format
 *****************************************************************************/
static int aout_SetFormat( aout_thread_t *p_aout )
{
Sam Hocevar's avatar
 
Sam Hocevar committed
153
    int i_ret;
Sam Hocevar's avatar
 
Sam Hocevar committed
154

Sam Hocevar's avatar
 
Sam Hocevar committed
155
    /* Force the output method */
Sam Hocevar's avatar
 
Sam Hocevar committed
156 157 158 159
    p_aout->p_sys->aout.i_format = p_aout->i_format;
    p_aout->p_sys->aout.i_channels = p_aout->i_channels;
    p_aout->p_sys->aout.i_rate = p_aout->i_rate;

Sam Hocevar's avatar
 
Sam Hocevar committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
    /*
     * Initialize audio device
     */
    i_ret = p_aout->p_sys->aout.pf_setformat( &p_aout->p_sys->aout );

    if( i_ret )
    {
        return i_ret;
    }

    if( p_aout->p_sys->aout.i_format != p_aout->i_format
         || p_aout->p_sys->aout.i_channels != p_aout->i_channels )
    {
        intf_ErrMsg( "aout error: plugin isn't cooperative" );
        return 0;
    }

    p_aout->i_channels = p_aout->p_sys->aout.i_channels;
    p_aout->i_format = p_aout->p_sys->aout.i_format;
    p_aout->i_rate = p_aout->p_sys->aout.i_rate;

    return 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
182 183 184 185 186 187 188
}

/*****************************************************************************
 * aout_GetBufInfo: buffer status query
 *****************************************************************************/
static int aout_GetBufInfo( aout_thread_t *p_aout, int i_buffer_limit )
{
Sam Hocevar's avatar
 
Sam Hocevar committed
189 190
    return p_aout->p_sys->aout.pf_getbufinfo( &p_aout->p_sys->aout,
                                              i_buffer_limit );
Sam Hocevar's avatar
 
Sam Hocevar committed
191 192 193 194 195 196 197 198 199 200
}

/*****************************************************************************
 * aout_Play: play a sound samples buffer
 *****************************************************************************
 * This function writes a buffer of i_length bytes in the socket
 *****************************************************************************/
static void aout_Play( aout_thread_t *p_aout, byte_t *p_buffer, int i_size )
{
    picture_t *p_outpic;
Sam Hocevar's avatar
 
Sam Hocevar committed
201 202
    int i_index, i_image;
    u8  *ppp_area[2][3];
Sam Hocevar's avatar
 
Sam Hocevar committed
203 204
    u16 *p_sample;

Sam Hocevar's avatar
 
Sam Hocevar committed
205 206 207 208
    /* Play the real sound */
    p_aout->p_sys->aout.pf_play( &p_aout->p_sys->aout, p_buffer, i_size );

    for( i_image = 0; (i_image + 1) * SCOPE_WIDTH * 8 < i_size ; i_image++ )
Sam Hocevar's avatar
 
Sam Hocevar committed
209
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
210 211
        /* Don't stay here forever */
        if( mdate() >= p_aout->date - 10000 )
Sam Hocevar's avatar
 
Sam Hocevar committed
212
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
213
            break;
Sam Hocevar's avatar
 
Sam Hocevar committed
214 215
        }

Sam Hocevar's avatar
 
Sam Hocevar committed
216 217 218 219 220 221 222 223 224 225
        /* This is a new frame. Get a structure from the video_output. */
        while( ( p_outpic = vout_CreatePicture( p_aout->p_sys->p_vout, 0, 0, 0 ) )
                  == NULL )
        {
            if( p_aout->b_die )
            {
                return;
            }
            msleep( VOUT_OUTMEM_SLEEP );
        }
Sam Hocevar's avatar
 
Sam Hocevar committed
226

Sam Hocevar's avatar
 
Sam Hocevar committed
227 228
        /* Blank the picture */
        for( i_index = 0 ; i_index < p_outpic->i_planes ; i_index++ )
Sam Hocevar's avatar
 
Sam Hocevar committed
229
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
230 231
            memset( p_outpic->p[i_index].p_pixels, i_index ? 0x80 : 0x00,
                    p_outpic->p[i_index].i_lines * p_outpic->p[i_index].i_pitch );
Sam Hocevar's avatar
 
Sam Hocevar committed
232 233
        }

Sam Hocevar's avatar
 
Sam Hocevar committed
234 235 236
        /* We only support 2 channels for now */
        for( i_index = 0 ; i_index < 2 ; i_index++ )
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
237 238 239 240 241 242 243
            int j;
            for( j = 0 ; j < 3 ; j++ )
            {
                ppp_area[i_index][j] =
                    p_outpic->p[j].p_pixels + i_index * p_outpic->p[j].i_lines
                                / p_aout->i_channels * p_outpic->p[j].i_pitch;
            }
Sam Hocevar's avatar
 
Sam Hocevar committed
244
        }
Sam Hocevar's avatar
 
Sam Hocevar committed
245

Sam Hocevar's avatar
 
Sam Hocevar committed
246 247 248
        for( i_index = 0, p_sample = (u16*)p_buffer;
             i_index < SCOPE_WIDTH;
             i_index++ )
Sam Hocevar's avatar
 
Sam Hocevar committed
249
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
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
            int i;
            u8 i_value;

            for( i = 0 ; i < 2 ; i++ )
            {
                /* Left channel */
                i_value = *p_sample++ / 256 + 128;
                *(ppp_area[0][0]
                   + p_outpic->p[0].i_pitch * i_index / SCOPE_WIDTH
                   + p_outpic->p[0].i_lines * i_value / 512
                       * p_outpic->p[0].i_pitch) = 0xbf;
                *(ppp_area[0][1]
                   + p_outpic->p[1].i_pitch * i_index / SCOPE_WIDTH
                   + p_outpic->p[1].i_lines * i_value / 512
                      * p_outpic->p[1].i_pitch) = 0xff;

                /* Right channel */
                i_value = *p_sample++ / 256 + 128;
                *(ppp_area[1][0]
                   + p_outpic->p[0].i_pitch * i_index / SCOPE_WIDTH
                   + p_outpic->p[0].i_lines * i_value / 512
                      * p_outpic->p[0].i_pitch) = 0x9f;
                *(ppp_area[1][2]
                   + p_outpic->p[2].i_pitch * i_index / SCOPE_WIDTH
                   + p_outpic->p[2].i_lines * i_value / 512
                      * p_outpic->p[2].i_pitch) = 0xdd;
            }
Sam Hocevar's avatar
 
Sam Hocevar committed
277 278
        }

Sam Hocevar's avatar
 
Sam Hocevar committed
279 280
        /* Display the picture - FIXME: find a better date :-) */
        vout_DatePicture( p_aout->p_sys->p_vout, p_outpic,
Sam Hocevar's avatar
 
Sam Hocevar committed
281
                          p_aout->date + i_image * 20000 );
Sam Hocevar's avatar
 
Sam Hocevar committed
282 283 284 285
        vout_DisplayPicture( p_aout->p_sys->p_vout, p_outpic );

        p_buffer += SCOPE_WIDTH * 4;
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
286 287 288 289 290 291 292
}

/*****************************************************************************
 * aout_Close: close the Esound socket
 *****************************************************************************/
static void aout_Close( aout_thread_t *p_aout )
{
Sam Hocevar's avatar
 
Sam Hocevar committed
293 294
    p_aout->p_sys->aout.pf_close( &p_aout->p_sys->aout );
    module_Unneed( p_aout->p_sys->aout.p_module );
Sam Hocevar's avatar
 
Sam Hocevar committed
295 296 297 298
    vout_DestroyThread( p_aout->p_sys->p_vout, NULL );
    free( p_aout->p_sys );
}