2 * common.c: some common bitstream operations
4 * Copyright (C) 1999-2010 The L.A.M.E. project
6 * Initially written by Michael Hipp, see also AUTHORS and README.
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.
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.
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.
24 /* $Id: common.c,v 1.40 2010/03/22 14:30:19 robert Exp $ */
42 #include <sys/types.h>
54 /* In C++ the array first must be prototyped, why ? */
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,} },
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,} }
68 const long freqs[9] = { 44100, 48000, 32000,
79 get_II_stuff(struct frame *fr)
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 } } };
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 };
98 table = translate[fr->sampling_frequency][2 - fr->stereo][fr->bitrate_index];
99 sblim = sblims[table];
101 fr->alloc = tables[table];
102 fr->II_sblimit = sblim;
106 #define HDRCMPMASK 0xfffffd00
108 #define MAX_INPUT_FRAMESIZE 4096
111 head_check(unsigned long head, int check_layer)
114 look for a valid header.
115 if check_layer > 0, then require that
116 nLayer = check_layer.
119 /* bits 13-14 = layer 3 */
120 int nLayer = 4 - ((head >> 17) & 3);
122 if ((head & 0xffe00000) != 0xffe00000) {
130 if (check_layer > 0 && nLayer != check_layer)
133 if (((head >> 12) & 0xf) == 0xf) {
134 /* bits 16,17,18,19 = 1111 invalid bitrate */
137 if (((head >> 10) & 0x3) == 0x3) {
138 /* bits 20,21 = 11 invalid sampling freq */
141 if ((head & 0x3) == 0x2)
142 /* invalid emphasis */
150 print_header(PMPSTR mp, struct frame *fr)
152 static const char *modes[4] = { "Stereo", "Joint-Stereo", "Dual-Channel", "Single-Channel" };
153 static const char *layers[4] = { "Unknown", "I", "II", "III" };
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);
167 print_header_compact(PMPSTR mp, struct frame *fr)
169 static const char *modes[4] = { "stereo", "joint-stereo", "dual-channel", "mono" };
170 static const char *layers[4] = { "Unknown", "I", "II", "III" };
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"),
175 tabsel_123[fr->lsf][fr->lay - 1][fr->bitrate_index],
176 freqs[fr->sampling_frequency], modes[fr->mode]);
182 * decode a header and write the information
183 * into the frame structure
186 decode_header(PMPSTR mp, struct frame *fr, unsigned long newhead)
190 if (newhead & (1 << 20)) {
191 fr->lsf = (newhead & (1 << 19)) ? 0x0 : 0x1;
200 fr->lay = 4 - ((newhead >> 17) & 3);
201 if (((newhead >> 10) & 0x3) == 0x3) {
202 lame_report_fnc(mp->report_err, "Stream error\n");
206 fr->sampling_frequency = 6 + ((newhead >> 10) & 0x3);
209 fr->sampling_frequency = ((newhead >> 10) & 0x3) + (fr->lsf * 3);
211 fr->error_protection = ((newhead >> 16) & 0x1) ^ 0x1;
213 if (fr->mpeg25) /* allow Bitrate change for 2.5 ... */
214 fr->bitrate_index = ((newhead >> 12) & 0xf);
216 fr->bitrate_index = ((newhead >> 12) & 0xf);
217 fr->padding = ((newhead >> 9) & 0x1);
218 fr->extension = ((newhead >> 8) & 0x1);
219 fr->mode = ((newhead >> 6) & 0x3);
220 fr->mode_ext = ((newhead >> 4) & 0x3);
221 fr->copyright = ((newhead >> 3) & 0x1);
222 fr->original = ((newhead >> 2) & 0x1);
223 fr->emphasis = newhead & 0x3;
225 fr->stereo = (fr->mode == MPG_MD_MONO) ? 1 : 2;
229 fr->framesize = (long) tabsel_123[fr->lsf][0][fr->bitrate_index] * 12000;
230 fr->framesize /= freqs[fr->sampling_frequency];
231 fr->framesize = ((fr->framesize + fr->padding) << 2) - 4;
233 fr->down_sample_sblimit = SBLIMIT >> (fr->down_sample);
237 fr->framesize = (long) tabsel_123[fr->lsf][1][fr->bitrate_index] * 144000;
238 fr->framesize /= freqs[fr->sampling_frequency];
239 fr->framesize += fr->padding - 4;
241 fr->down_sample_sblimit = SBLIMIT >> (fr->down_sample);
246 fr->do_layer = do_layer3;
248 ssize = (fr->stereo == 1) ? 9 : 17;
250 ssize = (fr->stereo == 1) ? 17 : 32;
254 if (fr->error_protection)
257 if (fr->framesize > MAX_INPUT_FRAMESIZE) {
258 lame_report_fnc(mp->report_err, "Frame size too big.\n");
259 fr->framesize = MAX_INPUT_FRAMESIZE;
264 if (fr->bitrate_index == 0)
267 fr->framesize = (long) tabsel_123[fr->lsf][2][fr->bitrate_index] * 144000;
268 fr->framesize /= freqs[fr->sampling_frequency] << (fr->lsf);
269 fr->framesize = fr->framesize + fr->padding - 4;
273 lame_report_fnc(mp->report_err, "Sorry, layer %d not supported\n", fr->lay);
276 /* print_header(mp, fr); */
283 getbits(PMPSTR mp, int number_of_bits)
287 if (number_of_bits <= 0 || !mp->wordpointer)
291 rval = mp->wordpointer[0];
293 rval |= mp->wordpointer[1];
295 rval |= mp->wordpointer[2];
296 rval <<= mp->bitindex;
299 mp->bitindex += number_of_bits;
301 rval >>= (24 - number_of_bits);
303 mp->wordpointer += (mp->bitindex >> 3);
310 getbits_fast(PMPSTR mp, int number_of_bits)
315 rval = mp->wordpointer[0];
317 rval |= mp->wordpointer[1];
318 rval <<= mp->bitindex;
320 mp->bitindex += number_of_bits;
322 rval >>= (16 - number_of_bits);
324 mp->wordpointer += (mp->bitindex >> 3);
331 get_leq_8_bits(PMPSTR mp, unsigned int number_of_bits)
333 assert(number_of_bits <= 8);
334 return (unsigned char) getbits_fast(mp, number_of_bits);
338 get_leq_16_bits(PMPSTR mp, unsigned int number_of_bits)
340 assert(number_of_bits <= 16);
341 return (unsigned short) getbits_fast(mp, number_of_bits);
345 set_pointer(PMPSTR mp, long backstep)
347 unsigned char *bsbufold;
349 if (mp->fsizeold < 0 && backstep > 0) {
350 lame_report_fnc(mp->report_err, "hip: Can't step back %ld bytes!\n", backstep);
353 bsbufold = mp->bsspace[1 - mp->bsnum] + 512;
354 mp->wordpointer -= backstep;
356 memcpy(mp->wordpointer, bsbufold + mp->fsizeold - backstep, (size_t) backstep);