]> git.sesse.net Git - vlc/blob - src/audio_decoder/audio_decoder.c
Initial revision
[vlc] / src / audio_decoder / audio_decoder.c
1 /******************************************************************************
2  * audio_decoder.c: MPEG1 Layer I-II audio decoder thread
3  * (c)1999 VideoLAN
4  ******************************************************************************/
5
6 /*
7  * TODO :
8  * - Optimiser les NeedBits() et les GetBits() du code là où c'est possible
9  */
10
11 /******************************************************************************
12  * Preamble
13  ******************************************************************************/
14 #include <unistd.h>
15
16 #include <pthread.h>
17 #include <stdio.h>                                            /* "intf_msg.h" */
18 #include <stdlib.h>                                       /* malloc(), free() */
19 #include <netinet/in.h>                                            /* ntohl() */
20 #include <sys/soundcard.h>                                /* "audio_output.h" */
21 #include <sys/uio.h>                                             /* "input.h" */
22
23 #include "common.h"
24 #include "config.h"
25 #include "mtime.h"
26 #include "debug.h"                                       /* "input_netlist.h" */
27
28 #include "intf_msg.h"                         /* intf_DbgMsg(), intf_ErrMsg() */
29
30 #include "input.h"                                            /* pes_packet_t */
31 #include "input_netlist.h"                          /* input_NetlistFreePES() */
32 #include "decoder_fifo.h"          /* DECODER_FIFO_(ISEMPTY|START|INCSTART)() */
33
34 #include "audio_output.h"
35
36 #include "audio_constants.h"
37 #include "audio_decoder.h"
38
39 /******************************************************************************
40  * Local prototypes
41  ******************************************************************************/
42 static int      InitThread              ( adec_thread_t * p_adec );
43 static void     RunThread               ( adec_thread_t * p_adec );
44 static void     ErrorThread             ( adec_thread_t * p_adec );
45 static void     EndThread               ( adec_thread_t * p_adec );
46
47 static int      adec_Layer1_Mono        ( adec_thread_t * p_adec );
48 static int      adec_Layer1_Stereo      ( adec_thread_t * p_adec );
49 static int      adec_Layer2_Mono        ( adec_thread_t * p_adec );
50 static int      adec_Layer2_Stereo      ( adec_thread_t * p_adec );
51
52 static byte_t   GetByte                 ( bit_stream_t * p_bit_stream );
53 static void     NeedBits                ( bit_stream_t * p_bit_stream, int i_bits );
54 static void     DumpBits                ( bit_stream_t * p_bit_stream, int i_bits );
55 static int      FindHeader              ( adec_thread_t * p_adec );
56
57 /******************************************************************************
58  * adec_CreateThread: creates an audio decoder thread
59  ******************************************************************************
60  * This function creates a new audio decoder thread, and returns a pointer to
61  * its description. On error, it returns NULL.
62  ******************************************************************************/
63 adec_thread_t * adec_CreateThread( input_thread_t * p_input )
64 {
65     adec_thread_t *     p_adec;
66
67     intf_DbgMsg("adec debug: creating audio decoder thread\n");
68
69     /* Allocate the memory needed to store the thread's structure */
70     if ( (p_adec = (adec_thread_t *)malloc( sizeof(adec_thread_t) )) == NULL )
71     {
72         intf_ErrMsg("adec error: not enough memory for adec_CreateThread() to create the new thread\n");
73         return( NULL );
74     }
75
76     /*
77      * Initialize the thread properties
78      */
79     p_adec->b_die = 0;
80     p_adec->b_error = 0;
81
82     /*
83      * Initialize the input properties
84      */
85     /* Initialize the decoder fifo's data lock and conditional variable and set
86      * its buffer as empty */
87     pthread_mutex_init( &p_adec->fifo.data_lock, NULL );
88     pthread_cond_init( &p_adec->fifo.data_wait, NULL );
89     p_adec->fifo.i_start = 0;
90     p_adec->fifo.i_end = 0;
91     /* Initialize the bit stream structure */
92     p_adec->bit_stream.p_input = p_input;
93     p_adec->bit_stream.p_decoder_fifo = &p_adec->fifo;
94     p_adec->bit_stream.fifo.buffer = 0;
95     p_adec->bit_stream.fifo.i_available = 0;
96
97     /*
98      * Initialize the decoder properties
99      */
100     p_adec->bank_0.actual = p_adec->bank_0.v1;
101     p_adec->bank_0.pos = 0;
102     p_adec->bank_1.actual = p_adec->bank_1.v1;
103     p_adec->bank_1.pos = 0;
104
105     /*
106      * Initialize the output properties
107      */
108     p_adec->p_aout = p_input->p_aout;
109     p_adec->p_aout_fifo = NULL;
110
111     /* Spawn the audio decoder thread */
112     if ( pthread_create(&p_adec->thread_id, NULL, (void *)RunThread, (void *)p_adec) )
113     {
114         intf_ErrMsg("adec error: can't spawn audio decoder thread\n");
115         free( p_adec );
116         return( NULL );
117     }
118
119     intf_DbgMsg("adec debug: audio decoder thread (%p) created\n", p_adec);
120     return( p_adec );
121 }
122
123 /******************************************************************************
124  * adec_DestroyThread: destroys an audio decoder thread
125  ******************************************************************************
126  * This function asks an audio decoder thread to terminate. This function has
127  * not to wait until the decoder thread has really died, because the killer (ie
128  * this function's caller) is the input thread, that's why we are sure that no
129  * other thread will try to access to this thread's descriptor after its
130  * destruction.
131  ******************************************************************************/
132 void adec_DestroyThread( adec_thread_t * p_adec )
133 {
134     intf_DbgMsg("adec debug: requesting termination of audio decoder thread %p\n", p_adec);
135
136     /* Ask thread to kill itself */
137     p_adec->b_die = 1;
138
139     /* Remove this as soon as the "status" flag is implemented */
140     pthread_join( p_adec->thread_id, NULL );         /* wait until it's done */
141 }
142
143 /* Following functions are local */
144
145 /******************************************************************************
146  * GetByte : reads the next byte in the input stream
147  ******************************************************************************/
148 static __inline__ byte_t GetByte( bit_stream_t * p_bit_stream )
149 {
150     /* Are there some bytes left in the current TS packet ? */
151     if ( p_bit_stream->i_byte < p_bit_stream->p_ts->i_payload_end )
152     {
153         return( p_bit_stream->p_ts->buffer[ p_bit_stream->i_byte++ ] );
154     }
155     else
156     {
157         /* We are looking for the next TS packet that contains real data,
158          * and not just a PES header */
159         do
160         {
161             /* We were reading the last TS packet of this PES packet... It's
162              * time to jump to the next PES packet */
163             if ( p_bit_stream->p_ts->p_next_ts == NULL )
164             {
165                 /* We are going to read/write the start and end indexes of the
166                  * decoder fifo and to use the fifo's conditional variable,
167                  * that's why we need to take the lock before */
168                 pthread_mutex_lock( &p_bit_stream->p_decoder_fifo->data_lock );
169
170                 /* We should increase the start index of the decoder fifo, but
171                  * if we do this now, the input thread could overwrite the
172                  * pointer to the current PES packet, and we weren't able to
173                  * give it back to the netlist. That's why we free the PES
174                  * packet first. */
175                 input_NetlistFreePES( p_bit_stream->p_input, DECODER_FIFO_START(*p_bit_stream->p_decoder_fifo) );
176                 DECODER_FIFO_INCSTART( *p_bit_stream->p_decoder_fifo );
177
178                 /* !! b_die !! */
179                 while ( DECODER_FIFO_ISEMPTY(*p_bit_stream->p_decoder_fifo) )
180                 {
181                     pthread_cond_wait( &p_bit_stream->p_decoder_fifo->data_wait,
182                                        &p_bit_stream->p_decoder_fifo->data_lock );
183                 }
184
185                 /* The next byte could be found in the next PES packet */
186 #ifdef DEBUG
187 //                fprintf(stderr, "*");
188 #endif
189                 p_bit_stream->p_ts = DECODER_FIFO_START( *p_bit_stream->p_decoder_fifo )->p_first_ts;
190
191                 /* We can release the fifo's data lock */
192                 pthread_mutex_unlock( &p_bit_stream->p_decoder_fifo->data_lock );
193             }
194             /* Perhaps the next TS packet of the current PES packet contains
195              * real data (ie its payload's size is greater than 0) */
196             else
197             {
198 #ifdef DEBUG
199 //                fprintf(stderr, ".");
200 #endif
201                 p_bit_stream->p_ts = p_bit_stream->p_ts->p_next_ts;
202             }
203         } while ( p_bit_stream->p_ts->i_payload_start == p_bit_stream->p_ts->i_payload_end );
204
205         /* We've found a TS packet which contains interesting data... As we
206          * return the payload's first byte, we set i_byte to the following
207          * one */
208         p_bit_stream->i_byte = p_bit_stream->p_ts->i_payload_start;
209         return( p_bit_stream->p_ts->buffer[ p_bit_stream->i_byte++ ] );
210     }
211 }
212
213 /******************************************************************************
214  * NeedBits : reads i_bits new bits in the bit stream and stores them in the
215  *            bit buffer
216  ******************************************************************************
217  * - i_bits must be less or equal 32 !
218  * - There is something important to notice with that function : if the number
219  * of bits available in the bit buffer when calling NeedBits() is greater than
220  * 24 (i_available > 24) but less than the number of needed bits
221  * (i_available < i_bits), the byte returned by GetByte() will be shifted with
222  * a negative value and the number of bits available in the bit buffer will be
223  * set to more than 32 !
224  ******************************************************************************/
225 static __inline__ void NeedBits( bit_stream_t * p_bit_stream, int i_bits )
226 {
227     while ( p_bit_stream->fifo.i_available < i_bits )
228     {
229         p_bit_stream->fifo.buffer |= ((u32)GetByte( p_bit_stream )) << (24 - p_bit_stream->fifo.i_available);
230         p_bit_stream->fifo.i_available += 8;
231     }
232 }
233
234 /******************************************************************************
235  * DumpBits : removes i_bits bits from the bit buffer
236  ******************************************************************************
237  * - i_bits <= i_available
238  * - i_bits < 32 (because (u32 << 32) <=> (u32 = u32))
239  ******************************************************************************/
240 static __inline__ void DumpBits( bit_stream_t * p_bit_stream, int i_bits )
241 {
242     p_bit_stream->fifo.buffer <<= i_bits;
243     p_bit_stream->fifo.i_available -= i_bits;
244 }
245
246 /******************************************************************************
247  * FindHeader : parses an input stream until an audio frame header could be
248  *              found
249  ******************************************************************************
250  * When this function returns successfully, the header can be found in the
251  * buffer of the bit stream fifo.
252  ******************************************************************************/
253 static int FindHeader( adec_thread_t * p_adec )
254 {
255     while ( (!p_adec->b_die) && (!p_adec->b_error) )
256     {
257         NeedBits( &p_adec->bit_stream, 32 );
258         if ( (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_SYNCWORD_MASK) == ADEC_HEADER_SYNCWORD_MASK )
259         {
260 #ifdef DEBUG
261 //            fprintf(stderr, "H");
262 #endif
263             return( 0 );
264         }
265 #ifdef DEBUG
266 //        fprintf(stderr, "!");
267 #endif
268         DumpBits( &p_adec->bit_stream, 8 );
269     }
270
271     return( -1 );
272 }
273
274 /******************************************************************************
275  * adec_Layer`L'_`M': decodes an mpeg 1, layer `L', mode `M', audio frame
276  ******************************************************************************
277  * These functions decode the audio frame which has already its header loaded
278  * in the i_header member of the audio decoder thread structure and its first
279  * byte of data described by the bit stream structure of the audio decoder
280  * thread (there is no bit available in the bit buffer yet)
281  ******************************************************************************/
282
283 /******************************************************************************
284  * adec_Layer1_Mono
285  ******************************************************************************/
286 static __inline__ int adec_Layer1_Mono( adec_thread_t * p_adec )
287 {
288     p_adec->bit_stream.fifo.buffer = 0;
289     p_adec->bit_stream.fifo.i_available = 0;
290     return( 0 );
291 }
292
293 /******************************************************************************
294  * adec_Layer1_Stereo
295  ******************************************************************************/
296 static __inline__ int adec_Layer1_Stereo( adec_thread_t * p_adec )
297 {
298     p_adec->bit_stream.fifo.buffer = 0;
299     p_adec->bit_stream.fifo.i_available = 0;
300     return( 0 );
301 }
302
303 /******************************************************************************
304  * adec_Layer2_Mono
305  ******************************************************************************/
306 static __inline__ int adec_Layer2_Mono( adec_thread_t * p_adec )
307 {
308     p_adec->bit_stream.fifo.buffer = 0;
309     p_adec->bit_stream.fifo.i_available = 0;
310     return( 0 );
311 }
312
313 /******************************************************************************
314  * adec_Layer2_Stereo
315  ******************************************************************************/
316 static __inline__ int adec_Layer2_Stereo( adec_thread_t * p_adec )
317 {
318     typedef struct requantization_s
319     {
320         byte_t                          i_bits_per_codeword;
321         const float *                   pf_ungroup;
322         float                           f_slope;
323         float                           f_offset;
324     } requantization_t;
325
326     static const float                  pf_scalefactor[64] = ADEC_SCALE_FACTOR;
327
328     static u32                          i_header;
329     static int                          i_sampling_frequency, i_mode, i_bound;
330     static int                          pi_allocation_0[32], pi_allocation_1[32]; /* see ISO/IEC 11172-3 2.4.1.6 */
331     int                                 i_sb, i_nbal;
332     float                               f_scalefactor_0, f_scalefactor_1;
333
334     static const byte_t                 ppi_bitrate_per_channel_index[4][15] = ADEC_LAYER2_BITRATE_PER_CHANNEL_INDEX;
335     static const byte_t                 ppi_sblimit[3][11] = ADEC_LAYER2_SBLIMIT;
336     static const byte_t                 ppi_nbal[2][32] = ADEC_LAYER2_NBAL;
337
338     static const float                  pf_ungroup3[3*3*3 * 3] = ADEC_LAYER2_UNGROUP3;
339     static const float                  pf_ungroup5[5*5*5 * 3] = ADEC_LAYER2_UNGROUP5;
340     static const float                  pf_ungroup9[9*9*9 * 3] = ADEC_LAYER2_UNGROUP9;
341
342     static const requantization_t       p_requantization_cd[16] = ADEC_LAYER2_REQUANTIZATION_CD;
343     static const requantization_t       p_requantization_ab1[16] = ADEC_LAYER2_REQUANTIZATION_AB1;
344     static const requantization_t       p_requantization_ab2[16] = ADEC_LAYER2_REQUANTIZATION_AB2;
345     static const requantization_t       p_requantization_ab3[16] = ADEC_LAYER2_REQUANTIZATION_AB3;
346     static const requantization_t       p_requantization_ab4[16] = ADEC_LAYER2_REQUANTIZATION_AB4;
347     static const requantization_t *     pp_requantization_ab[30] = ADEC_LAYER2_REQUANTIZATION_AB;
348
349     static int                          i_sblimit, i_bitrate_per_channel_index;
350     static int                          pi_scfsi_0[30], pi_scfsi_1[30];
351     static const byte_t *               pi_nbal;
352     static float                        ppf_sample_0[3][32], ppf_sample_1[3][32];
353     static const requantization_t *     pp_requantization_0[30];
354     static const requantization_t *     pp_requantization_1[30];
355     static requantization_t             requantization;
356     static const float *                pf_ungroup;
357
358     static float                        pf_scalefactor_0_0[30], pf_scalefactor_0_1[30], pf_scalefactor_0_2[30];
359     static float                        pf_scalefactor_1_0[30], pf_scalefactor_1_1[30], pf_scalefactor_1_2[30];
360
361     int                                 i_2nbal, i_gr;
362     float                               f_dummy;
363
364     long                                l_end_frame;
365     s16 *                               p_s16;
366
367     int                                 i_need = 0, i_dump = 0;
368 //    static const int                    pi_framesize[512] = ADEC_FRAME_SIZE;
369
370     /* Read the audio frame header and flush the bit buffer */
371     i_header = p_adec->bit_stream.fifo.buffer;
372     p_adec->bit_stream.fifo.buffer = 0;
373     p_adec->bit_stream.fifo.i_available = 0;
374     /* Read the sampling frequency (see ISO/IEC 11172-3 2.4.2.3) */
375     i_sampling_frequency = (int)((i_header & ADEC_HEADER_SAMPLING_FREQUENCY_MASK)
376         >> ADEC_HEADER_SAMPLING_FREQUENCY_SHIFT);
377     /* Read the mode (see ISO/IEC 11172-3 2.4.2.3) */
378     i_mode = (int)((i_header & ADEC_HEADER_MODE_MASK) >> ADEC_HEADER_MODE_SHIFT);
379     /* If a CRC can be found in the frame, get rid of it */
380     if ( (i_header & ADEC_HEADER_PROTECTION_BIT_MASK) == 0 )
381     {
382         GetByte( &p_adec->bit_stream );
383         GetByte( &p_adec->bit_stream );
384     }
385
386     /* Find out the bitrate per channel index */
387     i_bitrate_per_channel_index = (int)ppi_bitrate_per_channel_index[i_mode]
388         [(i_header & ADEC_HEADER_BITRATE_INDEX_MASK) >> ADEC_HEADER_BITRATE_INDEX_SHIFT];
389     /* Find out the number of subbands */
390     i_sblimit = (int)ppi_sblimit[i_sampling_frequency][i_bitrate_per_channel_index];
391     /* Check if the frame is valid or not */
392     if ( i_sblimit == 0 )
393     {
394         return( 0 );                                  /* the frame is invalid */
395     }
396     /* Find out the number of bits allocated */
397     pi_nbal = ppi_nbal[ (i_bitrate_per_channel_index <= 2) ? 0 : 1 ];
398
399     /* Find out the `bound' subband (see ISO/IEC 11172-3 2.4.2.3) */
400     if ( i_mode == 1 )
401     {
402         i_bound = (int)(((i_header & ADEC_HEADER_MODE_EXTENSION_MASK) >> (ADEC_HEADER_MODE_EXTENSION_SHIFT - 2)) + 4);
403         if ( i_bound > i_sblimit )
404         {
405             i_bound = i_sblimit;
406         }
407     }
408     else
409     {
410         i_bound = i_sblimit;
411     }
412
413     /* Read the allocation information (see ISO/IEC 11172-3 2.4.1.6) */
414     for ( i_sb = 0; i_sb < i_bound; i_sb++ )
415     {
416         i_2nbal = 2 * (i_nbal = (int)pi_nbal[ i_sb ]);
417         NeedBits( &p_adec->bit_stream, i_2nbal );
418         i_need += i_2nbal;
419         pi_allocation_0[ i_sb ] = (int)(p_adec->bit_stream.fifo.buffer >> (32 - i_nbal));
420         p_adec->bit_stream.fifo.buffer <<= i_nbal;
421         pi_allocation_1[ i_sb ] = (int)(p_adec->bit_stream.fifo.buffer >> (32 - i_nbal));
422         p_adec->bit_stream.fifo.buffer <<= i_nbal;
423         p_adec->bit_stream.fifo.i_available -= i_2nbal;
424         i_dump += i_2nbal;
425     }
426     for ( ; i_sb < i_sblimit; i_sb++ )
427     {
428         i_nbal = (int)pi_nbal[ i_sb ];
429         NeedBits( &p_adec->bit_stream, i_nbal );
430         i_need += i_nbal;
431         pi_allocation_0[ i_sb ] = (int)(p_adec->bit_stream.fifo.buffer >> (32 - i_nbal));
432         DumpBits( &p_adec->bit_stream, i_nbal );
433         i_dump += i_nbal;
434     }
435
436 #define MACRO( p_requantization ) \
437     for ( i_sb = 0; i_sb < i_bound; i_sb++ ) \
438     { \
439         if ( pi_allocation_0[i_sb] ) \
440         { \
441             pp_requantization_0[i_sb] = &((p_requantization)[pi_allocation_0[i_sb]]); \
442             NeedBits( &p_adec->bit_stream, 2 ); \
443             i_need += 2; \
444             pi_scfsi_0[i_sb] = (int)(p_adec->bit_stream.fifo.buffer >> (32 - 2)); \
445             DumpBits( &p_adec->bit_stream, 2 ); \
446             i_dump += 2; \
447         } \
448         else \
449         { \
450             ppf_sample_0[0][i_sb] = .0; \
451             ppf_sample_0[1][i_sb] = .0; \
452             ppf_sample_0[2][i_sb] = .0; \
453         } \
454 \
455         if ( pi_allocation_1[i_sb] ) \
456         { \
457             pp_requantization_1[i_sb] = &((p_requantization)[pi_allocation_1[i_sb]]); \
458             NeedBits( &p_adec->bit_stream, 2 ); \
459             i_need += 2; \
460             pi_scfsi_1[i_sb] = (int)(p_adec->bit_stream.fifo.buffer >> (32 - 2)); \
461             DumpBits( &p_adec->bit_stream, 2 ); \
462             i_dump += 2; \
463         } \
464         else \
465         { \
466             ppf_sample_1[0][i_sb] = .0; \
467             ppf_sample_1[1][i_sb] = .0; \
468             ppf_sample_1[2][i_sb] = .0; \
469         } \
470     } \
471 \
472     for ( ; i_sb < i_sblimit; i_sb++ ) \
473     { \
474         if ( pi_allocation_0[i_sb] ) \
475         { \
476             pp_requantization_0[i_sb] = &((p_requantization)[pi_allocation_0[i_sb]]); \
477             NeedBits( &p_adec->bit_stream, 4 ); \
478             i_need += 4; \
479             pi_scfsi_0[i_sb] = (int)(p_adec->bit_stream.fifo.buffer >> (32 - 2)); \
480             p_adec->bit_stream.fifo.buffer <<= 2; \
481             pi_scfsi_1[i_sb] = (int)(p_adec->bit_stream.fifo.buffer >> (32 - 2)); \
482             p_adec->bit_stream.fifo.buffer <<= 2; \
483             p_adec->bit_stream.fifo.i_available -= 4; \
484             i_dump += 4; \
485         } \
486         else \
487         { \
488             ppf_sample_0[0][i_sb] = .0; \
489             ppf_sample_0[1][i_sb] = .0; \
490             ppf_sample_0[2][i_sb] = .0; \
491             ppf_sample_1[0][i_sb] = .0; \
492             ppf_sample_1[1][i_sb] = .0; \
493             ppf_sample_1[2][i_sb] = .0; \
494         } \
495     }
496 /* #define MACRO */
497
498     if ( i_bitrate_per_channel_index <= 2 )
499     {
500         MACRO( p_requantization_cd )
501     }
502     else
503     {
504         MACRO( pp_requantization_ab[i_sb] )
505     }
506
507 #define SWITCH( pi_scfsi, pf_scalefactor_0, pf_scalefactor_1, pf_scalefactor_2 ) \
508     switch ( (pi_scfsi)[i_sb] ) \
509     { \
510         case 0: \
511             NeedBits( &p_adec->bit_stream, (3*6) ); \
512             i_need += 18; \
513             (pf_scalefactor_0)[i_sb] = pf_scalefactor[p_adec->bit_stream.fifo.buffer >> (32 - 6)]; \
514             p_adec->bit_stream.fifo.buffer <<= 6; \
515             (pf_scalefactor_1)[i_sb] = pf_scalefactor[p_adec->bit_stream.fifo.buffer >> (32 - 6)]; \
516             p_adec->bit_stream.fifo.buffer <<= 6; \
517             (pf_scalefactor_2)[i_sb] = pf_scalefactor[p_adec->bit_stream.fifo.buffer >> (32 - 6)]; \
518             p_adec->bit_stream.fifo.buffer <<= 6; \
519             p_adec->bit_stream.fifo.i_available -= (3*6); \
520             i_dump += 18; \
521             break; \
522 \
523         case 1: \
524             NeedBits( &p_adec->bit_stream, (2*6) ); \
525             i_need += 12; \
526             (pf_scalefactor_0)[i_sb] = \
527                 (pf_scalefactor_1)[i_sb] = pf_scalefactor[p_adec->bit_stream.fifo.buffer >> (32 - 6)]; \
528             p_adec->bit_stream.fifo.buffer <<= 6; \
529             (pf_scalefactor_2)[i_sb] = pf_scalefactor[p_adec->bit_stream.fifo.buffer >> (32 - 6)]; \
530             p_adec->bit_stream.fifo.buffer <<= 6; \
531             p_adec->bit_stream.fifo.i_available -= (2*6); \
532             i_dump += 12; \
533             break; \
534 \
535         case 2: \
536             NeedBits( &p_adec->bit_stream, (1*6) ); \
537             i_need += 6; \
538             (pf_scalefactor_0)[i_sb] = \
539                 (pf_scalefactor_1)[i_sb] = \
540                 (pf_scalefactor_2)[i_sb] = pf_scalefactor[p_adec->bit_stream.fifo.buffer >> (32 - 6)]; \
541             DumpBits( &p_adec->bit_stream, (1*6) ); \
542             i_dump += 6; \
543             break; \
544 \
545         case 3: \
546             NeedBits( &p_adec->bit_stream, (2*6) ); \
547             i_need += 12; \
548             (pf_scalefactor_0)[i_sb] = pf_scalefactor[p_adec->bit_stream.fifo.buffer >> (32 - 6)]; \
549             p_adec->bit_stream.fifo.buffer <<= 6; \
550             (pf_scalefactor_1)[i_sb] = \
551                 (pf_scalefactor_2)[i_sb] = pf_scalefactor[p_adec->bit_stream.fifo.buffer >> (32 - 6)]; \
552             p_adec->bit_stream.fifo.buffer <<= 6; \
553             p_adec->bit_stream.fifo.i_available -= (2*6); \
554             i_dump += 12; \
555             break; \
556     }
557 /* #define SWITCH */
558
559     for ( i_sb = 0; i_sb < i_bound; i_sb++ )
560     {
561         if ( pi_allocation_0[i_sb] )
562         {
563             SWITCH( pi_scfsi_0, pf_scalefactor_0_0, pf_scalefactor_0_1, pf_scalefactor_0_2 )
564         }
565         if ( pi_allocation_1[i_sb] )
566         {
567             SWITCH( pi_scfsi_1, pf_scalefactor_1_0, pf_scalefactor_1_1, pf_scalefactor_1_2 )
568         }
569     }
570     for ( ; i_sb < i_sblimit; i_sb++ )
571     {
572         if ( pi_allocation_0[i_sb] )
573         {
574             SWITCH( pi_scfsi_0, pf_scalefactor_0_0, pf_scalefactor_0_1, pf_scalefactor_0_2 )
575             SWITCH( pi_scfsi_1, pf_scalefactor_1_0, pf_scalefactor_1_1, pf_scalefactor_1_2 )
576         }
577     }
578     for ( ; i_sb < 32; i_sb++ )
579     {
580         ppf_sample_0[0][i_sb] = .0;
581         ppf_sample_0[1][i_sb] = .0;
582         ppf_sample_0[2][i_sb] = .0;
583         ppf_sample_1[0][i_sb] = .0;
584         ppf_sample_1[1][i_sb] = .0;
585         ppf_sample_1[2][i_sb] = .0;
586     }
587
588 #define NEXT_BUF \
589 /* fprintf(stderr, "%p\n", p_adec->p_aout_fifo->buffer); */ \
590 /* fprintf(stderr, "l_end_frame == %li, %p\n", l_end_frame, (aout_frame_t *)p_adec->p_aout_fifo->buffer + l_end_frame); */ \
591     p_s16 = ((aout_frame_t *)p_adec->p_aout_fifo->buffer)[ l_end_frame ]; \
592 /* fprintf(stderr, "p_s16 == %p\n", p_s16); */ \
593     l_end_frame += 1; \
594     l_end_frame &= AOUT_FIFO_SIZE;
595 /* #define NEXT_BUF */
596
597 #define GROUPTEST( pp_requantization, ppf_sample, pf_sf ) \
598     requantization = *((pp_requantization)[i_sb]); \
599     if ( requantization.pf_ungroup == NULL ) \
600     { \
601         NeedBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
602         i_need += requantization.i_bits_per_codeword; \
603         (ppf_sample)[0][i_sb] = (f_scalefactor_0 = (pf_sf)[i_sb]) * (requantization.f_slope * \
604             (p_adec->bit_stream.fifo.buffer >> (32 - requantization.i_bits_per_codeword)) + requantization.f_offset); \
605         DumpBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
606         i_dump += requantization.i_bits_per_codeword; \
607 \
608         NeedBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
609         i_need += requantization.i_bits_per_codeword; \
610         (ppf_sample)[1][i_sb] = f_scalefactor_0 * (requantization.f_slope * \
611             (p_adec->bit_stream.fifo.buffer >> (32 - requantization.i_bits_per_codeword)) + requantization.f_offset); \
612         DumpBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
613         i_dump += requantization.i_bits_per_codeword; \
614 \
615         NeedBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
616         i_need += requantization.i_bits_per_codeword; \
617         (ppf_sample)[2][i_sb] = f_scalefactor_0 * (requantization.f_slope * \
618             (p_adec->bit_stream.fifo.buffer >> (32 - requantization.i_bits_per_codeword)) + requantization.f_offset); \
619         DumpBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
620         i_dump += requantization.i_bits_per_codeword; \
621     } \
622     else \
623     { \
624         NeedBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
625         i_need += requantization.i_bits_per_codeword; \
626         pf_ungroup = requantization.pf_ungroup + 3 * \
627             (p_adec->bit_stream.fifo.buffer >> (32 - requantization.i_bits_per_codeword)); \
628         DumpBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
629         i_dump += requantization.i_bits_per_codeword; \
630         (ppf_sample)[0][i_sb] = (f_scalefactor_0 = (pf_sf)[i_sb]) * pf_ungroup[0]; \
631         (ppf_sample)[1][i_sb] = f_scalefactor_0 * pf_ungroup[1]; \
632         (ppf_sample)[2][i_sb] = f_scalefactor_0 * pf_ungroup[2]; \
633     }
634 /* #define GROUPTEST */
635
636 #define READ_SAMPLE_L2S( pf_scalefactor_0, pf_scalefactor_1, i_grlimit ) \
637     for ( ; i_gr < (i_grlimit); i_gr++ ) \
638     { \
639         for ( i_sb = 0; i_sb < i_bound; i_sb++ ) \
640         { \
641             if ( pi_allocation_0[i_sb] ) \
642             { \
643                 GROUPTEST( pp_requantization_0, ppf_sample_0, (pf_scalefactor_0) ) \
644             } \
645             if ( pi_allocation_1[i_sb] ) \
646             { \
647                 GROUPTEST( pp_requantization_1, ppf_sample_1, (pf_scalefactor_1) ) \
648             } \
649         } \
650         for ( ; i_sb < i_sblimit; i_sb++ ) \
651         { \
652             if ( pi_allocation_0[i_sb] ) \
653             { \
654                 requantization = *(pp_requantization_0[i_sb]); \
655                 if ( requantization.pf_ungroup == NULL ) \
656                 { \
657                     NeedBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
658                     i_need += requantization.i_bits_per_codeword; \
659                     ppf_sample_0[0][i_sb] = (f_scalefactor_0 = (pf_scalefactor_0)[i_sb]) * \
660                         (requantization.f_slope * (f_dummy = \
661                         (float)(p_adec->bit_stream.fifo.buffer >> (32 - requantization.i_bits_per_codeword))) + \
662                         requantization.f_offset); \
663                     DumpBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
664                     i_dump += requantization.i_bits_per_codeword; \
665                     ppf_sample_1[0][i_sb] = (f_scalefactor_1 = (pf_scalefactor_1)[i_sb]) * \
666                         (requantization.f_slope * f_dummy + requantization.f_offset); \
667 \
668                     NeedBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
669                     i_need += requantization.i_bits_per_codeword; \
670                     ppf_sample_0[1][i_sb] = f_scalefactor_0 * \
671                         (requantization.f_slope * (f_dummy = \
672                         (float)(p_adec->bit_stream.fifo.buffer >> (32 - requantization.i_bits_per_codeword))) + \
673                         requantization.f_offset); \
674                     DumpBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
675                     i_dump += requantization.i_bits_per_codeword; \
676                     ppf_sample_1[1][i_sb] = f_scalefactor_1 * \
677                         (requantization.f_slope * f_dummy + requantization.f_offset); \
678 \
679                     NeedBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
680                     i_need += requantization.i_bits_per_codeword; \
681                     ppf_sample_0[2][i_sb] = f_scalefactor_0 * \
682                         (requantization.f_slope * (f_dummy = \
683                         (float)(p_adec->bit_stream.fifo.buffer >> (32 - requantization.i_bits_per_codeword))) + \
684                         requantization.f_offset); \
685                     DumpBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
686                     i_dump += requantization.i_bits_per_codeword; \
687                     ppf_sample_1[2][i_sb] = f_scalefactor_1 * \
688                         (requantization.f_slope * f_dummy + requantization.f_offset); \
689                 } \
690                 else \
691                 { \
692                     NeedBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
693                     i_need += requantization.i_bits_per_codeword; \
694                     pf_ungroup = requantization.pf_ungroup + 3 * \
695                         (p_adec->bit_stream.fifo.buffer >> (32 - requantization.i_bits_per_codeword)); \
696                     DumpBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
697                     i_dump += requantization.i_bits_per_codeword; \
698 \
699                     ppf_sample_0[0][i_sb] = (f_scalefactor_0 = (pf_scalefactor_0)[i_sb]) * pf_ungroup[0]; \
700                     ppf_sample_0[1][i_sb] = f_scalefactor_0 * pf_ungroup[1]; \
701                     ppf_sample_0[2][i_sb] = f_scalefactor_0 * pf_ungroup[2]; \
702 \
703                     ppf_sample_1[0][i_sb] = (f_scalefactor_1 = (pf_scalefactor_1)[i_sb]) * pf_ungroup[0]; \
704                     ppf_sample_1[1][i_sb] = f_scalefactor_1 * pf_ungroup[1]; \
705                     ppf_sample_1[2][i_sb] = f_scalefactor_1 * pf_ungroup[2]; \
706                 } \
707             } \
708         } \
709 \
710 /* fprintf(stderr, "%p", p_s16); */ \
711         DCT32( ppf_sample_0[0], &p_adec->bank_0 ); \
712         PCM( &p_adec->bank_0, &p_s16, 2 ); \
713 /* fprintf(stderr, " %p", p_s16); */ \
714         p_s16 -= 63; \
715 /* fprintf(stderr, " %p\n", p_s16); */ \
716 \
717 /* fprintf(stderr, "%p", p_s16); */ \
718         DCT32( ppf_sample_1[0], &p_adec->bank_1 ); \
719         PCM( &p_adec->bank_1, &p_s16, 2 ); \
720 /* fprintf(stderr, " %p", p_s16); */ \
721         p_s16 -= 1; \
722 /* fprintf(stderr, " %p\n", p_s16); */ \
723 \
724 /* fprintf(stderr, "%p", p_s16); */ \
725         DCT32( ppf_sample_0[1], &p_adec->bank_0 ); \
726         PCM( &p_adec->bank_0, &p_s16, 2 ); \
727 /* fprintf(stderr, " %p", p_s16); */ \
728         p_s16 -= 63; \
729 /* fprintf(stderr, " %p\n", p_s16); */ \
730 \
731 /* fprintf(stderr, "%p", p_s16); */ \
732         DCT32( ppf_sample_1[1], &p_adec->bank_1 ); \
733         PCM( &p_adec->bank_1, &p_s16, 2 ); \
734 /* fprintf(stderr, " %p", p_s16); */ \
735         p_s16 -= 1; \
736 /* fprintf(stderr, " %p\n", p_s16); */ \
737 \
738 /* fprintf(stderr, "%p", p_s16); */ \
739         DCT32( ppf_sample_0[2], &p_adec->bank_0 ); \
740         PCM( &p_adec->bank_0, &p_s16, 2 ); \
741 /* fprintf(stderr, " %p", p_s16); */ \
742         p_s16 -= 63; \
743 /* fprintf(stderr, " %p\n", p_s16); */ \
744 \
745 /* fprintf(stderr, "%p", p_s16); */ \
746         DCT32( ppf_sample_1[2], &p_adec->bank_1 ); \
747         PCM( &p_adec->bank_1, &p_s16, 2 ); \
748 /* fprintf(stderr, " %p", p_s16); */ \
749         p_s16 -= 1; \
750 /* fprintf(stderr, " %p\n", p_s16); */ \
751     }
752 /* #define READ_SAMPLE_L2S */
753
754     l_end_frame = p_adec->p_aout_fifo->l_end_frame;
755     i_gr = 0;
756
757     NEXT_BUF
758     READ_SAMPLE_L2S( pf_scalefactor_0_0, pf_scalefactor_1_0, 2 )
759
760     NEXT_BUF
761     READ_SAMPLE_L2S( pf_scalefactor_0_0, pf_scalefactor_1_0, 4 )
762
763     NEXT_BUF
764     READ_SAMPLE_L2S( pf_scalefactor_0_1, pf_scalefactor_1_1, 6 )
765
766     NEXT_BUF
767     READ_SAMPLE_L2S( pf_scalefactor_0_1, pf_scalefactor_1_1, 8 )
768
769     NEXT_BUF
770     READ_SAMPLE_L2S( pf_scalefactor_0_2, pf_scalefactor_1_2, 10 )
771
772     NEXT_BUF
773     READ_SAMPLE_L2S( pf_scalefactor_0_2, pf_scalefactor_1_2, 12 )
774
775 //    fprintf(stderr, "adec debug: layer == %i, padding_bit == %i, sampling_frequency == %i, bitrate_index == %i\n",
776 //        (i_header & ADEC_HEADER_LAYER_MASK) >> ADEC_HEADER_LAYER_SHIFT,
777 //        (i_header & ADEC_HEADER_PADDING_BIT_MASK) >> ADEC_HEADER_PADDING_BIT_SHIFT,
778 //        (i_header & ADEC_HEADER_SAMPLING_FREQUENCY_MASK) >> ADEC_HEADER_SAMPLING_FREQUENCY_SHIFT,
779 //        (i_header & ADEC_HEADER_BITRATE_INDEX_MASK) >> ADEC_HEADER_BITRATE_INDEX_SHIFT);
780 //    fprintf(stderr, "adec debug: framesize == %i, i_need == %i, i_dump == %i\n",
781 //        pi_framesize[ 128 * ((i_header & ADEC_HEADER_LAYER_MASK) >> ADEC_HEADER_LAYER_SHIFT) +
782 //            64 * ((i_header & ADEC_HEADER_PADDING_BIT_MASK) >> ADEC_HEADER_PADDING_BIT_SHIFT) +
783 //            16 * ((i_header & ADEC_HEADER_SAMPLING_FREQUENCY_MASK) >> ADEC_HEADER_SAMPLING_FREQUENCY_SHIFT) +
784 //            1 * ((i_header & ADEC_HEADER_BITRATE_INDEX_MASK) >> ADEC_HEADER_BITRATE_INDEX_SHIFT) ],
785 //        i_need,
786 //        i_dump);
787     p_adec->bit_stream.fifo.buffer = 0;
788     p_adec->bit_stream.fifo.i_available = 0;
789     return( 6 );
790 }
791
792 /******************************************************************************
793  * InitThread : initialize an audio decoder thread
794  ******************************************************************************
795  * This function is called from RunThread and performs the second step of the
796  * initialization. It returns 0 on success.
797  ******************************************************************************/
798 static int InitThread( adec_thread_t * p_adec )
799 {
800     aout_fifo_t         aout_fifo;
801
802     intf_DbgMsg("adec debug: initializing audio decoder thread %p\n", p_adec);
803
804     /* Our first job is to initialize the bit stream structure with the
805      * beginning of the input stream */
806     pthread_mutex_lock( &p_adec->fifo.data_lock );
807     while ( DECODER_FIFO_ISEMPTY(p_adec->fifo) )
808     {
809         pthread_cond_wait( &p_adec->fifo.data_wait, &p_adec->fifo.data_lock );
810     }
811     p_adec->bit_stream.p_ts = DECODER_FIFO_START( p_adec->fifo )->p_first_ts;
812     p_adec->bit_stream.i_byte = p_adec->bit_stream.p_ts->i_payload_start;
813     pthread_mutex_unlock( &p_adec->fifo.data_lock );
814
815     /* Now we look for an audio frame header in the input stream */
816     if ( FindHeader(p_adec) )
817     {
818         return( -1 );                              /* b_die or b_error is set */
819     }
820
821     /*
822      * We have the header and all its informations : we must be able to create
823      * the audio output fifo.
824      */
825
826     /* Is the sound in mono mode or stereo mode ? */
827     if ( (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_MODE_MASK) == ADEC_HEADER_MODE_MASK )
828     {
829         intf_DbgMsg("adec debug: mode == mono\n");
830         aout_fifo.i_type = AOUT_ADEC_MONO_FIFO;
831         aout_fifo.b_stereo = 0;
832     }
833     else
834     {
835         intf_DbgMsg("adec debug: mode == stereo\n");
836         aout_fifo.i_type = AOUT_ADEC_STEREO_FIFO;
837         aout_fifo.b_stereo = 1;
838     }
839
840     /* Checking the sampling frequency */
841     switch ( (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_SAMPLING_FREQUENCY_MASK) \
842              >> ADEC_HEADER_SAMPLING_FREQUENCY_SHIFT )
843     {
844         case 0:
845             intf_DbgMsg("adec debug: sampling_frequency == 44100 Hz\n");
846             aout_fifo.l_rate = 44100;
847             break;
848
849         case 1:
850             intf_DbgMsg("adec debug: sampling_frequency == 48000 Hz\n");
851             aout_fifo.l_rate = 48000;
852             break;
853
854         case 2:
855             intf_DbgMsg("adec debug: sampling_frequency == 32000 Hz\n");
856             aout_fifo.l_rate = 32000;
857             break;
858
859         case 3:
860             intf_ErrMsg("adec error: can't create audio output fifo (sampling_frequency == `reserved')\n");
861             return( -1 );
862     }
863
864     /* Creating the audio output fifo */
865     if ( (p_adec->p_aout_fifo = aout_CreateFifo(p_adec->p_aout, &aout_fifo)) == NULL )
866     {
867         return( -1 );
868     }
869
870     intf_DbgMsg("adec debug: audio decoder thread %p initialized\n", p_adec);
871     return( 0 );
872 }
873
874 /******************************************************************************
875  * RunThread : audio decoder thread
876  ******************************************************************************
877  * Audio decoder thread. This function does only returns when the thread is
878  * terminated.
879  ******************************************************************************/
880 static void RunThread( adec_thread_t * p_adec )
881 {
882 //    static const int    pi_framesize[512] = ADEC_FRAME_SIZE;
883 //    int                 i_header;
884 //    int                 i_framesize;
885 //    int                 i_dummy;
886     mtime_t             adec_date = 0;
887
888     intf_DbgMsg("adec debug: running audio decoder thread (%p) (pid == %i)\n", p_adec, getpid());
889
890     /* Initializing the audio decoder thread */
891     if ( InitThread(p_adec) )
892     {
893         p_adec->b_error = 1;
894     }
895
896     /* Audio decoder thread's main loop */
897     while ( (!p_adec->b_die) && (!p_adec->b_error) )
898     {
899         switch ( (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_LAYER_MASK) >> ADEC_HEADER_LAYER_SHIFT )
900         {
901             case 0:
902                 intf_DbgMsg("adec debug: layer == 0 (reserved)\n");
903                 p_adec->bit_stream.fifo.buffer = 0;
904                 p_adec->bit_stream.fifo.i_available = 0;
905                 break;
906
907             case 1:
908                 p_adec->bit_stream.fifo.buffer = 0;
909                 p_adec->bit_stream.fifo.i_available = 0;
910                 break;
911
912             case 2:
913                 if ( (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_MODE_MASK) == ADEC_HEADER_MODE_MASK )
914                 {
915                     adec_Layer2_Mono( p_adec );
916                 }
917                 else
918                 {
919 //                    i_header = p_adec->bit_stream.fifo.buffer;
920 //                    i_framesize = pi_framesize[ 128*((i_header & ADEC_HEADER_LAYER_MASK) >> ADEC_HEADER_LAYER_SHIFT) +
921 //                        64*((i_header & ADEC_HEADER_PADDING_BIT_MASK) >> ADEC_HEADER_PADDING_BIT_SHIFT) +
922 //                        16*((i_header & ADEC_HEADER_SAMPLING_FREQUENCY_MASK) >> ADEC_HEADER_SAMPLING_FREQUENCY_SHIFT) +
923 //                        1*((i_header & ADEC_HEADER_BITRATE_INDEX_MASK) >> ADEC_HEADER_BITRATE_INDEX_SHIFT) ];
924 //                    for ( i_dummy = 0; i_dummy < i_framesize; i_dummy++ )
925 //                    {
926 //                        GetByte( &p_adec->bit_stream );
927 //                    }
928 //                    for ( i_dummy = 0; i_dummy < 512; i_dummy++ )
929 //                    {
930 //                        p_adec->bank_0.v1[ i_dummy ] = .0;
931 //                        p_adec->bank_1.v1[ i_dummy ] = .0;
932 //                        p_adec->bank_0.v2[ i_dummy ] = .0;
933 //                        p_adec->bank_1.v2[ i_dummy ] = .0;
934 //                    }
935
936                     pthread_mutex_lock( &p_adec->p_aout_fifo->data_lock );
937                     while ( (((p_adec->p_aout_fifo->l_end_frame + 6) - p_adec->p_aout_fifo->l_start_frame) & AOUT_FIFO_SIZE) < 6 ) /* !! */
938                     {
939                         pthread_cond_wait( &p_adec->p_aout_fifo->data_wait, &p_adec->p_aout_fifo->data_lock );
940                     }
941                     pthread_mutex_unlock( &p_adec->p_aout_fifo->data_lock );
942
943                     if ( adec_Layer2_Stereo(p_adec) )
944                     {
945                         pthread_mutex_lock( &p_adec->p_aout_fifo->data_lock );
946                         /* Frame 1 */
947                         p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] = adec_date;
948                         p_adec->p_aout_fifo->l_end_frame += 1;
949                         p_adec->p_aout_fifo->l_end_frame &= AOUT_FIFO_SIZE;
950                         /* Frame 2 */
951                         p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] = LAST_MDATE;
952                         p_adec->p_aout_fifo->l_end_frame += 1;
953                         p_adec->p_aout_fifo->l_end_frame &= AOUT_FIFO_SIZE;
954                         /* Frame 3 */
955                         p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] = LAST_MDATE;
956                         p_adec->p_aout_fifo->l_end_frame += 1;
957                         p_adec->p_aout_fifo->l_end_frame &= AOUT_FIFO_SIZE;
958                         /* Frame 4 */
959                         p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] = LAST_MDATE;
960                         p_adec->p_aout_fifo->l_end_frame += 1;
961                         p_adec->p_aout_fifo->l_end_frame &= AOUT_FIFO_SIZE;
962                         /* Frame 5 */
963                         p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] = LAST_MDATE;
964                         p_adec->p_aout_fifo->l_end_frame += 1;
965                         p_adec->p_aout_fifo->l_end_frame &= AOUT_FIFO_SIZE;
966                         /* Frame 6 */
967                         p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] = LAST_MDATE;
968                         p_adec->p_aout_fifo->l_end_frame += 1;
969                         p_adec->p_aout_fifo->l_end_frame &= AOUT_FIFO_SIZE;
970                         pthread_mutex_unlock( &p_adec->p_aout_fifo->data_lock );
971                         adec_date += 24000; /* !! */
972                     }
973                 }
974                 break;
975
976             case 3:
977                 if ( (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_MODE_MASK) == ADEC_HEADER_MODE_MASK )
978                 {
979                     adec_Layer1_Mono( p_adec );
980                 }
981                 else
982                 {
983                     adec_Layer1_Stereo( p_adec );
984                 }
985                 break;
986
987             default:
988                 intf_DbgMsg("adec debug: layer == %i (unknown)\n",
989                     (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_LAYER_MASK) >> ADEC_HEADER_LAYER_SHIFT);
990                 p_adec->bit_stream.fifo.buffer = 0;
991                 p_adec->bit_stream.fifo.i_available = 0;
992                 break;
993         }
994         FindHeader( p_adec );
995     }
996
997     /* If b_error is set, the audio decoder thread enters the error loop */
998     if ( p_adec->b_error )
999     {
1000         ErrorThread( p_adec );
1001     }
1002
1003     /* End of the audio decoder thread */
1004     EndThread( p_adec );
1005 }
1006
1007 /******************************************************************************
1008  * ErrorThread : audio decoder's RunThread() error loop
1009  ******************************************************************************
1010  * This function is called when an error occured during thread main's loop. The
1011  * thread can still receive feed, but must be ready to terminate as soon as
1012  * possible.
1013  ******************************************************************************/
1014 static void ErrorThread( adec_thread_t *p_adec )
1015 {
1016     /* We take the lock, because we are going to read/write the start/end
1017      * indexes of the decoder fifo */
1018     pthread_mutex_lock( &p_adec->fifo.data_lock );
1019
1020     /* Wait until a `die' order is sent */
1021     while( !p_adec->b_die )
1022     {
1023         /* Trash all received PES packets */
1024         while( !DECODER_FIFO_ISEMPTY(p_adec->fifo) )
1025         {
1026             input_NetlistFreePES( p_adec->bit_stream.p_input, DECODER_FIFO_START(p_adec->fifo) );
1027             DECODER_FIFO_INCSTART( p_adec->fifo );
1028 #ifdef DEBUG
1029 //            fprintf(stderr, "*");
1030 #endif
1031         }
1032
1033         /* Waiting for the input thread to put new PES packets in the fifo */
1034         pthread_cond_wait( &p_adec->fifo.data_wait, &p_adec->fifo.data_lock );
1035     }
1036
1037     /* We can release the lock before leaving */
1038     pthread_mutex_unlock( &p_adec->fifo.data_lock );
1039 }
1040
1041 /******************************************************************************
1042  * EndThread : audio decoder thread destruction
1043  ******************************************************************************
1044  * This function is called when the thread ends after a sucessfull 
1045  * initialization.
1046  ******************************************************************************/
1047 static void EndThread( adec_thread_t *p_adec )
1048 {
1049     intf_DbgMsg("adec debug: destroying audio decoder thread %p\n", p_adec);
1050
1051     /* If the audio output fifo was created, we destroy it */
1052     if ( p_adec->p_aout_fifo != NULL )
1053     {
1054         aout_DestroyFifo( p_adec->p_aout_fifo );
1055     }
1056     /* Destroy descriptor */
1057     free( p_adec );
1058
1059     intf_DbgMsg("adec debug: audio decoder thread %p destroyed\n", p_adec);
1060 }