Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
Loading...
Searching...
No Matches
channel_mapper_matrix.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 Roc Streaming authors
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 */
8
9//! @file roc_audio/channel_mapper_matrix.h
10//! @brief Channel mapping matrix.
11
12#ifndef ROC_AUDIO_CHANNEL_MAPPER_MATRIX_H_
13#define ROC_AUDIO_CHANNEL_MAPPER_MATRIX_H_
14
19
20namespace roc {
21namespace audio {
22
23//! Channel mapping matrix.
24//!
25//! Used for mapping between two surround layouts. Not used if one or both
26//! layouts are multitrack.
27//!
28//! In surround mapping, every output channel is calculated as a sum of every
29//! input channel multiplied by a coefficient from this matrix.
30//!
31//! Matrix coefficients are defined for physical channel indices in frame,
32//! e.g. coeff(1, 2) defines coefficient for second channel in output frame
33//! and third channel in input frame, no matter what is the logical position
34//! of the channels (L, R, ...).
35//!
36//! This allows to use this matrix not just for mapping between different
37//! channel masks, but also for different channel orders, in one operation.
39public:
41
42 //! Build matrix.
43 //! @remarks
44 //! Builds matrix based on three tables:
45 //! - two channel order tables
46 //! (define order of input and output channels)
47 //! - channel mapping table
48 //! (defines mapping coefficients between input and output channels)
49 void build(const ChannelSet& in_chans, const ChannelSet& out_chans);
50
51 //! Returns coefficient for a pair of input and output indices.
52 //! @remarks
53 //! @p out_index and @p in_index define physical channel offsets
54 //! in audio frame, not their logical positions.
55 sample_t coeff(size_t out_index, size_t in_index) const {
56 return index_matrix_[out_index][in_index];
57 }
58
59private:
60 // Mapping of physical index in frame <=> logical channel position.
61 // We create one index map for input and one for output.
62 struct IndexMap {
63 ChannelSet enabled_chans;
64 size_t chan_2_index[ChanPos_Max];
65 ChannelPosition index_2_chan[ChanPos_Max];
66
67 IndexMap() {
68 memset(chan_2_index, 0, sizeof(chan_2_index));
69 memset(index_2_chan, 0, sizeof(index_2_chan));
70 }
71 };
72
73 // Mapping matrix for downmixing or upmixing from input to output.
74 // Uses logical channel positions.
75 struct ChannelMap {
76 sample_t chan_matrix[ChanPos_Max][ChanPos_Max];
77
78 ChannelMap() {
79 memset(chan_matrix, 0, sizeof(chan_matrix));
80 }
81 };
82
83 void build_index_mapping_(IndexMap& index_map, const ChannelSet& ch_set);
84
85 bool build_channel_mapping_(ChannelMap& result_map,
86 const ChannelSet& in_chans,
87 const ChannelSet& out_chans);
88
89 bool can_downmix_(const ChannelSet& in_chans, const ChannelSet& out_chans);
90
91 const ChannelMapTable* next_downmix_table_(const ChannelSet& in_chans,
92 const ChannelSet& out_chans);
93 const ChannelMapTable* next_upmix_table_(const ChannelSet& in_chans,
94 const ChannelSet& out_chans);
95
96 void fill_mapping_from_table_(ChannelMap& result_map,
97 const ChannelMapTable& map_table,
98 bool is_downmixing,
99 const ChannelSet& in_chans,
100 const ChannelSet& out_chans);
101
102 void fill_fallback_mapping_(ChannelMap& result_map,
103 const ChannelSet& in_chans,
104 const ChannelSet& out_chans);
105
106 void combine_mappings_(ChannelMap& result_map, const ChannelMap& next_map);
107 void normalize_mapping_(ChannelMap& chan_map);
108
109 void populate_index_matrix_(const IndexMap& in_index_map,
110 const IndexMap& out_index_map,
111 const ChannelMap& chan_map);
112
113 void print_table_matrix_(const ChannelMap& chan_map);
114 void print_index_matrix_(const IndexMap& in_index_map, const IndexMap& out_index_map);
115
116 sample_t index_matrix_[ChanPos_Max][ChanPos_Max];
117};
118
119} // namespace audio
120} // namespace roc
121
122#endif // ROC_AUDIO_CHANNEL_MAPPER_MATRIX_H_
Channel layout, order, and positions.
Channel set.
Channel tables.
sample_t coeff(size_t out_index, size_t in_index) const
Returns coefficient for a pair of input and output indices.
void build(const ChannelSet &in_chans, const ChannelSet &out_chans)
Build matrix.
Channel set. Multi-word bitmask with bits corresponding to enabled channels. Meaning of each channel ...
Definition channel_set.h:26
Base class for non-copyable objects.
Definition noncopyable.h:23
ChannelPosition
Surround channel position.
@ ChanPos_Max
Maximum value of enum.
float sample_t
Raw audio sample.
Definition sample.h:22
Root namespace.
Non-copyable object.