]> git.sesse.net Git - vlc/blob - modules/codec/lpcm.c
* Several stability patches for multiple input streams aout.
[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.3 2002/09/28 13:05:16 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     InitBitstream( &p_dec->bit_stream, p_dec->p_fifo,
125                    NULL, NULL );
126    
127     /* FIXME : I suppose the number of channel and sampling rate 
128      * are somewhere in the headers */
129     p_dec->output_format.i_format = AOUT_FMT_S16_BE;
130     p_dec->output_format.i_channels = 2;
131     p_dec->output_format.i_rate = 48000;
132     
133     aout_DateInit( &p_dec->end_date, 48000 );
134     p_dec->p_aout = NULL;
135     p_dec->p_aout_input = aout_DecNew( p_dec->p_fifo,
136                                        &p_dec->p_aout,
137                                        &p_dec->output_format );
138
139     if ( p_dec->p_aout_input == NULL )
140     {
141         msg_Err( p_dec->p_fifo, "failed to create aout fifo" );
142         p_dec->p_fifo->b_error = 1;
143         return( -1 );
144     }
145
146     /* lpcm decoder thread's main loop */
147     while ( (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )
148     {
149         DecodeFrame(p_dec);
150     }
151
152     /* If b_error is set, the lpcm decoder thread enters the error loop */
153     if ( p_dec->p_fifo->b_error )
154     {
155         DecoderError( p_dec->p_fifo );
156     }
157
158     /* End of the lpcm decoder thread */
159     EndThread( p_dec );
160
161     return 0;
162 }
163
164 /*****************************************************************************
165  * DecodeFrame: decodes a frame.
166  *****************************************************************************/
167 void DecodeFrame( dec_thread_t * p_dec )
168 {
169     aout_buffer_t *    p_aout_buffer;
170     mtime_t     i_pts;
171
172     NextPTS( &p_dec->bit_stream, &i_pts, NULL );
173     
174     if( i_pts != 0 && i_pts != aout_DateGet( &p_dec->end_date ) )
175     {
176         aout_DateSet( &p_dec->end_date, i_pts );
177     }
178     
179     p_aout_buffer = aout_DecNewBuffer( p_dec->p_aout,
180                                        p_dec->p_aout_input,
181                                        LPCM_FRAME_NB );
182     
183     if( !p_aout_buffer )
184     {
185         msg_Err( p_dec->p_fifo, "cannot get aout buffer" );
186         p_dec->p_fifo->b_error = 1;
187         return;
188     }
189     
190     p_aout_buffer->start_date = aout_DateGet( &p_dec->end_date );
191     p_aout_buffer->end_date = aout_DateIncrement( &p_dec->end_date,
192                                                    LPCM_FRAME_NB );
193
194     /* Look for sync word - should be 0x0180 */
195     RealignBits( &p_dec->bit_stream );
196     while ( (GetBits( &p_dec->bit_stream, 16 ) ) != 0x0180 && 
197              (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error));
198
199     GetChunk( &p_dec->bit_stream, p_aout_buffer->p_buffer, 
200               LPCM_FRAME_NB * 4);
201
202     if( p_dec->p_fifo->b_die )
203     {
204         aout_DecDeleteBuffer( p_dec->p_aout, p_dec->p_aout_input,
205                               p_aout_buffer );
206         return;
207     }
208
209     aout_DecPlay( p_dec->p_aout, p_dec->p_aout_input, 
210                   p_aout_buffer );
211 }
212
213 /*****************************************************************************
214  * EndThread : lpcm decoder thread destruction
215  *****************************************************************************/
216 static void EndThread( dec_thread_t * p_dec )
217 {
218     if( p_dec->p_aout_input != NULL )
219     {
220         aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
221     }
222
223     free( p_dec );
224 }