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