Extract common code out into kdm_for_screen()
[dcpomatic.git] / src / lib / audio_mapping.cc
1 /*
2     Copyright (C) 2013-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "audio_mapping.h"
22 #include "util.h"
23 #include "digester.h"
24 #include "audio_processor.h"
25 #include <dcp/raw_convert.h>
26 #include <libcxml/cxml.h>
27 #include <libxml++/libxml++.h>
28 #include <boost/regex.hpp>
29 #include <iostream>
30
31 using std::list;
32 using std::cout;
33 using std::make_pair;
34 using std::pair;
35 using std::string;
36 using std::min;
37 using std::vector;
38 using std::abs;
39 using boost::shared_ptr;
40 using boost::dynamic_pointer_cast;
41 using boost::optional;
42 using dcp::raw_convert;
43
44 AudioMapping::AudioMapping ()
45         : _input_channels (0)
46         , _output_channels (0)
47 {
48
49 }
50
51 /** Create an empty AudioMapping.
52  *  @param input_channels Number of input channels.
53  *  @param output_channels Number of output channels.
54  */
55 AudioMapping::AudioMapping (int input_channels, int output_channels)
56 {
57         setup (input_channels, output_channels);
58 }
59
60 void
61 AudioMapping::setup (int input_channels, int output_channels)
62 {
63         _input_channels = input_channels;
64         _output_channels = output_channels;
65
66         _gain.resize (_input_channels);
67         for (int i = 0; i < _input_channels; ++i) {
68                 _gain[i].resize (_output_channels);
69         }
70
71         make_zero ();
72 }
73
74 void
75 AudioMapping::make_zero ()
76 {
77         for (int i = 0; i < _input_channels; ++i) {
78                 for (int j = 0; j < _output_channels; ++j) {
79                         _gain[i][j] = 0;
80                 }
81         }
82 }
83
84 struct ChannelRegex
85 {
86         ChannelRegex (string regex_, int channel_)
87                 : regex (regex_)
88                 , channel (channel_)
89         {}
90
91         string regex;
92         int channel;
93 };
94
95 void
96 AudioMapping::make_default (AudioProcessor const * processor, optional<boost::filesystem::path> filename)
97 {
98         static ChannelRegex const regex[] = {
99                 ChannelRegex(".*[\\._-]L[\\._-].*", 0),
100                 ChannelRegex(".*[\\._-]R[\\._-].*", 1),
101                 ChannelRegex(".*[\\._-]C[\\._-].*", 2),
102                 ChannelRegex(".*[\\._-]Lfe[\\._-].*", 3),
103                 ChannelRegex(".*[\\._-]LFE[\\._-].*", 3),
104                 ChannelRegex(".*[\\._-]Lss[\\._-].*", 4),
105                 ChannelRegex(".*[\\._-]Lsr[\\._-].*", 6),
106                 ChannelRegex(".*[\\._-]Ls[\\._-].*", 4),
107                 ChannelRegex(".*[\\._-]Rss[\\._-].*", 5),
108                 ChannelRegex(".*[\\._-]Rsr[\\._-].*", 7),
109                 ChannelRegex(".*[\\._-]Rs[\\._-].*", 5),
110         };
111
112         static int const regexes = sizeof(regex) / sizeof(*regex);
113
114         if (processor) {
115                 processor->make_audio_mapping_default (*this);
116         } else {
117                 make_zero ();
118                 if (input_channels() == 1) {
119                         bool guessed = false;
120
121                         /* See if we can guess where this stream should go */
122                         if (filename) {
123                                 for (int i = 0; i < regexes; ++i) {
124                                         boost::regex e (regex[i].regex, boost::regex::icase);
125                                         if (boost::regex_match(filename->string(), e) && regex[i].channel < output_channels()) {
126                                                 set (0, regex[i].channel, 1);
127                                                 guessed = true;
128                                         }
129                                 }
130                         }
131
132                         if (!guessed) {
133                                 /* If we have no idea, just put it on centre */
134                                 set (0, static_cast<int>(dcp::CENTRE), 1);
135                         }
136                 } else {
137                         /* 1:1 mapping */
138                         for (int i = 0; i < min (input_channels(), output_channels()); ++i) {
139                                 set (i, i, 1);
140                         }
141                 }
142         }
143 }
144
145 AudioMapping::AudioMapping (cxml::ConstNodePtr node, int state_version)
146 {
147         if (state_version < 32) {
148                 setup (node->number_child<int> ("ContentChannels"), MAX_DCP_AUDIO_CHANNELS);
149         } else {
150                 setup (node->number_child<int> ("InputChannels"), node->number_child<int> ("OutputChannels"));
151         }
152
153         if (state_version <= 5) {
154                 /* Old-style: on/off mapping */
155                 list<cxml::NodePtr> const c = node->node_children ("Map");
156                 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
157                         set ((*i)->number_child<int> ("ContentIndex"), static_cast<dcp::Channel> ((*i)->number_child<int> ("DCP")), 1);
158                 }
159         } else {
160                 list<cxml::NodePtr> const c = node->node_children ("Gain");
161                 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
162                         if (state_version < 32) {
163                                 set (
164                                         (*i)->number_attribute<int> ("Content"),
165                                         static_cast<dcp::Channel> ((*i)->number_attribute<int> ("DCP")),
166                                         raw_convert<float> ((*i)->content ())
167                                         );
168                         } else {
169                                 set (
170                                         (*i)->number_attribute<int> ("Input"),
171                                         (*i)->number_attribute<int> ("Output"),
172                                         raw_convert<float> ((*i)->content ())
173                                         );
174                         }
175                 }
176         }
177 }
178
179 void
180 AudioMapping::set (int input_channel, int output_channel, float g)
181 {
182         DCPOMATIC_ASSERT (input_channel < int(_gain.size()));
183         DCPOMATIC_ASSERT (output_channel < int(_gain[0].size()));
184         _gain[input_channel][output_channel] = g;
185 }
186
187 float
188 AudioMapping::get (int input_channel, int output_channel) const
189 {
190         DCPOMATIC_ASSERT (input_channel < int (_gain.size()));
191         DCPOMATIC_ASSERT (output_channel < int (_gain[0].size()));
192         return _gain[input_channel][output_channel];
193 }
194
195 void
196 AudioMapping::as_xml (xmlpp::Node* node) const
197 {
198         node->add_child ("InputChannels")->add_child_text (raw_convert<string> (_input_channels));
199         node->add_child ("OutputChannels")->add_child_text (raw_convert<string> (_output_channels));
200
201         for (int c = 0; c < _input_channels; ++c) {
202                 for (int d = 0; d < _output_channels; ++d) {
203                         xmlpp::Element* t = node->add_child ("Gain");
204                         t->set_attribute ("Input", raw_convert<string> (c));
205                         t->set_attribute ("Output", raw_convert<string> (d));
206                         t->add_child_text (raw_convert<string> (get (c, d)));
207                 }
208         }
209 }
210
211 /** @return a string which is unique for a given AudioMapping configuration, for
212  *  differentiation between different AudioMappings.
213  */
214 string
215 AudioMapping::digest () const
216 {
217         Digester digester;
218         digester.add (_input_channels);
219         digester.add (_output_channels);
220         for (int i = 0; i < _input_channels; ++i) {
221                 for (int j = 0; j < _output_channels; ++j) {
222                         digester.add (_gain[i][j]);
223                 }
224         }
225
226         return digester.get ();
227 }
228
229 list<int>
230 AudioMapping::mapped_output_channels () const
231 {
232         static float const minus_96_db = 0.000015849;
233
234         list<int> mapped;
235
236         for (vector<vector<float> >::const_iterator i = _gain.begin(); i != _gain.end(); ++i) {
237                 for (size_t j = 0; j < i->size(); ++j) {
238                         if (abs ((*i)[j]) > minus_96_db) {
239                                 mapped.push_back (j);
240                         }
241                 }
242         }
243
244         mapped.sort ();
245         mapped.unique ();
246
247         return mapped;
248 }
249
250 void
251 AudioMapping::unmap_all ()
252 {
253         for (vector<vector<float> >::iterator i = _gain.begin(); i != _gain.end(); ++i) {
254                 for (vector<float>::iterator j = i->begin(); j != i->end(); ++j) {
255                         *j = 0;
256                 }
257         }
258 }