• 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_AAC_ENCODER_DECODER_H_
13 #define CLI_AAC_ENCODER_DECODER_H_
14 
15 #include <cstdint>
16 #include <memory>
17 #include <vector>
18 
19 // This symbol conflicts with a macro in fdk_aac.
20 #ifdef IS_LITTLE_ENDIAN
21 #undef IS_LITTLE_ENDIAN
22 #endif
23 
24 #include "absl/status/status.h"
25 #include "iamf/cli/audio_frame_with_data.h"
26 #include "iamf/cli/codec/encoder_base.h"
27 #include "iamf/cli/proto/codec_config.pb.h"
28 #include "iamf/obu/codec_config.h"
29 #include "iamf/obu/decoder_config/aac_decoder_config.h"
30 #include "libAACenc/include/aacenc_lib.h"
31 
32 namespace iamf_tools {
33 
34 class AacEncoder : public EncoderBase {
35  public:
AacEncoder(const iamf_tools_cli_proto::AacEncoderMetadata & aac_encoder_metadata,const CodecConfigObu & codec_config,int num_channels)36   AacEncoder(
37       const iamf_tools_cli_proto::AacEncoderMetadata& aac_encoder_metadata,
38       const CodecConfigObu& codec_config, int num_channels)
39       : EncoderBase(codec_config, num_channels),
40         encoder_metadata_(aac_encoder_metadata),
41         decoder_config_(std::get<AacDecoderConfig>(
42             codec_config.GetCodecConfig().decoder_config)) {}
43 
44   ~AacEncoder() override;
45 
46  private:
47   /*!\brief Initializes the underlying encoder.
48    *
49    * \return `absl::OkStatus()` on success. A specific status on failure.
50    */
51   absl::Status InitializeEncoder() override;
52 
53   /*!\brief Initializes `required_samples_to_delay_at_start_`.
54    *
55    * `InitializeEncoder` is required to be called before calling this function.
56    *
57    * \param validate_codec_delay Ignored.
58    * \return `absl::OkStatus()` on success. A specific status on failure.
59    */
60   absl::Status SetNumberOfSamplesToDelayAtStart(
61       bool /*validate_codec_delay*/) override;
62 
63   /*!\brief Encodes an audio frame.
64    *
65    * \param input_bit_depth Bit-depth of the input data.
66    * \param samples Samples arranged in (time x channel) axes. The samples are
67    *        left-justified and stored in the upper `input_bit_depth` bits.
68    * \param partial_audio_frame_with_data Unique pointer to take ownership of.
69    *        The underlying `audio_frame_` is modified. All other fields are
70    *        blindly passed along.
71    * \return `absl::OkStatus()` on success. A specific status on failure.
72    */
73   absl::Status EncodeAudioFrame(
74       int input_bit_depth, const std::vector<std::vector<int32_t>>& samples,
75       std::unique_ptr<AudioFrameWithData> partial_audio_frame_with_data)
76       override;
77 
78   const iamf_tools_cli_proto::AacEncoderMetadata encoder_metadata_;
79   const AacDecoderConfig decoder_config_;
80 
81   // A pointer to the `fdk_aac` encoder.
82   AACENCODER* encoder_ = nullptr;
83 };
84 
85 }  // namespace iamf_tools
86 
87 #endif  // CLI_AAC_ENCODER_DECODER_H_
88