| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 | 11 |
| 12 #ifndef WEBRTC_MODULES_AUDIO_CODING_CODECS_CNG_WEBRTC_CNG_H_ | 12 #ifndef WEBRTC_MODULES_AUDIO_CODING_CODECS_CNG_WEBRTC_CNG_H_ |
| 13 #define WEBRTC_MODULES_AUDIO_CODING_CODECS_CNG_WEBRTC_CNG_H_ | 13 #define WEBRTC_MODULES_AUDIO_CODING_CODECS_CNG_WEBRTC_CNG_H_ |
| 14 | 14 |
| 15 #include <stddef.h> | 15 #include <cstddef> |
| 16 |
| 17 #include "webrtc/base/array_view.h" |
| 18 #include "webrtc/base/buffer.h" |
| 16 #include "webrtc/typedefs.h" | 19 #include "webrtc/typedefs.h" |
| 17 | 20 |
| 18 #ifdef __cplusplus | 21 #define WEBRTC_CNG_MAX_LPC_ORDER 12 |
| 19 extern "C" { | |
| 20 #endif | |
| 21 | 22 |
| 22 #define WEBRTC_CNG_MAX_LPC_ORDER 12 | 23 namespace webrtc { |
| 23 #define WEBRTC_CNG_MAX_OUTSIZE_ORDER 640 | |
| 24 | 24 |
| 25 /* Define Error codes. */ | 25 class ComfortNoiseDecoder { |
| 26 public: |
| 27 ComfortNoiseDecoder(); |
| 28 ~ComfortNoiseDecoder() = default; |
| 26 | 29 |
| 27 /* 6100 Encoder */ | 30 ComfortNoiseDecoder(const ComfortNoiseDecoder&) = delete; |
| 28 #define CNG_ENCODER_NOT_INITIATED 6120 | 31 ComfortNoiseDecoder& operator=(const ComfortNoiseDecoder&) = delete; |
| 29 #define CNG_DISALLOWED_LPC_ORDER 6130 | |
| 30 #define CNG_DISALLOWED_FRAME_SIZE 6140 | |
| 31 #define CNG_DISALLOWED_SAMPLING_FREQUENCY 6150 | |
| 32 /* 6200 Decoder */ | |
| 33 #define CNG_DECODER_NOT_INITIATED 6220 | |
| 34 | 32 |
| 35 typedef struct WebRtcCngEncInst CNG_enc_inst; | 33 void Reset(); |
| 36 typedef struct WebRtcCngDecInst CNG_dec_inst; | |
| 37 | 34 |
| 38 /**************************************************************************** | 35 // Updates the CN state when a new SID packet arrives. |
| 39 * WebRtcCng_CreateEnc/Dec(...) | 36 // |sid| is a view of the SID packet without the headers. |
| 40 * | 37 void UpdateSid(rtc::ArrayView<const uint8_t> sid); |
| 41 * These functions create an instance to the specified structure | |
| 42 * | |
| 43 * Input: | |
| 44 * - XXX_inst : Pointer to created instance that should be created | |
| 45 * | |
| 46 * Return value : 0 - Ok | |
| 47 * -1 - Error | |
| 48 */ | |
| 49 int16_t WebRtcCng_CreateEnc(CNG_enc_inst** cng_inst); | |
| 50 int16_t WebRtcCng_CreateDec(CNG_dec_inst** cng_inst); | |
| 51 | 38 |
| 52 /**************************************************************************** | 39 // Generates comfort noise. |
| 53 * WebRtcCng_InitEnc/Dec(...) | 40 // |out_data| will be filled with samples - its size determines the number of |
| 54 * | 41 // samples generated. When |new_period| is true, CNG history will be reset |
| 55 * This function initializes a instance | 42 // before any audio is generated. Returns |false| if outData is too large - |
| 56 * | 43 // currently 640 bytes (equalling 10ms at 64kHz). |
| 57 * Input: | 44 // TODO(ossu): Specify better limits for the size of out_data. Either let it |
| 58 * - cng_inst : Instance that should be initialized | 45 // be unbounded or limit to 10ms in the current sample rate. |
| 59 * | 46 bool Generate(rtc::ArrayView<int16_t> out_data, bool new_period); |
| 60 * - fs : 8000 for narrowband and 16000 for wideband | |
| 61 * - interval : generate SID data every interval ms | |
| 62 * - quality : Number of refl. coefs, maximum allowed is 12 | |
| 63 * | |
| 64 * Output: | |
| 65 * - cng_inst : Initialized instance | |
| 66 * | |
| 67 * Return value : 0 - Ok | |
| 68 * -1 - Error | |
| 69 */ | |
| 70 | 47 |
| 71 int WebRtcCng_InitEnc(CNG_enc_inst* cng_inst, int fs, int16_t interval, | 48 private: |
| 72 int16_t quality); | 49 uint32_t dec_seed_; |
| 73 void WebRtcCng_InitDec(CNG_dec_inst* cng_inst); | 50 int32_t dec_target_energy_; |
| 51 int32_t dec_used_energy_; |
| 52 int16_t dec_target_reflCoefs_[WEBRTC_CNG_MAX_LPC_ORDER + 1]; |
| 53 int16_t dec_used_reflCoefs_[WEBRTC_CNG_MAX_LPC_ORDER + 1]; |
| 54 int16_t dec_filtstate_[WEBRTC_CNG_MAX_LPC_ORDER + 1]; |
| 55 int16_t dec_filtstateLow_[WEBRTC_CNG_MAX_LPC_ORDER + 1]; |
| 56 uint16_t dec_order_; |
| 57 int16_t dec_target_scale_factor_; /* Q29 */ |
| 58 int16_t dec_used_scale_factor_; /* Q29 */ |
| 59 }; |
| 74 | 60 |
| 75 /**************************************************************************** | 61 class ComfortNoiseEncoder { |
| 76 * WebRtcCng_FreeEnc/Dec(...) | 62 public: |
| 77 * | 63 // Creates a comfort noise encoder. |
| 78 * These functions frees the dynamic memory of a specified instance | 64 // |fs| selects sample rate: 8000 for narrowband or 16000 for wideband. |
| 79 * | 65 // |interval| sets the interval at which to generate SID data (in ms). |
| 80 * Input: | 66 // |quality| selects the number of refl. coeffs. Maximum allowed is 12. |
| 81 * - cng_inst : Pointer to created instance that should be freed | 67 ComfortNoiseEncoder(int fs, int interval, int quality); |
| 82 * | 68 ~ComfortNoiseEncoder() = default; |
| 83 * Return value : 0 - Ok | |
| 84 * -1 - Error | |
| 85 */ | |
| 86 int16_t WebRtcCng_FreeEnc(CNG_enc_inst* cng_inst); | |
| 87 int16_t WebRtcCng_FreeDec(CNG_dec_inst* cng_inst); | |
| 88 | 69 |
| 89 /**************************************************************************** | 70 ComfortNoiseEncoder(const ComfortNoiseEncoder&) = delete; |
| 90 * WebRtcCng_Encode(...) | 71 ComfortNoiseEncoder& operator=(const ComfortNoiseEncoder&) = delete; |
| 91 * | |
| 92 * These functions analyzes background noise | |
| 93 * | |
| 94 * Input: | |
| 95 * - cng_inst : Pointer to created instance | |
| 96 * - speech : Signal to be analyzed | |
| 97 * - nrOfSamples : Size of speech vector | |
| 98 * - forceSID : not zero to force SID frame and reset | |
| 99 * | |
| 100 * Output: | |
| 101 * - bytesOut : Nr of bytes to transmit, might be 0 | |
| 102 * | |
| 103 * Return value : 0 - Ok | |
| 104 * -1 - Error | |
| 105 */ | |
| 106 int WebRtcCng_Encode(CNG_enc_inst* cng_inst, int16_t* speech, | |
| 107 size_t nrOfSamples, uint8_t* SIDdata, | |
| 108 size_t* bytesOut, int16_t forceSID); | |
| 109 | 72 |
| 110 /**************************************************************************** | 73 // Resets the comfort noise encoder to its initial state. |
| 111 * WebRtcCng_UpdateSid(...) | 74 // Parameters are set as during construction. |
| 112 * | 75 void Reset(int fs, int interval, int quality); |
| 113 * These functions updates the CN state, when a new SID packet arrives | |
| 114 * | |
| 115 * Input: | |
| 116 * - cng_inst : Pointer to created instance that should be freed | |
| 117 * - SID : SID packet, all headers removed | |
| 118 * - length : Length in bytes of SID packet | |
| 119 * | |
| 120 * Return value : 0 - Ok | |
| 121 * -1 - Error | |
| 122 */ | |
| 123 int16_t WebRtcCng_UpdateSid(CNG_dec_inst* cng_inst, uint8_t* SID, | |
| 124 size_t length); | |
| 125 | 76 |
| 126 /**************************************************************************** | 77 // Analyzes background noise from |speech| and appends coefficients to |
| 127 * WebRtcCng_Generate(...) | 78 // |output|. Returns the number of coefficients generated. If |force_sid| is |
| 128 * | 79 // true, a SID frame is forced and the internal sid interval counter is reset. |
| 129 * These functions generates CN data when needed | 80 // Will fail if the input size is too large (> 640 samples, see |
| 130 * | 81 // ComfortNoiseDecoder::Generate). |
| 131 * Input: | 82 size_t Encode(rtc::ArrayView<const int16_t> speech, |
| 132 * - cng_inst : Pointer to created instance that should be freed | 83 bool force_sid, |
| 133 * - outData : pointer to area to write CN data | 84 rtc::Buffer* output); |
| 134 * - nrOfSamples : How much data to generate | |
| 135 * - new_period : >0 if a new period of CNG, will reset history | |
| 136 * | |
| 137 * Return value : 0 - Ok | |
| 138 * -1 - Error | |
| 139 */ | |
| 140 int16_t WebRtcCng_Generate(CNG_dec_inst* cng_inst, int16_t* outData, | |
| 141 size_t nrOfSamples, int16_t new_period); | |
| 142 | 85 |
| 143 /***************************************************************************** | 86 private: |
| 144 * WebRtcCng_GetErrorCodeEnc/Dec(...) | 87 size_t enc_nrOfCoefs_; |
| 145 * | 88 int enc_sampfreq_; |
| 146 * This functions can be used to check the error code of a CNG instance. When | 89 int16_t enc_interval_; |
| 147 * a function returns -1 a error code will be set for that instance. The | 90 int16_t enc_msSinceSid_; |
| 148 * function below extract the code of the last error that occurred in the | 91 int32_t enc_Energy_; |
| 149 * specified instance. | 92 int16_t enc_reflCoefs_[WEBRTC_CNG_MAX_LPC_ORDER + 1]; |
| 150 * | 93 int32_t enc_corrVector_[WEBRTC_CNG_MAX_LPC_ORDER + 1]; |
| 151 * Input: | 94 uint32_t enc_seed_; |
| 152 * - CNG_inst : CNG enc/dec instance | 95 }; |
| 153 * | |
| 154 * Return value : Error code | |
| 155 */ | |
| 156 int16_t WebRtcCng_GetErrorCodeEnc(CNG_enc_inst* cng_inst); | |
| 157 int16_t WebRtcCng_GetErrorCodeDec(CNG_dec_inst* cng_inst); | |
| 158 | 96 |
| 159 #ifdef __cplusplus | 97 } // namespace webrtc |
| 160 } | |
| 161 #endif | |
| 162 | 98 |
| 163 #endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_CNG_WEBRTC_CNG_H_ | 99 #endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_CNG_WEBRTC_CNG_H_ |
| OLD | NEW |