Line data Source code
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 WEBRTC_MODULES_AUDIO_CODING_CODECS_ISAC_AUDIO_ENCODER_ISAC_T_IMPL_H_
12 : #define WEBRTC_MODULES_AUDIO_CODING_CODECS_ISAC_AUDIO_ENCODER_ISAC_T_IMPL_H_
13 :
14 : #include "webrtc/modules/audio_coding/codecs/isac/main/include/audio_encoder_isac.h"
15 :
16 : #include "webrtc/base/checks.h"
17 : #include "webrtc/common_types.h"
18 :
19 : namespace webrtc {
20 :
21 : template <typename T>
22 0 : typename AudioEncoderIsacT<T>::Config CreateIsacConfig(
23 : const CodecInst& codec_inst,
24 : const rtc::scoped_refptr<LockedIsacBandwidthInfo>& bwinfo) {
25 0 : typename AudioEncoderIsacT<T>::Config config;
26 0 : config.bwinfo = bwinfo;
27 0 : config.payload_type = codec_inst.pltype;
28 0 : config.sample_rate_hz = codec_inst.plfreq;
29 0 : config.frame_size_ms =
30 0 : rtc::CheckedDivExact(1000 * codec_inst.pacsize, config.sample_rate_hz);
31 0 : config.adaptive_mode = (codec_inst.rate == -1);
32 0 : if (codec_inst.rate != -1)
33 0 : config.bit_rate = codec_inst.rate;
34 0 : return config;
35 : }
36 :
37 : template <typename T>
38 0 : bool AudioEncoderIsacT<T>::Config::IsOk() const {
39 0 : if (max_bit_rate < 32000 && max_bit_rate != -1)
40 0 : return false;
41 0 : if (max_payload_size_bytes < 120 && max_payload_size_bytes != -1)
42 0 : return false;
43 0 : if (adaptive_mode && !bwinfo)
44 0 : return false;
45 0 : switch (sample_rate_hz) {
46 : case 16000:
47 0 : if (max_bit_rate > 53400)
48 0 : return false;
49 0 : if (max_payload_size_bytes > 400)
50 0 : return false;
51 0 : return (frame_size_ms == 30 || frame_size_ms == 60) &&
52 0 : (bit_rate == 0 || (bit_rate >= 10000 && bit_rate <= 32000));
53 : case 32000:
54 0 : if (max_bit_rate > 160000)
55 0 : return false;
56 0 : if (max_payload_size_bytes > 600)
57 0 : return false;
58 : return T::has_swb &&
59 0 : (frame_size_ms == 30 &&
60 0 : (bit_rate == 0 || (bit_rate >= 10000 && bit_rate <= 56000)));
61 : default:
62 0 : return false;
63 : }
64 : }
65 :
66 : template <typename T>
67 0 : AudioEncoderIsacT<T>::AudioEncoderIsacT(const Config& config) {
68 0 : RecreateEncoderInstance(config);
69 0 : }
70 :
71 : template <typename T>
72 0 : AudioEncoderIsacT<T>::AudioEncoderIsacT(
73 : const CodecInst& codec_inst,
74 : const rtc::scoped_refptr<LockedIsacBandwidthInfo>& bwinfo)
75 0 : : AudioEncoderIsacT(CreateIsacConfig<T>(codec_inst, bwinfo)) {}
76 :
77 : template <typename T>
78 0 : AudioEncoderIsacT<T>::~AudioEncoderIsacT() {
79 0 : RTC_CHECK_EQ(0, T::Free(isac_state_));
80 0 : }
81 :
82 : template <typename T>
83 0 : int AudioEncoderIsacT<T>::SampleRateHz() const {
84 0 : return T::EncSampRate(isac_state_);
85 : }
86 :
87 : template <typename T>
88 0 : size_t AudioEncoderIsacT<T>::NumChannels() const {
89 0 : return 1;
90 : }
91 :
92 : template <typename T>
93 0 : size_t AudioEncoderIsacT<T>::Num10MsFramesInNextPacket() const {
94 0 : const int samples_in_next_packet = T::GetNewFrameLen(isac_state_);
95 : return static_cast<size_t>(
96 0 : rtc::CheckedDivExact(samples_in_next_packet,
97 0 : rtc::CheckedDivExact(SampleRateHz(), 100)));
98 : }
99 :
100 : template <typename T>
101 0 : size_t AudioEncoderIsacT<T>::Max10MsFramesInAPacket() const {
102 0 : return 6; // iSAC puts at most 60 ms in a packet.
103 : }
104 :
105 : template <typename T>
106 0 : int AudioEncoderIsacT<T>::GetTargetBitrate() const {
107 0 : if (config_.adaptive_mode)
108 0 : return -1;
109 0 : return config_.bit_rate == 0 ? kDefaultBitRate : config_.bit_rate;
110 : }
111 :
112 : template <typename T>
113 0 : AudioEncoder::EncodedInfo AudioEncoderIsacT<T>::EncodeImpl(
114 : uint32_t rtp_timestamp,
115 : rtc::ArrayView<const int16_t> audio,
116 : rtc::Buffer* encoded) {
117 0 : if (!packet_in_progress_) {
118 : // Starting a new packet; remember the timestamp for later.
119 0 : packet_in_progress_ = true;
120 0 : packet_timestamp_ = rtp_timestamp;
121 : }
122 0 : if (bwinfo_) {
123 0 : IsacBandwidthInfo bwinfo = bwinfo_->Get();
124 0 : T::SetBandwidthInfo(isac_state_, &bwinfo);
125 : }
126 :
127 : size_t encoded_bytes = encoded->AppendData(
128 : kSufficientEncodeBufferSizeBytes,
129 0 : [&] (rtc::ArrayView<uint8_t> encoded) {
130 0 : int r = T::Encode(isac_state_, audio.data(), encoded.data());
131 :
132 0 : RTC_CHECK_GE(r, 0) << "Encode failed (error code "
133 0 : << T::GetErrorCode(isac_state_) << ")";
134 :
135 0 : return static_cast<size_t>(r);
136 0 : });
137 :
138 0 : if (encoded_bytes == 0)
139 0 : return EncodedInfo();
140 :
141 : // Got enough input to produce a packet. Return the saved timestamp from
142 : // the first chunk of input that went into the packet.
143 0 : packet_in_progress_ = false;
144 0 : EncodedInfo info;
145 0 : info.encoded_bytes = encoded_bytes;
146 0 : info.encoded_timestamp = packet_timestamp_;
147 0 : info.payload_type = config_.payload_type;
148 0 : info.encoder_type = CodecType::kIsac;
149 0 : return info;
150 : }
151 :
152 : template <typename T>
153 0 : void AudioEncoderIsacT<T>::Reset() {
154 0 : RecreateEncoderInstance(config_);
155 0 : }
156 :
157 : template <typename T>
158 0 : void AudioEncoderIsacT<T>::RecreateEncoderInstance(const Config& config) {
159 0 : RTC_CHECK(config.IsOk());
160 0 : packet_in_progress_ = false;
161 0 : bwinfo_ = config.bwinfo;
162 0 : if (isac_state_)
163 0 : RTC_CHECK_EQ(0, T::Free(isac_state_));
164 0 : RTC_CHECK_EQ(0, T::Create(&isac_state_));
165 0 : RTC_CHECK_EQ(0, T::EncoderInit(isac_state_, config.adaptive_mode ? 0 : 1));
166 0 : RTC_CHECK_EQ(0, T::SetEncSampRate(isac_state_, config.sample_rate_hz));
167 0 : const int bit_rate = config.bit_rate == 0 ? kDefaultBitRate : config.bit_rate;
168 0 : if (config.adaptive_mode) {
169 0 : RTC_CHECK_EQ(0, T::ControlBwe(isac_state_, bit_rate, config.frame_size_ms,
170 : config.enforce_frame_size));
171 : } else {
172 0 : RTC_CHECK_EQ(0, T::Control(isac_state_, bit_rate, config.frame_size_ms));
173 : }
174 0 : if (config.max_payload_size_bytes != -1)
175 0 : RTC_CHECK_EQ(
176 : 0, T::SetMaxPayloadSize(isac_state_, config.max_payload_size_bytes));
177 0 : if (config.max_bit_rate != -1)
178 0 : RTC_CHECK_EQ(0, T::SetMaxRate(isac_state_, config.max_bit_rate));
179 :
180 : // Set the decoder sample rate even though we just use the encoder. This
181 : // doesn't appear to be necessary to produce a valid encoding, but without it
182 : // we get an encoding that isn't bit-for-bit identical with what a combined
183 : // encoder+decoder object produces.
184 0 : RTC_CHECK_EQ(0, T::SetDecSampRate(isac_state_, config.sample_rate_hz));
185 :
186 0 : config_ = config;
187 0 : }
188 :
189 : } // namespace webrtc
190 :
191 : #endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_ISAC_AUDIO_ENCODER_ISAC_T_IMPL_H_
|