]> git.sesse.net Git - vlc/blob - src/ac3_decoder/ac3_decoder_thread.c
* Mandatory step for video output IV and the audio output quality
[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.31 2001/05/01 04:18:18 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
43 #include "config.h"
44 #include "common.h"
45 #include "threads.h"
46 #include "mtime.h"
47
48 #include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */
49
50 #include "stream_control.h"
51 #include "input_ext-dec.h"
52
53 #include "audio_output.h"
54
55 #include "ac3_decoder.h"
56 #include "ac3_decoder_thread.h"
57
58 #define AC3DEC_FRAME_SIZE (2*1536) 
59
60 /*****************************************************************************
61  * Local prototypes
62  *****************************************************************************/
63 static int      InitThread              (ac3dec_thread_t * p_adec);
64 static void     RunThread               (ac3dec_thread_t * p_adec);
65 static void     ErrorThread             (ac3dec_thread_t * p_adec);
66 static void     EndThread               (ac3dec_thread_t * p_adec);
67 static void     BitstreamCallback       ( bit_stream_t *p_bit_stream,
68                                               boolean_t b_new_pes );
69
70
71 /*****************************************************************************
72  * ac3dec_CreateThread: creates an ac3 decoder thread
73  *****************************************************************************/
74 vlc_thread_t ac3dec_CreateThread( adec_config_t * p_config )
75 {
76     ac3dec_thread_t *   p_ac3dec_t;
77
78     intf_DbgMsg( "ac3dec debug: creating ac3 decoder thread" );
79
80     /* Allocate the memory needed to store the thread's structure */
81     if((p_ac3dec_t = (ac3dec_thread_t *)malloc(sizeof(ac3dec_thread_t)))==NULL)
82     {
83         intf_ErrMsg ( "ac3dec error: not enough memory "
84                       "for ac3dec_CreateThread() to create the new thread");
85         return 0;
86     }
87     
88     /*
89      * Initialize the thread properties
90      */
91     p_ac3dec_t->p_config = p_config;
92     p_ac3dec_t->p_fifo = p_config->decoder_config.p_decoder_fifo;
93
94     /* Initialize the ac3 decoder structures */
95     ac3_init (&p_ac3dec_t->ac3_decoder);
96
97     /*
98      * Initialize the output properties
99      */
100     p_ac3dec_t->p_aout_fifo = NULL;
101
102     /* Spawn the ac3 decoder thread */
103     if (vlc_thread_create(&p_ac3dec_t->thread_id, "ac3 decoder", 
104                 (vlc_thread_func_t)RunThread, (void *)p_ac3dec_t))
105     {
106         intf_ErrMsg( "ac3dec error: can't spawn ac3 decoder thread" );
107         free (p_ac3dec_t);
108         return 0;
109     }
110
111     intf_DbgMsg ("ac3dec debug: ac3 decoder thread (%p) created", p_ac3dec_t);
112     return p_ac3dec_t->thread_id;
113 }
114
115 /* Following functions are local */
116
117 /*****************************************************************************
118  * InitThread : initialize an ac3 decoder thread
119  *****************************************************************************/
120 static int InitThread (ac3dec_thread_t * p_ac3dec_t)
121 {
122     intf_DbgMsg("ac3dec debug: initializing ac3 decoder thread %p",p_ac3dec_t);
123
124     p_ac3dec_t->p_config->decoder_config.pf_init_bit_stream(
125             &p_ac3dec_t->ac3_decoder.bit_stream,
126             p_ac3dec_t->p_config->decoder_config.p_decoder_fifo,
127             BitstreamCallback, (void *) p_ac3dec_t );
128
129     /* Creating the audio output fifo */
130     p_ac3dec_t->p_aout_fifo = aout_CreateFifo( AOUT_ADEC_STEREO_FIFO, 2, 0, 0,
131                                                AC3DEC_FRAME_SIZE, NULL  );
132     if ( p_ac3dec_t->p_aout_fifo == NULL )
133     {
134         return -1;
135     }
136
137     intf_DbgMsg("ac3dec debug: ac3 decoder thread %p initialized", p_ac3dec_t);
138     return 0;
139 }
140
141 /*****************************************************************************
142  * RunThread : ac3 decoder thread
143  *****************************************************************************/
144 static void RunThread (ac3dec_thread_t * p_ac3dec_t)
145 {
146     int sync;
147
148     intf_DbgMsg ("ac3dec debug: running ac3 decoder thread (%p) (pid == %i)", p_ac3dec_t, getpid());
149
150     /* Initializing the ac3 decoder thread */
151     if (InitThread (p_ac3dec_t)) /* XXX?? */
152     {
153         p_ac3dec_t->p_fifo->b_error = 1;
154     }
155
156     sync = 0;
157     p_ac3dec_t->sync_ptr = 0;
158
159     /* ac3 decoder thread's main loop */
160     /* FIXME : do we have enough room to store the decoded frames ?? */
161     while ((!p_ac3dec_t->p_fifo->b_die) && (!p_ac3dec_t->p_fifo->b_error))
162     {
163         s16 * buffer;
164         ac3_sync_info_t sync_info;
165         int ptr;
166
167         if (!sync) {
168             do {
169                 GetBits(&p_ac3dec_t->ac3_decoder.bit_stream,8);
170             } while ((!p_ac3dec_t->sync_ptr) && (!p_ac3dec_t->p_fifo->b_die)
171                     && (!p_ac3dec_t->p_fifo->b_error));
172             
173             ptr = p_ac3dec_t->sync_ptr;
174
175             while(ptr-- && (!p_ac3dec_t->p_fifo->b_die)
176                 && (!p_ac3dec_t->p_fifo->b_error))
177             {
178                 p_ac3dec_t->ac3_decoder.bit_stream.p_byte++;
179             }
180                         
181             /* we are in sync now */
182             sync = 1;
183         }
184
185         if (DECODER_FIFO_START(*p_ac3dec_t->p_fifo)->i_pts)
186         {
187             p_ac3dec_t->p_aout_fifo->date[p_ac3dec_t->p_aout_fifo->l_end_frame] =
188                 DECODER_FIFO_START(*p_ac3dec_t->p_fifo)->i_pts;
189             DECODER_FIFO_START(*p_ac3dec_t->p_fifo)->i_pts = 0;
190         } else {
191             p_ac3dec_t->p_aout_fifo->date[p_ac3dec_t->p_aout_fifo->l_end_frame] =
192                 LAST_MDATE;
193         }
194     
195         if (ac3_sync_frame (&p_ac3dec_t->ac3_decoder, &sync_info))
196         {
197             sync = 0;
198             goto bad_frame;
199         }
200
201         p_ac3dec_t->p_aout_fifo->l_rate = sync_info.sample_rate;
202
203         buffer = ((s16 *)p_ac3dec_t->p_aout_fifo->buffer) + 
204             (p_ac3dec_t->p_aout_fifo->l_end_frame * AC3DEC_FRAME_SIZE);
205
206         if (ac3_decode_frame (&p_ac3dec_t->ac3_decoder, buffer))
207         {
208             sync = 0;
209             goto bad_frame;
210         }
211         
212         vlc_mutex_lock (&p_ac3dec_t->p_aout_fifo->data_lock);
213         p_ac3dec_t->p_aout_fifo->l_end_frame = 
214             (p_ac3dec_t->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
215         vlc_cond_signal (&p_ac3dec_t->p_aout_fifo->data_wait);
216         vlc_mutex_unlock (&p_ac3dec_t->p_aout_fifo->data_lock);
217
218         bad_frame:
219             RealignBits(&p_ac3dec_t->ac3_decoder.bit_stream);
220     }
221
222     /* If b_error is set, the ac3 decoder thread enters the error loop */
223     if (p_ac3dec_t->p_fifo->b_error)
224     {
225         ErrorThread (p_ac3dec_t);
226     }
227
228     /* End of the ac3 decoder thread */
229     EndThread (p_ac3dec_t);
230 }
231
232 /*****************************************************************************
233  * ErrorThread : ac3 decoder's RunThread() error loop
234  *****************************************************************************/
235 static void ErrorThread (ac3dec_thread_t * p_ac3dec_t)
236 {
237     /* We take the lock, because we are going to read/write the start/end
238      * indexes of the decoder fifo */
239     vlc_mutex_lock (&p_ac3dec_t->p_fifo->data_lock);
240
241     /* Wait until a `die' order is sent */
242     while (!p_ac3dec_t->p_fifo->b_die)
243     {
244         /* Trash all received PES packets */
245         while (!DECODER_FIFO_ISEMPTY(*p_ac3dec_t->p_fifo))
246         {
247             p_ac3dec_t->p_fifo->pf_delete_pes(p_ac3dec_t->p_fifo->p_packets_mgt,
248                     DECODER_FIFO_START(*p_ac3dec_t->p_fifo));
249             DECODER_FIFO_INCSTART (*p_ac3dec_t->p_fifo);
250         }
251
252         /* Waiting for the input thread to put new PES packets in the fifo */
253         vlc_cond_wait (&p_ac3dec_t->p_fifo->data_wait,
254                        &p_ac3dec_t->p_fifo->data_lock);
255     }
256
257     /* We can release the lock before leaving */
258     vlc_mutex_unlock (&p_ac3dec_t->p_fifo->data_lock);
259 }
260
261 /*****************************************************************************
262  * EndThread : ac3 decoder thread destruction
263  *****************************************************************************/
264 static void EndThread (ac3dec_thread_t * p_ac3dec_t)
265 {
266     intf_DbgMsg ("ac3dec debug: destroying ac3 decoder thread %p", p_ac3dec_t);
267
268     /* If the audio output fifo was created, we destroy it */
269     if (p_ac3dec_t->p_aout_fifo != NULL)
270     {
271         aout_DestroyFifo (p_ac3dec_t->p_aout_fifo);
272
273         /* Make sure the output thread leaves the NextFrame() function */
274         vlc_mutex_lock (&(p_ac3dec_t->p_aout_fifo->data_lock));
275         vlc_cond_signal (&(p_ac3dec_t->p_aout_fifo->data_wait));
276         vlc_mutex_unlock (&(p_ac3dec_t->p_aout_fifo->data_lock));
277         
278     }
279
280     /* Destroy descriptor */
281     free( p_ac3dec_t->p_config );
282     free( p_ac3dec_t );
283
284     intf_DbgMsg ("ac3dec debug: ac3 decoder thread %p destroyed", p_ac3dec_t);
285 }
286
287 /*****************************************************************************
288 * BitstreamCallback: Import parameters from the new data/PES packet
289 *****************************************************************************
290 * This function is called by input's NextDataPacket.
291 *****************************************************************************/
292 static void BitstreamCallback ( bit_stream_t * p_bit_stream,
293                                         boolean_t b_new_pes)
294 {
295
296     ac3dec_thread_t *p_ac3dec_t=(ac3dec_thread_t *)p_bit_stream->p_callback_arg;
297
298     if( b_new_pes )
299     {
300         int ptr;
301         
302         ptr = *(p_bit_stream->p_byte + 1);
303         ptr <<= 8;
304         ptr |= *(p_bit_stream->p_byte + 2);
305         p_ac3dec_t->sync_ptr = ptr;
306         p_bit_stream->p_byte += 3;                                                            
307     }
308 }