Make terminate_threads() less likely to leave _threads containing invalid pointers.
[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 void
85 AudioMapping::make_default (AudioProcessor const * processor, optional<boost::filesystem::path> filename)
86 {
87         static string const regex[] = {
88                 ".*[\\._-]L[\\._-].*",
89                 ".*[\\._-]R[\\._-].*",
90                 ".*[\\._-]C[\\._-].*",
91                 ".*[\\._-]Lfe[\\._-].*",
92                 ".*[\\._-]Ls[\\._-].*",
93                 ".*[\\._-]Rs[\\._-].*"
94         };
95
96         static int const regexes = sizeof(regex) / sizeof(*regex);
97
98         if (processor) {
99                 processor->make_audio_mapping_default (*this);
100         } else {
101                 make_zero ();
102                 if (input_channels() == 1) {
103                         bool guessed = false;
104
105                         /* See if we can guess where this stream should go */
106                         if (filename) {
107                                 for (int i = 0; i < regexes; ++i) {
108                                         boost::regex e (regex[i], boost::regex::icase);
109                                         if (boost::regex_match(filename->string(), e) && i < output_channels()) {
110                                                 set (0, i, 1);
111                                                 guessed = true;
112                                         }
113                                 }
114                         }
115
116                         if (!guessed) {
117                                 /* If we have no idea, just put it on centre */
118                                 set (0, static_cast<int>(dcp::CENTRE), 1);
119                         }
120                 } else {
121                         /* 1:1 mapping */
122                         for (int i = 0; i < min (input_channels(), output_channels()); ++i) {
123                                 set (i, i, 1);
124                         }
125                 }
126         }
127 }
128
129 AudioMapping::AudioMapping (cxml::ConstNodePtr node, int state_version)
130 {
131         if (state_version < 32) {
132                 setup (node->number_child<int> ("ContentChannels"), MAX_DCP_AUDIO_CHANNELS);
133         } else {
134                 setup (node->number_child<int> ("InputChannels"), node->number_child<int> ("OutputChannels"));
135         }
136
137         if (state_version <= 5) {
138                 /* Old-style: on/off mapping */
139                 list<cxml::NodePtr> const c = node->node_children ("Map");
140                 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
141                         set ((*i)->number_child<int> ("ContentIndex"), static_cast<dcp::Channel> ((*i)->number_child<int> ("DCP")), 1);
142                 }
143         } else {
144                 list<cxml::NodePtr> const c = node->node_children ("Gain");
145                 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
146                         if (state_version < 32) {
147                                 set (
148                                         (*i)->number_attribute<int> ("Content"),
149                                         static_cast<dcp::Channel> ((*i)->number_attribute<int> ("DCP")),
150                                         raw_convert<float> ((*i)->content ())
151                                         );
152                         } else {
153                                 set (
154                                         (*i)->number_attribute<int> ("Input"),
155                                         (*i)->number_attribute<int> ("Output"),
156                                         raw_convert<float> ((*i)->content ())
157                                         );
158                         }
159                 }
160         }
161 }
162
163 void
164 AudioMapping::set (int input_channel, int output_channel, float g)
165 {
166         DCPOMATIC_ASSERT (input_channel < int(_gain.size()));
167         DCPOMATIC_ASSERT (output_channel < int(_gain[0].size()));
168         _gain[input_channel][output_channel] = g;
169 }
170
171 float
172 AudioMapping::get (int input_channel, int output_channel) const
173 {
174         DCPOMATIC_ASSERT (input_channel < int (_gain.size()));
175         DCPOMATIC_ASSERT (output_channel < int (_gain[0].size()));
176         return _gain[input_channel][output_channel];
177 }
178
179 void
180 AudioMapping::as_xml (xmlpp::Node* node) const
181 {
182         node->add_child ("InputChannels")->add_child_text (raw_convert<string> (_input_channels));
183         node->add_child ("OutputChannels")->add_child_text (raw_convert<string> (_output_channels));
184
185         for (int c = 0; c < _input_channels; ++c) {
186                 for (int d = 0; d < _output_channels; ++d) {
187                         xmlpp::Element* t = node->add_child ("Gain");
188                         t->set_attribute ("Input", raw_convert<string> (c));
189                         t->set_attribute ("Output", raw_convert<string> (d));
190                         t->add_child_text (raw_convert<string> (get (c, d)));
191                 }
192         }
193 }
194
195 /** @return a string which is unique for a given AudioMapping configuration, for
196  *  differentiation between different AudioMappings.
197  */
198 string
199 AudioMapping::digest () const
200 {
201         Digester digester;
202         digester.add (_input_channels);
203         digester.add (_output_channels);
204         for (int i = 0; i < _input_channels; ++i) {
205                 for (int j = 0; j < _output_channels; ++j) {
206                         digester.add (_gain[i][j]);
207                 }
208         }
209
210         return digester.get ();
211 }
212
213 list<int>
214 AudioMapping::mapped_output_channels () const
215 {
216         static float const minus_96_db = 0.000015849;
217
218         list<int> mapped;
219
220         for (vector<vector<float> >::const_iterator i = _gain.begin(); i != _gain.end(); ++i) {
221                 for (size_t j = 0; j < i->size(); ++j) {
222                         if (abs ((*i)[j]) > minus_96_db) {
223                                 mapped.push_back (j);
224                         }
225                 }
226         }
227
228         mapped.sort ();
229         mapped.unique ();
230
231         return mapped;
232 }
233
234 void
235 AudioMapping::unmap_all ()
236 {
237         for (vector<vector<float> >::iterator i = _gain.begin(); i != _gain.end(); ++i) {
238                 for (vector<float>::iterator j = i->begin(); j != i->end(); ++j) {
239                         *j = 0;
240                 }
241         }
242 }