]> git.sesse.net Git - vlc/blob - src/audio_decoder/adec_generic.c
8aa6a00e2c7c248b6fa5f61cb807a4be1951e54a
[vlc] / src / audio_decoder / adec_generic.c
1 /*****************************************************************************
2  * adec_generic.c: MPEG audio decoder
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  * $Id: adec_generic.c,v 1.6 2001/09/05 16:46:10 massiot Exp $
6  *
7  * Authors: Michel Kaempf <maxx@via.ecp.fr>
8  *          Michel Lespinasse <walken@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 #include "defs.h"
26
27 #include <string.h>                                    /* memcpy(), memset() */
28
29 #include "config.h"
30 #include "common.h"
31 #include "threads.h"
32 #include "mtime.h"
33 #include "stream_control.h"
34 #include "input_ext-dec.h"
35
36 #include "adec_generic.h"
37 #include "audio_decoder.h"
38 #include "adec_math.h"                                     /* DCT32(), PCM() */
39 #include "adec_layer1.h"
40 #include "adec_layer2.h"
41
42 int adec_Init( adec_thread_t * p_adec )
43 {
44     p_adec->bank_0.actual = p_adec->bank_0.v1;
45     p_adec->bank_0.pos = 0;
46     p_adec->bank_1.actual = p_adec->bank_1.v1;
47     p_adec->bank_1.pos = 0;
48     return 0;
49 }
50
51 int adec_SyncFrame( adec_thread_t * p_adec, adec_sync_info_t * p_sync_info )
52 {
53     static int mpeg1_sample_rate[3] = {44100, 48000, 32000};
54     static int mpeg1_layer1_bit_rate[15] =
55     {
56         0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448
57     };
58     static int mpeg1_layer2_bit_rate[15] =
59     {
60         0, 32, 48, 56,  64,  80,  96, 112, 128, 160, 192, 224, 256, 320, 384
61     };
62     static int mpeg2_layer1_bit_rate[15] =
63     {
64         0, 32, 48, 56,  64,  80,  96, 112, 128, 144, 160, 176, 192, 224, 256
65     };
66     static int mpeg2_layer2_bit_rate[15] =
67     {
68         0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128, 144, 160
69     };
70     static int * bit_rate_table[8] =
71     {
72         NULL, NULL, mpeg2_layer2_bit_rate, mpeg2_layer1_bit_rate,
73         NULL, NULL, mpeg1_layer2_bit_rate, mpeg1_layer1_bit_rate
74     };
75
76     u32 header;
77     int index;
78     int * bit_rates;
79     int sample_rate;
80     int bit_rate;
81     int frame_size;
82
83     /* We read the whole header, but only really take 8 bits */
84     header = GetBits( &p_adec->bit_stream, 8 ) << 24;
85     header |= ShowBits( &p_adec->bit_stream, 24 );
86
87     p_adec->header = header;
88
89     /* basic header check : sync word */
90     if( (header & 0xfff00000) != 0xfff00000 )
91     {
92         return 1;
93     }
94
95     /* calculate bit rate */
96     index = ( header >> 17 ) & 7; /* mpeg ID + layer */
97     bit_rates = bit_rate_table[ index ];
98     if( bit_rates == NULL )
99     {
100         return 1; /* invalid layer */
101     }
102
103     index = ( header >> 12 ) & 15; /* bit rate index */
104     if (index > 14)
105     {
106         return 1;
107     }
108     bit_rate = bit_rates[ index ];
109
110     /* mpeg 1 layer 2 : check that bitrate per channel is valid */
111
112     if( bit_rates == mpeg1_layer2_bit_rate )
113     {
114         if( (header & 0xc0) == 0xc0 )
115         {   /* mono */
116             if( index > 10 )
117             {
118                 return 1; /* invalid bitrate per channel */
119             }
120         }
121         else
122         {   /* stereo */
123             if( (1 << index) & 0x2e )
124             {
125                 return 1; /* invalid bitrate per channel */
126             }
127         }
128     }
129
130     /* calculate sample rate */
131
132     index = ( header >> 10 ) & 3; /* sample rate index */
133     if( index > 2 )
134     {
135         return 1;
136     }
137
138     sample_rate = mpeg1_sample_rate[ index ];
139
140     if( ! (header & 0x80000) )
141     {
142         sample_rate >>= 1; /* half sample rate for mpeg2 */
143     }
144
145     /* calculate frame length */
146
147     if( (header & 0x60000) == 0x60000 )
148     {
149         /* layer 1 */
150         frame_size = 48000 * bit_rate / sample_rate;
151
152         /* padding */
153         if( header & 0x200 )
154         {
155             frame_size += 4;
156         }
157     }
158     else
159     {
160         /* layer >1 */
161         frame_size = 144000 * bit_rate / sample_rate;
162
163         /* padding */
164         if( header & 0x200 )
165         {
166             frame_size ++;
167         }
168     }
169
170     /* Now we are sure we want this header, read it */
171     RemoveBits( &p_adec->bit_stream, 24 );
172     p_adec->i_read_bits = 32;
173
174     p_sync_info->sample_rate = sample_rate;
175     p_sync_info->bit_rate = bit_rate;
176     p_sync_info->frame_size = frame_size;
177     p_adec->frame_size = frame_size;
178
179     return 0;
180 }
181
182 int adec_DecodeFrame( adec_thread_t * p_adec, s16 * buffer )
183 {
184     int i_total_bytes_read;
185
186     if( ! (p_adec->header & 0x10000) )
187     {
188         /* Error check, skip it */
189         RemoveBits( &p_adec->bit_stream, 16 );
190         p_adec->i_read_bits += 16;
191     }
192
193     /* parse audio data */
194
195     switch( (p_adec->header >> 17) & 3 )
196     {
197     case 2:
198         /* layer 2 */
199         if( (p_adec->header & 0xc0) == 0xc0 )
200         {
201             if( adec_layer2_mono (p_adec, buffer) )
202             {
203                 return 1;
204             }
205         }
206         else
207         {
208             if( adec_layer2_stereo (p_adec, buffer) )
209             {
210                 return 1;
211             }
212         }
213         break;
214
215     case 3:
216         /* layer 1 */
217         if( (p_adec->header & 0xc0) == 0xc0 )
218         {
219             if( adec_layer1_mono (p_adec, buffer) )
220             {
221                 return 1;
222             }
223         }
224         else
225         {
226             if( adec_layer1_stereo (p_adec, buffer) )
227             {
228                 return 1;
229             }
230         }
231         break;
232     }
233
234     /* Skip ancillary data */
235
236     if( (p_adec->header & 0xf000) == 0 ) /* free bitrate format */
237     {
238         return 0;
239     }
240
241     RealignBits( &p_adec->bit_stream );
242     i_total_bytes_read = ( p_adec->i_read_bits + 7 ) / 8;
243
244     if( i_total_bytes_read > p_adec->frame_size )
245     {
246         return 1; /* overrun */
247     }
248
249     while( i_total_bytes_read++ < p_adec->frame_size )
250     {
251         RemoveBits( &p_adec->bit_stream, 8 ); /* skip ancillary data */
252     }
253
254     return 0;
255 }
256