]> git.sesse.net Git - vlc/blob - src/ac3_decoder/ac3_decoder_thread.c
6cac3615ac4a61c1370e4c155106e462fd46117f
[vlc] / src / ac3_decoder / ac3_decoder_thread.c
1 /*****************************************************************************
2  * ac3_decoder_thread.c: ac3 decoder thread
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  * $Id: ac3_decoder_thread.c,v 1.32 2001/05/06 04:32:02 sam Exp $
6  *
7  * Authors: Michel Lespinasse <walken@zoy.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*
25  * TODO :
26  *
27  * - vérifier l'état de la fifo de sortie avant d'y stocker les samples
28  *   décodés ;
29  * - vlc_cond_signal() / vlc_cond_wait()
30  *
31  */
32
33 /*****************************************************************************
34  * Preamble
35  *****************************************************************************/
36 #include "defs.h"
37
38 #include <unistd.h>                                              /* getpid() */
39
40 #include <stdio.h>                                           /* "intf_msg.h" */
41 #include <stdlib.h>                                      /* malloc(), free() */
42 #include <string.h>                                              /* memset() */
43
44 #include "config.h"
45 #include "common.h"
46 #include "threads.h"
47 #include "mtime.h"
48
49 #include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */
50
51 #include "stream_control.h"
52 #include "input_ext-dec.h"
53
54 #include "audio_output.h"
55
56 #include "ac3_decoder.h"
57 #include "ac3_decoder_thread.h"
58
59 #define AC3DEC_FRAME_SIZE (2*1536) 
60
61 /*****************************************************************************
62  * Local prototypes
63  *****************************************************************************/
64 static int      InitThread              (ac3dec_thread_t * p_adec);
65 static void     RunThread               (ac3dec_thread_t * p_adec);
66 static void     ErrorThread             (ac3dec_thread_t * p_adec);
67 static void     EndThread               (ac3dec_thread_t * p_adec);
68 static void     BitstreamCallback       ( bit_stream_t *p_bit_stream,
69                                               boolean_t b_new_pes );
70
71
72 /*****************************************************************************
73  * ac3dec_CreateThread: creates an ac3 decoder thread
74  *****************************************************************************/
75 vlc_thread_t ac3dec_CreateThread( adec_config_t * p_config )
76 {
77     ac3dec_thread_t *   p_ac3dec_t;
78
79     intf_DbgMsg( "ac3dec debug: creating ac3 decoder thread" );
80
81     /* Allocate the memory needed to store the thread's structure */
82     if((p_ac3dec_t = (ac3dec_thread_t *)malloc(sizeof(ac3dec_thread_t)))==NULL)
83     {
84         intf_ErrMsg ( "ac3dec error: not enough memory "
85                       "for ac3dec_CreateThread() to create the new thread");
86         return 0;
87     }
88     
89     /*
90      * Initialize the thread properties
91      */
92     p_ac3dec_t->p_config = p_config;
93     p_ac3dec_t->p_fifo = p_config->decoder_config.p_decoder_fifo;
94
95     /* Initialize the ac3 decoder structures */
96     ac3_init (&p_ac3dec_t->ac3_decoder);
97
98     /*
99      * Initialize the output properties
100      */
101     p_ac3dec_t->p_aout_fifo = NULL;
102
103     /* Spawn the ac3 decoder thread */
104     if (vlc_thread_create(&p_ac3dec_t->thread_id, "ac3 decoder", 
105                 (vlc_thread_func_t)RunThread, (void *)p_ac3dec_t))
106     {
107         intf_ErrMsg( "ac3dec error: can't spawn ac3 decoder thread" );
108         free (p_ac3dec_t);
109         return 0;
110     }
111
112     intf_DbgMsg ("ac3dec debug: ac3 decoder thread (%p) created", p_ac3dec_t);
113     return p_ac3dec_t->thread_id;
114 }
115
116 /* Following functions are local */
117
118 /*****************************************************************************
119  * InitThread : initialize an ac3 decoder thread
120  *****************************************************************************/
121 static int InitThread (ac3dec_thread_t * p_ac3dec_t)
122 {
123     intf_DbgMsg("ac3dec debug: initializing ac3 decoder thread %p",p_ac3dec_t);
124
125     p_ac3dec_t->p_config->decoder_config.pf_init_bit_stream(
126             &p_ac3dec_t->ac3_decoder.bit_stream,
127             p_ac3dec_t->p_config->decoder_config.p_decoder_fifo,
128             BitstreamCallback, (void *) p_ac3dec_t );
129
130     /* Creating the audio output fifo */
131     p_ac3dec_t->p_aout_fifo = aout_CreateFifo( AOUT_ADEC_STEREO_FIFO, 2, 0, 0,
132                                                AC3DEC_FRAME_SIZE, NULL  );
133     if ( p_ac3dec_t->p_aout_fifo == NULL )
134     {
135         return -1;
136     }
137
138     intf_DbgMsg("ac3dec debug: ac3 decoder thread %p initialized", p_ac3dec_t);
139     return 0;
140 }
141
142 /*****************************************************************************
143  * RunThread : ac3 decoder thread
144  *****************************************************************************/
145 static void RunThread (ac3dec_thread_t * p_ac3dec_t)
146 {
147     int sync;
148
149     intf_DbgMsg ("ac3dec debug: running ac3 decoder thread (%p) (pid == %i)", p_ac3dec_t, getpid());
150
151     /* Initializing the ac3 decoder thread */
152     if (InitThread (p_ac3dec_t)) /* XXX?? */
153     {
154         p_ac3dec_t->p_fifo->b_error = 1;
155     }
156
157     sync = 0;
158     p_ac3dec_t->sync_ptr = 0;
159
160     /* ac3 decoder thread's main loop */
161     /* FIXME : do we have enough room to store the decoded frames ?? */
162     while ((!p_ac3dec_t->p_fifo->b_die) && (!p_ac3dec_t->p_fifo->b_error))
163     {
164         s16 * buffer;
165         ac3_sync_info_t sync_info;
166         int ptr;
167
168         if (!sync) {
169             do {
170                 GetBits(&p_ac3dec_t->ac3_decoder.bit_stream,8);
171             } while ((!p_ac3dec_t->sync_ptr) && (!p_ac3dec_t->p_fifo->b_die)
172                     && (!p_ac3dec_t->p_fifo->b_error));
173             
174             ptr = p_ac3dec_t->sync_ptr;
175
176             while(ptr-- && (!p_ac3dec_t->p_fifo->b_die)
177                 && (!p_ac3dec_t->p_fifo->b_error))
178             {
179                 p_ac3dec_t->ac3_decoder.bit_stream.p_byte++;
180             }
181                         
182             /* we are in sync now */
183             sync = 1;
184         }
185
186         if (DECODER_FIFO_START(*p_ac3dec_t->p_fifo)->i_pts)
187         {
188             p_ac3dec_t->p_aout_fifo->date[p_ac3dec_t->p_aout_fifo->l_end_frame] =
189                 DECODER_FIFO_START(*p_ac3dec_t->p_fifo)->i_pts;
190             DECODER_FIFO_START(*p_ac3dec_t->p_fifo)->i_pts = 0;
191         } else {
192             p_ac3dec_t->p_aout_fifo->date[p_ac3dec_t->p_aout_fifo->l_end_frame] =
193                 LAST_MDATE;
194         }
195     
196         if (ac3_sync_frame (&p_ac3dec_t->ac3_decoder, &sync_info))
197         {
198             sync = 0;
199             goto bad_frame;
200         }
201
202         p_ac3dec_t->p_aout_fifo->l_rate = sync_info.sample_rate;
203
204         buffer = ((s16 *)p_ac3dec_t->p_aout_fifo->buffer) + 
205             (p_ac3dec_t->p_aout_fifo->l_end_frame * AC3DEC_FRAME_SIZE);
206
207         if (ac3_decode_frame (&p_ac3dec_t->ac3_decoder, buffer))
208         {
209             sync = 0;
210             goto bad_frame;
211         }
212         
213         vlc_mutex_lock (&p_ac3dec_t->p_aout_fifo->data_lock);
214         p_ac3dec_t->p_aout_fifo->l_end_frame = 
215             (p_ac3dec_t->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
216         vlc_cond_signal (&p_ac3dec_t->p_aout_fifo->data_wait);
217         vlc_mutex_unlock (&p_ac3dec_t->p_aout_fifo->data_lock);
218
219         bad_frame:
220             RealignBits(&p_ac3dec_t->ac3_decoder.bit_stream);
221     }
222
223     /* If b_error is set, the ac3 decoder thread enters the error loop */
224     if (p_ac3dec_t->p_fifo->b_error)
225     {
226         ErrorThread (p_ac3dec_t);
227     }
228
229     /* End of the ac3 decoder thread */
230     EndThread (p_ac3dec_t);
231 }
232
233 /*****************************************************************************
234  * ErrorThread : ac3 decoder's RunThread() error loop
235  *****************************************************************************/
236 static void ErrorThread (ac3dec_thread_t * p_ac3dec_t)
237 {
238     /* We take the lock, because we are going to read/write the start/end
239      * indexes of the decoder fifo */
240     vlc_mutex_lock (&p_ac3dec_t->p_fifo->data_lock);
241
242     /* Wait until a `die' order is sent */
243     while (!p_ac3dec_t->p_fifo->b_die)
244     {
245         /* Trash all received PES packets */
246         while (!DECODER_FIFO_ISEMPTY(*p_ac3dec_t->p_fifo))
247         {
248             p_ac3dec_t->p_fifo->pf_delete_pes(p_ac3dec_t->p_fifo->p_packets_mgt,
249                     DECODER_FIFO_START(*p_ac3dec_t->p_fifo));
250             DECODER_FIFO_INCSTART (*p_ac3dec_t->p_fifo);
251         }
252
253         /* Waiting for the input thread to put new PES packets in the fifo */
254         vlc_cond_wait (&p_ac3dec_t->p_fifo->data_wait,
255                        &p_ac3dec_t->p_fifo->data_lock);
256     }
257
258     /* We can release the lock before leaving */
259     vlc_mutex_unlock (&p_ac3dec_t->p_fifo->data_lock);
260 }
261
262 /*****************************************************************************
263  * EndThread : ac3 decoder thread destruction
264  *****************************************************************************/
265 static void EndThread (ac3dec_thread_t * p_ac3dec_t)
266 {
267     intf_DbgMsg ("ac3dec debug: destroying ac3 decoder thread %p", p_ac3dec_t);
268
269     /* If the audio output fifo was created, we destroy it */
270     if (p_ac3dec_t->p_aout_fifo != NULL)
271     {
272         aout_DestroyFifo (p_ac3dec_t->p_aout_fifo);
273
274         /* Make sure the output thread leaves the NextFrame() function */
275         vlc_mutex_lock (&(p_ac3dec_t->p_aout_fifo->data_lock));
276         vlc_cond_signal (&(p_ac3dec_t->p_aout_fifo->data_wait));
277         vlc_mutex_unlock (&(p_ac3dec_t->p_aout_fifo->data_lock));
278         
279     }
280
281     /* Destroy descriptor */
282     free( p_ac3dec_t->p_config );
283     free( p_ac3dec_t );
284
285     intf_DbgMsg ("ac3dec debug: ac3 decoder thread %p destroyed", p_ac3dec_t);
286 }
287
288 /*****************************************************************************
289 * BitstreamCallback: Import parameters from the new data/PES packet
290 *****************************************************************************
291 * This function is called by input's NextDataPacket.
292 *****************************************************************************/
293 static void BitstreamCallback ( bit_stream_t * p_bit_stream,
294                                         boolean_t b_new_pes)
295 {
296
297     ac3dec_thread_t *p_ac3dec_t=(ac3dec_thread_t *)p_bit_stream->p_callback_arg;
298
299     if( b_new_pes )
300     {
301         int ptr;
302         
303         ptr = *(p_bit_stream->p_byte + 1);
304         ptr <<= 8;
305         ptr |= *(p_bit_stream->p_byte + 2);
306         p_ac3dec_t->sync_ptr = ptr;
307         p_bit_stream->p_byte += 3;                                                            
308     }
309 }