blob: cff9685548c704a5750e1bf3e7847749ca147e0f [file] [log] [blame]
Karl Wiberg0b058792015-09-15 17:28:18 +02001/*
2 * Copyright (c) 2015 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_coding/codecs/opus/audio_decoder_opus.h"
Karl Wiberg0b058792015-09-15 17:28:18 +020012
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <memory>
ossua70695a2016-09-22 02:06:28 -070014#include <utility>
15
Yves Gerey988cc082018-10-23 12:03:01 +020016#include "absl/types/optional.h"
17#include "api/array_view.h"
Alex Loikoe5b94162019-04-08 17:19:41 +020018#include "modules/audio_coding/codecs/opus/audio_coder_opus_common.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/checks.h"
Karl Wiberg0b058792015-09-15 17:28:18 +020020
21namespace webrtc {
22
Karl Wiberg7eb0a5e2019-05-29 13:46:09 +020023AudioDecoderOpusImpl::AudioDecoderOpusImpl(size_t num_channels,
24 int sample_rate_hz)
25 : channels_{num_channels}, sample_rate_hz_{sample_rate_hz} {
Alex Loikoe5b94162019-04-08 17:19:41 +020026 RTC_DCHECK(num_channels == 1 || num_channels == 2);
Karl Wiberg7eb0a5e2019-05-29 13:46:09 +020027 RTC_DCHECK(sample_rate_hz == 16000 || sample_rate_hz == 48000);
28 const int error =
29 WebRtcOpus_DecoderCreate(&dec_state_, channels_, sample_rate_hz_);
Alex Loiko7a3e43a2019-01-29 12:27:08 +010030 RTC_DCHECK(error == 0);
Karl Wiberg0b058792015-09-15 17:28:18 +020031 WebRtcOpus_DecoderInit(dec_state_);
32}
33
kwiberg96d74bb2017-06-30 05:24:56 -070034AudioDecoderOpusImpl::~AudioDecoderOpusImpl() {
Karl Wiberg0b058792015-09-15 17:28:18 +020035 WebRtcOpus_DecoderFree(dec_state_);
36}
37
kwiberg96d74bb2017-06-30 05:24:56 -070038std::vector<AudioDecoder::ParseResult> AudioDecoderOpusImpl::ParsePayload(
ossua70695a2016-09-22 02:06:28 -070039 rtc::Buffer&& payload,
40 uint32_t timestamp) {
41 std::vector<ParseResult> results;
42
43 if (PacketHasFec(payload.data(), payload.size())) {
44 const int duration =
45 PacketDurationRedundant(payload.data(), payload.size());
46 RTC_DCHECK_GE(duration, 0);
47 rtc::Buffer payload_copy(payload.data(), payload.size());
48 std::unique_ptr<EncodedAudioFrame> fec_frame(
49 new OpusFrame(this, std::move(payload_copy), false));
50 results.emplace_back(timestamp - duration, 1, std::move(fec_frame));
51 }
52 std::unique_ptr<EncodedAudioFrame> frame(
53 new OpusFrame(this, std::move(payload), true));
54 results.emplace_back(timestamp, 0, std::move(frame));
55 return results;
56}
57
kwiberg96d74bb2017-06-30 05:24:56 -070058int AudioDecoderOpusImpl::DecodeInternal(const uint8_t* encoded,
59 size_t encoded_len,
60 int sample_rate_hz,
61 int16_t* decoded,
62 SpeechType* speech_type) {
Karl Wiberg7eb0a5e2019-05-29 13:46:09 +020063 RTC_DCHECK_EQ(sample_rate_hz, sample_rate_hz_);
Karl Wiberg0b058792015-09-15 17:28:18 +020064 int16_t temp_type = 1; // Default is speech.
65 int ret =
66 WebRtcOpus_Decode(dec_state_, encoded, encoded_len, decoded, &temp_type);
67 if (ret > 0)
68 ret *= static_cast<int>(channels_); // Return total number of samples.
69 *speech_type = ConvertSpeechType(temp_type);
70 return ret;
71}
72
kwiberg96d74bb2017-06-30 05:24:56 -070073int AudioDecoderOpusImpl::DecodeRedundantInternal(const uint8_t* encoded,
74 size_t encoded_len,
75 int sample_rate_hz,
76 int16_t* decoded,
77 SpeechType* speech_type) {
Karl Wiberg0b058792015-09-15 17:28:18 +020078 if (!PacketHasFec(encoded, encoded_len)) {
79 // This packet is a RED packet.
80 return DecodeInternal(encoded, encoded_len, sample_rate_hz, decoded,
81 speech_type);
82 }
83
Karl Wiberg7eb0a5e2019-05-29 13:46:09 +020084 RTC_DCHECK_EQ(sample_rate_hz, sample_rate_hz_);
Karl Wiberg0b058792015-09-15 17:28:18 +020085 int16_t temp_type = 1; // Default is speech.
86 int ret = WebRtcOpus_DecodeFec(dec_state_, encoded, encoded_len, decoded,
87 &temp_type);
88 if (ret > 0)
89 ret *= static_cast<int>(channels_); // Return total number of samples.
90 *speech_type = ConvertSpeechType(temp_type);
91 return ret;
92}
93
kwiberg96d74bb2017-06-30 05:24:56 -070094void AudioDecoderOpusImpl::Reset() {
Karl Wiberg0b058792015-09-15 17:28:18 +020095 WebRtcOpus_DecoderInit(dec_state_);
96}
97
kwiberg96d74bb2017-06-30 05:24:56 -070098int AudioDecoderOpusImpl::PacketDuration(const uint8_t* encoded,
99 size_t encoded_len) const {
Karl Wiberg0b058792015-09-15 17:28:18 +0200100 return WebRtcOpus_DurationEst(dec_state_, encoded, encoded_len);
101}
102
kwiberg96d74bb2017-06-30 05:24:56 -0700103int AudioDecoderOpusImpl::PacketDurationRedundant(const uint8_t* encoded,
104 size_t encoded_len) const {
Karl Wiberg0b058792015-09-15 17:28:18 +0200105 if (!PacketHasFec(encoded, encoded_len)) {
106 // This packet is a RED packet.
107 return PacketDuration(encoded, encoded_len);
108 }
109
Karl Wiberg7eb0a5e2019-05-29 13:46:09 +0200110 return WebRtcOpus_FecDurationEst(encoded, encoded_len, sample_rate_hz_);
Karl Wiberg0b058792015-09-15 17:28:18 +0200111}
112
kwiberg96d74bb2017-06-30 05:24:56 -0700113bool AudioDecoderOpusImpl::PacketHasFec(const uint8_t* encoded,
114 size_t encoded_len) const {
Karl Wiberg0b058792015-09-15 17:28:18 +0200115 int fec;
116 fec = WebRtcOpus_PacketHasFec(encoded, encoded_len);
117 return (fec == 1);
118}
119
kwiberg96d74bb2017-06-30 05:24:56 -0700120int AudioDecoderOpusImpl::SampleRateHz() const {
Karl Wiberg7eb0a5e2019-05-29 13:46:09 +0200121 return sample_rate_hz_;
kwiberg6c2eab32016-05-31 02:46:20 -0700122}
123
kwiberg96d74bb2017-06-30 05:24:56 -0700124size_t AudioDecoderOpusImpl::Channels() const {
Karl Wiberg0b058792015-09-15 17:28:18 +0200125 return channels_;
126}
127
128} // namespace webrtc