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 * GetByte : reads the next byte in the input stream
148 ******************************************************************************/
149 static __inline__ byte_t GetByte( bit_stream_t * p_bit_stream )
151 /* Are there some bytes left in the current TS packet ? */
152 if ( p_bit_stream->i_byte < p_bit_stream->p_ts->i_payload_end )
154 return( p_bit_stream->p_ts->buffer[ p_bit_stream->i_byte++ ] );
158 /* We are looking for the next TS packet that contains real data,
159 * and not just a PES header */
162 /* We were reading the last TS packet of this PES packet... It's
163 * time to jump to the next PES packet */
164 if ( p_bit_stream->p_ts->p_next_ts == NULL )
166 /* We are going to read/write the start and end indexes of the
167 * decoder fifo and to use the fifo's conditional variable,
168 * that's why we need to take the lock before */
169 pthread_mutex_lock( &p_bit_stream->p_decoder_fifo->data_lock );
171 /* We should increase the start index of the decoder fifo, but
172 * if we do this now, the input thread could overwrite the
173 * pointer to the current PES packet, and we weren't able to
174 * give it back to the netlist. That's why we free the PES
176 input_NetlistFreePES( p_bit_stream->p_input, DECODER_FIFO_START(*p_bit_stream->p_decoder_fifo) );
177 DECODER_FIFO_INCSTART( *p_bit_stream->p_decoder_fifo );
180 while ( DECODER_FIFO_ISEMPTY(*p_bit_stream->p_decoder_fifo) )
182 pthread_cond_wait( &p_bit_stream->p_decoder_fifo->data_wait,
183 &p_bit_stream->p_decoder_fifo->data_lock );
186 /* The next byte could be found in the next PES packet */
188 // fprintf(stderr, "*");
190 p_bit_stream->p_ts = DECODER_FIFO_START( *p_bit_stream->p_decoder_fifo )->p_first_ts;
192 /* We can release the fifo's data lock */
193 pthread_mutex_unlock( &p_bit_stream->p_decoder_fifo->data_lock );
195 /* Perhaps the next TS packet of the current PES packet contains
196 * real data (ie its payload's size is greater than 0) */
200 // fprintf(stderr, ".");
202 p_bit_stream->p_ts = p_bit_stream->p_ts->p_next_ts;
204 } while ( p_bit_stream->p_ts->i_payload_start == p_bit_stream->p_ts->i_payload_end );
206 /* We've found a TS packet which contains interesting data... As we
207 * return the payload's first byte, we set i_byte to the following
209 p_bit_stream->i_byte = p_bit_stream->p_ts->i_payload_start;
210 return( p_bit_stream->p_ts->buffer[ p_bit_stream->i_byte++ ] );
214 /******************************************************************************
215 * NeedBits : reads i_bits new bits in the bit stream and stores them in the
217 ******************************************************************************
218 * - i_bits must be less or equal 32 !
219 * - There is something important to notice with that function : if the number
220 * of bits available in the bit buffer when calling NeedBits() is greater than
221 * 24 (i_available > 24) but less than the number of needed bits
222 * (i_available < i_bits), the byte returned by GetByte() will be shifted with
223 * a negative value and the number of bits available in the bit buffer will be
224 * set to more than 32 !
225 ******************************************************************************/
226 static __inline__ void NeedBits( bit_stream_t * p_bit_stream, int i_bits )
228 while ( p_bit_stream->fifo.i_available < i_bits )
230 p_bit_stream->fifo.buffer |= ((u32)GetByte( p_bit_stream )) << (24 - p_bit_stream->fifo.i_available);
231 p_bit_stream->fifo.i_available += 8;
235 /******************************************************************************
236 * DumpBits : removes i_bits bits from the bit buffer
237 ******************************************************************************
238 * - i_bits <= i_available
239 * - i_bits < 32 (because (u32 << 32) <=> (u32 = u32))
240 ******************************************************************************/
241 static __inline__ void DumpBits( bit_stream_t * p_bit_stream, int i_bits )
243 p_bit_stream->fifo.buffer <<= i_bits;
244 p_bit_stream->fifo.i_available -= i_bits;
247 /******************************************************************************
248 * FindHeader : parses an input stream until an audio frame header could be
250 ******************************************************************************
251 * When this function returns successfully, the header can be found in the
252 * buffer of the bit stream fifo.
253 ******************************************************************************/
254 static int FindHeader( adec_thread_t * p_adec )
256 while ( (!p_adec->b_die) && (!p_adec->b_error) )
258 NeedBits( &p_adec->bit_stream, 32 );
259 if ( (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_SYNCWORD_MASK) == ADEC_HEADER_SYNCWORD_MASK )
262 // fprintf(stderr, "H");
267 // fprintf(stderr, "!");
269 DumpBits( &p_adec->bit_stream, 8 );
275 /******************************************************************************
276 * adec_Layer`L'_`M': decodes an mpeg 1, layer `L', mode `M', audio frame
277 ******************************************************************************
278 * These functions decode the audio frame which has already its header loaded
279 * in the i_header member of the audio decoder thread structure and its first
280 * byte of data described by the bit stream structure of the audio decoder
281 * thread (there is no bit available in the bit buffer yet)
282 ******************************************************************************/
284 /******************************************************************************
286 ******************************************************************************/
287 static __inline__ int adec_Layer1_Mono( adec_thread_t * p_adec )
289 p_adec->bit_stream.fifo.buffer = 0;
290 p_adec->bit_stream.fifo.i_available = 0;
294 /******************************************************************************
296 ******************************************************************************/
297 static __inline__ int adec_Layer1_Stereo( adec_thread_t * p_adec )
299 p_adec->bit_stream.fifo.buffer = 0;
300 p_adec->bit_stream.fifo.i_available = 0;
304 /******************************************************************************
306 ******************************************************************************/
307 static __inline__ int adec_Layer2_Mono( adec_thread_t * p_adec )
309 p_adec->bit_stream.fifo.buffer = 0;
310 p_adec->bit_stream.fifo.i_available = 0;
314 /******************************************************************************
316 ******************************************************************************/
317 static __inline__ int adec_Layer2_Stereo( adec_thread_t * p_adec )
319 typedef struct requantization_s
321 byte_t i_bits_per_codeword;
322 const float * pf_ungroup;
327 static const float pf_scalefactor[64] = ADEC_SCALE_FACTOR;
330 static int i_sampling_frequency, i_mode, i_bound;
331 static int pi_allocation_0[32], pi_allocation_1[32]; /* see ISO/IEC 11172-3 2.4.1.6 */
333 float f_scalefactor_0, f_scalefactor_1;
335 static const byte_t ppi_bitrate_per_channel_index[4][15] = ADEC_LAYER2_BITRATE_PER_CHANNEL_INDEX;
336 static const byte_t ppi_sblimit[3][11] = ADEC_LAYER2_SBLIMIT;
337 static const byte_t ppi_nbal[2][32] = ADEC_LAYER2_NBAL;
339 static const float pf_ungroup3[3*3*3 * 3] = ADEC_LAYER2_UNGROUP3;
340 static const float pf_ungroup5[5*5*5 * 3] = ADEC_LAYER2_UNGROUP5;
341 static const float pf_ungroup9[9*9*9 * 3] = ADEC_LAYER2_UNGROUP9;
343 static const requantization_t p_requantization_cd[16] = ADEC_LAYER2_REQUANTIZATION_CD;
344 static const requantization_t p_requantization_ab1[16] = ADEC_LAYER2_REQUANTIZATION_AB1;
345 static const requantization_t p_requantization_ab2[16] = ADEC_LAYER2_REQUANTIZATION_AB2;
346 static const requantization_t p_requantization_ab3[16] = ADEC_LAYER2_REQUANTIZATION_AB3;
347 static const requantization_t p_requantization_ab4[16] = ADEC_LAYER2_REQUANTIZATION_AB4;
348 static const requantization_t * pp_requantization_ab[30] = ADEC_LAYER2_REQUANTIZATION_AB;
350 static int i_sblimit, i_bitrate_per_channel_index;
351 static int pi_scfsi_0[30], pi_scfsi_1[30];
352 static const byte_t * pi_nbal;
353 static float ppf_sample_0[3][32], ppf_sample_1[3][32];
354 static const requantization_t * pp_requantization_0[30];
355 static const requantization_t * pp_requantization_1[30];
356 static requantization_t requantization;
357 static const float * pf_ungroup;
359 static float pf_scalefactor_0_0[30], pf_scalefactor_0_1[30], pf_scalefactor_0_2[30];
360 static float pf_scalefactor_1_0[30], pf_scalefactor_1_1[30], pf_scalefactor_1_2[30];
368 int i_need = 0, i_dump = 0;
369 // static const int pi_framesize[512] = ADEC_FRAME_SIZE;
371 /* Read the audio frame header and flush the bit buffer */
372 i_header = p_adec->bit_stream.fifo.buffer;
373 p_adec->bit_stream.fifo.buffer = 0;
374 p_adec->bit_stream.fifo.i_available = 0;
375 /* Read the sampling frequency (see ISO/IEC 11172-3 2.4.2.3) */
376 i_sampling_frequency = (int)((i_header & ADEC_HEADER_SAMPLING_FREQUENCY_MASK)
377 >> ADEC_HEADER_SAMPLING_FREQUENCY_SHIFT);
378 /* Read the mode (see ISO/IEC 11172-3 2.4.2.3) */
379 i_mode = (int)((i_header & ADEC_HEADER_MODE_MASK) >> ADEC_HEADER_MODE_SHIFT);
380 /* If a CRC can be found in the frame, get rid of it */
381 if ( (i_header & ADEC_HEADER_PROTECTION_BIT_MASK) == 0 )
383 GetByte( &p_adec->bit_stream );
384 GetByte( &p_adec->bit_stream );
387 /* Find out the bitrate per channel index */
388 i_bitrate_per_channel_index = (int)ppi_bitrate_per_channel_index[i_mode]
389 [(i_header & ADEC_HEADER_BITRATE_INDEX_MASK) >> ADEC_HEADER_BITRATE_INDEX_SHIFT];
390 /* Find out the number of subbands */
391 i_sblimit = (int)ppi_sblimit[i_sampling_frequency][i_bitrate_per_channel_index];
392 /* Check if the frame is valid or not */
393 if ( i_sblimit == 0 )
395 return( 0 ); /* the frame is invalid */
397 /* Find out the number of bits allocated */
398 pi_nbal = ppi_nbal[ (i_bitrate_per_channel_index <= 2) ? 0 : 1 ];
400 /* Find out the `bound' subband (see ISO/IEC 11172-3 2.4.2.3) */
403 i_bound = (int)(((i_header & ADEC_HEADER_MODE_EXTENSION_MASK) >> (ADEC_HEADER_MODE_EXTENSION_SHIFT - 2)) + 4);
404 if ( i_bound > i_sblimit )
414 /* Read the allocation information (see ISO/IEC 11172-3 2.4.1.6) */
415 for ( i_sb = 0; i_sb < i_bound; i_sb++ )
417 i_2nbal = 2 * (i_nbal = (int)pi_nbal[ i_sb ]);
418 NeedBits( &p_adec->bit_stream, i_2nbal );
420 pi_allocation_0[ i_sb ] = (int)(p_adec->bit_stream.fifo.buffer >> (32 - i_nbal));
421 p_adec->bit_stream.fifo.buffer <<= i_nbal;
422 pi_allocation_1[ i_sb ] = (int)(p_adec->bit_stream.fifo.buffer >> (32 - i_nbal));
423 p_adec->bit_stream.fifo.buffer <<= i_nbal;
424 p_adec->bit_stream.fifo.i_available -= i_2nbal;
427 for ( ; i_sb < i_sblimit; i_sb++ )
429 i_nbal = (int)pi_nbal[ i_sb ];
430 NeedBits( &p_adec->bit_stream, i_nbal );
432 pi_allocation_0[ i_sb ] = (int)(p_adec->bit_stream.fifo.buffer >> (32 - i_nbal));
433 DumpBits( &p_adec->bit_stream, i_nbal );
437 #define MACRO( p_requantization ) \
438 for ( i_sb = 0; i_sb < i_bound; i_sb++ ) \
440 if ( pi_allocation_0[i_sb] ) \
442 pp_requantization_0[i_sb] = &((p_requantization)[pi_allocation_0[i_sb]]); \
443 NeedBits( &p_adec->bit_stream, 2 ); \
445 pi_scfsi_0[i_sb] = (int)(p_adec->bit_stream.fifo.buffer >> (32 - 2)); \
446 DumpBits( &p_adec->bit_stream, 2 ); \
451 ppf_sample_0[0][i_sb] = .0; \
452 ppf_sample_0[1][i_sb] = .0; \
453 ppf_sample_0[2][i_sb] = .0; \
456 if ( pi_allocation_1[i_sb] ) \
458 pp_requantization_1[i_sb] = &((p_requantization)[pi_allocation_1[i_sb]]); \
459 NeedBits( &p_adec->bit_stream, 2 ); \
461 pi_scfsi_1[i_sb] = (int)(p_adec->bit_stream.fifo.buffer >> (32 - 2)); \
462 DumpBits( &p_adec->bit_stream, 2 ); \
467 ppf_sample_1[0][i_sb] = .0; \
468 ppf_sample_1[1][i_sb] = .0; \
469 ppf_sample_1[2][i_sb] = .0; \
473 for ( ; i_sb < i_sblimit; i_sb++ ) \
475 if ( pi_allocation_0[i_sb] ) \
477 pp_requantization_0[i_sb] = &((p_requantization)[pi_allocation_0[i_sb]]); \
478 NeedBits( &p_adec->bit_stream, 4 ); \
480 pi_scfsi_0[i_sb] = (int)(p_adec->bit_stream.fifo.buffer >> (32 - 2)); \
481 p_adec->bit_stream.fifo.buffer <<= 2; \
482 pi_scfsi_1[i_sb] = (int)(p_adec->bit_stream.fifo.buffer >> (32 - 2)); \
483 p_adec->bit_stream.fifo.buffer <<= 2; \
484 p_adec->bit_stream.fifo.i_available -= 4; \
489 ppf_sample_0[0][i_sb] = .0; \
490 ppf_sample_0[1][i_sb] = .0; \
491 ppf_sample_0[2][i_sb] = .0; \
492 ppf_sample_1[0][i_sb] = .0; \
493 ppf_sample_1[1][i_sb] = .0; \
494 ppf_sample_1[2][i_sb] = .0; \
499 if ( i_bitrate_per_channel_index <= 2 )
501 MACRO( p_requantization_cd )
505 MACRO( pp_requantization_ab[i_sb] )
508 #define SWITCH( pi_scfsi, pf_scalefactor_0, pf_scalefactor_1, pf_scalefactor_2 ) \
509 switch ( (pi_scfsi)[i_sb] ) \
512 NeedBits( &p_adec->bit_stream, (3*6) ); \
514 (pf_scalefactor_0)[i_sb] = pf_scalefactor[p_adec->bit_stream.fifo.buffer >> (32 - 6)]; \
515 p_adec->bit_stream.fifo.buffer <<= 6; \
516 (pf_scalefactor_1)[i_sb] = pf_scalefactor[p_adec->bit_stream.fifo.buffer >> (32 - 6)]; \
517 p_adec->bit_stream.fifo.buffer <<= 6; \
518 (pf_scalefactor_2)[i_sb] = pf_scalefactor[p_adec->bit_stream.fifo.buffer >> (32 - 6)]; \
519 p_adec->bit_stream.fifo.buffer <<= 6; \
520 p_adec->bit_stream.fifo.i_available -= (3*6); \
525 NeedBits( &p_adec->bit_stream, (2*6) ); \
527 (pf_scalefactor_0)[i_sb] = \
528 (pf_scalefactor_1)[i_sb] = pf_scalefactor[p_adec->bit_stream.fifo.buffer >> (32 - 6)]; \
529 p_adec->bit_stream.fifo.buffer <<= 6; \
530 (pf_scalefactor_2)[i_sb] = pf_scalefactor[p_adec->bit_stream.fifo.buffer >> (32 - 6)]; \
531 p_adec->bit_stream.fifo.buffer <<= 6; \
532 p_adec->bit_stream.fifo.i_available -= (2*6); \
537 NeedBits( &p_adec->bit_stream, (1*6) ); \
539 (pf_scalefactor_0)[i_sb] = \
540 (pf_scalefactor_1)[i_sb] = \
541 (pf_scalefactor_2)[i_sb] = pf_scalefactor[p_adec->bit_stream.fifo.buffer >> (32 - 6)]; \
542 DumpBits( &p_adec->bit_stream, (1*6) ); \
547 NeedBits( &p_adec->bit_stream, (2*6) ); \
549 (pf_scalefactor_0)[i_sb] = pf_scalefactor[p_adec->bit_stream.fifo.buffer >> (32 - 6)]; \
550 p_adec->bit_stream.fifo.buffer <<= 6; \
551 (pf_scalefactor_1)[i_sb] = \
552 (pf_scalefactor_2)[i_sb] = pf_scalefactor[p_adec->bit_stream.fifo.buffer >> (32 - 6)]; \
553 p_adec->bit_stream.fifo.buffer <<= 6; \
554 p_adec->bit_stream.fifo.i_available -= (2*6); \
560 for ( i_sb = 0; i_sb < i_bound; i_sb++ )
562 if ( pi_allocation_0[i_sb] )
564 SWITCH( pi_scfsi_0, pf_scalefactor_0_0, pf_scalefactor_0_1, pf_scalefactor_0_2 )
566 if ( pi_allocation_1[i_sb] )
568 SWITCH( pi_scfsi_1, pf_scalefactor_1_0, pf_scalefactor_1_1, pf_scalefactor_1_2 )
571 for ( ; i_sb < i_sblimit; i_sb++ )
573 if ( pi_allocation_0[i_sb] )
575 SWITCH( pi_scfsi_0, pf_scalefactor_0_0, pf_scalefactor_0_1, pf_scalefactor_0_2 )
576 SWITCH( pi_scfsi_1, pf_scalefactor_1_0, pf_scalefactor_1_1, pf_scalefactor_1_2 )
579 for ( ; i_sb < 32; i_sb++ )
581 ppf_sample_0[0][i_sb] = .0;
582 ppf_sample_0[1][i_sb] = .0;
583 ppf_sample_0[2][i_sb] = .0;
584 ppf_sample_1[0][i_sb] = .0;
585 ppf_sample_1[1][i_sb] = .0;
586 ppf_sample_1[2][i_sb] = .0;
590 /* fprintf(stderr, "%p\n", p_adec->p_aout_fifo->buffer); */ \
591 /* fprintf(stderr, "l_end_frame == %li, %p\n", l_end_frame, (aout_frame_t *)p_adec->p_aout_fifo->buffer + l_end_frame); */ \
592 p_s16 = ((aout_frame_t *)p_adec->p_aout_fifo->buffer)[ l_end_frame ]; \
593 /* fprintf(stderr, "p_s16 == %p\n", p_s16); */ \
595 l_end_frame &= AOUT_FIFO_SIZE;
596 /* #define NEXT_BUF */
598 #define GROUPTEST( pp_requantization, ppf_sample, pf_sf ) \
599 requantization = *((pp_requantization)[i_sb]); \
600 if ( requantization.pf_ungroup == NULL ) \
602 NeedBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
603 i_need += requantization.i_bits_per_codeword; \
604 (ppf_sample)[0][i_sb] = (f_scalefactor_0 = (pf_sf)[i_sb]) * (requantization.f_slope * \
605 (p_adec->bit_stream.fifo.buffer >> (32 - requantization.i_bits_per_codeword)) + requantization.f_offset); \
606 DumpBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
607 i_dump += requantization.i_bits_per_codeword; \
609 NeedBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
610 i_need += requantization.i_bits_per_codeword; \
611 (ppf_sample)[1][i_sb] = f_scalefactor_0 * (requantization.f_slope * \
612 (p_adec->bit_stream.fifo.buffer >> (32 - requantization.i_bits_per_codeword)) + requantization.f_offset); \
613 DumpBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
614 i_dump += requantization.i_bits_per_codeword; \
616 NeedBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
617 i_need += requantization.i_bits_per_codeword; \
618 (ppf_sample)[2][i_sb] = f_scalefactor_0 * (requantization.f_slope * \
619 (p_adec->bit_stream.fifo.buffer >> (32 - requantization.i_bits_per_codeword)) + requantization.f_offset); \
620 DumpBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
621 i_dump += requantization.i_bits_per_codeword; \
625 NeedBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
626 i_need += requantization.i_bits_per_codeword; \
627 pf_ungroup = requantization.pf_ungroup + 3 * \
628 (p_adec->bit_stream.fifo.buffer >> (32 - requantization.i_bits_per_codeword)); \
629 DumpBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
630 i_dump += requantization.i_bits_per_codeword; \
631 (ppf_sample)[0][i_sb] = (f_scalefactor_0 = (pf_sf)[i_sb]) * pf_ungroup[0]; \
632 (ppf_sample)[1][i_sb] = f_scalefactor_0 * pf_ungroup[1]; \
633 (ppf_sample)[2][i_sb] = f_scalefactor_0 * pf_ungroup[2]; \
635 /* #define GROUPTEST */
637 #define READ_SAMPLE_L2S( pf_scalefactor_0, pf_scalefactor_1, i_grlimit ) \
638 for ( ; i_gr < (i_grlimit); i_gr++ ) \
640 for ( i_sb = 0; i_sb < i_bound; i_sb++ ) \
642 if ( pi_allocation_0[i_sb] ) \
644 GROUPTEST( pp_requantization_0, ppf_sample_0, (pf_scalefactor_0) ) \
646 if ( pi_allocation_1[i_sb] ) \
648 GROUPTEST( pp_requantization_1, ppf_sample_1, (pf_scalefactor_1) ) \
651 for ( ; i_sb < i_sblimit; i_sb++ ) \
653 if ( pi_allocation_0[i_sb] ) \
655 requantization = *(pp_requantization_0[i_sb]); \
656 if ( requantization.pf_ungroup == NULL ) \
658 NeedBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
659 i_need += requantization.i_bits_per_codeword; \
660 ppf_sample_0[0][i_sb] = (f_scalefactor_0 = (pf_scalefactor_0)[i_sb]) * \
661 (requantization.f_slope * (f_dummy = \
662 (float)(p_adec->bit_stream.fifo.buffer >> (32 - requantization.i_bits_per_codeword))) + \
663 requantization.f_offset); \
664 DumpBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
665 i_dump += requantization.i_bits_per_codeword; \
666 ppf_sample_1[0][i_sb] = (f_scalefactor_1 = (pf_scalefactor_1)[i_sb]) * \
667 (requantization.f_slope * f_dummy + requantization.f_offset); \
669 NeedBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
670 i_need += requantization.i_bits_per_codeword; \
671 ppf_sample_0[1][i_sb] = f_scalefactor_0 * \
672 (requantization.f_slope * (f_dummy = \
673 (float)(p_adec->bit_stream.fifo.buffer >> (32 - requantization.i_bits_per_codeword))) + \
674 requantization.f_offset); \
675 DumpBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
676 i_dump += requantization.i_bits_per_codeword; \
677 ppf_sample_1[1][i_sb] = f_scalefactor_1 * \
678 (requantization.f_slope * f_dummy + requantization.f_offset); \
680 NeedBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
681 i_need += requantization.i_bits_per_codeword; \
682 ppf_sample_0[2][i_sb] = f_scalefactor_0 * \
683 (requantization.f_slope * (f_dummy = \
684 (float)(p_adec->bit_stream.fifo.buffer >> (32 - requantization.i_bits_per_codeword))) + \
685 requantization.f_offset); \
686 DumpBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
687 i_dump += requantization.i_bits_per_codeword; \
688 ppf_sample_1[2][i_sb] = f_scalefactor_1 * \
689 (requantization.f_slope * f_dummy + requantization.f_offset); \
693 NeedBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
694 i_need += requantization.i_bits_per_codeword; \
695 pf_ungroup = requantization.pf_ungroup + 3 * \
696 (p_adec->bit_stream.fifo.buffer >> (32 - requantization.i_bits_per_codeword)); \
697 DumpBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
698 i_dump += requantization.i_bits_per_codeword; \
700 ppf_sample_0[0][i_sb] = (f_scalefactor_0 = (pf_scalefactor_0)[i_sb]) * pf_ungroup[0]; \
701 ppf_sample_0[1][i_sb] = f_scalefactor_0 * pf_ungroup[1]; \
702 ppf_sample_0[2][i_sb] = f_scalefactor_0 * pf_ungroup[2]; \
704 ppf_sample_1[0][i_sb] = (f_scalefactor_1 = (pf_scalefactor_1)[i_sb]) * pf_ungroup[0]; \
705 ppf_sample_1[1][i_sb] = f_scalefactor_1 * pf_ungroup[1]; \
706 ppf_sample_1[2][i_sb] = f_scalefactor_1 * pf_ungroup[2]; \
711 /* fprintf(stderr, "%p", p_s16); */ \
712 DCT32( ppf_sample_0[0], &p_adec->bank_0 ); \
713 PCM( &p_adec->bank_0, &p_s16, 2 ); \
714 /* fprintf(stderr, " %p", p_s16); */ \
716 /* fprintf(stderr, " %p\n", p_s16); */ \
718 /* fprintf(stderr, "%p", p_s16); */ \
719 DCT32( ppf_sample_1[0], &p_adec->bank_1 ); \
720 PCM( &p_adec->bank_1, &p_s16, 2 ); \
721 /* fprintf(stderr, " %p", p_s16); */ \
723 /* fprintf(stderr, " %p\n", p_s16); */ \
725 /* fprintf(stderr, "%p", p_s16); */ \
726 DCT32( ppf_sample_0[1], &p_adec->bank_0 ); \
727 PCM( &p_adec->bank_0, &p_s16, 2 ); \
728 /* fprintf(stderr, " %p", p_s16); */ \
730 /* fprintf(stderr, " %p\n", p_s16); */ \
732 /* fprintf(stderr, "%p", p_s16); */ \
733 DCT32( ppf_sample_1[1], &p_adec->bank_1 ); \
734 PCM( &p_adec->bank_1, &p_s16, 2 ); \
735 /* fprintf(stderr, " %p", p_s16); */ \
737 /* fprintf(stderr, " %p\n", p_s16); */ \
739 /* fprintf(stderr, "%p", p_s16); */ \
740 DCT32( ppf_sample_0[2], &p_adec->bank_0 ); \
741 PCM( &p_adec->bank_0, &p_s16, 2 ); \
742 /* fprintf(stderr, " %p", p_s16); */ \
744 /* fprintf(stderr, " %p\n", p_s16); */ \
746 /* fprintf(stderr, "%p", p_s16); */ \
747 DCT32( ppf_sample_1[2], &p_adec->bank_1 ); \
748 PCM( &p_adec->bank_1, &p_s16, 2 ); \
749 /* fprintf(stderr, " %p", p_s16); */ \
751 /* fprintf(stderr, " %p\n", p_s16); */ \
753 /* #define READ_SAMPLE_L2S */
755 l_end_frame = p_adec->p_aout_fifo->l_end_frame;
759 READ_SAMPLE_L2S( pf_scalefactor_0_0, pf_scalefactor_1_0, 2 )
762 READ_SAMPLE_L2S( pf_scalefactor_0_0, pf_scalefactor_1_0, 4 )
765 READ_SAMPLE_L2S( pf_scalefactor_0_1, pf_scalefactor_1_1, 6 )
768 READ_SAMPLE_L2S( pf_scalefactor_0_1, pf_scalefactor_1_1, 8 )
771 READ_SAMPLE_L2S( pf_scalefactor_0_2, pf_scalefactor_1_2, 10 )
774 READ_SAMPLE_L2S( pf_scalefactor_0_2, pf_scalefactor_1_2, 12 )
776 // fprintf(stderr, "adec debug: layer == %i, padding_bit == %i, sampling_frequency == %i, bitrate_index == %i\n",
777 // (i_header & ADEC_HEADER_LAYER_MASK) >> ADEC_HEADER_LAYER_SHIFT,
778 // (i_header & ADEC_HEADER_PADDING_BIT_MASK) >> ADEC_HEADER_PADDING_BIT_SHIFT,
779 // (i_header & ADEC_HEADER_SAMPLING_FREQUENCY_MASK) >> ADEC_HEADER_SAMPLING_FREQUENCY_SHIFT,
780 // (i_header & ADEC_HEADER_BITRATE_INDEX_MASK) >> ADEC_HEADER_BITRATE_INDEX_SHIFT);
781 // fprintf(stderr, "adec debug: framesize == %i, i_need == %i, i_dump == %i\n",
782 // pi_framesize[ 128 * ((i_header & ADEC_HEADER_LAYER_MASK) >> ADEC_HEADER_LAYER_SHIFT) +
783 // 64 * ((i_header & ADEC_HEADER_PADDING_BIT_MASK) >> ADEC_HEADER_PADDING_BIT_SHIFT) +
784 // 16 * ((i_header & ADEC_HEADER_SAMPLING_FREQUENCY_MASK) >> ADEC_HEADER_SAMPLING_FREQUENCY_SHIFT) +
785 // 1 * ((i_header & ADEC_HEADER_BITRATE_INDEX_MASK) >> ADEC_HEADER_BITRATE_INDEX_SHIFT) ],
788 p_adec->bit_stream.fifo.buffer = 0;
789 p_adec->bit_stream.fifo.i_available = 0;
793 /******************************************************************************
794 * InitThread : initialize an audio decoder thread
795 ******************************************************************************
796 * This function is called from RunThread and performs the second step of the
797 * initialization. It returns 0 on success.
798 ******************************************************************************/
799 static int InitThread( adec_thread_t * p_adec )
801 aout_fifo_t aout_fifo;
803 intf_DbgMsg("adec debug: initializing audio decoder thread %p\n", p_adec);
805 /* Our first job is to initialize the bit stream structure with the
806 * beginning of the input stream */
807 pthread_mutex_lock( &p_adec->fifo.data_lock );
808 while ( DECODER_FIFO_ISEMPTY(p_adec->fifo) )
810 pthread_cond_wait( &p_adec->fifo.data_wait, &p_adec->fifo.data_lock );
812 p_adec->bit_stream.p_ts = DECODER_FIFO_START( p_adec->fifo )->p_first_ts;
813 p_adec->bit_stream.i_byte = p_adec->bit_stream.p_ts->i_payload_start;
814 pthread_mutex_unlock( &p_adec->fifo.data_lock );
816 /* Now we look for an audio frame header in the input stream */
817 if ( FindHeader(p_adec) )
819 return( -1 ); /* b_die or b_error is set */
823 * We have the header and all its informations : we must be able to create
824 * the audio output fifo.
827 /* Is the sound in mono mode or stereo mode ? */
828 if ( (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_MODE_MASK) == ADEC_HEADER_MODE_MASK )
830 intf_DbgMsg("adec debug: mode == mono\n");
831 aout_fifo.i_type = AOUT_ADEC_MONO_FIFO;
832 aout_fifo.b_stereo = 0;
836 intf_DbgMsg("adec debug: mode == stereo\n");
837 aout_fifo.i_type = AOUT_ADEC_STEREO_FIFO;
838 aout_fifo.b_stereo = 1;
841 /* Checking the sampling frequency */
842 switch ( (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_SAMPLING_FREQUENCY_MASK) \
843 >> ADEC_HEADER_SAMPLING_FREQUENCY_SHIFT )
846 intf_DbgMsg("adec debug: sampling_frequency == 44100 Hz\n");
847 aout_fifo.l_rate = 44100;
851 intf_DbgMsg("adec debug: sampling_frequency == 48000 Hz\n");
852 aout_fifo.l_rate = 48000;
856 intf_DbgMsg("adec debug: sampling_frequency == 32000 Hz\n");
857 aout_fifo.l_rate = 32000;
861 intf_ErrMsg("adec error: can't create audio output fifo (sampling_frequency == `reserved')\n");
865 /* Creating the audio output fifo */
866 if ( (p_adec->p_aout_fifo = aout_CreateFifo(p_adec->p_aout, &aout_fifo)) == NULL )
871 intf_DbgMsg("adec debug: audio decoder thread %p initialized\n", p_adec);
875 /******************************************************************************
876 * RunThread : audio decoder thread
877 ******************************************************************************
878 * Audio decoder thread. This function does only returns when the thread is
880 ******************************************************************************/
881 static void RunThread( adec_thread_t * p_adec )
883 // static const int pi_framesize[512] = ADEC_FRAME_SIZE;
887 mtime_t adec_date = 0;
889 intf_DbgMsg("adec debug: running audio decoder thread (%p) (pid == %i)\n", p_adec, getpid());
891 /* Initializing the audio decoder thread */
892 if ( InitThread(p_adec) )
897 /* Audio decoder thread's main loop */
898 while ( (!p_adec->b_die) && (!p_adec->b_error) )
900 switch ( (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_LAYER_MASK) >> ADEC_HEADER_LAYER_SHIFT )
903 intf_DbgMsg("adec debug: layer == 0 (reserved)\n");
904 p_adec->bit_stream.fifo.buffer = 0;
905 p_adec->bit_stream.fifo.i_available = 0;
909 p_adec->bit_stream.fifo.buffer = 0;
910 p_adec->bit_stream.fifo.i_available = 0;
914 if ( (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_MODE_MASK) == ADEC_HEADER_MODE_MASK )
916 adec_Layer2_Mono( p_adec );
920 // i_header = p_adec->bit_stream.fifo.buffer;
921 // i_framesize = pi_framesize[ 128*((i_header & ADEC_HEADER_LAYER_MASK) >> ADEC_HEADER_LAYER_SHIFT) +
922 // 64*((i_header & ADEC_HEADER_PADDING_BIT_MASK) >> ADEC_HEADER_PADDING_BIT_SHIFT) +
923 // 16*((i_header & ADEC_HEADER_SAMPLING_FREQUENCY_MASK) >> ADEC_HEADER_SAMPLING_FREQUENCY_SHIFT) +
924 // 1*((i_header & ADEC_HEADER_BITRATE_INDEX_MASK) >> ADEC_HEADER_BITRATE_INDEX_SHIFT) ];
925 // for ( i_dummy = 0; i_dummy < i_framesize; i_dummy++ )
927 // GetByte( &p_adec->bit_stream );
929 // for ( i_dummy = 0; i_dummy < 512; i_dummy++ )
931 // p_adec->bank_0.v1[ i_dummy ] = .0;
932 // p_adec->bank_1.v1[ i_dummy ] = .0;
933 // p_adec->bank_0.v2[ i_dummy ] = .0;
934 // p_adec->bank_1.v2[ i_dummy ] = .0;
937 pthread_mutex_lock( &p_adec->p_aout_fifo->data_lock );
938 while ( (((p_adec->p_aout_fifo->l_end_frame + 6) - p_adec->p_aout_fifo->l_start_frame) & AOUT_FIFO_SIZE) < 6 ) /* !! */
940 pthread_cond_wait( &p_adec->p_aout_fifo->data_wait, &p_adec->p_aout_fifo->data_lock );
942 pthread_mutex_unlock( &p_adec->p_aout_fifo->data_lock );
944 if ( adec_Layer2_Stereo(p_adec) )
946 pthread_mutex_lock( &p_adec->p_aout_fifo->data_lock );
948 p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] = adec_date;
949 p_adec->p_aout_fifo->l_end_frame += 1;
950 p_adec->p_aout_fifo->l_end_frame &= AOUT_FIFO_SIZE;
952 p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] = LAST_MDATE;
953 p_adec->p_aout_fifo->l_end_frame += 1;
954 p_adec->p_aout_fifo->l_end_frame &= AOUT_FIFO_SIZE;
956 p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] = LAST_MDATE;
957 p_adec->p_aout_fifo->l_end_frame += 1;
958 p_adec->p_aout_fifo->l_end_frame &= AOUT_FIFO_SIZE;
960 p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] = LAST_MDATE;
961 p_adec->p_aout_fifo->l_end_frame += 1;
962 p_adec->p_aout_fifo->l_end_frame &= AOUT_FIFO_SIZE;
964 p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] = LAST_MDATE;
965 p_adec->p_aout_fifo->l_end_frame += 1;
966 p_adec->p_aout_fifo->l_end_frame &= AOUT_FIFO_SIZE;
968 p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] = LAST_MDATE;
969 p_adec->p_aout_fifo->l_end_frame += 1;
970 p_adec->p_aout_fifo->l_end_frame &= AOUT_FIFO_SIZE;
971 pthread_mutex_unlock( &p_adec->p_aout_fifo->data_lock );
972 adec_date += 24000; /* !! */
978 if ( (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_MODE_MASK) == ADEC_HEADER_MODE_MASK )
980 adec_Layer1_Mono( p_adec );
984 adec_Layer1_Stereo( p_adec );
989 intf_DbgMsg("adec debug: layer == %i (unknown)\n",
990 (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_LAYER_MASK) >> ADEC_HEADER_LAYER_SHIFT);
991 p_adec->bit_stream.fifo.buffer = 0;
992 p_adec->bit_stream.fifo.i_available = 0;
995 FindHeader( p_adec );
998 /* If b_error is set, the audio decoder thread enters the error loop */
999 if ( p_adec->b_error )
1001 ErrorThread( p_adec );
1004 /* End of the audio decoder thread */
1005 EndThread( p_adec );
1008 /******************************************************************************
1009 * ErrorThread : audio decoder's RunThread() error loop
1010 ******************************************************************************
1011 * This function is called when an error occured during thread main's loop. The
1012 * thread can still receive feed, but must be ready to terminate as soon as
1014 ******************************************************************************/
1015 static void ErrorThread( adec_thread_t *p_adec )
1017 /* We take the lock, because we are going to read/write the start/end
1018 * indexes of the decoder fifo */
1019 pthread_mutex_lock( &p_adec->fifo.data_lock );
1021 /* Wait until a `die' order is sent */
1022 while( !p_adec->b_die )
1024 /* Trash all received PES packets */
1025 while( !DECODER_FIFO_ISEMPTY(p_adec->fifo) )
1027 input_NetlistFreePES( p_adec->bit_stream.p_input, DECODER_FIFO_START(p_adec->fifo) );
1028 DECODER_FIFO_INCSTART( p_adec->fifo );
1030 // fprintf(stderr, "*");
1034 /* Waiting for the input thread to put new PES packets in the fifo */
1035 pthread_cond_wait( &p_adec->fifo.data_wait, &p_adec->fifo.data_lock );
1038 /* We can release the lock before leaving */
1039 pthread_mutex_unlock( &p_adec->fifo.data_lock );
1042 /******************************************************************************
1043 * EndThread : audio decoder thread destruction
1044 ******************************************************************************
1045 * This function is called when the thread ends after a sucessfull
1047 ******************************************************************************/
1048 static void EndThread( adec_thread_t *p_adec )
1050 intf_DbgMsg("adec debug: destroying audio decoder thread %p\n", p_adec);
1052 /* If the audio output fifo was created, we destroy it */
1053 if ( p_adec->p_aout_fifo != NULL )
1055 aout_DestroyFifo( p_adec->p_aout_fifo );
1057 /* Destroy descriptor */
1060 intf_DbgMsg("adec debug: audio decoder thread %p destroyed\n", p_adec);