]> git.sesse.net Git - vlc/blob - src/ac3_decoder/ac3_decoder_thread.c
Bon. On ne rit pas, je m'�tais juste plant� dans l'en-t�te des
[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
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
21  *****************************************************************************/
22
23 /*
24  * TODO :
25  *
26  * - vérifier l'état de la fifo de sortie avant d'y stocker les samples
27  *   décodés ;
28  * - vlc_cond_signal() / vlc_cond_wait()
29  *
30  */
31
32 /*****************************************************************************
33  * Preamble
34  *****************************************************************************/
35 #include "defs.h"
36
37 #include <unistd.h>                                              /* getpid() */
38
39 #include <stdio.h>                                           /* "intf_msg.h" */
40 #include <stdlib.h>                                      /* malloc(), free() */
41 #include <sys/types.h>                        /* on BSD, uio.h needs types.h */
42 #include <sys/uio.h>                                            /* "input.h" */
43
44 #include "config.h"
45 #include "common.h"
46 #include "threads.h"
47 #include "mtime.h"
48 #include "plugins.h"
49 #include "debug.h"                                      /* "input_netlist.h" */
50
51 #include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */
52
53 #include "input.h"                                           /* pes_packet_t */
54 #include "input_netlist.h"                         /* input_NetlistFreePES() */
55 #include "decoder_fifo.h"         /* DECODER_FIFO_(ISEMPTY|START|INCSTART)() */
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 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         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         intf_ErrMsg  ("ac3dec error: can't spawn ac3 decoder thread\n");
118         free (p_ac3dec);
119         return NULL;
120     }
121
122     intf_DbgMsg ("ac3dec debug: ac3 decoder thread (%p) created\n", p_ac3dec);
123     return p_ac3dec;
124 }
125
126 /*****************************************************************************
127  * ac3dec_DestroyThread: destroys an ac3 decoder thread
128  *****************************************************************************/
129 void ac3dec_DestroyThread (ac3dec_thread_t * p_ac3dec)
130 {
131     intf_DbgMsg ("ac3dec debug: requesting termination of ac3 decoder thread %p\n", p_ac3dec);
132
133     /* Ask thread to kill itself */
134     p_ac3dec->b_die = 1;
135
136     /* Make sure the decoder thread leaves the GetByte() function */
137     vlc_mutex_lock (&(p_ac3dec->fifo.data_lock));
138     vlc_cond_signal (&(p_ac3dec->fifo.data_wait));
139     vlc_mutex_unlock (&(p_ac3dec->fifo.data_lock));
140
141     /* Waiting for the decoder thread to exit */
142     /* Remove this as soon as the "status" flag is implemented */
143     vlc_thread_join (p_ac3dec->thread_id);
144 }
145
146 /* Following functions are local */
147
148 /*****************************************************************************
149  * InitThread : initialize an ac3 decoder thread
150  *****************************************************************************/
151 static int InitThread (ac3dec_thread_t * p_ac3dec)
152 {
153     aout_fifo_t         aout_fifo;
154     ac3_byte_stream_t * byte_stream;
155
156     intf_DbgMsg ("ac3dec debug: initializing ac3 decoder thread %p\n", p_ac3dec);
157
158     /* Our first job is to initialize the bit stream structure with the
159      * beginning of the input stream */
160     vlc_mutex_lock (&p_ac3dec->fifo.data_lock);
161     while (DECODER_FIFO_ISEMPTY(p_ac3dec->fifo)) {
162         if (p_ac3dec->b_die) {
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     byte_stream = ac3_byte_stream (&p_ac3dec->ac3_decoder);
170     byte_stream->p_byte =
171         p_ac3dec->p_ts->buffer + p_ac3dec->p_ts->i_payload_start;
172     byte_stream->p_end =
173         p_ac3dec->p_ts->buffer + p_ac3dec->p_ts->i_payload_end;
174     byte_stream->info = p_ac3dec;
175     vlc_mutex_unlock (&p_ac3dec->fifo.data_lock);
176
177     aout_fifo.i_type = AOUT_ADEC_STEREO_FIFO;
178     aout_fifo.i_channels = 2;
179     aout_fifo.b_stereo = 1;
180
181     aout_fifo.l_frame_size = AC3DEC_FRAME_SIZE;
182
183     /* Creating the audio output fifo */
184     if ((p_ac3dec->p_aout_fifo = aout_CreateFifo(p_ac3dec->p_aout, &aout_fifo)) == NULL) {
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     int sync;
198
199     intf_DbgMsg ("ac3dec debug: running ac3 decoder thread (%p) (pid == %i)\n", p_ac3dec, getpid());
200
201     msleep (INPUT_PTS_DELAY);
202
203     /* Initializing the ac3 decoder thread */
204     if (InitThread (p_ac3dec)) /* XXX?? */ {
205         p_ac3dec->b_error = 1;
206     }
207
208     sync = 0;
209     p_ac3dec->sync_ptr = 0;
210
211     /* ac3 decoder thread's main loop */
212     /* FIXME : do we have enough room to store the decoded frames ?? */
213     while ((!p_ac3dec->b_die) && (!p_ac3dec->b_error)) {
214         s16 * buffer;
215         ac3_sync_info_t sync_info;
216
217         if (!sync) { /* have to find a synchro point */
218             int ptr;
219             ac3_byte_stream_t * p_byte_stream;
220
221             printf ("sync\n");
222
223             p_byte_stream = ac3_byte_stream (&p_ac3dec->ac3_decoder);
224
225             /* first read till next ac3 magic header */
226             do {
227                 ac3_byte_stream_next (p_byte_stream);
228             } while ((!p_ac3dec->sync_ptr) &&
229                     (!p_ac3dec->b_die) &&
230                     (!p_ac3dec->b_error));
231             /* skip the specified number of bytes */
232
233             ptr = p_ac3dec->sync_ptr;
234             while (--ptr && (!p_ac3dec->b_die) && (!p_ac3dec->b_error)) {
235                 if (p_byte_stream->p_byte >= p_byte_stream->p_end) {
236                     ac3_byte_stream_next (p_byte_stream);                   
237                 }
238                 p_byte_stream->p_byte++;
239             }
240
241             /* we are in sync now */
242
243             sync = 1;
244             p_ac3dec->sync_ptr = 0;
245         }
246
247         if (DECODER_FIFO_START(p_ac3dec->fifo)->b_has_pts) {
248             p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = DECODER_FIFO_START(p_ac3dec->fifo)->i_pts;
249             DECODER_FIFO_START(p_ac3dec->fifo)->b_has_pts = 0;
250         } else {
251             p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = LAST_MDATE;
252         }
253
254         if (ac3_sync_frame (&p_ac3dec->ac3_decoder, &sync_info)) {
255             sync = 0;
256             goto bad_frame;
257         }
258
259         p_ac3dec->p_aout_fifo->l_rate = sync_info.sample_rate;
260
261         buffer = ((s16 *)p_ac3dec->p_aout_fifo->buffer) + (p_ac3dec->p_aout_fifo->l_end_frame * AC3DEC_FRAME_SIZE);
262
263         if (ac3_decode_frame (&p_ac3dec->ac3_decoder, buffer)) {
264             sync = 0;
265             goto bad_frame;
266         }
267
268         vlc_mutex_lock (&p_ac3dec->p_aout_fifo->data_lock);
269         p_ac3dec->p_aout_fifo->l_end_frame = (p_ac3dec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
270         vlc_cond_signal (&p_ac3dec->p_aout_fifo->data_wait);
271         vlc_mutex_unlock (&p_ac3dec->p_aout_fifo->data_lock);
272
273     bad_frame:
274     }
275
276     /* If b_error is set, the ac3 decoder thread enters the error loop */
277     if (p_ac3dec->b_error) {
278         ErrorThread (p_ac3dec);
279     }
280
281     /* End of the ac3 decoder thread */
282     EndThread (p_ac3dec);
283 }
284
285 /*****************************************************************************
286  * ErrorThread : ac3 decoder's RunThread() error loop
287  *****************************************************************************/
288 static void ErrorThread (ac3dec_thread_t * p_ac3dec)
289 {
290     /* We take the lock, because we are going to read/write the start/end
291      * indexes of the decoder fifo */
292     vlc_mutex_lock (&p_ac3dec->fifo.data_lock);
293
294     /* Wait until a `die' order is sent */
295     while (!p_ac3dec->b_die) {
296         /* Trash all received PES packets */
297         while (!DECODER_FIFO_ISEMPTY(p_ac3dec->fifo)) {
298             input_NetlistFreePES (p_ac3dec->p_input, DECODER_FIFO_START(p_ac3dec->fifo));
299             DECODER_FIFO_INCSTART (p_ac3dec->fifo);
300         }
301
302         /* Waiting for the input thread to put new PES packets in the fifo */
303         vlc_cond_wait (&p_ac3dec->fifo.data_wait, &p_ac3dec->fifo.data_lock);
304     }
305
306     /* We can release the lock before leaving */
307     vlc_mutex_unlock (&p_ac3dec->fifo.data_lock);
308 }
309
310 /*****************************************************************************
311  * EndThread : ac3 decoder thread destruction
312  *****************************************************************************/
313 static void EndThread (ac3dec_thread_t * p_ac3dec)
314 {
315     intf_DbgMsg ("ac3dec debug: destroying ac3 decoder thread %p\n", p_ac3dec);
316
317     /* If the audio output fifo was created, we destroy it */
318     if (p_ac3dec->p_aout_fifo != NULL) {
319         aout_DestroyFifo (p_ac3dec->p_aout_fifo);
320
321         /* Make sure the output thread leaves the NextFrame() function */
322         vlc_mutex_lock (&(p_ac3dec->p_aout_fifo->data_lock));
323         vlc_cond_signal (&(p_ac3dec->p_aout_fifo->data_wait));
324         vlc_mutex_unlock (&(p_ac3dec->p_aout_fifo->data_lock));
325     }
326
327     /* Destroy descriptor */
328     free (p_ac3dec);
329
330     intf_DbgMsg ("ac3dec debug: ac3 decoder thread %p destroyed\n", p_ac3dec);
331 }
332
333 void ac3_byte_stream_next (ac3_byte_stream_t * p_byte_stream)
334 {
335     ac3dec_thread_t * p_ac3dec = p_byte_stream->info;
336
337     /* We are looking for the next TS packet that contains real data,
338      * and not just a PES header */
339     do {
340         /* We were reading the last TS packet of this PES packet... It's
341          * time to jump to the next PES packet */
342         if (p_ac3dec->p_ts->p_next_ts == NULL) {
343             int ptr;
344
345             /* We are going to read/write the start and end indexes of the 
346              * decoder fifo and to use the fifo's conditional variable, 
347              * that's why we need to take the lock before */ 
348             vlc_mutex_lock (&p_ac3dec->fifo.data_lock);
349             
350             /* Is the input thread dying ? */
351             if (p_ac3dec->p_input->b_die) {
352                 vlc_mutex_unlock (&(p_ac3dec->fifo.data_lock));
353                 return;
354             }
355
356             /* We should increase the start index of the decoder fifo, but
357              * if we do this now, the input thread could overwrite the
358              * pointer to the current PES packet, and we weren't able to
359              * give it back to the netlist. That's why we free the PES
360              * packet first. */
361             input_NetlistFreePES (p_ac3dec->p_input, DECODER_FIFO_START(p_ac3dec->fifo));
362
363             DECODER_FIFO_INCSTART (p_ac3dec->fifo);
364
365             while (DECODER_FIFO_ISEMPTY(p_ac3dec->fifo)) {
366                 vlc_cond_wait (&p_ac3dec->fifo.data_wait, &p_ac3dec->fifo.data_lock);
367
368                 if (p_ac3dec->p_input->b_die) {
369                     vlc_mutex_unlock (&(p_ac3dec->fifo.data_lock));
370                     return;
371                 }
372             }
373
374             /* The next byte could be found in the next PES packet */
375             p_ac3dec->p_ts = DECODER_FIFO_START (p_ac3dec->fifo)->p_first_ts;
376
377             /* parse ac3 magic header */
378             ptr = p_ac3dec->p_ts->buffer [p_ac3dec->p_ts->i_payload_start+2];
379             ptr <<= 8;
380             ptr |= p_ac3dec->p_ts->buffer [p_ac3dec->p_ts->i_payload_start+3];
381             p_ac3dec->sync_ptr = ptr;
382             p_ac3dec->p_ts->i_payload_start += 4;
383
384             /* We can release the fifo's data lock */
385             vlc_mutex_unlock (&p_ac3dec->fifo.data_lock);
386         }
387
388         /* Perhaps the next TS packet of the current PES packet contains 
389          * real data (ie its payload's size is greater than 0) */
390         else {
391             p_ac3dec->p_ts = p_ac3dec->p_ts->p_next_ts;
392         }
393     } while (p_ac3dec->p_ts->i_payload_start == p_ac3dec->p_ts->i_payload_end);
394     p_byte_stream->p_byte =
395         p_ac3dec->p_ts->buffer + p_ac3dec->p_ts->i_payload_start; 
396     p_byte_stream->p_end =
397         p_ac3dec->p_ts->buffer + p_ac3dec->p_ts->i_payload_end; 
398 }