FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/ac3enc_template.c
Date: 2022-07-04 19:11:22
Exec Total Coverage
Lines: 176 197 89.3%
Branches: 115 142 81.0%

Line Branch Exec Source
1 /*
2 * AC-3 encoder float/fixed template
3 * Copyright (c) 2000 Fabrice Bellard
4 * Copyright (c) 2006-2011 Justin Ruggles <justin.ruggles@gmail.com>
5 * Copyright (c) 2006-2010 Prakash Punnoor <prakash@punnoor.de>
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 /**
25 * @file
26 * AC-3 encoder float/fixed template
27 */
28
29 #include "config_components.h"
30
31 #include <stdint.h>
32
33 #include "libavutil/attributes.h"
34 #include "libavutil/internal.h"
35 #include "libavutil/mem_internal.h"
36
37 #include "audiodsp.h"
38 #include "ac3enc.h"
39 #include "eac3enc.h"
40
41
42 10 static int allocate_sample_buffers(AC3EncodeContext *s)
43 {
44 int ch;
45
46
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 if (!FF_ALLOC_TYPED_ARRAY(s->windowed_samples, AC3_WINDOW_SIZE) ||
47
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 !FF_ALLOCZ_TYPED_ARRAY(s->planar_samples, s->channels))
48 return AVERROR(ENOMEM);
49
50
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 10 times.
23 for (ch = 0; ch < s->channels; ch++) {
51
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
13 if (!(s->planar_samples[ch] = av_mallocz((AC3_FRAME_SIZE + AC3_BLOCK_SIZE) *
52 sizeof(**s->planar_samples))))
53 return AVERROR(ENOMEM);
54 }
55 10 return 0;
56 }
57
58
59 /*
60 * Copy input samples.
61 * Channels are reordered from FFmpeg's default order to AC-3 order.
62 */
63 1200 static void copy_input_samples(AC3EncodeContext *s, SampleType **samples)
64 {
65 int ch;
66
67 /* copy and remap input samples */
68
2/2
✓ Branch 0 taken 1919 times.
✓ Branch 1 taken 1200 times.
3119 for (ch = 0; ch < s->channels; ch++) {
69 /* copy last 256 samples of previous frame to the start of the current frame */
70 1919 memcpy(&s->planar_samples[ch][0], &s->planar_samples[ch][AC3_BLOCK_SIZE * s->num_blocks],
71 AC3_BLOCK_SIZE * sizeof(s->planar_samples[0][0]));
72
73 /* copy new samples for current frame */
74 1919 memcpy(&s->planar_samples[ch][AC3_BLOCK_SIZE],
75 1919 samples[s->channel_map[ch]],
76 1919 AC3_BLOCK_SIZE * s->num_blocks * sizeof(s->planar_samples[0][0]));
77 }
78 1200 }
79
80
81 /*
82 * Apply the MDCT to input samples to generate frequency coefficients.
83 * This applies the KBD window and normalizes the input to reduce precision
84 * loss due to fixed-point calculations.
85 */
86 1200 static void apply_mdct(AC3EncodeContext *s)
87 {
88 int blk, ch;
89
90
2/2
✓ Branch 0 taken 1919 times.
✓ Branch 1 taken 1200 times.
3119 for (ch = 0; ch < s->channels; ch++) {
91
2/2
✓ Branch 0 taken 11514 times.
✓ Branch 1 taken 1919 times.
13433 for (blk = 0; blk < s->num_blocks; blk++) {
92 11514 AC3Block *block = &s->blocks[blk];
93 11514 const SampleType *input_samples = &s->planar_samples[ch][blk * AC3_BLOCK_SIZE];
94
95 11514 s->fdsp->vector_fmul(s->windowed_samples, input_samples,
96 11514 s->mdct_window, AC3_BLOCK_SIZE);
97 11514 s->fdsp->vector_fmul_reverse(s->windowed_samples + AC3_BLOCK_SIZE,
98 11514 &input_samples[AC3_BLOCK_SIZE],
99 11514 s->mdct_window, AC3_BLOCK_SIZE);
100
101 11514 s->mdct.mdct_calc(&s->mdct, block->mdct_coef[ch+1],
102 11514 s->windowed_samples);
103 }
104 }
105 1200 }
106
107
108 /*
109 * Calculate coupling channel and coupling coordinates.
110 */
111 719 static void apply_channel_coupling(AC3EncodeContext *s)
112 {
113 719 LOCAL_ALIGNED_16(CoefType, cpl_coords, [AC3_MAX_BLOCKS], [AC3_MAX_CHANNELS][16]);
114 #if AC3ENC_FLOAT
115 546 LOCAL_ALIGNED_16(int32_t, fixed_cpl_coords, [AC3_MAX_BLOCKS], [AC3_MAX_CHANNELS][16]);
116 #else
117 173 int32_t (*fixed_cpl_coords)[AC3_MAX_CHANNELS][16] = cpl_coords;
118 #endif
119 719 int av_uninit(blk), ch, bnd, i, j;
120 719 CoefSumType energy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][16] = {{{0}}};
121 int cpl_start, num_cpl_coefs;
122
123 719 memset(cpl_coords, 0, AC3_MAX_BLOCKS * sizeof(*cpl_coords));
124 #if AC3ENC_FLOAT
125 546 memset(fixed_cpl_coords, 0, AC3_MAX_BLOCKS * sizeof(*cpl_coords));
126 #endif
127
128 /* align start to 16-byte boundary. align length to multiple of 32.
129 note: coupling start bin % 4 will always be 1 */
130 719 cpl_start = s->start_freq[CPL_CH] - 1;
131 719 num_cpl_coefs = FFALIGN(s->num_cpl_subbands * 12 + 1, 32);
132 719 cpl_start = FFMIN(256, cpl_start + num_cpl_coefs) - num_cpl_coefs;
133
134 /* calculate coupling channel from fbw channels */
135
2/2
✓ Branch 0 taken 4314 times.
✓ Branch 1 taken 719 times.
5033 for (blk = 0; blk < s->num_blocks; blk++) {
136 4314 AC3Block *block = &s->blocks[blk];
137 4314 CoefType *cpl_coef = &block->mdct_coef[CPL_CH][cpl_start];
138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4314 times.
4314 if (!block->cpl_in_use)
139 continue;
140 4314 memset(cpl_coef, 0, num_cpl_coefs * sizeof(*cpl_coef));
141
2/2
✓ Branch 0 taken 8628 times.
✓ Branch 1 taken 4314 times.
12942 for (ch = 1; ch <= s->fbw_channels; ch++) {
142 8628 CoefType *ch_coef = &block->mdct_coef[ch][cpl_start];
143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8628 times.
8628 if (!block->channel_in_cpl[ch])
144 continue;
145
2/2
✓ Branch 0 taken 552192 times.
✓ Branch 1 taken 8628 times.
560820 for (i = 0; i < num_cpl_coefs; i++)
146 552192 cpl_coef[i] += ch_coef[i];
147 }
148
149 /* coefficients must be clipped in order to be encoded */
150 4314 clip_coefficients(&s->adsp, cpl_coef, num_cpl_coefs);
151 }
152
153 /* calculate energy in each band in coupling channel and each fbw channel */
154 /* TODO: possibly use SIMD to speed up energy calculation */
155 719 bnd = 0;
156 719 i = s->start_freq[CPL_CH];
157
2/2
✓ Branch 0 taken 2876 times.
✓ Branch 1 taken 719 times.
3595 while (i < s->cpl_end_freq) {
158 2876 int band_size = s->cpl_band_sizes[bnd];
159
2/2
✓ Branch 0 taken 8628 times.
✓ Branch 1 taken 2876 times.
11504 for (ch = CPL_CH; ch <= s->fbw_channels; ch++) {
160
2/2
✓ Branch 0 taken 51768 times.
✓ Branch 1 taken 8628 times.
60396 for (blk = 0; blk < s->num_blocks; blk++) {
161 51768 AC3Block *block = &s->blocks[blk];
162
4/6
✓ Branch 0 taken 51768 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 34512 times.
✓ Branch 3 taken 17256 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 34512 times.
51768 if (!block->cpl_in_use || (ch > CPL_CH && !block->channel_in_cpl[ch]))
163 continue;
164
2/2
✓ Branch 0 taken 776520 times.
✓ Branch 1 taken 51768 times.
828288 for (j = 0; j < band_size; j++) {
165 776520 CoefType v = block->mdct_coef[ch][i+j];
166 776520 MAC_COEF(energy[blk][ch][bnd], v, v);
167 }
168 }
169 }
170 2876 i += band_size;
171 2876 bnd++;
172 }
173
174 /* calculate coupling coordinates for all blocks for all channels */
175
2/2
✓ Branch 0 taken 4314 times.
✓ Branch 1 taken 719 times.
5033 for (blk = 0; blk < s->num_blocks; blk++) {
176 4314 AC3Block *block = &s->blocks[blk];
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4314 times.
4314 if (!block->cpl_in_use)
178 continue;
179
2/2
✓ Branch 0 taken 8628 times.
✓ Branch 1 taken 4314 times.
12942 for (ch = 1; ch <= s->fbw_channels; ch++) {
180
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8628 times.
8628 if (!block->channel_in_cpl[ch])
181 continue;
182
2/2
✓ Branch 0 taken 34512 times.
✓ Branch 1 taken 8628 times.
43140 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
183 34512 cpl_coords[blk][ch][bnd] = calc_cpl_coord(energy[blk][ch][bnd],
184 energy[blk][CPL_CH][bnd]);
185 }
186 }
187 }
188
189 /* determine which blocks to send new coupling coordinates for */
190
2/2
✓ Branch 0 taken 4314 times.
✓ Branch 1 taken 719 times.
5033 for (blk = 0; blk < s->num_blocks; blk++) {
191 4314 AC3Block *block = &s->blocks[blk];
192
2/2
✓ Branch 0 taken 3595 times.
✓ Branch 1 taken 719 times.
4314 AC3Block *block0 = blk ? &s->blocks[blk-1] : NULL;
193
194 4314 memset(block->new_cpl_coords, 0, sizeof(block->new_cpl_coords));
195
196
1/2
✓ Branch 0 taken 4314 times.
✗ Branch 1 not taken.
4314 if (block->cpl_in_use) {
197 /* send new coordinates if this is the first block, if previous
198 * block did not use coupling but this block does, the channels
199 * using coupling has changed from the previous block, or the
200 * coordinate difference from the last block for any channel is
201 * greater than a threshold value. */
202
3/4
✓ Branch 0 taken 3595 times.
✓ Branch 1 taken 719 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3595 times.
4314 if (blk == 0 || !block0->cpl_in_use) {
203
2/2
✓ Branch 0 taken 1438 times.
✓ Branch 1 taken 719 times.
2157 for (ch = 1; ch <= s->fbw_channels; ch++)
204 1438 block->new_cpl_coords[ch] = 1;
205 } else {
206
2/2
✓ Branch 0 taken 7190 times.
✓ Branch 1 taken 3595 times.
10785 for (ch = 1; ch <= s->fbw_channels; ch++) {
207
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7190 times.
7190 if (!block->channel_in_cpl[ch])
208 continue;
209
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7190 times.
7190 if (!block0->channel_in_cpl[ch]) {
210 block->new_cpl_coords[ch] = 1;
211 } else {
212 7190 CoefSumType coord_diff = 0;
213
2/2
✓ Branch 0 taken 28760 times.
✓ Branch 1 taken 7190 times.
35950 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
214
2/2
✓ Branch 0 taken 6883 times.
✓ Branch 1 taken 37 times.
28760 coord_diff += FFABS(cpl_coords[blk-1][ch][bnd] -
215 cpl_coords[blk ][ch][bnd]);
216 }
217 7190 coord_diff /= s->num_cpl_bands;
218
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 7158 times.
7190 if (coord_diff > NEW_CPL_COORD_THRESHOLD)
219 32 block->new_cpl_coords[ch] = 1;
220 }
221 }
222 }
223 }
224 }
225
226 /* calculate final coupling coordinates, taking into account reusing of
227 coordinates in successive blocks */
228
2/2
✓ Branch 0 taken 2876 times.
✓ Branch 1 taken 719 times.
3595 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
229 2876 blk = 0;
230
2/2
✓ Branch 0 taken 2964 times.
✓ Branch 1 taken 2876 times.
5840 while (blk < s->num_blocks) {
231 2964 int av_uninit(blk1);
232 2964 AC3Block *block = &s->blocks[blk];
233
234
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2964 times.
2964 if (!block->cpl_in_use) {
235 blk++;
236 continue;
237 }
238
239
2/2
✓ Branch 0 taken 5928 times.
✓ Branch 1 taken 2964 times.
8892 for (ch = 1; ch <= s->fbw_channels; ch++) {
240 CoefSumType energy_ch, energy_cpl;
241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5928 times.
5928 if (!block->channel_in_cpl[ch])
242 continue;
243 5928 energy_cpl = energy[blk][CPL_CH][bnd];
244 5928 energy_ch = energy[blk][ch][bnd];
245 5928 blk1 = blk+1;
246
4/4
✓ Branch 0 taken 28888 times.
✓ Branch 1 taken 5808 times.
✓ Branch 2 taken 28768 times.
✓ Branch 3 taken 120 times.
34696 while (blk1 < s->num_blocks && !s->blocks[blk1].new_cpl_coords[ch]) {
247
1/2
✓ Branch 0 taken 28768 times.
✗ Branch 1 not taken.
28768 if (s->blocks[blk1].cpl_in_use) {
248 28768 energy_cpl += energy[blk1][CPL_CH][bnd];
249 28768 energy_ch += energy[blk1][ch][bnd];
250 }
251 28768 blk1++;
252 }
253 5928 cpl_coords[blk][ch][bnd] = calc_cpl_coord(energy_ch, energy_cpl);
254 }
255 2964 blk = blk1;
256 }
257 }
258
259 /* calculate exponents/mantissas for coupling coordinates */
260
2/2
✓ Branch 0 taken 4314 times.
✓ Branch 1 taken 719 times.
5033 for (blk = 0; blk < s->num_blocks; blk++) {
261 4314 AC3Block *block = &s->blocks[blk];
262
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4314 times.
4314 if (!block->cpl_in_use)
263 continue;
264
265 #if AC3ENC_FLOAT
266 3276 s->ac3dsp.float_to_fixed24(fixed_cpl_coords[blk][1],
267 3276 cpl_coords[blk][1],
268 3276 s->fbw_channels * 16);
269 #endif
270 4314 s->ac3dsp.extract_exponents(block->cpl_coord_exp[1],
271 4314 fixed_cpl_coords[blk][1],
272 4314 s->fbw_channels * 16);
273
274
2/2
✓ Branch 0 taken 8628 times.
✓ Branch 1 taken 4314 times.
12942 for (ch = 1; ch <= s->fbw_channels; ch++) {
275 int bnd, min_exp, max_exp, master_exp;
276
277
2/2
✓ Branch 0 taken 7158 times.
✓ Branch 1 taken 1470 times.
8628 if (!block->new_cpl_coords[ch])
278 7158 continue;
279
280 /* determine master exponent */
281 1470 min_exp = max_exp = block->cpl_coord_exp[ch][0];
282
2/2
✓ Branch 0 taken 4410 times.
✓ Branch 1 taken 1470 times.
5880 for (bnd = 1; bnd < s->num_cpl_bands; bnd++) {
283 4410 int exp = block->cpl_coord_exp[ch][bnd];
284 4410 min_exp = FFMIN(exp, min_exp);
285 4410 max_exp = FFMAX(exp, max_exp);
286 }
287 1470 master_exp = ((max_exp - 15) + 2) / 3;
288 1470 master_exp = FFMAX(master_exp, 0);
289
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1470 times.
1470 while (min_exp < master_exp * 3)
290 master_exp--;
291
2/2
✓ Branch 0 taken 5880 times.
✓ Branch 1 taken 1470 times.
7350 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
292 5880 block->cpl_coord_exp[ch][bnd] = av_clip(block->cpl_coord_exp[ch][bnd] -
293 5880 master_exp * 3, 0, 15);
294 }
295 1470 block->cpl_master_exp[ch] = master_exp;
296
297 /* quantize mantissas */
298
2/2
✓ Branch 0 taken 5880 times.
✓ Branch 1 taken 1470 times.
7350 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
299 5880 int cpl_exp = block->cpl_coord_exp[ch][bnd];
300 5880 int cpl_mant = (fixed_cpl_coords[blk][ch][bnd] << (5 + cpl_exp + master_exp * 3)) >> 24;
301
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5880 times.
5880 if (cpl_exp == 15)
302 cpl_mant >>= 1;
303 else
304 5880 cpl_mant -= 16;
305
306 5880 block->cpl_coord_mant[ch][bnd] = cpl_mant;
307 }
308 }
309 }
310
311
2/2
✓ Branch 0 taken 273 times.
✓ Branch 1 taken 273 times.
546 if (AC3ENC_FLOAT && CONFIG_EAC3_ENCODER && s->eac3)
312 273 ff_eac3_set_cpl_states(s);
313 719 }
314
315
316 /*
317 * Determine rematrixing flags for each block and band.
318 */
319 1200 static void compute_rematrixing_strategy(AC3EncodeContext *s)
320 {
321 int nb_coefs;
322 int blk, bnd;
323 1200 AC3Block *block, *block0 = NULL;
324
325
2/2
✓ Branch 0 taken 481 times.
✓ Branch 1 taken 719 times.
1200 if (s->channel_mode != AC3_CHMODE_STEREO)
326 481 return;
327
328
2/2
✓ Branch 0 taken 4314 times.
✓ Branch 1 taken 719 times.
5033 for (blk = 0; blk < s->num_blocks; blk++) {
329 4314 block = &s->blocks[blk];
330 4314 block->new_rematrixing_strategy = !blk;
331
332 4314 block->num_rematrixing_bands = 4;
333
1/2
✓ Branch 0 taken 4314 times.
✗ Branch 1 not taken.
4314 if (block->cpl_in_use) {
334 4314 block->num_rematrixing_bands -= (s->start_freq[CPL_CH] <= 61);
335 4314 block->num_rematrixing_bands -= (s->start_freq[CPL_CH] == 37);
336
3/4
✓ Branch 0 taken 3595 times.
✓ Branch 1 taken 719 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3595 times.
4314 if (blk && block->num_rematrixing_bands != block0->num_rematrixing_bands)
337 block->new_rematrixing_strategy = 1;
338 }
339 4314 nb_coefs = FFMIN(block->end_freq[1], block->end_freq[2]);
340
341
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4314 times.
4314 if (!s->rematrixing_enabled) {
342 block0 = block;
343 continue;
344 }
345
346
2/2
✓ Branch 0 taken 17256 times.
✓ Branch 1 taken 4314 times.
21570 for (bnd = 0; bnd < block->num_rematrixing_bands; bnd++) {
347 /* calculate sum of squared coeffs for one band in one block */
348 17256 int start = ff_ac3_rematrix_band_tab[bnd];
349 17256 int end = FFMIN(nb_coefs, ff_ac3_rematrix_band_tab[bnd+1]);
350 CoefSumType sum[4];
351 17256 sum_square_butterfly(s, sum, block->mdct_coef[1] + start,
352 17256 block->mdct_coef[2] + start, end - start);
353
354 /* compare sums to determine if rematrixing will be used for this band */
355
6/6
✓ Branch 0 taken 14315 times.
✓ Branch 1 taken 2941 times.
✓ Branch 2 taken 5234 times.
✓ Branch 3 taken 7870 times.
✓ Branch 4 taken 7354 times.
✓ Branch 5 taken 5750 times.
17256 if (FFMIN(sum[2], sum[3]) < FFMIN(sum[0], sum[1]))
356 9547 block->rematrixing_flags[bnd] = 1;
357 else
358 7709 block->rematrixing_flags[bnd] = 0;
359
360 /* determine if new rematrixing flags will be sent */
361
2/2
✓ Branch 0 taken 14380 times.
✓ Branch 1 taken 2876 times.
17256 if (blk &&
362
2/2
✓ Branch 0 taken 1886 times.
✓ Branch 1 taken 12494 times.
14380 block->rematrixing_flags[bnd] != block0->rematrixing_flags[bnd]) {
363 1886 block->new_rematrixing_strategy = 1;
364 }
365 }
366 4314 block0 = block;
367 }
368 }
369
370
371 1200 int AC3_NAME(encode_frame)(AVCodecContext *avctx, AVPacket *avpkt,
372 const AVFrame *frame, int *got_packet_ptr)
373 {
374 1200 AC3EncodeContext *s = avctx->priv_data;
375 int ret;
376
377
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1200 times.
1200 if (s->options.allow_per_frame_metadata) {
378 ret = ff_ac3_validate_metadata(s);
379 if (ret)
380 return ret;
381 }
382
383
2/4
✓ Branch 0 taken 654 times.
✓ Branch 1 taken 546 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1200 if (s->bit_alloc.sr_code == 1 || (AC3ENC_FLOAT && s->eac3))
384 1200 ff_ac3_adjust_frame_size(s);
385
386 1200 copy_input_samples(s, (SampleType **)frame->extended_data);
387
388 1200 apply_mdct(s);
389
390 1200 s->cpl_on = s->cpl_enabled;
391 1200 ff_ac3_compute_coupling_strategy(s);
392
393
2/2
✓ Branch 0 taken 719 times.
✓ Branch 1 taken 481 times.
1200 if (s->cpl_on)
394 719 apply_channel_coupling(s);
395
396 1200 compute_rematrixing_strategy(s);
397
398 #if AC3ENC_FLOAT
399 546 scale_coefficients(s);
400 #endif
401
402 1200 return ff_ac3_encode_frame_common_end(avctx, avpkt, frame, got_packet_ptr);
403 }
404