• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2016 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 "modules/audio_coding/neteq/tools/fake_decode_from_file.h"
12 
13 #include "modules/rtp_rtcp/source/byte_io.h"
14 #include "rtc_base/checks.h"
15 #include "rtc_base/numerics/safe_conversions.h"
16 
17 namespace webrtc {
18 namespace test {
19 
20 namespace {
21 
22 class FakeEncodedFrame : public AudioDecoder::EncodedAudioFrame {
23  public:
FakeEncodedFrame(AudioDecoder * decoder,rtc::Buffer && payload)24   FakeEncodedFrame(AudioDecoder* decoder, rtc::Buffer&& payload)
25       : decoder_(decoder), payload_(std::move(payload)) {}
26 
Duration() const27   size_t Duration() const override {
28     const int ret = decoder_->PacketDuration(payload_.data(), payload_.size());
29     return ret < 0 ? 0 : static_cast<size_t>(ret);
30   }
31 
Decode(rtc::ArrayView<int16_t> decoded) const32   absl::optional<DecodeResult> Decode(
33       rtc::ArrayView<int16_t> decoded) const override {
34     auto speech_type = AudioDecoder::kSpeech;
35     const int ret = decoder_->Decode(
36         payload_.data(), payload_.size(), decoder_->SampleRateHz(),
37         decoded.size() * sizeof(int16_t), decoded.data(), &speech_type);
38     return ret < 0 ? absl::nullopt
39                    : absl::optional<DecodeResult>(
40                          {static_cast<size_t>(ret), speech_type});
41   }
42 
43   // This is to mimic OpusFrame.
IsDtxPacket() const44   bool IsDtxPacket() const override {
45     uint32_t original_payload_size_bytes =
46         ByteReader<uint32_t>::ReadLittleEndian(&payload_.data()[8]);
47     return original_payload_size_bytes <= 2;
48   }
49 
50  private:
51   AudioDecoder* const decoder_;
52   const rtc::Buffer payload_;
53 };
54 
55 }  // namespace
56 
ParsePayload(rtc::Buffer && payload,uint32_t timestamp)57 std::vector<AudioDecoder::ParseResult> FakeDecodeFromFile::ParsePayload(
58     rtc::Buffer&& payload,
59     uint32_t timestamp) {
60   std::vector<ParseResult> results;
61   std::unique_ptr<EncodedAudioFrame> frame(
62       new FakeEncodedFrame(this, std::move(payload)));
63   results.emplace_back(timestamp, 0, std::move(frame));
64   return results;
65 }
66 
DecodeInternal(const uint8_t * encoded,size_t encoded_len,int sample_rate_hz,int16_t * decoded,SpeechType * speech_type)67 int FakeDecodeFromFile::DecodeInternal(const uint8_t* encoded,
68                                        size_t encoded_len,
69                                        int sample_rate_hz,
70                                        int16_t* decoded,
71                                        SpeechType* speech_type) {
72   RTC_DCHECK_EQ(sample_rate_hz, SampleRateHz());
73 
74   const int samples_to_decode = PacketDuration(encoded, encoded_len);
75   const int total_samples_to_decode = samples_to_decode * (stereo_ ? 2 : 1);
76 
77   if (encoded_len == 0) {
78     // Decoder is asked to produce codec-internal comfort noise.
79     RTC_DCHECK(!encoded);  // NetEq always sends nullptr in this case.
80     RTC_DCHECK(cng_mode_);
81     RTC_DCHECK_GT(total_samples_to_decode, 0);
82     std::fill_n(decoded, total_samples_to_decode, 0);
83     *speech_type = kComfortNoise;
84     return rtc::dchecked_cast<int>(total_samples_to_decode);
85   }
86 
87   RTC_CHECK_GE(encoded_len, 12);
88   uint32_t timestamp_to_decode =
89       ByteReader<uint32_t>::ReadLittleEndian(encoded);
90 
91   if (next_timestamp_from_input_ &&
92       timestamp_to_decode != *next_timestamp_from_input_) {
93     // A gap in the timestamp sequence is detected. Skip the same number of
94     // samples from the file.
95     uint32_t jump = timestamp_to_decode - *next_timestamp_from_input_;
96     RTC_CHECK(input_->Seek(jump));
97   }
98 
99   next_timestamp_from_input_ = timestamp_to_decode + samples_to_decode;
100 
101   uint32_t original_payload_size_bytes =
102       ByteReader<uint32_t>::ReadLittleEndian(&encoded[8]);
103   if (original_payload_size_bytes <= 2) {
104     // This is a comfort noise payload.
105     RTC_DCHECK_GT(total_samples_to_decode, 0);
106     std::fill_n(decoded, total_samples_to_decode, 0);
107     *speech_type = kComfortNoise;
108     cng_mode_ = true;
109     return rtc::dchecked_cast<int>(total_samples_to_decode);
110   }
111 
112   cng_mode_ = false;
113   RTC_CHECK(input_->Read(static_cast<size_t>(samples_to_decode), decoded));
114 
115   if (stereo_) {
116     InputAudioFile::DuplicateInterleaved(decoded, samples_to_decode, 2,
117                                          decoded);
118   }
119 
120   *speech_type = kSpeech;
121   last_decoded_length_ = samples_to_decode;
122   return rtc::dchecked_cast<int>(total_samples_to_decode);
123 }
124 
PacketDuration(const uint8_t * encoded,size_t encoded_len) const125 int FakeDecodeFromFile::PacketDuration(const uint8_t* encoded,
126                                        size_t encoded_len) const {
127   const uint32_t original_payload_size_bytes =
128       encoded_len < 8 + sizeof(uint32_t)
129           ? 0
130           : ByteReader<uint32_t>::ReadLittleEndian(&encoded[8]);
131   const uint32_t samples_to_decode =
132       encoded_len < 4 + sizeof(uint32_t)
133           ? 0
134           : ByteReader<uint32_t>::ReadLittleEndian(&encoded[4]);
135   if (  // Decoder is asked to produce codec-internal comfort noise
136       encoded_len == 0 ||
137       // Comfort noise payload
138       original_payload_size_bytes <= 2 || samples_to_decode == 0 ||
139       // Erroneous duration since it is not a multiple of 10ms
140       samples_to_decode % rtc::CheckedDivExact(SampleRateHz(), 100) != 0) {
141     if (last_decoded_length_ > 0) {
142       // Use length of last decoded packet.
143       return rtc::dchecked_cast<int>(last_decoded_length_);
144     } else {
145       // This is the first packet to decode, and we do not know the length of
146       // it. Set it to 10 ms.
147       return rtc::CheckedDivExact(SampleRateHz(), 100);
148     }
149   }
150   return samples_to_decode;
151 }
152 
PrepareEncoded(uint32_t timestamp,size_t samples,size_t original_payload_size_bytes,rtc::ArrayView<uint8_t> encoded)153 void FakeDecodeFromFile::PrepareEncoded(uint32_t timestamp,
154                                         size_t samples,
155                                         size_t original_payload_size_bytes,
156                                         rtc::ArrayView<uint8_t> encoded) {
157   RTC_CHECK_GE(encoded.size(), 12);
158   ByteWriter<uint32_t>::WriteLittleEndian(&encoded[0], timestamp);
159   ByteWriter<uint32_t>::WriteLittleEndian(&encoded[4],
160                                           rtc::checked_cast<uint32_t>(samples));
161   ByteWriter<uint32_t>::WriteLittleEndian(
162       &encoded[8], rtc::checked_cast<uint32_t>(original_payload_size_bytes));
163 }
164 
165 }  // namespace test
166 }  // namespace webrtc
167