OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 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 |
11 #include "webrtc/modules/audio_coding/codecs/g722/audio_decoder_g722.h" | 11 #include "webrtc/modules/audio_coding/codecs/g722/audio_decoder_g722.h" |
12 | 12 |
13 #include <string.h> | 13 #include <string.h> |
14 | 14 |
15 #include "webrtc/base/checks.h" | 15 #include "webrtc/base/checks.h" |
| 16 #include "webrtc/modules/audio_coding/codecs/split_by_samples.h" |
16 #include "webrtc/modules/audio_coding/codecs/g722/g722_interface.h" | 17 #include "webrtc/modules/audio_coding/codecs/g722/g722_interface.h" |
17 | 18 |
18 namespace webrtc { | 19 namespace webrtc { |
19 | 20 |
20 AudioDecoderG722::AudioDecoderG722() { | 21 AudioDecoderG722::AudioDecoderG722() { |
21 WebRtcG722_CreateDecoder(&dec_state_); | 22 WebRtcG722_CreateDecoder(&dec_state_); |
22 WebRtcG722_DecoderInit(dec_state_); | 23 WebRtcG722_DecoderInit(dec_state_); |
23 } | 24 } |
24 | 25 |
25 AudioDecoderG722::~AudioDecoderG722() { | 26 AudioDecoderG722::~AudioDecoderG722() { |
(...skipping 14 matching lines...) Expand all Loading... |
40 size_t ret = | 41 size_t ret = |
41 WebRtcG722_Decode(dec_state_, encoded, encoded_len, decoded, &temp_type); | 42 WebRtcG722_Decode(dec_state_, encoded, encoded_len, decoded, &temp_type); |
42 *speech_type = ConvertSpeechType(temp_type); | 43 *speech_type = ConvertSpeechType(temp_type); |
43 return static_cast<int>(ret); | 44 return static_cast<int>(ret); |
44 } | 45 } |
45 | 46 |
46 void AudioDecoderG722::Reset() { | 47 void AudioDecoderG722::Reset() { |
47 WebRtcG722_DecoderInit(dec_state_); | 48 WebRtcG722_DecoderInit(dec_state_); |
48 } | 49 } |
49 | 50 |
| 51 std::vector<AudioDecoder::PacketSplit> AudioDecoderG722::SplitPacket( |
| 52 rtc::ArrayView<const uint8_t> payload) const { |
| 53 return internal::SplitBySamples(payload, 8, 16); |
| 54 } |
| 55 |
50 int AudioDecoderG722::PacketDuration(const uint8_t* encoded, | 56 int AudioDecoderG722::PacketDuration(const uint8_t* encoded, |
51 size_t encoded_len) const { | 57 size_t encoded_len) const { |
52 // 1/2 encoded byte per sample per channel. | 58 // 1/2 encoded byte per sample per channel. |
53 return static_cast<int>(2 * encoded_len / Channels()); | 59 return static_cast<int>(2 * encoded_len / Channels()); |
54 } | 60 } |
55 | 61 |
56 int AudioDecoderG722::SampleRateHz() const { | 62 int AudioDecoderG722::SampleRateHz() const { |
57 return 16000; | 63 return 16000; |
58 } | 64 } |
59 | 65 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 | 116 |
111 size_t AudioDecoderG722Stereo::Channels() const { | 117 size_t AudioDecoderG722Stereo::Channels() const { |
112 return 2; | 118 return 2; |
113 } | 119 } |
114 | 120 |
115 void AudioDecoderG722Stereo::Reset() { | 121 void AudioDecoderG722Stereo::Reset() { |
116 WebRtcG722_DecoderInit(dec_state_left_); | 122 WebRtcG722_DecoderInit(dec_state_left_); |
117 WebRtcG722_DecoderInit(dec_state_right_); | 123 WebRtcG722_DecoderInit(dec_state_right_); |
118 } | 124 } |
119 | 125 |
| 126 std::vector<AudioDecoder::PacketSplit> AudioDecoderG722Stereo::SplitPacket( |
| 127 rtc::ArrayView<const uint8_t> payload) const { |
| 128 return internal::SplitBySamples(payload, 2 * 8, 16); |
| 129 } |
| 130 |
120 // Split the stereo packet and place left and right channel after each other | 131 // Split the stereo packet and place left and right channel after each other |
121 // in the output array. | 132 // in the output array. |
122 void AudioDecoderG722Stereo::SplitStereoPacket(const uint8_t* encoded, | 133 void AudioDecoderG722Stereo::SplitStereoPacket(const uint8_t* encoded, |
123 size_t encoded_len, | 134 size_t encoded_len, |
124 uint8_t* encoded_deinterleaved) { | 135 uint8_t* encoded_deinterleaved) { |
125 // Regroup the 4 bits/sample so |l1 l2| |r1 r2| |l3 l4| |r3 r4| ..., | 136 // Regroup the 4 bits/sample so |l1 l2| |r1 r2| |l3 l4| |r3 r4| ..., |
126 // where "lx" is 4 bits representing left sample number x, and "rx" right | 137 // where "lx" is 4 bits representing left sample number x, and "rx" right |
127 // sample. Two samples fit in one byte, represented with |...|. | 138 // sample. Two samples fit in one byte, represented with |...|. |
128 for (size_t i = 0; i + 1 < encoded_len; i += 2) { | 139 for (size_t i = 0; i + 1 < encoded_len; i += 2) { |
129 uint8_t right_byte = ((encoded[i] & 0x0F) << 4) + (encoded[i + 1] & 0x0F); | 140 uint8_t right_byte = ((encoded[i] & 0x0F) << 4) + (encoded[i + 1] & 0x0F); |
130 encoded_deinterleaved[i] = (encoded[i] & 0xF0) + (encoded[i + 1] >> 4); | 141 encoded_deinterleaved[i] = (encoded[i] & 0xF0) + (encoded[i + 1] >> 4); |
131 encoded_deinterleaved[i + 1] = right_byte; | 142 encoded_deinterleaved[i + 1] = right_byte; |
132 } | 143 } |
133 | 144 |
134 // Move one byte representing right channel each loop, and place it at the | 145 // Move one byte representing right channel each loop, and place it at the |
135 // end of the bytestream vector. After looping the data is reordered to: | 146 // end of the bytestream vector. After looping the data is reordered to: |
136 // |l1 l2| |l3 l4| ... |l(N-1) lN| |r1 r2| |r3 r4| ... |r(N-1) r(N)|, | 147 // |l1 l2| |l3 l4| ... |l(N-1) lN| |r1 r2| |r3 r4| ... |r(N-1) r(N)|, |
137 // where N is the total number of samples. | 148 // where N is the total number of samples. |
138 for (size_t i = 0; i < encoded_len / 2; i++) { | 149 for (size_t i = 0; i < encoded_len / 2; i++) { |
139 uint8_t right_byte = encoded_deinterleaved[i + 1]; | 150 uint8_t right_byte = encoded_deinterleaved[i + 1]; |
140 memmove(&encoded_deinterleaved[i + 1], &encoded_deinterleaved[i + 2], | 151 memmove(&encoded_deinterleaved[i + 1], &encoded_deinterleaved[i + 2], |
141 encoded_len - i - 2); | 152 encoded_len - i - 2); |
142 encoded_deinterleaved[encoded_len - 1] = right_byte; | 153 encoded_deinterleaved[encoded_len - 1] = right_byte; |
143 } | 154 } |
144 } | 155 } |
145 | 156 |
146 } // namespace webrtc | 157 } // namespace webrtc |
OLD | NEW |