1 /* Copyright (c) 2007-2008 CSIRO
2 Copyright (c) 2007-2009 Xiph.Org Foundation
3 Copyright (c) 2008 Gregory Maxwell
4 Written by Jean-Marc Valin and Gregory Maxwell */
5 /**
6 @file celt.h
7 @brief Contains all the functions for encoding and decoding audio
8 */
9
10 /*
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions
13 are met:
14
15 - Redistributions of source code must retain the above copyright
16 notice, this list of conditions and the following disclaimer.
17
18 - Redistributions in binary form must reproduce the above copyright
19 notice, this list of conditions and the following disclaimer in the
20 documentation and/or other materials provided with the distribution.
21
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
26 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #ifndef CELT_H
36 #define CELT_H
37
38 #include "opus_types.h"
39 #include "opus_defines.h"
40 #include "opus_custom.h"
41 #include "entenc.h"
42 #include "entdec.h"
43 #include "arch.h"
44
45 #ifdef ENABLE_DEEP_PLC
46 #include "lpcnet.h"
47 #endif
48
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52
53 #define CELTEncoder OpusCustomEncoder
54 #define CELTDecoder OpusCustomDecoder
55 #define CELTMode OpusCustomMode
56
57 #define LEAK_BANDS 19
58
59 typedef struct {
60 int valid;
61 float tonality;
62 float tonality_slope;
63 float noisiness;
64 float activity;
65 float music_prob;
66 float music_prob_min;
67 float music_prob_max;
68 int bandwidth;
69 float activity_probability;
70 float max_pitch_ratio;
71 /* Store as Q6 char to save space. */
72 unsigned char leak_boost[LEAK_BANDS];
73 } AnalysisInfo;
74
75 typedef struct {
76 int signalType;
77 int offset;
78 } SILKInfo;
79
80 #define __celt_check_mode_ptr_ptr(ptr) ((ptr) + ((ptr) - (const CELTMode**)(ptr)))
81
82 #define __celt_check_analysis_ptr(ptr) ((ptr) + ((ptr) - (const AnalysisInfo*)(ptr)))
83
84 #define __celt_check_silkinfo_ptr(ptr) ((ptr) + ((ptr) - (const SILKInfo*)(ptr)))
85
86 /* Encoder/decoder Requests */
87
88
89 #define CELT_SET_PREDICTION_REQUEST 10002
90 /** Controls the use of interframe prediction.
91 0=Independent frames
92 1=Short term interframe prediction allowed
93 2=Long term prediction allowed
94 */
95 #define CELT_SET_PREDICTION(x) CELT_SET_PREDICTION_REQUEST, __opus_check_int(x)
96
97 #define CELT_SET_INPUT_CLIPPING_REQUEST 10004
98 #define CELT_SET_INPUT_CLIPPING(x) CELT_SET_INPUT_CLIPPING_REQUEST, __opus_check_int(x)
99
100 #define CELT_GET_AND_CLEAR_ERROR_REQUEST 10007
101 #define CELT_GET_AND_CLEAR_ERROR(x) CELT_GET_AND_CLEAR_ERROR_REQUEST, __opus_check_int_ptr(x)
102
103 #define CELT_SET_CHANNELS_REQUEST 10008
104 #define CELT_SET_CHANNELS(x) CELT_SET_CHANNELS_REQUEST, __opus_check_int(x)
105
106
107 /* Internal */
108 #define CELT_SET_START_BAND_REQUEST 10010
109 #define CELT_SET_START_BAND(x) CELT_SET_START_BAND_REQUEST, __opus_check_int(x)
110
111 #define CELT_SET_END_BAND_REQUEST 10012
112 #define CELT_SET_END_BAND(x) CELT_SET_END_BAND_REQUEST, __opus_check_int(x)
113
114 #define CELT_GET_MODE_REQUEST 10015
115 /** Get the CELTMode used by an encoder or decoder */
116 #define CELT_GET_MODE(x) CELT_GET_MODE_REQUEST, __celt_check_mode_ptr_ptr(x)
117
118 #define CELT_SET_SIGNALLING_REQUEST 10016
119 #define CELT_SET_SIGNALLING(x) CELT_SET_SIGNALLING_REQUEST, __opus_check_int(x)
120
121 #define CELT_SET_TONALITY_REQUEST 10018
122 #define CELT_SET_TONALITY(x) CELT_SET_TONALITY_REQUEST, __opus_check_int(x)
123 #define CELT_SET_TONALITY_SLOPE_REQUEST 10020
124 #define CELT_SET_TONALITY_SLOPE(x) CELT_SET_TONALITY_SLOPE_REQUEST, __opus_check_int(x)
125
126 #define CELT_SET_ANALYSIS_REQUEST 10022
127 #define CELT_SET_ANALYSIS(x) CELT_SET_ANALYSIS_REQUEST, __celt_check_analysis_ptr(x)
128
129 #define OPUS_SET_LFE_REQUEST 10024
130 #define OPUS_SET_LFE(x) OPUS_SET_LFE_REQUEST, __opus_check_int(x)
131
132 #define OPUS_SET_ENERGY_MASK_REQUEST 10026
133 #define OPUS_SET_ENERGY_MASK(x) OPUS_SET_ENERGY_MASK_REQUEST, __opus_check_val16_ptr(x)
134
135 #define CELT_SET_SILK_INFO_REQUEST 10028
136 #define CELT_SET_SILK_INFO(x) CELT_SET_SILK_INFO_REQUEST, __celt_check_silkinfo_ptr(x)
137
138 /* Encoder stuff */
139
140 int celt_encoder_get_size(int channels);
141
142 int celt_encode_with_ec(OpusCustomEncoder * OPUS_RESTRICT st, const opus_val16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc);
143
144 int celt_encoder_init(CELTEncoder *st, opus_int32 sampling_rate, int channels,
145 int arch);
146
147
148
149 /* Decoder stuff */
150
151 int celt_decoder_get_size(int channels);
152
153
154 int celt_decoder_init(CELTDecoder *st, opus_int32 sampling_rate, int channels);
155
156 int celt_decode_with_ec_dred(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data,
157 int len, opus_val16 * OPUS_RESTRICT pcm, int frame_size, ec_dec *dec, int accum
158 #ifdef ENABLE_DEEP_PLC
159 ,LPCNetPLCState *lpcnet
160 #endif
161 );
162
163 int celt_decode_with_ec(OpusCustomDecoder * OPUS_RESTRICT st, const unsigned char *data,
164 int len, opus_val16 * OPUS_RESTRICT pcm, int frame_size, ec_dec *dec, int accum);
165
166 #define celt_encoder_ctl opus_custom_encoder_ctl
167 #define celt_decoder_ctl opus_custom_decoder_ctl
168
169
170 #ifdef CUSTOM_MODES
171 #define OPUS_CUSTOM_NOSTATIC
172 #else
173 #define OPUS_CUSTOM_NOSTATIC static OPUS_INLINE
174 #endif
175
176 static const unsigned char trim_icdf[11] = {126, 124, 119, 109, 87, 41, 19, 9, 4, 2, 0};
177 /* Probs: NONE: 21.875%, LIGHT: 6.25%, NORMAL: 65.625%, AGGRESSIVE: 6.25% */
178 static const unsigned char spread_icdf[4] = {25, 23, 2, 0};
179
180 static const unsigned char tapset_icdf[3]={2,1,0};
181
182 #ifdef CUSTOM_MODES
183 static const unsigned char toOpusTable[20] = {
184 0xE0, 0xE8, 0xF0, 0xF8,
185 0xC0, 0xC8, 0xD0, 0xD8,
186 0xA0, 0xA8, 0xB0, 0xB8,
187 0x00, 0x00, 0x00, 0x00,
188 0x80, 0x88, 0x90, 0x98,
189 };
190
191 static const unsigned char fromOpusTable[16] = {
192 0x80, 0x88, 0x90, 0x98,
193 0x40, 0x48, 0x50, 0x58,
194 0x20, 0x28, 0x30, 0x38,
195 0x00, 0x08, 0x10, 0x18
196 };
197
toOpus(unsigned char c)198 static OPUS_INLINE int toOpus(unsigned char c)
199 {
200 int ret=0;
201 if (c<0xA0)
202 ret = toOpusTable[c>>3];
203 if (ret == 0)
204 return -1;
205 else
206 return ret|(c&0x7);
207 }
208
fromOpus(unsigned char c)209 static OPUS_INLINE int fromOpus(unsigned char c)
210 {
211 if (c<0x80)
212 return -1;
213 else
214 return fromOpusTable[(c>>3)-16] | (c&0x7);
215 }
216 #endif /* CUSTOM_MODES */
217
218 #define COMBFILTER_MAXPERIOD 1024
219 #define COMBFILTER_MINPERIOD 15
220
221 extern const signed char tf_select_table[4][8];
222
223 #if defined(ENABLE_HARDENING) || defined(ENABLE_ASSERTIONS)
224 void validate_celt_decoder(CELTDecoder *st);
225 #define VALIDATE_CELT_DECODER(st) validate_celt_decoder(st)
226 #else
227 #define VALIDATE_CELT_DECODER(st)
228 #endif
229
230 int resampling_factor(opus_int32 rate);
231
232 void celt_preemphasis(const opus_val16 * OPUS_RESTRICT pcmp, celt_sig * OPUS_RESTRICT inp,
233 int N, int CC, int upsample, const opus_val16 *coef, celt_sig *mem, int clip);
234
235 void comb_filter(opus_val32 *y, opus_val32 *x, int T0, int T1, int N,
236 opus_val16 g0, opus_val16 g1, int tapset0, int tapset1,
237 const opus_val16 *window, int overlap, int arch);
238
239 void init_caps(const CELTMode *m,int *cap,int LM,int C);
240
241 #ifdef RESYNTH
242 void deemphasis(celt_sig *in[], opus_val16 *pcm, int N, int C, int downsample, const opus_val16 *coef, celt_sig *mem, int accum);
243 void celt_synthesis(const CELTMode *mode, celt_norm *X, celt_sig * out_syn[],
244 opus_val16 *oldBandE, int start, int effEnd, int C, int CC, int isTransient,
245 int LM, int downsample, int silence, int arch);
246 #endif
247
248 #ifdef __cplusplus
249 }
250 #endif
251
252 #endif /* CELT_H */
253