ac3_decoder.c 14.9 KB
Newer Older
1 2 3 4 5
/*****************************************************************************
 * ac3_decoder.c: ac3 decoder thread
 * (c)1999 VideoLAN
 *****************************************************************************/

6 7 8 9 10 11 12 13 14
/*
 * TODO :
 *
 * - vrifier l'tat de la fifo de sortie avant d'y stocker les samples
 *   dcods ;
 * - vlc_cond_signal() / vlc_cond_wait()
 *
 */

15 16 17
/*****************************************************************************
 * Preamble
 *****************************************************************************/
18 19 20 21
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include <unistd.h>                                              /* getpid() */

#include <stdio.h>                                           /* "intf_msg.h" */
#include <stdlib.h>                                      /* malloc(), free() */
#include <sys/soundcard.h>                               /* "audio_output.h" */
#include <sys/uio.h>                                            /* "input.h" */

#include "common.h"
#include "config.h"
#include "mtime.h"
#include "vlc_thread.h"
#include "debug.h"                                      /* "input_netlist.h" */

#include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */

#include "input.h"                                           /* pes_packet_t */
#include "input_netlist.h"                         /* input_NetlistFreePES() */
#include "decoder_fifo.h"         /* DECODER_FIFO_(ISEMPTY|START|INCSTART)() */

#include "audio_output.h"

#include "ac3_decoder.h"
44 45 46 47 48 49 50
#include "ac3_parse.h"
#include "ac3_exponent.h"
#include "ac3_bit_allocate.h"
#include "ac3_mantissa.h"
#include "ac3_rematrix.h"
#include "ac3_imdct.h"
#include "ac3_downmix.h"
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

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int      InitThread              ( ac3dec_thread_t * p_adec );
static void     RunThread               ( ac3dec_thread_t * p_adec );
static void     ErrorThread             ( ac3dec_thread_t * p_adec );
static void     EndThread               ( ac3dec_thread_t * p_adec );

/*****************************************************************************
 * ac3dec_CreateThread: creates an ac3 decoder thread
 *****************************************************************************/
ac3dec_thread_t * ac3dec_CreateThread( input_thread_t * p_input )
{
    ac3dec_thread_t *   p_ac3dec;

    intf_DbgMsg("ac3dec debug: creating ac3 decoder thread\n");

    /* Allocate the memory needed to store the thread's structure */
    if ( (p_ac3dec = (ac3dec_thread_t *)malloc( sizeof(ac3dec_thread_t) )) == NULL )
    {
        intf_ErrMsg("ac3dec error: not enough memory for ac3dec_CreateThread() to create the new thread\n");
        return( NULL );
    }

    /*
     * Initialize the thread properties
     */
    p_ac3dec->b_die = 0;
    p_ac3dec->b_error = 0;

    /*
     * Initialize the input properties
     */
    /* Initialize the decoder fifo's data lock and conditional variable and set
     * its buffer as empty */
    vlc_mutex_init( &p_ac3dec->fifo.data_lock );
    vlc_cond_init( &p_ac3dec->fifo.data_wait );
    p_ac3dec->fifo.i_start = 0;
    p_ac3dec->fifo.i_end = 0;
    /* Initialize the bit stream structure */
    p_ac3dec->bit_stream.p_input = p_input;
    p_ac3dec->bit_stream.p_decoder_fifo = &p_ac3dec->fifo;
    p_ac3dec->bit_stream.fifo.buffer = 0;
    p_ac3dec->bit_stream.fifo.i_available = 0;

    /*
     * Initialize the output properties
     */
    p_ac3dec->p_aout = p_input->p_aout;
    p_ac3dec->p_aout_fifo = NULL;

103 104
    imdct_init();

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    /* Spawn the ac3 decoder thread */
    if ( vlc_thread_create(&p_ac3dec->thread_id, "ac3 decoder", (vlc_thread_func_t)RunThread, (void *)p_ac3dec) )
    {
        intf_ErrMsg( "ac3dec error: can't spawn ac3 decoder thread\n" );
        free( p_ac3dec );
        return( NULL );
    }

    intf_DbgMsg( "ac3dec debug: ac3 decoder thread (%p) created\n", p_ac3dec );
    return( p_ac3dec );
}

