]> git.sesse.net Git - vlc/blob - src/ac3_decoder/ac3_decoder_thread.c
D�but du portage BeOS. Beaucoup de fuchiers ont �t� modifi� car il a fallu
[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  *
6  * Authors:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public
19  * License along with this program; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, 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 <unistd.h>                                              /* getpid() */
37
38 #include <stdio.h>                                           /* "intf_msg.h" */
39 #include <stdlib.h>                                      /* malloc(), free() */
40 #include <sys/types.h>                        /* on BSD, uio.h needs types.h */
41 #include <sys/uio.h>                                            /* "input.h" */
42
43 #include "threads.h"
44 #include "common.h"
45 #include "config.h"
46 #include "mtime.h"
47 #include "plugins.h"
48 #include "debug.h"                                      /* "input_netlist.h" */
49
50 #include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */
51
52 #include "input.h"                                           /* pes_packet_t */
53 #include "input_netlist.h"                         /* input_NetlistFreePES() */
54 #include "decoder_fifo.h"         /* DECODER_FIFO_(ISEMPTY|START|INCSTART)() */
55
56 #include "audio_output.h"
57
58 #include "ac3_decoder.h"
59 #include "ac3_decoder_thread.h"
60
61 #define AC3DEC_FRAME_SIZE (2*1536) 
62 typedef s16 ac3dec_frame_t[ AC3DEC_FRAME_SIZE ];
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 ac3dec_thread_t * ac3dec_CreateThread( input_thread_t * p_input )
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 for ac3dec_CreateThread() to create the new thread\n");
85         return( NULL );
86     }
87
88     /*
89      * Initialize the thread properties
90      */
91     p_ac3dec->b_die = 0;
92     p_ac3dec->b_error = 0;
93
94     /*
95      * Initialize the input properties
96      */
97     /* Initialize the decoder fifo's data lock and conditional variable and set
98      * its buffer as empty */
99     vlc_mutex_init( &p_ac3dec->fifo.data_lock );
100     vlc_cond_init( &p_ac3dec->fifo.data_wait );
101     p_ac3dec->fifo.i_start = 0;
102     p_ac3dec->fifo.i_end = 0;
103
104     /* Initialize the ac3 decoder structures */
105     ac3_init (&p_ac3dec->ac3_decoder);
106
107     /* Initialize the bit stream structure */
108     p_ac3dec->p_input = p_input;
109
110     /*
111      * Initialize the output properties
112      */
113     p_ac3dec->p_aout = p_input->p_aout;
114     p_ac3dec->p_aout_fifo = NULL;
115
116     /* Spawn the ac3 decoder thread */
117     if ( vlc_thread_create(&p_ac3dec->thread_id, "ac3 decoder", (vlc_thread_func_t)RunThread, (void *)p_ac3dec) )
118     {
119         intf_ErrMsg( "ac3dec error: can't spawn ac3 decoder thread\n" );
120         free( p_ac3dec );
121         return( NULL );
122     }
123
124     intf_DbgMsg( "ac3dec debug: ac3 decoder thread (%p) created\n", p_ac3dec );
125     return( p_ac3dec );
126 }
127
128 /*****************************************************************************
129  * ac3dec_DestroyThread: destroys an ac3 decoder thread
130  *****************************************************************************/
131 void ac3dec_DestroyThread( ac3dec_thread_t * p_ac3dec )
132 {
133     intf_DbgMsg( "ac3dec debug: requesting termination of ac3 decoder thread %p\n", p_ac3dec );
134
135     /* Ask thread to kill itself */
136     p_ac3dec->b_die = 1;
137
138     /* Make sure the decoder thread leaves the GetByte() function */
139     vlc_mutex_lock( &(p_ac3dec->fifo.data_lock) );
140     vlc_cond_signal( &(p_ac3dec->fifo.data_wait) );
141     vlc_mutex_unlock( &(p_ac3dec->fifo.data_lock) );
142
143     /* Waiting for the decoder thread to exit */
144     /* Remove this as soon as the "status" flag is implemented */
145     vlc_thread_join( p_ac3dec->thread_id );
146 }
147
148 /* Following functions are local */
149
150 /*****************************************************************************
151  * InitThread : initialize an ac3 decoder thread
152  *****************************************************************************/
153 static int InitThread( ac3dec_thread_t * p_ac3dec )
154 {
155     aout_fifo_t         aout_fifo;
156     ac3_byte_stream_t * byte_stream;
157
158     intf_DbgMsg( "ac3dec debug: initializing ac3 decoder thread %p\n", p_ac3dec );
159
160     /* Our first job is to initialize the bit stream structure with the
161      * beginning of the input stream */
162     vlc_mutex_lock( &p_ac3dec->fifo.data_lock );
163     while ( DECODER_FIFO_ISEMPTY(p_ac3dec->fifo) )
164     {
165         if ( p_ac3dec->b_die )
166         {
167             vlc_mutex_unlock( &p_ac3dec->fifo.data_lock );
168             return( -1 );
169         }
170         vlc_cond_wait( &p_ac3dec->fifo.data_wait, &p_ac3dec->fifo.data_lock );
171     }
172     p_ac3dec->p_ts = DECODER_FIFO_START( p_ac3dec->fifo )->p_first_ts;
173     byte_stream = ac3_byte_stream (&p_ac3dec->ac3_decoder);
174     byte_stream->p_byte =
175         p_ac3dec->p_ts->buffer + p_ac3dec->p_ts->i_payload_start;
176     byte_stream->p_end =
177         p_ac3dec->p_ts->buffer + p_ac3dec->p_ts->i_payload_end;
178     byte_stream->info = p_ac3dec;
179     vlc_mutex_unlock( &p_ac3dec->fifo.data_lock );
180
181     aout_fifo.i_type = AOUT_ADEC_STEREO_FIFO;
182     aout_fifo.i_channels = 2;
183     aout_fifo.b_stereo = 1;
184
185     aout_fifo.l_frame_size = AC3DEC_FRAME_SIZE;
186
187     /* Creating the audio output fifo */
188     if ( (p_ac3dec->p_aout_fifo = aout_CreateFifo(p_ac3dec->p_aout, &aout_fifo)) == NULL )
189     {
190         return( -1 );
191     }
192
193     intf_DbgMsg( "ac3dec debug: ac3 decoder thread %p initialized\n", p_ac3dec );
194     return( 0 );
195 }
196
197 /*****************************************************************************
198  * RunThread : ac3 decoder thread
199  *****************************************************************************/
200 static void RunThread( ac3dec_thread_t * p_ac3dec )
201 {
202     int sync;
203
204     intf_DbgMsg( "ac3dec debug: running ac3 decoder thread (%p) (pid == %i)\n", p_ac3dec, getpid() );
205
206     msleep( INPUT_PTS_DELAY );
207
208     /* Initializing the ac3 decoder thread */
209     if ( InitThread(p_ac3dec) ) /* XXX?? */
210     {
211         p_ac3dec->b_error = 1;
212     }
213
214     sync = 0;
215     p_ac3dec->sync_ptr = 0;
216
217     /* ac3 decoder thread's main loop */
218     /* FIXME : do we have enough room to store the decoded frames ?? */
219     while ( (!p_ac3dec->b_die) && (!p_ac3dec->b_error) )
220     {
221         s16 * buffer;
222         ac3_sync_info_t sync_info;
223
224         if (!sync) { /* have to find a synchro point */
225             int ptr;
226             ac3_byte_stream_t * p_byte_stream;
227
228             printf ("sync\n");
229
230             p_byte_stream = ac3_byte_stream (&p_ac3dec->ac3_decoder);
231
232             /* first read till next ac3 magic header */
233             do {
234                 ac3_byte_stream_next (p_byte_stream);
235             } while ((!p_ac3dec->sync_ptr) &&
236                      (!p_ac3dec->b_die) &&
237                      (!p_ac3dec->b_error));
238             /* skip the specified number of bytes */
239
240             ptr = p_ac3dec->sync_ptr;
241             while (--ptr && (!p_ac3dec->b_die) && (!p_ac3dec->b_error)) {
242                 if (p_byte_stream->p_byte >= p_byte_stream->p_end) {
243                     ac3_byte_stream_next (p_byte_stream);                   
244                 }
245                 p_byte_stream->p_byte++;
246             }
247
248             /* we are in sync now */
249
250             sync = 1;
251             p_ac3dec->sync_ptr = 0;
252         }
253
254         if ( DECODER_FIFO_START(p_ac3dec->fifo)->b_has_pts )
255         {
256                 p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = DECODER_FIFO_START(p_ac3dec->fifo)->i_pts;
257                 DECODER_FIFO_START(p_ac3dec->fifo)->b_has_pts = 0;
258         }
259         else
260         {
261                 p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = LAST_MDATE;
262         }
263
264         if (ac3_sync_frame (&p_ac3dec->ac3_decoder, &sync_info)) {
265             sync = 0;
266             goto bad_frame;
267         }
268
269         p_ac3dec->p_aout_fifo->l_rate = sync_info.sample_rate;
270
271         buffer = ((ac3dec_frame_t *)p_ac3dec->p_aout_fifo->buffer)[ p_ac3dec->p_aout_fifo->l_end_frame ];
272
273         if (ac3_decode_frame (&p_ac3dec->ac3_decoder, buffer)) {
274             sync = 0;
275             goto bad_frame;
276         }
277
278         vlc_mutex_lock( &p_ac3dec->p_aout_fifo->data_lock );
279         p_ac3dec->p_aout_fifo->l_end_frame = (p_ac3dec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
280         vlc_cond_signal( &p_ac3dec->p_aout_fifo->data_wait );
281         vlc_mutex_unlock( &p_ac3dec->p_aout_fifo->data_lock );
282
283 bad_frame:
284     }
285
286     /* If b_error is set, the ac3 decoder thread enters the error loop */
287     if ( p_ac3dec->b_error )
288     {
289         ErrorThread( p_ac3dec );
290     }
291
292     /* End of the ac3 decoder thread */
293     EndThread( p_ac3dec );
294 }
295
296 /*****************************************************************************
297  * ErrorThread : ac3 decoder's RunThread() error loop
298  *****************************************************************************/
299 static void ErrorThread( ac3dec_thread_t * p_ac3dec )
300 {
301     /* We take the lock, because we are going to read/write the start/end
302      * indexes of the decoder fifo */
303     vlc_mutex_lock( &p_ac3dec->fifo.data_lock );
304
305     /* Wait until a `die' order is sent */
306     while( !p_ac3dec->b_die )
307     {
308         /* Trash all received PES packets */
309         while( !DECODER_FIFO_ISEMPTY(p_ac3dec->fifo) )
310         {
311             input_NetlistFreePES( p_ac3dec->p_input, DECODER_FIFO_START(p_ac3dec->fifo) );
312             DECODER_FIFO_INCSTART( p_ac3dec->fifo );
313         }
314
315         /* Waiting for the input thread to put new PES packets in the fifo */
316         vlc_cond_wait( &p_ac3dec->fifo.data_wait, &p_ac3dec->fifo.data_lock );
317     }
318
319     /* We can release the lock before leaving */
320     vlc_mutex_unlock( &p_ac3dec->fifo.data_lock );
321 }
322
323 /*****************************************************************************
324  * EndThread : ac3 decoder thread destruction
325  *****************************************************************************/
326 static void EndThread( ac3dec_thread_t * p_ac3dec )
327 {
328     intf_DbgMsg( "ac3dec debug: destroying ac3 decoder thread %p\n", p_ac3dec );
329
330     /* If the audio output fifo was created, we destroy it */
331     if ( p_ac3dec->p_aout_fifo != NULL )
332     {
333         aout_DestroyFifo( p_ac3dec->p_aout_fifo );
334
335         /* Make sure the output thread leaves the NextFrame() function */
336         vlc_mutex_lock( &(p_ac3dec->p_aout_fifo->data_lock) );
337         vlc_cond_signal( &(p_ac3dec->p_aout_fifo->data_wait) );
338         vlc_mutex_unlock( &(p_ac3dec->p_aout_fifo->data_lock) );
339     }
340
341     /* Destroy descriptor */
342     free( p_ac3dec );
343
344     intf_DbgMsg( "ac3dec debug: ac3 decoder thread %p destroyed\n", p_ac3dec );
345 }
346
347 void ac3_byte_stream_next (ac3_byte_stream_t * p_byte_stream)
348 {
349     ac3dec_thread_t * p_ac3dec = p_byte_stream->info;
350
351     /* We are looking for the next TS packet that contains real data,
352      * and not just a PES header */
353     do {
354         /* We were reading the last TS packet of this PES packet... It's
355          * time to jump to the next PES packet */
356         if (p_ac3dec->p_ts->p_next_ts == NULL) {
357             int ptr;
358
359             /* We are going to read/write the start and end indexes of the 
360              * decoder fifo and to use the fifo's conditional variable, 
361              * that's why we need to take the lock before */ 
362             vlc_mutex_lock (&p_ac3dec->fifo.data_lock);
363             
364             /* Is the input thread dying ? */
365             if (p_ac3dec->p_input->b_die) {
366                 vlc_mutex_unlock (&(p_ac3dec->fifo.data_lock));
367                 return;
368             }
369
370             /* We should increase the start index of the decoder fifo, but
371              * if we do this now, the input thread could overwrite the
372              * pointer to the current PES packet, and we weren't able to
373              * give it back to the netlist. That's why we free the PES
374              * packet first. */
375             input_NetlistFreePES (p_ac3dec->p_input, DECODER_FIFO_START(p_ac3dec->fifo) );
376
377             DECODER_FIFO_INCSTART (p_ac3dec->fifo);
378
379             while (DECODER_FIFO_ISEMPTY(p_ac3dec->fifo)) {
380                 vlc_cond_wait (&p_ac3dec->fifo.data_wait, &p_ac3dec->fifo.data_lock );
381
382                 if (p_ac3dec->p_input->b_die) {
383                     vlc_mutex_unlock (&(p_ac3dec->fifo.data_lock));
384                     return;
385                 }
386             }
387
388             /* The next byte could be found in the next PES packet */
389             p_ac3dec->p_ts = DECODER_FIFO_START (p_ac3dec->fifo)->p_first_ts;
390
391             /* parse ac3 magic header */
392             ptr = p_ac3dec->p_ts->buffer [p_ac3dec->p_ts->i_payload_start+2];
393             ptr <<= 8;
394             ptr |= p_ac3dec->p_ts->buffer [p_ac3dec->p_ts->i_payload_start+3];
395             p_ac3dec->sync_ptr = ptr;
396             p_ac3dec->p_ts->i_payload_start += 4;
397
398             /* We can release the fifo's data lock */
399             vlc_mutex_unlock (&p_ac3dec->fifo.data_lock);
400         }
401
402         /* Perhaps the next TS packet of the current PES packet contains 
403          * real data (ie its payload's size is greater than 0) */
404         else {
405             p_ac3dec->p_ts = p_ac3dec->p_ts->p_next_ts;
406         }
407     } while (p_ac3dec->p_ts->i_payload_start == p_ac3dec->p_ts->i_payload_end);
408     p_byte_stream->p_byte =
409         p_ac3dec->p_ts->buffer + p_ac3dec->p_ts->i_payload_start; 
410     p_byte_stream->p_end =
411         p_ac3dec->p_ts->buffer + p_ac3dec->p_ts->i_payload_end; 
412 }