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/g722/audio_encoder_g722.h"
12 
13 #include <cstdint>
14 
15 #include "modules/audio_coding/codecs/g722/g722_interface.h"
16 #include "rtc_base/checks.h"
17 #include "rtc_base/numerics/safe_conversions.h"
18 
19 namespace webrtc {
20 
21 namespace {
22 
23 const size_t kSampleRateHz = 16000;
24 
25 }  // namespace
26 
AudioEncoderG722Impl(const AudioEncoderG722Config & config,int payload_type)27 AudioEncoderG722Impl::AudioEncoderG722Impl(const AudioEncoderG722Config& config,
28                                            int payload_type)
29     : num_channels_(config.num_channels),
30       payload_type_(payload_type),
31       num_10ms_frames_per_packet_(
32           static_cast<size_t>(config.frame_size_ms / 10)),
33       num_10ms_frames_buffered_(0),
34       first_timestamp_in_buffer_(0),
35       encoders_(new EncoderState[num_channels_]),
36       interleave_buffer_(2 * num_channels_) {
37   RTC_CHECK(config.IsOk());
38   const size_t samples_per_channel =
39       kSampleRateHz / 100 * num_10ms_frames_per_packet_;
40   for (size_t i = 0; i < num_channels_; ++i) {
41     encoders_[i].speech_buffer.reset(new int16_t[samples_per_channel]);
42     encoders_[i].encoded_buffer.SetSize(samples_per_channel / 2);
43   }
44   Reset();
45 }
46 
47 AudioEncoderG722Impl::~AudioEncoderG722Impl() = default;
48 
SampleRateHz() const49 int AudioEncoderG722Impl::SampleRateHz() const {
50   return kSampleRateHz;
51 }
52 
NumChannels() const53 size_t AudioEncoderG722Impl::NumChannels() const {
54   return num_channels_;
55 }
56 
RtpTimestampRateHz() const57 int AudioEncoderG722Impl::RtpTimestampRateHz() const {
58   // The RTP timestamp rate for G.722 is 8000 Hz, even though it is a 16 kHz
59   // codec.
60   return kSampleRateHz / 2;
61 }
62 
Num10MsFramesInNextPacket() const63 size_t AudioEncoderG722Impl::Num10MsFramesInNextPacket() const {
64   return num_10ms_frames_per_packet_;
65 }
66 
Max10MsFramesInAPacket() const67 size_t AudioEncoderG722Impl::Max10MsFramesInAPacket() const {
68   return num_10ms_frames_per_packet_;
69 }
70 
GetTargetBitrate() const71 int AudioEncoderG722Impl::GetTargetBitrate() const {
72   // 4 bits/sample, 16000 samples/s/channel.
73   return static_cast<int>(64000 * NumChannels());
74 }
75 
Reset()76 void AudioEncoderG722Impl::Reset() {
77   num_10ms_frames_buffered_ = 0;
78   for (size_t i = 0; i < num_channels_; ++i)
79     RTC_CHECK_EQ(0, WebRtcG722_EncoderInit(encoders_[i].encoder));
80 }
81 
82 absl::optional<std::pair<TimeDelta, TimeDelta>>
GetFrameLengthRange() const83 AudioEncoderG722Impl::GetFrameLengthRange() const {
84   return {{TimeDelta::Millis(num_10ms_frames_per_packet_ * 10),
85            TimeDelta::Millis(num_10ms_frames_per_packet_ * 10)}};
86 }
87 
EncodeImpl(uint32_t rtp_timestamp,rtc::ArrayView<const int16_t> audio,rtc::Buffer * encoded)88 AudioEncoder::EncodedInfo AudioEncoderG722Impl::EncodeImpl(
89     uint32_t rtp_timestamp,
90     rtc::ArrayView<const int16_t> audio,
91     rtc::Buffer* encoded) {
92   if (num_10ms_frames_buffered_ == 0)
93     first_timestamp_in_buffer_ = rtp_timestamp;
94 
95   // Deinterleave samples and save them in each channel's buffer.
96   const size_t start = kSampleRateHz / 100 * num_10ms_frames_buffered_;
97   for (size_t i = 0; i < kSampleRateHz / 100; ++i)
98     for (size_t j = 0; j < num_channels_; ++j)
99       encoders_[j].speech_buffer[start + i] = audio[i * num_channels_ + j];
100 
101   // If we don't yet have enough samples for a packet, we're done for now.
102   if (++num_10ms_frames_buffered_ < num_10ms_frames_per_packet_) {
103     return EncodedInfo();
104   }
105 
106   // Encode each channel separately.
107   RTC_CHECK_EQ(num_10ms_frames_buffered_, num_10ms_frames_per_packet_);
108   num_10ms_frames_buffered_ = 0;
109   const size_t samples_per_channel = SamplesPerChannel();
110   for (size_t i = 0; i < num_channels_; ++i) {
111     const size_t bytes_encoded = WebRtcG722_Encode(
112         encoders_[i].encoder, encoders_[i].speech_buffer.get(),
113         samples_per_channel, encoders_[i].encoded_buffer.data());
114     RTC_CHECK_EQ(bytes_encoded, samples_per_channel / 2);
115   }
116 
117   const size_t bytes_to_encode = samples_per_channel / 2 * num_channels_;
118   EncodedInfo info;
119   info.encoded_bytes = encoded->AppendData(
120       bytes_to_encode, [&](rtc::ArrayView<uint8_t> encoded) {
121         // Interleave the encoded bytes of the different channels. Each separate
122         // channel and the interleaved stream encodes two samples per byte, most
123         // significant half first.
124         for (size_t i = 0; i < samples_per_channel / 2; ++i) {
125           for (size_t j = 0; j < num_channels_; ++j) {
126             uint8_t two_samples = encoders_[j].encoded_buffer.data()[i];
127             interleave_buffer_.data()[j] = two_samples >> 4;
128             interleave_buffer_.data()[num_channels_ + j] = two_samples & 0xf;
129           }
130           for (size_t j = 0; j < num_channels_; ++j)
131             encoded[i * num_channels_ + j] =
132                 interleave_buffer_.data()[2 * j] << 4 |
133                 interleave_buffer_.data()[2 * j + 1];
134         }
135 
136         return bytes_to_encode;
137       });
138   info.encoded_timestamp = first_timestamp_in_buffer_;
139   info.payload_type = payload_type_;
140   info.encoder_type = CodecType::kG722;
141   return info;
142 }
143 
EncoderState()144 AudioEncoderG722Impl::EncoderState::EncoderState() {
145   RTC_CHECK_EQ(0, WebRtcG722_CreateEncoder(&encoder));
146 }
147 
~EncoderState()148 AudioEncoderG722Impl::EncoderState::~EncoderState() {
149   RTC_CHECK_EQ(0, WebRtcG722_FreeEncoder(encoder));
150 }
151 
SamplesPerChannel() const152 size_t AudioEncoderG722Impl::SamplesPerChannel() const {
153   return kSampleRateHz / 100 * num_10ms_frames_per_packet_;
154 }
155 
156 }  // namespace webrtc
157