Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(642)

Side by Side Diff: webrtc/modules/audio_coding/neteq/tools/fake_decode_from_file.cc

Issue 2275903002: Make FakeDecodeFromFile handle codec-internal CNG (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Fixing compile errors Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2016 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/neteq/tools/fake_decode_from_file.h" 11 #include "webrtc/modules/audio_coding/neteq/tools/fake_decode_from_file.h"
12 12
13 #include "webrtc/base/checks.h" 13 #include "webrtc/base/checks.h"
14 #include "webrtc/base/safe_conversions.h" 14 #include "webrtc/base/safe_conversions.h"
15 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" 15 #include "webrtc/modules/rtp_rtcp/source/byte_io.h"
16 16
17 namespace webrtc { 17 namespace webrtc {
18 namespace test { 18 namespace test {
19 19
20 int FakeDecodeFromFile::DecodeInternal(const uint8_t* encoded, 20 int FakeDecodeFromFile::DecodeInternal(const uint8_t* encoded,
21 size_t encoded_len, 21 size_t encoded_len,
22 int /*sample_rate_hz*/, 22 int sample_rate_hz,
23 int16_t* decoded, 23 int16_t* decoded,
24 SpeechType* speech_type) { 24 SpeechType* speech_type) {
25 RTC_CHECK_GE(encoded_len, 8u); 25 if (encoded_len == 0) {
26 // Decoder is asked to produce codec-internal comfort noise.
27 RTC_DCHECK(!encoded); // NetEq always sends nullptr in this case.
28 RTC_DCHECK(cng_mode_);
29 RTC_DCHECK_GT(last_decoded_length_, 0u);
30 std::fill_n(decoded, last_decoded_length_, 0);
31 *speech_type = kComfortNoise;
32 return last_decoded_length_;
33 }
34
35 RTC_CHECK_GE(encoded_len, 12u);
26 uint32_t timestamp_to_decode = 36 uint32_t timestamp_to_decode =
27 ByteReader<uint32_t>::ReadLittleEndian(encoded); 37 ByteReader<uint32_t>::ReadLittleEndian(encoded);
28 uint32_t samples_to_decode = 38 uint32_t samples_to_decode =
29 ByteReader<uint32_t>::ReadLittleEndian(&encoded[4]); 39 ByteReader<uint32_t>::ReadLittleEndian(&encoded[4]);
40 if (samples_to_decode == 0) {
41 // Number of samples in packet is unknown.
42 if (last_decoded_length_ > 0) {
43 // Use length of last decoded packet, but since this is the total for all
44 // channels, we have to divide by 2 in the stereo case.
45 samples_to_decode = rtc::CheckedDivExact(
46 last_decoded_length_, static_cast<size_t>(stereo_ ? 2uL : 1uL));
47 } else {
48 // This is the first packet to decode, and we do not know the length of
49 // it. Set it to 10 ms.
50 samples_to_decode = rtc::CheckedDivExact(sample_rate_hz, 100);
51 }
52 }
30 53
31 if (next_timestamp_from_input_ && 54 if (next_timestamp_from_input_ &&
32 timestamp_to_decode != *next_timestamp_from_input_) { 55 timestamp_to_decode != *next_timestamp_from_input_) {
33 // A gap in the timestamp sequence is detected. Skip the same number of 56 // A gap in the timestamp sequence is detected. Skip the same number of
34 // samples from the file. 57 // samples from the file.
35 uint32_t jump = timestamp_to_decode - *next_timestamp_from_input_; 58 uint32_t jump = timestamp_to_decode - *next_timestamp_from_input_;
36 RTC_CHECK(input_->Seek(jump)); 59 RTC_CHECK(input_->Seek(jump));
37 } 60 }
38 61
39 RTC_CHECK(input_->Read(static_cast<size_t>(samples_to_decode), decoded));
40 next_timestamp_from_input_ = 62 next_timestamp_from_input_ =
41 rtc::Optional<uint32_t>(timestamp_to_decode + samples_to_decode); 63 rtc::Optional<uint32_t>(timestamp_to_decode + samples_to_decode);
42 64
65 uint32_t original_payload_size_bytes =
66 ByteReader<uint32_t>::ReadLittleEndian(&encoded[8]);
67 if (original_payload_size_bytes == 1) {
68 // This is a comfort noise payload.
69 RTC_DCHECK_GT(last_decoded_length_, 0u);
70 std::fill_n(decoded, last_decoded_length_, 0);
71 *speech_type = kComfortNoise;
72 cng_mode_ = true;
73 return last_decoded_length_;
74 }
75
76 cng_mode_ = false;
77 RTC_CHECK(input_->Read(static_cast<size_t>(samples_to_decode), decoded));
78
43 if (stereo_) { 79 if (stereo_) {
44 InputAudioFile::DuplicateInterleaved(decoded, samples_to_decode, 2, 80 InputAudioFile::DuplicateInterleaved(decoded, samples_to_decode, 2,
45 decoded); 81 decoded);
46 samples_to_decode *= 2; 82 samples_to_decode *= 2;
47 } 83 }
48 84
49 *speech_type = kSpeech; 85 *speech_type = kSpeech;
50 return samples_to_decode; 86 return last_decoded_length_ = samples_to_decode;
51 } 87 }
52 88
53 void FakeDecodeFromFile::PrepareEncoded(uint32_t timestamp, 89 void FakeDecodeFromFile::PrepareEncoded(uint32_t timestamp,
54 size_t samples, 90 size_t samples,
91 size_t original_payload_size_bytes,
55 rtc::ArrayView<uint8_t> encoded) { 92 rtc::ArrayView<uint8_t> encoded) {
56 RTC_CHECK_GE(encoded.size(), 8u); 93 RTC_CHECK_GE(encoded.size(), 12u);
57 ByteWriter<uint32_t>::WriteLittleEndian(&encoded[0], timestamp); 94 ByteWriter<uint32_t>::WriteLittleEndian(&encoded[0], timestamp);
58 ByteWriter<uint32_t>::WriteLittleEndian(&encoded[4], 95 ByteWriter<uint32_t>::WriteLittleEndian(&encoded[4],
59 rtc::checked_cast<uint32_t>(samples)); 96 rtc::checked_cast<uint32_t>(samples));
97 ByteWriter<uint32_t>::WriteLittleEndian(
98 &encoded[8], rtc::checked_cast<uint32_t>(original_payload_size_bytes));
60 } 99 }
61 100
62 } // namespace test 101 } // namespace test
63 } // namespace webrtc 102 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698