]> git.sesse.net Git - vlc/blob - src/audio_decoder/adec_generic.c
* Header cleaning: filled all empty authors fields, added CVS $Id stuff.
[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.4 2001/03/21 13:42:34 sam 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 "config.h"
28 #include "common.h"
29 #include "threads.h"
30 #include "mtime.h"
31 #include "stream_control.h"
32 #include "input_ext-dec.h"
33
34 #include "adec_generic.h"
35 #include "audio_decoder.h"
36 #include "adec_math.h"                                     /* DCT32(), PCM() */
37 #include "adec_layer1.h"
38 #include "adec_layer2.h"
39
40 int adec_Init( adec_thread_t * p_adec )
41 {
42     p_adec->bank_0.actual = p_adec->bank_0.v1;
43     p_adec->bank_0.pos = 0;
44     p_adec->bank_1.actual = p_adec->bank_1.v1;
45     p_adec->bank_1.pos = 0;
46     return 0;
47 }
48
49 int adec_SyncFrame( adec_thread_t * p_adec, adec_sync_info_t * p_sync_info )
50 {
51     static int mpeg1_sample_rate[3] = {44100, 48000, 32000};
52     static int mpeg1_layer1_bit_rate[15] =
53     {
54         0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448
55     };
56     static int mpeg1_layer2_bit_rate[15] =
57     {
58         0, 32, 48, 56,  64,  80,  96, 112, 128, 160, 192, 224, 256, 320, 384
59     };
60     static int mpeg2_layer1_bit_rate[15] =
61     {
62         0, 32, 48, 56,  64,  80,  96, 112, 128, 144, 160, 176, 192, 224, 256
63     };
64     static int mpeg2_layer2_bit_rate[15] =
65     {
66         0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128, 144, 160
67     };
68     static int * bit_rate_table[8] =
69     {
70         NULL, NULL, mpeg2_layer2_bit_rate, mpeg2_layer1_bit_rate,
71         NULL, NULL, mpeg1_layer2_bit_rate, mpeg1_layer1_bit_rate
72     };
73
74     u32 header;
75     int index;
76     int * bit_rates;
77     int sample_rate;
78     int bit_rate;
79     int frame_size;
80
81     /* We read the whole header, but only really take 8 bits */
82     header = GetBits( &p_adec->bit_stream, 8 ) << 24;
83     header |= ShowBits( &p_adec->bit_stream, 24 );
84
85     p_adec->header = header;
86
87     /* basic header check : sync word, no emphasis */
88     if( (header & 0xfff00003) != 0xfff00000 )
89     {
90         return 1;
91     }
92
93     /* calculate bit rate */
94     index = ( header >> 17 ) & 7; /* mpeg ID + layer */
95     bit_rates = bit_rate_table[ index ];
96     if( bit_rate_table == NULL )
97     {
98         return 1; /* invalid layer */
99     }
100
101     index = ( header >> 12 ) & 15; /* bit rate index */
102     if (index > 14)
103     {
104         return 1;
105     }
106     bit_rate = bit_rates[ index ];
107
108     /* mpeg 1 layer 2 : check that bitrate per channel is valid */
109
110     if( bit_rates == mpeg1_layer2_bit_rate )
111     {
112         if( (header & 0xc0) == 0xc0 )
113         {   /* mono */
114             if( index > 10 )
115             {
116                 return 1; /* invalid bitrate per channel */
117             }
118         }
119         else
120         {   /* stereo */
121             if( (1 << index) & 0x2e )
122             {
123                 return 1; /* invalid bitrate per channel */
124             }
125         }
126     }
127
128     /* calculate sample rate */
129
130     index = ( header >> 10 ) & 3; /* sample rate index */
131     if( index > 2 )
132     {
133         return 1;
134     }
135
136     sample_rate = mpeg1_sample_rate[ index ];
137
138     if( ! (header & 0x80000) )
139     {
140         sample_rate >>= 1; /* half sample rate for mpeg2 */
141     }
142
143     /* calculate frame length */
144
145     if( (header & 0x60000) == 0x60000 )
146     {
147         /* layer 1 */
148         frame_size = 48000 * bit_rate / sample_rate;
149
150         /* padding */
151         if( header & 0x200 )
152         {
153             frame_size += 4;
154         }
155     }
156     else
157     {
158         /* layer >1 */
159         frame_size = 144000 * bit_rate / sample_rate;
160
161         /* padding */
162         if( header & 0x200 )
163         {
164             frame_size ++;
165         }
166     }
167
168     /* Now we are sure we want this header, read it */
169     RemoveBits( &p_adec->bit_stream, 24 );
170     p_adec->i_read_bits = 32;
171
172     p_sync_info->sample_rate = sample_rate;
173     p_sync_info->bit_rate = bit_rate;
174     p_sync_info->frame_size = frame_size;
175     p_adec->frame_size = frame_size;
176
177     return 0;
178 }
179
180 int adec_DecodeFrame( adec_thread_t * p_adec, s16 * buffer )
181 {
182     int i_total_bytes_read;
183
184     if( ! (p_adec->header & 0x10000) )
185     {
186         /* Error check, skip it */
187         RemoveBits( &p_adec->bit_stream, 16 );
188         p_adec->i_read_bits += 16;
189     }
190
191     /* parse audio data */
192
193     switch( (p_adec->header >> 17) & 3 )
194     {
195     case 2:
196         /* layer 2 */
197         if( (p_adec->header & 0xc0) == 0xc0 )
198         {
199             if( adec_layer2_mono (p_adec, buffer) )
200             {
201                 return 1;
202             }
203         }
204         else
205         {
206             if( adec_layer2_stereo (p_adec, buffer) )
207             {
208                 return 1;
209             }
210         }
211         break;
212
213     case 3:
214         /* layer 1 */
215         if( (p_adec->header & 0xc0) == 0xc0 )
216         {
217             if( adec_layer1_mono (p_adec, buffer) )
218             {
219                 return 1;
220             }
221         }
222         else
223         {
224             if( adec_layer1_stereo (p_adec, buffer) )
225             {
226                 return 1;
227             }
228         }
229         break;
230     }
231
232     /* Skip ancillary data */
233
234     if( (p_adec->header & 0xf000) == 0 ) /* free bitrate format */
235     {
236         return 0;
237     }
238
239     RealignBits( &p_adec->bit_stream );
240     i_total_bytes_read = ( p_adec->i_read_bits + 7 ) / 8;
241
242     if( i_total_bytes_read > p_adec->frame_size )
243     {
244         return 1; /* overrun */
245     }
246
247     while( i_total_bytes_read++ < p_adec->frame_size )
248     {
249         RemoveBits( &p_adec->bit_stream, 8 ); /* skip ancillary data */
250     }
251
252     return 0;
253 }
254