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