celt.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. typedef struct {
  45. int valid;
  46. float tonality;
  47. float tonality_slope;
  48. float noisiness;
  49. float activity;
  50. float music_prob;
  51. int bandwidth;
  52. float activity_probability;
  53. } AnalysisInfo;
  54. typedef struct {
  55. int signalType;
  56. int offset;
  57. } SILKInfo;
  58. #define __celt_check_mode_ptr_ptr(ptr) ((ptr) + ((ptr) - (const CELTMode**)(ptr)))
  59. #define __celt_check_analysis_ptr(ptr) ((ptr) + ((ptr) - (const AnalysisInfo*)(ptr)))
  60. #define __celt_check_silkinfo_ptr(ptr) ((ptr) + ((ptr) - (const SILKInfo*)(ptr)))
  61. /* Encoder/decoder Requests */
  62. /* Expose this option again when variable framesize actually works */
  63. #define OPUS_FRAMESIZE_VARIABLE 5010 /**< Optimize the frame size dynamically */
  64. #define CELT_SET_PREDICTION_REQUEST 10002
  65. /** Controls the use of interframe prediction.
  66. 0=Independent frames
  67. 1=Short term interframe prediction allowed
  68. 2=Long term prediction allowed
  69. */
  70. #define CELT_SET_PREDICTION(x) CELT_SET_PREDICTION_REQUEST, __opus_check_int(x)
  71. #define CELT_SET_INPUT_CLIPPING_REQUEST 10004
  72. #define CELT_SET_INPUT_CLIPPING(x) CELT_SET_INPUT_CLIPPING_REQUEST, __opus_check_int(x)
  73. #define CELT_GET_AND_CLEAR_ERROR_REQUEST 10007
  74. #define CELT_GET_AND_CLEAR_ERROR(x) CELT_GET_AND_CLEAR_ERROR_REQUEST, __opus_check_int_ptr(x)
  75. #define CELT_SET_CHANNELS_REQUEST 10008
  76. #define CELT_SET_CHANNELS(x) CELT_SET_CHANNELS_REQUEST, __opus_check_int(x)
  77. /* Internal */
  78. #define CELT_SET_START_BAND_REQUEST 10010
  79. #define CELT_SET_START_BAND(x) CELT_SET_START_BAND_REQUEST, __opus_check_int(x)
  80. #define CELT_SET_END_BAND_REQUEST 10012
  81. #define CELT_SET_END_BAND(x) CELT_SET_END_BAND_REQUEST, __opus_check_int(x)
  82. #define CELT_GET_MODE_REQUEST 10015
  83. /** Get the CELTMode used by an encoder or decoder */
  84. #define CELT_GET_MODE(x) CELT_GET_MODE_REQUEST, __celt_check_mode_ptr_ptr(x)
  85. #define CELT_SET_SIGNALLING_REQUEST 10016
  86. #define CELT_SET_SIGNALLING(x) CELT_SET_SIGNALLING_REQUEST, __opus_check_int(x)
  87. #define CELT_SET_TONALITY_REQUEST 10018
  88. #define CELT_SET_TONALITY(x) CELT_SET_TONALITY_REQUEST, __opus_check_int(x)
  89. #define CELT_SET_TONALITY_SLOPE_REQUEST 10020
  90. #define CELT_SET_TONALITY_SLOPE(x) CELT_SET_TONALITY_SLOPE_REQUEST, __opus_check_int(x)
  91. #define CELT_SET_ANALYSIS_REQUEST 10022
  92. #define CELT_SET_ANALYSIS(x) CELT_SET_ANALYSIS_REQUEST, __celt_check_analysis_ptr(x)
  93. #define OPUS_SET_LFE_REQUEST 10024
  94. #define OPUS_SET_LFE(x) OPUS_SET_LFE_REQUEST, __opus_check_int(x)
  95. #define OPUS_SET_ENERGY_MASK_REQUEST 10026
  96. #define OPUS_SET_ENERGY_MASK(x) OPUS_SET_ENERGY_MASK_REQUEST, __opus_check_val16_ptr(x)
  97. #define CELT_SET_SILK_INFO_REQUEST 10028
  98. #define CELT_SET_SILK_INFO(x) CELT_SET_SILK_INFO_REQUEST, __celt_check_silkinfo_ptr(x)
  99. /* Encoder stuff */
  100. int celt_encoder_get_size(int channels);
  101. int celt_encode_with_ec(OpusCustomEncoder * OPUS_RESTRICT st, const opus_val16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc);
  102. int celt_encoder_init(CELTEncoder *st, opus_int32 sampling_rate, int channels,
  103. int arch);
  104. /* Decoder stuff */
  105. int celt_decoder_get_size(int channels);
  106. int celt_decoder_init(CELTDecoder *st, opus_int32 sampling_rate, int channels);
  107. int celt_decode_with_ec(OpusCustomDecoder * OPUS_RESTRICT st, const unsigned char *data,
  108. int len, opus_val16 * OPUS_RESTRICT pcm, int frame_size, ec_dec *dec, int accum);
  109. #define celt_encoder_ctl opus_custom_encoder_ctl
  110. #define celt_decoder_ctl opus_custom_decoder_ctl
  111. #ifdef CUSTOM_MODES
  112. #define OPUS_CUSTOM_NOSTATIC
  113. #else
  114. #define OPUS_CUSTOM_NOSTATIC static OPUS_INLINE
  115. #endif
  116. static const unsigned char trim_icdf[11] = {126, 124, 119, 109, 87, 41, 19, 9, 4, 2, 0};
  117. /* Probs: NONE: 21.875%, LIGHT: 6.25%, NORMAL: 65.625%, AGGRESSIVE: 6.25% */
  118. static const unsigned char spread_icdf[4] = {25, 23, 2, 0};
  119. static const unsigned char tapset_icdf[3]={2,1,0};
  120. #ifdef CUSTOM_MODES
  121. static const unsigned char toOpusTable[20] = {
  122. 0xE0, 0xE8, 0xF0, 0xF8,
  123. 0xC0, 0xC8, 0xD0, 0xD8,
  124. 0xA0, 0xA8, 0xB0, 0xB8,
  125. 0x00, 0x00, 0x00, 0x00,
  126. 0x80, 0x88, 0x90, 0x98,
  127. };
  128. static const unsigned char fromOpusTable[16] = {
  129. 0x80, 0x88, 0x90, 0x98,
  130. 0x40, 0x48, 0x50, 0x58,
  131. 0x20, 0x28, 0x30, 0x38,
  132. 0x00, 0x08, 0x10, 0x18
  133. };
  134. static OPUS_INLINE int toOpus(unsigned char c)
  135. {
  136. int ret=0;
  137. if (c<0xA0)
  138. ret = toOpusTable[c>>3];
  139. if (ret == 0)
  140. return -1;
  141. else
  142. return ret|(c&0x7);
  143. }
  144. static OPUS_INLINE int fromOpus(unsigned char c)
  145. {
  146. if (c<0x80)
  147. return -1;
  148. else
  149. return fromOpusTable[(c>>3)-16] | (c&0x7);
  150. }
  151. #endif /* CUSTOM_MODES */
  152. #define COMBFILTER_MAXPERIOD 1024
  153. #define COMBFILTER_MINPERIOD 15
  154. extern const signed char tf_select_table[4][8];
  155. int resampling_factor(opus_int32 rate);
  156. void celt_preemphasis(const opus_val16 * OPUS_RESTRICT pcmp, celt_sig * OPUS_RESTRICT inp,
  157. int N, int CC, int upsample, const opus_val16 *coef, celt_sig *mem, int clip);
  158. void comb_filter(opus_val32 *y, opus_val32 *x, int T0, int T1, int N,
  159. opus_val16 g0, opus_val16 g1, int tapset0, int tapset1,
  160. const opus_val16 *window, int overlap, int arch);
  161. #ifdef NON_STATIC_COMB_FILTER_CONST_C
  162. void comb_filter_const_c(opus_val32 *y, opus_val32 *x, int T, int N,
  163. opus_val16 g10, opus_val16 g11, opus_val16 g12);
  164. #endif
  165. #ifndef OVERRIDE_COMB_FILTER_CONST
  166. # define comb_filter_const(y, x, T, N, g10, g11, g12, arch) \
  167. ((void)(arch),comb_filter_const_c(y, x, T, N, g10, g11, g12))
  168. #endif
  169. void init_caps(const CELTMode *m,int *cap,int LM,int C);
  170. #ifdef RESYNTH
  171. void deemphasis(celt_sig *in[], opus_val16 *pcm, int N, int C, int downsample, const opus_val16 *coef, celt_sig *mem);
  172. void celt_synthesis(const CELTMode *mode, celt_norm *X, celt_sig * out_syn[],
  173. opus_val16 *oldBandE, int start, int effEnd, int C, int CC, int isTransient,
  174. int LM, int downsample, int silence);
  175. #endif
  176. #ifdef __cplusplus
  177. }
  178. #endif
  179. #endif /* CELT_H */