/*****************************************************************************
 * ac3dec_DestroyThread: destroys an ac3 decoder thread
 *****************************************************************************/
void ac3dec_DestroyThread( ac3dec_thread_t * p_ac3dec )
{
    intf_DbgMsg( "ac3dec debug: requesting termination of ac3 decoder thread %p\n", p_ac3dec );

    /* Ask thread to kill itself */
    p_ac3dec->b_die = 1;
Michel Kaempf's avatar
Michel Kaempf committed
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
    /* Make sure the decoder thread leaves the GetByte() function */
    vlc_mutex_lock( &(p_ac3dec->fifo.data_lock) );
    vlc_cond_signal( &(p_ac3dec->fifo.data_wait) );
    vlc_mutex_unlock( &(p_ac3dec->fifo.data_lock) );

    /* Waiting for the decoder thread to exit */
    /* Remove this as soon as the "status" flag is implemented */
    vlc_thread_join( p_ac3dec->thread_id );
}

/* Following functions are local */

/*****************************************************************************
 * decode_find_sync()
 *****************************************************************************/
static __inline__ int decode_find_sync( ac3dec_thread_t * p_ac3dec )
{
    while ( (!p_ac3dec->b_die) && (!p_ac3dec->b_error) )
    {
        NeedBits( &(p_ac3dec->bit_stream), 16 );
        if ( (p_ac3dec->bit_stream.fifo.buffer >> (32 - 16)) == 0x0b77 )
        {
            DumpBits( &(p_ac3dec->bit_stream), 16 );
            p_ac3dec->total_bits_read = 16;
            return( 0 );
        }
Michel Kaempf's avatar
Michel Kaempf committed
153
        DumpBits( &(p_ac3dec->bit_stream), 1 ); /* XXX */
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    }
    return( -1 );
}

/*****************************************************************************
 * InitThread : initialize an ac3 decoder thread
 *****************************************************************************/
static int InitThread( ac3dec_thread_t * p_ac3dec )
{
    aout_fifo_t         aout_fifo;

    intf_DbgMsg( "ac3dec debug: initializing ac3 decoder thread %p\n", p_ac3dec );

    /* Our first job is to initialize the bit stream structure with the
     * beginning of the input stream */
    vlc_mutex_lock( &p_ac3dec->fifo.data_lock );
    while ( DECODER_FIFO_ISEMPTY(p_ac3dec->fifo) )
    {
Michel Kaempf's avatar
Michel Kaempf committed
172
        if ( p_ac3dec->b_die )
173 174 175 176
        {
            vlc_mutex_unlock( &p_ac3dec->fifo.data_lock );
            return( -1 );
        }
Michel Kaempf's avatar
Michel Kaempf committed
177
        vlc_cond_wait( &p_ac3dec->fifo.data_wait, &p_ac3dec->fifo.data_lock );
178 179 180 181 182 183
    }
    p_ac3dec->bit_stream.p_ts = DECODER_FIFO_START( p_ac3dec->fifo )->p_first_ts;
    p_ac3dec->bit_stream.i_byte = p_ac3dec->bit_stream.p_ts->i_payload_start;
    vlc_mutex_unlock( &p_ac3dec->fifo.data_lock );

    aout_fifo.i_type = AOUT_ADEC_STEREO_FIFO;
184
    aout_fifo.i_channels = 2;
185
    aout_fifo.b_stereo = 1;
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

    aout_fifo.l_frame_size = AC3DEC_FRAME_SIZE;

    /* Creating the audio output fifo */
    if ( (p_ac3dec->p_aout_fifo = aout_CreateFifo(p_ac3dec->p_aout, &aout_fifo)) == NULL )
    {
        return( -1 );
    }

    intf_DbgMsg( "ac3dec debug: ac3 decoder thread %p initialized\n", p_ac3dec );
    return( 0 );
}

