1 #include "../libzmpeg3.h"
3 int zaudio_decoder_layer_t::
4 get_scale_factors_1(int *scf, l3_info_t *l3_info, int ch, int gr)
6 static uint8_t slen[2][16] =
7 {{0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4},
8 {0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3}};
10 int num0 = slen[0][l3_info->scalefac_compress];
11 int num1 = slen[1][l3_info->scalefac_compress];
12 if( l3_info->block_type == 2 ) {
14 numbits = (num0 + num1) * 18;
16 if( l3_info->mixed_block_flag ) {
17 for( ; --i>9; ) *scf++ = stream->get_bits(num0);
18 /* num0*17 + num1*18 */
21 while( --i >= 0 ) *scf++ = stream->get_bits(num0);
22 for( i=18; --i>=0; ) *scf++ = stream->get_bits(num1);
23 /* short[13][0..2] = 0 */
24 *scf++ = 0; *scf++ = 0; *scf++ = 0;
27 int scfsi = l3_info->scfsi;
29 /* scfsi < 0 => granule == 0 */
30 for( i=11; --i>=0; ) *scf++ = stream->get_bits(num0);
31 for( i=10; --i>=0; ) *scf++ = stream->get_bits(num1);
32 numbits = (num0 + num1) * 10 + num0;
37 if( !(scfsi & 0x8) ) {
38 for( i=6; --i>=0; ) *scf++ = stream->get_bits(num0);
43 if( !(scfsi & 0x4) ) {
44 for( i=5; --i>=0; ) *scf++ = stream->get_bits(num0);
50 for( i=5; --i>=0; ) *scf++ = stream->get_bits(num1);
55 if( !(scfsi & 0x1) ) {
56 for( i=5; --i>=0; ) *scf++ = stream->get_bits(num1);
61 *scf++ = 0; /* no l[21] in original sources */
67 int zaudio_decoder_layer_t::
68 get_scale_factors_2(int *scf, l3_info_t *l3_info, int i_stereo)
71 static uint8_t stab[3][6][4] =
72 {{{ 6, 5, 5,5 }, { 6, 5, 7,3 }, { 11,10,0,0},
73 { 7, 7, 7,0 }, { 6, 6, 6,3 }, { 8, 8,5,0}},
74 {{ 9, 9, 9,9 }, { 9, 9,12,6 }, { 18,18,0,0},
75 {12,12,12,0 }, {12, 9, 9,6 }, { 15,12,9,0}},
76 {{ 6, 9, 9,9 }, { 6, 9,12,6 }, { 15,18,0,0},
77 { 6,15,12,0 }, { 6,12, 9,6 }, { 6,18,9,0}}};
79 /* i_stereo AND second channel -> do_layer3() checks this */
80 unsigned int slen = i_stereo ?
81 i_slen2[l3_info->scalefac_compress >> 1] :
82 n_slen2[l3_info->scalefac_compress];
84 l3_info->preflag = (slen >> 15) & 0x1;
86 if( l3_info->block_type == 2 ) {
88 if( l3_info->mixed_block_flag ) ++n ;
90 uint8_t *pnt = stab[n][(slen >> 12) & 0x7];
92 for( i=0; i<4; ++i ) {
96 for( j=(int)pnt[i]; --j>=0; ) *scf++ = stream->get_bits(num);
97 numbits += pnt[i] * num;
100 for( j=(int)pnt[i]; --j>=0; ) *scf++ = 0;
110 static int pretab1[22] = {0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,2,2,3,3,3,2,0};
111 static int pretab2[22] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
114 * Dequantize samples (includes huffman decoding)
116 * 24 is enough because tab13 has max. a 19 bit huffvector
119 #define BITSHIFT ((int)(sizeof(int32_t)-1) * 8)
120 #define REFRESH_MASK if( num < BITSHIFT ) { \
121 if( -part2remain >= num ) break; \
123 mask |= stream->get_bits(8) << (BITSHIFT-num); \
124 num += 8; part2remain -= 8; \
125 } while( num < BITSHIFT ); }
127 int zaudio_decoder_layer_t::
128 dequantize_sample(float xr[SBLIMIT][SSLIMIT], int *scf,
129 l3_info_t *l3_info, int sfreq, int part2bits)
133 int i, l[3], cb, mc, *m, *me;
134 int shift = 1 + l3_info->scalefac_scale;
135 float *xrpnt = (float*)xr;
136 int part2remain = l3_info->part2_3_length - part2bits;
137 int num = stream->get_bit_offset();
138 int32_t mask = stream->get_bits(num);
140 //zmsgs("1 %08x %d\n", mask, num);
141 mask = mask << (BITSHIFT + 8 - num);
144 int bv = l3_info->big_values;
145 int region1 = l3_info->region1start;
146 int region2 = l3_info->region2start;
147 int l3 = ((576 >> 1) - bv) >> 1;
148 /* we may lose the 'odd' bit here !!, check this later again */
149 if( bv <= region1 ) {
155 if( bv <= region2 ) {
156 l[1] = bv - l[0]; l[2] = 0;
159 l[1] = region2 - l[0];
164 if( l3_info->block_type == 2 ) {
165 /* decoding with short or mixed mode BandIndex table */
171 if( l3_info->mixed_block_flag ) {
172 max[0] = max[1] = max[2] = 2;
175 me = mapend[sfreq][0];
178 /* max[3] not floatly needed in this case */
179 max[0] = max[1] = max[2] = max[3] = -1;
181 me = mapend[sfreq][1];
184 for( mc=i=0; i<2; ++i ) {
186 huffman_t *h = ht + l3_info->table_select[i];
187 for( ; lp>0; --lp, --mc ) {
191 xrpnt = ((float*)xr) + (*m++);
195 v = l3_info->pow2gain[(*scf++) << shift];
199 v = l3_info->full_gain[lwin][(*scf++) << shift];
206 while( (y=*val++) < 0) {
207 if( mask < 0 ) val -= y;
214 if( x == 15 && h->linbits ) {
217 x += ((uint32_t)mask) >> (BITSHIFT + 8 - h->linbits);
218 num -= h->linbits + 1;
221 *xrpnt = mask < 0 ? -vv : vv;
226 if( -part2remain >= num ) break;
228 *xrpnt = mask < 0 ? -vv : vv;
236 if( y == 15 && h->linbits ) {
239 y += ((uint32_t) mask) >> (BITSHIFT + 8 - h->linbits);
240 num -= h->linbits + 1;
243 *xrpnt = mask < 0 ? -vv : vv;
248 if( -part2remain >= num ) break;
250 *xrpnt = mask < 0 ? -vv : vv;
260 for( ;l3 && -part2remain < num; l3--) {
261 huffman_t *h = htc + l3_info->count1table_select;
265 while( (a=*val++) < 0) {
266 if( mask < 0 ) val -= a;
271 for( i=0; i<4; ++i ) {
275 xrpnt = ((float*)xr) + (*m++);
279 v = l3_info->pow2gain[(*scf++) << shift];
283 v = l3_info->full_gain[lwin][(*scf++) << shift];
289 if( (a & (0x8 >> i)) ) {
291 if( -part2remain >= num ) break;
292 *xrpnt = mask < 0 ? -v : v;
302 if( lwin < 3 ) { /* short band? */
305 /* short band -> step=3 */
306 *xrpnt = 0.0; xrpnt += 3;
307 *xrpnt = 0.0; xrpnt += 3;
311 xrpnt = ((float*)xr) + *m++;
312 /* optimize: field will be set to zero at the end of the function */
313 if( *m++ == 0 ) break;
319 l3_info->maxband[0] = max[0] + 1;
320 l3_info->maxband[1] = max[1] + 1;
321 l3_info->maxband[2] = max[2] + 1;
322 l3_info->maxbandl = max[3] + 1;
323 int rmax = max[0] > max[1] ? max[0] : max[1];
324 rmax = (rmax > max[2] ? rmax : max[2]) + 1;
325 l3_info->maxb = rmax ?
326 shortLimit[sfreq][rmax] :
327 longLimit[sfreq][max[3] + 1];
330 /* decoding with 'long' BandIndex table (block_type != 2) */
331 int *pretab = l3_info->preflag ? pretab1 : pretab2;
337 /* long hash table values */
338 for( i=0; i<3; ++i ) {
340 huffman_t *h = ht + l3_info->table_select[i];
341 for( ; lp>0; --lp, --mc ) {
347 l3_info->pow2gain[(*scf++ + *pretab++) << shift] : 0.0;
351 while( (y=*val++) < 0 ) {
352 if( mask < 0 ) val -= y;
359 if( x == 15 && h->linbits ) {
362 x += ((uint32_t) mask) >> (BITSHIFT + 8 - h->linbits);
363 num -= h->linbits + 1;
366 *xrpnt++ = mask < 0 ? -vv : vv;
371 if( -part2remain >= num ) break;
373 *xrpnt++ = mask < 0 ? -vv : vv;
380 if( y == 15 && h->linbits ) {
383 y += ((uint32_t) mask) >> (BITSHIFT + 8 - h->linbits);
384 num -= h->linbits + 1;
387 *xrpnt++ = mask < 0 ? -vv : vv;
392 if( -part2remain >= num ) break;
394 *xrpnt++ = mask < 0 ? -vv : vv;
403 /* short (count1table) values */
404 for( ; l3 && -part2remain < num; l3-- ) {
405 huffman_t *h = htc + l3_info->count1table_select;
408 while( (a=*val++) < 0) {
409 if( mask < 0 ) val -= a;
414 for( i=0; i<4; ++i ) {
420 l3_info->pow2gain[((*scf++) + (*pretab++)) << shift] : 0.0;
424 if( (a & (0x8 >> i)) ) {
426 if( -part2remain >= num ) break;
427 *xrpnt++ = mask < 0 ? -v : v;
436 l3_info->maxbandl = max + 1;
437 l3_info->maxb = longLimit[sfreq][l3_info->maxbandl];
440 while( xrpnt < &xr[SBLIMIT][0] ) *xrpnt++ = 0.0;
442 if( -part2remain > num )
447 stream->start_reverse();
448 stream->get_bits_reverse(num);
449 stream->start_forward();
452 //zmsgs("3 %d %04x\n", stream->bit_number, stream->show_bits(16));
453 while( part2remain > 16 ) {
454 stream->get_bits(16); /* Dismiss stuffing Bits */
458 if( part2remain > 0 )
459 stream->get_bits(part2remain);
460 else if( part2remain < 0 ) {
461 zmsgs("can't rewind stream %d bits! data=%02x%02x%02x%02x\n", -part2remain,
462 (uint8_t)stream->input_ptr[-3], (uint8_t)stream->input_ptr[-2],
463 (uint8_t)stream->input_ptr[-1], (uint8_t)stream->input_ptr[0]);
464 return 1; /* -> error */
469 int zaudio_decoder_layer_t::
470 get_side_info(l3_sideinfo_t *si, int channels, int ms_stereo,
471 long sfreq, int single, int lsf)
474 int powdiff = (single == 3) ? 4 : 0;
475 static const int tabs[2][5] = { { 2,9,5,3,4 } , { 1,8,1,2,9 } };
476 const int *tab = tabs[lsf];
478 si->main_data_begin = stream->get_bits(tab[1]);
479 si->private_bits = stream->get_bits(channels == 1 ? tab[2] : tab[3]);
481 for( ch=0; ch<channels; ++ch ) {
482 si->ch[ch].gr[0].scfsi = -1;
483 si->ch[ch].gr[1].scfsi = stream->get_bits(4);
487 for( gr=0; gr<tab[0]; ++gr ) {
488 for( ch=0; ch<channels; ++ch ) {
489 l3_info_t *l3_info = &(si->ch[ch].gr[gr]);
490 l3_info->part2_3_length = stream->get_bits(12);
491 l3_info->big_values = stream->get_bits(9);
492 if( l3_info->big_values > 288 ) {
493 zerrs(" big_values too large! %d\n",l3_info->big_values);
494 l3_info->big_values = 288;
496 l3_info->pow2gain = gainpow2 + 256 - stream->get_bits(8) + powdiff;
497 if( ms_stereo ) l3_info->pow2gain += 2;
498 l3_info->scalefac_compress = stream->get_bits(tab[4]);
500 if(stream->get_bits(1)) {
501 /* window switch flag */
502 l3_info->block_type = stream->get_bits(2);
503 l3_info->mixed_block_flag = stream->get_bits(1);
504 l3_info->table_select[0] = stream->get_bits(5);
505 l3_info->table_select[1] = stream->get_bits(5);
506 /* table_select[2] not needed, because there is no region2, */
507 /* but to satisfy some verifications tools we set it either. */
508 l3_info->table_select[2] = 0;
510 l3_info->full_gain[i] = l3_info->pow2gain + (stream->get_bits(3) << 3);
512 if( l3_info->block_type == 0 ) {
513 zerr("Blocktype == 0 and window-switching == 1 not allowed.\n");
516 /* region_count/start parameters are implicit in this case. */
517 if( !lsf || l3_info->block_type == 2 )
518 l3_info->region1start = 36 >> 1;
520 /* check this again for 2.5 and sfreq=8 */
521 l3_info->region1start = sfreq == 8 ? 108 >> 1 : 54 >> 1;
523 l3_info->region2start = 576 >> 1;
528 l3_info->table_select[i] = stream->get_bits(5);
529 r0c = stream->get_bits(4);
530 r1c = stream->get_bits(3);
531 l3_info->region1start = bandInfo[sfreq].longIdx[r0c + 1] >> 1 ;
532 l3_info->region2start = bandInfo[sfreq].longIdx[r0c + 1 + r1c + 1] >> 1;
533 l3_info->block_type = 0;
534 l3_info->mixed_block_flag = 0;
536 if( !lsf ) l3_info->preflag = stream->get_bits(1);
537 l3_info->scalefac_scale = stream->get_bits(1);
538 l3_info->count1table_select = stream->get_bits(1);
544 int zaudio_decoder_layer_t::
545 hybrid( float fsIn[SBLIMIT][SSLIMIT], float tsOut[SSLIMIT][SBLIMIT],
546 int ch, l3_info_t *l3_info)
548 float *tspnt = (float *) tsOut;
549 float *rawout1,*rawout2;
551 int (*zdct)(float *inbuf, float *o1, float *o2, float *wintab, float *tsbuf);
554 rawout1 = mp3_block[b][ch];
556 rawout2 = mp3_block[b][ch];
560 if( l3_info->mixed_block_flag ) {
562 dct36(fsIn[0], rawout1, rawout2, win[0], tspnt);
563 dct36(fsIn[1], rawout1 + 18, rawout2 + 18, win1[0], tspnt + 1);
564 rawout1 += 36; rawout2 += 36;
568 bt = l3_info->block_type;
569 zdct = bt == 2 ? dct12 : dct36;
570 for( ; sb <(int)l3_info->maxb; sb+=2, tspnt+=2, rawout1+=36, rawout2+=36 ) {
571 zdct(fsIn[sb], rawout1, rawout2, win[bt], tspnt);
572 zdct(fsIn[sb + 1], rawout1 + 18, rawout2 + 18, win1[bt], tspnt + 1);
575 for( ; sb < (int)SBLIMIT; ++sb, ++tspnt ) {
576 for( i=0; i<(int)SSLIMIT; ++i ) {
577 tspnt[i * SBLIMIT] = *rawout1++;
584 int zaudio_decoder_layer_t::
585 antialias(float xr[SBLIMIT][SSLIMIT], l3_info_t *l3_info)
588 if( l3_info->block_type == 2 ) {
589 if( !l3_info->mixed_block_flag ) return 0;
593 sblim = l3_info->maxb-1;
595 /* 31 alias-reduction operations between each pair of sub-bands */
596 /* with 8 butterflies between each pair */
598 float *xr1 = (float*)xr[1];
599 for( int sb=sblim; sb > 0; --sb, xr1+=10 ) {
603 for( int ss=8; --ss>=0;) {
604 /* upper and lower butterfly inputs */
607 *xr2 = (bu * (*cs) ) - (bd * (*ca) );
608 *xr1++ = (bd * (*cs++) ) + (bu * (*ca++) );
615 * calculate float channel values for Joint-I-Stereo-mode
617 int zaudio_decoder_layer_t::
618 calc_i_stereo(float xr_buf[2][SBLIMIT][SSLIMIT], int *scalefac,
619 l3_info_t *l3_info, int sfreq, int ms_stereo, int lsf)
621 float (*xr)[SBLIMIT*SSLIMIT] = (float (*)[SBLIMIT*SSLIMIT])xr_buf;
622 struct bandInfoStruct *bi = &bandInfo[sfreq];
623 static const float *tabs[3][2][2] = { /* TODO: optimize as static */
624 { { tan1_1, tan2_1 } , { tan1_2, tan2_2 } },
625 { { pow1_1[0], pow2_1[0] } , { pow1_2[0], pow2_2[0] } } ,
626 { { pow1_1[1], pow2_1[1] } , { pow1_2[1], pow2_2[1] } }
629 int tab = lsf + (l3_info->scalefac_compress & lsf);
630 const float *tab1 = tabs[tab][ms_stereo][0];
631 const float *tab2 = tabs[tab][ms_stereo][1];
632 if( l3_info->block_type == 2 ) {
634 if( l3_info->mixed_block_flag ) do_l = 1;
635 for(lwin = 0; lwin < 3; lwin++) {
636 /* process each window */
637 /* get first band with zero values */
638 /* sfb is minimal 3 for mixed mode */
639 int is_p, sb, idx, sfb = l3_info->maxband[lwin];
640 if(sfb > 3) do_l = 0;
642 for( ; sfb < 12 ; ++sfb ) {
644 is_p = scalefac[sfb * 3 + lwin - l3_info->mixed_block_flag];
646 sb = bi->shortDiff[sfb];
647 idx = bi->shortIdx[sfb] + lwin;
648 float t1 = tab1[is_p];
649 float t2 = tab2[is_p];
650 for( ; sb > 0; --sb, idx+=3 ) {
651 float v = xr[0][idx];
658 /* in the original: copy 10 to 11, */
659 /* here: copy 11 to 12 maybe still wrong??? (copy 12 to 13?) */
661 is_p = scalefac[11 * 3 + lwin - l3_info->mixed_block_flag];
662 sb = bi->shortDiff[12];
663 idx = bi->shortIdx[12] + lwin;
668 for( ; sb > 0; --sb, idx+=3 ) {
669 float v = xr[0][idx];
674 } /* end for(lwin; .. ; . ) */
676 /* also check l-part, if ALL bands in the three windows are 'empty' */
677 /* and mode = mixed_mode */
679 int sfb = l3_info->maxbandl;
680 int idx = bi->longIdx[sfb];
682 for ( ; sfb < 8; ++sfb ) {
683 int sb = bi->longDiff[sfb];
685 int is_p = scalefac[sfb];
690 for( ; sb > 0; sb--, idx++) {
691 float v = xr[0][idx];
701 else { /* ((l3_info->block_type != 2)) */
702 int sfb = l3_info->maxbandl;
703 int is_p, idx = bi->longIdx[sfb];
704 for( ; sfb < 21; sfb++) {
705 int sb = bi->longDiff[sfb];
707 is_p = scalefac[sfb];
709 float t1 = tab1[is_p];
710 float t2 = tab2[is_p];
711 for( ; sb > 0; --sb, ++idx ) {
712 float v = xr[0][idx];
723 /* copy l-band 20 to l-band 21 */
724 float t1 = tab1[is_p];
725 float t2 = tab2[is_p];
726 for( int sb=bi->longDiff[21]; sb > 0; --sb, ++idx ) {
727 float v = xr[0][idx];
737 int zaudio_decoder_layer_t::
738 do_layer3(uint8_t *zframe, int zframe_size, float **zoutput, int render)
741 int i, n, gr, ch, ss;
742 /* max 39 for short[13][3] mode, mixed: 38, long: 22 */
743 int scalefacs[2][39];
744 l3_sideinfo_t sideinfo;
745 int ms_stereo=0, i_stereo=0;
746 int sfreq = sampling_frequency_code;
747 int stereo1, granules;
748 int output_offset = 0;
751 zframe += 4; /* Skip header */
754 /* flip/init buffer */
755 bsbuf = &bsspace[bsnum][512];
757 /* Copy frame into history buffer */
758 memcpy(bsbuf, zframe, zframe_size);
759 //zmsgs(" %d %02x%02x%02x%02x\n", first_frame,
760 // (uint8_t)bsbuf[0], (uint8_t)bsbuf[1], (uint8_t)bsbuf[2], (uint8_t)bsbuf[3]);
765 /* Set up bitstream to use buffer */
766 stream->use_ptr(bsbuf);
767 //zmsgs(" 7 %x\n", stream->show_bits(16));
768 /* CRC must be skipped here for proper alignment with the backstep */
769 if( error_protection ) stream->get_bits(16);
770 //zmsgs(" 8 %x\n", stream->show_bits(16));
771 if( channels == 1 ) { /* stream is mono */
779 if( mode == md_JOINT_STEREO ) {
780 ms_stereo = (mode_ext & 0x2) >> 1;
781 i_stereo = mode_ext & 0x1;
784 ms_stereo = i_stereo = 0;
785 granules = lsf ? 1 : 2;
787 if( get_side_info(&sideinfo, channels, ms_stereo, sfreq, single, lsf) ) {
789 return output_offset;
793 if( sideinfo.main_data_begin >= 512 ) return output_offset;
794 // if( sideinfo.main_data_begin ) { /* apparently, zero is legal */
795 prev_len = sideinfo.main_data_begin;
796 uint8_t *prev = prev_bsbuf + prev_framesize - prev_len;
797 //zmsgs(" 7 %ld %d %ld\n", ssize, sideinfo.main_data_begin, prev_framesize);
798 ptr = bsbuf + ssize - prev_len;
799 memcpy(ptr, prev, prev_len);
800 past_framesize += prev_framesize;
803 if( ptr && past_framesize >= prev_len ) {
804 stream->use_ptr(ptr);
805 for( gr=0; gr<granules; ++gr ) {
806 float hybridIn [2][SBLIMIT][SSLIMIT];
807 float hybridOut[2][SSLIMIT][SBLIMIT];
809 l3_info_t *l3_info = &(sideinfo.ch[0].gr[gr]);
812 get_scale_factors_2(scalefacs[0], l3_info, 0) :
813 get_scale_factors_1(scalefacs[0], l3_info, 0, gr);
814 //zmsgs("4 %04x\n", stream->show_bits(16));
815 if( dequantize_sample(hybridIn[0], scalefacs[0],
816 l3_info, sfreq, part2bits) ) {
818 return output_offset;
820 //zmsgs("5 %04x\n", stream->show_bits(16));
821 if( channels == 2 ) {
822 l3_info_t *l3_info = &(sideinfo.ch[1].gr[gr]);
823 int32_t part2bits = lsf ?
824 get_scale_factors_2(scalefacs[1], l3_info, i_stereo) :
825 get_scale_factors_1(scalefacs[1], l3_info, 1, gr);
827 if( dequantize_sample(hybridIn[1], scalefacs[1],
828 l3_info, sfreq, part2bits) ) {
830 return output_offset;
834 int maxb = sideinfo.ch[0].gr[gr].maxb;
835 if( (int)sideinfo.ch[1].gr[gr].maxb > maxb )
836 maxb = sideinfo.ch[1].gr[gr].maxb;
837 for( i=0; i<(int)SSLIMIT * maxb; ++i ) {
838 float tmp0 = ((float*)hybridIn[0])[i];
839 float tmp1 = ((float*)hybridIn[1])[i];
840 ((float*)hybridIn[0])[i] = tmp0 + tmp1;
841 ((float*)hybridIn[1])[i] = tmp0 - tmp1;
846 calc_i_stereo(hybridIn, scalefacs[1], l3_info, sfreq, ms_stereo, lsf);
847 if( ms_stereo || i_stereo || (single == 3)) {
848 if( l3_info->maxb > sideinfo.ch[0].gr[gr].maxb )
849 sideinfo.ch[0].gr[gr].maxb = l3_info->maxb;
851 l3_info->maxb = sideinfo.ch[0].gr[gr].maxb;
853 in0 = (float*)hybridIn[0];
854 in1 = (float*)hybridIn[1];
855 n = SSLIMIT*l3_info->maxb;
858 /* *0.5 done by pow-scale */
859 for( i=0; i<n; ++i, ++in0 ) *in0 += *in1++;
862 for( i=0; i<n; ++i ) *in0++ = *in1++;
868 for(ch = 0; ch < stereo1; ch++) {
869 l3_info_t *l3_info = &sideinfo.ch[ch].gr[gr];
871 antialias(hybridIn[ch], l3_info);
873 hybrid(hybridIn[ch], hybridOut[ch], ch, l3_info);
877 if( render && (zoutput[0] || (single < 0 && zoutput[1])) ) {
878 int offset0 = output_offset;
880 for( ss=0; ss<(int)SSLIMIT; ++ss )
881 synth_stereo(hybridOut[0][ss], 0, zoutput[0], &output_offset);
884 for( ss=0; ss<(int)SSLIMIT; ++ss ) {
885 int offset1 = offset0;
887 synth_stereo(hybridOut[0][ss], 0, zoutput[0], &offset0);
889 synth_stereo(hybridOut[1][ss], 1, zoutput[1], &offset1);
895 output_offset += 32 * SSLIMIT;
902 prev_framesize = zframe_size;
903 return output_offset;
906 void zaudio_decoder_layer_t::
913 bsbuf = &bsspace[1][512];
915 /* prev_framesize = 0; */
916 /* memset(bsspace, 0, sizeof(bsspace)); */
917 memset(mp3_block, 0, sizeof(mp3_block));
918 memset(mp3_blc, 0, sizeof(mp3_blc));
922 /* Return 1 if the head check doesn't find a header. */
923 int zaudio_decoder_layer_t::
924 layer_check(uint8_t *data)
927 ((uint32_t)data[0] << 24) | ((uint32_t)data[1] << 16) |
928 ((uint32_t)data[2] << 8) | ((uint32_t)data[3]);
929 if( (head & 0xffe00000) != 0xffe00000 ) return 1;
930 if( !((head >> 17) & 3) ) return 1;
931 if( ((head >> 12) & 0xf) == 0xf ) return 1;
932 if( !((head >> 12) & 0xf) ) return 1;
933 if( ((head >> 10) & 0x3) == 0x3 ) return 1;
934 if( ((head >> 19) & 1) == 1 &&
935 ((head >> 17) & 3) == 3 &&
936 ((head >> 16) & 1) == 1 ) return 1;
937 if( (head & 0xffff0000) == 0xfffe0000 ) return 1;
938 if( (head & 0xffff0000) == 0xffed0000 ) return 1; /* JPEG header */
942 int zaudio_decoder_layer_t::
943 id3_check(uint8_t *data)
945 return data[0]=='I' && data[1]=='D' && data[2]=='3' ? 1 : 0;
948 /* Decode layer header */
949 int zaudio_decoder_layer_t::
950 layer3_header(uint8_t *data)
953 int zlayer, zchannels, zmode;
954 int zsampling_frequency_code;
956 switch( id3_state ) { /* ID3 tag */
959 if( id3_check(data) ) {
960 id3_state = id3_HEADER;
961 id3_current_byte = 0;
967 if( ++id3_current_byte >= 6 ) {
969 ((uint32_t)data[0] << 21) | ((uint32_t)data[1] << 14) |
970 ((uint32_t)data[2] << 7) | ((uint32_t)data[3]);
971 id3_current_byte = 0;
972 id3_state = id3_SKIP;
973 //zmsgs("%d %02x%02x%02x%02x size=0x%x layer=%d\n", __LINE__,
974 // data[0], data[1], data[2], data[3], id3_size, layer);
979 //zmsgs("%d id3_current_byte=0x%x %02x%02x%02x%02x\n", __LINE__,
980 // id3_current_byte, data[0], data[1], data[2], data[3]);
981 if( ++id3_current_byte >= id3_size )
982 id3_state = id3_IDLE;
986 if( layer_check(data) ) return 0;
987 //zmsgs("%d id3_state=%d %02x%02x%02x%02x\n", __LINE__,
988 // id3_state, data[0], data[1], data[2], data[3]);
990 ((uint32_t)data[0] << 24) | ((uint32_t)data[1] << 16) |
991 ((uint32_t)data[2] << 8) | ((uint32_t)data[3]);
992 if( zheader & (1 << 20) ) {
993 zlsf = (zheader & (1 << 19)) ? 0x0 : 0x1;
1001 zlayer = 4 - ((zheader >> 17) & 3);
1002 //zmsgs("1 %d zheader=%08x zlayer=%d layer=%d\n", __LINE__,
1003 // zheader, zlayer, layer);
1004 if( layer != 0 && zlayer != layer ) return 0;
1005 zsampling_frequency_code = zmpeg35 ?
1006 6 + ((zheader >> 10) & 0x3) : ((zheader >> 10) & 0x3) + (zlsf * 3);
1008 if( samplerate != 0 &&
1009 zsampling_frequency_code != sampling_frequency_code ) return 0;
1010 zmode = ((zheader >> 6) & 0x3);
1011 zchannels = (zmode == md_MONO) ? 1 : 2;
1012 /* if( channels >= 0 && zchannels != channels ) return 0; */
1013 /* if( zchannels > channels ) channels = zchannels; */
1014 channels = zchannels;
1019 sampling_frequency_code = zsampling_frequency_code;
1020 samplerate = freqs[sampling_frequency_code];
1021 error_protection = ((zheader >> 16) & 0x1) ^ 0x1;
1022 bitrate_index = ((zheader >> 12) & 0xf);
1023 padding = ((zheader >> 9) & 0x1);
1024 extension = ((zheader >> 8) & 0x1);
1025 mode_ext = ((zheader >> 4) & 0x3);
1026 copyright = ((zheader >> 3) & 0x1);
1027 original = ((zheader >> 2) & 0x1);
1028 emphasis = zheader & 0x3;
1029 single = channels > 1 ? -1 : 3;
1030 if( !bitrate_index ) return 0;
1031 bitrate = 1000 * tabsel_123[lsf][layer-1][bitrate_index];
1034 framesize = (long)tabsel_123[lsf][0][bitrate_index] * 12000;
1035 framesize /= freqs[sampling_frequency_code];
1036 framesize = ((framesize + padding) << 2);
1039 framesize = (long)tabsel_123[lsf][1][bitrate_index] * 144000;
1040 framesize /= freqs[sampling_frequency_code];
1041 framesize += padding;
1045 ((channels == 1) ? 9 : 17) :
1046 ((channels == 1) ? 17 : 32);
1047 if( error_protection ) ssize += 2;
1048 framesize = (long)tabsel_123[lsf][2][bitrate_index] * 144000;
1049 framesize /= freqs[sampling_frequency_code] << lsf;
1050 framesize += padding;
1055 //zmsgs("%d bitrate=%d framesize=%ld samplerate=%d channels=%d layer=%d\n",
1056 // __LINE__, bitrate, framesize, samplerate, channels, layer);
1057 if( bitrate < 64000 && layer != 3 ) return 0;
1058 if( framesize > (int)MAXFRAMESIZE ) return 0;
1059 //zmsgs("10 %d\n", layer);
1063 zaudio_decoder_layer_t::
1064 audio_decoder_layer_t()
1068 stream = new bits_t(0, 0);
1069 init_decode_tables();
1073 zaudio_decoder_layer_t::
1074 ~audio_decoder_layer_t()