OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/audio_coding/codecs/opus/audio_decoder_opus.h" | 11 #include "webrtc/modules/audio_coding/codecs/opus/audio_decoder_opus.h" |
12 | 12 |
13 #include <utility> | 13 #include <utility> |
14 | 14 |
15 #include "webrtc/base/checks.h" | 15 #include "webrtc/base/checks.h" |
16 | 16 |
17 namespace webrtc { | 17 namespace webrtc { |
18 | 18 |
19 namespace { | 19 namespace { |
20 class OpusFrame : public AudioDecoder::EncodedAudioFrame { | 20 class OpusFrame : public AudioDecoder::EncodedAudioFrame { |
21 public: | 21 public: |
22 OpusFrame(AudioDecoderOpusImpl* decoder, | 22 OpusFrame(AudioDecoderOpus* decoder, |
23 rtc::Buffer&& payload, | 23 rtc::Buffer&& payload, |
24 bool is_primary_payload) | 24 bool is_primary_payload) |
25 : decoder_(decoder), | 25 : decoder_(decoder), |
26 payload_(std::move(payload)), | 26 payload_(std::move(payload)), |
27 is_primary_payload_(is_primary_payload) {} | 27 is_primary_payload_(is_primary_payload) {} |
28 | 28 |
29 size_t Duration() const override { | 29 size_t Duration() const override { |
30 int ret; | 30 int ret; |
31 if (is_primary_payload_) { | 31 if (is_primary_payload_) { |
32 ret = decoder_->PacketDuration(payload_.data(), payload_.size()); | 32 ret = decoder_->PacketDuration(payload_.data(), payload_.size()); |
(...skipping 17 matching lines...) Expand all Loading... |
50 decoded.size() * sizeof(int16_t), decoded.data(), &speech_type); | 50 decoded.size() * sizeof(int16_t), decoded.data(), &speech_type); |
51 } | 51 } |
52 | 52 |
53 if (ret < 0) | 53 if (ret < 0) |
54 return rtc::Optional<DecodeResult>(); | 54 return rtc::Optional<DecodeResult>(); |
55 | 55 |
56 return rtc::Optional<DecodeResult>({static_cast<size_t>(ret), speech_type}); | 56 return rtc::Optional<DecodeResult>({static_cast<size_t>(ret), speech_type}); |
57 } | 57 } |
58 | 58 |
59 private: | 59 private: |
60 AudioDecoderOpusImpl* const decoder_; | 60 AudioDecoderOpus* const decoder_; |
61 const rtc::Buffer payload_; | 61 const rtc::Buffer payload_; |
62 const bool is_primary_payload_; | 62 const bool is_primary_payload_; |
63 }; | 63 }; |
64 | 64 |
65 } // namespace | 65 } // namespace |
66 | 66 |
67 AudioDecoderOpusImpl::AudioDecoderOpusImpl(size_t num_channels) | 67 AudioDecoderOpus::AudioDecoderOpus(size_t num_channels) |
68 : channels_(num_channels) { | 68 : channels_(num_channels) { |
69 RTC_DCHECK(num_channels == 1 || num_channels == 2); | 69 RTC_DCHECK(num_channels == 1 || num_channels == 2); |
70 WebRtcOpus_DecoderCreate(&dec_state_, channels_); | 70 WebRtcOpus_DecoderCreate(&dec_state_, channels_); |
71 WebRtcOpus_DecoderInit(dec_state_); | 71 WebRtcOpus_DecoderInit(dec_state_); |
72 } | 72 } |
73 | 73 |
74 AudioDecoderOpusImpl::~AudioDecoderOpusImpl() { | 74 AudioDecoderOpus::~AudioDecoderOpus() { |
75 WebRtcOpus_DecoderFree(dec_state_); | 75 WebRtcOpus_DecoderFree(dec_state_); |
76 } | 76 } |
77 | 77 |
78 std::vector<AudioDecoder::ParseResult> AudioDecoderOpusImpl::ParsePayload( | 78 std::vector<AudioDecoder::ParseResult> AudioDecoderOpus::ParsePayload( |
79 rtc::Buffer&& payload, | 79 rtc::Buffer&& payload, |
80 uint32_t timestamp) { | 80 uint32_t timestamp) { |
81 std::vector<ParseResult> results; | 81 std::vector<ParseResult> results; |
82 | 82 |
83 if (PacketHasFec(payload.data(), payload.size())) { | 83 if (PacketHasFec(payload.data(), payload.size())) { |
84 const int duration = | 84 const int duration = |
85 PacketDurationRedundant(payload.data(), payload.size()); | 85 PacketDurationRedundant(payload.data(), payload.size()); |
86 RTC_DCHECK_GE(duration, 0); | 86 RTC_DCHECK_GE(duration, 0); |
87 rtc::Buffer payload_copy(payload.data(), payload.size()); | 87 rtc::Buffer payload_copy(payload.data(), payload.size()); |
88 std::unique_ptr<EncodedAudioFrame> fec_frame( | 88 std::unique_ptr<EncodedAudioFrame> fec_frame( |
89 new OpusFrame(this, std::move(payload_copy), false)); | 89 new OpusFrame(this, std::move(payload_copy), false)); |
90 results.emplace_back(timestamp - duration, 1, std::move(fec_frame)); | 90 results.emplace_back(timestamp - duration, 1, std::move(fec_frame)); |
91 } | 91 } |
92 std::unique_ptr<EncodedAudioFrame> frame( | 92 std::unique_ptr<EncodedAudioFrame> frame( |
93 new OpusFrame(this, std::move(payload), true)); | 93 new OpusFrame(this, std::move(payload), true)); |
94 results.emplace_back(timestamp, 0, std::move(frame)); | 94 results.emplace_back(timestamp, 0, std::move(frame)); |
95 return results; | 95 return results; |
96 } | 96 } |
97 | 97 |
98 int AudioDecoderOpusImpl::DecodeInternal(const uint8_t* encoded, | 98 int AudioDecoderOpus::DecodeInternal(const uint8_t* encoded, |
99 size_t encoded_len, | 99 size_t encoded_len, |
100 int sample_rate_hz, | 100 int sample_rate_hz, |
101 int16_t* decoded, | 101 int16_t* decoded, |
102 SpeechType* speech_type) { | 102 SpeechType* speech_type) { |
103 RTC_DCHECK_EQ(sample_rate_hz, 48000); | 103 RTC_DCHECK_EQ(sample_rate_hz, 48000); |
104 int16_t temp_type = 1; // Default is speech. | 104 int16_t temp_type = 1; // Default is speech. |
105 int ret = | 105 int ret = |
106 WebRtcOpus_Decode(dec_state_, encoded, encoded_len, decoded, &temp_type); | 106 WebRtcOpus_Decode(dec_state_, encoded, encoded_len, decoded, &temp_type); |
107 if (ret > 0) | 107 if (ret > 0) |
108 ret *= static_cast<int>(channels_); // Return total number of samples. | 108 ret *= static_cast<int>(channels_); // Return total number of samples. |
109 *speech_type = ConvertSpeechType(temp_type); | 109 *speech_type = ConvertSpeechType(temp_type); |
110 return ret; | 110 return ret; |
111 } | 111 } |
112 | 112 |
113 int AudioDecoderOpusImpl::DecodeRedundantInternal(const uint8_t* encoded, | 113 int AudioDecoderOpus::DecodeRedundantInternal(const uint8_t* encoded, |
114 size_t encoded_len, | 114 size_t encoded_len, |
115 int sample_rate_hz, | 115 int sample_rate_hz, |
116 int16_t* decoded, | 116 int16_t* decoded, |
117 SpeechType* speech_type) { | 117 SpeechType* speech_type) { |
118 if (!PacketHasFec(encoded, encoded_len)) { | 118 if (!PacketHasFec(encoded, encoded_len)) { |
119 // This packet is a RED packet. | 119 // This packet is a RED packet. |
120 return DecodeInternal(encoded, encoded_len, sample_rate_hz, decoded, | 120 return DecodeInternal(encoded, encoded_len, sample_rate_hz, decoded, |
121 speech_type); | 121 speech_type); |
122 } | 122 } |
123 | 123 |
124 RTC_DCHECK_EQ(sample_rate_hz, 48000); | 124 RTC_DCHECK_EQ(sample_rate_hz, 48000); |
125 int16_t temp_type = 1; // Default is speech. | 125 int16_t temp_type = 1; // Default is speech. |
126 int ret = WebRtcOpus_DecodeFec(dec_state_, encoded, encoded_len, decoded, | 126 int ret = WebRtcOpus_DecodeFec(dec_state_, encoded, encoded_len, decoded, |
127 &temp_type); | 127 &temp_type); |
128 if (ret > 0) | 128 if (ret > 0) |
129 ret *= static_cast<int>(channels_); // Return total number of samples. | 129 ret *= static_cast<int>(channels_); // Return total number of samples. |
130 *speech_type = ConvertSpeechType(temp_type); | 130 *speech_type = ConvertSpeechType(temp_type); |
131 return ret; | 131 return ret; |
132 } | 132 } |
133 | 133 |
134 void AudioDecoderOpusImpl::Reset() { | 134 void AudioDecoderOpus::Reset() { |
135 WebRtcOpus_DecoderInit(dec_state_); | 135 WebRtcOpus_DecoderInit(dec_state_); |
136 } | 136 } |
137 | 137 |
138 int AudioDecoderOpusImpl::PacketDuration(const uint8_t* encoded, | 138 int AudioDecoderOpus::PacketDuration(const uint8_t* encoded, |
139 size_t encoded_len) const { | 139 size_t encoded_len) const { |
140 return WebRtcOpus_DurationEst(dec_state_, encoded, encoded_len); | 140 return WebRtcOpus_DurationEst(dec_state_, encoded, encoded_len); |
141 } | 141 } |
142 | 142 |
143 int AudioDecoderOpusImpl::PacketDurationRedundant(const uint8_t* encoded, | 143 int AudioDecoderOpus::PacketDurationRedundant(const uint8_t* encoded, |
144 size_t encoded_len) const { | 144 size_t encoded_len) const { |
145 if (!PacketHasFec(encoded, encoded_len)) { | 145 if (!PacketHasFec(encoded, encoded_len)) { |
146 // This packet is a RED packet. | 146 // This packet is a RED packet. |
147 return PacketDuration(encoded, encoded_len); | 147 return PacketDuration(encoded, encoded_len); |
148 } | 148 } |
149 | 149 |
150 return WebRtcOpus_FecDurationEst(encoded, encoded_len); | 150 return WebRtcOpus_FecDurationEst(encoded, encoded_len); |
151 } | 151 } |
152 | 152 |
153 bool AudioDecoderOpusImpl::PacketHasFec(const uint8_t* encoded, | 153 bool AudioDecoderOpus::PacketHasFec(const uint8_t* encoded, |
154 size_t encoded_len) const { | 154 size_t encoded_len) const { |
155 int fec; | 155 int fec; |
156 fec = WebRtcOpus_PacketHasFec(encoded, encoded_len); | 156 fec = WebRtcOpus_PacketHasFec(encoded, encoded_len); |
157 return (fec == 1); | 157 return (fec == 1); |
158 } | 158 } |
159 | 159 |
160 int AudioDecoderOpusImpl::SampleRateHz() const { | 160 int AudioDecoderOpus::SampleRateHz() const { |
161 return 48000; | 161 return 48000; |
162 } | 162 } |
163 | 163 |
164 size_t AudioDecoderOpusImpl::Channels() const { | 164 size_t AudioDecoderOpus::Channels() const { |
165 return channels_; | 165 return channels_; |
166 } | 166 } |
167 | 167 |
168 } // namespace webrtc | 168 } // namespace webrtc |
OLD | NEW |