blob: aaba175919ac6ef54511d5bd08134484690818ba [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>
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
Henrik Lundin00eb12a2018-09-05 18:14:52 +0200133// TODO(bugs.webrtc.org/9676): Remove default impementation.
134void AudioDecoder::GeneratePlc(size_t /*requested_samples_per_channel*/,
135 rtc::BufferT<int16_t>* /*concealment_audio*/) {
136 return;
137}
138
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +0000139int AudioDecoder::IncomingPacket(const uint8_t* payload,
140 size_t payload_len,
141 uint16_t rtp_sequence_number,
142 uint32_t rtp_timestamp,
143 uint32_t arrival_timestamp) {
144 return 0;
145}
146
kwiberg087bd342017-02-10 08:15:44 -0800147int AudioDecoder::ErrorCode() {
148 return 0;
149}
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +0000150
minyue@webrtc.orga8cc3442015-02-13 14:01:54 +0000151int AudioDecoder::PacketDuration(const uint8_t* encoded,
152 size_t encoded_len) const {
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +0000153 return kNotImplemented;
154}
155
156int AudioDecoder::PacketDurationRedundant(const uint8_t* encoded,
157 size_t encoded_len) const {
158 return kNotImplemented;
159}
160
161bool AudioDecoder::PacketHasFec(const uint8_t* encoded,
162 size_t encoded_len) const {
163 return false;
164}
165
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +0000166AudioDecoder::SpeechType AudioDecoder::ConvertSpeechType(int16_t type) {
167 switch (type) {
168 case 0: // TODO(hlundin): Both iSAC and Opus return 0 for speech.
169 case 1:
170 return kSpeech;
171 case 2:
172 return kComfortNoise;
173 default:
174 assert(false);
175 return kSpeech;
176 }
177}
178
179} // namespace webrtc