1 /*****************************************************************************
2 * adec_generic.c: MPEG audio decoder
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
29 #include "stream_control.h"
30 #include "input_ext-dec.h"
32 #include "adec_generic.h"
33 #include "audio_decoder.h"
34 #include "adec_math.h" /* DCT32(), PCM() */
35 #include "adec_layer1.h"
36 #include "adec_layer2.h"
38 int adec_Init( adec_thread_t * p_adec )
40 p_adec->bank_0.actual = p_adec->bank_0.v1;
41 p_adec->bank_0.pos = 0;
42 p_adec->bank_1.actual = p_adec->bank_1.v1;
43 p_adec->bank_1.pos = 0;
47 int adec_SyncFrame( adec_thread_t * p_adec, adec_sync_info_t * p_sync_info )
49 static int mpeg1_sample_rate[3] = {44100, 48000, 32000};
50 static int mpeg1_layer1_bit_rate[15] =
52 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448
54 static int mpeg1_layer2_bit_rate[15] =
56 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384
58 static int mpeg2_layer1_bit_rate[15] =
60 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256
62 static int mpeg2_layer2_bit_rate[15] =
64 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160
66 static int * bit_rate_table[8] =
68 NULL, NULL, mpeg2_layer2_bit_rate, mpeg2_layer1_bit_rate,
69 NULL, NULL, mpeg1_layer2_bit_rate, mpeg1_layer1_bit_rate
79 /* We read the whole header, but only really take 8 bits */
80 header = GetBits( &p_adec->bit_stream, 8 ) << 24;
81 header |= ShowBits( &p_adec->bit_stream, 24 );
83 p_adec->header = header;
85 /* basic header check : sync word, no emphasis */
86 if( (header & 0xfff00003) != 0xfff00000 )
91 /* calculate bit rate */
92 index = ( header >> 17 ) & 7; /* mpeg ID + layer */
93 bit_rates = bit_rate_table[ index ];
94 if( bit_rate_table == NULL )
96 return 1; /* invalid layer */
99 index = ( header >> 12 ) & 15; /* bit rate index */
104 bit_rate = bit_rates[ index ];
106 /* mpeg 1 layer 2 : check that bitrate per channel is valid */
108 if( bit_rates == mpeg1_layer2_bit_rate )
110 if( (header & 0xc0) == 0xc0 )
114 return 1; /* invalid bitrate per channel */
119 if( (1 << index) & 0x2e )
121 return 1; /* invalid bitrate per channel */
126 /* calculate sample rate */
128 index = ( header >> 10 ) & 3; /* sample rate index */
134 sample_rate = mpeg1_sample_rate[ index ];
136 if( ! (header & 0x80000) )
138 sample_rate >>= 1; /* half sample rate for mpeg2 */
141 /* calculate frame length */
143 if( (header & 0x60000) == 0x60000 )
146 frame_size = 48000 * bit_rate / sample_rate;
157 frame_size = 144000 * bit_rate / sample_rate;
166 /* Now we are sure we want this header, read it */
167 RemoveBits( &p_adec->bit_stream, 24 );
168 p_adec->i_read_bits = 32;
170 p_sync_info->sample_rate = sample_rate;
171 p_sync_info->bit_rate = bit_rate;
172 p_sync_info->frame_size = frame_size;
173 p_adec->frame_size = frame_size;
178 int adec_DecodeFrame( adec_thread_t * p_adec, s16 * buffer )
180 int i_total_bytes_read;
182 if( ! (p_adec->header & 0x10000) )
184 /* Error check, skip it */
185 RemoveBits( &p_adec->bit_stream, 16 );
186 p_adec->i_read_bits += 16;
189 /* parse audio data */
191 switch( (p_adec->header >> 17) & 3 )
195 if( (p_adec->header & 0xc0) == 0xc0 )
197 if( adec_layer2_mono (p_adec, buffer) )
204 if( adec_layer2_stereo (p_adec, buffer) )
213 if( (p_adec->header & 0xc0) == 0xc0 )
215 if( adec_layer1_mono (p_adec, buffer) )
222 if( adec_layer1_stereo (p_adec, buffer) )
230 /* Skip ancillary data */
232 if( (p_adec->header & 0xf000) == 0 ) /* free bitrate format */
237 RealignBits( &p_adec->bit_stream );
238 i_total_bytes_read = ( p_adec->i_read_bits + 7 ) / 8;
240 if( i_total_bytes_read > p_adec->frame_size )
242 return 1; /* overrun */
245 while( i_total_bytes_read++ < p_adec->frame_size )
247 RemoveBits( &p_adec->bit_stream, 8 ); /* skip ancillary data */