]> git.sesse.net Git - vlc/blob - modules/codec/lpcm.c
* ALL: removed GetPES and NextPES, we now use input_ExtractPES everywhere instead
[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.6 2002/10/27 16:58:14 gbazin Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Henri Fallon <henri@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <string.h>                                    /* memcpy(), memset() */
30
31 #include <vlc/vlc.h>
32 #include <vlc/aout.h>
33 #include <vlc/decoder.h>
34 #include <input_ext-dec.h>
35
36 #ifdef HAVE_UNISTD_H
37 #   include <unistd.h>                                           /* getpid() */
38 #endif
39
40 #define LPCM_FRAME_NB 502
41
42 /*****************************************************************************
43  * dec_thread_t : lpcm decoder thread descriptor
44  *****************************************************************************/
45 typedef struct dec_thread_t
46 {
47     /*
48      * Thread properties
49      */
50     vlc_thread_t        thread_id;                /* id for thread functions */
51
52     /*
53      * Input properties
54      */
55     decoder_fifo_t *    p_fifo;                /* stores the PES stream data */
56     bit_stream_t        bit_stream;
57     int                 sync_ptr;         /* sync ptr from lpcm magic header */
58
59     /*
60      * Output properties
61      */
62     aout_instance_t        *p_aout;
63     aout_input_t           *p_aout_input;
64     audio_sample_format_t   output_format;
65     audio_date_t            end_date;
66 } dec_thread_t;
67
68 /*****************************************************************************
69  * Local prototypes
70  *****************************************************************************/
71 static int  OpenDecoder    ( vlc_object_t * );
72 static int  RunDecoder     ( decoder_fifo_t * );
73
74        void DecodeFrame    ( dec_thread_t * );
75 // static int  InitThread     ( dec_thread_t * );
76 static void EndThread      ( dec_thread_t * );
77
78 /*****************************************************************************
79  * Module descriptor
80  *****************************************************************************/
81 vlc_module_begin();
82     set_description( _("linear PCM audio parser") );
83     set_capability( "decoder", 100 );
84     set_callbacks( OpenDecoder, NULL );
85 vlc_module_end();
86
87 /*****************************************************************************
88  * OpenDecoder: probe the decoder and return score
89  *****************************************************************************/
90 static int OpenDecoder( vlc_object_t *p_this )
91 {
92     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
93
94     if( p_fifo->i_fourcc != VLC_FOURCC('l','p','c','m')
95          && p_fifo->i_fourcc != VLC_FOURCC('l','p','c','b') )
96     {   
97         return VLC_EGENERIC;
98     }
99     
100     p_fifo->pf_run = RunDecoder;
101     return VLC_SUCCESS;
102 }
103
104 /*****************************************************************************
105  * RunDecoder: the lpcm decoder
106  *****************************************************************************/
107 static int RunDecoder( decoder_fifo_t * p_fifo )
108 {
109     dec_thread_t *   p_dec;
110
111     /* Allocate the memory needed to store the thread's structure */
112     if( (p_dec = (dec_thread_t *)malloc (sizeof(dec_thread_t)) )
113             == NULL) 
114     {
115         msg_Err( p_fifo, "out of memory" );
116         DecoderError( p_fifo );
117         return -1;
118     }
119
120     /* Initialize the thread properties */
121     p_dec->p_fifo = p_fifo;
122
123     /* Init the bitstream */
124     if( InitBitstream( &p_dec->bit_stream, p_dec->p_fifo,
125                        NULL, NULL ) != VLC_SUCCESS )
126     {
127         msg_Err( p_fifo, "cannot initialize bitstream" );
128         DecoderError( p_fifo );
129         EndThread( p_dec );
130         return -1;
131     }
132    
133     /* FIXME : I suppose the number of channel and sampling rate 
134      * are somewhere in the headers */
135     p_dec->output_format.i_format = VLC_FOURCC('s','1','6','b');
136     p_dec->output_format.i_channels = AOUT_CHAN_STEREO;
137     p_dec->output_format.i_rate = 48000;
138     
139     aout_DateInit( &p_dec->end_date, 48000 );
140     p_dec->p_aout = NULL;
141     p_dec->p_aout_input = aout_DecNew( p_dec->p_fifo,
142                                        &p_dec->p_aout,
143                                        &p_dec->output_format );
144
145     if ( p_dec->p_aout_input == NULL )
146     {
147         msg_Err( p_dec->p_fifo, "failed to create aout fifo" );
148         p_dec->p_fifo->b_error = 1;
149         EndThread( p_dec );
150         return( -1 );
151     }
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 void DecodeFrame( dec_thread_t * p_dec )
175 {
176     aout_buffer_t *    p_aout_buffer;
177     mtime_t     i_pts;
178
179     NextPTS( &p_dec->bit_stream, &i_pts, NULL );
180     
181     if( i_pts != 0 && i_pts != aout_DateGet( &p_dec->end_date ) )
182     {
183         aout_DateSet( &p_dec->end_date, i_pts );
184     }
185     
186     p_aout_buffer = aout_DecNewBuffer( p_dec->p_aout,
187                                        p_dec->p_aout_input,
188                                        LPCM_FRAME_NB );
189     
190     if( !p_aout_buffer )
191     {
192         msg_Err( p_dec->p_fifo, "cannot get aout buffer" );
193         p_dec->p_fifo->b_error = 1;
194         return;
195     }
196     
197     p_aout_buffer->start_date = aout_DateGet( &p_dec->end_date );
198     p_aout_buffer->end_date = aout_DateIncrement( &p_dec->end_date,
199                                                    LPCM_FRAME_NB );
200
201     /* Look for sync word - should be 0x0180 */
202     RealignBits( &p_dec->bit_stream );
203     while ( (GetBits( &p_dec->bit_stream, 16 ) ) != 0x0180 && 
204              (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error));
205
206     GetChunk( &p_dec->bit_stream, p_aout_buffer->p_buffer, 
207               LPCM_FRAME_NB * 4);
208
209     if( p_dec->p_fifo->b_die )
210     {
211         aout_DecDeleteBuffer( p_dec->p_aout, p_dec->p_aout_input,
212                               p_aout_buffer );
213         return;
214     }
215
216     aout_DecPlay( p_dec->p_aout, p_dec->p_aout_input, 
217                   p_aout_buffer );
218 }
219
220 /*****************************************************************************
221  * EndThread : lpcm decoder thread destruction
222  *****************************************************************************/
223 static void EndThread( dec_thread_t * p_dec )
224 {
225     if( p_dec->p_aout_input != NULL )
226     {
227         aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
228     }
229
230     CloseBitstream( &p_dec->bit_stream );
231     free( p_dec );
232 }