audio_decoder_pcm16b.cc (2980B)
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/pcm16b/audio_decoder_pcm16b.h" 12 13 #include <cstddef> 14 #include <cstdint> 15 #include <utility> 16 #include <vector> 17 18 #include "api/audio_codecs/audio_decoder.h" 19 #include "modules/audio_coding/codecs/legacy_encoded_audio_frame.h" 20 #include "modules/audio_coding/codecs/pcm16b/pcm16b.h" 21 #include "rtc_base/buffer.h" 22 #include "rtc_base/checks.h" 23 24 namespace webrtc { 25 26 AudioDecoderPcm16B::AudioDecoderPcm16B(int sample_rate_hz, size_t num_channels) 27 : sample_rate_hz_(sample_rate_hz), num_channels_(num_channels) { 28 RTC_DCHECK(sample_rate_hz == 8000 || sample_rate_hz == 16000 || 29 sample_rate_hz == 32000 || sample_rate_hz == 48000) 30 << "Unsupported sample rate " << sample_rate_hz; 31 RTC_DCHECK_GE(num_channels, 1); 32 RTC_DCHECK_LE(num_channels, AudioDecoder::kMaxNumberOfChannels); 33 } 34 35 void AudioDecoderPcm16B::Reset() {} 36 37 int AudioDecoderPcm16B::SampleRateHz() const { 38 return sample_rate_hz_; 39 } 40 41 size_t AudioDecoderPcm16B::Channels() const { 42 return num_channels_; 43 } 44 45 int AudioDecoderPcm16B::DecodeInternal(const uint8_t* encoded, 46 size_t encoded_len, 47 int sample_rate_hz, 48 int16_t* decoded, 49 SpeechType* speech_type) { 50 RTC_DCHECK_EQ(sample_rate_hz_, sample_rate_hz); 51 // Adjust the encoded length down to ensure the same number of samples in each 52 // channel. 53 const size_t encoded_len_adjusted = 54 PacketDuration(encoded, encoded_len) * 2 * 55 Channels(); // 2 bytes per sample per channel 56 size_t ret = WebRtcPcm16b_Decode(encoded, encoded_len_adjusted, decoded); 57 *speech_type = ConvertSpeechType(1); 58 return static_cast<int>(ret); 59 } 60 61 std::vector<AudioDecoder::ParseResult> AudioDecoderPcm16B::ParsePayload( 62 Buffer&& payload, 63 uint32_t timestamp) { 64 const int samples_per_ms = CheckedDivExact(sample_rate_hz_, 1000); 65 return LegacyEncodedAudioFrame::SplitBySamples( 66 this, std::move(payload), timestamp, samples_per_ms * 2 * num_channels_, 67 samples_per_ms); 68 } 69 70 int AudioDecoderPcm16B::PacketDuration(const uint8_t* /* encoded */, 71 size_t encoded_len) const { 72 // Two encoded byte per sample per channel. 73 return static_cast<int>(encoded_len / (2 * Channels())); 74 } 75 76 int AudioDecoderPcm16B::PacketDurationRedundant(const uint8_t* encoded, 77 size_t encoded_len) const { 78 return PacketDuration(encoded, encoded_len); 79 } 80 81 } // namespace webrtc