celt.h 7.9 KB

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