2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.
11 #include "webrtc/modules/audio_coding/main/acm2/acm_celt.h"
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"
25 #ifndef WEBRTC_CODEC_CELT
27 ACMCELT::ACMCELT(int16_t /* codec_id */)
28 : enc_inst_ptr_(NULL),
39 int16_t ACMCELT::InternalEncode(uint8_t* /* bitstream */,
40 int16_t* /* bitstream_len_byte */) {
44 int16_t ACMCELT::InternalInitEncoder(WebRtcACMCodecParams* /* codec_params */) {
48 ACMGenericCodec* ACMCELT::CreateInstance(void) {
52 int16_t ACMCELT::InternalCreateEncoder() {
56 void ACMCELT::DestructEncoderSafe() {
60 void ACMCELT::InternalDestructEncoderInst(void* /* ptr_inst */) {
64 int16_t ACMCELT::SetBitRateSafe(const int32_t /*rate*/) {
68 #else //===================== Actual Implementation =======================
70 ACMCELT::ACMCELT(int16_t codec_id)
71 : enc_inst_ptr_(NULL),
72 sampling_freq_(32000), // Default sampling frequency.
73 bitrate_(64000), // Default rate.
74 channels_(1) { // Default send mono.
75 // TODO(tlegrand): remove later when ACMGenericCodec has a new constructor.
82 if (enc_inst_ptr_ != NULL) {
83 WebRtcCelt_FreeEnc(enc_inst_ptr_);
89 int16_t ACMCELT::InternalEncode(uint8_t* bitstream,
90 int16_t* bitstream_len_byte) {
91 *bitstream_len_byte = 0;
94 *bitstream_len_byte = WebRtcCelt_Encode(enc_inst_ptr_,
95 &in_audio_[in_audio_ix_read_],
98 // Increment the read index this tell the caller that how far
99 // we have gone forward in reading the audio buffer.
100 in_audio_ix_read_ += frame_len_smpl_ * channels_;
102 if (*bitstream_len_byte < 0) {
103 // Error reported from the encoder.
104 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_,
105 "InternalEncode: Encode error for Celt");
106 *bitstream_len_byte = 0;
110 return *bitstream_len_byte;
113 int16_t ACMCELT::InternalInitEncoder(WebRtcACMCodecParams* codec_params) {
114 // Set bitrate and check that it is within the valid range.
115 int16_t status = SetBitRateSafe((codec_params->codec_inst).rate);
120 // If number of channels changed we need to re-create memory.
121 if (codec_params->codec_inst.channels != channels_) {
122 WebRtcCelt_FreeEnc(enc_inst_ptr_);
123 enc_inst_ptr_ = NULL;
124 // Store new number of channels.
125 channels_ = codec_params->codec_inst.channels;
126 if (WebRtcCelt_CreateEnc(&enc_inst_ptr_, channels_) < 0) {
132 if (WebRtcCelt_EncoderInit(enc_inst_ptr_, channels_, bitrate_) >= 0) {
139 ACMGenericCodec* ACMCELT::CreateInstance(void) {
143 int16_t ACMCELT::InternalCreateEncoder() {
144 if (WebRtcCelt_CreateEnc(&enc_inst_ptr_, num_channels_) < 0) {
145 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_,
146 "InternalCreateEncoder: create encoder failed for Celt");
149 channels_ = num_channels_;
153 void ACMCELT::DestructEncoderSafe() {
154 encoder_exist_ = false;
155 encoder_initialized_ = false;
156 if (enc_inst_ptr_ != NULL) {
157 WebRtcCelt_FreeEnc(enc_inst_ptr_);
158 enc_inst_ptr_ = NULL;
162 void ACMCELT::InternalDestructEncoderInst(void* ptr_inst) {
163 if (ptr_inst != NULL) {
164 WebRtcCelt_FreeEnc(static_cast<CELT_encinst_t*>(ptr_inst));
169 int16_t ACMCELT::SetBitRateSafe(const int32_t rate) {
170 // Check that rate is in the valid range.
171 if ((rate >= 48000) && (rate <= 128000)) {
175 // Initiate encoder with new rate.
176 if (WebRtcCelt_EncoderInit(enc_inst_ptr_, channels_, bitrate_) >= 0) {
179 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_,
180 "SetBitRateSafe: Failed to initiate Celt with rate %d",
185 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_,
186 "SetBitRateSafe: Invalid rate Celt, %d", rate);
195 } // namespace webrtc