comparison eac3dec.c @ 7745:b09973d487e6 libavcodec

commit more OKed parts of the E-AC-3 decoder
author jbr
date Sat, 30 Aug 2008 23:13:10 +0000
parents e27395cc7321
children f9bd775992d4
comparison
equal deleted inserted replaced
7744:7477cbdacb20 7745:b09973d487e6
76 pre_mant[2] = even2 + odd2; 76 pre_mant[2] = even2 + odd2;
77 pre_mant[3] = even2 - odd2; 77 pre_mant[3] = even2 - odd2;
78 pre_mant[4] = even1 - odd1; 78 pre_mant[4] = even1 - odd1;
79 pre_mant[5] = even0 - odd0; 79 pre_mant[5] = even0 - odd0;
80 } 80 }
81
82 void ff_eac3_decode_transform_coeffs_aht_ch(AC3DecodeContext *s, int ch)
83 {
84 int bin, blk, gs;
85 int end_bap, gaq_mode;
86 GetBitContext *gbc = &s->gbc;
87 int gaq_gain[AC3_MAX_COEFS];
88
89 gaq_mode = get_bits(gbc, 2);
90 end_bap = (gaq_mode < 2) ? 12 : 17;
91
92 /* if GAQ gain is used, decode gain codes for bins with hebap between
93 8 and end_bap */
94 gs = 0;
95 if (gaq_mode == EAC3_GAQ_12 || gaq_mode == EAC3_GAQ_14) {
96 /* read 1-bit GAQ gain codes */
97 for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
98 if (s->bap[ch][bin] > 7 && s->bap[ch][bin] < end_bap)
99 gaq_gain[gs++] = get_bits1(gbc) << (gaq_mode-1);
100 }
101 } else if (gaq_mode == EAC3_GAQ_124) {
102 /* read 1.67-bit GAQ gain codes (3 codes in 5 bits) */
103 int gc = 2;
104 for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
105 if (s->bap[ch][bin] > 7 && s->bap[ch][bin] < 17) {
106 if (gc++ == 2) {
107 int group_code = get_bits(gbc, 5);
108 if (group_code > 26) {
109 av_log(s->avctx, AV_LOG_WARNING, "GAQ gain group code out-of-range\n");
110 group_code = 26;
111 }
112 gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][0];
113 gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][1];
114 gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][2];
115 gc = 0;
116 }
117 }
118 }
119 }
120
121 gs=0;
122 for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
123 int hebap = s->bap[ch][bin];
124 int bits = ff_eac3_bits_vs_hebap[hebap];
125 if (!hebap) {
126 /* zero-mantissa dithering */
127 for (blk = 0; blk < 6; blk++) {
128 s->pre_mantissa[ch][bin][blk] = (av_lfg_get(&s->dith_state) & 0x7FFFFF) - 0x400000;
129 }
130 } else if (hebap < 8) {
131 /* Vector Quantization */
132 int v = get_bits(gbc, bits);
133 for (blk = 0; blk < 6; blk++) {
134 s->pre_mantissa[ch][bin][blk] = ff_eac3_vq_hebap[hebap][v][blk] << 8;
135 }
136 } else {
137 /* Gain Adaptive Quantization */
138 int gbits, log_gain;
139 if (gaq_mode != EAC3_GAQ_NO && hebap < end_bap) {
140 log_gain = gaq_gain[gs++];
141 } else {
142 log_gain = 0;
143 }
144 gbits = bits - log_gain;
145
146 for (blk = 0; blk < 6; blk++) {
147 int mant = get_sbits(gbc, gbits);
148 if (mant == -(1 << (gbits-1))) {
149 /* large mantissa */
150 int b;
151 mant = get_sbits(gbc, bits-2+log_gain) << (26-log_gain-bits);
152 /* remap mantissa value to correct for asymmetric quantization */
153 if (mant >= 0)
154 b = 32768 >> (log_gain+8);
155 else
156 b = ff_eac3_gaq_remap_2_4_b[hebap-8][log_gain-1];
157 mant += (ff_eac3_gaq_remap_2_4_a[hebap-8][log_gain-1] * (mant>>8) + b) >> 7;
158 } else {
159 /* small mantissa, no GAQ, or Gk=1 */
160 mant <<= 24 - bits;
161 if (!log_gain) {
162 /* remap mantissa value for no GAQ or Gk=1 */
163 mant += (ff_eac3_gaq_remap_1[hebap-8] * (mant>>8)) >> 7;
164 }
165 }
166 s->pre_mantissa[ch][bin][blk] = mant;
167 }
168 }
169 idct6(s->pre_mantissa[ch][bin]);
170 }
171 }
172
173 int ff_eac3_parse_header(AC3DecodeContext *s)
174 {
175 int i, blk, ch;
176 int ac3_exponent_strategy, parse_aht_info, parse_spx_atten_data;
177 int parse_transient_proc_info;
178 int num_cpl_blocks;
179 GetBitContext *gbc = &s->gbc;
180
181 /* An E-AC-3 stream can have multiple independent streams which the
182 application can select from. each independent stream can also contain
183 dependent streams which are used to add or replace channels. */
184 if (s->frame_type == EAC3_FRAME_TYPE_DEPENDENT) {
185 av_log_missing_feature(s->avctx, "Dependent substream decoding", 1);
186 return AC3_PARSE_ERROR_FRAME_TYPE;
187 } else if (s->frame_type == EAC3_FRAME_TYPE_RESERVED) {
188 av_log(s->avctx, AV_LOG_ERROR, "Reserved frame type\n");
189 return AC3_PARSE_ERROR_FRAME_TYPE;
190 }
191
192 /* The substream id indicates which substream this frame belongs to. each
193 independent stream has its own substream id, and the dependent streams
194 associated to an independent stream have matching substream id's. */
195 if (s->substreamid) {
196 /* only decode substream with id=0. skip any additional substreams. */
197 av_log_missing_feature(s->avctx, "Additional substreams", 1);
198 return AC3_PARSE_ERROR_FRAME_TYPE;
199 }
200
201 if (s->bit_alloc_params.sr_code == EAC3_SR_CODE_REDUCED) {
202 /* The E-AC-3 specification does not tell how to handle reduced sample
203 rates in bit allocation. The best assumption would be that it is
204 handled like AC-3 DolbyNet, but we cannot be sure until we have a
205 sample which utilizes this feature. */
206 av_log_missing_feature(s->avctx, "Reduced sampling rates", 1);
207 return -1;
208 }
209 skip_bits(gbc, 5); // skip bitstream id
210
211 /* volume control params */
212 for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
213 skip_bits(gbc, 5); // skip dialog normalization
214 if (get_bits1(gbc)) {
215 skip_bits(gbc, 8); // skip compression gain word
216 }
217 }
218
219 /* dependent stream channel map */
220 if (s->frame_type == EAC3_FRAME_TYPE_DEPENDENT) {
221 if (get_bits1(gbc)) {
222 skip_bits(gbc, 16); // skip custom channel map
223 }
224 }
225
226 /* mixing metadata */
227 if (get_bits1(gbc)) {
228 /* center and surround mix levels */
229 if (s->channel_mode > AC3_CHMODE_STEREO) {
230 skip_bits(gbc, 2); // skip preferred stereo downmix mode
231 if (s->channel_mode & 1) {
232 /* if three front channels exist */
233 skip_bits(gbc, 3); //skip Lt/Rt center mix level
234 s->center_mix_level = get_bits(gbc, 3);
235 }
236 if (s->channel_mode & 4) {
237 /* if a surround channel exists */
238 skip_bits(gbc, 3); //skip Lt/Rt surround mix level
239 s->surround_mix_level = get_bits(gbc, 3);
240 }
241 }
242
243 /* lfe mix level */
244 if (s->lfe_on && get_bits1(gbc)) {
245 // TODO: use LFE mix level
246 skip_bits(gbc, 5); // skip LFE mix level code
247 }
248
249 /* info for mixing with other streams and substreams */
250 if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT) {
251 for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
252 // TODO: apply program scale factor
253 if (get_bits1(gbc)) {
254 skip_bits(gbc, 6); // skip program scale factor
255 }
256 }
257 if (get_bits1(gbc)) {
258 skip_bits(gbc, 6); // skip external program scale factor
259 }
260 /* skip mixing parameter data */
261 switch(get_bits(gbc, 2)) {
262 case 1: skip_bits(gbc, 5); break;
263 case 2: skip_bits(gbc, 12); break;
264 case 3: {
265 int mix_data_size = (get_bits(gbc, 5) + 2) << 3;
266 skip_bits_long(gbc, mix_data_size);
267 break;
268 }
269 }
270 /* skip pan information for mono or dual mono source */
271 if (s->channel_mode < AC3_CHMODE_STEREO) {
272 for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
273 if (get_bits1(gbc)) {
274 /* note: this is not in the ATSC A/52B specification
275 reference: ETSI TS 102 366 V1.1.1
276 section: E.1.3.1.25 */
277 skip_bits(gbc, 8); // skip pan mean direction index
278 skip_bits(gbc, 6); // skip reserved paninfo bits
279 }
280 }
281 }
282 /* skip mixing configuration information */
283 if (get_bits1(gbc)) {
284 for (blk = 0; blk < s->num_blocks; blk++) {
285 if (s->num_blocks == 1 || get_bits1(gbc)) {
286 skip_bits(gbc, 5);
287 }
288 }
289 }
290 }
291 }
292
293 /* informational metadata */
294 if (get_bits1(gbc)) {
295 skip_bits(gbc, 3); // skip bit stream mode
296 skip_bits(gbc, 2); // skip copyright bit and original bitstream bit
297 if (s->channel_mode == AC3_CHMODE_STEREO) {
298 skip_bits(gbc, 4); // skip Dolby surround and headphone mode
299 }
300 if (s->channel_mode >= AC3_CHMODE_2F2R) {
301 skip_bits(gbc, 2); // skip Dolby surround EX mode
302 }
303 for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
304 if (get_bits1(gbc)) {
305 skip_bits(gbc, 8); // skip mix level, room type, and A/D converter type
306 }
307 }
308 if (s->bit_alloc_params.sr_code != EAC3_SR_CODE_REDUCED) {
309 skip_bits1(gbc); // skip source sample rate code
310 }
311 }
312
313 /* converter synchronization flag
314 If frames are less than six blocks, this bit should be turned on
315 once every 6 blocks to indicate the start of a frame set.
316 reference: RFC 4598, Section 2.1.3 Frame Sets */
317 if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT && s->num_blocks != 6) {
318 skip_bits1(gbc); // skip converter synchronization flag
319 }
320
321 /* original frame size code if this stream was converted from AC-3 */
322 if (s->frame_type == EAC3_FRAME_TYPE_AC3_CONVERT &&
323 (s->num_blocks == 6 || get_bits1(gbc))) {
324 skip_bits(gbc, 6); // skip frame size code
325 }
326
327 /* additional bitstream info */
328 if (get_bits1(gbc)) {
329 int addbsil = get_bits(gbc, 6);
330 for (i = 0; i < addbsil + 1; i++) {
331 skip_bits(gbc, 8); // skip additional bit stream info
332 }
333 }
334
335 /* audio frame syntax flags, strategy data, and per-frame data */
336
337 if (s->num_blocks == 6) {
338 ac3_exponent_strategy = get_bits1(gbc);
339 parse_aht_info = get_bits1(gbc);
340 } else {
341 /* less than 6 blocks, so use AC-3-style exponent strategy syntax, and
342 do not use AHT */
343 ac3_exponent_strategy = 1;
344 parse_aht_info = 0;
345 }
346
347 s->snr_offset_strategy = get_bits(gbc, 2);
348 parse_transient_proc_info = get_bits1(gbc);
349
350 s->block_switch_syntax = get_bits1(gbc);
351 if (!s->block_switch_syntax)
352 memset(s->block_switch, 0, sizeof(s->block_switch));
353
354 s->dither_flag_syntax = get_bits1(gbc);
355 if (!s->dither_flag_syntax) {
356 for (ch = 1; ch <= s->fbw_channels; ch++)
357 s->dither_flag[ch] = 1;
358 }
359 s->dither_flag[CPL_CH] = s->dither_flag[s->lfe_ch] = 0;
360
361 s->bit_allocation_syntax = get_bits1(gbc);
362 if (!s->bit_allocation_syntax) {
363 /* set default bit allocation parameters */
364 s->bit_alloc_params.slow_decay = ff_ac3_slow_decay_tab[2];
365 s->bit_alloc_params.fast_decay = ff_ac3_fast_decay_tab[1];
366 s->bit_alloc_params.slow_gain = ff_ac3_slow_gain_tab [1];
367 s->bit_alloc_params.db_per_bit = ff_ac3_db_per_bit_tab[2];
368 s->bit_alloc_params.floor = ff_ac3_floor_tab [7];
369 }
370
371 s->fast_gain_syntax = get_bits1(gbc);
372 s->dba_syntax = get_bits1(gbc);
373 s->skip_syntax = get_bits1(gbc);
374 parse_spx_atten_data = get_bits1(gbc);
375
376 /* coupling strategy occurance and coupling use per block */
377 num_cpl_blocks = 0;
378 if (s->channel_mode > 1) {
379 for (blk = 0; blk < s->num_blocks; blk++) {
380 s->cpl_strategy_exists[blk] = (!blk || get_bits1(gbc));
381 if (s->cpl_strategy_exists[blk]) {
382 s->cpl_in_use[blk] = get_bits1(gbc);
383 } else {
384 s->cpl_in_use[blk] = s->cpl_in_use[blk-1];
385 }
386 num_cpl_blocks += s->cpl_in_use[blk];
387 }
388 } else {
389 memset(s->cpl_in_use, 0, sizeof(s->cpl_in_use));
390 }
391
392 /* exponent strategy data */
393 if (ac3_exponent_strategy) {
394 /* AC-3-style exponent strategy syntax */
395 for (blk = 0; blk < s->num_blocks; blk++) {
396 for (ch = !s->cpl_in_use[blk]; ch <= s->fbw_channels; ch++) {
397 s->exp_strategy[blk][ch] = get_bits(gbc, 2);
398 }
399 }
400 } else {
401 /* LUT-based exponent strategy syntax */
402 int frmchexpstr;
403 for (ch = !((s->channel_mode > 1) && num_cpl_blocks); ch <= s->fbw_channels; ch++) {
404 frmchexpstr = get_bits(gbc, 5);
405 for (blk = 0; blk < 6; blk++) {
406 s->exp_strategy[blk][ch] = ff_eac3_frm_expstr[frmchexpstr][blk];
407 }
408 }
409 }
410 /* LFE exponent strategy */
411 if (s->lfe_on) {
412 for (blk = 0; blk < s->num_blocks; blk++) {
413 s->exp_strategy[blk][s->lfe_ch] = get_bits1(gbc);
414 }
415 }
416 /* original exponent strategies if this stream was converted from AC-3 */
417 if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT &&
418 (s->num_blocks == 6 || get_bits1(gbc))) {
419 for (ch = 1; ch <= s->fbw_channels; ch++) {
420 skip_bits(gbc, 5); // skip converter channel exponent strategy
421 }
422 }
423
424 /* determine which channels use AHT */
425 if (parse_aht_info) {
426 /* AHT is only available when there are 6 blocks in the frame.
427 The coupling channel can only use AHT when coupling is in use for
428 all blocks.
429 reference: Section E3.3.2 Bit Stream Helper Variables */
430 s->channel_uses_aht[CPL_CH]=0;
431 for (ch = (num_cpl_blocks != 6); ch <= s->channels; ch++) {
432 int nchregs = 0;
433 for (blk = 0; blk < 6; blk++) {
434 if (ch)
435 nchregs += (s->exp_strategy[blk][ch] != EXP_REUSE);
436 else
437 nchregs += s->cpl_strategy_exists[blk] ||
438 (s->exp_strategy[blk][CPL_CH] != EXP_REUSE);
439 }
440 s->channel_uses_aht[ch] = (nchregs == 1) && get_bits1(gbc);
441 }
442 } else {
443 memset(s->channel_uses_aht, 0, sizeof(s->channel_uses_aht));
444 }
445
446 /* per-frame SNR offset */
447 if (!s->snr_offset_strategy) {
448 int csnroffst = (get_bits(gbc, 6) - 15) << 4;
449 int snroffst = (csnroffst + get_bits(gbc, 4)) << 2;
450 for (ch = 0; ch <= s->channels; ch++)
451 s->snr_offset[ch] = snroffst;
452 }
453
454 /* transient pre-noise processing data */
455 if (parse_transient_proc_info) {
456 for (ch = 1; ch <= s->fbw_channels; ch++) {
457 if (get_bits1(gbc)) { // channel in transient processing
458 skip_bits(gbc, 10); // skip transient processing location
459 skip_bits(gbc, 8); // skip transient processing length
460 }
461 }
462 }
463
464 /* spectral extension attenuation data */
465 if (parse_spx_atten_data) {
466 av_log_missing_feature(s->avctx, "Spectral extension attenuation", 1);
467 for (ch = 1; ch <= s->fbw_channels; ch++) {
468 if (get_bits1(gbc)) { // channel has spx attenuation
469 skip_bits(gbc, 5); // skip spx attenuation code
470 }
471 }
472 }
473
474 /* block start information */
475 if (s->num_blocks > 1 && get_bits1(gbc)) {
476 /* reference: Section E2.3.2.27
477 nblkstrtbits = (numblks - 1) * (4 + ceiling(log2(words_per_frame)))
478 The spec does not say what this data is or what it's used for.
479 It is likely the offset of each block within the frame. */
480 int block_start_bits = (s->num_blocks-1) * (4 + av_log2(s->frame_size-2));
481 skip_bits(gbc, block_start_bits);
482 }
483
484 /* syntax state initialization */
485 for (ch = 1; ch <= s->fbw_channels; ch++) {
486 s->first_cpl_coords[ch] = 1;
487 }
488 s->first_cpl_leak = 1;
489
490 return 0;
491 }