1 /*____________________________________________________________________________
2
3 FreeAmp - The Free MP3 Player
4
5 MP3 Decoder originally Copyright (C) 1995-1997 Xing Technology
6 Corp. http://www.xingtech.com
7
8 Portions Copyright (C) 1998-1999 EMusic.com
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., 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 $Id: cupl3.c,v 1.8 1999/10/19 07:13:08 elrod Exp $
25 ____________________________________________________________________________*/
26
27 /**** cupL3.c ***************************************************
28 unpack Layer III
29
30
31 mod 8/18/97 bugfix crc problem
32
33 mod 10/9/97 add pMP3Stream->band_limit12 for short blocks
34
35 mod 10/22/97 zero buf_ptrs in init
36
37 mod 5/15/98 mpeg 2.5
38
39 mod 8/19/98 decode 22 sf bands
40
41 ******************************************************************/
42
43 /*---------------------------------------
44 TO DO: Test mixed blocks (mixed long/short)
45 No mixed blocks in mpeg-1 test stream being used for development
46
47 -----------------------------------------*/
48
49 #include <stdlib.h>
50 #include <stdio.h>
51 #include <float.h>
52 #include <math.h>
53 #include <memory.h>
54 #include <string.h>
55 #include <assert.h>
56 #include "mhead.h" /* mpeg header structure */
57 #include "l3.h"
58 #include "jdw.h"
59
60 #include "mp3struct.h"
61
62 #if !defined(min)
63 # define min(a, b) ((a) < (b) ? (a) : (b))
64 #endif
65
66 /*====================================================================*/
67 static const int mp_sr20_table[2][4] =
68 {{441, 480, 320, -999}, {882, 960, 640, -999}};
69 static const int mp_br_tableL3[2][16] =
70 {{0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0}, /* mpeg 2 */
71 {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0}};
72
73 /*====================================================================*/
74
75 /*-- global band tables */
76 /*-- short portion is 3*x !! --*/
77 ////@@@@int nBand[2][22]; /* [long/short][cb] */
78 ////@@@@int sfBandIndex[2][22]; /* [long/short][cb] */
79
80 /*====================================================================*/
81
82 /*----------------*/
83 extern DEC_INFO decinfo; ////@@@@ this is ok, only written to during init, then chucked.
84
85 /*----------------*/
86 ////@@@@static int pMP3Stream->mpeg25_flag; // L3 only
87
88 //int iframe;
89
90 /*-------*/
91 ////@@@@static int pMP3Stream->band_limit = (576); // L3 only
92 ////@@@@static int pMP3Stream->band_limit21 = (576); // limit for sf band 21 // L3 only
93 ////@@@@static int pMP3Stream->band_limit12 = (576); // limit for sf band 12 short //L3 only
94
95 ////@@@@int band_limit_nsb = 32; /* global for hybrid */
96 ////@@@@static int pMP3Stream->nsb_limit = 32;
97 ////@@@@static int pMP3Stream->gain_adjust = 0; /* adjust gain e.g. cvt to mono */ // L3 only
98 ////@@@@static int id; // L3 only
99 ////@@@@static int pMP3Stream->ncbl_mixed; /* number of long cb's in mixed block 8 or 6 */ // L3 only
100 ////@@@@static int pMP3Stream->sr_index; // L3 only (99%)
101
102 //@@@@
103 ////@@@@static int pMP3Stream->outvalues; //
104 ////@@@@static int pMP3Stream->outbytes; //
105 ////@@@@static int pMP3Stream->half_outbytes; // L3 only
106 ////@@@@static int pMP3Stream->framebytes; //
107
108 //static int padframebytes;
109 ////@@@@static int pMP3Stream->crcbytes; // L3 only
110 ////@@@@static int pMP3Stream->pad; //
111 //static int stereo_flag; // only written to
112 ////@@@@static int pMP3Stream->nchan; // L3 only
113 ////@@@@static int pMP3Stream->ms_mode; // L3 only (99%)
114 ////@@@@static int pMP3Stream->is_mode; // L3 only
115 ////@@@@static unsigned int pMP3Stream->zero_level_pcm = 0; // L3 only
116
117 /* cb_info[igr][ch], compute by dequant, used by joint */
118 static CB_INFO cb_info[2][2]; // L3 only ############ I think this is ok, only a scratchpad?
119 static IS_SF_INFO is_sf_info; /* MPEG-2 intensity stereo */ // L3 only ############## scratchpad?
120
121 /*---------------------------------*/
122 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
123 /* main data bit buffer */
124 /*@@@@
125 #define NBUF (8*1024)
126 #define BUF_TRIGGER (NBUF-1500)
127 static unsigned char buf[NBUF];
128 static int buf_ptr0 = 0; // !!!!!!!!!!!
129 static int buf_ptr1 = 0; // !!!!!!!!!!!
130 static int main_pos_bit;
131 */
132 /*---------------------------------*/
133 static SIDE_INFO side_info; // ####### scratchpad?
134
135 static SCALEFACT sf[2][2]; /* [gr][ch] */ // ########## scratchpad?
136
137 static int nsamp[2][2]; /* must start = 0, for nsamp[igr_prev] */ // ########## scratchpad?
138
139 /*- sample union of int/float sample[ch][gr][576] */
140 /* static SAMPLE sample[2][2][576]; */
141 // @@@@FINDME
142 ////@@@@extern SAMPLE sample[2][2][576]; ////////////????? suspicious, mainly used in decode loop, but zeroed init code
143 static float yout[576]; /* hybrid out, sbt in */ //////////// scratchpad
144
145 ////@@@@typedef void (*SBT_FUNCTION) (float *sample, short *pcm, int ch);
146 void sbt_dual_L3(float *sample, short *pcm, int n);
147 ////@@@@static SBT_FUNCTION sbt_L3 = sbt_dual_L3; // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
148
149 ////@@@@typedef void (*XFORM_FUNCTION) (void *pcm, int igr);
150 static void Xform_dual(void *pcm, int igr);
151 ////@@@@static XFORM_FUNCTION Xform = Xform_dual; // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
152
153 IN_OUT L3audio_decode_MPEG1(unsigned char *bs, unsigned char *pcm);
154 IN_OUT L3audio_decode_MPEG2(unsigned char *bs, unsigned char *pcm);
155 ////@@@@typedef IN_OUT(*DECODE_FUNCTION) (unsigned char *bs, unsigned char *pcm);
156 ////@@@@static DECODE_FUNCTION decode_function = L3audio_decode_MPEG1; <------------------ needs streaming, ditto above!!!
157
158
159 /*====================================================================*/
160 int hybrid(void *xin, void *xprev, float *y,
161 int btype, int nlong, int ntot, int nprev);
162 int hybrid_sum(void *xin, void *xin_left, float *y,
163 int btype, int nlong, int ntot);
164 void sum_f_bands(void *a, void *b, int n);
165 void FreqInvert(float *y, int n);
166 void antialias(void *x, int n);
167 void ms_process(void *x, int n); /* sum-difference stereo */
168 void is_process_MPEG1(void *x, /* intensity stereo */
169 SCALEFACT * sf,
170 CB_INFO cb_info[2], /* [ch] */
171 int nsamp, int ms_mode);
172 void is_process_MPEG2(void *x, /* intensity stereo */
173 SCALEFACT * sf,
174 CB_INFO cb_info[2], /* [ch] */
175 IS_SF_INFO * is_sf_info,
176 int nsamp, int ms_mode);
177
178 void unpack_huff(void *xy, int n, int ntable);
179 int unpack_huff_quad(void *vwxy, int n, int nbits, int ntable);
180 void dequant(SAMPLE sample[], int *nsamp,
181 SCALEFACT * sf,
182 GR * gr,
183 CB_INFO * cb_info, int ncbl_mixed);
184 void unpack_sf_sub_MPEG1(SCALEFACT * scalefac, GR * gr,
185 int scfsi, /* bit flag */
186 int igr);
187 void unpack_sf_sub_MPEG2(SCALEFACT sf[], /* return intensity scale */
188 GR * grdat,
189 int is_and_ch, IS_SF_INFO * is_sf_info);
190
191 /*====================================================================*/
192 /* get bits from bitstream in endian independent way */
193
194 BITDAT bitdat; /* global for inline use by Huff */ // !!!!!!!!!!!!!!!!!!!
195
196 /*------------- initialize bit getter -------------*/
bitget_init(unsigned char * buf)197 static void bitget_init(unsigned char *buf)
198 {
199 bitdat.bs_ptr0 = bitdat.bs_ptr = buf;
200 bitdat.bits = 0;
201 bitdat.bitbuf = 0;
202 }
203 /*------------- initialize bit getter -------------*/
bitget_init_end(unsigned char * buf_end)204 static void bitget_init_end(unsigned char *buf_end)
205 {
206 bitdat.bs_ptr_end = buf_end;
207 }
208 /*------------- get n bits from bitstream -------------*/
bitget_bits_used()209 int bitget_bits_used()
210 {
211 int n; /* compute bits used from last init call */
212
213 n = ((bitdat.bs_ptr - bitdat.bs_ptr0) << 3) - bitdat.bits;
214 return n;
215 }
216 /*------------- check for n bits in bitbuf -------------*/
bitget_check(int n)217 void bitget_check(int n)
218 {
219 if (bitdat.bits < n)
220 {
221 while (bitdat.bits <= 24)
222 {
223 bitdat.bitbuf = (bitdat.bitbuf << 8) | *bitdat.bs_ptr++;
224 bitdat.bits += 8;
225 }
226 }
227 }
228 /*------------- get n bits from bitstream -------------*/
bitget(int n)229 unsigned int bitget(int n)
230 {
231 unsigned int x;
232
233 if (bitdat.bits < n)
234 { /* refill bit buf if necessary */
235 while (bitdat.bits <= 24)
236 {
237 bitdat.bitbuf = (bitdat.bitbuf << 8) | *bitdat.bs_ptr++;
238 bitdat.bits += 8;
239 }
240 }
241 bitdat.bits -= n;
242 x = bitdat.bitbuf >> bitdat.bits;
243 bitdat.bitbuf -= x << bitdat.bits;
244 return x;
245 }
246 /*------------- get 1 bit from bitstream -------------*/
bitget_1bit()247 unsigned int bitget_1bit()
248 {
249 unsigned int x;
250
251 if (bitdat.bits <= 0)
252 { /* refill bit buf if necessary */
253 while (bitdat.bits <= 24)
254 {
255 bitdat.bitbuf = (bitdat.bitbuf << 8) | *bitdat.bs_ptr++;
256 bitdat.bits += 8;
257 }
258 }
259 bitdat.bits--;
260 x = bitdat.bitbuf >> bitdat.bits;
261 bitdat.bitbuf -= x << bitdat.bits;
262 return x;
263 }
264 /*====================================================================*/
Xform_mono(void * pcm,int igr)265 static void Xform_mono(void *pcm, int igr)
266 {
267 int igr_prev, n1, n2;
268
269 /*--- hybrid + sbt ---*/
270 n1 = n2 = nsamp[igr][0]; /* total number bands */
271 if (side_info.gr[igr][0].block_type == 2)
272 { /* long bands */
273 n1 = 0;
274 if (side_info.gr[igr][0].mixed_block_flag)
275 n1 = pMP3Stream->sfBandIndex[0][pMP3Stream->ncbl_mixed - 1];
276 }
277 if (n1 > pMP3Stream->band_limit)
278 n1 = pMP3Stream->band_limit;
279 if (n2 > pMP3Stream->band_limit)
280 n2 = pMP3Stream->band_limit;
281 igr_prev = igr ^ 1;
282
283 nsamp[igr][0] = hybrid(pMP3Stream->sample[0][igr], pMP3Stream->sample[0][igr_prev],
284 yout, side_info.gr[igr][0].block_type, n1, n2, nsamp[igr_prev][0]);
285 FreqInvert(yout, nsamp[igr][0]);
286 pMP3Stream->sbt_L3(yout, pcm, 0);
287
288 }
289 /*--------------------------------------------------------------------*/
Xform_dual_right(void * pcm,int igr)290 static void Xform_dual_right(void *pcm, int igr)
291 {
292 int igr_prev, n1, n2;
293
294 /*--- hybrid + sbt ---*/
295 n1 = n2 = nsamp[igr][1]; /* total number bands */
296 if (side_info.gr[igr][1].block_type == 2)
297 { /* long bands */
298 n1 = 0;
299 if (side_info.gr[igr][1].mixed_block_flag)
300 n1 = pMP3Stream->sfBandIndex[0][pMP3Stream->ncbl_mixed - 1];
301 }
302 if (n1 > pMP3Stream->band_limit)
303 n1 = pMP3Stream->band_limit;
304 if (n2 > pMP3Stream->band_limit)
305 n2 = pMP3Stream->band_limit;
306 igr_prev = igr ^ 1;
307 nsamp[igr][1] = hybrid(pMP3Stream->sample[1][igr], pMP3Stream->sample[1][igr_prev],
308 yout, side_info.gr[igr][1].block_type, n1, n2, nsamp[igr_prev][1]);
309 FreqInvert(yout, nsamp[igr][1]);
310 pMP3Stream->sbt_L3(yout, pcm, 0);
311
312 }
313 /*--------------------------------------------------------------------*/
Xform_dual(void * pcm,int igr)314 static void Xform_dual(void *pcm, int igr)
315 {
316 int ch;
317 int igr_prev, n1, n2;
318
319 /*--- hybrid + sbt ---*/
320 igr_prev = igr ^ 1;
321 for (ch = 0; ch < pMP3Stream->nchan; ch++)
322 {
323 n1 = n2 = nsamp[igr][ch]; /* total number bands */
324 if (side_info.gr[igr][ch].block_type == 2)
325 { /* long bands */
326 n1 = 0;
327 if (side_info.gr[igr][ch].mixed_block_flag)
328 n1 = pMP3Stream->sfBandIndex[0][pMP3Stream->ncbl_mixed - 1];
329 }
330 if (n1 > pMP3Stream->band_limit)
331 n1 = pMP3Stream->band_limit;
332 if (n2 > pMP3Stream->band_limit)
333 n2 = pMP3Stream->band_limit;
334 nsamp[igr][ch] = hybrid(pMP3Stream->sample[ch][igr], pMP3Stream->sample[ch][igr_prev],
335 yout, side_info.gr[igr][ch].block_type, n1, n2, nsamp[igr_prev][ch]);
336 FreqInvert(yout, nsamp[igr][ch]);
337 pMP3Stream->sbt_L3(yout, pcm, ch);
338 }
339
340 }
341 /*--------------------------------------------------------------------*/
Xform_dual_mono(void * pcm,int igr)342 static void Xform_dual_mono(void *pcm, int igr)
343 {
344 int igr_prev, n1, n2, n3;
345
346 /*--- hybrid + sbt ---*/
347 igr_prev = igr ^ 1;
348 if ((side_info.gr[igr][0].block_type == side_info.gr[igr][1].block_type)
349 && (side_info.gr[igr][0].mixed_block_flag == 0)
350 && (side_info.gr[igr][1].mixed_block_flag == 0))
351 {
352
353 n2 = nsamp[igr][0]; /* total number bands max of L R */
354 if (n2 < nsamp[igr][1])
355 n2 = nsamp[igr][1];
356 if (n2 > pMP3Stream->band_limit)
357 n2 = pMP3Stream->band_limit;
358 n1 = n2; /* n1 = number long bands */
359 if (side_info.gr[igr][0].block_type == 2)
360 n1 = 0;
361 sum_f_bands(pMP3Stream->sample[0][igr], pMP3Stream->sample[1][igr], n2);
362 n3 = nsamp[igr][0] = hybrid(pMP3Stream->sample[0][igr], pMP3Stream->sample[0][igr_prev],
363 yout, side_info.gr[igr][0].block_type, n1, n2, nsamp[igr_prev][0]);
364 }
365 else
366 { /* transform and then sum (not tested - never happens in test) */
367 /*-- left chan --*/
368 n1 = n2 = nsamp[igr][0]; /* total number bands */
369 if (side_info.gr[igr][0].block_type == 2)
370 {
371 n1 = 0; /* long bands */
372 if (side_info.gr[igr][0].mixed_block_flag)
373 n1 = pMP3Stream->sfBandIndex[0][pMP3Stream->ncbl_mixed - 1];
374 }
375 n3 = nsamp[igr][0] = hybrid(pMP3Stream->sample[0][igr], pMP3Stream->sample[0][igr_prev],
376 yout, side_info.gr[igr][0].block_type, n1, n2, nsamp[igr_prev][0]);
377 /*-- right chan --*/
378 n1 = n2 = nsamp[igr][1]; /* total number bands */
379 if (side_info.gr[igr][1].block_type == 2)
380 {
381 n1 = 0; /* long bands */
382 if (side_info.gr[igr][1].mixed_block_flag)
383 n1 = pMP3Stream->sfBandIndex[0][pMP3Stream->ncbl_mixed - 1];
384 }
385 nsamp[igr][1] = hybrid_sum(pMP3Stream->sample[1][igr], pMP3Stream->sample[0][igr],
386 yout, side_info.gr[igr][1].block_type, n1, n2);
387 if (n3 < nsamp[igr][1])
388 n1 = nsamp[igr][1];
389 }
390
391 /*--------*/
392 FreqInvert(yout, n3);
393 pMP3Stream->sbt_L3(yout, pcm, 0);
394
395 }
396 /*--------------------------------------------------------------------*/
397 /*====================================================================*/
unpack_side_MPEG1()398 static int unpack_side_MPEG1()
399 {
400 int prot;
401 int br_index;
402 int igr, ch;
403 int side_bytes;
404
405 /* decode partial header plus initial side info */
406 /* at entry bit getter points at id, sync skipped by caller */
407
408 pMP3Stream->id = bitget(1); /* id */
409 bitget(2); /* skip layer */
410 prot = bitget(1); /* bitget prot bit */
411 br_index = bitget(4);
412 pMP3Stream->sr_index = bitget(2);
413 pMP3Stream->pad = bitget(1);
414 bitget(1); /* skip to mode */
415 side_info.mode = bitget(2); /* mode */
416 side_info.mode_ext = bitget(2); /* mode ext */
417
418 if (side_info.mode != 1)
419 side_info.mode_ext = 0;
420
421 /* adjust global gain in ms mode to avoid having to mult by 1/sqrt(2) */
422 pMP3Stream->ms_mode = side_info.mode_ext >> 1;
423 pMP3Stream->is_mode = side_info.mode_ext & 1;
424
425
426 pMP3Stream->crcbytes = 0;
427 if (prot)
428 bitget(4); /* skip to data */
429 else
430 {
431 bitget(20); /* skip crc */
432 pMP3Stream->crcbytes = 2;
433 }
434
435 if (br_index > 0) /* pMP3Stream->framebytes fixed for free format */
436 {
437 pMP3Stream->framebytes =
438 2880 * mp_br_tableL3[pMP3Stream->id][br_index] / mp_sr20_table[pMP3Stream->id][pMP3Stream->sr_index];
439 }
440
441 side_info.main_data_begin = bitget(9);
442 if (side_info.mode == 3)
443 {
444 side_info.private_bits = bitget(5);
445 pMP3Stream->nchan = 1;
446 // stereo_flag = 0;
447 side_bytes = (4 + 17);
448 /*-- with header --*/
449 }
450 else
451 {
452 side_info.private_bits = bitget(3);
453 pMP3Stream->nchan = 2;
454 // stereo_flag = 1;
455 side_bytes = (4 + 32);
456 /*-- with header --*/
457 }
458 for (ch = 0; ch < pMP3Stream->nchan; ch++)
459 side_info.scfsi[ch] = bitget(4);
460 /* this always 0 (both igr) for short blocks */
461
462 for (igr = 0; igr < 2; igr++)
463 {
464 for (ch = 0; ch < pMP3Stream->nchan; ch++)
465 {
466 side_info.gr[igr][ch].part2_3_length = bitget(12);
467 side_info.gr[igr][ch].big_values = bitget(9);
468 side_info.gr[igr][ch].global_gain = bitget(8) + pMP3Stream->gain_adjust;
469 if (pMP3Stream->ms_mode)
470 side_info.gr[igr][ch].global_gain -= 2;
471 side_info.gr[igr][ch].scalefac_compress = bitget(4);
472 side_info.gr[igr][ch].window_switching_flag = bitget(1);
473 if (side_info.gr[igr][ch].window_switching_flag)
474 {
475 side_info.gr[igr][ch].block_type = bitget(2);
476 side_info.gr[igr][ch].mixed_block_flag = bitget(1);
477 side_info.gr[igr][ch].table_select[0] = bitget(5);
478 side_info.gr[igr][ch].table_select[1] = bitget(5);
479 side_info.gr[igr][ch].subblock_gain[0] = bitget(3);
480 side_info.gr[igr][ch].subblock_gain[1] = bitget(3);
481 side_info.gr[igr][ch].subblock_gain[2] = bitget(3);
482 /* region count set in terms of long block cb's/bands */
483 /* r1 set so r0+r1+1 = 21 (lookup produces 576 bands ) */
484 /* if(window_switching_flag) always 36 samples in region0 */
485 side_info.gr[igr][ch].region0_count = (8 - 1); /* 36 samples */
486 side_info.gr[igr][ch].region1_count = 20 - (8 - 1);
487 }
488 else
489 {
490 side_info.gr[igr][ch].mixed_block_flag = 0;
491 side_info.gr[igr][ch].block_type = 0;
492 side_info.gr[igr][ch].table_select[0] = bitget(5);
493 side_info.gr[igr][ch].table_select[1] = bitget(5);
494 side_info.gr[igr][ch].table_select[2] = bitget(5);
495 side_info.gr[igr][ch].region0_count = bitget(4);
496 side_info.gr[igr][ch].region1_count = bitget(3);
497 }
498 side_info.gr[igr][ch].preflag = bitget(1);
499 side_info.gr[igr][ch].scalefac_scale = bitget(1);
500 side_info.gr[igr][ch].count1table_select = bitget(1);
501 }
502 }
503
504
505
506 /* return bytes in header + side info */
507 return side_bytes;
508 }
509 /*====================================================================*/
unpack_side_MPEG2(int igr)510 static int unpack_side_MPEG2(int igr)
511 {
512 int prot;
513 int br_index;
514 int ch;
515 int side_bytes;
516
517 /* decode partial header plus initial side info */
518 /* at entry bit getter points at id, sync skipped by caller */
519
520 pMP3Stream->id = bitget(1); /* id */
521 bitget(2); /* skip layer */
522 prot = bitget(1); /* bitget prot bit */
523 br_index = bitget(4);
524 pMP3Stream->sr_index = bitget(2);
525 pMP3Stream->pad = bitget(1);
526 bitget(1); /* skip to mode */
527 side_info.mode = bitget(2); /* mode */
528 side_info.mode_ext = bitget(2); /* mode ext */
529
530 if (side_info.mode != 1)
531 side_info.mode_ext = 0;
532
533 /* adjust global gain in ms mode to avoid having to mult by 1/sqrt(2) */
534 pMP3Stream->ms_mode = side_info.mode_ext >> 1;
535 pMP3Stream->is_mode = side_info.mode_ext & 1;
536
537 pMP3Stream->crcbytes = 0;
538 if (prot)
539 bitget(4); /* skip to data */
540 else
541 {
542 bitget(20); /* skip crc */
543 pMP3Stream->crcbytes = 2;
544 }
545
546 if (br_index > 0)
547 { /* pMP3Stream->framebytes fixed for free format */
548 if (pMP3Stream->mpeg25_flag == 0)
549 {
550 pMP3Stream->framebytes =
551 1440 * mp_br_tableL3[pMP3Stream->id][br_index] / mp_sr20_table[pMP3Stream->id][pMP3Stream->sr_index];
552 }
553 else
554 {
555 pMP3Stream->framebytes =
556 2880 * mp_br_tableL3[pMP3Stream->id][br_index] / mp_sr20_table[pMP3Stream->id][pMP3Stream->sr_index];
557 //if( pMP3Stream->sr_index == 2 ) return 0; // fail mpeg25 8khz
558 }
559 }
560 side_info.main_data_begin = bitget(8);
561 if (side_info.mode == 3)
562 {
563 side_info.private_bits = bitget(1);
564 pMP3Stream->nchan = 1;
565 // stereo_flag = 0;
566 side_bytes = (4 + 9);
567 /*-- with header --*/
568 }
569 else
570 {
571 side_info.private_bits = bitget(2);
572 pMP3Stream->nchan = 2;
573 // stereo_flag = 1;
574 side_bytes = (4 + 17);
575 /*-- with header --*/
576 }
577 side_info.scfsi[1] = side_info.scfsi[0] = 0;
578
579
580 for (ch = 0; ch < pMP3Stream->nchan; ch++)
581 {
582 side_info.gr[igr][ch].part2_3_length = bitget(12);
583 side_info.gr[igr][ch].big_values = bitget(9);
584 side_info.gr[igr][ch].global_gain = bitget(8) + pMP3Stream->gain_adjust;
585 if (pMP3Stream->ms_mode)
586 side_info.gr[igr][ch].global_gain -= 2;
587 side_info.gr[igr][ch].scalefac_compress = bitget(9);
588 side_info.gr[igr][ch].window_switching_flag = bitget(1);
589 if (side_info.gr[igr][ch].window_switching_flag)
590 {
591 side_info.gr[igr][ch].block_type = bitget(2);
592 side_info.gr[igr][ch].mixed_block_flag = bitget(1);
593 side_info.gr[igr][ch].table_select[0] = bitget(5);
594 side_info.gr[igr][ch].table_select[1] = bitget(5);
595 side_info.gr[igr][ch].subblock_gain[0] = bitget(3);
596 side_info.gr[igr][ch].subblock_gain[1] = bitget(3);
597 side_info.gr[igr][ch].subblock_gain[2] = bitget(3);
598 /* region count set in terms of long block cb's/bands */
599 /* r1 set so r0+r1+1 = 21 (lookup produces 576 bands ) */
600 /* bt=1 or 3 54 samples */
601 /* bt=2 mixed=0 36 samples */
602 /* bt=2 mixed=1 54 (8 long sf) samples? or maybe 36 */
603 /* region0 discussion says 54 but this would mix long */
604 /* and short in region0 if scale factors switch */
605 /* at band 36 (6 long scale factors) */
606 if (side_info.gr[igr][ch].block_type == 2)
607 {
608 side_info.gr[igr][ch].region0_count = (6 - 1); /* 36 samples */
609 side_info.gr[igr][ch].region1_count = 20 - (6 - 1);
610 }
611 else
612 { /* long block type 1 or 3 */
613 side_info.gr[igr][ch].region0_count = (8 - 1); /* 54 samples */
614 side_info.gr[igr][ch].region1_count = 20 - (8 - 1);
615 }
616 }
617 else
618 {
619 side_info.gr[igr][ch].mixed_block_flag = 0;
620 side_info.gr[igr][ch].block_type = 0;
621 side_info.gr[igr][ch].table_select[0] = bitget(5);
622 side_info.gr[igr][ch].table_select[1] = bitget(5);
623 side_info.gr[igr][ch].table_select[2] = bitget(5);
624 side_info.gr[igr][ch].region0_count = bitget(4);
625 side_info.gr[igr][ch].region1_count = bitget(3);
626 }
627 side_info.gr[igr][ch].preflag = 0;
628 side_info.gr[igr][ch].scalefac_scale = bitget(1);
629 side_info.gr[igr][ch].count1table_select = bitget(1);
630 }
631
632 /* return bytes in header + side info */
633 return side_bytes;
634 }
635 /*-----------------------------------------------------------------*/
unpack_main(unsigned char * pcm,int igr)636 static void unpack_main(unsigned char *pcm, int igr)
637 {
638 int ch;
639 int bit0;
640 int n1, n2, n3, n4, nn2, nn3;
641 int nn4;
642 int qbits;
643 int m0;
644
645
646 for (ch = 0; ch < pMP3Stream->nchan; ch++)
647 {
648 bitget_init(pMP3Stream->buf + (pMP3Stream->main_pos_bit >> 3));
649 bit0 = (pMP3Stream->main_pos_bit & 7);
650 if (bit0)
651 bitget(bit0);
652 pMP3Stream->main_pos_bit += side_info.gr[igr][ch].part2_3_length;
653 bitget_init_end(pMP3Stream->buf + ((pMP3Stream->main_pos_bit + 39) >> 3));
654 /*-- scale factors --*/
655 if (pMP3Stream->id)
656 unpack_sf_sub_MPEG1(&sf[igr][ch],
657 &side_info.gr[igr][ch], side_info.scfsi[ch], igr);
658 else
659 unpack_sf_sub_MPEG2(&sf[igr][ch],
660 &side_info.gr[igr][ch], pMP3Stream->is_mode & ch, &is_sf_info);
661 /*--- huff data ---*/
662 n1 = pMP3Stream->sfBandIndex[0][side_info.gr[igr][ch].region0_count];
663 n2 = pMP3Stream->sfBandIndex[0][side_info.gr[igr][ch].region0_count
664 + side_info.gr[igr][ch].region1_count + 1];
665 n3 = side_info.gr[igr][ch].big_values;
666 n3 = n3 + n3;
667
668
669 if (n3 > pMP3Stream->band_limit)
670 n3 = pMP3Stream->band_limit;
671 if (n2 > n3)
672 n2 = n3;
673 if (n1 > n3)
674 n1 = n3;
675 nn3 = n3 - n2;
676 nn2 = n2 - n1;
677 unpack_huff(pMP3Stream->sample[ch][igr], n1, side_info.gr[igr][ch].table_select[0]);
678 unpack_huff(pMP3Stream->sample[ch][igr] + n1, nn2, side_info.gr[igr][ch].table_select[1]);
679 unpack_huff(pMP3Stream->sample[ch][igr] + n2, nn3, side_info.gr[igr][ch].table_select[2]);
680 qbits = side_info.gr[igr][ch].part2_3_length - (bitget_bits_used() - bit0);
681 nn4 = unpack_huff_quad(pMP3Stream->sample[ch][igr] + n3, pMP3Stream->band_limit - n3, qbits,
682 side_info.gr[igr][ch].count1table_select);
683 n4 = n3 + nn4;
684 nsamp[igr][ch] = n4;
685 //limit n4 or allow deqaunt to sf band 22
686 if (side_info.gr[igr][ch].block_type == 2)
687 n4 = min(n4, pMP3Stream->band_limit12);
688 else
689 n4 = min(n4, pMP3Stream->band_limit21);
690 if (n4 < 576)
691 memset(pMP3Stream->sample[ch][igr] + n4, 0, sizeof(SAMPLE) * (576 - n4));
692 if (bitdat.bs_ptr > bitdat.bs_ptr_end)
693 { // bad data overrun
694
695 memset(pMP3Stream->sample[ch][igr], 0, sizeof(SAMPLE) * (576));
696 }
697 }
698
699
700
701 /*--- dequant ---*/
702 for (ch = 0; ch < pMP3Stream->nchan; ch++)
703 {
704 dequant(pMP3Stream->sample[ch][igr],
705 &nsamp[igr][ch], /* nsamp updated for shorts */
706 &sf[igr][ch], &side_info.gr[igr][ch],
707 &cb_info[igr][ch], pMP3Stream->ncbl_mixed);
708 }
709
710 /*--- ms stereo processing ---*/
711 if (pMP3Stream->ms_mode)
712 {
713 if (pMP3Stream->is_mode == 0)
714 {
715 m0 = nsamp[igr][0]; /* process to longer of left/right */
716 if (m0 < nsamp[igr][1])
717 m0 = nsamp[igr][1];
718 }
719 else
720 { /* process to last cb in right */
721 m0 = pMP3Stream->sfBandIndex[cb_info[igr][1].cbtype][cb_info[igr][1].cbmax];
722 }
723 ms_process(pMP3Stream->sample[0][igr], m0);
724 }
725
726 /*--- is stereo processing ---*/
727 if (pMP3Stream->is_mode)
728 {
729 if (pMP3Stream->id)
730 is_process_MPEG1(pMP3Stream->sample[0][igr], &sf[igr][1],
731 cb_info[igr], nsamp[igr][0], pMP3Stream->ms_mode);
732 else
733 is_process_MPEG2(pMP3Stream->sample[0][igr], &sf[igr][1],
734 cb_info[igr], &is_sf_info,
735 nsamp[igr][0], pMP3Stream->ms_mode);
736 }
737
738 /*-- adjust ms and is modes to max of left/right */
739 if (side_info.mode_ext)
740 {
741 if (nsamp[igr][0] < nsamp[igr][1])
742 nsamp[igr][0] = nsamp[igr][1];
743 else
744 nsamp[igr][1] = nsamp[igr][0];
745 }
746
747 /*--- antialias ---*/
748 for (ch = 0; ch < pMP3Stream->nchan; ch++)
749 {
750 if (cb_info[igr][ch].ncbl == 0)
751 continue; /* have no long blocks */
752 if (side_info.gr[igr][ch].mixed_block_flag)
753 n1 = 1; /* 1 -> 36 samples */
754 else
755 n1 = (nsamp[igr][ch] + 7) / 18;
756 if (n1 > 31)
757 n1 = 31;
758 antialias(pMP3Stream->sample[ch][igr], n1);
759 n1 = 18 * n1 + 8; /* update number of samples */
760 if (n1 > nsamp[igr][ch])
761 nsamp[igr][ch] = n1;
762 }
763
764
765
766 /*--- hybrid + sbt ---*/
767 pMP3Stream->Xform(pcm, igr);
768
769
770 /*-- done --*/
771 }
772 /*--------------------------------------------------------------------*/
773 /*-----------------------------------------------------------------*/
L3audio_decode(unsigned char * bs,unsigned char * pcm)774 IN_OUT L3audio_decode(unsigned char *bs, unsigned char *pcm)
775 {
776 return pMP3Stream->decode_function(bs, pcm);
777 }
778
779 /*--------------------------------------------------------------------*/
780 extern unsigned char *gpNextByteAfterData;
L3audio_decode_MPEG1(unsigned char * bs,unsigned char * pcm)781 IN_OUT L3audio_decode_MPEG1(unsigned char *bs, unsigned char *pcm)
782 {
783 int sync;
784 IN_OUT in_out;
785 int side_bytes;
786 int nbytes;
787
788 int padframebytes; ////@@@@
789
790 // iframe++;
791
792 bitget_init(bs); /* initialize bit getter */
793 /* test sync */
794 in_out.in_bytes = 0; /* assume fail */
795 in_out.out_bytes = 0;
796 sync = bitget(12);
797
798 if (sync != 0xFFF)
799 return in_out; /* sync fail */
800 /*-----------*/
801
802 /*-- unpack side info --*/
803 side_bytes = unpack_side_MPEG1();
804 padframebytes = pMP3Stream->framebytes + pMP3Stream->pad;
805
806 if (bs + padframebytes > gpNextByteAfterData)
807 return in_out; // error check if we're about to read off the end of the legal memory (caused by certain MP3 writers' goofy comment formats) -ste.
808 in_out.in_bytes = padframebytes;
809
810 /*-- load main data and update buf pointer --*/
811 /*-------------------------------------------
812 if start point < 0, must just cycle decoder
813 if jumping into middle of stream,
814 w---------------------------------------------*/
815 pMP3Stream->buf_ptr0 = pMP3Stream->buf_ptr1 - side_info.main_data_begin; /* decode start point */
816 if (pMP3Stream->buf_ptr1 > BUF_TRIGGER)
817 { /* shift buffer */
818 memmove(pMP3Stream->buf, pMP3Stream->buf + pMP3Stream->buf_ptr0, side_info.main_data_begin);
819 pMP3Stream->buf_ptr0 = 0;
820 pMP3Stream->buf_ptr1 = side_info.main_data_begin;
821 }
822 nbytes = padframebytes - side_bytes - pMP3Stream->crcbytes;
823
824 // RAK: This is no bueno. :-(
825 if (nbytes < 0 || nbytes > NBUF)
826 {
827 in_out.in_bytes = 0;
828 return in_out;
829 }
830
831 if (bFastEstimateOnly)
832 {
833 in_out.out_bytes = pMP3Stream->outbytes;
834 return in_out;
835 }
836
837 memmove(pMP3Stream->buf + pMP3Stream->buf_ptr1, bs + side_bytes + pMP3Stream->crcbytes, nbytes);
838 pMP3Stream->buf_ptr1 += nbytes;
839 /*-----------------------*/
840
841 if (pMP3Stream->buf_ptr0 >= 0)
842 {
843 // dump_frame(buf+buf_ptr0, 64);
844 pMP3Stream->main_pos_bit = pMP3Stream->buf_ptr0 << 3;
845 unpack_main(pcm, 0);
846 unpack_main(pcm + pMP3Stream->half_outbytes, 1);
847 in_out.out_bytes = pMP3Stream->outbytes;
848 }
849 else
850 {
851 memset(pcm, pMP3Stream->zero_level_pcm, pMP3Stream->outbytes); /* fill out skipped frames */
852 in_out.out_bytes = pMP3Stream->outbytes;
853 /* iframe--; in_out.out_bytes = 0; // test test */
854 }
855
856 return in_out;
857 }
858 /*--------------------------------------------------------------------*/
859 /*--------------------------------------------------------------------*/
L3audio_decode_MPEG2(unsigned char * bs,unsigned char * pcm)860 IN_OUT L3audio_decode_MPEG2(unsigned char *bs, unsigned char *pcm)
861 {
862 int sync;
863 IN_OUT in_out;
864 int side_bytes;
865 int nbytes;
866 static int igr = 0;
867
868 int padframebytes; ////@@@@
869
870 // iframe++;
871
872
873 bitget_init(bs); /* initialize bit getter */
874 /* test sync */
875 in_out.in_bytes = 0; /* assume fail */
876 in_out.out_bytes = 0;
877 sync = bitget(12);
878
879 // if( sync != 0xFFF ) return in_out; /* sync fail */
880
881 pMP3Stream->mpeg25_flag = 0;
882 if (sync != 0xFFF)
883 {
884 pMP3Stream->mpeg25_flag = 1; /* mpeg 2.5 sync */
885 if (sync != 0xFFE)
886 return in_out; /* sync fail */
887 }
888 /*-----------*/
889
890
891 /*-- unpack side info --*/
892 side_bytes = unpack_side_MPEG2(igr);
893 padframebytes = pMP3Stream->framebytes + pMP3Stream->pad;
894 in_out.in_bytes = padframebytes;
895
896 pMP3Stream->buf_ptr0 = pMP3Stream->buf_ptr1 - side_info.main_data_begin; /* decode start point */
897 if (pMP3Stream->buf_ptr1 > BUF_TRIGGER)
898 { /* shift buffer */
899 memmove(pMP3Stream->buf, pMP3Stream->buf + pMP3Stream->buf_ptr0, side_info.main_data_begin);
900 pMP3Stream->buf_ptr0 = 0;
901 pMP3Stream->buf_ptr1 = side_info.main_data_begin;
902 }
903 nbytes = padframebytes - side_bytes - pMP3Stream->crcbytes;
904 // RAK: This is no bueno. :-(
905 if (nbytes < 0 || nbytes > NBUF)
906 {
907 in_out.in_bytes = 0;
908 return in_out;
909 }
910
911 if (bFastEstimateOnly)
912 {
913 in_out.out_bytes = pMP3Stream->outbytes;
914 return in_out;
915 }
916
917 memmove(pMP3Stream->buf + pMP3Stream->buf_ptr1, bs + side_bytes + pMP3Stream->crcbytes, nbytes);
918 pMP3Stream->buf_ptr1 += nbytes;
919 /*-----------------------*/
920
921 if (pMP3Stream->buf_ptr0 >= 0)
922 {
923 pMP3Stream->main_pos_bit = pMP3Stream->buf_ptr0 << 3;
924 unpack_main(pcm, igr);
925 in_out.out_bytes = pMP3Stream->outbytes;
926 }
927 else
928 {
929 memset(pcm, pMP3Stream->zero_level_pcm, pMP3Stream->outbytes); /* fill out skipped frames */
930 in_out.out_bytes = pMP3Stream->outbytes;
931 // iframe--; in_out.out_bytes = 0; return in_out;// test test */
932 }
933
934
935
936 igr = igr ^ 1;
937 return in_out;
938 }
939 /*--------------------------------------------------------------------*/
940 /*--------------------------------------------------------------------*/
941 /*--------------------------------------------------------------------*/
942 static const int sr_table[8] =
943 {22050, 24000, 16000, 1,
944 44100, 48000, 32000, 1};
945
946 static const struct
947 {
948 int l[23];
949 int s[14];
950 }
951 sfBandIndexTable[3][3] =
952 {
953 /* mpeg-2 */
954 {
955 {
956 {
957 0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464, 522, 576
958 }
959 ,
960 {
961 0, 4, 8, 12, 18, 24, 32, 42, 56, 74, 100, 132, 174, 192
962 }
963 }
964 ,
965 {
966 {
967 0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 114, 136, 162, 194, 232, 278, 332, 394, 464, 540, 576
968 }
969 ,
970 {
971 0, 4, 8, 12, 18, 26, 36, 48, 62, 80, 104, 136, 180, 192
972 }
973 }
974 ,
975 {
976 {
977 0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464, 522, 576
978 }
979 ,
980 {
981 0, 4, 8, 12, 18, 26, 36, 48, 62, 80, 104, 134, 174, 192
982 }
983 }
984 ,
985 }
986 ,
987 /* mpeg-1 */
988 {
989 {
990 {
991 0, 4, 8, 12, 16, 20, 24, 30, 36, 44, 52, 62, 74, 90, 110, 134, 162, 196, 238, 288, 342, 418, 576
992 }
993 ,
994 {
995 0, 4, 8, 12, 16, 22, 30, 40, 52, 66, 84, 106, 136, 192
996 }
997 }
998 ,
999 {
1000 {
1001 0, 4, 8, 12, 16, 20, 24, 30, 36, 42, 50, 60, 72, 88, 106, 128, 156, 190, 230, 276, 330, 384, 576
1002 }
1003 ,
1004 {
1005 0, 4, 8, 12, 16, 22, 28, 38, 50, 64, 80, 100, 126, 192
1006 }
1007 }
1008 ,
1009 {
1010 {
1011 0, 4, 8, 12, 16, 20, 24, 30, 36, 44, 54, 66, 82, 102, 126, 156, 194, 240, 296, 364, 448, 550, 576
1012 }
1013 ,
1014 {
1015 0, 4, 8, 12, 16, 22, 30, 42, 58, 78, 104, 138, 180, 192
1016 }
1017 }
1018 }
1019 ,
1020
1021 /* mpeg-2.5, 11 & 12 KHz seem ok, 8 ok */
1022 {
1023 {
1024 {
1025 0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464, 522, 576
1026 }
1027 ,
1028 {
1029 0, 4, 8, 12, 18, 26, 36, 48, 62, 80, 104, 134, 174, 192
1030 }
1031 }
1032 ,
1033 {
1034 {
1035 0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464, 522, 576
1036 }
1037 ,
1038 {
1039 0, 4, 8, 12, 18, 26, 36, 48, 62, 80, 104, 134, 174, 192
1040 }
1041 }
1042 ,
1043 // this 8khz table, and only 8khz, from mpeg123)
1044 {
1045 {
1046 0, 12, 24, 36, 48, 60, 72, 88, 108, 132, 160, 192, 232, 280, 336, 400, 476, 566, 568, 570, 572, 574, 576
1047 }
1048 ,
1049 {
1050 0, 8, 16, 24, 36, 52, 72, 96, 124, 160, 162, 164, 166, 192
1051 }
1052 }
1053 ,
1054 }
1055 ,
1056 };
1057
1058
1059 void sbt_mono_L3(float *sample, signed short *pcm, int ch);
1060 void sbt_dual_L3(float *sample, signed short *pcm, int ch);
1061 void sbt16_mono_L3(float *sample, signed short *pcm, int ch);
1062 void sbt16_dual_L3(float *sample, signed short *pcm, int ch);
1063 void sbt8_mono_L3(float *sample, signed short *pcm, int ch);
1064 void sbt8_dual_L3(float *sample, signed short *pcm, int ch);
1065
1066 void sbtB_mono_L3(float *sample, unsigned char *pcm, int ch);
1067 void sbtB_dual_L3(float *sample, unsigned char *pcm, int ch);
1068 void sbtB16_mono_L3(float *sample, unsigned char *pcm, int ch);
1069 void sbtB16_dual_L3(float *sample, unsigned char *pcm, int ch);
1070 void sbtB8_mono_L3(float *sample, unsigned char *pcm, int ch);
1071 void sbtB8_dual_L3(float *sample, unsigned char *pcm, int ch);
1072
1073
1074
1075 static const SBT_FUNCTION sbt_table[2][3][2] =
1076 {
1077 {{ (SBT_FUNCTION) sbt_mono_L3,
1078 (SBT_FUNCTION) sbt_dual_L3 } ,
1079 { (SBT_FUNCTION) sbt16_mono_L3,
1080 (SBT_FUNCTION) sbt16_dual_L3 } ,
1081 { (SBT_FUNCTION) sbt8_mono_L3,
1082 (SBT_FUNCTION) sbt8_dual_L3 }} ,
1083 /*-- 8 bit output -*/
1084 {{ (SBT_FUNCTION) sbtB_mono_L3,
1085 (SBT_FUNCTION) sbtB_dual_L3 },
1086 { (SBT_FUNCTION) sbtB16_mono_L3,
1087 (SBT_FUNCTION) sbtB16_dual_L3 },
1088 { (SBT_FUNCTION) sbtB8_mono_L3,
1089 (SBT_FUNCTION) sbtB8_dual_L3 }}
1090 };
1091
1092
1093 void Xform_mono(void *pcm, int igr);
1094 void Xform_dual(void *pcm, int igr);
1095 void Xform_dual_mono(void *pcm, int igr);
1096 void Xform_dual_right(void *pcm, int igr);
1097
1098 static XFORM_FUNCTION xform_table[5] =
1099 {
1100 Xform_mono,
1101 Xform_dual,
1102 Xform_dual_mono,
1103 Xform_mono, /* left */
1104 Xform_dual_right,
1105 };
1106 int L3table_init();
1107 void msis_init();
1108 void sbt_init();
1109 typedef int iARRAY22[22];
1110 iARRAY22 *quant_init_band_addr();
1111 iARRAY22 *msis_init_band_addr();
1112
1113 /*---------------------------------------------------------*/
1114 /* mpeg_head defined in mhead.h frame bytes is without pMP3Stream->pad */
1115 ////@@@@INIT
L3audio_decode_init(MPEG_HEAD * h,int framebytes_arg,int reduction_code,int transform_code,int convert_code,int freq_limit)1116 int L3audio_decode_init(MPEG_HEAD * h, int framebytes_arg,
1117 int reduction_code, int transform_code, int convert_code,
1118 int freq_limit)
1119 {
1120 int i, j, k;
1121 // static int first_pass = 1;
1122 int samprate;
1123 int limit;
1124 int bit_code;
1125 int out_chans;
1126
1127 pMP3Stream->buf_ptr0 = 0;
1128 pMP3Stream->buf_ptr1 = 0;
1129
1130 /* check if code handles */
1131 if (h->option != 1)
1132 return 0; /* layer III only */
1133
1134 if (h->id)
1135 pMP3Stream->ncbl_mixed = 8; /* mpeg-1 */
1136 else
1137 pMP3Stream->ncbl_mixed = 6; /* mpeg-2 */
1138
1139 pMP3Stream->framebytes = framebytes_arg;
1140
1141 bit_code = 0;
1142 if (convert_code & 8)
1143 bit_code = 1;
1144 convert_code = convert_code & 3; /* higher bits used by dec8 freq cvt */
1145 if (reduction_code < 0)
1146 reduction_code = 0;
1147 if (reduction_code > 2)
1148 reduction_code = 2;
1149 if (freq_limit < 1000)
1150 freq_limit = 1000;
1151
1152
1153 samprate = sr_table[4 * h->id + h->sr_index];
1154 if ((h->sync & 1) == 0)
1155 samprate = samprate / 2; // mpeg 2.5
1156 /*----- compute pMP3Stream->nsb_limit --------*/
1157 pMP3Stream->nsb_limit = (freq_limit * 64L + samprate / 2) / samprate;
1158 /*- caller limit -*/
1159 limit = (32 >> reduction_code);
1160 if (limit > 8)
1161 limit--;
1162 if (pMP3Stream->nsb_limit > limit)
1163 pMP3Stream->nsb_limit = limit;
1164 limit = 18 * pMP3Stream->nsb_limit;
1165
1166 k = h->id;
1167 if ((h->sync & 1) == 0)
1168 k = 2; // mpeg 2.5
1169
1170 if (k == 1)
1171 {
1172 pMP3Stream->band_limit12 = 3 * sfBandIndexTable[k][h->sr_index].s[13];
1173 pMP3Stream->band_limit = pMP3Stream->band_limit21 = sfBandIndexTable[k][h->sr_index].l[22];
1174 }
1175 else
1176 {
1177 pMP3Stream->band_limit12 = 3 * sfBandIndexTable[k][h->sr_index].s[12];
1178 pMP3Stream->band_limit = pMP3Stream->band_limit21 = sfBandIndexTable[k][h->sr_index].l[21];
1179 }
1180 pMP3Stream->band_limit += 8; /* allow for antialias */
1181 if (pMP3Stream->band_limit > limit)
1182 pMP3Stream->band_limit = limit;
1183
1184 if (pMP3Stream->band_limit21 > pMP3Stream->band_limit)
1185 pMP3Stream->band_limit21 = pMP3Stream->band_limit;
1186 if (pMP3Stream->band_limit12 > pMP3Stream->band_limit)
1187 pMP3Stream->band_limit12 = pMP3Stream->band_limit;
1188
1189
1190 pMP3Stream->band_limit_nsb = (pMP3Stream->band_limit + 17) / 18; /* limit nsb's rounded up */
1191 /*----------------------------------------------*/
1192 pMP3Stream->gain_adjust = 0; /* adjust gain e.g. cvt to mono sum channel */
1193 if ((h->mode != 3) && (convert_code == 1))
1194 pMP3Stream->gain_adjust = -4;
1195
1196 pMP3Stream->outvalues = 1152 >> reduction_code;
1197 if (h->id == 0)
1198 pMP3Stream->outvalues /= 2;
1199
1200 out_chans = 2;
1201 if (h->mode == 3)
1202 out_chans = 1;
1203 if (convert_code)
1204 out_chans = 1;
1205
1206 pMP3Stream->sbt_L3 = sbt_table[bit_code][reduction_code][out_chans - 1];
1207 k = 1 + convert_code;
1208 if (h->mode == 3)
1209 k = 0;
1210 pMP3Stream->Xform = xform_table[k];
1211
1212
1213 pMP3Stream->outvalues *= out_chans;
1214
1215 if (bit_code)
1216 pMP3Stream->outbytes = pMP3Stream->outvalues;
1217 else
1218 pMP3Stream->outbytes = sizeof(short) * pMP3Stream->outvalues;
1219
1220 if (bit_code)
1221 pMP3Stream->zero_level_pcm = 128; /* 8 bit output */
1222 else
1223 pMP3Stream->zero_level_pcm = 0;
1224
1225
1226 decinfo.channels = out_chans;
1227 decinfo.outvalues = pMP3Stream->outvalues;
1228 decinfo.samprate = samprate >> reduction_code;
1229 if (bit_code)
1230 decinfo.bits = 8;
1231 else
1232 decinfo.bits = sizeof(short) * 8;
1233
1234 decinfo.framebytes = pMP3Stream->framebytes;
1235 decinfo.type = 0;
1236
1237 pMP3Stream->half_outbytes = pMP3Stream->outbytes / 2;
1238 /*------------------------------------------*/
1239
1240 /*- init band tables --*/
1241
1242
1243 k = h->id;
1244 if ((h->sync & 1) == 0)
1245 k = 2; // mpeg 2.5
1246
1247 for (i = 0; i < 22; i++)
1248 pMP3Stream->sfBandIndex[0][i] = sfBandIndexTable[k][h->sr_index].l[i + 1];
1249 for (i = 0; i < 13; i++)
1250 pMP3Stream->sfBandIndex[1][i] = 3 * sfBandIndexTable[k][h->sr_index].s[i + 1];
1251 for (i = 0; i < 22; i++)
1252 pMP3Stream->nBand[0][i] = sfBandIndexTable[k][h->sr_index].l[i + 1] - sfBandIndexTable[k][h->sr_index].l[i];
1253 for (i = 0; i < 13; i++)
1254 pMP3Stream->nBand[1][i] = sfBandIndexTable[k][h->sr_index].s[i + 1] - sfBandIndexTable[k][h->sr_index].s[i];
1255
1256
1257 /* init tables */
1258 L3table_init();
1259 /* init ms and is stereo modes */
1260 msis_init();
1261
1262 /*----- init sbt ---*/
1263 sbt_init();
1264
1265
1266
1267 /*--- clear buffers --*/
1268 for (i = 0; i < 576; i++)
1269 yout[i] = 0.0f;
1270 for (j = 0; j < 2; j++)
1271 {
1272 for (k = 0; k < 2; k++)
1273 {
1274 for (i = 0; i < 576; i++)
1275 {
1276 pMP3Stream->sample[j][k][i].x = 0.0f;
1277 pMP3Stream->sample[j][k][i].s = 0;
1278 }
1279 }
1280 }
1281
1282 if (h->id == 1)
1283 pMP3Stream->decode_function = L3audio_decode_MPEG1;
1284 else
1285 pMP3Stream->decode_function = L3audio_decode_MPEG2;
1286
1287 return 1;
1288 }
1289 /*---------------------------------------------------------*/
1290 /*==========================================================*/
1291