xref: /aosp_15_r20/external/libopus/dnn/nndsp.h (revision a58d3d2adb790c104798cd88c8a3aff4fa8b82cc)
1 /* Copyright (c) 2023 Amazon
2    Written by Jan Buethe */
3 /*
4    Redistribution and use in source and binary forms, with or without
5    modification, are permitted provided that the following conditions
6    are met:
7 
8    - Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10 
11    - Redistributions in binary form must reproduce the above copyright
12    notice, this list of conditions and the following disclaimer in the
13    documentation and/or other materials provided with the distribution.
14 
15    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19    OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #ifndef NNDSP_H
29 #define NNDSP_H
30 
31 #include "opus_types.h"
32 #include "nnet.h"
33 #include <string.h>
34 
35 
36 #define ADACONV_MAX_KERNEL_SIZE 16
37 #define ADACONV_MAX_INPUT_CHANNELS 2
38 #define ADACONV_MAX_OUTPUT_CHANNELS 2
39 #define ADACONV_MAX_FRAME_SIZE 80
40 #define ADACONV_MAX_OVERLAP_SIZE 40
41 
42 #define ADACOMB_MAX_LAG 300
43 #define ADACOMB_MAX_KERNEL_SIZE 16
44 #define ADACOMB_MAX_FRAME_SIZE 80
45 #define ADACOMB_MAX_OVERLAP_SIZE 40
46 
47 #define ADASHAPE_MAX_INPUT_DIM 512
48 #define ADASHAPE_MAX_FRAME_SIZE 160
49 
50 /*#define DEBUG_NNDSP*/
51 #ifdef DEBUG_NNDSP
52 #include <stdio.h>
53 #endif
54 
55 
56 void print_float_vector(const char* name, const float *vec, int length);
57 
58 typedef struct {
59     float history[ADACONV_MAX_KERNEL_SIZE * ADACONV_MAX_INPUT_CHANNELS];
60     float last_kernel[ADACONV_MAX_KERNEL_SIZE * ADACONV_MAX_INPUT_CHANNELS * ADACONV_MAX_OUTPUT_CHANNELS];
61     float last_gain;
62 } AdaConvState;
63 
64 
65 typedef struct {
66     float history[ADACOMB_MAX_KERNEL_SIZE + ADACOMB_MAX_LAG];
67     float last_kernel[ADACOMB_MAX_KERNEL_SIZE];
68     float last_global_gain;
69     int last_pitch_lag;
70 } AdaCombState;
71 
72 
73 typedef struct {
74     float conv_alpha1f_state[ADASHAPE_MAX_INPUT_DIM];
75     float conv_alpha1t_state[ADASHAPE_MAX_INPUT_DIM];
76     float conv_alpha2_state[ADASHAPE_MAX_FRAME_SIZE];
77 } AdaShapeState;
78 
79 void init_adaconv_state(AdaConvState *hAdaConv);
80 
81 void init_adacomb_state(AdaCombState *hAdaComb);
82 
83 void init_adashape_state(AdaShapeState *hAdaShape);
84 
85 void compute_overlap_window(float *window, int overlap_size);
86 
87 void adaconv_process_frame(
88     AdaConvState* hAdaConv,
89     float *x_out,
90     const float *x_in,
91     const float *features,
92     const LinearLayer *kernel_layer,
93     const LinearLayer *gain_layer,
94     int feature_dim, /* not strictly necessary */
95     int frame_size,
96     int overlap_size,
97     int in_channels,
98     int out_channels,
99     int kernel_size,
100     int left_padding,
101     float filter_gain_a,
102     float filter_gain_b,
103     float shape_gain,
104     float *window,
105     int arch
106 );
107 
108 void adacomb_process_frame(
109     AdaCombState* hAdaComb,
110     float *x_out,
111     const float *x_in,
112     const float *features,
113     const LinearLayer *kernel_layer,
114     const LinearLayer *gain_layer,
115     const LinearLayer *global_gain_layer,
116     int pitch_lag,
117     int feature_dim,
118     int frame_size,
119     int overlap_size,
120     int kernel_size,
121     int left_padding,
122     float filter_gain_a,
123     float filter_gain_b,
124     float log_gain_limit,
125     float *window,
126     int arch
127 );
128 
129 void adashape_process_frame(
130     AdaShapeState *hAdaShape,
131     float *x_out,
132     const float *x_in,
133     const float *features,
134     const LinearLayer *alpha1f,
135     const LinearLayer *alpha1t,
136     const LinearLayer *alpha2,
137     int feature_dim,
138     int frame_size,
139     int avg_pool_k,
140     int arch
141 );
142 
143 #endif
144