• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   // Adjust the encoded length down to ensure the same number of samples in each
44   // channel.
45   const size_t encoded_len_adjusted =
46       PacketDuration(encoded, encoded_len) *
47       Channels();         // 1 byte per sample per channel
48   int16_t temp_type = 1;  // Default is speech.
49   size_t ret =
50       WebRtcG711_DecodeU(encoded, encoded_len_adjusted, decoded, &temp_type);
51   *speech_type = ConvertSpeechType(temp_type);
52   return static_cast<int>(ret);
53 }
54 
PacketDuration(const uint8_t * encoded,size_t encoded_len) const55 int AudioDecoderPcmU::PacketDuration(const uint8_t* encoded,
56                                      size_t encoded_len) const {
57   // One encoded byte per sample per channel.
58   return static_cast<int>(encoded_len / Channels());
59 }
60 
Reset()61 void AudioDecoderPcmA::Reset() {}
62 
ParsePayload(rtc::Buffer && payload,uint32_t timestamp)63 std::vector<AudioDecoder::ParseResult> AudioDecoderPcmA::ParsePayload(
64     rtc::Buffer&& payload,
65     uint32_t timestamp) {
66   return LegacyEncodedAudioFrame::SplitBySamples(
67       this, std::move(payload), timestamp, 8 * num_channels_, 8);
68 }
69 
SampleRateHz() const70 int AudioDecoderPcmA::SampleRateHz() const {
71   return 8000;
72 }
73 
Channels() const74 size_t AudioDecoderPcmA::Channels() const {
75   return num_channels_;
76 }
77 
DecodeInternal(const uint8_t * encoded,size_t encoded_len,int sample_rate_hz,int16_t * decoded,SpeechType * speech_type)78 int AudioDecoderPcmA::DecodeInternal(const uint8_t* encoded,
79                                      size_t encoded_len,
80                                      int sample_rate_hz,
81                                      int16_t* decoded,
82                                      SpeechType* speech_type) {
83   RTC_DCHECK_EQ(SampleRateHz(), sample_rate_hz);
84   // Adjust the encoded length down to ensure the same number of samples in each
85   // channel.
86   const size_t encoded_len_adjusted =
87       PacketDuration(encoded, encoded_len) *
88       Channels();         // 1 byte per sample per channel
89   int16_t temp_type = 1;  // Default is speech.
90   size_t ret =
91       WebRtcG711_DecodeA(encoded, encoded_len_adjusted, decoded, &temp_type);
92   *speech_type = ConvertSpeechType(temp_type);
93   return static_cast<int>(ret);
94 }
95 
PacketDuration(const uint8_t * encoded,size_t encoded_len) const96 int AudioDecoderPcmA::PacketDuration(const uint8_t* encoded,
97                                      size_t encoded_len) const {
98   // One encoded byte per sample per channel.
99   return static_cast<int>(encoded_len / Channels());
100 }
101 
102 }  // namespace webrtc
103