1 /*****************************************************************************
2 * lpcm.c: lpcm decoder module
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: lpcm.c,v 1.12 2003/03/03 14:19:09 massiot Exp $
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Henri Fallon <henri@videolan.org>
9 * Christophe Massiot <massiot@via.ecp.fr>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
29 #include <stdlib.h> /* malloc(), free() */
30 #include <string.h> /* memcpy(), memset() */
34 #include <vlc/decoder.h>
35 #include <input_ext-dec.h>
38 # include <unistd.h> /* getpid() */
41 /* DVD PES size (2048) - 40 bytes (headers) */
42 #define LPCM_FRAME_LENGTH 2008
44 /*****************************************************************************
45 * dec_thread_t : lpcm decoder thread descriptor
46 *****************************************************************************/
47 typedef struct dec_thread_t
52 vlc_thread_t thread_id; /* id for thread functions */
57 decoder_fifo_t * p_fifo; /* stores the PES stream data */
58 bit_stream_t bit_stream;
59 int sync_ptr; /* sync ptr from lpcm magic header */
64 aout_instance_t *p_aout;
65 aout_input_t *p_aout_input;
66 audio_sample_format_t output_format;
67 audio_date_t end_date;
73 * - private stream ID (16 bits) == 0xA0 -> not in the bitstream
74 * - frame number (8 bits)
75 * - unknown (16 bits) == 0x0003 ?
77 * - current frame (4 bits)
79 * - frequency (2 bits) 0 == 48 kHz, 1 == 32 kHz, 2 == ?, 3 == ?
81 * - number of channels - 1 (3 bits) 1 == 2 channels
82 * - start code (8 bits) == 0x80
85 /*****************************************************************************
87 *****************************************************************************/
88 static int OpenDecoder ( vlc_object_t * );
89 static int RunDecoder ( decoder_fifo_t * );
91 static void DecodeFrame ( dec_thread_t * );
92 static void EndThread ( dec_thread_t * );
94 /*****************************************************************************
96 *****************************************************************************/
98 set_description( _("linear PCM audio parser") );
99 set_capability( "decoder", 100 );
100 set_callbacks( OpenDecoder, NULL );
103 /*****************************************************************************
104 * OpenDecoder: probe the decoder and return score
105 *****************************************************************************/
106 static int OpenDecoder( vlc_object_t *p_this )
108 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
110 if( p_fifo->i_fourcc != VLC_FOURCC('l','p','c','m')
111 && p_fifo->i_fourcc != VLC_FOURCC('l','p','c','b') )
116 p_fifo->pf_run = RunDecoder;
120 /*****************************************************************************
121 * RunDecoder: the lpcm decoder
122 *****************************************************************************/
123 static int RunDecoder( decoder_fifo_t * p_fifo )
125 dec_thread_t * p_dec;
127 /* Allocate the memory needed to store the thread's structure */
128 if( (p_dec = (dec_thread_t *)malloc (sizeof(dec_thread_t)) )
131 msg_Err( p_fifo, "out of memory" );
132 DecoderError( p_fifo );
136 /* Initialize the thread properties */
137 p_dec->p_fifo = p_fifo;
139 /* Init the bitstream */
140 if( InitBitstream( &p_dec->bit_stream, p_dec->p_fifo,
141 NULL, NULL ) != VLC_SUCCESS )
143 msg_Err( p_fifo, "cannot initialize bitstream" );
144 DecoderError( p_fifo );
149 p_dec->output_format.i_format = VLC_FOURCC('s','1','6','b');
150 p_dec->p_aout = NULL;
151 p_dec->p_aout_input = NULL;
153 /* LPCM decoder thread's main loop */
154 while ( (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )
159 /* If b_error is set, the lpcm decoder thread enters the error loop */
160 if ( p_dec->p_fifo->b_error )
162 DecoderError( p_dec->p_fifo );
165 /* End of the lpcm decoder thread */
171 /*****************************************************************************
172 * DecodeFrame: decodes a frame.
173 *****************************************************************************/
174 static void DecodeFrame( dec_thread_t * p_dec )
176 aout_buffer_t * p_buffer;
179 unsigned int i_rate, i_original_channels;
181 /* Look for sync word - should be 0xXX80 */
182 RealignBits( &p_dec->bit_stream );
183 while ( (ShowBits( &p_dec->bit_stream, 16 ) & 0xc8ff) != 0x0080 &&
184 (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )
186 RemoveBits( &p_dec->bit_stream, 8 );
188 if ( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error ) return;
190 NextPTS( &p_dec->bit_stream, &i_pts, NULL );
191 if( i_pts != 0 && i_pts != aout_DateGet( &p_dec->end_date ) )
193 aout_DateSet( &p_dec->end_date, i_pts );
196 /* Get LPCM header. */
197 i_header = GetBits( &p_dec->bit_stream, 16 ) >> 8;
199 switch ( i_header >> 4 )
215 switch ( i_header & 0x7 )
218 i_original_channels = AOUT_CHAN_CENTER;
221 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
224 /* This is unsure. */
225 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE;
228 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
229 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
232 /* This is unsure. */
233 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
234 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
238 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
239 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
240 | AOUT_CHAN_CENTER | AOUT_CHAN_LFE;
243 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
244 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
245 | AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT
246 | AOUT_CHAN_MIDDLERIGHT;
249 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
250 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
251 | AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT
252 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE;
256 if( (p_dec->p_aout_input != NULL) &&
257 ( (p_dec->output_format.i_rate != i_rate)
258 || (p_dec->output_format.i_original_channels
259 != i_original_channels) ) )
261 /* Parameters changed - this should not happen. */
262 aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
263 p_dec->p_aout_input = NULL;
266 /* Creating the audio input if not created yet. */
267 if( p_dec->p_aout_input == NULL )
269 p_dec->output_format.i_rate = i_rate;
270 p_dec->output_format.i_original_channels = i_original_channels;
271 p_dec->output_format.i_physical_channels
272 = i_original_channels & AOUT_CHAN_PHYSMASK;
273 aout_DateInit( &p_dec->end_date, i_rate );
274 p_dec->p_aout_input = aout_DecNew( p_dec->p_fifo,
276 &p_dec->output_format );
278 if ( p_dec->p_aout_input == NULL )
280 p_dec->p_fifo->b_error = 1;
285 if ( !aout_DateGet( &p_dec->end_date ) )
287 byte_t p_junk[LPCM_FRAME_LENGTH];
289 /* We've just started the stream, wait for the first PTS. */
290 GetChunk( &p_dec->bit_stream, p_junk, LPCM_FRAME_LENGTH );
294 p_buffer = aout_DecNewBuffer( p_dec->p_aout, p_dec->p_aout_input,
295 LPCM_FRAME_LENGTH / p_dec->output_format.i_bytes_per_frame );
297 if( p_buffer == NULL )
299 msg_Err( p_dec->p_fifo, "cannot get aout buffer" );
300 p_dec->p_fifo->b_error = 1;
303 p_buffer->start_date = aout_DateGet( &p_dec->end_date );
304 p_buffer->end_date = aout_DateIncrement( &p_dec->end_date,
305 LPCM_FRAME_LENGTH / p_dec->output_format.i_bytes_per_frame );
307 /* Get the whole frame. */
308 GetChunk( &p_dec->bit_stream, p_buffer->p_buffer,
310 if( p_dec->p_fifo->b_die )
312 aout_DecDeleteBuffer( p_dec->p_aout, p_dec->p_aout_input,
317 /* Send the buffer to the aout core. */
318 aout_DecPlay( p_dec->p_aout, p_dec->p_aout_input, p_buffer );
321 /*****************************************************************************
322 * EndThread : lpcm decoder thread destruction
323 *****************************************************************************/
324 static void EndThread( dec_thread_t * p_dec )
326 if( p_dec->p_aout_input != NULL )
328 aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
331 CloseBitstream( &p_dec->bit_stream );