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 #ifndef MODULES_AUDIO_CODING_CODECS_ISAC_AUDIO_ENCODER_ISAC_T_IMPL_H_
12 #define MODULES_AUDIO_CODING_CODECS_ISAC_AUDIO_ENCODER_ISAC_T_IMPL_H_
13
14 #include "rtc_base/checks.h"
15 #include "rtc_base/numerics/safe_minmax.h"
16
17 namespace webrtc {
18
19 template <typename T>
IsOk()20 bool AudioEncoderIsacT<T>::Config::IsOk() const {
21 if (max_bit_rate < 32000 && max_bit_rate != -1)
22 return false;
23 if (max_payload_size_bytes < 120 && max_payload_size_bytes != -1)
24 return false;
25
26 switch (sample_rate_hz) {
27 case 16000:
28 if (max_bit_rate > 53400)
29 return false;
30 if (max_payload_size_bytes > 400)
31 return false;
32 return (frame_size_ms == 30 || frame_size_ms == 60) &&
33 (bit_rate == 0 || (bit_rate >= 10000 && bit_rate <= 32000));
34 case 32000:
35 if (max_bit_rate > 160000)
36 return false;
37 if (max_payload_size_bytes > 600)
38 return false;
39 return T::has_swb &&
40 (frame_size_ms == 30 &&
41 (bit_rate == 0 || (bit_rate >= 10000 && bit_rate <= 56000)));
42 default:
43 return false;
44 }
45 }
46
47 template <typename T>
AudioEncoderIsacT(const Config & config)48 AudioEncoderIsacT<T>::AudioEncoderIsacT(const Config& config) {
49 RecreateEncoderInstance(config);
50 }
51
52 template <typename T>
~AudioEncoderIsacT()53 AudioEncoderIsacT<T>::~AudioEncoderIsacT() {
54 RTC_CHECK_EQ(0, T::Free(isac_state_));
55 }
56
57 template <typename T>
SampleRateHz()58 int AudioEncoderIsacT<T>::SampleRateHz() const {
59 return T::EncSampRate(isac_state_);
60 }
61
62 template <typename T>
NumChannels()63 size_t AudioEncoderIsacT<T>::NumChannels() const {
64 return 1;
65 }
66
67 template <typename T>
Num10MsFramesInNextPacket()68 size_t AudioEncoderIsacT<T>::Num10MsFramesInNextPacket() const {
69 const int samples_in_next_packet = T::GetNewFrameLen(isac_state_);
70 return static_cast<size_t>(rtc::CheckedDivExact(
71 samples_in_next_packet, rtc::CheckedDivExact(SampleRateHz(), 100)));
72 }
73
74 template <typename T>
Max10MsFramesInAPacket()75 size_t AudioEncoderIsacT<T>::Max10MsFramesInAPacket() const {
76 return 6; // iSAC puts at most 60 ms in a packet.
77 }
78
79 template <typename T>
GetTargetBitrate()80 int AudioEncoderIsacT<T>::GetTargetBitrate() const {
81 return config_.bit_rate == 0 ? kDefaultBitRate : config_.bit_rate;
82 }
83
84 template <typename T>
SetTargetBitrate(int target_bps)85 void AudioEncoderIsacT<T>::SetTargetBitrate(int target_bps) {
86 // Set target bitrate directly without subtracting per-packet overhead,
87 // because that's what AudioEncoderOpus does.
88 SetTargetBitrate(target_bps,
89 /*subtract_per_packet_overhead=*/false);
90 }
91
92 template <typename T>
OnReceivedTargetAudioBitrate(int target_bps)93 void AudioEncoderIsacT<T>::OnReceivedTargetAudioBitrate(int target_bps) {
94 // Set target bitrate directly without subtracting per-packet overhead,
95 // because that's what AudioEncoderOpus does.
96 SetTargetBitrate(target_bps,
97 /*subtract_per_packet_overhead=*/false);
98 }
99
100 template <typename T>
OnReceivedUplinkBandwidth(int target_audio_bitrate_bps,absl::optional<int64_t>)101 void AudioEncoderIsacT<T>::OnReceivedUplinkBandwidth(
102 int target_audio_bitrate_bps,
103 absl::optional<int64_t> /*bwe_period_ms*/) {
104 // Set target bitrate, subtracting the per-packet overhead if
105 // WebRTC-SendSideBwe-WithOverhead is enabled, because that's what
106 // AudioEncoderOpus does.
107 SetTargetBitrate(
108 target_audio_bitrate_bps,
109 /*subtract_per_packet_overhead=*/send_side_bwe_with_overhead_);
110 }
111
112 template <typename T>
OnReceivedUplinkAllocation(BitrateAllocationUpdate update)113 void AudioEncoderIsacT<T>::OnReceivedUplinkAllocation(
114 BitrateAllocationUpdate update) {
115 // Set target bitrate, subtracting the per-packet overhead if
116 // WebRTC-SendSideBwe-WithOverhead is enabled, because that's what
117 // AudioEncoderOpus does.
118 SetTargetBitrate(
119 update.target_bitrate.bps<int>(),
120 /*subtract_per_packet_overhead=*/send_side_bwe_with_overhead_);
121 }
122
123 template <typename T>
OnReceivedOverhead(size_t overhead_bytes_per_packet)124 void AudioEncoderIsacT<T>::OnReceivedOverhead(
125 size_t overhead_bytes_per_packet) {
126 overhead_per_packet_ = DataSize::Bytes(overhead_bytes_per_packet);
127 }
128
129 template <typename T>
EncodeImpl(uint32_t rtp_timestamp,rtc::ArrayView<const int16_t> audio,rtc::Buffer * encoded)130 AudioEncoder::EncodedInfo AudioEncoderIsacT<T>::EncodeImpl(
131 uint32_t rtp_timestamp,
132 rtc::ArrayView<const int16_t> audio,
133 rtc::Buffer* encoded) {
134 if (!packet_in_progress_) {
135 // Starting a new packet; remember the timestamp for later.
136 packet_in_progress_ = true;
137 packet_timestamp_ = rtp_timestamp;
138 }
139 size_t encoded_bytes = encoded->AppendData(
140 kSufficientEncodeBufferSizeBytes, [&](rtc::ArrayView<uint8_t> encoded) {
141 int r = T::Encode(isac_state_, audio.data(), encoded.data());
142
143 RTC_CHECK_GE(r, 0) << "Encode failed (error code "
144 << T::GetErrorCode(isac_state_) << ")";
145
146 return static_cast<size_t>(r);
147 });
148
149 if (encoded_bytes == 0)
150 return EncodedInfo();
151
152 // Got enough input to produce a packet. Return the saved timestamp from
153 // the first chunk of input that went into the packet.
154 packet_in_progress_ = false;
155 EncodedInfo info;
156 info.encoded_bytes = encoded_bytes;
157 info.encoded_timestamp = packet_timestamp_;
158 info.payload_type = config_.payload_type;
159 info.encoder_type = CodecType::kIsac;
160 return info;
161 }
162
163 template <typename T>
Reset()164 void AudioEncoderIsacT<T>::Reset() {
165 RecreateEncoderInstance(config_);
166 }
167
168 template <typename T>
169 absl::optional<std::pair<TimeDelta, TimeDelta>>
GetFrameLengthRange()170 AudioEncoderIsacT<T>::GetFrameLengthRange() const {
171 return {{TimeDelta::Millis(config_.frame_size_ms),
172 TimeDelta::Millis(config_.frame_size_ms)}};
173 }
174
175 template <typename T>
SetTargetBitrate(int target_bps,bool subtract_per_packet_overhead)176 void AudioEncoderIsacT<T>::SetTargetBitrate(int target_bps,
177 bool subtract_per_packet_overhead) {
178 if (subtract_per_packet_overhead) {
179 const DataRate overhead_rate =
180 overhead_per_packet_ / TimeDelta::Millis(config_.frame_size_ms);
181 target_bps -= overhead_rate.bps();
182 }
183 target_bps = rtc::SafeClamp(target_bps, kMinBitrateBps,
184 MaxBitrateBps(config_.sample_rate_hz));
185 int result = T::Control(isac_state_, target_bps, config_.frame_size_ms);
186 RTC_DCHECK_EQ(result, 0);
187 config_.bit_rate = target_bps;
188 }
189
190 template <typename T>
RecreateEncoderInstance(const Config & config)191 void AudioEncoderIsacT<T>::RecreateEncoderInstance(const Config& config) {
192 RTC_CHECK(config.IsOk());
193 packet_in_progress_ = false;
194 if (isac_state_)
195 RTC_CHECK_EQ(0, T::Free(isac_state_));
196 RTC_CHECK_EQ(0, T::Create(&isac_state_));
197 RTC_CHECK_EQ(0, T::EncoderInit(isac_state_, /*coding_mode=*/1));
198 RTC_CHECK_EQ(0, T::SetEncSampRate(isac_state_, config.sample_rate_hz));
199 const int bit_rate = config.bit_rate == 0 ? kDefaultBitRate : config.bit_rate;
200 RTC_CHECK_EQ(0, T::Control(isac_state_, bit_rate, config.frame_size_ms));
201
202 if (config.max_payload_size_bytes != -1)
203 RTC_CHECK_EQ(
204 0, T::SetMaxPayloadSize(isac_state_, config.max_payload_size_bytes));
205 if (config.max_bit_rate != -1)
206 RTC_CHECK_EQ(0, T::SetMaxRate(isac_state_, config.max_bit_rate));
207
208 // Set the decoder sample rate even though we just use the encoder. This
209 // doesn't appear to be necessary to produce a valid encoding, but without it
210 // we get an encoding that isn't bit-for-bit identical with what a combined
211 // encoder+decoder object produces.
212 RTC_CHECK_EQ(0, T::SetDecSampRate(isac_state_, config.sample_rate_hz));
213
214 config_ = config;
215 }
216
217 } // namespace webrtc
218
219 #endif // MODULES_AUDIO_CODING_CODECS_ISAC_AUDIO_ENCODER_ISAC_T_IMPL_H_
220