FFmpeg
Loading...
Searching...
No Matches
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 "libavutil/mem.h"
30#include "libavutil/opt.h"
31#include "libavutil/thread.h"
32#include "libavutil/tx.h"
33#include "avcodec.h"
34#include "codec_internal.h"
35#include "dcaadpcm.h"
36#include "dcamath.h"
37#include "dca_core.h"
38#include "dcadata.h"
39#include "dcaenc.h"
40#include "encode.h"
41#include "put_bits.h"
42
43#define MAX_CHANNELS 6
44#define DCA_MAX_FRAME_SIZE 16384
45#define DCA_HEADER_SIZE 13
46#define DCA_LFE_SAMPLES 8
47
48#define DCAENC_SUBBANDS 32
49#define SUBFRAMES 1
50#define SUBSUBFRAMES 2
51#define SUBBAND_SAMPLES (SUBFRAMES * SUBSUBFRAMES * 8)
52#define AUBANDS 25
53
54#define COS_T(x) (c->cos_table[(x) & 2047])
55
59
60typedef struct DCAEncContext {
61 AVClass *class;
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
101 int consumed_adpcm_bits; ///< Number of bits to transmit ADPCM related info
102
111
112/* Transfer function of outer and middle ear, Hz -> dB */
113static 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
123static 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 AVERROR(ENOMEM);
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 +
148 }
149 }
150 return 0;
151}
152
154{
155 if (c->subband[0][0]) {
156 int32_t *bufer = c->subband[0][0] - DCA_ADPCM_COEFFS;
157 av_free(bufer);
158 c->subband[0][0] = NULL;
159 }
160}
161
162static uint16_t bitalloc_12_table[DCA_BITALLOC_12_COUNT][12 + 1][2];
163
165static const uint16_t (*bitalloc_tables[DCA_CODE_BOOKS][8])[2];
166
167static av_cold void create_enc_table(uint16_t dst[][2], unsigned count,
168 const uint8_t (**src_tablep)[2])
169{
170 const uint8_t (*src_table)[2] = *src_tablep;
171 uint16_t code = 0;
172
173 for (unsigned i = 0; i < count; i++) {
174 unsigned dst_idx = src_table[i][0];
175
176 dst[dst_idx][0] = code >> (16 - src_table[i][1]);
177 dst[dst_idx][1] = src_table[i][1];
178
179 code += 1 << (16 - src_table[i][1]);
180 }
181 *src_tablep += count;
182}
183
185{
186 uint16_t (*bitalloc_dst)[2] = bitalloc_table;
187 const uint8_t (*src_table)[2] = ff_dca_vlc_src_tables;
188
189 for (unsigned i = 0; i < DCA_CODE_BOOKS; i++) {
190 for (unsigned j = 0; j < ff_dca_quant_index_group_size[i]; j++) {
192 &src_table);
193 bitalloc_tables[i][j] = bitalloc_dst - ff_dca_bitalloc_offsets[i];
194 bitalloc_dst += ff_dca_bitalloc_sizes[i];
195 }
196 }
197
198 for (unsigned i = 0; i < DCA_BITALLOC_12_COUNT; i++)
199 create_enc_table(&bitalloc_12_table[i][1], 12, &src_table);
200}
201
203{
204 static AVOnce init_static_once = AV_ONCE_INIT;
205 DCAEncContext *c = avctx->priv_data;
207 int i, j, k, min_frame_bits;
208 float scale = 1.0f;
209 int ret;
210
211 if ((ret = subband_bufer_alloc(c)) < 0)
212 return ret;
213
214 c->fullband_channels = c->channels = layout.nb_channels;
215 c->lfe_channel = (c->channels == 3 || c->channels == 6);
216 c->band_interpolation = c->band_interpolation_tab[1];
217 c->band_spectrum = c->band_spectrum_tab[1];
218 c->worst_quantization_noise = -2047;
219 c->worst_noise_ever = -2047;
220 c->consumed_adpcm_bits = 0;
221
222 if (ff_dcaadpcm_init(&c->adpcm_ctx))
223 return AVERROR(ENOMEM);
224
225 switch (layout.nb_channels) {
226 case 1: /* mono */
227 c->channel_config = 0;
228 break;
229 case 2: /* stereo */
230 c->channel_config = 2;
231 break;
232 case 4: /* 2.2 */
233 c->channel_config = 8;
234 break;
235 case 5: /* 5.0 */
236 c->channel_config = 9;
237 break;
238 case 6: /* 5.1 */
239 c->channel_config = 9;
240 break;
241 default:
242 av_assert1(!"impossible channel layout");
243 }
244
245 if (c->lfe_channel) {
246 c->fullband_channels--;
247 c->channel_order_tab = channel_reorder_lfe[c->channel_config];
248 } else {
249 c->channel_order_tab = channel_reorder_nolfe[c->channel_config];
250 }
251
252 for (i = 0; i < MAX_CHANNELS; i++) {
253 for (j = 0; j < DCA_CODE_BOOKS; j++) {
254 c->quant_index_sel[i][j] = ff_dca_quant_index_group_size[j];
255 }
256 /* 6 - no Huffman */
257 c->bit_allocation_sel[i] = 6;
258
259 for (j = 0; j < DCAENC_SUBBANDS; j++) {
260 /* -1 - no ADPCM */
261 c->prediction_mode[i][j] = -1;
262 memset(c->adpcm_history[i][j], 0, sizeof(int32_t)*DCA_ADPCM_COEFFS);
263 }
264 }
265
266 for (i = 0; i < 9; i++) {
267 if (sample_rates[i] == avctx->sample_rate)
268 break;
269 }
270 if (i == 9)
271 return AVERROR(EINVAL);
272 c->samplerate_index = i;
273
274 if (avctx->bit_rate < 32000 || avctx->bit_rate > 3840000) {
275 av_log(avctx, AV_LOG_ERROR, "Bit rate %"PRId64" not supported.", avctx->bit_rate);
276 return AVERROR(EINVAL);
277 }
278 for (i = 0; ff_dca_bit_rates[i] < avctx->bit_rate; i++)
279 ;
280 c->bitrate_index = i;
281 c->frame_bits = FFALIGN((avctx->bit_rate * 512 + avctx->sample_rate - 1) / avctx->sample_rate, 32);
282 min_frame_bits = 132 + (493 + 28 * 32) * c->fullband_channels + c->lfe_channel * 72;
283 if (c->frame_bits < min_frame_bits || c->frame_bits > (DCA_MAX_FRAME_SIZE << 3))
284 return AVERROR(EINVAL);
285
286 c->frame_size = (c->frame_bits + 7) / 8;
287
288 avctx->frame_size = 32 * SUBBAND_SAMPLES;
289
290 if ((ret = av_tx_init(&c->mdct, &c->mdct_fn, AV_TX_INT32_MDCT, 0, 256, &scale, 0)) < 0)
291 return ret;
292
293 /* Init all tables */
294 c->cos_table[0] = 0x7fffffff;
295 c->cos_table[512] = 0;
296 c->cos_table[1024] = -c->cos_table[0];
297 for (i = 1; i < 512; i++) {
298 c->cos_table[i] = (int32_t)(0x7fffffff * cos(M_PI * i / 1024));
299 c->cos_table[1024-i] = -c->cos_table[i];
300 c->cos_table[1024+i] = -c->cos_table[i];
301 c->cos_table[2048-i] = +c->cos_table[i];
302 }
303
304 for (i = 0; i < 2048; i++)
305 c->cb_to_level[i] = (int32_t)(0x7fffffff * ff_exp10(-0.005 * i));
306
307 for (k = 0; k < 32; k++) {
308 for (j = 0; j < 8; j++) {
309 c->lfe_fir_64i[64 * j + k] = (int32_t)(0xffffff800000ULL * ff_dca_lfe_fir_64[8 * k + j]);
310 c->lfe_fir_64i[64 * (7-j) + (63 - k)] = (int32_t)(0xffffff800000ULL * ff_dca_lfe_fir_64[8 * k + j]);
311 }
312 }
313
314 for (i = 0; i < 512; i++) {
315 c->band_interpolation_tab[0][i] = (int32_t)(0x1000000000ULL * ff_dca_fir_32bands_perfect[i]);
316 c->band_interpolation_tab[1][i] = (int32_t)(0x1000000000ULL * ff_dca_fir_32bands_nonperfect[i]);
317 }
318
319 for (i = 0; i < 9; i++) {
320 for (j = 0; j < AUBANDS; j++) {
321 for (k = 0; k < 256; k++) {
322 double freq = sample_rates[i] * (k + 0.5) / 512;
323
324 c->auf[i][j][k] = (int32_t)(10 * (hom(freq) + gammafilter(j, freq)));
325 }
326 }
327 }
328
329 for (i = 0; i < 256; i++) {
330 double add = 1 + ff_exp10(-0.01 * i);
331 c->cb_to_add[i] = (int32_t)(100 * log10(add));
332 }
333 for (j = 0; j < 8; j++) {
334 double accum = 0;
335 for (i = 0; i < 512; i++) {
336 double reconst = ff_dca_fir_32bands_perfect[i] * ((i & 64) ? (-1) : 1);
337 accum += reconst * cos(2 * M_PI * (i + 0.5 - 256) * (j + 0.5) / 512);
338 }
339 c->band_spectrum_tab[0][j] = (int32_t)(200 * log10(accum));
340 }
341 for (j = 0; j < 8; j++) {
342 double accum = 0;
343 for (i = 0; i < 512; i++) {
344 double reconst = ff_dca_fir_32bands_nonperfect[i] * ((i & 64) ? (-1) : 1);
345 accum += reconst * cos(2 * M_PI * (i + 0.5 - 256) * (j + 0.5) / 512);
346 }
347 c->band_spectrum_tab[1][j] = (int32_t)(200 * log10(accum));
348 }
349
350 ff_thread_once(&init_static_once, dcaenc_init_static_tables);
351 return 0;
352}
353
355{
356 DCAEncContext *c = avctx->priv_data;
357 av_tx_uninit(&c->mdct);
359 ff_dcaadpcm_free(&c->adpcm_ctx);
360
361 return 0;
362}
363
364static void subband_transform(DCAEncContext *c, const int32_t *input)
365{
366 int ch, subs, i, k, j;
367
368 for (ch = 0; ch < c->fullband_channels; ch++) {
369 /* History is copied because it is also needed for PSY */
370 int32_t hist[512];
371 int hist_start = 0;
372 const int chi = c->channel_order_tab[ch];
373
374 memcpy(hist, &c->history[ch][0], 512 * sizeof(int32_t));
375
376 for (subs = 0; subs < SUBBAND_SAMPLES; subs++) {
377 int32_t accum[64];
378 int32_t resp;
379 int band;
380
381 /* Calculate the convolutions at once */
382 memset(accum, 0, 64 * sizeof(int32_t));
383
384 for (k = 0, i = hist_start, j = 0;
385 i < 512; k = (k + 1) & 63, i++, j++)
386 accum[k] += mul32(hist[i], c->band_interpolation[j]);
387 for (i = 0; i < hist_start; k = (k + 1) & 63, i++, j++)
388 accum[k] += mul32(hist[i], c->band_interpolation[j]);
389
390 for (k = 16; k < 32; k++)
391 accum[k] = accum[k] - accum[31 - k];
392 for (k = 32; k < 48; k++)
393 accum[k] = accum[k] + accum[95 - k];
394
395 for (band = 0; band < 32; band++) {
396 resp = 0;
397 for (i = 16; i < 48; i++) {
398 int s = (2 * band + 1) * (2 * (i + 16) + 1);
399 resp += mul32(accum[i], COS_T(s << 3)) >> 3;
400 }
401
402 c->subband[ch][band][subs] = ((band + 1) & 2) ? -resp : resp;
403 }
404
405 /* Copy in 32 new samples from input */
406 for (i = 0; i < 32; i++)
407 hist[i + hist_start] = input[(subs * 32 + i) * c->channels + chi];
408
409 hist_start = (hist_start + 32) & 511;
410 }
411 }
412}
413
414static void lfe_downsample(DCAEncContext *c, const int32_t *input)
415{
416 /* FIXME: make 128x LFE downsampling possible */
417 const int lfech = lfe_index[c->channel_config];
418 int i, j, lfes;
419 int32_t hist[512];
420 int32_t accum;
421 int hist_start = 0;
422
423 memcpy(hist, &c->history[c->channels - 1][0], 512 * sizeof(int32_t));
424
425 for (lfes = 0; lfes < DCA_LFE_SAMPLES; lfes++) {
426 /* Calculate the convolution */
427 accum = 0;
428
429 for (i = hist_start, j = 0; i < 512; i++, j++)
430 accum += mul32(hist[i], c->lfe_fir_64i[j]);
431 for (i = 0; i < hist_start; i++, j++)
432 accum += mul32(hist[i], c->lfe_fir_64i[j]);
433
434 c->downsampled_lfe[lfes] = accum;
435
436 /* Copy in 64 new samples from input */
437 for (i = 0; i < 64; i++)
438 hist[i + hist_start] = input[(lfes * 64 + i) * c->channels + lfech];
439
440 hist_start = (hist_start + 64) & 511;
441 }
442}
443
444static uint32_t dca_vlc_calc_alloc_bits(const int values[], uint8_t n, uint8_t sel)
445{
446 uint32_t sum = 0;
447 for (unsigned i = 0; i < n; i++)
448 sum += bitalloc_12_table[sel][values[i]][1];
449 return sum;
450}
451
452static void dca_vlc_enc_alloc(PutBitContext *pb, const int values[],
453 uint8_t n, uint8_t sel)
454{
455 for (unsigned i = 0; i < n; i++)
456 put_bits(pb, bitalloc_12_table[sel][values[i]][1],
457 bitalloc_12_table[sel][values[i]][0]);
458}
459
460static uint32_t dca_vlc_calc_quant_bits(const int values[], uint8_t n,
461 uint8_t sel, uint8_t table)
462{
463 uint32_t sum = 0;
464 for (unsigned i = 0; i < n; i++)
465 sum += bitalloc_tables[table][sel][values[i]][1];
466 return sum;
467}
468
469static void dca_vlc_enc_quant(PutBitContext *pb, const int values[],
470 uint8_t n, uint8_t sel, uint8_t table)
471{
472 for (unsigned i = 0; i < n; i++)
473 put_bits(pb, bitalloc_tables[table][sel][values[i]][1],
474 bitalloc_tables[table][sel][values[i]][0]);
475}
476
478{
479 int i, res = 0;
480 in = FFABS(in);
481
482 for (i = 1024; i > 0; i >>= 1) {
483 if (c->cb_to_level[i + res] >= in)
484 res += i;
485 }
486 return -res;
487}
488
490{
491 if (a < b)
492 FFSWAP(int32_t, a, b);
493
494 if (a - b >= 256)
495 return a;
496 return a + c->cb_to_add[a - b];
497}
498
500 const int32_t in[2 * 256], int32_t power[256])
501{
502 int i;
505
506 for (i = 0; i < 512; i++)
507 data[i] = norm__(mul32(in[i], 0x3fffffff - (COS_T(4 * i + 2) >> 1)), 4);
508
509 c->mdct_fn(c->mdct, coeff, data, sizeof(int32_t));
510 for (i = 0; i < 256; i++) {
511 const int32_t cb = get_cb(c, coeff[i]);
512 power[i] = add_cb(c, cb, cb);
513 }
514}
515
517 const int32_t in[512], int32_t out_cb[256])
518{
519 int32_t power[256];
520 int32_t out_cb_unnorm[256];
521 int32_t denom;
522 const int32_t ca_cb = -1114;
523 const int32_t cs_cb = 928;
524 const int samplerate_index = c->samplerate_index;
525 int i, j;
526
527 calc_power(c, in, power);
528
529 for (j = 0; j < 256; j++)
530 out_cb_unnorm[j] = -2047; /* and can only grow */
531
532 for (i = 0; i < AUBANDS; i++) {
533 denom = ca_cb; /* and can only grow */
534 for (j = 0; j < 256; j++)
535 denom = add_cb(c, denom, power[j] + c->auf[samplerate_index][i][j]);
536 for (j = 0; j < 256; j++)
537 out_cb_unnorm[j] = add_cb(c, out_cb_unnorm[j],
538 -denom + c->auf[samplerate_index][i][j]);
539 }
540
541 for (j = 0; j < 256; j++)
542 out_cb[j] = add_cb(c, out_cb[j], -out_cb_unnorm[j] - ca_cb - cs_cb);
543}
544
545typedef void (*walk_band_t)(DCAEncContext *c, int band1, int band2, int f,
546 int32_t spectrum1, int32_t spectrum2, int channel,
547 int32_t * arg);
548
549static void walk_band_low(DCAEncContext *c, int band, int channel,
550 walk_band_t walk, int32_t *arg)
551{
552 int f;
553
554 if (band == 0) {
555 for (f = 0; f < 4; f++)
556 walk(c, 0, 0, f, 0, -2047, channel, arg);
557 } else {
558 for (f = 0; f < 8; f++)
559 walk(c, band, band - 1, 8 * band - 4 + f,
560 c->band_spectrum[7 - f], c->band_spectrum[f], channel, arg);
561 }
562}
563
564static void walk_band_high(DCAEncContext *c, int band, int channel,
565 walk_band_t walk, int32_t *arg)
566{
567 int f;
568
569 if (band == 31) {
570 for (f = 0; f < 4; f++)
571 walk(c, 31, 31, 256 - 4 + f, 0, -2047, channel, arg);
572 } else {
573 for (f = 0; f < 8; f++)
574 walk(c, band, band + 1, 8 * band + 4 + f,
575 c->band_spectrum[f], c->band_spectrum[7 - f], channel, arg);
576 }
577}
578
579static void update_band_masking(DCAEncContext *c, int band1, int band2,
580 int f, int32_t spectrum1, int32_t spectrum2,
581 int channel, int32_t * arg)
582{
583 int32_t value = c->eff_masking_curve_cb[f] - spectrum1;
584
585 if (value < c->band_masking_cb[band1])
586 c->band_masking_cb[band1] = value;
587}
588
589static void calc_masking(DCAEncContext *c, const int32_t *input)
590{
591 int i, k, band, ch, ssf;
592 int32_t data[512];
593
594 for (i = 0; i < 256; i++)
595 for (ssf = 0; ssf < SUBSUBFRAMES; ssf++)
596 c->masking_curve_cb[ssf][i] = -2047;
597
598 for (ssf = 0; ssf < SUBSUBFRAMES; ssf++)
599 for (ch = 0; ch < c->fullband_channels; ch++) {
600 const int chi = c->channel_order_tab[ch];
601
602 for (i = 0, k = 128 + 256 * ssf; k < 512; i++, k++)
603 data[i] = c->history[ch][k];
604 for (k -= 512; i < 512; i++, k++)
605 data[i] = input[k * c->channels + chi];
606 adjust_jnd(c, data, c->masking_curve_cb[ssf]);
607 }
608 for (i = 0; i < 256; i++) {
609 int32_t m = 2048;
610
611 for (ssf = 0; ssf < SUBSUBFRAMES; ssf++)
612 if (c->masking_curve_cb[ssf][i] < m)
613 m = c->masking_curve_cb[ssf][i];
614 c->eff_masking_curve_cb[i] = m;
615 }
616
617 for (band = 0; band < 32; band++) {
618 c->band_masking_cb[band] = 2048;
621 }
622}
623
624static inline int32_t find_peak(DCAEncContext *c, const int32_t *in, int len)
625{
626 int sample;
627 int32_t m = 0;
628 for (sample = 0; sample < len; sample++) {
629 int32_t s = abs(in[sample]);
630 if (m < s)
631 m = s;
632 }
633 return get_cb(c, m);
634}
635
637{
638 int band, ch;
639
640 for (ch = 0; ch < c->fullband_channels; ch++) {
641 for (band = 0; band < 32; band++)
642 c->peak_cb[ch][band] = find_peak(c, c->subband[ch][band],
644 }
645
646 if (c->lfe_channel)
647 c->lfe_peak_cb = find_peak(c, c->downsampled_lfe, DCA_LFE_SAMPLES);
648}
649
651{
652 int ch, band;
653 int pred_vq_id;
654 int32_t *samples;
655 int32_t estimated_diff[SUBBAND_SAMPLES];
656
657 c->consumed_adpcm_bits = 0;
658 for (ch = 0; ch < c->fullband_channels; ch++) {
659 for (band = 0; band < 32; band++) {
660 samples = c->subband[ch][band] - DCA_ADPCM_COEFFS;
661 pred_vq_id = ff_dcaadpcm_subband_analysis(&c->adpcm_ctx, samples,
662 SUBBAND_SAMPLES, estimated_diff);
663 if (pred_vq_id >= 0) {
664 c->prediction_mode[ch][band] = pred_vq_id;
665 c->consumed_adpcm_bits += 12; //12 bits to transmit prediction vq index
666 c->diff_peak_cb[ch][band] = find_peak(c, estimated_diff, 16);
667 } else {
668 c->prediction_mode[ch][band] = -1;
669 }
670 }
671 }
672}
673
674static const int snr_fudge = 128;
675#define USED_1ABITS 1
676#define USED_26ABITS 4
677
678static inline int32_t get_step_size(DCAEncContext *c, int ch, int band)
679{
680 int32_t step_size;
681
682 if (c->bitrate_index == 3)
683 step_size = ff_dca_lossless_quant[c->abits[ch][band]];
684 else
685 step_size = ff_dca_lossy_quant[c->abits[ch][band]];
686
687 return step_size;
688}
689
690static int calc_one_scale(DCAEncContext *c, int32_t peak_cb, int abits,
692{
693 int32_t peak;
694 int our_nscale, try_remove;
695 softfloat our_quant;
696
697 av_assert0(peak_cb <= 0);
698 av_assert0(peak_cb >= -2047);
699
700 our_nscale = 127;
701 peak = c->cb_to_level[-peak_cb];
702
703 for (try_remove = 64; try_remove > 0; try_remove >>= 1) {
704 if (scalefactor_inv[our_nscale - try_remove].e + stepsize_inv[abits].e <= 17)
705 continue;
706 our_quant.m = mul32(scalefactor_inv[our_nscale - try_remove].m, stepsize_inv[abits].m);
707 our_quant.e = scalefactor_inv[our_nscale - try_remove].e + stepsize_inv[abits].e - 17;
708 if ((ff_dca_quant_levels[abits] - 1) / 2 < quantize_value(peak, our_quant))
709 continue;
710 our_nscale -= try_remove;
711 }
712
713 if (our_nscale >= 125)
714 our_nscale = 124;
715
716 quant->m = mul32(scalefactor_inv[our_nscale].m, stepsize_inv[abits].m);
717 quant->e = scalefactor_inv[our_nscale].e + stepsize_inv[abits].e - 17;
718 av_assert0((ff_dca_quant_levels[abits] - 1) / 2 >= quantize_value(peak, *quant));
719
720 return our_nscale;
721}
722
723static inline void quantize_adpcm_subband(DCAEncContext *c, int ch, int band)
724{
725 int32_t step_size;
726 int32_t diff_peak_cb = c->diff_peak_cb[ch][band];
727 c->scale_factor[ch][band] = calc_one_scale(c, diff_peak_cb,
728 c->abits[ch][band],
729 &c->quant[ch][band]);
730
731 step_size = get_step_size(c, ch, band);
732 ff_dcaadpcm_do_real(c->prediction_mode[ch][band],
733 c->quant[ch][band],
734 ff_dca_scale_factor_quant7[c->scale_factor[ch][band]],
735 step_size, c->adpcm_history[ch][band], c->subband[ch][band],
736 c->adpcm_history[ch][band] + 4, c->quantized[ch][band],
737 SUBBAND_SAMPLES, c->cb_to_level[-diff_peak_cb]);
738}
739
741{
742 int band, ch;
743
744 for (ch = 0; ch < c->fullband_channels; ch++)
745 for (band = 0; band < 32; band++)
746 if (c->prediction_mode[ch][band] >= 0)
747 quantize_adpcm_subband(c, ch, band);
748}
749
751{
752 int sample, band, ch;
753
754 for (ch = 0; ch < c->fullband_channels; ch++) {
755 for (band = 0; band < 32; band++) {
756 if (c->prediction_mode[ch][band] == -1) {
757 for (sample = 0; sample < SUBBAND_SAMPLES; sample++) {
758 int32_t val = quantize_value(c->subband[ch][band][sample],
759 c->quant[ch][band]);
760 c->quantized[ch][band][sample] = val;
761 }
762 }
763 }
764 }
765}
766
767static void accumulate_huff_bit_consumption(int abits, int32_t *quantized,
768 uint32_t *result)
769{
770 uint8_t sel, id = abits - 1;
771 for (sel = 0; sel < ff_dca_quant_index_group_size[id]; sel++)
772 result[sel] += dca_vlc_calc_quant_bits(quantized, SUBBAND_SAMPLES,
773 sel, id);
774}
775
776static uint32_t set_best_code(uint32_t vlc_bits[DCA_CODE_BOOKS][7],
777 uint32_t clc_bits[DCA_CODE_BOOKS],
779{
780 uint8_t i, sel;
781 uint32_t best_sel_bits[DCA_CODE_BOOKS];
782 int32_t best_sel_id[DCA_CODE_BOOKS];
783 uint32_t t, bits = 0;
784
785 for (i = 0; i < DCA_CODE_BOOKS; i++) {
786
787 av_assert0(!((!!vlc_bits[i][0]) ^ (!!clc_bits[i])));
788 if (vlc_bits[i][0] == 0) {
789 /* do not transmit adjustment index for empty codebooks */
791 /* and skip it */
792 continue;
793 }
794
795 best_sel_bits[i] = vlc_bits[i][0];
796 best_sel_id[i] = 0;
797 for (sel = 0; sel < ff_dca_quant_index_group_size[i]; sel++) {
798 if (best_sel_bits[i] > vlc_bits[i][sel] && vlc_bits[i][sel]) {
799 best_sel_bits[i] = vlc_bits[i][sel];
800 best_sel_id[i] = sel;
801 }
802 }
803
804 /* 2 bits to transmit scale factor adjustment index */
805 t = best_sel_bits[i] + 2;
806 if (t < clc_bits[i]) {
807 res[i] = best_sel_id[i];
808 bits += t;
809 } else {
811 bits += clc_bits[i];
812 }
813 }
814 return bits;
815}
816
817static uint32_t set_best_abits_code(int abits[DCAENC_SUBBANDS], int bands,
818 int32_t *res)
819{
820 uint8_t i;
821 uint32_t t;
822 int32_t best_sel = 6;
823 int32_t best_bits = bands * 5;
824
825 /* Check do we have subband which cannot be encoded by Huffman tables */
826 for (i = 0; i < bands; i++) {
827 if (abits[i] > 12 || abits[i] == 0) {
828 *res = best_sel;
829 return best_bits;
830 }
831 }
832
833 for (i = 0; i < DCA_BITALLOC_12_COUNT; i++) {
834 t = dca_vlc_calc_alloc_bits(abits, bands, i);
835 if (t < best_bits) {
836 best_bits = t;
837 best_sel = i;
838 }
839 }
840
841 *res = best_sel;
842 return best_bits;
843}
844
845static int init_quantization_noise(DCAEncContext *c, int noise, int forbid_zero)
846{
847 int ch, band, ret = USED_26ABITS | USED_1ABITS;
848 uint32_t huff_bit_count_accum[MAX_CHANNELS][DCA_CODE_BOOKS][7];
849 uint32_t clc_bit_count_accum[MAX_CHANNELS][DCA_CODE_BOOKS];
850 uint32_t bits_counter = 0;
851
852 c->consumed_bits = 132 + 333 * c->fullband_channels;
853 c->consumed_bits += c->consumed_adpcm_bits;
854 if (c->lfe_channel)
855 c->consumed_bits += 72;
856
857 /* attempt to guess the bit distribution based on the previous frame */
858 for (ch = 0; ch < c->fullband_channels; ch++) {
859 for (band = 0; band < 32; band++) {
860 int snr_cb = c->peak_cb[ch][band] - c->band_masking_cb[band] - noise;
861
862 if (snr_cb >= 1312) {
863 c->abits[ch][band] = 26;
864 ret &= ~USED_1ABITS;
865 } else if (snr_cb >= 222) {
866 c->abits[ch][band] = 8 + mul32(snr_cb - 222, 69000000);
867 ret &= ~(USED_26ABITS | USED_1ABITS);
868 } else if (snr_cb >= 0) {
869 c->abits[ch][band] = 2 + mul32(snr_cb, 106000000);
870 ret &= ~(USED_26ABITS | USED_1ABITS);
871 } else if (forbid_zero || snr_cb >= -140) {
872 c->abits[ch][band] = 1;
873 ret &= ~USED_26ABITS;
874 } else {
875 c->abits[ch][band] = 0;
876 ret &= ~(USED_26ABITS | USED_1ABITS);
877 }
878 }
879 c->consumed_bits += set_best_abits_code(c->abits[ch], 32,
880 &c->bit_allocation_sel[ch]);
881 }
882
883 /* Recalc scale_factor each time to get bits consumption in case of Huffman coding.
884 It is suboptimal solution */
885 /* TODO: May be cache scaled values */
886 for (ch = 0; ch < c->fullband_channels; ch++) {
887 for (band = 0; band < 32; band++) {
888 if (c->prediction_mode[ch][band] == -1) {
889 c->scale_factor[ch][band] = calc_one_scale(c, c->peak_cb[ch][band],
890 c->abits[ch][band],
891 &c->quant[ch][band]);
892 }
893 }
894 }
897
898 memset(huff_bit_count_accum, 0, MAX_CHANNELS * DCA_CODE_BOOKS * 7 * sizeof(uint32_t));
899 memset(clc_bit_count_accum, 0, MAX_CHANNELS * DCA_CODE_BOOKS * sizeof(uint32_t));
900 for (ch = 0; ch < c->fullband_channels; ch++) {
901 for (band = 0; band < 32; band++) {
902 if (c->abits[ch][band] && c->abits[ch][band] <= DCA_CODE_BOOKS) {
903 accumulate_huff_bit_consumption(c->abits[ch][band],
904 c->quantized[ch][band],
905 huff_bit_count_accum[ch][c->abits[ch][band] - 1]);
906 clc_bit_count_accum[ch][c->abits[ch][band] - 1] += bit_consumption[c->abits[ch][band]];
907 } else {
908 bits_counter += bit_consumption[c->abits[ch][band]];
909 }
910 }
911 }
912
913 for (ch = 0; ch < c->fullband_channels; ch++) {
914 bits_counter += set_best_code(huff_bit_count_accum[ch],
915 clc_bit_count_accum[ch],
916 c->quant_index_sel[ch]);
917 }
918
919 c->consumed_bits += bits_counter;
920
921 return ret;
922}
923
925{
926 /* Find the bounds where the binary search should work */
927 int low, high, down;
928 int used_abits = 0;
929 int forbid_zero = 1;
930restart:
931 init_quantization_noise(c, c->worst_quantization_noise, forbid_zero);
932 low = high = c->worst_quantization_noise;
933 if (c->consumed_bits > c->frame_bits) {
934 while (c->consumed_bits > c->frame_bits) {
935 if (used_abits == USED_1ABITS && forbid_zero) {
936 forbid_zero = 0;
937 goto restart;
938 }
939 low = high;
940 high += snr_fudge;
941 used_abits = init_quantization_noise(c, high, forbid_zero);
942 }
943 } else {
944 while (c->consumed_bits <= c->frame_bits) {
945 high = low;
946 if (used_abits == USED_26ABITS)
947 goto out; /* The requested bitrate is too high, pad with zeros */
948 low -= snr_fudge;
949 used_abits = init_quantization_noise(c, low, forbid_zero);
950 }
951 }
952
953 /* Now do a binary search between low and high to see what fits */
954 for (down = snr_fudge >> 1; down; down >>= 1) {
955 init_quantization_noise(c, high - down, forbid_zero);
956 if (c->consumed_bits <= c->frame_bits)
957 high -= down;
958 }
959 init_quantization_noise(c, high, forbid_zero);
960out:
961 c->worst_quantization_noise = high;
962 if (high > c->worst_noise_ever)
963 c->worst_noise_ever = high;
964}
965
966static void shift_history(DCAEncContext *c, const int32_t *input)
967{
968 int k, ch;
969
970 for (k = 0; k < 512; k++)
971 for (ch = 0; ch < c->channels; ch++) {
972 const int chi = c->channel_order_tab[ch];
973
974 c->history[ch][k] = input[k * c->channels + chi];
975 }
976}
977
979{
980 int ch, band;
981 int32_t step_size;
982 /* We fill in ADPCM work buffer for subbands which hasn't been ADPCM coded
983 * in current frame - we need this data if subband of next frame is
984 * ADPCM
985 */
986 for (ch = 0; ch < c->channels; ch++) {
987 for (band = 0; band < 32; band++) {
988 int32_t *samples = c->subband[ch][band] - DCA_ADPCM_COEFFS;
989 if (c->prediction_mode[ch][band] == -1) {
990 step_size = get_step_size(c, ch, band);
991
992 ff_dca_core_dequantize(c->adpcm_history[ch][band],
993 c->quantized[ch][band]+12, step_size,
994 ff_dca_scale_factor_quant7[c->scale_factor[ch][band]], 0, 4);
995 } else {
996 AV_COPY128U(c->adpcm_history[ch][band], c->adpcm_history[ch][band]+4);
997 }
998 /* Copy dequantized values for LPC analysis.
999 * It reduces artifacts in case of extreme quantization,
1000 * example: in current frame abits is 1 and has no prediction flag,
1001 * but end of this frame is sine like signal. In this case, if LPC analysis uses
1002 * original values, likely LPC analysis returns good prediction gain, and sets prediction flag.
1003 * But there are no proper value in decoder history, so likely result will be no good.
1004 * Bitstream has "Predictor history flag switch", but this flag disables history for all subbands
1005 */
1006 samples[0] = c->adpcm_history[ch][band][0] * (1 << 7);
1007 samples[1] = c->adpcm_history[ch][band][1] * (1 << 7);
1008 samples[2] = c->adpcm_history[ch][band][2] * (1 << 7);
1009 samples[3] = c->adpcm_history[ch][band][3] * (1 << 7);
1010 }
1011 }
1012}
1013
1015{
1016 if (c->lfe_channel)
1017 c->lfe_scale_factor = calc_one_scale(c, c->lfe_peak_cb, 11, &c->lfe_quant);
1018}
1019
1021{
1022 /* SYNC */
1023 put_bits(&c->pb, 16, 0x7ffe);
1024 put_bits(&c->pb, 16, 0x8001);
1025
1026 /* Frame type: normal */
1027 put_bits(&c->pb, 1, 1);
1028
1029 /* Deficit sample count: none */
1030 put_bits(&c->pb, 5, 31);
1031
1032 /* CRC is not present */
1033 put_bits(&c->pb, 1, 0);
1034
1035 /* Number of PCM sample blocks */
1036 put_bits(&c->pb, 7, SUBBAND_SAMPLES - 1);
1037
1038 /* Primary frame byte size */
1039 put_bits(&c->pb, 14, c->frame_size - 1);
1040
1041 /* Audio channel arrangement */
1042 put_bits(&c->pb, 6, c->channel_config);
1043
1044 /* Core audio sampling frequency */
1045 put_bits(&c->pb, 4, bitstream_sfreq[c->samplerate_index]);
1046
1047 /* Transmission bit rate */
1048 put_bits(&c->pb, 5, c->bitrate_index);
1049
1050 /* Embedded down mix: disabled */
1051 put_bits(&c->pb, 1, 0);
1052
1053 /* Embedded dynamic range flag: not present */
1054 put_bits(&c->pb, 1, 0);
1055
1056 /* Embedded time stamp flag: not present */
1057 put_bits(&c->pb, 1, 0);
1058
1059 /* Auxiliary data flag: not present */
1060 put_bits(&c->pb, 1, 0);
1061
1062 /* HDCD source: no */
1063 put_bits(&c->pb, 1, 0);
1064
1065 /* Extension audio ID: N/A */
1066 put_bits(&c->pb, 3, 0);
1067
1068 /* Extended audio data: not present */
1069 put_bits(&c->pb, 1, 0);
1070
1071 /* Audio sync word insertion flag: after each sub-frame */
1072 put_bits(&c->pb, 1, 0);
1073
1074 /* Low frequency effects flag: not present or 64x subsampling */
1075 put_bits(&c->pb, 2, c->lfe_channel ? 2 : 0);
1076
1077 /* Predictor history switch flag: on */
1078 put_bits(&c->pb, 1, 1);
1079
1080 /* No CRC */
1081 /* Multirate interpolator switch: non-perfect reconstruction */
1082 put_bits(&c->pb, 1, 0);
1083
1084 /* Encoder software revision: 7 */
1085 put_bits(&c->pb, 4, 7);
1086
1087 /* Copy history: 0 */
1088 put_bits(&c->pb, 2, 0);
1089
1090 /* Source PCM resolution: 16 bits, not DTS ES */
1091 put_bits(&c->pb, 3, 0);
1092
1093 /* Front sum/difference coding: no */
1094 put_bits(&c->pb, 1, 0);
1095
1096 /* Surrounds sum/difference coding: no */
1097 put_bits(&c->pb, 1, 0);
1098
1099 /* Dialog normalization: 0 dB */
1100 put_bits(&c->pb, 4, 0);
1101}
1102
1104{
1105 int ch, i;
1106 /* Number of subframes */
1107 put_bits(&c->pb, 4, SUBFRAMES - 1);
1108
1109 /* Number of primary audio channels */
1110 put_bits(&c->pb, 3, c->fullband_channels - 1);
1111
1112 /* Subband activity count */
1113 for (ch = 0; ch < c->fullband_channels; ch++)
1114 put_bits(&c->pb, 5, DCAENC_SUBBANDS - 2);
1115
1116 /* High frequency VQ start subband */
1117 for (ch = 0; ch < c->fullband_channels; ch++)
1118 put_bits(&c->pb, 5, DCAENC_SUBBANDS - 1);
1119
1120 /* Joint intensity coding index: 0, 0 */
1121 for (ch = 0; ch < c->fullband_channels; ch++)
1122 put_bits(&c->pb, 3, 0);
1123
1124 /* Transient mode codebook: A4, A4 (arbitrary) */
1125 for (ch = 0; ch < c->fullband_channels; ch++)
1126 put_bits(&c->pb, 2, 0);
1127
1128 /* Scale factor code book: 7 bit linear, 7-bit sqrt table (for each channel) */
1129 for (ch = 0; ch < c->fullband_channels; ch++)
1130 put_bits(&c->pb, 3, 6);
1131
1132 /* Bit allocation quantizer select: linear 5-bit */
1133 for (ch = 0; ch < c->fullband_channels; ch++)
1134 put_bits(&c->pb, 3, c->bit_allocation_sel[ch]);
1135
1136 /* Quantization index codebook select */
1137 for (i = 0; i < DCA_CODE_BOOKS; i++)
1138 for (ch = 0; ch < c->fullband_channels; ch++)
1139 put_bits(&c->pb, ff_dca_quant_index_sel_nbits[i], c->quant_index_sel[ch][i]);
1140
1141 /* Scale factor adjustment index: transmitted in case of Huffman coding */
1142 for (i = 0; i < DCA_CODE_BOOKS; i++)
1143 for (ch = 0; ch < c->fullband_channels; ch++)
1144 if (c->quant_index_sel[ch][i] < ff_dca_quant_index_group_size[i])
1145 put_bits(&c->pb, 2, 0);
1146
1147 /* Audio header CRC check word: not transmitted */
1148}
1149
1150static void put_subframe_samples(DCAEncContext *c, int ss, int band, int ch)
1151{
1152 int i, j, sum, bits, sel;
1153 if (c->abits[ch][band] <= DCA_CODE_BOOKS) {
1154 av_assert0(c->abits[ch][band] > 0);
1155 sel = c->quant_index_sel[ch][c->abits[ch][band] - 1];
1156 // Huffman codes
1157 if (sel < ff_dca_quant_index_group_size[c->abits[ch][band] - 1]) {
1158 dca_vlc_enc_quant(&c->pb, &c->quantized[ch][band][ss * 8], 8,
1159 sel, c->abits[ch][band] - 1);
1160 return;
1161 }
1162
1163 // Block codes
1164 if (c->abits[ch][band] <= 7) {
1165 for (i = 0; i < 8; i += 4) {
1166 sum = 0;
1167 for (j = 3; j >= 0; j--) {
1168 sum *= ff_dca_quant_levels[c->abits[ch][band]];
1169 sum += c->quantized[ch][band][ss * 8 + i + j];
1170 sum += (ff_dca_quant_levels[c->abits[ch][band]] - 1) / 2;
1171 }
1172 put_bits(&c->pb, bit_consumption[c->abits[ch][band]] / 4, sum);
1173 }
1174 return;
1175 }
1176 }
1177
1178 for (i = 0; i < 8; i++) {
1179 bits = bit_consumption[c->abits[ch][band]] / 16;
1180 put_sbits(&c->pb, bits, c->quantized[ch][band][ss * 8 + i]);
1181 }
1182}
1183
1184static void put_subframe(DCAEncContext *c, int subframe)
1185{
1186 int i, band, ss, ch;
1187
1188 /* Subsubframes count */
1189 put_bits(&c->pb, 2, SUBSUBFRAMES -1);
1190
1191 /* Partial subsubframe sample count: dummy */
1192 put_bits(&c->pb, 3, 0);
1193
1194 /* Prediction mode: no ADPCM, in each channel and subband */
1195 for (ch = 0; ch < c->fullband_channels; ch++)
1196 for (band = 0; band < DCAENC_SUBBANDS; band++)
1197 put_bits(&c->pb, 1, !(c->prediction_mode[ch][band] == -1));
1198
1199 /* Prediction VQ address */
1200 for (ch = 0; ch < c->fullband_channels; ch++)
1201 for (band = 0; band < DCAENC_SUBBANDS; band++)
1202 if (c->prediction_mode[ch][band] >= 0)
1203 put_bits(&c->pb, 12, c->prediction_mode[ch][band]);
1204
1205 /* Bit allocation index */
1206 for (ch = 0; ch < c->fullband_channels; ch++) {
1207 if (c->bit_allocation_sel[ch] == 6) {
1208 for (band = 0; band < DCAENC_SUBBANDS; band++) {
1209 put_bits(&c->pb, 5, c->abits[ch][band]);
1210 }
1211 } else {
1212 dca_vlc_enc_alloc(&c->pb, c->abits[ch], DCAENC_SUBBANDS,
1213 c->bit_allocation_sel[ch]);
1214 }
1215 }
1216
1217 if (SUBSUBFRAMES > 1) {
1218 /* Transition mode: none for each channel and subband */
1219 for (ch = 0; ch < c->fullband_channels; ch++)
1220 for (band = 0; band < DCAENC_SUBBANDS; band++)
1221 if (c->abits[ch][band])
1222 put_bits(&c->pb, 1, 0); /* codebook A4 */
1223 }
1224
1225 /* Scale factors */
1226 for (ch = 0; ch < c->fullband_channels; ch++)
1227 for (band = 0; band < DCAENC_SUBBANDS; band++)
1228 if (c->abits[ch][band])
1229 put_bits(&c->pb, 7, c->scale_factor[ch][band]);
1230
1231 /* Joint subband scale factor codebook select: not transmitted */
1232 /* Scale factors for joint subband coding: not transmitted */
1233 /* Stereo down-mix coefficients: not transmitted */
1234 /* Dynamic range coefficient: not transmitted */
1235 /* Stde information CRC check word: not transmitted */
1236 /* VQ encoded high frequency subbands: not transmitted */
1237
1238 /* LFE data: 8 samples and scalefactor */
1239 if (c->lfe_channel) {
1240 for (i = 0; i < DCA_LFE_SAMPLES; i++)
1241 put_bits(&c->pb, 8, quantize_value(c->downsampled_lfe[i], c->lfe_quant) & 0xff);
1242 put_bits(&c->pb, 8, c->lfe_scale_factor);
1243 }
1244
1245 /* Audio data (subsubframes) */
1246 for (ss = 0; ss < SUBSUBFRAMES ; ss++)
1247 for (ch = 0; ch < c->fullband_channels; ch++)
1248 for (band = 0; band < DCAENC_SUBBANDS; band++)
1249 if (c->abits[ch][band])
1250 put_subframe_samples(c, ss, band, ch);
1251
1252 /* DSYNC */
1253 put_bits(&c->pb, 16, 0xffff);
1254}
1255
1256static int encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
1257 const AVFrame *frame, int *got_packet_ptr)
1258{
1259 DCAEncContext *c = avctx->priv_data;
1260 const int32_t *samples;
1261 int ret, i;
1262
1263 if ((ret = ff_get_encode_buffer(avctx, avpkt, c->frame_size, 0)) < 0)
1264 return ret;
1265
1266 samples = (const int32_t *)frame->data[0];
1267
1268 subband_transform(c, samples);
1269 if (c->lfe_channel)
1270 lfe_downsample(c, samples);
1271
1272 calc_masking(c, samples);
1273 if (c->options.adpcm_mode)
1275 find_peaks(c);
1276 assign_bits(c);
1278 shift_history(c, samples);
1279
1280 init_put_bits(&c->pb, avpkt->data, avpkt->size);
1284 for (i = 0; i < SUBFRAMES; i++)
1285 put_subframe(c, i);
1286
1287 flush_put_bits(&c->pb);
1288 memset(put_bits_ptr(&c->pb), 0, put_bytes_left(&c->pb, 0));
1289
1290 *got_packet_ptr = 1;
1291 return 0;
1292}
1293
1294#define DCAENC_FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
1295
1296static const AVOption options[] = {
1297 { "dca_adpcm", "Use ADPCM encoding", offsetof(DCAEncContext, options.adpcm_mode), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DCAENC_FLAGS },
1298 { NULL },
1299};
1300
1301static const AVClass dcaenc_class = {
1302 .class_name = "DCA (DTS Coherent Acoustics)",
1303 .item_name = av_default_item_name,
1304 .option = options,
1305 .version = LIBAVUTIL_VERSION_INT,
1306};
1307
1308static const FFCodecDefault defaults[] = {
1309 { "b", "1411200" },
1310 { NULL },
1311};
1312
1314 .p.name = "dca",
1315 CODEC_LONG_NAME("DCA (DTS Coherent Acoustics)"),
1316 .p.type = AVMEDIA_TYPE_AUDIO,
1317 .p.id = AV_CODEC_ID_DTS,
1318 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_EXPERIMENTAL |
1320 .priv_data_size = sizeof(DCAEncContext),
1321 .init = encode_init,
1324 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1330 .defaults = defaults,
1331 .p.priv_class = &dcaenc_class,
1332};
#define MAX_CHANNELS
Definition aac.h:33
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static double val(void *priv, double ch)
Definition aeval.c:77
static const float bands[]
const FFCodec ff_dca_encoder
Definition dcaenc.c:1313
static const FFCodecDefault defaults[]
Definition amfenc_av1.c:723
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
static av_cold int encode_init(AVCodecContext *avctx)
Definition asvenc.c:373
int32_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
#define ss(width, name, subs,...)
Definition cbs_vp9.c:202
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define CODEC_SAMPLERATES_ARRAY(array)
#define CODEC_CH_LAYOUTS(...)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define CODEC_SAMPLEFMTS(...)
common internal and external API header
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
#define NULL
Definition coverity.c:32
#define abs(x)
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:226
static const int8_t channel_reorder_lfe[7][5]
Definition dca_lbr.c:98
static const uint8_t lfe_index[7]
Definition dca_lbr.c:108
static const int8_t channel_reorder_nolfe[7][5]
Definition dca_lbr.c:88
int ff_dcaadpcm_subband_analysis(const DCAADPCMEncContext *s, const int32_t *in, int len, int *diff)
Definition dcaadpcm.c:126
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:184
av_cold void ff_dcaadpcm_free(DCAADPCMEncContext *s)
Definition dcaadpcm.c:226
av_cold int ff_dcaadpcm_init(DCAADPCMEncContext *s)
Definition dcaadpcm.c:213
const uint8_t ff_dca_quant_index_sel_nbits[DCA_CODE_BOOKS]
Definition dcadata.c:49
const float ff_dca_fir_32bands_nonperfect[512]
Definition dcadata.c:6808
const uint32_t ff_dca_lossless_quant[32]
Definition dcadata.c:4231
const uint32_t ff_dca_scale_factor_quant7[128]
Definition dcadata.c:4172
const uint8_t ff_dca_quant_index_group_size[DCA_CODE_BOOKS]
Definition dcadata.c:53
const uint32_t ff_dca_lossy_quant[32]
Definition dcadata.c:4223
const float ff_dca_fir_32bands_perfect[512]
Definition dcadata.c:6293
const uint32_t ff_dca_quant_levels[32]
Definition dcadata.c:4215
const float ff_dca_lfe_fir_64[256]
Definition dcadata.c:7339
const uint32_t ff_dca_bit_rates[32]
Definition dcadata.c:32
#define DCA_ADPCM_COEFFS
Definition dcadata.h:28
static double hom(double f)
Definition dcaenc.c:113
#define SUBFRAMES
Definition dcaenc.c:49
static void calc_lfe_scales(DCAEncContext *c)
Definition dcaenc.c:1014
static uint16_t bitalloc_table[DCA_NUM_BITALLOC_CODES][2]
Definition dcaenc.c:164
#define AUBANDS
Definition dcaenc.c:52
static void put_frame_header(DCAEncContext *c)
Definition dcaenc.c:1020
#define DCAENC_FLAGS
Definition dcaenc.c:1294
static const int snr_fudge
Definition dcaenc.c:674
static void walk_band_low(DCAEncContext *c, int band, int channel, walk_band_t walk, int32_t *arg)
Definition dcaenc.c:549
static const uint16_t(*[DCA_CODE_BOOKS][8] bitalloc_tables)[2]
Definition dcaenc.c:165
static void assign_bits(DCAEncContext *c)
Definition dcaenc.c:924
static uint32_t dca_vlc_calc_quant_bits(const int values[], uint8_t n, uint8_t sel, uint8_t table)
Definition dcaenc.c:460
#define DCA_MAX_FRAME_SIZE
Definition dcaenc.c:44
static void shift_history(DCAEncContext *c, const int32_t *input)
Definition dcaenc.c:966
static void calc_power(DCAEncContext *c, const int32_t in[2 *256], int32_t power[256])
Definition dcaenc.c:499
#define SUBSUBFRAMES
Definition dcaenc.c:50
static int init_quantization_noise(DCAEncContext *c, int noise, int forbid_zero)
Definition dcaenc.c:845
static void put_subframe(DCAEncContext *c, int subframe)
Definition dcaenc.c:1184
static void adjust_jnd(DCAEncContext *c, const int32_t in[512], int32_t out_cb[256])
Definition dcaenc.c:516
static void dca_vlc_enc_alloc(PutBitContext *pb, const int values[], uint8_t n, uint8_t sel)
Definition dcaenc.c:452
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:545
static int subband_bufer_alloc(DCAEncContext *c)
Definition dcaenc.c:132
static int32_t get_step_size(DCAEncContext *c, int ch, int band)
Definition dcaenc.c:678
static void adpcm_analysis(DCAEncContext *c)
Definition dcaenc.c:650
static int32_t find_peak(DCAEncContext *c, const int32_t *in, int len)
Definition dcaenc.c:624
static void quantize_pcm(DCAEncContext *c)
Definition dcaenc.c:750
#define USED_1ABITS
Definition dcaenc.c:675
static av_cold int encode_init(AVCodecContext *avctx)
Definition dcaenc.c:202
static av_cold int encode_close(AVCodecContext *avctx)
Definition dcaenc.c:354
static void quantize_adpcm(DCAEncContext *c)
Definition dcaenc.c:740
static int32_t add_cb(DCAEncContext *c, int32_t a, int32_t b)
Definition dcaenc.c:489
static void subband_transform(DCAEncContext *c, const int32_t *input)
Definition dcaenc.c:364
static uint32_t dca_vlc_calc_alloc_bits(const int values[], uint8_t n, uint8_t sel)
Definition dcaenc.c:444
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:776
static void put_primary_audio_header(DCAEncContext *c)
Definition dcaenc.c:1103
static uint16_t bitalloc_12_table[DCA_BITALLOC_12_COUNT][12+1][2]
Definition dcaenc.c:162
static av_cold void dcaenc_init_static_tables(void)
Definition dcaenc.c:184
static av_cold void create_enc_table(uint16_t dst[][2], unsigned count, const uint8_t(**src_tablep)[2])
Definition dcaenc.c:167
static void calc_masking(DCAEncContext *c, const int32_t *input)
Definition dcaenc.c:589
static uint32_t set_best_abits_code(int abits[DCAENC_SUBBANDS], int bands, int32_t *res)
Definition dcaenc.c:817
static double gammafilter(int i, double f)
Definition dcaenc.c:123
static void lfe_downsample(DCAEncContext *c, const int32_t *input)
Definition dcaenc.c:414
static void walk_band_high(DCAEncContext *c, int band, int channel, walk_band_t walk, int32_t *arg)
Definition dcaenc.c:564
static void put_subframe_samples(DCAEncContext *c, int ss, int band, int ch)
Definition dcaenc.c:1150
#define MAX_CHANNELS
Definition dcaenc.c:43
static void accumulate_huff_bit_consumption(int abits, int32_t *quantized, uint32_t *result)
Definition dcaenc.c:767
static const AVClass dcaenc_class
Definition dcaenc.c:1301
static int calc_one_scale(DCAEncContext *c, int32_t peak_cb, int abits, softfloat *quant)
Definition dcaenc.c:690
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:579
#define COS_T(x)
Definition dcaenc.c:54
static int32_t get_cb(DCAEncContext *c, int32_t in)
Definition dcaenc.c:477
#define SUBBAND_SAMPLES
Definition dcaenc.c:51
static void subband_bufer_free(DCAEncContext *c)
Definition dcaenc.c:153
#define DCA_LFE_SAMPLES
Definition dcaenc.c:46
static void fill_in_adpcm_bufer(DCAEncContext *c)
Definition dcaenc.c:978
static void find_peaks(DCAEncContext *c)
Definition dcaenc.c:636
#define USED_26ABITS
Definition dcaenc.c:676
static void quantize_adpcm_subband(DCAEncContext *c, int ch, int band)
Definition dcaenc.c:723
static int encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition dcaenc.c:1256
#define DCAENC_SUBBANDS
Definition dcaenc.c:48
static void dca_vlc_enc_quant(PutBitContext *pb, const int values[], uint8_t n, uint8_t sel, uint8_t table)
Definition dcaenc.c:469
static int32_t quantize_value(int32_t value, softfloat quant)
Definition dcaenc.h:149
static const softfloat scalefactor_inv[128]
Definition dcaenc.h:63
static const int sample_rates[]
Definition dcaenc.h:34
static const uint16_t fc[]
Definition dcaenc.h:43
static const int bit_consumption[27]
Definition dcaenc.h:101
static const softfloat stepsize_inv[27]
Definition dcaenc.h:53
static const uint8_t bitstream_sfreq[]
Definition dcaenc.h:38
static const uint16_t erb[]
Definition dcaenc.h:48
const uint8_t ff_dca_vlc_src_tables[][2]
Definition dcahuff.c:58
const int8_t ff_dca_bitalloc_offsets[DCA_CODE_BOOKS]
Definition dcahuff.c:41
const uint8_t ff_dca_bitalloc_sizes[DCA_CODE_BOOKS]
Definition dcahuff.c:37
#define DCA_NUM_BITALLOC_CODES
Definition dcahuff.h:34
#define DCA_CODE_BOOKS
Definition dcahuff.h:32
#define DCA_BITALLOC_12_COUNT
Definition dcahuff.h:33
static int32_t mul32(int32_t a, int32_t b)
Definition dcamath.h:52
static int32_t norm__(int64_t a, int bits)
Definition dcamath.h:27
static AVFrame * frame
int high
Definition dovi_rpuenc.c:39
enum AVCodecID id
Definition dts2pts.c:607
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
channel
Use these values when setting the channel map with ebur128_set_channel().
Definition ebur128.h:39
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition encode.c:106
double value
Definition eval.c:102
int8_t exp
Definition eval.c:76
static const uint8_t bits[8]
Definition fastaudio.c:100
internal math functions header
static av_always_inline double ff_exp10(double x)
Compute 10^x for floating point values.
Definition ffmath.h:42
static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame, AVPacket *pkt)
Definition ffmpeg_enc.c:694
#define sample
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition codec.h:147
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_EXPERIMENTAL
Codec is experimental and is thus avoided in favor of non experimental encoders.
Definition codec.h:90
@ AV_CODEC_ID_DTS
Definition codec_id.h:457
#define AV_CHANNEL_LAYOUT_5POINT0
#define AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_2_2
#define AV_CHANNEL_LAYOUT_5POINT1
#define AV_CHANNEL_LAYOUT_MONO
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_S32
signed 32 bits
Definition samplefmt.h:59
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
int a
#define b
Definition input.c:43
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition intra.c:278
#define AV_COPY128U(d, s)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition j2kenc.c:154
const char * arg
Definition jacosubdec.c:65
#define av_cold
Definition attributes.h:117
#define AVOnce
Definition thread.h:202
static int ff_thread_once(char *control, void(*routine)(void))
Definition thread.h:205
#define AV_ONCE_INIT
Definition thread.h:203
#define FFSWAP(type, a, b)
Definition macros.h:52
#define FFALIGN(x, a)
Definition macros.h:78
#define M_PI
Definition mathematics.h:67
uint64_t layout
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
#define LOCAL_ALIGNED_32(t, v,...)
const char data[16]
Definition mxf.c:149
static int noise(AVBSFContext *ctx, AVPacket *pkt)
Definition noise.c:124
AVOptions.
static float power(float r, float g, float b, float max)
static const uint16_t table[]
Definition prosumer.c:203
bitstream writer API
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition put_bits.h:291
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition put_bits.h:62
static uint8_t * put_bits_ptr(PutBitContext *s)
Return the pointer to the byte where the bitstream writer will put the next bit.
Definition put_bits.h:402
static int put_bytes_left(const PutBitContext *s, int round_up)
Definition put_bits.h:145
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition put_bits.h:153
const uint8_t * code
Definition spdifenc.c:433
An AVChannelLayout holds information about the channel layout of audio data.
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
int64_t bit_rate
the average bitrate
Definition avcodec.h:493
int sample_rate
samples per second
Definition avcodec.h:1040
int frame_size
Number of samples per channel in an audio frame.
Definition avcodec.h:1068
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
int lfe_channel
Definition dcaenc.c:71
const int32_t * band_interpolation
Definition dcaenc.c:75
int scale_factor[MAX_CHANNELS][DCAENC_SUBBANDS]
Definition dcaenc.c:93
int32_t eff_masking_curve_cb[256]
Definition dcaenc.c:96
int frame_bits
Definition dcaenc.c:68
int fullband_channels
Definition dcaenc.c:69
int32_t masking_curve_cb[SUBSUBFRAMES][256]
Definition dcaenc.c:90
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 adpcm_history[MAX_CHANNELS][DCAENC_SUBBANDS][DCA_ADPCM_COEFFS *2]
Definition dcaenc.c:83
int32_t quant_index_sel[MAX_CHANNELS][DCA_CODE_BOOKS]
Definition dcaenc.c:95
int32_t lfe_peak_cb
Definition dcaenc.c:79
int32_t prediction_mode[MAX_CHANNELS][DCAENC_SUBBANDS]
Definition dcaenc.c:82
DCAADPCMEncContext adpcm_ctx
Definition dcaenc.c:63
int32_t worst_quantization_noise
Definition dcaenc.c:98
const int8_t * channel_order_tab
channel reordering table, lfe and non lfe
Definition dcaenc.c:80
av_tx_fn mdct_fn
Definition dcaenc.c:65
int32_t * subband[MAX_CHANNELS][DCAENC_SUBBANDS]
Definition dcaenc.c:85
int32_t cb_to_add[256]
Definition dcaenc.c:107
int32_t band_masking_cb[32]
Definition dcaenc.c:97
int32_t band_spectrum_tab[2][8]
Definition dcaenc.c:105
AVTXContext * mdct
Definition dcaenc.c:64
int32_t quantized[MAX_CHANNELS][DCAENC_SUBBANDS][SUBBAND_SAMPLES]
Definition dcaenc.c:86
int abits[MAX_CHANNELS][DCAENC_SUBBANDS]
Definition dcaenc.c:92
int channel_config
Definition dcaenc.c:74
int bitrate_index
Definition dcaenc.c:73
int consumed_adpcm_bits
Number of bits to transmit ADPCM related info.
Definition dcaenc.c:101
int32_t cb_to_level[2048]
Definition dcaenc.c:108
int32_t lfe_fir_64i[512]
Definition dcaenc.c:109
int32_t auf[9][AUBANDS][256]
Definition dcaenc.c:106
int32_t diff_peak_cb[MAX_CHANNELS][DCAENC_SUBBANDS]
expected peak of residual signal
Definition dcaenc.c:88
int channels
Definition dcaenc.c:70
int32_t cos_table[2048]
Definition dcaenc.c:103
int32_t band_interpolation_tab[2][512]
Definition dcaenc.c:104
int32_t bit_allocation_sel[MAX_CHANNELS]
Definition dcaenc.c:91
softfloat quant[MAX_CHANNELS][DCAENC_SUBBANDS]
Definition dcaenc.c:94
int32_t history[MAX_CHANNELS][512]
Definition dcaenc.c:84
const int32_t * band_spectrum
Definition dcaenc.c:76
int32_t worst_noise_ever
Definition dcaenc.c:99
CompressionOptions options
Definition dcaenc.c:66
PutBitContext pb
Definition dcaenc.c:62
int consumed_bits
Definition dcaenc.c:100
int frame_size
Definition dcaenc.c:67
int lfe_scale_factor
Definition dcaenc.c:77
int samplerate_index
Definition dcaenc.c:72
softfloat lfe_quant
Definition dcaenc.c:78
int32_t m
Definition dcaenc.h:30
int32_t e
Definition dcaenc.h:31
#define av_free(p)
#define av_log(a,...)
static FILE * out
Definition movenc.c:55
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets *ctx to NULL, does nothing when *ctx == NULL.
Definition tx.c:295
av_cold int av_tx_init(AVTXContext **ctx, av_tx_fn *tx, enum AVTXType type, int inv, int len, const void *scale, uint64_t flags)
Initialize a transform context with the given configuration (i)MDCTs with an odd length are currently...
Definition tx.c:903
@ AV_TX_INT32_MDCT
Definition tx.h:70
void(* av_tx_fn)(AVTXContext *s, void *out, void *in, ptrdiff_t stride)
Function pointer to a function to perform the transform.
Definition tx.h:151
static double cb(void *priv, double x, double y)
Definition vf_geq.c:247
static const double coeff[2][5]
static const uint8_t quant[64]
Definition vmixdec.c:71
int len
static double c[64]