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_opus.h"
13 #ifdef WEBRTC_CODEC_OPUS
14 #include "webrtc/modules/audio_coding/codecs/opus/interface/opus_interface.h"
15 #include "webrtc/modules/audio_coding/main/acm2/acm_codec_database.h"
16 #include "webrtc/modules/audio_coding/main/acm2/acm_common_defs.h"
17 #include "webrtc/system_wrappers/interface/trace.h"
24 #ifndef WEBRTC_CODEC_OPUS
26 ACMOpus::ACMOpus(int16_t /* codec_id */)
27 : encoder_inst_ptr_(NULL),
38 int16_t ACMOpus::InternalEncode(uint8_t* /* bitstream */,
39 int16_t* /* bitstream_len_byte */) {
43 int16_t ACMOpus::InternalInitEncoder(WebRtcACMCodecParams* /* codec_params */) {
47 ACMGenericCodec* ACMOpus::CreateInstance(void) {
51 int16_t ACMOpus::InternalCreateEncoder() {
55 void ACMOpus::DestructEncoderSafe() {
59 void ACMOpus::InternalDestructEncoderInst(void* /* ptr_inst */) {
63 int16_t ACMOpus::SetBitRateSafe(const int32_t /*rate*/) {
67 #else //===================== Actual Implementation =======================
69 ACMOpus::ACMOpus(int16_t codec_id)
70 : encoder_inst_ptr_(NULL),
71 sample_freq_(32000), // Default sampling frequency.
72 bitrate_(20000), // Default bit-rate.
73 channels_(1) { // Default mono
75 // Opus has internal DTX, but we dont use it for now.
76 has_internal_dtx_ = false;
78 has_internal_fec_ = true;
80 if (codec_id_ != ACMCodecDB::kOpus) {
81 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_,
82 "Wrong codec id for Opus.");
90 if (encoder_inst_ptr_ != NULL) {
91 WebRtcOpus_EncoderFree(encoder_inst_ptr_);
92 encoder_inst_ptr_ = NULL;
96 int16_t ACMOpus::InternalEncode(uint8_t* bitstream,
97 int16_t* bitstream_len_byte) {
99 *bitstream_len_byte = WebRtcOpus_Encode(encoder_inst_ptr_,
100 &in_audio_[in_audio_ix_read_],
102 MAX_PAYLOAD_SIZE_BYTE, bitstream);
103 // Check for error reported from encoder.
104 if (*bitstream_len_byte < 0) {
105 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_,
106 "InternalEncode: Encode error for Opus");
107 *bitstream_len_byte = 0;
111 // Increment the read index. This tells the caller how far
112 // we have gone forward in reading the audio buffer.
113 in_audio_ix_read_ += frame_len_smpl_ * channels_;
115 return *bitstream_len_byte;
118 int16_t ACMOpus::InternalInitEncoder(WebRtcACMCodecParams* codec_params) {
120 if (encoder_inst_ptr_ != NULL) {
121 WebRtcOpus_EncoderFree(encoder_inst_ptr_);
122 encoder_inst_ptr_ = NULL;
124 ret = WebRtcOpus_EncoderCreate(&encoder_inst_ptr_,
125 codec_params->codec_inst.channels);
126 // Store number of channels.
127 channels_ = codec_params->codec_inst.channels;
130 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_,
131 "Encoder creation failed for Opus");
134 ret = WebRtcOpus_SetBitRate(encoder_inst_ptr_,
135 codec_params->codec_inst.rate);
137 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_,
138 "Setting initial bitrate failed for Opus");
143 bitrate_ = codec_params->codec_inst.rate;
145 // TODO(tlegrand): Remove this code when we have proper APIs to set the
146 // complexity at a higher level.
147 #if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) || defined(WEBRTC_ARCH_ARM)
148 // If we are on Android, iOS and/or ARM, use a lower complexity setting as
149 // default, to save encoder complexity.
150 const int kOpusComplexity5 = 5;
151 WebRtcOpus_SetComplexity(encoder_inst_ptr_, kOpusComplexity5);
153 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_,
154 "Setting complexity failed for Opus");
162 ACMGenericCodec* ACMOpus::CreateInstance(void) {
166 int16_t ACMOpus::InternalCreateEncoder() {
167 // Real encoder will be created in InternalInitEncoder.
171 void ACMOpus::DestructEncoderSafe() {
172 if (encoder_inst_ptr_) {
173 WebRtcOpus_EncoderFree(encoder_inst_ptr_);
174 encoder_inst_ptr_ = NULL;
178 void ACMOpus::InternalDestructEncoderInst(void* ptr_inst) {
179 if (ptr_inst != NULL) {
180 WebRtcOpus_EncoderFree(static_cast<OpusEncInst*>(ptr_inst));
185 int16_t ACMOpus::SetBitRateSafe(const int32_t rate) {
186 if (rate < 6000 || rate > 510000) {
187 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_,
188 "SetBitRateSafe: Invalid rate Opus");
194 // Ask the encoder for the new rate.
195 if (WebRtcOpus_SetBitRate(encoder_inst_ptr_, bitrate_) >= 0) {
196 encoder_params_.codec_inst.rate = bitrate_;
203 int ACMOpus::SetFEC(bool enable_fec) {
204 // Ask the encoder to enable FEC.
206 if (WebRtcOpus_EnableFec(encoder_inst_ptr_) == 0) {
211 if (WebRtcOpus_DisableFec(encoder_inst_ptr_) == 0) {
212 fec_enabled_ = false;
219 int ACMOpus::SetPacketLossRate(int loss_rate) {
220 // Ask the encoder to change the target packet loss rate.
221 if (WebRtcOpus_SetPacketLossRate(encoder_inst_ptr_, loss_rate) == 0) {
222 packet_loss_rate_ = loss_rate;
228 #endif // WEBRTC_CODEC_OPUS
232 } // namespace webrtc