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