1 /*****************************************************************************
2 * lpcm.c: lpcm decoder module
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: lpcm.c,v 1.18 2003/11/16 21:07:30 gbazin 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 /*****************************************************************************
42 * dec_thread_t : lpcm decoder thread descriptor
43 *****************************************************************************/
44 typedef struct dec_thread_t
49 decoder_fifo_t * p_fifo; /* stores the PES stream data */
50 /* Some filters don't handle well too small buffers (coreaudio resampler).
51 * Thus an aout buffer will be two PES packets. */
52 pes_packet_t * p_buffered_pes;
53 data_packet_t * p_buffered_data;
54 size_t i_buffered_size;
59 aout_instance_t *p_aout;
60 aout_input_t *p_aout_input;
61 audio_sample_format_t output_format;
62 audio_date_t end_date;
68 * - private stream ID (16 bits) == 0xA0 -> not in the bitstream
69 * - frame number (8 bits)
70 * - unknown (16 bits) == 0x0003 ?
72 * - current frame (4 bits)
74 * - frequency (2 bits) 0 == 48 kHz, 1 == 32 kHz, 2 == ?, 3 == ?
76 * - number of channels - 1 (3 bits) 1 == 2 channels
77 * - start code (8 bits) == 0x80
80 #define LPCM_HEADER_LEN 6
82 /*****************************************************************************
84 *****************************************************************************/
85 static int OpenDecoder ( vlc_object_t * );
86 static int RunDecoder ( decoder_fifo_t * );
88 static void DecodeFrame ( dec_thread_t * );
89 static void EndThread ( dec_thread_t * );
91 /*****************************************************************************
93 *****************************************************************************/
95 set_description( _("linear PCM audio parser") );
96 set_capability( "decoder", 100 );
97 set_callbacks( OpenDecoder, NULL );
100 /*****************************************************************************
101 * OpenDecoder: probe the decoder and return score
102 *****************************************************************************/
103 static int OpenDecoder( vlc_object_t *p_this )
105 decoder_t *p_dec = (decoder_t*)p_this;
107 if( p_dec->fmt_in.i_codec != VLC_FOURCC('l','p','c','m')
108 && p_dec->fmt_in.i_codec != VLC_FOURCC('l','p','c','b') )
113 p_dec->pf_run = RunDecoder;
117 /*****************************************************************************
118 * RunDecoder: the lpcm decoder
119 *****************************************************************************/
120 static int RunDecoder( decoder_fifo_t * p_fifo )
122 dec_thread_t * p_dec;
124 /* Allocate the memory needed to store the thread's structure */
125 if( (p_dec = (dec_thread_t *)malloc( sizeof(dec_thread_t)) )
128 msg_Err( p_fifo, "out of memory" );
129 DecoderError( p_fifo );
133 /* Initialize the thread properties */
134 p_dec->p_fifo = p_fifo;
135 p_dec->i_buffered_size = 0;
137 p_dec->output_format.i_format = VLC_FOURCC('s','1','6','b');
138 p_dec->p_aout = NULL;
139 p_dec->p_aout_input = NULL;
141 /* LPCM decoder thread's main loop */
142 while ( (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )
147 /* If b_error is set, the lpcm decoder thread enters the error loop */
148 if ( p_dec->p_fifo->b_error )
150 DecoderError( p_dec->p_fifo );
153 /* End of the lpcm decoder thread */
159 /*****************************************************************************
160 * DecodeFrame: decodes a frame.
161 *****************************************************************************/
162 static void DecodeFrame( dec_thread_t * p_dec )
164 pes_packet_t * p_pes;
165 data_packet_t * p_data;
166 aout_buffer_t * p_buffer;
170 unsigned int i_rate = 0, i_original_channels = 0, i_size;
173 input_ExtractPES( p_dec->p_fifo, &p_pes );
176 p_dec->p_fifo->b_error = 1;
180 /* Compute the size of the PES - i_pes_size includes the PES header. */
181 p_data = p_pes->p_first;
183 while ( p_data != NULL )
185 i_size += p_data->p_payload_end - p_data->p_payload_start;
186 p_data = p_data->p_next;
188 if ( i_size < LPCM_HEADER_LEN )
190 msg_Err(p_dec->p_fifo, "PES packet is too short");
191 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
195 i_pts = p_pes->i_pts;
196 if( i_pts != 0 && i_pts != aout_DateGet( &p_dec->end_date ) )
198 aout_DateSet( &p_dec->end_date, i_pts );
201 p_data = p_pes->p_first;
202 /* It necessarily contains one byte. */
203 /* Get LPCM header. */
205 /* Drop the first four bytes. */
206 for ( i = 0; i < 4; i++ )
208 if ( p_data->p_payload_end == p_data->p_payload_start )
210 p_data = p_data->p_next;
212 p_data->p_payload_start++;
215 i_header = p_data->p_payload_start[0];
216 p_data->p_payload_start++;
218 switch ( (i_header >> 4) & 0x3 )
234 switch ( i_header & 0x7 )
237 i_original_channels = AOUT_CHAN_CENTER;
240 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
243 /* This is unsure. */
244 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE;
247 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
248 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
251 /* This is unsure. */
252 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
253 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
257 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
258 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
259 | AOUT_CHAN_CENTER | AOUT_CHAN_LFE;
262 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
263 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
264 | AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT
265 | AOUT_CHAN_MIDDLERIGHT;
268 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
269 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
270 | AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT
271 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE;
275 /* Check frame sync and drop it. */
276 if ( p_data->p_payload_end == p_data->p_payload_start )
278 p_data = p_data->p_next;
280 if ( p_data->p_payload_start[0] != 0x80 )
282 msg_Warn(p_dec->p_fifo, "no frame sync");
283 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
286 p_data->p_payload_start++;
288 if( (p_dec->p_aout_input != NULL) &&
289 ( (p_dec->output_format.i_rate != i_rate)
290 || (p_dec->output_format.i_original_channels
291 != i_original_channels) ) )
293 /* Parameters changed - this should not happen. */
294 aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
295 p_dec->p_aout_input = NULL;
298 /* Creating the audio input if not created yet. */
299 if( p_dec->p_aout_input == NULL )
301 p_dec->output_format.i_rate = i_rate;
302 p_dec->output_format.i_original_channels = i_original_channels;
303 p_dec->output_format.i_physical_channels
304 = i_original_channels & AOUT_CHAN_PHYSMASK;
305 aout_DateInit( &p_dec->end_date, i_rate );
306 aout_DateSet( &p_dec->end_date, i_pts );
307 p_dec->p_aout_input = aout_DecNew( p_dec->p_fifo,
309 &p_dec->output_format );
311 if ( p_dec->p_aout_input == NULL )
313 p_dec->p_fifo->b_error = 1;
314 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
318 if ( p_dec->i_buffered_size )
320 input_DeletePES( p_dec->p_fifo->p_packets_mgt,
321 p_dec->p_buffered_pes );
322 p_dec->i_buffered_size = 0;
326 if ( !aout_DateGet( &p_dec->end_date ) )
328 /* We've just started the stream, wait for the first PTS. */
329 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
333 if ( p_dec->i_buffered_size != 0 )
335 p_buffer = aout_DecNewBuffer( p_dec->p_aout, p_dec->p_aout_input,
336 (i_size - LPCM_HEADER_LEN + p_dec->i_buffered_size)
337 / p_dec->output_format.i_bytes_per_frame );
339 if( p_buffer == NULL )
341 msg_Err( p_dec->p_fifo, "cannot get aout buffer" );
342 p_dec->p_fifo->b_error = 1;
343 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
346 p_buffer->start_date = aout_DateGet( &p_dec->end_date );
347 p_buffer->end_date = aout_DateIncrement( &p_dec->end_date,
348 (i_size - LPCM_HEADER_LEN + p_dec->i_buffered_size)
349 / p_dec->output_format.i_bytes_per_frame );
351 /* Get the whole frame. */
352 p_dest = p_buffer->p_buffer;
354 while ( p_dec->p_buffered_data != NULL )
356 p_dec->p_fifo->p_vlc->pf_memcpy( p_dest,
357 p_dec->p_buffered_data->p_payload_start,
358 p_dec->p_buffered_data->p_payload_end
359 - p_dec->p_buffered_data->p_payload_start );
360 p_dest += p_dec->p_buffered_data->p_payload_end
361 - p_dec->p_buffered_data->p_payload_start;
362 p_dec->p_buffered_data = p_dec->p_buffered_data->p_next;
364 input_DeletePES( p_dec->p_fifo->p_packets_mgt,
365 p_dec->p_buffered_pes );
367 p_dest = p_buffer->p_buffer + p_dec->i_buffered_size;
369 while ( p_data != NULL )
371 p_dec->p_fifo->p_vlc->pf_memcpy( p_dest, p_data->p_payload_start,
372 p_data->p_payload_end - p_data->p_payload_start );
373 p_dest += p_data->p_payload_end - p_data->p_payload_start;
374 p_data = p_data->p_next;
376 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
378 /* Send the buffer to the aout core. */
379 aout_DecPlay( p_dec->p_aout, p_dec->p_aout_input, p_buffer );
381 p_dec->i_buffered_size = 0;
385 p_dec->i_buffered_size = i_size - LPCM_HEADER_LEN;
386 p_dec->p_buffered_pes = p_pes;
387 p_dec->p_buffered_data = p_data;
391 /*****************************************************************************
392 * EndThread : lpcm decoder thread destruction
393 *****************************************************************************/
394 static void EndThread( dec_thread_t * p_dec )
396 if( p_dec->p_aout_input != NULL )
398 aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
401 if ( p_dec->i_buffered_size )
403 input_DeletePES( p_dec->p_fifo->p_packets_mgt,
404 p_dec->p_buffered_pes );
405 p_dec->i_buffered_size = 0;