FFmpeg  4.1.11
eac3dec.c
Go to the documentation of this file.
1 /*
2  * E-AC-3 decoder
3  * Copyright (c) 2007 Bartlomiej Wolowiec <bartek.wolowiec@gmail.com>
4  * Copyright (c) 2008 Justin Ruggles
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22 
23 /*
24  * There are several features of E-AC-3 that this decoder does not yet support.
25  *
26  * Enhanced Coupling
27  * No known samples exist. If any ever surface, this feature should not be
28  * too difficult to implement.
29  *
30  * Reduced Sample Rates
31  * No known samples exist. The spec also does not give clear information
32  * on how this is to be implemented.
33  *
34  * Dependent Streams
35  * Only the independent stream is currently decoded. Any dependent
36  * streams are skipped. We have only come across two examples of this, and
37  * they are both just test streams, one for HD-DVD and the other for
38  * Blu-ray.
39  *
40  * Transient Pre-noise Processing
41  * This is side information which a decoder should use to reduce artifacts
42  * caused by transients. There are samples which are known to have this
43  * information, but this decoder currently ignores it.
44  */
45 
46 
47 #include "avcodec.h"
48 #include "internal.h"
49 #include "aac_ac3_parser.h"
50 #include "ac3.h"
51 #include "ac3dec.h"
52 #include "ac3dec_data.h"
53 #include "eac3_data.h"
54 
55 /** gain adaptive quantization mode */
56 typedef enum {
61 } EAC3GaqMode;
62 
63 #define EAC3_SR_CODE_REDUCED 3
64 
65 static void ff_eac3_apply_spectral_extension(AC3DecodeContext *s)
66 {
67  int bin, bnd, ch, i;
68  uint8_t wrapflag[SPX_MAX_BANDS]={1,0,}, num_copy_sections, copy_sizes[SPX_MAX_BANDS];
69  float rms_energy[SPX_MAX_BANDS];
70 
71  /* Set copy index mapping table. Set wrap flags to apply a notch filter at
72  wrap points later on. */
73  bin = s->spx_dst_start_freq;
74  num_copy_sections = 0;
75  for (bnd = 0; bnd < s->num_spx_bands; bnd++) {
76  int copysize;
77  int bandsize = s->spx_band_sizes[bnd];
78  if (bin + bandsize > s->spx_src_start_freq) {
79  copy_sizes[num_copy_sections++] = bin - s->spx_dst_start_freq;
80  bin = s->spx_dst_start_freq;
81  wrapflag[bnd] = 1;
82  }
83  for (i = 0; i < bandsize; i += copysize) {
84  if (bin == s->spx_src_start_freq) {
85  copy_sizes[num_copy_sections++] = bin - s->spx_dst_start_freq;
86  bin = s->spx_dst_start_freq;
87  }
88  copysize = FFMIN(bandsize - i, s->spx_src_start_freq - bin);
89  bin += copysize;
90  }
91  }
92  copy_sizes[num_copy_sections++] = bin - s->spx_dst_start_freq;
93 
94  for (ch = 1; ch <= s->fbw_channels; ch++) {
95  if (!s->channel_uses_spx[ch])
96  continue;
97 
98  /* Copy coeffs from normal bands to extension bands */
99  bin = s->spx_src_start_freq;
100  for (i = 0; i < num_copy_sections; i++) {
101  memcpy(&s->transform_coeffs[ch][bin],
102  &s->transform_coeffs[ch][s->spx_dst_start_freq],
103  copy_sizes[i]*sizeof(INTFLOAT));
104  bin += copy_sizes[i];
105  }
106 
107  /* Calculate RMS energy for each SPX band. */
108  bin = s->spx_src_start_freq;
109  for (bnd = 0; bnd < s->num_spx_bands; bnd++) {
110  int bandsize = s->spx_band_sizes[bnd];
111  float accum = 0.0f;
112  for (i = 0; i < bandsize; i++) {
113  float coeff = s->transform_coeffs[ch][bin++];
114  accum += coeff * coeff;
115  }
116  rms_energy[bnd] = sqrtf(accum / bandsize);
117  }
118 
119  /* Apply a notch filter at transitions between normal and extension
120  bands and at all wrap points. */
121  if (s->spx_atten_code[ch] >= 0) {
122  const float *atten_tab = ff_eac3_spx_atten_tab[s->spx_atten_code[ch]];
123  bin = s->spx_src_start_freq - 2;
124  for (bnd = 0; bnd < s->num_spx_bands; bnd++) {
125  if (wrapflag[bnd]) {
126  INTFLOAT *coeffs = &s->transform_coeffs[ch][bin];
127  coeffs[0] *= atten_tab[0];
128  coeffs[1] *= atten_tab[1];
129  coeffs[2] *= atten_tab[2];
130  coeffs[3] *= atten_tab[1];
131  coeffs[4] *= atten_tab[0];
132  }
133  bin += s->spx_band_sizes[bnd];
134  }
135  }
136 
137  /* Apply noise-blended coefficient scaling based on previously
138  calculated RMS energy, blending factors, and SPX coordinates for
139  each band. */
140  bin = s->spx_src_start_freq;
141  for (bnd = 0; bnd < s->num_spx_bands; bnd++) {
142  float nscale = s->spx_noise_blend[ch][bnd] * rms_energy[bnd] * (1.0f / INT32_MIN);
143  float sscale = s->spx_signal_blend[ch][bnd];
144 #if USE_FIXED
145  // spx_noise_blend and spx_signal_blend are both FP.23
146  nscale *= 1.0 / (1<<23);
147  sscale *= 1.0 / (1<<23);
148  if (nscale < -1.0)
149  nscale = -1.0;
150 #endif
151  for (i = 0; i < s->spx_band_sizes[bnd]; i++) {
152  UINTFLOAT noise = (INTFLOAT)(nscale * (int32_t)av_lfg_get(&s->dith_state));
153  s->transform_coeffs[ch][bin] *= sscale;
154  s->transform_coeffs[ch][bin++] += noise;
155  }
156  }
157  }
158 }
159 
160 
161 /** lrint(M_SQRT2*cos(2*M_PI/12)*(1<<23)) */
162 #define COEFF_0 10273905LL
163 
164 /** lrint(M_SQRT2*cos(0*M_PI/12)*(1<<23)) = lrint(M_SQRT2*(1<<23)) */
165 #define COEFF_1 11863283LL
166 
167 /** lrint(M_SQRT2*cos(5*M_PI/12)*(1<<23)) */
168 #define COEFF_2 3070444LL
169 
170 /**
171  * Calculate 6-point IDCT of the pre-mantissas.
172  * All calculations are 24-bit fixed-point.
173  */
174 static void idct6(int pre_mant[6])
175 {
176  int tmp;
177  int even0, even1, even2, odd0, odd1, odd2;
178 
179  odd1 = pre_mant[1] - pre_mant[3] - pre_mant[5];
180 
181  even2 = ( pre_mant[2] * COEFF_0) >> 23;
182  tmp = ( pre_mant[4] * COEFF_1) >> 23;
183  odd0 = ((pre_mant[1] + pre_mant[5]) * COEFF_2) >> 23;
184 
185  even0 = pre_mant[0] + (tmp >> 1);
186  even1 = pre_mant[0] - tmp;
187 
188  tmp = even0;
189  even0 = tmp + even2;
190  even2 = tmp - even2;
191 
192  tmp = odd0;
193  odd0 = tmp + pre_mant[1] + pre_mant[3];
194  odd2 = tmp + pre_mant[5] - pre_mant[3];
195 
196  pre_mant[0] = even0 + odd0;
197  pre_mant[1] = even1 + odd1;
198  pre_mant[2] = even2 + odd2;
199  pre_mant[3] = even2 - odd2;
200  pre_mant[4] = even1 - odd1;
201  pre_mant[5] = even0 - odd0;
202 }
203 
204 static void ff_eac3_decode_transform_coeffs_aht_ch(AC3DecodeContext *s, int ch)
205 {
206  int bin, blk, gs;
207  int end_bap, gaq_mode;
208  GetBitContext *gbc = &s->gbc;
209  int gaq_gain[AC3_MAX_COEFS];
210 
211  gaq_mode = get_bits(gbc, 2);
212  end_bap = (gaq_mode < 2) ? 12 : 17;
213 
214  /* if GAQ gain is used, decode gain codes for bins with hebap between
215  8 and end_bap */
216  gs = 0;
217  if (gaq_mode == EAC3_GAQ_12 || gaq_mode == EAC3_GAQ_14) {
218  /* read 1-bit GAQ gain codes */
219  for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
220  if (s->bap[ch][bin] > 7 && s->bap[ch][bin] < end_bap)
221  gaq_gain[gs++] = get_bits1(gbc) << (gaq_mode-1);
222  }
223  } else if (gaq_mode == EAC3_GAQ_124) {
224  /* read 1.67-bit GAQ gain codes (3 codes in 5 bits) */
225  int gc = 2;
226  for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
227  if (s->bap[ch][bin] > 7 && s->bap[ch][bin] < 17) {
228  if (gc++ == 2) {
229  int group_code = get_bits(gbc, 5);
230  if (group_code > 26) {
231  av_log(s->avctx, AV_LOG_WARNING, "GAQ gain group code out-of-range\n");
232  group_code = 26;
233  }
234  gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][0];
235  gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][1];
236  gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][2];
237  gc = 0;
238  }
239  }
240  }
241  }
242 
243  gs=0;
244  for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
245  int hebap = s->bap[ch][bin];
246  int bits = ff_eac3_bits_vs_hebap[hebap];
247  if (!hebap) {
248  /* zero-mantissa dithering */
249  for (blk = 0; blk < 6; blk++) {
250  s->pre_mantissa[ch][bin][blk] = (av_lfg_get(&s->dith_state) & 0x7FFFFF) - 0x400000;
251  }
252  } else if (hebap < 8) {
253  /* Vector Quantization */
254  int v = get_bits(gbc, bits);
255  for (blk = 0; blk < 6; blk++) {
256  s->pre_mantissa[ch][bin][blk] = ff_eac3_mantissa_vq[hebap][v][blk] * (1 << 8);
257  }
258  } else {
259  /* Gain Adaptive Quantization */
260  int gbits, log_gain;
261  if (gaq_mode != EAC3_GAQ_NO && hebap < end_bap) {
262  log_gain = gaq_gain[gs++];
263  } else {
264  log_gain = 0;
265  }
266  gbits = bits - log_gain;
267 
268  for (blk = 0; blk < 6; blk++) {
269  int mant = get_sbits(gbc, gbits);
270  if (log_gain && mant == -(1 << (gbits-1))) {
271  /* large mantissa */
272  int b;
273  int mbits = bits - (2 - log_gain);
274  mant = get_sbits(gbc, mbits);
275  mant = ((unsigned)mant) << (23 - (mbits - 1));
276  /* remap mantissa value to correct for asymmetric quantization */
277  if (mant >= 0)
278  b = 1 << (23 - log_gain);
279  else
280  b = ff_eac3_gaq_remap_2_4_b[hebap-8][log_gain-1] * (1 << 8);
281  mant += ((ff_eac3_gaq_remap_2_4_a[hebap-8][log_gain-1] * (int64_t)mant) >> 15) + b;
282  } else {
283  /* small mantissa, no GAQ, or Gk=1 */
284  mant *= (1 << 24 - bits);
285  if (!log_gain) {
286  /* remap mantissa value for no GAQ or Gk=1 */
287  mant += (ff_eac3_gaq_remap_1[hebap-8] * (int64_t)mant) >> 15;
288  }
289  }
290  s->pre_mantissa[ch][bin][blk] = mant;
291  }
292  }
293  idct6(s->pre_mantissa[ch][bin]);
294  }
295 }
296 
297 static int ff_eac3_parse_header(AC3DecodeContext *s)
298 {
299  int i, blk, ch;
300  int ac3_exponent_strategy, parse_aht_info, parse_spx_atten_data;
301  int parse_transient_proc_info;
302  int num_cpl_blocks;
303  GetBitContext *gbc = &s->gbc;
304 
305  /* An E-AC-3 stream can have multiple independent streams which the
306  application can select from. each independent stream can also contain
307  dependent streams which are used to add or replace channels. */
308  if (s->frame_type == EAC3_FRAME_TYPE_RESERVED) {
309  av_log(s->avctx, AV_LOG_ERROR, "Reserved frame type\n");
311  }
312 
313  /* The substream id indicates which substream this frame belongs to. each
314  independent stream has its own substream id, and the dependent streams
315  associated to an independent stream have matching substream id's. */
316  if (s->substreamid) {
317  /* only decode substream with id=0. skip any additional substreams. */
318  if (!s->eac3_subsbtreamid_found) {
319  s->eac3_subsbtreamid_found = 1;
320  avpriv_request_sample(s->avctx, "Additional substreams");
321  }
323  }
324 
325  if (s->bit_alloc_params.sr_code == EAC3_SR_CODE_REDUCED) {
326  /* The E-AC-3 specification does not tell how to handle reduced sample
327  rates in bit allocation. The best assumption would be that it is
328  handled like AC-3 DolbyNet, but we cannot be sure until we have a
329  sample which utilizes this feature. */
330  avpriv_request_sample(s->avctx, "Reduced sampling rate");
331  return AVERROR_PATCHWELCOME;
332  }
333  skip_bits(gbc, 5); // skip bitstream id
334 
335  /* volume control params */
336  for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
337  s->dialog_normalization[i] = -get_bits(gbc, 5);
338  if (s->dialog_normalization[i] == 0) {
339  s->dialog_normalization[i] = -31;
340  }
341  if (s->target_level != 0) {
342  s->level_gain[i] = powf(2.0f,
343  (float)(s->target_level - s->dialog_normalization[i])/6.0f);
344  }
345  s->compression_exists[i] = get_bits1(gbc);
346  if (s->compression_exists[i]) {
347  s->heavy_dynamic_range[i] = AC3_HEAVY_RANGE(get_bits(gbc, 8));
348  }
349  }
350 
351  /* dependent stream channel map */
352  if (s->frame_type == EAC3_FRAME_TYPE_DEPENDENT) {
353  if (get_bits1(gbc)) {
354  int64_t channel_layout = 0;
355  int channel_map = get_bits(gbc, 16);
356  av_log(s->avctx, AV_LOG_DEBUG, "channel_map: %0X\n", channel_map);
357 
358  for (i = 0; i < 16; i++)
359  if (channel_map & (1 << (EAC3_MAX_CHANNELS - i - 1)))
360  channel_layout |= ff_eac3_custom_channel_map_locations[i][1];
361 
362  if (av_popcount64(channel_layout) > EAC3_MAX_CHANNELS) {
363  return AVERROR_INVALIDDATA;
364  }
365  s->channel_map = channel_map;
366  }
367  }
368 
369  /* mixing metadata */
370  if (get_bits1(gbc)) {
371  /* center and surround mix levels */
372  if (s->channel_mode > AC3_CHMODE_STEREO) {
373  s->preferred_downmix = get_bits(gbc, 2);
374  if (s->channel_mode & 1) {
375  /* if three front channels exist */
376  s->center_mix_level_ltrt = get_bits(gbc, 3);
377  s->center_mix_level = get_bits(gbc, 3);
378  }
379  if (s->channel_mode & 4) {
380  /* if a surround channel exists */
381  s->surround_mix_level_ltrt = av_clip(get_bits(gbc, 3), 3, 7);
382  s->surround_mix_level = av_clip(get_bits(gbc, 3), 3, 7);
383  }
384  }
385 
386  /* lfe mix level */
387  if (s->lfe_on && (s->lfe_mix_level_exists = get_bits1(gbc))) {
388  s->lfe_mix_level = get_bits(gbc, 5);
389  }
390 
391  /* info for mixing with other streams and substreams */
392  if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT) {
393  for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
394  // TODO: apply program scale factor
395  if (get_bits1(gbc)) {
396  skip_bits(gbc, 6); // skip program scale factor
397  }
398  }
399  if (get_bits1(gbc)) {
400  skip_bits(gbc, 6); // skip external program scale factor
401  }
402  /* skip mixing parameter data */
403  switch(get_bits(gbc, 2)) {
404  case 1: skip_bits(gbc, 5); break;
405  case 2: skip_bits(gbc, 12); break;
406  case 3: {
407  int mix_data_size = (get_bits(gbc, 5) + 2) << 3;
408  skip_bits_long(gbc, mix_data_size);
409  break;
410  }
411  }
412  /* skip pan information for mono or dual mono source */
413  if (s->channel_mode < AC3_CHMODE_STEREO) {
414  for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
415  if (get_bits1(gbc)) {
416  /* note: this is not in the ATSC A/52B specification
417  reference: ETSI TS 102 366 V1.1.1
418  section: E.1.3.1.25 */
419  skip_bits(gbc, 8); // skip pan mean direction index
420  skip_bits(gbc, 6); // skip reserved paninfo bits
421  }
422  }
423  }
424  /* skip mixing configuration information */
425  if (get_bits1(gbc)) {
426  for (blk = 0; blk < s->num_blocks; blk++) {
427  if (s->num_blocks == 1 || get_bits1(gbc)) {
428  skip_bits(gbc, 5);
429  }
430  }
431  }
432  }
433  }
434 
435  /* informational metadata */
436  if (get_bits1(gbc)) {
437  s->bitstream_mode = get_bits(gbc, 3);
438  skip_bits(gbc, 2); // skip copyright bit and original bitstream bit
439  if (s->channel_mode == AC3_CHMODE_STEREO) {
440  s->dolby_surround_mode = get_bits(gbc, 2);
441  s->dolby_headphone_mode = get_bits(gbc, 2);
442  }
443  if (s->channel_mode >= AC3_CHMODE_2F2R) {
444  s->dolby_surround_ex_mode = get_bits(gbc, 2);
445  }
446  for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
447  if (get_bits1(gbc)) {
448  skip_bits(gbc, 8); // skip mix level, room type, and A/D converter type
449  }
450  }
451  if (s->bit_alloc_params.sr_code != EAC3_SR_CODE_REDUCED) {
452  skip_bits1(gbc); // skip source sample rate code
453  }
454  }
455 
456  /* converter synchronization flag
457  If frames are less than six blocks, this bit should be turned on
458  once every 6 blocks to indicate the start of a frame set.
459  reference: RFC 4598, Section 2.1.3 Frame Sets */
460  if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT && s->num_blocks != 6) {
461  skip_bits1(gbc); // skip converter synchronization flag
462  }
463 
464  /* original frame size code if this stream was converted from AC-3 */
465  if (s->frame_type == EAC3_FRAME_TYPE_AC3_CONVERT &&
466  (s->num_blocks == 6 || get_bits1(gbc))) {
467  skip_bits(gbc, 6); // skip frame size code
468  }
469 
470  /* additional bitstream info */
471  if (get_bits1(gbc)) {
472  int addbsil = get_bits(gbc, 6);
473  for (i = 0; i < addbsil + 1; i++) {
474  skip_bits(gbc, 8); // skip additional bit stream info
475  }
476  }
477 
478  /* audio frame syntax flags, strategy data, and per-frame data */
479 
480  if (s->num_blocks == 6) {
481  ac3_exponent_strategy = get_bits1(gbc);
482  parse_aht_info = get_bits1(gbc);
483  } else {
484  /* less than 6 blocks, so use AC-3-style exponent strategy syntax, and
485  do not use AHT */
486  ac3_exponent_strategy = 1;
487  parse_aht_info = 0;
488  }
489 
490  s->snr_offset_strategy = get_bits(gbc, 2);
491  parse_transient_proc_info = get_bits1(gbc);
492 
493  s->block_switch_syntax = get_bits1(gbc);
494  if (!s->block_switch_syntax)
495  memset(s->block_switch, 0, sizeof(s->block_switch));
496 
497  s->dither_flag_syntax = get_bits1(gbc);
498  if (!s->dither_flag_syntax) {
499  for (ch = 1; ch <= s->fbw_channels; ch++)
500  s->dither_flag[ch] = 1;
501  }
502  s->dither_flag[CPL_CH] = s->dither_flag[s->lfe_ch] = 0;
503 
504  s->bit_allocation_syntax = get_bits1(gbc);
505  if (!s->bit_allocation_syntax) {
506  /* set default bit allocation parameters */
507  s->bit_alloc_params.slow_decay = ff_ac3_slow_decay_tab[2];
508  s->bit_alloc_params.fast_decay = ff_ac3_fast_decay_tab[1];
509  s->bit_alloc_params.slow_gain = ff_ac3_slow_gain_tab [1];
510  s->bit_alloc_params.db_per_bit = ff_ac3_db_per_bit_tab[2];
511  s->bit_alloc_params.floor = ff_ac3_floor_tab [7];
512  }
513 
514  s->fast_gain_syntax = get_bits1(gbc);
515  s->dba_syntax = get_bits1(gbc);
516  s->skip_syntax = get_bits1(gbc);
517  parse_spx_atten_data = get_bits1(gbc);
518 
519  /* coupling strategy occurrence and coupling use per block */
520  num_cpl_blocks = 0;
521  if (s->channel_mode > 1) {
522  for (blk = 0; blk < s->num_blocks; blk++) {
523  s->cpl_strategy_exists[blk] = (!blk || get_bits1(gbc));
524  if (s->cpl_strategy_exists[blk]) {
525  s->cpl_in_use[blk] = get_bits1(gbc);
526  } else {
527  s->cpl_in_use[blk] = s->cpl_in_use[blk-1];
528  }
529  num_cpl_blocks += s->cpl_in_use[blk];
530  }
531  } else {
532  memset(s->cpl_in_use, 0, sizeof(s->cpl_in_use));
533  }
534 
535  /* exponent strategy data */
536  if (ac3_exponent_strategy) {
537  /* AC-3-style exponent strategy syntax */
538  for (blk = 0; blk < s->num_blocks; blk++) {
539  for (ch = !s->cpl_in_use[blk]; ch <= s->fbw_channels; ch++) {
540  s->exp_strategy[blk][ch] = get_bits(gbc, 2);
541  }
542  }
543  } else {
544  /* LUT-based exponent strategy syntax */
545  for (ch = !((s->channel_mode > 1) && num_cpl_blocks); ch <= s->fbw_channels; ch++) {
546  int frmchexpstr = get_bits(gbc, 5);
547  for (blk = 0; blk < 6; blk++) {
548  s->exp_strategy[blk][ch] = ff_eac3_frm_expstr[frmchexpstr][blk];
549  }
550  }
551  }
552  /* LFE exponent strategy */
553  if (s->lfe_on) {
554  for (blk = 0; blk < s->num_blocks; blk++) {
555  s->exp_strategy[blk][s->lfe_ch] = get_bits1(gbc);
556  }
557  }
558  /* original exponent strategies if this stream was converted from AC-3 */
559  if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT &&
560  (s->num_blocks == 6 || get_bits1(gbc))) {
561  skip_bits(gbc, 5 * s->fbw_channels); // skip converter channel exponent strategy
562  }
563 
564  /* determine which channels use AHT */
565  if (parse_aht_info) {
566  /* For AHT to be used, all non-zero blocks must reuse exponents from
567  the first block. Furthermore, for AHT to be used in the coupling
568  channel, all blocks must use coupling and use the same coupling
569  strategy. */
570  s->channel_uses_aht[CPL_CH]=0;
571  for (ch = (num_cpl_blocks != 6); ch <= s->channels; ch++) {
572  int use_aht = 1;
573  for (blk = 1; blk < 6; blk++) {
574  if ((s->exp_strategy[blk][ch] != EXP_REUSE) ||
575  (!ch && s->cpl_strategy_exists[blk])) {
576  use_aht = 0;
577  break;
578  }
579  }
580  s->channel_uses_aht[ch] = use_aht && get_bits1(gbc);
581  }
582  } else {
583  memset(s->channel_uses_aht, 0, sizeof(s->channel_uses_aht));
584  }
585 
586  /* per-frame SNR offset */
587  if (!s->snr_offset_strategy) {
588  int csnroffst = (get_bits(gbc, 6) - 15) << 4;
589  int snroffst = (csnroffst + get_bits(gbc, 4)) << 2;
590  for (ch = 0; ch <= s->channels; ch++)
591  s->snr_offset[ch] = snroffst;
592  }
593 
594  /* transient pre-noise processing data */
595  if (parse_transient_proc_info) {
596  for (ch = 1; ch <= s->fbw_channels; ch++) {
597  if (get_bits1(gbc)) { // channel in transient processing
598  skip_bits(gbc, 10); // skip transient processing location
599  skip_bits(gbc, 8); // skip transient processing length
600  }
601  }
602  }
603 
604  /* spectral extension attenuation data */
605  for (ch = 1; ch <= s->fbw_channels; ch++) {
606  if (parse_spx_atten_data && get_bits1(gbc)) {
607  s->spx_atten_code[ch] = get_bits(gbc, 5);
608  } else {
609  s->spx_atten_code[ch] = -1;
610  }
611  }
612 
613  /* block start information */
614  if (s->num_blocks > 1 && get_bits1(gbc)) {
615  /* reference: Section E2.3.2.27
616  nblkstrtbits = (numblks - 1) * (4 + ceiling(log2(words_per_frame)))
617  The spec does not say what this data is or what it's used for.
618  It is likely the offset of each block within the frame. */
619  int block_start_bits = (s->num_blocks-1) * (4 + av_log2(s->frame_size-2));
620  skip_bits_long(gbc, block_start_bits);
621  avpriv_request_sample(s->avctx, "Block start info");
622  }
623 
624  /* syntax state initialization */
625  for (ch = 1; ch <= s->fbw_channels; ch++) {
626  s->first_spx_coords[ch] = 1;
627  s->first_cpl_coords[ch] = 1;
628  }
629  s->first_cpl_leak = 1;
630 
631  return 0;
632 }
float UINTFLOAT
Definition: aac_defines.h:87
EAC3GaqMode
gain adaptive quantization mode
Definition: eac3dec.c:56
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
const uint8_t ff_ac3_slow_decay_tab[4]
Definition: ac3tab.c:280
const float ff_eac3_spx_atten_tab[32][3]
Table E.25: Spectral Extension Attenuation Table ff_eac3_spx_atten_tab[code][bin]=pow(2.0,(bin+1)*(code+1)/-15.0);.
Definition: eac3_data.c:1101
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:381
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
const uint8_t ff_ac3_ungroup_3_in_5_bits_tab[32][3]
Table used to ungroup 3 values stored in 5 bits.
Definition: ac3dec_data.c:35
static int ff_eac3_parse_header(AC3DecodeContext *s)
Definition: eac3dec.c:297
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:293
#define AC3_MAX_COEFS
Definition: ac3.h:35
#define avpriv_request_sample(...)
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(const uint8_t *) pi - 0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(const int16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(const int16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(const int32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(const int32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(const int64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(const float *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(const double *) pi *(INT64_C(1)<< 63))) #define FMT_PAIR_FUNC(out, in) static conv_func_type *const fmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={ FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64), };static void cpy1(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, len);} static void cpy2(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 2 *len);} static void cpy4(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 4 *len);} static void cpy8(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 8 *len);} AudioConvert *swri_audio_convert_alloc(enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, const int *ch_map, int flags) { AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) return NULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) return NULL;if(channels==1){ in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);} ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map) { switch(av_get_bytes_per_sample(in_fmt)){ case 1:ctx->simd_f=cpy1;break;case 2:ctx->simd_f=cpy2;break;case 4:ctx->simd_f=cpy4;break;case 8:ctx->simd_f=cpy8;break;} } if(HAVE_X86ASM &&HAVE_MMX) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);return ctx;} void swri_audio_convert_free(AudioConvert **ctx) { av_freep(ctx);} int swri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, int len) { int ch;int off=0;const int os=(out->planar ? 1 :out->ch_count) *out->bps;unsigned misaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask) { int planes=in->planar ? in->ch_count :1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;} if(ctx->out_simd_align_mask) { int planes=out->planar ? out->ch_count :1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;} if(ctx->simd_f &&!ctx->ch_map &&!misaligned){ off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){ if(out->planar==in->planar){ int planes=out->planar ? out->ch_count :1;for(ch=0;ch< planes;ch++){ ctx->simd_f(out-> ch ch
Definition: audioconvert.c:56
const char * b
Definition: vf_curves.c:116
#define EXP_REUSE
Definition: ac3.h:48
const uint16_t ff_ac3_slow_gain_tab[4]
Definition: ac3tab.c:288
#define blk(i)
Definition: sha.c:185
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:361
float INTFLOAT
Definition: aac_defines.h:86
#define COEFF_0
lrint(M_SQRT2*cos(2*M_PI/12)*(1<<23))
Definition: eac3dec.c:162
uint8_t
#define f(width, name)
Definition: cbs_vp9.c:255
#define av_log(a,...)
static void ff_eac3_decode_transform_coeffs_aht_ch(AC3DecodeContext *s, int ch)
Definition: eac3dec.c:204
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:258
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
const uint8_t ff_ac3_fast_decay_tab[4]
Definition: ac3tab.c:284
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
#define COEFF_2
lrint(M_SQRT2*cos(5*M_PI/12)*(1<<23))
Definition: eac3dec.c:168
#define EAC3_SR_CODE_REDUCED
Definition: eac3dec.c:63
#define powf(x, y)
Definition: libm.h:50
static void idct6(int pre_mant[6])
Calculate 6-point IDCT of the pre-mantissas.
Definition: eac3dec.c:174
#define AC3_HEAVY_RANGE(x)
Definition: ac3.h:92
#define FFMIN(a, b)
Definition: common.h:96
int32_t
const int16_t ff_eac3_gaq_remap_1[12]
Table E3.6, Gk=1 No gain (Gk=1) inverse quantization, remapping scale factors ff_eac3_gaq_remap[hebap...
Definition: eac3_data.c:40
#define s(width, name)
Definition: cbs_vp9.c:257
const int16_t ff_eac3_gaq_remap_2_4_a[9][2]
Table E3.6, Gk=2 & Gk=4, A Large mantissa inverse quantization, remapping scale factors ff_eac3_gaq_r...
Definition: eac3_data.c:49
#define COEFF_1
lrint(M_SQRT2*cos(0*M_PI/12)*(1<<23)) = lrint(M_SQRT2*(1<<23))
Definition: eac3dec.c:165
#define INTFLOAT
const uint8_t ff_eac3_bits_vs_hebap[20]
Definition: eac3_data.c:30
#define av_log2
Definition: intmath.h:83
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define CPL_CH
coupling channel index
Definition: ac3.h:33
Libavcodec external API header.
const int16_t ff_eac3_gaq_remap_2_4_b[9][2]
Table E3.6, Gk=2 & Gk=4, B Large mantissa inverse quantization, negative mantissa remapping offsets f...
Definition: eac3_data.c:66
static void ff_eac3_apply_spectral_extension(AC3DecodeContext *s)
Definition: eac3dec.c:65
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition: lfg.h:53
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:487
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:523
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:460
const int16_t ff_ac3_floor_tab[8]
Definition: ac3tab.c:296
static int noise(AVBSFContext *ctx, AVPacket *pkt)
Definition: noise_bsf.c:38
const uint64_t ff_eac3_custom_channel_map_locations[16][2]
Definition: ac3tab.c:317
common internal api header.
const uint8_t ff_eac3_frm_expstr[32][6]
Table E2.14 Frame Exponent Strategy Combinations.
Definition: eac3_data.c:1062
#define EAC3_MAX_CHANNELS
maximum number of channels in EAC3
Definition: ac3.h:31
static const double coeff[2][5]
Definition: vf_owdenoise.c:72
const int16_t(*const [8] ff_eac3_mantissa_vq)[6]
Definition: eac3_data.c:1048
const uint16_t ff_ac3_db_per_bit_tab[4]
Definition: ac3tab.c:292
Common code between the AC-3 encoder and decoder.
static uint8_t tmp[11]
Definition: aes_ctr.c:26