Rename function: s/colmult/apply_window/
[ffmpeg-lucabe.git] / libavcodec / eac3dec.c
blob0b10b41848a1256a9880f83b1b3e8b28143f46ee
1 /*
2 * E-AC-3 decoder
3 * Copyright (c) 2007 Bartlomiej Wolowiec <bartek.wolowiec@gmail.com>
4 * Copyright (c) 2008 Justin Ruggles
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "avcodec.h"
24 #include "ac3.h"
25 #include "ac3_parser.h"
26 #include "ac3dec.h"
27 #include "ac3dec_data.h"
29 /** gain adaptive quantization mode */
30 typedef enum {
31 EAC3_GAQ_NO =0,
32 EAC3_GAQ_12,
33 EAC3_GAQ_14,
34 EAC3_GAQ_124
35 } EAC3GaqMode;
37 #define EAC3_SR_CODE_REDUCED 3
39 /** lrint(M_SQRT2*cos(2*M_PI/12)*(1<<23)) */
40 #define COEFF_0 10273905LL
42 /** lrint(M_SQRT2*cos(0*M_PI/12)*(1<<23)) = lrint(M_SQRT2*(1<<23)) */
43 #define COEFF_1 11863283LL
45 /** lrint(M_SQRT2*cos(5*M_PI/12)*(1<<23)) */
46 #define COEFF_2 3070444LL
48 /**
49 * Calculate 6-point IDCT of the pre-mantissas.
50 * All calculations are 24-bit fixed-point.
52 static void idct6(int pre_mant[6])
54 int tmp;
55 int even0, even1, even2, odd0, odd1, odd2;
57 odd1 = pre_mant[1] - pre_mant[3] - pre_mant[5];
59 even2 = ( pre_mant[2] * COEFF_0) >> 23;
60 tmp = ( pre_mant[4] * COEFF_1) >> 23;
61 odd0 = ((pre_mant[1] + pre_mant[5]) * COEFF_2) >> 23;
63 even0 = pre_mant[0] + (tmp >> 1);
64 even1 = pre_mant[0] - tmp;
66 tmp = even0;
67 even0 = tmp + even2;
68 even2 = tmp - even2;
70 tmp = odd0;
71 odd0 = tmp + pre_mant[1] + pre_mant[3];
72 odd2 = tmp + pre_mant[5] - pre_mant[3];
74 pre_mant[0] = even0 + odd0;
75 pre_mant[1] = even1 + odd1;
76 pre_mant[2] = even2 + odd2;
77 pre_mant[3] = even2 - odd2;
78 pre_mant[4] = even1 - odd1;
79 pre_mant[5] = even0 - odd0;
82 void ff_eac3_decode_transform_coeffs_aht_ch(AC3DecodeContext *s, int ch)
84 int bin, blk, gs;
85 int end_bap, gaq_mode;
86 GetBitContext *gbc = &s->gbc;
87 int gaq_gain[AC3_MAX_COEFS];
89 gaq_mode = get_bits(gbc, 2);
90 end_bap = (gaq_mode < 2) ? 12 : 17;
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);
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;
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;
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;
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_mantissa_vq[hebap][v][blk] << 8;
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;
144 gbits = bits - log_gain;
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;
166 s->pre_mantissa[ch][bin][blk] = mant;
169 idct6(s->pre_mantissa[ch][bin]);
173 int ff_eac3_parse_header(AC3DecodeContext *s)
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;
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;
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;
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;
209 skip_bits(gbc, 5); // skip bitstream id
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
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
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);
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);
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
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
257 if (get_bits1(gbc)) {
258 skip_bits(gbc, 6); // skip external program scale factor
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;
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
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);
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
300 if (s->channel_mode >= AC3_CHMODE_2F2R) {
301 skip_bits(gbc, 2); // skip Dolby surround EX mode
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
308 if (s->bit_alloc_params.sr_code != EAC3_SR_CODE_REDUCED) {
309 skip_bits1(gbc); // skip source sample rate code
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
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
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
335 /* audio frame syntax flags, strategy data, and per-frame data */
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;
347 s->snr_offset_strategy = get_bits(gbc, 2);
348 parse_transient_proc_info = get_bits1(gbc);
350 s->block_switch_syntax = get_bits1(gbc);
351 if (!s->block_switch_syntax)
352 memset(s->block_switch, 0, sizeof(s->block_switch));
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;
359 s->dither_flag[CPL_CH] = s->dither_flag[s->lfe_ch] = 0;
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];
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);
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];
386 num_cpl_blocks += s->cpl_in_use[blk];
388 } else {
389 memset(s->cpl_in_use, 0, sizeof(s->cpl_in_use));
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);
400 } else {
401 /* LUT-based exponent strategy syntax */
402 for (ch = !((s->channel_mode > 1) && num_cpl_blocks); ch <= s->fbw_channels; ch++) {
403 int frmchexpstr = get_bits(gbc, 5);
404 for (blk = 0; blk < 6; blk++) {
405 s->exp_strategy[blk][ch] = ff_eac3_frm_expstr[frmchexpstr][blk];
409 /* LFE exponent strategy */
410 if (s->lfe_on) {
411 for (blk = 0; blk < s->num_blocks; blk++) {
412 s->exp_strategy[blk][s->lfe_ch] = get_bits1(gbc);
415 /* original exponent strategies if this stream was converted from AC-3 */
416 if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT &&
417 (s->num_blocks == 6 || get_bits1(gbc))) {
418 skip_bits(gbc, 5 * s->fbw_channels); // skip converter channel exponent strategy
421 /* determine which channels use AHT */
422 if (parse_aht_info) {
423 /* For AHT to be used, all non-zero blocks must reuse exponents from
424 the first block. Furthermore, for AHT to be used in the coupling
425 channel, all blocks must use coupling and use the same coupling
426 strategy. */
427 s->channel_uses_aht[CPL_CH]=0;
428 for (ch = (num_cpl_blocks != 6); ch <= s->channels; ch++) {
429 int use_aht = 1;
430 for (blk = 1; blk < 6; blk++) {
431 if ((s->exp_strategy[blk][ch] != EXP_REUSE) ||
432 (!ch && s->cpl_strategy_exists[blk])) {
433 use_aht = 0;
434 break;
437 s->channel_uses_aht[ch] = use_aht && get_bits1(gbc);
439 } else {
440 memset(s->channel_uses_aht, 0, sizeof(s->channel_uses_aht));
443 /* per-frame SNR offset */
444 if (!s->snr_offset_strategy) {
445 int csnroffst = (get_bits(gbc, 6) - 15) << 4;
446 int snroffst = (csnroffst + get_bits(gbc, 4)) << 2;
447 for (ch = 0; ch <= s->channels; ch++)
448 s->snr_offset[ch] = snroffst;
451 /* transient pre-noise processing data */
452 if (parse_transient_proc_info) {
453 for (ch = 1; ch <= s->fbw_channels; ch++) {
454 if (get_bits1(gbc)) { // channel in transient processing
455 skip_bits(gbc, 10); // skip transient processing location
456 skip_bits(gbc, 8); // skip transient processing length
461 /* spectral extension attenuation data */
462 if (parse_spx_atten_data) {
463 av_log_missing_feature(s->avctx, "Spectral extension attenuation", 1);
464 for (ch = 1; ch <= s->fbw_channels; ch++) {
465 if (get_bits1(gbc)) { // channel has spx attenuation
466 skip_bits(gbc, 5); // skip spx attenuation code
471 /* block start information */
472 if (s->num_blocks > 1 && get_bits1(gbc)) {
473 /* reference: Section E2.3.2.27
474 nblkstrtbits = (numblks - 1) * (4 + ceiling(log2(words_per_frame)))
475 The spec does not say what this data is or what it's used for.
476 It is likely the offset of each block within the frame. */
477 int block_start_bits = (s->num_blocks-1) * (4 + av_log2(s->frame_size-2));
478 skip_bits(gbc, block_start_bits);
481 /* syntax state initialization */
482 for (ch = 1; ch <= s->fbw_channels; ch++) {
483 s->first_cpl_coords[ch] = 1;
485 s->first_cpl_leak = 1;
487 return 0;