]> git.sesse.net Git - vlc/blob - src/ac3_decoder/ac3_decoder_thread.c
Separation du decodeur ac3 et de la partie specifique a videolan
[vlc] / src / ac3_decoder / ac3_decoder_thread.c
1 /*****************************************************************************
2  * ac3_decoder.c: ac3 decoder thread
3  * (c)1999 VideoLAN
4  *****************************************************************************/
5
6 /*
7  * TODO :
8  *
9  * - vérifier l'état de la fifo de sortie avant d'y stocker les samples
10  *   décodés ;
11  * - vlc_cond_signal() / vlc_cond_wait()
12  *
13  */
14
15 /*****************************************************************************
16  * Preamble
17  *****************************************************************************/
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21
22 #include <unistd.h>                                              /* getpid() */
23
24 #include <stdio.h>                                           /* "intf_msg.h" */
25 #include <stdlib.h>                                      /* malloc(), free() */
26 #include <sys/soundcard.h>                               /* "audio_output.h" */
27 #include <sys/uio.h>                                            /* "input.h" */
28
29 #include "common.h"
30 #include "config.h"
31 #include "mtime.h"
32 #include "vlc_thread.h"
33 #include "debug.h"                                      /* "input_netlist.h" */
34
35 #include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */
36
37 #include "input.h"                                           /* pes_packet_t */
38 #include "input_netlist.h"                         /* input_NetlistFreePES() */
39 #include "decoder_fifo.h"         /* DECODER_FIFO_(ISEMPTY|START|INCSTART)() */
40
41 #include "audio_output.h"
42
43 #include "ac3_decoder.h"
44 #include "ac3_decoder_thread.h"
45 #include "ac3_parse.h"
46 #include "ac3_imdct.h"
47
48 /*****************************************************************************
49  * Local prototypes
50  *****************************************************************************/
51 static int      InitThread              ( ac3dec_thread_t * p_adec );
52 static void     RunThread               ( ac3dec_thread_t * p_adec );
53 static void     ErrorThread             ( ac3dec_thread_t * p_adec );
54 static void     EndThread               ( ac3dec_thread_t * p_adec );
55
56 /*****************************************************************************
57  * ac3dec_CreateThread: creates an ac3 decoder thread
58  *****************************************************************************/
59 ac3dec_thread_t * ac3dec_CreateThread( input_thread_t * p_input )
60 {
61     ac3dec_thread_t *   p_ac3dec;
62
63     intf_DbgMsg("ac3dec debug: creating ac3 decoder thread\n");
64
65     /* Allocate the memory needed to store the thread's structure */
66     if ( (p_ac3dec = (ac3dec_thread_t *)malloc( sizeof(ac3dec_thread_t) )) == NULL )
67     {
68         intf_ErrMsg("ac3dec error: not enough memory for ac3dec_CreateThread() to create the new thread\n");
69         return( NULL );
70     }
71
72     /*
73      * Initialize the thread properties
74      */
75     p_ac3dec->b_die = 0;
76     p_ac3dec->b_error = 0;
77
78     /*
79      * Initialize the input properties
80      */
81     /* Initialize the decoder fifo's data lock and conditional variable and set
82      * its buffer as empty */
83     vlc_mutex_init( &p_ac3dec->fifo.data_lock );
84     vlc_cond_init( &p_ac3dec->fifo.data_wait );
85     p_ac3dec->fifo.i_start = 0;
86     p_ac3dec->fifo.i_end = 0;
87     /* Initialize the bit stream structure */
88     p_ac3dec->p_input = p_input;
89     p_ac3dec->ac3_decoder.bit_stream.buffer = 0;
90     p_ac3dec->ac3_decoder.bit_stream.i_available = 0;
91
92     /*
93      * Initialize the output properties
94      */
95     p_ac3dec->p_aout = p_input->p_aout;
96     p_ac3dec->p_aout_fifo = NULL;
97
98     imdct_init();
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\n" );
104         free( p_ac3dec );
105         return( NULL );
106     }
107
108     intf_DbgMsg( "ac3dec debug: ac3 decoder thread (%p) created\n", p_ac3dec );
109     return( p_ac3dec );
110 }
111
112 /*****************************************************************************
113  * ac3dec_DestroyThread: destroys an ac3 decoder thread
114  *****************************************************************************/
115 void ac3dec_DestroyThread( ac3dec_thread_t * p_ac3dec )
116 {
117     intf_DbgMsg( "ac3dec debug: requesting termination of ac3 decoder thread %p\n", p_ac3dec );
118
119     /* Ask thread to kill itself */
120     p_ac3dec->b_die = 1;
121
122     /* Make sure the decoder thread leaves the GetByte() function */
123     vlc_mutex_lock( &(p_ac3dec->fifo.data_lock) );
124     vlc_cond_signal( &(p_ac3dec->fifo.data_wait) );
125     vlc_mutex_unlock( &(p_ac3dec->fifo.data_lock) );
126
127     /* Waiting for the decoder thread to exit */
128     /* Remove this as soon as the "status" flag is implemented */
129     vlc_thread_join( p_ac3dec->thread_id );
130 }
131
132 /* Following functions are local */
133
134 /*****************************************************************************
135  * decode_find_sync()
136  *****************************************************************************/
137 static __inline__ int decode_find_sync( ac3dec_thread_t * p_ac3dec )
138 {
139     while ( (!p_ac3dec->b_die) && (!p_ac3dec->b_error) )
140     {
141         if (! (ac3_test_sync (&p_ac3dec->ac3_decoder)))
142             return 0;
143     }
144     return( -1 );
145 }
146
147 /*****************************************************************************
148  * InitThread : initialize an ac3 decoder thread
149  *****************************************************************************/
150 static int InitThread( ac3dec_thread_t * p_ac3dec )
151 {
152     aout_fifo_t         aout_fifo;
153
154     intf_DbgMsg( "ac3dec debug: initializing ac3 decoder thread %p\n", p_ac3dec );
155
156     /* Our first job is to initialize the bit stream structure with the
157      * beginning of the input stream */
158     vlc_mutex_lock( &p_ac3dec->fifo.data_lock );
159     while ( DECODER_FIFO_ISEMPTY(p_ac3dec->fifo) )
160     {
161         if ( p_ac3dec->b_die )
162         {
163             vlc_mutex_unlock( &p_ac3dec->fifo.data_lock );
164             return( -1 );
165         }
166         vlc_cond_wait( &p_ac3dec->fifo.data_wait, &p_ac3dec->fifo.data_lock );
167     }
168     p_ac3dec->p_ts = DECODER_FIFO_START( p_ac3dec->fifo )->p_first_ts;
169     p_ac3dec->ac3_decoder.bit_stream.byte_stream.p_byte =
170         p_ac3dec->p_ts->buffer + p_ac3dec->p_ts->i_payload_start;
171     p_ac3dec->ac3_decoder.bit_stream.byte_stream.p_end =
172         p_ac3dec->p_ts->buffer + p_ac3dec->p_ts->i_payload_end;
173     p_ac3dec->ac3_decoder.bit_stream.byte_stream.info = p_ac3dec;
174     vlc_mutex_unlock( &p_ac3dec->fifo.data_lock );
175
176     aout_fifo.i_type = AOUT_ADEC_STEREO_FIFO;
177     aout_fifo.i_channels = 2;
178     aout_fifo.b_stereo = 1;
179
180     aout_fifo.l_frame_size = AC3DEC_FRAME_SIZE;
181
182     /* Creating the audio output fifo */
183     if ( (p_ac3dec->p_aout_fifo = aout_CreateFifo(p_ac3dec->p_aout, &aout_fifo)) == NULL )
184     {
185         return( -1 );
186     }
187
188     intf_DbgMsg( "ac3dec debug: ac3 decoder thread %p initialized\n", p_ac3dec );
189     return( 0 );
190 }
191
192 /*****************************************************************************
193  * RunThread : ac3 decoder thread
194  *****************************************************************************/
195 static void RunThread( ac3dec_thread_t * p_ac3dec )
196 {
197     intf_DbgMsg( "ac3dec debug: running ac3 decoder thread (%p) (pid == %i)\n", p_ac3dec, getpid() );
198
199     msleep( INPUT_PTS_DELAY );
200
201     /* Initializing the ac3 decoder thread */
202     if ( InitThread(p_ac3dec) ) /* XXX?? */
203     {
204         p_ac3dec->b_error = 1;
205     }
206
207     /* ac3 decoder thread's main loop */
208     /* FIXME : do we have enough room to store the decoded frames ?? */
209     while ( (!p_ac3dec->b_die) && (!p_ac3dec->b_error) )
210     {
211         int i;
212
213         decode_find_sync( p_ac3dec );
214
215         if ( DECODER_FIFO_START(p_ac3dec->fifo)->b_has_pts )
216         {
217                 p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = DECODER_FIFO_START(p_ac3dec->fifo)->i_pts;
218                 DECODER_FIFO_START(p_ac3dec->fifo)->b_has_pts = 0;
219         }
220         else
221         {
222                 p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = LAST_MDATE;
223         }
224
225         parse_syncinfo( &p_ac3dec->ac3_decoder );
226         switch ( p_ac3dec->ac3_decoder.syncinfo.fscod )
227         {
228                 case 0:
229                         p_ac3dec->p_aout_fifo->l_rate = 48000;
230                         break;
231
232                 case 1:
233                         p_ac3dec->p_aout_fifo->l_rate = 44100;
234                         break;
235
236                 case 2:
237                         p_ac3dec->p_aout_fifo->l_rate = 32000;
238                         break;
239
240                 default: /* XXX?? */
241                         fprintf( stderr, "ac3dec debug: invalid fscod\n" );
242                         continue;
243         }
244
245         parse_bsi( &p_ac3dec->ac3_decoder );
246
247         for (i = 0; i < 6; i++)
248             {
249             s16 * buffer;
250
251             buffer = ((ac3dec_frame_t *)p_ac3dec->p_aout_fifo->buffer)[ p_ac3dec->p_aout_fifo->l_end_frame ];
252
253             if (ac3_audio_block (&p_ac3dec->ac3_decoder, buffer))
254                 goto bad_frame;
255
256             if (i)
257                 p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = LAST_MDATE;
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
264         parse_auxdata( &p_ac3dec->ac3_decoder );
265 bad_frame:
266     }
267
268     /* If b_error is set, the ac3 decoder thread enters the error loop */
269     if ( p_ac3dec->b_error )
270     {
271         ErrorThread( p_ac3dec );
272     }
273
274     /* End of the ac3 decoder thread */
275     EndThread( p_ac3dec );
276 }
277
278 /*****************************************************************************
279  * ErrorThread : ac3 decoder's RunThread() error loop
280  *****************************************************************************/
281 static void ErrorThread( ac3dec_thread_t * p_ac3dec )
282 {
283     /* We take the lock, because we are going to read/write the start/end
284      * indexes of the decoder fifo */
285     vlc_mutex_lock( &p_ac3dec->fifo.data_lock );
286
287     /* Wait until a `die' order is sent */
288     while( !p_ac3dec->b_die )
289     {
290         /* Trash all received PES packets */
291         while( !DECODER_FIFO_ISEMPTY(p_ac3dec->fifo) )
292         {
293             input_NetlistFreePES( p_ac3dec->p_input, DECODER_FIFO_START(p_ac3dec->fifo) );
294             DECODER_FIFO_INCSTART( p_ac3dec->fifo );
295         }
296
297         /* Waiting for the input thread to put new PES packets in the fifo */
298         vlc_cond_wait( &p_ac3dec->fifo.data_wait, &p_ac3dec->fifo.data_lock );
299     }
300
301     /* We can release the lock before leaving */
302     vlc_mutex_unlock( &p_ac3dec->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         /* We were reading the last TS packet of this PES packet... It's
337          * time to jump to the next PES packet */
338         if (p_ac3dec->p_ts->p_next_ts == NULL) {
339             /* We are going to read/write the start and end indexes of the 
340              * decoder fifo and to use the fifo's conditional variable, 
341              * that's why we need to take the lock before */ 
342             vlc_mutex_lock (&p_ac3dec->fifo.data_lock);
343             
344             /* Is the input thread dying ? */
345             if (p_ac3dec->p_input->b_die) {
346                 vlc_mutex_unlock (&(p_ac3dec->fifo.data_lock));
347                 return;
348             }
349
350             /* We should increase the start index of the decoder fifo, but
351              * if we do this now, the input thread could overwrite the
352              * pointer to the current PES packet, and we weren't able to
353              * give it back to the netlist. That's why we free the PES
354              * packet first. */
355             input_NetlistFreePES (p_ac3dec->p_input, DECODER_FIFO_START(p_ac3dec->fifo) );
356
357             DECODER_FIFO_INCSTART (p_ac3dec->fifo);
358
359             while (DECODER_FIFO_ISEMPTY(p_ac3dec->fifo)) {
360                 vlc_cond_wait (&p_ac3dec->fifo.data_wait, &p_ac3dec->fifo.data_lock );
361
362                 if (p_ac3dec->p_input->b_die) {
363                     vlc_mutex_unlock (&(p_ac3dec->fifo.data_lock));
364                     return;
365                 }
366             }
367
368             /* The next byte could be found in the next PES packet */
369             p_ac3dec->p_ts = DECODER_FIFO_START (p_ac3dec->fifo)->p_first_ts;
370
371             /* We can release the fifo's data lock */
372             vlc_mutex_unlock (&p_ac3dec->fifo.data_lock);
373         }
374
375         /* Perhaps the next TS packet of the current PES packet contains 
376          * real data (ie its payload's size is greater than 0) */
377         else {
378             p_ac3dec->p_ts = p_ac3dec->p_ts->p_next_ts;
379         }
380     } while (p_ac3dec->p_ts->i_payload_start == p_ac3dec->p_ts->i_payload_end);
381     p_byte_stream->p_byte =
382         p_ac3dec->p_ts->buffer + p_ac3dec->p_ts->i_payload_start; 
383     p_byte_stream->p_end =
384         p_ac3dec->p_ts->buffer + p_ac3dec->p_ts->i_payload_end; 
385 }