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