xref: /aosp_15_r20/external/libopus/dnn/fwgan.h (revision a58d3d2adb790c104798cd88c8a3aff4fa8b82cc)
1 /* Copyright (c) 2023 Amazon */
2 /*
3    Redistribution and use in source and binary forms, with or without
4    modification, are permitted provided that the following conditions
5    are met:
6 
7    - Redistributions of source code must retain the above copyright
8    notice, this list of conditions and the following disclaimer.
9 
10    - Redistributions in binary form must reproduce the above copyright
11    notice, this list of conditions and the following disclaimer in the
12    documentation and/or other materials provided with the distribution.
13 
14    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
18    OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26 
27 #ifndef FWGAN_H
28 #define FWGAN_H
29 
30 #include "freq.h"
31 #include "fwgan_data.h"
32 
33 #define FWGAN_CONT_SAMPLES 320
34 #define NB_SUBFRAMES 4
35 #define SUBFRAME_SIZE 40
36 #define FWGAN_FRAME_SIZE (NB_SUBFRAMES*SUBFRAME_SIZE)
37 #define CONT_PCM_INPUTS 320
38 #define MAX_CONT_SIZE CONT_NET_0_OUT_SIZE
39 #define FWGAN_GAMMA 0.92f
40 #define FWGAN_DEEMPHASIS 0.85f
41 
42 /* FIXME: Derive those from the model rather than hardcoding. */
43 #define FWC1_STATE_SIZE 512
44 #define FWC2_STATE_SIZE 512
45 #define FWC3_STATE_SIZE 256
46 #define FWC4_STATE_SIZE 256
47 #define FWC5_STATE_SIZE 128
48 #define FWC6_STATE_SIZE 128
49 #define FWC7_STATE_SIZE 80
50 
51 typedef struct {
52   FWGAN model;
53   int arch;
54   int cont_initialized;
55   float embed_phase[2];
56   float last_gain;
57   float last_lpc[LPC_ORDER];
58   float syn_mem[LPC_ORDER];
59   float preemph_mem;
60   float deemph_mem;
61   float pcm_buf[FWGAN_FRAME_SIZE];
62   float cont[CONT_NET_10_OUT_SIZE];
63   float cont_conv1_mem[FEAT_IN_CONV1_CONV_STATE_SIZE];
64   float rnn_state[RNN_GRU_STATE_SIZE];
65   float fwc1_state[FWC1_STATE_SIZE];
66   float fwc2_state[FWC2_STATE_SIZE];
67   float fwc3_state[FWC3_STATE_SIZE];
68   float fwc4_state[FWC4_STATE_SIZE];
69   float fwc5_state[FWC5_STATE_SIZE];
70   float fwc6_state[FWC6_STATE_SIZE];
71   float fwc7_state[FWC7_STATE_SIZE];
72 } FWGANState;
73 
74 void fwgan_init(FWGANState *st);
75 int fwgan_load_model(FWGANState *st, const unsigned char *data, int len);
76 
77 void fwgan_cont(FWGANState *st, const float *pcm0, const float *features0);
78 
79 void fwgan_synthesize(FWGANState *st, float *pcm, const float *features);
80 void fwgan_synthesize_int(FWGANState *st, opus_int16 *pcm, const float *features);
81 
82 
83 #endif /* FWGAN_H */
84