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