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