• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023, 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 
13 #ifndef CLI_DECODER_BASE_H_
14 #define CLI_DECODER_BASE_H_
15 
16 #include <cstddef>
17 #include <cstdint>
18 #include <vector>
19 
20 #include "absl/status/status.h"
21 
22 namespace iamf_tools {
23 
24 /*!\brief A common interfaces for all decoders.
25  */
26 class DecoderBase {
27  public:
28   /*!\brief Constructor.
29    *
30    * After constructing `Initialize` MUST be called and return successfully
31    * before using most functionality of the decoder.
32    *
33    * \param num_channels Number of channels for this stream.
34    * \param num_samples_per_channel Number of samples per channel.
35    */
DecoderBase(int num_channels,uint32_t num_samples_per_channel)36   DecoderBase(int num_channels, uint32_t num_samples_per_channel)
37       : num_channels_(num_channels),
38         num_samples_per_channel_(num_samples_per_channel),
39         decoded_samples_(num_samples_per_channel_,
40                          std::vector<int32_t>(num_channels)),
41         num_valid_ticks_(0) {}
42 
43   /*!\brief Destructor.
44    */
45   virtual ~DecoderBase() = default;
46 
47   /*!\brief Initializes the underlying decoder.
48    *
49    * \return `absl::OkStatus()` on success. A specific status on failure.
50    */
51   virtual absl::Status Initialize() = 0;
52 
53   /*!\brief Decodes an audio frame.
54    *
55    * \param encoded_frame Frame to decode.
56    * \return `absl::OkStatus()` on success. A specific status on failure.
57    */
58   virtual absl::Status DecodeAudioFrame(
59       const std::vector<uint8_t>& encoded_frame) = 0;
60 
61   /*!\brief Outputs valid decoded samples.
62    *
63    * \return Valid decoded samples.
64    */
ValidDecodedSamples()65   std::vector<std::vector<int32_t>> ValidDecodedSamples() const {
66     return {decoded_samples_.begin(),
67             decoded_samples_.begin() + num_valid_ticks_};
68   }
69 
70  protected:
71   const int num_channels_;
72   const uint32_t num_samples_per_channel_;
73 
74   // Stores the output decoded frames arranged in (time, sample) axes. That
75   // is to say, each inner vector has one sample for per channel and the outer
76   // vector contains one inner vector for each time tick. When the decoded
77   // samples is shorter than a frame, only the first `num_valid_ticks_` ticks
78   // should be used.
79   std::vector<std::vector<int32_t>> decoded_samples_;
80 
81   // Number of ticks (time samples) in `decoded_samples_` that are valid.
82   size_t num_valid_ticks_;
83 };
84 
85 }  // namespace iamf_tools
86 
87 #endif  // CLI_DECODER_BASE_H_
88