comparison libac3/parse.c @ 0:986e461dc072 libavcodec

Initial revision
author glantau
date Sun, 22 Jul 2001 14:18:56 +0000
parents
children 5aa6292a1660
comparison
equal deleted inserted replaced
-1:000000000000 0:986e461dc072
1 /*
2 * parse.c
3 *
4 * Copyright (C) Aaron Holtzman - May 1999
5 *
6 * This file is part of ac3dec, a free Dolby AC-3 stream decoder.
7 *
8 * ac3dec is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
12 *
13 * ac3dec is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with GNU Make; see the file COPYING. If not, write to
20 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21 *
22 *
23 */
24
25 #include <inttypes.h>
26 #include <string.h>
27
28 #include "ac3.h"
29 #include "ac3_internal.h"
30
31 #include "bitstream.h"
32 #include "tables.h"
33
34 extern stream_samples_t samples; // FIXME
35 static float delay[6][256];
36
37 void ac3_init (void)
38 {
39 imdct_init ();
40 }
41
42 static uint8_t halfrate[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3};
43
44 int ac3_syncinfo (uint8_t * buf, int * flags,
45 int * sample_rate, int * bit_rate)
46 {
47 static int rate[] = { 32, 40, 48, 56, 64, 80, 96, 112,
48 128, 160, 192, 224, 256, 320, 384, 448,
49 512, 576, 640};
50 static uint8_t lfeon[8] = {0x10, 0x10, 0x04, 0x04, 0x04, 0x01, 0x04, 0x01};
51 int frmsizecod;
52 int bitrate;
53 int half;
54 int acmod;
55
56 if ((buf[0] != 0x0b) || (buf[1] != 0x77)) // syncword
57 return 0;
58
59 if (buf[5] >= 0x60) // bsid >= 12
60 return 0;
61 half = halfrate[buf[5] >> 3];
62
63 // acmod, dsurmod and lfeon
64 acmod = buf[6] >> 5;
65 *flags = (((buf[6] & 0xf8) == 0x50) ? AC3_DOLBY : acmod) |
66 ((buf[6] & lfeon[acmod]) ? AC3_LFE : 0);
67
68 frmsizecod = buf[4] & 63;
69 if (frmsizecod >= 38)
70 return 0;
71 bitrate = rate [frmsizecod >> 1];
72 *bit_rate = (bitrate * 1000) >> half;
73
74 switch (buf[4] & 0xc0) {
75 case 0: // 48 KHz
76 *sample_rate = 48000 >> half;
77 return 4 * bitrate;
78 case 0x40:
79 *sample_rate = 44100 >> half;
80 return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
81 case 0x80:
82 *sample_rate = 32000 >> half;
83 return 6 * bitrate;
84 default:
85 return 0;
86 }
87 }
88
89 int ac3_frame (ac3_state_t * state, uint8_t * buf, int * flags, float * level,
90 float bias)
91 {
92 static float clev[4] = {LEVEL_3DB, LEVEL_45DB, LEVEL_6DB, LEVEL_45DB};
93 static float slev[4] = {LEVEL_3DB, LEVEL_6DB, 0, LEVEL_6DB};
94 int chaninfo;
95 int acmod;
96
97 state->fscod = buf[4] >> 6;
98 state->halfrate = halfrate[buf[5] >> 3];
99 state->acmod = acmod = buf[6] >> 5;
100
101 bitstream_set_ptr (buf + 6);
102 bitstream_get (3); // skip acmod we already parsed
103
104 if ((acmod == 2) && (bitstream_get (2) == 2)) // dsurmod
105 acmod = AC3_DOLBY;
106
107 if ((acmod & 1) && (acmod != 1))
108 state->clev = clev[bitstream_get (2)]; // cmixlev
109
110 if (acmod & 4)
111 state->slev = slev[bitstream_get (2)]; // surmixlev
112
113 state->lfeon = bitstream_get (1);
114
115 state->output = downmix_init (acmod, *flags, level,
116 state->clev, state->slev);
117 if (state->output < 0)
118 return 1;
119 *flags = state->output;
120 state->level = *level;
121 state->bias = bias;
122
123 chaninfo = !acmod;
124 do {
125 bitstream_get (5); // dialnorm
126 if (bitstream_get (1)) // compre
127 bitstream_get (8); // compr
128 if (bitstream_get (1)) // langcode
129 bitstream_get (8); // langcod
130 if (bitstream_get (1)) // audprodie
131 bitstream_get (7); // mixlevel + roomtyp
132 } while (chaninfo--);
133
134 bitstream_get (2); // copyrightb + origbs
135
136 if (bitstream_get (1)) // timecod1e
137 bitstream_get (14); // timecod1
138 if (bitstream_get (1)) // timecod2e
139 bitstream_get (14); // timecod2
140
141 if (bitstream_get (1)) { // addbsie
142 int addbsil;
143
144 addbsil = bitstream_get (6);
145 do {
146 bitstream_get (8); // addbsi
147 } while (addbsil--);
148 }
149
150 return 0;
151 }
152
153 static int parse_exponents (int expstr, int ngrps, uint8_t exponent,
154 uint8_t * dest)
155 {
156 int exps;
157
158 while (ngrps--) {
159 exps = bitstream_get (7);
160
161 exponent += exp_1[exps];
162 if (exponent > 24)
163 return 1;
164
165 switch (expstr) {
166 case EXP_D45:
167 *(dest++) = exponent;
168 *(dest++) = exponent;
169 case EXP_D25:
170 *(dest++) = exponent;
171 case EXP_D15:
172 *(dest++) = exponent;
173 }
174
175 exponent += exp_2[exps];
176 if (exponent > 24)
177 return 1;
178
179 switch (expstr) {
180 case EXP_D45:
181 *(dest++) = exponent;
182 *(dest++) = exponent;
183 case EXP_D25:
184 *(dest++) = exponent;
185 case EXP_D15:
186 *(dest++) = exponent;
187 }
188
189 exponent += exp_3[exps];
190 if (exponent > 24)
191 return 1;
192
193 switch (expstr) {
194 case EXP_D45:
195 *(dest++) = exponent;
196 *(dest++) = exponent;
197 case EXP_D25:
198 *(dest++) = exponent;
199 case EXP_D15:
200 *(dest++) = exponent;
201 }
202 }
203
204 return 0;
205 }
206
207 static int parse_deltba (int8_t * deltba)
208 {
209 int deltnseg, deltlen, delta, j;
210
211 memset (deltba, 0, 50);
212
213 deltnseg = bitstream_get (3);
214 j = 0;
215 do {
216 j += bitstream_get (5);
217 deltlen = bitstream_get (4);
218 delta = bitstream_get (3);
219 delta -= (delta >= 4) ? 3 : 4;
220 if (!deltlen)
221 continue;
222 if (j + deltlen >= 50)
223 return 1;
224 while (deltlen--)
225 deltba[j++] = delta;
226 } while (deltnseg--);
227
228 return 0;
229 }
230
231 static inline int zero_snr_offsets (int nfchans, ac3_state_t * state)
232 {
233 int i;
234
235 if ((state->csnroffst) || (state->cplinu && state->cplba.fsnroffst) ||
236 (state->lfeon && state->lfeba.fsnroffst))
237 return 0;
238 for (i = 0; i < nfchans; i++)
239 if (state->ba[i].fsnroffst)
240 return 0;
241 return 1;
242 }
243
244 static float q_1[2];
245 static float q_2[2];
246 static float q_4;
247 static int q_1_pointer;
248 static int q_2_pointer;
249 static int q_4_pointer;
250
251 #define GET_COEFF(COEFF,DITHER) \
252 switch (bap[i]) { \
253 case 0: \
254 DITHER (scale_factor[exp[i]]); \
255 \
256 case -1: \
257 if (q_1_pointer >= 0) { \
258 COEFF (q_1[q_1_pointer--] * scale_factor[exp[i]]); \
259 } else { \
260 int code; \
261 \
262 code = bitstream_get (5); \
263 \
264 q_1_pointer = 1; \
265 q_1[0] = q_1_2[code]; \
266 q_1[1] = q_1_1[code]; \
267 COEFF (q_1_0[code] * scale_factor[exp[i]]); \
268 } \
269 \
270 case -2: \
271 if (q_2_pointer >= 0) { \
272 COEFF (q_2[q_2_pointer--] * scale_factor[exp[i]]); \
273 } else { \
274 int code; \
275 \
276 code = bitstream_get (7); \
277 \
278 q_2_pointer = 1; \
279 q_2[0] = q_2_2[code]; \
280 q_2[1] = q_2_1[code]; \
281 COEFF (q_2_0[code] * scale_factor[exp[i]]); \
282 } \
283 \
284 case 3: \
285 COEFF (q_3[bitstream_get (3)] * scale_factor[exp[i]]); \
286 \
287 case -3: \
288 if (q_4_pointer == 0) { \
289 q_4_pointer = -1; \
290 COEFF (q_4 * scale_factor[exp[i]]); \
291 } else { \
292 int code; \
293 \
294 code = bitstream_get (7); \
295 \
296 q_4_pointer = 0; \
297 q_4 = q_4_1[code]; \
298 COEFF (q_4_0[code] * scale_factor[exp[i]]); \
299 } \
300 \
301 case 4: \
302 COEFF (q_5[bitstream_get (4)] * scale_factor[exp[i]]); \
303 \
304 default: \
305 COEFF (((int16_t)(bitstream_get(bap[i]) << (16 - bap[i]))) * \
306 scale_factor[exp[i]]); \
307 }
308
309 #define CHANNEL_COEFF(val) \
310 coeff[i++] = val; \
311 continue;
312
313 #define CHANNEL_DITHER(val) \
314 if (dither) { \
315 coeff[i++] = dither_gen () * val; \
316 continue; \
317 } else { \
318 coeff[i++] = 0; \
319 continue; \
320 }
321
322 static uint16_t lfsr_state = 1;
323
324 static inline int16_t dither_gen(void)
325 {
326 int16_t state;
327
328 state = dither_lut[lfsr_state >> 8] ^ (lfsr_state << 8);
329
330 lfsr_state = (uint16_t) state;
331
332 return ((state * (int) (LEVEL_3DB * 256)) >> 8);
333 }
334
335 static void coeff_get (float * coeff, uint8_t * exp, int8_t * bap,
336 int dither, int end)
337 {
338 int i;
339
340 i = 0;
341 while (i < end)
342 GET_COEFF (CHANNEL_COEFF, CHANNEL_DITHER);
343 }
344
345 #define COUPLING_COEFF(val) \
346 cplcoeff = val; \
347 break;
348
349 #define COUPLING_DITHER(val) \
350 cplcoeff = val; \
351 for (ch = 0; ch < nfchans; ch++) \
352 if (state->chincpl[ch]) { \
353 if (dithflag[ch]) \
354 samples[ch][i] = \
355 state->cplco[ch][bnd] * dither_gen () * cplcoeff; \
356 else \
357 samples[ch][i] = 0; \
358 } \
359 i++; \
360 continue;
361
362 int ac3_block (ac3_state_t * state)
363 {
364 static const uint8_t nfchans_tbl[8] = {2, 1, 2, 3, 3, 4, 4, 5};
365 static int rematrix_band[4] = {25, 37, 61, 253};
366 int i, nfchans, chaninfo;
367 uint8_t cplexpstr, chexpstr[5], lfeexpstr, do_bit_alloc, done_cpl;
368 uint8_t blksw[5], dithflag[5];
369
370 nfchans = nfchans_tbl[state->acmod];
371
372 for (i = 0; i < nfchans; i++)
373 blksw[i] = bitstream_get (1);
374
375 for (i = 0; i < nfchans; i++)
376 dithflag[i] = bitstream_get (1);
377
378 chaninfo = !(state->acmod);
379 do {
380 if (bitstream_get (1)) // dynrnge
381 bitstream_get (8); // dynrng
382 } while (chaninfo--);
383
384 if (bitstream_get (1)) { // cplstre
385 state->cplinu = bitstream_get (1);
386 if (state->cplinu) {
387 static int bndtab[16] = {31, 35, 37, 39, 41, 42, 43, 44,
388 45, 45, 46, 46, 47, 47, 48, 48};
389 int cplbegf;
390 int cplendf;
391 int ncplsubnd;
392
393 for (i = 0; i < nfchans; i++)
394 state->chincpl[i] = bitstream_get (1);
395 switch (state->acmod) {
396 case 0: case 1:
397 return 1;
398 case 2:
399 state->phsflginu = bitstream_get (1);
400 }
401 cplbegf = bitstream_get (4);
402 cplendf = bitstream_get (4);
403
404 if (cplendf + 3 - cplbegf < 0)
405 return 1;
406 state->ncplbnd = ncplsubnd = cplendf + 3 - cplbegf;
407 state->cplstrtbnd = bndtab[cplbegf];
408 state->cplstrtmant = cplbegf * 12 + 37;
409 state->cplendmant = cplendf * 12 + 73;
410
411 for (i = 0; i < ncplsubnd - 1; i++) {
412 state->cplbndstrc[i] = bitstream_get (1);
413 state->ncplbnd -= state->cplbndstrc[i];
414 }
415 state->cplbndstrc[i] = 0; // last value is a sentinel
416 }
417 }
418
419 if (state->cplinu) {
420 int j, cplcoe;
421
422 cplcoe = 0;
423 for (i = 0; i < nfchans; i++)
424 if (state->chincpl[i])
425 if (bitstream_get (1)) { // cplcoe
426 int mstrcplco, cplcoexp, cplcomant;
427
428 cplcoe = 1;
429 mstrcplco = 3 * bitstream_get (2);
430 for (j = 0; j < state->ncplbnd; j++) {
431 cplcoexp = bitstream_get (4);
432 cplcomant = bitstream_get (4);
433 if (cplcoexp == 15)
434 cplcomant <<= 14;
435 else
436 cplcomant = (cplcomant | 0x10) << 13;
437 state->cplco[i][j] =
438 cplcomant * scale_factor[cplcoexp + mstrcplco];
439 }
440 }
441 if ((state->acmod == 2) && state->phsflginu && cplcoe)
442 for (j = 0; j < state->ncplbnd; j++)
443 if (bitstream_get (1)) // phsflg
444 state->cplco[1][j] = -state->cplco[1][j];
445 }
446
447 if ((state->acmod == 2) && (bitstream_get (1))) { // rematstr
448 int end;
449
450 end = (state->cplinu) ? state->cplstrtmant : 253;
451 i = 0;
452 do
453 state->rematflg[i] = bitstream_get (1);
454 while (rematrix_band[i++] < end);
455 }
456
457 cplexpstr = EXP_REUSE;
458 lfeexpstr = EXP_REUSE;
459 if (state->cplinu)
460 cplexpstr = bitstream_get (2);
461 for (i = 0; i < nfchans; i++)
462 chexpstr[i] = bitstream_get (2);
463 if (state->lfeon)
464 lfeexpstr = bitstream_get (1);
465
466 for (i = 0; i < nfchans; i++)
467 if (chexpstr[i] != EXP_REUSE) {
468 if (state->cplinu && state->chincpl[i])
469 state->endmant[i] = state->cplstrtmant;
470 else {
471 int chbwcod;
472
473 chbwcod = bitstream_get (6);
474 if (chbwcod > 60)
475 return 1;
476 state->endmant[i] = chbwcod * 3 + 73;
477 }
478 }
479
480 do_bit_alloc = 0;
481
482 if (cplexpstr != EXP_REUSE) {
483 int cplabsexp, ncplgrps;
484
485 do_bit_alloc = 1;
486 ncplgrps = ((state->cplendmant - state->cplstrtmant) /
487 (3 << (cplexpstr - 1)));
488 cplabsexp = bitstream_get (4) << 1;
489 if (parse_exponents (cplexpstr, ncplgrps, cplabsexp,
490 state->cpl_exp + state->cplstrtmant))
491 return 1;
492 }
493 for (i = 0; i < nfchans; i++)
494 if (chexpstr[i] != EXP_REUSE) {
495 int grp_size, nchgrps;
496
497 do_bit_alloc = 1;
498 grp_size = 3 << (chexpstr[i] - 1);
499 nchgrps = (state->endmant[i] + grp_size - 4) / grp_size;
500 state->fbw_exp[i][0] = bitstream_get (4);
501 if (parse_exponents (chexpstr[i], nchgrps, state->fbw_exp[i][0],
502 state->fbw_exp[i] + 1))
503 return 1;
504 bitstream_get (2); // gainrng
505 }
506 if (lfeexpstr != EXP_REUSE) {
507 do_bit_alloc = 1;
508 state->lfe_exp[0] = bitstream_get (4);
509 if (parse_exponents (lfeexpstr, 2, state->lfe_exp[0],
510 state->lfe_exp + 1))
511 return 1;
512 }
513
514 if (bitstream_get (1)) { // baie
515 do_bit_alloc = 1;
516 state->sdcycod = bitstream_get (2);
517 state->fdcycod = bitstream_get (2);
518 state->sgaincod = bitstream_get (2);
519 state->dbpbcod = bitstream_get (2);
520 state->floorcod = bitstream_get (3);
521 }
522 if (bitstream_get (1)) { //snroffste
523 do_bit_alloc = 1;
524 state->csnroffst = bitstream_get (6);
525 if (state->cplinu) {
526 state->cplba.fsnroffst = bitstream_get (4);
527 state->cplba.fgaincod = bitstream_get (3);
528 }
529 for (i = 0; i < nfchans; i++) {
530 state->ba[i].fsnroffst = bitstream_get (4);
531 state->ba[i].fgaincod = bitstream_get (3);
532 }
533 if (state->lfeon) {
534 state->lfeba.fsnroffst = bitstream_get (4);
535 state->lfeba.fgaincod = bitstream_get (3);
536 }
537 }
538 if ((state->cplinu) && (bitstream_get (1))) { // cplleake
539 do_bit_alloc = 1;
540 state->cplfleak = 2304 - (bitstream_get (3) << 8);
541 state->cplsleak = 2304 - (bitstream_get (3) << 8);
542 }
543
544 if (bitstream_get (1)) { // deltbaie
545 do_bit_alloc = 1;
546 if (state->cplinu)
547 state->cplba.deltbae = bitstream_get (2);
548 for (i = 0; i < nfchans; i++)
549 state->ba[i].deltbae = bitstream_get (2);
550 if (state->cplinu && (state->cplba.deltbae == DELTA_BIT_NEW) &&
551 parse_deltba (state->cplba.deltba))
552 return 1;
553 for (i = 0; i < nfchans; i++)
554 if ((state->ba[i].deltbae == DELTA_BIT_NEW) &&
555 parse_deltba (state->ba[i].deltba))
556 return 1;
557 }
558
559 if (do_bit_alloc) {
560 if (zero_snr_offsets (nfchans, state)) {
561 memset (state->cpl_bap, 0, sizeof (state->cpl_bap));
562 memset (state->fbw_bap, 0, sizeof (state->fbw_bap));
563 memset (state->lfe_bap, 0, sizeof (state->lfe_bap));
564 } else {
565 if (state->cplinu)
566 bit_allocate (state, &state->cplba, state->cplstrtbnd,
567 state->cplstrtmant, state->cplendmant,
568 state->cplfleak, state->cplsleak,
569 state->cpl_exp, state->cpl_bap);
570 for (i = 0; i < nfchans; i++)
571 bit_allocate (state, state->ba + i, 0, 0, state->endmant[i],
572 0, 0, state->fbw_exp[i], state->fbw_bap[i]);
573 if (state->lfeon) {
574 state->lfeba.deltbae = DELTA_BIT_NONE;
575 bit_allocate (state, &state->lfeba, 0, 0, 7, 0, 0,
576 state->lfe_exp, state->lfe_bap);
577 }
578 }
579 }
580
581 if (bitstream_get (1)) { // skiple
582 i = bitstream_get (9); // skipl
583 while (i--)
584 bitstream_get (8);
585 }
586
587 q_1_pointer = q_2_pointer = q_4_pointer = -1;
588 done_cpl = 0;
589
590 for (i = 0; i < nfchans; i++) {
591 int j;
592
593 coeff_get (samples[i], state->fbw_exp[i], state->fbw_bap[i],
594 dithflag[i], state->endmant[i]);
595
596 if (state->cplinu && state->chincpl[i]) {
597 if (!done_cpl) {
598 int i, i_end, bnd, sub_bnd, ch;
599 float cplcoeff;
600
601 done_cpl = 1;
602
603 #define bap state->cpl_bap
604 #define exp state->cpl_exp
605
606 sub_bnd = bnd = 0;
607 i = state->cplstrtmant;
608 while (i < state->cplendmant) {
609 i_end = i + 12;
610 while (state->cplbndstrc[sub_bnd++])
611 i_end += 12;
612
613 while (i < i_end) {
614 GET_COEFF (COUPLING_COEFF, COUPLING_DITHER);
615 for (ch = 0; ch < nfchans; ch++)
616 if (state->chincpl[ch])
617 samples[ch][i] =
618 state->cplco[ch][bnd] * cplcoeff;
619 i++;
620 }
621 bnd++;
622 }
623
624 #undef bap
625 #undef exp
626 }
627 j = state->cplendmant;
628 } else
629 j = state->endmant[i];
630 for (; j < 256; j++)
631 samples[i][j] = 0;
632 }
633
634 if (state->acmod == 2) {
635 int j, end, band;
636
637 end = ((state->endmant[0] < state->endmant[1]) ?
638 state->endmant[0] : state->endmant[1]);
639
640 i = 0;
641 j = 13;
642 do {
643 if (!state->rematflg[i]) {
644 j = rematrix_band[i++];
645 continue;
646 }
647 band = rematrix_band[i++];
648 if (band > end)
649 band = end;
650 do {
651 float tmp0, tmp1;
652
653 tmp0 = samples[0][j];
654 tmp1 = samples[1][j];
655 samples[0][j] = tmp0 + tmp1;
656 samples[1][j] = tmp0 - tmp1;
657 } while (++j < band);
658 } while (j < end);
659 }
660
661 if (state->lfeon) {
662 coeff_get (samples[5], state->lfe_exp, state->lfe_bap, 0, 7);
663 #if 0
664 for (i = 7; i < 256; i++)
665 samples[5][i] = 0;
666 #endif
667 }
668
669 for (i = 0; i < nfchans; i++)
670 if (blksw[i])
671 imdct_256 (samples[i], delay[i]);
672 else
673 imdct_512 (samples[i], delay[i]);
674
675 #if 0
676 if (state->lfeon)
677 imdct_512 (samples[5], delay[5]);
678 #endif
679
680 downmix (*samples, state->acmod, state->output, state->level, state->bias,
681 state->clev, state->slev);
682
683 return 0;
684 }