| 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/legacy_encoded_audio_frame.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::ParseResult> AudioDecoderG722::ParsePayload( |
| 52 rtc::Buffer&& payload, |
| 53 uint32_t timestamp, |
| 54 bool is_primary) { |
| 55 return LegacyEncodedAudioFrame::SplitBySamples(this, std::move(payload), |
| 56 timestamp, is_primary, 8, 16); |
| 57 } |
| 58 |
| 50 int AudioDecoderG722::PacketDuration(const uint8_t* encoded, | 59 int AudioDecoderG722::PacketDuration(const uint8_t* encoded, |
| 51 size_t encoded_len) const { | 60 size_t encoded_len) const { |
| 52 // 1/2 encoded byte per sample per channel. | 61 // 1/2 encoded byte per sample per channel. |
| 53 return static_cast<int>(2 * encoded_len / Channels()); | 62 return static_cast<int>(2 * encoded_len / Channels()); |
| 54 } | 63 } |
| 55 | 64 |
| 56 int AudioDecoderG722::SampleRateHz() const { | 65 int AudioDecoderG722::SampleRateHz() const { |
| 57 return 16000; | 66 return 16000; |
| 58 } | 67 } |
| 59 | 68 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 | 119 |
| 111 size_t AudioDecoderG722Stereo::Channels() const { | 120 size_t AudioDecoderG722Stereo::Channels() const { |
| 112 return 2; | 121 return 2; |
| 113 } | 122 } |
| 114 | 123 |
| 115 void AudioDecoderG722Stereo::Reset() { | 124 void AudioDecoderG722Stereo::Reset() { |
| 116 WebRtcG722_DecoderInit(dec_state_left_); | 125 WebRtcG722_DecoderInit(dec_state_left_); |
| 117 WebRtcG722_DecoderInit(dec_state_right_); | 126 WebRtcG722_DecoderInit(dec_state_right_); |
| 118 } | 127 } |
| 119 | 128 |
| 129 std::vector<AudioDecoder::ParseResult> AudioDecoderG722Stereo::ParsePayload( |
| 130 rtc::Buffer&& payload, |
| 131 uint32_t timestamp, |
| 132 bool is_primary) { |
| 133 return LegacyEncodedAudioFrame::SplitBySamples( |
| 134 this, std::move(payload), timestamp, is_primary, 2 * 8, 16); |
| 135 } |
| 136 |
| 120 // Split the stereo packet and place left and right channel after each other | 137 // Split the stereo packet and place left and right channel after each other |
| 121 // in the output array. | 138 // in the output array. |
| 122 void AudioDecoderG722Stereo::SplitStereoPacket(const uint8_t* encoded, | 139 void AudioDecoderG722Stereo::SplitStereoPacket(const uint8_t* encoded, |
| 123 size_t encoded_len, | 140 size_t encoded_len, |
| 124 uint8_t* encoded_deinterleaved) { | 141 uint8_t* encoded_deinterleaved) { |
| 125 // Regroup the 4 bits/sample so |l1 l2| |r1 r2| |l3 l4| |r3 r4| ..., | 142 // 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 | 143 // where "lx" is 4 bits representing left sample number x, and "rx" right |
| 127 // sample. Two samples fit in one byte, represented with |...|. | 144 // sample. Two samples fit in one byte, represented with |...|. |
| 128 for (size_t i = 0; i + 1 < encoded_len; i += 2) { | 145 for (size_t i = 0; i + 1 < encoded_len; i += 2) { |
| 129 uint8_t right_byte = ((encoded[i] & 0x0F) << 4) + (encoded[i + 1] & 0x0F); | 146 uint8_t right_byte = ((encoded[i] & 0x0F) << 4) + (encoded[i + 1] & 0x0F); |
| 130 encoded_deinterleaved[i] = (encoded[i] & 0xF0) + (encoded[i + 1] >> 4); | 147 encoded_deinterleaved[i] = (encoded[i] & 0xF0) + (encoded[i + 1] >> 4); |
| 131 encoded_deinterleaved[i + 1] = right_byte; | 148 encoded_deinterleaved[i + 1] = right_byte; |
| 132 } | 149 } |
| 133 | 150 |
| 134 // Move one byte representing right channel each loop, and place it at the | 151 // 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: | 152 // 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)|, | 153 // |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. | 154 // where N is the total number of samples. |
| 138 for (size_t i = 0; i < encoded_len / 2; i++) { | 155 for (size_t i = 0; i < encoded_len / 2; i++) { |
| 139 uint8_t right_byte = encoded_deinterleaved[i + 1]; | 156 uint8_t right_byte = encoded_deinterleaved[i + 1]; |
| 140 memmove(&encoded_deinterleaved[i + 1], &encoded_deinterleaved[i + 2], | 157 memmove(&encoded_deinterleaved[i + 1], &encoded_deinterleaved[i + 2], |
| 141 encoded_len - i - 2); | 158 encoded_len - i - 2); |
| 142 encoded_deinterleaved[encoded_len - 1] = right_byte; | 159 encoded_deinterleaved[encoded_len - 1] = right_byte; |
| 143 } | 160 } |
| 144 } | 161 } |
| 145 | 162 |
| 146 } // namespace webrtc | 163 } // namespace webrtc |
| OLD | NEW |