FFmpeg
opusenc_utils.h
Go to the documentation of this file.
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_OPUSENC_UTILS_H
23 #define AVCODEC_OPUSENC_UTILS_H
24 
25 #include "opus.h"
26 
27 typedef struct FFBesselFilter {
28  float a[3];
29  float b[2];
30  float x[3];
31  float y[3];
33 
34 /* Fills the coefficients, returns 1 if filter will be unstable */
35 static inline int bessel_reinit(FFBesselFilter *s, float n, float f0, float fs,
36  int highpass)
37 {
38  int unstable;
39  float c, cfreq, w0, k1, k2;
40 
41  if (!highpass) {
42  c = (1.0f/sqrtf(sqrtf(pow(2.0f, 1.0f/n) - 3.0f/4.0f) - 0.5f))/sqrtf(3.0f);
43  cfreq = c*f0/fs;
44  unstable = (cfreq <= 0.0f || cfreq >= 1.0f/4.0f);
45  } else {
46  c = sqrtf(3.0f)*sqrtf(sqrtf(pow(2.0f, 1.0f/n) - 3.0f/4.0f) - 0.5f);
47  cfreq = 0.5f - c*f0/fs;
48  unstable = (cfreq <= 3.0f/8.0f || cfreq >= 1.0f/2.0f);
49  }
50 
51  w0 = tanf(M_PI*cfreq);
52  k1 = 3.0f * w0;
53  k2 = 3.0f * w0;
54 
55  s->a[0] = k2/(1.0f + k1 + k2);
56  s->a[1] = 2.0f * s->a[0];
57  s->a[2] = s->a[0];
58  s->b[0] = 2.0f * s->a[0] * (1.0f/k2 - 1.0f);
59  s->b[1] = 1.0f - (s->a[0] + s->a[1] + s->a[2] + s->b[0]);
60 
61  if (highpass) {
62  s->a[1] *= -1;
63  s->b[0] *= -1;
64  }
65 
66  return unstable;
67 }
68 
69 static inline int bessel_init(FFBesselFilter *s, float n, float f0, float fs,
70  int highpass)
71 {
72  memset(s, 0, sizeof(FFBesselFilter));
73  return bessel_reinit(s, n, f0, fs, highpass);
74 }
75 
76 static inline float bessel_filter(FFBesselFilter *s, float x)
77 {
78  s->x[2] = s->x[1];
79  s->x[1] = s->x[0];
80  s->x[0] = x;
81  s->y[2] = s->y[1];
82  s->y[1] = s->y[0];
83  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];
84  return s->y[0];
85 }
86 
87 #endif /* AVCODEC_OPUSENC_UTILS_H */
static int bessel_reinit(FFBesselFilter *s, float n, float f0, float fs, int highpass)
Definition: opusenc_utils.h:35
static float bessel_filter(FFBesselFilter *s, float x)
Definition: opusenc_utils.h:76
#define fs(width, name, subs,...)
Definition: cbs_vp9.c:259
#define f(width, name)
Definition: cbs_vp9.c:255
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
#define s(width, name)
Definition: cbs_vp9.c:257
int n
Definition: avisynth_c.h:760
static int bessel_init(FFBesselFilter *s, float n, float f0, float fs, int highpass)
Definition: opusenc_utils.h:69
#define M_PI
Definition: mathematics.h:52