FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/opus/enc_utils.h
Date: 2026-07-21 08:37:06
Exec Total Coverage
Lines: 31 31 100.0%
Functions: 3 3 100.0%
Branches: 7 12 58.3%

Line Branch Exec Source
1 /*
2 * Opus encoder
3 * Copyright (c) 2017 Rostislav Pehlivanov <atomnuker@gmail.com>
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 #ifndef AVCODEC_OPUS_ENC_UTILS_H
23 #define AVCODEC_OPUS_ENC_UTILS_H
24
25 #include <math.h>
26 #include <string.h>
27
28 #include "opus.h"
29
30 typedef struct FFBesselFilter {
31 float a[3];
32 float b[2];
33 float x[3];
34 float y[3];
35 } FFBesselFilter;
36
37 /* Fills the coefficients, returns 1 if filter will be unstable */
38 84 static inline int bessel_reinit(FFBesselFilter *s, float n, float f0, float fs,
39 int highpass)
40 {
41 int unstable;
42 float c, cfreq, w0, k1, k2;
43
44
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 42 times.
84 if (!highpass) {
45 42 c = (1.0f/sqrtf(sqrtf(pow(2.0f, 1.0f/n) - 3.0f/4.0f) - 0.5f))/sqrtf(3.0f);
46 42 cfreq = c*f0/fs;
47
2/4
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 42 times.
42 unstable = (cfreq <= 0.0f || cfreq >= 1.0f/4.0f);
48 } else {
49 42 c = sqrtf(3.0f)*sqrtf(sqrtf(pow(2.0f, 1.0f/n) - 3.0f/4.0f) - 0.5f);
50 42 cfreq = 0.5f - c*f0/fs;
51
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
42 unstable = (cfreq <= 3.0f/8.0f || cfreq >= 1.0f/2.0f);
52 }
53
54 84 w0 = tanf(M_PI*cfreq);
55 84 k1 = 3.0f * w0;
56 84 k2 = 3.0f * w0;
57
58 84 s->a[0] = k2/(1.0f + k1 + k2);
59 84 s->a[1] = 2.0f * s->a[0];
60 84 s->a[2] = s->a[0];
61 84 s->b[0] = 2.0f * s->a[0] * (1.0f/k2 - 1.0f);
62 84 s->b[1] = 1.0f - (s->a[0] + s->a[1] + s->a[2] + s->b[0]);
63
64
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 42 times.
84 if (highpass) {
65 42 s->a[1] *= -1;
66 42 s->b[0] *= -1;
67 }
68
69 84 return unstable;
70 }
71
72 84 static inline int bessel_init(FFBesselFilter *s, float n, float f0, float fs,
73 int highpass)
74 {
75 84 memset(s, 0, sizeof(FFBesselFilter));
76 84 return bessel_reinit(s, n, f0, fs, highpass);
77 }
78
79 168 static inline float bessel_filter(FFBesselFilter *s, float x)
80 {
81 168 s->x[2] = s->x[1];
82 168 s->x[1] = s->x[0];
83 168 s->x[0] = x;
84 168 s->y[2] = s->y[1];
85 168 s->y[1] = s->y[0];
86 168 s->y[0] = s->a[0]*s->x[0] + s->a[1]*s->x[1] + s->a[2]*s->x[2] + s->b[0]*s->y[1] + s->b[1]*s->y[2];
87 168 return s->y[0];
88 }
89
90 #endif /* AVCODEC_OPUS_ENC_UTILS_H */
91