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