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