blob: 4fc995172be3a386f39e647fbaef77f4c71ef5f2 [file] [log] [blame]
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "api/audio_codecs/audio_decoder.h"
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +000012
Jonas Olssona4d87372019-07-05 19:08:33 +020013
ossu0d526d52016-09-21 01:57:31 -070014#include <memory>
15#include <utility>
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +000016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#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.orge04a93b2014-12-09 10:12:53 +000021
22namespace webrtc {
23
kwiberg087bd342017-02-10 08:15:44 -080024namespace {
25
26class 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 Chapovalov0bc58cf2018-06-21 13:32:56 +020036 absl::optional<DecodeResult> Decode(
kwiberg087bd342017-02-10 08:15:44 -080037 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 Chapovalov0bc58cf2018-06-21 13:32:56 +020042 return ret < 0 ? absl::nullopt
43 : absl::optional<DecodeResult>(
kwiberg087bd342017-02-10 08:15:44 -080044 {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 Creusenc7f09ad2018-05-22 13:21:01 +020054bool AudioDecoder::EncodedAudioFrame::IsDtxPacket() const {
55 return false;
56}
57
ossu61a208b2016-09-20 01:38:00 -070058AudioDecoder::ParseResult::ParseResult() = default;
59AudioDecoder::ParseResult::ParseResult(ParseResult&& b) = default;
60AudioDecoder::ParseResult::ParseResult(uint32_t timestamp,
ossua70695a2016-09-22 02:06:28 -070061 int priority,
ossu61a208b2016-09-20 01:38:00 -070062 std::unique_ptr<EncodedAudioFrame> frame)
ossua70695a2016-09-22 02:06:28 -070063 : timestamp(timestamp), priority(priority), frame(std::move(frame)) {
64 RTC_DCHECK_GE(priority, 0);
65}
ossu61a208b2016-09-20 01:38:00 -070066
67AudioDecoder::ParseResult::~ParseResult() = default;
68
69AudioDecoder::ParseResult& AudioDecoder::ParseResult::operator=(
70 ParseResult&& b) = default;
71
72std::vector<AudioDecoder::ParseResult> AudioDecoder::ParsePayload(
73 rtc::Buffer&& payload,
ossua70695a2016-09-22 02:06:28 -070074 uint32_t timestamp) {
ossu61a208b2016-09-20 01:38:00 -070075 std::vector<ParseResult> results;
76 std::unique_ptr<EncodedAudioFrame> frame(
kwiberg087bd342017-02-10 08:15:44 -080077 new OldStyleEncodedFrame(this, std::move(payload)));
ossua70695a2016-09-22 02:06:28 -070078 results.emplace_back(timestamp, 0, std::move(frame));
ossu61a208b2016-09-20 01:38:00 -070079 return results;
80}
81
kwiberg087bd342017-02-10 08:15:44 -080082int 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ömd7b7ae82015-12-08 13:41:35 +010088 TRACE_EVENT0("webrtc", "AudioDecoder::Decode");
kwibergac554ee2016-09-02 00:39:33 -070089 rtc::MsanCheckInitialized(rtc::MakeArrayView(encoded, encoded_len));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +000090 int duration = PacketDuration(encoded, encoded_len);
Minyue323b1322015-05-25 13:49:37 +020091 if (duration >= 0 &&
92 duration * Channels() * sizeof(int16_t) > max_decoded_bytes) {
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +000093 return -1;
94 }
95 return DecodeInternal(encoded, encoded_len, sample_rate_hz, decoded,
96 speech_type);
97}
98
kwiberg087bd342017-02-10 08:15:44 -080099int 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ömd7b7ae82015-12-08 13:41:35 +0100105 TRACE_EVENT0("webrtc", "AudioDecoder::DecodeRedundant");
kwibergac554ee2016-09-02 00:39:33 -0700106 rtc::MsanCheckInitialized(rtc::MakeArrayView(encoded, encoded_len));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000107 int duration = PacketDurationRedundant(encoded, encoded_len);
Minyue323b1322015-05-25 13:49:37 +0200108 if (duration >= 0 &&
109 duration * Channels() * sizeof(int16_t) > max_decoded_bytes) {
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000110 return -1;
111 }
112 return DecodeRedundantInternal(encoded, encoded_len, sample_rate_hz, decoded,
113 speech_type);
114}
115
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000116int AudioDecoder::DecodeRedundantInternal(const uint8_t* encoded,
117 size_t encoded_len,
kwiberg087bd342017-02-10 08:15:44 -0800118 int sample_rate_hz,
119 int16_t* decoded,
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000120 SpeechType* speech_type) {
121 return DecodeInternal(encoded, encoded_len, sample_rate_hz, decoded,
122 speech_type);
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +0000123}
124
kwiberg087bd342017-02-10 08:15:44 -0800125bool AudioDecoder::HasDecodePlc() const {
126 return false;
127}
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +0000128
Peter Kastingdce40cf2015-08-24 14:52:23 -0700129size_t AudioDecoder::DecodePlc(size_t num_frames, int16_t* decoded) {
130 return 0;
131}
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +0000132
Benjamin Wright3aa584f2019-03-13 17:31:06 -0700133// TODO(bugs.webrtc.org/9676): Remove default implementation.
Henrik Lundin00eb12a2018-09-05 18:14:52 +0200134void AudioDecoder::GeneratePlc(size_t /*requested_samples_per_channel*/,
Benjamin Wright3aa584f2019-03-13 17:31:06 -0700135 rtc::BufferT<int16_t>* /*concealment_audio*/) {}
Henrik Lundin00eb12a2018-09-05 18:14:52 +0200136
kwiberg087bd342017-02-10 08:15:44 -0800137int AudioDecoder::ErrorCode() {
138 return 0;
139}
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +0000140
minyue@webrtc.orga8cc3442015-02-13 14:01:54 +0000141int AudioDecoder::PacketDuration(const uint8_t* encoded,
142 size_t encoded_len) const {
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +0000143 return kNotImplemented;
144}
145
146int AudioDecoder::PacketDurationRedundant(const uint8_t* encoded,
147 size_t encoded_len) const {
148 return kNotImplemented;
149}
150
151bool AudioDecoder::PacketHasFec(const uint8_t* encoded,
152 size_t encoded_len) const {
153 return false;
154}
155
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +0000156AudioDecoder::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:
Mirko Bonadei25ab3222021-07-08 20:08:20 +0200164 RTC_NOTREACHED();
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +0000165 return kSpeech;
166 }
167}
168
169} // namespace webrtc