]> git.sesse.net Git - vlc/blob - modules/codec/lpcm.c
* Fixed a bug in LPCM codec.
[vlc] / modules / codec / lpcm.c
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 $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Henri Fallon <henri@videolan.org>
9  *          Christophe Massiot <massiot@via.ecp.fr>
10  *
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.
15  * 
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.
20  *
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  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <stdlib.h>                                      /* malloc(), free() */
30 #include <string.h>                                    /* memcpy(), memset() */
31
32 #include <vlc/vlc.h>
33 #include <vlc/aout.h>
34 #include <vlc/decoder.h>
35 #include <input_ext-dec.h>
36
37 #ifdef HAVE_UNISTD_H
38 #   include <unistd.h>                                           /* getpid() */
39 #endif
40
41 /* DVD PES size (2048) - 40 bytes (headers) */
42 #define LPCM_FRAME_LENGTH 2008
43
44 /*****************************************************************************
45  * dec_thread_t : lpcm decoder thread descriptor
46  *****************************************************************************/
47 typedef struct dec_thread_t
48 {
49     /*
50      * Thread properties
51      */
52     vlc_thread_t        thread_id;                /* id for thread functions */
53
54     /*
55      * Input properties
56      */
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 */
60
61     /*
62      * Output properties
63      */
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;
68 } dec_thread_t;
69
70 /*
71  * LPCM header :
72  * - PES header
73  * - private stream ID (16 bits) == 0xA0 -> not in the bitstream
74  * - frame number (8 bits)
75  * - unknown (16 bits) == 0x0003 ?
76  * - unknown (4 bits)
77  * - current frame (4 bits)
78  * - unknown (2 bits)
79  * - frequency (2 bits) 0 == 48 kHz, 1 == 32 kHz, 2 == ?, 3 == ?
80  * - unknown (1 bit)
81  * - number of channels - 1 (3 bits) 1 == 2 channels
82  * - start code (8 bits) == 0x80
83  */
84
85 /*****************************************************************************
86  * Local prototypes
87  *****************************************************************************/
88 static int  OpenDecoder    ( vlc_object_t * );
89 static int  RunDecoder     ( decoder_fifo_t * );
90
91 static void DecodeFrame    ( dec_thread_t * );
92 static void EndThread      ( dec_thread_t * );
93
94 /*****************************************************************************
95  * Module descriptor
96  *****************************************************************************/
97 vlc_module_begin();
98     set_description( _("linear PCM audio parser") );
99     set_capability( "decoder", 100 );
100     set_callbacks( OpenDecoder, NULL );
101 vlc_module_end();
102
103 /*****************************************************************************
104  * OpenDecoder: probe the decoder and return score
105  *****************************************************************************/
106 static int OpenDecoder( vlc_object_t *p_this )
107 {
108     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
109
110     if( p_fifo->i_fourcc != VLC_FOURCC('l','p','c','m')
111          && p_fifo->i_fourcc != VLC_FOURCC('l','p','c','b') )
112     {   
113         return VLC_EGENERIC;
114     }
115     
116     p_fifo->pf_run = RunDecoder;
117     return VLC_SUCCESS;
118 }
119
120 /*****************************************************************************
121  * RunDecoder: the lpcm decoder
122  *****************************************************************************/
123 static int RunDecoder( decoder_fifo_t * p_fifo )
124 {
125     dec_thread_t *   p_dec;
126
127     /* Allocate the memory needed to store the thread's structure */
128     if( (p_dec = (dec_thread_t *)malloc (sizeof(dec_thread_t)) )
129             == NULL) 
130     {
131         msg_Err( p_fifo, "out of memory" );
132         DecoderError( p_fifo );
133         return -1;
134     }
135
136     /* Initialize the thread properties */
137     p_dec->p_fifo = p_fifo;
138
139     /* Init the bitstream */
140     if( InitBitstream( &p_dec->bit_stream, p_dec->p_fifo,
141                        NULL, NULL ) != VLC_SUCCESS )
142     {
143         msg_Err( p_fifo, "cannot initialize bitstream" );
144         DecoderError( p_fifo );
145         EndThread( p_dec );
146         return -1;
147     }
148    
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;
152
153     /* LPCM decoder thread's main loop */
154     while ( (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )
155     {
156         DecodeFrame(p_dec);
157     }
158
159     /* If b_error is set, the lpcm decoder thread enters the error loop */
160     if ( p_dec->p_fifo->b_error )
161     {
162         DecoderError( p_dec->p_fifo );
163     }
164
165     /* End of the lpcm decoder thread */
166     EndThread( p_dec );
167
168     return 0;
169 }
170
171 /*****************************************************************************
172  * DecodeFrame: decodes a frame.
173  *****************************************************************************/
174 static void DecodeFrame( dec_thread_t * p_dec )
175 {
176     aout_buffer_t *    p_buffer;
177     mtime_t            i_pts;
178     uint8_t            i_header;
179     unsigned int       i_rate, i_original_channels;
180
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) )
185     {
186         RemoveBits( &p_dec->bit_stream, 8 );
187     }
188     if ( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error ) return;
189
190     NextPTS( &p_dec->bit_stream, &i_pts, NULL );
191     if( i_pts != 0 && i_pts != aout_DateGet( &p_dec->end_date ) )
192     {
193         aout_DateSet( &p_dec->end_date, i_pts );
194     }
195     
196     /* Get LPCM header. */
197     i_header = GetBits( &p_dec->bit_stream, 16 ) >> 8;
198
199     switch ( i_header >> 4 )
200     {
201     case 0:
202         i_rate = 48000;
203         break;
204     case 1:
205         i_rate = 96000;
206         break;
207     case 2:
208         i_rate = 44100;
209         break;
210     case 3:
211         i_rate = 32000;
212         break;
213     }
214
215     switch ( i_header & 0x7 )
216     {
217     case 0:
218         i_original_channels = AOUT_CHAN_CENTER;
219         break;
220     case 1:
221         i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
222         break;
223     case 2:
224         /* This is unsure. */
225         i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE;
226         break;
227     case 3:
228         i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
229                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
230         break;
231     case 4:
232         /* This is unsure. */
233         i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
234                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
235                                | AOUT_CHAN_LFE;
236         break;
237     case 5:
238         i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
239                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
240                                | AOUT_CHAN_CENTER | AOUT_CHAN_LFE;
241         break;
242     case 6:
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;
247         break;
248     case 7:
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;
253         break;
254     }
255
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) ) )
260     {
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;
264     }
265
266     /* Creating the audio input if not created yet. */
267     if( p_dec->p_aout_input == NULL )
268     {
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,
275                                            &p_dec->p_aout,
276                                            &p_dec->output_format );
277
278         if ( p_dec->p_aout_input == NULL )
279         {
280             p_dec->p_fifo->b_error = 1;
281             return;
282         }
283     }
284
285     if ( !aout_DateGet( &p_dec->end_date ) )
286     {
287         byte_t p_junk[LPCM_FRAME_LENGTH];
288
289         /* We've just started the stream, wait for the first PTS. */
290         GetChunk( &p_dec->bit_stream, p_junk, LPCM_FRAME_LENGTH );
291         return;
292     }
293
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 );
296     
297     if( p_buffer == NULL )
298     {
299         msg_Err( p_dec->p_fifo, "cannot get aout buffer" );
300         p_dec->p_fifo->b_error = 1;
301         return;
302     }
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 );
306
307     /* Get the whole frame. */
308     GetChunk( &p_dec->bit_stream, p_buffer->p_buffer, 
309               LPCM_FRAME_LENGTH);
310     if( p_dec->p_fifo->b_die )
311     {
312         aout_DecDeleteBuffer( p_dec->p_aout, p_dec->p_aout_input,
313                               p_buffer );
314         return;
315     }
316
317     /* Send the buffer to the aout core. */
318     aout_DecPlay( p_dec->p_aout, p_dec->p_aout_input, p_buffer );
319 }
320
321 /*****************************************************************************
322  * EndThread : lpcm decoder thread destruction
323  *****************************************************************************/
324 static void EndThread( dec_thread_t * p_dec )
325 {
326     if( p_dec->p_aout_input != NULL )
327     {
328         aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
329     }
330
331     CloseBitstream( &p_dec->bit_stream );
332     free( p_dec );
333 }