• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * common.c: some common bitstream operations
3  *
4  * Copyright (C) 1999-2010 The L.A.M.E. project
5  *
6  * Initially written by Michael Hipp, see also AUTHORS and README.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 
24 /* $Id$ */
25 
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29 
30 #include <ctype.h>
31 #include <stdlib.h>
32 #include <signal.h>
33 
34 #ifdef HAVE_FCNTL_H
35 #include <fcntl.h>
36 #endif
37 
38 #ifdef macintosh
39 #include   <types.h>
40 #include   <stat.h>
41 #else
42 #include  <sys/types.h>
43 #include  <sys/stat.h>
44 #endif
45 
46 #include <assert.h>
47 
48 #include "common.h"
49 
50 #ifdef WITH_DMALLOC
51 #include <dmalloc.h>
52 #endif
53 
54 /* In C++ the array first must be prototyped, why ? */
55 
56 
57     /* *INDENT-OFF* */
58 const int tabsel_123 [2] [3] [16] = {
59    { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,},
60      {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,},
61      {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} },
62 
63    { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,},
64      {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,},
65      {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} }
66 };
67 
68 const long freqs[9] = { 44100, 48000, 32000,
69                         22050, 24000, 16000,
70                         11025, 12000,  8000 };
71 
72     /* *INDENT-ON* */
73 
74 
75 real    muls[27][64];
76 
77 #if 0
78 static void
79 get_II_stuff(struct frame *fr)
80 {
81     /* *INDENT-OFF* */
82   static const int translate [3] [2] [16] =   /* char ? */
83    { { { 0,2,2,2,2,2,2,0,0,0,1,1,1,1,1,0 } ,
84        { 0,2,2,0,0,0,1,1,1,1,1,1,1,1,1,0 } } ,
85      { { 0,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0 } ,
86        { 0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0 } } ,
87      { { 0,3,3,3,3,3,3,0,0,0,1,1,1,1,1,0 } ,
88        { 0,3,3,0,0,0,1,1,1,1,1,1,1,1,1,0 } } };
89     /* *INDENT-ON* */
90 
91     int     table, sblim;
92     static const struct al_table2 *tables[5] = { alloc_0, alloc_1, alloc_2, alloc_3, alloc_4 };
93     static int sblims[5] = { 27, 30, 8, 12, 30 };
94 
95     if (fr->lsf)
96         table = 4;
97     else
98         table = translate[fr->sampling_frequency][2 - fr->stereo][fr->bitrate_index];
99     sblim = sblims[table];
100 
101     fr->alloc = tables[table];
102     fr->II_sblimit = sblim;
103 }
104 #endif
105 
106 #define HDRCMPMASK 0xfffffd00
107 
108 #define MAX_INPUT_FRAMESIZE 4096
109 
110 int
head_check(unsigned long head,int check_layer)111 head_check(unsigned long head, int check_layer)
112 {
113     /*
114        look for a valid header.
115        if check_layer > 0, then require that
116        nLayer = check_layer.
117      */
118 
119     /* bits 13-14 = layer 3 */
120     int     nLayer = 4 - ((head >> 17) & 3);
121 
122     if ((head & 0xffe00000) != 0xffe00000) {
123         /* syncword */
124         return FALSE;
125     }
126 
127     if (nLayer == 4)
128         return FALSE;
129 
130     if (check_layer > 0 && nLayer != check_layer)
131         return FALSE;
132 
133     if (((head >> 12) & 0xf) == 0xf) {
134         /* bits 16,17,18,19 = 1111  invalid bitrate */
135         return FALSE;
136     }
137     if (((head >> 10) & 0x3) == 0x3) {
138         /* bits 20,21 = 11  invalid sampling freq */
139         return FALSE;
140     }
141     if ((head & 0x3) == 0x2)
142         /* invalid emphasis */
143         return FALSE;
144     return TRUE;
145 }
146 
147 
148 #if 0
149 static void
150 print_header(PMPSTR mp, struct frame *fr)
151 {
152     static const char *modes[4] = { "Stereo", "Joint-Stereo", "Dual-Channel", "Single-Channel" };
153     static const char *layers[4] = { "Unknown", "I", "II", "III" };
154 
155     lame_report_fnc(mp->report_msg, "MPEG %s, Layer: %s, Freq: %ld, mode: %s, modext: %d, BPF : %d\n",
156             fr->mpeg25 ? "2.5" : (fr->lsf ? "2.0" : "1.0"),
157             layers[fr->lay], freqs[fr->sampling_frequency],
158             modes[fr->mode], fr->mode_ext, fr->framesize + 4);
159     lame_report_fnc(mp->report_msg, "Channels: %d, copyright: %s, original: %s, CRC: %s, emphasis: %d.\n",
160             fr->stereo, fr->copyright ? "Yes" : "No",
161             fr->original ? "Yes" : "No", fr->error_protection ? "Yes" : "No", fr->emphasis);
162     lame_report_fnc(mp->report_msg, "Bitrate: %d Kbits/s, Extension value: %d\n",
163             tabsel_123[fr->lsf][fr->lay - 1][fr->bitrate_index], fr->extension);
164 }
165 
166 static void
167 print_header_compact(PMPSTR mp, struct frame *fr)
168 {
169     static const char *modes[4] = { "stereo", "joint-stereo", "dual-channel", "mono" };
170     static const char *layers[4] = { "Unknown", "I", "II", "III" };
171 
172     lame_report_fnc(mp->report_err, "MPEG %s layer %s, %d kbit/s, %ld Hz %s\n",
173             fr->mpeg25 ? "2.5" : (fr->lsf ? "2.0" : "1.0"),
174             layers[fr->lay],
175             tabsel_123[fr->lsf][fr->lay - 1][fr->bitrate_index],
176             freqs[fr->sampling_frequency], modes[fr->mode]);
177 }
178 
179 #endif
180 
181 /*
182  * decode a header and write the information
183  * into the frame structure
184  */
185 int
decode_header(PMPSTR mp,struct frame * fr,unsigned long newhead)186 decode_header(PMPSTR mp, struct frame *fr, unsigned long newhead)
187 {
188 
189 
190     if (newhead & (1 << 20)) {
191         fr->lsf = (newhead & (1 << 19)) ? 0x0 : 0x1;
192         fr->mpeg25 = 0;
193     }
194     else {
195         fr->lsf = 1;
196         fr->mpeg25 = 1;
197     }
198 
199 
200     fr->lay = 4 - ((newhead >> 17) & 3);
201 
202     if (fr->lay != 3 && fr->mpeg25) {
203         lame_report_fnc(mp->report_err, "MPEG-2.5 is supported by Layer3 only\n");
204         return 0;
205     }
206     if (((newhead >> 10) & 0x3) == 0x3) {
207         lame_report_fnc(mp->report_err, "Stream error\n");
208         return 0;
209     }
210     if (fr->mpeg25) {
211         fr->sampling_frequency = 6 + ((newhead >> 10) & 0x3);
212     }
213     else
214         fr->sampling_frequency = ((newhead >> 10) & 0x3) + (fr->lsf * 3);
215 
216     fr->error_protection = ((newhead >> 16) & 0x1) ^ 0x1;
217 
218     if (fr->mpeg25)     /* allow Bitrate change for 2.5 ... */
219         fr->bitrate_index = ((newhead >> 12) & 0xf);
220 
221     fr->bitrate_index = ((newhead >> 12) & 0xf);
222     fr->padding = ((newhead >> 9) & 0x1);
223     fr->extension = ((newhead >> 8) & 0x1);
224     fr->mode = ((newhead >> 6) & 0x3);
225     fr->mode_ext = ((newhead >> 4) & 0x3);
226     fr->copyright = ((newhead >> 3) & 0x1);
227     fr->original = ((newhead >> 2) & 0x1);
228     fr->emphasis = newhead & 0x3;
229 
230     fr->stereo = (fr->mode == MPG_MD_MONO) ? 1 : 2;
231 
232     switch (fr->lay) {
233     case 1:
234         fr->framesize = (long) tabsel_123[fr->lsf][0][fr->bitrate_index] * 12000;
235         fr->framesize /= freqs[fr->sampling_frequency];
236         fr->framesize = ((fr->framesize + fr->padding) << 2) - 4;
237         fr->down_sample = 0;
238         fr->down_sample_sblimit = SBLIMIT >> (fr->down_sample);
239         break;
240 
241     case 2:
242         fr->framesize = (long) tabsel_123[fr->lsf][1][fr->bitrate_index] * 144000;
243         fr->framesize /= freqs[fr->sampling_frequency];
244         fr->framesize += fr->padding - 4;
245         fr->down_sample = 0;
246         fr->down_sample_sblimit = SBLIMIT >> (fr->down_sample);
247         break;
248 
249     case 3:
250 #if 0
251         fr->do_layer = do_layer3;
252         if (fr->lsf)
253             ssize = (fr->stereo == 1) ? 9 : 17;
254         else
255             ssize = (fr->stereo == 1) ? 17 : 32;
256 #endif
257 
258 #if 0
259         if (fr->error_protection)
260             ssize += 2;
261 #endif
262         if (fr->framesize > MAX_INPUT_FRAMESIZE) {
263             lame_report_fnc(mp->report_err, "Frame size too big.\n");
264             fr->framesize = MAX_INPUT_FRAMESIZE;
265             return (0);
266         }
267 
268 
269         if (fr->bitrate_index == 0)
270             fr->framesize = 0;
271         else {
272             fr->framesize = (long) tabsel_123[fr->lsf][2][fr->bitrate_index] * 144000;
273             fr->framesize /= freqs[fr->sampling_frequency] << (fr->lsf);
274             fr->framesize = fr->framesize + fr->padding - 4;
275         }
276         break;
277     default:
278         lame_report_fnc(mp->report_err, "Sorry, layer %d not supported\n", fr->lay);
279         return (0);
280     }
281     /*    print_header(mp, fr); */
282 
283     return 1;
284 }
285 
286 
287 unsigned int
getbits(PMPSTR mp,int number_of_bits)288 getbits(PMPSTR mp, int number_of_bits)
289 {
290     unsigned long rval;
291 
292     if (number_of_bits <= 0 || !mp->wordpointer)
293         return 0;
294 
295     {
296         rval = mp->wordpointer[0];
297         rval <<= 8;
298         rval |= mp->wordpointer[1];
299         rval <<= 8;
300         rval |= mp->wordpointer[2];
301         rval <<= mp->bitindex;
302         rval &= 0xffffff;
303 
304         mp->bitindex += number_of_bits;
305 
306         rval >>= (24 - number_of_bits);
307 
308         mp->wordpointer += (mp->bitindex >> 3);
309         mp->bitindex &= 7;
310     }
311     return rval;
312 }
313 
314 unsigned int
getbits_fast(PMPSTR mp,int number_of_bits)315 getbits_fast(PMPSTR mp, int number_of_bits)
316 {
317     unsigned long rval;
318 
319     {
320         rval = mp->wordpointer[0];
321         rval <<= 8;
322         rval |= mp->wordpointer[1];
323         rval <<= mp->bitindex;
324         rval &= 0xffff;
325         mp->bitindex += number_of_bits;
326 
327         rval >>= (16 - number_of_bits);
328 
329         mp->wordpointer += (mp->bitindex >> 3);
330         mp->bitindex &= 7;
331     }
332     return rval;
333 }
334 
335 unsigned char
get_leq_8_bits(PMPSTR mp,unsigned int number_of_bits)336 get_leq_8_bits(PMPSTR mp, unsigned int number_of_bits)
337 {
338     assert(number_of_bits <= 8);
339     return (unsigned char) getbits_fast(mp, number_of_bits);
340 }
341 
342 unsigned short
get_leq_16_bits(PMPSTR mp,unsigned int number_of_bits)343 get_leq_16_bits(PMPSTR mp, unsigned int number_of_bits)
344 {
345     assert(number_of_bits <= 16);
346     return (unsigned short) getbits_fast(mp, number_of_bits);
347 }
348 
349 int
set_pointer(PMPSTR mp,long backstep)350 set_pointer(PMPSTR mp, long backstep)
351 {
352     unsigned char *bsbufold;
353 
354     if (mp->fsizeold < 0 && backstep > 0) {
355         lame_report_fnc(mp->report_err, "hip: Can't step back %ld bytes!\n", backstep);
356         return MP3_ERR;
357     }
358     bsbufold = mp->bsspace[1 - mp->bsnum] + 512;
359     mp->wordpointer -= backstep;
360     if (backstep)
361         memcpy(mp->wordpointer, bsbufold + mp->fsizeold - backstep, (size_t) backstep);
362     mp->bitindex = 0;
363     return MP3_OK;
364 }
365