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