1 /*
2 * Copyright (c) 2012 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 "api/audio_codecs/audio_decoder.h"
12
13
14 #include <memory>
15 #include <utility>
16
17 #include "api/array_view.h"
18 #include "rtc_base/checks.h"
19 #include "rtc_base/sanitizer.h"
20 #include "rtc_base/trace_event.h"
21
22 namespace webrtc {
23
24 namespace {
25
26 class OldStyleEncodedFrame final : public AudioDecoder::EncodedAudioFrame {
27 public:
OldStyleEncodedFrame(AudioDecoder * decoder,rtc::Buffer && payload)28 OldStyleEncodedFrame(AudioDecoder* decoder, rtc::Buffer&& payload)
29 : decoder_(decoder), payload_(std::move(payload)) {}
30
Duration() const31 size_t Duration() const override {
32 const int ret = decoder_->PacketDuration(payload_.data(), payload_.size());
33 return ret < 0 ? 0 : static_cast<size_t>(ret);
34 }
35
Decode(rtc::ArrayView<int16_t> decoded) const36 absl::optional<DecodeResult> Decode(
37 rtc::ArrayView<int16_t> decoded) const override {
38 auto speech_type = AudioDecoder::kSpeech;
39 const int ret = decoder_->Decode(
40 payload_.data(), payload_.size(), decoder_->SampleRateHz(),
41 decoded.size() * sizeof(int16_t), decoded.data(), &speech_type);
42 return ret < 0 ? absl::nullopt
43 : absl::optional<DecodeResult>(
44 {static_cast<size_t>(ret), speech_type});
45 }
46
47 private:
48 AudioDecoder* const decoder_;
49 const rtc::Buffer payload_;
50 };
51
52 } // namespace
53
IsDtxPacket() const54 bool AudioDecoder::EncodedAudioFrame::IsDtxPacket() const {
55 return false;
56 }
57
58 AudioDecoder::ParseResult::ParseResult() = default;
59 AudioDecoder::ParseResult::ParseResult(ParseResult&& b) = default;
ParseResult(uint32_t timestamp,int priority,std::unique_ptr<EncodedAudioFrame> frame)60 AudioDecoder::ParseResult::ParseResult(uint32_t timestamp,
61 int priority,
62 std::unique_ptr<EncodedAudioFrame> frame)
63 : timestamp(timestamp), priority(priority), frame(std::move(frame)) {
64 RTC_DCHECK_GE(priority, 0);
65 }
66
67 AudioDecoder::ParseResult::~ParseResult() = default;
68
69 AudioDecoder::ParseResult& AudioDecoder::ParseResult::operator=(
70 ParseResult&& b) = default;
71
ParsePayload(rtc::Buffer && payload,uint32_t timestamp)72 std::vector<AudioDecoder::ParseResult> AudioDecoder::ParsePayload(
73 rtc::Buffer&& payload,
74 uint32_t timestamp) {
75 std::vector<ParseResult> results;
76 std::unique_ptr<EncodedAudioFrame> frame(
77 new OldStyleEncodedFrame(this, std::move(payload)));
78 results.emplace_back(timestamp, 0, std::move(frame));
79 return results;
80 }
81
Decode(const uint8_t * encoded,size_t encoded_len,int sample_rate_hz,size_t max_decoded_bytes,int16_t * decoded,SpeechType * speech_type)82 int AudioDecoder::Decode(const uint8_t* encoded,
83 size_t encoded_len,
84 int sample_rate_hz,
85 size_t max_decoded_bytes,
86 int16_t* decoded,
87 SpeechType* speech_type) {
88 TRACE_EVENT0("webrtc", "AudioDecoder::Decode");
89 rtc::MsanCheckInitialized(rtc::MakeArrayView(encoded, encoded_len));
90 int duration = PacketDuration(encoded, encoded_len);
91 if (duration >= 0 &&
92 duration * Channels() * sizeof(int16_t) > max_decoded_bytes) {
93 return -1;
94 }
95 return DecodeInternal(encoded, encoded_len, sample_rate_hz, decoded,
96 speech_type);
97 }
98
DecodeRedundant(const uint8_t * encoded,size_t encoded_len,int sample_rate_hz,size_t max_decoded_bytes,int16_t * decoded,SpeechType * speech_type)99 int AudioDecoder::DecodeRedundant(const uint8_t* encoded,
100 size_t encoded_len,
101 int sample_rate_hz,
102 size_t max_decoded_bytes,
103 int16_t* decoded,
104 SpeechType* speech_type) {
105 TRACE_EVENT0("webrtc", "AudioDecoder::DecodeRedundant");
106 rtc::MsanCheckInitialized(rtc::MakeArrayView(encoded, encoded_len));
107 int duration = PacketDurationRedundant(encoded, encoded_len);
108 if (duration >= 0 &&
109 duration * Channels() * sizeof(int16_t) > max_decoded_bytes) {
110 return -1;
111 }
112 return DecodeRedundantInternal(encoded, encoded_len, sample_rate_hz, decoded,
113 speech_type);
114 }
115
DecodeRedundantInternal(const uint8_t * encoded,size_t encoded_len,int sample_rate_hz,int16_t * decoded,SpeechType * speech_type)116 int AudioDecoder::DecodeRedundantInternal(const uint8_t* encoded,
117 size_t encoded_len,
118 int sample_rate_hz,
119 int16_t* decoded,
120 SpeechType* speech_type) {
121 return DecodeInternal(encoded, encoded_len, sample_rate_hz, decoded,
122 speech_type);
123 }
124
HasDecodePlc() const125 bool AudioDecoder::HasDecodePlc() const {
126 return false;
127 }
128
DecodePlc(size_t num_frames,int16_t * decoded)129 size_t AudioDecoder::DecodePlc(size_t num_frames, int16_t* decoded) {
130 return 0;
131 }
132
133 // TODO(bugs.webrtc.org/9676): Remove default implementation.
GeneratePlc(size_t,rtc::BufferT<int16_t> *)134 void AudioDecoder::GeneratePlc(size_t /*requested_samples_per_channel*/,
135 rtc::BufferT<int16_t>* /*concealment_audio*/) {}
136
ErrorCode()137 int AudioDecoder::ErrorCode() {
138 return 0;
139 }
140
PacketDuration(const uint8_t * encoded,size_t encoded_len) const141 int AudioDecoder::PacketDuration(const uint8_t* encoded,
142 size_t encoded_len) const {
143 return kNotImplemented;
144 }
145
PacketDurationRedundant(const uint8_t * encoded,size_t encoded_len) const146 int AudioDecoder::PacketDurationRedundant(const uint8_t* encoded,
147 size_t encoded_len) const {
148 return kNotImplemented;
149 }
150
PacketHasFec(const uint8_t * encoded,size_t encoded_len) const151 bool AudioDecoder::PacketHasFec(const uint8_t* encoded,
152 size_t encoded_len) const {
153 return false;
154 }
155
ConvertSpeechType(int16_t type)156 AudioDecoder::SpeechType AudioDecoder::ConvertSpeechType(int16_t type) {
157 switch (type) {
158 case 0: // TODO(hlundin): Both iSAC and Opus return 0 for speech.
159 case 1:
160 return kSpeech;
161 case 2:
162 return kComfortNoise;
163 default:
164 RTC_DCHECK_NOTREACHED();
165 return kSpeech;
166 }
167 }
168
169 constexpr int AudioDecoder::kMaxNumberOfChannels;
170 } // namespace webrtc
171