1 /*____________________________________________________________________________
3 FreeAmp - The Free MP3 Player
5 MP3 Decoder originally Copyright (C) 1995-1997 Xing Technology
6 Corp. http://www.xingtech.com
8 Portions Copyright (C) 1998-1999 EMusic.com
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.
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.
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.
25 ____________________________________________________________________________*/
27 /**** cupL3.c ***************************************************
31 mod 8/18/97 bugfix crc problem
33 mod 10/9/97 add band_limit12 for short blocks
35 mod 10/22/97 zero buf_ptrs in init
39 mod 8/19/98 decode 22 sf bands
41 ******************************************************************/
43 /*---------------------------------------
44 TO DO: Test mixed blocks (mixed long/short)
45 No mixed blocks in mpeg-1 test stream being used for development
47 -----------------------------------------*/
57 #include "mhead.h" /* mpeg header structure */
62 /*====================================================================*/
63 static int mp_sr20_table[2][4] =
64 {{441, 480, 320, -999}, {882, 960, 640, -999}};
65 static int mp_br_tableL3[2][16] =
66 {{0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0}, /* mpeg 2 */
67 {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0}};
69 /*====================================================================*/
71 /*-- global band tables */
72 /*-- short portion is 3*x !! --*/
74 /*====================================================================*/
76 /*---------------------------------*/
77 /*---------------------------------*/
78 /*- sample union of int/float sample[ch][gr][576] */
79 /* Sample is the same as cup.sample */
81 void sbt_dual_L3(MPEG *m, float *sample, short *pcm, int n);
83 IN_OUT L3audio_decode_MPEG1(void *mv, unsigned char *bs, unsigned char *pcm);
84 IN_OUT L3audio_decode_MPEG2(void *mv, unsigned char *bs, unsigned char *pcm);
86 static DECODE_FUNCTION decode_function = L3audio_decode_MPEG1;
89 /*====================================================================*/
90 /* get bits from bitstream in endian independent way */
92 BITDAT bitdat; /* global for inline use by Huff */
94 /*------------- initialize bit getter -------------*/
95 static void bitget_init(unsigned char *buf)
97 bitdat.bs_ptr0 = bitdat.bs_ptr = buf;
101 /*------------- initialize bit getter -------------*/
102 static void bitget_init_end(unsigned char *buf_end)
104 bitdat.bs_ptr_end = buf_end;
106 /*------------- get n bits from bitstream -------------*/
107 int bitget_bits_used(void)
109 int n; /* compute bits used from last init call */
111 n = ((bitdat.bs_ptr - bitdat.bs_ptr0) << 3) - bitdat.bits;
114 /*------------- check for n bits in bitbuf -------------*/
115 void bitget_check(int n)
119 while (bitdat.bits <= 24)
121 bitdat.bitbuf = (bitdat.bitbuf << 8) | *bitdat.bs_ptr++;
126 /*------------- get n bits from bitstream -------------*/
127 unsigned int bitget(int n)
132 { /* refill bit buf if necessary */
133 while (bitdat.bits <= 24)
135 bitdat.bitbuf = (bitdat.bitbuf << 8) | *bitdat.bs_ptr++;
140 x = bitdat.bitbuf >> bitdat.bits;
141 bitdat.bitbuf -= x << bitdat.bits;
144 /*------------- get 1 bit from bitstream -------------*/
145 unsigned int bitget_1bit(void)
149 if (bitdat.bits <= 0)
150 { /* refill bit buf if necessary */
151 while (bitdat.bits <= 24)
153 bitdat.bitbuf = (bitdat.bitbuf << 8) | *bitdat.bs_ptr++;
158 x = bitdat.bitbuf >> bitdat.bits;
159 bitdat.bitbuf -= x << bitdat.bits;
162 /*====================================================================*/
163 static void Xform_mono(void *mv, void *pcm, int igr)
166 int igr_prev, n1, n2;
168 /*--- hybrid + sbt ---*/
169 n1 = n2 = m->cupl.nsamp[igr][0]; /* total number bands */
170 if (m->cupl.side_info.gr[igr][0].block_type == 2)
173 if (m->cupl.side_info.gr[igr][0].mixed_block_flag)
174 n1 = m->cupl.sfBandIndex[0][m->cupl.ncbl_mixed - 1];
176 if (n1 > m->cupl.band_limit)
177 n1 = m->cupl.band_limit;
178 if (n2 > m->cupl.band_limit)
179 n2 = m->cupl.band_limit;
182 m->cupl.nsamp[igr][0] = hybrid(m,m->cupl.sample[0][igr], m->cupl.sample[0][igr_prev],
183 m->cupl.yout, m->cupl.side_info.gr[igr][0].block_type, n1, n2, m->cupl.nsamp[igr_prev][0]);
184 FreqInvert(m->cupl.yout, m->cupl.nsamp[igr][0]);
185 m->cupl.sbt_L3(m,m->cupl.yout, pcm, 0);
189 /*--------------------------------------------------------------------*/
190 static void Xform_dual_right(void *mv, void *pcm, int igr)
193 int igr_prev, n1, n2;
195 /*--- hybrid + sbt ---*/
196 n1 = n2 = m->cupl.nsamp[igr][1]; /* total number bands */
197 if (m->cupl.side_info.gr[igr][1].block_type == 2)
200 if (m->cupl.side_info.gr[igr][1].mixed_block_flag)
201 n1 = m->cupl.sfBandIndex[0][m->cupl.ncbl_mixed - 1];
203 if (n1 > m->cupl.band_limit)
204 n1 = m->cupl.band_limit;
205 if (n2 > m->cupl.band_limit)
206 n2 = m->cupl.band_limit;
208 m->cupl.nsamp[igr][1] = hybrid(m,m->cupl.sample[1][igr], m->cupl.sample[1][igr_prev],
209 m->cupl.yout, m->cupl.side_info.gr[igr][1].block_type, n1, n2, m->cupl.nsamp[igr_prev][1]);
210 FreqInvert(m->cupl.yout, m->cupl.nsamp[igr][1]);
211 m->cupl.sbt_L3(m,m->cupl.yout, pcm, 0);
214 /*--------------------------------------------------------------------*/
215 static void Xform_dual(void *mv, void *pcm, int igr)
219 int igr_prev, n1, n2;
221 /*--- hybrid + sbt ---*/
223 for (ch = 0; ch < m->cupl.nchan; ch++)
225 n1 = n2 = m->cupl.nsamp[igr][ch]; /* total number bands */
226 if (m->cupl.side_info.gr[igr][ch].block_type == 2)
229 if (m->cupl.side_info.gr[igr][ch].mixed_block_flag)
230 n1 = m->cupl.sfBandIndex[0][m->cupl.ncbl_mixed - 1];
232 if (n1 > m->cupl.band_limit)
233 n1 = m->cupl.band_limit;
234 if (n2 > m->cupl.band_limit)
235 n2 = m->cupl.band_limit;
236 m->cupl.nsamp[igr][ch] = hybrid(m,m->cupl.sample[ch][igr], m->cupl.sample[ch][igr_prev],
237 m->cupl.yout, m->cupl.side_info.gr[igr][ch].block_type, n1, n2, m->cupl.nsamp[igr_prev][ch]);
238 FreqInvert(m->cupl.yout, m->cupl.nsamp[igr][ch]);
239 m->cupl.sbt_L3(m,m->cupl.yout, pcm, ch);
243 /*--------------------------------------------------------------------*/
244 static void Xform_dual_mono(void *mv, void *pcm, int igr)
247 int igr_prev, n1, n2, n3;
249 /*--- hybrid + sbt ---*/
251 if ((m->cupl.side_info.gr[igr][0].block_type == m->cupl.side_info.gr[igr][1].block_type)
252 && (m->cupl.side_info.gr[igr][0].mixed_block_flag == 0)
253 && (m->cupl.side_info.gr[igr][1].mixed_block_flag == 0))
256 n2 = m->cupl.nsamp[igr][0]; /* total number bands max of L R */
257 if (n2 < m->cupl.nsamp[igr][1])
258 n2 = m->cupl.nsamp[igr][1];
259 if (n2 > m->cupl.band_limit)
260 n2 = m->cupl.band_limit;
261 n1 = n2; /* n1 = number long bands */
262 if (m->cupl.side_info.gr[igr][0].block_type == 2)
264 sum_f_bands(m->cupl.sample[0][igr], m->cupl.sample[1][igr], n2);
265 n3 = m->cupl.nsamp[igr][0] = hybrid(m,m->cupl.sample[0][igr], m->cupl.sample[0][igr_prev],
266 m->cupl.yout, m->cupl.side_info.gr[igr][0].block_type, n1, n2, m->cupl.nsamp[igr_prev][0]);
269 { /* transform and then sum (not tested - never happens in test) */
271 n1 = n2 = m->cupl.nsamp[igr][0]; /* total number bands */
272 if (m->cupl.side_info.gr[igr][0].block_type == 2)
274 n1 = 0; /* long bands */
275 if (m->cupl.side_info.gr[igr][0].mixed_block_flag)
276 n1 = m->cupl.sfBandIndex[0][m->cupl.ncbl_mixed - 1];
278 n3 = m->cupl.nsamp[igr][0] = hybrid(m,m->cupl.sample[0][igr], m->cupl.sample[0][igr_prev],
279 m->cupl.yout, m->cupl.side_info.gr[igr][0].block_type, n1, n2, m->cupl.nsamp[igr_prev][0]);
281 n1 = n2 = m->cupl.nsamp[igr][1]; /* total number bands */
282 if (m->cupl.side_info.gr[igr][1].block_type == 2)
284 n1 = 0; /* long bands */
285 if (m->cupl.side_info.gr[igr][1].mixed_block_flag)
286 n1 = m->cupl.sfBandIndex[0][m->cupl.ncbl_mixed - 1];
288 m->cupl.nsamp[igr][1] = hybrid_sum(m, m->cupl.sample[1][igr], m->cupl.sample[0][igr],
289 m->cupl.yout, m->cupl.side_info.gr[igr][1].block_type, n1, n2);
290 if (n3 < m->cupl.nsamp[igr][1])
291 n1 = m->cupl.nsamp[igr][1];
295 FreqInvert(m->cupl.yout, n3);
296 m->cupl.sbt_L3(m,m->cupl.yout, pcm, 0);
299 /*--------------------------------------------------------------------*/
300 /*====================================================================*/
301 static int unpack_side_MPEG1(MPEG *m)
308 /* decode partial header plus initial side info */
309 /* at entry bit getter points at id, sync skipped by caller */
311 m->cupl.id = bitget(1); /* id */
312 bitget(2); /* skip layer */
313 prot = bitget(1); /* bitget prot bit */
314 br_index = bitget(4);
315 m->cupl.sr_index = bitget(2);
316 m->cupl.pad = bitget(1);
317 bitget(1); /* skip to mode */
318 m->cupl.side_info.mode = bitget(2); /* mode */
319 m->cupl.side_info.mode_ext = bitget(2); /* mode ext */
321 if (m->cupl.side_info.mode != 1)
322 m->cupl.side_info.mode_ext = 0;
324 /* adjust global gain in ms mode to avoid having to mult by 1/sqrt(2) */
325 m->cupl.ms_mode = m->cupl.side_info.mode_ext >> 1;
326 m->cupl.is_mode = m->cupl.side_info.mode_ext & 1;
329 m->cupl.crcbytes = 0;
331 bitget(4); /* skip to data */
334 bitget(20); /* skip crc */
335 m->cupl.crcbytes = 2;
338 if (br_index > 0) /* framebytes fixed for free format */
341 2880 * mp_br_tableL3[m->cupl.id][br_index] / mp_sr20_table[m->cupl.id][m->cupl.sr_index];
344 m->cupl.side_info.main_data_begin = bitget(9);
345 if (m->cupl.side_info.mode == 3)
347 m->cupl.side_info.private_bits = bitget(5);
349 m->cupl.stereo_flag = 0;
350 side_bytes = (4 + 17);
351 /*-- with header --*/
355 m->cupl.side_info.private_bits = bitget(3);
357 m->cupl.stereo_flag = 1;
358 side_bytes = (4 + 32);
359 /*-- with header --*/
361 for (ch = 0; ch < m->cupl.nchan; ch++)
362 m->cupl.side_info.scfsi[ch] = bitget(4);
363 /* this always 0 (both igr) for short blocks */
365 for (igr = 0; igr < 2; igr++)
367 for (ch = 0; ch < m->cupl.nchan; ch++)
369 m->cupl.side_info.gr[igr][ch].part2_3_length = bitget(12);
370 m->cupl.side_info.gr[igr][ch].big_values = bitget(9);
371 m->cupl.side_info.gr[igr][ch].global_gain = bitget(8) + m->cupl.gain_adjust;
373 m->cupl.side_info.gr[igr][ch].global_gain -= 2;
374 m->cupl.side_info.gr[igr][ch].scalefac_compress = bitget(4);
375 m->cupl.side_info.gr[igr][ch].window_switching_flag = bitget(1);
376 if (m->cupl.side_info.gr[igr][ch].window_switching_flag)
378 m->cupl.side_info.gr[igr][ch].block_type = bitget(2);
379 m->cupl.side_info.gr[igr][ch].mixed_block_flag = bitget(1);
380 m->cupl.side_info.gr[igr][ch].table_select[0] = bitget(5);
381 m->cupl.side_info.gr[igr][ch].table_select[1] = bitget(5);
382 m->cupl.side_info.gr[igr][ch].subblock_gain[0] = bitget(3);
383 m->cupl.side_info.gr[igr][ch].subblock_gain[1] = bitget(3);
384 m->cupl.side_info.gr[igr][ch].subblock_gain[2] = bitget(3);
385 /* region count set in terms of long block cb's/bands */
386 /* r1 set so r0+r1+1 = 21 (lookup produces 576 bands ) */
387 /* if(window_switching_flag) always 36 samples in region0 */
388 m->cupl.side_info.gr[igr][ch].region0_count = (8 - 1); /* 36 samples */
389 m->cupl.side_info.gr[igr][ch].region1_count = 20 - (8 - 1);
393 m->cupl.side_info.gr[igr][ch].mixed_block_flag = 0;
394 m->cupl.side_info.gr[igr][ch].block_type = 0;
395 m->cupl.side_info.gr[igr][ch].table_select[0] = bitget(5);
396 m->cupl.side_info.gr[igr][ch].table_select[1] = bitget(5);
397 m->cupl.side_info.gr[igr][ch].table_select[2] = bitget(5);
398 m->cupl.side_info.gr[igr][ch].region0_count = bitget(4);
399 m->cupl.side_info.gr[igr][ch].region1_count = bitget(3);
401 m->cupl.side_info.gr[igr][ch].preflag = bitget(1);
402 m->cupl.side_info.gr[igr][ch].scalefac_scale = bitget(1);
403 m->cupl.side_info.gr[igr][ch].count1table_select = bitget(1);
409 /* return bytes in header + side info */
412 /*====================================================================*/
413 static int unpack_side_MPEG2(MPEG *m, int igr)
420 /* decode partial header plus initial side info */
421 /* at entry bit getter points at id, sync skipped by caller */
423 m->cupl.id = bitget(1); /* id */
424 bitget(2); /* skip layer */
425 prot = bitget(1); /* bitget prot bit */
426 br_index = bitget(4);
427 m->cupl.sr_index = bitget(2);
428 m->cupl.pad = bitget(1);
429 bitget(1); /* skip to mode */
430 m->cupl.side_info.mode = bitget(2); /* mode */
431 m->cupl.side_info.mode_ext = bitget(2); /* mode ext */
433 if (m->cupl.side_info.mode != 1)
434 m->cupl.side_info.mode_ext = 0;
436 /* adjust global gain in ms mode to avoid having to mult by 1/sqrt(2) */
437 m->cupl.ms_mode = m->cupl.side_info.mode_ext >> 1;
438 m->cupl.is_mode = m->cupl.side_info.mode_ext & 1;
440 m->cupl.crcbytes = 0;
442 bitget(4); /* skip to data */
445 bitget(20); /* skip crc */
446 m->cupl.crcbytes = 2;
450 { /* framebytes fixed for free format */
451 if (m->cupl.mpeg25_flag == 0)
454 1440 * mp_br_tableL3[m->cupl.id][br_index] / mp_sr20_table[m->cupl.id][m->cupl.sr_index];
459 2880 * mp_br_tableL3[m->cupl.id][br_index] / mp_sr20_table[m->cupl.id][m->cupl.sr_index];
460 //if( sr_index == 2 ) return 0; // fail mpeg25 8khz
463 m->cupl.side_info.main_data_begin = bitget(8);
464 if (m->cupl.side_info.mode == 3)
466 m->cupl.side_info.private_bits = bitget(1);
468 m->cupl.stereo_flag = 0;
469 side_bytes = (4 + 9);
470 /*-- with header --*/
474 m->cupl.side_info.private_bits = bitget(2);
476 m->cupl.stereo_flag = 1;
477 side_bytes = (4 + 17);
478 /*-- with header --*/
480 m->cupl.side_info.scfsi[1] = m->cupl.side_info.scfsi[0] = 0;
483 for (ch = 0; ch < m->cupl.nchan; ch++)
485 m->cupl.side_info.gr[igr][ch].part2_3_length = bitget(12);
486 m->cupl.side_info.gr[igr][ch].big_values = bitget(9);
487 m->cupl.side_info.gr[igr][ch].global_gain = bitget(8) + m->cupl.gain_adjust;
489 m->cupl.side_info.gr[igr][ch].global_gain -= 2;
490 m->cupl.side_info.gr[igr][ch].scalefac_compress = bitget(9);
491 m->cupl.side_info.gr[igr][ch].window_switching_flag = bitget(1);
492 if (m->cupl.side_info.gr[igr][ch].window_switching_flag)
494 m->cupl.side_info.gr[igr][ch].block_type = bitget(2);
495 m->cupl.side_info.gr[igr][ch].mixed_block_flag = bitget(1);
496 m->cupl.side_info.gr[igr][ch].table_select[0] = bitget(5);
497 m->cupl.side_info.gr[igr][ch].table_select[1] = bitget(5);
498 m->cupl.side_info.gr[igr][ch].subblock_gain[0] = bitget(3);
499 m->cupl.side_info.gr[igr][ch].subblock_gain[1] = bitget(3);
500 m->cupl.side_info.gr[igr][ch].subblock_gain[2] = bitget(3);
501 /* region count set in terms of long block cb's/bands */
502 /* r1 set so r0+r1+1 = 21 (lookup produces 576 bands ) */
503 /* bt=1 or 3 54 samples */
504 /* bt=2 mixed=0 36 samples */
505 /* bt=2 mixed=1 54 (8 long sf) samples? or maybe 36 */
506 /* region0 discussion says 54 but this would mix long */
507 /* and short in region0 if scale factors switch */
508 /* at band 36 (6 long scale factors) */
509 if ((m->cupl.side_info.gr[igr][ch].block_type == 2))
511 m->cupl.side_info.gr[igr][ch].region0_count = (6 - 1); /* 36 samples */
512 m->cupl.side_info.gr[igr][ch].region1_count = 20 - (6 - 1);
515 { /* long block type 1 or 3 */
516 m->cupl.side_info.gr[igr][ch].region0_count = (8 - 1); /* 54 samples */
517 m->cupl.side_info.gr[igr][ch].region1_count = 20 - (8 - 1);
522 m->cupl.side_info.gr[igr][ch].mixed_block_flag = 0;
523 m->cupl.side_info.gr[igr][ch].block_type = 0;
524 m->cupl.side_info.gr[igr][ch].table_select[0] = bitget(5);
525 m->cupl.side_info.gr[igr][ch].table_select[1] = bitget(5);
526 m->cupl.side_info.gr[igr][ch].table_select[2] = bitget(5);
527 m->cupl.side_info.gr[igr][ch].region0_count = bitget(4);
528 m->cupl.side_info.gr[igr][ch].region1_count = bitget(3);
530 m->cupl.side_info.gr[igr][ch].preflag = 0;
531 m->cupl.side_info.gr[igr][ch].scalefac_scale = bitget(1);
532 m->cupl.side_info.gr[igr][ch].count1table_select = bitget(1);
535 /* return bytes in header + side info */
538 /*-----------------------------------------------------------------*/
539 static void unpack_main(MPEG *m, unsigned char *pcm, int igr)
543 int n1, n2, n3, n4, nn2, nn3;
549 for (ch = 0; ch < m->cupl.nchan; ch++)
551 bitget_init(m->cupl.buf + (m->cupl.main_pos_bit >> 3));
552 bit0 = (m->cupl.main_pos_bit & 7);
555 m->cupl.main_pos_bit += m->cupl.side_info.gr[igr][ch].part2_3_length;
556 bitget_init_end(m->cupl.buf + ((m->cupl.main_pos_bit + 39) >> 3));
557 /*-- scale factors --*/
559 unpack_sf_sub_MPEG1(&m->cupl.sf[igr][ch],
560 &m->cupl.side_info.gr[igr][ch], m->cupl.side_info.scfsi[ch], igr);
562 unpack_sf_sub_MPEG2(&m->cupl.sf[igr][ch],
563 &m->cupl.side_info.gr[igr][ch], m->cupl.is_mode & ch, &m->cupl.is_sf_info);
564 /*--- huff data ---*/
565 n1 = m->cupl.sfBandIndex[0][m->cupl.side_info.gr[igr][ch].region0_count];
566 n2 = m->cupl.sfBandIndex[0][m->cupl.side_info.gr[igr][ch].region0_count
567 + m->cupl.side_info.gr[igr][ch].region1_count + 1];
568 n3 = m->cupl.side_info.gr[igr][ch].big_values;
572 if (n3 > m->cupl.band_limit)
573 n3 = m->cupl.band_limit;
580 unpack_huff(m->cupl.sample[ch][igr], n1, m->cupl.side_info.gr[igr][ch].table_select[0]);
581 unpack_huff(m->cupl.sample[ch][igr] + n1, nn2, m->cupl.side_info.gr[igr][ch].table_select[1]);
582 unpack_huff(m->cupl.sample[ch][igr] + n2, nn3, m->cupl.side_info.gr[igr][ch].table_select[2]);
583 qbits = m->cupl.side_info.gr[igr][ch].part2_3_length - (bitget_bits_used() - bit0);
584 nn4 = unpack_huff_quad(m->cupl.sample[ch][igr] + n3, m->cupl.band_limit - n3, qbits,
585 m->cupl.side_info.gr[igr][ch].count1table_select);
587 m->cupl.nsamp[igr][ch] = n4;
588 //limit n4 or allow deqaunt to sf band 22
589 if (m->cupl.side_info.gr[igr][ch].block_type == 2)
590 n4 = min(n4, m->cupl.band_limit12);
592 n4 = min(n4, m->cupl.band_limit21);
594 memset(m->cupl.sample[ch][igr] + n4, 0, sizeof(SAMPLE) * (576 - n4));
595 if (bitdat.bs_ptr > bitdat.bs_ptr_end)
596 { // bad data overrun
598 memset(m->cupl.sample[ch][igr], 0, sizeof(SAMPLE) * (576));
605 for (ch = 0; ch < m->cupl.nchan; ch++)
607 dequant(m,m->cupl.sample[ch][igr],
608 &m->cupl.nsamp[igr][ch], /* nsamp updated for shorts */
609 &m->cupl.sf[igr][ch], &m->cupl.side_info.gr[igr][ch],
610 &m->cupl.cb_info[igr][ch], m->cupl.ncbl_mixed);
613 /*--- ms stereo processing ---*/
616 if (m->cupl.is_mode == 0)
618 m0 = m->cupl.nsamp[igr][0]; /* process to longer of left/right */
619 if (m0 < m->cupl.nsamp[igr][1])
620 m0 = m->cupl.nsamp[igr][1];
623 { /* process to last cb in right */
624 m0 = m->cupl.sfBandIndex[m->cupl.cb_info[igr][1].cbtype][m->cupl.cb_info[igr][1].cbmax];
626 ms_process(m->cupl.sample[0][igr], m0);
629 /*--- is stereo processing ---*/
633 is_process_MPEG1(m, m->cupl.sample[0][igr], &m->cupl.sf[igr][1],
634 m->cupl.cb_info[igr], m->cupl.nsamp[igr][0], m->cupl.ms_mode);
636 is_process_MPEG2(m,m->cupl.sample[0][igr], &m->cupl.sf[igr][1],
637 m->cupl.cb_info[igr], &m->cupl.is_sf_info,
638 m->cupl.nsamp[igr][0], m->cupl.ms_mode);
641 /*-- adjust ms and is modes to max of left/right */
642 if (m->cupl.side_info.mode_ext)
644 if (m->cupl.nsamp[igr][0] < m->cupl.nsamp[igr][1])
645 m->cupl.nsamp[igr][0] = m->cupl.nsamp[igr][1];
647 m->cupl.nsamp[igr][1] = m->cupl.nsamp[igr][0];
650 /*--- antialias ---*/
651 for (ch = 0; ch < m->cupl.nchan; ch++)
653 if (m->cupl.cb_info[igr][ch].ncbl == 0)
654 continue; /* have no long blocks */
655 if (m->cupl.side_info.gr[igr][ch].mixed_block_flag)
656 n1 = 1; /* 1 -> 36 samples */
658 n1 = (m->cupl.nsamp[igr][ch] + 7) / 18;
661 antialias(m, m->cupl.sample[ch][igr], n1);
662 n1 = 18 * n1 + 8; /* update number of samples */
663 if (n1 > m->cupl.nsamp[igr][ch])
664 m->cupl.nsamp[igr][ch] = n1;
669 /*--- hybrid + sbt ---*/
670 m->cupl.Xform(m, pcm, igr);
675 /*--------------------------------------------------------------------*/
676 /*-----------------------------------------------------------------*/
677 IN_OUT L3audio_decode(void *mv, unsigned char *bs, unsigned char *pcm)
680 return m->cupl.decode_function((MPEG *)mv, bs, pcm);
683 /*--------------------------------------------------------------------*/
684 IN_OUT L3audio_decode_MPEG1(void *mv, unsigned char *bs, unsigned char *pcm)
694 bitget_init(bs); /* initialize bit getter */
696 in_out.in_bytes = 0; /* assume fail */
697 in_out.out_bytes = 0;
701 return in_out; /* sync fail */
704 /*-- unpack side info --*/
705 side_bytes = unpack_side_MPEG1(m);
706 m->cupl.padframebytes = m->cupl.framebytes + m->cupl.pad;
707 in_out.in_bytes = m->cupl.padframebytes;
709 /*-- load main data and update buf pointer --*/
710 /*-------------------------------------------
711 if start point < 0, must just cycle decoder
712 if jumping into middle of stream,
713 w---------------------------------------------*/
714 m->cupl.buf_ptr0 = m->cupl.buf_ptr1 - m->cupl.side_info.main_data_begin; /* decode start point */
715 if (m->cupl.buf_ptr1 > BUF_TRIGGER)
717 memmove(m->cupl.buf, m->cupl.buf + m->cupl.buf_ptr0, m->cupl.side_info.main_data_begin);
718 m->cupl.buf_ptr0 = 0;
719 m->cupl.buf_ptr1 = m->cupl.side_info.main_data_begin;
721 nbytes = m->cupl.padframebytes - side_bytes - m->cupl.crcbytes;
723 // RAK: This is no bueno. :-(
724 if (nbytes < 0 || nbytes > NBUF)
730 memmove(m->cupl.buf + m->cupl.buf_ptr1, bs + side_bytes + m->cupl.crcbytes, nbytes);
731 m->cupl.buf_ptr1 += nbytes;
732 /*-----------------------*/
734 if (m->cupl.buf_ptr0 >= 0)
736 // dump_frame(buf+buf_ptr0, 64);
737 m->cupl.main_pos_bit = m->cupl.buf_ptr0 << 3;
738 unpack_main(m,pcm, 0);
739 unpack_main(m,pcm + m->cupl.half_outbytes, 1);
740 in_out.out_bytes = m->cupl.outbytes;
744 memset(pcm, m->cupl.zero_level_pcm, m->cupl.outbytes); /* fill out skipped frames */
745 in_out.out_bytes = m->cupl.outbytes;
746 /* iframe--; in_out.out_bytes = 0; // test test */
751 /*--------------------------------------------------------------------*/
752 /*--------------------------------------------------------------------*/
753 IN_OUT L3audio_decode_MPEG2(void *mv, unsigned char *bs, unsigned char *pcm)
765 bitget_init(bs); /* initialize bit getter */
767 in_out.in_bytes = 0; /* assume fail */
768 in_out.out_bytes = 0;
771 // if( sync != 0xFFF ) return in_out; /* sync fail */
773 m->cupl.mpeg25_flag = 0;
776 m->cupl.mpeg25_flag = 1; /* mpeg 2.5 sync */
778 return in_out; /* sync fail */
783 /*-- unpack side info --*/
784 side_bytes = unpack_side_MPEG2(m,igr);
785 m->cupl.padframebytes = m->cupl.framebytes + m->cupl.pad;
786 in_out.in_bytes = m->cupl.padframebytes;
788 m->cupl.buf_ptr0 = m->cupl.buf_ptr1 - m->cupl.side_info.main_data_begin; /* decode start point */
789 if (m->cupl.buf_ptr1 > BUF_TRIGGER)
791 memmove(m->cupl.buf, m->cupl.buf + m->cupl.buf_ptr0, m->cupl.side_info.main_data_begin);
792 m->cupl.buf_ptr0 = 0;
793 m->cupl.buf_ptr1 = m->cupl.side_info.main_data_begin;
795 nbytes = m->cupl.padframebytes - side_bytes - m->cupl.crcbytes;
796 // RAK: This is no bueno. :-(
797 if (nbytes < 0 || nbytes > NBUF)
802 memmove(m->cupl.buf + m->cupl.buf_ptr1, bs + side_bytes + m->cupl.crcbytes, nbytes);
803 m->cupl.buf_ptr1 += nbytes;
804 /*-----------------------*/
806 if (m->cupl.buf_ptr0 >= 0)
808 m->cupl.main_pos_bit = m->cupl.buf_ptr0 << 3;
809 unpack_main(m,pcm, igr);
810 in_out.out_bytes = m->cupl.outbytes;
814 memset(pcm, m->cupl.zero_level_pcm, m->cupl.outbytes); /* fill out skipped frames */
815 in_out.out_bytes = m->cupl.outbytes;
816 // iframe--; in_out.out_bytes = 0; return in_out;// test test */
824 /*--------------------------------------------------------------------*/
825 /*--------------------------------------------------------------------*/
826 /*--------------------------------------------------------------------*/
827 static int const sr_table[8] =
828 {22050, 24000, 16000, 1,
829 44100, 48000, 32000, 1};
836 sfBandIndexTable[3][3] =
842 0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464, 522, 576
846 0, 4, 8, 12, 18, 24, 32, 42, 56, 74, 100, 132, 174, 192
852 0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 114, 136, 162, 194, 232, 278, 332, 394, 464, 540, 576
856 0, 4, 8, 12, 18, 26, 36, 48, 62, 80, 104, 136, 180, 192
862 0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464, 522, 576
866 0, 4, 8, 12, 18, 26, 36, 48, 62, 80, 104, 134, 174, 192
876 0, 4, 8, 12, 16, 20, 24, 30, 36, 44, 52, 62, 74, 90, 110, 134, 162, 196, 238, 288, 342, 418, 576
880 0, 4, 8, 12, 16, 22, 30, 40, 52, 66, 84, 106, 136, 192
886 0, 4, 8, 12, 16, 20, 24, 30, 36, 42, 50, 60, 72, 88, 106, 128, 156, 190, 230, 276, 330, 384, 576
890 0, 4, 8, 12, 16, 22, 28, 38, 50, 64, 80, 100, 126, 192
896 0, 4, 8, 12, 16, 20, 24, 30, 36, 44, 54, 66, 82, 102, 126, 156, 194, 240, 296, 364, 448, 550, 576
900 0, 4, 8, 12, 16, 22, 30, 42, 58, 78, 104, 138, 180, 192
906 /* mpeg-2.5, 11 & 12 KHz seem ok, 8 ok */
910 0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464, 522, 576
914 0, 4, 8, 12, 18, 26, 36, 48, 62, 80, 104, 134, 174, 192
920 0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464, 522, 576
924 0, 4, 8, 12, 18, 26, 36, 48, 62, 80, 104, 134, 174, 192
928 // this 8khz table, and only 8khz, from mpeg123)
931 0, 12, 24, 36, 48, 60, 72, 88, 108, 132, 160, 192, 232, 280, 336, 400, 476, 566, 568, 570, 572, 574, 576
935 0, 8, 16, 24, 36, 52, 72, 96, 124, 160, 162, 164, 166, 192
944 void sbt_mono_L3(MPEG *m, float *sample, signed short *pcm, int ch);
945 void sbt_dual_L3(MPEG *m, float *sample, signed short *pcm, int ch);
946 void sbt16_mono_L3(MPEG *m, float *sample, signed short *pcm, int ch);
947 void sbt16_dual_L3(MPEG *m, float *sample, signed short *pcm, int ch);
948 void sbt8_mono_L3(MPEG *m, float *sample, signed short *pcm, int ch);
949 void sbt8_dual_L3(MPEG *m, float *sample, signed short *pcm, int ch);
951 void sbtB_mono_L3(MPEG *m, float *sample, unsigned char *pcm, int ch);
952 void sbtB_dual_L3(MPEG *m, float *sample, unsigned char *pcm, int ch);
953 void sbtB16_mono_L3(MPEG *m, float *sample, unsigned char *pcm, int ch);
954 void sbtB16_dual_L3(MPEG *m, float *sample, unsigned char *pcm, int ch);
955 void sbtB8_mono_L3(MPEG *m, float *sample, unsigned char *pcm, int ch);
956 void sbtB8_dual_L3(MPEG *m, float *sample, unsigned char *pcm, int ch);
960 static SBT_FUNCTION_F sbt_table[2][3][2] =
962 {{ (SBT_FUNCTION_F) sbt_mono_L3,
963 (SBT_FUNCTION_F) sbt_dual_L3 } ,
964 { (SBT_FUNCTION_F) sbt16_mono_L3,
965 (SBT_FUNCTION_F) sbt16_dual_L3 } ,
966 { (SBT_FUNCTION_F) sbt8_mono_L3,
967 (SBT_FUNCTION_F) sbt8_dual_L3 }} ,
968 /*-- 8 bit output -*/
969 {{ (SBT_FUNCTION_F) sbtB_mono_L3,
970 (SBT_FUNCTION_F) sbtB_dual_L3 },
971 { (SBT_FUNCTION_F) sbtB16_mono_L3,
972 (SBT_FUNCTION_F) sbtB16_dual_L3 },
973 { (SBT_FUNCTION_F) sbtB8_mono_L3,
974 (SBT_FUNCTION_F) sbtB8_dual_L3 }}
978 void Xform_mono(void *mv, void *pcm, int igr);
979 void Xform_dual(void *mv, void *pcm, int igr);
980 void Xform_dual_mono(void *mv, void *pcm, int igr);
981 void Xform_dual_right(void *mv, void *pcm, int igr);
983 static XFORM_FUNCTION xform_table[5] =
988 Xform_mono, /* left */
991 int L3table_init(MPEG *m);
992 void msis_init(MPEG *m);
993 void sbt_init(MPEG *m);
995 typedef int iARRAY22[22];
998 /*---------------------------------------------------------*/
999 /* mpeg_head defined in mhead.h frame bytes is without pad */
1000 int L3audio_decode_init(void *mv, MPEG_HEAD * h, int framebytes_arg,
1001 int reduction_code, int transform_code, int convert_code,
1006 // static int first_pass = 1;
1012 m->cupl.buf_ptr0 = 0;
1013 m->cupl.buf_ptr1 = 0;
1015 /* check if code handles */
1017 return 0; /* layer III only */
1020 m->cupl.ncbl_mixed = 8; /* mpeg-1 */
1022 m->cupl.ncbl_mixed = 6; /* mpeg-2 */
1024 m->cupl.framebytes = framebytes_arg;
1026 transform_code = transform_code; /* not used, asm compatability */
1028 if (convert_code & 8)
1030 convert_code = convert_code & 3; /* higher bits used by dec8 freq cvt */
1031 if (reduction_code < 0)
1033 if (reduction_code > 2)
1035 if (freq_limit < 1000)
1039 samprate = sr_table[4 * h->id + h->sr_index];
1040 if ((h->sync & 1) == 0)
1041 samprate = samprate / 2; // mpeg 2.5
1042 /*----- compute nsb_limit --------*/
1043 m->cupl.nsb_limit = (freq_limit * 64L + samprate / 2) / samprate;
1044 /*- caller limit -*/
1045 limit = (32 >> reduction_code);
1048 if (m->cupl.nsb_limit > limit)
1049 m->cupl.nsb_limit = limit;
1050 limit = 18 * m->cupl.nsb_limit;
1053 if ((h->sync & 1) == 0)
1058 m->cupl.band_limit12 = 3 * sfBandIndexTable[k][h->sr_index].s[13];
1059 m->cupl.band_limit = m->cupl.band_limit21 = sfBandIndexTable[k][h->sr_index].l[22];
1063 m->cupl.band_limit12 = 3 * sfBandIndexTable[k][h->sr_index].s[12];
1064 m->cupl.band_limit = m->cupl.band_limit21 = sfBandIndexTable[k][h->sr_index].l[21];
1066 m->cupl.band_limit += 8; /* allow for antialias */
1067 if (m->cupl.band_limit > limit)
1068 m->cupl.band_limit = limit;
1070 if (m->cupl.band_limit21 > m->cupl.band_limit)
1071 m->cupl.band_limit21 = m->cupl.band_limit;
1072 if (m->cupl.band_limit12 > m->cupl.band_limit)
1073 m->cupl.band_limit12 = m->cupl.band_limit;
1076 m->cupl.band_limit_nsb = (m->cupl.band_limit + 17) / 18; /* limit nsb's rounded up */
1077 /*----------------------------------------------*/
1078 m->cupl.gain_adjust = 0; /* adjust gain e.g. cvt to mono sum channel */
1079 if ((h->mode != 3) && (convert_code == 1))
1080 m->cupl.gain_adjust = -4;
1082 m->cupl.outvalues = 1152 >> reduction_code;
1084 m->cupl.outvalues /= 2;
1092 m->cupl.sbt_L3 = sbt_table[bit_code][reduction_code][out_chans - 1];
1093 k = 1 + convert_code;
1096 m->cupl.Xform = xform_table[k];
1099 m->cupl.outvalues *= out_chans;
1102 m->cupl.outbytes = m->cupl.outvalues;
1104 m->cupl.outbytes = sizeof(short) * m->cupl.outvalues;
1107 m->cupl.zero_level_pcm = 128; /* 8 bit output */
1109 m->cupl.zero_level_pcm = 0;
1112 m->cup.decinfo.channels = out_chans;
1113 m->cup.decinfo.outvalues = m->cupl.outvalues;
1114 m->cup.decinfo.samprate = samprate >> reduction_code;
1116 m->cup.decinfo.bits = 8;
1118 m->cup.decinfo.bits = sizeof(short) * 8;
1120 m->cup.decinfo.framebytes = m->cupl.framebytes;
1121 m->cup.decinfo.type = 0;
1123 m->cupl.half_outbytes = m->cupl.outbytes / 2;
1124 /*------------------------------------------*/
1126 /*- init band tables --*/
1130 if ((h->sync & 1) == 0)
1133 for (i = 0; i < 22; i++)
1134 m->cupl.sfBandIndex[0][i] = sfBandIndexTable[k][h->sr_index].l[i + 1];
1135 for (i = 0; i < 13; i++)
1136 m->cupl.sfBandIndex[1][i] = 3 * sfBandIndexTable[k][h->sr_index].s[i + 1];
1137 for (i = 0; i < 22; i++)
1138 m->cupl.nBand[0][i] =
1139 sfBandIndexTable[k][h->sr_index].l[i + 1]
1140 - sfBandIndexTable[k][h->sr_index].l[i];
1141 for (i = 0; i < 13; i++)
1142 m->cupl.nBand[1][i] =
1143 sfBandIndexTable[k][h->sr_index].s[i + 1]
1144 - sfBandIndexTable[k][h->sr_index].s[i];
1149 /* init ms and is stereo modes */
1152 /*----- init sbt ---*/
1157 /*--- clear buffers --*/
1158 for (i = 0; i < 576; i++)
1159 m->cupl.yout[i] = 0.0f;
1160 for (j = 0; j < 2; j++)
1162 for (k = 0; k < 2; k++)
1164 for (i = 0; i < 576; i++)
1166 m->cupl.sample[j][k][i].x = 0.0f;
1167 m->cupl.sample[j][k][i].s = 0;
1173 m->cupl.decode_function = L3audio_decode_MPEG1;
1175 m->cupl.decode_function = L3audio_decode_MPEG2;
1179 /*---------------------------------------------------------*/
1180 /*==========================================================*/
1181 void cup3_init(MPEG *m)
1183 m->cupl.sbt_L3 = sbt_dual_L3;
1184 m->cupl.Xform = Xform_dual;
1185 m->cupl.sbt_L3 = sbt_dual_L3;
1186 m->cupl.decode_function = L3audio_decode_MPEG1;