]> 4ch.mooo.com Git - 16.git/blob - src/lib/doslib/ext/lame/common.c
3cc136bf9462f7af08c5977686a67f40370182e6
[16.git] / src / lib / doslib / ext / lame / common.c
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: common.c,v 1.40 2010/03/22 14:30:19 robert Exp $ */
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
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
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     if (((newhead >> 10) & 0x3) == 0x3) {
202         lame_report_fnc(mp->report_err, "Stream error\n");
203         exit(1);
204     }
205     if (fr->mpeg25) {
206         fr->sampling_frequency = 6 + ((newhead >> 10) & 0x3);
207     }
208     else
209         fr->sampling_frequency = ((newhead >> 10) & 0x3) + (fr->lsf * 3);
210
211     fr->error_protection = ((newhead >> 16) & 0x1) ^ 0x1;
212
213     if (fr->mpeg25)     /* allow Bitrate change for 2.5 ... */
214         fr->bitrate_index = ((newhead >> 12) & 0xf);
215
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;
224
225     fr->stereo = (fr->mode == MPG_MD_MONO) ? 1 : 2;
226
227     switch (fr->lay) {
228     case 1:
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;
232         fr->down_sample = 0;
233         fr->down_sample_sblimit = SBLIMIT >> (fr->down_sample);
234         break;
235
236     case 2:
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;
240         fr->down_sample = 0;
241         fr->down_sample_sblimit = SBLIMIT >> (fr->down_sample);
242         break;
243
244     case 3:
245 #if 0
246         fr->do_layer = do_layer3;
247         if (fr->lsf)
248             ssize = (fr->stereo == 1) ? 9 : 17;
249         else
250             ssize = (fr->stereo == 1) ? 17 : 32;
251 #endif
252
253 #if 0
254         if (fr->error_protection)
255             ssize += 2;
256 #endif
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;
260             return (0);
261         }
262
263
264         if (fr->bitrate_index == 0)
265             fr->framesize = 0;
266         else {
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;
270         }
271         break;
272     default:
273         lame_report_fnc(mp->report_err, "Sorry, layer %d not supported\n", fr->lay);
274         return (0);
275     }
276     /*    print_header(mp, fr); */
277
278     return 1;
279 }
280
281
282 unsigned int
283 getbits(PMPSTR mp, int number_of_bits)
284 {
285     unsigned long rval;
286
287     if (number_of_bits <= 0 || !mp->wordpointer)
288         return 0;
289
290     {
291         rval = mp->wordpointer[0];
292         rval <<= 8;
293         rval |= mp->wordpointer[1];
294         rval <<= 8;
295         rval |= mp->wordpointer[2];
296         rval <<= mp->bitindex;
297         rval &= 0xffffff;
298
299         mp->bitindex += number_of_bits;
300
301         rval >>= (24 - number_of_bits);
302
303         mp->wordpointer += (mp->bitindex >> 3);
304         mp->bitindex &= 7;
305     }
306     return rval;
307 }
308
309 unsigned int
310 getbits_fast(PMPSTR mp, int number_of_bits)
311 {
312     unsigned long rval;
313
314     {
315         rval = mp->wordpointer[0];
316         rval <<= 8;
317         rval |= mp->wordpointer[1];
318         rval <<= mp->bitindex;
319         rval &= 0xffff;
320         mp->bitindex += number_of_bits;
321
322         rval >>= (16 - number_of_bits);
323
324         mp->wordpointer += (mp->bitindex >> 3);
325         mp->bitindex &= 7;
326     }
327     return rval;
328 }
329
330 unsigned char
331 get_leq_8_bits(PMPSTR mp, unsigned int number_of_bits)
332 {
333     assert(number_of_bits <= 8);
334     return (unsigned char) getbits_fast(mp, number_of_bits);
335 }
336
337 unsigned short
338 get_leq_16_bits(PMPSTR mp, unsigned int number_of_bits)
339 {
340     assert(number_of_bits <= 16);
341     return (unsigned short) getbits_fast(mp, number_of_bits);
342 }
343
344 int
345 set_pointer(PMPSTR mp, long backstep)
346 {
347     unsigned char *bsbufold;
348
349     if (mp->fsizeold < 0 && backstep > 0) {
350         lame_report_fnc(mp->report_err, "hip: Can't step back %ld bytes!\n", backstep);
351         return MP3_ERR;
352     }
353     bsbufold = mp->bsspace[1 - mp->bsnum] + 512;
354     mp->wordpointer -= backstep;
355     if (backstep)
356         memcpy(mp->wordpointer, bsbufold + mp->fsizeold - backstep, (size_t) backstep);
357     mp->bitindex = 0;
358     return MP3_OK;
359 }