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