1 /******************************************************************************
2 * audio_decoder.c: MPEG1 Layer I-II audio decoder thread
4 ******************************************************************************/
8 * - Optimiser les NeedBits() et les GetBits() du code là où c'est possible
11 /******************************************************************************
13 ******************************************************************************/
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" */
27 #include "vlc_thread.h"
28 #include "debug.h" /* "input_netlist.h" */
30 #include "intf_msg.h" /* intf_DbgMsg(), intf_ErrMsg() */
32 #include "input.h" /* pes_packet_t */
33 #include "input_netlist.h" /* input_NetlistFreePES() */
34 #include "decoder_fifo.h" /* DECODER_FIFO_(ISEMPTY|START|INCSTART)() */
36 #include "audio_output.h"
38 #include "audio_constants.h"
39 #include "audio_decoder.h"
40 #include "audio_math.h"
42 /******************************************************************************
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 );
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 );
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 );
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 )
70 adec_thread_t * p_adec;
72 intf_DbgMsg("adec debug: creating audio decoder thread\n");
74 /* Allocate the memory needed to store the thread's structure */
75 if ( (p_adec = (adec_thread_t *)malloc( sizeof(adec_thread_t) )) == NULL )
77 intf_ErrMsg("adec error: not enough memory for adec_CreateThread() to create the new thread\n");
82 * Initialize the thread properties
88 * Initialize the input properties
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;
103 * Initialize the decoder properties
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;
111 * Initialize the output properties
113 p_adec->p_aout = p_input->p_aout;
114 p_adec->p_aout_fifo = NULL;
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) )
119 intf_ErrMsg("adec error: can't spawn audio decoder thread\n");
124 intf_DbgMsg("adec debug: audio decoder thread (%p) created\n", p_adec);
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
136 ******************************************************************************/
137 void adec_DestroyThread( adec_thread_t * p_adec )
139 intf_DbgMsg("adec debug: requesting termination of audio decoder thread %p\n", p_adec);
141 /* Ask thread to kill itself */
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) );
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 );
153 /* Following functions are local */
155 /******************************************************************************
156 * FindHeader : parses an input stream until an audio frame header could be
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 )
164 while ( (!p_adec->b_die) && (!p_adec->b_error) )
166 NeedBits( &p_adec->bit_stream, 32 );
167 if ( (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_SYNCWORD_MASK) == ADEC_HEADER_SYNCWORD_MASK )
171 DumpBits( &p_adec->bit_stream, 8 );
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 ******************************************************************************/
186 /******************************************************************************
188 ******************************************************************************/
189 static __inline__ int adec_Layer1_Mono( adec_thread_t * p_adec )
191 p_adec->bit_stream.fifo.buffer = 0;
192 p_adec->bit_stream.fifo.i_available = 0;
196 /******************************************************************************
198 ******************************************************************************/
199 static __inline__ int adec_Layer1_Stereo( adec_thread_t * p_adec )
201 p_adec->bit_stream.fifo.buffer = 0;
202 p_adec->bit_stream.fifo.i_available = 0;
206 /******************************************************************************
208 ******************************************************************************/
209 static __inline__ int adec_Layer2_Mono( adec_thread_t * p_adec )
211 p_adec->bit_stream.fifo.buffer = 0;
212 p_adec->bit_stream.fifo.i_available = 0;
216 /******************************************************************************
218 ******************************************************************************/
219 static __inline__ int adec_Layer2_Stereo( adec_thread_t * p_adec )
221 typedef struct requantization_s
223 byte_t i_bits_per_codeword;
224 const float * pf_ungroup;
229 static const float pf_scalefactor[64] = ADEC_SCALE_FACTOR;
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 */
235 float f_scalefactor_0, f_scalefactor_1;
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;
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;
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;
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;
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];
270 int i_need = 0, i_dump = 0;
271 // static const int pi_framesize[512] = ADEC_FRAME_SIZE;
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 )
285 GetByte( &p_adec->bit_stream );
286 GetByte( &p_adec->bit_stream );
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 )
297 return( 0 ); /* the frame is invalid */
299 /* Find out the number of bits allocated */
300 pi_nbal = ppi_nbal[ (i_bitrate_per_channel_index <= 2) ? 0 : 1 ];
302 /* Find out the `bound' subband (see ISO/IEC 11172-3 2.4.2.3) */
305 i_bound = (int)(((i_header & ADEC_HEADER_MODE_EXTENSION_MASK) >> (ADEC_HEADER_MODE_EXTENSION_SHIFT - 2)) + 4);
306 if ( i_bound > i_sblimit )
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++ )
319 i_2nbal = 2 * (i_nbal = (int)pi_nbal[ i_sb ]);
320 NeedBits( &p_adec->bit_stream, 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;
329 for ( ; i_sb < i_sblimit; i_sb++ )
331 i_nbal = (int)pi_nbal[ i_sb ];
332 NeedBits( &p_adec->bit_stream, 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 );
339 #define MACRO( p_requantization ) \
340 for ( i_sb = 0; i_sb < i_bound; i_sb++ ) \
342 if ( pi_allocation_0[i_sb] ) \
344 pp_requantization_0[i_sb] = &((p_requantization)[pi_allocation_0[i_sb]]); \
345 NeedBits( &p_adec->bit_stream, 2 ); \
347 pi_scfsi_0[i_sb] = (int)(p_adec->bit_stream.fifo.buffer >> (32 - 2)); \
348 DumpBits( &p_adec->bit_stream, 2 ); \
353 ppf_sample_0[0][i_sb] = .0; \
354 ppf_sample_0[1][i_sb] = .0; \
355 ppf_sample_0[2][i_sb] = .0; \
358 if ( pi_allocation_1[i_sb] ) \
360 pp_requantization_1[i_sb] = &((p_requantization)[pi_allocation_1[i_sb]]); \
361 NeedBits( &p_adec->bit_stream, 2 ); \
363 pi_scfsi_1[i_sb] = (int)(p_adec->bit_stream.fifo.buffer >> (32 - 2)); \
364 DumpBits( &p_adec->bit_stream, 2 ); \
369 ppf_sample_1[0][i_sb] = .0; \
370 ppf_sample_1[1][i_sb] = .0; \
371 ppf_sample_1[2][i_sb] = .0; \
375 for ( ; i_sb < i_sblimit; i_sb++ ) \
377 if ( pi_allocation_0[i_sb] ) \
379 pp_requantization_0[i_sb] = &((p_requantization)[pi_allocation_0[i_sb]]); \
380 NeedBits( &p_adec->bit_stream, 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; \
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; \
401 if ( i_bitrate_per_channel_index <= 2 )
403 MACRO( p_requantization_cd )
407 MACRO( pp_requantization_ab[i_sb] )
410 #define SWITCH( pi_scfsi, pf_scalefactor_0, pf_scalefactor_1, pf_scalefactor_2 ) \
411 switch ( (pi_scfsi)[i_sb] ) \
414 NeedBits( &p_adec->bit_stream, (3*6) ); \
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); \
427 NeedBits( &p_adec->bit_stream, (2*6) ); \
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); \
439 NeedBits( &p_adec->bit_stream, (1*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) ); \
449 NeedBits( &p_adec->bit_stream, (2*6) ); \
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); \
462 for ( i_sb = 0; i_sb < i_bound; i_sb++ )
464 if ( pi_allocation_0[i_sb] )
466 SWITCH( pi_scfsi_0, pf_scalefactor_0_0, pf_scalefactor_0_1, pf_scalefactor_0_2 )
468 if ( pi_allocation_1[i_sb] )
470 SWITCH( pi_scfsi_1, pf_scalefactor_1_0, pf_scalefactor_1_1, pf_scalefactor_1_2 )
473 for ( ; i_sb < i_sblimit; i_sb++ )
475 if ( pi_allocation_0[i_sb] )
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 )
481 for ( ; i_sb < 32; i_sb++ )
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;
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); */ \
497 l_end_frame &= AOUT_FIFO_SIZE;
498 /* #define NEXT_BUF */
500 #define GROUPTEST( pp_requantization, ppf_sample, pf_sf ) \
501 requantization = *((pp_requantization)[i_sb]); \
502 if ( requantization.pf_ungroup == NULL ) \
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; \
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; \
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; \
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]; \
537 /* #define GROUPTEST */
539 #define READ_SAMPLE_L2S( pf_scalefactor_0, pf_scalefactor_1, i_grlimit ) \
540 for ( ; i_gr < (i_grlimit); i_gr++ ) \
542 for ( i_sb = 0; i_sb < i_bound; i_sb++ ) \
544 if ( pi_allocation_0[i_sb] ) \
546 GROUPTEST( pp_requantization_0, ppf_sample_0, (pf_scalefactor_0) ) \
548 if ( pi_allocation_1[i_sb] ) \
550 GROUPTEST( pp_requantization_1, ppf_sample_1, (pf_scalefactor_1) ) \
553 for ( ; i_sb < i_sblimit; i_sb++ ) \
555 if ( pi_allocation_0[i_sb] ) \
557 requantization = *(pp_requantization_0[i_sb]); \
558 if ( requantization.pf_ungroup == NULL ) \
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); \
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); \
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); \
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; \
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]; \
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]; \
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); */ \
618 /* fprintf(stderr, " %p\n", p_s16); */ \
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); */ \
625 /* fprintf(stderr, " %p\n", p_s16); */ \
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); */ \
632 /* fprintf(stderr, " %p\n", p_s16); */ \
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); */ \
639 /* fprintf(stderr, " %p\n", p_s16); */ \
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); */ \
646 /* fprintf(stderr, " %p\n", p_s16); */ \
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); */ \
653 /* fprintf(stderr, " %p\n", p_s16); */ \
655 /* #define READ_SAMPLE_L2S */
657 l_end_frame = p_adec->p_aout_fifo->l_end_frame;
661 READ_SAMPLE_L2S( pf_scalefactor_0_0, pf_scalefactor_1_0, 2 )
664 READ_SAMPLE_L2S( pf_scalefactor_0_0, pf_scalefactor_1_0, 4 )
667 READ_SAMPLE_L2S( pf_scalefactor_0_1, pf_scalefactor_1_1, 6 )
670 READ_SAMPLE_L2S( pf_scalefactor_0_1, pf_scalefactor_1_1, 8 )
673 READ_SAMPLE_L2S( pf_scalefactor_0_2, pf_scalefactor_1_2, 10 )
676 READ_SAMPLE_L2S( pf_scalefactor_0_2, pf_scalefactor_1_2, 12 )
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) ],
690 p_adec->bit_stream.fifo.buffer = 0;
691 p_adec->bit_stream.fifo.i_available = 0;
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 )
703 aout_fifo_t aout_fifo;
705 intf_DbgMsg("adec debug: initializing audio decoder thread %p\n", p_adec);
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) )
712 vlc_cond_wait( &p_adec->fifo.data_wait, &p_adec->fifo.data_lock );
714 p_adec->bit_stream.p_ts = DECODER_FIFO_START( p_adec->fifo )->p_first_ts;
715 p_adec->bit_stream.i_byte = p_adec->bit_stream.p_ts->i_payload_start;
716 vlc_mutex_unlock( &p_adec->fifo.data_lock );
718 /* Now we look for an audio frame header in the input stream */
719 if ( FindHeader(p_adec) )
721 return( -1 ); /* b_die or b_error is set */
725 * We have the header and all its informations : we must be able to create
726 * the audio output fifo.
729 /* Is the sound in mono mode or stereo mode ? */
730 if ( (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_MODE_MASK) == ADEC_HEADER_MODE_MASK )
732 intf_DbgMsg("adec debug: mode == mono\n");
733 aout_fifo.i_type = AOUT_ADEC_MONO_FIFO;
734 aout_fifo.b_stereo = 0;
738 intf_DbgMsg("adec debug: mode == stereo\n");
739 aout_fifo.i_type = AOUT_ADEC_STEREO_FIFO;
740 aout_fifo.b_stereo = 1;
743 /* Checking the sampling frequency */
744 switch ( (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_SAMPLING_FREQUENCY_MASK) \
745 >> ADEC_HEADER_SAMPLING_FREQUENCY_SHIFT )
748 intf_DbgMsg("adec debug: sampling_frequency == 44100 Hz\n");
749 aout_fifo.l_rate = 44100;
753 intf_DbgMsg("adec debug: sampling_frequency == 48000 Hz\n");
754 aout_fifo.l_rate = 48000;
758 intf_DbgMsg("adec debug: sampling_frequency == 32000 Hz\n");
759 aout_fifo.l_rate = 32000;
763 intf_ErrMsg("adec error: can't create audio output fifo (sampling_frequency == `reserved')\n");
767 aout_fifo.l_frame_size = ADEC_FRAME_SIZE;
769 /* Creating the audio output fifo */
770 if ( (p_adec->p_aout_fifo = aout_CreateFifo(p_adec->p_aout, &aout_fifo)) == NULL )
775 intf_DbgMsg("adec debug: audio decoder thread %p initialized\n", p_adec);
779 /******************************************************************************
780 * RunThread : audio decoder thread
781 ******************************************************************************
782 * Audio decoder thread. This function does only returns when the thread is
784 ******************************************************************************/
785 static void RunThread( adec_thread_t * p_adec )
787 // static const int pi_framesize[512] = ADEC_FRAME_SIZE;
792 intf_DbgMsg("adec debug: running audio decoder thread (%p) (pid == %i)\n", p_adec, getpid());
794 /* Initializing the audio decoder thread */
795 if ( InitThread(p_adec) )
800 /* Audio decoder thread's main loop */
801 while ( (!p_adec->b_die) && (!p_adec->b_error) )
803 switch ( (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_LAYER_MASK) >> ADEC_HEADER_LAYER_SHIFT )
807 intf_DbgMsg("adec debug: layer == 0 (reserved)\n");
808 p_adec->bit_stream.fifo.buffer = 0;
809 p_adec->bit_stream.fifo.i_available = 0;
814 p_adec->bit_stream.fifo.buffer = 0;
815 p_adec->bit_stream.fifo.i_available = 0;
820 if ( (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_MODE_MASK) == ADEC_HEADER_MODE_MASK )
822 adec_Layer2_Mono( p_adec );
826 /* Waiting until there is enough free space in the audio output fifo
827 * in order to store the new decoded frames */
828 vlc_mutex_lock( &p_adec->p_aout_fifo->data_lock );
829 /* adec_Layer2_Stereo() produces 6 output frames (2*1152/384)...
830 * If these 6 frames were recorded in the audio output fifo, the
831 * l_end_frame index would be incremented 6 times. But, if after
832 * this operation the audio output fifo contains less than 6 frames,
833 * it would mean that we had not enough room to store the 6 frames :-P */
834 while ( (((p_adec->p_aout_fifo->l_end_frame + 6) - p_adec->p_aout_fifo->l_start_frame) & AOUT_FIFO_SIZE) < 6 ) /* !! */
836 vlc_cond_wait( &p_adec->p_aout_fifo->data_wait, &p_adec->p_aout_fifo->data_lock );
838 if ( DECODER_FIFO_START(p_adec->fifo)->b_has_pts )
840 p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] = DECODER_FIFO_START(p_adec->fifo)->i_pts;
841 DECODER_FIFO_START(p_adec->fifo)->b_has_pts = 0;
845 p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] = LAST_MDATE;
847 vlc_mutex_unlock( &p_adec->p_aout_fifo->data_lock );
849 /* Decoding the frames */
850 if ( adec_Layer2_Stereo(p_adec) )
852 vlc_mutex_lock( &p_adec->p_aout_fifo->data_lock );
854 p_adec->p_aout_fifo->l_end_frame = (p_adec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
856 p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] = LAST_MDATE;
857 p_adec->p_aout_fifo->l_end_frame = (p_adec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
859 p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] = LAST_MDATE;
860 p_adec->p_aout_fifo->l_end_frame = (p_adec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
862 p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] = LAST_MDATE;
863 p_adec->p_aout_fifo->l_end_frame = (p_adec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
865 p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] = LAST_MDATE;
866 p_adec->p_aout_fifo->l_end_frame = (p_adec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
868 p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] = LAST_MDATE;
869 p_adec->p_aout_fifo->l_end_frame = (p_adec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
870 vlc_mutex_unlock( &p_adec->p_aout_fifo->data_lock );
877 if ( (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_MODE_MASK) == ADEC_HEADER_MODE_MASK )
879 adec_Layer1_Mono( p_adec );
883 adec_Layer1_Stereo( p_adec );
888 intf_DbgMsg("adec debug: layer == %i (unknown)\n",
889 (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_LAYER_MASK) >> ADEC_HEADER_LAYER_SHIFT);
890 p_adec->bit_stream.fifo.buffer = 0;
891 p_adec->bit_stream.fifo.i_available = 0;
894 FindHeader( p_adec );
897 /* If b_error is set, the audio decoder thread enters the error loop */
898 if ( p_adec->b_error )
900 ErrorThread( p_adec );
903 /* End of the audio decoder thread */
907 /******************************************************************************
908 * ErrorThread : audio decoder's RunThread() error loop
909 ******************************************************************************
910 * This function is called when an error occured during thread main's loop. The
911 * thread can still receive feed, but must be ready to terminate as soon as
913 ******************************************************************************/
914 static void ErrorThread( adec_thread_t *p_adec )
916 /* We take the lock, because we are going to read/write the start/end
917 * indexes of the decoder fifo */
918 vlc_mutex_lock( &p_adec->fifo.data_lock );
920 /* Wait until a `die' order is sent */
921 while( !p_adec->b_die )
923 /* Trash all received PES packets */
924 while( !DECODER_FIFO_ISEMPTY(p_adec->fifo) )
926 input_NetlistFreePES( p_adec->bit_stream.p_input, DECODER_FIFO_START(p_adec->fifo) );
927 DECODER_FIFO_INCSTART( p_adec->fifo );
930 /* Waiting for the input thread to put new PES packets in the fifo */
931 vlc_cond_wait( &p_adec->fifo.data_wait, &p_adec->fifo.data_lock );
934 /* We can release the lock before leaving */
935 vlc_mutex_unlock( &p_adec->fifo.data_lock );
938 /******************************************************************************
939 * EndThread : audio decoder thread destruction
940 ******************************************************************************
941 * This function is called when the thread ends after a sucessfull
943 ******************************************************************************/
944 static void EndThread( adec_thread_t *p_adec )
946 intf_DbgMsg("adec debug: destroying audio decoder thread %p\n", p_adec);
948 /* If the audio output fifo was created, we destroy it */
949 if ( p_adec->p_aout_fifo != NULL )
951 aout_DestroyFifo( p_adec->p_aout_fifo );
953 /* Destroy descriptor */
956 intf_DbgMsg("adec debug: audio decoder thread %p destroyed\n", p_adec);