FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/dcaenc.c
Date: 2022-07-07 01:21:54
Exec Total Coverage
Lines: 518 601 86.2%
Branches: 308 388 79.4%

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