audio_encoder_opus.cc revision 0561716ae262461eaa3fe5291f4626c76822108a
1/* 2 * Copyright (c) 2014 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/codecs/opus/interface/audio_encoder_opus.h" 12 13#include "webrtc/base/checks.h" 14#include "webrtc/modules/audio_coding/codecs/opus/interface/opus_interface.h" 15 16namespace webrtc { 17 18namespace { 19 20const int kMinBitrateBps = 500; 21const int kMaxBitrateBps = 512000; 22 23// TODO(tlegrand): Remove this code when we have proper APIs to set the 24// complexity at a higher level. 25#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) || defined(WEBRTC_ARCH_ARM) 26// If we are on Android, iOS and/or ARM, use a lower complexity setting as 27// default, to save encoder complexity. 28const int kDefaultComplexity = 5; 29#else 30const int kDefaultComplexity = 9; 31#endif 32 33// We always encode at 48 kHz. 34const int kSampleRateHz = 48000; 35 36int16_t ClampInt16(size_t x) { 37 return static_cast<int16_t>( 38 std::min(x, static_cast<size_t>(std::numeric_limits<int16_t>::max()))); 39} 40 41int16_t CastInt16(size_t x) { 42 DCHECK_LE(x, static_cast<size_t>(std::numeric_limits<int16_t>::max())); 43 return static_cast<int16_t>(x); 44} 45 46} // namespace 47 48AudioEncoderOpus::Config::Config() 49 : frame_size_ms(20), 50 num_channels(1), 51 payload_type(120), 52 application(kVoip), 53 bitrate_bps(64000), 54 fec_enabled(false), 55 max_playback_rate_hz(48000), 56 complexity(kDefaultComplexity), 57 dtx_enabled(false) { 58} 59 60bool AudioEncoderOpus::Config::IsOk() const { 61 if (frame_size_ms <= 0 || frame_size_ms % 10 != 0) 62 return false; 63 if (num_channels != 1 && num_channels != 2) 64 return false; 65 if (bitrate_bps < kMinBitrateBps || bitrate_bps > kMaxBitrateBps) 66 return false; 67 if (complexity < 0 || complexity > 10) 68 return false; 69 if (dtx_enabled && application != kVoip) 70 return false; 71 return true; 72} 73 74AudioEncoderOpus::AudioEncoderOpus(const Config& config) 75 : num_10ms_frames_per_packet_( 76 rtc::CheckedDivExact(config.frame_size_ms, 10)), 77 num_channels_(config.num_channels), 78 payload_type_(config.payload_type), 79 application_(config.application), 80 samples_per_10ms_frame_(rtc::CheckedDivExact(kSampleRateHz, 100) * 81 num_channels_), 82 packet_loss_rate_(0.0) { 83 CHECK(config.IsOk()); 84 input_buffer_.reserve(num_10ms_frames_per_packet_ * samples_per_10ms_frame_); 85 CHECK_EQ(0, WebRtcOpus_EncoderCreate(&inst_, num_channels_, application_)); 86 SetTargetBitrate(config.bitrate_bps); 87 if (config.fec_enabled) { 88 CHECK_EQ(0, WebRtcOpus_EnableFec(inst_)); 89 } else { 90 CHECK_EQ(0, WebRtcOpus_DisableFec(inst_)); 91 } 92 CHECK_EQ(0, 93 WebRtcOpus_SetMaxPlaybackRate(inst_, config.max_playback_rate_hz)); 94 CHECK_EQ(0, WebRtcOpus_SetComplexity(inst_, config.complexity)); 95 if (config.dtx_enabled) { 96 CHECK_EQ(0, WebRtcOpus_EnableDtx(inst_)); 97 } else { 98 CHECK_EQ(0, WebRtcOpus_DisableDtx(inst_)); 99 } 100} 101 102AudioEncoderOpus::~AudioEncoderOpus() { 103 CHECK_EQ(0, WebRtcOpus_EncoderFree(inst_)); 104} 105 106int AudioEncoderOpus::SampleRateHz() const { 107 return kSampleRateHz; 108} 109 110int AudioEncoderOpus::NumChannels() const { 111 return num_channels_; 112} 113 114int AudioEncoderOpus::Num10MsFramesInNextPacket() const { 115 return num_10ms_frames_per_packet_; 116} 117 118int AudioEncoderOpus::Max10MsFramesInAPacket() const { 119 return num_10ms_frames_per_packet_; 120} 121 122void AudioEncoderOpus::SetTargetBitrate(int bits_per_second) { 123 CHECK_EQ(WebRtcOpus_SetBitRate( 124 inst_, std::max(std::min(bits_per_second, kMaxBitrateBps), 125 kMinBitrateBps)), 126 0); 127} 128 129void AudioEncoderOpus::SetProjectedPacketLossRate(double fraction) { 130 DCHECK_GE(fraction, 0.0); 131 DCHECK_LE(fraction, 1.0); 132 // Optimize the loss rate to configure Opus. Basically, optimized loss rate is 133 // the input loss rate rounded down to various levels, because a robustly good 134 // audio quality is achieved by lowering the packet loss down. 135 // Additionally, to prevent toggling, margins are used, i.e., when jumping to 136 // a loss rate from below, a higher threshold is used than jumping to the same 137 // level from above. 138 const double kPacketLossRate20 = 0.20; 139 const double kPacketLossRate10 = 0.10; 140 const double kPacketLossRate5 = 0.05; 141 const double kPacketLossRate1 = 0.01; 142 const double kLossRate20Margin = 0.02; 143 const double kLossRate10Margin = 0.01; 144 const double kLossRate5Margin = 0.01; 145 double opt_loss_rate; 146 if (fraction >= 147 kPacketLossRate20 + 148 kLossRate20Margin * 149 (kPacketLossRate20 - packet_loss_rate_ > 0 ? 1 : -1)) { 150 opt_loss_rate = kPacketLossRate20; 151 } else if (fraction >= 152 kPacketLossRate10 + 153 kLossRate10Margin * 154 (kPacketLossRate10 - packet_loss_rate_ > 0 ? 1 : -1)) { 155 opt_loss_rate = kPacketLossRate10; 156 } else if (fraction >= 157 kPacketLossRate5 + 158 kLossRate5Margin * 159 (kPacketLossRate5 - packet_loss_rate_ > 0 ? 1 : -1)) { 160 opt_loss_rate = kPacketLossRate5; 161 } else if (fraction >= kPacketLossRate1) { 162 opt_loss_rate = kPacketLossRate1; 163 } else { 164 opt_loss_rate = 0; 165 } 166 167 if (packet_loss_rate_ != opt_loss_rate) { 168 // Ask the encoder to change the target packet loss rate. 169 CHECK_EQ(WebRtcOpus_SetPacketLossRate( 170 inst_, static_cast<int32_t>(opt_loss_rate * 100 + .5)), 171 0); 172 packet_loss_rate_ = opt_loss_rate; 173 } 174} 175 176void AudioEncoderOpus::EncodeInternal(uint32_t rtp_timestamp, 177 const int16_t* audio, 178 size_t max_encoded_bytes, 179 uint8_t* encoded, 180 EncodedInfo* info) { 181 if (input_buffer_.empty()) 182 first_timestamp_in_buffer_ = rtp_timestamp; 183 input_buffer_.insert(input_buffer_.end(), audio, 184 audio + samples_per_10ms_frame_); 185 if (input_buffer_.size() < (static_cast<size_t>(num_10ms_frames_per_packet_) * 186 samples_per_10ms_frame_)) { 187 info->encoded_bytes = 0; 188 return; 189 } 190 CHECK_EQ(input_buffer_.size(), 191 static_cast<size_t>(num_10ms_frames_per_packet_) * 192 samples_per_10ms_frame_); 193 int16_t r = WebRtcOpus_Encode( 194 inst_, &input_buffer_[0], 195 rtc::CheckedDivExact(CastInt16(input_buffer_.size()), 196 static_cast<int16_t>(num_channels_)), 197 ClampInt16(max_encoded_bytes), encoded); 198 CHECK_GE(r, 0); // Fails only if fed invalid data. 199 input_buffer_.clear(); 200 info->encoded_bytes = r; 201 info->encoded_timestamp = first_timestamp_in_buffer_; 202 info->payload_type = payload_type_; 203 // Allows Opus to send empty packets. 204 info->send_even_if_empty = true; 205} 206 207} // namespace webrtc 208