FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 #include "libavutil/avassert.h"
26 #include "libavutil/common.h"
27 #include "libavutil/ffmath.h"
28 #include "avcodec.h"
29 #include "dca.h"
30 #include "dcadata.h"
31 #include "dcaenc.h"
32 #include "internal.h"
33 #include "mathops.h"
34 #include "put_bits.h"
35 
36 #define MAX_CHANNELS 6
37 #define DCA_MAX_FRAME_SIZE 16384
38 #define DCA_HEADER_SIZE 13
39 #define DCA_LFE_SAMPLES 8
40 
41 #define DCAENC_SUBBANDS 32
42 #define SUBFRAMES 1
43 #define SUBSUBFRAMES 2
44 #define SUBBAND_SAMPLES (SUBFRAMES * SUBSUBFRAMES * 8)
45 #define AUBANDS 25
46 
47 typedef struct DCAEncContext {
52  int channels;
62  const int8_t *channel_order_tab; ///< channel reordering table, lfe and non lfe
63 
64  int32_t history[512][MAX_CHANNELS]; /* This is a circular buffer */
79 
80 static int32_t cos_table[2048];
81 static int32_t band_interpolation[2][512];
82 static int32_t band_spectrum[2][8];
83 static int32_t auf[9][AUBANDS][256];
84 static int32_t cb_to_add[256];
85 static int32_t cb_to_level[2048];
86 static int32_t lfe_fir_64i[512];
87 
88 /* Transfer function of outer and middle ear, Hz -> dB */
89 static double hom(double f)
90 {
91  double f1 = f / 1000;
92 
93  return -3.64 * pow(f1, -0.8)
94  + 6.8 * exp(-0.6 * (f1 - 3.4) * (f1 - 3.4))
95  - 6.0 * exp(-0.15 * (f1 - 8.7) * (f1 - 8.7))
96  - 0.0006 * (f1 * f1) * (f1 * f1);
97 }
98 
99 static double gammafilter(int i, double f)
100 {
101  double h = (f - fc[i]) / erb[i];
102 
103  h = 1 + h * h;
104  h = 1 / (h * h);
105  return 20 * log10(h);
106 }
107 
108 static int encode_init(AVCodecContext *avctx)
109 {
110  DCAEncContext *c = avctx->priv_data;
111  uint64_t layout = avctx->channel_layout;
112  int i, min_frame_bits;
113 
114  c->fullband_channels = c->channels = avctx->channels;
115  c->lfe_channel = (avctx->channels == 3 || avctx->channels == 6);
118  c->worst_quantization_noise = -2047;
119  c->worst_noise_ever = -2047;
120 
121  if (!layout) {
122  av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The "
123  "encoder will guess the layout, but it "
124  "might be incorrect.\n");
125  layout = av_get_default_channel_layout(avctx->channels);
126  }
127  switch (layout) {
128  case AV_CH_LAYOUT_MONO: c->channel_config = 0; break;
129  case AV_CH_LAYOUT_STEREO: c->channel_config = 2; break;
130  case AV_CH_LAYOUT_2_2: c->channel_config = 8; break;
131  case AV_CH_LAYOUT_5POINT0: c->channel_config = 9; break;
132  case AV_CH_LAYOUT_5POINT1: c->channel_config = 9; break;
133  default:
134  av_log(avctx, AV_LOG_ERROR, "Unsupported channel layout!\n");
135  return AVERROR_PATCHWELCOME;
136  }
137 
138  if (c->lfe_channel) {
139  c->fullband_channels--;
141  } else {
143  }
144 
145  for (i = 0; i < 9; i++) {
146  if (sample_rates[i] == avctx->sample_rate)
147  break;
148  }
149  if (i == 9)
150  return AVERROR(EINVAL);
151  c->samplerate_index = i;
152 
153  if (avctx->bit_rate < 32000 || avctx->bit_rate > 3840000) {
154  av_log(avctx, AV_LOG_ERROR, "Bit rate %"PRId64" not supported.", (int64_t)avctx->bit_rate);
155  return AVERROR(EINVAL);
156  }
157  for (i = 0; ff_dca_bit_rates[i] < avctx->bit_rate; i++)
158  ;
159  c->bitrate_index = i;
160  c->frame_bits = FFALIGN((avctx->bit_rate * 512 + avctx->sample_rate - 1) / avctx->sample_rate, 32);
161  min_frame_bits = 132 + (493 + 28 * 32) * c->fullband_channels + c->lfe_channel * 72;
162  if (c->frame_bits < min_frame_bits || c->frame_bits > (DCA_MAX_FRAME_SIZE << 3))
163  return AVERROR(EINVAL);
164 
165  c->frame_size = (c->frame_bits + 7) / 8;
166 
167  avctx->frame_size = 32 * SUBBAND_SAMPLES;
168 
169  if (!cos_table[0]) {
170  int j, k;
171 
172  cos_table[0] = 0x7fffffff;
173  cos_table[512] = 0;
174  cos_table[1024] = -cos_table[0];
175  for (i = 1; i < 512; i++) {
176  cos_table[i] = (int32_t)(0x7fffffff * cos(M_PI * i / 1024));
177  cos_table[1024-i] = -cos_table[i];
178  cos_table[1024+i] = -cos_table[i];
179  cos_table[2048-i] = cos_table[i];
180  }
181  for (i = 0; i < 2048; i++) {
182  cb_to_level[i] = (int32_t)(0x7fffffff * ff_exp10(-0.005 * i));
183  }
184 
185  for (k = 0; k < 32; k++) {
186  for (j = 0; j < 8; j++) {
187  lfe_fir_64i[64 * j + k] = (int32_t)(0xffffff800000ULL * ff_dca_lfe_fir_64[8 * k + j]);
188  lfe_fir_64i[64 * (7-j) + (63 - k)] = (int32_t)(0xffffff800000ULL * ff_dca_lfe_fir_64[8 * k + j]);
189  }
190  }
191 
192  for (i = 0; i < 512; i++) {
193  band_interpolation[0][i] = (int32_t)(0x1000000000ULL * ff_dca_fir_32bands_perfect[i]);
194  band_interpolation[1][i] = (int32_t)(0x1000000000ULL * ff_dca_fir_32bands_nonperfect[i]);
195  }
196 
197  for (i = 0; i < 9; i++) {
198  for (j = 0; j < AUBANDS; j++) {
199  for (k = 0; k < 256; k++) {
200  double freq = sample_rates[i] * (k + 0.5) / 512;
201 
202  auf[i][j][k] = (int32_t)(10 * (hom(freq) + gammafilter(j, freq)));
203  }
204  }
205  }
206 
207  for (i = 0; i < 256; i++) {
208  double add = 1 + ff_exp10(-0.01 * i);
209  cb_to_add[i] = (int32_t)(100 * log10(add));
210  }
211  for (j = 0; j < 8; j++) {
212  double accum = 0;
213  for (i = 0; i < 512; i++) {
214  double reconst = ff_dca_fir_32bands_perfect[i] * ((i & 64) ? (-1) : 1);
215  accum += reconst * cos(2 * M_PI * (i + 0.5 - 256) * (j + 0.5) / 512);
216  }
217  band_spectrum[0][j] = (int32_t)(200 * log10(accum));
218  }
219  for (j = 0; j < 8; j++) {
220  double accum = 0;
221  for (i = 0; i < 512; i++) {
222  double reconst = ff_dca_fir_32bands_nonperfect[i] * ((i & 64) ? (-1) : 1);
223  accum += reconst * cos(2 * M_PI * (i + 0.5 - 256) * (j + 0.5) / 512);
224  }
225  band_spectrum[1][j] = (int32_t)(200 * log10(accum));
226  }
227  }
228  return 0;
229 }
230 
231 static inline int32_t cos_t(int x)
232 {
233  return cos_table[x & 2047];
234 }
235 
236 static inline int32_t sin_t(int x)
237 {
238  return cos_t(x - 512);
239 }
240 
241 static inline int32_t half32(int32_t a)
242 {
243  return (a + 1) >> 1;
244 }
245 
246 static inline int32_t mul32(int32_t a, int32_t b)
247 {
248  int64_t r = (int64_t)a * b + 0x80000000ULL;
249  return r >> 32;
250 }
251 
252 static void subband_transform(DCAEncContext *c, const int32_t *input)
253 {
254  int ch, subs, i, k, j;
255 
256  for (ch = 0; ch < c->fullband_channels; ch++) {
257  /* History is copied because it is also needed for PSY */
258  int32_t hist[512];
259  int hist_start = 0;
260  const int chi = c->channel_order_tab[ch];
261 
262  for (i = 0; i < 512; i++)
263  hist[i] = c->history[i][ch];
264 
265  for (subs = 0; subs < SUBBAND_SAMPLES; subs++) {
266  int32_t accum[64];
267  int32_t resp;
268  int band;
269 
270  /* Calculate the convolutions at once */
271  for (i = 0; i < 64; i++)
272  accum[i] = 0;
273 
274  for (k = 0, i = hist_start, j = 0;
275  i < 512; k = (k + 1) & 63, i++, j++)
276  accum[k] += mul32(hist[i], c->band_interpolation[j]);
277  for (i = 0; i < hist_start; k = (k + 1) & 63, i++, j++)
278  accum[k] += mul32(hist[i], c->band_interpolation[j]);
279 
280  for (k = 16; k < 32; k++)
281  accum[k] = accum[k] - accum[31 - k];
282  for (k = 32; k < 48; k++)
283  accum[k] = accum[k] + accum[95 - k];
284 
285  for (band = 0; band < 32; band++) {
286  resp = 0;
287  for (i = 16; i < 48; i++) {
288  int s = (2 * band + 1) * (2 * (i + 16) + 1);
289  resp += mul32(accum[i], cos_t(s << 3)) >> 3;
290  }
291 
292  c->subband[subs][band][ch] = ((band + 1) & 2) ? -resp : resp;
293  }
294 
295  /* Copy in 32 new samples from input */
296  for (i = 0; i < 32; i++)
297  hist[i + hist_start] = input[(subs * 32 + i) * c->channels + chi];
298  hist_start = (hist_start + 32) & 511;
299  }
300  }
301 }
302 
303 static void lfe_downsample(DCAEncContext *c, const int32_t *input)
304 {
305  /* FIXME: make 128x LFE downsampling possible */
306  const int lfech = lfe_index[c->channel_config];
307  int i, j, lfes;
308  int32_t hist[512];
309  int32_t accum;
310  int hist_start = 0;
311 
312  for (i = 0; i < 512; i++)
313  hist[i] = c->history[i][c->channels - 1];
314 
315  for (lfes = 0; lfes < DCA_LFE_SAMPLES; lfes++) {
316  /* Calculate the convolution */
317  accum = 0;
318 
319  for (i = hist_start, j = 0; i < 512; i++, j++)
320  accum += mul32(hist[i], lfe_fir_64i[j]);
321  for (i = 0; i < hist_start; i++, j++)
322  accum += mul32(hist[i], lfe_fir_64i[j]);
323 
324  c->downsampled_lfe[lfes] = accum;
325 
326  /* Copy in 64 new samples from input */
327  for (i = 0; i < 64; i++)
328  hist[i + hist_start] = input[(lfes * 64 + i) * c->channels + lfech];
329 
330  hist_start = (hist_start + 64) & 511;
331  }
332 }
333 
334 typedef struct {
337 } cplx32;
338 
339 static void fft(const int32_t in[2 * 256], cplx32 out[256])
340 {
341  cplx32 buf[256], rin[256], rout[256];
342  int i, j, k, l;
343 
344  /* do two transforms in parallel */
345  for (i = 0; i < 256; i++) {
346  /* Apply the Hann window */
347  rin[i].re = mul32(in[2 * i], 0x3fffffff - (cos_t(8 * i + 2) >> 1));
348  rin[i].im = mul32(in[2 * i + 1], 0x3fffffff - (cos_t(8 * i + 6) >> 1));
349  }
350  /* pre-rotation */
351  for (i = 0; i < 256; i++) {
352  buf[i].re = mul32(cos_t(4 * i + 2), rin[i].re)
353  - mul32(sin_t(4 * i + 2), rin[i].im);
354  buf[i].im = mul32(cos_t(4 * i + 2), rin[i].im)
355  + mul32(sin_t(4 * i + 2), rin[i].re);
356  }
357 
358  for (j = 256, l = 1; j != 1; j >>= 1, l <<= 1) {
359  for (k = 0; k < 256; k += j) {
360  for (i = k; i < k + j / 2; i++) {
361  cplx32 sum, diff;
362  int t = 8 * l * i;
363 
364  sum.re = buf[i].re + buf[i + j / 2].re;
365  sum.im = buf[i].im + buf[i + j / 2].im;
366 
367  diff.re = buf[i].re - buf[i + j / 2].re;
368  diff.im = buf[i].im - buf[i + j / 2].im;
369 
370  buf[i].re = half32(sum.re);
371  buf[i].im = half32(sum.im);
372 
373  buf[i + j / 2].re = mul32(diff.re, cos_t(t))
374  - mul32(diff.im, sin_t(t));
375  buf[i + j / 2].im = mul32(diff.im, cos_t(t))
376  + mul32(diff.re, sin_t(t));
377  }
378  }
379  }
380  /* post-rotation */
381  for (i = 0; i < 256; i++) {
382  int b = ff_reverse[i];
383  rout[i].re = mul32(buf[b].re, cos_t(4 * i))
384  - mul32(buf[b].im, sin_t(4 * i));
385  rout[i].im = mul32(buf[b].im, cos_t(4 * i))
386  + mul32(buf[b].re, sin_t(4 * i));
387  }
388  for (i = 0; i < 256; i++) {
389  /* separate the results of the two transforms */
390  cplx32 o1, o2;
391 
392  o1.re = rout[i].re - rout[255 - i].re;
393  o1.im = rout[i].im + rout[255 - i].im;
394 
395  o2.re = rout[i].im - rout[255 - i].im;
396  o2.im = -rout[i].re - rout[255 - i].re;
397 
398  /* combine them into one long transform */
399  out[i].re = mul32( o1.re + o2.re, cos_t(2 * i + 1))
400  + mul32( o1.im - o2.im, sin_t(2 * i + 1));
401  out[i].im = mul32( o1.im + o2.im, cos_t(2 * i + 1))
402  + mul32(-o1.re + o2.re, sin_t(2 * i + 1));
403  }
404 }
405 
407 {
408  int i, res;
409 
410  res = 0;
411  if (in < 0)
412  in = -in;
413  for (i = 1024; i > 0; i >>= 1) {
414  if (cb_to_level[i + res] >= in)
415  res += i;
416  }
417  return -res;
418 }
419 
421 {
422  if (a < b)
423  FFSWAP(int32_t, a, b);
424 
425  if (a - b >= 256)
426  return a;
427  return a + cb_to_add[a - b];
428 }
429 
430 static void adjust_jnd(int samplerate_index,
431  const int32_t in[512], int32_t out_cb[256])
432 {
433  int32_t power[256];
434  cplx32 out[256];
435  int32_t out_cb_unnorm[256];
436  int32_t denom;
437  const int32_t ca_cb = -1114;
438  const int32_t cs_cb = 928;
439  int i, j;
440 
441  fft(in, out);
442 
443  for (j = 0; j < 256; j++) {
444  power[j] = add_cb(get_cb(out[j].re), get_cb(out[j].im));
445  out_cb_unnorm[j] = -2047; /* and can only grow */
446  }
447 
448  for (i = 0; i < AUBANDS; i++) {
449  denom = ca_cb; /* and can only grow */
450  for (j = 0; j < 256; j++)
451  denom = add_cb(denom, power[j] + auf[samplerate_index][i][j]);
452  for (j = 0; j < 256; j++)
453  out_cb_unnorm[j] = add_cb(out_cb_unnorm[j],
454  -denom + auf[samplerate_index][i][j]);
455  }
456 
457  for (j = 0; j < 256; j++)
458  out_cb[j] = add_cb(out_cb[j], -out_cb_unnorm[j] - ca_cb - cs_cb);
459 }
460 
461 typedef void (*walk_band_t)(DCAEncContext *c, int band1, int band2, int f,
462  int32_t spectrum1, int32_t spectrum2, int channel,
463  int32_t * arg);
464 
465 static void walk_band_low(DCAEncContext *c, int band, int channel,
466  walk_band_t walk, int32_t *arg)
467 {
468  int f;
469 
470  if (band == 0) {
471  for (f = 0; f < 4; f++)
472  walk(c, 0, 0, f, 0, -2047, channel, arg);
473  } else {
474  for (f = 0; f < 8; f++)
475  walk(c, band, band - 1, 8 * band - 4 + f,
476  c->band_spectrum[7 - f], c->band_spectrum[f], channel, arg);
477  }
478 }
479 
480 static void walk_band_high(DCAEncContext *c, int band, int channel,
481  walk_band_t walk, int32_t *arg)
482 {
483  int f;
484 
485  if (band == 31) {
486  for (f = 0; f < 4; f++)
487  walk(c, 31, 31, 256 - 4 + f, 0, -2047, channel, arg);
488  } else {
489  for (f = 0; f < 8; f++)
490  walk(c, band, band + 1, 8 * band + 4 + f,
491  c->band_spectrum[f], c->band_spectrum[7 - f], channel, arg);
492  }
493 }
494 
495 static void update_band_masking(DCAEncContext *c, int band1, int band2,
496  int f, int32_t spectrum1, int32_t spectrum2,
497  int channel, int32_t * arg)
498 {
499  int32_t value = c->eff_masking_curve_cb[f] - spectrum1;
500 
501  if (value < c->band_masking_cb[band1])
502  c->band_masking_cb[band1] = value;
503 }
504 
505 static void calc_masking(DCAEncContext *c, const int32_t *input)
506 {
507  int i, k, band, ch, ssf;
508  int32_t data[512];
509 
510  for (i = 0; i < 256; i++)
511  for (ssf = 0; ssf < SUBSUBFRAMES; ssf++)
512  c->masking_curve_cb[ssf][i] = -2047;
513 
514  for (ssf = 0; ssf < SUBSUBFRAMES; ssf++)
515  for (ch = 0; ch < c->fullband_channels; ch++) {
516  const int chi = c->channel_order_tab[ch];
517 
518  for (i = 0, k = 128 + 256 * ssf; k < 512; i++, k++)
519  data[i] = c->history[k][ch];
520  for (k -= 512; i < 512; i++, k++)
521  data[i] = input[k * c->channels + chi];
522  adjust_jnd(c->samplerate_index, data, c->masking_curve_cb[ssf]);
523  }
524  for (i = 0; i < 256; i++) {
525  int32_t m = 2048;
526 
527  for (ssf = 0; ssf < SUBSUBFRAMES; ssf++)
528  if (c->masking_curve_cb[ssf][i] < m)
529  m = c->masking_curve_cb[ssf][i];
530  c->eff_masking_curve_cb[i] = m;
531  }
532 
533  for (band = 0; band < 32; band++) {
534  c->band_masking_cb[band] = 2048;
535  walk_band_low(c, band, 0, update_band_masking, NULL);
537  }
538 }
539 
541 {
542  int band, ch;
543 
544  for (band = 0; band < 32; band++)
545  for (ch = 0; ch < c->fullband_channels; ch++) {
546  int sample;
547  int32_t m = 0;
548 
549  for (sample = 0; sample < SUBBAND_SAMPLES; sample++) {
550  int32_t s = abs(c->subband[sample][band][ch]);
551  if (m < s)
552  m = s;
553  }
554  c->peak_cb[band][ch] = get_cb(m);
555  }
556 
557  if (c->lfe_channel) {
558  int sample;
559  int32_t m = 0;
560 
561  for (sample = 0; sample < DCA_LFE_SAMPLES; sample++)
562  if (m < abs(c->downsampled_lfe[sample]))
563  m = abs(c->downsampled_lfe[sample]);
564  c->lfe_peak_cb = get_cb(m);
565  }
566 }
567 
568 static const int snr_fudge = 128;
569 #define USED_1ABITS 1
570 #define USED_NABITS 2
571 #define USED_26ABITS 4
572 
574 {
575  int ch, band, ret = 0;
576 
577  c->consumed_bits = 132 + 493 * c->fullband_channels;
578  if (c->lfe_channel)
579  c->consumed_bits += 72;
580 
581  /* attempt to guess the bit distribution based on the prevoius frame */
582  for (ch = 0; ch < c->fullband_channels; ch++) {
583  for (band = 0; band < 32; band++) {
584  int snr_cb = c->peak_cb[band][ch] - c->band_masking_cb[band] - noise;
585 
586  if (snr_cb >= 1312) {
587  c->abits[band][ch] = 26;
588  ret |= USED_26ABITS;
589  } else if (snr_cb >= 222) {
590  c->abits[band][ch] = 8 + mul32(snr_cb - 222, 69000000);
591  ret |= USED_NABITS;
592  } else if (snr_cb >= 0) {
593  c->abits[band][ch] = 2 + mul32(snr_cb, 106000000);
594  ret |= USED_NABITS;
595  } else {
596  c->abits[band][ch] = 1;
597  ret |= USED_1ABITS;
598  }
599  }
600  }
601 
602  for (band = 0; band < 32; band++)
603  for (ch = 0; ch < c->fullband_channels; ch++) {
604  c->consumed_bits += bit_consumption[c->abits[band][ch]];
605  }
606 
607  return ret;
608 }
609 
611 {
612  /* Find the bounds where the binary search should work */
613  int low, high, down;
614  int used_abits = 0;
615 
617  low = high = c->worst_quantization_noise;
618  if (c->consumed_bits > c->frame_bits) {
619  while (c->consumed_bits > c->frame_bits) {
620  av_assert0(used_abits != USED_1ABITS);
621  low = high;
622  high += snr_fudge;
623  used_abits = init_quantization_noise(c, high);
624  }
625  } else {
626  while (c->consumed_bits <= c->frame_bits) {
627  high = low;
628  if (used_abits == USED_26ABITS)
629  goto out; /* The requested bitrate is too high, pad with zeros */
630  low -= snr_fudge;
631  used_abits = init_quantization_noise(c, low);
632  }
633  }
634 
635  /* Now do a binary search between low and high to see what fits */
636  for (down = snr_fudge >> 1; down; down >>= 1) {
637  init_quantization_noise(c, high - down);
638  if (c->consumed_bits <= c->frame_bits)
639  high -= down;
640  }
641  init_quantization_noise(c, high);
642 out:
643  c->worst_quantization_noise = high;
644  if (high > c->worst_noise_ever)
645  c->worst_noise_ever = high;
646 }
647 
648 static void shift_history(DCAEncContext *c, const int32_t *input)
649 {
650  int k, ch;
651 
652  for (k = 0; k < 512; k++)
653  for (ch = 0; ch < c->channels; ch++) {
654  const int chi = c->channel_order_tab[ch];
655 
656  c->history[k][ch] = input[k * c->channels + chi];
657  }
658 }
659 
661 {
662  int32_t offset = 1 << (quant.e - 1);
663 
664  value = mul32(value, quant.m) + offset;
665  value = value >> quant.e;
666  return value;
667 }
668 
669 static int calc_one_scale(int32_t peak_cb, int abits, softfloat *quant)
670 {
671  int32_t peak;
672  int our_nscale, try_remove;
673  softfloat our_quant;
674 
675  av_assert0(peak_cb <= 0);
676  av_assert0(peak_cb >= -2047);
677 
678  our_nscale = 127;
679  peak = cb_to_level[-peak_cb];
680 
681  for (try_remove = 64; try_remove > 0; try_remove >>= 1) {
682  if (scalefactor_inv[our_nscale - try_remove].e + stepsize_inv[abits].e <= 17)
683  continue;
684  our_quant.m = mul32(scalefactor_inv[our_nscale - try_remove].m, stepsize_inv[abits].m);
685  our_quant.e = scalefactor_inv[our_nscale - try_remove].e + stepsize_inv[abits].e - 17;
686  if ((ff_dca_quant_levels[abits] - 1) / 2 < quantize_value(peak, our_quant))
687  continue;
688  our_nscale -= try_remove;
689  }
690 
691  if (our_nscale >= 125)
692  our_nscale = 124;
693 
694  quant->m = mul32(scalefactor_inv[our_nscale].m, stepsize_inv[abits].m);
695  quant->e = scalefactor_inv[our_nscale].e + stepsize_inv[abits].e - 17;
696  av_assert0((ff_dca_quant_levels[abits] - 1) / 2 >= quantize_value(peak, *quant));
697 
698  return our_nscale;
699 }
700 
702 {
703  int band, ch;
704 
705  for (band = 0; band < 32; band++)
706  for (ch = 0; ch < c->fullband_channels; ch++)
707  c->scale_factor[band][ch] = calc_one_scale(c->peak_cb[band][ch],
708  c->abits[band][ch],
709  &c->quant[band][ch]);
710 
711  if (c->lfe_channel)
713 }
714 
716 {
717  int sample, band, ch;
718 
719  for (sample = 0; sample < SUBBAND_SAMPLES; sample++)
720  for (band = 0; band < 32; band++)
721  for (ch = 0; ch < c->fullband_channels; ch++)
722  c->quantized[sample][band][ch] = quantize_value(c->subband[sample][band][ch], c->quant[band][ch]);
723 }
724 
726 {
727  /* SYNC */
728  put_bits(&c->pb, 16, 0x7ffe);
729  put_bits(&c->pb, 16, 0x8001);
730 
731  /* Frame type: normal */
732  put_bits(&c->pb, 1, 1);
733 
734  /* Deficit sample count: none */
735  put_bits(&c->pb, 5, 31);
736 
737  /* CRC is not present */
738  put_bits(&c->pb, 1, 0);
739 
740  /* Number of PCM sample blocks */
741  put_bits(&c->pb, 7, SUBBAND_SAMPLES - 1);
742 
743  /* Primary frame byte size */
744  put_bits(&c->pb, 14, c->frame_size - 1);
745 
746  /* Audio channel arrangement */
747  put_bits(&c->pb, 6, c->channel_config);
748 
749  /* Core audio sampling frequency */
751 
752  /* Transmission bit rate */
753  put_bits(&c->pb, 5, c->bitrate_index);
754 
755  /* Embedded down mix: disabled */
756  put_bits(&c->pb, 1, 0);
757 
758  /* Embedded dynamic range flag: not present */
759  put_bits(&c->pb, 1, 0);
760 
761  /* Embedded time stamp flag: not present */
762  put_bits(&c->pb, 1, 0);
763 
764  /* Auxiliary data flag: not present */
765  put_bits(&c->pb, 1, 0);
766 
767  /* HDCD source: no */
768  put_bits(&c->pb, 1, 0);
769 
770  /* Extension audio ID: N/A */
771  put_bits(&c->pb, 3, 0);
772 
773  /* Extended audio data: not present */
774  put_bits(&c->pb, 1, 0);
775 
776  /* Audio sync word insertion flag: after each sub-frame */
777  put_bits(&c->pb, 1, 0);
778 
779  /* Low frequency effects flag: not present or 64x subsampling */
780  put_bits(&c->pb, 2, c->lfe_channel ? 2 : 0);
781 
782  /* Predictor history switch flag: on */
783  put_bits(&c->pb, 1, 1);
784 
785  /* No CRC */
786  /* Multirate interpolator switch: non-perfect reconstruction */
787  put_bits(&c->pb, 1, 0);
788 
789  /* Encoder software revision: 7 */
790  put_bits(&c->pb, 4, 7);
791 
792  /* Copy history: 0 */
793  put_bits(&c->pb, 2, 0);
794 
795  /* Source PCM resolution: 16 bits, not DTS ES */
796  put_bits(&c->pb, 3, 0);
797 
798  /* Front sum/difference coding: no */
799  put_bits(&c->pb, 1, 0);
800 
801  /* Surrounds sum/difference coding: no */
802  put_bits(&c->pb, 1, 0);
803 
804  /* Dialog normalization: 0 dB */
805  put_bits(&c->pb, 4, 0);
806 }
807 
809 {
810  static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
811  static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
812 
813  int ch, i;
814  /* Number of subframes */
815  put_bits(&c->pb, 4, SUBFRAMES - 1);
816 
817  /* Number of primary audio channels */
818  put_bits(&c->pb, 3, c->fullband_channels - 1);
819 
820  /* Subband activity count */
821  for (ch = 0; ch < c->fullband_channels; ch++)
822  put_bits(&c->pb, 5, DCAENC_SUBBANDS - 2);
823 
824  /* High frequency VQ start subband */
825  for (ch = 0; ch < c->fullband_channels; ch++)
826  put_bits(&c->pb, 5, DCAENC_SUBBANDS - 1);
827 
828  /* Joint intensity coding index: 0, 0 */
829  for (ch = 0; ch < c->fullband_channels; ch++)
830  put_bits(&c->pb, 3, 0);
831 
832  /* Transient mode codebook: A4, A4 (arbitrary) */
833  for (ch = 0; ch < c->fullband_channels; ch++)
834  put_bits(&c->pb, 2, 0);
835 
836  /* Scale factor code book: 7 bit linear, 7-bit sqrt table (for each channel) */
837  for (ch = 0; ch < c->fullband_channels; ch++)
838  put_bits(&c->pb, 3, 6);
839 
840  /* Bit allocation quantizer select: linear 5-bit */
841  for (ch = 0; ch < c->fullband_channels; ch++)
842  put_bits(&c->pb, 3, 6);
843 
844  /* Quantization index codebook select: dummy data
845  to avoid transmission of scale factor adjustment */
846  for (i = 1; i < 11; i++)
847  for (ch = 0; ch < c->fullband_channels; ch++)
848  put_bits(&c->pb, bitlen[i], thr[i]);
849 
850  /* Scale factor adjustment index: not transmitted */
851  /* Audio header CRC check word: not transmitted */
852 }
853 
854 static void put_subframe_samples(DCAEncContext *c, int ss, int band, int ch)
855 {
856  if (c->abits[band][ch] <= 7) {
857  int sum, i, j;
858  for (i = 0; i < 8; i += 4) {
859  sum = 0;
860  for (j = 3; j >= 0; j--) {
861  sum *= ff_dca_quant_levels[c->abits[band][ch]];
862  sum += c->quantized[ss * 8 + i + j][band][ch];
863  sum += (ff_dca_quant_levels[c->abits[band][ch]] - 1) / 2;
864  }
865  put_bits(&c->pb, bit_consumption[c->abits[band][ch]] / 4, sum);
866  }
867  } else {
868  int i;
869  for (i = 0; i < 8; i++) {
870  int bits = bit_consumption[c->abits[band][ch]] / 16;
871  put_sbits(&c->pb, bits, c->quantized[ss * 8 + i][band][ch]);
872  }
873  }
874 }
875 
876 static void put_subframe(DCAEncContext *c, int subframe)
877 {
878  int i, band, ss, ch;
879 
880  /* Subsubframes count */
881  put_bits(&c->pb, 2, SUBSUBFRAMES -1);
882 
883  /* Partial subsubframe sample count: dummy */
884  put_bits(&c->pb, 3, 0);
885 
886  /* Prediction mode: no ADPCM, in each channel and subband */
887  for (ch = 0; ch < c->fullband_channels; ch++)
888  for (band = 0; band < DCAENC_SUBBANDS; band++)
889  put_bits(&c->pb, 1, 0);
890 
891  /* Prediction VQ address: not transmitted */
892  /* Bit allocation index */
893  for (ch = 0; ch < c->fullband_channels; ch++)
894  for (band = 0; band < DCAENC_SUBBANDS; band++)
895  put_bits(&c->pb, 5, c->abits[band][ch]);
896 
897  if (SUBSUBFRAMES > 1) {
898  /* Transition mode: none for each channel and subband */
899  for (ch = 0; ch < c->fullband_channels; ch++)
900  for (band = 0; band < DCAENC_SUBBANDS; band++)
901  put_bits(&c->pb, 1, 0); /* codebook A4 */
902  }
903 
904  /* Scale factors */
905  for (ch = 0; ch < c->fullband_channels; ch++)
906  for (band = 0; band < DCAENC_SUBBANDS; band++)
907  put_bits(&c->pb, 7, c->scale_factor[band][ch]);
908 
909  /* Joint subband scale factor codebook select: not transmitted */
910  /* Scale factors for joint subband coding: not transmitted */
911  /* Stereo down-mix coefficients: not transmitted */
912  /* Dynamic range coefficient: not transmitted */
913  /* Stde information CRC check word: not transmitted */
914  /* VQ encoded high frequency subbands: not transmitted */
915 
916  /* LFE data: 8 samples and scalefactor */
917  if (c->lfe_channel) {
918  for (i = 0; i < DCA_LFE_SAMPLES; i++)
919  put_bits(&c->pb, 8, quantize_value(c->downsampled_lfe[i], c->lfe_quant) & 0xff);
920  put_bits(&c->pb, 8, c->lfe_scale_factor);
921  }
922 
923  /* Audio data (subsubframes) */
924  for (ss = 0; ss < SUBSUBFRAMES ; ss++)
925  for (ch = 0; ch < c->fullband_channels; ch++)
926  for (band = 0; band < DCAENC_SUBBANDS; band++)
927  put_subframe_samples(c, ss, band, ch);
928 
929  /* DSYNC */
930  put_bits(&c->pb, 16, 0xffff);
931 }
932 
933 static int encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
934  const AVFrame *frame, int *got_packet_ptr)
935 {
936  DCAEncContext *c = avctx->priv_data;
937  const int32_t *samples;
938  int ret, i;
939 
940  if ((ret = ff_alloc_packet2(avctx, avpkt, c->frame_size, 0)) < 0)
941  return ret;
942 
943  samples = (const int32_t *)frame->data[0];
944 
945  subband_transform(c, samples);
946  if (c->lfe_channel)
947  lfe_downsample(c, samples);
948 
949  calc_masking(c, samples);
950  find_peaks(c);
951  assign_bits(c);
952  calc_scales(c);
953  quantize_all(c);
954  shift_history(c, samples);
955 
956  init_put_bits(&c->pb, avpkt->data, avpkt->size);
957  put_frame_header(c);
959  for (i = 0; i < SUBFRAMES; i++)
960  put_subframe(c, i);
961 
962 
963  for (i = put_bits_count(&c->pb); i < 8*c->frame_size; i++)
964  put_bits(&c->pb, 1, 0);
965 
966  flush_put_bits(&c->pb);
967 
968  avpkt->pts = frame->pts;
969  avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
970  avpkt->size = put_bits_count(&c->pb) >> 3;
971  *got_packet_ptr = 1;
972  return 0;
973 }
974 
975 static const AVCodecDefault defaults[] = {
976  { "b", "1411200" },
977  { NULL },
978 };
979 
981  .name = "dca",
982  .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
983  .type = AVMEDIA_TYPE_AUDIO,
984  .id = AV_CODEC_ID_DTS,
985  .priv_data_size = sizeof(DCAEncContext),
986  .init = encode_init,
987  .encode2 = encode_frame,
988  .capabilities = AV_CODEC_CAP_EXPERIMENTAL,
989  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S32,
991  .supported_samplerates = sample_rates,
992  .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
997  0 },
998  .defaults = defaults,
999 };
#define MAX_CHANNELS
Definition: dcaenc.c:36
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
int32_t m
Definition: dcaenc.h:28
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
static int32_t cb_to_add[256]
Definition: dcaenc.c:84
static int noise(AVBSFContext *ctx, AVPacket *out)
Definition: noise_bsf.c:37
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int32_t eff_masking_curve_cb[256]
Definition: dcaenc.c:73
float re
Definition: fft.c:73
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:200
static int32_t auf[9][AUBANDS][256]
Definition: dcaenc.c:83
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:206
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
static void put_frame_header(DCAEncContext *c)
Definition: dcaenc.c:725
int64_t bit_rate
the average bitrate
Definition: avcodec.h:1714
#define AUBANDS
Definition: dcaenc.c:45
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:62
static int32_t band_spectrum[2][8]
Definition: dcaenc.c:82
int size
Definition: avcodec.h:1581
const char * b
Definition: vf_curves.c:109
static const uint8_t bitstream_sfreq[]
Definition: dcaenc.h:36
static const uint16_t erb[]
Definition: dcaenc.h:46
#define AV_CODEC_CAP_EXPERIMENTAL
Codec is experimental and is thus avoided in favor of non experimental encoders.
Definition: avcodec.h:1011
static void shift_history(DCAEncContext *c, const int32_t *input)
Definition: dcaenc.c:648
softfloat lfe_quant
Definition: dcaenc.c:60
#define AV_CH_LAYOUT_STEREO
static int encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: dcaenc.c:933
#define sample
static void walk_band_high(DCAEncContext *c, int band, int channel, walk_band_t walk, int32_t *arg)
Definition: dcaenc.c:480
AVCodec.
Definition: avcodec.h:3542
#define AV_CH_LAYOUT_5POINT0
int32_t subband[SUBBAND_SAMPLES][DCAENC_SUBBANDS][MAX_CHANNELS]
Definition: dcaenc.c:65
#define USED_NABITS
Definition: dcaenc.c:570
int frame_size
Definition: dcaenc.c:49
const float ff_dca_fir_32bands_nonperfect[512]
Definition: dcadata.c:6804
static void walk_band_low(DCAEncContext *c, int band, int channel, walk_band_t walk, int32_t *arg)
Definition: dcaenc.c:465
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#define SUBSUBFRAMES
Definition: dcaenc.c:43
static void calc_masking(DCAEncContext *c, const int32_t *input)
Definition: dcaenc.c:505
static const softfloat stepsize_inv[27]
Definition: dcaenc.h:51
const uint32_t ff_dca_bit_rates[32]
Definition: dcadata.c:32
uint8_t bits
Definition: crc.c:296
static int32_t lfe_fir_64i[512]
Definition: dcaenc.c:86
int32_t im
Definition: dcaenc.c:336
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1598
static int32_t cos_table[2048]
Definition: dcaenc.c:80
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:268
static int calc_one_scale(int32_t peak_cb, int abits, softfloat *quant)
Definition: dcaenc.c:669
int scale_factor[DCAENC_SUBBANDS][MAX_CHANNELS]
Definition: dcaenc.c:71
int32_t history[512][MAX_CHANNELS]
Definition: dcaenc.c:64
int32_t masking_curve_cb[SUBSUBFRAMES][256]
Definition: dcaenc.c:69
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1580
static void quantize_all(DCAEncContext *c)
Definition: dcaenc.c:715
static int32_t sin_t(int x)
Definition: dcaenc.c:236
int frame_bits
Definition: dcaenc.c:50
int lfe_channel
Definition: dcaenc.c:53
int32_t re
Definition: dcaenc.c:335
signed 32 bits
Definition: samplefmt.h:62
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
unsigned m
Definition: audioconvert.c:187
#define DCA_LFE_SAMPLES
Definition: dcaenc.c:39
#define AV_CH_LAYOUT_5POINT1
static int32_t add_cb(int32_t a, int32_t b)
Definition: dcaenc.c:420
#define USED_1ABITS
Definition: dcaenc.c:569
static const softfloat scalefactor_inv[128]
Definition: dcaenc.h:61
static void lfe_downsample(DCAEncContext *c, const int32_t *input)
Definition: dcaenc.c:303
softfloat quant[DCAENC_SUBBANDS][MAX_CHANNELS]
Definition: dcaenc.c:72
static double hom(double f)
Definition: dcaenc.c:89
int32_t band_masking_cb[32]
Definition: dcaenc.c:74
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:876
Definition: dcaenc.c:334
int samplerate_index
Definition: dcaenc.c:54
static const int snr_fudge
Definition: dcaenc.c:568
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
const uint8_t ff_reverse[256]
Definition: reverse.c:23
int channels
Definition: dcaenc.c:52
int abits[DCAENC_SUBBANDS][MAX_CHANNELS]
Definition: dcaenc.c:70
const char * r
Definition: vf_curves.c:107
const float ff_dca_lfe_fir_64[256]
Definition: dcadata.c:7335
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:495
simple assert() macros that are a bit more flexible than ISO C assert().
const char * name
Name of the codec implementation.
Definition: avcodec.h:3549
const uint32_t ff_dca_quant_levels[32]
Definition: dcadata.c:4211
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
int8_t exp
Definition: eval.c:64
static int32_t quantize_value(int32_t value, softfloat quant)
Definition: dcaenc.c:660
static const int sample_rates[]
Definition: dcaenc.h:32
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2461
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:85
int32_t e
Definition: dcaenc.h:29
#define AV_CH_LAYOUT_2_2
static const uint16_t fc[]
Definition: dcaenc.h:41
static void assign_bits(DCAEncContext *c)
Definition: dcaenc.c:610
audio channel layout utility functions
int fullband_channels
Definition: dcaenc.c:51
static int init_quantization_noise(DCAEncContext *c, int noise)
Definition: dcaenc.c:573
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
int32_t
int32_t worst_noise_ever
Definition: dcaenc.c:76
#define DCA_MAX_FRAME_SIZE
Definition: dcaenc.c:37
#define DCAENC_SUBBANDS
Definition: dcaenc.c:41
int32_t lfe_peak_cb
Definition: dcaenc.c:61
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
int32_t quantized[SUBBAND_SAMPLES][DCAENC_SUBBANDS][MAX_CHANNELS]
Definition: dcaenc.c:66
int32_t peak_cb[DCAENC_SUBBANDS][MAX_CHANNELS]
Definition: dcaenc.c:67
#define SUBBAND_SAMPLES
Definition: dcaenc.c:44
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:2430
int bitrate_index
Definition: dcaenc.c:55
static const int8_t channel_reorder_lfe[7][5]
Definition: dca_lbr.c:100
static void put_primary_audio_header(DCAEncContext *c)
Definition: dcaenc.c:808
int frame_size
Definition: mxfenc.c:1821
static void find_peaks(DCAEncContext *c)
Definition: dcaenc.c:540
static int32_t mul32(int32_t a, int32_t b)
Definition: dcaenc.c:246
Libavcodec external API header.
const int32_t * band_spectrum
Definition: dcaenc.c:58
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
static int32_t cb_to_level[2048]
Definition: dcaenc.c:85
int sample_rate
samples per second
Definition: avcodec.h:2410
#define ss
main external API structure.
Definition: avcodec.h:1649
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;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);returnNULL;}returnac;}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;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->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);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
void * buf
Definition: avisynth_c.h:553
static void put_subframe_samples(DCAEncContext *c, int ss, int band, int ch)
Definition: dcaenc.c:854
static void adjust_jnd(int samplerate_index, const int32_t in[512], int32_t out_cb[256])
Definition: dcaenc.c:430
float im
Definition: fft.c:73
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1690
int32_t worst_quantization_noise
Definition: dcaenc.c:75
static int encode_init(AVCodecContext *avctx)
Definition: dcaenc.c:108
static int32_t band_interpolation[2][512]
Definition: dcaenc.c:81
static int32_t cos_t(int x)
Definition: dcaenc.c:231
static void calc_scales(DCAEncContext *c)
Definition: dcaenc.c:701
const uint8_t * quant
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:198
#define SUBFRAMES
Definition: dcaenc.c:42
static void subband_transform(DCAEncContext *c, const int32_t *input)
Definition: dcaenc.c:252
internal math functions header
int channel_config
Definition: dcaenc.c:56
AVCodec ff_dca_encoder
Definition: dcaenc.c:980
PutBitContext pb
Definition: dcaenc.c:48
static void fft(const int32_t in[2 *256], cplx32 out[256])
Definition: dcaenc.c:339
common internal api header.
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
if(ret< 0)
Definition: vf_mcdeint.c:282
#define USED_26ABITS
Definition: dcaenc.c:571
static double c[64]
static int32_t get_cb(int32_t in)
Definition: dcaenc.c:406
int lfe_scale_factor
Definition: dcaenc.c:59
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
void * priv_data
Definition: avcodec.h:1691
static av_always_inline int diff(const uint32_t a, const uint32_t b)
int consumed_bits
Definition: dcaenc.c:77
int channels
number of audio channels
Definition: avcodec.h:2411
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:461
static int32_t half32(int32_t a)
Definition: dcaenc.c:241
static const int8_t channel_reorder_nolfe[7][5]
Definition: dca_lbr.c:90
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
const int32_t * band_interpolation
Definition: dcaenc.c:57
int32_t downsampled_lfe[DCA_LFE_SAMPLES]
Definition: dcaenc.c:68
#define M_PI
Definition: mathematics.h:46
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:248
#define FFSWAP(type, a, b)
Definition: common.h:99
static const AVCodecDefault defaults[]
Definition: dcaenc.c:975
static const uint8_t lfe_index[7]
Definition: dca_lbr.c:110
static const int bit_consumption[27]
Definition: dcaenc.h:100
const float ff_dca_fir_32bands_perfect[512]
Definition: dcadata.c:6289
#define AV_CH_LAYOUT_MONO
This structure stores compressed data.
Definition: avcodec.h:1557
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:241
static double gammafilter(int i, double f)
Definition: dcaenc.c:99
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1573
for(j=16;j >0;--j)
bitstream writer API