annotate liba52/parse.c @ 760:e858fe220ce0 libavcodec

xvid qpel bug workaround
author michaelni
date Tue, 22 Oct 2002 18:55:20 +0000
parents dd4f4c3d7171
children e113a950c074
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
332
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
1 /*
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
2 * parse.c
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
3 * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
4 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
5 *
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
6 * This file is part of a52dec, a free ATSC A-52 stream decoder.
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
7 * See http://liba52.sourceforge.net/ for updates.
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
8 *
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
9 * a52dec is free software; you can redistribute it and/or modify
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
10 * it under the terms of the GNU General Public License as published by
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
11 * the Free Software Foundation; either version 2 of the License, or
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
12 * (at your option) any later version.
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
13 *
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
14 * a52dec is distributed in the hope that it will be useful,
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
17 * GNU General Public License for more details.
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
18 *
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
19 * You should have received a copy of the GNU General Public License
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
20 * along with this program; if not, write to the Free Software
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
22 */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
23
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
24 #include "a52.h"
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
25 #include "a52_internal.h"
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
26 #include "bitstream.h"
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
27 #include "tables.h"
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
28
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
29 #ifdef HAVE_MEMALIGN
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
30 /* some systems have memalign() but no declaration for it */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
31 void * memalign (size_t align, size_t size);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
32 #else
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
33 /* assume malloc alignment is sufficient */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
34 #define memalign(align,size) malloc (size)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
35 #endif
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
36
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
37 typedef struct {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
38 sample_t q1[2];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
39 sample_t q2[2];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
40 sample_t q4;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
41 int q1_ptr;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
42 int q2_ptr;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
43 int q4_ptr;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
44 } quantizer_t;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
45
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
46 static uint8_t halfrate[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3};
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
47
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
48 a52_state_t * a52_init (uint32_t mm_accel)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
49 {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
50 a52_state_t * state;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
51 int i;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
52
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
53 state = malloc (sizeof (a52_state_t));
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
54 if (state == NULL)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
55 return NULL;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
56
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
57 state->samples = memalign (16, 256 * 12 * sizeof (sample_t));
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
58 if (state->samples == NULL) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
59 free (state);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
60 return NULL;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
61 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
62
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
63 for (i = 0; i < 256 * 12; i++)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
64 state->samples[i] = 0;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
65
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
66 state->downmixed = 1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
67
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
68 a52_imdct_init (mm_accel);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
69
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
70 return state;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
71 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
72
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
73 sample_t * a52_samples (a52_state_t * state)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
74 {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
75 return state->samples;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
76 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
77
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
78 int a52_syncinfo (uint8_t * buf, int * flags,
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
79 int * sample_rate, int * bit_rate)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
80 {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
81 static int rate[] = { 32, 40, 48, 56, 64, 80, 96, 112,
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
82 128, 160, 192, 224, 256, 320, 384, 448,
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
83 512, 576, 640};
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
84 static uint8_t lfeon[8] = {0x10, 0x10, 0x04, 0x04, 0x04, 0x01, 0x04, 0x01};
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
85 int frmsizecod;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
86 int bitrate;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
87 int half;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
88 int acmod;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
89
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
90 if ((buf[0] != 0x0b) || (buf[1] != 0x77)) /* syncword */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
91 return 0;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
92
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
93 if (buf[5] >= 0x60) /* bsid >= 12 */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
94 return 0;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
95 half = halfrate[buf[5] >> 3];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
96
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
97 /* acmod, dsurmod and lfeon */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
98 acmod = buf[6] >> 5;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
99 *flags = ((((buf[6] & 0xf8) == 0x50) ? A52_DOLBY : acmod) |
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
100 ((buf[6] & lfeon[acmod]) ? A52_LFE : 0));
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
101
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
102 frmsizecod = buf[4] & 63;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
103 if (frmsizecod >= 38)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
104 return 0;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
105 bitrate = rate [frmsizecod >> 1];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
106 *bit_rate = (bitrate * 1000) >> half;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
107
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
108 switch (buf[4] & 0xc0) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
109 case 0:
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
110 *sample_rate = 48000 >> half;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
111 return 4 * bitrate;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
112 case 0x40:
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
113 *sample_rate = 44100 >> half;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
114 return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
115 case 0x80:
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
116 *sample_rate = 32000 >> half;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
117 return 6 * bitrate;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
118 default:
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
119 return 0;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
120 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
121 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
122
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
123 int a52_frame (a52_state_t * state, uint8_t * buf, int * flags,
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
124 sample_t * level, sample_t bias)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
125 {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
126 static sample_t clev[4] = {LEVEL_3DB, LEVEL_45DB, LEVEL_6DB, LEVEL_45DB};
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
127 static sample_t slev[4] = {LEVEL_3DB, LEVEL_6DB, 0, LEVEL_6DB};
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
128 int chaninfo;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
129 int acmod;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
130
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
131 state->fscod = buf[4] >> 6;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
132 state->halfrate = halfrate[buf[5] >> 3];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
133 state->acmod = acmod = buf[6] >> 5;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
134
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
135 a52_bitstream_set_ptr (buf + 6);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
136 bitstream_get (3); /* skip acmod we already parsed */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
137
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
138 if ((acmod == 2) && (bitstream_get (2) == 2)) /* dsurmod */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
139 acmod = A52_DOLBY;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
140
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
141 if ((acmod & 1) && (acmod != 1))
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
142 state->clev = clev[bitstream_get (2)]; /* cmixlev */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
143
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
144 if (acmod & 4)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
145 state->slev = slev[bitstream_get (2)]; /* surmixlev */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
146
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
147 state->lfeon = bitstream_get (1);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
148
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
149 state->output = a52_downmix_init (acmod, *flags, level,
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
150 state->clev, state->slev);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
151 if (state->output < 0)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
152 return 1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
153 if (state->lfeon && (*flags & A52_LFE))
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
154 state->output |= A52_LFE;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
155 *flags = state->output;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
156 /* the 2* compensates for differences in imdct */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
157 state->dynrng = state->level = 2 * *level;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
158 state->bias = bias;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
159 state->dynrnge = 1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
160 state->dynrngcall = NULL;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
161 state->cplba.deltbae = DELTA_BIT_NONE;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
162 state->ba[0].deltbae = state->ba[1].deltbae = state->ba[2].deltbae =
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
163 state->ba[3].deltbae = state->ba[4].deltbae = DELTA_BIT_NONE;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
164
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
165 chaninfo = !acmod;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
166 do {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
167 bitstream_get (5); /* dialnorm */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
168 if (bitstream_get (1)) /* compre */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
169 bitstream_get (8); /* compr */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
170 if (bitstream_get (1)) /* langcode */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
171 bitstream_get (8); /* langcod */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
172 if (bitstream_get (1)) /* audprodie */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
173 bitstream_get (7); /* mixlevel + roomtyp */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
174 } while (chaninfo--);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
175
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
176 bitstream_get (2); /* copyrightb + origbs */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
177
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
178 if (bitstream_get (1)) /* timecod1e */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
179 bitstream_get (14); /* timecod1 */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
180 if (bitstream_get (1)) /* timecod2e */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
181 bitstream_get (14); /* timecod2 */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
182
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
183 if (bitstream_get (1)) { /* addbsie */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
184 int addbsil;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
185
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
186 addbsil = bitstream_get (6);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
187 do {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
188 bitstream_get (8); /* addbsi */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
189 } while (addbsil--);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
190 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
191
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
192 return 0;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
193 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
194
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
195 void a52_dynrng (a52_state_t * state,
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
196 sample_t (* call) (sample_t, void *), void * data)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
197 {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
198 state->dynrnge = 0;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
199 if (call) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
200 state->dynrnge = 1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
201 state->dynrngcall = call;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
202 state->dynrngdata = data;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
203 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
204 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
205
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
206 static int parse_exponents (int expstr, int ngrps, uint8_t exponent,
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
207 uint8_t * dest)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
208 {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
209 int exps;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
210
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
211 while (ngrps--) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
212 exps = bitstream_get (7);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
213
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
214 exponent += exp_1[exps];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
215 if (exponent > 24)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
216 return 1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
217
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
218 switch (expstr) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
219 case EXP_D45:
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
220 *(dest++) = exponent;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
221 *(dest++) = exponent;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
222 case EXP_D25:
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
223 *(dest++) = exponent;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
224 case EXP_D15:
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
225 *(dest++) = exponent;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
226 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
227
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
228 exponent += exp_2[exps];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
229 if (exponent > 24)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
230 return 1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
231
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
232 switch (expstr) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
233 case EXP_D45:
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
234 *(dest++) = exponent;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
235 *(dest++) = exponent;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
236 case EXP_D25:
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
237 *(dest++) = exponent;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
238 case EXP_D15:
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
239 *(dest++) = exponent;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
240 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
241
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
242 exponent += exp_3[exps];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
243 if (exponent > 24)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
244 return 1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
245
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
246 switch (expstr) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
247 case EXP_D45:
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
248 *(dest++) = exponent;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
249 *(dest++) = exponent;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
250 case EXP_D25:
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
251 *(dest++) = exponent;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
252 case EXP_D15:
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
253 *(dest++) = exponent;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
254 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
255 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
256
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
257 return 0;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
258 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
259
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
260 static int parse_deltba (int8_t * deltba)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
261 {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
262 int deltnseg, deltlen, delta, j;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
263
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
264 memset (deltba, 0, 50);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
265
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
266 deltnseg = bitstream_get (3);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
267 j = 0;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
268 do {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
269 j += bitstream_get (5);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
270 deltlen = bitstream_get (4);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
271 delta = bitstream_get (3);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
272 delta -= (delta >= 4) ? 3 : 4;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
273 if (!deltlen)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
274 continue;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
275 if (j + deltlen >= 50)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
276 return 1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
277 while (deltlen--)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
278 deltba[j++] = delta;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
279 } while (deltnseg--);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
280
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
281 return 0;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
282 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
283
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
284 static inline int zero_snr_offsets (int nfchans, a52_state_t * state)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
285 {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
286 int i;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
287
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
288 if ((state->csnroffst) ||
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
289 (state->chincpl && state->cplba.bai >> 3) || /* cplinu, fsnroffst */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
290 (state->lfeon && state->lfeba.bai >> 3)) /* fsnroffst */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
291 return 0;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
292 for (i = 0; i < nfchans; i++)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
293 if (state->ba[i].bai >> 3) /* fsnroffst */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
294 return 0;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
295 return 1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
296 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
297
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
298 static inline int16_t dither_gen (void)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
299 {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
300 static uint16_t lfsr_state = 1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
301 int16_t state;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
302
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
303 state = dither_lut[lfsr_state >> 8] ^ (lfsr_state << 8);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
304
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
305 lfsr_state = (uint16_t) state;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
306
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
307 return state;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
308 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
309
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
310 static void coeff_get (sample_t * coeff, expbap_t * expbap,
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
311 quantizer_t * quantizer, sample_t level,
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
312 int dither, int end)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
313 {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
314 int i;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
315 uint8_t * exp;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
316 int8_t * bap;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
317 sample_t factor[25];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
318
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
319 for (i = 0; i <= 24; i++)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
320 factor[i] = scale_factor[i] * level;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
321
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
322 exp = expbap->exp;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
323 bap = expbap->bap;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
324
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
325 for (i = 0; i < end; i++) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
326 int bapi;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
327
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
328 bapi = bap[i];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
329 switch (bapi) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
330 case 0:
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
331 if (dither) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
332 coeff[i] = dither_gen() * LEVEL_3DB * factor[exp[i]];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
333 continue;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
334 } else {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
335 coeff[i] = 0;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
336 continue;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
337 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
338
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
339 case -1:
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
340 if (quantizer->q1_ptr >= 0) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
341 coeff[i] = quantizer->q1[quantizer->q1_ptr--] * factor[exp[i]];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
342 continue;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
343 } else {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
344 int code;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
345
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
346 code = bitstream_get (5);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
347
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
348 quantizer->q1_ptr = 1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
349 quantizer->q1[0] = q_1_2[code];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
350 quantizer->q1[1] = q_1_1[code];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
351 coeff[i] = q_1_0[code] * factor[exp[i]];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
352 continue;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
353 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
354
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
355 case -2:
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
356 if (quantizer->q2_ptr >= 0) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
357 coeff[i] = quantizer->q2[quantizer->q2_ptr--] * factor[exp[i]];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
358 continue;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
359 } else {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
360 int code;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
361
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
362 code = bitstream_get (7);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
363
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
364 quantizer->q2_ptr = 1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
365 quantizer->q2[0] = q_2_2[code];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
366 quantizer->q2[1] = q_2_1[code];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
367 coeff[i] = q_2_0[code] * factor[exp[i]];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
368 continue;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
369 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
370
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
371 case 3:
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
372 coeff[i] = q_3[bitstream_get (3)] * factor[exp[i]];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
373 continue;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
374
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
375 case -3:
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
376 if (quantizer->q4_ptr == 0) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
377 quantizer->q4_ptr = -1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
378 coeff[i] = quantizer->q4 * factor[exp[i]];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
379 continue;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
380 } else {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
381 int code;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
382
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
383 code = bitstream_get (7);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
384
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
385 quantizer->q4_ptr = 0;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
386 quantizer->q4 = q_4_1[code];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
387 coeff[i] = q_4_0[code] * factor[exp[i]];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
388 continue;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
389 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
390
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
391 case 4:
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
392 coeff[i] = q_5[bitstream_get (4)] * factor[exp[i]];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
393 continue;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
394
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
395 default:
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
396 coeff[i] = ((bitstream_get_2 (bapi) << (16 - bapi)) *
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
397 factor[exp[i]]);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
398 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
399 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
400 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
401
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
402 static void coeff_get_coupling (a52_state_t * state, int nfchans,
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
403 sample_t * coeff, sample_t (* samples)[256],
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
404 quantizer_t * quantizer, uint8_t dithflag[5])
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
405 {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
406 int cplbndstrc, bnd, i, i_end, ch;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
407 uint8_t * exp;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
408 int8_t * bap;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
409 sample_t cplco[5];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
410
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
411 exp = state->cpl_expbap.exp;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
412 bap = state->cpl_expbap.bap;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
413 bnd = 0;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
414 cplbndstrc = state->cplbndstrc;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
415 i = state->cplstrtmant;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
416 while (i < state->cplendmant) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
417 i_end = i + 12;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
418 while (cplbndstrc & 1) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
419 cplbndstrc >>= 1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
420 i_end += 12;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
421 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
422 cplbndstrc >>= 1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
423 for (ch = 0; ch < nfchans; ch++)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
424 cplco[ch] = state->cplco[ch][bnd] * coeff[ch];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
425 bnd++;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
426
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
427 while (i < i_end) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
428 sample_t cplcoeff;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
429 int bapi;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
430
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
431 bapi = bap[i];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
432 switch (bapi) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
433 case 0:
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
434 cplcoeff = LEVEL_3DB * scale_factor[exp[i]];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
435 for (ch = 0; ch < nfchans; ch++)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
436 if ((state->chincpl >> ch) & 1) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
437 if (dithflag[ch])
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
438 samples[ch][i] = (cplcoeff * cplco[ch] *
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
439 dither_gen ());
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
440 else
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
441 samples[ch][i] = 0;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
442 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
443 i++;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
444 continue;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
445
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
446 case -1:
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
447 if (quantizer->q1_ptr >= 0) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
448 cplcoeff = quantizer->q1[quantizer->q1_ptr--];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
449 break;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
450 } else {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
451 int code;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
452
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
453 code = bitstream_get (5);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
454
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
455 quantizer->q1_ptr = 1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
456 quantizer->q1[0] = q_1_2[code];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
457 quantizer->q1[1] = q_1_1[code];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
458 cplcoeff = q_1_0[code];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
459 break;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
460 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
461
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
462 case -2:
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
463 if (quantizer->q2_ptr >= 0) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
464 cplcoeff = quantizer->q2[quantizer->q2_ptr--];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
465 break;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
466 } else {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
467 int code;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
468
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
469 code = bitstream_get (7);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
470
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
471 quantizer->q2_ptr = 1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
472 quantizer->q2[0] = q_2_2[code];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
473 quantizer->q2[1] = q_2_1[code];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
474 cplcoeff = q_2_0[code];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
475 break;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
476 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
477
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
478 case 3:
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
479 cplcoeff = q_3[bitstream_get (3)];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
480 break;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
481
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
482 case -3:
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
483 if (quantizer->q4_ptr == 0) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
484 quantizer->q4_ptr = -1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
485 cplcoeff = quantizer->q4;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
486 break;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
487 } else {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
488 int code;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
489
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
490 code = bitstream_get (7);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
491
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
492 quantizer->q4_ptr = 0;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
493 quantizer->q4 = q_4_1[code];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
494 cplcoeff = q_4_0[code];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
495 break;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
496 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
497
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
498 case 4:
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
499 cplcoeff = q_5[bitstream_get (4)];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
500 break;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
501
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
502 default:
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
503 cplcoeff = bitstream_get_2 (bapi) << (16 - bapi);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
504 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
505
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
506 cplcoeff *= scale_factor[exp[i]];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
507 for (ch = 0; ch < nfchans; ch++)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
508 if ((state->chincpl >> ch) & 1)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
509 samples[ch][i] = cplcoeff * cplco[ch];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
510 i++;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
511 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
512 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
513 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
514
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
515 int a52_block (a52_state_t * state)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
516 {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
517 static const uint8_t nfchans_tbl[] = {2, 1, 2, 3, 3, 4, 4, 5, 1, 1, 2};
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
518 static int rematrix_band[4] = {25, 37, 61, 253};
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
519 int i, nfchans, chaninfo;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
520 uint8_t cplexpstr, chexpstr[5], lfeexpstr, do_bit_alloc, done_cpl;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
521 uint8_t blksw[5], dithflag[5];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
522 sample_t coeff[5];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
523 int chanbias;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
524 quantizer_t quantizer;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
525 sample_t * samples;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
526
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
527 nfchans = nfchans_tbl[state->acmod];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
528
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
529 for (i = 0; i < nfchans; i++)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
530 blksw[i] = bitstream_get (1);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
531
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
532 for (i = 0; i < nfchans; i++)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
533 dithflag[i] = bitstream_get (1);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
534
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
535 chaninfo = !state->acmod;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
536 do {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
537 if (bitstream_get (1)) { /* dynrnge */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
538 int dynrng;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
539
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
540 dynrng = bitstream_get_2 (8);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
541 if (state->dynrnge) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
542 sample_t range;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
543
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
544 range = ((((dynrng & 0x1f) | 0x20) << 13) *
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
545 scale_factor[3 - (dynrng >> 5)]);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
546 if (state->dynrngcall)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
547 range = state->dynrngcall (range, state->dynrngdata);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
548 state->dynrng = state->level * range;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
549 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
550 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
551 } while (chaninfo--);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
552
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
553 if (bitstream_get (1)) { /* cplstre */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
554 state->chincpl = 0;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
555 if (bitstream_get (1)) { /* cplinu */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
556 static uint8_t bndtab[16] = {31, 35, 37, 39, 41, 42, 43, 44,
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
557 45, 45, 46, 46, 47, 47, 48, 48};
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
558 int cplbegf;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
559 int cplendf;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
560 int ncplsubnd;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
561
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
562 for (i = 0; i < nfchans; i++)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
563 state->chincpl |= bitstream_get (1) << i;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
564 switch (state->acmod) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
565 case 0: case 1:
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
566 return 1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
567 case 2:
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
568 state->phsflginu = bitstream_get (1);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
569 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
570 cplbegf = bitstream_get (4);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
571 cplendf = bitstream_get (4);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
572
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
573 if (cplendf + 3 - cplbegf < 0)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
574 return 1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
575 state->ncplbnd = ncplsubnd = cplendf + 3 - cplbegf;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
576 state->cplstrtbnd = bndtab[cplbegf];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
577 state->cplstrtmant = cplbegf * 12 + 37;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
578 state->cplendmant = cplendf * 12 + 73;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
579
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
580 state->cplbndstrc = 0;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
581 for (i = 0; i < ncplsubnd - 1; i++)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
582 if (bitstream_get (1)) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
583 state->cplbndstrc |= 1 << i;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
584 state->ncplbnd--;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
585 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
586 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
587 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
588
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
589 if (state->chincpl) { /* cplinu */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
590 int j, cplcoe;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
591
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
592 cplcoe = 0;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
593 for (i = 0; i < nfchans; i++)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
594 if ((state->chincpl) >> i & 1)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
595 if (bitstream_get (1)) { /* cplcoe */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
596 int mstrcplco, cplcoexp, cplcomant;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
597
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
598 cplcoe = 1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
599 mstrcplco = 3 * bitstream_get (2);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
600 for (j = 0; j < state->ncplbnd; j++) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
601 cplcoexp = bitstream_get (4);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
602 cplcomant = bitstream_get (4);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
603 if (cplcoexp == 15)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
604 cplcomant <<= 14;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
605 else
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
606 cplcomant = (cplcomant | 0x10) << 13;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
607 state->cplco[i][j] =
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
608 cplcomant * scale_factor[cplcoexp + mstrcplco];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
609 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
610 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
611 if ((state->acmod == 2) && state->phsflginu && cplcoe)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
612 for (j = 0; j < state->ncplbnd; j++)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
613 if (bitstream_get (1)) /* phsflg */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
614 state->cplco[1][j] = -state->cplco[1][j];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
615 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
616
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
617 if ((state->acmod == 2) && (bitstream_get (1))) { /* rematstr */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
618 int end;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
619
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
620 state->rematflg = 0;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
621 end = (state->chincpl) ? state->cplstrtmant : 253; /* cplinu */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
622 i = 0;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
623 do
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
624 state->rematflg |= bitstream_get (1) << i;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
625 while (rematrix_band[i++] < end);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
626 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
627
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
628 cplexpstr = EXP_REUSE;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
629 lfeexpstr = EXP_REUSE;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
630 if (state->chincpl) /* cplinu */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
631 cplexpstr = bitstream_get (2);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
632 for (i = 0; i < nfchans; i++)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
633 chexpstr[i] = bitstream_get (2);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
634 if (state->lfeon)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
635 lfeexpstr = bitstream_get (1);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
636
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
637 for (i = 0; i < nfchans; i++)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
638 if (chexpstr[i] != EXP_REUSE) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
639 if ((state->chincpl >> i) & 1)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
640 state->endmant[i] = state->cplstrtmant;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
641 else {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
642 int chbwcod;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
643
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
644 chbwcod = bitstream_get (6);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
645 if (chbwcod > 60)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
646 return 1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
647 state->endmant[i] = chbwcod * 3 + 73;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
648 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
649 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
650
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
651 do_bit_alloc = 0;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
652
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
653 if (cplexpstr != EXP_REUSE) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
654 int cplabsexp, ncplgrps;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
655
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
656 do_bit_alloc = 64;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
657 ncplgrps = ((state->cplendmant - state->cplstrtmant) /
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
658 (3 << (cplexpstr - 1)));
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
659 cplabsexp = bitstream_get (4) << 1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
660 if (parse_exponents (cplexpstr, ncplgrps, cplabsexp,
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
661 state->cpl_expbap.exp + state->cplstrtmant))
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
662 return 1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
663 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
664 for (i = 0; i < nfchans; i++)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
665 if (chexpstr[i] != EXP_REUSE) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
666 int grp_size, nchgrps;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
667
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
668 do_bit_alloc |= 1 << i;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
669 grp_size = 3 << (chexpstr[i] - 1);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
670 nchgrps = (state->endmant[i] + grp_size - 4) / grp_size;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
671 state->fbw_expbap[i].exp[0] = bitstream_get (4);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
672 if (parse_exponents (chexpstr[i], nchgrps,
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
673 state->fbw_expbap[i].exp[0],
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
674 state->fbw_expbap[i].exp + 1))
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
675 return 1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
676 bitstream_get (2); /* gainrng */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
677 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
678 if (lfeexpstr != EXP_REUSE) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
679 do_bit_alloc |= 32;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
680 state->lfe_expbap.exp[0] = bitstream_get (4);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
681 if (parse_exponents (lfeexpstr, 2, state->lfe_expbap.exp[0],
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
682 state->lfe_expbap.exp + 1))
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
683 return 1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
684 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
685
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
686 if (bitstream_get (1)) { /* baie */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
687 do_bit_alloc = -1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
688 state->bai = bitstream_get (11);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
689 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
690 if (bitstream_get (1)) { /* snroffste */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
691 do_bit_alloc = -1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
692 state->csnroffst = bitstream_get (6);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
693 if (state->chincpl) /* cplinu */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
694 state->cplba.bai = bitstream_get (7);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
695 for (i = 0; i < nfchans; i++)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
696 state->ba[i].bai = bitstream_get (7);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
697 if (state->lfeon)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
698 state->lfeba.bai = bitstream_get (7);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
699 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
700 if ((state->chincpl) && (bitstream_get (1))) { /* cplinu, cplleake */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
701 do_bit_alloc |= 64;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
702 state->cplfleak = 9 - bitstream_get (3);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
703 state->cplsleak = 9 - bitstream_get (3);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
704 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
705
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
706 if (bitstream_get (1)) { /* deltbaie */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
707 do_bit_alloc = -1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
708 if (state->chincpl) /* cplinu */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
709 state->cplba.deltbae = bitstream_get (2);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
710 for (i = 0; i < nfchans; i++)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
711 state->ba[i].deltbae = bitstream_get (2);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
712 if (state->chincpl && /* cplinu */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
713 (state->cplba.deltbae == DELTA_BIT_NEW) &&
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
714 parse_deltba (state->cplba.deltba))
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
715 return 1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
716 for (i = 0; i < nfchans; i++)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
717 if ((state->ba[i].deltbae == DELTA_BIT_NEW) &&
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
718 parse_deltba (state->ba[i].deltba))
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
719 return 1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
720 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
721
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
722 if (do_bit_alloc) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
723 if (zero_snr_offsets (nfchans, state)) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
724 memset (state->cpl_expbap.bap, 0, sizeof (state->cpl_expbap.bap));
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
725 for (i = 0; i < nfchans; i++)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
726 memset (state->fbw_expbap[i].bap, 0,
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
727 sizeof (state->fbw_expbap[i].bap));
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
728 memset (state->lfe_expbap.bap, 0, sizeof (state->lfe_expbap.bap));
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
729 } else {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
730 if (state->chincpl && (do_bit_alloc & 64)) /* cplinu */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
731 a52_bit_allocate (state, &state->cplba, state->cplstrtbnd,
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
732 state->cplstrtmant, state->cplendmant,
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
733 state->cplfleak << 8, state->cplsleak << 8,
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
734 &state->cpl_expbap);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
735 for (i = 0; i < nfchans; i++)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
736 if (do_bit_alloc & (1 << i))
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
737 a52_bit_allocate (state, state->ba + i, 0, 0,
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
738 state->endmant[i], 0, 0,
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
739 state->fbw_expbap +i);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
740 if (state->lfeon && (do_bit_alloc & 32)) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
741 state->lfeba.deltbae = DELTA_BIT_NONE;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
742 a52_bit_allocate (state, &state->lfeba, 0, 0, 7, 0, 0,
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
743 &state->lfe_expbap);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
744 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
745 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
746 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
747
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
748 if (bitstream_get (1)) { /* skiple */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
749 i = bitstream_get (9); /* skipl */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
750 while (i--)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
751 bitstream_get (8);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
752 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
753
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
754 samples = state->samples;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
755 if (state->output & A52_LFE)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
756 samples += 256; /* shift for LFE channel */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
757
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
758 chanbias = a52_downmix_coeff (coeff, state->acmod, state->output,
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
759 state->dynrng, state->clev, state->slev);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
760
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
761 quantizer.q1_ptr = quantizer.q2_ptr = quantizer.q4_ptr = -1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
762 done_cpl = 0;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
763
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
764 for (i = 0; i < nfchans; i++) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
765 int j;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
766
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
767 coeff_get (samples + 256 * i, state->fbw_expbap +i, &quantizer,
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
768 coeff[i], dithflag[i], state->endmant[i]);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
769
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
770 if ((state->chincpl >> i) & 1) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
771 if (!done_cpl) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
772 done_cpl = 1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
773 coeff_get_coupling (state, nfchans, coeff,
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
774 (sample_t (*)[256])samples, &quantizer,
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
775 dithflag);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
776 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
777 j = state->cplendmant;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
778 } else
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
779 j = state->endmant[i];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
780 do
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
781 (samples + 256 * i)[j] = 0;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
782 while (++j < 256);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
783 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
784
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
785 if (state->acmod == 2) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
786 int j, end, band, rematflg;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
787
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
788 end = ((state->endmant[0] < state->endmant[1]) ?
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
789 state->endmant[0] : state->endmant[1]);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
790
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
791 i = 0;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
792 j = 13;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
793 rematflg = state->rematflg;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
794 do {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
795 if (! (rematflg & 1)) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
796 rematflg >>= 1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
797 j = rematrix_band[i++];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
798 continue;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
799 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
800 rematflg >>= 1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
801 band = rematrix_band[i++];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
802 if (band > end)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
803 band = end;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
804 do {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
805 sample_t tmp0, tmp1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
806
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
807 tmp0 = samples[j];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
808 tmp1 = (samples+256)[j];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
809 samples[j] = tmp0 + tmp1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
810 (samples+256)[j] = tmp0 - tmp1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
811 } while (++j < band);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
812 } while (j < end);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
813 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
814
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
815 if (state->lfeon) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
816 if (state->output & A52_LFE) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
817 coeff_get (samples - 256, &state->lfe_expbap, &quantizer,
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
818 state->dynrng, 0, 7);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
819 for (i = 7; i < 256; i++)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
820 (samples-256)[i] = 0;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
821 a52_imdct_512 (samples - 256, samples + 1536 - 256, state->bias);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
822 } else {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
823 /* just skip the LFE coefficients */
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
824 coeff_get (samples + 1280, &state->lfe_expbap, &quantizer,
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
825 0, 0, 7);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
826 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
827 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
828
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
829 i = 0;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
830 if (nfchans_tbl[state->output & A52_CHANNEL_MASK] < nfchans)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
831 for (i = 1; i < nfchans; i++)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
832 if (blksw[i] != blksw[0])
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
833 break;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
834
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
835 if (i < nfchans) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
836 if (state->downmixed) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
837 state->downmixed = 0;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
838 a52_upmix (samples + 1536, state->acmod, state->output);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
839 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
840
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
841 for (i = 0; i < nfchans; i++) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
842 sample_t bias;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
843
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
844 bias = 0;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
845 if (!(chanbias & (1 << i)))
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
846 bias = state->bias;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
847
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
848 if (coeff[i]) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
849 if (blksw[i])
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
850 a52_imdct_256 (samples + 256 * i, samples + 1536 + 256 * i,
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
851 bias);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
852 else
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
853 a52_imdct_512 (samples + 256 * i, samples + 1536 + 256 * i,
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
854 bias);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
855 } else {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
856 int j;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
857
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
858 for (j = 0; j < 256; j++)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
859 (samples + 256 * i)[j] = bias;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
860 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
861 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
862
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
863 a52_downmix (samples, state->acmod, state->output, state->bias,
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
864 state->clev, state->slev);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
865 } else {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
866 nfchans = nfchans_tbl[state->output & A52_CHANNEL_MASK];
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
867
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
868 a52_downmix (samples, state->acmod, state->output, 0,
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
869 state->clev, state->slev);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
870
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
871 if (!state->downmixed) {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
872 state->downmixed = 1;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
873 a52_downmix (samples + 1536, state->acmod, state->output, 0,
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
874 state->clev, state->slev);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
875 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
876
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
877 if (blksw[0])
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
878 for (i = 0; i < nfchans; i++)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
879 a52_imdct_256 (samples + 256 * i, samples + 1536 + 256 * i,
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
880 state->bias);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
881 else
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
882 for (i = 0; i < nfchans; i++)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
883 a52_imdct_512 (samples + 256 * i, samples + 1536 + 256 * i,
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
884 state->bias);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
885 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
886
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
887 return 0;
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
888 }
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
889
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
890 void a52_free (a52_state_t * state)
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
891 {
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
892 free (state->samples);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
893 free (state);
207cc56d18f8 * add support for liba52
kabi
parents:
diff changeset
894 }