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