]> git.sesse.net Git - vlc/blob - src/ac3_decoder/ac3_decoder_thread.c
* Begin of the new ac3_decoder ;)
[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.28 2001/04/20 12:14:34 reno 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 = p_config->p_aout;
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     aout_fifo_t         aout_fifo;
124
125     intf_DbgMsg("ac3dec debug: initializing ac3 decoder thread %p",p_ac3dec_t);
126
127     p_ac3dec_t->p_config->decoder_config.pf_init_bit_stream(
128             &p_ac3dec_t->ac3_decoder.bit_stream,
129             p_ac3dec_t->p_config->decoder_config.p_decoder_fifo );
130     p_ac3dec_t->ac3_decoder.bit_stream.pf_bitstream_callback=BitstreamCallback;
131     p_ac3dec_t->ac3_decoder.bit_stream.p_callback_arg = (void *) p_ac3dec_t;
132
133     
134     aout_fifo.i_type = AOUT_ADEC_STEREO_FIFO;
135     aout_fifo.i_channels = 2;
136     aout_fifo.b_stereo = 1;
137
138     aout_fifo.l_frame_size = AC3DEC_FRAME_SIZE;
139
140     /* Creating the audio output fifo */
141     if ((p_ac3dec_t->p_aout_fifo = aout_CreateFifo(p_ac3dec_t->p_aout, &aout_fifo)) == NULL)
142     {
143         return -1;
144     }
145
146     /* InitBitstream has normally begun to read a PES packet, get its
147      * PTS/DTS */
148      if( !p_ac3dec_t->p_fifo->b_die )
149      {
150         BitstreamCallback( &p_ac3dec_t->ac3_decoder.bit_stream, 1 );
151      }
152
153     intf_DbgMsg("ac3dec debug: ac3 decoder thread %p initialized", p_ac3dec_t);
154     return 0;
155 }
156
157 /*****************************************************************************
158  * RunThread : ac3 decoder thread
159  *****************************************************************************/
160 static void RunThread (ac3dec_thread_t * p_ac3dec_t)
161 {
162     int sync;
163
164     intf_DbgMsg ("ac3dec debug: running ac3 decoder thread (%p) (pid == %i)", p_ac3dec_t, getpid());
165
166     /* Initializing the ac3 decoder thread */
167     if (InitThread (p_ac3dec_t)) /* XXX?? */
168     {
169         p_ac3dec_t->p_fifo->b_error = 1;
170     }
171
172     sync = 0;
173     p_ac3dec_t->sync_ptr = 0;
174
175     /* ac3 decoder thread's main loop */
176     /* FIXME : do we have enough room to store the decoded frames ?? */
177     while ((!p_ac3dec_t->p_fifo->b_die) && (!p_ac3dec_t->p_fifo->b_error))
178     {
179         s16 * buffer;
180         ac3_sync_info_t sync_info;
181                         
182         if (!sync) {
183             do {
184                 GetBits(&p_ac3dec_t->ac3_decoder.bit_stream,8);
185             } while ((!p_ac3dec_t->sync_ptr) && (!p_ac3dec_t->p_fifo->b_die)
186                     && (!p_ac3dec_t->p_fifo->b_error));
187
188             /* we are in sync now */
189             sync = 1;
190         }
191
192         if (DECODER_FIFO_START(*p_ac3dec_t->p_fifo)->i_pts)
193         {
194             p_ac3dec_t->p_aout_fifo->date[p_ac3dec_t->p_aout_fifo->l_end_frame] =
195                 DECODER_FIFO_START(*p_ac3dec_t->p_fifo)->i_pts;
196             DECODER_FIFO_START(*p_ac3dec_t->p_fifo)->i_pts = 0;
197         } else {
198             p_ac3dec_t->p_aout_fifo->date[p_ac3dec_t->p_aout_fifo->l_end_frame] =
199                 LAST_MDATE;
200         }
201     
202         if (ac3_sync_frame (&p_ac3dec_t->ac3_decoder, &sync_info))
203         {
204             sync = 0;
205             goto bad_frame;
206         }
207
208         p_ac3dec_t->p_aout_fifo->l_rate = sync_info.sample_rate;
209
210         buffer = ((s16 *)p_ac3dec_t->p_aout_fifo->buffer) + 
211             (p_ac3dec_t->p_aout_fifo->l_end_frame * AC3DEC_FRAME_SIZE);
212
213         if (ac3_decode_frame (&p_ac3dec_t->ac3_decoder, buffer))
214         {
215             sync = 0;
216             goto bad_frame;
217         }
218         
219         vlc_mutex_lock (&p_ac3dec_t->p_aout_fifo->data_lock);
220         p_ac3dec_t->p_aout_fifo->l_end_frame = 
221             (p_ac3dec_t->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
222         vlc_cond_signal (&p_ac3dec_t->p_aout_fifo->data_wait);
223         vlc_mutex_unlock (&p_ac3dec_t->p_aout_fifo->data_lock);
224
225         bad_frame:
226             RealignBits(&p_ac3dec_t->ac3_decoder.bit_stream);
227     }
228
229     /* If b_error is set, the ac3 decoder thread enters the error loop */
230     if (p_ac3dec_t->p_fifo->b_error)
231     {
232         ErrorThread (p_ac3dec_t);
233     }
234
235     /* End of the ac3 decoder thread */
236     EndThread (p_ac3dec_t);
237 }
238
239 /*****************************************************************************
240  * ErrorThread : ac3 decoder's RunThread() error loop
241  *****************************************************************************/
242 static void ErrorThread (ac3dec_thread_t * p_ac3dec_t)
243 {
244     /* We take the lock, because we are going to read/write the start/end
245      * indexes of the decoder fifo */
246     vlc_mutex_lock (&p_ac3dec_t->p_fifo->data_lock);
247
248     /* Wait until a `die' order is sent */
249     while (!p_ac3dec_t->p_fifo->b_die)
250     {
251         /* Trash all received PES packets */
252         while (!DECODER_FIFO_ISEMPTY(*p_ac3dec_t->p_fifo))
253         {
254             p_ac3dec_t->p_fifo->pf_delete_pes(p_ac3dec_t->p_fifo->p_packets_mgt,
255                     DECODER_FIFO_START(*p_ac3dec_t->p_fifo));
256             DECODER_FIFO_INCSTART (*p_ac3dec_t->p_fifo);
257         }
258
259         /* Waiting for the input thread to put new PES packets in the fifo */
260         vlc_cond_wait (&p_ac3dec_t->p_fifo->data_wait,
261                        &p_ac3dec_t->p_fifo->data_lock);
262     }
263
264     /* We can release the lock before leaving */
265     vlc_mutex_unlock (&p_ac3dec_t->p_fifo->data_lock);
266 }
267
268 /*****************************************************************************
269  * EndThread : ac3 decoder thread destruction
270  *****************************************************************************/
271 static void EndThread (ac3dec_thread_t * p_ac3dec_t)
272 {
273     intf_DbgMsg ("ac3dec debug: destroying ac3 decoder thread %p", p_ac3dec_t);
274
275     /* If the audio output fifo was created, we destroy it */
276     if (p_ac3dec_t->p_aout_fifo != NULL)
277     {
278         aout_DestroyFifo (p_ac3dec_t->p_aout_fifo);
279
280         /* Make sure the output thread leaves the NextFrame() function */
281         vlc_mutex_lock (&(p_ac3dec_t->p_aout_fifo->data_lock));
282         vlc_cond_signal (&(p_ac3dec_t->p_aout_fifo->data_wait));
283         vlc_mutex_unlock (&(p_ac3dec_t->p_aout_fifo->data_lock));
284         
285     }
286
287     /* Destroy descriptor */
288     free( p_ac3dec_t->p_config );
289     free( p_ac3dec_t );
290
291     intf_DbgMsg ("ac3dec debug: ac3 decoder thread %p destroyed", p_ac3dec_t);
292 }
293
294 /*****************************************************************************
295 * BitstreamCallback: Import parameters from the new data/PES packet
296 *****************************************************************************
297 * This function is called by input's NextDataPacket.
298 *****************************************************************************/
299 static void BitstreamCallback ( bit_stream_t * p_bit_stream,
300                                         boolean_t b_new_pes)
301 {
302
303     ac3dec_thread_t *p_ac3dec_t=(ac3dec_thread_t *)p_bit_stream->p_callback_arg;
304
305     if( b_new_pes )
306     {
307         int ptr;
308         
309         ptr = *(p_bit_stream->p_byte + 1);
310         ptr <<= 8;
311         ptr |= *(p_bit_stream->p_byte + 2);
312         p_ac3dec_t->sync_ptr = ptr;
313         p_bit_stream->p_byte += 3;                                                            
314     }
315 }