• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef CLI_INTERNAL_RENDERER_AUDIO_ELEMENT_RENDERER_CHANNEL_TO_CHANNEL_H_
13 #define CLI_INTERNAL_RENDERER_AUDIO_ELEMENT_RENDERER_CHANNEL_TO_CHANNEL_H_
14 #include <cstddef>
15 #include <memory>
16 #include <string>
17 #include <vector>
18 
19 #include "absl/base/thread_annotations.h"
20 #include "absl/status/status.h"
21 #include "absl/strings/string_view.h"
22 #include "absl/types/span.h"
23 #include "iamf/cli/channel_label.h"
24 #include "iamf/cli/renderer/audio_element_renderer_base.h"
25 #include "iamf/obu/audio_element.h"
26 #include "iamf/obu/mix_presentation.h"
27 #include "iamf/obu/types.h"
28 
29 namespace iamf_tools {
30 /*!\brief Renders demixed channels to the requested output layout.
31  *
32  * This class represents a renderer which is suitable for use when the
33  * associated audio element has a layer which does not matches the playback
34  * layout according to IAMF Spec 7.3.2.1
35  * (https://aomediacodec.github.io/iamf/#processing-mixpresentation-rendering-m2l).
36  *
37  * - Call `RenderAudioFrame()` to render a labeled frame. The rendering may
38  *   happen asynchronously.
39  * - Call `SamplesAvailable()` to see if there are samples available.
40  * - Call `Flush()` to retrieve finished frames, in the order they were
41  *   received by `RenderLabeledFrame()`.
42  * - Call `Finalize()` to close the renderer, telling it to finish rendering
43  *   any remaining frames, which can be retrieved one last time via `Flush()`.
44  *   After calling `Finalize()`, any subsequent call to `RenderAudioFrame()`
45  *   may fail.
46  */
47 class AudioElementRendererChannelToChannel : public AudioElementRendererBase {
48  public:
49   /*!\brief Creates a channel to channel renderer from a channel-based config.
50    *
51    * \param scalable_channel_layout_config Config for the scalable channel
52    *        layout.
53    * \param playback_layout Layout of the audio element to be rendered.
54    * \param num_samples_per_frame Number of samples per frame.
55    * \return Render to use or `nullptr` on failure.
56    */
57   static std::unique_ptr<AudioElementRendererChannelToChannel>
58   CreateFromScalableChannelLayoutConfig(
59       const ScalableChannelLayoutConfig& scalable_channel_layout_config,
60       const Layout& playback_layout, size_t num_samples_per_frame);
61 
62   /*!\brief Destructor. */
63   ~AudioElementRendererChannelToChannel() override = default;
64 
65  private:
66   /*!\brief Constructor.
67    *
68    * Used only by the factory method.
69    *
70    * \param input_key Key representing the input loudspeaker layout.
71    * \param output_key Key representing the output loudspeaker layout.
72    * \param num_output_channels Number of output channels.
73    * \param num_samples_per_frame Number of samples per frame.
74    * \param ordered_labels Ordered list of channel labels to render.
75    */
AudioElementRendererChannelToChannel(absl::string_view input_key,absl::string_view output_key,size_t num_output_channels,size_t num_samples_per_frame,const std::vector<ChannelLabel::Label> & ordered_labels,const std::vector<std::vector<double>> & gains)76   AudioElementRendererChannelToChannel(
77       absl::string_view input_key, absl::string_view output_key,
78       size_t num_output_channels, size_t num_samples_per_frame,
79       const std::vector<ChannelLabel::Label>& ordered_labels,
80       const std::vector<std::vector<double>>& gains)
81       : AudioElementRendererBase(ordered_labels, num_samples_per_frame,
82                                  num_output_channels),
83         input_key_(input_key),
84         output_key_(output_key),
85         gains_(gains) {}
86 
87   /*!\brief Renders samples.
88    *
89    * \param samples_to_render Samples to render arranged in (time, channel).
90    * \param rendered_samples Output rendered samples.
91    * \return `absl::OkStatus()` on success. A specific status on failure.
92    */
93   absl::Status RenderSamples(
94       absl::Span<const std::vector<InternalSampleType>> samples_to_render,
95       std::vector<InternalSampleType>& rendered_samples)
96       ABSL_SHARED_LOCKS_REQUIRED(mutex_) override;
97 
98   const std::string input_key_;
99   const std::string output_key_;
100   const std::vector<std::vector<double>> gains_;
101 };
102 
103 }  // namespace iamf_tools
104 #endif  // CLI_INTERNAL_RENDERER_AUDIO_ELEMENT_RENDERER_CHANNEL_TO_CHANNEL_H_
105