OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 | 84 |
85 size_t AudioEncoderG722::Max10MsFramesInAPacket() const { | 85 size_t AudioEncoderG722::Max10MsFramesInAPacket() const { |
86 return num_10ms_frames_per_packet_; | 86 return num_10ms_frames_per_packet_; |
87 } | 87 } |
88 | 88 |
89 int AudioEncoderG722::GetTargetBitrate() const { | 89 int AudioEncoderG722::GetTargetBitrate() const { |
90 // 4 bits/sample, 16000 samples/s/channel. | 90 // 4 bits/sample, 16000 samples/s/channel. |
91 return static_cast<int>(64000 * NumChannels()); | 91 return static_cast<int>(64000 * NumChannels()); |
92 } | 92 } |
93 | 93 |
| 94 void AudioEncoderG722::Reset() { |
| 95 num_10ms_frames_buffered_ = 0; |
| 96 for (size_t i = 0; i < num_channels_; ++i) |
| 97 RTC_CHECK_EQ(0, WebRtcG722_EncoderInit(encoders_[i].encoder)); |
| 98 } |
| 99 |
94 AudioEncoder::EncodedInfo AudioEncoderG722::EncodeInternal( | 100 AudioEncoder::EncodedInfo AudioEncoderG722::EncodeInternal( |
95 uint32_t rtp_timestamp, | 101 uint32_t rtp_timestamp, |
96 rtc::ArrayView<const int16_t> audio, | 102 rtc::ArrayView<const int16_t> audio, |
97 size_t max_encoded_bytes, | 103 rtc::Buffer* encoded) { |
98 uint8_t* encoded) { | |
99 RTC_CHECK_GE(max_encoded_bytes, MaxEncodedBytes()); | |
100 | |
101 if (num_10ms_frames_buffered_ == 0) | 104 if (num_10ms_frames_buffered_ == 0) |
102 first_timestamp_in_buffer_ = rtp_timestamp; | 105 first_timestamp_in_buffer_ = rtp_timestamp; |
103 | 106 |
104 // Deinterleave samples and save them in each channel's buffer. | 107 // Deinterleave samples and save them in each channel's buffer. |
105 const size_t start = kSampleRateHz / 100 * num_10ms_frames_buffered_; | 108 const size_t start = kSampleRateHz / 100 * num_10ms_frames_buffered_; |
106 for (size_t i = 0; i < kSampleRateHz / 100; ++i) | 109 for (size_t i = 0; i < kSampleRateHz / 100; ++i) |
107 for (size_t j = 0; j < num_channels_; ++j) | 110 for (size_t j = 0; j < num_channels_; ++j) |
108 encoders_[j].speech_buffer[start + i] = audio[i * num_channels_ + j]; | 111 encoders_[j].speech_buffer[start + i] = audio[i * num_channels_ + j]; |
109 | 112 |
110 // If we don't yet have enough samples for a packet, we're done for now. | 113 // 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_) { | 114 if (++num_10ms_frames_buffered_ < num_10ms_frames_per_packet_) { |
112 return EncodedInfo(); | 115 return EncodedInfo(); |
113 } | 116 } |
114 | 117 |
115 // Encode each channel separately. | 118 // Encode each channel separately. |
116 RTC_CHECK_EQ(num_10ms_frames_buffered_, num_10ms_frames_per_packet_); | 119 RTC_CHECK_EQ(num_10ms_frames_buffered_, num_10ms_frames_per_packet_); |
117 num_10ms_frames_buffered_ = 0; | 120 num_10ms_frames_buffered_ = 0; |
118 const size_t samples_per_channel = SamplesPerChannel(); | 121 const size_t samples_per_channel = SamplesPerChannel(); |
119 for (size_t i = 0; i < num_channels_; ++i) { | 122 for (size_t i = 0; i < num_channels_; ++i) { |
120 const size_t encoded = WebRtcG722_Encode( | 123 const size_t bytes_encoded = WebRtcG722_Encode( |
121 encoders_[i].encoder, encoders_[i].speech_buffer.get(), | 124 encoders_[i].encoder, encoders_[i].speech_buffer.get(), |
122 samples_per_channel, encoders_[i].encoded_buffer.data()); | 125 samples_per_channel, encoders_[i].encoded_buffer.data()); |
123 RTC_CHECK_EQ(encoded, samples_per_channel / 2); | 126 RTC_CHECK_EQ(bytes_encoded, samples_per_channel / 2); |
124 } | 127 } |
125 | 128 |
126 // Interleave the encoded bytes of the different channels. Each separate | 129 const size_t bytes_to_encode = samples_per_channel / 2 * num_channels_; |
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; | 130 EncodedInfo info; |
140 info.encoded_bytes = samples_per_channel / 2 * num_channels_; | 131 info.encoded_bytes = encoded->AppendData( |
| 132 bytes_to_encode, [&] (rtc::ArrayView<uint8_t> encoded) { |
| 133 // Interleave the encoded bytes of the different channels. Each separate |
| 134 // channel and the interleaved stream encodes two samples per byte, most |
| 135 // significant half first. |
| 136 for (size_t i = 0; i < samples_per_channel / 2; ++i) { |
| 137 for (size_t j = 0; j < num_channels_; ++j) { |
| 138 uint8_t two_samples = encoders_[j].encoded_buffer.data()[i]; |
| 139 interleave_buffer_.data()[j] = two_samples >> 4; |
| 140 interleave_buffer_.data()[num_channels_ + j] = two_samples & 0xf; |
| 141 } |
| 142 for (size_t j = 0; j < num_channels_; ++j) |
| 143 encoded[i * num_channels_ + j] = |
| 144 interleave_buffer_.data()[2 * j] << 4 | |
| 145 interleave_buffer_.data()[2 * j + 1]; |
| 146 } |
| 147 |
| 148 return bytes_to_encode; |
| 149 }); |
141 info.encoded_timestamp = first_timestamp_in_buffer_; | 150 info.encoded_timestamp = first_timestamp_in_buffer_; |
142 info.payload_type = payload_type_; | 151 info.payload_type = payload_type_; |
143 return info; | 152 return info; |
144 } | 153 } |
145 | 154 |
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 | |
152 AudioEncoderG722::EncoderState::EncoderState() { | 155 AudioEncoderG722::EncoderState::EncoderState() { |
153 RTC_CHECK_EQ(0, WebRtcG722_CreateEncoder(&encoder)); | 156 RTC_CHECK_EQ(0, WebRtcG722_CreateEncoder(&encoder)); |
154 } | 157 } |
155 | 158 |
156 AudioEncoderG722::EncoderState::~EncoderState() { | 159 AudioEncoderG722::EncoderState::~EncoderState() { |
157 RTC_CHECK_EQ(0, WebRtcG722_FreeEncoder(encoder)); | 160 RTC_CHECK_EQ(0, WebRtcG722_FreeEncoder(encoder)); |
158 } | 161 } |
159 | 162 |
160 size_t AudioEncoderG722::SamplesPerChannel() const { | 163 size_t AudioEncoderG722::SamplesPerChannel() const { |
161 return kSampleRateHz / 100 * num_10ms_frames_per_packet_; | 164 return kSampleRateHz / 100 * num_10ms_frames_per_packet_; |
162 } | 165 } |
163 | 166 |
164 } // namespace webrtc | 167 } // namespace webrtc |
OLD | NEW |