blob: 6c67924260b14f69588c460baf11d480ef32f558 [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
ossu61a208b2016-09-20 01:38:00 -070017#include <utility>
18
kwibergac554ee2016-09-02 00:39:33 -070019#include "webrtc/base/array_view.h"
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +000020#include "webrtc/base/checks.h"
kwibergac554ee2016-09-02 00:39:33 -070021#include "webrtc/base/sanitizer.h"
Peter Boströmd7b7ae82015-12-08 13:41:35 +010022#include "webrtc/base/trace_event.h"
ossu0d526d52016-09-21 01:57:31 -070023#include "webrtc/modules/audio_coding/codecs/legacy_encoded_audio_frame.h"
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +000024
25namespace webrtc {
26
ossu61a208b2016-09-20 01:38:00 -070027AudioDecoder::ParseResult::ParseResult() = default;
28AudioDecoder::ParseResult::ParseResult(ParseResult&& b) = default;
29AudioDecoder::ParseResult::ParseResult(uint32_t timestamp,
30 bool primary,
31 std::unique_ptr<EncodedAudioFrame> frame)
32 : timestamp(timestamp), primary(primary), frame(std::move(frame)) {}
33
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,
41 uint32_t timestamp,
42 bool is_primary) {
43 std::vector<ParseResult> results;
44 std::unique_ptr<EncodedAudioFrame> frame(
ossu0d526d52016-09-21 01:57:31 -070045 new LegacyEncodedAudioFrame(this, std::move(payload), is_primary));
ossu61a208b2016-09-20 01:38:00 -070046 results.emplace_back(timestamp, is_primary, std::move(frame));
47 return results;
48}
49
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +000050int AudioDecoder::Decode(const uint8_t* encoded, size_t encoded_len,
51 int sample_rate_hz, size_t max_decoded_bytes,
52 int16_t* decoded, SpeechType* speech_type) {
Peter Boströmd7b7ae82015-12-08 13:41:35 +010053 TRACE_EVENT0("webrtc", "AudioDecoder::Decode");
kwibergac554ee2016-09-02 00:39:33 -070054 rtc::MsanCheckInitialized(rtc::MakeArrayView(encoded, encoded_len));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +000055 int duration = PacketDuration(encoded, encoded_len);
Minyue323b1322015-05-25 13:49:37 +020056 if (duration >= 0 &&
57 duration * Channels() * sizeof(int16_t) > max_decoded_bytes) {
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +000058 return -1;
59 }
60 return DecodeInternal(encoded, encoded_len, sample_rate_hz, decoded,
61 speech_type);
62}
63
64int AudioDecoder::DecodeRedundant(const uint8_t* encoded, size_t encoded_len,
65 int sample_rate_hz, size_t max_decoded_bytes,
66 int16_t* decoded, SpeechType* speech_type) {
Peter Boströmd7b7ae82015-12-08 13:41:35 +010067 TRACE_EVENT0("webrtc", "AudioDecoder::DecodeRedundant");
kwibergac554ee2016-09-02 00:39:33 -070068 rtc::MsanCheckInitialized(rtc::MakeArrayView(encoded, encoded_len));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +000069 int duration = PacketDurationRedundant(encoded, encoded_len);
Minyue323b1322015-05-25 13:49:37 +020070 if (duration >= 0 &&
71 duration * Channels() * sizeof(int16_t) > max_decoded_bytes) {
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +000072 return -1;
73 }
74 return DecodeRedundantInternal(encoded, encoded_len, sample_rate_hz, decoded,
75 speech_type);
76}
77
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +000078int AudioDecoder::DecodeRedundantInternal(const uint8_t* encoded,
79 size_t encoded_len,
80 int sample_rate_hz, int16_t* decoded,
81 SpeechType* speech_type) {
82 return DecodeInternal(encoded, encoded_len, sample_rate_hz, decoded,
83 speech_type);
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +000084}
85
86bool AudioDecoder::HasDecodePlc() const { return false; }
87
Peter Kastingdce40cf2015-08-24 14:52:23 -070088size_t AudioDecoder::DecodePlc(size_t num_frames, int16_t* decoded) {
89 return 0;
90}
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +000091
92int AudioDecoder::IncomingPacket(const uint8_t* payload,
93 size_t payload_len,
94 uint16_t rtp_sequence_number,
95 uint32_t rtp_timestamp,
96 uint32_t arrival_timestamp) {
97 return 0;
98}
99
100int AudioDecoder::ErrorCode() { return 0; }
101
minyue@webrtc.orga8cc3442015-02-13 14:01:54 +0000102int AudioDecoder::PacketDuration(const uint8_t* encoded,
103 size_t encoded_len) const {
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +0000104 return kNotImplemented;
105}
106
107int AudioDecoder::PacketDurationRedundant(const uint8_t* encoded,
108 size_t encoded_len) const {
109 return kNotImplemented;
110}
111
112bool AudioDecoder::PacketHasFec(const uint8_t* encoded,
113 size_t encoded_len) const {
114 return false;
115}
116
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +0000117AudioDecoder::SpeechType AudioDecoder::ConvertSpeechType(int16_t type) {
118 switch (type) {
119 case 0: // TODO(hlundin): Both iSAC and Opus return 0 for speech.
120 case 1:
121 return kSpeech;
122 case 2:
123 return kComfortNoise;
124 default:
125 assert(false);
126 return kSpeech;
127 }
128}
129
130} // namespace webrtc