]> git.sesse.net Git - vlc/blob - plugins/lpcm_adec/lpcm_adec.c
The loop in lpcm decoder DecodeFrame() could not exit after b_die
[vlc] / plugins / lpcm_adec / lpcm_adec.c
1 /*****************************************************************************
2  * lpcm_adec.c: lpcm decoder thread
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: lpcm_adec.c,v 1.18 2002/07/17 21:24:06 stef 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 <string.h>                                    /* memcpy(), memset() */
29 #include <stdlib.h>                                      /* malloc(), free() */
30
31 #include <vlc/vlc.h>
32 #include <vlc/aout.h>
33 #include <vlc/decoder.h>
34
35 #ifdef HAVE_UNISTD_H
36 #   include <unistd.h>                                           /* getpid() */
37 #endif
38
39 #include "lpcm_adec.h"
40
41 /*****************************************************************************
42  * Local prototypes
43  *****************************************************************************/
44 static int  decoder_Probe  ( u8 * );
45 static int  decoder_Run    ( decoder_fifo_t * );
46        void DecodeFrame    ( lpcmdec_thread_t * );
47 static int  InitThread     ( lpcmdec_thread_t * );
48 static void EndThread      ( lpcmdec_thread_t * );
49
50
51 /*****************************************************************************
52  * Capabilities
53  *****************************************************************************/
54 void _M( adec_getfunctions )( function_list_t * p_function_list )
55 {
56     p_function_list->functions.dec.pf_probe = decoder_Probe;
57     p_function_list->functions.dec.pf_run   = decoder_Run;
58 }
59
60 /*****************************************************************************
61  * Build configuration tree.
62  *****************************************************************************/
63 MODULE_CONFIG_START
64 MODULE_CONFIG_STOP
65
66 MODULE_INIT_START
67     SET_DESCRIPTION( _("linear PCM audio decoder") )
68     ADD_CAPABILITY( DECODER, 100 )
69 MODULE_INIT_STOP
70
71 MODULE_ACTIVATE_START
72     _M( adec_getfunctions )( &p_module->p_functions->dec );
73 MODULE_ACTIVATE_STOP
74
75 MODULE_DEACTIVATE_START
76 MODULE_DEACTIVATE_STOP
77
78 /*****************************************************************************
79  * decoder_Probe: probe the decoder and return score
80  *****************************************************************************/
81 static int decoder_Probe( u8 *pi_type )
82 {
83     return ( *pi_type == LPCM_AUDIO_ES ) ? 0 : -1;
84 }
85
86 /*****************************************************************************
87  * decoder_Run: the lpcm decoder
88  *****************************************************************************/
89 static int decoder_Run( decoder_fifo_t * p_fifo )
90 {
91     lpcmdec_thread_t *   p_lpcmdec;
92
93     /* Allocate the memory needed to store the thread's structure */
94     if( (p_lpcmdec = (lpcmdec_thread_t *)malloc (sizeof(lpcmdec_thread_t)) )
95             == NULL) 
96     {
97         msg_Err( p_fifo, "out of memory" );
98         DecoderError( p_fifo );
99         return( -1 );
100     }
101
102     /*
103      * Initialize the thread properties
104      */
105     p_lpcmdec->p_fifo = p_fifo;
106
107     if( InitThread( p_lpcmdec ) )
108     {
109         DecoderError( p_fifo );
110         free( p_lpcmdec );
111         return( -1 );
112     }
113
114     /* lpcm decoder thread's main loop */
115     while ((!p_lpcmdec->p_fifo->b_die) && (!p_lpcmdec->p_fifo->b_error))
116     {
117         DecodeFrame(p_lpcmdec);
118     }
119
120     /* If b_error is set, the lpcm decoder thread enters the error loop */
121     if (p_lpcmdec->p_fifo->b_error)
122     {
123         DecoderError( p_lpcmdec->p_fifo );
124     }
125
126     /* End of the lpcm decoder thread */
127     EndThread (p_lpcmdec);
128
129     return( 0 );
130 }
131
132 /*****************************************************************************
133  * InitThread : initialize an lpcm decoder thread
134  *****************************************************************************/
135 static int InitThread (lpcmdec_thread_t * p_lpcmdec)
136 {
137
138     /* Init the BitStream */
139     InitBitstream( &p_lpcmdec->bit_stream, p_lpcmdec->p_fifo,
140                    NULL, NULL);
141
142     /* Creating the audio output fifo */
143     p_lpcmdec->p_aout_fifo =
144                 aout_CreateFifo( p_lpcmdec->p_fifo, AOUT_FIFO_PCM,
145                                  2, 48000, LPCMDEC_FRAME_SIZE / 2, NULL  );
146     if ( p_lpcmdec->p_aout_fifo == NULL )
147     {
148         return( -1 );
149     }
150     return( 0 );
151 }
152
153 /*****************************************************************************
154  * DecodeFrame: decodes a frame.
155  *****************************************************************************/
156 void DecodeFrame( lpcmdec_thread_t * p_lpcmdec )
157 {
158     byte_t *    buffer,p_temp[LPCMDEC_FRAME_SIZE];
159     vlc_bool_t  b_sync;
160     int         i_loop;
161
162     CurrentPTS( &p_lpcmdec->bit_stream,
163         &p_lpcmdec->p_aout_fifo->date[p_lpcmdec->p_aout_fifo->i_end_frame],
164         NULL );
165     if( !p_lpcmdec->p_aout_fifo->date[p_lpcmdec->p_aout_fifo->i_end_frame] )
166     {
167         p_lpcmdec->p_aout_fifo->date[p_lpcmdec->p_aout_fifo->i_end_frame] =
168             LAST_MDATE;
169     }
170
171     buffer = ((byte_t *)p_lpcmdec->p_aout_fifo->buffer) + 
172               (p_lpcmdec->p_aout_fifo->i_end_frame * LPCMDEC_FRAME_SIZE);
173
174     RemoveBits32(&p_lpcmdec->bit_stream);
175 #if 0
176     byte1 = GetBits(&p_lpcmdec->bit_stream, 8) ;
177     byte2 = GetBits(&p_lpcmdec->bit_stream, 8) ;
178     
179     /* I only have 2 test streams. As far as I understand
180      * after the RemoveBits and the 2 GetBits, we should be exactly 
181      * where we whant : the sync word : 0x0180.
182      * If not, we got and find it. */
183     while( ( byte1 != 0x01 || byte2 != 0x80 ) && (!p_lpcmdec->p_fifo->b_die)
184                                        && (!p_lpcmdec->p_fifo->b_error) )
185     {
186         byte1 = byte2;
187         byte2 = GetBits(&p_lpcmdec->bit_stream, 8);
188     }
189 #else
190     b_sync = 0;
191     while( ( !p_lpcmdec->p_fifo->b_die ) &&
192            ( !p_lpcmdec->p_fifo->b_error ) &&
193            ( !b_sync ) )
194     {
195         while( ( !p_lpcmdec->p_fifo->b_die ) &&
196                ( !p_lpcmdec->p_fifo->b_error ) &&
197                ( GetBits( &p_lpcmdec->bit_stream, 8 ) != 0x01 ) );
198         b_sync = ( ShowBits( &p_lpcmdec->bit_stream, 8 ) == 0x80 );
199     }
200     RemoveBits( &p_lpcmdec->bit_stream, 8 );
201 #endif
202     
203     GetChunk( &p_lpcmdec->bit_stream, p_temp, LPCMDEC_FRAME_SIZE);
204     if( p_lpcmdec->p_fifo->b_die || p_lpcmdec->p_fifo->b_error ) return;
205
206     for( i_loop = 0; i_loop < LPCMDEC_FRAME_SIZE/2; i_loop++ )
207     {
208         buffer[2*i_loop]=p_temp[2*i_loop+1];
209         buffer[2*i_loop+1]=p_temp[2*i_loop];
210     }
211     
212     vlc_mutex_lock (&p_lpcmdec->p_aout_fifo->data_lock);
213     p_lpcmdec->p_aout_fifo->i_end_frame = 
214         (p_lpcmdec->p_aout_fifo->i_end_frame + 1) & AOUT_FIFO_SIZE;
215     vlc_cond_signal (&p_lpcmdec->p_aout_fifo->data_wait);
216     vlc_mutex_unlock (&p_lpcmdec->p_aout_fifo->data_lock);
217 }
218
219 /*****************************************************************************
220  * EndThread : lpcm decoder thread destruction
221  *****************************************************************************/
222 static void EndThread( lpcmdec_thread_t * p_lpcmdec )
223 {
224     /* If the audio output fifo was created, we destroy it */
225     if( p_lpcmdec->p_aout_fifo != NULL ) 
226     {
227         aout_DestroyFifo( p_lpcmdec->p_aout_fifo );
228
229         /* Make sure the output thread leaves the NextFrame() function */
230         vlc_mutex_lock( &(p_lpcmdec->p_aout_fifo->data_lock) );
231         vlc_cond_signal( &(p_lpcmdec->p_aout_fifo->data_wait) );
232         vlc_mutex_unlock( &(p_lpcmdec->p_aout_fifo->data_lock) );
233     }
234
235     /* Destroy descriptor */
236     free( p_lpcmdec );
237 }