FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/ac3dsp.c
Date: 2021-09-23 20:34:37
Exec Total Coverage
Lines: 209 218 95.9%
Branches: 84 102 82.4%

Line Branch Exec Source
1 /*
2 * AC-3 DSP functions
3 * Copyright (c) 2011 Justin Ruggles
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <math.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "config.h"
27 #include "libavutil/attributes.h"
28 #include "libavutil/common.h"
29 #include "libavutil/intmath.h"
30 #include "libavutil/mem_internal.h"
31
32 #include "ac3.h"
33 #include "ac3dsp.h"
34 #include "mathops.h"
35
36 3154 static void ac3_exponent_min_c(uint8_t *exp, int num_reuse_blocks, int nb_coefs)
37 {
38 int blk, i;
39
40
2/2
✓ Branch 0 taken 532 times.
✓ Branch 1 taken 2622 times.
3154 if (!num_reuse_blocks)
41 532 return;
42
43
2/2
✓ Branch 0 taken 671232 times.
✓ Branch 1 taken 2622 times.
673854 for (i = 0; i < nb_coefs; i++) {
44 671232 uint8_t min_exp = *exp;
45 671232 uint8_t *exp1 = exp + 256;
46
2/2
✓ Branch 0 taken 2822144 times.
✓ Branch 1 taken 671232 times.
3493376 for (blk = 0; blk < num_reuse_blocks; blk++) {
47 2822144 uint8_t next_exp = *exp1;
48
2/2
✓ Branch 0 taken 430495 times.
✓ Branch 1 taken 2391649 times.
2822144 if (next_exp < min_exp)
49 430495 min_exp = next_exp;
50 2822144 exp1 += 256;
51 }
52 671232 *exp++ = min_exp;
53 }
54 }
55
56 3822 static void float_to_fixed24_c(int32_t *dst, const float *src, unsigned int len)
57 {
58 3822 const float scale = 1 << 24;
59 do {
60 327600 *dst++ = lrintf(*src++ * scale);
61 327600 *dst++ = lrintf(*src++ * scale);
62 327600 *dst++ = lrintf(*src++ * scale);
63 327600 *dst++ = lrintf(*src++ * scale);
64 327600 *dst++ = lrintf(*src++ * scale);
65 327600 *dst++ = lrintf(*src++ * scale);
66 327600 *dst++ = lrintf(*src++ * scale);
67 327600 *dst++ = lrintf(*src++ * scale);
68 327600 len -= 8;
69
2/2
✓ Branch 0 taken 323778 times.
✓ Branch 1 taken 3822 times.
327600 } while (len > 0);
70 3822 }
71
72 40472 static void ac3_bit_alloc_calc_bap_c(int16_t *mask, int16_t *psd,
73 int start, int end,
74 int snr_offset, int floor,
75 const uint8_t *bap_tab, uint8_t *bap)
76 {
77 int bin, band, band_end;
78
79 /* special case, if snr offset is -960, set all bap's to zero */
80
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40472 times.
40472 if (snr_offset == -960) {
81 memset(bap, 0, AC3_MAX_COEFS);
82 return;
83 }
84
85 40472 bin = start;
86 40472 band = ff_ac3_bin_to_band_tab[start];
87 do {
88 1194629 int m = (FFMAX(mask[band] - snr_offset - floor, 0) & 0x1FE0) + floor;
89 1194629 band_end = ff_ac3_band_start_tab[++band];
90 1194629 band_end = FFMIN(band_end, end);
91
92
2/2
✓ Branch 0 taken 4341432 times.
✓ Branch 1 taken 1194629 times.
5536061 for (; bin < band_end; bin++) {
93 4341432 int address = av_clip_uintp2((psd[bin] - m) >> 5, 6);
94 4341432 bap[bin] = bap_tab[address];
95 }
96
2/2
✓ Branch 0 taken 1154157 times.
✓ Branch 1 taken 40472 times.
1194629 } while (end > band_end);
97 }
98
99 120006 static void ac3_update_bap_counts_c(uint16_t mant_cnt[16], uint8_t *bap,
100 int len)
101 {
102
2/2
✓ Branch 0 taken 11691258 times.
✓ Branch 1 taken 120006 times.
11811264 while (len-- > 0)
103 11691258 mant_cnt[bap[len]]++;
104 120006 }
105
106 DECLARE_ALIGNED(16, const uint16_t, ff_ac3_bap_bits)[16] = {
107 0, 0, 0, 3, 0, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16
108 };
109
110 7877 static int ac3_compute_mantissa_size_c(uint16_t mant_cnt[6][16])
111 {
112 int blk, bap;
113 7877 int bits = 0;
114
115
2/2
✓ Branch 0 taken 47262 times.
✓ Branch 1 taken 7877 times.
55139 for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
116 // bap=1 : 3 mantissas in 5 bits
117 47262 bits += (mant_cnt[blk][1] / 3) * 5;
118 // bap=2 : 3 mantissas in 7 bits
119 // bap=4 : 2 mantissas in 7 bits
120 47262 bits += ((mant_cnt[blk][2] / 3) + (mant_cnt[blk][4] >> 1)) * 7;
121 // bap=3 : 1 mantissa in 3 bits
122 47262 bits += mant_cnt[blk][3] * 3;
123 // bap=5 to 15 : get bits per mantissa from table
124
2/2
✓ Branch 0 taken 519882 times.
✓ Branch 1 taken 47262 times.
567144 for (bap = 5; bap < 16; bap++)
125 519882 bits += mant_cnt[blk][bap] * ff_ac3_bap_bits[bap];
126 }
127 7877 return bits;
128 }
129
130 5239 static void ac3_extract_exponents_c(uint8_t *exp, int32_t *coef, int nb_coefs)
131 {
132 int i;
133
134
2/2
✓ Branch 0 taken 3767616 times.
✓ Branch 1 taken 5239 times.
3772855 for (i = 0; i < nb_coefs; i++) {
135 3767616 int v = abs(coef[i]);
136
2/2
✓ Branch 0 taken 2756472 times.
✓ Branch 1 taken 1011144 times.
3767616 exp[i] = v ? 23 - av_log2(v) : 24;
137 }
138 5239 }
139
140 4152 static void ac3_sum_square_butterfly_int32_c(int64_t sum[4],
141 const int32_t *coef0,
142 const int32_t *coef1,
143 int len)
144 {
145 int i;
146
147 4152 sum[0] = sum[1] = sum[2] = sum[3] = 0;
148
149
2/2
✓ Branch 0 taken 87192 times.
✓ Branch 1 taken 4152 times.
91344 for (i = 0; i < len; i++) {
150 87192 int lt = coef0[i];
151 87192 int rt = coef1[i];
152 87192 int md = lt + rt;
153 87192 int sd = lt - rt;
154 87192 MAC64(sum[0], lt, lt);
155 87192 MAC64(sum[1], rt, rt);
156 87192 MAC64(sum[2], md, md);
157 87192 MAC64(sum[3], sd, sd);
158 }
159 4152 }
160
161 13104 static void ac3_sum_square_butterfly_float_c(float sum[4],
162 const float *coef0,
163 const float *coef1,
164 int len)
165 {
166 int i;
167
168 13104 sum[0] = sum[1] = sum[2] = sum[3] = 0;
169
170
2/2
✓ Branch 0 taken 275184 times.
✓ Branch 1 taken 13104 times.
288288 for (i = 0; i < len; i++) {
171 275184 float lt = coef0[i];
172 275184 float rt = coef1[i];
173 275184 float md = lt + rt;
174 275184 float sd = lt - rt;
175 275184 sum[0] += lt * lt;
176 275184 sum[1] += rt * rt;
177 275184 sum[2] += md * md;
178 275184 sum[3] += sd * sd;
179 }
180 13104 }
181
182 342 static void ac3_downmix_5_to_2_symmetric_c(float **samples, float **matrix,
183 int len)
184 {
185 int i;
186 float v0, v1;
187 342 float front_mix = matrix[0][0];
188 342 float center_mix = matrix[0][1];
189 342 float surround_mix = matrix[0][3];
190
191
2/2
✓ Branch 0 taken 87552 times.
✓ Branch 1 taken 342 times.
87894 for (i = 0; i < len; i++) {
192 87552 v0 = samples[0][i] * front_mix +
193 87552 samples[1][i] * center_mix +
194 87552 samples[3][i] * surround_mix;
195
196 87552 v1 = samples[1][i] * center_mix +
197 87552 samples[2][i] * front_mix +
198 87552 samples[4][i] * surround_mix;
199
200 87552 samples[0][i] = v0;
201 87552 samples[1][i] = v1;
202 }
203 342 }
204
205 342 static void ac3_downmix_5_to_1_symmetric_c(float **samples, float **matrix,
206 int len)
207 {
208 int i;
209 342 float front_mix = matrix[0][0];
210 342 float center_mix = matrix[0][1];
211 342 float surround_mix = matrix[0][3];
212
213
2/2
✓ Branch 0 taken 87552 times.
✓ Branch 1 taken 342 times.
87894 for (i = 0; i < len; i++) {
214 87552 samples[0][i] = samples[0][i] * front_mix +
215 87552 samples[1][i] * center_mix +
216 87552 samples[2][i] * front_mix +
217 87552 samples[3][i] * surround_mix +
218 87552 samples[4][i] * surround_mix;
219 }
220 342 }
221
222 764 static void ac3_downmix_c(float **samples, float **matrix,
223 int out_ch, int in_ch, int len)
224 {
225 int i, j;
226 float v0, v1;
227
228
2/2
✓ Branch 0 taken 379 times.
✓ Branch 1 taken 385 times.
764 if (out_ch == 2) {
229
2/2
✓ Branch 0 taken 96896 times.
✓ Branch 1 taken 379 times.
97275 for (i = 0; i < len; i++) {
230 96896 v0 = v1 = 0.0f;
231
2/2
✓ Branch 0 taken 387584 times.
✓ Branch 1 taken 96896 times.
484480 for (j = 0; j < in_ch; j++) {
232 387584 v0 += samples[j][i] * matrix[0][j];
233 387584 v1 += samples[j][i] * matrix[1][j];
234 }
235 96896 samples[0][i] = v0;
236 96896 samples[1][i] = v1;
237 }
238
1/2
✓ Branch 0 taken 385 times.
✗ Branch 1 not taken.
385 } else if (out_ch == 1) {
239
2/2
✓ Branch 0 taken 98432 times.
✓ Branch 1 taken 385 times.
98817 for (i = 0; i < len; i++) {
240 98432 v0 = 0.0f;
241
2/2
✓ Branch 0 taken 393728 times.
✓ Branch 1 taken 98432 times.
492160 for (j = 0; j < in_ch; j++)
242 393728 v0 += samples[j][i] * matrix[0][j];
243 98432 samples[0][i] = v0;
244 }
245 }
246 764 }
247
248 330 static void ac3_downmix_5_to_2_symmetric_c_fixed(int32_t **samples, int16_t **matrix,
249 int len)
250 {
251 int i;
252 int64_t v0, v1;
253 330 int16_t front_mix = matrix[0][0];
254 330 int16_t center_mix = matrix[0][1];
255 330 int16_t surround_mix = matrix[0][3];
256
257
2/2
✓ Branch 0 taken 84480 times.
✓ Branch 1 taken 330 times.
84810 for (i = 0; i < len; i++) {
258 84480 v0 = (int64_t)samples[0][i] * front_mix +
259 84480 (int64_t)samples[1][i] * center_mix +
260 84480 (int64_t)samples[3][i] * surround_mix;
261
262 84480 v1 = (int64_t)samples[1][i] * center_mix +
263 84480 (int64_t)samples[2][i] * front_mix +
264 84480 (int64_t)samples[4][i] * surround_mix;
265
266 84480 samples[0][i] = (v0+2048)>>12;
267 84480 samples[1][i] = (v1+2048)>>12;
268 }
269 330 }
270
271 330 static void ac3_downmix_5_to_1_symmetric_c_fixed(int32_t **samples, int16_t **matrix,
272 int len)
273 {
274 int i;
275 int64_t v0;
276 330 int16_t front_mix = matrix[0][0];
277 330 int16_t center_mix = matrix[0][1];
278 330 int16_t surround_mix = matrix[0][3];
279
280
2/2
✓ Branch 0 taken 84480 times.
✓ Branch 1 taken 330 times.
84810 for (i = 0; i < len; i++) {
281 84480 v0 = (int64_t)samples[0][i] * front_mix +
282 84480 (int64_t)samples[1][i] * center_mix +
283 84480 (int64_t)samples[2][i] * front_mix +
284 84480 (int64_t)samples[3][i] * surround_mix +
285 84480 (int64_t)samples[4][i] * surround_mix;
286
287 84480 samples[0][i] = (v0+2048)>>12;
288 }
289 330 }
290
291 372 static void ac3_downmix_c_fixed(int32_t **samples, int16_t **matrix,
292 int out_ch, int in_ch, int len)
293 {
294 int i, j;
295 int64_t v0, v1;
296
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 372 times.
372 if (out_ch == 2) {
297 for (i = 0; i < len; i++) {
298 v0 = v1 = 0;
299 for (j = 0; j < in_ch; j++) {
300 v0 += (int64_t)samples[j][i] * matrix[0][j];
301 v1 += (int64_t)samples[j][i] * matrix[1][j];
302 }
303 samples[0][i] = (v0+2048)>>12;
304 samples[1][i] = (v1+2048)>>12;
305 }
306
1/2
✓ Branch 0 taken 372 times.
✗ Branch 1 not taken.
372 } else if (out_ch == 1) {
307
2/2
✓ Branch 0 taken 95104 times.
✓ Branch 1 taken 372 times.
95476 for (i = 0; i < len; i++) {
308 95104 v0 = 0;
309
2/2
✓ Branch 0 taken 380416 times.
✓ Branch 1 taken 95104 times.
475520 for (j = 0; j < in_ch; j++)
310 380416 v0 += (int64_t)samples[j][i] * matrix[0][j];
311 95104 samples[0][i] = (v0+2048)>>12;
312 }
313 }
314 372 }
315
316 1032 void ff_ac3dsp_downmix_fixed(AC3DSPContext *c, int32_t **samples, int16_t **matrix,
317 int out_ch, int in_ch, int len)
318 {
319
3/4
✓ Branch 0 taken 1029 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1029 times.
1032 if (c->in_channels != in_ch || c->out_channels != out_ch) {
320 3 c->in_channels = in_ch;
321 3 c->out_channels = out_ch;
322 3 c->downmix_fixed = NULL;
323
324
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
3 if (in_ch == 5 && out_ch == 2 &&
325 1 !(matrix[1][0] | matrix[0][2] |
326 1 matrix[1][3] | matrix[0][4] |
327 1 (matrix[0][1] ^ matrix[1][1]) |
328
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 (matrix[0][0] ^ matrix[1][2]))) {
329 1 c->downmix_fixed = ac3_downmix_5_to_2_symmetric_c_fixed;
330
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
2 } else if (in_ch == 5 && out_ch == 1 &&
331
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 matrix[0][0] == matrix[0][2] &&
332
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 matrix[0][3] == matrix[0][4]) {
333 1 c->downmix_fixed = ac3_downmix_5_to_1_symmetric_c_fixed;
334 }
335 }
336
337
2/2
✓ Branch 0 taken 660 times.
✓ Branch 1 taken 372 times.
1032 if (c->downmix_fixed)
338 660 c->downmix_fixed(samples, matrix, len);
339 else
340 372 ac3_downmix_c_fixed(samples, matrix, out_ch, in_ch, len);
341 1032 }
342
343 1448 void ff_ac3dsp_downmix(AC3DSPContext *c, float **samples, float **matrix,
344 int out_ch, int in_ch, int len)
345 {
346
3/4
✓ Branch 0 taken 1437 times.
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1437 times.
1448 if (c->in_channels != in_ch || c->out_channels != out_ch) {
347 11 int **matrix_cmp = (int **)matrix;
348
349 11 c->in_channels = in_ch;
350 11 c->out_channels = out_ch;
351 11 c->downmix = NULL;
352
353
4/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 3 times.
11 if (in_ch == 5 && out_ch == 2 &&
354 3 !(matrix_cmp[1][0] | matrix_cmp[0][2] |
355 3 matrix_cmp[1][3] | matrix_cmp[0][4] |
356 3 (matrix_cmp[0][1] ^ matrix_cmp[1][1]) |
357
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 (matrix_cmp[0][0] ^ matrix_cmp[1][2]))) {
358 3 c->downmix = ac3_downmix_5_to_2_symmetric_c;
359
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
8 } else if (in_ch == 5 && out_ch == 1 &&
360
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 matrix_cmp[0][0] == matrix_cmp[0][2] &&
361
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 matrix_cmp[0][3] == matrix_cmp[0][4]) {
362 3 c->downmix = ac3_downmix_5_to_1_symmetric_c;
363 }
364
365 if (ARCH_X86)
366 11 ff_ac3dsp_set_downmix_x86(c);
367 }
368
369
2/2
✓ Branch 0 taken 684 times.
✓ Branch 1 taken 764 times.
1448 if (c->downmix)
370 684 c->downmix(samples, matrix, len);
371 else
372 764 ac3_downmix_c(samples, matrix, out_ch, in_ch, len);
373 1448 }
374
375 75 av_cold void ff_ac3dsp_init(AC3DSPContext *c, int bit_exact)
376 {
377 75 c->ac3_exponent_min = ac3_exponent_min_c;
378 75 c->float_to_fixed24 = float_to_fixed24_c;
379 75 c->bit_alloc_calc_bap = ac3_bit_alloc_calc_bap_c;
380 75 c->update_bap_counts = ac3_update_bap_counts_c;
381 75 c->compute_mantissa_size = ac3_compute_mantissa_size_c;
382 75 c->extract_exponents = ac3_extract_exponents_c;
383 75 c->sum_square_butterfly_int32 = ac3_sum_square_butterfly_int32_c;
384 75 c->sum_square_butterfly_float = ac3_sum_square_butterfly_float_c;
385 75 c->in_channels = 0;
386 75 c->out_channels = 0;
387 75 c->downmix = NULL;
388 75 c->downmix_fixed = NULL;
389
390 if (ARCH_ARM)
391 ff_ac3dsp_init_arm(c, bit_exact);
392 if (ARCH_X86)
393 75 ff_ac3dsp_init_x86(c, bit_exact);
394 if (ARCH_MIPS)
395 ff_ac3dsp_init_mips(c, bit_exact);
396 75 }
397