]> git.sesse.net Git - vlc/blob - src/ac3_decoder/ac3_decoder_thread.c
Beginning of Interface II
[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.24 2001/02/11 01:15:10 sam Exp $
6  *
7  * Authors:
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
68 /*****************************************************************************
69  * ac3dec_CreateThread: creates an ac3 decoder thread
70  *****************************************************************************/
71 vlc_thread_t ac3dec_CreateThread( adec_config_t * p_config )
72 {
73     ac3dec_thread_t *   p_ac3dec;
74
75     intf_DbgMsg( "ac3dec debug: creating ac3 decoder thread" );
76
77     /* Allocate the memory needed to store the thread's structure */
78     if ((p_ac3dec = (ac3dec_thread_t *)malloc (sizeof(ac3dec_thread_t))) == NULL)
79     {
80         intf_ErrMsg ( "ac3dec error: not enough memory "
81                       "for ac3dec_CreateThread() to create the new thread");
82         return 0;
83     }
84
85     /*
86      * Initialize the thread properties
87      */
88     p_ac3dec->p_config = p_config;
89     p_ac3dec->p_fifo = p_config->decoder_config.p_decoder_fifo;
90
91     /* Initialize the ac3 decoder structures */
92     ac3_init (&p_ac3dec->ac3_decoder);
93
94     /*
95      * Initialize the output properties
96      */
97     p_ac3dec->p_aout = p_config->p_aout;
98     p_ac3dec->p_aout_fifo = NULL;
99
100     /* Spawn the ac3 decoder thread */
101     if (vlc_thread_create(&p_ac3dec->thread_id, "ac3 decoder", (vlc_thread_func_t)RunThread, (void *)p_ac3dec))
102     {
103         intf_ErrMsg( "ac3dec error: can't spawn ac3 decoder thread" );
104         free (p_ac3dec);
105         return 0;
106     }
107
108     intf_DbgMsg ("ac3dec debug: ac3 decoder thread (%p) created", p_ac3dec);
109     return p_ac3dec->thread_id;
110 }
111
112 /* Following functions are local */
113
114 /*****************************************************************************
115  * InitThread : initialize an ac3 decoder thread
116  *****************************************************************************/
117 static int InitThread (ac3dec_thread_t * p_ac3dec)
118 {
119     aout_fifo_t         aout_fifo;
120     ac3_byte_stream_t * byte_stream;
121
122     intf_DbgMsg ("ac3dec debug: initializing ac3 decoder thread %p", p_ac3dec);
123
124     /* Get the first data packet. */
125     vlc_mutex_lock( &p_ac3dec->p_fifo->data_lock );
126     while ( DECODER_FIFO_ISEMPTY( *p_ac3dec->p_fifo ) )
127     {
128         if ( p_ac3dec->p_fifo->b_die )
129         {
130             vlc_mutex_unlock( &p_ac3dec->p_fifo->data_lock );
131             return -1;
132         }
133         vlc_cond_wait( &p_ac3dec->p_fifo->data_wait, &p_ac3dec->p_fifo->data_lock );
134     }
135     p_ac3dec->p_data = DECODER_FIFO_START(*p_ac3dec->p_fifo)->p_first;
136     byte_stream = ac3_byte_stream (&p_ac3dec->ac3_decoder);
137     byte_stream->p_byte = p_ac3dec->p_data->p_payload_start;
138     byte_stream->p_end = p_ac3dec->p_data->p_payload_end;
139     byte_stream->info = p_ac3dec;
140     vlc_mutex_unlock (&p_ac3dec->p_fifo->data_lock);
141
142     aout_fifo.i_type = AOUT_ADEC_STEREO_FIFO;
143     aout_fifo.i_channels = 2;
144     aout_fifo.b_stereo = 1;
145
146     aout_fifo.l_frame_size = AC3DEC_FRAME_SIZE;
147
148     /* Creating the audio output fifo */
149     if ((p_ac3dec->p_aout_fifo = aout_CreateFifo(p_ac3dec->p_aout, &aout_fifo)) == NULL)
150     {
151         return -1;
152     }
153
154     intf_DbgMsg ("ac3dec debug: ac3 decoder thread %p initialized", p_ac3dec);
155     return 0;
156 }
157
158 /*****************************************************************************
159  * RunThread : ac3 decoder thread
160  *****************************************************************************/
161 static void RunThread (ac3dec_thread_t * p_ac3dec)
162 {
163     int sync;
164
165     intf_DbgMsg ("ac3dec debug: running ac3 decoder thread (%p) (pid == %i)", p_ac3dec, getpid());
166
167     /* FIXME ! Qu'est-ce que c'est que ce bordel !?!?!?!? --Meuuh */
168     //msleep (INPUT_PTS_DELAY);
169
170     /* Initializing the ac3 decoder thread */
171     if (InitThread (p_ac3dec)) /* XXX?? */
172     {
173         p_ac3dec->p_fifo->b_error = 1;
174     }
175
176     sync = 0;
177     p_ac3dec->sync_ptr = 0;
178
179     /* ac3 decoder thread's main loop */
180     /* FIXME : do we have enough room to store the decoded frames ?? */
181     while ((!p_ac3dec->p_fifo->b_die) && (!p_ac3dec->p_fifo->b_error))
182     {
183         s16 * buffer;
184         ac3_sync_info_t sync_info;
185
186         if (!sync) { /* have to find a synchro point */
187             int ptr;
188             ac3_byte_stream_t * p_byte_stream;
189
190             intf_DbgMsg ("ac3dec: sync");
191
192             p_byte_stream = ac3_byte_stream (&p_ac3dec->ac3_decoder);
193
194             /* first read till next ac3 magic header */
195             do
196             {
197                 ac3_byte_stream_next (p_byte_stream);
198             } while ((!p_ac3dec->sync_ptr) &&
199                     (!p_ac3dec->p_fifo->b_die) &&
200                     (!p_ac3dec->p_fifo->b_error));
201             /* skip the specified number of bytes */
202
203             if( p_ac3dec->p_fifo->b_die || p_ac3dec->p_fifo->b_error )
204             {
205                 goto bad_frame;
206             }
207
208             ptr = p_ac3dec->sync_ptr;
209             while (--ptr && (!p_ac3dec->p_fifo->b_die) && (!p_ac3dec->p_fifo->b_error))
210             {
211                 if (p_byte_stream->p_byte >= p_byte_stream->p_end)
212                 {
213                     ac3_byte_stream_next (p_byte_stream);                    
214                 }
215                 p_byte_stream->p_byte++;
216             }
217
218             if( p_ac3dec->p_fifo->b_die || p_ac3dec->p_fifo->b_error )
219             {
220                 goto bad_frame;
221             }
222
223             /* we are in sync now */
224             sync = 1;
225             p_ac3dec->sync_ptr = 0;
226         }
227
228         if (DECODER_FIFO_START(*p_ac3dec->p_fifo)->i_pts)
229         {
230             p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = DECODER_FIFO_START(*p_ac3dec->p_fifo)->i_pts;
231             DECODER_FIFO_START(*p_ac3dec->p_fifo)->i_pts = 0;
232         }
233         else
234         {
235             p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = LAST_MDATE;
236         }
237
238         if (ac3_sync_frame (&p_ac3dec->ac3_decoder, &sync_info))
239         {
240             sync = 0;
241             goto bad_frame;
242         }
243
244         p_ac3dec->p_aout_fifo->l_rate = sync_info.sample_rate;
245
246         buffer = ((s16 *)p_ac3dec->p_aout_fifo->buffer) + (p_ac3dec->p_aout_fifo->l_end_frame * AC3DEC_FRAME_SIZE);
247
248         if (ac3_decode_frame (&p_ac3dec->ac3_decoder, buffer))
249         {
250             sync = 0;
251             goto bad_frame;
252         }
253
254         vlc_mutex_lock (&p_ac3dec->p_aout_fifo->data_lock);
255         p_ac3dec->p_aout_fifo->l_end_frame = (p_ac3dec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
256         vlc_cond_signal (&p_ac3dec->p_aout_fifo->data_wait);
257         vlc_mutex_unlock (&p_ac3dec->p_aout_fifo->data_lock);
258
259     bad_frame:
260     }
261
262     /* If b_error is set, the ac3 decoder thread enters the error loop */
263     if (p_ac3dec->p_fifo->b_error)
264     {
265         ErrorThread (p_ac3dec);
266     }
267
268     /* End of the ac3 decoder thread */
269     EndThread (p_ac3dec);
270 }
271
272 /*****************************************************************************
273  * ErrorThread : ac3 decoder's RunThread() error loop
274  *****************************************************************************/
275 static void ErrorThread (ac3dec_thread_t * p_ac3dec)
276 {
277     /* We take the lock, because we are going to read/write the start/end
278      * indexes of the decoder fifo */
279     vlc_mutex_lock (&p_ac3dec->p_fifo->data_lock);
280
281     /* Wait until a `die' order is sent */
282     while (!p_ac3dec->p_fifo->b_die)
283     {
284         /* Trash all received PES packets */
285         while (!DECODER_FIFO_ISEMPTY(*p_ac3dec->p_fifo))
286         {
287             p_ac3dec->p_fifo->pf_delete_pes(p_ac3dec->p_fifo->p_packets_mgt,
288                     DECODER_FIFO_START(*p_ac3dec->p_fifo));
289             DECODER_FIFO_INCSTART (*p_ac3dec->p_fifo);
290         }
291
292         /* Waiting for the input thread to put new PES packets in the fifo */
293         vlc_cond_wait (&p_ac3dec->p_fifo->data_wait,
294                        &p_ac3dec->p_fifo->data_lock);
295     }
296
297     /* We can release the lock before leaving */
298     vlc_mutex_unlock (&p_ac3dec->p_fifo->data_lock);
299 }
300
301 /*****************************************************************************
302  * EndThread : ac3 decoder thread destruction
303  *****************************************************************************/
304 static void EndThread (ac3dec_thread_t * p_ac3dec)
305 {
306     intf_DbgMsg ("ac3dec debug: destroying ac3 decoder thread %p", p_ac3dec);
307
308     /* If the audio output fifo was created, we destroy it */
309     if (p_ac3dec->p_aout_fifo != NULL)
310     {
311         aout_DestroyFifo (p_ac3dec->p_aout_fifo);
312
313         /* Make sure the output thread leaves the NextFrame() function */
314         vlc_mutex_lock (&(p_ac3dec->p_aout_fifo->data_lock));
315         vlc_cond_signal (&(p_ac3dec->p_aout_fifo->data_wait));
316         vlc_mutex_unlock (&(p_ac3dec->p_aout_fifo->data_lock));
317     }
318
319     /* Destroy descriptor */
320     free( p_ac3dec->p_config );
321     free( p_ac3dec );
322
323     intf_DbgMsg ("ac3dec debug: ac3 decoder thread %p destroyed", p_ac3dec);
324 }
325
326 void ac3_byte_stream_next (ac3_byte_stream_t * p_byte_stream)
327 {
328     ac3dec_thread_t * p_ac3dec = p_byte_stream->info;
329
330     /* We are looking for the next TS packet that contains real data,
331      * and not just a PES header */
332     do
333     {
334         /* We were reading the last TS packet of this PES packet... It's
335          * time to jump to the next PES packet */
336         if (p_ac3dec->p_data->p_next == NULL)
337         {
338             int ptr;
339
340             /* We are going to read/write the start and end indexes of the 
341              * decoder fifo and to use the fifo's conditional variable, 
342              * that's why we need to take the lock before */ 
343             vlc_mutex_lock (&p_ac3dec->p_fifo->data_lock);
344
345             /* Is the input thread dying ? */
346             if (p_ac3dec->p_fifo->b_die)
347             {
348                 vlc_mutex_unlock (&p_ac3dec->p_fifo->data_lock);
349                 return;
350             }
351
352             /* We should increase the start index of the decoder fifo, but
353              * if we do this now, the input thread could overwrite the
354              * pointer to the current PES packet, and we weren't able to
355              * give it back to the netlist. That's why we free the PES
356              * packet first. */
357             p_ac3dec->p_fifo->pf_delete_pes(p_ac3dec->p_fifo->p_packets_mgt,
358                     DECODER_FIFO_START(*p_ac3dec->p_fifo));
359
360             DECODER_FIFO_INCSTART (*p_ac3dec->p_fifo);
361
362             while (DECODER_FIFO_ISEMPTY(*p_ac3dec->p_fifo))
363             {
364                 vlc_cond_wait(&p_ac3dec->p_fifo->data_wait, &p_ac3dec->p_fifo->data_lock);
365
366                 if (p_ac3dec->p_fifo->b_die)
367                 {
368                     vlc_mutex_unlock (&p_ac3dec->p_fifo->data_lock);
369                     return;
370                 }
371             }
372
373             /* The next byte could be found in the next PES packet */
374             p_ac3dec->p_data = DECODER_FIFO_START(*p_ac3dec->p_fifo)->p_first;
375
376             /* parse ac3 magic header */
377             ptr = *(p_ac3dec->p_data->p_payload_start + 1);
378             ptr <<= 8;
379             ptr |= *(p_ac3dec->p_data->p_payload_start + 2);
380             p_ac3dec->sync_ptr = ptr;
381             p_ac3dec->p_data->p_payload_start += 3;
382
383             /* We can release the fifo's data lock */
384             vlc_mutex_unlock (&p_ac3dec->p_fifo->data_lock);
385         }
386         /* Perhaps the next TS packet of the current PES packet contains 
387          * real data (ie its payload's size is greater than 0) */
388         else
389         {
390             p_ac3dec->p_data = p_ac3dec->p_data->p_next;
391         }
392     } while (p_ac3dec->p_data->p_payload_start == p_ac3dec->p_data->p_payload_end);
393     p_byte_stream->p_byte = p_ac3dec->p_data->p_payload_start; 
394     p_byte_stream->p_end = p_ac3dec->p_data->p_payload_end; 
395 }