3
* Copyright (C) 2000-2003 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
24
#include "a52_internal.h"
25
#include "bitstream.h"
28
#if defined(HAVE_MEMALIGN) && !defined(__cplusplus)
29
/* some systems have memalign() but no declaration for it */
30
void * memalign (size_t align, size_t size);
32
/* assume malloc alignment is sufficient */
33
#define memalign(align,size) malloc (size)
45
static uint8_t halfrate[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3};
47
a52_state_t * a52_init (uint32_t mm_accel)
52
state = (a52_state_t *) malloc (sizeof (a52_state_t));
56
state->samples = (sample_t *) memalign (16, 256 * 12 * sizeof (sample_t));
57
if (state->samples == NULL) {
62
for (i = 0; i < 256 * 12; i++)
63
state->samples[i] = 0;
67
state->lfsr_state = 1;
69
a52_imdct_init (mm_accel);
74
sample_t * a52_samples (a52_state_t * state)
76
return state->samples;
79
int a52_syncinfo (uint8_t * buf, int * flags,
80
int * sample_rate, int * bit_rate)
82
static int rate[] = { 32, 40, 48, 56, 64, 80, 96, 112,
83
128, 160, 192, 224, 256, 320, 384, 448,
85
static uint8_t lfeon[8] = {0x10, 0x10, 0x04, 0x04, 0x04, 0x01, 0x04, 0x01};
91
if ((buf[0] != 0x0b) || (buf[1] != 0x77)) /* syncword */
94
if (buf[5] >= 0x60) /* bsid >= 12 */
96
half = halfrate[buf[5] >> 3];
98
/* acmod, dsurmod and lfeon */
100
*flags = ((((buf[6] & 0xf8) == 0x50) ? A52_DOLBY : acmod) |
101
((buf[6] & lfeon[acmod]) ? A52_LFE : 0));
103
frmsizecod = buf[4] & 63;
104
if (frmsizecod >= 38)
106
bitrate = rate [frmsizecod >> 1];
107
*bit_rate = (bitrate * 1000) >> half;
109
switch (buf[4] & 0xc0) {
111
*sample_rate = 48000 >> half;
114
*sample_rate = 44100 >> half;
115
return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
117
*sample_rate = 32000 >> half;
124
int a52_frame (a52_state_t * state, uint8_t * buf, int * flags,
125
level_t * level, sample_t bias)
127
static level_t clev[4] = { LEVEL (LEVEL_3DB), LEVEL (LEVEL_45DB),
128
LEVEL (LEVEL_6DB), LEVEL (LEVEL_45DB) };
129
static level_t slev[4] = { LEVEL (LEVEL_3DB), LEVEL (LEVEL_6DB),
130
0, LEVEL (LEVEL_6DB) };
134
state->fscod = buf[4] >> 6;
135
state->halfrate = halfrate[buf[5] >> 3];
136
state->acmod = acmod = buf[6] >> 5;
138
a52_bitstream_set_ptr (state, buf + 6);
139
bitstream_get (state, 3); /* skip acmod we already parsed */
141
if ((acmod == 2) && (bitstream_get (state, 2) == 2)) /* dsurmod */
144
state->clev = state->slev = 0;
146
if ((acmod & 1) && (acmod != 1))
147
state->clev = clev[bitstream_get (state, 2)]; /* cmixlev */
150
state->slev = slev[bitstream_get (state, 2)]; /* surmixlev */
152
state->lfeon = bitstream_get (state, 1);
154
state->output = a52_downmix_init (acmod, *flags, level,
155
state->clev, state->slev);
156
if (state->output < 0)
158
if (state->lfeon && (*flags & A52_LFE))
159
state->output |= A52_LFE;
160
*flags = state->output;
161
/* the 2* compensates for differences in imdct */
162
state->dynrng = state->level = MUL_C (*level, 2);
165
state->dynrngcall = NULL;
166
state->cplba.deltbae = DELTA_BIT_NONE;
167
state->ba[0].deltbae = state->ba[1].deltbae = state->ba[2].deltbae =
168
state->ba[3].deltbae = state->ba[4].deltbae = DELTA_BIT_NONE;
172
bitstream_get (state, 5); /* dialnorm */
173
if (bitstream_get (state, 1)) /* compre */
174
bitstream_get (state, 8); /* compr */
175
if (bitstream_get (state, 1)) /* langcode */
176
bitstream_get (state, 8); /* langcod */
177
if (bitstream_get (state, 1)) /* audprodie */
178
bitstream_get (state, 7); /* mixlevel + roomtyp */
179
} while (chaninfo--);
181
bitstream_get (state, 2); /* copyrightb + origbs */
183
if (bitstream_get (state, 1)) /* timecod1e */
184
bitstream_get (state, 14); /* timecod1 */
185
if (bitstream_get (state, 1)) /* timecod2e */
186
bitstream_get (state, 14); /* timecod2 */
188
if (bitstream_get (state, 1)) { /* addbsie */
191
addbsil = bitstream_get (state, 6);
193
bitstream_get (state, 8); /* addbsi */
200
void a52_dynrng (a52_state_t * state,
201
level_t (* call) (level_t, void *), void * data)
206
state->dynrngcall = call;
207
state->dynrngdata = data;
211
static int parse_exponents (a52_state_t * state, int expstr, int ngrps,
212
uint8_t exponent, uint8_t * dest)
217
exps = bitstream_get (state, 7);
219
exponent += exp_1[exps];
225
*(dest++) = exponent;
226
*(dest++) = exponent;
228
*(dest++) = exponent;
230
*(dest++) = exponent;
233
exponent += exp_2[exps];
239
*(dest++) = exponent;
240
*(dest++) = exponent;
242
*(dest++) = exponent;
244
*(dest++) = exponent;
247
exponent += exp_3[exps];
253
*(dest++) = exponent;
254
*(dest++) = exponent;
256
*(dest++) = exponent;
258
*(dest++) = exponent;
265
static int parse_deltba (a52_state_t * state, int8_t * deltba)
267
int deltnseg, deltlen, delta, j;
269
memset (deltba, 0, 50);
271
deltnseg = bitstream_get (state, 3);
274
j += bitstream_get (state, 5);
275
deltlen = bitstream_get (state, 4);
276
delta = bitstream_get (state, 3);
277
delta -= (delta >= 4) ? 3 : 4;
280
if (j + deltlen >= 50)
284
} while (deltnseg--);
289
static inline int zero_snr_offsets (int nfchans, a52_state_t * state)
293
if ((state->csnroffst) ||
294
(state->chincpl && state->cplba.bai >> 3) || /* cplinu, fsnroffst */
295
(state->lfeon && state->lfeba.bai >> 3)) /* fsnroffst */
297
for (i = 0; i < nfchans; i++)
298
if (state->ba[i].bai >> 3) /* fsnroffst */
303
static inline int16_t dither_gen (a52_state_t * state)
307
nstate = dither_lut[state->lfsr_state >> 8] ^ (state->lfsr_state << 8);
309
state->lfsr_state = (uint16_t) nstate;
311
return (3 * nstate) >> 2;
315
#define COEFF(c,t,l,s,e) (c) = (t) * (s)[e]
317
#define COEFF(c,_t,_l,s,e) do { \
318
quantizer_t t = (_t); \
321
sample_t tmp = t * (l >> 16) + ((t * (l & 0xffff)) >> 16); \
323
(c) = tmp >> shift; \
325
(c) = tmp << -shift; \
329
static void coeff_get (a52_state_t * state, sample_t * coeff,
330
expbap_t * expbap, quantizer_set_t * quant,
331
level_t level, int dither, int end)
340
for (i = 0; i <= 24; i++)
341
factor[i] = scale_factor[i] * level;
347
for (i = 0; i < end; i++) {
354
COEFF (coeff[i], dither_gen (state), level, factor, exp[i]);
362
if (quant->q1_ptr >= 0) {
363
COEFF (coeff[i], quant->q1[quant->q1_ptr--], level,
369
code = bitstream_get (state, 5);
372
quant->q1[0] = q_1_2[code];
373
quant->q1[1] = q_1_1[code];
374
COEFF (coeff[i], q_1_0[code], level, factor, exp[i]);
379
if (quant->q2_ptr >= 0) {
380
COEFF (coeff[i], quant->q2[quant->q2_ptr--], level,
386
code = bitstream_get (state, 7);
389
quant->q2[0] = q_2_2[code];
390
quant->q2[1] = q_2_1[code];
391
COEFF (coeff[i], q_2_0[code], level, factor, exp[i]);
396
COEFF (coeff[i], q_3[bitstream_get (state, 3)], level,
401
if (quant->q4_ptr == 0) {
403
COEFF (coeff[i], quant->q4, level, factor, exp[i]);
408
code = bitstream_get (state, 7);
411
quant->q4 = q_4_1[code];
412
COEFF (coeff[i], q_4_0[code], level, factor, exp[i]);
417
COEFF (coeff[i], q_5[bitstream_get (state, 4)], level,
422
COEFF (coeff[i], bitstream_get_2 (state, bapi) << (16 - bapi),
423
level, factor, exp[i]);
428
static void coeff_get_coupling (a52_state_t * state, int nfchans,
429
level_t * coeff, sample_t (* samples)[256],
430
quantizer_set_t * quant, uint8_t dithflag[5])
432
int cplbndstrc, bnd, i, i_end, ch;
437
exp = state->cpl_expbap.exp;
438
bap = state->cpl_expbap.bap;
440
cplbndstrc = state->cplbndstrc;
441
i = state->cplstrtmant;
442
while (i < state->cplendmant) {
444
while (cplbndstrc & 1) {
449
for (ch = 0; ch < nfchans; ch++)
450
cplco[ch] = MUL_L (state->cplco[ch][bnd], coeff[ch]);
454
quantizer_t cplcoeff;
460
for (ch = 0; ch < nfchans; ch++)
461
if ((state->chincpl >> ch) & 1) {
464
samples[ch][i] = (scale_factor[exp[i]] *
465
cplco[ch] * dither_gen (state));
467
COEFF (samples[ch][i], dither_gen (state),
468
cplco[ch], scale_factor, exp[i]);
477
if (quant->q1_ptr >= 0) {
478
cplcoeff = quant->q1[quant->q1_ptr--];
483
code = bitstream_get (state, 5);
486
quant->q1[0] = q_1_2[code];
487
quant->q1[1] = q_1_1[code];
488
cplcoeff = q_1_0[code];
493
if (quant->q2_ptr >= 0) {
494
cplcoeff = quant->q2[quant->q2_ptr--];
499
code = bitstream_get (state, 7);
502
quant->q2[0] = q_2_2[code];
503
quant->q2[1] = q_2_1[code];
504
cplcoeff = q_2_0[code];
509
cplcoeff = q_3[bitstream_get (state, 3)];
513
if (quant->q4_ptr == 0) {
515
cplcoeff = quant->q4;
520
code = bitstream_get (state, 7);
523
quant->q4 = q_4_1[code];
524
cplcoeff = q_4_0[code];
529
cplcoeff = q_5[bitstream_get (state, 4)];
533
cplcoeff = bitstream_get_2 (state, bapi) << (16 - bapi);
536
cplcoeff *= scale_factor[exp[i]];
538
for (ch = 0; ch < nfchans; ch++)
539
if ((state->chincpl >> ch) & 1)
541
samples[ch][i] = cplcoeff * cplco[ch];
543
COEFF (samples[ch][i], cplcoeff, cplco[ch],
544
scale_factor, exp[i]);
551
int a52_block (a52_state_t * state)
553
static const uint8_t nfchans_tbl[] = {2, 1, 2, 3, 3, 4, 4, 5, 1, 1, 2};
554
static int rematrix_band[4] = {25, 37, 61, 253};
555
int i, nfchans, chaninfo;
556
uint8_t cplexpstr, chexpstr[5], lfeexpstr, do_bit_alloc, done_cpl;
557
uint8_t blksw[5], dithflag[5];
560
quantizer_set_t quant;
563
nfchans = nfchans_tbl[state->acmod];
565
for (i = 0; i < nfchans; i++)
566
blksw[i] = bitstream_get (state, 1);
568
for (i = 0; i < nfchans; i++)
569
dithflag[i] = bitstream_get (state, 1);
571
chaninfo = !state->acmod;
573
if (bitstream_get (state, 1)) { /* dynrnge */
576
dynrng = bitstream_get_2 (state, 8);
577
if (state->dynrnge) {
580
#if !defined(LIBA52_FIXED)
581
range = ((((dynrng & 0x1f) | 0x20) << 13) *
582
scale_factor[3 - (dynrng >> 5)]);
584
range = ((dynrng & 0x1f) | 0x20) << (21 + (dynrng >> 5));
586
if (state->dynrngcall)
587
range = state->dynrngcall (range, state->dynrngdata);
588
state->dynrng = MUL_L (state->level, range);
591
} while (chaninfo--);
593
if (bitstream_get (state, 1)) { /* cplstre */
595
if (bitstream_get (state, 1)) { /* cplinu */
596
static uint8_t bndtab[16] = {31, 35, 37, 39, 41, 42, 43, 44,
597
45, 45, 46, 46, 47, 47, 48, 48};
602
for (i = 0; i < nfchans; i++)
603
state->chincpl |= bitstream_get (state, 1) << i;
604
switch (state->acmod) {
608
state->phsflginu = bitstream_get (state, 1);
610
cplbegf = bitstream_get (state, 4);
611
cplendf = bitstream_get (state, 4);
613
if (cplendf + 3 - cplbegf < 0)
615
state->ncplbnd = ncplsubnd = cplendf + 3 - cplbegf;
616
state->cplstrtbnd = bndtab[cplbegf];
617
state->cplstrtmant = cplbegf * 12 + 37;
618
state->cplendmant = cplendf * 12 + 73;
620
state->cplbndstrc = 0;
621
for (i = 0; i < ncplsubnd - 1; i++)
622
if (bitstream_get (state, 1)) {
623
state->cplbndstrc |= 1 << i;
629
if (state->chincpl) { /* cplinu */
633
for (i = 0; i < nfchans; i++)
634
if ((state->chincpl) >> i & 1)
635
if (bitstream_get (state, 1)) { /* cplcoe */
636
int mstrcplco, cplcoexp, cplcomant;
639
mstrcplco = 3 * bitstream_get (state, 2);
640
for (j = 0; j < state->ncplbnd; j++) {
641
cplcoexp = bitstream_get (state, 4);
642
cplcomant = bitstream_get (state, 4);
646
cplcomant = (cplcomant | 0x10) << 13;
649
cplcomant * scale_factor[cplcoexp + mstrcplco];
651
state->cplco[i][j] = (cplcomant << 11) >> (cplcoexp + mstrcplco);
656
if ((state->acmod == 2) && state->phsflginu && cplcoe)
657
for (j = 0; j < state->ncplbnd; j++)
658
if (bitstream_get (state, 1)) /* phsflg */
659
state->cplco[1][j] = -state->cplco[1][j];
662
if ((state->acmod == 2) && (bitstream_get (state, 1))) { /* rematstr */
666
end = (state->chincpl) ? state->cplstrtmant : 253; /* cplinu */
669
state->rematflg |= bitstream_get (state, 1) << i;
670
while (rematrix_band[i++] < end);
673
cplexpstr = EXP_REUSE;
674
lfeexpstr = EXP_REUSE;
675
if (state->chincpl) /* cplinu */
676
cplexpstr = bitstream_get (state, 2);
677
for (i = 0; i < nfchans; i++)
678
chexpstr[i] = bitstream_get (state, 2);
680
lfeexpstr = bitstream_get (state, 1);
682
for (i = 0; i < nfchans; i++)
683
if (chexpstr[i] != EXP_REUSE) {
684
if ((state->chincpl >> i) & 1)
685
state->endmant[i] = state->cplstrtmant;
689
chbwcod = bitstream_get (state, 6);
692
state->endmant[i] = chbwcod * 3 + 73;
698
if (cplexpstr != EXP_REUSE) {
699
int cplabsexp, ncplgrps;
702
ncplgrps = ((state->cplendmant - state->cplstrtmant) /
703
(3 << (cplexpstr - 1)));
704
cplabsexp = bitstream_get (state, 4) << 1;
705
if (parse_exponents (state, cplexpstr, ncplgrps, cplabsexp,
706
state->cpl_expbap.exp + state->cplstrtmant))
709
for (i = 0; i < nfchans; i++)
710
if (chexpstr[i] != EXP_REUSE) {
711
int grp_size, nchgrps;
713
do_bit_alloc |= 1 << i;
714
grp_size = 3 << (chexpstr[i] - 1);
715
nchgrps = (state->endmant[i] + grp_size - 4) / grp_size;
716
state->fbw_expbap[i].exp[0] = bitstream_get (state, 4);
717
if (parse_exponents (state, chexpstr[i], nchgrps,
718
state->fbw_expbap[i].exp[0],
719
state->fbw_expbap[i].exp + 1))
721
bitstream_get (state, 2); /* gainrng */
723
if (lfeexpstr != EXP_REUSE) {
725
state->lfe_expbap.exp[0] = bitstream_get (state, 4);
726
if (parse_exponents (state, lfeexpstr, 2, state->lfe_expbap.exp[0],
727
state->lfe_expbap.exp + 1))
731
if (bitstream_get (state, 1)) { /* baie */
733
state->bai = bitstream_get (state, 11);
735
if (bitstream_get (state, 1)) { /* snroffste */
737
state->csnroffst = bitstream_get (state, 6);
738
if (state->chincpl) /* cplinu */
739
state->cplba.bai = bitstream_get (state, 7);
740
for (i = 0; i < nfchans; i++)
741
state->ba[i].bai = bitstream_get (state, 7);
743
state->lfeba.bai = bitstream_get (state, 7);
745
if ((state->chincpl) && (bitstream_get (state, 1))) { /* cplleake */
747
state->cplfleak = 9 - bitstream_get (state, 3);
748
state->cplsleak = 9 - bitstream_get (state, 3);
751
if (bitstream_get (state, 1)) { /* deltbaie */
753
if (state->chincpl) /* cplinu */
754
state->cplba.deltbae = bitstream_get (state, 2);
755
for (i = 0; i < nfchans; i++)
756
state->ba[i].deltbae = bitstream_get (state, 2);
757
if (state->chincpl && /* cplinu */
758
(state->cplba.deltbae == DELTA_BIT_NEW) &&
759
parse_deltba (state, state->cplba.deltba))
761
for (i = 0; i < nfchans; i++)
762
if ((state->ba[i].deltbae == DELTA_BIT_NEW) &&
763
parse_deltba (state, state->ba[i].deltba))
768
if (zero_snr_offsets (nfchans, state)) {
769
memset (state->cpl_expbap.bap, 0, sizeof (state->cpl_expbap.bap));
770
for (i = 0; i < nfchans; i++)
771
memset (state->fbw_expbap[i].bap, 0,
772
sizeof (state->fbw_expbap[i].bap));
773
memset (state->lfe_expbap.bap, 0, sizeof (state->lfe_expbap.bap));
775
if (state->chincpl && (do_bit_alloc & 64)) /* cplinu */
776
a52_bit_allocate (state, &state->cplba, state->cplstrtbnd,
777
state->cplstrtmant, state->cplendmant,
778
state->cplfleak << 8, state->cplsleak << 8,
780
for (i = 0; i < nfchans; i++)
781
if (do_bit_alloc & (1 << i))
782
a52_bit_allocate (state, state->ba + i, 0, 0,
783
state->endmant[i], 0, 0,
784
state->fbw_expbap +i);
785
if (state->lfeon && (do_bit_alloc & 32)) {
786
state->lfeba.deltbae = DELTA_BIT_NONE;
787
a52_bit_allocate (state, &state->lfeba, 0, 0, 7, 0, 0,
793
if (bitstream_get (state, 1)) { /* skiple */
794
i = bitstream_get (state, 9); /* skipl */
796
bitstream_get (state, 8);
799
samples = state->samples;
800
if (state->output & A52_LFE)
801
samples += 256; /* shift for LFE channel */
803
chanbias = a52_downmix_coeff (coeff, state->acmod, state->output,
804
state->dynrng, state->clev, state->slev);
806
quant.q1_ptr = quant.q2_ptr = quant.q4_ptr = -1;
809
for (i = 0; i < nfchans; i++) {
812
coeff_get (state, samples + 256 * i, state->fbw_expbap +i, &quant,
813
coeff[i], dithflag[i], state->endmant[i]);
815
if ((state->chincpl >> i) & 1) {
818
coeff_get_coupling (state, nfchans, coeff,
819
(sample_t (*)[256])samples, &quant,
822
j = state->cplendmant;
824
j = state->endmant[i];
826
(samples + 256 * i)[j] = 0;
830
if (state->acmod == 2) {
831
int j, end, band, rematflg;
833
end = ((state->endmant[0] < state->endmant[1]) ?
834
state->endmant[0] : state->endmant[1]);
838
rematflg = state->rematflg;
840
if (! (rematflg & 1)) {
842
j = rematrix_band[i++];
846
band = rematrix_band[i++];
853
tmp1 = (samples+256)[j];
854
samples[j] = tmp0 + tmp1;
855
(samples+256)[j] = tmp0 - tmp1;
856
} while (++j < band);
861
if (state->output & A52_LFE) {
862
coeff_get (state, samples - 256, &state->lfe_expbap, &quant,
863
state->dynrng, 0, 7);
864
for (i = 7; i < 256; i++)
865
(samples-256)[i] = 0;
866
a52_imdct_512 (samples - 256, samples + 1536 - 256, state->bias);
868
/* just skip the LFE coefficients */
869
coeff_get (state, samples + 1280, &state->lfe_expbap, &quant,
875
if (nfchans_tbl[state->output & A52_CHANNEL_MASK] < nfchans)
876
for (i = 1; i < nfchans; i++)
877
if (blksw[i] != blksw[0])
881
if (state->downmixed) {
882
state->downmixed = 0;
883
a52_upmix (samples + 1536, state->acmod, state->output);
886
for (i = 0; i < nfchans; i++) {
890
if (!(chanbias & (1 << i)))
895
a52_imdct_256 (samples + 256 * i, samples + 1536 + 256 * i,
898
a52_imdct_512 (samples + 256 * i, samples + 1536 + 256 * i,
903
for (j = 0; j < 256; j++)
904
(samples + 256 * i)[j] = bias;
908
a52_downmix (samples, state->acmod, state->output, state->bias,
909
state->clev, state->slev);
911
nfchans = nfchans_tbl[state->output & A52_CHANNEL_MASK];
913
a52_downmix (samples, state->acmod, state->output, 0,
914
state->clev, state->slev);
916
if (!state->downmixed) {
917
state->downmixed = 1;
918
a52_downmix (samples + 1536, state->acmod, state->output, 0,
919
state->clev, state->slev);
923
for (i = 0; i < nfchans; i++)
924
a52_imdct_256 (samples + 256 * i, samples + 1536 + 256 * i,
927
for (i = 0; i < nfchans; i++)
928
a52_imdct_512 (samples + 256 * i, samples + 1536 + 256 * i,
935
void a52_free (a52_state_t * state)
937
free (state->samples);