• 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 #ifndef CLI_OPUS_ENCODER_DECODER_H_
13 #define CLI_OPUS_ENCODER_DECODER_H_
14 
15 #include <cstdint>
16 #include <memory>
17 #include <vector>
18 
19 #include "absl/status/status.h"
20 #include "iamf/cli/audio_frame_with_data.h"
21 #include "iamf/cli/codec/encoder_base.h"
22 #include "iamf/cli/proto/codec_config.pb.h"
23 #include "iamf/obu/codec_config.h"
24 #include "iamf/obu/decoder_config/opus_decoder_config.h"
25 #include "include/opus.h"
26 
27 namespace iamf_tools {
28 
29 class OpusEncoder : public EncoderBase {
30  public:
OpusEncoder(const iamf_tools_cli_proto::OpusEncoderMetadata & opus_encoder_metadata,const CodecConfigObu & codec_config,int num_channels,int substream_id)31   OpusEncoder(
32       const iamf_tools_cli_proto::OpusEncoderMetadata& opus_encoder_metadata,
33       const CodecConfigObu& codec_config, int num_channels, int substream_id)
34       : EncoderBase(codec_config, num_channels),
35         encoder_metadata_(opus_encoder_metadata),
36         decoder_config_(std::get<OpusDecoderConfig>(
37             codec_config.GetCodecConfig().decoder_config)),
38         substream_id_(substream_id) {}
39 
40   ~OpusEncoder() override;
41 
42  private:
43   // The encoder from `libopus` is in the global namespace.
44   typedef ::OpusEncoder LibOpusEncoder;
45 
46   /*!\brief Initializes the underlying encoder.
47    *
48    * \return `absl::OkStatus()` on success. A specific status on failure.
49    */
50   absl::Status InitializeEncoder() override;
51 
52   /*!\brief Initializes `required_samples_to_delay_at_start_`.
53    *
54    * `InitializeEncoder` is required to be called before calling this function.
55    * This value may vary based on `encoder_metadata_`, num_channels_` or
56    * settings in the associated Codec Config OBU.
57    *
58    * \param validate_codec_delay If true, validates `pre_skip` agrees with the
59    *        encoder.
60    * \return `absl::OkStatus()` on success. A specific status on failure.
61    */
62   absl::Status SetNumberOfSamplesToDelayAtStart(
63       bool validate_codec_delay) override;
64 
65   /*!\brief Encodes an audio frame.
66    *
67    * \param input_bit_depth Ignored.
68    * \param samples Samples arranged in (time x channel) axes. The samples are
69    *        left-justified and stored in the upper `input_bit_depth` bits.
70    * \param partial_audio_frame_with_data Unique pointer to take ownership of.
71    *        The underlying `audio_frame_` is modified. All other fields are
72    *        blindly passed along.
73    * \return `absl::OkStatus()` on success. A specific status on failure.
74    */
75   absl::Status EncodeAudioFrame(
76       int /*input_bit_depth*/, const std::vector<std::vector<int32_t>>& samples,
77       std::unique_ptr<AudioFrameWithData> partial_audio_frame_with_data)
78       override;
79 
80   /*!\brief Validates the underlying encoder.
81    *
82    * Configures the encoder based on the `encoder_metadata_`, the associated
83    * Codec Config OBU, and IAMF requirements.
84    *
85    * \return `absl::OkStatus()` on success. A specific status on failure.
86    */
87   absl::Status ValidateEncoderInfo();
88 
89   const iamf_tools_cli_proto::OpusEncoderMetadata encoder_metadata_;
90   const OpusDecoderConfig decoder_config_;
91   const int substream_id_;
92 
93   LibOpusEncoder* encoder_ = nullptr;
94 };
95 
96 }  // namespace iamf_tools
97 
98 #endif  // CLI_OPUS_ENCODER_DECODER_H_
99