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 "modules/audio_coding/codecs/ilbc/audio_encoder_ilbc.h"
12
13 #include <algorithm>
14 #include <cstdint>
15
16 #include "modules/audio_coding/codecs/ilbc/ilbc.h"
17 #include "rtc_base/checks.h"
18 #include "rtc_base/numerics/safe_conversions.h"
19
20 namespace webrtc {
21
22 namespace {
23
24 const int kSampleRateHz = 8000;
25
GetIlbcBitrate(int ptime)26 int GetIlbcBitrate(int ptime) {
27 switch (ptime) {
28 case 20:
29 case 40:
30 // 38 bytes per frame of 20 ms => 15200 bits/s.
31 return 15200;
32 case 30:
33 case 60:
34 // 50 bytes per frame of 30 ms => (approx) 13333 bits/s.
35 return 13333;
36 default:
37 FATAL();
38 }
39 }
40
41 } // namespace
42
AudioEncoderIlbcImpl(const AudioEncoderIlbcConfig & config,int payload_type)43 AudioEncoderIlbcImpl::AudioEncoderIlbcImpl(const AudioEncoderIlbcConfig& config,
44 int payload_type)
45 : frame_size_ms_(config.frame_size_ms),
46 payload_type_(payload_type),
47 num_10ms_frames_per_packet_(
48 static_cast<size_t>(config.frame_size_ms / 10)),
49 encoder_(nullptr) {
50 RTC_CHECK(config.IsOk());
51 Reset();
52 }
53
~AudioEncoderIlbcImpl()54 AudioEncoderIlbcImpl::~AudioEncoderIlbcImpl() {
55 RTC_CHECK_EQ(0, WebRtcIlbcfix_EncoderFree(encoder_));
56 }
57
SampleRateHz() const58 int AudioEncoderIlbcImpl::SampleRateHz() const {
59 return kSampleRateHz;
60 }
61
NumChannels() const62 size_t AudioEncoderIlbcImpl::NumChannels() const {
63 return 1;
64 }
65
Num10MsFramesInNextPacket() const66 size_t AudioEncoderIlbcImpl::Num10MsFramesInNextPacket() const {
67 return num_10ms_frames_per_packet_;
68 }
69
Max10MsFramesInAPacket() const70 size_t AudioEncoderIlbcImpl::Max10MsFramesInAPacket() const {
71 return num_10ms_frames_per_packet_;
72 }
73
GetTargetBitrate() const74 int AudioEncoderIlbcImpl::GetTargetBitrate() const {
75 return GetIlbcBitrate(rtc::dchecked_cast<int>(num_10ms_frames_per_packet_) *
76 10);
77 }
78
EncodeImpl(uint32_t rtp_timestamp,rtc::ArrayView<const int16_t> audio,rtc::Buffer * encoded)79 AudioEncoder::EncodedInfo AudioEncoderIlbcImpl::EncodeImpl(
80 uint32_t rtp_timestamp,
81 rtc::ArrayView<const int16_t> audio,
82 rtc::Buffer* encoded) {
83 // Save timestamp if starting a new packet.
84 if (num_10ms_frames_buffered_ == 0)
85 first_timestamp_in_buffer_ = rtp_timestamp;
86
87 // Buffer input.
88 std::copy(audio.cbegin(), audio.cend(),
89 input_buffer_ + kSampleRateHz / 100 * num_10ms_frames_buffered_);
90
91 // If we don't yet have enough buffered input for a whole packet, we're done
92 // for now.
93 if (++num_10ms_frames_buffered_ < num_10ms_frames_per_packet_) {
94 return EncodedInfo();
95 }
96
97 // Encode buffered input.
98 RTC_DCHECK_EQ(num_10ms_frames_buffered_, num_10ms_frames_per_packet_);
99 num_10ms_frames_buffered_ = 0;
100 size_t encoded_bytes = encoded->AppendData(
101 RequiredOutputSizeBytes(), [&](rtc::ArrayView<uint8_t> encoded) {
102 const int r = WebRtcIlbcfix_Encode(
103 encoder_, input_buffer_,
104 kSampleRateHz / 100 * num_10ms_frames_per_packet_, encoded.data());
105 RTC_CHECK_GE(r, 0);
106
107 return static_cast<size_t>(r);
108 });
109
110 RTC_DCHECK_EQ(encoded_bytes, RequiredOutputSizeBytes());
111
112 EncodedInfo info;
113 info.encoded_bytes = encoded_bytes;
114 info.encoded_timestamp = first_timestamp_in_buffer_;
115 info.payload_type = payload_type_;
116 info.encoder_type = CodecType::kIlbc;
117 return info;
118 }
119
Reset()120 void AudioEncoderIlbcImpl::Reset() {
121 if (encoder_)
122 RTC_CHECK_EQ(0, WebRtcIlbcfix_EncoderFree(encoder_));
123 RTC_CHECK_EQ(0, WebRtcIlbcfix_EncoderCreate(&encoder_));
124 const int encoder_frame_size_ms =
125 frame_size_ms_ > 30 ? frame_size_ms_ / 2 : frame_size_ms_;
126 RTC_CHECK_EQ(0, WebRtcIlbcfix_EncoderInit(encoder_, encoder_frame_size_ms));
127 num_10ms_frames_buffered_ = 0;
128 }
129
130 absl::optional<std::pair<TimeDelta, TimeDelta>>
GetFrameLengthRange() const131 AudioEncoderIlbcImpl::GetFrameLengthRange() const {
132 return {{TimeDelta::Millis(num_10ms_frames_per_packet_ * 10),
133 TimeDelta::Millis(num_10ms_frames_per_packet_ * 10)}};
134 }
135
RequiredOutputSizeBytes() const136 size_t AudioEncoderIlbcImpl::RequiredOutputSizeBytes() const {
137 switch (num_10ms_frames_per_packet_) {
138 case 2:
139 return 38;
140 case 3:
141 return 50;
142 case 4:
143 return 2 * 38;
144 case 6:
145 return 2 * 50;
146 default:
147 FATAL();
148 }
149 }
150
151 } // namespace webrtc
152