xref: /aosp_15_r20/external/libopus/dnn/lpcnet.h (revision a58d3d2adb790c104798cd88c8a3aff4fa8b82cc)
1 /* Copyright (c) 2018 Mozilla */
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 FOUNDATION OR
18    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 LPCNET_H_
28 #define LPCNET_H_
29 
30 #include "opus_types.h"
31 
32 #define NB_FEATURES 20
33 #define NB_TOTAL_FEATURES 36
34 
35 /** Number of audio samples in a feature frame (not for encoding/decoding). */
36 #define LPCNET_FRAME_SIZE (160)
37 
38 typedef struct LPCNetState LPCNetState;
39 
40 typedef struct LPCNetDecState LPCNetDecState;
41 
42 typedef struct LPCNetEncState LPCNetEncState;
43 
44 typedef struct LPCNetPLCState LPCNetPLCState;
45 
46 
47 /** Gets the size of an <code>LPCNetDecState</code> structure.
48   * @returns The size in bytes.
49   */
50 int lpcnet_decoder_get_size(void);
51 
52 /** Initializes a previously allocated decoder state
53   * The memory pointed to by st must be at least the size returned by lpcnet_decoder_get_size().
54   * This is intended for applications which use their own allocator instead of malloc.
55   * @see lpcnet_decoder_create(),lpcnet_decoder_get_size()
56   * @param [in] st <tt>LPCNetDecState*</tt>: Decoder state
57   * @retval 0 Success
58   */
59 int lpcnet_decoder_init(LPCNetDecState *st);
60 
61 void lpcnet_reset(LPCNetState *lpcnet);
62 
63 /** Allocates and initializes a decoder state.
64   *  @returns The newly created state
65   */
66 LPCNetDecState *lpcnet_decoder_create(void);
67 
68 /** Frees an <code>LPCNetDecState</code> allocated by lpcnet_decoder_create().
69   * @param[in] st <tt>LPCNetDecState*</tt>: State to be freed.
70   */
71 void lpcnet_decoder_destroy(LPCNetDecState *st);
72 
73 /** Decodes a packet of LPCNET_COMPRESSED_SIZE bytes (currently 8) into LPCNET_PACKET_SAMPLES samples (currently 640).
74   * @param [in] st <tt>LPCNetDecState*</tt>: Decoder state
75   * @param [in] buf <tt>const unsigned char *</tt>: Compressed packet
76   * @param [out] pcm <tt>opus_int16 *</tt>: Decoded audio
77   * @retval 0 Success
78   */
79 int lpcnet_decode(LPCNetDecState *st, const unsigned char *buf, opus_int16 *pcm);
80 
81 
82 
83 /** Gets the size of an <code>LPCNetEncState</code> structure.
84   * @returns The size in bytes.
85   */
86 int lpcnet_encoder_get_size(void);
87 
88 /** Initializes a previously allocated encoder state
89   * The memory pointed to by st must be at least the size returned by lpcnet_encoder_get_size().
90   * This is intended for applications which use their own allocator instead of malloc.
91   * @see lpcnet_encoder_create(),lpcnet_encoder_get_size()
92   * @param [in] st <tt>LPCNetEncState*</tt>: Encoder state
93   * @retval 0 Success
94   */
95 int lpcnet_encoder_init(LPCNetEncState *st);
96 
97 int lpcnet_encoder_load_model(LPCNetEncState *st, const void *data, int len);
98 
99 /** Allocates and initializes an encoder state.
100   *  @returns The newly created state
101   */
102 LPCNetEncState *lpcnet_encoder_create(void);
103 
104 /** Frees an <code>LPCNetEncState</code> allocated by lpcnet_encoder_create().
105   * @param[in] st <tt>LPCNetEncState*</tt>: State to be freed.
106   */
107 void lpcnet_encoder_destroy(LPCNetEncState *st);
108 
109 /** Encodes LPCNET_PACKET_SAMPLES speech samples (currently 640) into a packet of LPCNET_COMPRESSED_SIZE bytes (currently 8).
110   * @param [in] st <tt>LPCNetDecState*</tt>: Encoder state
111   * @param [in] pcm <tt>opus_int16 *</tt>: Input speech to be encoded
112   * @param [out] buf <tt>const unsigned char *</tt>: Compressed packet
113   * @retval 0 Success
114   */
115 int lpcnet_encode(LPCNetEncState *st, const opus_int16 *pcm, unsigned char *buf);
116 
117 /** Compute features on LPCNET_FRAME_SIZE speech samples (currently 160) and output features for one 10-ms frame.
118   * @param [in] st <tt>LPCNetDecState*</tt>: Encoder state
119   * @param [in] pcm <tt>opus_int16 *</tt>: Input speech to be analyzed
120   * @param [out] features <tt>float[NB_TOTAL_FEATURES]</tt>: Four feature vectors
121   * @retval 0 Success
122   */
123 int lpcnet_compute_single_frame_features(LPCNetEncState *st, const opus_int16 *pcm, float features[NB_TOTAL_FEATURES], int arch);
124 
125 
126 /** Compute features on LPCNET_FRAME_SIZE speech samples (currently 160) and output features for one 10-ms frame.
127   * @param [in] st <tt>LPCNetDecState*</tt>: Encoder state
128   * @param [in] pcm <tt>float *</tt>: Input speech to be analyzed
129   * @param [out] features <tt>float[NB_TOTAL_FEATURES]</tt>: Four feature vectors
130   * @retval 0 Success
131   */
132 int lpcnet_compute_single_frame_features_float(LPCNetEncState *st, const float *pcm, float features[NB_TOTAL_FEATURES], int arch);
133 
134 /** Gets the size of an <code>LPCNetState</code> structure.
135   * @returns The size in bytes.
136   */
137 int lpcnet_get_size(void);
138 
139 /** Initializes a previously allocated synthesis state
140   * The memory pointed to by st must be at least the size returned by lpcnet_get_size().
141   * This is intended for applications which use their own allocator instead of malloc.
142   * @see lpcnet_create(),lpcnet_get_size()
143   * @param [in] st <tt>LPCNetState*</tt>: Synthesis state
144   * @retval 0 Success
145   */
146 int lpcnet_init(LPCNetState *st);
147 
148 /** Allocates and initializes a synthesis state.
149   *  @returns The newly created state
150   */
151 LPCNetState *lpcnet_create(void);
152 
153 /** Frees an <code>LPCNetState</code> allocated by lpcnet_create().
154   * @param[in] st <tt>LPCNetState*</tt>: State to be freed.
155   */
156 void lpcnet_destroy(LPCNetState *st);
157 
158 /** Synthesizes speech from an LPCNet feature vector.
159   * @param [in] st <tt>LPCNetState*</tt>: Synthesis state
160   * @param [in] features <tt>const float *</tt>: Compressed packet
161   * @param [out] output <tt>opus_int16 **</tt>: Synthesized speech
162   * @param [in] N <tt>int</tt>: Number of samples to generate
163   * @retval 0 Success
164   */
165 void lpcnet_synthesize(LPCNetState *st, const float *features, opus_int16 *output, int N);
166 
167 
168 
169 int lpcnet_plc_init(LPCNetPLCState *st);
170 void lpcnet_plc_reset(LPCNetPLCState *st);
171 
172 int lpcnet_plc_update(LPCNetPLCState *st, opus_int16 *pcm);
173 
174 int lpcnet_plc_conceal(LPCNetPLCState *st, opus_int16 *pcm);
175 
176 void lpcnet_plc_fec_add(LPCNetPLCState *st, const float *features);
177 
178 void lpcnet_plc_fec_clear(LPCNetPLCState *st);
179 
180 int lpcnet_load_model(LPCNetState *st, const void *data, int len);
181 int lpcnet_plc_load_model(LPCNetPLCState *st, const void *data, int len);
182 
183 #endif
184