kwiberg@webrtc.org | e04a93b | 2014-12-09 10:12:53 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "api/audio_codecs/audio_decoder.h" |
kwiberg@webrtc.org | e04a93b | 2014-12-09 10:12:53 +0000 | [diff] [blame] | 12 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 13 | |
ossu | 0d526d5 | 2016-09-21 01:57:31 -0700 | [diff] [blame] | 14 | #include <memory> |
| 15 | #include <utility> |
kwiberg@webrtc.org | e04a93b | 2014-12-09 10:12:53 +0000 | [diff] [blame] | 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 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" |
kwiberg@webrtc.org | e04a93b | 2014-12-09 10:12:53 +0000 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | |
kwiberg | 087bd34 | 2017-02-10 08:15:44 -0800 | [diff] [blame] | 24 | namespace { |
| 25 | |
| 26 | class OldStyleEncodedFrame final : public AudioDecoder::EncodedAudioFrame { |
| 27 | public: |
| 28 | OldStyleEncodedFrame(AudioDecoder* decoder, rtc::Buffer&& payload) |
| 29 | : decoder_(decoder), payload_(std::move(payload)) {} |
| 30 | |
| 31 | 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 | |
Danil Chapovalov | 0bc58cf | 2018-06-21 13:32:56 +0200 | [diff] [blame] | 36 | absl::optional<DecodeResult> Decode( |
kwiberg | 087bd34 | 2017-02-10 08:15:44 -0800 | [diff] [blame] | 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); |
Danil Chapovalov | 0bc58cf | 2018-06-21 13:32:56 +0200 | [diff] [blame] | 42 | return ret < 0 ? absl::nullopt |
| 43 | : absl::optional<DecodeResult>( |
kwiberg | 087bd34 | 2017-02-10 08:15:44 -0800 | [diff] [blame] | 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 | |
Ivo Creusen | c7f09ad | 2018-05-22 13:21:01 +0200 | [diff] [blame] | 54 | bool AudioDecoder::EncodedAudioFrame::IsDtxPacket() const { |
| 55 | return false; |
| 56 | } |
| 57 | |
ossu | 61a208b | 2016-09-20 01:38:00 -0700 | [diff] [blame] | 58 | AudioDecoder::ParseResult::ParseResult() = default; |
| 59 | AudioDecoder::ParseResult::ParseResult(ParseResult&& b) = default; |
| 60 | AudioDecoder::ParseResult::ParseResult(uint32_t timestamp, |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 61 | int priority, |
ossu | 61a208b | 2016-09-20 01:38:00 -0700 | [diff] [blame] | 62 | std::unique_ptr<EncodedAudioFrame> frame) |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 63 | : timestamp(timestamp), priority(priority), frame(std::move(frame)) { |
| 64 | RTC_DCHECK_GE(priority, 0); |
| 65 | } |
ossu | 61a208b | 2016-09-20 01:38:00 -0700 | [diff] [blame] | 66 | |
| 67 | AudioDecoder::ParseResult::~ParseResult() = default; |
| 68 | |
| 69 | AudioDecoder::ParseResult& AudioDecoder::ParseResult::operator=( |
| 70 | ParseResult&& b) = default; |
| 71 | |
| 72 | std::vector<AudioDecoder::ParseResult> AudioDecoder::ParsePayload( |
| 73 | rtc::Buffer&& payload, |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 74 | uint32_t timestamp) { |
ossu | 61a208b | 2016-09-20 01:38:00 -0700 | [diff] [blame] | 75 | std::vector<ParseResult> results; |
| 76 | std::unique_ptr<EncodedAudioFrame> frame( |
kwiberg | 087bd34 | 2017-02-10 08:15:44 -0800 | [diff] [blame] | 77 | new OldStyleEncodedFrame(this, std::move(payload))); |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 78 | results.emplace_back(timestamp, 0, std::move(frame)); |
ossu | 61a208b | 2016-09-20 01:38:00 -0700 | [diff] [blame] | 79 | return results; |
| 80 | } |
| 81 | |
kwiberg | 087bd34 | 2017-02-10 08:15:44 -0800 | [diff] [blame] | 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) { |
Peter Boström | d7b7ae8 | 2015-12-08 13:41:35 +0100 | [diff] [blame] | 88 | TRACE_EVENT0("webrtc", "AudioDecoder::Decode"); |
kwiberg | ac554ee | 2016-09-02 00:39:33 -0700 | [diff] [blame] | 89 | rtc::MsanCheckInitialized(rtc::MakeArrayView(encoded, encoded_len)); |
minyue@webrtc.org | 7f7d7e3 | 2015-03-16 12:30:37 +0000 | [diff] [blame] | 90 | int duration = PacketDuration(encoded, encoded_len); |
Minyue | 323b132 | 2015-05-25 13:49:37 +0200 | [diff] [blame] | 91 | if (duration >= 0 && |
| 92 | duration * Channels() * sizeof(int16_t) > max_decoded_bytes) { |
minyue@webrtc.org | 7f7d7e3 | 2015-03-16 12:30:37 +0000 | [diff] [blame] | 93 | return -1; |
| 94 | } |
| 95 | return DecodeInternal(encoded, encoded_len, sample_rate_hz, decoded, |
| 96 | speech_type); |
| 97 | } |
| 98 | |
kwiberg | 087bd34 | 2017-02-10 08:15:44 -0800 | [diff] [blame] | 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) { |
Peter Boström | d7b7ae8 | 2015-12-08 13:41:35 +0100 | [diff] [blame] | 105 | TRACE_EVENT0("webrtc", "AudioDecoder::DecodeRedundant"); |
kwiberg | ac554ee | 2016-09-02 00:39:33 -0700 | [diff] [blame] | 106 | rtc::MsanCheckInitialized(rtc::MakeArrayView(encoded, encoded_len)); |
minyue@webrtc.org | 7f7d7e3 | 2015-03-16 12:30:37 +0000 | [diff] [blame] | 107 | int duration = PacketDurationRedundant(encoded, encoded_len); |
Minyue | 323b132 | 2015-05-25 13:49:37 +0200 | [diff] [blame] | 108 | if (duration >= 0 && |
| 109 | duration * Channels() * sizeof(int16_t) > max_decoded_bytes) { |
minyue@webrtc.org | 7f7d7e3 | 2015-03-16 12:30:37 +0000 | [diff] [blame] | 110 | return -1; |
| 111 | } |
| 112 | return DecodeRedundantInternal(encoded, encoded_len, sample_rate_hz, decoded, |
| 113 | speech_type); |
| 114 | } |
| 115 | |
minyue@webrtc.org | 7f7d7e3 | 2015-03-16 12:30:37 +0000 | [diff] [blame] | 116 | int AudioDecoder::DecodeRedundantInternal(const uint8_t* encoded, |
| 117 | size_t encoded_len, |
kwiberg | 087bd34 | 2017-02-10 08:15:44 -0800 | [diff] [blame] | 118 | int sample_rate_hz, |
| 119 | int16_t* decoded, |
minyue@webrtc.org | 7f7d7e3 | 2015-03-16 12:30:37 +0000 | [diff] [blame] | 120 | SpeechType* speech_type) { |
| 121 | return DecodeInternal(encoded, encoded_len, sample_rate_hz, decoded, |
| 122 | speech_type); |
kwiberg@webrtc.org | e04a93b | 2014-12-09 10:12:53 +0000 | [diff] [blame] | 123 | } |
| 124 | |
kwiberg | 087bd34 | 2017-02-10 08:15:44 -0800 | [diff] [blame] | 125 | bool AudioDecoder::HasDecodePlc() const { |
| 126 | return false; |
| 127 | } |
kwiberg@webrtc.org | e04a93b | 2014-12-09 10:12:53 +0000 | [diff] [blame] | 128 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 129 | size_t AudioDecoder::DecodePlc(size_t num_frames, int16_t* decoded) { |
| 130 | return 0; |
| 131 | } |
kwiberg@webrtc.org | e04a93b | 2014-12-09 10:12:53 +0000 | [diff] [blame] | 132 | |
Benjamin Wright | 3aa584f | 2019-03-13 17:31:06 -0700 | [diff] [blame] | 133 | // TODO(bugs.webrtc.org/9676): Remove default implementation. |
Henrik Lundin | 00eb12a | 2018-09-05 18:14:52 +0200 | [diff] [blame] | 134 | void AudioDecoder::GeneratePlc(size_t /*requested_samples_per_channel*/, |
Benjamin Wright | 3aa584f | 2019-03-13 17:31:06 -0700 | [diff] [blame] | 135 | rtc::BufferT<int16_t>* /*concealment_audio*/) {} |
Henrik Lundin | 00eb12a | 2018-09-05 18:14:52 +0200 | [diff] [blame] | 136 | |
kwiberg | 087bd34 | 2017-02-10 08:15:44 -0800 | [diff] [blame] | 137 | int AudioDecoder::ErrorCode() { |
| 138 | return 0; |
| 139 | } |
kwiberg@webrtc.org | e04a93b | 2014-12-09 10:12:53 +0000 | [diff] [blame] | 140 | |
minyue@webrtc.org | a8cc344 | 2015-02-13 14:01:54 +0000 | [diff] [blame] | 141 | int AudioDecoder::PacketDuration(const uint8_t* encoded, |
| 142 | size_t encoded_len) const { |
kwiberg@webrtc.org | e04a93b | 2014-12-09 10:12:53 +0000 | [diff] [blame] | 143 | return kNotImplemented; |
| 144 | } |
| 145 | |
| 146 | int AudioDecoder::PacketDurationRedundant(const uint8_t* encoded, |
| 147 | size_t encoded_len) const { |
| 148 | return kNotImplemented; |
| 149 | } |
| 150 | |
| 151 | bool AudioDecoder::PacketHasFec(const uint8_t* encoded, |
| 152 | size_t encoded_len) const { |
| 153 | return false; |
| 154 | } |
| 155 | |
kwiberg@webrtc.org | e04a93b | 2014-12-09 10:12:53 +0000 | [diff] [blame] | 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: |
Artem Titov | d325196 | 2021-11-15 16:57:07 +0100 | [diff] [blame^] | 164 | RTC_DCHECK_NOTREACHED(); |
kwiberg@webrtc.org | e04a93b | 2014-12-09 10:12:53 +0000 | [diff] [blame] | 165 | return kSpeech; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | } // namespace webrtc |