/*****************************************************************************
 * RunThread : ac3 decoder thread
 *****************************************************************************/
static void RunThread( ac3dec_thread_t * p_ac3dec )
{
    intf_DbgMsg( "ac3dec debug: running ac3 decoder thread (%p) (pid == %i)\n", p_ac3dec, getpid() );

206
    msleep( INPUT_PTS_DELAY );
Michel Kaempf's avatar
Michel Kaempf committed
207

208
    /* Initializing the ac3 decoder thread */
Michel Kaempf's avatar
Michel Kaempf committed
209
    if ( InitThread(p_ac3dec) ) /* XXX */
210 211 212 213 214
    {
        p_ac3dec->b_error = 1;
    }

    /* ac3 decoder thread's main loop */
Michel Kaempf's avatar
Michel Kaempf committed
215
    /* FIXME : do we have enough room to store the decoded frames ? */
216 217
    while ( (!p_ac3dec->b_die) && (!p_ac3dec->b_error) )
    {
Michel Kaempf's avatar
Michel Kaempf committed
218 219
        p_ac3dec->b_invalid = 0;

Michel Kaempf's avatar
Michel Kaempf committed
220 221 222 223 224 225 226 227 228 229 230 231 232
	decode_find_sync( p_ac3dec );

	if ( DECODER_FIFO_START(p_ac3dec->fifo)->b_has_pts )
	{
		p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = DECODER_FIFO_START(p_ac3dec->fifo)->i_pts;
		DECODER_FIFO_START(p_ac3dec->fifo)->b_has_pts = 0;
	}
	else
	{
		p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = LAST_MDATE;
	}

	parse_syncinfo( p_ac3dec );
Michel Kaempf's avatar
Michel Kaempf committed
233 234 235 236 237 238 239 240 241
	switch ( p_ac3dec->syncinfo.fscod )
	{
		case 0:
			p_ac3dec->p_aout_fifo->l_rate = 48000;
			break;

		case 1:
			p_ac3dec->p_aout_fifo->l_rate = 44100;
			break;
Michel Kaempf's avatar
Michel Kaempf committed
242

Michel Kaempf's avatar
Michel Kaempf committed
243 244 245 246
		case 2:
			p_ac3dec->p_aout_fifo->l_rate = 32000;
			break;

Michel Kaempf's avatar
Michel Kaempf committed
247
		default: /* XXX */
Michel Kaempf's avatar
Michel Kaempf committed
248
			fprintf( stderr, "ac3dec debug: invalid fscod\n" );
Michel Kaempf's avatar
Michel Kaempf committed
249
			p_ac3dec->b_invalid = 1;
Michel Kaempf's avatar
Michel Kaempf committed
250 251
			break;
	}
Michel Kaempf's avatar
Michel Kaempf committed
252 253 254 255
	if ( p_ac3dec->b_invalid ) /* XXX */
	{
		continue;
	}
Michel Kaempf's avatar
Michel Kaempf committed
256

257
	parse_bsi( p_ac3dec );
Michel Kaempf's avatar
Michel Kaempf committed
258 259 260 261

	/* frame 1 */
	parse_audblk( p_ac3dec );
	exponent_unpack( p_ac3dec );
Michel Kaempf's avatar
Michel Kaempf committed
262 263 264 265
	if ( p_ac3dec->b_invalid )
	{
		continue;
	}
Michel Kaempf's avatar
Michel Kaempf committed
266 267
	bit_allocate( p_ac3dec );
	mantissa_unpack( p_ac3dec );
Michel Kaempf's avatar
Michel Kaempf committed
268 269 270 271
	if ( p_ac3dec->b_invalid )
	{
		continue;
	}
Michel Kaempf's avatar
Michel Kaempf committed
272 273 274 275 276 277 278 279
	if ( p_ac3dec->bsi.acmod == 0x2 )
	{
		rematrix( p_ac3dec );
	}
	imdct( p_ac3dec );
	downmix( p_ac3dec, ((ac3dec_frame_t *)p_ac3dec->p_aout_fifo->buffer)[ p_ac3dec->p_aout_fifo->l_end_frame ] );
	vlc_mutex_lock( &p_ac3dec->p_aout_fifo->data_lock );
	p_ac3dec->p_aout_fifo->l_end_frame = (p_ac3dec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
Michel Kaempf's avatar
Michel Kaempf committed
280
	vlc_cond_signal( &p_ac3dec->p_aout_fifo->data_wait );
Michel Kaempf's avatar
Michel Kaempf committed
281 282 283 284 285
	vlc_mutex_unlock( &p_ac3dec->p_aout_fifo->data_lock );

	/* frame 2 */
	parse_audblk( p_ac3dec );
	exponent_unpack( p_ac3dec );
Michel Kaempf's avatar
Michel Kaempf committed
286 287 288 289
	if ( p_ac3dec->b_invalid )
	{
		continue;
	}
Michel Kaempf's avatar
Michel Kaempf committed
290 291
	bit_allocate( p_ac3dec );
	mantissa_unpack( p_ac3dec );
Michel Kaempf's avatar
Michel Kaempf committed
292 293 294 295
	if ( p_ac3dec->b_invalid )
	{
		continue;
	}
Michel Kaempf's avatar
Michel Kaempf committed
296 297 298 299 300 301 302 303 304
	if ( p_ac3dec->bsi.acmod == 0x2 )
	{
		rematrix( p_ac3dec );
	}
	imdct( p_ac3dec );
	downmix( p_ac3dec, ((ac3dec_frame_t *)p_ac3dec->p_aout_fifo->buffer)[ p_ac3dec->p_aout_fifo->l_end_frame ] );
	p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = LAST_MDATE;
	vlc_mutex_lock( &p_ac3dec->p_aout_fifo->data_lock );
	p_ac3dec->p_aout_fifo->l_end_frame = (p_ac3dec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
Michel Kaempf's avatar
Michel Kaempf committed
305
	vlc_cond_signal( &p_ac3dec->p_aout_fifo->data_wait );
Michel Kaempf's avatar
Michel Kaempf committed
306 307 308 309 310
	vlc_mutex_unlock( &p_ac3dec->p_aout_fifo->data_lock );

	/* frame 3 */
	parse_audblk( p_ac3dec );
	exponent_unpack( p_ac3dec );
Michel Kaempf's avatar
Michel Kaempf committed
311 312 313 314
	if ( p_ac3dec->b_invalid )
	{
		continue;
	}
Michel Kaempf's avatar
Michel Kaempf committed
315 316
	bit_allocate( p_ac3dec );
	mantissa_unpack( p_ac3dec );
Michel Kaempf's avatar
Michel Kaempf committed
317 318 319 320
	if ( p_ac3dec->b_invalid )
	{
		continue;
	}
Michel Kaempf's avatar
Michel Kaempf committed
321 322 323 324 325 326 327 328 329
	if ( p_ac3dec->bsi.acmod == 0x2 )
	{
		rematrix( p_ac3dec );
	}
	imdct( p_ac3dec );
	downmix( p_ac3dec, ((ac3dec_frame_t *)p_ac3dec->p_aout_fifo->buffer)[ p_ac3dec->p_aout_fifo->l_end_frame ] );
	p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = LAST_MDATE;
	vlc_mutex_lock( &p_ac3dec->p_aout_fifo->data_lock );
	p_ac3dec->p_aout_fifo->l_end_frame = (p_ac3dec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
Michel Kaempf's avatar
Michel Kaempf committed
330
	vlc_cond_signal( &p_ac3dec->p_aout_fifo->data_wait );
Michel Kaempf's avatar
Michel Kaempf committed
331 332 333 334 335
	vlc_mutex_unlock( &p_ac3dec->p_aout_fifo->data_lock );

	/* frame 4 */
	parse_audblk( p_ac3dec );
	exponent_unpack( p_ac3dec );
Michel Kaempf's avatar
Michel Kaempf committed
336 337 338 339
	if ( p_ac3dec->b_invalid )
	{
		continue;
	}
Michel Kaempf's avatar
Michel Kaempf committed
340 341
	bit_allocate( p_ac3dec );
	mantissa_unpack( p_ac3dec );
Michel Kaempf's avatar
Michel Kaempf committed
342 343 344 345
	if ( p_ac3dec->b_invalid )
	{
		continue;
	}
Michel Kaempf's avatar
Michel Kaempf committed
346 347 348 349 350 351 352 353 354
	if ( p_ac3dec->bsi.acmod == 0x2 )
	{
		rematrix( p_ac3dec );
	}
	imdct( p_ac3dec );
	downmix( p_ac3dec, ((ac3dec_frame_t *)p_ac3dec->p_aout_fifo->buffer)[ p_ac3dec->p_aout_fifo->l_end_frame ] );
	p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = LAST_MDATE;
	vlc_mutex_lock( &p_ac3dec->p_aout_fifo->data_lock );
	p_ac3dec->p_aout_fifo->l_end_frame = (p_ac3dec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
Michel Kaempf's avatar
Michel Kaempf committed
355
	vlc_cond_signal( &p_ac3dec->p_aout_fifo->data_wait );
Michel Kaempf's avatar
Michel Kaempf committed
356 357 358 359 360
	vlc_mutex_unlock( &p_ac3dec->p_aout_fifo->data_lock );

	/* frame 5 */
	parse_audblk( p_ac3dec );
	exponent_unpack( p_ac3dec );
Michel Kaempf's avatar
Michel Kaempf committed
361 362 363 364
	if ( p_ac3dec->b_invalid )
	{
		continue;
	}
Michel Kaempf's avatar
Michel Kaempf committed
365 366
	bit_allocate( p_ac3dec );
	mantissa_unpack( p_ac3dec );
Michel Kaempf's avatar
Michel Kaempf committed
367 368 369 370
	if ( p_ac3dec->b_invalid )
	{
		continue;
	}
Michel Kaempf's avatar
Michel Kaempf committed
371 372 373 374 375 376 377 378 379
	if ( p_ac3dec->bsi.acmod == 0x2 )
	{
		rematrix( p_ac3dec );
	}
	imdct( p_ac3dec );
	downmix( p_ac3dec, ((ac3dec_frame_t *)p_ac3dec->p_aout_fifo->buffer)[ p_ac3dec->p_aout_fifo->l_end_frame ] );
	p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = LAST_MDATE;
	vlc_mutex_lock( &p_ac3dec->p_aout_fifo->data_lock );
	p_ac3dec->p_aout_fifo->l_end_frame = (p_ac3dec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
Michel Kaempf's avatar
Michel Kaempf committed
380
	vlc_cond_signal( &p_ac3dec->p_aout_fifo->data_wait );
Michel Kaempf's avatar
Michel Kaempf committed
381 382 383 384 385
	vlc_mutex_unlock( &p_ac3dec->p_aout_fifo->data_lock );

	/* frame 6 */
	parse_audblk( p_ac3dec );
	exponent_unpack( p_ac3dec );
Michel Kaempf's avatar
Michel Kaempf committed
386 387 388 389
	if ( p_ac3dec->b_invalid )
	{
		continue;
	}
Michel Kaempf's avatar
Michel Kaempf committed
390 391
	bit_allocate( p_ac3dec );
	mantissa_unpack( p_ac3dec );
Michel Kaempf's avatar
Michel Kaempf committed
392 393 394 395
	if ( p_ac3dec->b_invalid )
	{
		continue;
	}
Michel Kaempf's avatar
Michel Kaempf committed
396 397 398 399 400 401 402 403 404
	if ( p_ac3dec->bsi.acmod == 0x2 )
	{
		rematrix( p_ac3dec );
	}
	imdct( p_ac3dec );
	downmix( p_ac3dec, ((ac3dec_frame_t *)p_ac3dec->p_aout_fifo->buffer)[ p_ac3dec->p_aout_fifo->l_end_frame ] );
	p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = LAST_MDATE;
	vlc_mutex_lock( &p_ac3dec->p_aout_fifo->data_lock );
	p_ac3dec->p_aout_fifo->l_end_frame = (p_ac3dec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
Michel Kaempf's avatar
Michel Kaempf committed
405
	vlc_cond_signal( &p_ac3dec->p_aout_fifo->data_wait );
Michel Kaempf's avatar
Michel Kaempf committed
406 407 408
	vlc_mutex_unlock( &p_ac3dec->p_aout_fifo->data_lock );

	parse_auxdata( p_ac3dec );
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
    }

    /* If b_error is set, the ac3 decoder thread enters the error loop */
    if ( p_ac3dec->b_error )
    {
        ErrorThread( p_ac3dec );
    }

    /* End of the ac3 decoder thread */
    EndThread( p_ac3dec );
}

/*****************************************************************************
 * ErrorThread : ac3 decoder's RunThread() error loop
 *****************************************************************************/
static void ErrorThread( ac3dec_thread_t * p_ac3dec )
{
    /* We take the lock, because we are going to read/write the start/end
     * indexes of the decoder fifo */
    vlc_mutex_lock( &p_ac3dec->fifo.data_lock );

    /* Wait until a `die' order is sent */
    while( !p_ac3dec->b_die )
    {
        /* Trash all received PES packets */
        while( !DECODER_FIFO_ISEMPTY(p_ac3dec->fifo) )
        {
            input_NetlistFreePES( p_ac3dec->bit_stream.p_input, DECODER_FIFO_START(p_ac3dec->fifo) );
	    DECODER_FIFO_INCSTART( p_ac3dec->fifo );
        }

        /* Waiting for the input thread to put new PES packets in the fifo */
        vlc_cond_wait( &p_ac3dec->fifo.data_wait, &p_ac3dec->fifo.data_lock );
    }

    /* We can release the lock before leaving */
    vlc_mutex_unlock( &p_ac3dec->fifo.data_lock );
}

/*****************************************************************************
 * EndThread : ac3 decoder thread destruction
 *****************************************************************************/
static void EndThread( ac3dec_thread_t * p_ac3dec )
{
    intf_DbgMsg( "ac3dec debug: destroying ac3 decoder thread %p\n", p_ac3dec );

    /* If the audio output fifo was created, we destroy it */
    if ( p_ac3dec->p_aout_fifo != NULL )
    {
        aout_DestroyFifo( p_ac3dec->p_aout_fifo );
Michel Kaempf's avatar
Michel Kaempf committed
459 460 461 462 463

        /* Make sure the output thread leaves the NextFrame() function */
        vlc_mutex_lock( &(p_ac3dec->p_aout_fifo->data_lock) );
        vlc_cond_signal( &(p_ac3dec->p_aout_fifo->data_wait) );
        vlc_mutex_unlock( &(p_ac3dec->p_aout_fifo->data_lock) );
464 465 466 467 468 469 470
    }

    /* Destroy descriptor */
    free( p_ac3dec );

    intf_DbgMsg( "ac3dec debug: ac3 decoder thread %p destroyed\n", p_ac3dec );
}