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