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