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