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 int16_t temp_type = 1; // Default is speech.
93 // De-interleave the bit-stream into two separate payloads.
94 uint8_t* encoded_deinterleaved = new uint8_t[encoded_len];
95 SplitStereoPacket(encoded, encoded_len, encoded_deinterleaved);
96 // Decode left and right.
97 size_t decoded_len = WebRtcG722_Decode(dec_state_left_, encoded_deinterleaved,
98 encoded_len / 2, decoded, &temp_type);
99 size_t ret = WebRtcG722_Decode(
100 dec_state_right_, &encoded_deinterleaved[encoded_len / 2],
101 encoded_len / 2, &decoded[decoded_len], &temp_type);
102 if (ret == decoded_len) {
103 ret += decoded_len; // Return total number of samples.
104 // Interleave output.
105 for (size_t k = ret / 2; k < ret; k++) {
106 int16_t temp = decoded[k];
107 memmove(&decoded[2 * k - ret + 2], &decoded[2 * k - ret + 1],
108 (ret - k - 1) * sizeof(int16_t));
109 decoded[2 * k - ret + 1] = temp;
110 }
111 }
112 *speech_type = ConvertSpeechType(temp_type);
113 delete[] encoded_deinterleaved;
114 return static_cast<int>(ret);
115 }
116
SampleRateHz() const117 int AudioDecoderG722StereoImpl::SampleRateHz() const {
118 return 16000;
119 }
120
Channels() const121 size_t AudioDecoderG722StereoImpl::Channels() const {
122 return 2;
123 }
124
Reset()125 void AudioDecoderG722StereoImpl::Reset() {
126 WebRtcG722_DecoderInit(dec_state_left_);
127 WebRtcG722_DecoderInit(dec_state_right_);
128 }
129
ParsePayload(rtc::Buffer && payload,uint32_t timestamp)130 std::vector<AudioDecoder::ParseResult> AudioDecoderG722StereoImpl::ParsePayload(
131 rtc::Buffer&& payload,
132 uint32_t timestamp) {
133 return LegacyEncodedAudioFrame::SplitBySamples(this, std::move(payload),
134 timestamp, 2 * 8, 16);
135 }
136
137 // Split the stereo packet and place left and right channel after each other
138 // in the output array.
SplitStereoPacket(const uint8_t * encoded,size_t encoded_len,uint8_t * encoded_deinterleaved)139 void AudioDecoderG722StereoImpl::SplitStereoPacket(
140 const uint8_t* encoded,
141 size_t encoded_len,
142 uint8_t* encoded_deinterleaved) {
143 // Regroup the 4 bits/sample so |l1 l2| |r1 r2| |l3 l4| |r3 r4| ...,
144 // where "lx" is 4 bits representing left sample number x, and "rx" right
145 // sample. Two samples fit in one byte, represented with |...|.
146 for (size_t i = 0; i + 1 < encoded_len; i += 2) {
147 uint8_t right_byte = ((encoded[i] & 0x0F) << 4) + (encoded[i + 1] & 0x0F);
148 encoded_deinterleaved[i] = (encoded[i] & 0xF0) + (encoded[i + 1] >> 4);
149 encoded_deinterleaved[i + 1] = right_byte;
150 }
151
152 // Move one byte representing right channel each loop, and place it at the
153 // end of the bytestream vector. After looping the data is reordered to:
154 // |l1 l2| |l3 l4| ... |l(N-1) lN| |r1 r2| |r3 r4| ... |r(N-1) r(N)|,
155 // where N is the total number of samples.
156 for (size_t i = 0; i < encoded_len / 2; i++) {
157 uint8_t right_byte = encoded_deinterleaved[i + 1];
158 memmove(&encoded_deinterleaved[i + 1], &encoded_deinterleaved[i + 2],
159 encoded_len - i - 2);
160 encoded_deinterleaved[encoded_len - 1] = right_byte;
161 }
162 }
163
164 } // namespace webrtc
165