kwiberg@webrtc.org | e04a93b | 2014-12-09 10:12:53 +0000 | [diff] [blame] | 1 | /* |
| 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> |
ossu | 0d526d5 | 2016-09-21 01:57:31 -0700 | [diff] [blame] | 14 | #include <memory> |
| 15 | #include <utility> |
kwiberg@webrtc.org | e04a93b | 2014-12-09 10:12:53 +0000 | [diff] [blame] | 16 | |
kwiberg | ac554ee | 2016-09-02 00:39:33 -0700 | [diff] [blame] | 17 | #include "webrtc/base/array_view.h" |
kwiberg@webrtc.org | e04a93b | 2014-12-09 10:12:53 +0000 | [diff] [blame] | 18 | #include "webrtc/base/checks.h" |
kwiberg | ac554ee | 2016-09-02 00:39:33 -0700 | [diff] [blame] | 19 | #include "webrtc/base/sanitizer.h" |
Peter Boström | d7b7ae8 | 2015-12-08 13:41:35 +0100 | [diff] [blame] | 20 | #include "webrtc/base/trace_event.h" |
ossu | 0d526d5 | 2016-09-21 01:57:31 -0700 | [diff] [blame] | 21 | #include "webrtc/modules/audio_coding/codecs/legacy_encoded_audio_frame.h" |
kwiberg@webrtc.org | e04a93b | 2014-12-09 10:12:53 +0000 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | |
ossu | 61a208b | 2016-09-20 01:38:00 -0700 | [diff] [blame] | 25 | AudioDecoder::ParseResult::ParseResult() = default; |
| 26 | AudioDecoder::ParseResult::ParseResult(ParseResult&& b) = default; |
| 27 | AudioDecoder::ParseResult::ParseResult(uint32_t timestamp, |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 28 | int priority, |
ossu | 61a208b | 2016-09-20 01:38:00 -0700 | [diff] [blame] | 29 | std::unique_ptr<EncodedAudioFrame> frame) |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 30 | : timestamp(timestamp), priority(priority), frame(std::move(frame)) { |
| 31 | RTC_DCHECK_GE(priority, 0); |
| 32 | } |
ossu | 61a208b | 2016-09-20 01:38:00 -0700 | [diff] [blame] | 33 | |
| 34 | AudioDecoder::ParseResult::~ParseResult() = default; |
| 35 | |
| 36 | AudioDecoder::ParseResult& AudioDecoder::ParseResult::operator=( |
| 37 | ParseResult&& b) = default; |
| 38 | |
| 39 | std::vector<AudioDecoder::ParseResult> AudioDecoder::ParsePayload( |
| 40 | rtc::Buffer&& payload, |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 41 | uint32_t timestamp) { |
ossu | 61a208b | 2016-09-20 01:38:00 -0700 | [diff] [blame] | 42 | std::vector<ParseResult> results; |
| 43 | std::unique_ptr<EncodedAudioFrame> frame( |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 44 | new LegacyEncodedAudioFrame(this, std::move(payload))); |
| 45 | results.emplace_back(timestamp, 0, std::move(frame)); |
ossu | 61a208b | 2016-09-20 01:38:00 -0700 | [diff] [blame] | 46 | return results; |
| 47 | } |
| 48 | |
minyue@webrtc.org | 7f7d7e3 | 2015-03-16 12:30:37 +0000 | [diff] [blame] | 49 | int 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öm | d7b7ae8 | 2015-12-08 13:41:35 +0100 | [diff] [blame] | 52 | TRACE_EVENT0("webrtc", "AudioDecoder::Decode"); |
kwiberg | ac554ee | 2016-09-02 00:39:33 -0700 | [diff] [blame] | 53 | rtc::MsanCheckInitialized(rtc::MakeArrayView(encoded, encoded_len)); |
minyue@webrtc.org | 7f7d7e3 | 2015-03-16 12:30:37 +0000 | [diff] [blame] | 54 | int duration = PacketDuration(encoded, encoded_len); |
Minyue | 323b132 | 2015-05-25 13:49:37 +0200 | [diff] [blame] | 55 | if (duration >= 0 && |
| 56 | duration * Channels() * sizeof(int16_t) > max_decoded_bytes) { |
minyue@webrtc.org | 7f7d7e3 | 2015-03-16 12:30:37 +0000 | [diff] [blame] | 57 | return -1; |
| 58 | } |
| 59 | return DecodeInternal(encoded, encoded_len, sample_rate_hz, decoded, |
| 60 | speech_type); |
| 61 | } |
| 62 | |
| 63 | int 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öm | d7b7ae8 | 2015-12-08 13:41:35 +0100 | [diff] [blame] | 66 | TRACE_EVENT0("webrtc", "AudioDecoder::DecodeRedundant"); |
kwiberg | ac554ee | 2016-09-02 00:39:33 -0700 | [diff] [blame] | 67 | rtc::MsanCheckInitialized(rtc::MakeArrayView(encoded, encoded_len)); |
minyue@webrtc.org | 7f7d7e3 | 2015-03-16 12:30:37 +0000 | [diff] [blame] | 68 | int duration = PacketDurationRedundant(encoded, encoded_len); |
Minyue | 323b132 | 2015-05-25 13:49:37 +0200 | [diff] [blame] | 69 | if (duration >= 0 && |
| 70 | duration * Channels() * sizeof(int16_t) > max_decoded_bytes) { |
minyue@webrtc.org | 7f7d7e3 | 2015-03-16 12:30:37 +0000 | [diff] [blame] | 71 | return -1; |
| 72 | } |
| 73 | return DecodeRedundantInternal(encoded, encoded_len, sample_rate_hz, decoded, |
| 74 | speech_type); |
| 75 | } |
| 76 | |
minyue@webrtc.org | 7f7d7e3 | 2015-03-16 12:30:37 +0000 | [diff] [blame] | 77 | int 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.org | e04a93b | 2014-12-09 10:12:53 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | bool AudioDecoder::HasDecodePlc() const { return false; } |
| 86 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 87 | size_t AudioDecoder::DecodePlc(size_t num_frames, int16_t* decoded) { |
| 88 | return 0; |
| 89 | } |
kwiberg@webrtc.org | e04a93b | 2014-12-09 10:12:53 +0000 | [diff] [blame] | 90 | |
| 91 | int 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 | |
| 99 | int AudioDecoder::ErrorCode() { return 0; } |
| 100 | |
minyue@webrtc.org | a8cc344 | 2015-02-13 14:01:54 +0000 | [diff] [blame] | 101 | int AudioDecoder::PacketDuration(const uint8_t* encoded, |
| 102 | size_t encoded_len) const { |
kwiberg@webrtc.org | e04a93b | 2014-12-09 10:12:53 +0000 | [diff] [blame] | 103 | return kNotImplemented; |
| 104 | } |
| 105 | |
| 106 | int AudioDecoder::PacketDurationRedundant(const uint8_t* encoded, |
| 107 | size_t encoded_len) const { |
| 108 | return kNotImplemented; |
| 109 | } |
| 110 | |
| 111 | bool AudioDecoder::PacketHasFec(const uint8_t* encoded, |
| 112 | size_t encoded_len) const { |
| 113 | return false; |
| 114 | } |
| 115 | |
kwiberg@webrtc.org | e04a93b | 2014-12-09 10:12:53 +0000 | [diff] [blame] | 116 | AudioDecoder::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 |