blob: afa5115d5a1740036c566ded3c9c9019b78bb233 [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
11#include "webrtc/modules/audio_coding/codecs/audio_decoder.h"
12
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
kwibergac554ee2016-09-02 00:39:33 -070017#include "webrtc/base/array_view.h"
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +000018#include "webrtc/base/checks.h"
kwibergac554ee2016-09-02 00:39:33 -070019#include "webrtc/base/sanitizer.h"
Peter Boströmd7b7ae82015-12-08 13:41:35 +010020#include "webrtc/base/trace_event.h"
ossu0d526d52016-09-21 01:57:31 -070021#include "webrtc/modules/audio_coding/codecs/legacy_encoded_audio_frame.h"
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +000022
23namespace webrtc {
24
ossu61a208b2016-09-20 01:38:00 -070025AudioDecoder::ParseResult::ParseResult() = default;
26AudioDecoder::ParseResult::ParseResult(ParseResult&& b) = default;
27AudioDecoder::ParseResult::ParseResult(uint32_t timestamp,
ossua70695a2016-09-22 02:06:28 -070028 int priority,
ossu61a208b2016-09-20 01:38:00 -070029 std::unique_ptr<EncodedAudioFrame> frame)
ossua70695a2016-09-22 02:06:28 -070030 : timestamp(timestamp), priority(priority), frame(std::move(frame)) {
31 RTC_DCHECK_GE(priority, 0);
32}
ossu61a208b2016-09-20 01:38:00 -070033
34AudioDecoder::ParseResult::~ParseResult() = default;
35
36AudioDecoder::ParseResult& AudioDecoder::ParseResult::operator=(
37 ParseResult&& b) = default;
38
39std::vector<AudioDecoder::ParseResult> AudioDecoder::ParsePayload(
40 rtc::Buffer&& payload,
ossua70695a2016-09-22 02:06:28 -070041 uint32_t timestamp) {
ossu61a208b2016-09-20 01:38:00 -070042 std::vector<ParseResult> results;
43 std::unique_ptr<EncodedAudioFrame> frame(
ossua70695a2016-09-22 02:06:28 -070044 new LegacyEncodedAudioFrame(this, std::move(payload)));
45 results.emplace_back(timestamp, 0, std::move(frame));
ossu61a208b2016-09-20 01:38:00 -070046 return results;
47}
48
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +000049int AudioDecoder::Decode(const uint8_t* encoded, size_t encoded_len,
50 int sample_rate_hz, size_t max_decoded_bytes,
51 int16_t* decoded, SpeechType* speech_type) {
Peter Boströmd7b7ae82015-12-08 13:41:35 +010052 TRACE_EVENT0("webrtc", "AudioDecoder::Decode");
kwibergac554ee2016-09-02 00:39:33 -070053 rtc::MsanCheckInitialized(rtc::MakeArrayView(encoded, encoded_len));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +000054 int duration = PacketDuration(encoded, encoded_len);
Minyue323b1322015-05-25 13:49:37 +020055 if (duration >= 0 &&
56 duration * Channels() * sizeof(int16_t) > max_decoded_bytes) {
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +000057 return -1;
58 }
59 return DecodeInternal(encoded, encoded_len, sample_rate_hz, decoded,
60 speech_type);
61}
62
63int AudioDecoder::DecodeRedundant(const uint8_t* encoded, size_t encoded_len,
64 int sample_rate_hz, size_t max_decoded_bytes,
65 int16_t* decoded, SpeechType* speech_type) {
Peter Boströmd7b7ae82015-12-08 13:41:35 +010066 TRACE_EVENT0("webrtc", "AudioDecoder::DecodeRedundant");
kwibergac554ee2016-09-02 00:39:33 -070067 rtc::MsanCheckInitialized(rtc::MakeArrayView(encoded, encoded_len));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +000068 int duration = PacketDurationRedundant(encoded, encoded_len);
Minyue323b1322015-05-25 13:49:37 +020069 if (duration >= 0 &&
70 duration * Channels() * sizeof(int16_t) > max_decoded_bytes) {
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +000071 return -1;
72 }
73 return DecodeRedundantInternal(encoded, encoded_len, sample_rate_hz, decoded,
74 speech_type);
75}
76
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +000077int AudioDecoder::DecodeRedundantInternal(const uint8_t* encoded,
78 size_t encoded_len,
79 int sample_rate_hz, int16_t* decoded,
80 SpeechType* speech_type) {
81 return DecodeInternal(encoded, encoded_len, sample_rate_hz, decoded,
82 speech_type);
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +000083}
84
85bool AudioDecoder::HasDecodePlc() const { return false; }
86
Peter Kastingdce40cf2015-08-24 14:52:23 -070087size_t AudioDecoder::DecodePlc(size_t num_frames, int16_t* decoded) {
88 return 0;
89}
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +000090
91int AudioDecoder::IncomingPacket(const uint8_t* payload,
92 size_t payload_len,
93 uint16_t rtp_sequence_number,
94 uint32_t rtp_timestamp,
95 uint32_t arrival_timestamp) {
96 return 0;
97}
98
99int AudioDecoder::ErrorCode() { return 0; }
100
minyue@webrtc.orga8cc3442015-02-13 14:01:54 +0000101int AudioDecoder::PacketDuration(const uint8_t* encoded,
102 size_t encoded_len) const {
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +0000103 return kNotImplemented;
104}
105
106int AudioDecoder::PacketDurationRedundant(const uint8_t* encoded,
107 size_t encoded_len) const {
108 return kNotImplemented;
109}
110
111bool AudioDecoder::PacketHasFec(const uint8_t* encoded,
112 size_t encoded_len) const {
113 return false;
114}
115
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +0000116AudioDecoder::SpeechType AudioDecoder::ConvertSpeechType(int16_t type) {
117 switch (type) {
118 case 0: // TODO(hlundin): Both iSAC and Opus return 0 for speech.
119 case 1:
120 return kSpeech;
121 case 2:
122 return kComfortNoise;
123 default:
124 assert(false);
125 return kSpeech;
126 }
127}
128
129} // namespace webrtc