celt.h (8947B)
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 #include "kiss_fft.h" 45 46 #ifdef ENABLE_QEXT 47 #define ARG_QEXT(arg) , arg 48 #else 49 #define ARG_QEXT(arg) 50 #endif 51 52 53 #ifdef ENABLE_DEEP_PLC 54 #include "lpcnet.h" 55 #endif 56 57 #ifdef __cplusplus 58 extern "C" { 59 #endif 60 61 #define CELTEncoder OpusCustomEncoder 62 #define CELTDecoder OpusCustomDecoder 63 #define CELTMode OpusCustomMode 64 65 #define QEXT_EXTENSION_ID 124 66 67 #define LEAK_BANDS 19 68 69 typedef struct { 70 int valid; 71 float tonality; 72 float tonality_slope; 73 float noisiness; 74 float activity; 75 float music_prob; 76 float music_prob_min; 77 float music_prob_max; 78 int bandwidth; 79 float activity_probability; 80 float max_pitch_ratio; 81 /* Store as Q6 char to save space. */ 82 unsigned char leak_boost[LEAK_BANDS]; 83 } AnalysisInfo; 84 85 typedef struct { 86 int signalType; 87 int offset; 88 } SILKInfo; 89 90 #define celt_check_mode_ptr_ptr(ptr) ((ptr) + ((ptr) - (const CELTMode**)(ptr))) 91 92 #define celt_check_analysis_ptr(ptr) ((ptr) + ((ptr) - (const AnalysisInfo*)(ptr))) 93 94 #define celt_check_silkinfo_ptr(ptr) ((ptr) + ((ptr) - (const SILKInfo*)(ptr))) 95 96 #define celt_check_glog_ptr(ptr) ((ptr) + ((ptr) - (celt_glog*)(ptr))) 97 98 /* Encoder/decoder Requests */ 99 100 101 #define CELT_SET_PREDICTION_REQUEST 10002 102 /** Controls the use of interframe prediction. 103 0=Independent frames 104 1=Short term interframe prediction allowed 105 2=Long term prediction allowed 106 */ 107 #define CELT_SET_PREDICTION(x) CELT_SET_PREDICTION_REQUEST, opus_check_int(x) 108 109 #define CELT_SET_INPUT_CLIPPING_REQUEST 10004 110 #define CELT_SET_INPUT_CLIPPING(x) CELT_SET_INPUT_CLIPPING_REQUEST, opus_check_int(x) 111 112 #define CELT_GET_AND_CLEAR_ERROR_REQUEST 10007 113 #define CELT_GET_AND_CLEAR_ERROR(x) CELT_GET_AND_CLEAR_ERROR_REQUEST, opus_check_int_ptr(x) 114 115 #define CELT_SET_CHANNELS_REQUEST 10008 116 #define CELT_SET_CHANNELS(x) CELT_SET_CHANNELS_REQUEST, opus_check_int(x) 117 118 119 /* Internal */ 120 #define CELT_SET_START_BAND_REQUEST 10010 121 #define CELT_SET_START_BAND(x) CELT_SET_START_BAND_REQUEST, opus_check_int(x) 122 123 #define CELT_SET_END_BAND_REQUEST 10012 124 #define CELT_SET_END_BAND(x) CELT_SET_END_BAND_REQUEST, opus_check_int(x) 125 126 #define CELT_GET_MODE_REQUEST 10015 127 /** Get the CELTMode used by an encoder or decoder */ 128 #define CELT_GET_MODE(x) CELT_GET_MODE_REQUEST, celt_check_mode_ptr_ptr(x) 129 130 #define CELT_SET_SIGNALLING_REQUEST 10016 131 #define CELT_SET_SIGNALLING(x) CELT_SET_SIGNALLING_REQUEST, opus_check_int(x) 132 133 #define CELT_SET_TONALITY_REQUEST 10018 134 #define CELT_SET_TONALITY(x) CELT_SET_TONALITY_REQUEST, opus_check_int(x) 135 #define CELT_SET_TONALITY_SLOPE_REQUEST 10020 136 #define CELT_SET_TONALITY_SLOPE(x) CELT_SET_TONALITY_SLOPE_REQUEST, opus_check_int(x) 137 138 #define CELT_SET_ANALYSIS_REQUEST 10022 139 #define CELT_SET_ANALYSIS(x) CELT_SET_ANALYSIS_REQUEST, celt_check_analysis_ptr(x) 140 141 #define OPUS_SET_LFE_REQUEST 10024 142 #define OPUS_SET_LFE(x) OPUS_SET_LFE_REQUEST, opus_check_int(x) 143 144 #define OPUS_SET_ENERGY_MASK_REQUEST 10026 145 #define OPUS_SET_ENERGY_MASK(x) OPUS_SET_ENERGY_MASK_REQUEST, celt_check_glog_ptr(x) 146 147 #define CELT_SET_SILK_INFO_REQUEST 10028 148 #define CELT_SET_SILK_INFO(x) CELT_SET_SILK_INFO_REQUEST, celt_check_silkinfo_ptr(x) 149 150 151 static OPUS_INLINE opus_int32 bits_to_bitrate(opus_int32 bits, opus_int32 Fs, opus_int32 frame_size) { 152 return bits*(6*Fs/frame_size)/6; 153 } 154 155 static OPUS_INLINE opus_int32 bitrate_to_bits(opus_int32 bitrate, opus_int32 Fs, opus_int32 frame_size) { 156 return bitrate*6/(6*Fs/frame_size); 157 } 158 159 /* Encoder stuff */ 160 161 int celt_encoder_get_size(int channels); 162 163 int celt_encode_with_ec(OpusCustomEncoder * OPUS_RESTRICT st, const opus_res * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc); 164 165 int celt_encoder_init(CELTEncoder *st, opus_int32 sampling_rate, int channels, 166 int arch); 167 168 169 170 /* Decoder stuff */ 171 172 int celt_decoder_get_size(int channels); 173 174 175 int celt_decoder_init(CELTDecoder *st, opus_int32 sampling_rate, int channels); 176 177 int celt_decode_with_ec_dred(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, 178 int len, opus_res * OPUS_RESTRICT pcm, int frame_size, ec_dec *dec, int accum 179 #ifdef ENABLE_DEEP_PLC 180 ,LPCNetPLCState *lpcnet 181 #endif 182 ARG_QEXT(const unsigned char *qext_payload) ARG_QEXT(int qext_payload_len) 183 ); 184 185 int celt_decode_with_ec(OpusCustomDecoder * OPUS_RESTRICT st, const unsigned char *data, 186 int len, opus_res * OPUS_RESTRICT pcm, int frame_size, ec_dec *dec, int accum); 187 188 #define celt_encoder_ctl opus_custom_encoder_ctl 189 #define celt_decoder_ctl opus_custom_decoder_ctl 190 191 192 #if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API) 193 #define OPUS_CUSTOM_NOSTATIC 194 #else 195 #define OPUS_CUSTOM_NOSTATIC static OPUS_INLINE 196 #endif 197 198 static const unsigned char trim_icdf[11] = {126, 124, 119, 109, 87, 41, 19, 9, 4, 2, 0}; 199 /* Probs: NONE: 21.875%, LIGHT: 6.25%, NORMAL: 65.625%, AGGRESSIVE: 6.25% */ 200 static const unsigned char spread_icdf[4] = {25, 23, 2, 0}; 201 202 static const unsigned char tapset_icdf[3]={2,1,0}; 203 204 #if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API) 205 static const unsigned char toOpusTable[20] = { 206 0xE0, 0xE8, 0xF0, 0xF8, 207 0xC0, 0xC8, 0xD0, 0xD8, 208 0xA0, 0xA8, 0xB0, 0xB8, 209 0x00, 0x00, 0x00, 0x00, 210 0x80, 0x88, 0x90, 0x98, 211 }; 212 213 static const unsigned char fromOpusTable[16] = { 214 0x80, 0x88, 0x90, 0x98, 215 0x40, 0x48, 0x50, 0x58, 216 0x20, 0x28, 0x30, 0x38, 217 0x00, 0x08, 0x10, 0x18 218 }; 219 220 static OPUS_INLINE int toOpus(unsigned char c) 221 { 222 int ret=0; 223 if (c<0xA0) 224 ret = toOpusTable[c>>3]; 225 if (ret == 0) 226 return -1; 227 else 228 return ret|(c&0x7); 229 } 230 231 static OPUS_INLINE int fromOpus(unsigned char c) 232 { 233 if (c<0x80) 234 return -1; 235 else 236 return fromOpusTable[(c>>3)-16] | (c&0x7); 237 } 238 #endif /* CUSTOM_MODES */ 239 240 #define COMBFILTER_MAXPERIOD 1024 241 #define COMBFILTER_MINPERIOD 15 242 243 extern const signed char tf_select_table[4][8]; 244 245 #if defined(ENABLE_HARDENING) || defined(ENABLE_ASSERTIONS) 246 void validate_celt_decoder(CELTDecoder *st); 247 #define VALIDATE_CELT_DECODER(st) validate_celt_decoder(st) 248 #else 249 #define VALIDATE_CELT_DECODER(st) 250 #endif 251 252 int resampling_factor(opus_int32 rate); 253 254 void celt_preemphasis(const opus_res * OPUS_RESTRICT pcmp, celt_sig * OPUS_RESTRICT inp, 255 int N, int CC, int upsample, const opus_val16 *coef, celt_sig *mem, int clip); 256 257 void comb_filter(opus_val32 *y, opus_val32 *x, int T0, int T1, int N, 258 opus_val16 g0, opus_val16 g1, int tapset0, int tapset1, 259 const celt_coef *window, int overlap, int arch); 260 261 void init_caps(const CELTMode *m,int *cap,int LM,int C); 262 263 #ifdef RESYNTH 264 void deemphasis(celt_sig *in[], opus_res *pcm, int N, int C, int downsample, const opus_val16 *coef, celt_sig *mem, int accum); 265 void celt_synthesis(const CELTMode *mode, celt_norm *X, celt_sig * out_syn[], 266 celt_glog *oldBandE, int start, int effEnd, int C, int CC, int isTransient, 267 int LM, int downsample, int silence, int arch ARG_QEXT(const CELTMode *qext_mode) ARG_QEXT(const celt_glog *qext_bandLogE) ARG_QEXT(int qext_end)); 268 #endif 269 270 #ifdef ENABLE_QEXT 271 #define QEXT_SCALE(x) ((qext_scale)*(x)) 272 #define QEXT_SCALE2(x, qext_scale) ((qext_scale)*(x)) 273 #else 274 #define QEXT_SCALE(x) (x) 275 #define QEXT_SCALE2(x, qext_scale) (x) 276 #endif 277 278 #ifdef __cplusplus 279 } 280 #endif 281 282 #endif /* CELT_H */