3 * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
4 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
6 * This file is part of a52dec, a free ATSC A-52 stream decoder.
7 * See http://liba52.sourceforge.net/ for updates.
9 * a52dec is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * a52dec is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include "a52_internal.h"
32 #include "bitstream.h"
36 /* some systems have memalign() but no declaration for it */
37 void * memalign (size_t align, size_t size);
39 /* assume malloc alignment is sufficient */
40 #define memalign(align,size) malloc (size)
52 static uint8_t halfrate[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3};
54 a52_state_t * a52_init (uint32_t mm_accel)
59 state = malloc (sizeof (a52_state_t));
63 state->samples = memalign (16, 256 * 12 * sizeof (sample_t));
64 if (state->samples == NULL) {
69 for (i = 0; i < 256 * 12; i++)
70 state->samples[i] = 0;
74 a52_imdct_init (mm_accel);
79 sample_t * a52_samples (a52_state_t * state)
81 return state->samples;
84 int a52_syncinfo (uint8_t * buf, int * flags,
85 int * sample_rate, int * bit_rate)
87 static int rate[] = { 32, 40, 48, 56, 64, 80, 96, 112,
88 128, 160, 192, 224, 256, 320, 384, 448,
90 static uint8_t lfeon[8] = {0x10, 0x10, 0x04, 0x04, 0x04, 0x01, 0x04, 0x01};
96 if ((buf[0] != 0x0b) || (buf[1] != 0x77)) /* syncword */
99 if (buf[5] >= 0x60) /* bsid >= 12 */
101 half = halfrate[buf[5] >> 3];
103 /* acmod, dsurmod and lfeon */
105 *flags = ((((buf[6] & 0xf8) == 0x50) ? A52_DOLBY : acmod) |
106 ((buf[6] & lfeon[acmod]) ? A52_LFE : 0));
108 frmsizecod = buf[4] & 63;
109 if (frmsizecod >= 38)
111 bitrate = rate [frmsizecod >> 1];
112 *bit_rate = (bitrate * 1000) >> half;
114 switch (buf[4] & 0xc0) {
116 *sample_rate = 48000 >> half;
119 *sample_rate = 44100 >> half;
120 return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
122 *sample_rate = 32000 >> half;
129 int a52_frame (a52_state_t * state, uint8_t * buf, int * flags,
130 sample_t * level, sample_t bias)
132 static sample_t clev[4] = {LEVEL_3DB, LEVEL_45DB, LEVEL_6DB, LEVEL_45DB};
133 static sample_t slev[4] = {LEVEL_3DB, LEVEL_6DB, 0, LEVEL_6DB};
137 state->fscod = buf[4] >> 6;
138 state->halfrate = halfrate[buf[5] >> 3];
139 state->acmod = acmod = buf[6] >> 5;
141 a52_bitstream_set_ptr (buf + 6);
142 bitstream_get (3); /* skip acmod we already parsed */
144 if ((acmod == 2) && (bitstream_get (2) == 2)) /* dsurmod */
147 if ((acmod & 1) && (acmod != 1))
148 state->clev = clev[bitstream_get (2)]; /* cmixlev */
151 state->slev = slev[bitstream_get (2)]; /* surmixlev */
153 state->lfeon = bitstream_get (1);
155 state->output = a52_downmix_init (acmod, *flags, level,
156 state->clev, state->slev);
157 if (state->output < 0)
159 if (state->lfeon && (*flags & A52_LFE))
160 state->output |= A52_LFE;
161 *flags = state->output;
162 /* the 2* compensates for differences in imdct */
163 state->dynrng = state->level = 2 * *level;
166 state->dynrngcall = NULL;
167 state->cplba.deltbae = DELTA_BIT_NONE;
168 state->ba[0].deltbae = state->ba[1].deltbae = state->ba[2].deltbae =
169 state->ba[3].deltbae = state->ba[4].deltbae = DELTA_BIT_NONE;
173 bitstream_get (5); /* dialnorm */
174 if (bitstream_get (1)) /* compre */
175 bitstream_get (8); /* compr */
176 if (bitstream_get (1)) /* langcode */
177 bitstream_get (8); /* langcod */
178 if (bitstream_get (1)) /* audprodie */
179 bitstream_get (7); /* mixlevel + roomtyp */
180 } while (chaninfo--);
182 bitstream_get (2); /* copyrightb + origbs */
184 if (bitstream_get (1)) /* timecod1e */
185 bitstream_get (14); /* timecod1 */
186 if (bitstream_get (1)) /* timecod2e */
187 bitstream_get (14); /* timecod2 */
189 if (bitstream_get (1)) { /* addbsie */
192 addbsil = bitstream_get (6);
194 bitstream_get (8); /* addbsi */
201 void a52_dynrng (a52_state_t * state,
202 sample_t (* call) (sample_t, void *), void * data)
207 state->dynrngcall = call;
208 state->dynrngdata = data;
212 static int parse_exponents (int expstr, int ngrps, uint8_t exponent,
218 exps = bitstream_get (7);
220 exponent += exp_1[exps];
226 *(dest++) = exponent;
227 *(dest++) = exponent;
229 *(dest++) = exponent;
231 *(dest++) = exponent;
234 exponent += exp_2[exps];
240 *(dest++) = exponent;
241 *(dest++) = exponent;
243 *(dest++) = exponent;
245 *(dest++) = exponent;
248 exponent += exp_3[exps];
254 *(dest++) = exponent;
255 *(dest++) = exponent;
257 *(dest++) = exponent;
259 *(dest++) = exponent;
266 static int parse_deltba (int8_t * deltba)
268 int deltnseg, deltlen, delta, j;
270 memset (deltba, 0, 50);
272 deltnseg = bitstream_get (3);
275 j += bitstream_get (5);
276 deltlen = bitstream_get (4);
277 delta = bitstream_get (3);
278 delta -= (delta >= 4) ? 3 : 4;
281 if (j + deltlen >= 50)
285 } while (deltnseg--);
290 static inline int zero_snr_offsets (int nfchans, a52_state_t * state)
294 if ((state->csnroffst) ||
295 (state->chincpl && state->cplba.bai >> 3) || /* cplinu, fsnroffst */
296 (state->lfeon && state->lfeba.bai >> 3)) /* fsnroffst */
298 for (i = 0; i < nfchans; i++)
299 if (state->ba[i].bai >> 3) /* fsnroffst */
304 static inline int16_t dither_gen (void)
306 static uint16_t lfsr_state = 1;
309 state = dither_lut[lfsr_state >> 8] ^ (lfsr_state << 8);
311 lfsr_state = (uint16_t) state;
316 static void coeff_get (sample_t * coeff, expbap_t * expbap,
317 quantizer_t * quantizer, sample_t level,
325 for (i = 0; i <= 24; i++)
326 factor[i] = scale_factor[i] * level;
331 for (i = 0; i < end; i++) {
338 coeff[i] = dither_gen() * LEVEL_3DB * factor[exp[i]];
346 if (quantizer->q1_ptr >= 0) {
347 coeff[i] = quantizer->q1[quantizer->q1_ptr--] * factor[exp[i]];
352 code = bitstream_get (5);
354 quantizer->q1_ptr = 1;
355 quantizer->q1[0] = q_1_2[code];
356 quantizer->q1[1] = q_1_1[code];
357 coeff[i] = q_1_0[code] * factor[exp[i]];
362 if (quantizer->q2_ptr >= 0) {
363 coeff[i] = quantizer->q2[quantizer->q2_ptr--] * factor[exp[i]];
368 code = bitstream_get (7);
370 quantizer->q2_ptr = 1;
371 quantizer->q2[0] = q_2_2[code];
372 quantizer->q2[1] = q_2_1[code];
373 coeff[i] = q_2_0[code] * factor[exp[i]];
378 coeff[i] = q_3[bitstream_get (3)] * factor[exp[i]];
382 if (quantizer->q4_ptr == 0) {
383 quantizer->q4_ptr = -1;
384 coeff[i] = quantizer->q4 * factor[exp[i]];
389 code = bitstream_get (7);
391 quantizer->q4_ptr = 0;
392 quantizer->q4 = q_4_1[code];
393 coeff[i] = q_4_0[code] * factor[exp[i]];
398 coeff[i] = q_5[bitstream_get (4)] * factor[exp[i]];
402 coeff[i] = ((bitstream_get_2 (bapi) << (16 - bapi)) *
408 static void coeff_get_coupling (a52_state_t * state, int nfchans,
409 sample_t * coeff, sample_t (* samples)[256],
410 quantizer_t * quantizer, uint8_t dithflag[5])
412 int cplbndstrc, bnd, i, i_end, ch;
417 exp = state->cpl_expbap.exp;
418 bap = state->cpl_expbap.bap;
420 cplbndstrc = state->cplbndstrc;
421 i = state->cplstrtmant;
422 while (i < state->cplendmant) {
424 while (cplbndstrc & 1) {
429 for (ch = 0; ch < nfchans; ch++)
430 cplco[ch] = state->cplco[ch][bnd] * coeff[ch];
440 cplcoeff = LEVEL_3DB * scale_factor[exp[i]];
441 for (ch = 0; ch < nfchans; ch++)
442 if ((state->chincpl >> ch) & 1) {
444 samples[ch][i] = (cplcoeff * cplco[ch] *
453 if (quantizer->q1_ptr >= 0) {
454 cplcoeff = quantizer->q1[quantizer->q1_ptr--];
459 code = bitstream_get (5);
461 quantizer->q1_ptr = 1;
462 quantizer->q1[0] = q_1_2[code];
463 quantizer->q1[1] = q_1_1[code];
464 cplcoeff = q_1_0[code];
469 if (quantizer->q2_ptr >= 0) {
470 cplcoeff = quantizer->q2[quantizer->q2_ptr--];
475 code = bitstream_get (7);
477 quantizer->q2_ptr = 1;
478 quantizer->q2[0] = q_2_2[code];
479 quantizer->q2[1] = q_2_1[code];
480 cplcoeff = q_2_0[code];
485 cplcoeff = q_3[bitstream_get (3)];
489 if (quantizer->q4_ptr == 0) {
490 quantizer->q4_ptr = -1;
491 cplcoeff = quantizer->q4;
496 code = bitstream_get (7);
498 quantizer->q4_ptr = 0;
499 quantizer->q4 = q_4_1[code];
500 cplcoeff = q_4_0[code];
505 cplcoeff = q_5[bitstream_get (4)];
509 cplcoeff = bitstream_get_2 (bapi) << (16 - bapi);
512 cplcoeff *= scale_factor[exp[i]];
513 for (ch = 0; ch < nfchans; ch++)
514 if ((state->chincpl >> ch) & 1)
515 samples[ch][i] = cplcoeff * cplco[ch];
521 int a52_block (a52_state_t * state)
523 static const uint8_t nfchans_tbl[] = {2, 1, 2, 3, 3, 4, 4, 5, 1, 1, 2};
524 static int rematrix_band[4] = {25, 37, 61, 253};
525 int i, nfchans, chaninfo;
526 uint8_t cplexpstr, chexpstr[5], lfeexpstr, do_bit_alloc, done_cpl;
527 uint8_t blksw[5], dithflag[5];
530 quantizer_t quantizer;
533 nfchans = nfchans_tbl[state->acmod];
535 for (i = 0; i < nfchans; i++)
536 blksw[i] = bitstream_get (1);
538 for (i = 0; i < nfchans; i++)
539 dithflag[i] = bitstream_get (1);
541 chaninfo = !state->acmod;
543 if (bitstream_get (1)) { /* dynrnge */
546 dynrng = bitstream_get_2 (8);
547 if (state->dynrnge) {
550 range = ((((dynrng & 0x1f) | 0x20) << 13) *
551 scale_factor[3 - (dynrng >> 5)]);
552 if (state->dynrngcall)
553 range = state->dynrngcall (range, state->dynrngdata);
554 state->dynrng = state->level * range;
557 } while (chaninfo--);
559 if (bitstream_get (1)) { /* cplstre */
561 if (bitstream_get (1)) { /* cplinu */
562 static uint8_t bndtab[16] = {31, 35, 37, 39, 41, 42, 43, 44,
563 45, 45, 46, 46, 47, 47, 48, 48};
568 for (i = 0; i < nfchans; i++)
569 state->chincpl |= bitstream_get (1) << i;
570 switch (state->acmod) {
574 state->phsflginu = bitstream_get (1);
576 cplbegf = bitstream_get (4);
577 cplendf = bitstream_get (4);
579 if (cplendf + 3 - cplbegf < 0)
581 state->ncplbnd = ncplsubnd = cplendf + 3 - cplbegf;
582 state->cplstrtbnd = bndtab[cplbegf];
583 state->cplstrtmant = cplbegf * 12 + 37;
584 state->cplendmant = cplendf * 12 + 73;
586 state->cplbndstrc = 0;
587 for (i = 0; i < ncplsubnd - 1; i++)
588 if (bitstream_get (1)) {
589 state->cplbndstrc |= 1 << i;
595 if (state->chincpl) { /* cplinu */
599 for (i = 0; i < nfchans; i++)
600 if ((state->chincpl) >> i & 1)
601 if (bitstream_get (1)) { /* cplcoe */
602 int mstrcplco, cplcoexp, cplcomant;
605 mstrcplco = 3 * bitstream_get (2);
606 for (j = 0; j < state->ncplbnd; j++) {
607 cplcoexp = bitstream_get (4);
608 cplcomant = bitstream_get (4);
612 cplcomant = (cplcomant | 0x10) << 13;
614 cplcomant * scale_factor[cplcoexp + mstrcplco];
617 if ((state->acmod == 2) && state->phsflginu && cplcoe)
618 for (j = 0; j < state->ncplbnd; j++)
619 if (bitstream_get (1)) /* phsflg */
620 state->cplco[1][j] = -state->cplco[1][j];
623 if ((state->acmod == 2) && (bitstream_get (1))) { /* rematstr */
627 end = (state->chincpl) ? state->cplstrtmant : 253; /* cplinu */
630 state->rematflg |= bitstream_get (1) << i;
631 while (rematrix_band[i++] < end);
634 cplexpstr = EXP_REUSE;
635 lfeexpstr = EXP_REUSE;
636 if (state->chincpl) /* cplinu */
637 cplexpstr = bitstream_get (2);
638 for (i = 0; i < nfchans; i++)
639 chexpstr[i] = bitstream_get (2);
641 lfeexpstr = bitstream_get (1);
643 for (i = 0; i < nfchans; i++)
644 if (chexpstr[i] != EXP_REUSE) {
645 if ((state->chincpl >> i) & 1)
646 state->endmant[i] = state->cplstrtmant;
650 chbwcod = bitstream_get (6);
653 state->endmant[i] = chbwcod * 3 + 73;
659 if (cplexpstr != EXP_REUSE) {
660 int cplabsexp, ncplgrps;
663 ncplgrps = ((state->cplendmant - state->cplstrtmant) /
664 (3 << (cplexpstr - 1)));
665 cplabsexp = bitstream_get (4) << 1;
666 if (parse_exponents (cplexpstr, ncplgrps, cplabsexp,
667 state->cpl_expbap.exp + state->cplstrtmant))
670 for (i = 0; i < nfchans; i++)
671 if (chexpstr[i] != EXP_REUSE) {
672 int grp_size, nchgrps;
674 do_bit_alloc |= 1 << i;
675 grp_size = 3 << (chexpstr[i] - 1);
676 nchgrps = (state->endmant[i] + grp_size - 4) / grp_size;
677 state->fbw_expbap[i].exp[0] = bitstream_get (4);
678 if (parse_exponents (chexpstr[i], nchgrps,
679 state->fbw_expbap[i].exp[0],
680 state->fbw_expbap[i].exp + 1))
682 bitstream_get (2); /* gainrng */
684 if (lfeexpstr != EXP_REUSE) {
686 state->lfe_expbap.exp[0] = bitstream_get (4);
687 if (parse_exponents (lfeexpstr, 2, state->lfe_expbap.exp[0],
688 state->lfe_expbap.exp + 1))
692 if (bitstream_get (1)) { /* baie */
694 state->bai = bitstream_get (11);
696 if (bitstream_get (1)) { /* snroffste */
698 state->csnroffst = bitstream_get (6);
699 if (state->chincpl) /* cplinu */
700 state->cplba.bai = bitstream_get (7);
701 for (i = 0; i < nfchans; i++)
702 state->ba[i].bai = bitstream_get (7);
704 state->lfeba.bai = bitstream_get (7);
706 if ((state->chincpl) && (bitstream_get (1))) { /* cplinu, cplleake */
708 state->cplfleak = 9 - bitstream_get (3);
709 state->cplsleak = 9 - bitstream_get (3);
712 if (bitstream_get (1)) { /* deltbaie */
714 if (state->chincpl) /* cplinu */
715 state->cplba.deltbae = bitstream_get (2);
716 for (i = 0; i < nfchans; i++)
717 state->ba[i].deltbae = bitstream_get (2);
718 if (state->chincpl && /* cplinu */
719 (state->cplba.deltbae == DELTA_BIT_NEW) &&
720 parse_deltba (state->cplba.deltba))
722 for (i = 0; i < nfchans; i++)
723 if ((state->ba[i].deltbae == DELTA_BIT_NEW) &&
724 parse_deltba (state->ba[i].deltba))
729 if (zero_snr_offsets (nfchans, state)) {
730 memset (state->cpl_expbap.bap, 0, sizeof (state->cpl_expbap.bap));
731 for (i = 0; i < nfchans; i++)
732 memset (state->fbw_expbap[i].bap, 0,
733 sizeof (state->fbw_expbap[i].bap));
734 memset (state->lfe_expbap.bap, 0, sizeof (state->lfe_expbap.bap));
736 if (state->chincpl && (do_bit_alloc & 64)) /* cplinu */
737 a52_bit_allocate (state, &state->cplba, state->cplstrtbnd,
738 state->cplstrtmant, state->cplendmant,
739 state->cplfleak << 8, state->cplsleak << 8,
741 for (i = 0; i < nfchans; i++)
742 if (do_bit_alloc & (1 << i))
743 a52_bit_allocate (state, state->ba + i, 0, 0,
744 state->endmant[i], 0, 0,
745 state->fbw_expbap +i);
746 if (state->lfeon && (do_bit_alloc & 32)) {
747 state->lfeba.deltbae = DELTA_BIT_NONE;
748 a52_bit_allocate (state, &state->lfeba, 0, 0, 7, 0, 0,
754 if (bitstream_get (1)) { /* skiple */
755 i = bitstream_get (9); /* skipl */
760 samples = state->samples;
761 if (state->output & A52_LFE)
762 samples += 256; /* shift for LFE channel */
764 chanbias = a52_downmix_coeff (coeff, state->acmod, state->output,
765 state->dynrng, state->clev, state->slev);
767 quantizer.q1_ptr = quantizer.q2_ptr = quantizer.q4_ptr = -1;
770 for (i = 0; i < nfchans; i++) {
773 coeff_get (samples + 256 * i, state->fbw_expbap +i, &quantizer,
774 coeff[i], dithflag[i], state->endmant[i]);
776 if ((state->chincpl >> i) & 1) {
779 coeff_get_coupling (state, nfchans, coeff,
780 (sample_t (*)[256])samples, &quantizer,
783 j = state->cplendmant;
785 j = state->endmant[i];
787 (samples + 256 * i)[j] = 0;
791 if (state->acmod == 2) {
792 int j, end, band, rematflg;
794 end = ((state->endmant[0] < state->endmant[1]) ?
795 state->endmant[0] : state->endmant[1]);
799 rematflg = state->rematflg;
801 if (! (rematflg & 1)) {
803 j = rematrix_band[i++];
807 band = rematrix_band[i++];
814 tmp1 = (samples+256)[j];
815 samples[j] = tmp0 + tmp1;
816 (samples+256)[j] = tmp0 - tmp1;
817 } while (++j < band);
822 if (state->output & A52_LFE) {
823 coeff_get (samples - 256, &state->lfe_expbap, &quantizer,
824 state->dynrng, 0, 7);
825 for (i = 7; i < 256; i++)
826 (samples-256)[i] = 0;
827 a52_imdct_512 (samples - 256, samples + 1536 - 256, state->bias);
829 /* just skip the LFE coefficients */
830 coeff_get (samples + 1280, &state->lfe_expbap, &quantizer,
836 if (nfchans_tbl[state->output & A52_CHANNEL_MASK] < nfchans)
837 for (i = 1; i < nfchans; i++)
838 if (blksw[i] != blksw[0])
842 if (state->downmixed) {
843 state->downmixed = 0;
844 a52_upmix (samples + 1536, state->acmod, state->output);
847 for (i = 0; i < nfchans; i++) {
851 if (!(chanbias & (1 << i)))
856 a52_imdct_256 (samples + 256 * i, samples + 1536 + 256 * i,
859 a52_imdct_512 (samples + 256 * i, samples + 1536 + 256 * i,
864 for (j = 0; j < 256; j++)
865 (samples + 256 * i)[j] = bias;
869 a52_downmix (samples, state->acmod, state->output, state->bias,
870 state->clev, state->slev);
872 nfchans = nfchans_tbl[state->output & A52_CHANNEL_MASK];
874 a52_downmix (samples, state->acmod, state->output, 0,
875 state->clev, state->slev);
877 if (!state->downmixed) {
878 state->downmixed = 1;
879 a52_downmix (samples + 1536, state->acmod, state->output, 0,
880 state->clev, state->slev);
884 for (i = 0; i < nfchans; i++)
885 a52_imdct_256 (samples + 256 * i, samples + 1536 + 256 * i,
888 for (i = 0; i < nfchans; i++)
889 a52_imdct_512 (samples + 256 * i, samples + 1536 + 256 * i,
896 void a52_free (a52_state_t * state)
898 free (state->samples);