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 | |
kwiberg | 087bd34 | 2017-02-10 08:15:44 -0800 | [diff] [blame] | 11 | #include "webrtc/api/audio_codecs/audio_decoder.h" |
kwiberg@webrtc.org | e04a93b | 2014-12-09 10:12:53 +0000 | [diff] [blame] | 12 | |
| 13 | #include <assert.h> |
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 | |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame^] | 17 | #include "webrtc/rtc_base/array_view.h" |
| 18 | #include "webrtc/rtc_base/checks.h" |
| 19 | #include "webrtc/rtc_base/sanitizer.h" |
| 20 | #include "webrtc/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 | |
| 36 | rtc::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 ? rtc::Optional<DecodeResult>() |
| 43 | : rtc::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 | |
ossu | 61a208b | 2016-09-20 01:38:00 -0700 | [diff] [blame] | 54 | AudioDecoder::ParseResult::ParseResult() = default; |
| 55 | AudioDecoder::ParseResult::ParseResult(ParseResult&& b) = default; |
| 56 | AudioDecoder::ParseResult::ParseResult(uint32_t timestamp, |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 57 | int priority, |
ossu | 61a208b | 2016-09-20 01:38:00 -0700 | [diff] [blame] | 58 | std::unique_ptr<EncodedAudioFrame> frame) |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 59 | : timestamp(timestamp), priority(priority), frame(std::move(frame)) { |
| 60 | RTC_DCHECK_GE(priority, 0); |
| 61 | } |
ossu | 61a208b | 2016-09-20 01:38:00 -0700 | [diff] [blame] | 62 | |
| 63 | AudioDecoder::ParseResult::~ParseResult() = default; |
| 64 | |
| 65 | AudioDecoder::ParseResult& AudioDecoder::ParseResult::operator=( |
| 66 | ParseResult&& b) = default; |
| 67 | |
| 68 | std::vector<AudioDecoder::ParseResult> AudioDecoder::ParsePayload( |
| 69 | rtc::Buffer&& payload, |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 70 | uint32_t timestamp) { |
ossu | 61a208b | 2016-09-20 01:38:00 -0700 | [diff] [blame] | 71 | std::vector<ParseResult> results; |
| 72 | std::unique_ptr<EncodedAudioFrame> frame( |
kwiberg | 087bd34 | 2017-02-10 08:15:44 -0800 | [diff] [blame] | 73 | new OldStyleEncodedFrame(this, std::move(payload))); |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 74 | results.emplace_back(timestamp, 0, std::move(frame)); |
ossu | 61a208b | 2016-09-20 01:38:00 -0700 | [diff] [blame] | 75 | return results; |
| 76 | } |
| 77 | |
kwiberg | 087bd34 | 2017-02-10 08:15:44 -0800 | [diff] [blame] | 78 | int AudioDecoder::Decode(const uint8_t* encoded, |
| 79 | size_t encoded_len, |
| 80 | int sample_rate_hz, |
| 81 | size_t max_decoded_bytes, |
| 82 | int16_t* decoded, |
| 83 | SpeechType* speech_type) { |
Peter Boström | d7b7ae8 | 2015-12-08 13:41:35 +0100 | [diff] [blame] | 84 | TRACE_EVENT0("webrtc", "AudioDecoder::Decode"); |
kwiberg | ac554ee | 2016-09-02 00:39:33 -0700 | [diff] [blame] | 85 | rtc::MsanCheckInitialized(rtc::MakeArrayView(encoded, encoded_len)); |
minyue@webrtc.org | 7f7d7e3 | 2015-03-16 12:30:37 +0000 | [diff] [blame] | 86 | int duration = PacketDuration(encoded, encoded_len); |
Minyue | 323b132 | 2015-05-25 13:49:37 +0200 | [diff] [blame] | 87 | if (duration >= 0 && |
| 88 | duration * Channels() * sizeof(int16_t) > max_decoded_bytes) { |
minyue@webrtc.org | 7f7d7e3 | 2015-03-16 12:30:37 +0000 | [diff] [blame] | 89 | return -1; |
| 90 | } |
| 91 | return DecodeInternal(encoded, encoded_len, sample_rate_hz, decoded, |
| 92 | speech_type); |
| 93 | } |
| 94 | |
kwiberg | 087bd34 | 2017-02-10 08:15:44 -0800 | [diff] [blame] | 95 | int AudioDecoder::DecodeRedundant(const uint8_t* encoded, |
| 96 | size_t encoded_len, |
| 97 | int sample_rate_hz, |
| 98 | size_t max_decoded_bytes, |
| 99 | int16_t* decoded, |
| 100 | SpeechType* speech_type) { |
Peter Boström | d7b7ae8 | 2015-12-08 13:41:35 +0100 | [diff] [blame] | 101 | TRACE_EVENT0("webrtc", "AudioDecoder::DecodeRedundant"); |
kwiberg | ac554ee | 2016-09-02 00:39:33 -0700 | [diff] [blame] | 102 | rtc::MsanCheckInitialized(rtc::MakeArrayView(encoded, encoded_len)); |
minyue@webrtc.org | 7f7d7e3 | 2015-03-16 12:30:37 +0000 | [diff] [blame] | 103 | int duration = PacketDurationRedundant(encoded, encoded_len); |
Minyue | 323b132 | 2015-05-25 13:49:37 +0200 | [diff] [blame] | 104 | if (duration >= 0 && |
| 105 | duration * Channels() * sizeof(int16_t) > max_decoded_bytes) { |
minyue@webrtc.org | 7f7d7e3 | 2015-03-16 12:30:37 +0000 | [diff] [blame] | 106 | return -1; |
| 107 | } |
| 108 | return DecodeRedundantInternal(encoded, encoded_len, sample_rate_hz, decoded, |
| 109 | speech_type); |
| 110 | } |
| 111 | |
minyue@webrtc.org | 7f7d7e3 | 2015-03-16 12:30:37 +0000 | [diff] [blame] | 112 | int AudioDecoder::DecodeRedundantInternal(const uint8_t* encoded, |
| 113 | size_t encoded_len, |
kwiberg | 087bd34 | 2017-02-10 08:15:44 -0800 | [diff] [blame] | 114 | int sample_rate_hz, |
| 115 | int16_t* decoded, |
minyue@webrtc.org | 7f7d7e3 | 2015-03-16 12:30:37 +0000 | [diff] [blame] | 116 | SpeechType* speech_type) { |
| 117 | return DecodeInternal(encoded, encoded_len, sample_rate_hz, decoded, |
| 118 | speech_type); |
kwiberg@webrtc.org | e04a93b | 2014-12-09 10:12:53 +0000 | [diff] [blame] | 119 | } |
| 120 | |
kwiberg | 087bd34 | 2017-02-10 08:15:44 -0800 | [diff] [blame] | 121 | bool AudioDecoder::HasDecodePlc() const { |
| 122 | return false; |
| 123 | } |
kwiberg@webrtc.org | e04a93b | 2014-12-09 10:12:53 +0000 | [diff] [blame] | 124 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 125 | size_t AudioDecoder::DecodePlc(size_t num_frames, int16_t* decoded) { |
| 126 | return 0; |
| 127 | } |
kwiberg@webrtc.org | e04a93b | 2014-12-09 10:12:53 +0000 | [diff] [blame] | 128 | |
| 129 | int AudioDecoder::IncomingPacket(const uint8_t* payload, |
| 130 | size_t payload_len, |
| 131 | uint16_t rtp_sequence_number, |
| 132 | uint32_t rtp_timestamp, |
| 133 | uint32_t arrival_timestamp) { |
| 134 | return 0; |
| 135 | } |
| 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: |
| 164 | assert(false); |
| 165 | return kSpeech; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | } // namespace webrtc |