a52old.c 14 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * a52old.c: A52 decoder module main file
 *****************************************************************************
 * Copyright (C) 1999-2001 VideoLAN
5
 * $Id: a52old.c,v 1.6 2002/08/30 22:22:24 massiot Exp $
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
 *
 * Authors: Michel Lespinasse <walken@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>                                              /* memset() */

#include <vlc/vlc.h>
#include <vlc/aout.h>
#include <vlc/decoder.h>

34 35
#include "aout_internal.h"

36 37 38 39 40 41 42 43
#ifdef HAVE_UNISTD_H
#   include <unistd.h>                                           /* getpid() */
#endif

#include "imdct.h"
#include "downmix.h"
#include "adec.h"

44
#define A52DEC_FRAME_SIZE 1536
45 46 47 48 49 50

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  OpenDecoder       ( vlc_object_t * );
static int  RunDecoder        ( decoder_fifo_t * );
51 52 53
static int  InitThread        ( a52dec_t * );
static void EndThread         ( a52dec_t * );
static void BitstreamCallback ( bit_stream_t *, vlc_bool_t );
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

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
vlc_module_begin();
    add_category_hint( N_("Miscellaneous"), NULL );
    add_module  ( "a52-downmix", "downmix", NULL, NULL,
                  N_("A52 downmix module"), NULL );
    add_module  ( "a52-imdct", "imdct", NULL, NULL,
                  N_("A52 IMDCT module"), NULL );
    set_description( _("software A52 decoder") );
    set_capability( "decoder", 50 );
    set_callbacks( OpenDecoder, NULL );
    add_shortcut( "a52" );
vlc_module_end();         

/*****************************************************************************
 * OpenDecoder: probe the decoder and return score
 *****************************************************************************
 * Tries to launch a decoder and return score so that the interface is able 
 * to chose.
 *****************************************************************************/
static int OpenDecoder( vlc_object_t *p_this )
{
    decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
    
80 81
    if( p_fifo->i_fourcc != VLC_FOURCC('a','5','2',' ')
         && p_fifo->i_fourcc != VLC_FOURCC('a','5','2','b') )
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    {   
        return VLC_EGENERIC;
    }

    p_fifo->pf_run = RunDecoder;
    return VLC_SUCCESS;
}

/*****************************************************************************
 * RunDecoder: this function is called just after the thread is created
 *****************************************************************************/
