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