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 ******************************************************************************/
17 #include <stdio.h> /* "intf_msg.h" */
18 #include <stdlib.h> /* malloc(), free() */
19 #include <netinet/in.h> /* ntohl() */
20 #include <sys/soundcard.h> /* "audio_output.h" */
21 #include <sys/uio.h> /* "input.h" */
26 #include "debug.h" /* "input_netlist.h" */
28 #include "intf_msg.h" /* intf_DbgMsg(), intf_ErrMsg() */
30 #include "input.h" /* pes_packet_t */
31 #include "input_netlist.h" /* input_NetlistFreePES() */
32 #include "decoder_fifo.h" /* DECODER_FIFO_(ISEMPTY|START|INCSTART)() */
34 #include "audio_output.h"
36 #include "audio_constants.h"
37 #include "audio_decoder.h"
38 #include "audio_math.h"
40 /******************************************************************************
42 ******************************************************************************/
43 static int InitThread ( adec_thread_t * p_adec );
44 static void RunThread ( adec_thread_t * p_adec );
45 static void ErrorThread ( adec_thread_t * p_adec );
46 static void EndThread ( adec_thread_t * p_adec );
48 static int adec_Layer1_Mono ( adec_thread_t * p_adec );
49 static int adec_Layer1_Stereo ( adec_thread_t * p_adec );
50 static int adec_Layer2_Mono ( adec_thread_t * p_adec );
51 static int adec_Layer2_Stereo ( adec_thread_t * p_adec );
53 static byte_t GetByte ( bit_stream_t * p_bit_stream );
54 static void NeedBits ( bit_stream_t * p_bit_stream, int i_bits );
55 static void DumpBits ( bit_stream_t * p_bit_stream, int i_bits );
56 static int FindHeader ( adec_thread_t * p_adec );
58 /******************************************************************************
59 * adec_CreateThread: creates an audio decoder thread
60 ******************************************************************************
61 * This function creates a new audio decoder thread, and returns a pointer to
62 * its description. On error, it returns NULL.
63 ******************************************************************************/
64 adec_thread_t * adec_CreateThread( input_thread_t * p_input )
66 adec_thread_t * p_adec;
68 intf_DbgMsg("adec debug: creating audio decoder thread\n");
70 /* Allocate the memory needed to store the thread's structure */
71 if ( (p_adec = (adec_thread_t *)malloc( sizeof(adec_thread_t) )) == NULL )
73 intf_ErrMsg("adec error: not enough memory for adec_CreateThread() to create the new thread\n");
78 * Initialize the thread properties
84 * Initialize the input properties
86 /* Initialize the decoder fifo's data lock and conditional variable and set
87 * its buffer as empty */
88 pthread_mutex_init( &p_adec->fifo.data_lock, NULL );
89 pthread_cond_init( &p_adec->fifo.data_wait, NULL );
90 p_adec->fifo.i_start = 0;
91 p_adec->fifo.i_end = 0;
92 /* Initialize the bit stream structure */
93 p_adec->bit_stream.p_input = p_input;
94 p_adec->bit_stream.p_decoder_fifo = &p_adec->fifo;
95 p_adec->bit_stream.fifo.buffer = 0;
96 p_adec->bit_stream.fifo.i_available = 0;
99 * Initialize the decoder properties
101 p_adec->bank_0.actual = p_adec->bank_0.v1;
102 p_adec->bank_0.pos = 0;
103 p_adec->bank_1.actual = p_adec->bank_1.v1;
104 p_adec->bank_1.pos = 0;
107 * Initialize the output properties
109 p_adec->p_aout = p_input->p_aout;
110 p_adec->p_aout_fifo = NULL;
112 /* Spawn the audio decoder thread */
113 if ( pthread_create(&p_adec->thread_id, NULL, (void *)RunThread, (void *)p_adec) )
115 intf_ErrMsg("adec error: can't spawn audio decoder thread\n");
120 intf_DbgMsg("adec debug: audio decoder thread (%p) created\n", p_adec);
124 /******************************************************************************
125 * adec_DestroyThread: destroys an audio decoder thread
126 ******************************************************************************
127 * This function asks an audio decoder thread to terminate. This function has
128 * not to wait until the decoder thread has really died, because the killer (ie
129 * this function's caller) is the input thread, that's why we are sure that no
130 * other thread will try to access to this thread's descriptor after its
132 ******************************************************************************/
133 void adec_DestroyThread( adec_thread_t * p_adec )
135 intf_DbgMsg("adec debug: requesting termination of audio decoder thread %p\n", p_adec);
137 /* Ask thread to kill itself */
140 /* Remove this as soon as the "status" flag is implemented */
141 pthread_join( p_adec->thread_id, NULL ); /* wait until it's done */
144 /* Following functions are local */
146 /******************************************************************************
147 * FindHeader : parses an input stream until an audio frame header could be
149 ******************************************************************************
150 * When this function returns successfully, the header can be found in the
151 * buffer of the bit stream fifo.
152 ******************************************************************************/
153 static int FindHeader( adec_thread_t * p_adec )
155 while ( (!p_adec->b_die) && (!p_adec->b_error) )
157 NeedBits( &p_adec->bit_stream, 32 );
158 if ( (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_SYNCWORD_MASK) == ADEC_HEADER_SYNCWORD_MASK )
161 // fprintf(stderr, "H");
166 // fprintf(stderr, "!");
168 DumpBits( &p_adec->bit_stream, 8 );
174 /******************************************************************************
175 * adec_Layer`L'_`M': decodes an mpeg 1, layer `L', mode `M', audio frame
176 ******************************************************************************
177 * These functions decode the audio frame which has already its header loaded
178 * in the i_header member of the audio decoder thread structure and its first
179 * byte of data described by the bit stream structure of the audio decoder
180 * thread (there is no bit available in the bit buffer yet)
181 ******************************************************************************/
183 /******************************************************************************
185 ******************************************************************************/
186 static __inline__ int adec_Layer1_Mono( adec_thread_t * p_adec )
188 p_adec->bit_stream.fifo.buffer = 0;
189 p_adec->bit_stream.fifo.i_available = 0;
193 /******************************************************************************
195 ******************************************************************************/
196 static __inline__ int adec_Layer1_Stereo( adec_thread_t * p_adec )
198 p_adec->bit_stream.fifo.buffer = 0;
199 p_adec->bit_stream.fifo.i_available = 0;
203 /******************************************************************************
205 ******************************************************************************/
206 static __inline__ int adec_Layer2_Mono( adec_thread_t * p_adec )
208 p_adec->bit_stream.fifo.buffer = 0;
209 p_adec->bit_stream.fifo.i_available = 0;
213 /******************************************************************************
215 ******************************************************************************/
216 static __inline__ int adec_Layer2_Stereo( adec_thread_t * p_adec )
218 typedef struct requantization_s
220 byte_t i_bits_per_codeword;
221 const float * pf_ungroup;
226 static const float pf_scalefactor[64] = ADEC_SCALE_FACTOR;
229 static int i_sampling_frequency, i_mode, i_bound;
230 static int pi_allocation_0[32], pi_allocation_1[32]; /* see ISO/IEC 11172-3 2.4.1.6 */
232 float f_scalefactor_0, f_scalefactor_1;
234 static const byte_t ppi_bitrate_per_channel_index[4][15] = ADEC_LAYER2_BITRATE_PER_CHANNEL_INDEX;
235 static const byte_t ppi_sblimit[3][11] = ADEC_LAYER2_SBLIMIT;
236 static const byte_t ppi_nbal[2][32] = ADEC_LAYER2_NBAL;
238 static const float pf_ungroup3[3*3*3 * 3] = ADEC_LAYER2_UNGROUP3;
239 static const float pf_ungroup5[5*5*5 * 3] = ADEC_LAYER2_UNGROUP5;
240 static const float pf_ungroup9[9*9*9 * 3] = ADEC_LAYER2_UNGROUP9;
242 static const requantization_t p_requantization_cd[16] = ADEC_LAYER2_REQUANTIZATION_CD;
243 static const requantization_t p_requantization_ab1[16] = ADEC_LAYER2_REQUANTIZATION_AB1;
244 static const requantization_t p_requantization_ab2[16] = ADEC_LAYER2_REQUANTIZATION_AB2;
245 static const requantization_t p_requantization_ab3[16] = ADEC_LAYER2_REQUANTIZATION_AB3;
246 static const requantization_t p_requantization_ab4[16] = ADEC_LAYER2_REQUANTIZATION_AB4;
247 static const requantization_t * pp_requantization_ab[30] = ADEC_LAYER2_REQUANTIZATION_AB;
249 static int i_sblimit, i_bitrate_per_channel_index;
250 static int pi_scfsi_0[30], pi_scfsi_1[30];
251 static const byte_t * pi_nbal;
252 static float ppf_sample_0[3][32], ppf_sample_1[3][32];
253 static const requantization_t * pp_requantization_0[30];
254 static const requantization_t * pp_requantization_1[30];
255 static requantization_t requantization;
256 static const float * pf_ungroup;
258 static float pf_scalefactor_0_0[30], pf_scalefactor_0_1[30], pf_scalefactor_0_2[30];
259 static float pf_scalefactor_1_0[30], pf_scalefactor_1_1[30], pf_scalefactor_1_2[30];
267 int i_need = 0, i_dump = 0;
268 // static const int pi_framesize[512] = ADEC_FRAME_SIZE;
270 /* Read the audio frame header and flush the bit buffer */
271 i_header = p_adec->bit_stream.fifo.buffer;
272 p_adec->bit_stream.fifo.buffer = 0;
273 p_adec->bit_stream.fifo.i_available = 0;
274 /* Read the sampling frequency (see ISO/IEC 11172-3 2.4.2.3) */
275 i_sampling_frequency = (int)((i_header & ADEC_HEADER_SAMPLING_FREQUENCY_MASK)
276 >> ADEC_HEADER_SAMPLING_FREQUENCY_SHIFT);
277 /* Read the mode (see ISO/IEC 11172-3 2.4.2.3) */
278 i_mode = (int)((i_header & ADEC_HEADER_MODE_MASK) >> ADEC_HEADER_MODE_SHIFT);
279 /* If a CRC can be found in the frame, get rid of it */
280 if ( (i_header & ADEC_HEADER_PROTECTION_BIT_MASK) == 0 )
282 GetByte( &p_adec->bit_stream );
283 GetByte( &p_adec->bit_stream );
286 /* Find out the bitrate per channel index */
287 i_bitrate_per_channel_index = (int)ppi_bitrate_per_channel_index[i_mode]
288 [(i_header & ADEC_HEADER_BITRATE_INDEX_MASK) >> ADEC_HEADER_BITRATE_INDEX_SHIFT];
289 /* Find out the number of subbands */
290 i_sblimit = (int)ppi_sblimit[i_sampling_frequency][i_bitrate_per_channel_index];
291 /* Check if the frame is valid or not */
292 if ( i_sblimit == 0 )
294 return( 0 ); /* the frame is invalid */
296 /* Find out the number of bits allocated */
297 pi_nbal = ppi_nbal[ (i_bitrate_per_channel_index <= 2) ? 0 : 1 ];
299 /* Find out the `bound' subband (see ISO/IEC 11172-3 2.4.2.3) */
302 i_bound = (int)(((i_header & ADEC_HEADER_MODE_EXTENSION_MASK) >> (ADEC_HEADER_MODE_EXTENSION_SHIFT - 2)) + 4);
303 if ( i_bound > i_sblimit )
313 /* Read the allocation information (see ISO/IEC 11172-3 2.4.1.6) */
314 for ( i_sb = 0; i_sb < i_bound; i_sb++ )
316 i_2nbal = 2 * (i_nbal = (int)pi_nbal[ i_sb ]);
317 NeedBits( &p_adec->bit_stream, i_2nbal );
319 pi_allocation_0[ i_sb ] = (int)(p_adec->bit_stream.fifo.buffer >> (32 - i_nbal));
320 p_adec->bit_stream.fifo.buffer <<= i_nbal;
321 pi_allocation_1[ i_sb ] = (int)(p_adec->bit_stream.fifo.buffer >> (32 - i_nbal));
322 p_adec->bit_stream.fifo.buffer <<= i_nbal;
323 p_adec->bit_stream.fifo.i_available -= i_2nbal;
326 for ( ; i_sb < i_sblimit; i_sb++ )
328 i_nbal = (int)pi_nbal[ i_sb ];
329 NeedBits( &p_adec->bit_stream, i_nbal );
331 pi_allocation_0[ i_sb ] = (int)(p_adec->bit_stream.fifo.buffer >> (32 - i_nbal));
332 DumpBits( &p_adec->bit_stream, i_nbal );
336 #define MACRO( p_requantization ) \
337 for ( i_sb = 0; i_sb < i_bound; i_sb++ ) \
339 if ( pi_allocation_0[i_sb] ) \
341 pp_requantization_0[i_sb] = &((p_requantization)[pi_allocation_0[i_sb]]); \
342 NeedBits( &p_adec->bit_stream, 2 ); \
344 pi_scfsi_0[i_sb] = (int)(p_adec->bit_stream.fifo.buffer >> (32 - 2)); \
345 DumpBits( &p_adec->bit_stream, 2 ); \
350 ppf_sample_0[0][i_sb] = .0; \
351 ppf_sample_0[1][i_sb] = .0; \
352 ppf_sample_0[2][i_sb] = .0; \
355 if ( pi_allocation_1[i_sb] ) \
357 pp_requantization_1[i_sb] = &((p_requantization)[pi_allocation_1[i_sb]]); \
358 NeedBits( &p_adec->bit_stream, 2 ); \
360 pi_scfsi_1[i_sb] = (int)(p_adec->bit_stream.fifo.buffer >> (32 - 2)); \
361 DumpBits( &p_adec->bit_stream, 2 ); \
366 ppf_sample_1[0][i_sb] = .0; \
367 ppf_sample_1[1][i_sb] = .0; \
368 ppf_sample_1[2][i_sb] = .0; \
372 for ( ; i_sb < i_sblimit; i_sb++ ) \
374 if ( pi_allocation_0[i_sb] ) \
376 pp_requantization_0[i_sb] = &((p_requantization)[pi_allocation_0[i_sb]]); \
377 NeedBits( &p_adec->bit_stream, 4 ); \
379 pi_scfsi_0[i_sb] = (int)(p_adec->bit_stream.fifo.buffer >> (32 - 2)); \
380 p_adec->bit_stream.fifo.buffer <<= 2; \
381 pi_scfsi_1[i_sb] = (int)(p_adec->bit_stream.fifo.buffer >> (32 - 2)); \
382 p_adec->bit_stream.fifo.buffer <<= 2; \
383 p_adec->bit_stream.fifo.i_available -= 4; \
388 ppf_sample_0[0][i_sb] = .0; \
389 ppf_sample_0[1][i_sb] = .0; \
390 ppf_sample_0[2][i_sb] = .0; \
391 ppf_sample_1[0][i_sb] = .0; \
392 ppf_sample_1[1][i_sb] = .0; \
393 ppf_sample_1[2][i_sb] = .0; \
398 if ( i_bitrate_per_channel_index <= 2 )
400 MACRO( p_requantization_cd )
404 MACRO( pp_requantization_ab[i_sb] )
407 #define SWITCH( pi_scfsi, pf_scalefactor_0, pf_scalefactor_1, pf_scalefactor_2 ) \
408 switch ( (pi_scfsi)[i_sb] ) \
411 NeedBits( &p_adec->bit_stream, (3*6) ); \
413 (pf_scalefactor_0)[i_sb] = pf_scalefactor[p_adec->bit_stream.fifo.buffer >> (32 - 6)]; \
414 p_adec->bit_stream.fifo.buffer <<= 6; \
415 (pf_scalefactor_1)[i_sb] = pf_scalefactor[p_adec->bit_stream.fifo.buffer >> (32 - 6)]; \
416 p_adec->bit_stream.fifo.buffer <<= 6; \
417 (pf_scalefactor_2)[i_sb] = pf_scalefactor[p_adec->bit_stream.fifo.buffer >> (32 - 6)]; \
418 p_adec->bit_stream.fifo.buffer <<= 6; \
419 p_adec->bit_stream.fifo.i_available -= (3*6); \
424 NeedBits( &p_adec->bit_stream, (2*6) ); \
426 (pf_scalefactor_0)[i_sb] = \
427 (pf_scalefactor_1)[i_sb] = pf_scalefactor[p_adec->bit_stream.fifo.buffer >> (32 - 6)]; \
428 p_adec->bit_stream.fifo.buffer <<= 6; \
429 (pf_scalefactor_2)[i_sb] = pf_scalefactor[p_adec->bit_stream.fifo.buffer >> (32 - 6)]; \
430 p_adec->bit_stream.fifo.buffer <<= 6; \
431 p_adec->bit_stream.fifo.i_available -= (2*6); \
436 NeedBits( &p_adec->bit_stream, (1*6) ); \
438 (pf_scalefactor_0)[i_sb] = \
439 (pf_scalefactor_1)[i_sb] = \
440 (pf_scalefactor_2)[i_sb] = pf_scalefactor[p_adec->bit_stream.fifo.buffer >> (32 - 6)]; \
441 DumpBits( &p_adec->bit_stream, (1*6) ); \
446 NeedBits( &p_adec->bit_stream, (2*6) ); \
448 (pf_scalefactor_0)[i_sb] = pf_scalefactor[p_adec->bit_stream.fifo.buffer >> (32 - 6)]; \
449 p_adec->bit_stream.fifo.buffer <<= 6; \
450 (pf_scalefactor_1)[i_sb] = \
451 (pf_scalefactor_2)[i_sb] = pf_scalefactor[p_adec->bit_stream.fifo.buffer >> (32 - 6)]; \
452 p_adec->bit_stream.fifo.buffer <<= 6; \
453 p_adec->bit_stream.fifo.i_available -= (2*6); \
459 for ( i_sb = 0; i_sb < i_bound; i_sb++ )
461 if ( pi_allocation_0[i_sb] )
463 SWITCH( pi_scfsi_0, pf_scalefactor_0_0, pf_scalefactor_0_1, pf_scalefactor_0_2 )
465 if ( pi_allocation_1[i_sb] )
467 SWITCH( pi_scfsi_1, pf_scalefactor_1_0, pf_scalefactor_1_1, pf_scalefactor_1_2 )
470 for ( ; i_sb < i_sblimit; i_sb++ )
472 if ( pi_allocation_0[i_sb] )
474 SWITCH( pi_scfsi_0, pf_scalefactor_0_0, pf_scalefactor_0_1, pf_scalefactor_0_2 )
475 SWITCH( pi_scfsi_1, pf_scalefactor_1_0, pf_scalefactor_1_1, pf_scalefactor_1_2 )
478 for ( ; i_sb < 32; i_sb++ )
480 ppf_sample_0[0][i_sb] = .0;
481 ppf_sample_0[1][i_sb] = .0;
482 ppf_sample_0[2][i_sb] = .0;
483 ppf_sample_1[0][i_sb] = .0;
484 ppf_sample_1[1][i_sb] = .0;
485 ppf_sample_1[2][i_sb] = .0;
489 /* fprintf(stderr, "%p\n", p_adec->p_aout_fifo->buffer); */ \
490 /* fprintf(stderr, "l_end_frame == %li, %p\n", l_end_frame, (aout_frame_t *)p_adec->p_aout_fifo->buffer + l_end_frame); */ \
491 p_s16 = ((aout_frame_t *)p_adec->p_aout_fifo->buffer)[ l_end_frame ]; \
492 /* fprintf(stderr, "p_s16 == %p\n", p_s16); */ \
494 l_end_frame &= AOUT_FIFO_SIZE;
495 /* #define NEXT_BUF */
497 #define GROUPTEST( pp_requantization, ppf_sample, pf_sf ) \
498 requantization = *((pp_requantization)[i_sb]); \
499 if ( requantization.pf_ungroup == NULL ) \
501 NeedBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
502 i_need += requantization.i_bits_per_codeword; \
503 (ppf_sample)[0][i_sb] = (f_scalefactor_0 = (pf_sf)[i_sb]) * (requantization.f_slope * \
504 (p_adec->bit_stream.fifo.buffer >> (32 - requantization.i_bits_per_codeword)) + requantization.f_offset); \
505 DumpBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
506 i_dump += requantization.i_bits_per_codeword; \
508 NeedBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
509 i_need += requantization.i_bits_per_codeword; \
510 (ppf_sample)[1][i_sb] = f_scalefactor_0 * (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; \
515 NeedBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
516 i_need += requantization.i_bits_per_codeword; \
517 (ppf_sample)[2][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; \
524 NeedBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
525 i_need += requantization.i_bits_per_codeword; \
526 pf_ungroup = requantization.pf_ungroup + 3 * \
527 (p_adec->bit_stream.fifo.buffer >> (32 - requantization.i_bits_per_codeword)); \
528 DumpBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
529 i_dump += requantization.i_bits_per_codeword; \
530 (ppf_sample)[0][i_sb] = (f_scalefactor_0 = (pf_sf)[i_sb]) * pf_ungroup[0]; \
531 (ppf_sample)[1][i_sb] = f_scalefactor_0 * pf_ungroup[1]; \
532 (ppf_sample)[2][i_sb] = f_scalefactor_0 * pf_ungroup[2]; \
534 /* #define GROUPTEST */
536 #define READ_SAMPLE_L2S( pf_scalefactor_0, pf_scalefactor_1, i_grlimit ) \
537 for ( ; i_gr < (i_grlimit); i_gr++ ) \
539 for ( i_sb = 0; i_sb < i_bound; i_sb++ ) \
541 if ( pi_allocation_0[i_sb] ) \
543 GROUPTEST( pp_requantization_0, ppf_sample_0, (pf_scalefactor_0) ) \
545 if ( pi_allocation_1[i_sb] ) \
547 GROUPTEST( pp_requantization_1, ppf_sample_1, (pf_scalefactor_1) ) \
550 for ( ; i_sb < i_sblimit; i_sb++ ) \
552 if ( pi_allocation_0[i_sb] ) \
554 requantization = *(pp_requantization_0[i_sb]); \
555 if ( requantization.pf_ungroup == NULL ) \
557 NeedBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
558 i_need += requantization.i_bits_per_codeword; \
559 ppf_sample_0[0][i_sb] = (f_scalefactor_0 = (pf_scalefactor_0)[i_sb]) * \
560 (requantization.f_slope * (f_dummy = \
561 (float)(p_adec->bit_stream.fifo.buffer >> (32 - requantization.i_bits_per_codeword))) + \
562 requantization.f_offset); \
563 DumpBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
564 i_dump += requantization.i_bits_per_codeword; \
565 ppf_sample_1[0][i_sb] = (f_scalefactor_1 = (pf_scalefactor_1)[i_sb]) * \
566 (requantization.f_slope * f_dummy + requantization.f_offset); \
568 NeedBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
569 i_need += requantization.i_bits_per_codeword; \
570 ppf_sample_0[1][i_sb] = f_scalefactor_0 * \
571 (requantization.f_slope * (f_dummy = \
572 (float)(p_adec->bit_stream.fifo.buffer >> (32 - requantization.i_bits_per_codeword))) + \
573 requantization.f_offset); \
574 DumpBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
575 i_dump += requantization.i_bits_per_codeword; \
576 ppf_sample_1[1][i_sb] = f_scalefactor_1 * \
577 (requantization.f_slope * f_dummy + requantization.f_offset); \
579 NeedBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
580 i_need += requantization.i_bits_per_codeword; \
581 ppf_sample_0[2][i_sb] = f_scalefactor_0 * \
582 (requantization.f_slope * (f_dummy = \
583 (float)(p_adec->bit_stream.fifo.buffer >> (32 - requantization.i_bits_per_codeword))) + \
584 requantization.f_offset); \
585 DumpBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
586 i_dump += requantization.i_bits_per_codeword; \
587 ppf_sample_1[2][i_sb] = f_scalefactor_1 * \
588 (requantization.f_slope * f_dummy + requantization.f_offset); \
592 NeedBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
593 i_need += requantization.i_bits_per_codeword; \
594 pf_ungroup = requantization.pf_ungroup + 3 * \
595 (p_adec->bit_stream.fifo.buffer >> (32 - requantization.i_bits_per_codeword)); \
596 DumpBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
597 i_dump += requantization.i_bits_per_codeword; \
599 ppf_sample_0[0][i_sb] = (f_scalefactor_0 = (pf_scalefactor_0)[i_sb]) * pf_ungroup[0]; \
600 ppf_sample_0[1][i_sb] = f_scalefactor_0 * pf_ungroup[1]; \
601 ppf_sample_0[2][i_sb] = f_scalefactor_0 * pf_ungroup[2]; \
603 ppf_sample_1[0][i_sb] = (f_scalefactor_1 = (pf_scalefactor_1)[i_sb]) * pf_ungroup[0]; \
604 ppf_sample_1[1][i_sb] = f_scalefactor_1 * pf_ungroup[1]; \
605 ppf_sample_1[2][i_sb] = f_scalefactor_1 * pf_ungroup[2]; \
610 /* fprintf(stderr, "%p", p_s16); */ \
611 DCT32( ppf_sample_0[0], &p_adec->bank_0 ); \
612 PCM( &p_adec->bank_0, &p_s16, 2 ); \
613 /* fprintf(stderr, " %p", p_s16); */ \
615 /* fprintf(stderr, " %p\n", p_s16); */ \
617 /* fprintf(stderr, "%p", p_s16); */ \
618 DCT32( ppf_sample_1[0], &p_adec->bank_1 ); \
619 PCM( &p_adec->bank_1, &p_s16, 2 ); \
620 /* fprintf(stderr, " %p", p_s16); */ \
622 /* fprintf(stderr, " %p\n", p_s16); */ \
624 /* fprintf(stderr, "%p", p_s16); */ \
625 DCT32( ppf_sample_0[1], &p_adec->bank_0 ); \
626 PCM( &p_adec->bank_0, &p_s16, 2 ); \
627 /* fprintf(stderr, " %p", p_s16); */ \
629 /* fprintf(stderr, " %p\n", p_s16); */ \
631 /* fprintf(stderr, "%p", p_s16); */ \
632 DCT32( ppf_sample_1[1], &p_adec->bank_1 ); \
633 PCM( &p_adec->bank_1, &p_s16, 2 ); \
634 /* fprintf(stderr, " %p", p_s16); */ \
636 /* fprintf(stderr, " %p\n", p_s16); */ \
638 /* fprintf(stderr, "%p", p_s16); */ \
639 DCT32( ppf_sample_0[2], &p_adec->bank_0 ); \
640 PCM( &p_adec->bank_0, &p_s16, 2 ); \
641 /* fprintf(stderr, " %p", p_s16); */ \
643 /* fprintf(stderr, " %p\n", p_s16); */ \
645 /* fprintf(stderr, "%p", p_s16); */ \
646 DCT32( ppf_sample_1[2], &p_adec->bank_1 ); \
647 PCM( &p_adec->bank_1, &p_s16, 2 ); \
648 /* fprintf(stderr, " %p", p_s16); */ \
650 /* fprintf(stderr, " %p\n", p_s16); */ \
652 /* #define READ_SAMPLE_L2S */
654 l_end_frame = p_adec->p_aout_fifo->l_end_frame;
658 READ_SAMPLE_L2S( pf_scalefactor_0_0, pf_scalefactor_1_0, 2 )
661 READ_SAMPLE_L2S( pf_scalefactor_0_0, pf_scalefactor_1_0, 4 )
664 READ_SAMPLE_L2S( pf_scalefactor_0_1, pf_scalefactor_1_1, 6 )
667 READ_SAMPLE_L2S( pf_scalefactor_0_1, pf_scalefactor_1_1, 8 )
670 READ_SAMPLE_L2S( pf_scalefactor_0_2, pf_scalefactor_1_2, 10 )
673 READ_SAMPLE_L2S( pf_scalefactor_0_2, pf_scalefactor_1_2, 12 )
675 // fprintf(stderr, "adec debug: layer == %i, padding_bit == %i, sampling_frequency == %i, bitrate_index == %i\n",
676 // (i_header & ADEC_HEADER_LAYER_MASK) >> ADEC_HEADER_LAYER_SHIFT,
677 // (i_header & ADEC_HEADER_PADDING_BIT_MASK) >> ADEC_HEADER_PADDING_BIT_SHIFT,
678 // (i_header & ADEC_HEADER_SAMPLING_FREQUENCY_MASK) >> ADEC_HEADER_SAMPLING_FREQUENCY_SHIFT,
679 // (i_header & ADEC_HEADER_BITRATE_INDEX_MASK) >> ADEC_HEADER_BITRATE_INDEX_SHIFT);
680 // fprintf(stderr, "adec debug: framesize == %i, i_need == %i, i_dump == %i\n",
681 // pi_framesize[ 128 * ((i_header & ADEC_HEADER_LAYER_MASK) >> ADEC_HEADER_LAYER_SHIFT) +
682 // 64 * ((i_header & ADEC_HEADER_PADDING_BIT_MASK) >> ADEC_HEADER_PADDING_BIT_SHIFT) +
683 // 16 * ((i_header & ADEC_HEADER_SAMPLING_FREQUENCY_MASK) >> ADEC_HEADER_SAMPLING_FREQUENCY_SHIFT) +
684 // 1 * ((i_header & ADEC_HEADER_BITRATE_INDEX_MASK) >> ADEC_HEADER_BITRATE_INDEX_SHIFT) ],
687 p_adec->bit_stream.fifo.buffer = 0;
688 p_adec->bit_stream.fifo.i_available = 0;
692 /******************************************************************************
693 * InitThread : initialize an audio decoder thread
694 ******************************************************************************
695 * This function is called from RunThread and performs the second step of the
696 * initialization. It returns 0 on success.
697 ******************************************************************************/
698 static int InitThread( adec_thread_t * p_adec )
700 aout_fifo_t aout_fifo;
702 intf_DbgMsg("adec debug: initializing audio decoder thread %p\n", p_adec);
704 /* Our first job is to initialize the bit stream structure with the
705 * beginning of the input stream */
706 pthread_mutex_lock( &p_adec->fifo.data_lock );
707 while ( DECODER_FIFO_ISEMPTY(p_adec->fifo) )
709 pthread_cond_wait( &p_adec->fifo.data_wait, &p_adec->fifo.data_lock );
711 p_adec->bit_stream.p_ts = DECODER_FIFO_START( p_adec->fifo )->p_first_ts;
712 p_adec->bit_stream.i_byte = p_adec->bit_stream.p_ts->i_payload_start;
713 pthread_mutex_unlock( &p_adec->fifo.data_lock );
715 /* Now we look for an audio frame header in the input stream */
716 if ( FindHeader(p_adec) )
718 return( -1 ); /* b_die or b_error is set */
722 * We have the header and all its informations : we must be able to create
723 * the audio output fifo.
726 /* Is the sound in mono mode or stereo mode ? */
727 if ( (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_MODE_MASK) == ADEC_HEADER_MODE_MASK )
729 intf_DbgMsg("adec debug: mode == mono\n");
730 aout_fifo.i_type = AOUT_ADEC_MONO_FIFO;
731 aout_fifo.b_stereo = 0;
735 intf_DbgMsg("adec debug: mode == stereo\n");
736 aout_fifo.i_type = AOUT_ADEC_STEREO_FIFO;
737 aout_fifo.b_stereo = 1;
740 /* Checking the sampling frequency */
741 switch ( (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_SAMPLING_FREQUENCY_MASK) \
742 >> ADEC_HEADER_SAMPLING_FREQUENCY_SHIFT )
745 intf_DbgMsg("adec debug: sampling_frequency == 44100 Hz\n");
746 aout_fifo.l_rate = 44100;
750 intf_DbgMsg("adec debug: sampling_frequency == 48000 Hz\n");
751 aout_fifo.l_rate = 48000;
755 intf_DbgMsg("adec debug: sampling_frequency == 32000 Hz\n");
756 aout_fifo.l_rate = 32000;
760 intf_ErrMsg("adec error: can't create audio output fifo (sampling_frequency == `reserved')\n");
764 /* Creating the audio output fifo */
765 if ( (p_adec->p_aout_fifo = aout_CreateFifo(p_adec->p_aout, &aout_fifo)) == NULL )
770 intf_DbgMsg("adec debug: audio decoder thread %p initialized\n", p_adec);
774 #define UPDATE_INCREMENT( increment, integer ) \
775 if ( ((increment).l_remainder += (increment).l_euclidean_remainder) >= 0 ) \
777 (integer) += (increment).l_euclidean_integer + 1; \
778 (increment).l_remainder -= (increment).l_euclidean_denominator; \
782 (integer) += (increment).l_euclidean_integer; \
785 /******************************************************************************
786 * RunThread : audio decoder thread
787 ******************************************************************************
788 * Audio decoder thread. This function does only returns when the thread is
790 ******************************************************************************/
791 static void RunThread( adec_thread_t * p_adec )
793 // static const int pi_framesize[512] = ADEC_FRAME_SIZE;
799 /* The synchronization needs date and date_increment for the moment */
801 aout_increment_t date_increment;
803 intf_DbgMsg("adec debug: running audio decoder thread (%p) (pid == %i)\n", p_adec, getpid());
805 /* Initializing the audio decoder thread */
806 if ( InitThread(p_adec) )
811 /* Initializing date_increment */
812 switch ( (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_LAYER_MASK) >> ADEC_HEADER_LAYER_SHIFT )
816 s64_numerator = 1152 * 1000000;
817 s64_denominator = (s64)p_adec->p_aout->dsp.l_rate;
822 s64_numerator = 384 * 1000000;
823 s64_denominator = (s64)p_adec->p_aout->dsp.l_rate;
826 date_increment.l_remainder = -(long)s64_denominator;
827 date_increment.l_euclidean_integer = 0;
828 while ( s64_numerator >= s64_denominator )
830 date_increment.l_euclidean_integer++;
831 s64_numerator -= s64_denominator;
833 date_increment.l_euclidean_remainder = (long)s64_numerator;
834 date_increment.l_euclidean_denominator = (long)s64_denominator;
836 /* Audio decoder thread's main loop */
837 while ( (!p_adec->b_die) && (!p_adec->b_error) )
839 switch ( (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_LAYER_MASK) >> ADEC_HEADER_LAYER_SHIFT )
842 intf_DbgMsg("adec debug: layer == 0 (reserved)\n");
843 p_adec->bit_stream.fifo.buffer = 0;
844 p_adec->bit_stream.fifo.i_available = 0;
848 p_adec->bit_stream.fifo.buffer = 0;
849 p_adec->bit_stream.fifo.i_available = 0;
853 if ( (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_MODE_MASK) == ADEC_HEADER_MODE_MASK )
855 adec_Layer2_Mono( p_adec );
859 // i_header = p_adec->bit_stream.fifo.buffer;
860 // i_framesize = pi_framesize[ 128*((i_header & ADEC_HEADER_LAYER_MASK) >> ADEC_HEADER_LAYER_SHIFT) +
861 // 64*((i_header & ADEC_HEADER_PADDING_BIT_MASK) >> ADEC_HEADER_PADDING_BIT_SHIFT) +
862 // 16*((i_header & ADEC_HEADER_SAMPLING_FREQUENCY_MASK) >> ADEC_HEADER_SAMPLING_FREQUENCY_SHIFT) +
863 // 1*((i_header & ADEC_HEADER_BITRATE_INDEX_MASK) >> ADEC_HEADER_BITRATE_INDEX_SHIFT) ];
864 // for ( i_dummy = 0; i_dummy < i_framesize; i_dummy++ )
866 // GetByte( &p_adec->bit_stream );
868 // for ( i_dummy = 0; i_dummy < 512; i_dummy++ )
870 // p_adec->bank_0.v1[ i_dummy ] = .0;
871 // p_adec->bank_1.v1[ i_dummy ] = .0;
872 // p_adec->bank_0.v2[ i_dummy ] = .0;
873 // p_adec->bank_1.v2[ i_dummy ] = .0;
876 pthread_mutex_lock( &p_adec->p_aout_fifo->data_lock );
877 /* adec_Layer2_Stereo() produces 6 output frames (2*1152/384)...
878 * If these 6 frames would be recorded in the audio output fifo,
879 * the l_end_frame index would be incremented 6 times. But, if after
880 * this operation the audio output fifo would contain less than 6 frames,
881 * it would mean that we had not enough room to store the 6 frames :-P */
882 while ( (((p_adec->p_aout_fifo->l_end_frame + 6) - p_adec->p_aout_fifo->l_start_frame) & AOUT_FIFO_SIZE) < 6 ) /* !! */
884 pthread_cond_wait( &p_adec->p_aout_fifo->data_wait, &p_adec->p_aout_fifo->data_lock );
886 pthread_mutex_unlock( &p_adec->p_aout_fifo->data_lock );
888 if ( adec_Layer2_Stereo(p_adec) )
890 pthread_mutex_lock( &p_adec->p_aout_fifo->data_lock );
892 p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] = date;
893 p_adec->p_aout_fifo->l_end_frame = (p_adec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
895 p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] = LAST_MDATE;
896 p_adec->p_aout_fifo->l_end_frame = (p_adec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
898 p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] = LAST_MDATE;
899 p_adec->p_aout_fifo->l_end_frame = (p_adec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
901 p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] = LAST_MDATE;
902 p_adec->p_aout_fifo->l_end_frame = (p_adec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
904 p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] = LAST_MDATE;
905 p_adec->p_aout_fifo->l_end_frame = (p_adec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
907 p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] = LAST_MDATE;
908 p_adec->p_aout_fifo->l_end_frame = (p_adec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
909 pthread_mutex_unlock( &p_adec->p_aout_fifo->data_lock );
912 UPDATE_INCREMENT( date_increment, date )
919 if ( (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_MODE_MASK) == ADEC_HEADER_MODE_MASK )
921 adec_Layer1_Mono( p_adec );
925 adec_Layer1_Stereo( p_adec );
930 intf_DbgMsg("adec debug: layer == %i (unknown)\n",
931 (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_LAYER_MASK) >> ADEC_HEADER_LAYER_SHIFT);
932 p_adec->bit_stream.fifo.buffer = 0;
933 p_adec->bit_stream.fifo.i_available = 0;
936 FindHeader( p_adec );
939 /* If b_error is set, the audio decoder thread enters the error loop */
940 if ( p_adec->b_error )
942 ErrorThread( p_adec );
945 /* End of the audio decoder thread */
949 /******************************************************************************
950 * ErrorThread : audio decoder's RunThread() error loop
951 ******************************************************************************
952 * This function is called when an error occured during thread main's loop. The
953 * thread can still receive feed, but must be ready to terminate as soon as
955 ******************************************************************************/
956 static void ErrorThread( adec_thread_t *p_adec )
958 /* We take the lock, because we are going to read/write the start/end
959 * indexes of the decoder fifo */
960 pthread_mutex_lock( &p_adec->fifo.data_lock );
962 /* Wait until a `die' order is sent */
963 while( !p_adec->b_die )
965 /* Trash all received PES packets */
966 while( !DECODER_FIFO_ISEMPTY(p_adec->fifo) )
968 input_NetlistFreePES( p_adec->bit_stream.p_input, DECODER_FIFO_START(p_adec->fifo) );
969 DECODER_FIFO_INCSTART( p_adec->fifo );
971 // fprintf(stderr, "*");
975 /* Waiting for the input thread to put new PES packets in the fifo */
976 pthread_cond_wait( &p_adec->fifo.data_wait, &p_adec->fifo.data_lock );
979 /* We can release the lock before leaving */
980 pthread_mutex_unlock( &p_adec->fifo.data_lock );
983 /******************************************************************************
984 * EndThread : audio decoder thread destruction
985 ******************************************************************************
986 * This function is called when the thread ends after a sucessfull
988 ******************************************************************************/
989 static void EndThread( adec_thread_t *p_adec )
991 intf_DbgMsg("adec debug: destroying audio decoder thread %p\n", p_adec);
993 /* If the audio output fifo was created, we destroy it */
994 if ( p_adec->p_aout_fifo != NULL )
996 aout_DestroyFifo( p_adec->p_aout_fifo );
998 /* Destroy descriptor */
1001 intf_DbgMsg("adec debug: audio decoder thread %p destroyed\n", p_adec);