Missed update to private test repo version.
[dcpomatic.git] / src / lib / audio_content.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_content.h"
22 #include "film.h"
23 #include "exceptions.h"
24 #include "config.h"
25 #include "frame_rate_change.h"
26 #include "compose.hpp"
27 #include <dcp/raw_convert.h>
28 #include <libcxml/cxml.h>
29 #include <libxml++/libxml++.h>
30 #include <boost/foreach.hpp>
31 #include <iostream>
32
33 #include "i18n.h"
34
35 using std::string;
36 using std::cout;
37 using std::vector;
38 using std::fixed;
39 using std::list;
40 using std::pair;
41 using std::setprecision;
42 using boost::shared_ptr;
43 using boost::dynamic_pointer_cast;
44 using boost::optional;
45 using dcp::raw_convert;
46
47 /** Something stream-related has changed */
48 int const AudioContentProperty::STREAMS = 200;
49 int const AudioContentProperty::GAIN = 201;
50 int const AudioContentProperty::DELAY = 202;
51
52 AudioContent::AudioContent (Content* parent)
53         : ContentPart (parent)
54         , _gain (0)
55         , _delay (Config::instance()->default_audio_delay ())
56 {
57
58 }
59
60 shared_ptr<AudioContent>
61 AudioContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version)
62 {
63         if (version < 34) {
64                 /* With old metadata FFmpeg content has the audio-related tags even with no
65                    audio streams, so check for that.
66                 */
67                 if (node->string_child("Type") == "FFmpeg" && node->node_children("AudioStream").empty()) {
68                         return shared_ptr<AudioContent> ();
69                 }
70
71                 /* Otherwise we can drop through to the newer logic */
72         }
73
74         if (!node->optional_number_child<double> ("AudioGain")) {
75                 return shared_ptr<AudioContent> ();
76         }
77
78         return shared_ptr<AudioContent> (new AudioContent (parent, node));
79 }
80
81 AudioContent::AudioContent (Content* parent, cxml::ConstNodePtr node)
82         : ContentPart (parent)
83 {
84         _gain = node->number_child<double> ("AudioGain");
85         _delay = node->number_child<int> ("AudioDelay");
86
87         /* Backwards compatibility */
88         optional<double> r = node->optional_number_child<double> ("AudioVideoFrameRate");
89         if (r) {
90                 _parent->set_video_frame_rate (r.get ());
91         }
92 }
93
94 AudioContent::AudioContent (Content* parent, vector<shared_ptr<Content> > c)
95         : ContentPart (parent)
96 {
97         shared_ptr<AudioContent> ref = c[0]->audio;
98         DCPOMATIC_ASSERT (ref);
99
100         for (size_t i = 1; i < c.size(); ++i) {
101                 if (c[i]->audio->gain() != ref->gain()) {
102                         throw JoinError (_("Content to be joined must have the same audio gain."));
103                 }
104
105                 if (c[i]->audio->delay() != ref->delay()) {
106                         throw JoinError (_("Content to be joined must have the same audio delay."));
107                 }
108         }
109
110         _gain = ref->gain ();
111         _delay = ref->delay ();
112         _streams = ref->streams ();
113 }
114
115 void
116 AudioContent::as_xml (xmlpp::Node* node) const
117 {
118         boost::mutex::scoped_lock lm (_mutex);
119         node->add_child("AudioGain")->add_child_text (raw_convert<string> (_gain));
120         node->add_child("AudioDelay")->add_child_text (raw_convert<string> (_delay));
121 }
122
123 void
124 AudioContent::set_gain (double g)
125 {
126         maybe_set (_gain, g, AudioContentProperty::GAIN);
127 }
128
129 void
130 AudioContent::set_delay (int d)
131 {
132         maybe_set (_delay, d, AudioContentProperty::DELAY);
133 }
134
135 string
136 AudioContent::technical_summary () const
137 {
138         string s = "audio: ";
139         BOOST_FOREACH (AudioStreamPtr i, streams ()) {
140                 s += String::compose ("stream channels %1 rate %2 ", i->channels(), i->frame_rate());
141         }
142
143         return s;
144 }
145
146 void
147 AudioContent::set_mapping (AudioMapping mapping)
148 {
149         ChangeSignaller<Content> cc (_parent, AudioContentProperty::STREAMS);
150
151         int c = 0;
152         BOOST_FOREACH (AudioStreamPtr i, streams ()) {
153                 AudioMapping stream_mapping (i->channels (), MAX_DCP_AUDIO_CHANNELS);
154                 for (int j = 0; j < i->channels(); ++j) {
155                         for (int k = 0; k < MAX_DCP_AUDIO_CHANNELS; ++k) {
156                                 stream_mapping.set (j, k, mapping.get (c, k));
157                         }
158                         ++c;
159                 }
160                 i->set_mapping (stream_mapping);
161         }
162 }
163
164 AudioMapping
165 AudioContent::mapping () const
166 {
167         int channels = 0;
168         BOOST_FOREACH (AudioStreamPtr i, streams ()) {
169                 channels += i->channels ();
170         }
171
172         AudioMapping merged (channels, MAX_DCP_AUDIO_CHANNELS);
173         merged.make_zero ();
174
175         int c = 0;
176         int s = 0;
177         BOOST_FOREACH (AudioStreamPtr i, streams ()) {
178                 AudioMapping mapping = i->mapping ();
179                 for (int j = 0; j < mapping.input_channels(); ++j) {
180                         for (int k = 0; k < MAX_DCP_AUDIO_CHANNELS; ++k) {
181                                 if (k < mapping.output_channels()) {
182                                         merged.set (c, k, mapping.get (j, k));
183                                 }
184                         }
185                         ++c;
186                 }
187                 ++s;
188         }
189
190         return merged;
191 }
192
193 /** @return the frame rate that this content should be resampled to in order
194  *  that it is in sync with the active video content at its start time.
195  */
196 int
197 AudioContent::resampled_frame_rate (shared_ptr<const Film> film) const
198 {
199         double t = film->audio_frame_rate ();
200
201         FrameRateChange frc (film, _parent);
202
203         /* Compensate if the DCP is being run at a different frame rate
204            to the source; that is, if the video is run such that it will
205            look different in the DCP compared to the source (slower or faster).
206         */
207
208         if (frc.change_speed) {
209                 t /= frc.speed_up;
210         }
211
212         return lrint (t);
213 }
214
215 string
216 AudioContent::processing_description (shared_ptr<const Film> film) const
217 {
218         if (streams().empty ()) {
219                 return "";
220         }
221
222         /* Possible answers are:
223            1. all audio will be resampled from x to y.
224            2. all audio will be resampled to y (from a variety of rates)
225            3. some audio will be resampled to y (from a variety of rates)
226            4. nothing will be resampled.
227         */
228
229         bool not_resampled = false;
230         bool resampled = false;
231         bool same = true;
232
233         optional<int> common_frame_rate;
234         BOOST_FOREACH (AudioStreamPtr i, streams()) {
235                 if (i->frame_rate() != resampled_frame_rate(film)) {
236                         resampled = true;
237                 } else {
238                         not_resampled = true;
239                 }
240
241                 if (common_frame_rate && common_frame_rate != i->frame_rate ()) {
242                         same = false;
243                 }
244                 common_frame_rate = i->frame_rate ();
245         }
246
247         if (not_resampled && !resampled) {
248                 return _("Audio will not be resampled");
249         }
250
251         if (not_resampled && resampled) {
252                 return String::compose (_("Some audio will be resampled to %1Hz"), resampled_frame_rate(film));
253         }
254
255         if (!not_resampled && resampled) {
256                 if (same) {
257                         return String::compose (_("Audio will be resampled from %1Hz to %2Hz"), common_frame_rate.get(), resampled_frame_rate(film));
258                 } else {
259                         return String::compose (_("Audio will be resampled to %1Hz"), resampled_frame_rate(film));
260                 }
261         }
262
263         return "";
264 }
265
266 /** @return User-visible names of each of our audio channels */
267 vector<string>
268 AudioContent::channel_names () const
269 {
270         vector<string> n;
271
272         int t = 1;
273         BOOST_FOREACH (AudioStreamPtr i, streams ()) {
274                 for (int j = 0; j < i->channels(); ++j) {
275                         n.push_back (String::compose ("%1:%2", t, j + 1));
276                 }
277                 ++t;
278         }
279
280         return n;
281 }
282
283 void
284 AudioContent::add_properties (shared_ptr<const Film> film, list<UserProperty>& p) const
285 {
286         shared_ptr<const AudioStream> stream;
287         if (streams().size() == 1) {
288                 stream = streams().front ();
289         }
290
291         if (stream) {
292                 p.push_back (UserProperty (UserProperty::AUDIO, _("Channels"), stream->channels ()));
293                 p.push_back (UserProperty (UserProperty::AUDIO, _("Content audio sample rate"), stream->frame_rate(), _("Hz")));
294         }
295
296         FrameRateChange const frc (_parent->active_video_frame_rate(film), film->video_frame_rate());
297         ContentTime const c (_parent->full_length(film), frc);
298
299         p.push_back (
300                 UserProperty (UserProperty::LENGTH, _("Full length in video frames at content rate"), c.frames_round(frc.source))
301                 );
302
303         if (stream) {
304                 p.push_back (
305                         UserProperty (
306                                 UserProperty::LENGTH,
307                                 _("Full length in audio samples at content rate"),
308                                 c.frames_round (stream->frame_rate ())
309                                 )
310                         );
311         }
312
313         p.push_back (UserProperty (UserProperty::AUDIO, _("DCP sample rate"), resampled_frame_rate(film), _("Hz")));
314         p.push_back (UserProperty (UserProperty::LENGTH, _("Full length in video frames at DCP rate"), c.frames_round (frc.dcp)));
315
316         if (stream) {
317                 p.push_back (
318                         UserProperty (
319                                 UserProperty::LENGTH,
320                                 _("Full length in audio samples at DCP rate"),
321                                 c.frames_round(resampled_frame_rate(film))
322                                 )
323                         );
324         }
325 }
326
327 void
328 AudioContent::set_streams (vector<AudioStreamPtr> streams)
329 {
330         ChangeSignaller<Content> cc (_parent, AudioContentProperty::STREAMS);
331
332         {
333                 boost::mutex::scoped_lock lm (_mutex);
334                 _streams = streams;
335         }
336 }
337
338 AudioStreamPtr
339 AudioContent::stream () const
340 {
341         boost::mutex::scoped_lock lm (_mutex);
342         DCPOMATIC_ASSERT (_streams.size() == 1);
343         return _streams.front ();
344 }
345
346 void
347 AudioContent::add_stream (AudioStreamPtr stream)
348 {
349         ChangeSignaller<Content> cc (_parent, AudioContentProperty::STREAMS);
350
351         {
352                 boost::mutex::scoped_lock lm (_mutex);
353                 _streams.push_back (stream);
354         }
355 }
356
357 void
358 AudioContent::set_stream (AudioStreamPtr stream)
359 {
360         ChangeSignaller<Content> cc (_parent, AudioContentProperty::STREAMS);
361
362         {
363                 boost::mutex::scoped_lock lm (_mutex);
364                 _streams.clear ();
365                 _streams.push_back (stream);
366         }
367 }
368
369 void
370 AudioContent::take_settings_from (shared_ptr<const AudioContent> c)
371 {
372         set_gain (c->_gain);
373         set_delay (c->_delay);
374
375         size_t i = 0;
376         size_t j = 0;
377
378         while (i < _streams.size() && j < c->_streams.size()) {
379                 _streams[i]->set_mapping (c->_streams[j]->mapping());
380                 ++i;
381                 ++j;
382         }
383 }
384
385 void
386 AudioContent::modify_position (shared_ptr<const Film> film, DCPTime& pos) const
387 {
388         pos = pos.round (film->audio_frame_rate());
389 }
390
391 void
392 AudioContent::modify_trim_start (ContentTime& trim) const
393 {
394         DCPOMATIC_ASSERT (!_streams.empty());
395         /* XXX: we're in trouble if streams have different rates */
396         trim = trim.round (_streams.front()->frame_rate());
397 }