static int RunDecoder( decoder_fifo_t *p_fifo )
{
    a52dec_t *   p_a52dec;
    void *       p_orig;                          /* pointer before memalign */
    vlc_bool_t   b_sync = 0;

99
    mtime_t i_pts = 0;
100 101
    aout_buffer_t *p_aout_buffer;
    audio_sample_format_t output_format;
102
    audio_date_t end_date;
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
    /* Allocate the memory needed to store the thread's structure */
    p_a52dec = (a52dec_t *)vlc_memalign( &p_orig, 16, sizeof(a52dec_t) );
    memset( p_a52dec, 0, sizeof( a52dec_t ) );

    if( p_a52dec == NULL )
    {
        msg_Err( p_fifo, "out of memory" );
        DecoderError( p_fifo );
        return( -1 );
    }

    /*
     * Initialize the thread properties
     */
    p_a52dec->p_fifo = p_fifo;
    if( InitThread( p_a52dec ) )
    {
        msg_Err( p_fifo, "could not initialize thread" );
        DecoderError( p_fifo );
        free( p_orig );
        return( -1 );
    }

    /* A52 decoder thread's main loop */
    /* FIXME : do we have enough room to store the decoded frames ?? */
    while ((!p_a52dec->p_fifo->b_die) && (!p_a52dec->p_fifo->b_error))
    {
        sync_info_t sync_info;

        if( !b_sync )
        {
             int i_sync_ptr;
#define p_bit_stream (&p_a52dec->bit_stream)

             /* Go to the next PES packet and jump to sync_ptr */
             do {
                BitstreamNextDataPacket( p_bit_stream );
             } while( !p_bit_stream->p_decoder_fifo->b_die
                       && !p_bit_stream->p_decoder_fifo->b_error
                       && p_bit_stream->p_data !=
                          p_bit_stream->p_decoder_fifo->p_first->p_first );
             i_sync_ptr = *(p_bit_stream->p_byte - 2) << 8
                            | *(p_bit_stream->p_byte - 1);
             p_bit_stream->p_byte += i_sync_ptr;

             /* Empty the bit FIFO and realign the bit stream */
             p_bit_stream->fifo.buffer = 0;
             p_bit_stream->fifo.i_available = 0;
             AlignWord( p_bit_stream );
             b_sync = 1;
#undef p_bit_stream
        }

        if (sync_frame (p_a52dec, &sync_info))
        {
            b_sync = 0;
            continue;
        }

163 164
        if( ( p_a52dec->p_aout_input == NULL )||
            ( output_format.i_rate != sync_info.sample_rate ) )
165
        {
166 167 168 169 170 171
            if( p_a52dec->p_aout_input )
            {
                /* Delete old output */
                msg_Warn( p_a52dec->p_fifo, "opening a new aout" );
                aout_InputDelete( p_a52dec->p_aout, p_a52dec->p_aout_input );
            }
172

173 174 175 176
            /* Set output configuration */
            output_format.i_format   = AOUT_FMT_S16_NE;
            output_format.i_channels = 2; /* FIXME ! */
            output_format.i_rate     = sync_info.sample_rate;
177
            aout_DateInit( &end_date, output_format.i_rate );
178 179 180
            p_a52dec->p_aout_input = aout_InputNew( p_a52dec->p_fifo,
                                                    &p_a52dec->p_aout,
                                                    &output_format );
181 182
        }

183 184 185 186 187
        if( p_a52dec->p_aout_input == NULL )
        {
            msg_Err( p_a52dec->p_fifo, "failed to create aout fifo" );
            p_a52dec->p_fifo->b_error = 1;
            continue;
188 189
        }

190
        NextPTS( &p_a52dec->bit_stream, &i_pts, NULL );
191 192 193 194 195 196 197 198 199 200
        if( i_pts != 0 && i_pts != aout_DateGet( &end_date ) )
        {
            aout_DateSet( &end_date, i_pts );
        }

        if( !aout_DateGet( &end_date ) )
        {
            continue;
        }

201 202 203 204
        p_aout_buffer = aout_BufferNew( p_a52dec->p_aout,
                                        p_a52dec->p_aout_input,
                                        A52DEC_FRAME_SIZE );
        if( !p_aout_buffer )
205
        {
206 207 208
            msg_Err( p_a52dec->p_fifo, "cannot get aout buffer" );
            p_a52dec->p_fifo->b_error = 1;
            continue;
209 210
        }

211 212 213
        p_aout_buffer->start_date = aout_DateGet( &end_date );
        p_aout_buffer->end_date = aout_DateIncrement( &end_date,
                                                      A52DEC_FRAME_SIZE );
214 215

        if (decode_frame (p_a52dec, (s16*)p_aout_buffer->p_buffer))
216 217
        {
            b_sync = 0;
218 219
            aout_BufferDelete( p_a52dec->p_aout, p_a52dec->p_aout_input,
                                                 p_aout_buffer );
220 221
            continue;
        }
222 223 224 225 226
        else
        {
            aout_BufferPlay( p_a52dec->p_aout, p_a52dec->p_aout_input,
                                               p_aout_buffer );
        }
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

        RealignBits(&p_a52dec->bit_stream);
    }

    /* If b_error is set, the A52 decoder thread enters the error loop */
    if (p_a52dec->p_fifo->b_error)
    {
        DecoderError( p_a52dec->p_fifo );
    }

    /* End of the A52 decoder thread */
    EndThread (p_a52dec);

    free( p_orig );

    return( 0 );
}

/*****************************************************************************
 * InitThread: initialize data before entering main loop
 *****************************************************************************/
