xref: /aosp_15_r20/external/libopus/dnn/common.h (revision a58d3d2adb790c104798cd88c8a3aff4fa8b82cc)
1 
2 
3 #ifndef COMMON_H
4 #define COMMON_H
5 
6 #include <stdlib.h>
7 #include <string.h>
8 #include <math.h>
9 #include "opus_defines.h"
10 
11 #define LOG256 5.5451774445f
log2_approx(float x)12 static OPUS_INLINE float log2_approx(float x)
13 {
14    int integer;
15    float frac;
16    union {
17       float f;
18       int i;
19    } in;
20    in.f = x;
21    integer = (in.i>>23)-127;
22    in.i -= integer<<23;
23    frac = in.f - 1.5f;
24    frac = -0.41445418f + frac*(0.95909232f
25           + frac*(-0.33951290f + frac*0.16541097f));
26    return 1+integer+frac;
27 }
28 
29 #define log_approx(x) (0.69315f*log2_approx(x))
30 
ulaw2lin(float u)31 static OPUS_INLINE float ulaw2lin(float u)
32 {
33     float s;
34     float scale_1 = 32768.f/255.f;
35     u = u - 128.f;
36     s = u >= 0.f ? 1.f : -1.f;
37     u = fabs(u);
38     return s*scale_1*(exp(u/128.*LOG256)-1);
39 }
40 
lin2ulaw(float x)41 static OPUS_INLINE int lin2ulaw(float x)
42 {
43     float u;
44     float scale = 255.f/32768.f;
45     int s = x >= 0 ? 1 : -1;
46     x = fabs(x);
47     u = (s*(128*log_approx(1+scale*x)/LOG256));
48     u = 128 + u;
49     if (u < 0) u = 0;
50     if (u > 255) u = 255;
51     return (int)floor(.5 + u);
52 }
53 
54 
55 
56 #endif
57