1 /* 2 * Copyright (c) 2024, Alliance for Open Media. All rights reserved 3 * 4 * This source code is subject to the terms of the BSD 3-Clause Clear License 5 * and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear 6 * License was not distributed with this source code in the LICENSE file, you 7 * can obtain it at www.aomedia.org/license/software-license/bsd-3-c-c. If the 8 * Alliance for Open Media Patent License 1.0 was not distributed with this 9 * source code in the PATENTS file, you can obtain it at 10 * www.aomedia.org/license/patent. 11 */ 12 #include "iamf/cli/sample_processor_base.h" 13 14 #include <cstdint> 15 #include <vector> 16 17 #include "absl/status/status.h" 18 #include "absl/strings/str_cat.h" 19 #include "absl/types/span.h" 20 21 namespace iamf_tools { 22 ~SampleProcessorBase()23SampleProcessorBase::~SampleProcessorBase() {}; 24 PushFrame(absl::Span<const std::vector<int32_t>> time_channel_samples)25absl::Status SampleProcessorBase::PushFrame( 26 absl::Span<const std::vector<int32_t>> time_channel_samples) { 27 if (state_ != State::kTakingSamples) { 28 return absl::FailedPreconditionError(absl::StrCat( 29 "Do not use PushFrame() after Flush() is called. State= ", state_)); 30 } 31 32 // Check the shape of the input data. 33 if (time_channel_samples.size() > max_input_samples_per_frame_) { 34 return absl::InvalidArgumentError( 35 "Too many samples per frame. The maximum number of samples per frame " 36 "is: " + 37 absl::StrCat(max_input_samples_per_frame_) + 38 ". The number of samples per frame received is: " + 39 absl::StrCat(time_channel_samples.size())); 40 } 41 for (const auto& channel_samples : time_channel_samples) { 42 if (channel_samples.size() != num_channels_) { 43 return absl::InvalidArgumentError(absl::StrCat( 44 "Number of channels does not match the expected number of channels, " 45 "num_channels_", 46 num_channels_, " vs. ", channel_samples.size())); 47 } 48 } 49 50 num_valid_ticks_ = 0; 51 return PushFrameDerived(time_channel_samples); 52 } 53 Flush()54absl::Status SampleProcessorBase::Flush() { 55 if (state_ == State::kFlushCalled) { 56 return absl::FailedPreconditionError( 57 "Flush() called in unexpected state. Do not call Flush() twice."); 58 } 59 60 state_ = State::kFlushCalled; 61 num_valid_ticks_ = 0; 62 return FlushDerived(); 63 } 64 65 absl::Span<const std::vector<int32_t>> GetOutputSamplesAsSpan() const66SampleProcessorBase::GetOutputSamplesAsSpan() const { 67 return absl::MakeConstSpan(output_time_channel_samples_) 68 .first(num_valid_ticks_); 69 } 70 71 } // namespace iamf_tools 72