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