]> git.sesse.net Git - vlc/blob - modules/codec/lpcm.c
* modules/codec/lpcm.c: Attempt at supporting all channel configurations,
[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.11 2003/02/11 11:16:04 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 = 32000;
206         break;
207     default:
208         msg_Err( p_dec->p_fifo, "unsupported LPCM rate (0x%x)", i_header );
209         p_dec->p_fifo->b_error = 1;
210         return;
211     }
212
213     switch ( i_header & 0x7 )
214     {
215     case 0:
216         i_original_channels = AOUT_CHAN_CENTER;
217         break;
218     case 1:
219         i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
220         break;
221     case 2:
222         /* This is unsure. */
223         i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE;
224         break;
225     case 3:
226         i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
227                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
228         break;
229     case 4:
230         /* This is unsure. */
231         i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
232                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
233                                | AOUT_CHAN_LFE;
234         break;
235     case 5:
236         i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
237                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
238                                | AOUT_CHAN_CENTER | AOUT_CHAN_LFE;
239         break;
240     case 6:
241         i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
242                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
243                                | AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT
244                                | AOUT_CHAN_MIDDLERIGHT;
245         break;
246     case 7:
247         i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
248                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
249                                | AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT
250                                | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE;
251         break;
252     }
253
254     if( (p_dec->p_aout_input != NULL) &&
255         ( (p_dec->output_format.i_rate != i_rate)
256             || (p_dec->output_format.i_original_channels
257                   != i_original_channels) ) )
258     {
259         /* Parameters changed - this should not happen. */
260         aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
261         p_dec->p_aout_input = NULL;
262     }
263
264     /* Creating the audio input if not created yet. */
265     if( p_dec->p_aout_input == NULL )
266     {
267         p_dec->output_format.i_rate = i_rate;
268         p_dec->output_format.i_original_channels = i_original_channels;
269         p_dec->output_format.i_physical_channels
270                    = i_original_channels & AOUT_CHAN_PHYSMASK;
271         aout_DateInit( &p_dec->end_date, i_rate );
272         p_dec->p_aout_input = aout_DecNew( p_dec->p_fifo,
273                                            &p_dec->p_aout,
274                                            &p_dec->output_format );
275
276         if ( p_dec->p_aout_input == NULL )
277         {
278             p_dec->p_fifo->b_error = 1;
279             return;
280         }
281     }
282
283     if ( !aout_DateGet( &p_dec->end_date ) )
284     {
285         byte_t p_junk[LPCM_FRAME_LENGTH];
286
287         /* We've just started the stream, wait for the first PTS. */
288         GetChunk( &p_dec->bit_stream, p_junk, LPCM_FRAME_LENGTH );
289         return;
290     }
291
292     p_buffer = aout_DecNewBuffer( p_dec->p_aout, p_dec->p_aout_input,
293             LPCM_FRAME_LENGTH / p_dec->output_format.i_bytes_per_frame );
294     
295     if( p_buffer == NULL )
296     {
297         msg_Err( p_dec->p_fifo, "cannot get aout buffer" );
298         p_dec->p_fifo->b_error = 1;
299         return;
300     }
301     p_buffer->start_date = aout_DateGet( &p_dec->end_date );
302     p_buffer->end_date = aout_DateIncrement( &p_dec->end_date,
303             LPCM_FRAME_LENGTH / p_dec->output_format.i_bytes_per_frame );
304
305     /* Get the whole frame. */
306     GetChunk( &p_dec->bit_stream, p_buffer->p_buffer, 
307               LPCM_FRAME_LENGTH);
308     if( p_dec->p_fifo->b_die )
309     {
310         aout_DecDeleteBuffer( p_dec->p_aout, p_dec->p_aout_input,
311                               p_buffer );
312         return;
313     }
314
315     /* Send the buffer to the aout core. */
316     aout_DecPlay( p_dec->p_aout, p_dec->p_aout_input, p_buffer );
317 }
318
319 /*****************************************************************************
320  * EndThread : lpcm decoder thread destruction
321  *****************************************************************************/
322 static void EndThread( dec_thread_t * p_dec )
323 {
324     if( p_dec->p_aout_input != NULL )
325     {
326         aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
327     }
328
329     CloseBitstream( &p_dec->bit_stream );
330     free( p_dec );
331 }