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