FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/ac3enc_template.c
Date: 2024-04-26 14:42:52
Exec Total Coverage
Lines: 161 177 91.0%
Functions: 4 4 100.0%
Branches: 105 124 84.7%

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/mem_internal.h"
35
36 #include "audiodsp.h"
37 #include "ac3enc.h"
38 #include "eac3enc.h"
39
40 #if AC3ENC_FLOAT
41 #define RENAME(element) element ## _float
42 #else
43 #define RENAME(element) element ## _fixed
44 #endif
45
46 /*
47 * Apply the MDCT to input samples to generate frequency coefficients.
48 * This applies the KBD window and normalizes the input to reduce precision
49 * loss due to fixed-point calculations.
50 */
51 1243 static void apply_mdct(AC3EncodeContext *s, uint8_t * const *samples)
52 {
53 int blk, ch;
54
55
2/2
✓ Branch 0 taken 1962 times.
✓ Branch 1 taken 1243 times.
3205 for (ch = 0; ch < s->channels; ch++) {
56 1962 const SampleType *input_samples0 = (const SampleType*)s->planar_samples[ch];
57 /* Reorder channels from native order to AC-3 order. */
58 1962 const SampleType *input_samples1 = (const SampleType*)samples[s->channel_map[ch]];
59
60
2/2
✓ Branch 0 taken 11772 times.
✓ Branch 1 taken 1962 times.
13734 for (blk = 0; blk < s->num_blocks; blk++) {
61 11772 AC3Block *block = &s->blocks[blk];
62 11772 SampleType *windowed_samples = s->RENAME(windowed_samples);
63
64 11772 s->fdsp->vector_fmul(windowed_samples, input_samples0,
65 11772 s->RENAME(mdct_window), AC3_BLOCK_SIZE);
66 11772 s->fdsp->vector_fmul_reverse(windowed_samples + AC3_BLOCK_SIZE,
67 input_samples1,
68 11772 s->RENAME(mdct_window), AC3_BLOCK_SIZE);
69
70 11772 s->tx_fn(s->tx, block->mdct_coef[ch+1],
71 windowed_samples, sizeof(*windowed_samples));
72 11772 input_samples0 = input_samples1;
73 11772 input_samples1 += AC3_BLOCK_SIZE;
74 }
75 /* Store last 256 samples of current frame */
76 1962 memcpy(s->planar_samples[ch], input_samples0,
77 AC3_BLOCK_SIZE * sizeof(*input_samples0));
78 }
79 1243 }
80
81
82 /*
83 * Calculate coupling channel and coupling coordinates.
84 */
85 719 static void apply_channel_coupling(AC3EncodeContext *s)
86 {
87 719 LOCAL_ALIGNED_32(CoefType, cpl_coords, [AC3_MAX_BLOCKS], [AC3_MAX_CHANNELS][16]);
88 #if AC3ENC_FLOAT
89 546 LOCAL_ALIGNED_32(int32_t, fixed_cpl_coords, [AC3_MAX_BLOCKS], [AC3_MAX_CHANNELS][16]);
90 #else
91 173 int32_t (*fixed_cpl_coords)[AC3_MAX_CHANNELS][16] = cpl_coords;
92 #endif
93 719 int av_uninit(blk), ch, bnd, i, j;
94 719 CoefSumType energy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][16] = {{{0}}};
95 int cpl_start, num_cpl_coefs;
96
97 719 memset(cpl_coords, 0, AC3_MAX_BLOCKS * sizeof(*cpl_coords));
98 #if AC3ENC_FLOAT
99 546 memset(fixed_cpl_coords, 0, AC3_MAX_BLOCKS * sizeof(*cpl_coords));
100 #endif
101
102 /* align start to 16-byte boundary. align length to multiple of 32.
103 note: coupling start bin % 4 will always be 1 */
104 719 cpl_start = s->start_freq[CPL_CH] - 1;
105 719 num_cpl_coefs = FFALIGN(s->num_cpl_subbands * 12 + 1, 32);
106 719 cpl_start = FFMIN(256, cpl_start + num_cpl_coefs) - num_cpl_coefs;
107
108 /* calculate coupling channel from fbw channels */
109
2/2
✓ Branch 0 taken 4314 times.
✓ Branch 1 taken 719 times.
5033 for (blk = 0; blk < s->num_blocks; blk++) {
110 4314 AC3Block *block = &s->blocks[blk];
111 4314 CoefType *cpl_coef = &block->mdct_coef[CPL_CH][cpl_start];
112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4314 times.
4314 if (!block->cpl_in_use)
113 continue;
114 4314 memset(cpl_coef, 0, num_cpl_coefs * sizeof(*cpl_coef));
115
2/2
✓ Branch 0 taken 8628 times.
✓ Branch 1 taken 4314 times.
12942 for (ch = 1; ch <= s->fbw_channels; ch++) {
116 8628 CoefType *ch_coef = &block->mdct_coef[ch][cpl_start];
117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8628 times.
8628 if (!block->channel_in_cpl[ch])
118 continue;
119
2/2
✓ Branch 0 taken 552192 times.
✓ Branch 1 taken 8628 times.
560820 for (i = 0; i < num_cpl_coefs; i++)
120 552192 cpl_coef[i] += ch_coef[i];
121 }
122
123 /* coefficients must be clipped in order to be encoded */
124 4314 clip_coefficients(&s->adsp, cpl_coef, num_cpl_coefs);
125 }
126
127 /* calculate energy in each band in coupling channel and each fbw channel */
128 /* TODO: possibly use SIMD to speed up energy calculation */
129 719 bnd = 0;
130 719 i = s->start_freq[CPL_CH];
131
2/2
✓ Branch 0 taken 2876 times.
✓ Branch 1 taken 719 times.
3595 while (i < s->cpl_end_freq) {
132 2876 int band_size = s->cpl_band_sizes[bnd];
133
2/2
✓ Branch 0 taken 8628 times.
✓ Branch 1 taken 2876 times.
11504 for (ch = CPL_CH; ch <= s->fbw_channels; ch++) {
134
2/2
✓ Branch 0 taken 51768 times.
✓ Branch 1 taken 8628 times.
60396 for (blk = 0; blk < s->num_blocks; blk++) {
135 51768 AC3Block *block = &s->blocks[blk];
136
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]))
137 continue;
138
2/2
✓ Branch 0 taken 776520 times.
✓ Branch 1 taken 51768 times.
828288 for (j = 0; j < band_size; j++) {
139 776520 CoefType v = block->mdct_coef[ch][i+j];
140 776520 MAC_COEF(energy[blk][ch][bnd], v, v);
141 }
142 }
143 }
144 2876 i += band_size;
145 2876 bnd++;
146 }
147
148 /* calculate coupling coordinates for all blocks for all channels */
149
2/2
✓ Branch 0 taken 4314 times.
✓ Branch 1 taken 719 times.
5033 for (blk = 0; blk < s->num_blocks; blk++) {
150 4314 AC3Block *block = &s->blocks[blk];
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4314 times.
4314 if (!block->cpl_in_use)
152 continue;
153
2/2
✓ Branch 0 taken 8628 times.
✓ Branch 1 taken 4314 times.
12942 for (ch = 1; ch <= s->fbw_channels; ch++) {
154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8628 times.
8628 if (!block->channel_in_cpl[ch])
155 continue;
156
2/2
✓ Branch 0 taken 34512 times.
✓ Branch 1 taken 8628 times.
43140 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
157 34512 cpl_coords[blk][ch][bnd] = calc_cpl_coord(energy[blk][ch][bnd],
158 energy[blk][CPL_CH][bnd]);
159 }
160 }
161 }
162
163 /* determine which blocks to send new coupling coordinates for */
164
2/2
✓ Branch 0 taken 4314 times.
✓ Branch 1 taken 719 times.
5033 for (blk = 0; blk < s->num_blocks; blk++) {
165 4314 AC3Block *block = &s->blocks[blk];
166
2/2
✓ Branch 0 taken 3595 times.
✓ Branch 1 taken 719 times.
4314 AC3Block *block0 = blk ? &s->blocks[blk-1] : NULL;
167
168 4314 memset(block->new_cpl_coords, 0, sizeof(block->new_cpl_coords));
169
170
1/2
✓ Branch 0 taken 4314 times.
✗ Branch 1 not taken.
4314 if (block->cpl_in_use) {
171 /* send new coordinates if this is the first block, if previous
172 * block did not use coupling but this block does, the channels
173 * using coupling has changed from the previous block, or the
174 * coordinate difference from the last block for any channel is
175 * greater than a threshold value. */
176
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) {
177
2/2
✓ Branch 0 taken 1438 times.
✓ Branch 1 taken 719 times.
2157 for (ch = 1; ch <= s->fbw_channels; ch++)
178 1438 block->new_cpl_coords[ch] = 1;
179 } else {
180
2/2
✓ Branch 0 taken 7190 times.
✓ Branch 1 taken 3595 times.
10785 for (ch = 1; ch <= s->fbw_channels; ch++) {
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7190 times.
7190 if (!block->channel_in_cpl[ch])
182 continue;
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7190 times.
7190 if (!block0->channel_in_cpl[ch]) {
184 block->new_cpl_coords[ch] = 1;
185 } else {
186 7190 CoefSumType coord_diff = 0;
187
2/2
✓ Branch 0 taken 28760 times.
✓ Branch 1 taken 7190 times.
35950 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
188
2/2
✓ Branch 0 taken 6883 times.
✓ Branch 1 taken 37 times.
28760 coord_diff += FFABS(cpl_coords[blk-1][ch][bnd] -
189 cpl_coords[blk ][ch][bnd]);
190 }
191 7190 coord_diff /= s->num_cpl_bands;
192
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 7158 times.
7190 if (coord_diff > NEW_CPL_COORD_THRESHOLD)
193 32 block->new_cpl_coords[ch] = 1;
194 }
195 }
196 }
197 }
198 }
199
200 av_assert1(s->fbw_channels > 0);
201
202 /* calculate final coupling coordinates, taking into account reusing of
203 coordinates in successive blocks */
204
2/2
✓ Branch 0 taken 2876 times.
✓ Branch 1 taken 719 times.
3595 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
205 2876 blk = 0;
206
2/2
✓ Branch 0 taken 2964 times.
✓ Branch 1 taken 2876 times.
5840 while (blk < s->num_blocks) {
207 2964 int av_uninit(blk1);
208 2964 AC3Block *block = &s->blocks[blk];
209
210
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2964 times.
2964 if (!block->cpl_in_use) {
211 blk++;
212 continue;
213 }
214
215
2/2
✓ Branch 0 taken 5928 times.
✓ Branch 1 taken 2964 times.
8892 for (ch = 1; ch <= s->fbw_channels; ch++) {
216 CoefSumType energy_ch, energy_cpl;
217
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5928 times.
5928 if (!block->channel_in_cpl[ch])
218 continue;
219 5928 energy_cpl = energy[blk][CPL_CH][bnd];
220 5928 energy_ch = energy[blk][ch][bnd];
221 5928 blk1 = blk+1;
222
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]) {
223
1/2
✓ Branch 0 taken 28768 times.
✗ Branch 1 not taken.
28768 if (s->blocks[blk1].cpl_in_use) {
224 28768 energy_cpl += energy[blk1][CPL_CH][bnd];
225 28768 energy_ch += energy[blk1][ch][bnd];
226 }
227 28768 blk1++;
228 }
229 5928 cpl_coords[blk][ch][bnd] = calc_cpl_coord(energy_ch, energy_cpl);
230 }
231 2964 blk = blk1;
232 }
233 }
234
235 /* calculate exponents/mantissas for coupling coordinates */
236
2/2
✓ Branch 0 taken 4314 times.
✓ Branch 1 taken 719 times.
5033 for (blk = 0; blk < s->num_blocks; blk++) {
237 4314 AC3Block *block = &s->blocks[blk];
238
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4314 times.
4314 if (!block->cpl_in_use)
239 continue;
240
241 #if AC3ENC_FLOAT
242 3276 s->ac3dsp.float_to_fixed24(fixed_cpl_coords[blk][1],
243 3276 cpl_coords[blk][1],
244 3276 s->fbw_channels * 16);
245 #endif
246 4314 s->ac3dsp.extract_exponents(block->cpl_coord_exp[1],
247 4314 fixed_cpl_coords[blk][1],
248 4314 s->fbw_channels * 16);
249
250
2/2
✓ Branch 0 taken 8628 times.
✓ Branch 1 taken 4314 times.
12942 for (ch = 1; ch <= s->fbw_channels; ch++) {
251 int bnd, min_exp, max_exp, master_exp;
252
253
2/2
✓ Branch 0 taken 7158 times.
✓ Branch 1 taken 1470 times.
8628 if (!block->new_cpl_coords[ch])
254 7158 continue;
255
256 /* determine master exponent */
257 1470 min_exp = max_exp = block->cpl_coord_exp[ch][0];
258
2/2
✓ Branch 0 taken 4410 times.
✓ Branch 1 taken 1470 times.
5880 for (bnd = 1; bnd < s->num_cpl_bands; bnd++) {
259 4410 int exp = block->cpl_coord_exp[ch][bnd];
260 4410 min_exp = FFMIN(exp, min_exp);
261 4410 max_exp = FFMAX(exp, max_exp);
262 }
263 1470 master_exp = ((max_exp - 15) + 2) / 3;
264 1470 master_exp = FFMAX(master_exp, 0);
265
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1470 times.
1470 while (min_exp < master_exp * 3)
266 master_exp--;
267
2/2
✓ Branch 0 taken 5880 times.
✓ Branch 1 taken 1470 times.
7350 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
268 5880 block->cpl_coord_exp[ch][bnd] = av_clip(block->cpl_coord_exp[ch][bnd] -
269 5880 master_exp * 3, 0, 15);
270 }
271 1470 block->cpl_master_exp[ch] = master_exp;
272
273 /* quantize mantissas */
274
2/2
✓ Branch 0 taken 5880 times.
✓ Branch 1 taken 1470 times.
7350 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
275 5880 int cpl_exp = block->cpl_coord_exp[ch][bnd];
276 5880 int cpl_mant = (fixed_cpl_coords[blk][ch][bnd] << (5 + cpl_exp + master_exp * 3)) >> 24;
277
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5880 times.
5880 if (cpl_exp == 15)
278 cpl_mant >>= 1;
279 else
280 5880 cpl_mant -= 16;
281
282 5880 block->cpl_coord_mant[ch][bnd] = cpl_mant;
283 }
284 }
285 }
286
287
2/2
✓ Branch 0 taken 273 times.
✓ Branch 1 taken 273 times.
546 if (AC3ENC_FLOAT && CONFIG_EAC3_ENCODER && s->eac3)
288 273 ff_eac3_set_cpl_states(s);
289 719 }
290
291
292 /*
293 * Determine rematrixing flags for each block and band.
294 */
295 1243 static void compute_rematrixing_strategy(AC3EncodeContext *s)
296 {
297 int nb_coefs;
298 int blk, bnd;
299 1243 AC3Block *block, *block0 = NULL;
300
301
2/2
✓ Branch 0 taken 524 times.
✓ Branch 1 taken 719 times.
1243 if (s->channel_mode != AC3_CHMODE_STEREO)
302 524 return;
303
304
2/2
✓ Branch 0 taken 4314 times.
✓ Branch 1 taken 719 times.
5033 for (blk = 0; blk < s->num_blocks; blk++) {
305 4314 block = &s->blocks[blk];
306 4314 block->new_rematrixing_strategy = !blk;
307
308 4314 block->num_rematrixing_bands = 4;
309
1/2
✓ Branch 0 taken 4314 times.
✗ Branch 1 not taken.
4314 if (block->cpl_in_use) {
310 4314 block->num_rematrixing_bands -= (s->start_freq[CPL_CH] <= 61);
311 4314 block->num_rematrixing_bands -= (s->start_freq[CPL_CH] == 37);
312
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)
313 block->new_rematrixing_strategy = 1;
314 }
315 4314 nb_coefs = FFMIN(block->end_freq[1], block->end_freq[2]);
316
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4314 times.
4314 if (!s->rematrixing_enabled) {
318 block0 = block;
319 continue;
320 }
321
322
2/2
✓ Branch 0 taken 17256 times.
✓ Branch 1 taken 4314 times.
21570 for (bnd = 0; bnd < block->num_rematrixing_bands; bnd++) {
323 /* calculate sum of squared coeffs for one band in one block */
324 17256 int start = ff_ac3_rematrix_band_tab[bnd];
325 17256 int end = FFMIN(nb_coefs, ff_ac3_rematrix_band_tab[bnd+1]);
326 CoefSumType sum[4];
327 17256 sum_square_butterfly(s, sum, block->mdct_coef[1] + start,
328 17256 block->mdct_coef[2] + start, end - start);
329
330 /* compare sums to determine if rematrixing will be used for this band */
331
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]))
332 9547 block->rematrixing_flags[bnd] = 1;
333 else
334 7709 block->rematrixing_flags[bnd] = 0;
335
336 /* determine if new rematrixing flags will be sent */
337
2/2
✓ Branch 0 taken 14380 times.
✓ Branch 1 taken 2876 times.
17256 if (blk &&
338
2/2
✓ Branch 0 taken 1886 times.
✓ Branch 1 taken 12494 times.
14380 block->rematrixing_flags[bnd] != block0->rematrixing_flags[bnd]) {
339 1886 block->new_rematrixing_strategy = 1;
340 }
341 }
342 4314 block0 = block;
343 }
344 }
345
346
347 1243 static void encode_frame(AC3EncodeContext *s, uint8_t * const *samples)
348 {
349 1243 apply_mdct(s, samples);
350
351 1243 s->cpl_on = s->cpl_enabled;
352 1243 ff_ac3_compute_coupling_strategy(s);
353
354
2/2
✓ Branch 0 taken 719 times.
✓ Branch 1 taken 524 times.
1243 if (s->cpl_on)
355 719 apply_channel_coupling(s);
356
357 1243 compute_rematrixing_strategy(s);
358
359 #if AC3ENC_FLOAT
360 546 scale_coefficients(s);
361 #endif
362 1243 }
363