* lpcm.c: lpcm decoder module
*****************************************************************************
* Copyright (C) 1999-2001 VideoLAN
- * $Id: lpcm.c,v 1.1 2002/09/20 23:27:03 massiot Exp $
+ * $Id: lpcm.c,v 1.17 2003/09/02 20:19:25 gbazin Exp $
*
* Authors: Samuel Hocevar <sam@zoy.org>
* Henri Fallon <henri@videolan.org>
+ * Christophe Massiot <massiot@via.ecp.fr>
*
* 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
# include <unistd.h> /* getpid() */
#endif
-#define LPCM_FRAME_NB 502
-
/*****************************************************************************
* dec_thread_t : lpcm decoder thread descriptor
*****************************************************************************/
typedef struct dec_thread_t
{
- /*
- * Thread properties
- */
- vlc_thread_t thread_id; /* id for thread functions */
-
/*
* Input properties
*/
decoder_fifo_t * p_fifo; /* stores the PES stream data */
- bit_stream_t bit_stream;
- int sync_ptr; /* sync ptr from lpcm magic header */
+ /* Some filters don't handle well too small buffers (coreaudio resampler).
+ * Thus an aout buffer will be two PES packets. */
+ pes_packet_t * p_buffered_pes;
+ data_packet_t * p_buffered_data;
+ size_t i_buffered_size;
/*
* Output properties
audio_date_t end_date;
} dec_thread_t;
+/*
+ * LPCM header :
+ * - PES header
+ * - private stream ID (16 bits) == 0xA0 -> not in the bitstream
+ * - frame number (8 bits)
+ * - unknown (16 bits) == 0x0003 ?
+ * - unknown (4 bits)
+ * - current frame (4 bits)
+ * - unknown (2 bits)
+ * - frequency (2 bits) 0 == 48 kHz, 1 == 32 kHz, 2 == ?, 3 == ?
+ * - unknown (1 bit)
+ * - number of channels - 1 (3 bits) 1 == 2 channels
+ * - start code (8 bits) == 0x80
+ */
+
+#define LPCM_HEADER_LEN 6
+
/*****************************************************************************
* Local prototypes
*****************************************************************************/
static int OpenDecoder ( vlc_object_t * );
static int RunDecoder ( decoder_fifo_t * );
- void DecodeFrame ( dec_thread_t * );
-// static int InitThread ( dec_thread_t * );
+static void DecodeFrame ( dec_thread_t * );
static void EndThread ( dec_thread_t * );
/*****************************************************************************
*****************************************************************************/
static int OpenDecoder( vlc_object_t *p_this )
{
- decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
+ decoder_t *p_dec = (decoder_t*)p_this;
- if( p_fifo->i_fourcc != VLC_FOURCC('l','p','c','m')
- && p_fifo->i_fourcc != VLC_FOURCC('l','p','c','b') )
+ if( p_dec->p_fifo->i_fourcc != VLC_FOURCC('l','p','c','m')
+ && p_dec->p_fifo->i_fourcc != VLC_FOURCC('l','p','c','b') )
{
return VLC_EGENERIC;
}
- p_fifo->pf_run = RunDecoder;
+ p_dec->pf_run = RunDecoder;
return VLC_SUCCESS;
}
dec_thread_t * p_dec;
/* Allocate the memory needed to store the thread's structure */
- if( (p_dec = (dec_thread_t *)malloc (sizeof(dec_thread_t)) )
+ if( (p_dec = (dec_thread_t *)malloc( sizeof(dec_thread_t)) )
== NULL)
{
msg_Err( p_fifo, "out of memory" );
/* Initialize the thread properties */
p_dec->p_fifo = p_fifo;
+ p_dec->i_buffered_size = 0;
- /* Init the bitstream */
- InitBitstream( &p_dec->bit_stream, p_dec->p_fifo,
- NULL, NULL );
-
- /* FIXME : I suppose the number of channel and sampling rate
- * are somewhere in the headers */
- p_dec->output_format.i_format = AOUT_FMT_S16_BE;
- p_dec->output_format.i_channels = 2;
- p_dec->output_format.i_rate = 48000;
-
- aout_DateInit( &p_dec->end_date, 48000 );
- p_dec->p_aout_input = aout_InputNew( p_dec->p_fifo,
- &p_dec->p_aout,
- &p_dec->output_format );
-
- if ( p_dec->p_aout_input == NULL )
- {
- msg_Err( p_dec->p_fifo, "failed to create aout fifo" );
- p_dec->p_fifo->b_error = 1;
- return( -1 );
- }
+ p_dec->output_format.i_format = VLC_FOURCC('s','1','6','b');
+ p_dec->p_aout = NULL;
+ p_dec->p_aout_input = NULL;
- /* lpcm decoder thread's main loop */
+ /* LPCM decoder thread's main loop */
while ( (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )
{
DecodeFrame(p_dec);
/*****************************************************************************
* DecodeFrame: decodes a frame.
*****************************************************************************/
-void DecodeFrame( dec_thread_t * p_dec )
+static void DecodeFrame( dec_thread_t * p_dec )
{
- aout_buffer_t * p_aout_buffer;
- mtime_t i_pts;
+ pes_packet_t * p_pes;
+ data_packet_t * p_data;
+ aout_buffer_t * p_buffer;
+ void * p_dest;
+ mtime_t i_pts;
+ uint8_t i_header;
+ unsigned int i_rate = 0, i_original_channels = 0, i_size;
+ int i;
- NextPTS( &p_dec->bit_stream, &i_pts, NULL );
-
+ input_ExtractPES( p_dec->p_fifo, &p_pes );
+ if ( !p_pes )
+ {
+ p_dec->p_fifo->b_error = 1;
+ return;
+ }
+
+ /* Compute the size of the PES - i_pes_size includes the PES header. */
+ p_data = p_pes->p_first;
+ i_size = 0;
+ while ( p_data != NULL )
+ {
+ i_size += p_data->p_payload_end - p_data->p_payload_start;
+ p_data = p_data->p_next;
+ }
+ if ( i_size < LPCM_HEADER_LEN )
+ {
+ msg_Err(p_dec->p_fifo, "PES packet is too short");
+ input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
+ return;
+ }
+
+ i_pts = p_pes->i_pts;
if( i_pts != 0 && i_pts != aout_DateGet( &p_dec->end_date ) )
{
aout_DateSet( &p_dec->end_date, i_pts );
}
-
- p_aout_buffer = aout_BufferNew( p_dec->p_aout,
- p_dec->p_aout_input,
- LPCM_FRAME_NB );
-
- if( !p_aout_buffer )
+
+ p_data = p_pes->p_first;
+ /* It necessarily contains one byte. */
+ /* Get LPCM header. */
+
+ /* Drop the first four bytes. */
+ for ( i = 0; i < 4; i++ )
{
- msg_Err( p_dec->p_fifo, "cannot get aout buffer" );
- p_dec->p_fifo->b_error = 1;
+ if ( p_data->p_payload_end == p_data->p_payload_start )
+ {
+ p_data = p_data->p_next;
+ }
+ p_data->p_payload_start++;
+ }
+
+ i_header = p_data->p_payload_start[0];
+ p_data->p_payload_start++;
+
+ switch ( (i_header >> 4) & 0x3 )
+ {
+ case 0:
+ i_rate = 48000;
+ break;
+ case 1:
+ i_rate = 96000;
+ break;
+ case 2:
+ i_rate = 44100;
+ break;
+ case 3:
+ i_rate = 32000;
+ break;
+ }
+
+ switch ( i_header & 0x7 )
+ {
+ case 0:
+ i_original_channels = AOUT_CHAN_CENTER;
+ break;
+ case 1:
+ i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
+ break;
+ case 2:
+ /* This is unsure. */
+ i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE;
+ break;
+ case 3:
+ i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
+ | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
+ break;
+ case 4:
+ /* This is unsure. */
+ i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
+ | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
+ | AOUT_CHAN_LFE;
+ break;
+ case 5:
+ i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
+ | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
+ | AOUT_CHAN_CENTER | AOUT_CHAN_LFE;
+ break;
+ case 6:
+ i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
+ | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
+ | AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT
+ | AOUT_CHAN_MIDDLERIGHT;
+ break;
+ case 7:
+ i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
+ | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
+ | AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT
+ | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE;
+ break;
+ }
+
+ /* Check frame sync and drop it. */
+ if ( p_data->p_payload_end == p_data->p_payload_start )
+ {
+ p_data = p_data->p_next;
+ }
+ if ( p_data->p_payload_start[0] != 0x80 )
+ {
+ msg_Warn(p_dec->p_fifo, "no frame sync");
+ input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
return;
}
-
- p_aout_buffer->start_date = aout_DateGet( &p_dec->end_date );
- p_aout_buffer->end_date = aout_DateIncrement( &p_dec->end_date,
- LPCM_FRAME_NB );
+ p_data->p_payload_start++;
+
+ if( (p_dec->p_aout_input != NULL) &&
+ ( (p_dec->output_format.i_rate != i_rate)
+ || (p_dec->output_format.i_original_channels
+ != i_original_channels) ) )
+ {
+ /* Parameters changed - this should not happen. */
+ aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
+ p_dec->p_aout_input = NULL;
+ }
- /* Look for sync word - should be 0x0180 */
- RealignBits( &p_dec->bit_stream );
- while ( (GetBits( &p_dec->bit_stream, 16 ) ) != 0x0180 &&
- (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error));
+ /* Creating the audio input if not created yet. */
+ if( p_dec->p_aout_input == NULL )
+ {
+ p_dec->output_format.i_rate = i_rate;
+ p_dec->output_format.i_original_channels = i_original_channels;
+ p_dec->output_format.i_physical_channels
+ = i_original_channels & AOUT_CHAN_PHYSMASK;
+ aout_DateInit( &p_dec->end_date, i_rate );
+ aout_DateSet( &p_dec->end_date, i_pts );
+ p_dec->p_aout_input = aout_DecNew( p_dec->p_fifo,
+ &p_dec->p_aout,
+ &p_dec->output_format );
- GetChunk( &p_dec->bit_stream, p_aout_buffer->p_buffer,
- LPCM_FRAME_NB * 4);
+ if ( p_dec->p_aout_input == NULL )
+ {
+ p_dec->p_fifo->b_error = 1;
+ input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
+ return;
+ }
- if( p_dec->p_fifo->b_die )
+ if ( p_dec->i_buffered_size )
+ {
+ input_DeletePES( p_dec->p_fifo->p_packets_mgt,
+ p_dec->p_buffered_pes );
+ p_dec->i_buffered_size = 0;
+ }
+ }
+
+ if ( !aout_DateGet( &p_dec->end_date ) )
{
- aout_BufferDelete( p_dec->p_aout, p_dec->p_aout_input,
- p_aout_buffer );
+ /* We've just started the stream, wait for the first PTS. */
+ input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
return;
}
- aout_BufferPlay( p_dec->p_aout, p_dec->p_aout_input,
- p_aout_buffer );
+ if ( p_dec->i_buffered_size != 0 )
+ {
+ p_buffer = aout_DecNewBuffer( p_dec->p_aout, p_dec->p_aout_input,
+ (i_size - LPCM_HEADER_LEN + p_dec->i_buffered_size)
+ / p_dec->output_format.i_bytes_per_frame );
+
+ if( p_buffer == NULL )
+ {
+ msg_Err( p_dec->p_fifo, "cannot get aout buffer" );
+ p_dec->p_fifo->b_error = 1;
+ input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
+ return;
+ }
+ p_buffer->start_date = aout_DateGet( &p_dec->end_date );
+ p_buffer->end_date = aout_DateIncrement( &p_dec->end_date,
+ (i_size - LPCM_HEADER_LEN + p_dec->i_buffered_size)
+ / p_dec->output_format.i_bytes_per_frame );
+
+ /* Get the whole frame. */
+ p_dest = p_buffer->p_buffer;
+
+ while ( p_dec->p_buffered_data != NULL )
+ {
+ p_dec->p_fifo->p_vlc->pf_memcpy( p_dest,
+ p_dec->p_buffered_data->p_payload_start,
+ p_dec->p_buffered_data->p_payload_end
+ - p_dec->p_buffered_data->p_payload_start );
+ p_dest += p_dec->p_buffered_data->p_payload_end
+ - p_dec->p_buffered_data->p_payload_start;
+ p_dec->p_buffered_data = p_dec->p_buffered_data->p_next;
+ }
+ input_DeletePES( p_dec->p_fifo->p_packets_mgt,
+ p_dec->p_buffered_pes );
+
+ p_dest = p_buffer->p_buffer + p_dec->i_buffered_size;
+
+ while ( p_data != NULL )
+ {
+ p_dec->p_fifo->p_vlc->pf_memcpy( p_dest, p_data->p_payload_start,
+ p_data->p_payload_end - p_data->p_payload_start );
+ p_dest += p_data->p_payload_end - p_data->p_payload_start;
+ p_data = p_data->p_next;
+ }
+ input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
+
+ /* Send the buffer to the aout core. */
+ aout_DecPlay( p_dec->p_aout, p_dec->p_aout_input, p_buffer );
+
+ p_dec->i_buffered_size = 0;
+ }
+ else
+ {
+ p_dec->i_buffered_size = i_size - LPCM_HEADER_LEN;
+ p_dec->p_buffered_pes = p_pes;
+ p_dec->p_buffered_data = p_data;
+ }
}
/*****************************************************************************
{
if( p_dec->p_aout_input != NULL )
{
- aout_InputDelete( p_dec->p_aout, p_dec->p_aout_input );
+ aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
+ }
+
+ if ( p_dec->i_buffered_size )
+ {
+ input_DeletePES( p_dec->p_fifo->p_packets_mgt,
+ p_dec->p_buffered_pes );
+ p_dec->i_buffered_size = 0;
}
free( p_dec );