static int InitThread( a52dec_t * p_a52dec )
{
    /*
     * Choose the best downmix module
     */
    p_a52dec->p_downmix = vlc_object_create( p_a52dec->p_fifo,
                                             sizeof( downmix_t ) );
    p_a52dec->p_downmix->psz_object_name = "downmix";

    p_a52dec->p_downmix->p_module =
258
                module_Need( p_a52dec->p_downmix, "downmix", "$a52-downmix" );
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274

    if( p_a52dec->p_downmix->p_module == NULL )
    {
        msg_Err( p_a52dec->p_fifo, "no suitable downmix module" );
        vlc_object_destroy( p_a52dec->p_downmix );
        return( -1 );
    }

    /*
     * Choose the best IMDCT module
     */
    p_a52dec->p_imdct = vlc_object_create( p_a52dec->p_fifo,
                                           sizeof( imdct_t ) );
    
#define IMDCT p_a52dec->p_imdct
    p_a52dec->p_imdct->p_module =
275
                   module_Need( p_a52dec->p_imdct, "imdct", "$a52-imdct" );
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

    if( p_a52dec->p_imdct->p_module == NULL )
    {
        msg_Err( p_a52dec->p_fifo, "no suitable IMDCT module" );
        vlc_object_destroy( p_a52dec->p_imdct );
        module_Unneed( p_a52dec->p_downmix, p_a52dec->p_downmix->p_module );
        vlc_object_destroy( p_a52dec->p_downmix );
        return( -1 );
    }

    /* Initialize the A52 decoder structures */
    p_a52dec->samples = vlc_memalign( &p_a52dec->samples_orig,
                                      16, 6 * 256 * sizeof(float) );

    IMDCT->buf    = vlc_memalign( &IMDCT->buf_orig,
                                  16, N/4 * sizeof(complex_t) );
    IMDCT->delay  = vlc_memalign( &IMDCT->delay_orig,
                                  16, 6 * 256 * sizeof(float) );
    IMDCT->delay1 = vlc_memalign( &IMDCT->delay1_orig,
                                  16, 6 * 256 * sizeof(float) );
    IMDCT->xcos1  = vlc_memalign( &IMDCT->xcos1_orig,
                                  16, N/4 * sizeof(float) );
    IMDCT->xsin1  = vlc_memalign( &IMDCT->xsin1_orig,
                                  16, N/4 * sizeof(float) );
    IMDCT->xcos2  = vlc_memalign( &IMDCT->xcos2_orig,
                                  16, N/8 * sizeof(float) );
    IMDCT->xsin2  = vlc_memalign( &IMDCT->xsin2_orig,
                                  16, N/8 * sizeof(float) );
    IMDCT->xcos_sin_sse = vlc_memalign( &IMDCT->xcos_sin_sse_orig,
                                        16, 128 * 4 * sizeof(float) );
    IMDCT->w_1    = vlc_memalign( &IMDCT->w_1_orig,
                                  16, 1  * sizeof(complex_t) );
    IMDCT->w_2    = vlc_memalign( &IMDCT->w_2_orig,
                                  16, 2  * sizeof(complex_t) );
    IMDCT->w_4    = vlc_memalign( &IMDCT->w_4_orig,
                                  16, 4  * sizeof(complex_t) );
    IMDCT->w_8    = vlc_memalign( &IMDCT->w_8_orig,
                                  16, 8  * sizeof(complex_t) );
    IMDCT->w_16   = vlc_memalign( &IMDCT->w_16_orig,
                                  16, 16 * sizeof(complex_t) );
    IMDCT->w_32   = vlc_memalign( &IMDCT->w_32_orig,
                                  16, 32 * sizeof(complex_t) );
    IMDCT->w_64   = vlc_memalign( &IMDCT->w_64_orig,
                                  16, 64 * sizeof(complex_t) );
#undef IMDCT

    E_( a52_init )( p_a52dec );

    /*
     * Initialize the output properties
     */
327 328
    p_a52dec->p_aout = NULL;
    p_a52dec->p_aout_input = NULL;
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344

    /*
     * Bit stream
     */
    InitBitstream( &p_a52dec->bit_stream, p_a52dec->p_fifo,
                   BitstreamCallback, (void *) p_a52dec );
    
    return( 0 );
}

/*****************************************************************************
 * EndThread : A52 decoder thread destruction
 *****************************************************************************/
static void EndThread (a52dec_t * p_a52dec)
{
    /* If the audio output fifo was created, we destroy it */
345
    if( p_a52dec->p_aout_input )
346
    {
347
        aout_InputDelete( p_a52dec->p_aout, p_a52dec->p_aout_input );
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
    }

    /* Free allocated structures */
#define IMDCT p_a52dec->p_imdct
    free( IMDCT->w_1_orig );
    free( IMDCT->w_64_orig );
    free( IMDCT->w_32_orig );
    free( IMDCT->w_16_orig );
    free( IMDCT->w_8_orig );
    free( IMDCT->w_4_orig );
    free( IMDCT->w_2_orig );
    free( IMDCT->xcos_sin_sse_orig );
    free( IMDCT->xsin2_orig );
    free( IMDCT->xcos2_orig );
    free( IMDCT->xsin1_orig );
    free( IMDCT->xcos1_orig );
    free( IMDCT->delay1_orig );
    free( IMDCT->delay_orig );
    free( IMDCT->buf_orig );
#undef IMDCT

    free( p_a52dec->samples_orig );

    /* Unlock the modules */
    module_Unneed( p_a52dec->p_downmix, p_a52dec->p_downmix->p_module );
    vlc_object_destroy( p_a52dec->p_downmix );

    module_Unneed( p_a52dec->p_imdct, p_a52dec->p_imdct->p_module );
    vlc_object_destroy( p_a52dec->p_imdct );

    /* Free what's left of the decoder */
    free( p_a52dec->imdct_orig );
}

/*****************************************************************************
 * BitstreamCallback: Import parameters from the new data/PES packet
 *****************************************************************************
 * This function is called by input's NextDataPacket.
 *****************************************************************************/
static void BitstreamCallback ( bit_stream_t * p_bit_stream,
                                vlc_bool_t b_new_pes )
{
    if( b_new_pes )
    {
        /* Drop special A52 header */
/*        p_bit_stream->p_byte += 3; */
    }
}