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