1 /*
2 * Copyright (c) 2015 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_decoder_g722.h"
12
13 #include <string.h>
14
15 #include <utility>
16
17 #include "modules/audio_coding/codecs/g722/g722_interface.h"
18 #include "modules/audio_coding/codecs/legacy_encoded_audio_frame.h"
19 #include "rtc_base/checks.h"
20
21 namespace webrtc {
22
AudioDecoderG722Impl()23 AudioDecoderG722Impl::AudioDecoderG722Impl() {
24 WebRtcG722_CreateDecoder(&dec_state_);
25 WebRtcG722_DecoderInit(dec_state_);
26 }
27
~AudioDecoderG722Impl()28 AudioDecoderG722Impl::~AudioDecoderG722Impl() {
29 WebRtcG722_FreeDecoder(dec_state_);
30 }
31
HasDecodePlc() const32 bool AudioDecoderG722Impl::HasDecodePlc() const {
33 return false;
34 }
35
DecodeInternal(const uint8_t * encoded,size_t encoded_len,int sample_rate_hz,int16_t * decoded,SpeechType * speech_type)36 int AudioDecoderG722Impl::DecodeInternal(const uint8_t* encoded,
37 size_t encoded_len,
38 int sample_rate_hz,
39 int16_t* decoded,
40 SpeechType* speech_type) {
41 RTC_DCHECK_EQ(SampleRateHz(), sample_rate_hz);
42 int16_t temp_type = 1; // Default is speech.
43 size_t ret =
44 WebRtcG722_Decode(dec_state_, encoded, encoded_len, decoded, &temp_type);
45 *speech_type = ConvertSpeechType(temp_type);
46 return static_cast<int>(ret);
47 }
48
Reset()49 void AudioDecoderG722Impl::Reset() {
50 WebRtcG722_DecoderInit(dec_state_);
51 }
52
ParsePayload(rtc::Buffer && payload,uint32_t timestamp)53 std::vector<AudioDecoder::ParseResult> AudioDecoderG722Impl::ParsePayload(
54 rtc::Buffer&& payload,
55 uint32_t timestamp) {
56 return LegacyEncodedAudioFrame::SplitBySamples(this, std::move(payload),
57 timestamp, 8, 16);
58 }
59
PacketDuration(const uint8_t * encoded,size_t encoded_len) const60 int AudioDecoderG722Impl::PacketDuration(const uint8_t* encoded,
61 size_t encoded_len) const {
62 // 1/2 encoded byte per sample per channel.
63 return static_cast<int>(2 * encoded_len / Channels());
64 }
65
SampleRateHz() const66 int AudioDecoderG722Impl::SampleRateHz() const {
67 return 16000;
68 }
69
Channels() const70 size_t AudioDecoderG722Impl::Channels() const {
71 return 1;
72 }
73
AudioDecoderG722StereoImpl()74 AudioDecoderG722StereoImpl::AudioDecoderG722StereoImpl() {
75 WebRtcG722_CreateDecoder(&dec_state_left_);
76 WebRtcG722_CreateDecoder(&dec_state_right_);
77 WebRtcG722_DecoderInit(dec_state_left_);
78 WebRtcG722_DecoderInit(dec_state_right_);
79 }
80
~AudioDecoderG722StereoImpl()81 AudioDecoderG722StereoImpl::~AudioDecoderG722StereoImpl() {
82 WebRtcG722_FreeDecoder(dec_state_left_);
83 WebRtcG722_FreeDecoder(dec_state_right_);
84 }
85
DecodeInternal(const uint8_t * encoded,size_t encoded_len,int sample_rate_hz,int16_t * decoded,SpeechType * speech_type)86 int AudioDecoderG722StereoImpl::DecodeInternal(const uint8_t* encoded,
87 size_t encoded_len,
88 int sample_rate_hz,
89 int16_t* decoded,
90 SpeechType* speech_type) {
91 RTC_DCHECK_EQ(SampleRateHz(), sample_rate_hz);
92 // Adjust the encoded length down to ensure the same number of samples in each
93 // channel.
94 const size_t encoded_len_adjusted = PacketDuration(encoded, encoded_len) *
95 Channels() /
96 2; // 1/2 byte per sample per channel
97 int16_t temp_type = 1; // Default is speech.
98 // De-interleave the bit-stream into two separate payloads.
99 uint8_t* encoded_deinterleaved = new uint8_t[encoded_len_adjusted];
100 SplitStereoPacket(encoded, encoded_len_adjusted, encoded_deinterleaved);
101 // Decode left and right.
102 size_t decoded_len =
103 WebRtcG722_Decode(dec_state_left_, encoded_deinterleaved,
104 encoded_len_adjusted / 2, decoded, &temp_type);
105 size_t ret = WebRtcG722_Decode(
106 dec_state_right_, &encoded_deinterleaved[encoded_len_adjusted / 2],
107 encoded_len_adjusted / 2, &decoded[decoded_len], &temp_type);
108 if (ret == decoded_len) {
109 ret += decoded_len; // Return total number of samples.
110 // Interleave output.
111 for (size_t k = ret / 2; k < ret; k++) {
112 int16_t temp = decoded[k];
113 memmove(&decoded[2 * k - ret + 2], &decoded[2 * k - ret + 1],
114 (ret - k - 1) * sizeof(int16_t));
115 decoded[2 * k - ret + 1] = temp;
116 }
117 }
118 *speech_type = ConvertSpeechType(temp_type);
119 delete[] encoded_deinterleaved;
120 return static_cast<int>(ret);
121 }
122
PacketDuration(const uint8_t * encoded,size_t encoded_len) const123 int AudioDecoderG722StereoImpl::PacketDuration(const uint8_t* encoded,
124 size_t encoded_len) const {
125 // 1/2 encoded byte per sample per channel. Make sure the length represents
126 // an equal number of bytes per channel. Otherwise, we cannot de-interleave
127 // the encoded data later.
128 return static_cast<int>(2 * (encoded_len / Channels()));
129 }
130
SampleRateHz() const131 int AudioDecoderG722StereoImpl::SampleRateHz() const {
132 return 16000;
133 }
134
Channels() const135 size_t AudioDecoderG722StereoImpl::Channels() const {
136 return 2;
137 }
138
Reset()139 void AudioDecoderG722StereoImpl::Reset() {
140 WebRtcG722_DecoderInit(dec_state_left_);
141 WebRtcG722_DecoderInit(dec_state_right_);
142 }
143
ParsePayload(rtc::Buffer && payload,uint32_t timestamp)144 std::vector<AudioDecoder::ParseResult> AudioDecoderG722StereoImpl::ParsePayload(
145 rtc::Buffer&& payload,
146 uint32_t timestamp) {
147 return LegacyEncodedAudioFrame::SplitBySamples(this, std::move(payload),
148 timestamp, 2 * 8, 16);
149 }
150
151 // Split the stereo packet and place left and right channel after each other
152 // in the output array.
SplitStereoPacket(const uint8_t * encoded,size_t encoded_len,uint8_t * encoded_deinterleaved)153 void AudioDecoderG722StereoImpl::SplitStereoPacket(
154 const uint8_t* encoded,
155 size_t encoded_len,
156 uint8_t* encoded_deinterleaved) {
157 // Regroup the 4 bits/sample so |l1 l2| |r1 r2| |l3 l4| |r3 r4| ...,
158 // where "lx" is 4 bits representing left sample number x, and "rx" right
159 // sample. Two samples fit in one byte, represented with |...|.
160 for (size_t i = 0; i + 1 < encoded_len; i += 2) {
161 uint8_t right_byte = ((encoded[i] & 0x0F) << 4) + (encoded[i + 1] & 0x0F);
162 encoded_deinterleaved[i] = (encoded[i] & 0xF0) + (encoded[i + 1] >> 4);
163 encoded_deinterleaved[i + 1] = right_byte;
164 }
165
166 // Move one byte representing right channel each loop, and place it at the
167 // end of the bytestream vector. After looping the data is reordered to:
168 // |l1 l2| |l3 l4| ... |l(N-1) lN| |r1 r2| |r3 r4| ... |r(N-1) r(N)|,
169 // where N is the total number of samples.
170 for (size_t i = 0; i < encoded_len / 2; i++) {
171 uint8_t right_byte = encoded_deinterleaved[i + 1];
172 memmove(&encoded_deinterleaved[i + 1], &encoded_deinterleaved[i + 2],
173 encoded_len - i - 2);
174 encoded_deinterleaved[encoded_len - 1] = right_byte;
175 }
176 }
177
178 } // namespace webrtc
179