comparison ac3dec.c @ 5303:b8821ff5c30d libavcodec

AC-3 decoder, soc revision 9, Jun 14 03:52:02 2006 UTC by cloud9 initial commit NOTE: This and all subsequent ac3dec.c revisions from the soc repository violate the GPL. This will be fixed after all soc revisions are imported. This file is not compiled yet, so the license compliance of any builds in the meantime will not be affected.
author jbr
date Sat, 14 Jul 2007 15:38:39 +0000
parents
children eff51058fe13
comparison
equal deleted inserted replaced
5302:689617564c3d 5303:b8821ff5c30d
1 /* AC3 Audio Decoder.
2 *
3 * Copyright (c) 2006 Kartikey Mahendra BHATT (bhattkm at gmail dot com).
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include <stdio.h>
21 #include <stddef.h>
22 #include <math.h>
23 #include <inttypes.h>
24 #include <string.h>
25
26 #define ALT_BITSTREAM_READER
27 #include "ac3_decoder.h"
28 #include "avcodec.h"
29 #include "bitstream.h"
30 #include "dsputil.h"
31 #include "avutil.h"
32
33 static const int sampling_rates[3] = { 32000, 44100, 48000 };
34
35 static const struct
36 {
37 int bit_rate;
38 int frame_sizes[3];
39 } frame_size_table[38] = {
40 { 32, { 96, 69, 64 } },
41 { 32, { 96, 70, 64 } },
42 { 40, { 120, 87, 80 } },
43 { 40, { 120, 88, 80 } },
44 { 48, { 144, 104, 96 } },
45 { 48, { 144, 105, 96 } },
46 { 56, { 168, 121, 112 } },
47 { 56, { 168, 122, 112 } },
48 { 64, { 192, 139, 128 } },
49 { 64, { 192, 140, 128 } },
50 { 80, { 240, 174, 160 } },
51 { 80, { 240, 175, 160 } },
52 { 96, { 288, 208, 192 } },
53 { 96, { 288, 209, 192 } },
54 { 112, { 336, 243, 224 } },
55 { 112, { 336, 244, 224 } },
56 { 128, { 384, 278, 256 } },
57 { 128, { 384, 279, 256 } },
58 { 160, { 480, 348, 320 } },
59 { 160, { 480, 349, 320 } },
60 { 192, { 576, 417, 384 } },
61 { 192, { 576, 418, 384 } },
62 { 224, { 672, 487, 448 } },
63 { 224, { 672, 488, 448 } },
64 { 256, { 768, 557, 512 } },
65 { 256, { 768, 558, 512 } },
66 { 320, { 960, 696, 640 } },
67 { 320, { 960, 697, 640 } },
68 { 384, { 1152, 835, 768 } },
69 { 384, { 1152, 836, 768 } },
70 { 448, { 1344, 975, 896 } },
71 { 448, { 1344, 976, 896 } },
72 { 512, { 1536, 1114, 1024 } },
73 { 512, { 1536, 1115, 1024 } },
74 { 576, { 1728, 1253, 1152 } },
75 { 576, { 1728, 1254, 1152 } },
76 { 640, { 1920, 1393, 1280 } }
77 };
78
79 static int
80 ac3_decode_init (AVCodecContext * avctx)
81 {
82 AC3DecodeContext *ctx = avctx->priv_data;
83
84 ff_mdct_init (&ctx->mdct_ctx_256, 8, 1);
85 ff_mdct_init (&ctx->mdct_ctx_512, 9, 1);
86 ctx->samples = av_mallocz (6 * 6 * 256 * sizeof (float));
87 if (!(ctx->samples))
88 return -1;
89
90 return 0;
91 }
92
93 static int
94 ac3_synchronize (uint8_t * buf, int buf_size)
95 {
96 int i;
97
98 for (i = 0; i < buf_size - 1; i++)
99 if (buf[i] == 0x0b && buf[i + 1] == 0x77)
100 return i;
101
102 return -1;
103 }
104
105 //Returns -1 when 'fscod' is not valid;
106 static int
107 ac3_parse_sync_info (AC3DecodeContext * ctx)
108 {
109 ac3_sync_info *sync_info = &ctx->sync_info;
110 GetBitContext *gb = &ctx->gb;
111
112 sync_info->sync_word = get_bits_long (gb, 16);
113 sync_info->crc1 = get_bits_long (gb, 16);
114 sync_info->fscod = get_bits_long (gb, 2);
115 if (sync_info->fscod == 0x03)
116 return -1;
117 sync_info->frmsizecod = get_bits_long (gb, 6);
118 if (sync_info->frmsizecod >= 0x38)
119 return -1;
120 sync_info->sampling_rate = sampling_rates[sync_info->fscod];
121 sync_info->bit_rate = frame_size_table[sync_info->frmsizecod].bit_rate;
122 sync_info->frame_size = frame_size_table[sync_info->frmsizecod].frame_sizes[sync_info->fscod];
123
124 return 0;
125 }
126
127 static const int nfchans_tbl[8] = { 2, 1, 2, 3, 3, 4, 4, 5 };
128
129 //Returns -1 when
130 static int
131 ac3_parse_bsi (AC3DecodeContext * ctx)
132 {
133 ac3_bsi *bsi = &ctx->bsi;
134 uint32_t *flags = &bsi->flags;
135 GetBitContext *gb = &ctx->gb;
136
137 *flags = 0;
138 bsi->cmixlev = 0;
139 bsi->surmixlev = 0;
140 bsi->dsurmod = 0;
141
142 bsi->bsid = get_bits_long (gb, 5);
143 if (bsi->bsid > 0x08)
144 return -1;
145 bsi->bsmod = get_bits_long (gb, 3);
146 bsi->acmod = get_bits_long (gb, 3);
147 if (bsi->acmod & 0x01 && bsi->acmod != 0x01)
148 bsi->cmixlev = get_bits_long (gb, 2);
149 if (bsi->acmod & 0x04)
150 bsi->surmixlev = get_bits_long (gb, 2);
151 if (bsi->acmod == 0x02)
152 bsi->dsurmod = get_bits_long (gb, 2);
153 if (get_bits_long (gb, 1))
154 *flags |= AC3_BSI_LFEON;
155 bsi->dialnorm = get_bits_long (gb, 5);
156 if (get_bits_long (gb, 1)) {
157 *flags |= AC3_BSI_COMPRE;
158 bsi->compr = get_bits_long (gb, 5);
159 }
160 if (get_bits_long (gb, 1)) {
161 *flags |= AC3_BSI_LANGCODE;
162 bsi->langcod = get_bits_long (gb, 8);
163 }
164 if (get_bits_long (gb, 1)) {
165 *flags |= AC3_BSI_AUDPRODIE;
166 bsi->mixlevel = get_bits_long (gb, 5);
167 bsi->roomtyp = get_bits_long (gb, 2);
168 }
169 if (bsi->acmod == 0x00) {
170 bsi->dialnorm2 = get_bits_long (gb, 5);
171 if (get_bits_long (gb, 1)) {
172 *flags |= AC3_BSI_COMPR2E;
173 bsi->compr2 = get_bits_long (gb, 5);
174 }
175 if (get_bits_long (gb, 1)) {
176 *flags |= AC3_BSI_LANGCOD2E;
177 bsi->langcod2 = get_bits_long (gb, 8);
178 }
179 if (get_bits_long (gb, 1)) {
180 *flags |= AC3_BSI_AUDPRODIE;
181 bsi->mixlevel2 = get_bits_long (gb, 5);
182 bsi->roomtyp2 = get_bits_long (gb, 2);
183 }
184 }
185 if (get_bits_long (gb, 1))
186 *flags |= AC3_BSI_COPYRIGHTB;
187 if (get_bits_long (gb, 1))
188 *flags |= AC3_BSI_ORIGBS;
189 if (get_bits_long (gb, 1)) {
190 *flags |= AC3_BSI_TIMECOD1E;
191 bsi->timecod1 = get_bits_long (gb, 14);
192 }
193 if (get_bits_long (gb, 1)) {
194 *flags |= AC3_BSI_TIMECOD2E;
195 bsi->timecod2 = get_bits_long (gb, 14);
196 }
197 if (get_bits_long (gb, 1)) {
198 *flags |= AC3_BSI_ADDBSIE;
199 bsi->addbsil = get_bits_long (gb, 6);
200 do {
201 get_bits_long (gb, 8);
202 } while (bsi->addbsil--);
203 }
204
205 bsi->nfchans = nfchans_tbl[bsi->acmod];
206 return 0;
207 }
208
209 static int bands[16] =
210 { 31, 35, 37, 39, 41, 42, 43, 44,
211 45, 45, 46, 46, 47, 47, 48, 48 };
212
213 static const int diff_exps_M1[128] =
214 { -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
215 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
216 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
217 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
218 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
219 25, 25, 25 };
220
221 static const int diff_exps_M2[128] =
222 { -2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
223 -2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
224 -2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
225 -2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
226 -2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
227 25, 25, 25 };
228
229 static const int diff_exps_M3[128] =
230 { -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2,
231 -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2,
232 -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2,
233 -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2,
234 -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2,
235 25, 25, 25 };
236
237 /* Decodes the grouped exponents (gexps) and stores them
238 * in decoded exponents (dexps).
239 */
240 static int
241 _decode_exponents (int expstr, int ngrps, uint8_t absexp, uint8_t * gexps, uint8_t * dexps)
242 {
243 int i = 0, exp;
244 while (ngrps--) {
245 exp = gexps[i++];
246
247 absexp += diff_exps_M1[exp];
248 if (absexp > 24)
249 return -1;
250 if (expstr == AC3_EXPSTR_D45) {
251 *(dexps++) = absexp;
252 *(dexps++) = absexp;
253 }
254 else if (expstr == AC3_EXPSTR_D25)
255 *(dexps++) = absexp;
256 else
257 *(dexps++) = absexp;
258
259 absexp += diff_exps_M2[exp];
260 if (absexp > 24)
261 return -1;
262 if (expstr == AC3_EXPSTR_D45) {
263 *(dexps++) = absexp;
264 *(dexps++) = absexp;
265 }
266 else if (expstr == AC3_EXPSTR_D25)
267 *(dexps++) = absexp;
268 else
269 *(dexps++) = absexp;
270
271 absexp += diff_exps_M3[exp];
272 if (absexp > 24)
273 return -1;
274 if (expstr == AC3_EXPSTR_D45) {
275 *(dexps++) = absexp;
276 *(dexps++) = absexp;
277 }
278 else if (expstr == AC3_EXPSTR_D25)
279 *(dexps++) = absexp;
280 else
281 *(dexps++) = absexp;
282 }
283
284 return 0;
285 }
286
287 static int
288 decode_exponents (AC3DecodeContext * ctx)
289 {
290 ac3_audio_block *ab = &ctx->audio_block;
291 int i;
292 uint8_t *exps;
293 uint8_t *dexps;
294
295 if (ab->flags & AC3_AB_CPLINU && ab->cplexpstr != AC3_EXPSTR_REUSE)
296 if (_decode_exponents (ab->cplexpstr, ab->ncplgrps, ab->cplabsexp,
297 ab->cplexps, ab->dcplexps + ab->cplstrtmant))
298 return -1;
299 for (i = 0; i < ctx->bsi.nfchans; i++)
300 if (ab->chexpstr[i] != AC3_EXPSTR_REUSE) {
301 exps = ab->exps[i];
302 dexps = ab->dexps[i];
303 if (_decode_exponents (ab->chexpstr[i], ab->nchgrps[i], exps[0], exps + 1, dexps + 1))
304 return -1;
305 }
306 if (ctx->bsi.flags & AC3_BSI_LFEON && ab->lfeexpstr != AC3_EXPSTR_REUSE)
307 if (_decode_exponents (ab->lfeexpstr, 2, ab->lfeexps[0], ab->lfeexps + 1, ab->dlfeexps))
308 return -1;
309 return 0;
310 }
311
312 static const int16_t slowdec[4] = { 0x0f, 0x11, 0x13, 0x15 }; /* slow decay table */
313 static const int16_t fastdec[4] = { 0x3f, 0x53, 0x67, 0x7b }; /* fast decay table */
314 static const int16_t slowgain[4] = { 0x540, 0x4d8, 0x478, 0x410 }; /* slow gain table */
315 static const int16_t dbpbtab[4] = { 0x000, 0x700, 0x900, 0xb00 }; /* dB/bit table */
316
317 static const int16_t floortab[8] = /* floor table */
318 { 0x2f0, 0x2b0, 0x270, 0x230,
319 0x1f0, 0x170, 0x0f0, 0xf800 };
320
321 static const int16_t fastgain[8] = /* fast gain table */
322 { 0x080, 0x100, 0x180, 0x200,
323 0x280, 0x300, 0x380, 0x400 };
324
325 static const int16_t bndtab[50] = /* start band table */
326 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
327 27, 28, 31, 34, 37, 40, 43, 46, 49, 55, 61, 67, 73, 79, 85, 97, 109, 121, 133, 157, 181, 205, 229 };
328
329 static const int16_t bndsz[50] = /* band size table */
330 { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
331 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 6, 12, 12, 12, 12, 24, 24, 24, 24, 24 };
332
333 static const int16_t masktab[256] = /* masking table */
334 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
335 25, 26, 27, 28, 28, 28, 29, 29, 29, 30, 30, 30, 31, 31, 31, 32, 32, 32, 33, 33, 33, 34, 34, 34, 35,
336 35, 35, 35, 35, 35, 36, 36, 36, 36, 36, 36, 37, 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, 38, 39, 39,
337 39, 39, 39, 39, 40, 40, 40, 40, 40, 40, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 42, 42, 42,
338 42, 42, 42, 42, 42, 42, 42, 42, 42, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 44, 44, 44, 44,
339 44, 44, 44, 44, 44, 44, 44, 44, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45,
340 45, 45, 45, 45, 45, 45, 45, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46,
341 46, 46, 46, 46, 46, 46, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47,
342 47, 47, 47, 47, 47, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
343 48, 48, 48, 48, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49,
344 49, 49, 49, 0, 0, 0 };
345
346 static const int16_t latab[256] = /* log addition table */
347 { 0x0040, 0x003f, 0x003e, 0x003d, 0x003c, 0x003b, 0x003a, 0x0039, 0x0038, 0x0037, 0x0036, 0x0035,
348 0x0034, 0x0034, 0x0033, 0x0032, 0x0031, 0x0030, 0x002f, 0x002f, 0x002e, 0x002d, 0x002c, 0x002c,
349 0x002b, 0x002a, 0x0029, 0x0029, 0x0028, 0x0027, 0x0026, 0x0026, 0x0025, 0x0024, 0x0024, 0x0023,
350 0x0023, 0x0022, 0x0021, 0x0021, 0x0020, 0x0020, 0x001f, 0x001e, 0x001e, 0x001d, 0x001d, 0x001c,
351 0x001c, 0x001b, 0x001b, 0x001a, 0x001a, 0x0019, 0x0019, 0x0018, 0x0018, 0x0017, 0x0017, 0x0016,
352 0x0016, 0x0015, 0x0015, 0x0015, 0x0014, 0x0014, 0x0013, 0x0013, 0x0013, 0x0012, 0x0012, 0x0012,
353 0x0011, 0x0011, 0x0011, 0x0010, 0x0010, 0x0010, 0x000f, 0x000f, 0x000f, 0x000e, 0x000e, 0x000e,
354 0x000d, 0x000d, 0x000d, 0x000d, 0x000c, 0x000c, 0x000c, 0x000c, 0x000b, 0x000b, 0x000b, 0x000b,
355 0x000a, 0x000a, 0x000a, 0x000a, 0x000a, 0x0009, 0x0009, 0x0009, 0x0009, 0x0009, 0x0008, 0x0008,
356 0x0008, 0x0008, 0x0008, 0x0008, 0x0007, 0x0007, 0x0007, 0x0007, 0x0007, 0x0007, 0x0006, 0x0006,
357 0x0006, 0x0006, 0x0006, 0x0006, 0x0006, 0x0006, 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, 0x0005,
358 0x0005, 0x0005, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004,
359 0x0004, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003,
360 0x0003, 0x0003, 0x0003, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002,
361 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0001, 0x0001,
362 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001,
363 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001,
364 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
365 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
366 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
367 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
368 0x0000, 0x0000, 0x0000, 0x0000 };
369
370 static const int16_t hth[3][50] = /* hearing threshold table */
371 {
372 {0x04d0, 0x04d0, 0x0440, 0x0400, 0x03e0, 0x03c0, 0x03b0, 0x03b0, 0x03a0, 0x03a0, 0x03a0, 0x03a0,
373 0x03a0, 0x0390, 0x0390, 0x0390, 0x0380, 0x0380, 0x0370, 0x0370, 0x0360, 0x0360, 0x0350, 0x0350,
374 0x0340, 0x0340, 0x0330, 0x0320, 0x0310, 0x0300, 0x02f0, 0x02f0, 0x02f0, 0x02f0, 0x0300, 0x0310,
375 0x0340, 0x0390, 0x03e0, 0x0420, 0x0460, 0x0490, 0x04a0, 0x0440, 0x0440, 0x0400, 0x0520, 0x0800,
376 0x0840, 0x0840},
377 {0x04f0, 0x04f0, 0x0460, 0x0410, 0x03e0, 0x03d0, 0x03c0, 0x03b0, 0x03b0, 0x03a0, 0x03a0, 0x03a0,
378 0x03a0, 0x03a0, 0x0390, 0x0390, 0x0390, 0x0380, 0x0380, 0x0380, 0x0370, 0x0370, 0x0360, 0x0360,
379 0x0350, 0x0350, 0x0340, 0x0340, 0x0320, 0x0310, 0x0300, 0x02f0, 0x02f0, 0x02f0, 0x02f0, 0x0300,
380 0x0320, 0x0350, 0x0390, 0x03e0, 0x0420, 0x0450, 0x04a0, 0x0490, 0x0460, 0x0440, 0x0480, 0x0630,
381 0x0840, 0x0840},
382 {0x0580, 0x0580, 0x04b0, 0x0450, 0x0420, 0x03f0, 0x03e0, 0x03d0, 0x03c0, 0x03b0, 0x03b0, 0x03b0,
383 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x0390, 0x0390, 0x0390, 0x0390,
384 0x0380, 0x0380, 0x0380, 0x0370, 0x0360, 0x0350, 0x0340, 0x0330, 0x0320, 0x0310, 0x0300, 0x02f0,
385 0x02f0, 0x02f0, 0x0300, 0x0310, 0x0330, 0x0350, 0x03c0, 0x0410, 0x0470, 0x04a0, 0x0460, 0x0440,
386 0x0450, 0x04e0}
387 };
388
389 static const uint8_t baptab[64] = /* bit allocation pointer table */
390 { 0, 1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10,
391 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 15,
392 15, 15, 15, 15, 15, 15, 15, 15 };
393
394 static inline int16_t
395 logadd (int16_t a, int16_t b)
396 {
397 int16_t c = a - b;
398 uint8_t address = FFMIN ((ABS (c) >> 1), 255);
399
400 return ((c >= 0) ? (a + latab[address]) : (b + latab[address]));
401 }
402
403 static inline int16_t
404 calc_lowcomp (int16_t a, int16_t b0, int16_t b1, uint8_t bin)
405 {
406 if (bin < 7) {
407 if ((b0 + 256) == b1)
408 a = 384;
409 else if (b0 > b1)
410 a = FFMAX (0, a - 64);
411 }
412 else if (bin < 20) {
413 if ((b0 + 256) == b1)
414 a = 320;
415 else if (b0 > b1)
416 a = FFMAX (0, a - 64);
417 }
418 else {
419 a = FFMAX (0, a - 128);
420 }
421
422 return a;
423 }
424
425 /* do the bit allocation for chnl.
426 * chnl = 0 to 4 - fbw channel
427 * chnl = 5 coupling channel
428 * chnl = 6 lfe channel
429 */
430 static int
431 _do_bit_allocation (AC3DecodeContext * ctx, int chnl)
432 {
433 ac3_audio_block *ab = &ctx->audio_block;
434 int16_t sdecay, fdecay, sgain, dbknee, floor;
435 int16_t lowcomp, fgain, snroffset, fastleak, slowleak;
436 int16_t psd[256], bndpsd[50], excite[50], mask[50], delta;
437 uint8_t start, end, bin, i, j, k, lastbin, bndstrt, bndend, begin, deltnseg, band, seg, address;
438 uint8_t fscod = ctx->sync_info.fscod;
439 uint8_t *exps, *deltoffst, *deltlen, *deltba;
440 uint8_t *baps;
441 int do_delta = 0;
442
443 /* initialization */
444 sdecay = slowdec[ab->sdcycod];
445 fdecay = fastdec[ab->fdcycod];
446 sgain = slowgain[ab->sgaincod];
447 dbknee = dbpbtab[ab->dbpbcod];
448 floor = dbpbtab[ab->floorcod];
449
450 if (chnl == 5) {
451 start = ab->cplstrtmant;
452 end = ab->cplendmant;
453 fgain = fastgain[ab->cplfgaincod];
454 snroffset = (((ab->csnroffst - 15) << 4) + ab->cplfsnroffst) << 2;
455 fastleak = (ab->cplfleak << 8) + 768;
456 slowleak = (ab->cplsleak << 8) + 768;
457 exps = ab->dcplexps;
458 baps = ab->cplbap;
459 if (ab->cpldeltbae == 0 || ab->cpldeltbae == 1) {
460 do_delta = 1;
461 deltnseg = ab->cpldeltnseg;
462 deltoffst = ab->cpldeltoffst;
463 deltlen = ab->cpldeltlen;
464 deltba = ab->cpldeltba;
465 }
466 }
467 else if (chnl == 6) {
468 start = 0;
469 end = 7;
470 lowcomp = 0;
471 fgain = fastgain[ab->lfefgaincod];
472 snroffset = (((ab->csnroffst - 15) << 4) + ab->lfefsnroffst) << 2;
473 exps = ab->dlfeexps;
474 baps = ab->lfebap;
475 }
476 else {
477 start = 0;
478 end = ab->endmant[chnl];
479 lowcomp = 0;
480 fgain = fastgain[ab->fgaincod[chnl]];
481 snroffset = (((ab->csnroffst - 15) << 4) + ab->fsnroffst[chnl]) << 2;
482 exps = ab->dexps[chnl];
483 baps = ab->bap[chnl];
484 if (ab->deltbae[chnl] == 0 || ab->deltbae[chnl] == 1) {
485 do_delta = 1;
486 deltnseg = ab->deltnseg[chnl];
487 deltoffst = ab->deltoffst[chnl];
488 deltlen = ab->deltlen[chnl];
489 deltba = ab->deltba[chnl];
490 }
491 }
492
493 for (bin = start; bin < end; bin++) /* exponent mapping into psd */
494 psd[bin] = (3072 - ((int16_t) (exps[bin] << 7)));
495
496 /* psd integration */
497 j = start;
498 k = masktab[start];
499 do {
500 lastbin = FFMIN (bndtab[k] + bndsz[k], end);
501 bndpsd[k] = psd[j];
502 j++;
503 for (i = j; i < lastbin; i++) {
504 bndpsd[k] = logadd (bndpsd[k], psd[j]);
505 j++;
506 }
507 k++;
508 } while (end > lastbin);
509
510 /* compute the excite function */
511 bndstrt = masktab[start];
512 bndend = masktab[end - 1] + 1;
513 if (bndstrt == 0) {
514 lowcomp = calc_lowcomp (lowcomp, bndpsd[0], bndpsd[1], 0);
515 excite[0] = bndpsd[0] - fgain - lowcomp;
516 lowcomp = calc_lowcomp (lowcomp, bndpsd[1], bndpsd[2], 1);
517 excite[1] = bndpsd[1] - fgain - lowcomp;
518 begin = 7;
519 for (bin = 2; bin < 7; bin++) {
520 if (bndend != 7 || bin != 6)
521 lowcomp = calc_lowcomp (lowcomp, bndpsd[bin], bndpsd[bin + 1], bin);
522 fastleak = bndpsd[bin] - fgain;
523 slowleak = bndpsd[bin] - sgain;
524 excite[bin] = fastleak - lowcomp;
525 if (bndend != 7 || bin != 6)
526 if (bndpsd[bin] <= bndpsd[bin + 1]) {
527 begin = bin + 1;
528 break;
529 }
530 }
531 for (bin = begin; bin < (FFMIN (bndend, 22)); bin++) {
532 if (bndend != 7 || bin != 6)
533 lowcomp = calc_lowcomp (lowcomp, bndpsd[bin], bndpsd[bin + 1], bin);
534 fastleak -= fdecay;
535 fastleak = FFMAX (fastleak, bndpsd[bin] - fgain);
536 slowleak -= sdecay;
537 slowleak = FFMAX (slowleak, bndpsd[bin] - sgain);
538 excite[bin] = FFMAX (fastleak - lowcomp, slowleak);
539 }
540 begin = 22;
541 }
542 else {
543 begin = bndstrt;
544 }
545 for (bin = begin; bin < bndend; bin++) {
546 fastleak -= fdecay;
547 fastleak = FFMAX (fastleak, bndpsd[bin] - fgain);
548 slowleak -= sdecay;
549 slowleak = FFMAX (slowleak, bndpsd[bin] - sgain);
550 excite[bin] = FFMAX (fastleak, slowleak);
551 }
552
553 /* compute the masking curve */
554 for (bin = bndstrt; bin < bndend; bin++) {
555 if (bndpsd[bin] < dbknee)
556 excite[bin] += ((dbknee - bndpsd[bin]) >> 2);
557 mask[bin] = FFMAX (excite[bin], hth[fscod][bin]);
558 }
559
560 /* apply the delta bit allocation */
561 if (do_delta) {
562 band = 0;
563 for (seg = 0; seg < deltnseg + 1; seg++) {
564 band += deltoffst[seg];
565 if (deltba[seg] >= 4)
566 delta = (deltba[seg] - 3) << 7;
567 else
568 delta = (deltba[seg] - 4) << 7;
569 for (k = 0; k < deltlen[seg]; k++) {
570 mask[band] += delta;
571 band++;
572 }
573 }
574 }
575
576 /*compute the bit allocation */
577 i = start;
578 j = masktab[start];
579 do {
580 lastbin = FFMIN (bndtab[j] + bndsz[j], end);
581 mask[j] -= snroffset;
582 mask[j] -= floor;
583 if (mask[j] < 0)
584 mask[j] = 0;
585 mask[j] &= 0x1fe0;
586 mask[j] += floor;
587 for (k = i; k < lastbin; k++) {
588 address = (psd[i] - mask[j]) >> 5;
589 address = FFMIN (63, (FFMAX (0, address)));
590 baps[i] = baptab[address];
591 i++;
592 }
593 j++;
594 } while (end > lastbin);
595
596 return 0;
597 }
598
599 static int
600 do_bit_allocation (AC3DecodeContext * ctx, int flags)
601 {
602 ac3_audio_block *ab = &ctx->audio_block;
603 int i, snroffst = 0;
604
605 if (!flags) /* bit allocation is not required */
606 return 0;
607
608 if (ab->flags & AC3_AB_SNROFFSTE) { /* check whether snroffsts are zero */
609 snroffst += ab->csnroffst;
610 if (ab->flags & AC3_AB_CPLINU)
611 snroffst += ab->cplfsnroffst;
612 for (i = 0; i < ctx->bsi.nfchans; i++)
613 snroffst += ab->fsnroffst[i];
614 if (ctx->bsi.flags & AC3_BSI_LFEON)
615 snroffst += ab->lfefsnroffst;
616 if (!snroffst) {
617 memset (ab->cplbap, 0, sizeof (ab->cplbap));
618 for (i = 0; i < ctx->bsi.nfchans; i++)
619 memset (ab->bap[i], 0, sizeof (ab->bap[i]));
620 memset (ab->lfebap, 0, sizeof (ab->lfebap));
621
622 return 0;
623 }
624 }
625
626 /* perform bit allocation */
627 if ((ab->flags & AC3_AB_CPLINU) && (flags & 64))
628 if (_do_bit_allocation (ctx, 5))
629 return -1;
630 for (i = 0; i < ctx->bsi.nfchans; i++)
631 if (flags & (1 << i))
632 if (_do_bit_allocation (ctx, i))
633 return -1;
634 if ((ctx->bsi.flags & AC3_BSI_LFEON) && (flags & 32))
635 if (_do_bit_allocation (ctx, 6))
636 return -1;
637
638 return 0;
639 }
640
641 /* table for exponent to scale_factor mapping
642 * scale_factor[i] = 2 ^ -(i + 15)
643 */
644 static const float scale_factors[25] = {
645 0.000030517578125000000000000000000000000,
646 0.000015258789062500000000000000000000000,
647 0.000007629394531250000000000000000000000,
648 0.000003814697265625000000000000000000000,
649 0.000001907348632812500000000000000000000,
650 0.000000953674316406250000000000000000000,
651 0.000000476837158203125000000000000000000,
652 0.000000238418579101562500000000000000000,
653 0.000000119209289550781250000000000000000,
654 0.000000059604644775390625000000000000000,
655 0.000000029802322387695312500000000000000,
656 0.000000014901161193847656250000000000000,
657 0.000000007450580596923828125000000000000,
658 0.000000003725290298461914062500000000000,
659 0.000000001862645149230957031250000000000,
660 0.000000000931322574615478515625000000000,
661 0.000000000465661287307739257812500000000,
662 0.000000000232830643653869628906250000000,
663 0.000000000116415321826934814453125000000,
664 0.000000000058207660913467407226562500000,
665 0.000000000029103830456733703613281250000,
666 0.000000000014551915228366851806640625000,
667 0.000000000007275957614183425903320312500,
668 0.000000000003637978807091712951660156250,
669 0.000000000001818989403545856475830078125
670 };
671
672 static const int16_t l3_q_tab[3] = { /* 3-level quantization table */
673 (-2 << 15) / 3, 0, (2 << 15) / 3
674 };
675
676 static const int16_t l5_q_tab[5] = { /* 5-level quantization table */
677 (-4 << 15) / 5, (-2 << 15) / 5, 0, (2 << 15) / 5, (4 << 15) / 5
678 };
679
680 static const int16_t l7_q_tab[7] = { /* 7-level quantization table */
681 (-6 << 15) / 7, (-4 << 15) / 7, (-2 << 15) / 7, 0,
682 (2 << 15) / 7, (4 << 15) / 7, (6 << 15) / 7
683 };
684
685 static const int16_t l11_q_tab[11] = { /* 11-level quantization table */
686 (-10 << 15) / 11, (-8 << 15) / 11, (-6 << 15) / 11, (-4 << 15) / 11, (-2 << 15) / 11, 0,
687 (2 << 15) / 11, (4 << 15) / 11, (6 << 15) / 11, (8 << 15) / 11, (10 << 15) / 11
688 };
689
690 static const int16_t l15_q_tab[15] = { /* 15-level quantization table */
691 (-14 << 15) / 15, (-12 << 15) / 15, (-10 << 15) / 15, (-8 << 15) / 15,
692 (-6 << 15) / 15, (-4 << 15) / 15, (-2 << 15) / 15, 0,
693 (2 << 15) / 15, (4 << 15) / 15, (6 << 15) / 15, (8 << 15) / 15,
694 (10 << 15) / 15, (12 << 15) / 15, (14 << 15) / 15
695 };
696
697 static const uint8_t qntztab[16] = { 0, 5, 7, 3, 7, 4, 5, 6, 7, 8, 9, 10, 12, 12, 14, 16 };
698
699 static inline float
700 to_float (uint8_t exp, int16_t mantissa)
701 {
702 return ((float) (mantissa * scale_factors[exp]));
703 }
704
705 typedef struct
706 { /* grouped mantissas for 3-level 5-leve and 11-level quantization */
707 uint8_t gcodes[3];
708 uint8_t gcptr;
709 } mant_group;
710
711 /* Get the transform coefficients for particular channel */
712 static int
713 _get_transform_coeffs (uint8_t * exps, uint8_t * bap, float *samples,
714 int start, int end, int dith_flag, GetBitContext * gb)
715 {
716 int16_t mantissa;
717 int i;
718 int gcode;
719 mant_group l3_grp, l5_grp, l11_grp;
720
721 for (i = 0; i < 3; i++)
722 l3_grp.gcodes[i] = l5_grp.gcodes[i] = l11_grp.gcodes[i] = -1;
723 l3_grp.gcptr = l5_grp.gcptr = 3;
724 l11_grp.gcptr = 2;
725
726 i = 0;
727 while (i < start)
728 samples[i++] = 0;
729
730 for (i = start; i < end; i++) {
731 switch (bap[i]) {
732 case 0:
733 if (!dith_flag)
734 mantissa = 0;
735 else
736 mantissa = gen_dither ();
737 samples[i] = to_float (exps[i], mantissa);
738 break;
739
740 case 1:
741 if (l3_grp.gcptr > 2) {
742 gcode = get_bits_long (gb, qntztab[1]);
743 if (gcode > 26)
744 return -1;
745 l3_grp.gcodes[0] = gcode / 9;
746 l3_grp.gcodes[1] = (gcode % 9) / 3;
747 l3_grp.gcodes[2] = (gcode % 9) % 3;
748 l3_grp.gcptr = 0;
749 }
750 mantissa = l3_q_tab[l3_grp.gcodes[l3_grp.gcptr++]];
751 samples[i] = to_float (exps[i], mantissa);
752 break;
753
754 case 2:
755 if (l5_grp.gcptr > 2) {
756 gcode = get_bits_long (gb, qntztab[2]);
757 if (gcode > 124)
758 return -1;
759 l5_grp.gcodes[0] = gcode / 25;
760 l5_grp.gcodes[1] = (gcode % 25) / 5;
761 l5_grp.gcodes[2] = (gcode % 25) % 5;
762 l5_grp.gcptr = 0;
763 }
764 mantissa = l5_q_tab[l5_grp.gcodes[l5_grp.gcptr++]];
765 samples[i] = to_float (exps[i], mantissa);
766 break;
767
768 case 3:
769 mantissa = get_bits_long (gb, qntztab[3]);
770 if (mantissa > 6)
771 return -1;
772 mantissa = l7_q_tab[mantissa];
773 samples[i] = to_float (exps[i], mantissa);
774 break;
775
776 case 4:
777 if (l11_grp.gcptr > 1) {
778 gcode = get_bits_long (gb, qntztab[4]);
779 if (gcode > 120)
780 return -1;
781 l11_grp.gcodes[0] = gcode / 11;
782 l11_grp.gcodes[1] = gcode % 11;
783 }
784 mantissa = l11_q_tab[l11_grp.gcodes[l11_grp.gcptr++]];
785 samples[i] = to_float (exps[i], mantissa);
786 break;
787
788 case 5:
789 mantissa = get_bits_long (gb, qntztab[5]);
790 if (mantissa > 14)
791 return -1;
792 mantissa = l15_q_tab[mantissa];
793 break;
794
795 default:
796 mantissa = get_bits_long (gb, qntztab[bap[i]]) << (16 - qntztab[bap[i]]);
797 samples[i] = to_float (exps[i], mantissa);
798 break;
799 }
800 }
801
802 i = end;
803 while (i < 256)
804 samples[i++] = 0;
805
806 return 0;
807 }
808
809 static int
810 uncouple_channels (AC3DecodeContext * ctx)
811 {
812 ac3_audio_block *ab = &ctx->audio_block;
813 int ch, sbnd, bin;
814 int index;
815 float (*samples)[256];
816 int16_t mantissa;
817
818 samples = (float (*)[256]) (ab->ab_samples);
819 samples += (ctx->bsi.flags & AC3_BSI_LFEON) ? 256 : 0;
820
821 /* uncouple channels */
822 for (ch = 0; ch < ctx->bsi.nfchans; ch++)
823 if (ab->chincpl & (1 << ch))
824 for (sbnd = ab->cplbegf; sbnd < 3 + ab->cplendf; sbnd++)
825 for (bin = 0; bin < 12; bin++) {
826 index = sbnd * 12 + bin + 37;
827 samples[ch][index] = ab->cplcoeffs[index] * ab->cplco[ch][sbnd] * 8;
828 }
829
830 /* generate dither if required */
831 for (ch = 0; ch < ctx->bsi.nfchans; ch++)
832 if ((ab->chincpl & (1 << ch)) && (ab->dithflag & (1 << ch)))
833 for (index = 0; index < ab->endmant[ch]; index++)
834 if (!ab->bap[ch][index]) {
835 mantissa = gen_dither ();
836 samples[ch][index] = to_float (ab->dexps[ch][index], mantissa);
837 }
838
839 return 0;
840 }
841
842 static int
843 get_transform_coeffs (AC3DecodeContext * ctx)
844 {
845 int i;
846 ac3_audio_block *ab = &ctx->audio_block;
847 float *samples = ab->ab_samples;
848 int got_cplchan = 0;
849 int dithflag = 0;
850
851 samples += (ctx->bsi.flags & AC3_BSI_LFEON) ? 256 : 0;
852 for (i = 0; i < ctx->bsi.nfchans; i++) {
853 if ((ab->flags & AC3_AB_CPLINU) && (ab->chincpl & (1 << i)))
854 dithflag = 0; /* don't generate dither until channels are decoupled */
855 else
856 dithflag = ab->dithflag & (1 << i);
857 /* transform coefficients for individual channel */
858 if (_get_transform_coeffs (ab->dexps[i], ab->bap[i], samples + (i * 256),
859 0, ab->endmant[i], dithflag, &ctx->gb))
860 return -1;
861 /* tranform coefficients for coupling channels */
862 if ((ab->flags & AC3_AB_CPLINU) && (ab->chincpl & (1 << i)) && !got_cplchan) {
863 if (_get_transform_coeffs (ab->dcplexps, ab->cplbap, ab->cplcoeffs,
864 ab->cplstrtmant, ab->cplendmant, 0, &ctx->gb))
865 return -1;
866 got_cplchan = 1;
867 }
868 }
869
870 /* uncouple the channels from the coupling channel */
871 if (ab->flags & AC3_AB_CPLINU)
872 if (uncouple_channels (ctx))
873 return -1;
874
875 return 0;
876 }
877
878 /* generate coupling co-ordinates for each coupling subband
879 * from coupling co-ordinates of each band and coupling band
880 * structure information
881 */
882 static int
883 generate_coupling_coordinates (AC3DecodeContext * ctx)
884 {
885 ac3_audio_block *ab = &ctx->audio_block;
886 uint8_t exp, mstrcplco;
887 int16_t mant;
888 uint32_t cplbndstrc = (1 << ab->ncplsubnd) >> 1;
889 int ch, bnd, sbnd;
890 float cplco;
891
892 if (ab->cplcoe)
893 for (ch = 0; ch < ctx->bsi.nfchans; ch++)
894 if (ab->cplcoe & (1 << ch)) {
895 mstrcplco = 3 * ab->mstrcplco[ch];
896 sbnd = ab->cplbegf;
897 for (bnd = 0; bnd < ab->ncplbnd; bnd++) {
898 exp = ab->cplcoexp[ch][bnd];
899 if (exp == 15)
900 mant = ab->cplcomant[ch][bnd] <<= 14;
901 else
902 mant = (ab->cplcomant[ch][bnd] | 0x10) << 13;
903 cplco = to_float (exp + mstrcplco, mant);
904 if (ctx->bsi.acmod == 0x02 && (ab->flags & AC3_AB_PHSFLGINU) && ch == 1
905 && (ab->phsflg & (1 << bnd)))
906 cplco = -cplco; /* invert the right channel */
907 ab->cplco[ch][sbnd++] = cplco;
908 while (cplbndstrc & ab->cplbndstrc) {
909 cplbndstrc >>= 1;
910 ab->cplco[ch][sbnd++] = cplco;
911 }
912 cplbndstrc >>= 1;
913 }
914 }
915
916 return 0;
917 }
918
919 static int
920 ac3_parse_audio_block (AC3DecodeContext * ctx, int index)
921 {
922 ac3_audio_block *ab = &ctx->audio_block;
923 int nfchans = ctx->bsi.nfchans;
924 int acmod = ctx->bsi.acmod;
925 int i, bnd, rbnd, grp, seg;
926 GetBitContext *gb = &ctx->gb;
927 uint32_t *flags = &ab->flags;
928 int bit_alloc_flags = 0;
929
930 *flags = 0;
931 ab->blksw = 0;
932 for (i = 0; i < nfchans; i++) /*block switch flag */
933 ab->blksw |= get_bits_long (gb, 1) << i;
934 ab->dithflag = 0;
935 for (i = 0; i < nfchans; i++) /* dithering flag */
936 ab->dithflag |= get_bits_long (gb, 1) << i;
937 if (get_bits_long (gb, 1)) { /* dynamic range */
938 *flags |= AC3_AB_DYNRNGE;
939 ab->dynrng = get_bits_long (gb, 8);
940 }
941 if (acmod == 0x00) { /* dynamic range 1+1 mode */
942 if (get_bits_long (gb, 1)) {
943 *flags |= AC3_AB_DYNRNG2E;
944 ab->dynrng2 = get_bits_long (gb, 8);
945 }
946 }
947 ab->chincpl = 0;
948 if (get_bits_long (gb, 1)) { /* coupling strategy */
949 *flags |= AC3_AB_CPLSTRE;
950 ab->cplbndstrc = 0;
951 if (get_bits_long (gb, 1)) { /* coupling in use */
952 *flags |= AC3_AB_CPLINU;
953 for (i = 0; i < nfchans; i++)
954 ab->chincpl |= get_bits_long (gb, 1) << i;
955 if (acmod == 0x02)
956 if (get_bits_long (gb, 1)) /* phase flag in use */
957 *flags |= AC3_AB_PHSFLGINU;
958 ab->cplbegf = get_bits_long (gb, 4);
959 ab->cplendf = get_bits_long (gb, 4);
960 if ((ab->ncplsubnd = 3 + ab->cplendf - ab->cplbegf) < 0)
961 return -1;
962 ab->ncplbnd = ab->ncplsubnd;
963 for (i = 0; i < ab->ncplsubnd - 1; i++) /* coupling band structure */
964 if (get_bits_long (gb, 1)) {
965 ab->cplbndstrc |= 1 << i;
966 ab->ncplbnd--;
967 }
968 }
969 }
970 if (*flags & AC3_AB_CPLINU) {
971 ab->cplcoe = 0;
972 for (i = 0; i < nfchans; i++)
973 if (ab->chincpl & (1 << i))
974 if (get_bits_long (gb, 1)) { /* coupling co-ordinates */
975 ab->cplcoe |= 1 << i;
976 ab->mstrcplco[i] = get_bits_long (gb, 2);
977 for (bnd = 0; bnd < ab->ncplbnd; bnd++) {
978 ab->cplcoexp[i][bnd] = get_bits_long (gb, 4);
979 ab->cplcomant[i][bnd] = get_bits_long (gb, 4);
980 }
981 }
982 }
983 ab->phsflg = 0;
984 if ((acmod == 0x02) && (*flags & AC3_AB_PHSFLGINU) && (ab->cplcoe & 1 || ab->cplcoe & (1 << 1))) {
985 for (bnd = 0; bnd < ab->ncplbnd; bnd++)
986 if (get_bits_long (gb, 1))
987 ab->phsflg |= 1 << bnd;
988 }
989 generate_coupling_coordinates (ctx);
990 ab->rematflg = 0;
991 if (acmod == 0x02) /* rematrixing */
992 if (get_bits_long (gb, 1)) {
993 *flags |= AC3_AB_REMATSTR;
994 if (ab->cplbegf > 2 || !(*flags & AC3_AB_CPLINU))
995 for (rbnd = 0; rbnd < 4; rbnd++)
996 ab->rematflg |= get_bits_long (gb, 1) << bnd;
997 else if (ab->cplbegf > 0 && ab->cplbegf <= 2 && *flags & AC3_AB_CPLINU)
998 for (rbnd = 0; rbnd < 3; rbnd++)
999 ab->rematflg |= get_bits_long (gb, 1) << bnd;
1000 else if (!(ab->cplbegf) && *flags & AC3_AB_CPLINU)
1001 for (rbnd = 0; rbnd < 2; rbnd++)
1002 ab->rematflg |= get_bits_long (gb, 1) << bnd;
1003 }
1004 if (*flags & AC3_AB_CPLINU) /* coupling exponent strategy */
1005 ab->cplexpstr = get_bits_long (gb, 2);
1006 for (i = 0; i < nfchans; i++) /* channel exponent strategy */
1007 ab->chexpstr[i] = get_bits_long (gb, 2);
1008 if (ctx->bsi.flags & AC3_BSI_LFEON) /* lfe exponent strategy */
1009 ab->lfeexpstr = get_bits_long (gb, 1);
1010 for (i = 0; i < nfchans; i++) /* channel bandwidth code */
1011 if (ab->chexpstr[i] != AC3_EXPSTR_REUSE)
1012 if (!(ab->chincpl & (1 << i))) {
1013 ab->chbwcod[i] = get_bits_long (gb, 6);
1014 if (ab->chbwcod[i] > 60)
1015 return -1;
1016 }
1017 if (*flags & AC3_AB_CPLINU)
1018 if (ab->cplexpstr != AC3_EXPSTR_REUSE) {/* coupling exponents */
1019 bit_alloc_flags |= 64;
1020 ab->cplabsexp = get_bits_long (gb, 4) << 1;
1021 ab->cplstrtmant = (ab->cplbegf * 12) + 37;
1022 ab->cplendmant = ((ab->cplendmant + 3) * 12) + 37;
1023 ab->ncplgrps = (ab->cplendmant - ab->cplstrtmant) / (3 << (ab->cplexpstr - 1));
1024 for (grp = 0; grp < ab->ncplgrps; grp++)
1025 ab->cplexps[grp] = get_bits_long (gb, 7);
1026 }
1027 for (i = 0; i < nfchans; i++) /* fbw channel exponents */
1028 if (ab->chexpstr[i] != AC3_EXPSTR_REUSE) {
1029 bit_alloc_flags |= 1 << i;
1030 if (ab->chincpl & (1 << i))
1031 ab->endmant[i] = (ab->cplbegf * 12) + 37;
1032 else
1033 ab->endmant[i] = ((ab->chbwcod[i] + 3) * 12) + 37;
1034 ab->nchgrps[i] =
1035 (ab->endmant[i] + (3 << (ab->chexpstr[i] - 1)) - 4) / (3 << (ab->chexpstr[i] - 1));
1036 ab->exps[i][0] = ab->dexps[i][0] = get_bits_long (gb, 4);
1037 for (grp = 1; grp <= ab->nchgrps[i]; grp++)
1038 ab->exps[i][grp] = get_bits_long (gb, 7);
1039 ab->gainrng[i] = get_bits_long (gb, 2);
1040 }
1041 if (ctx->bsi.flags & AC3_BSI_LFEON) /* lfe exponents */
1042 if (ab->lfeexpstr != AC3_EXPSTR_REUSE) {
1043 bit_alloc_flags |= 32;
1044 ab->lfeexps[0] = ab->dlfeexps[0] = get_bits_long (gb, 4);
1045 ab->lfeexps[1] = get_bits_long (gb, 7);
1046 ab->lfeexps[2] = get_bits_long (gb, 7);
1047 }
1048 if (decode_exponents (ctx)) /* decode the exponents for this block */
1049 return -1;
1050 if (get_bits_long (gb, 1)) { /* bit allocation information */
1051 *flags |= AC3_AB_BAIE;
1052 bit_alloc_flags |= 127;
1053 ab->sdcycod = get_bits_long (gb, 2);
1054 ab->fdcycod = get_bits_long (gb, 2);
1055 ab->sgaincod = get_bits_long (gb, 2);
1056 ab->dbpbcod = get_bits_long (gb, 2);
1057 ab->floorcod = get_bits_long (gb, 3);
1058 }
1059 if (get_bits_long (gb, 1)) { /* snroffset */
1060 *flags |= AC3_AB_SNROFFSTE;
1061 bit_alloc_flags |= 127;
1062 ab->csnroffst = get_bits_long (gb, 6);
1063 if (*flags & AC3_AB_CPLINU) { /* couling fine snr offset and fast gain code */
1064 ab->cplfsnroffst = get_bits_long (gb, 4);
1065 ab->cplfgaincod = get_bits_long (gb, 3);
1066 }
1067 for (i = 0; i < nfchans; i++) { /* channel fine snr offset and fast gain code */
1068 ab->fsnroffst[i] = get_bits_long (gb, 4);
1069 ab->fgaincod[i] = get_bits_long (gb, 3);
1070 }
1071 if (ctx->bsi.flags & AC3_BSI_LFEON) { /* lfe fine snr offset and fast gain code */
1072 ab->lfefsnroffst = get_bits_long (gb, 4);
1073 ab->lfefgaincod = get_bits_long (gb, 3);
1074 }
1075 }
1076 if (*flags & AC3_AB_CPLINU)
1077 if (get_bits_long (gb, 1)) { /* coupling leak information */
1078 bit_alloc_flags |= 64;
1079 *flags |= AC3_AB_CPLLEAKE;
1080 ab->cplfleak = get_bits_long (gb, 3);
1081 ab->cplsleak = get_bits_long (gb, 3);
1082 }
1083 if (get_bits_long (gb, 1)) { /* delta bit allocation information */
1084 *flags |= AC3_AB_DELTBAIE;
1085 bit_alloc_flags |= 127;
1086 if (*flags & AC3_AB_CPLINU) {
1087 ab->cpldeltbae = get_bits_long (gb, 2);
1088 if (ab->cpldeltbae == AC3_DBASTR_RESERVED)
1089 return -1;
1090 }
1091 for (i = 0; i < nfchans; i++) {
1092 ab->deltbae[i] = get_bits_long (gb, 2);
1093 if (ab->deltbae[i] == AC3_DBASTR_RESERVED)
1094 return -1;
1095 }
1096 if (*flags & AC3_AB_CPLINU)
1097 if (ab->cpldeltbae == AC3_DBASTR_NEW) { /*coupling delta offset, len and bit allocation */
1098 ab->cpldeltnseg = get_bits_long (gb, 3);
1099 for (seg = 0; seg <= ab->cpldeltnseg; seg++) {
1100 ab->cpldeltoffst[seg] = get_bits_long (gb, 5);
1101 ab->cpldeltlen[seg] = get_bits_long (gb, 4);
1102 ab->cpldeltba[seg] = get_bits_long (gb, 3);
1103 }
1104 }
1105 for (i = 0; i < nfchans; i++)
1106 if (ab->deltbae[i] == AC3_DBASTR_NEW) {/*channel delta offset, len and bit allocation */
1107 ab->deltnseg[i] = get_bits_long (gb, 3);
1108 for (seg = 0; seg <= ab->deltnseg[i]; seg++) {
1109 ab->deltoffst[i][seg] = get_bits_long (gb, 5);
1110 ab->deltlen[i][seg] = get_bits_long (gb, 4);
1111 ab->deltba[i][seg] = get_bits_long (gb, 3);
1112 }
1113 }
1114 }
1115 if (do_bit_allocation (ctx, bit_alloc_flags)) /* perform the bit allocation */
1116 return -1;
1117 if (get_bits_long (gb, 1)) { /* unused dummy data */
1118 *flags |= AC3_AB_SKIPLE;
1119 ab->skipl = get_bits_long (gb, 9);
1120 while (ab->skipl) {
1121 get_bits_long (gb, 8);
1122 ab->skipl--;
1123 }
1124 }
1125 /* point ab_samples to the right place within smaples */
1126 if (!index)
1127 ab->ab_samples = ctx->samples;
1128 else {
1129 ab->ab_samples = ctx->samples + (i * nfchans * 256);
1130 ab->ab_samples += ((ctx->bsi.flags & AC3_BSI_LFEON) ? 256 : 0);
1131 }
1132 /* unpack the transform coefficients
1133 * this also uncouples channels if coupling is in use.
1134 */
1135 if (get_transform_coeffs (ctx))
1136 return -1;
1137
1138 return 0;
1139 }
1140
1141
1142 static int
1143 ac3_decode_frame (AVCodecContext * avctx, void *data, int *data_size, uint8_t * buf, int buf_size)
1144 {
1145 AC3DecodeContext *ctx = avctx->priv_data;
1146 int frame_start;
1147 int i;
1148
1149 //Synchronize the frame.
1150 frame_start = ac3_synchronize (buf, buf_size);
1151 if (frame_start == -1) {
1152 *data_size = 0;
1153 return -1;
1154 }
1155
1156 //Initialize the GetBitContext with the start of valid AC3 Frame.
1157 init_get_bits (&(ctx->gb), buf + frame_start, (buf_size - frame_start) * 8);
1158
1159 //Parse the syncinfo.
1160 //If 'fscod' is not valid the decoder shall mute as per the standard.
1161 if (ac3_parse_sync_info (ctx)) {
1162 *data_size = 0;
1163 return -1;
1164 }
1165
1166 //Check for the errors.
1167 /*if (ac3_error_check(ctx))
1168 {
1169 *data_size = 0;
1170 return -1;
1171 } */
1172
1173 //Parse the BSI.
1174 //If 'bsid' is not valid decoder shall not decode the audio as per the standard.
1175 if (ac3_parse_bsi (ctx)) {
1176 *data_size = 0;
1177 return -1;
1178 }
1179
1180 //Parse the Audio Blocks.
1181 for (i = 0; i < 6; i++)
1182 if (ac3_parse_audio_block (ctx, i)) {
1183 *data_size = 0;
1184 return -1;
1185 }
1186
1187 return 0;
1188 }