]> git.sesse.net Git - vlc/blob - modules/codec/lpcm.c
Major change of the channels management. p_format->i_channels disappeares
[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.7 2002/11/14 22:38:47 massiot 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_physical_channels
137            = p_dec->output_format.i_original_channels
138            = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
139     p_dec->output_format.i_rate = 48000;
140     
141     aout_DateInit( &p_dec->end_date, 48000 );
142     p_dec->p_aout = NULL;
143     p_dec->p_aout_input = aout_DecNew( p_dec->p_fifo,
144                                        &p_dec->p_aout,
145                                        &p_dec->output_format );
146
147     if ( p_dec->p_aout_input == NULL )
148     {
149         msg_Err( p_dec->p_fifo, "failed to create aout fifo" );
150         p_dec->p_fifo->b_error = 1;
151         EndThread( p_dec );
152         return( -1 );
153     }
154
155     /* lpcm decoder thread's main loop */
156     while ( (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )
157     {
158         DecodeFrame(p_dec);
159     }
160
161     /* If b_error is set, the lpcm decoder thread enters the error loop */
162     if ( p_dec->p_fifo->b_error )
163     {
164         DecoderError( p_dec->p_fifo );
165     }
166
167     /* End of the lpcm decoder thread */
168     EndThread( p_dec );
169
170     return 0;
171 }
172
173 /*****************************************************************************
174  * DecodeFrame: decodes a frame.
175  *****************************************************************************/
176 void DecodeFrame( dec_thread_t * p_dec )
177 {
178     aout_buffer_t *    p_aout_buffer;
179     mtime_t     i_pts;
180
181     NextPTS( &p_dec->bit_stream, &i_pts, NULL );
182     
183     if( i_pts != 0 && i_pts != aout_DateGet( &p_dec->end_date ) )
184     {
185         aout_DateSet( &p_dec->end_date, i_pts );
186     }
187     
188     p_aout_buffer = aout_DecNewBuffer( p_dec->p_aout,
189                                        p_dec->p_aout_input,
190                                        LPCM_FRAME_NB );
191     
192     if( !p_aout_buffer )
193     {
194         msg_Err( p_dec->p_fifo, "cannot get aout buffer" );
195         p_dec->p_fifo->b_error = 1;
196         return;
197     }
198     
199     p_aout_buffer->start_date = aout_DateGet( &p_dec->end_date );
200     p_aout_buffer->end_date = aout_DateIncrement( &p_dec->end_date,
201                                                    LPCM_FRAME_NB );
202
203     /* Look for sync word - should be 0x0180 */
204     RealignBits( &p_dec->bit_stream );
205     while ( (GetBits( &p_dec->bit_stream, 16 ) ) != 0x0180 && 
206              (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error));
207
208     GetChunk( &p_dec->bit_stream, p_aout_buffer->p_buffer, 
209               LPCM_FRAME_NB * 4);
210
211     if( p_dec->p_fifo->b_die )
212     {
213         aout_DecDeleteBuffer( p_dec->p_aout, p_dec->p_aout_input,
214                               p_aout_buffer );
215         return;
216     }
217
218     aout_DecPlay( p_dec->p_aout, p_dec->p_aout_input, 
219                   p_aout_buffer );
220 }
221
222 /*****************************************************************************
223  * EndThread : lpcm decoder thread destruction
224  *****************************************************************************/
225 static void EndThread( dec_thread_t * p_dec )
226 {
227     if( p_dec->p_aout_input != NULL )
228     {
229         aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
230     }
231
232     CloseBitstream( &p_dec->bit_stream );
233     free( p_dec );
234 }