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