1 /* 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 3 * 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 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #include "webrtc/modules/audio_coding/main/acm2/acm_celt.h" 12 13 #ifdef WEBRTC_CODEC_CELT 14 // NOTE! Celt is not included in the open-source package. Modify this file or 15 // your codec API to match the function call and name of used CELT API file. 16 #include "webrtc/modules/audio_coding/codecs/celt/include/celt_interface.h" 17 #include "webrtc/modules/audio_coding/main/acm2/acm_common_defs.h" 18 #include "webrtc/system_wrappers/interface/trace.h" 19 #endif 20 21 namespace webrtc { 22 23 namespace acm2 { 24 25 #ifndef WEBRTC_CODEC_CELT 26 27 ACMCELT::ACMCELT(int16_t /* codec_id */) 28 : enc_inst_ptr_(NULL), 29 sampling_freq_(0), 30 bitrate_(0), 31 channels_(1) { 32 return; 33 } 34 35 ACMCELT::~ACMCELT() { 36 return; 37 } 38 39 int16_t ACMCELT::InternalEncode(uint8_t* /* bitstream */, 40 int16_t* /* bitstream_len_byte */) { 41 return -1; 42 } 43 44 int16_t ACMCELT::InternalInitEncoder(WebRtcACMCodecParams* /* codec_params */) { 45 return -1; 46 } 47 48 ACMGenericCodec* ACMCELT::CreateInstance(void) { 49 return NULL; 50 } 51 52 int16_t ACMCELT::InternalCreateEncoder() { 53 return -1; 54 } 55 56 void ACMCELT::DestructEncoderSafe() { 57 return; 58 } 59 60 int16_t ACMCELT::SetBitRateSafe(const int32_t /*rate*/) { 61 return -1; 62 } 63 64 #else //===================== Actual Implementation ======================= 65 66 ACMCELT::ACMCELT(int16_t codec_id) 67 : enc_inst_ptr_(NULL), 68 sampling_freq_(32000), // Default sampling frequency. 69 bitrate_(64000), // Default rate. 70 channels_(1) { // Default send mono. 71 // TODO(tlegrand): remove later when ACMGenericCodec has a new constructor. 72 codec_id_ = codec_id; 73 74 return; 75 } 76 77 ACMCELT::~ACMCELT() { 78 if (enc_inst_ptr_ != NULL) { 79 WebRtcCelt_FreeEnc(enc_inst_ptr_); 80 enc_inst_ptr_ = NULL; 81 } 82 return; 83 } 84 85 int16_t ACMCELT::InternalEncode(uint8_t* bitstream, 86 int16_t* bitstream_len_byte) { 87 *bitstream_len_byte = 0; 88 89 // Call Encoder. 90 *bitstream_len_byte = WebRtcCelt_Encode(enc_inst_ptr_, 91 &in_audio_[in_audio_ix_read_], 92 bitstream); 93 94 // Increment the read index this tell the caller that how far 95 // we have gone forward in reading the audio buffer. 96 in_audio_ix_read_ += frame_len_smpl_ * channels_; 97 98 if (*bitstream_len_byte < 0) { 99 // Error reported from the encoder. 100 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_, 101 "InternalEncode: Encode error for Celt"); 102 *bitstream_len_byte = 0; 103 return -1; 104 } 105 106 return *bitstream_len_byte; 107 } 108 109 int16_t ACMCELT::InternalInitEncoder(WebRtcACMCodecParams* codec_params) { 110 // Set bitrate and check that it is within the valid range. 111 int16_t status = SetBitRateSafe((codec_params->codec_inst).rate); 112 if (status < 0) { 113 return -1; 114 } 115 116 // If number of channels changed we need to re-create memory. 117 if (codec_params->codec_inst.channels != channels_) { 118 WebRtcCelt_FreeEnc(enc_inst_ptr_); 119 enc_inst_ptr_ = NULL; 120 // Store new number of channels. 121 channels_ = codec_params->codec_inst.channels; 122 if (WebRtcCelt_CreateEnc(&enc_inst_ptr_, channels_) < 0) { 123 return -1; 124 } 125 } 126 127 // Initiate encoder. 128 if (WebRtcCelt_EncoderInit(enc_inst_ptr_, channels_, bitrate_) >= 0) { 129 return 0; 130 } else { 131 return -1; 132 } 133 } 134 135 ACMGenericCodec* ACMCELT::CreateInstance(void) { 136 return NULL; 137 } 138 139 int16_t ACMCELT::InternalCreateEncoder() { 140 if (WebRtcCelt_CreateEnc(&enc_inst_ptr_, num_channels_) < 0) { 141 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_, 142 "InternalCreateEncoder: create encoder failed for Celt"); 143 return -1; 144 } 145 channels_ = num_channels_; 146 return 0; 147 } 148 149 void ACMCELT::DestructEncoderSafe() { 150 encoder_exist_ = false; 151 encoder_initialized_ = false; 152 if (enc_inst_ptr_ != NULL) { 153 WebRtcCelt_FreeEnc(enc_inst_ptr_); 154 enc_inst_ptr_ = NULL; 155 } 156 } 157 158 int16_t ACMCELT::SetBitRateSafe(const int32_t rate) { 159 // Check that rate is in the valid range. 160 if ((rate >= 48000) && (rate <= 128000)) { 161 // Store new rate. 162 bitrate_ = rate; 163 164 // Initiate encoder with new rate. 165 if (WebRtcCelt_EncoderInit(enc_inst_ptr_, channels_, bitrate_) >= 0) { 166 return 0; 167 } else { 168 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_, 169 "SetBitRateSafe: Failed to initiate Celt with rate %d", 170 rate); 171 return -1; 172 } 173 } else { 174 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_, 175 "SetBitRateSafe: Invalid rate Celt, %d", rate); 176 return -1; 177 } 178 } 179 180 #endif 181 182 } // namespace acm2 183 184 } // namespace webrtc 185