blob: 98dd496e633c167e048b3c5ca4da3758713e57a8 [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
kwiberg087bd342017-02-10 08:15:44 -080011#include "webrtc/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
Edward Lemurc20978e2017-07-06 19:44:34 +020017#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.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
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
ossu61a208b2016-09-20 01:38:00 -070054AudioDecoder::ParseResult::ParseResult() = default;
55AudioDecoder::ParseResult::ParseResult(ParseResult&& b) = default;
56AudioDecoder::ParseResult::ParseResult(uint32_t timestamp,
ossua70695a2016-09-22 02:06:28 -070057 int priority,
ossu61a208b2016-09-20 01:38:00 -070058 std::unique_ptr<EncodedAudioFrame> frame)
ossua70695a2016-09-22 02:06:28 -070059 : timestamp(timestamp), priority(priority), frame(std::move(frame)) {
60 RTC_DCHECK_GE(priority, 0);
61}
ossu61a208b2016-09-20 01:38:00 -070062
63AudioDecoder::ParseResult::~ParseResult() = default;
64
65AudioDecoder::ParseResult& AudioDecoder::ParseResult::operator=(
66 ParseResult&& b) = default;
67
68std::vector<AudioDecoder::ParseResult> AudioDecoder::ParsePayload(
69 rtc::Buffer&& payload,
ossua70695a2016-09-22 02:06:28 -070070 uint32_t timestamp) {
ossu61a208b2016-09-20 01:38:00 -070071 std::vector<ParseResult> results;
72 std::unique_ptr<EncodedAudioFrame> frame(
kwiberg087bd342017-02-10 08:15:44 -080073 new OldStyleEncodedFrame(this, std::move(payload)));
ossua70695a2016-09-22 02:06:28 -070074 results.emplace_back(timestamp, 0, std::move(frame));
ossu61a208b2016-09-20 01:38:00 -070075 return results;
76}
77
kwiberg087bd342017-02-10 08:15:44 -080078int 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ömd7b7ae82015-12-08 13:41:35 +010084 TRACE_EVENT0("webrtc", "AudioDecoder::Decode");
kwibergac554ee2016-09-02 00:39:33 -070085 rtc::MsanCheckInitialized(rtc::MakeArrayView(encoded, encoded_len));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +000086 int duration = PacketDuration(encoded, encoded_len);
Minyue323b1322015-05-25 13:49:37 +020087 if (duration >= 0 &&
88 duration * Channels() * sizeof(int16_t) > max_decoded_bytes) {
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +000089 return -1;
90 }
91 return DecodeInternal(encoded, encoded_len, sample_rate_hz, decoded,
92 speech_type);
93}
94
kwiberg087bd342017-02-10 08:15:44 -080095int 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ömd7b7ae82015-12-08 13:41:35 +0100101 TRACE_EVENT0("webrtc", "AudioDecoder::DecodeRedundant");
kwibergac554ee2016-09-02 00:39:33 -0700102 rtc::MsanCheckInitialized(rtc::MakeArrayView(encoded, encoded_len));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000103 int duration = PacketDurationRedundant(encoded, encoded_len);
Minyue323b1322015-05-25 13:49:37 +0200104 if (duration >= 0 &&
105 duration * Channels() * sizeof(int16_t) > max_decoded_bytes) {
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000106 return -1;
107 }
108 return DecodeRedundantInternal(encoded, encoded_len, sample_rate_hz, decoded,
109 speech_type);
110}
111
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000112int AudioDecoder::DecodeRedundantInternal(const uint8_t* encoded,
113 size_t encoded_len,
kwiberg087bd342017-02-10 08:15:44 -0800114 int sample_rate_hz,
115 int16_t* decoded,
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000116 SpeechType* speech_type) {
117 return DecodeInternal(encoded, encoded_len, sample_rate_hz, decoded,
118 speech_type);
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +0000119}
120
kwiberg087bd342017-02-10 08:15:44 -0800121bool AudioDecoder::HasDecodePlc() const {
122 return false;
123}
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +0000124
Peter Kastingdce40cf2015-08-24 14:52:23 -0700125size_t AudioDecoder::DecodePlc(size_t num_frames, int16_t* decoded) {
126 return 0;
127}
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +0000128
129int 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
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:
164 assert(false);
165 return kSpeech;
166 }
167}
168
169} // namespace webrtc