FFmpeg  4.0
dcaenc.c
Go to the documentation of this file.
1 /*
2  * DCA encoder
3  * Copyright (C) 2008-2012 Alexander E. Patrakov
4  * 2010 Benjamin Larsson
5  * 2011 Xiang Wang
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 #define FFT_FLOAT 0
25 #define FFT_FIXED_32 1
26 
27 #include "libavutil/avassert.h"
29 #include "libavutil/common.h"
30 #include "libavutil/ffmath.h"
31 #include "libavutil/opt.h"
32 #include "avcodec.h"
33 #include "dca.h"
34 #include "dcaadpcm.h"
35 #include "dcamath.h"
36 #include "dca_core.h"
37 #include "dcadata.h"
38 #include "dcaenc.h"
39 #include "fft.h"
40 #include "internal.h"
41 #include "mathops.h"
42 #include "put_bits.h"
43 
44 #define MAX_CHANNELS 6
45 #define DCA_MAX_FRAME_SIZE 16384
46 #define DCA_HEADER_SIZE 13
47 #define DCA_LFE_SAMPLES 8
48 
49 #define DCAENC_SUBBANDS 32
50 #define SUBFRAMES 1
51 #define SUBSUBFRAMES 2
52 #define SUBBAND_SAMPLES (SUBFRAMES * SUBSUBFRAMES * 8)
53 #define AUBANDS 25
54 
55 #define COS_T(x) (c->cos_table[(x) & 2047])
56 
57 typedef struct CompressionOptions {
60 
61 typedef struct DCAEncContext {
62  AVClass *class;
70  int channels;
80  const int8_t *channel_order_tab; ///< channel reordering table, lfe and non lfe
81 
84  int32_t history[MAX_CHANNELS][512]; /* This is a circular buffer */
88  int32_t diff_peak_cb[MAX_CHANNELS][DCAENC_SUBBANDS]; ///< expected peak of residual signal
89  int32_t downsampled_lfe[DCA_LFE_SAMPLES];
90  int32_t masking_curve_cb[SUBSUBFRAMES][256];
91  int32_t bit_allocation_sel[MAX_CHANNELS];
93  int scale_factor[MAX_CHANNELS][DCAENC_SUBBANDS];
95  int32_t quant_index_sel[MAX_CHANNELS][DCA_CODE_BOOKS];
96  int32_t eff_masking_curve_cb[256];
97  int32_t band_masking_cb[32];
101  int consumed_adpcm_bits; ///< Number of bits to transmit ADPCM related info
102 
104  int32_t band_interpolation_tab[2][512];
105  int32_t band_spectrum_tab[2][8];
106  int32_t auf[9][AUBANDS][256];
107  int32_t cb_to_add[256];
108  int32_t cb_to_level[2048];
109  int32_t lfe_fir_64i[512];
110 } DCAEncContext;
111 
112 /* Transfer function of outer and middle ear, Hz -> dB */
113 static double hom(double f)
114 {
115  double f1 = f / 1000;
116 
117  return -3.64 * pow(f1, -0.8)
118  + 6.8 * exp(-0.6 * (f1 - 3.4) * (f1 - 3.4))
119  - 6.0 * exp(-0.15 * (f1 - 8.7) * (f1 - 8.7))
120  - 0.0006 * (f1 * f1) * (f1 * f1);
121 }
122 
123 static double gammafilter(int i, double f)
124 {
125  double h = (f - fc[i]) / erb[i];
126 
127  h = 1 + h * h;
128  h = 1 / (h * h);
129  return 20 * log10(h);
130 }
131 
133 {
134  int ch, band;
137  sizeof(int32_t));
138  if (!bufer)
139  return -1;
140 
141  /* we need a place for DCA_ADPCM_COEFF samples from previous frame
142  * to calc prediction coefficients for each subband */
143  for (ch = 0; ch < MAX_CHANNELS; ch++) {
144  for (band = 0; band < DCAENC_SUBBANDS; band++) {
145  c->subband[ch][band] = bufer +
146  ch * DCAENC_SUBBANDS * (SUBBAND_SAMPLES + DCA_ADPCM_COEFFS) +
148  }
149  }
150  return 0;
151 }
152 
154 {
155  int32_t *bufer = c->subband[0][0] - DCA_ADPCM_COEFFS;
156  av_freep(&bufer);
157 }
158 
159 static int encode_init(AVCodecContext *avctx)
160 {
161  DCAEncContext *c = avctx->priv_data;
162  uint64_t layout = avctx->channel_layout;
163  int i, j, k, min_frame_bits;
164  int ret;
165 
166  if (subband_bufer_alloc(c))
167  return AVERROR(ENOMEM);
168 
169  c->fullband_channels = c->channels = avctx->channels;
170  c->lfe_channel = (avctx->channels == 3 || avctx->channels == 6);
172  c->band_spectrum = c->band_spectrum_tab[1];
173  c->worst_quantization_noise = -2047;
174  c->worst_noise_ever = -2047;
175  c->consumed_adpcm_bits = 0;
176 
177  if (ff_dcaadpcm_init(&c->adpcm_ctx))
178  return AVERROR(ENOMEM);
179 
180  if (!layout) {
181  av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The "
182  "encoder will guess the layout, but it "
183  "might be incorrect.\n");
184  layout = av_get_default_channel_layout(avctx->channels);
185  }
186  switch (layout) {
187  case AV_CH_LAYOUT_MONO: c->channel_config = 0; break;
188  case AV_CH_LAYOUT_STEREO: c->channel_config = 2; break;
189  case AV_CH_LAYOUT_2_2: c->channel_config = 8; break;
190  case AV_CH_LAYOUT_5POINT0: c->channel_config = 9; break;
191  case AV_CH_LAYOUT_5POINT1: c->channel_config = 9; break;
192  default:
193  av_log(avctx, AV_LOG_ERROR, "Unsupported channel layout!\n");
194  return AVERROR_PATCHWELCOME;
195  }
196 
197  if (c->lfe_channel) {
198  c->fullband_channels--;
200  } else {
202  }
203 
204  for (i = 0; i < MAX_CHANNELS; i++) {
205  for (j = 0; j < DCA_CODE_BOOKS; j++) {
207  }
208  /* 6 - no Huffman */
209  c->bit_allocation_sel[i] = 6;
210 
211  for (j = 0; j < DCAENC_SUBBANDS; j++) {
212  /* -1 - no ADPCM */
213  c->prediction_mode[i][j] = -1;
214  memset(c->adpcm_history[i][j], 0, sizeof(int32_t)*DCA_ADPCM_COEFFS);
215  }
216  }
217 
218  for (i = 0; i < 9; i++) {
219  if (sample_rates[i] == avctx->sample_rate)
220  break;
221  }
222  if (i == 9)
223  return AVERROR(EINVAL);
224  c->samplerate_index = i;
225 
226  if (avctx->bit_rate < 32000 || avctx->bit_rate > 3840000) {
227  av_log(avctx, AV_LOG_ERROR, "Bit rate %"PRId64" not supported.", avctx->bit_rate);
228  return AVERROR(EINVAL);
229  }
230  for (i = 0; ff_dca_bit_rates[i] < avctx->bit_rate; i++)
231  ;
232  c->bitrate_index = i;
233  c->frame_bits = FFALIGN((avctx->bit_rate * 512 + avctx->sample_rate - 1) / avctx->sample_rate, 32);
234  min_frame_bits = 132 + (493 + 28 * 32) * c->fullband_channels + c->lfe_channel * 72;
235  if (c->frame_bits < min_frame_bits || c->frame_bits > (DCA_MAX_FRAME_SIZE << 3))
236  return AVERROR(EINVAL);
237 
238  c->frame_size = (c->frame_bits + 7) / 8;
239 
240  avctx->frame_size = 32 * SUBBAND_SAMPLES;
241 
242  if ((ret = ff_mdct_init(&c->mdct, 9, 0, 1.0)) < 0)
243  return ret;
244 
245  /* Init all tables */
246  c->cos_table[0] = 0x7fffffff;
247  c->cos_table[512] = 0;
248  c->cos_table[1024] = -c->cos_table[0];
249  for (i = 1; i < 512; i++) {
250  c->cos_table[i] = (int32_t)(0x7fffffff * cos(M_PI * i / 1024));
251  c->cos_table[1024-i] = -c->cos_table[i];
252  c->cos_table[1024+i] = -c->cos_table[i];
253  c->cos_table[2048-i] = +c->cos_table[i];
254  }
255 
256  for (i = 0; i < 2048; i++)
257  c->cb_to_level[i] = (int32_t)(0x7fffffff * ff_exp10(-0.005 * i));
258 
259  for (k = 0; k < 32; k++) {
260  for (j = 0; j < 8; j++) {
261  c->lfe_fir_64i[64 * j + k] = (int32_t)(0xffffff800000ULL * ff_dca_lfe_fir_64[8 * k + j]);
262  c->lfe_fir_64i[64 * (7-j) + (63 - k)] = (int32_t)(0xffffff800000ULL * ff_dca_lfe_fir_64[8 * k + j]);
263  }
264  }
265 
266  for (i = 0; i < 512; i++) {
267  c->band_interpolation_tab[0][i] = (int32_t)(0x1000000000ULL * ff_dca_fir_32bands_perfect[i]);
268  c->band_interpolation_tab[1][i] = (int32_t)(0x1000000000ULL * ff_dca_fir_32bands_nonperfect[i]);
269  }
270 
271  for (i = 0; i < 9; i++) {
272  for (j = 0; j < AUBANDS; j++) {
273  for (k = 0; k < 256; k++) {
274  double freq = sample_rates[i] * (k + 0.5) / 512;
275 
276  c->auf[i][j][k] = (int32_t)(10 * (hom(freq) + gammafilter(j, freq)));
277  }
278  }
279  }
280 
281  for (i = 0; i < 256; i++) {
282  double add = 1 + ff_exp10(-0.01 * i);
283  c->cb_to_add[i] = (int32_t)(100 * log10(add));
284  }
285  for (j = 0; j < 8; j++) {
286  double accum = 0;
287  for (i = 0; i < 512; i++) {
288  double reconst = ff_dca_fir_32bands_perfect[i] * ((i & 64) ? (-1) : 1);
289  accum += reconst * cos(2 * M_PI * (i + 0.5 - 256) * (j + 0.5) / 512);
290  }
291  c->band_spectrum_tab[0][j] = (int32_t)(200 * log10(accum));
292  }
293  for (j = 0; j < 8; j++) {
294  double accum = 0;
295  for (i = 0; i < 512; i++) {
296  double reconst = ff_dca_fir_32bands_nonperfect[i] * ((i & 64) ? (-1) : 1);
297  accum += reconst * cos(2 * M_PI * (i + 0.5 - 256) * (j + 0.5) / 512);
298  }
299  c->band_spectrum_tab[1][j] = (int32_t)(200 * log10(accum));
300  }
301 
302  return 0;
303 }
304 
306 {
307  DCAEncContext *c = avctx->priv_data;
308  ff_mdct_end(&c->mdct);
311 
312  return 0;
313 }
314 
315 static void subband_transform(DCAEncContext *c, const int32_t *input)
316 {
317  int ch, subs, i, k, j;
318 
319  for (ch = 0; ch < c->fullband_channels; ch++) {
320  /* History is copied because it is also needed for PSY */
321  int32_t hist[512];
322  int hist_start = 0;
323  const int chi = c->channel_order_tab[ch];
324 
325  memcpy(hist, &c->history[ch][0], 512 * sizeof(int32_t));
326 
327  for (subs = 0; subs < SUBBAND_SAMPLES; subs++) {
328  int32_t accum[64];
329  int32_t resp;
330  int band;
331 
332  /* Calculate the convolutions at once */
333  memset(accum, 0, 64 * sizeof(int32_t));
334 
335  for (k = 0, i = hist_start, j = 0;
336  i < 512; k = (k + 1) & 63, i++, j++)
337  accum[k] += mul32(hist[i], c->band_interpolation[j]);
338  for (i = 0; i < hist_start; k = (k + 1) & 63, i++, j++)
339  accum[k] += mul32(hist[i], c->band_interpolation[j]);
340 
341  for (k = 16; k < 32; k++)
342  accum[k] = accum[k] - accum[31 - k];
343  for (k = 32; k < 48; k++)
344  accum[k] = accum[k] + accum[95 - k];
345 
346  for (band = 0; band < 32; band++) {
347  resp = 0;
348  for (i = 16; i < 48; i++) {
349  int s = (2 * band + 1) * (2 * (i + 16) + 1);
350  resp += mul32(accum[i], COS_T(s << 3)) >> 3;
351  }
352 
353  c->subband[ch][band][subs] = ((band + 1) & 2) ? -resp : resp;
354  }
355 
356  /* Copy in 32 new samples from input */
357  for (i = 0; i < 32; i++)
358  hist[i + hist_start] = input[(subs * 32 + i) * c->channels + chi];
359 
360  hist_start = (hist_start + 32) & 511;
361  }
362  }
363 }
364 
365 static void lfe_downsample(DCAEncContext *c, const int32_t *input)
366 {
367  /* FIXME: make 128x LFE downsampling possible */
368  const int lfech = lfe_index[c->channel_config];
369  int i, j, lfes;
370  int32_t hist[512];
371  int32_t accum;
372  int hist_start = 0;
373 
374  memcpy(hist, &c->history[c->channels - 1][0], 512 * sizeof(int32_t));
375 
376  for (lfes = 0; lfes < DCA_LFE_SAMPLES; lfes++) {
377  /* Calculate the convolution */
378  accum = 0;
379 
380  for (i = hist_start, j = 0; i < 512; i++, j++)
381  accum += mul32(hist[i], c->lfe_fir_64i[j]);
382  for (i = 0; i < hist_start; i++, j++)
383  accum += mul32(hist[i], c->lfe_fir_64i[j]);
384 
385  c->downsampled_lfe[lfes] = accum;
386 
387  /* Copy in 64 new samples from input */
388  for (i = 0; i < 64; i++)
389  hist[i + hist_start] = input[(lfes * 64 + i) * c->channels + lfech];
390 
391  hist_start = (hist_start + 64) & 511;
392  }
393 }
394 
396 {
397  int i, res = 0;
398  in = FFABS(in);
399 
400  for (i = 1024; i > 0; i >>= 1) {
401  if (c->cb_to_level[i + res] >= in)
402  res += i;
403  }
404  return -res;
405 }
406 
408 {
409  if (a < b)
410  FFSWAP(int32_t, a, b);
411 
412  if (a - b >= 256)
413  return a;
414  return a + c->cb_to_add[a - b];
415 }
416 
418  const int32_t in[2 * 256], int32_t power[256])
419 {
420  int i;
421  LOCAL_ALIGNED_32(int32_t, data, [512]);
422  LOCAL_ALIGNED_32(int32_t, coeff, [256]);
423 
424  for (i = 0; i < 512; i++)
425  data[i] = norm__(mul32(in[i], 0x3fffffff - (COS_T(4 * i + 2) >> 1)), 4);
426 
427  c->mdct.mdct_calc(&c->mdct, coeff, data);
428  for (i = 0; i < 256; i++) {
429  const int32_t cb = get_cb(c, coeff[i]);
430  power[i] = add_cb(c, cb, cb);
431  }
432 }
433 
435  const int32_t in[512], int32_t out_cb[256])
436 {
437  int32_t power[256];
438  int32_t out_cb_unnorm[256];
439  int32_t denom;
440  const int32_t ca_cb = -1114;
441  const int32_t cs_cb = 928;
442  const int samplerate_index = c->samplerate_index;
443  int i, j;
444 
445  calc_power(c, in, power);
446 
447  for (j = 0; j < 256; j++)
448  out_cb_unnorm[j] = -2047; /* and can only grow */
449 
450  for (i = 0; i < AUBANDS; i++) {
451  denom = ca_cb; /* and can only grow */
452  for (j = 0; j < 256; j++)
453  denom = add_cb(c, denom, power[j] + c->auf[samplerate_index][i][j]);
454  for (j = 0; j < 256; j++)
455  out_cb_unnorm[j] = add_cb(c, out_cb_unnorm[j],
456  -denom + c->auf[samplerate_index][i][j]);
457  }
458 
459  for (j = 0; j < 256; j++)
460  out_cb[j] = add_cb(c, out_cb[j], -out_cb_unnorm[j] - ca_cb - cs_cb);
461 }
462 
463 typedef void (*walk_band_t)(DCAEncContext *c, int band1, int band2, int f,
464  int32_t spectrum1, int32_t spectrum2, int channel,
465  int32_t * arg);
466 
467 static void walk_band_low(DCAEncContext *c, int band, int channel,
468  walk_band_t walk, int32_t *arg)
469 {
470  int f;
471 
472  if (band == 0) {
473  for (f = 0; f < 4; f++)
474  walk(c, 0, 0, f, 0, -2047, channel, arg);
475  } else {
476  for (f = 0; f < 8; f++)
477  walk(c, band, band - 1, 8 * band - 4 + f,
478  c->band_spectrum[7 - f], c->band_spectrum[f], channel, arg);
479  }
480 }
481 
482 static void walk_band_high(DCAEncContext *c, int band, int channel,
483  walk_band_t walk, int32_t *arg)
484 {
485  int f;
486 
487  if (band == 31) {
488  for (f = 0; f < 4; f++)
489  walk(c, 31, 31, 256 - 4 + f, 0, -2047, channel, arg);
490  } else {
491  for (f = 0; f < 8; f++)
492  walk(c, band, band + 1, 8 * band + 4 + f,
493  c->band_spectrum[f], c->band_spectrum[7 - f], channel, arg);
494  }
495 }
496 
497 static void update_band_masking(DCAEncContext *c, int band1, int band2,
498  int f, int32_t spectrum1, int32_t spectrum2,
499  int channel, int32_t * arg)
500 {
501  int32_t value = c->eff_masking_curve_cb[f] - spectrum1;
502 
503  if (value < c->band_masking_cb[band1])
504  c->band_masking_cb[band1] = value;
505 }
506 
507 static void calc_masking(DCAEncContext *c, const int32_t *input)
508 {
509  int i, k, band, ch, ssf;
510  int32_t data[512];
511 
512  for (i = 0; i < 256; i++)
513  for (ssf = 0; ssf < SUBSUBFRAMES; ssf++)
514  c->masking_curve_cb[ssf][i] = -2047;
515 
516  for (ssf = 0; ssf < SUBSUBFRAMES; ssf++)
517  for (ch = 0; ch < c->fullband_channels; ch++) {
518  const int chi = c->channel_order_tab[ch];
519 
520  for (i = 0, k = 128 + 256 * ssf; k < 512; i++, k++)
521  data[i] = c->history[ch][k];
522  for (k -= 512; i < 512; i++, k++)
523  data[i] = input[k * c->channels + chi];
524  adjust_jnd(c, data, c->masking_curve_cb[ssf]);
525  }
526  for (i = 0; i < 256; i++) {
527  int32_t m = 2048;
528 
529  for (ssf = 0; ssf < SUBSUBFRAMES; ssf++)
530  if (c->masking_curve_cb[ssf][i] < m)
531  m = c->masking_curve_cb[ssf][i];
532  c->eff_masking_curve_cb[i] = m;
533  }
534 
535  for (band = 0; band < 32; band++) {
536  c->band_masking_cb[band] = 2048;
537  walk_band_low(c, band, 0, update_band_masking, NULL);
539  }
540 }
541 
542 static inline int32_t find_peak(DCAEncContext *c, const int32_t *in, int len)
543 {
544  int sample;
545  int32_t m = 0;
546  for (sample = 0; sample < len; sample++) {
547  int32_t s = abs(in[sample]);
548  if (m < s)
549  m = s;
550  }
551  return get_cb(c, m);
552 }
553 
555 {
556  int band, ch;
557 
558  for (ch = 0; ch < c->fullband_channels; ch++) {
559  for (band = 0; band < 32; band++)
560  c->peak_cb[ch][band] = find_peak(c, c->subband[ch][band],
562  }
563 
564  if (c->lfe_channel)
566 }
567 
569 {
570  int ch, band;
571  int pred_vq_id;
572  int32_t *samples;
573  int32_t estimated_diff[SUBBAND_SAMPLES];
574 
575  c->consumed_adpcm_bits = 0;
576  for (ch = 0; ch < c->fullband_channels; ch++) {
577  for (band = 0; band < 32; band++) {
578  samples = c->subband[ch][band] - DCA_ADPCM_COEFFS;
579  pred_vq_id = ff_dcaadpcm_subband_analysis(&c->adpcm_ctx, samples,
580  SUBBAND_SAMPLES, estimated_diff);
581  if (pred_vq_id >= 0) {
582  c->prediction_mode[ch][band] = pred_vq_id;
583  c->consumed_adpcm_bits += 12; //12 bits to transmit prediction vq index
584  c->diff_peak_cb[ch][band] = find_peak(c, estimated_diff, 16);
585  } else {
586  c->prediction_mode[ch][band] = -1;
587  }
588  }
589  }
590 }
591 
592 static const int snr_fudge = 128;
593 #define USED_1ABITS 1
594 #define USED_26ABITS 4
595 
596 static inline int32_t get_step_size(DCAEncContext *c, int ch, int band)
597 {
598  int32_t step_size;
599 
600  if (c->bitrate_index == 3)
601  step_size = ff_dca_lossless_quant[c->abits[ch][band]];
602  else
603  step_size = ff_dca_lossy_quant[c->abits[ch][band]];
604 
605  return step_size;
606 }
607 
608 static int calc_one_scale(DCAEncContext *c, int32_t peak_cb, int abits,
609  softfloat *quant)
610 {
611  int32_t peak;
612  int our_nscale, try_remove;
613  softfloat our_quant;
614 
615  av_assert0(peak_cb <= 0);
616  av_assert0(peak_cb >= -2047);
617 
618  our_nscale = 127;
619  peak = c->cb_to_level[-peak_cb];
620 
621  for (try_remove = 64; try_remove > 0; try_remove >>= 1) {
622  if (scalefactor_inv[our_nscale - try_remove].e + stepsize_inv[abits].e <= 17)
623  continue;
624  our_quant.m = mul32(scalefactor_inv[our_nscale - try_remove].m, stepsize_inv[abits].m);
625  our_quant.e = scalefactor_inv[our_nscale - try_remove].e + stepsize_inv[abits].e - 17;
626  if ((ff_dca_quant_levels[abits] - 1) / 2 < quantize_value(peak, our_quant))
627  continue;
628  our_nscale -= try_remove;
629  }
630 
631  if (our_nscale >= 125)
632  our_nscale = 124;
633 
634  quant->m = mul32(scalefactor_inv[our_nscale].m, stepsize_inv[abits].m);
635  quant->e = scalefactor_inv[our_nscale].e + stepsize_inv[abits].e - 17;
636  av_assert0((ff_dca_quant_levels[abits] - 1) / 2 >= quantize_value(peak, *quant));
637 
638  return our_nscale;
639 }
640 
641 static inline void quantize_adpcm_subband(DCAEncContext *c, int ch, int band)
642 {
643  int32_t step_size;
644  int32_t diff_peak_cb = c->diff_peak_cb[ch][band];
645  c->scale_factor[ch][band] = calc_one_scale(c, diff_peak_cb,
646  c->abits[ch][band],
647  &c->quant[ch][band]);
648 
649  step_size = get_step_size(c, ch, band);
651  c->quant[ch][band],
653  step_size, c->adpcm_history[ch][band], c->subband[ch][band],
654  c->adpcm_history[ch][band] + 4, c->quantized[ch][band],
655  SUBBAND_SAMPLES, c->cb_to_level[-diff_peak_cb]);
656 }
657 
659 {
660  int band, ch;
661 
662  for (ch = 0; ch < c->fullband_channels; ch++)
663  for (band = 0; band < 32; band++)
664  if (c->prediction_mode[ch][band] >= 0)
665  quantize_adpcm_subband(c, ch, band);
666 }
667 
669 {
670  int sample, band, ch;
671 
672  for (ch = 0; ch < c->fullband_channels; ch++) {
673  for (band = 0; band < 32; band++) {
674  if (c->prediction_mode[ch][band] == -1) {
675  for (sample = 0; sample < SUBBAND_SAMPLES; sample++) {
676  int32_t val = quantize_value(c->subband[ch][band][sample],
677  c->quant[ch][band]);
678  c->quantized[ch][band][sample] = val;
679  }
680  }
681  }
682  }
683 }
684 
685 static void accumulate_huff_bit_consumption(int abits, int32_t *quantized,
686  uint32_t *result)
687 {
688  uint8_t sel, id = abits - 1;
689  for (sel = 0; sel < ff_dca_quant_index_group_size[id]; sel++)
690  result[sel] += ff_dca_vlc_calc_quant_bits(quantized, SUBBAND_SAMPLES,
691  sel, id);
692 }
693 
694 static uint32_t set_best_code(uint32_t vlc_bits[DCA_CODE_BOOKS][7],
695  uint32_t clc_bits[DCA_CODE_BOOKS],
696  int32_t res[DCA_CODE_BOOKS])
697 {
698  uint8_t i, sel;
699  uint32_t best_sel_bits[DCA_CODE_BOOKS];
700  int32_t best_sel_id[DCA_CODE_BOOKS];
701  uint32_t t, bits = 0;
702 
703  for (i = 0; i < DCA_CODE_BOOKS; i++) {
704 
705  av_assert0(!((!!vlc_bits[i][0]) ^ (!!clc_bits[i])));
706  if (vlc_bits[i][0] == 0) {
707  /* do not transmit adjustment index for empty codebooks */
708  res[i] = ff_dca_quant_index_group_size[i];
709  /* and skip it */
710  continue;
711  }
712 
713  best_sel_bits[i] = vlc_bits[i][0];
714  best_sel_id[i] = 0;
715  for (sel = 0; sel < ff_dca_quant_index_group_size[i]; sel++) {
716  if (best_sel_bits[i] > vlc_bits[i][sel] && vlc_bits[i][sel]) {
717  best_sel_bits[i] = vlc_bits[i][sel];
718  best_sel_id[i] = sel;
719  }
720  }
721 
722  /* 2 bits to transmit scale factor adjustment index */
723  t = best_sel_bits[i] + 2;
724  if (t < clc_bits[i]) {
725  res[i] = best_sel_id[i];
726  bits += t;
727  } else {
728  res[i] = ff_dca_quant_index_group_size[i];
729  bits += clc_bits[i];
730  }
731  }
732  return bits;
733 }
734 
735 static uint32_t set_best_abits_code(int abits[DCAENC_SUBBANDS], int bands,
736  int32_t *res)
737 {
738  uint8_t i;
739  uint32_t t;
740  int32_t best_sel = 6;
741  int32_t best_bits = bands * 5;
742 
743  /* Check do we have subband which cannot be encoded by Huffman tables */
744  for (i = 0; i < bands; i++) {
745  if (abits[i] > 12 || abits[i] == 0) {
746  *res = best_sel;
747  return best_bits;
748  }
749  }
750 
751  for (i = 0; i < DCA_BITALLOC_12_COUNT; i++) {
752  t = ff_dca_vlc_calc_alloc_bits(abits, bands, i);
753  if (t < best_bits) {
754  best_bits = t;
755  best_sel = i;
756  }
757  }
758 
759  *res = best_sel;
760  return best_bits;
761 }
762 
763 static int init_quantization_noise(DCAEncContext *c, int noise, int forbid_zero)
764 {
765  int ch, band, ret = USED_26ABITS | USED_1ABITS;
766  uint32_t huff_bit_count_accum[MAX_CHANNELS][DCA_CODE_BOOKS][7];
767  uint32_t clc_bit_count_accum[MAX_CHANNELS][DCA_CODE_BOOKS];
768  uint32_t bits_counter = 0;
769 
770  c->consumed_bits = 132 + 333 * c->fullband_channels;
772  if (c->lfe_channel)
773  c->consumed_bits += 72;
774 
775  /* attempt to guess the bit distribution based on the prevoius frame */
776  for (ch = 0; ch < c->fullband_channels; ch++) {
777  for (band = 0; band < 32; band++) {
778  int snr_cb = c->peak_cb[ch][band] - c->band_masking_cb[band] - noise;
779 
780  if (snr_cb >= 1312) {
781  c->abits[ch][band] = 26;
782  ret &= ~USED_1ABITS;
783  } else if (snr_cb >= 222) {
784  c->abits[ch][band] = 8 + mul32(snr_cb - 222, 69000000);
785  ret &= ~(USED_26ABITS | USED_1ABITS);
786  } else if (snr_cb >= 0) {
787  c->abits[ch][band] = 2 + mul32(snr_cb, 106000000);
788  ret &= ~(USED_26ABITS | USED_1ABITS);
789  } else if (forbid_zero || snr_cb >= -140) {
790  c->abits[ch][band] = 1;
791  ret &= ~USED_26ABITS;
792  } else {
793  c->abits[ch][band] = 0;
794  ret &= ~(USED_26ABITS | USED_1ABITS);
795  }
796  }
797  c->consumed_bits += set_best_abits_code(c->abits[ch], 32,
798  &c->bit_allocation_sel[ch]);
799  }
800 
801  /* Recalc scale_factor each time to get bits consumption in case of Huffman coding.
802  It is suboptimal solution */
803  /* TODO: May be cache scaled values */
804  for (ch = 0; ch < c->fullband_channels; ch++) {
805  for (band = 0; band < 32; band++) {
806  if (c->prediction_mode[ch][band] == -1) {
807  c->scale_factor[ch][band] = calc_one_scale(c, c->peak_cb[ch][band],
808  c->abits[ch][band],
809  &c->quant[ch][band]);
810  }
811  }
812  }
813  quantize_adpcm(c);
814  quantize_pcm(c);
815 
816  memset(huff_bit_count_accum, 0, MAX_CHANNELS * DCA_CODE_BOOKS * 7 * sizeof(uint32_t));
817  memset(clc_bit_count_accum, 0, MAX_CHANNELS * DCA_CODE_BOOKS * sizeof(uint32_t));
818  for (ch = 0; ch < c->fullband_channels; ch++) {
819  for (band = 0; band < 32; band++) {
820  if (c->abits[ch][band] && c->abits[ch][band] <= DCA_CODE_BOOKS) {
822  c->quantized[ch][band],
823  huff_bit_count_accum[ch][c->abits[ch][band] - 1]);
824  clc_bit_count_accum[ch][c->abits[ch][band] - 1] += bit_consumption[c->abits[ch][band]];
825  } else {
826  bits_counter += bit_consumption[c->abits[ch][band]];
827  }
828  }
829  }
830 
831  for (ch = 0; ch < c->fullband_channels; ch++) {
832  bits_counter += set_best_code(huff_bit_count_accum[ch],
833  clc_bit_count_accum[ch],
834  c->quant_index_sel[ch]);
835  }
836 
837  c->consumed_bits += bits_counter;
838 
839  return ret;
840 }
841 
843 {
844  /* Find the bounds where the binary search should work */
845  int low, high, down;
846  int used_abits = 0;
847  int forbid_zero = 1;
848 restart:
850  low = high = c->worst_quantization_noise;
851  if (c->consumed_bits > c->frame_bits) {
852  while (c->consumed_bits > c->frame_bits) {
853  if (used_abits == USED_1ABITS && forbid_zero) {
854  forbid_zero = 0;
855  goto restart;
856  }
857  low = high;
858  high += snr_fudge;
859  used_abits = init_quantization_noise(c, high, forbid_zero);
860  }
861  } else {
862  while (c->consumed_bits <= c->frame_bits) {
863  high = low;
864  if (used_abits == USED_26ABITS)
865  goto out; /* The requested bitrate is too high, pad with zeros */
866  low -= snr_fudge;
867  used_abits = init_quantization_noise(c, low, forbid_zero);
868  }
869  }
870 
871  /* Now do a binary search between low and high to see what fits */
872  for (down = snr_fudge >> 1; down; down >>= 1) {
873  init_quantization_noise(c, high - down, forbid_zero);
874  if (c->consumed_bits <= c->frame_bits)
875  high -= down;
876  }
877  init_quantization_noise(c, high, forbid_zero);
878 out:
879  c->worst_quantization_noise = high;
880  if (high > c->worst_noise_ever)
881  c->worst_noise_ever = high;
882 }
883 
884 static void shift_history(DCAEncContext *c, const int32_t *input)
885 {
886  int k, ch;
887 
888  for (k = 0; k < 512; k++)
889  for (ch = 0; ch < c->channels; ch++) {
890  const int chi = c->channel_order_tab[ch];
891 
892  c->history[ch][k] = input[k * c->channels + chi];
893  }
894 }
895 
897 {
898  int ch, band;
899  int32_t step_size;
900  /* We fill in ADPCM work buffer for subbands which hasn't been ADPCM coded
901  * in current frame - we need this data if subband of next frame is
902  * ADPCM
903  */
904  for (ch = 0; ch < c->channels; ch++) {
905  for (band = 0; band < 32; band++) {
906  int32_t *samples = c->subband[ch][band] - DCA_ADPCM_COEFFS;
907  if (c->prediction_mode[ch][band] == -1) {
908  step_size = get_step_size(c, ch, band);
909 
911  c->quantized[ch][band]+12, step_size,
912  ff_dca_scale_factor_quant7[c->scale_factor[ch][band]], 0, 4);
913  } else {
914  AV_COPY128U(c->adpcm_history[ch][band], c->adpcm_history[ch][band]+4);
915  }
916  /* Copy dequantized values for LPC analysis.
917  * It reduces artifacts in case of extreme quantization,
918  * example: in current frame abits is 1 and has no prediction flag,
919  * but end of this frame is sine like signal. In this case, if LPC analysis uses
920  * original values, likely LPC analysis returns good prediction gain, and sets prediction flag.
921  * But there are no proper value in decoder history, so likely result will be no good.
922  * Bitstream has "Predictor history flag switch", but this flag disables history for all subbands
923  */
924  samples[0] = c->adpcm_history[ch][band][0] << 7;
925  samples[1] = c->adpcm_history[ch][band][1] << 7;
926  samples[2] = c->adpcm_history[ch][band][2] << 7;
927  samples[3] = c->adpcm_history[ch][band][3] << 7;
928  }
929  }
930 }
931 
933 {
934  if (c->lfe_channel)
936 }
937 
939 {
940  /* SYNC */
941  put_bits(&c->pb, 16, 0x7ffe);
942  put_bits(&c->pb, 16, 0x8001);
943 
944  /* Frame type: normal */
945  put_bits(&c->pb, 1, 1);
946 
947  /* Deficit sample count: none */
948  put_bits(&c->pb, 5, 31);
949 
950  /* CRC is not present */
951  put_bits(&c->pb, 1, 0);
952 
953  /* Number of PCM sample blocks */
954  put_bits(&c->pb, 7, SUBBAND_SAMPLES - 1);
955 
956  /* Primary frame byte size */
957  put_bits(&c->pb, 14, c->frame_size - 1);
958 
959  /* Audio channel arrangement */
960  put_bits(&c->pb, 6, c->channel_config);
961 
962  /* Core audio sampling frequency */
964 
965  /* Transmission bit rate */
966  put_bits(&c->pb, 5, c->bitrate_index);
967 
968  /* Embedded down mix: disabled */
969  put_bits(&c->pb, 1, 0);
970 
971  /* Embedded dynamic range flag: not present */
972  put_bits(&c->pb, 1, 0);
973 
974  /* Embedded time stamp flag: not present */
975  put_bits(&c->pb, 1, 0);
976 
977  /* Auxiliary data flag: not present */
978  put_bits(&c->pb, 1, 0);
979 
980  /* HDCD source: no */
981  put_bits(&c->pb, 1, 0);
982 
983  /* Extension audio ID: N/A */
984  put_bits(&c->pb, 3, 0);
985 
986  /* Extended audio data: not present */
987  put_bits(&c->pb, 1, 0);
988 
989  /* Audio sync word insertion flag: after each sub-frame */
990  put_bits(&c->pb, 1, 0);
991 
992  /* Low frequency effects flag: not present or 64x subsampling */
993  put_bits(&c->pb, 2, c->lfe_channel ? 2 : 0);
994 
995  /* Predictor history switch flag: on */
996  put_bits(&c->pb, 1, 1);
997 
998  /* No CRC */
999  /* Multirate interpolator switch: non-perfect reconstruction */
1000  put_bits(&c->pb, 1, 0);
1001 
1002  /* Encoder software revision: 7 */
1003  put_bits(&c->pb, 4, 7);
1004 
1005  /* Copy history: 0 */
1006  put_bits(&c->pb, 2, 0);
1007 
1008  /* Source PCM resolution: 16 bits, not DTS ES */
1009  put_bits(&c->pb, 3, 0);
1010 
1011  /* Front sum/difference coding: no */
1012  put_bits(&c->pb, 1, 0);
1013 
1014  /* Surrounds sum/difference coding: no */
1015  put_bits(&c->pb, 1, 0);
1016 
1017  /* Dialog normalization: 0 dB */
1018  put_bits(&c->pb, 4, 0);
1019 }
1020 
1022 {
1023  int ch, i;
1024  /* Number of subframes */
1025  put_bits(&c->pb, 4, SUBFRAMES - 1);
1026 
1027  /* Number of primary audio channels */
1028  put_bits(&c->pb, 3, c->fullband_channels - 1);
1029 
1030  /* Subband activity count */
1031  for (ch = 0; ch < c->fullband_channels; ch++)
1032  put_bits(&c->pb, 5, DCAENC_SUBBANDS - 2);
1033 
1034  /* High frequency VQ start subband */
1035  for (ch = 0; ch < c->fullband_channels; ch++)
1036  put_bits(&c->pb, 5, DCAENC_SUBBANDS - 1);
1037 
1038  /* Joint intensity coding index: 0, 0 */
1039  for (ch = 0; ch < c->fullband_channels; ch++)
1040  put_bits(&c->pb, 3, 0);
1041 
1042  /* Transient mode codebook: A4, A4 (arbitrary) */
1043  for (ch = 0; ch < c->fullband_channels; ch++)
1044  put_bits(&c->pb, 2, 0);
1045 
1046  /* Scale factor code book: 7 bit linear, 7-bit sqrt table (for each channel) */
1047  for (ch = 0; ch < c->fullband_channels; ch++)
1048  put_bits(&c->pb, 3, 6);
1049 
1050  /* Bit allocation quantizer select: linear 5-bit */
1051  for (ch = 0; ch < c->fullband_channels; ch++)
1052  put_bits(&c->pb, 3, c->bit_allocation_sel[ch]);
1053 
1054  /* Quantization index codebook select */
1055  for (i = 0; i < DCA_CODE_BOOKS; i++)
1056  for (ch = 0; ch < c->fullband_channels; ch++)
1058 
1059  /* Scale factor adjustment index: transmitted in case of Huffman coding */
1060  for (i = 0; i < DCA_CODE_BOOKS; i++)
1061  for (ch = 0; ch < c->fullband_channels; ch++)
1063  put_bits(&c->pb, 2, 0);
1064 
1065  /* Audio header CRC check word: not transmitted */
1066 }
1067 
1068 static void put_subframe_samples(DCAEncContext *c, int ss, int band, int ch)
1069 {
1070  int i, j, sum, bits, sel;
1071  if (c->abits[ch][band] <= DCA_CODE_BOOKS) {
1072  av_assert0(c->abits[ch][band] > 0);
1073  sel = c->quant_index_sel[ch][c->abits[ch][band] - 1];
1074  // Huffman codes
1075  if (sel < ff_dca_quant_index_group_size[c->abits[ch][band] - 1]) {
1076  ff_dca_vlc_enc_quant(&c->pb, &c->quantized[ch][band][ss * 8], 8,
1077  sel, c->abits[ch][band] - 1);
1078  return;
1079  }
1080 
1081  // Block codes
1082  if (c->abits[ch][band] <= 7) {
1083  for (i = 0; i < 8; i += 4) {
1084  sum = 0;
1085  for (j = 3; j >= 0; j--) {
1086  sum *= ff_dca_quant_levels[c->abits[ch][band]];
1087  sum += c->quantized[ch][band][ss * 8 + i + j];
1088  sum += (ff_dca_quant_levels[c->abits[ch][band]] - 1) / 2;
1089  }
1090  put_bits(&c->pb, bit_consumption[c->abits[ch][band]] / 4, sum);
1091  }
1092  return;
1093  }
1094  }
1095 
1096  for (i = 0; i < 8; i++) {
1097  bits = bit_consumption[c->abits[ch][band]] / 16;
1098  put_sbits(&c->pb, bits, c->quantized[ch][band][ss * 8 + i]);
1099  }
1100 }
1101 
1102 static void put_subframe(DCAEncContext *c, int subframe)
1103 {
1104  int i, band, ss, ch;
1105 
1106  /* Subsubframes count */
1107  put_bits(&c->pb, 2, SUBSUBFRAMES -1);
1108 
1109  /* Partial subsubframe sample count: dummy */
1110  put_bits(&c->pb, 3, 0);
1111 
1112  /* Prediction mode: no ADPCM, in each channel and subband */
1113  for (ch = 0; ch < c->fullband_channels; ch++)
1114  for (band = 0; band < DCAENC_SUBBANDS; band++)
1115  put_bits(&c->pb, 1, !(c->prediction_mode[ch][band] == -1));
1116 
1117  /* Prediction VQ address */
1118  for (ch = 0; ch < c->fullband_channels; ch++)
1119  for (band = 0; band < DCAENC_SUBBANDS; band++)
1120  if (c->prediction_mode[ch][band] >= 0)
1121  put_bits(&c->pb, 12, c->prediction_mode[ch][band]);
1122 
1123  /* Bit allocation index */
1124  for (ch = 0; ch < c->fullband_channels; ch++) {
1125  if (c->bit_allocation_sel[ch] == 6) {
1126  for (band = 0; band < DCAENC_SUBBANDS; band++) {
1127  put_bits(&c->pb, 5, c->abits[ch][band]);
1128  }
1129  } else {
1130  ff_dca_vlc_enc_alloc(&c->pb, c->abits[ch], DCAENC_SUBBANDS,
1131  c->bit_allocation_sel[ch]);
1132  }
1133  }
1134 
1135  if (SUBSUBFRAMES > 1) {
1136  /* Transition mode: none for each channel and subband */
1137  for (ch = 0; ch < c->fullband_channels; ch++)
1138  for (band = 0; band < DCAENC_SUBBANDS; band++)
1139  if (c->abits[ch][band])
1140  put_bits(&c->pb, 1, 0); /* codebook A4 */
1141  }
1142 
1143  /* Scale factors */
1144  for (ch = 0; ch < c->fullband_channels; ch++)
1145  for (band = 0; band < DCAENC_SUBBANDS; band++)
1146  if (c->abits[ch][band])
1147  put_bits(&c->pb, 7, c->scale_factor[ch][band]);
1148 
1149  /* Joint subband scale factor codebook select: not transmitted */
1150  /* Scale factors for joint subband coding: not transmitted */
1151  /* Stereo down-mix coefficients: not transmitted */
1152  /* Dynamic range coefficient: not transmitted */
1153  /* Stde information CRC check word: not transmitted */
1154  /* VQ encoded high frequency subbands: not transmitted */
1155 
1156  /* LFE data: 8 samples and scalefactor */
1157  if (c->lfe_channel) {
1158  for (i = 0; i < DCA_LFE_SAMPLES; i++)
1159  put_bits(&c->pb, 8, quantize_value(c->downsampled_lfe[i], c->lfe_quant) & 0xff);
1160  put_bits(&c->pb, 8, c->lfe_scale_factor);
1161  }
1162 
1163  /* Audio data (subsubframes) */
1164  for (ss = 0; ss < SUBSUBFRAMES ; ss++)
1165  for (ch = 0; ch < c->fullband_channels; ch++)
1166  for (band = 0; band < DCAENC_SUBBANDS; band++)
1167  if (c->abits[ch][band])
1168  put_subframe_samples(c, ss, band, ch);
1169 
1170  /* DSYNC */
1171  put_bits(&c->pb, 16, 0xffff);
1172 }
1173 
1174 static int encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
1175  const AVFrame *frame, int *got_packet_ptr)
1176 {
1177  DCAEncContext *c = avctx->priv_data;
1178  const int32_t *samples;
1179  int ret, i;
1180 
1181  if ((ret = ff_alloc_packet2(avctx, avpkt, c->frame_size, 0)) < 0)
1182  return ret;
1183 
1184  samples = (const int32_t *)frame->data[0];
1185 
1186  subband_transform(c, samples);
1187  if (c->lfe_channel)
1188  lfe_downsample(c, samples);
1189 
1190  calc_masking(c, samples);
1191  if (c->options.adpcm_mode)
1192  adpcm_analysis(c);
1193  find_peaks(c);
1194  assign_bits(c);
1195  calc_lfe_scales(c);
1196  shift_history(c, samples);
1197 
1198  init_put_bits(&c->pb, avpkt->data, avpkt->size);
1200  put_frame_header(c);
1202  for (i = 0; i < SUBFRAMES; i++)
1203  put_subframe(c, i);
1204 
1205 
1206  for (i = put_bits_count(&c->pb); i < 8*c->frame_size; i++)
1207  put_bits(&c->pb, 1, 0);
1208 
1209  flush_put_bits(&c->pb);
1210 
1211  avpkt->pts = frame->pts;
1212  avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
1213  avpkt->size = put_bits_count(&c->pb) >> 3;
1214  *got_packet_ptr = 1;
1215  return 0;
1216 }
1217 
1218 #define DCAENC_FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
1219 
1220 static const AVOption options[] = {
1221  { "dca_adpcm", "Use ADPCM encoding", offsetof(DCAEncContext, options.adpcm_mode), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DCAENC_FLAGS },
1222  { NULL },
1223 };
1224 
1225 static const AVClass dcaenc_class = {
1226  .class_name = "DCA (DTS Coherent Acoustics)",
1227  .item_name = av_default_item_name,
1228  .option = options,
1229  .version = LIBAVUTIL_VERSION_INT,
1230 };
1231 
1232 static const AVCodecDefault defaults[] = {
1233  { "b", "1411200" },
1234  { NULL },
1235 };
1236 
1238  .name = "dca",
1239  .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
1240  .type = AVMEDIA_TYPE_AUDIO,
1241  .id = AV_CODEC_ID_DTS,
1242  .priv_data_size = sizeof(DCAEncContext),
1243  .init = encode_init,
1244  .close = encode_close,
1245  .encode2 = encode_frame,
1246  .capabilities = AV_CODEC_CAP_EXPERIMENTAL,
1248  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S32,
1250  .supported_samplerates = sample_rates,
1251  .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
1256  0 },
1257  .defaults = defaults,
1258  .priv_class = &dcaenc_class,
1259 };
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:48
av_cold int ff_dcaadpcm_init(DCAADPCMEncContext *s)
Definition: dcaadpcm.c:212
static int32_t find_peak(DCAEncContext *c, const int32_t *in, int len)
Definition: dcaenc.c:542
#define MAX_CHANNELS
Definition: dcaenc.c:44
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:771
const char * s
Definition: avisynth_c.h:768
int32_t diff_peak_cb[MAX_CHANNELS][DCAENC_SUBBANDS]
expected peak of residual signal
Definition: dcaenc.c:88
int32_t m
Definition: dcaenc.h:30
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
uint32_t ff_dca_vlc_calc_alloc_bits(int *values, uint8_t n, uint8_t sel)
Definition: dcahuff.c:1360
AVOption.
Definition: opt.h:246
int32_t eff_masking_curve_cb[256]
Definition: dcaenc.c:96
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:240
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:207
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
static void put_frame_header(DCAEncContext *c)
Definition: dcaenc.c:938
const uint32_t ff_dca_lossy_quant[32]
Definition: dcadata.c:4223
int64_t bit_rate
the average bitrate
Definition: avcodec.h:1568
#define AUBANDS
Definition: dcaenc.c:53
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
const int8_t * channel_order_tab
channel reordering table, lfe and non lfe
Definition: dcaenc.c:80
int size
Definition: avcodec.h:1431
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:113
static const uint8_t bitstream_sfreq[]
Definition: dcaenc.h:38
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
static const uint16_t erb[]
Definition: dcaenc.h:48
#define AV_CODEC_CAP_EXPERIMENTAL
Codec is experimental and is thus avoided in favor of non experimental encoders.
Definition: avcodec.h:1007
static int calc_one_scale(DCAEncContext *c, int32_t peak_cb, int abits, softfloat *quant)
Definition: dcaenc.c:608
static void shift_history(DCAEncContext *c, const int32_t *input)
Definition: dcaenc.c:884
softfloat lfe_quant
Definition: dcaenc.c:78
#define AV_CH_LAYOUT_STEREO
static int encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: dcaenc.c:1174
#define sample
static void walk_band_high(DCAEncContext *c, int band, int channel, walk_band_t walk, int32_t *arg)
Definition: dcaenc.c:482
AVCodec.
Definition: avcodec.h:3408
#define AV_CH_LAYOUT_5POINT0
CompressionOptions options
Definition: dcaenc.c:66
int abits[MAX_CHANNELS][DCAENC_SUBBANDS]
Definition: dcaenc.c:92
static av_cold int encode_close(AVCodecContext *avctx)
Definition: dcaenc.c:305
int frame_size
Definition: dcaenc.c:67
const float ff_dca_fir_32bands_nonperfect[512]
Definition: dcadata.c:6808
static void walk_band_low(DCAEncContext *c, int band, int channel, walk_band_t walk, int32_t *arg)
Definition: dcaenc.c:467
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:244
int ff_dcaadpcm_do_real(int pred_vq_index, softfloat quant, int32_t scale_factor, int32_t step_size, const int32_t *prev_hist, const int32_t *in, int32_t *next_hist, int32_t *out, int len, int32_t peak)
Definition: dcaadpcm.c:183
static int32_t quantize_value(int32_t value, softfloat quant)
Definition: dcaenc.h:149
softfloat quant[MAX_CHANNELS][DCAENC_SUBBANDS]
Definition: dcaenc.c:94
static void accumulate_huff_bit_consumption(int abits, int32_t *quantized, uint32_t *result)
Definition: dcaenc.c:685
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
int32_t cb_to_add[256]
Definition: dcaenc.c:107
static int32_t get_cb(DCAEncContext *c, int32_t in)
Definition: dcaenc.c:395
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#define SUBSUBFRAMES
Definition: dcaenc.c:51
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:32
static double cb(void *priv, double x, double y)
Definition: vf_geq.c:112
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:40
static void calc_masking(DCAEncContext *c, const int32_t *input)
Definition: dcaenc.c:507
static const softfloat stepsize_inv[27]
Definition: dcaenc.h:53
const uint32_t ff_dca_bit_rates[32]
Definition: dcadata.c:32
uint8_t
#define av_cold
Definition: attributes.h:82
AVOptions.
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1448
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:311
int32_t masking_curve_cb[SUBSUBFRAMES][256]
Definition: dcaenc.c:90
static AVFrame * frame
int32_t cb_to_level[2048]
Definition: dcaenc.c:108
const char data[16]
Definition: mxf.c:90
uint8_t * data
Definition: avcodec.h:1430
int frame_bits
Definition: dcaenc.c:68
int lfe_channel
Definition: dcaenc.c:71
static void adjust_jnd(DCAEncContext *c, const int32_t in[512], int32_t out_cb[256])
Definition: dcaenc.c:434
static void ff_dca_core_dequantize(int32_t *output, const int32_t *input, int32_t step_size, int32_t scale, int residual, int len)
Definition: dca_core.h:227
signed 32 bits
Definition: samplefmt.h:62
#define AV_COPY128U(d, s)
Definition: intreadwrite.h:565
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
int scale_factor[MAX_CHANNELS][DCAENC_SUBBANDS]
Definition: dcaenc.c:93
#define DCA_LFE_SAMPLES
Definition: dcaenc.c:47
static void adpcm_analysis(DCAEncContext *c)
Definition: dcaenc.c:568
#define AV_CH_LAYOUT_5POINT1
#define USED_1ABITS
Definition: dcaenc.c:593
static const softfloat scalefactor_inv[128]
Definition: dcaenc.h:63
static void lfe_downsample(DCAEncContext *c, const int32_t *input)
Definition: dcaenc.c:365
static double hom(double f)
Definition: dcaenc.c:113
int32_t band_masking_cb[32]
Definition: dcaenc.c:97
static av_always_inline double ff_exp10(double x)
Compute 10^x for floating point values.
Definition: ffmath.h:42
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static void put_subframe(DCAEncContext *c, int subframe)
Definition: dcaenc.c:1102
int32_t auf[9][AUBANDS][256]
Definition: dcaenc.c:106
int samplerate_index
Definition: dcaenc.c:72
static const int snr_fudge
Definition: dcaenc.c:592
#define AVERROR(e)
Definition: error.h:43
#define DCA_ADPCM_COEFFS
Definition: dcadata.h:28
const uint8_t ff_dca_quant_index_group_size[DCA_CODE_BOOKS]
Definition: dcadata.c:53
const uint32_t ff_dca_lossless_quant[32]
Definition: dcadata.c:4231
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
int channels
Definition: dcaenc.c:70
static uint32_t set_best_abits_code(int abits[DCAENC_SUBBANDS], int bands, int32_t *res)
Definition: dcaenc.c:735
const float ff_dca_lfe_fir_64[256]
Definition: dcadata.c:7339
const char * arg
Definition: jacosubdec.c:66
static void update_band_masking(DCAEncContext *c, int band1, int band2, int f, int32_t spectrum1, int32_t spectrum2, int channel, int32_t *arg)
Definition: dcaenc.c:497
void(* mdct_calc)(struct FFTContext *s, FFTSample *output, const FFTSample *input)
Definition: fft.h:109
simple assert() macros that are a bit more flexible than ISO C assert().
const char * name
Name of the codec implementation.
Definition: avcodec.h:3415
const uint32_t ff_dca_quant_levels[32]
Definition: dcadata.c:4215
#define ff_mdct_init
Definition: fft.h:169
int8_t exp
Definition: eval.c:72
static int32_t add_cb(DCAEncContext *c, int32_t a, int32_t b)
Definition: dcaenc.c:407
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2224
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:85
int32_t e
Definition: dcaenc.h:31
#define AV_CH_LAYOUT_2_2
static const unsigned short cos_table[(1<< COS_TABLE_BITS)+2]
Definition: audiogen.c:52
static const uint16_t fc[]
Definition: dcaenc.h:43
Definition: fft.h:88
static void assign_bits(DCAEncContext *c)
Definition: dcaenc.c:842
audio channel layout utility functions
static int subband_bufer_alloc(DCAEncContext *c)
Definition: dcaenc.c:132
int32_t prediction_mode[MAX_CHANNELS][DCAENC_SUBBANDS]
Definition: dcaenc.c:82
static void calc_lfe_scales(DCAEncContext *c)
Definition: dcaenc.c:932
int fullband_channels
Definition: dcaenc.c:69
DCAADPCMEncContext adpcm_ctx
Definition: dcaenc.c:64
uint32_t ff_dca_vlc_calc_quant_bits(int *values, uint8_t n, uint8_t sel, uint8_t table)
Definition: dcahuff.c:1338
int32_t
void(* walk_band_t)(DCAEncContext *c, int band1, int band2, int f, int32_t spectrum1, int32_t spectrum2, int channel, int32_t *arg)
Definition: dcaenc.c:463
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
static void quantize_adpcm_subband(DCAEncContext *c, int ch, int band)
Definition: dcaenc.c:641
int32_t worst_noise_ever
Definition: dcaenc.c:99
#define DCA_MAX_FRAME_SIZE
Definition: dcaenc.c:45
int consumed_adpcm_bits
Number of bits to transmit ADPCM related info.
Definition: dcaenc.c:101
if(ret< 0)
Definition: vf_mcdeint.c:279
#define DCAENC_SUBBANDS
Definition: dcaenc.c:49
int32_t lfe_peak_cb
Definition: dcaenc.c:79
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
const uint32_t ff_dca_scale_factor_quant7[128]
Definition: dcadata.c:4172
static int32_t mul32(int32_t a, int32_t b)
Definition: dcamath.h:52
#define SUBBAND_SAMPLES
Definition: dcaenc.c:52
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:2193
int32_t band_spectrum_tab[2][8]
Definition: dcaenc.c:105
static int init_quantization_noise(DCAEncContext *c, int noise, int forbid_zero)
Definition: dcaenc.c:763
int bitrate_index
Definition: dcaenc.c:73
static const int8_t channel_reorder_lfe[7][5]
Definition: dca_lbr.c:95
static void put_primary_audio_header(DCAEncContext *c)
Definition: dcaenc.c:1021
int frame_size
Definition: mxfenc.c:1947
static void find_peaks(DCAEncContext *c)
Definition: dcaenc.c:554
void ff_dca_vlc_enc_quant(PutBitContext *pb, int *values, uint8_t n, uint8_t sel, uint8_t table)
Definition: dcahuff.c:1350
Libavcodec external API header.
const int32_t * band_spectrum
Definition: dcaenc.c:76
#define DCAENC_FLAGS
Definition: dcaenc.c:1218
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
typedef void(RENAME(mix_any_func_type))
int32_t history[MAX_CHANNELS][512]
Definition: dcaenc.c:84
#define DCA_CODE_BOOKS
Definition: dcahuff.h:32
int sample_rate
samples per second
Definition: avcodec.h:2173
#define ss
main external API structure.
Definition: avcodec.h:1518
static const float bands[]
int32_t cos_table[2048]
Definition: dcaenc.c:103
static void put_subframe_samples(DCAEncContext *c, int ss, int band, int ch)
Definition: dcaenc.c:1068
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
static void quantize_adpcm(DCAEncContext *c)
Definition: dcaenc.c:658
double value
Definition: eval.c:98
static void calc_power(DCAEncContext *c, const int32_t in[2 *256], int32_t power[256])
Definition: dcaenc.c:417
Describe the class of an AVClass context structure.
Definition: log.h:67
FFTContext mdct
Definition: dcaenc.c:65
static const AVOption options[]
Definition: dcaenc.c:1220
int ff_dcaadpcm_subband_analysis(const DCAADPCMEncContext *s, const int32_t *in, int len, int *diff)
Definition: dcaadpcm.c:125
const uint8_t ff_dca_quant_index_sel_nbits[DCA_CODE_BOOKS]
Definition: dcadata.c:49
int32_t worst_quantization_noise
Definition: dcaenc.c:98
static int encode_init(AVCodecContext *avctx)
Definition: dcaenc.c:159
static void fill_in_adpcm_bufer(DCAEncContext *c)
Definition: dcaenc.c:896
static void quantize_pcm(DCAEncContext *c)
Definition: dcaenc.c:668
#define DCA_BITALLOC_12_COUNT
Definition: dcahuff.h:33
static int noise(AVBSFContext *ctx, AVPacket *pkt)
Definition: noise_bsf.c:38
const uint8_t * quant
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:232
#define SUBFRAMES
Definition: dcaenc.c:50
static void subband_transform(DCAEncContext *c, const int32_t *input)
Definition: dcaenc.c:315
#define LOCAL_ALIGNED_32(t, v,...)
Definition: internal.h:137
internal math functions header
int channel_config
Definition: dcaenc.c:74
AVCodec ff_dca_encoder
Definition: dcaenc.c:1237
#define COS_T(x)
Definition: dcaenc.c:55
PutBitContext pb
Definition: dcaenc.c:63
common internal api header.
sample_rates
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
common internal and external API header
#define USED_26ABITS
Definition: dcaenc.c:594
#define ff_mdct_end
Definition: fft.h:170
static double c[64]
channel
Use these values when setting the channel map with ebur128_set_channel().
Definition: ebur128.h:39
static void subband_bufer_free(DCAEncContext *c)
Definition: dcaenc.c:153
int lfe_scale_factor
Definition: dcaenc.c:77
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
int32_t quant_index_sel[MAX_CHANNELS][DCA_CODE_BOOKS]
Definition: dcaenc.c:95
void * priv_data
Definition: avcodec.h:1545
int32_t quantized[MAX_CHANNELS][DCAENC_SUBBANDS][SUBBAND_SAMPLES]
Definition: dcaenc.c:86
int consumed_bits
Definition: dcaenc.c:100
int len
int channels
number of audio channels
Definition: avcodec.h:2174
static int32_t norm__(int64_t a, int bits)
Definition: dcamath.h:27
int32_t * subband[MAX_CHANNELS][DCAENC_SUBBANDS]
Definition: dcaenc.c:85
static const double coeff[2][5]
Definition: vf_owdenoise.c:72
static const int8_t channel_reorder_nolfe[7][5]
Definition: dca_lbr.c:85
uint64_t layout
int64_t av_get_default_channel_layout(int nb_channels)
Return default channel layout for a given number of channels.
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
FILE * out
Definition: movenc.c:54
static uint32_t set_best_code(uint32_t vlc_bits[DCA_CODE_BOOKS][7], uint32_t clc_bits[DCA_CODE_BOOKS], int32_t res[DCA_CODE_BOOKS])
Definition: dcaenc.c:694
#define av_freep(p)
const int32_t * band_interpolation
Definition: dcaenc.c:75
int32_t downsampled_lfe[DCA_LFE_SAMPLES]
Definition: dcaenc.c:89
int32_t peak_cb[MAX_CHANNELS][DCAENC_SUBBANDS]
Definition: dcaenc.c:87
int32_t bit_allocation_sel[MAX_CHANNELS]
Definition: dcaenc.c:91
#define M_PI
Definition: mathematics.h:52
av_cold void ff_dcaadpcm_free(DCAADPCMEncContext *s)
Definition: dcaadpcm.c:225
static av_always_inline int64_t ff_samples_to_time_base(AVCodecContext *avctx, int64_t samples)
Rescale from sample rate to AVCodecContext.time_base.
Definition: internal.h:280
#define FFSWAP(type, a, b)
Definition: common.h:99
static const AVCodecDefault defaults[]
Definition: dcaenc.c:1232
static const uint8_t lfe_index[7]
Definition: dca_lbr.c:105
static const int bit_consumption[27]
Definition: dcaenc.h:101
const float ff_dca_fir_32bands_perfect[512]
Definition: dcadata.c:6293
int32_t adpcm_history[MAX_CHANNELS][DCAENC_SUBBANDS][DCA_ADPCM_COEFFS *2]
Definition: dcaenc.c:83
#define AV_CH_LAYOUT_MONO
int32_t lfe_fir_64i[512]
Definition: dcaenc.c:109
enum AVCodecID id
This structure stores compressed data.
Definition: avcodec.h:1407
static const AVClass dcaenc_class
Definition: dcaenc.c:1225
void ff_dca_vlc_enc_alloc(PutBitContext *pb, int *values, uint8_t n, uint8_t sel)
Definition: dcahuff.c:1371
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:284
static double gammafilter(int i, double f)
Definition: dcaenc.c:123
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1423
int32_t band_interpolation_tab[2][512]
Definition: dcaenc.c:104
for(j=16;j >0;--j)
static int32_t get_step_size(DCAEncContext *c, int ch, int band)
Definition: dcaenc.c:596
bitstream writer API