]> git.sesse.net Git - vlc/blob - modules/codec/lpcm.c
* modules/codec/*: Fixed a PTS bug at the initialization of some codecs
[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.15 2003/03/31 22:39:28 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 /*****************************************************************************
42  * dec_thread_t : lpcm decoder thread descriptor
43  *****************************************************************************/
44 typedef struct dec_thread_t
45 {
46     /*
47      * Input properties
48      */
49     decoder_fifo_t *    p_fifo;                /* stores the PES stream data */
50
51     /*
52      * Output properties
53      */
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;
58 } dec_thread_t;
59
60 /*
61  * LPCM header :
62  * - PES header
63  * - private stream ID (16 bits) == 0xA0 -> not in the bitstream
64  * - frame number (8 bits)
65  * - unknown (16 bits) == 0x0003 ?
66  * - unknown (4 bits)
67  * - current frame (4 bits)
68  * - unknown (2 bits)
69  * - frequency (2 bits) 0 == 48 kHz, 1 == 32 kHz, 2 == ?, 3 == ?
70  * - unknown (1 bit)
71  * - number of channels - 1 (3 bits) 1 == 2 channels
72  * - start code (8 bits) == 0x80
73  */
74
75 #define LPCM_HEADER_LEN 6
76
77 /*****************************************************************************
78  * Local prototypes
79  *****************************************************************************/
80 static int  OpenDecoder    ( vlc_object_t * );
81 static int  RunDecoder     ( decoder_fifo_t * );
82
83 static void DecodeFrame    ( dec_thread_t * );
84 static void EndThread      ( dec_thread_t * );
85
86 /*****************************************************************************
87  * Module descriptor
88  *****************************************************************************/
89 vlc_module_begin();
90     set_description( _("linear PCM audio parser") );
91     set_capability( "decoder", 100 );
92     set_callbacks( OpenDecoder, NULL );
93 vlc_module_end();
94
95 /*****************************************************************************
96  * OpenDecoder: probe the decoder and return score
97  *****************************************************************************/
98 static int OpenDecoder( vlc_object_t *p_this )
99 {
100     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
101
102     if( p_fifo->i_fourcc != VLC_FOURCC('l','p','c','m')
103          && p_fifo->i_fourcc != VLC_FOURCC('l','p','c','b') )
104     {   
105         return VLC_EGENERIC;
106     }
107     
108     p_fifo->pf_run = RunDecoder;
109     return VLC_SUCCESS;
110 }
111
112 /*****************************************************************************
113  * RunDecoder: the lpcm decoder
114  *****************************************************************************/
115 static int RunDecoder( decoder_fifo_t * p_fifo )
116 {
117     dec_thread_t *   p_dec;
118
119     /* Allocate the memory needed to store the thread's structure */
120     if( (p_dec = (dec_thread_t *)malloc( sizeof(dec_thread_t)) )
121             == NULL) 
122     {
123         msg_Err( p_fifo, "out of memory" );
124         DecoderError( p_fifo );
125         return -1;
126     }
127
128     /* Initialize the thread properties */
129     p_dec->p_fifo = p_fifo;
130
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;
134
135     /* LPCM decoder thread's main loop */
136     while ( (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )
137     {
138         DecodeFrame(p_dec);
139     }
140
141     /* If b_error is set, the lpcm decoder thread enters the error loop */
142     if ( p_dec->p_fifo->b_error )
143     {
144         DecoderError( p_dec->p_fifo );
145     }
146
147     /* End of the lpcm decoder thread */
148     EndThread( p_dec );
149
150     return 0;
151 }
152
153 /*****************************************************************************
154  * DecodeFrame: decodes a frame.
155  *****************************************************************************/
156 static void DecodeFrame( dec_thread_t * p_dec )
157 {
158     pes_packet_t *     p_pes;
159     data_packet_t *    p_data;
160     aout_buffer_t *    p_buffer;
161     void *             p_dest;
162     mtime_t            i_pts;
163     uint8_t            i_header;
164     unsigned int       i_rate = 0, i_original_channels = 0, i_size;
165     int                i;
166
167     input_ExtractPES( p_dec->p_fifo, &p_pes );
168     if ( !p_pes )
169     {
170         p_dec->p_fifo->b_error = 1;
171         return;
172     }
173
174     /* Compute the size of the PES - i_pes_size includes the PES header. */
175     p_data = p_pes->p_first;
176     i_size = 0;
177     while ( p_data != NULL )
178     {
179         i_size += p_data->p_payload_end - p_data->p_payload_start;
180         p_data = p_data->p_next;
181     }
182     if ( i_size < LPCM_HEADER_LEN )
183     {
184         msg_Err(p_dec->p_fifo, "PES packet is too short");
185         input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
186         return;
187     }
188
189     i_pts = p_pes->i_pts;
190     if( i_pts != 0 && i_pts != aout_DateGet( &p_dec->end_date ) )
191     {
192         aout_DateSet( &p_dec->end_date, i_pts );
193     }
194
195     p_data = p_pes->p_first;
196     /* It necessarily contains one byte. */
197     /* Get LPCM header. */
198
199     /* Drop the first four bytes. */
200     for ( i = 0; i < 4; i++ )
201     {
202         if ( p_data->p_payload_end == p_data->p_payload_start )
203         {
204             p_data = p_data->p_next;
205         }
206         p_data->p_payload_start++;
207     }
208
209     i_header = p_data->p_payload_start[0];
210     p_data->p_payload_start++;
211
212     switch ( (i_header >> 4) & 0x3 )
213     {
214     case 0:
215         i_rate = 48000;
216         break;
217     case 1:
218         i_rate = 96000;
219         break;
220     case 2:
221         i_rate = 44100;
222         break;
223     case 3:
224         i_rate = 32000;
225         break;
226     }
227
228     switch ( i_header & 0x7 )
229     {
230     case 0:
231         i_original_channels = AOUT_CHAN_CENTER;
232         break;
233     case 1:
234         i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
235         break;
236     case 2:
237         /* This is unsure. */
238         i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE;
239         break;
240     case 3:
241         i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
242                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
243         break;
244     case 4:
245         /* This is unsure. */
246         i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
247                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
248                                | AOUT_CHAN_LFE;
249         break;
250     case 5:
251         i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
252                                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
253                                | AOUT_CHAN_CENTER | AOUT_CHAN_LFE;
254         break;
255     case 6:
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;
260         break;
261     case 7:
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;
266         break;
267     }
268
269     /* Check frame sync and drop it. */
270     if ( p_data->p_payload_end == p_data->p_payload_start )
271     {
272         p_data = p_data->p_next;
273     }
274     if ( p_data->p_payload_start[0] != 0x80 )
275     {
276         msg_Warn(p_dec->p_fifo, "no frame sync");
277         input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
278         return;
279     }
280     p_data->p_payload_start++;
281
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) ) )
286     {
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;
290     }
291
292     /* Creating the audio input if not created yet. */
293     if( p_dec->p_aout_input == NULL )
294     {
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         aout_DateSet( &p_dec->end_date, i_pts );
301         p_dec->p_aout_input = aout_DecNew( p_dec->p_fifo,
302                                            &p_dec->p_aout,
303                                            &p_dec->output_format );
304
305         if ( p_dec->p_aout_input == NULL )
306         {
307             p_dec->p_fifo->b_error = 1;
308             input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
309             return;
310         }
311     }
312
313     if ( !aout_DateGet( &p_dec->end_date ) )
314     {
315         /* We've just started the stream, wait for the first PTS. */
316         input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
317         return;
318     }
319
320     p_buffer = aout_DecNewBuffer( p_dec->p_aout, p_dec->p_aout_input,
321             (i_size - LPCM_HEADER_LEN)
322                 / p_dec->output_format.i_bytes_per_frame );
323     
324     if( p_buffer == NULL )
325     {
326         msg_Err( p_dec->p_fifo, "cannot get aout buffer" );
327         p_dec->p_fifo->b_error = 1;
328         input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
329         return;
330     }
331     p_buffer->start_date = aout_DateGet( &p_dec->end_date );
332     p_buffer->end_date = aout_DateIncrement( &p_dec->end_date,
333             (i_size - LPCM_HEADER_LEN)
334                 / p_dec->output_format.i_bytes_per_frame );
335
336     /* Get the whole frame. */
337     p_dest = p_buffer->p_buffer;
338     while ( p_data != NULL )
339     {
340         p_dec->p_fifo->p_vlc->pf_memcpy( p_dest, p_data->p_payload_start,
341                 p_data->p_payload_end - p_data->p_payload_start );
342         p_dest += p_data->p_payload_end - p_data->p_payload_start;
343         p_data = p_data->p_next;
344     }
345     input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
346
347     /* Send the buffer to the aout core. */
348     aout_DecPlay( p_dec->p_aout, p_dec->p_aout_input, p_buffer );
349 }
350
351 /*****************************************************************************
352  * EndThread : lpcm decoder thread destruction
353  *****************************************************************************/
354 static void EndThread( dec_thread_t * p_dec )
355 {
356     if( p_dec->p_aout_input != NULL )
357     {
358         aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
359     }
360
361     free( p_dec );
362 }