blob: 97cda27a03110b2e16c55dee2802aed145596e08 [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
13#include <assert.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
ossu0d526d52016-09-21 01:57:31 -070015#include <memory>
16#include <utility>
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +000017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/array_view.h"
19#include "rtc_base/checks.h"
20#include "rtc_base/sanitizer.h"
21#include "rtc_base/trace_event.h"
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +000022
23namespace webrtc {
24
kwiberg087bd342017-02-10 08:15:44 -080025namespace {
26
27class OldStyleEncodedFrame final : public AudioDecoder::EncodedAudioFrame {
28 public:
29 OldStyleEncodedFrame(AudioDecoder* decoder, rtc::Buffer&& payload)
30 : decoder_(decoder), payload_(std::move(payload)) {}
31
32 size_t Duration() const override {
33 const int ret = decoder_->PacketDuration(payload_.data(), payload_.size());
34 return ret < 0 ? 0 : static_cast<size_t>(ret);
35 }
36
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020037 absl::optional<DecodeResult> Decode(
kwiberg087bd342017-02-10 08:15:44 -080038 rtc::ArrayView<int16_t> decoded) const override {
39 auto speech_type = AudioDecoder::kSpeech;
40 const int ret = decoder_->Decode(
41 payload_.data(), payload_.size(), decoder_->SampleRateHz(),
42 decoded.size() * sizeof(int16_t), decoded.data(), &speech_type);
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020043 return ret < 0 ? absl::nullopt
44 : absl::optional<DecodeResult>(
kwiberg087bd342017-02-10 08:15:44 -080045 {static_cast<size_t>(ret), speech_type});
46 }
47
48 private:
49 AudioDecoder* const decoder_;
50 const rtc::Buffer payload_;
51};
52
53} // namespace
54
Ivo Creusenc7f09ad2018-05-22 13:21:01 +020055bool AudioDecoder::EncodedAudioFrame::IsDtxPacket() const {
56 return false;
57}
58
ossu61a208b2016-09-20 01:38:00 -070059AudioDecoder::ParseResult::ParseResult() = default;
60AudioDecoder::ParseResult::ParseResult(ParseResult&& b) = default;
61AudioDecoder::ParseResult::ParseResult(uint32_t timestamp,
ossua70695a2016-09-22 02:06:28 -070062 int priority,
ossu61a208b2016-09-20 01:38:00 -070063 std::unique_ptr<EncodedAudioFrame> frame)
ossua70695a2016-09-22 02:06:28 -070064 : timestamp(timestamp), priority(priority), frame(std::move(frame)) {
65 RTC_DCHECK_GE(priority, 0);
66}
ossu61a208b2016-09-20 01:38:00 -070067
68AudioDecoder::ParseResult::~ParseResult() = default;
69
70AudioDecoder::ParseResult& AudioDecoder::ParseResult::operator=(
71 ParseResult&& b) = default;
72
73std::vector<AudioDecoder::ParseResult> AudioDecoder::ParsePayload(
74 rtc::Buffer&& payload,
ossua70695a2016-09-22 02:06:28 -070075 uint32_t timestamp) {
ossu61a208b2016-09-20 01:38:00 -070076 std::vector<ParseResult> results;
77 std::unique_ptr<EncodedAudioFrame> frame(
kwiberg087bd342017-02-10 08:15:44 -080078 new OldStyleEncodedFrame(this, std::move(payload)));
ossua70695a2016-09-22 02:06:28 -070079 results.emplace_back(timestamp, 0, std::move(frame));
ossu61a208b2016-09-20 01:38:00 -070080 return results;
81}
82
kwiberg087bd342017-02-10 08:15:44 -080083int AudioDecoder::Decode(const uint8_t* encoded,
84 size_t encoded_len,
85 int sample_rate_hz,
86 size_t max_decoded_bytes,
87 int16_t* decoded,
88 SpeechType* speech_type) {
Peter Boströmd7b7ae82015-12-08 13:41:35 +010089 TRACE_EVENT0("webrtc", "AudioDecoder::Decode");
kwibergac554ee2016-09-02 00:39:33 -070090 rtc::MsanCheckInitialized(rtc::MakeArrayView(encoded, encoded_len));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +000091 int duration = PacketDuration(encoded, encoded_len);
Minyue323b1322015-05-25 13:49:37 +020092 if (duration >= 0 &&
93 duration * Channels() * sizeof(int16_t) > max_decoded_bytes) {
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +000094 return -1;
95 }
96 return DecodeInternal(encoded, encoded_len, sample_rate_hz, decoded,
97 speech_type);
98}
99
kwiberg087bd342017-02-10 08:15:44 -0800100int AudioDecoder::DecodeRedundant(const uint8_t* encoded,
101 size_t encoded_len,
102 int sample_rate_hz,
103 size_t max_decoded_bytes,
104 int16_t* decoded,
105 SpeechType* speech_type) {
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100106 TRACE_EVENT0("webrtc", "AudioDecoder::DecodeRedundant");
kwibergac554ee2016-09-02 00:39:33 -0700107 rtc::MsanCheckInitialized(rtc::MakeArrayView(encoded, encoded_len));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000108 int duration = PacketDurationRedundant(encoded, encoded_len);
Minyue323b1322015-05-25 13:49:37 +0200109 if (duration >= 0 &&
110 duration * Channels() * sizeof(int16_t) > max_decoded_bytes) {
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000111 return -1;
112 }
113 return DecodeRedundantInternal(encoded, encoded_len, sample_rate_hz, decoded,
114 speech_type);
115}
116
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000117int AudioDecoder::DecodeRedundantInternal(const uint8_t* encoded,
118 size_t encoded_len,
kwiberg087bd342017-02-10 08:15:44 -0800119 int sample_rate_hz,
120 int16_t* decoded,
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000121 SpeechType* speech_type) {
122 return DecodeInternal(encoded, encoded_len, sample_rate_hz, decoded,
123 speech_type);
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +0000124}
125
kwiberg087bd342017-02-10 08:15:44 -0800126bool AudioDecoder::HasDecodePlc() const {
127 return false;
128}
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +0000129
Peter Kastingdce40cf2015-08-24 14:52:23 -0700130size_t AudioDecoder::DecodePlc(size_t num_frames, int16_t* decoded) {
131 return 0;
132}
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +0000133
Benjamin Wright3aa584f2019-03-13 17:31:06 -0700134// TODO(bugs.webrtc.org/9676): Remove default implementation.
Henrik Lundin00eb12a2018-09-05 18:14:52 +0200135void AudioDecoder::GeneratePlc(size_t /*requested_samples_per_channel*/,
Benjamin Wright3aa584f2019-03-13 17:31:06 -0700136 rtc::BufferT<int16_t>* /*concealment_audio*/) {}
Henrik Lundin00eb12a2018-09-05 18:14:52 +0200137
kwiberg087bd342017-02-10 08:15:44 -0800138int AudioDecoder::ErrorCode() {
139 return 0;
140}
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +0000141
minyue@webrtc.orga8cc3442015-02-13 14:01:54 +0000142int AudioDecoder::PacketDuration(const uint8_t* encoded,
143 size_t encoded_len) const {
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +0000144 return kNotImplemented;
145}
146
147int AudioDecoder::PacketDurationRedundant(const uint8_t* encoded,
148 size_t encoded_len) const {
149 return kNotImplemented;
150}
151
152bool AudioDecoder::PacketHasFec(const uint8_t* encoded,
153 size_t encoded_len) const {
154 return false;
155}
156
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +0000157AudioDecoder::SpeechType AudioDecoder::ConvertSpeechType(int16_t type) {
158 switch (type) {
159 case 0: // TODO(hlundin): Both iSAC and Opus return 0 for speech.
160 case 1:
161 return kSpeech;
162 case 2:
163 return kComfortNoise;
164 default:
165 assert(false);
166 return kSpeech;
167 }
168}
169
170} // namespace webrtc