Skip to content

Commit

Permalink
Implement PacketDuration() for FakeDecoderFromFile.
Browse files Browse the repository at this point in the history
Bug: None
Change-Id: Ie4ab1ce737608706f12f298f793f76571805ca91
Reviewed-on: https://webrtc-review.googlesource.com/86160
Reviewed-by: Ivo Creusen <[email protected]>
Commit-Queue: Minyue Li <[email protected]>
Cr-Commit-Position: refs/heads/master@{#23780}
  • Loading branch information
minyuel authored and Commit Bot committed Jun 29, 2018
1 parent c54f706 commit a91deca
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
42 changes: 25 additions & 17 deletions modules/audio_coding/neteq/tools/fake_decode_from_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ int FakeDecodeFromFile::DecodeInternal(const uint8_t* encoded,
int sample_rate_hz,
int16_t* decoded,
SpeechType* speech_type) {
RTC_DCHECK_EQ(sample_rate_hz, SampleRateHz());
if (encoded_len == 0) {
// Decoder is asked to produce codec-internal comfort noise.
RTC_DCHECK(!encoded); // NetEq always sends nullptr in this case.
Expand All @@ -35,23 +36,6 @@ int FakeDecodeFromFile::DecodeInternal(const uint8_t* encoded,
RTC_CHECK_GE(encoded_len, 12);
uint32_t timestamp_to_decode =
ByteReader<uint32_t>::ReadLittleEndian(encoded);
uint32_t samples_to_decode =
ByteReader<uint32_t>::ReadLittleEndian(&encoded[4]);
if (samples_to_decode == 0 ||
samples_to_decode % rtc::CheckedDivExact(sample_rate_hz, 100) != 0) {
// Number of samples being zero or non-multiple of 10ms is considered
// erroneous.
if (last_decoded_length_ > 0) {
// Use length of last decoded packet, but since this is the total for all
// channels, we have to divide by 2 in the stereo case.
samples_to_decode = rtc::dchecked_cast<int>(rtc::CheckedDivExact(
last_decoded_length_, static_cast<size_t>(stereo_ ? 2uL : 1uL)));
} else {
// This is the first packet to decode, and we do not know the length of
// it. Set it to 10 ms.
samples_to_decode = rtc::CheckedDivExact(sample_rate_hz, 100);
}
}

if (next_timestamp_from_input_ &&
timestamp_to_decode != *next_timestamp_from_input_) {
Expand All @@ -61,6 +45,7 @@ int FakeDecodeFromFile::DecodeInternal(const uint8_t* encoded,
RTC_CHECK(input_->Seek(jump));
}

int samples_to_decode = PacketDuration(encoded, encoded_len);
next_timestamp_from_input_ = timestamp_to_decode + samples_to_decode;

uint32_t original_payload_size_bytes =
Expand Down Expand Up @@ -88,6 +73,29 @@ int FakeDecodeFromFile::DecodeInternal(const uint8_t* encoded,
return rtc::dchecked_cast<int>(last_decoded_length_);
}

int FakeDecodeFromFile::PacketDuration(const uint8_t* encoded,
size_t encoded_len) const {
uint32_t samples_to_decode =
encoded_len == 0 ? 0
: ByteReader<uint32_t>::ReadLittleEndian(&encoded[4]);
if (samples_to_decode == 0 ||
samples_to_decode % rtc::CheckedDivExact(SampleRateHz(), 100) != 0) {
// Number of samples being zero or non-multiple of 10ms is considered
// erroneous.
if (last_decoded_length_ > 0) {
// Use length of last decoded packet, but since this is the total for all
// channels, we have to divide by 2 in the stereo case.
samples_to_decode = rtc::dchecked_cast<int>(rtc::CheckedDivExact(
last_decoded_length_, static_cast<size_t>(stereo_ ? 2uL : 1uL)));
} else {
// This is the first packet to decode, and we do not know the length of
// it. Set it to 10 ms.
samples_to_decode = rtc::CheckedDivExact(SampleRateHz(), 100);
}
}
return samples_to_decode;
}

void FakeDecodeFromFile::PrepareEncoded(uint32_t timestamp,
size_t samples,
size_t original_payload_size_bytes,
Expand Down
2 changes: 2 additions & 0 deletions modules/audio_coding/neteq/tools/fake_decode_from_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class FakeDecodeFromFile : public AudioDecoder {
int16_t* decoded,
SpeechType* speech_type) override;

int PacketDuration(const uint8_t* encoded, size_t encoded_len) const override;

// Helper method. Writes |timestamp|, |samples| and
// |original_payload_size_bytes| to |encoded| in a format that the
// FakeDecodeFromFile decoder will understand. |encoded| must be at least 12
Expand Down

0 comments on commit a91deca

Please sign in to comment.