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