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/g711/audio_decoder_pcm.h"
12 
13 #include <utility>
14 
15 #include "modules/audio_coding/codecs/g711/g711_interface.h"
16 #include "modules/audio_coding/codecs/legacy_encoded_audio_frame.h"
17 
18 namespace webrtc {
19 
Reset()20 void AudioDecoderPcmU::Reset() {}
21 
ParsePayload(rtc::Buffer && payload,uint32_t timestamp)22 std::vector<AudioDecoder::ParseResult> AudioDecoderPcmU::ParsePayload(
23     rtc::Buffer&& payload,
24     uint32_t timestamp) {
25   return LegacyEncodedAudioFrame::SplitBySamples(
26       this, std::move(payload), timestamp, 8 * num_channels_, 8);
27 }
28 
SampleRateHz() const29 int AudioDecoderPcmU::SampleRateHz() const {
30   return 8000;
31 }
32 
Channels() const33 size_t AudioDecoderPcmU::Channels() const {
34   return num_channels_;
35 }
36 
DecodeInternal(const uint8_t * encoded,size_t encoded_len,int sample_rate_hz,int16_t * decoded,SpeechType * speech_type)37 int AudioDecoderPcmU::DecodeInternal(const uint8_t* encoded,
38                                      size_t encoded_len,
39                                      int sample_rate_hz,
40                                      int16_t* decoded,
41                                      SpeechType* speech_type) {
42   RTC_DCHECK_EQ(SampleRateHz(), sample_rate_hz);
43   int16_t temp_type = 1;  // Default is speech.
44   size_t ret = WebRtcG711_DecodeU(encoded, encoded_len, decoded, &temp_type);
45   *speech_type = ConvertSpeechType(temp_type);
46   return static_cast<int>(ret);
47 }
48 
PacketDuration(const uint8_t * encoded,size_t encoded_len) const49 int AudioDecoderPcmU::PacketDuration(const uint8_t* encoded,
50                                      size_t encoded_len) const {
51   // One encoded byte per sample per channel.
52   return static_cast<int>(encoded_len / Channels());
53 }
54 
Reset()55 void AudioDecoderPcmA::Reset() {}
56 
ParsePayload(rtc::Buffer && payload,uint32_t timestamp)57 std::vector<AudioDecoder::ParseResult> AudioDecoderPcmA::ParsePayload(
58     rtc::Buffer&& payload,
59     uint32_t timestamp) {
60   return LegacyEncodedAudioFrame::SplitBySamples(
61       this, std::move(payload), timestamp, 8 * num_channels_, 8);
62 }
63 
SampleRateHz() const64 int AudioDecoderPcmA::SampleRateHz() const {
65   return 8000;
66 }
67 
Channels() const68 size_t AudioDecoderPcmA::Channels() const {
69   return num_channels_;
70 }
71 
DecodeInternal(const uint8_t * encoded,size_t encoded_len,int sample_rate_hz,int16_t * decoded,SpeechType * speech_type)72 int AudioDecoderPcmA::DecodeInternal(const uint8_t* encoded,
73                                      size_t encoded_len,
74                                      int sample_rate_hz,
75                                      int16_t* decoded,
76                                      SpeechType* speech_type) {
77   RTC_DCHECK_EQ(SampleRateHz(), sample_rate_hz);
78   int16_t temp_type = 1;  // Default is speech.
79   size_t ret = WebRtcG711_DecodeA(encoded, encoded_len, decoded, &temp_type);
80   *speech_type = ConvertSpeechType(temp_type);
81   return static_cast<int>(ret);
82 }
83 
PacketDuration(const uint8_t * encoded,size_t encoded_len) const84 int AudioDecoderPcmA::PacketDuration(const uint8_t* encoded,
85                                      size_t encoded_len) const {
86   // One encoded byte per sample per channel.
87   return static_cast<int>(encoded_len / Channels());
88 }
89 
90 }  // namespace webrtc
91