blob: d9efc211da3dac6800b21b5d979db91bd6de6831 [file] [log] [blame]
ossu0d526d52016-09-21 08:57:311/*
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
Mirko Bonadei92ea95e2017-09-15 04:47:3111#include "modules/audio_coding/codecs/legacy_encoded_audio_frame.h"
ossu0d526d52016-09-21 08:57:3112
13#include <algorithm>
14#include <memory>
15#include <utility>
16
Yves Gerey988cc082018-10-23 10:03:0117#include "rtc_base/checks.h"
18
ossu0d526d52016-09-21 08:57:3119namespace webrtc {
20
21LegacyEncodedAudioFrame::LegacyEncodedAudioFrame(AudioDecoder* decoder,
ossua70695a2016-09-22 09:06:2822 rtc::Buffer&& payload)
23 : decoder_(decoder), payload_(std::move(payload)) {}
ossu0d526d52016-09-21 08:57:3124
25LegacyEncodedAudioFrame::~LegacyEncodedAudioFrame() = default;
26
27size_t LegacyEncodedAudioFrame::Duration() const {
ossua70695a2016-09-22 09:06:2828 const int ret = decoder_->PacketDuration(payload_.data(), payload_.size());
ossu0d526d52016-09-21 08:57:3129 return (ret < 0) ? 0 : static_cast<size_t>(ret);
30}
31
Danil Chapovalovb6021232018-06-19 11:26:3632absl::optional<AudioDecoder::EncodedAudioFrame::DecodeResult>
ossu0d526d52016-09-21 08:57:3133LegacyEncodedAudioFrame::Decode(rtc::ArrayView<int16_t> decoded) const {
34 AudioDecoder::SpeechType speech_type = AudioDecoder::kSpeech;
ossua70695a2016-09-22 09:06:2835 const int ret = decoder_->Decode(
36 payload_.data(), payload_.size(), decoder_->SampleRateHz(),
37 decoded.size() * sizeof(int16_t), decoded.data(), &speech_type);
ossu0d526d52016-09-21 08:57:3138
39 if (ret < 0)
Danil Chapovalovb6021232018-06-19 11:26:3640 return absl::nullopt;
ossu0d526d52016-09-21 08:57:3141
Oskar Sundbom12ab00b2017-11-16 14:31:3842 return DecodeResult{static_cast<size_t>(ret), speech_type};
ossu0d526d52016-09-21 08:57:3143}
44
45std::vector<AudioDecoder::ParseResult> LegacyEncodedAudioFrame::SplitBySamples(
46 AudioDecoder* decoder,
47 rtc::Buffer&& payload,
48 uint32_t timestamp,
ossu0d526d52016-09-21 08:57:3149 size_t bytes_per_ms,
50 uint32_t timestamps_per_ms) {
51 RTC_DCHECK(payload.data());
52 std::vector<AudioDecoder::ParseResult> results;
53 size_t split_size_bytes = payload.size();
54
55 // Find a "chunk size" >= 20 ms and < 40 ms.
56 const size_t min_chunk_size = bytes_per_ms * 20;
57 if (min_chunk_size >= payload.size()) {
58 std::unique_ptr<LegacyEncodedAudioFrame> frame(
ossua70695a2016-09-22 09:06:2859 new LegacyEncodedAudioFrame(decoder, std::move(payload)));
60 results.emplace_back(timestamp, 0, std::move(frame));
ossu0d526d52016-09-21 08:57:3161 } else {
62 // Reduce the split size by half as long as |split_size_bytes| is at least
63 // twice the minimum chunk size (so that the resulting size is at least as
64 // large as the minimum chunk size).
65 while (split_size_bytes >= 2 * min_chunk_size) {
66 split_size_bytes /= 2;
67 }
68
69 const uint32_t timestamps_per_chunk = static_cast<uint32_t>(
70 split_size_bytes * timestamps_per_ms / bytes_per_ms);
71 size_t byte_offset;
72 uint32_t timestamp_offset;
Yves Gerey665174f2018-06-19 13:03:0573 for (byte_offset = 0, timestamp_offset = 0; byte_offset < payload.size();
ossu0d526d52016-09-21 08:57:3174 byte_offset += split_size_bytes,
Yves Gerey665174f2018-06-19 13:03:0575 timestamp_offset += timestamps_per_chunk) {
ossu0d526d52016-09-21 08:57:3176 split_size_bytes =
77 std::min(split_size_bytes, payload.size() - byte_offset);
78 rtc::Buffer new_payload(payload.data() + byte_offset, split_size_bytes);
79 std::unique_ptr<LegacyEncodedAudioFrame> frame(
ossua70695a2016-09-22 09:06:2880 new LegacyEncodedAudioFrame(decoder, std::move(new_payload)));
81 results.emplace_back(timestamp + timestamp_offset, 0, std::move(frame));
ossu0d526d52016-09-21 08:57:3182 }
83 }
84
85 return results;
86}
87
88} // namespace webrtc