FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/dcaenc.c
Date: 2026-10-19 01:23:10
Exec Total Coverage
Lines: 597 648 92.1%
Functions: 41 43 95.3%
Branches: 351 400 87.8%

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 #include "libavutil/avassert.h"
25 #include "libavutil/channel_layout.h"
26 #include "libavutil/common.h"
27 #include "libavutil/ffmath.h"
28 #include "libavutil/mem.h"
29 #include "libavutil/mem_internal.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
56 typedef struct CompressionOptions {
57 int adpcm_mode;
58 } CompressionOptions;
59
60 typedef struct DCAEncContext {
61 AVClass *class;
62 PutBitContext pb;
63 DCAADPCMEncContext adpcm_ctx;
64 AVTXContext *mdct;
65 av_tx_fn mdct_fn;
66 CompressionOptions options;
67 int frame_size;
68 int frame_bits;
69 int fullband_channels;
70 int channels;
71 int lfe_channel;
72 int samplerate_index;
73 int bitrate_index;
74 int channel_config;
75 const int32_t *band_interpolation;
76 const int32_t *band_spectrum;
77 int lfe_scale_factor;
78 softfloat lfe_quant;
79 int32_t lfe_peak_cb;
80 const int8_t *channel_order_tab; ///< channel reordering table, lfe and non lfe
81
82 int32_t prediction_mode[MAX_CHANNELS][DCAENC_SUBBANDS];
83 int32_t adpcm_history[MAX_CHANNELS][DCAENC_SUBBANDS][DCA_ADPCM_COEFFS * 2];
84 int32_t history[MAX_CHANNELS][512]; /* This is a circular buffer */
85 int32_t *subband[MAX_CHANNELS][DCAENC_SUBBANDS];
86 int32_t quantized[MAX_CHANNELS][DCAENC_SUBBANDS][SUBBAND_SAMPLES];
87 int32_t peak_cb[MAX_CHANNELS][DCAENC_SUBBANDS];
88 int32_t diff_peak_cb[MAX_CHANNELS][DCAENC_SUBBANDS]; ///< expected peak of residual signal
89 int32_t downsampled_lfe[DCA_LFE_SAMPLES];
90 int32_t masking_curve_cb[SUBSUBFRAMES][256];
91 int32_t bit_allocation_sel[MAX_CHANNELS];
92 int abits[MAX_CHANNELS][DCAENC_SUBBANDS];
93 int scale_factor[MAX_CHANNELS][DCAENC_SUBBANDS];
94 softfloat quant[MAX_CHANNELS][DCAENC_SUBBANDS];
95 int32_t quant_index_sel[MAX_CHANNELS][DCA_CODE_BOOKS];
96 int32_t eff_masking_curve_cb[256];
97 int32_t band_masking_cb[32];
98 int32_t worst_quantization_noise;
99 int32_t worst_noise_ever;
100 int consumed_bits;
101 int consumed_adpcm_bits; ///< Number of bits to transmit ADPCM related info
102
103 int32_t cos_table[2048];
104 int32_t band_interpolation_tab[2][512];
105 int32_t band_spectrum_tab[2][8];
106 int32_t auf[9][AUBANDS][256];
107 int32_t cb_to_add[256];
108 int32_t cb_to_level[2048];
109 int32_t lfe_fir_64i[512];
110 } DCAEncContext;
111
112 /* Transfer function of outer and middle ear, Hz -> dB */
113 172800 static double hom(double f)
114 {
115 172800 double f1 = f / 1000;
116
117 172800 return -3.64 * pow(f1, -0.8)
118 172800 + 6.8 * exp(-0.6 * (f1 - 3.4) * (f1 - 3.4))
119 172800 - 6.0 * exp(-0.15 * (f1 - 8.7) * (f1 - 8.7))
120 172800 - 0.0006 * (f1 * f1) * (f1 * f1);
121 }
122
123 172800 static double gammafilter(int i, double f)
124 {
125 172800 double h = (f - fc[i]) / erb[i];
126
127 172800 h = 1 + h * h;
128 172800 h = 1 / (h * h);
129 172800 return 20 * log10(h);
130 }
131
132 3 static int subband_bufer_alloc(DCAEncContext *c)
133 {
134 int ch, band;
135 3 int32_t *bufer = av_calloc(MAX_CHANNELS * DCAENC_SUBBANDS *
136 (SUBBAND_SAMPLES + DCA_ADPCM_COEFFS),
137 sizeof(int32_t));
138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 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
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 3 times.
21 for (ch = 0; ch < MAX_CHANNELS; ch++) {
144
2/2
✓ Branch 0 taken 576 times.
✓ Branch 1 taken 18 times.
594 for (band = 0; band < DCAENC_SUBBANDS; band++) {
145 576 c->subband[ch][band] = bufer +
146 576 ch * DCAENC_SUBBANDS * (SUBBAND_SAMPLES + DCA_ADPCM_COEFFS) +
147 576 band * (SUBBAND_SAMPLES + DCA_ADPCM_COEFFS) + DCA_ADPCM_COEFFS;
148 }
149 }
150 3 return 0;
151 }
152
153 3 static void subband_bufer_free(DCAEncContext *c)
154 {
155
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (c->subband[0][0]) {
156 3 int32_t *bufer = c->subband[0][0] - DCA_ADPCM_COEFFS;
157 3 av_free(bufer);
158 3 c->subband[0][0] = NULL;
159 }
160 3 }
161
162 static uint16_t bitalloc_12_table[DCA_BITALLOC_12_COUNT][12 + 1][2];
163
164 static uint16_t bitalloc_table[DCA_NUM_BITALLOC_CODES][2];
165 static const uint16_t (*bitalloc_tables[DCA_CODE_BOOKS][8])[2];
166
167 159 static av_cold void create_enc_table(uint16_t dst[][2], unsigned count,
168 const uint8_t (**src_tablep)[2])
169 {
170 159 const uint8_t (*src_table)[2] = *src_tablep;
171 159 uint16_t code = 0;
172
173
2/2
✓ Branch 0 taken 6144 times.
✓ Branch 1 taken 159 times.
6303 for (unsigned i = 0; i < count; i++) {
174 6144 unsigned dst_idx = src_table[i][0];
175
176 6144 dst[dst_idx][0] = code >> (16 - src_table[i][1]);
177 6144 dst[dst_idx][1] = src_table[i][1];
178
179 6144 code += 1 << (16 - src_table[i][1]);
180 }
181 159 *src_tablep += count;
182 159 }
183
184 3 static av_cold void dcaenc_init_static_tables(void)
185 {
186 3 uint16_t (*bitalloc_dst)[2] = bitalloc_table;
187 3 const uint8_t (*src_table)[2] = ff_dca_vlc_src_tables;
188
189
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 3 times.
33 for (unsigned i = 0; i < DCA_CODE_BOOKS; i++) {
190
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 30 times.
174 for (unsigned j = 0; j < ff_dca_quant_index_group_size[i]; j++) {
191 144 create_enc_table(bitalloc_dst, ff_dca_bitalloc_sizes[i],
192 &src_table);
193 144 bitalloc_tables[i][j] = bitalloc_dst - ff_dca_bitalloc_offsets[i];
194 144 bitalloc_dst += ff_dca_bitalloc_sizes[i];
195 }
196 }
197
198
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 3 times.
18 for (unsigned i = 0; i < DCA_BITALLOC_12_COUNT; i++)
199 15 create_enc_table(&bitalloc_12_table[i][1], 12, &src_table);
200 3 }
201
202 3 static av_cold int encode_init(AVCodecContext *avctx)
203 {
204 static AVOnce init_static_once = AV_ONCE_INIT;
205 3 DCAEncContext *c = avctx->priv_data;
206 3 AVChannelLayout layout = avctx->ch_layout;
207 int i, j, k, min_frame_bits;
208 3 float scale = 1.0f;
209 int ret;
210
211
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if ((ret = subband_bufer_alloc(c)) < 0)
212 ✗ return ret;
213
214 3 c->fullband_channels = c->channels = layout.nb_channels;
215
3/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
3 c->lfe_channel = (c->channels == 3 || c->channels == 6);
216 3 c->band_interpolation = c->band_interpolation_tab[1];
217 3 c->band_spectrum = c->band_spectrum_tab[1];
218 3 c->worst_quantization_noise = -2047;
219 3 c->worst_noise_ever = -2047;
220 3 c->consumed_adpcm_bits = 0;
221
222
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (ff_dcaadpcm_init(&c->adpcm_ctx))
223 ✗ return AVERROR(ENOMEM);
224
225
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
3 switch (layout.nb_channels) {
226 ✗ case 1: /* mono */
227 ✗ c->channel_config = 0;
228 ✗ break;
229 2 case 2: /* stereo */
230 2 c->channel_config = 2;
231 2 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 1 case 6: /* 5.1 */
239 1 c->channel_config = 9;
240 1 break;
241 3 default:
242 av_assert1(!"impossible channel layout");
243 }
244
245
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (c->lfe_channel) {
246 1 c->fullband_channels--;
247 1 c->channel_order_tab = channel_reorder_lfe[c->channel_config];
248 } else {
249 2 c->channel_order_tab = channel_reorder_nolfe[c->channel_config];
250 }
251
252
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 3 times.
21 for (i = 0; i < MAX_CHANNELS; i++) {
253
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 18 times.
198 for (j = 0; j < DCA_CODE_BOOKS; j++) {
254 180 c->quant_index_sel[i][j] = ff_dca_quant_index_group_size[j];
255 }
256 /* 6 - no Huffman */
257 18 c->bit_allocation_sel[i] = 6;
258
259
2/2
✓ Branch 0 taken 576 times.
✓ Branch 1 taken 18 times.
594 for (j = 0; j < DCAENC_SUBBANDS; j++) {
260 /* -1 - no ADPCM */
261 576 c->prediction_mode[i][j] = -1;
262 576 memset(c->adpcm_history[i][j], 0, sizeof(int32_t)*DCA_ADPCM_COEFFS);
263 }
264 }
265
266
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 for (i = 0; i < 9; i++) {
267
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 15 times.
18 if (sample_rates[i] == avctx->sample_rate)
268 3 break;
269 }
270
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (i == 9)
271 ✗ return AVERROR(EINVAL);
272 3 c->samplerate_index = i;
273
274
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 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
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 3 times.
69 for (i = 0; ff_dca_bit_rates[i] < avctx->bit_rate; i++)
279 ;
280 3 c->bitrate_index = i;
281 3 c->frame_bits = FFALIGN((avctx->bit_rate * 512 + avctx->sample_rate - 1) / avctx->sample_rate, 32);
282 3 min_frame_bits = 132 + (493 + 28 * 32) * c->fullband_channels + c->lfe_channel * 72;
283
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if (c->frame_bits < min_frame_bits || c->frame_bits > (DCA_MAX_FRAME_SIZE << 3))
284 ✗ return AVERROR(EINVAL);
285
286 3 c->frame_size = (c->frame_bits + 7) / 8;
287
288 3 avctx->frame_size = 32 * SUBBAND_SAMPLES;
289
290
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 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 3 c->cos_table[0] = 0x7fffffff;
295 3 c->cos_table[512] = 0;
296 3 c->cos_table[1024] = -c->cos_table[0];
297
2/2
✓ Branch 0 taken 1533 times.
✓ Branch 1 taken 3 times.
1536 for (i = 1; i < 512; i++) {
298 1533 c->cos_table[i] = (int32_t)(0x7fffffff * cos(M_PI * i / 1024));
299 1533 c->cos_table[1024-i] = -c->cos_table[i];
300 1533 c->cos_table[1024+i] = -c->cos_table[i];
301 1533 c->cos_table[2048-i] = +c->cos_table[i];
302 }
303
304
2/2
✓ Branch 0 taken 6144 times.
✓ Branch 1 taken 3 times.
6147 for (i = 0; i < 2048; i++)
305 6144 c->cb_to_level[i] = (int32_t)(0x7fffffff * ff_exp10(-0.005 * i));
306
307
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 3 times.
99 for (k = 0; k < 32; k++) {
308
2/2
✓ Branch 0 taken 768 times.
✓ Branch 1 taken 96 times.
864 for (j = 0; j < 8; j++) {
309 768 c->lfe_fir_64i[64 * j + k] = (int32_t)(0xffffff800000ULL * (double)ff_dca_lfe_fir_64[8 * k + j]);
310 768 c->lfe_fir_64i[64 * (7-j) + (63 - k)] = (int32_t)(0xffffff800000ULL * (double)ff_dca_lfe_fir_64[8 * k + j]);
311 }
312 }
313
314
2/2
✓ Branch 0 taken 1536 times.
✓ Branch 1 taken 3 times.
1539 for (i = 0; i < 512; i++) {
315 1536 c->band_interpolation_tab[0][i] = (int32_t)(0x1000000000ULL * ff_dca_fir_32bands_perfect[i]);
316 1536 c->band_interpolation_tab[1][i] = (int32_t)(0x1000000000ULL * ff_dca_fir_32bands_nonperfect[i]);
317 }
318
319
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 3 times.
30 for (i = 0; i < 9; i++) {
320
2/2
✓ Branch 0 taken 675 times.
✓ Branch 1 taken 27 times.
702 for (j = 0; j < AUBANDS; j++) {
321
2/2
✓ Branch 0 taken 172800 times.
✓ Branch 1 taken 675 times.
173475 for (k = 0; k < 256; k++) {
322 172800 double freq = sample_rates[i] * (k + 0.5) / 512;
323
324 172800 c->auf[i][j][k] = (int32_t)(10 * (hom(freq) + gammafilter(j, freq)));
325 }
326 }
327 }
328
329
2/2
✓ Branch 0 taken 768 times.
✓ Branch 1 taken 3 times.
771 for (i = 0; i < 256; i++) {
330 768 double add = 1 + ff_exp10(-0.01 * i);
331 768 c->cb_to_add[i] = (int32_t)(100 * log10(add));
332 }
333
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 3 times.
27 for (j = 0; j < 8; j++) {
334 24 double accum = 0;
335
2/2
✓ Branch 0 taken 12288 times.
✓ Branch 1 taken 24 times.
12312 for (i = 0; i < 512; i++) {
336
2/2
✓ Branch 0 taken 6144 times.
✓ Branch 1 taken 6144 times.
12288 double reconst = ff_dca_fir_32bands_perfect[i] * ((i & 64) ? (-1) : 1);
337 12288 accum += reconst * cos(2 * M_PI * (i + 0.5 - 256) * (j + 0.5) / 512);
338 }
339 24 c->band_spectrum_tab[0][j] = (int32_t)(200 * log10(accum));
340 }
341
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 3 times.
27 for (j = 0; j < 8; j++) {
342 24 double accum = 0;
343
2/2
✓ Branch 0 taken 12288 times.
✓ Branch 1 taken 24 times.
12312 for (i = 0; i < 512; i++) {
344
2/2
✓ Branch 0 taken 6144 times.
✓ Branch 1 taken 6144 times.
12288 double reconst = ff_dca_fir_32bands_nonperfect[i] * ((i & 64) ? (-1) : 1);
345 12288 accum += reconst * cos(2 * M_PI * (i + 0.5 - 256) * (j + 0.5) / 512);
346 }
347 24 c->band_spectrum_tab[1][j] = (int32_t)(200 * log10(accum));
348 }
349
350 3 ff_thread_once(&init_static_once, dcaenc_init_static_tables);
351 3 return 0;
352 }
353
354 3 static av_cold int encode_close(AVCodecContext *avctx)
355 {
356 3 DCAEncContext *c = avctx->priv_data;
357 3 av_tx_uninit(&c->mdct);
358 3 subband_bufer_free(c);
359 3 ff_dcaadpcm_free(&c->adpcm_ctx);
360
361 3 return 0;
362 }
363
364 1551 static void subband_transform(DCAEncContext *c, const int32_t *input)
365 {
366 int ch, subs, i, k, j;
367
368
2/2
✓ Branch 0 taken 4653 times.
✓ Branch 1 taken 1551 times.
6204 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 4653 int hist_start = 0;
372 4653 const int chi = c->channel_order_tab[ch];
373
374 4653 memcpy(hist, &c->history[ch][0], 512 * sizeof(int32_t));
375
376
2/2
✓ Branch 0 taken 74448 times.
✓ Branch 1 taken 4653 times.
79101 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 74448 memset(accum, 0, 64 * sizeof(int32_t));
383
384 74448 for (k = 0, i = hist_start, j = 0;
385
2/2
✓ Branch 0 taken 20249856 times.
✓ Branch 1 taken 74448 times.
20324304 i < 512; k = (k + 1) & 63, i++, j++)
386 20249856 accum[k] += mul32(hist[i], c->band_interpolation[j]);
387
2/2
✓ Branch 0 taken 17867520 times.
✓ Branch 1 taken 74448 times.
17941968 for (i = 0; i < hist_start; k = (k + 1) & 63, i++, j++)
388 17867520 accum[k] += mul32(hist[i], c->band_interpolation[j]);
389
390
2/2
✓ Branch 0 taken 1191168 times.
✓ Branch 1 taken 74448 times.
1265616 for (k = 16; k < 32; k++)
391 1191168 accum[k] = accum[k] - accum[31 - k];
392
2/2
✓ Branch 0 taken 1191168 times.
✓ Branch 1 taken 74448 times.
1265616 for (k = 32; k < 48; k++)
393 1191168 accum[k] = accum[k] + accum[95 - k];
394
395
2/2
✓ Branch 0 taken 2382336 times.
✓ Branch 1 taken 74448 times.
2456784 for (band = 0; band < 32; band++) {
396 2382336 resp = 0;
397
2/2
✓ Branch 0 taken 76234752 times.
✓ Branch 1 taken 2382336 times.
78617088 for (i = 16; i < 48; i++) {
398 76234752 int s = (2 * band + 1) * (2 * (i + 16) + 1);
399 76234752 resp += mul32(accum[i], COS_T(s << 3)) >> 3;
400 }
401
402
2/2
✓ Branch 0 taken 1191168 times.
✓ Branch 1 taken 1191168 times.
2382336 c->subband[ch][band][subs] = ((band + 1) & 2) ? -resp : resp;
403 }
404
405 /* Copy in 32 new samples from input */
406
2/2
✓ Branch 0 taken 2382336 times.
✓ Branch 1 taken 74448 times.
2456784 for (i = 0; i < 32; i++)
407 2382336 hist[i + hist_start] = input[(subs * 32 + i) * c->channels + chi];
408
409 74448 hist_start = (hist_start + 32) & 511;
410 }
411 }
412 1551 }
413
414 517 static void lfe_downsample(DCAEncContext *c, const int32_t *input)
415 {
416 /* FIXME: make 128x LFE downsampling possible */
417 517 const int lfech = lfe_index[c->channel_config];
418 int i, j, lfes;
419 int32_t hist[512];
420 int32_t accum;
421 517 int hist_start = 0;
422
423 517 memcpy(hist, &c->history[c->channels - 1][0], 512 * sizeof(int32_t));
424
425
2/2
✓ Branch 0 taken 4136 times.
✓ Branch 1 taken 517 times.
4653 for (lfes = 0; lfes < DCA_LFE_SAMPLES; lfes++) {
426 /* Calculate the convolution */
427 4136 accum = 0;
428
429
2/2
✓ Branch 0 taken 1191168 times.
✓ Branch 1 taken 4136 times.
1195304 for (i = hist_start, j = 0; i < 512; i++, j++)
430 1191168 accum += mul32(hist[i], c->lfe_fir_64i[j]);
431
2/2
✓ Branch 0 taken 926464 times.
✓ Branch 1 taken 4136 times.
930600 for (i = 0; i < hist_start; i++, j++)
432 926464 accum += mul32(hist[i], c->lfe_fir_64i[j]);
433
434 4136 c->downsampled_lfe[lfes] = accum;
435
436 /* Copy in 64 new samples from input */
437
2/2
✓ Branch 0 taken 264704 times.
✓ Branch 1 taken 4136 times.
268840 for (i = 0; i < 64; i++)
438 264704 hist[i + hist_start] = input[(lfes * 64 + i) * c->channels + lfech];
439
440 4136 hist_start = (hist_start + 64) & 511;
441 }
442 517 }
443
444 7810 static uint32_t dca_vlc_calc_alloc_bits(const int values[], uint8_t n, uint8_t sel)
445 {
446 7810 uint32_t sum = 0;
447
2/2
✓ Branch 0 taken 249920 times.
✓ Branch 1 taken 7810 times.
257730 for (unsigned i = 0; i < n; i++)
448 249920 sum += bitalloc_12_table[sel][values[i]][1];
449 7810 return sum;
450 }
451
452 180 static void dca_vlc_enc_alloc(PutBitContext *pb, const int values[],
453 uint8_t n, uint8_t sel)
454 {
455
2/2
✓ Branch 0 taken 5760 times.
✓ Branch 1 taken 180 times.
5940 for (unsigned i = 0; i < n; i++)
456 5760 put_bits(pb, bitalloc_12_table[sel][values[i]][1],
457 5760 bitalloc_12_table[sel][values[i]][0]);
458 180 }
459
460 3237631 static uint32_t dca_vlc_calc_quant_bits(const int values[], uint8_t n,
461 uint8_t sel, uint8_t table)
462 {
463 3237631 uint32_t sum = 0;
464
2/2
✓ Branch 0 taken 51802096 times.
✓ Branch 1 taken 3237631 times.
55039727 for (unsigned i = 0; i < n; i++)
465 51802096 sum += bitalloc_tables[table][sel][values[i]][1];
466 3237631 return sum;
467 }
468
469 123924 static void dca_vlc_enc_quant(PutBitContext *pb, const int values[],
470 uint8_t n, uint8_t sel, uint8_t table)
471 {
472
2/2
✓ Branch 0 taken 991392 times.
✓ Branch 1 taken 123924 times.
1115316 for (unsigned i = 0; i < n; i++)
473 991392 put_bits(pb, bitalloc_tables[table][sel][values[i]][1],
474 991392 bitalloc_tables[table][sel][values[i]][0]);
475 123924 }
476
477 2531749 static int32_t get_cb(DCAEncContext *c, int32_t in)
478 {
479 2531749 int i, res = 0;
480 2531749 in = FFABS(in);
481
482
2/2
✓ Branch 0 taken 27849239 times.
✓ Branch 1 taken 2531749 times.
30380988 for (i = 1024; i > 0; i >>= 1) {
483
2/2
✓ Branch 0 taken 14791429 times.
✓ Branch 1 taken 13057810 times.
27849239 if (c->cb_to_level[i + res] >= in)
484 14791429 res += i;
485 }
486 2531749 return -res;
487 }
488
489 123881472 static int32_t add_cb(DCAEncContext *c, int32_t a, int32_t b)
490 {
491
2/2
✓ Branch 0 taken 29312426 times.
✓ Branch 1 taken 94569046 times.
123881472 if (a < b)
492 29312426 FFSWAP(int32_t, a, b);
493
494
2/2
✓ Branch 0 taken 70907837 times.
✓ Branch 1 taken 52973635 times.
123881472 if (a - b >= 256)
495 70907837 return a;
496 52973635 return a + c->cb_to_add[a - b];
497 }
498
499 9306 static void calc_power(DCAEncContext *c,
500 const int32_t in[2 * 256], int32_t power[256])
501 {
502 int i;
503 9306 LOCAL_ALIGNED_32(int32_t, data, [512]);
504 9306 LOCAL_ALIGNED_32(int32_t, coeff, [256]);
505
506
2/2
✓ Branch 0 taken 4764672 times.
✓ Branch 1 taken 9306 times.
4773978 for (i = 0; i < 512; i++)
507 4764672 data[i] = norm__(mul32(in[i], 0x3fffffff - (COS_T(4 * i + 2) >> 1)), 4);
508
509 9306 c->mdct_fn(c->mdct, coeff, data, sizeof(int32_t));
510
2/2
✓ Branch 0 taken 2382336 times.
✓ Branch 1 taken 9306 times.
2391642 for (i = 0; i < 256; i++) {
511 2382336 const int32_t cb = get_cb(c, coeff[i]);
512 2382336 power[i] = add_cb(c, cb, cb);
513 }
514 9306 }
515
516 9306 static void adjust_jnd(DCAEncContext *c,
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 9306 const int32_t ca_cb = -1114;
523 9306 const int32_t cs_cb = 928;
524 9306 const int samplerate_index = c->samplerate_index;
525 int i, j;
526
527 9306 calc_power(c, in, power);
528
529
2/2
✓ Branch 0 taken 2382336 times.
✓ Branch 1 taken 9306 times.
2391642 for (j = 0; j < 256; j++)
530 2382336 out_cb_unnorm[j] = -2047; /* and can only grow */
531
532
2/2
✓ Branch 0 taken 232650 times.
✓ Branch 1 taken 9306 times.
241956 for (i = 0; i < AUBANDS; i++) {
533 232650 denom = ca_cb; /* and can only grow */
534
2/2
✓ Branch 0 taken 59558400 times.
✓ Branch 1 taken 232650 times.
59791050 for (j = 0; j < 256; j++)
535 59558400 denom = add_cb(c, denom, power[j] + c->auf[samplerate_index][i][j]);
536
2/2
✓ Branch 0 taken 59558400 times.
✓ Branch 1 taken 232650 times.
59791050 for (j = 0; j < 256; j++)
537 59558400 out_cb_unnorm[j] = add_cb(c, out_cb_unnorm[j],
538 59558400 -denom + c->auf[samplerate_index][i][j]);
539 }
540
541
2/2
✓ Branch 0 taken 2382336 times.
✓ Branch 1 taken 9306 times.
2391642 for (j = 0; j < 256; j++)
542 2382336 out_cb[j] = add_cb(c, out_cb[j], -out_cb_unnorm[j] - ca_cb - cs_cb);
543 9306 }
544
545 typedef 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
549 49632 static void walk_band_low(DCAEncContext *c, int band, int channel,
550 walk_band_t walk, int32_t *arg)
551 {
552 int f;
553
554
2/2
✓ Branch 0 taken 1551 times.
✓ Branch 1 taken 48081 times.
49632 if (band == 0) {
555
2/2
✓ Branch 0 taken 6204 times.
✓ Branch 1 taken 1551 times.
7755 for (f = 0; f < 4; f++)
556 6204 walk(c, 0, 0, f, 0, -2047, channel, arg);
557 } else {
558
2/2
✓ Branch 0 taken 384648 times.
✓ Branch 1 taken 48081 times.
432729 for (f = 0; f < 8; f++)
559 384648 walk(c, band, band - 1, 8 * band - 4 + f,
560 384648 c->band_spectrum[7 - f], c->band_spectrum[f], channel, arg);
561 }
562 49632 }
563
564 49632 static void walk_band_high(DCAEncContext *c, int band, int channel,
565 walk_band_t walk, int32_t *arg)
566 {
567 int f;
568
569
2/2
✓ Branch 0 taken 1551 times.
✓ Branch 1 taken 48081 times.
49632 if (band == 31) {
570
2/2
✓ Branch 0 taken 6204 times.
✓ Branch 1 taken 1551 times.
7755 for (f = 0; f < 4; f++)
571 6204 walk(c, 31, 31, 256 - 4 + f, 0, -2047, channel, arg);
572 } else {
573
2/2
✓ Branch 0 taken 384648 times.
✓ Branch 1 taken 48081 times.
432729 for (f = 0; f < 8; f++)
574 384648 walk(c, band, band + 1, 8 * band + 4 + f,
575 384648 c->band_spectrum[f], c->band_spectrum[7 - f], channel, arg);
576 }
577 49632 }
578
579 781704 static 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 781704 int32_t value = c->eff_masking_curve_cb[f] - spectrum1;
584
585
2/2
✓ Branch 0 taken 319187 times.
✓ Branch 1 taken 462517 times.
781704 if (value < c->band_masking_cb[band1])
586 319187 c->band_masking_cb[band1] = value;
587 781704 }
588
589 1551 static void calc_masking(DCAEncContext *c, const int32_t *input)
590 {
591 int i, k, band, ch, ssf;
592 int32_t data[512];
593
594
2/2
✓ Branch 0 taken 397056 times.
✓ Branch 1 taken 1551 times.
398607 for (i = 0; i < 256; i++)
595
2/2
✓ Branch 0 taken 794112 times.
✓ Branch 1 taken 397056 times.
1191168 for (ssf = 0; ssf < SUBSUBFRAMES; ssf++)
596 794112 c->masking_curve_cb[ssf][i] = -2047;
597
598
2/2
✓ Branch 0 taken 3102 times.
✓ Branch 1 taken 1551 times.
4653 for (ssf = 0; ssf < SUBSUBFRAMES; ssf++)
599
2/2
✓ Branch 0 taken 9306 times.
✓ Branch 1 taken 3102 times.
12408 for (ch = 0; ch < c->fullband_channels; ch++) {
600 9306 const int chi = c->channel_order_tab[ch];
601
602
2/2
✓ Branch 0 taken 2382336 times.
✓ Branch 1 taken 9306 times.
2391642 for (i = 0, k = 128 + 256 * ssf; k < 512; i++, k++)
603 2382336 data[i] = c->history[ch][k];
604
2/2
✓ Branch 0 taken 2382336 times.
✓ Branch 1 taken 9306 times.
2391642 for (k -= 512; i < 512; i++, k++)
605 2382336 data[i] = input[k * c->channels + chi];
606 9306 adjust_jnd(c, data, c->masking_curve_cb[ssf]);
607 }
608
2/2
✓ Branch 0 taken 397056 times.
✓ Branch 1 taken 1551 times.
398607 for (i = 0; i < 256; i++) {
609 397056 int32_t m = 2048;
610
611
2/2
✓ Branch 0 taken 794112 times.
✓ Branch 1 taken 397056 times.
1191168 for (ssf = 0; ssf < SUBSUBFRAMES; ssf++)
612
2/2
✓ Branch 0 taken 579537 times.
✓ Branch 1 taken 214575 times.
794112 if (c->masking_curve_cb[ssf][i] < m)
613 579537 m = c->masking_curve_cb[ssf][i];
614 397056 c->eff_masking_curve_cb[i] = m;
615 }
616
617
2/2
✓ Branch 0 taken 49632 times.
✓ Branch 1 taken 1551 times.
51183 for (band = 0; band < 32; band++) {
618 49632 c->band_masking_cb[band] = 2048;
619 49632 walk_band_low(c, band, 0, update_band_masking, NULL);
620 49632 walk_band_high(c, band, 0, update_band_masking, NULL);
621 }
622 1551 }
623
624 149413 static inline int32_t find_peak(DCAEncContext *c, const int32_t *in, int len)
625 {
626 int sample;
627 149413 int32_t m = 0;
628
2/2
✓ Branch 0 taken 2386472 times.
✓ Branch 1 taken 149413 times.
2535885 for (sample = 0; sample < len; sample++) {
629 2386472 int32_t s = abs(in[sample]);
630
2/2
✓ Branch 0 taken 516187 times.
✓ Branch 1 taken 1870285 times.
2386472 if (m < s)
631 516187 m = s;
632 }
633 149413 return get_cb(c, m);
634 }
635
636 1551 static void find_peaks(DCAEncContext *c)
637 {
638 int band, ch;
639
640
2/2
✓ Branch 0 taken 4653 times.
✓ Branch 1 taken 1551 times.
6204 for (ch = 0; ch < c->fullband_channels; ch++) {
641
2/2
✓ Branch 0 taken 148896 times.
✓ Branch 1 taken 4653 times.
153549 for (band = 0; band < 32; band++)
642 148896 c->peak_cb[ch][band] = find_peak(c, c->subband[ch][band],
643 SUBBAND_SAMPLES);
644 }
645
646
2/2
✓ Branch 0 taken 517 times.
✓ Branch 1 taken 1034 times.
1551 if (c->lfe_channel)
647 517 c->lfe_peak_cb = find_peak(c, c->downsampled_lfe, DCA_LFE_SAMPLES);
648 1551 }
649
650 ✗ static void adpcm_analysis(DCAEncContext *c)
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
674 static const int snr_fudge = 128;
675 #define USED_1ABITS 1
676 #define USED_26ABITS 4
677
678 165440 static inline int32_t get_step_size(DCAEncContext *c, int ch, int band)
679 {
680 int32_t step_size;
681
682
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 165440 times.
165440 if (c->bitrate_index == 3)
683 ✗ step_size = ff_dca_lossless_quant[c->abits[ch][band]];
684 else
685 165440 step_size = ff_dca_lossy_quant[c->abits[ch][band]];
686
687 165440 return step_size;
688 }
689
690 1500709 static int calc_one_scale(DCAEncContext *c, int32_t peak_cb, int abits,
691 softfloat *quant)
692 {
693 int32_t peak;
694 int our_nscale, try_remove;
695 softfloat our_quant;
696
697
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1500709 times.
1500709 av_assert0(peak_cb <= 0);
698
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1500709 times.
1500709 av_assert0(peak_cb >= -2047);
699
700 1500709 our_nscale = 127;
701 1500709 peak = c->cb_to_level[-peak_cb];
702
703
2/2
✓ Branch 0 taken 10504963 times.
✓ Branch 1 taken 1500709 times.
12005672 for (try_remove = 64; try_remove > 0; try_remove >>= 1) {
704
2/2
✓ Branch 0 taken 1591995 times.
✓ Branch 1 taken 8912968 times.
10504963 if (scalefactor_inv[our_nscale - try_remove].e + stepsize_inv[abits].e <= 17)
705 1591995 continue;
706 8912968 our_quant.m = mul32(scalefactor_inv[our_nscale - try_remove].m, stepsize_inv[abits].m);
707 8912968 our_quant.e = scalefactor_inv[our_nscale - try_remove].e + stepsize_inv[abits].e - 17;
708
2/2
✓ Branch 1 taken 2930283 times.
✓ Branch 2 taken 5982685 times.
8912968 if ((ff_dca_quant_levels[abits] - 1) / 2 < quantize_value(peak, our_quant))
709 2930283 continue;
710 5982685 our_nscale -= try_remove;
711 }
712
713
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1500709 times.
1500709 if (our_nscale >= 125)
714 ✗ our_nscale = 124;
715
716 1500709 quant->m = mul32(scalefactor_inv[our_nscale].m, stepsize_inv[abits].m);
717 1500709 quant->e = scalefactor_inv[our_nscale].e + stepsize_inv[abits].e - 17;
718
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1500709 times.
1500709 av_assert0((ff_dca_quant_levels[abits] - 1) / 2 >= quantize_value(peak, *quant));
719
720 1500709 return our_nscale;
721 }
722
723 ✗ static 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
740 15627 static void quantize_adpcm(DCAEncContext *c)
741 {
742 int band, ch;
743
744
2/2
✓ Branch 0 taken 46881 times.
✓ Branch 1 taken 15627 times.
62508 for (ch = 0; ch < c->fullband_channels; ch++)
745
2/2
✓ Branch 0 taken 1500192 times.
✓ Branch 1 taken 46881 times.
1547073 for (band = 0; band < 32; band++)
746
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1500192 times.
1500192 if (c->prediction_mode[ch][band] >= 0)
747 ✗ quantize_adpcm_subband(c, ch, band);
748 15627 }
749
750 15627 static void quantize_pcm(DCAEncContext *c)
751 {
752 int sample, band, ch;
753
754
2/2
✓ Branch 0 taken 46881 times.
✓ Branch 1 taken 15627 times.
62508 for (ch = 0; ch < c->fullband_channels; ch++) {
755
2/2
✓ Branch 0 taken 1500192 times.
✓ Branch 1 taken 46881 times.
1547073 for (band = 0; band < 32; band++) {
756
1/2
✓ Branch 0 taken 1500192 times.
✗ Branch 1 not taken.
1500192 if (c->prediction_mode[ch][band] == -1) {
757
2/2
✓ Branch 0 taken 24003072 times.
✓ Branch 1 taken 1500192 times.
25503264 for (sample = 0; sample < SUBBAND_SAMPLES; sample++) {
758 24003072 int32_t val = quantize_value(c->subband[ch][band][sample],
759 c->quant[ch][band]);
760 24003072 c->quantized[ch][band][sample] = val;
761 }
762 }
763 }
764 }
765 15627 }
766
767 768531 static void accumulate_huff_bit_consumption(int abits, int32_t *quantized,
768 uint32_t *result)
769 {
770 768531 uint8_t sel, id = abits - 1;
771
2/2
✓ Branch 0 taken 3237631 times.
✓ Branch 1 taken 768531 times.
4006162 for (sel = 0; sel < ff_dca_quant_index_group_size[id]; sel++)
772 3237631 result[sel] += dca_vlc_calc_quant_bits(quantized, SUBBAND_SAMPLES,
773 sel, id);
774 768531 }
775
776 46881 static uint32_t set_best_code(uint32_t vlc_bits[DCA_CODE_BOOKS][7],
777 uint32_t clc_bits[DCA_CODE_BOOKS],
778 int32_t res[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 46881 uint32_t t, bits = 0;
784
785
2/2
✓ Branch 0 taken 468810 times.
✓ Branch 1 taken 46881 times.
515691 for (i = 0; i < DCA_CODE_BOOKS; i++) {
786
787
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 468810 times.
468810 av_assert0(!((!!vlc_bits[i][0]) ^ (!!clc_bits[i])));
788
2/2
✓ Branch 0 taken 197011 times.
✓ Branch 1 taken 271799 times.
468810 if (vlc_bits[i][0] == 0) {
789 /* do not transmit adjustment index for empty codebooks */
790 197011 res[i] = ff_dca_quant_index_group_size[i];
791 /* and skip it */
792 197011 continue;
793 }
794
795 271799 best_sel_bits[i] = vlc_bits[i][0];
796 271799 best_sel_id[i] = 0;
797
2/2
✓ Branch 0 taken 1299573 times.
✓ Branch 1 taken 271799 times.
1571372 for (sel = 0; sel < ff_dca_quant_index_group_size[i]; sel++) {
798
3/4
✓ Branch 0 taken 366734 times.
✓ Branch 1 taken 932839 times.
✓ Branch 2 taken 366734 times.
✗ Branch 3 not taken.
1299573 if (best_sel_bits[i] > vlc_bits[i][sel] && vlc_bits[i][sel]) {
799 366734 best_sel_bits[i] = vlc_bits[i][sel];
800 366734 best_sel_id[i] = sel;
801 }
802 }
803
804 /* 2 bits to transmit scale factor adjustment index */
805 271799 t = best_sel_bits[i] + 2;
806
2/2
✓ Branch 0 taken 194284 times.
✓ Branch 1 taken 77515 times.
271799 if (t < clc_bits[i]) {
807 194284 res[i] = best_sel_id[i];
808 194284 bits += t;
809 } else {
810 77515 res[i] = ff_dca_quant_index_group_size[i];
811 77515 bits += clc_bits[i];
812 }
813 }
814 46881 return bits;
815 }
816
817 46881 static 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 46881 int32_t best_sel = 6;
823 46881 int32_t best_bits = bands * 5;
824
825 /* Check do we have subband which cannot be encoded by Huffman tables */
826
2/2
✓ Branch 0 taken 95748 times.
✓ Branch 1 taken 1562 times.
97310 for (i = 0; i < bands; i++) {
827
3/4
✓ Branch 0 taken 50429 times.
✓ Branch 1 taken 45319 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 50429 times.
95748 if (abits[i] > 12 || abits[i] == 0) {
828 45319 *res = best_sel;
829 45319 return best_bits;
830 }
831 }
832
833
2/2
✓ Branch 0 taken 7810 times.
✓ Branch 1 taken 1562 times.
9372 for (i = 0; i < DCA_BITALLOC_12_COUNT; i++) {
834 7810 t = dca_vlc_calc_alloc_bits(abits, bands, i);
835
2/2
✓ Branch 0 taken 1639 times.
✓ Branch 1 taken 6171 times.
7810 if (t < best_bits) {
836 1639 best_bits = t;
837 1639 best_sel = i;
838 }
839 }
840
841 1562 *res = best_sel;
842 1562 return best_bits;
843 }
844
845 15627 static int init_quantization_noise(DCAEncContext *c, int noise, int forbid_zero)
846 {
847 15627 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 15627 uint32_t bits_counter = 0;
851
852 15627 c->consumed_bits = 132 + 333 * c->fullband_channels;
853 15627 c->consumed_bits += c->consumed_adpcm_bits;
854
2/2
✓ Branch 0 taken 5209 times.
✓ Branch 1 taken 10418 times.
15627 if (c->lfe_channel)
855 5209 c->consumed_bits += 72;
856
857 /* attempt to guess the bit distribution based on the previous frame */
858
2/2
✓ Branch 0 taken 46881 times.
✓ Branch 1 taken 15627 times.
62508 for (ch = 0; ch < c->fullband_channels; ch++) {
859
2/2
✓ Branch 0 taken 1500192 times.
✓ Branch 1 taken 46881 times.
1547073 for (band = 0; band < 32; band++) {
860 1500192 int snr_cb = c->peak_cb[ch][band] - c->band_masking_cb[band] - noise;
861
862
2/2
✓ Branch 0 taken 60194 times.
✓ Branch 1 taken 1439998 times.
1500192 if (snr_cb >= 1312) {
863 60194 c->abits[ch][band] = 26;
864 60194 ret &= ~USED_1ABITS;
865
2/2
✓ Branch 0 taken 935917 times.
✓ Branch 1 taken 504081 times.
1439998 } else if (snr_cb >= 222) {
866 935917 c->abits[ch][band] = 8 + mul32(snr_cb - 222, 69000000);
867 935917 ret &= ~(USED_26ABITS | USED_1ABITS);
868
2/2
✓ Branch 0 taken 217436 times.
✓ Branch 1 taken 286645 times.
504081 } else if (snr_cb >= 0) {
869 217436 c->abits[ch][band] = 2 + mul32(snr_cb, 106000000);
870 217436 ret &= ~(USED_26ABITS | USED_1ABITS);
871
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 286645 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
286645 } else if (forbid_zero || snr_cb >= -140) {
872 286645 c->abits[ch][band] = 1;
873 286645 ret &= ~USED_26ABITS;
874 } else {
875 ✗ c->abits[ch][band] = 0;
876 ✗ ret &= ~(USED_26ABITS | USED_1ABITS);
877 }
878 }
879 46881 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
2/2
✓ Branch 0 taken 46881 times.
✓ Branch 1 taken 15627 times.
62508 for (ch = 0; ch < c->fullband_channels; ch++) {
887
2/2
✓ Branch 0 taken 1500192 times.
✓ Branch 1 taken 46881 times.
1547073 for (band = 0; band < 32; band++) {
888
1/2
✓ Branch 0 taken 1500192 times.
✗ Branch 1 not taken.
1500192 if (c->prediction_mode[ch][band] == -1) {
889 1500192 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 }
895 15627 quantize_adpcm(c);
896 15627 quantize_pcm(c);
897
898 15627 memset(huff_bit_count_accum, 0, MAX_CHANNELS * DCA_CODE_BOOKS * 7 * sizeof(uint32_t));
899 15627 memset(clc_bit_count_accum, 0, MAX_CHANNELS * DCA_CODE_BOOKS * sizeof(uint32_t));
900
2/2
✓ Branch 0 taken 46881 times.
✓ Branch 1 taken 15627 times.
62508 for (ch = 0; ch < c->fullband_channels; ch++) {
901
2/2
✓ Branch 0 taken 1500192 times.
✓ Branch 1 taken 46881 times.
1547073 for (band = 0; band < 32; band++) {
902
3/4
✓ Branch 0 taken 1500192 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 768531 times.
✓ Branch 3 taken 731661 times.
1500192 if (c->abits[ch][band] && c->abits[ch][band] <= DCA_CODE_BOOKS) {
903 768531 accumulate_huff_bit_consumption(c->abits[ch][band],
904 768531 c->quantized[ch][band],
905 768531 huff_bit_count_accum[ch][c->abits[ch][band] - 1]);
906 768531 clc_bit_count_accum[ch][c->abits[ch][band] - 1] += bit_consumption[c->abits[ch][band]];
907 } else {
908 731661 bits_counter += bit_consumption[c->abits[ch][band]];
909 }
910 }
911 }
912
913
2/2
✓ Branch 0 taken 46881 times.
✓ Branch 1 taken 15627 times.
62508 for (ch = 0; ch < c->fullband_channels; ch++) {
914 46881 bits_counter += set_best_code(huff_bit_count_accum[ch],
915 46881 clc_bit_count_accum[ch],
916 46881 c->quant_index_sel[ch]);
917 }
918
919 15627 c->consumed_bits += bits_counter;
920
921 15627 return ret;
922 }
923
924 1551 static void assign_bits(DCAEncContext *c)
925 {
926 /* Find the bounds where the binary search should work */
927 int low, high, down;
928 1551 int used_abits = 0;
929 1551 int forbid_zero = 1;
930 1551 restart:
931 1551 init_quantization_noise(c, c->worst_quantization_noise, forbid_zero);
932 1551 low = high = c->worst_quantization_noise;
933
2/2
✓ Branch 0 taken 767 times.
✓ Branch 1 taken 784 times.
1551 if (c->consumed_bits > c->frame_bits) {
934
2/2
✓ Branch 0 taken 833 times.
✓ Branch 1 taken 767 times.
1600 while (c->consumed_bits > c->frame_bits) {
935
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 833 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
833 if (used_abits == USED_1ABITS && forbid_zero) {
936 ✗ forbid_zero = 0;
937 ✗ goto restart;
938 }
939 833 low = high;
940 833 high += snr_fudge;
941 833 used_abits = init_quantization_noise(c, high, forbid_zero);
942 }
943 } else {
944
2/2
✓ Branch 0 taken 835 times.
✓ Branch 1 taken 784 times.
1619 while (c->consumed_bits <= c->frame_bits) {
945 835 high = low;
946
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 835 times.
835 if (used_abits == USED_26ABITS)
947 ✗ goto out; /* The requested bitrate is too high, pad with zeros */
948 835 low -= snr_fudge;
949 835 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
2/2
✓ Branch 0 taken 10857 times.
✓ Branch 1 taken 1551 times.
12408 for (down = snr_fudge >> 1; down; down >>= 1) {
955 10857 init_quantization_noise(c, high - down, forbid_zero);
956
2/2
✓ Branch 0 taken 5425 times.
✓ Branch 1 taken 5432 times.
10857 if (c->consumed_bits <= c->frame_bits)
957 5425 high -= down;
958 }
959 1551 init_quantization_noise(c, high, forbid_zero);
960 1551 out:
961 1551 c->worst_quantization_noise = high;
962
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1548 times.
1551 if (high > c->worst_noise_ever)
963 3 c->worst_noise_ever = high;
964 1551 }
965
966 1551 static void shift_history(DCAEncContext *c, const int32_t *input)
967 {
968 int k, ch;
969
970
2/2
✓ Branch 0 taken 794112 times.
✓ Branch 1 taken 1551 times.
795663 for (k = 0; k < 512; k++)
971
2/2
✓ Branch 0 taken 2647040 times.
✓ Branch 1 taken 794112 times.
3441152 for (ch = 0; ch < c->channels; ch++) {
972 7676416 const int chi = ch < c->fullband_channels ? c->channel_order_tab[ch]
973
2/2
✓ Branch 0 taken 2382336 times.
✓ Branch 1 taken 264704 times.
2647040 : lfe_index[c->channel_config];
974
975 2647040 c->history[ch][k] = input[k * c->channels + chi];
976 }
977 1551 }
978
979 1551 static void fill_in_adpcm_bufer(DCAEncContext *c)
980 {
981 int ch, band;
982 int32_t step_size;
983 /* We fill in ADPCM work buffer for subbands which hasn't been ADPCM coded
984 * in current frame - we need this data if subband of next frame is
985 * ADPCM
986 */
987
2/2
✓ Branch 0 taken 5170 times.
✓ Branch 1 taken 1551 times.
6721 for (ch = 0; ch < c->channels; ch++) {
988
2/2
✓ Branch 0 taken 165440 times.
✓ Branch 1 taken 5170 times.
170610 for (band = 0; band < 32; band++) {
989 165440 int32_t *samples = c->subband[ch][band] - DCA_ADPCM_COEFFS;
990
1/2
✓ Branch 0 taken 165440 times.
✗ Branch 1 not taken.
165440 if (c->prediction_mode[ch][band] == -1) {
991 165440 step_size = get_step_size(c, ch, band);
992
993 165440 ff_dca_core_dequantize(c->adpcm_history[ch][band],
994 165440 c->quantized[ch][band]+12, step_size,
995 165440 ff_dca_scale_factor_quant7[c->scale_factor[ch][band]], 0, 4);
996 } else {
997 ✗ AV_COPY128U(c->adpcm_history[ch][band], c->adpcm_history[ch][band]+4);
998 }
999 /* Copy dequantized values for LPC analysis.
1000 * It reduces artifacts in case of extreme quantization,
1001 * example: in current frame abits is 1 and has no prediction flag,
1002 * but end of this frame is sine like signal. In this case, if LPC analysis uses
1003 * original values, likely LPC analysis returns good prediction gain, and sets prediction flag.
1004 * But there are no proper value in decoder history, so likely result will be no good.
1005 * Bitstream has "Predictor history flag switch", but this flag disables history for all subbands
1006 */
1007 165440 samples[0] = c->adpcm_history[ch][band][0] * (1 << 7);
1008 165440 samples[1] = c->adpcm_history[ch][band][1] * (1 << 7);
1009 165440 samples[2] = c->adpcm_history[ch][band][2] * (1 << 7);
1010 165440 samples[3] = c->adpcm_history[ch][band][3] * (1 << 7);
1011 }
1012 }
1013 1551 }
1014
1015 1551 static void calc_lfe_scales(DCAEncContext *c)
1016 {
1017
2/2
✓ Branch 0 taken 517 times.
✓ Branch 1 taken 1034 times.
1551 if (c->lfe_channel)
1018 517 c->lfe_scale_factor = calc_one_scale(c, c->lfe_peak_cb, 11, &c->lfe_quant);
1019 1551 }
1020
1021 1551 static void put_frame_header(DCAEncContext *c)
1022 {
1023 /* SYNC */
1024 1551 put_bits(&c->pb, 16, 0x7ffe);
1025 1551 put_bits(&c->pb, 16, 0x8001);
1026
1027 /* Frame type: normal */
1028 1551 put_bits(&c->pb, 1, 1);
1029
1030 /* Deficit sample count: none */
1031 1551 put_bits(&c->pb, 5, 31);
1032
1033 /* CRC is not present */
1034 1551 put_bits(&c->pb, 1, 0);
1035
1036 /* Number of PCM sample blocks */
1037 1551 put_bits(&c->pb, 7, SUBBAND_SAMPLES - 1);
1038
1039 /* Primary frame byte size */
1040 1551 put_bits(&c->pb, 14, c->frame_size - 1);
1041
1042 /* Audio channel arrangement */
1043 1551 put_bits(&c->pb, 6, c->channel_config);
1044
1045 /* Core audio sampling frequency */
1046 1551 put_bits(&c->pb, 4, bitstream_sfreq[c->samplerate_index]);
1047
1048 /* Transmission bit rate */
1049 1551 put_bits(&c->pb, 5, c->bitrate_index);
1050
1051 /* Embedded down mix: disabled */
1052 1551 put_bits(&c->pb, 1, 0);
1053
1054 /* Embedded dynamic range flag: not present */
1055 1551 put_bits(&c->pb, 1, 0);
1056
1057 /* Embedded time stamp flag: not present */
1058 1551 put_bits(&c->pb, 1, 0);
1059
1060 /* Auxiliary data flag: not present */
1061 1551 put_bits(&c->pb, 1, 0);
1062
1063 /* HDCD source: no */
1064 1551 put_bits(&c->pb, 1, 0);
1065
1066 /* Extension audio ID: N/A */
1067 1551 put_bits(&c->pb, 3, 0);
1068
1069 /* Extended audio data: not present */
1070 1551 put_bits(&c->pb, 1, 0);
1071
1072 /* Audio sync word insertion flag: after each sub-frame */
1073 1551 put_bits(&c->pb, 1, 0);
1074
1075 /* Low frequency effects flag: not present or 64x subsampling */
1076
2/2
✓ Branch 0 taken 517 times.
✓ Branch 1 taken 1034 times.
1551 put_bits(&c->pb, 2, c->lfe_channel ? 2 : 0);
1077
1078 /* Predictor history switch flag: on */
1079 1551 put_bits(&c->pb, 1, 1);
1080
1081 /* No CRC */
1082 /* Multirate interpolator switch: non-perfect reconstruction */
1083 1551 put_bits(&c->pb, 1, 0);
1084
1085 /* Encoder software revision: 7 */
1086 1551 put_bits(&c->pb, 4, 7);
1087
1088 /* Copy history: 0 */
1089 1551 put_bits(&c->pb, 2, 0);
1090
1091 /* Source PCM resolution: 16 bits, not DTS ES */
1092 1551 put_bits(&c->pb, 3, 0);
1093
1094 /* Front sum/difference coding: no */
1095 1551 put_bits(&c->pb, 1, 0);
1096
1097 /* Surrounds sum/difference coding: no */
1098 1551 put_bits(&c->pb, 1, 0);
1099
1100 /* Dialog normalization: 0 dB */
1101 1551 put_bits(&c->pb, 4, 0);
1102 1551 }
1103
1104 1551 static void put_primary_audio_header(DCAEncContext *c)
1105 {
1106 int ch, i;
1107 /* Number of subframes */
1108 1551 put_bits(&c->pb, 4, SUBFRAMES - 1);
1109
1110 /* Number of primary audio channels */
1111 1551 put_bits(&c->pb, 3, c->fullband_channels - 1);
1112
1113 /* Subband activity count */
1114
2/2
✓ Branch 0 taken 4653 times.
✓ Branch 1 taken 1551 times.
6204 for (ch = 0; ch < c->fullband_channels; ch++)
1115 4653 put_bits(&c->pb, 5, DCAENC_SUBBANDS - 2);
1116
1117 /* High frequency VQ start subband */
1118
2/2
✓ Branch 0 taken 4653 times.
✓ Branch 1 taken 1551 times.
6204 for (ch = 0; ch < c->fullband_channels; ch++)
1119 4653 put_bits(&c->pb, 5, DCAENC_SUBBANDS - 1);
1120
1121 /* Joint intensity coding index: 0, 0 */
1122
2/2
✓ Branch 0 taken 4653 times.
✓ Branch 1 taken 1551 times.
6204 for (ch = 0; ch < c->fullband_channels; ch++)
1123 4653 put_bits(&c->pb, 3, 0);
1124
1125 /* Transient mode codebook: A4, A4 (arbitrary) */
1126
2/2
✓ Branch 0 taken 4653 times.
✓ Branch 1 taken 1551 times.
6204 for (ch = 0; ch < c->fullband_channels; ch++)
1127 4653 put_bits(&c->pb, 2, 0);
1128
1129 /* Scale factor code book: 7 bit linear, 7-bit sqrt table (for each channel) */
1130
2/2
✓ Branch 0 taken 4653 times.
✓ Branch 1 taken 1551 times.
6204 for (ch = 0; ch < c->fullband_channels; ch++)
1131 4653 put_bits(&c->pb, 3, 6);
1132
1133 /* Bit allocation quantizer select: linear 5-bit */
1134
2/2
✓ Branch 0 taken 4653 times.
✓ Branch 1 taken 1551 times.
6204 for (ch = 0; ch < c->fullband_channels; ch++)
1135 4653 put_bits(&c->pb, 3, c->bit_allocation_sel[ch]);
1136
1137 /* Quantization index codebook select */
1138
2/2
✓ Branch 0 taken 15510 times.
✓ Branch 1 taken 1551 times.
17061 for (i = 0; i < DCA_CODE_BOOKS; i++)
1139
2/2
✓ Branch 0 taken 46530 times.
✓ Branch 1 taken 15510 times.
62040 for (ch = 0; ch < c->fullband_channels; ch++)
1140 46530 put_bits(&c->pb, ff_dca_quant_index_sel_nbits[i], c->quant_index_sel[ch][i]);
1141
1142 /* Scale factor adjustment index: transmitted in case of Huffman coding */
1143
2/2
✓ Branch 0 taken 15510 times.
✓ Branch 1 taken 1551 times.
17061 for (i = 0; i < DCA_CODE_BOOKS; i++)
1144
2/2
✓ Branch 0 taken 46530 times.
✓ Branch 1 taken 15510 times.
62040 for (ch = 0; ch < c->fullband_channels; ch++)
1145
2/2
✓ Branch 0 taken 19589 times.
✓ Branch 1 taken 26941 times.
46530 if (c->quant_index_sel[ch][i] < ff_dca_quant_index_group_size[i])
1146 19589 put_bits(&c->pb, 2, 0);
1147
1148 /* Audio header CRC check word: not transmitted */
1149 1551 }
1150
1151 297792 static void put_subframe_samples(DCAEncContext *c, int ss, int band, int ch)
1152 {
1153 int i, j, sum, bits, sel;
1154
2/2
✓ Branch 0 taken 155086 times.
✓ Branch 1 taken 142706 times.
297792 if (c->abits[ch][band] <= DCA_CODE_BOOKS) {
1155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 155086 times.
155086 av_assert0(c->abits[ch][band] > 0);
1156 155086 sel = c->quant_index_sel[ch][c->abits[ch][band] - 1];
1157 // Huffman codes
1158
2/2
✓ Branch 0 taken 123924 times.
✓ Branch 1 taken 31162 times.
155086 if (sel < ff_dca_quant_index_group_size[c->abits[ch][band] - 1]) {
1159 123924 dca_vlc_enc_quant(&c->pb, &c->quantized[ch][band][ss * 8], 8,
1160 123924 sel, c->abits[ch][band] - 1);
1161 123924 return;
1162 }
1163
1164 // Block codes
1165
2/2
✓ Branch 0 taken 11866 times.
✓ Branch 1 taken 19296 times.
31162 if (c->abits[ch][band] <= 7) {
1166
2/2
✓ Branch 0 taken 23732 times.
✓ Branch 1 taken 11866 times.
35598 for (i = 0; i < 8; i += 4) {
1167 23732 sum = 0;
1168
2/2
✓ Branch 0 taken 94928 times.
✓ Branch 1 taken 23732 times.
118660 for (j = 3; j >= 0; j--) {
1169 94928 sum *= ff_dca_quant_levels[c->abits[ch][band]];
1170 94928 sum += c->quantized[ch][band][ss * 8 + i + j];
1171 94928 sum += (ff_dca_quant_levels[c->abits[ch][band]] - 1) / 2;
1172 }
1173 23732 put_bits(&c->pb, bit_consumption[c->abits[ch][band]] / 4, sum);
1174 }
1175 11866 return;
1176 }
1177 }
1178
1179
2/2
✓ Branch 0 taken 1296016 times.
✓ Branch 1 taken 162002 times.
1458018 for (i = 0; i < 8; i++) {
1180 1296016 bits = bit_consumption[c->abits[ch][band]] / 16;
1181 1296016 put_sbits(&c->pb, bits, c->quantized[ch][band][ss * 8 + i]);
1182 }
1183 }
1184
1185 1551 static void put_subframe(DCAEncContext *c, int subframe)
1186 {
1187 int i, band, ss, ch;
1188
1189 /* Subsubframes count */
1190 1551 put_bits(&c->pb, 2, SUBSUBFRAMES -1);
1191
1192 /* Partial subsubframe sample count: dummy */
1193 1551 put_bits(&c->pb, 3, 0);
1194
1195 /* Prediction mode: no ADPCM, in each channel and subband */
1196
2/2
✓ Branch 0 taken 4653 times.
✓ Branch 1 taken 1551 times.
6204 for (ch = 0; ch < c->fullband_channels; ch++)
1197
2/2
✓ Branch 0 taken 148896 times.
✓ Branch 1 taken 4653 times.
153549 for (band = 0; band < DCAENC_SUBBANDS; band++)
1198 148896 put_bits(&c->pb, 1, !(c->prediction_mode[ch][band] == -1));
1199
1200 /* Prediction VQ address */
1201
2/2
✓ Branch 0 taken 4653 times.
✓ Branch 1 taken 1551 times.
6204 for (ch = 0; ch < c->fullband_channels; ch++)
1202
2/2
✓ Branch 0 taken 148896 times.
✓ Branch 1 taken 4653 times.
153549 for (band = 0; band < DCAENC_SUBBANDS; band++)
1203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 148896 times.
148896 if (c->prediction_mode[ch][band] >= 0)
1204 ✗ put_bits(&c->pb, 12, c->prediction_mode[ch][band]);
1205
1206 /* Bit allocation index */
1207
2/2
✓ Branch 0 taken 4653 times.
✓ Branch 1 taken 1551 times.
6204 for (ch = 0; ch < c->fullband_channels; ch++) {
1208
2/2
✓ Branch 0 taken 4473 times.
✓ Branch 1 taken 180 times.
4653 if (c->bit_allocation_sel[ch] == 6) {
1209
2/2
✓ Branch 0 taken 143136 times.
✓ Branch 1 taken 4473 times.
147609 for (band = 0; band < DCAENC_SUBBANDS; band++) {
1210 143136 put_bits(&c->pb, 5, c->abits[ch][band]);
1211 }
1212 } else {
1213 180 dca_vlc_enc_alloc(&c->pb, c->abits[ch], DCAENC_SUBBANDS,
1214 180 c->bit_allocation_sel[ch]);
1215 }
1216 }
1217
1218 if (SUBSUBFRAMES > 1) {
1219 /* Transition mode: none for each channel and subband */
1220
2/2
✓ Branch 0 taken 4653 times.
✓ Branch 1 taken 1551 times.
6204 for (ch = 0; ch < c->fullband_channels; ch++)
1221
2/2
✓ Branch 0 taken 148896 times.
✓ Branch 1 taken 4653 times.
153549 for (band = 0; band < DCAENC_SUBBANDS; band++)
1222
1/2
✓ Branch 0 taken 148896 times.
✗ Branch 1 not taken.
148896 if (c->abits[ch][band])
1223 148896 put_bits(&c->pb, 1, 0); /* codebook A4 */
1224 }
1225
1226 /* Scale factors */
1227
2/2
✓ Branch 0 taken 4653 times.
✓ Branch 1 taken 1551 times.
6204 for (ch = 0; ch < c->fullband_channels; ch++)
1228
2/2
✓ Branch 0 taken 148896 times.
✓ Branch 1 taken 4653 times.
153549 for (band = 0; band < DCAENC_SUBBANDS; band++)
1229
1/2
✓ Branch 0 taken 148896 times.
✗ Branch 1 not taken.
148896 if (c->abits[ch][band])
1230 148896 put_bits(&c->pb, 7, c->scale_factor[ch][band]);
1231
1232 /* Joint subband scale factor codebook select: not transmitted */
1233 /* Scale factors for joint subband coding: not transmitted */
1234 /* Stereo down-mix coefficients: not transmitted */
1235 /* Dynamic range coefficient: not transmitted */
1236 /* Stde information CRC check word: not transmitted */
1237 /* VQ encoded high frequency subbands: not transmitted */
1238
1239 /* LFE data: 8 samples and scalefactor */
1240
2/2
✓ Branch 0 taken 517 times.
✓ Branch 1 taken 1034 times.
1551 if (c->lfe_channel) {
1241
2/2
✓ Branch 0 taken 4136 times.
✓ Branch 1 taken 517 times.
4653 for (i = 0; i < DCA_LFE_SAMPLES; i++)
1242 4136 put_bits(&c->pb, 8, quantize_value(c->downsampled_lfe[i], c->lfe_quant) & 0xff);
1243 517 put_bits(&c->pb, 8, c->lfe_scale_factor);
1244 }
1245
1246 /* Audio data (subsubframes) */
1247
2/2
✓ Branch 0 taken 3102 times.
✓ Branch 1 taken 1551 times.
4653 for (ss = 0; ss < SUBSUBFRAMES ; ss++)
1248
2/2
✓ Branch 0 taken 9306 times.
✓ Branch 1 taken 3102 times.
12408 for (ch = 0; ch < c->fullband_channels; ch++)
1249
2/2
✓ Branch 0 taken 297792 times.
✓ Branch 1 taken 9306 times.
307098 for (band = 0; band < DCAENC_SUBBANDS; band++)
1250
1/2
✓ Branch 0 taken 297792 times.
✗ Branch 1 not taken.
297792 if (c->abits[ch][band])
1251 297792 put_subframe_samples(c, ss, band, ch);
1252
1253 /* DSYNC */
1254 1551 put_bits(&c->pb, 16, 0xffff);
1255 1551 }
1256
1257 1551 static int encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
1258 const AVFrame *frame, int *got_packet_ptr)
1259 {
1260 1551 DCAEncContext *c = avctx->priv_data;
1261 const int32_t *samples;
1262 int ret, i;
1263
1264
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1551 times.
1551 if ((ret = ff_get_encode_buffer(avctx, avpkt, c->frame_size, 0)) < 0)
1265 ✗ return ret;
1266
1267 1551 samples = (const int32_t *)frame->data[0];
1268
1269 1551 subband_transform(c, samples);
1270
2/2
✓ Branch 0 taken 517 times.
✓ Branch 1 taken 1034 times.
1551 if (c->lfe_channel)
1271 517 lfe_downsample(c, samples);
1272
1273 1551 calc_masking(c, samples);
1274
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1551 times.
1551 if (c->options.adpcm_mode)
1275 ✗ adpcm_analysis(c);
1276 1551 find_peaks(c);
1277 1551 assign_bits(c);
1278 1551 calc_lfe_scales(c);
1279 1551 shift_history(c, samples);
1280
1281 1551 init_put_bits(&c->pb, avpkt->data, avpkt->size);
1282 1551 fill_in_adpcm_bufer(c);
1283 1551 put_frame_header(c);
1284 1551 put_primary_audio_header(c);
1285
2/2
✓ Branch 0 taken 1551 times.
✓ Branch 1 taken 1551 times.
3102 for (i = 0; i < SUBFRAMES; i++)
1286 1551 put_subframe(c, i);
1287
1288 1551 flush_put_bits(&c->pb);
1289 1551 memset(put_bits_ptr(&c->pb), 0, put_bytes_left(&c->pb, 0));
1290
1291 1551 *got_packet_ptr = 1;
1292 1551 return 0;
1293 }
1294
1295 #define DCAENC_FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
1296
1297 static const AVOption options[] = {
1298 { "dca_adpcm", "Use ADPCM encoding", offsetof(DCAEncContext, options.adpcm_mode), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DCAENC_FLAGS },
1299 { NULL },
1300 };
1301
1302 static const AVClass dcaenc_class = {
1303 .class_name = "DCA (DTS Coherent Acoustics)",
1304 .item_name = av_default_item_name,
1305 .option = options,
1306 .version = LIBAVUTIL_VERSION_INT,
1307 };
1308
1309 static const FFCodecDefault defaults[] = {
1310 { "b", "1411200" },
1311 { NULL },
1312 };
1313
1314 const FFCodec ff_dca_encoder = {
1315 .p.name = "dca",
1316 CODEC_LONG_NAME("DCA (DTS Coherent Acoustics)"),
1317 .p.type = AVMEDIA_TYPE_AUDIO,
1318 .p.id = AV_CODEC_ID_DTS,
1319 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_EXPERIMENTAL |
1320 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
1321 .priv_data_size = sizeof(DCAEncContext),
1322 .init = encode_init,
1323 .close = encode_close,
1324 FF_CODEC_ENCODE_CB(encode_frame),
1325 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1326 CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_S32),
1327 CODEC_SAMPLERATES_ARRAY(sample_rates),
1328 CODEC_CH_LAYOUTS(AV_CHANNEL_LAYOUT_MONO, AV_CHANNEL_LAYOUT_STEREO,
1329 AV_CHANNEL_LAYOUT_2_2, AV_CHANNEL_LAYOUT_5POINT0,
1330 AV_CHANNEL_LAYOUT_5POINT1),
1331 .defaults = defaults,
1332 .p.priv_class = &dcaenc_class,
1333 };
1334