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 );
50 static int adec_Layer1_Mono ( adec_thread_t * p_adec );
51 static int adec_Layer1_Stereo ( adec_thread_t * p_adec );
52 static int adec_Layer2_Mono ( adec_thread_t * p_adec );
53 static int adec_Layer2_Stereo ( adec_thread_t * p_adec );
55 static byte_t GetByte ( bit_stream_t * p_bit_stream );
56 static void NeedBits ( bit_stream_t * p_bit_stream, int i_bits );
57 static void DumpBits ( bit_stream_t * p_bit_stream, int i_bits );
58 static int FindHeader ( adec_thread_t * p_adec );
60 /******************************************************************************
61 * adec_CreateThread: creates an audio decoder thread
62 ******************************************************************************
63 * This function creates a new audio decoder thread, and returns a pointer to
64 * its description. On error, it returns NULL.
65 ******************************************************************************/
66 adec_thread_t * adec_CreateThread( input_thread_t * p_input )
68 adec_thread_t * p_adec;
70 intf_DbgMsg("adec debug: creating audio decoder thread\n");
72 /* Allocate the memory needed to store the thread's structure */
73 if ( (p_adec = (adec_thread_t *)malloc( sizeof(adec_thread_t) )) == NULL )
75 intf_ErrMsg("adec error: not enough memory for adec_CreateThread() to create the new thread\n");
80 * Initialize the thread properties
86 * Initialize the input properties
88 /* Initialize the decoder fifo's data lock and conditional variable and set
89 * its buffer as empty */
90 vlc_mutex_init( &p_adec->fifo.data_lock );
91 vlc_cond_init( &p_adec->fifo.data_wait );
92 p_adec->fifo.i_start = 0;
93 p_adec->fifo.i_end = 0;
94 /* Initialize the bit stream structure */
95 p_adec->bit_stream.p_input = p_input;
96 p_adec->bit_stream.p_decoder_fifo = &p_adec->fifo;
97 p_adec->bit_stream.fifo.buffer = 0;
98 p_adec->bit_stream.fifo.i_available = 0;
101 * Initialize the decoder properties
103 p_adec->bank_0.actual = p_adec->bank_0.v1;
104 p_adec->bank_0.pos = 0;
105 p_adec->bank_1.actual = p_adec->bank_1.v1;
106 p_adec->bank_1.pos = 0;
109 * Initialize the output properties
111 p_adec->p_aout = p_input->p_aout;
112 p_adec->p_aout_fifo = NULL;
114 /* Spawn the audio decoder thread */
115 if ( vlc_thread_create(&p_adec->thread_id, "audio decoder", (vlc_thread_func_t)RunThread, (void *)p_adec) )
117 intf_ErrMsg("adec error: can't spawn audio decoder thread\n");
122 intf_DbgMsg("adec debug: audio decoder thread (%p) created\n", p_adec);
126 /******************************************************************************
127 * adec_DestroyThread: destroys an audio decoder thread
128 ******************************************************************************
129 * This function asks an audio decoder thread to terminate. This function has
130 * not to wait until the decoder thread has really died, because the killer (ie
131 * this function's caller) is the input thread, that's why we are sure that no
132 * other thread will try to access to this thread's descriptor after its
134 ******************************************************************************/
135 void adec_DestroyThread( adec_thread_t * p_adec )
137 intf_DbgMsg("adec debug: requesting termination of audio decoder thread %p\n", p_adec);
139 /* Ask thread to kill itself */
141 /* Make sure the decoder thread leaves the GetByte() function */
142 vlc_mutex_lock( &(p_adec->fifo.data_lock) );
143 vlc_cond_signal( &(p_adec->fifo.data_wait) );
144 vlc_mutex_unlock( &(p_adec->fifo.data_lock) );
146 /* Waiting for the decoder thread to exit */
147 /* Remove this as soon as the "status" flag is implemented */
148 vlc_thread_join( p_adec->thread_id );
151 /* Following functions are local */
153 /******************************************************************************
154 * FindHeader : parses an input stream until an audio frame header could be
156 ******************************************************************************
157 * When this function returns successfully, the header can be found in the
158 * buffer of the bit stream fifo.
159 ******************************************************************************/
160 static int FindHeader( adec_thread_t * p_adec )
162 while ( (!p_adec->b_die) && (!p_adec->b_error) )
164 NeedBits( &p_adec->bit_stream, 32 );
165 if ( (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_SYNCWORD_MASK) == ADEC_HEADER_SYNCWORD_MASK )
169 DumpBits( &p_adec->bit_stream, 8 );
175 /******************************************************************************
176 * adec_Layer`L'_`M': decodes an mpeg 1, layer `L', mode `M', audio frame
177 ******************************************************************************
178 * These functions decode the audio frame which has already its header loaded
179 * in the i_header member of the audio decoder thread structure and its first
180 * byte of data described by the bit stream structure of the audio decoder
181 * thread (there is no bit available in the bit buffer yet)
182 ******************************************************************************/
184 /******************************************************************************
186 ******************************************************************************/
187 static __inline__ int adec_Layer1_Mono( adec_thread_t * p_adec )
189 p_adec->bit_stream.fifo.buffer = 0;
190 p_adec->bit_stream.fifo.i_available = 0;
194 /******************************************************************************
196 ******************************************************************************/
197 static __inline__ int adec_Layer1_Stereo( adec_thread_t * p_adec )
199 p_adec->bit_stream.fifo.buffer = 0;
200 p_adec->bit_stream.fifo.i_available = 0;
204 /******************************************************************************
206 ******************************************************************************/
207 static __inline__ int adec_Layer2_Mono( adec_thread_t * p_adec )
209 p_adec->bit_stream.fifo.buffer = 0;
210 p_adec->bit_stream.fifo.i_available = 0;
214 /******************************************************************************
216 ******************************************************************************/
217 static __inline__ int adec_Layer2_Stereo( adec_thread_t * p_adec )
219 typedef struct requantization_s
221 byte_t i_bits_per_codeword;
222 const float * pf_ungroup;
227 static const float pf_scalefactor[64] = ADEC_SCALE_FACTOR;
230 static int i_sampling_frequency, i_mode, i_bound;
231 static int pi_allocation_0[32], pi_allocation_1[32]; /* see ISO/IEC 11172-3 2.4.1.6 */
233 float f_scalefactor_0, f_scalefactor_1;
235 static const byte_t ppi_bitrate_per_channel_index[4][15] = ADEC_LAYER2_BITRATE_PER_CHANNEL_INDEX;
236 static const byte_t ppi_sblimit[3][11] = ADEC_LAYER2_SBLIMIT;
237 static const byte_t ppi_nbal[2][32] = ADEC_LAYER2_NBAL;
239 static const float pf_ungroup3[3*3*3 * 3] = ADEC_LAYER2_UNGROUP3;
240 static const float pf_ungroup5[5*5*5 * 3] = ADEC_LAYER2_UNGROUP5;
241 static const float pf_ungroup9[9*9*9 * 3] = ADEC_LAYER2_UNGROUP9;
243 static const requantization_t p_requantization_cd[16] = ADEC_LAYER2_REQUANTIZATION_CD;
244 static const requantization_t p_requantization_ab1[16] = ADEC_LAYER2_REQUANTIZATION_AB1;
245 static const requantization_t p_requantization_ab2[16] = ADEC_LAYER2_REQUANTIZATION_AB2;
246 static const requantization_t p_requantization_ab3[16] = ADEC_LAYER2_REQUANTIZATION_AB3;
247 static const requantization_t p_requantization_ab4[16] = ADEC_LAYER2_REQUANTIZATION_AB4;
248 static const requantization_t * pp_requantization_ab[30] = ADEC_LAYER2_REQUANTIZATION_AB;
250 static int i_sblimit, i_bitrate_per_channel_index;
251 static int pi_scfsi_0[30], pi_scfsi_1[30];
252 static const byte_t * pi_nbal;
253 static float ppf_sample_0[3][32], ppf_sample_1[3][32];
254 static const requantization_t * pp_requantization_0[30];
255 static const requantization_t * pp_requantization_1[30];
256 static requantization_t requantization;
257 static const float * pf_ungroup;
259 static float pf_scalefactor_0_0[30], pf_scalefactor_0_1[30], pf_scalefactor_0_2[30];
260 static float pf_scalefactor_1_0[30], pf_scalefactor_1_1[30], pf_scalefactor_1_2[30];
268 int i_need = 0, i_dump = 0;
269 // static const int pi_framesize[512] = ADEC_FRAME_SIZE;
271 /* Read the audio frame header and flush the bit buffer */
272 i_header = p_adec->bit_stream.fifo.buffer;
273 p_adec->bit_stream.fifo.buffer = 0;
274 p_adec->bit_stream.fifo.i_available = 0;
275 /* Read the sampling frequency (see ISO/IEC 11172-3 2.4.2.3) */
276 i_sampling_frequency = (int)((i_header & ADEC_HEADER_SAMPLING_FREQUENCY_MASK)
277 >> ADEC_HEADER_SAMPLING_FREQUENCY_SHIFT);
278 /* Read the mode (see ISO/IEC 11172-3 2.4.2.3) */
279 i_mode = (int)((i_header & ADEC_HEADER_MODE_MASK) >> ADEC_HEADER_MODE_SHIFT);
280 /* If a CRC can be found in the frame, get rid of it */
281 if ( (i_header & ADEC_HEADER_PROTECTION_BIT_MASK) == 0 )
283 GetByte( &p_adec->bit_stream );
284 GetByte( &p_adec->bit_stream );
287 /* Find out the bitrate per channel index */
288 i_bitrate_per_channel_index = (int)ppi_bitrate_per_channel_index[i_mode]
289 [(i_header & ADEC_HEADER_BITRATE_INDEX_MASK) >> ADEC_HEADER_BITRATE_INDEX_SHIFT];
290 /* Find out the number of subbands */
291 i_sblimit = (int)ppi_sblimit[i_sampling_frequency][i_bitrate_per_channel_index];
292 /* Check if the frame is valid or not */
293 if ( i_sblimit == 0 )
295 return( 0 ); /* the frame is invalid */
297 /* Find out the number of bits allocated */
298 pi_nbal = ppi_nbal[ (i_bitrate_per_channel_index <= 2) ? 0 : 1 ];
300 /* Find out the `bound' subband (see ISO/IEC 11172-3 2.4.2.3) */
303 i_bound = (int)(((i_header & ADEC_HEADER_MODE_EXTENSION_MASK) >> (ADEC_HEADER_MODE_EXTENSION_SHIFT - 2)) + 4);
304 if ( i_bound > i_sblimit )
314 /* Read the allocation information (see ISO/IEC 11172-3 2.4.1.6) */
315 for ( i_sb = 0; i_sb < i_bound; i_sb++ )
317 i_2nbal = 2 * (i_nbal = (int)pi_nbal[ i_sb ]);
318 NeedBits( &p_adec->bit_stream, i_2nbal );
320 pi_allocation_0[ i_sb ] = (int)(p_adec->bit_stream.fifo.buffer >> (32 - i_nbal));
321 p_adec->bit_stream.fifo.buffer <<= i_nbal;
322 pi_allocation_1[ i_sb ] = (int)(p_adec->bit_stream.fifo.buffer >> (32 - i_nbal));
323 p_adec->bit_stream.fifo.buffer <<= i_nbal;
324 p_adec->bit_stream.fifo.i_available -= i_2nbal;
327 for ( ; i_sb < i_sblimit; i_sb++ )
329 i_nbal = (int)pi_nbal[ i_sb ];
330 NeedBits( &p_adec->bit_stream, i_nbal );
332 pi_allocation_0[ i_sb ] = (int)(p_adec->bit_stream.fifo.buffer >> (32 - i_nbal));
333 DumpBits( &p_adec->bit_stream, i_nbal );
337 #define MACRO( p_requantization ) \
338 for ( i_sb = 0; i_sb < i_bound; i_sb++ ) \
340 if ( pi_allocation_0[i_sb] ) \
342 pp_requantization_0[i_sb] = &((p_requantization)[pi_allocation_0[i_sb]]); \
343 NeedBits( &p_adec->bit_stream, 2 ); \
345 pi_scfsi_0[i_sb] = (int)(p_adec->bit_stream.fifo.buffer >> (32 - 2)); \
346 DumpBits( &p_adec->bit_stream, 2 ); \
351 ppf_sample_0[0][i_sb] = .0; \
352 ppf_sample_0[1][i_sb] = .0; \
353 ppf_sample_0[2][i_sb] = .0; \
356 if ( pi_allocation_1[i_sb] ) \
358 pp_requantization_1[i_sb] = &((p_requantization)[pi_allocation_1[i_sb]]); \
359 NeedBits( &p_adec->bit_stream, 2 ); \
361 pi_scfsi_1[i_sb] = (int)(p_adec->bit_stream.fifo.buffer >> (32 - 2)); \
362 DumpBits( &p_adec->bit_stream, 2 ); \
367 ppf_sample_1[0][i_sb] = .0; \
368 ppf_sample_1[1][i_sb] = .0; \
369 ppf_sample_1[2][i_sb] = .0; \
373 for ( ; i_sb < i_sblimit; i_sb++ ) \
375 if ( pi_allocation_0[i_sb] ) \
377 pp_requantization_0[i_sb] = &((p_requantization)[pi_allocation_0[i_sb]]); \
378 NeedBits( &p_adec->bit_stream, 4 ); \
380 pi_scfsi_0[i_sb] = (int)(p_adec->bit_stream.fifo.buffer >> (32 - 2)); \
381 p_adec->bit_stream.fifo.buffer <<= 2; \
382 pi_scfsi_1[i_sb] = (int)(p_adec->bit_stream.fifo.buffer >> (32 - 2)); \
383 p_adec->bit_stream.fifo.buffer <<= 2; \
384 p_adec->bit_stream.fifo.i_available -= 4; \
389 ppf_sample_0[0][i_sb] = .0; \
390 ppf_sample_0[1][i_sb] = .0; \
391 ppf_sample_0[2][i_sb] = .0; \
392 ppf_sample_1[0][i_sb] = .0; \
393 ppf_sample_1[1][i_sb] = .0; \
394 ppf_sample_1[2][i_sb] = .0; \
399 if ( i_bitrate_per_channel_index <= 2 )
401 MACRO( p_requantization_cd )
405 MACRO( pp_requantization_ab[i_sb] )
408 #define SWITCH( pi_scfsi, pf_scalefactor_0, pf_scalefactor_1, pf_scalefactor_2 ) \
409 switch ( (pi_scfsi)[i_sb] ) \
412 NeedBits( &p_adec->bit_stream, (3*6) ); \
414 (pf_scalefactor_0)[i_sb] = pf_scalefactor[p_adec->bit_stream.fifo.buffer >> (32 - 6)]; \
415 p_adec->bit_stream.fifo.buffer <<= 6; \
416 (pf_scalefactor_1)[i_sb] = pf_scalefactor[p_adec->bit_stream.fifo.buffer >> (32 - 6)]; \
417 p_adec->bit_stream.fifo.buffer <<= 6; \
418 (pf_scalefactor_2)[i_sb] = pf_scalefactor[p_adec->bit_stream.fifo.buffer >> (32 - 6)]; \
419 p_adec->bit_stream.fifo.buffer <<= 6; \
420 p_adec->bit_stream.fifo.i_available -= (3*6); \
425 NeedBits( &p_adec->bit_stream, (2*6) ); \
427 (pf_scalefactor_0)[i_sb] = \
428 (pf_scalefactor_1)[i_sb] = pf_scalefactor[p_adec->bit_stream.fifo.buffer >> (32 - 6)]; \
429 p_adec->bit_stream.fifo.buffer <<= 6; \
430 (pf_scalefactor_2)[i_sb] = pf_scalefactor[p_adec->bit_stream.fifo.buffer >> (32 - 6)]; \
431 p_adec->bit_stream.fifo.buffer <<= 6; \
432 p_adec->bit_stream.fifo.i_available -= (2*6); \
437 NeedBits( &p_adec->bit_stream, (1*6) ); \
439 (pf_scalefactor_0)[i_sb] = \
440 (pf_scalefactor_1)[i_sb] = \
441 (pf_scalefactor_2)[i_sb] = pf_scalefactor[p_adec->bit_stream.fifo.buffer >> (32 - 6)]; \
442 DumpBits( &p_adec->bit_stream, (1*6) ); \
447 NeedBits( &p_adec->bit_stream, (2*6) ); \
449 (pf_scalefactor_0)[i_sb] = pf_scalefactor[p_adec->bit_stream.fifo.buffer >> (32 - 6)]; \
450 p_adec->bit_stream.fifo.buffer <<= 6; \
451 (pf_scalefactor_1)[i_sb] = \
452 (pf_scalefactor_2)[i_sb] = pf_scalefactor[p_adec->bit_stream.fifo.buffer >> (32 - 6)]; \
453 p_adec->bit_stream.fifo.buffer <<= 6; \
454 p_adec->bit_stream.fifo.i_available -= (2*6); \
460 for ( i_sb = 0; i_sb < i_bound; i_sb++ )
462 if ( pi_allocation_0[i_sb] )
464 SWITCH( pi_scfsi_0, pf_scalefactor_0_0, pf_scalefactor_0_1, pf_scalefactor_0_2 )
466 if ( pi_allocation_1[i_sb] )
468 SWITCH( pi_scfsi_1, pf_scalefactor_1_0, pf_scalefactor_1_1, pf_scalefactor_1_2 )
471 for ( ; i_sb < i_sblimit; i_sb++ )
473 if ( pi_allocation_0[i_sb] )
475 SWITCH( pi_scfsi_0, pf_scalefactor_0_0, pf_scalefactor_0_1, pf_scalefactor_0_2 )
476 SWITCH( pi_scfsi_1, pf_scalefactor_1_0, pf_scalefactor_1_1, pf_scalefactor_1_2 )
479 for ( ; i_sb < 32; i_sb++ )
481 ppf_sample_0[0][i_sb] = .0;
482 ppf_sample_0[1][i_sb] = .0;
483 ppf_sample_0[2][i_sb] = .0;
484 ppf_sample_1[0][i_sb] = .0;
485 ppf_sample_1[1][i_sb] = .0;
486 ppf_sample_1[2][i_sb] = .0;
490 /* fprintf(stderr, "%p\n", p_adec->p_aout_fifo->buffer); */ \
491 /* fprintf(stderr, "l_end_frame == %li, %p\n", l_end_frame, (aout_frame_t *)p_adec->p_aout_fifo->buffer + l_end_frame); */ \
492 p_s16 = ((aout_frame_t *)p_adec->p_aout_fifo->buffer)[ l_end_frame ]; \
493 /* fprintf(stderr, "p_s16 == %p\n", p_s16); */ \
495 l_end_frame &= AOUT_FIFO_SIZE;
496 /* #define NEXT_BUF */
498 #define GROUPTEST( pp_requantization, ppf_sample, pf_sf ) \
499 requantization = *((pp_requantization)[i_sb]); \
500 if ( requantization.pf_ungroup == NULL ) \
502 NeedBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
503 i_need += requantization.i_bits_per_codeword; \
504 (ppf_sample)[0][i_sb] = (f_scalefactor_0 = (pf_sf)[i_sb]) * (requantization.f_slope * \
505 (p_adec->bit_stream.fifo.buffer >> (32 - requantization.i_bits_per_codeword)) + requantization.f_offset); \
506 DumpBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
507 i_dump += requantization.i_bits_per_codeword; \
509 NeedBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
510 i_need += requantization.i_bits_per_codeword; \
511 (ppf_sample)[1][i_sb] = f_scalefactor_0 * (requantization.f_slope * \
512 (p_adec->bit_stream.fifo.buffer >> (32 - requantization.i_bits_per_codeword)) + requantization.f_offset); \
513 DumpBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
514 i_dump += requantization.i_bits_per_codeword; \
516 NeedBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
517 i_need += requantization.i_bits_per_codeword; \
518 (ppf_sample)[2][i_sb] = f_scalefactor_0 * (requantization.f_slope * \
519 (p_adec->bit_stream.fifo.buffer >> (32 - requantization.i_bits_per_codeword)) + requantization.f_offset); \
520 DumpBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
521 i_dump += requantization.i_bits_per_codeword; \
525 NeedBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
526 i_need += requantization.i_bits_per_codeword; \
527 pf_ungroup = requantization.pf_ungroup + 3 * \
528 (p_adec->bit_stream.fifo.buffer >> (32 - requantization.i_bits_per_codeword)); \
529 DumpBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
530 i_dump += requantization.i_bits_per_codeword; \
531 (ppf_sample)[0][i_sb] = (f_scalefactor_0 = (pf_sf)[i_sb]) * pf_ungroup[0]; \
532 (ppf_sample)[1][i_sb] = f_scalefactor_0 * pf_ungroup[1]; \
533 (ppf_sample)[2][i_sb] = f_scalefactor_0 * pf_ungroup[2]; \
535 /* #define GROUPTEST */
537 #define READ_SAMPLE_L2S( pf_scalefactor_0, pf_scalefactor_1, i_grlimit ) \
538 for ( ; i_gr < (i_grlimit); i_gr++ ) \
540 for ( i_sb = 0; i_sb < i_bound; i_sb++ ) \
542 if ( pi_allocation_0[i_sb] ) \
544 GROUPTEST( pp_requantization_0, ppf_sample_0, (pf_scalefactor_0) ) \
546 if ( pi_allocation_1[i_sb] ) \
548 GROUPTEST( pp_requantization_1, ppf_sample_1, (pf_scalefactor_1) ) \
551 for ( ; i_sb < i_sblimit; i_sb++ ) \
553 if ( pi_allocation_0[i_sb] ) \
555 requantization = *(pp_requantization_0[i_sb]); \
556 if ( requantization.pf_ungroup == NULL ) \
558 NeedBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
559 i_need += requantization.i_bits_per_codeword; \
560 ppf_sample_0[0][i_sb] = (f_scalefactor_0 = (pf_scalefactor_0)[i_sb]) * \
561 (requantization.f_slope * (f_dummy = \
562 (float)(p_adec->bit_stream.fifo.buffer >> (32 - requantization.i_bits_per_codeword))) + \
563 requantization.f_offset); \
564 DumpBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
565 i_dump += requantization.i_bits_per_codeword; \
566 ppf_sample_1[0][i_sb] = (f_scalefactor_1 = (pf_scalefactor_1)[i_sb]) * \
567 (requantization.f_slope * f_dummy + requantization.f_offset); \
569 NeedBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
570 i_need += requantization.i_bits_per_codeword; \
571 ppf_sample_0[1][i_sb] = f_scalefactor_0 * \
572 (requantization.f_slope * (f_dummy = \
573 (float)(p_adec->bit_stream.fifo.buffer >> (32 - requantization.i_bits_per_codeword))) + \
574 requantization.f_offset); \
575 DumpBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
576 i_dump += requantization.i_bits_per_codeword; \
577 ppf_sample_1[1][i_sb] = f_scalefactor_1 * \
578 (requantization.f_slope * f_dummy + requantization.f_offset); \
580 NeedBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
581 i_need += requantization.i_bits_per_codeword; \
582 ppf_sample_0[2][i_sb] = f_scalefactor_0 * \
583 (requantization.f_slope * (f_dummy = \
584 (float)(p_adec->bit_stream.fifo.buffer >> (32 - requantization.i_bits_per_codeword))) + \
585 requantization.f_offset); \
586 DumpBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
587 i_dump += requantization.i_bits_per_codeword; \
588 ppf_sample_1[2][i_sb] = f_scalefactor_1 * \
589 (requantization.f_slope * f_dummy + requantization.f_offset); \
593 NeedBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
594 i_need += requantization.i_bits_per_codeword; \
595 pf_ungroup = requantization.pf_ungroup + 3 * \
596 (p_adec->bit_stream.fifo.buffer >> (32 - requantization.i_bits_per_codeword)); \
597 DumpBits( &p_adec->bit_stream, requantization.i_bits_per_codeword ); \
598 i_dump += requantization.i_bits_per_codeword; \
600 ppf_sample_0[0][i_sb] = (f_scalefactor_0 = (pf_scalefactor_0)[i_sb]) * pf_ungroup[0]; \
601 ppf_sample_0[1][i_sb] = f_scalefactor_0 * pf_ungroup[1]; \
602 ppf_sample_0[2][i_sb] = f_scalefactor_0 * pf_ungroup[2]; \
604 ppf_sample_1[0][i_sb] = (f_scalefactor_1 = (pf_scalefactor_1)[i_sb]) * pf_ungroup[0]; \
605 ppf_sample_1[1][i_sb] = f_scalefactor_1 * pf_ungroup[1]; \
606 ppf_sample_1[2][i_sb] = f_scalefactor_1 * pf_ungroup[2]; \
611 /* fprintf(stderr, "%p", p_s16); */ \
612 DCT32( ppf_sample_0[0], &p_adec->bank_0 ); \
613 PCM( &p_adec->bank_0, &p_s16, 2 ); \
614 /* fprintf(stderr, " %p", p_s16); */ \
616 /* fprintf(stderr, " %p\n", p_s16); */ \
618 /* fprintf(stderr, "%p", p_s16); */ \
619 DCT32( ppf_sample_1[0], &p_adec->bank_1 ); \
620 PCM( &p_adec->bank_1, &p_s16, 2 ); \
621 /* fprintf(stderr, " %p", p_s16); */ \
623 /* fprintf(stderr, " %p\n", p_s16); */ \
625 /* fprintf(stderr, "%p", p_s16); */ \
626 DCT32( ppf_sample_0[1], &p_adec->bank_0 ); \
627 PCM( &p_adec->bank_0, &p_s16, 2 ); \
628 /* fprintf(stderr, " %p", p_s16); */ \
630 /* fprintf(stderr, " %p\n", p_s16); */ \
632 /* fprintf(stderr, "%p", p_s16); */ \
633 DCT32( ppf_sample_1[1], &p_adec->bank_1 ); \
634 PCM( &p_adec->bank_1, &p_s16, 2 ); \
635 /* fprintf(stderr, " %p", p_s16); */ \
637 /* fprintf(stderr, " %p\n", p_s16); */ \
639 /* fprintf(stderr, "%p", p_s16); */ \
640 DCT32( ppf_sample_0[2], &p_adec->bank_0 ); \
641 PCM( &p_adec->bank_0, &p_s16, 2 ); \
642 /* fprintf(stderr, " %p", p_s16); */ \
644 /* fprintf(stderr, " %p\n", p_s16); */ \
646 /* fprintf(stderr, "%p", p_s16); */ \
647 DCT32( ppf_sample_1[2], &p_adec->bank_1 ); \
648 PCM( &p_adec->bank_1, &p_s16, 2 ); \
649 /* fprintf(stderr, " %p", p_s16); */ \
651 /* fprintf(stderr, " %p\n", p_s16); */ \
653 /* #define READ_SAMPLE_L2S */
655 l_end_frame = p_adec->p_aout_fifo->l_end_frame;
659 READ_SAMPLE_L2S( pf_scalefactor_0_0, pf_scalefactor_1_0, 2 )
662 READ_SAMPLE_L2S( pf_scalefactor_0_0, pf_scalefactor_1_0, 4 )
665 READ_SAMPLE_L2S( pf_scalefactor_0_1, pf_scalefactor_1_1, 6 )
668 READ_SAMPLE_L2S( pf_scalefactor_0_1, pf_scalefactor_1_1, 8 )
671 READ_SAMPLE_L2S( pf_scalefactor_0_2, pf_scalefactor_1_2, 10 )
674 READ_SAMPLE_L2S( pf_scalefactor_0_2, pf_scalefactor_1_2, 12 )
676 // fprintf(stderr, "adec debug: layer == %i, padding_bit == %i, sampling_frequency == %i, bitrate_index == %i\n",
677 // (i_header & ADEC_HEADER_LAYER_MASK) >> ADEC_HEADER_LAYER_SHIFT,
678 // (i_header & ADEC_HEADER_PADDING_BIT_MASK) >> ADEC_HEADER_PADDING_BIT_SHIFT,
679 // (i_header & ADEC_HEADER_SAMPLING_FREQUENCY_MASK) >> ADEC_HEADER_SAMPLING_FREQUENCY_SHIFT,
680 // (i_header & ADEC_HEADER_BITRATE_INDEX_MASK) >> ADEC_HEADER_BITRATE_INDEX_SHIFT);
681 // fprintf(stderr, "adec debug: framesize == %i, i_need == %i, i_dump == %i\n",
682 // pi_framesize[ 128 * ((i_header & ADEC_HEADER_LAYER_MASK) >> ADEC_HEADER_LAYER_SHIFT) +
683 // 64 * ((i_header & ADEC_HEADER_PADDING_BIT_MASK) >> ADEC_HEADER_PADDING_BIT_SHIFT) +
684 // 16 * ((i_header & ADEC_HEADER_SAMPLING_FREQUENCY_MASK) >> ADEC_HEADER_SAMPLING_FREQUENCY_SHIFT) +
685 // 1 * ((i_header & ADEC_HEADER_BITRATE_INDEX_MASK) >> ADEC_HEADER_BITRATE_INDEX_SHIFT) ],
688 p_adec->bit_stream.fifo.buffer = 0;
689 p_adec->bit_stream.fifo.i_available = 0;
693 /******************************************************************************
694 * InitThread : initialize an audio decoder thread
695 ******************************************************************************
696 * This function is called from RunThread and performs the second step of the
697 * initialization. It returns 0 on success.
698 ******************************************************************************/
699 static int InitThread( adec_thread_t * p_adec )
701 aout_fifo_t aout_fifo;
703 intf_DbgMsg("adec debug: initializing audio decoder thread %p\n", p_adec);
705 /* Our first job is to initialize the bit stream structure with the
706 * beginning of the input stream */
707 vlc_mutex_lock( &p_adec->fifo.data_lock );
708 while ( DECODER_FIFO_ISEMPTY(p_adec->fifo) )
710 vlc_cond_wait( &p_adec->fifo.data_wait, &p_adec->fifo.data_lock );
712 p_adec->bit_stream.p_ts = DECODER_FIFO_START( p_adec->fifo )->p_first_ts;
713 p_adec->bit_stream.i_byte = p_adec->bit_stream.p_ts->i_payload_start;
714 vlc_mutex_unlock( &p_adec->fifo.data_lock );
716 /* Now we look for an audio frame header in the input stream */
717 if ( FindHeader(p_adec) )
719 return( -1 ); /* b_die or b_error is set */
723 * We have the header and all its informations : we must be able to create
724 * the audio output fifo.
727 /* Is the sound in mono mode or stereo mode ? */
728 if ( (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_MODE_MASK) == ADEC_HEADER_MODE_MASK )
730 intf_DbgMsg("adec debug: mode == mono\n");
731 aout_fifo.i_type = AOUT_ADEC_MONO_FIFO;
732 aout_fifo.b_stereo = 0;
736 intf_DbgMsg("adec debug: mode == stereo\n");
737 aout_fifo.i_type = AOUT_ADEC_STEREO_FIFO;
738 aout_fifo.b_stereo = 1;
741 /* Checking the sampling frequency */
742 switch ( (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_SAMPLING_FREQUENCY_MASK) \
743 >> ADEC_HEADER_SAMPLING_FREQUENCY_SHIFT )
746 intf_DbgMsg("adec debug: sampling_frequency == 44100 Hz\n");
747 aout_fifo.l_rate = 44100;
751 intf_DbgMsg("adec debug: sampling_frequency == 48000 Hz\n");
752 aout_fifo.l_rate = 48000;
756 intf_DbgMsg("adec debug: sampling_frequency == 32000 Hz\n");
757 aout_fifo.l_rate = 32000;
761 intf_ErrMsg("adec error: can't create audio output fifo (sampling_frequency == `reserved')\n");
765 aout_fifo.l_frame_size = AOUT_FRAME_SIZE;
767 /* Creating the audio output fifo */
768 if ( (p_adec->p_aout_fifo = aout_CreateFifo(p_adec->p_aout, &aout_fifo)) == NULL )
773 intf_DbgMsg("adec debug: audio decoder thread %p initialized\n", p_adec);
777 /******************************************************************************
778 * RunThread : audio decoder thread
779 ******************************************************************************
780 * Audio decoder thread. This function does only returns when the thread is
782 ******************************************************************************/
783 static void RunThread( adec_thread_t * p_adec )
785 // static const int pi_framesize[512] = ADEC_FRAME_SIZE;
790 intf_DbgMsg("adec debug: running audio decoder thread (%p) (pid == %i)\n", p_adec, getpid());
792 /* Initializing the audio decoder thread */
793 if ( InitThread(p_adec) )
798 /* Audio decoder thread's main loop */
799 while ( (!p_adec->b_die) && (!p_adec->b_error) )
801 switch ( (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_LAYER_MASK) >> ADEC_HEADER_LAYER_SHIFT )
805 intf_DbgMsg("adec debug: layer == 0 (reserved)\n");
806 p_adec->bit_stream.fifo.buffer = 0;
807 p_adec->bit_stream.fifo.i_available = 0;
812 p_adec->bit_stream.fifo.buffer = 0;
813 p_adec->bit_stream.fifo.i_available = 0;
818 if ( (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_MODE_MASK) == ADEC_HEADER_MODE_MASK )
820 adec_Layer2_Mono( p_adec );
824 /* Waiting until there is enough free space in the audio output fifo
825 * in order to store the new decoded frames */
826 vlc_mutex_lock( &p_adec->p_aout_fifo->data_lock );
827 /* adec_Layer2_Stereo() produces 6 output frames (2*1152/384)...
828 * If these 6 frames were recorded in the audio output fifo, the
829 * l_end_frame index would be incremented 6 times. But, if after
830 * this operation the audio output fifo contains less than 6 frames,
831 * it would mean that we had not enough room to store the 6 frames :-P */
832 while ( (((p_adec->p_aout_fifo->l_end_frame + 6) - p_adec->p_aout_fifo->l_start_frame) & AOUT_FIFO_SIZE) < 6 ) /* !! */
834 vlc_cond_wait( &p_adec->p_aout_fifo->data_wait, &p_adec->p_aout_fifo->data_lock );
836 if ( DECODER_FIFO_START(p_adec->fifo)->b_has_pts )
838 p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] = DECODER_FIFO_START(p_adec->fifo)->i_pts;
839 DECODER_FIFO_START(p_adec->fifo)->b_has_pts = 0;
843 p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] = LAST_MDATE;
845 vlc_mutex_unlock( &p_adec->p_aout_fifo->data_lock );
847 /* Decoding the frames */
848 if ( adec_Layer2_Stereo(p_adec) )
850 vlc_mutex_lock( &p_adec->p_aout_fifo->data_lock );
852 p_adec->p_aout_fifo->l_end_frame = (p_adec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
854 p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] = LAST_MDATE;
855 p_adec->p_aout_fifo->l_end_frame = (p_adec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
857 p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] = LAST_MDATE;
858 p_adec->p_aout_fifo->l_end_frame = (p_adec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
860 p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] = LAST_MDATE;
861 p_adec->p_aout_fifo->l_end_frame = (p_adec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
863 p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] = LAST_MDATE;
864 p_adec->p_aout_fifo->l_end_frame = (p_adec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
866 p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] = LAST_MDATE;
867 p_adec->p_aout_fifo->l_end_frame = (p_adec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
868 vlc_mutex_unlock( &p_adec->p_aout_fifo->data_lock );
875 if ( (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_MODE_MASK) == ADEC_HEADER_MODE_MASK )
877 adec_Layer1_Mono( p_adec );
881 adec_Layer1_Stereo( p_adec );
886 intf_DbgMsg("adec debug: layer == %i (unknown)\n",
887 (p_adec->bit_stream.fifo.buffer & ADEC_HEADER_LAYER_MASK) >> ADEC_HEADER_LAYER_SHIFT);
888 p_adec->bit_stream.fifo.buffer = 0;
889 p_adec->bit_stream.fifo.i_available = 0;
892 FindHeader( p_adec );
895 /* If b_error is set, the audio decoder thread enters the error loop */
896 if ( p_adec->b_error )
898 ErrorThread( p_adec );
901 /* End of the audio decoder thread */
905 /******************************************************************************
906 * ErrorThread : audio decoder's RunThread() error loop
907 ******************************************************************************
908 * This function is called when an error occured during thread main's loop. The
909 * thread can still receive feed, but must be ready to terminate as soon as
911 ******************************************************************************/
912 static void ErrorThread( adec_thread_t *p_adec )
914 /* We take the lock, because we are going to read/write the start/end
915 * indexes of the decoder fifo */
916 vlc_mutex_lock( &p_adec->fifo.data_lock );
918 /* Wait until a `die' order is sent */
919 while( !p_adec->b_die )
921 /* Trash all received PES packets */
922 while( !DECODER_FIFO_ISEMPTY(p_adec->fifo) )
924 input_NetlistFreePES( p_adec->bit_stream.p_input, DECODER_FIFO_START(p_adec->fifo) );
925 DECODER_FIFO_INCSTART( p_adec->fifo );
928 /* Waiting for the input thread to put new PES packets in the fifo */
929 vlc_cond_wait( &p_adec->fifo.data_wait, &p_adec->fifo.data_lock );
932 /* We can release the lock before leaving */
933 vlc_mutex_unlock( &p_adec->fifo.data_lock );
936 /******************************************************************************
937 * EndThread : audio decoder thread destruction
938 ******************************************************************************
939 * This function is called when the thread ends after a sucessfull
941 ******************************************************************************/
942 static void EndThread( adec_thread_t *p_adec )
944 intf_DbgMsg("adec debug: destroying audio decoder thread %p\n", p_adec);
946 /* If the audio output fifo was created, we destroy it */
947 if ( p_adec->p_aout_fifo != NULL )
949 aout_DestroyFifo( p_adec->p_aout_fifo );
951 /* Destroy descriptor */
954 intf_DbgMsg("adec debug: audio decoder thread %p destroyed\n", p_adec);