]> git.sesse.net Git - vlc/blob - plugins/mpeg_adec/mpeg_adec_generic.c
* ./plugins/mad/mad_adec.c, ./plugins/mad/mad_libmad.c: use intf_WarnMsg for
[vlc] / plugins / mpeg_adec / mpeg_adec_generic.c
1 /*****************************************************************************
2  * adec_generic.c: MPEG audio decoder
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: mpeg_adec_generic.c,v 1.6 2002/01/09 00:33:37 asmax 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 <videolan/vlc.h>
29
30 #include "input_ext-dec.h"
31 #include "stream_control.h"
32
33 #include "mpeg_adec_generic.h"
34 #include "mpeg_adec.h"
35 #include "adec_math.h"                                     /* DCT32(), PCM() */
36 #include "adec_layer1.h"
37 #include "adec_layer2.h"
38
39 int adec_Init( adec_thread_t * p_adec )
40 {
41     p_adec->bank_0.actual = p_adec->bank_0.v1;
42     p_adec->bank_0.pos = 0;
43     p_adec->bank_1.actual = p_adec->bank_1.v1;
44     p_adec->bank_1.pos = 0;
45     return 0;
46 }
47
48 int adec_SyncFrame( adec_thread_t * p_adec, adec_sync_info_t * p_sync_info )
49 {
50     static int mpeg1_sample_rate[3] = {44100, 48000, 32000};
51     static int mpeg1_layer1_bit_rate[15] =
52     {
53         0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448
54     };
55     static int mpeg1_layer2_bit_rate[15] =
56     {
57         0, 32, 48, 56,  64,  80,  96, 112, 128, 160, 192, 224, 256, 320, 384
58     };
59     static int mpeg2_layer1_bit_rate[15] =
60     {
61         0, 32, 48, 56,  64,  80,  96, 112, 128, 144, 160, 176, 192, 224, 256
62     };
63     static int mpeg2_layer2_bit_rate[15] =
64     {
65         0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128, 144, 160
66     };
67     static int * bit_rate_table[8] =
68     {
69         NULL, NULL, mpeg2_layer2_bit_rate, mpeg2_layer1_bit_rate,
70         NULL, NULL, mpeg1_layer2_bit_rate, mpeg1_layer1_bit_rate
71     };
72
73     u32 header;
74     int index;
75     int * bit_rates;
76     int sample_rate;
77     int bit_rate;
78     int frame_size;
79
80     /* We read the whole header, but only really take 8 bits */
81     header = GetBits( &p_adec->bit_stream, 8 ) << 24;
82     header |= ShowBits( &p_adec->bit_stream, 24 );
83
84     p_adec->header = header;
85
86     /* basic header check : sync word */
87     if( (header & 0xfff00000) != 0xfff00000 )
88     {
89         return 1;
90     }
91
92     /* calculate bit rate */
93     index = ( header >> 17 ) & 7; /* mpeg ID + layer */
94     bit_rates = bit_rate_table[ index ];
95     if( bit_rates == NULL )
96     {
97         return 1; /* invalid layer */
98     }
99
100     index = ( header >> 12 ) & 15; /* bit rate index */
101     if (index > 14)
102     {
103         return 1;
104     }
105     bit_rate = bit_rates[ index ];
106
107     /* mpeg 1 layer 2 : check that bitrate per channel is valid */
108
109     if( bit_rates == mpeg1_layer2_bit_rate )
110     {
111         if( (header & 0xc0) == 0xc0 )
112         {   /* mono */
113             if( index > 10 )
114             {
115                 return 1; /* invalid bitrate per channel */
116             }
117         }
118         else
119         {   /* stereo */
120             if( (1 << index) & 0x2e )
121             {
122                 return 1; /* invalid bitrate per channel */
123             }
124         }
125     }
126
127     /* calculate sample rate */
128
129     index = ( header >> 10 ) & 3; /* sample rate index */
130     if( index > 2 )
131     {
132         return 1;
133     }
134
135     sample_rate = mpeg1_sample_rate[ index ];
136
137     if( ! (header & 0x80000) )
138     {
139         sample_rate >>= 1; /* half sample rate for mpeg2 */
140     }
141
142     /* calculate frame length */
143
144     if( (header & 0x60000) == 0x60000 )
145     {
146         /* layer 1 */
147         frame_size = 48000 * bit_rate / sample_rate;
148
149         /* padding */
150         if( header & 0x200 )
151         {
152             frame_size += 4;
153         }
154     }
155     else
156     {
157         /* layer >1 */
158         frame_size = 144000 * bit_rate / sample_rate;
159
160         /* padding */
161         if( header & 0x200 )
162         {
163             frame_size ++;
164         }
165     }
166
167     /* Now we are sure we want this header, read it */
168     RemoveBits( &p_adec->bit_stream, 24 );
169     p_adec->i_read_bits = 32;
170
171     if( ! (p_adec->header & 0x10000) )
172     {
173         /* Error check, skip it */
174         RemoveBits( &p_adec->bit_stream, 16 );
175         p_adec->i_read_bits += 16;
176     }
177
178     p_sync_info->b_stereo = ((p_adec->header & 0xc0) != 0xc0);
179     p_sync_info->sample_rate = sample_rate;
180     p_sync_info->bit_rate = bit_rate;
181     p_sync_info->frame_size = frame_size;
182     p_adec->frame_size = frame_size;
183
184     return 0;
185 }
186
187 int adec_DecodeFrame( adec_thread_t * p_adec, s16 * buffer )
188 {
189     int i_total_bytes_read;
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