1 /*****************************************************************************
2 * lpcm.c: lpcm decoder module
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: lpcm.c,v 1.13 2003/03/04 13:21:19 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 /*****************************************************************************
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 */
54 aout_instance_t *p_aout;
55 aout_input_t *p_aout_input;
56 audio_sample_format_t output_format;
57 audio_date_t end_date;
63 * - private stream ID (16 bits) == 0xA0 -> not in the bitstream
64 * - frame number (8 bits)
65 * - unknown (16 bits) == 0x0003 ?
67 * - current frame (4 bits)
69 * - frequency (2 bits) 0 == 48 kHz, 1 == 32 kHz, 2 == ?, 3 == ?
71 * - number of channels - 1 (3 bits) 1 == 2 channels
72 * - start code (8 bits) == 0x80
75 #define LPCM_HEADER_LEN 6
77 /*****************************************************************************
79 *****************************************************************************/
80 static int OpenDecoder ( vlc_object_t * );
81 static int RunDecoder ( decoder_fifo_t * );
83 static void DecodeFrame ( dec_thread_t * );
84 static void EndThread ( dec_thread_t * );
86 /*****************************************************************************
88 *****************************************************************************/
90 set_description( _("linear PCM audio parser") );
91 set_capability( "decoder", 100 );
92 set_callbacks( OpenDecoder, NULL );
95 /*****************************************************************************
96 * OpenDecoder: probe the decoder and return score
97 *****************************************************************************/
98 static int OpenDecoder( vlc_object_t *p_this )
100 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
102 if( p_fifo->i_fourcc != VLC_FOURCC('l','p','c','m')
103 && p_fifo->i_fourcc != VLC_FOURCC('l','p','c','b') )
108 p_fifo->pf_run = RunDecoder;
112 /*****************************************************************************
113 * RunDecoder: the lpcm decoder
114 *****************************************************************************/
115 static int RunDecoder( decoder_fifo_t * p_fifo )
117 dec_thread_t * p_dec;
119 /* Allocate the memory needed to store the thread's structure */
120 if( (p_dec = (dec_thread_t *)malloc( sizeof(dec_thread_t)) )
123 msg_Err( p_fifo, "out of memory" );
124 DecoderError( p_fifo );
128 /* Initialize the thread properties */
129 p_dec->p_fifo = p_fifo;
131 p_dec->output_format.i_format = VLC_FOURCC('s','1','6','b');
132 p_dec->p_aout = NULL;
133 p_dec->p_aout_input = NULL;
135 /* LPCM decoder thread's main loop */
136 while ( (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )
141 /* If b_error is set, the lpcm decoder thread enters the error loop */
142 if ( p_dec->p_fifo->b_error )
144 DecoderError( p_dec->p_fifo );
147 /* End of the lpcm decoder thread */
153 /*****************************************************************************
154 * DecodeFrame: decodes a frame.
155 *****************************************************************************/
156 static void DecodeFrame( dec_thread_t * p_dec )
158 pes_packet_t * p_pes;
159 data_packet_t * p_data;
160 aout_buffer_t * p_buffer;
164 unsigned int i_rate, i_original_channels, i_size;
167 input_ExtractPES( p_dec->p_fifo, &p_pes );
170 p_dec->p_fifo->b_error = 1;
174 /* Compute the size of the PES - i_pes_size includes the PES header. */
175 p_data = p_pes->p_first;
177 while ( p_data != NULL )
179 i_size += p_data->p_payload_end - p_data->p_payload_start;
180 p_data = p_data->p_next;
182 if ( i_size < LPCM_HEADER_LEN )
184 msg_Err(p_dec->p_fifo, "PES packet is too short");
185 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
189 i_pts = p_pes->i_pts;
190 if( i_pts != 0 && i_pts != aout_DateGet( &p_dec->end_date ) )
192 aout_DateSet( &p_dec->end_date, i_pts );
195 p_data = p_pes->p_first;
196 /* It necessarily contains one byte. */
197 /* Get LPCM header. */
199 /* Drop the first four bytes. */
200 for ( i = 0; i < 4; i++ )
202 if ( p_data->p_payload_end == p_data->p_payload_start )
204 p_data = p_data->p_next;
206 p_data->p_payload_start++;
209 i_header = p_data->p_payload_start[0];
210 p_data->p_payload_start++;
212 switch ( (i_header >> 4) & 0x3 )
228 switch ( i_header & 0x7 )
231 i_original_channels = AOUT_CHAN_CENTER;
234 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
237 /* This is unsure. */
238 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE;
241 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
242 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
245 /* This is unsure. */
246 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
247 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
251 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
252 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
253 | AOUT_CHAN_CENTER | AOUT_CHAN_LFE;
256 i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
257 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
258 | AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT
259 | AOUT_CHAN_MIDDLERIGHT;
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 | AOUT_CHAN_LFE;
269 /* Check frame sync and drop it. */
270 if ( p_data->p_payload_end == p_data->p_payload_start )
272 p_data = p_data->p_next;
274 if ( p_data->p_payload_start[0] != 0x80 )
276 msg_Warn(p_dec->p_fifo, "no frame sync");
277 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
280 p_data->p_payload_start++;
282 if( (p_dec->p_aout_input != NULL) &&
283 ( (p_dec->output_format.i_rate != i_rate)
284 || (p_dec->output_format.i_original_channels
285 != i_original_channels) ) )
287 /* Parameters changed - this should not happen. */
288 aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
289 p_dec->p_aout_input = NULL;
292 /* Creating the audio input if not created yet. */
293 if( p_dec->p_aout_input == NULL )
295 p_dec->output_format.i_rate = i_rate;
296 p_dec->output_format.i_original_channels = i_original_channels;
297 p_dec->output_format.i_physical_channels
298 = i_original_channels & AOUT_CHAN_PHYSMASK;
299 aout_DateInit( &p_dec->end_date, i_rate );
300 p_dec->p_aout_input = aout_DecNew( p_dec->p_fifo,
302 &p_dec->output_format );
304 if ( p_dec->p_aout_input == NULL )
306 p_dec->p_fifo->b_error = 1;
307 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
312 if ( !aout_DateGet( &p_dec->end_date ) )
314 /* We've just started the stream, wait for the first PTS. */
315 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
319 p_buffer = aout_DecNewBuffer( p_dec->p_aout, p_dec->p_aout_input,
320 (i_size - LPCM_HEADER_LEN)
321 / p_dec->output_format.i_bytes_per_frame );
323 if( p_buffer == NULL )
325 msg_Err( p_dec->p_fifo, "cannot get aout buffer" );
326 p_dec->p_fifo->b_error = 1;
327 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
330 p_buffer->start_date = aout_DateGet( &p_dec->end_date );
331 p_buffer->end_date = aout_DateIncrement( &p_dec->end_date,
332 (i_size - LPCM_HEADER_LEN)
333 / p_dec->output_format.i_bytes_per_frame );
335 /* Get the whole frame. */
336 p_dest = p_buffer->p_buffer;
337 while ( p_data != NULL )
339 p_dec->p_fifo->p_vlc->pf_memcpy( p_dest, p_data->p_payload_start,
340 p_data->p_payload_end - p_data->p_payload_start );
341 p_dest += p_data->p_payload_end - p_data->p_payload_start;
342 p_data = p_data->p_next;
344 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
346 /* Send the buffer to the aout core. */
347 aout_DecPlay( p_dec->p_aout, p_dec->p_aout_input, p_buffer );
350 /*****************************************************************************
351 * EndThread : lpcm decoder thread destruction
352 *****************************************************************************/
353 static void EndThread( dec_thread_t * p_dec )
355 if( p_dec->p_aout_input != NULL )
357 aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );