Fix crash with multiple audio streams.
[dcpomatic.git] / src / lib / ffmpeg_content.cc
1 /*
2     Copyright (C) 2013-2016 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 "ffmpeg_content.h"
22 #include "video_content.h"
23 #include "audio_content.h"
24 #include "ffmpeg_examiner.h"
25 #include "ffmpeg_subtitle_stream.h"
26 #include "ffmpeg_audio_stream.h"
27 #include "compose.hpp"
28 #include "job.h"
29 #include "util.h"
30 #include "filter.h"
31 #include "film.h"
32 #include "log.h"
33 #include "exceptions.h"
34 #include "frame_rate_change.h"
35 #include "safe_stringstream.h"
36 #include "raw_convert.h"
37 #include "subtitle_content.h"
38 #include <libcxml/cxml.h>
39 extern "C" {
40 #include <libavformat/avformat.h>
41 #include <libavutil/pixdesc.h>
42 }
43 #include <libxml++/libxml++.h>
44 #include <boost/foreach.hpp>
45 #include <iostream>
46
47 #include "i18n.h"
48
49 #define LOG_GENERAL(...) film->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL);
50
51 using std::string;
52 using std::vector;
53 using std::list;
54 using std::cout;
55 using std::pair;
56 using std::make_pair;
57 using std::max;
58 using boost::shared_ptr;
59 using boost::dynamic_pointer_cast;
60 using boost::optional;
61
62 int const FFmpegContentProperty::SUBTITLE_STREAMS = 100;
63 int const FFmpegContentProperty::SUBTITLE_STREAM = 101;
64 int const FFmpegContentProperty::FILTERS = 102;
65
66 FFmpegContent::FFmpegContent (shared_ptr<const Film> film, boost::filesystem::path p)
67         : Content (film, p)
68 {
69
70 }
71
72 FFmpegContent::FFmpegContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version, list<string>& notes)
73         : Content (film, node)
74 {
75         video = VideoContent::from_xml (this, node, version);
76         audio = AudioContent::from_xml (this, node);
77         subtitle = SubtitleContent::from_xml (this, node, version);
78
79         list<cxml::NodePtr> c = node->node_children ("SubtitleStream");
80         for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
81                 _subtitle_streams.push_back (shared_ptr<FFmpegSubtitleStream> (new FFmpegSubtitleStream (*i, version)));
82                 if ((*i)->optional_number_child<int> ("Selected")) {
83                         _subtitle_stream = _subtitle_streams.back ();
84                 }
85         }
86
87         c = node->node_children ("AudioStream");
88         for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
89                 shared_ptr<FFmpegAudioStream> as (new FFmpegAudioStream (*i, version));
90                 audio->add_stream (as);
91                 if (version < 11 && !(*i)->optional_node_child ("Selected")) {
92                         /* This is an old file and this stream is not selected, so un-map it */
93                         as->set_mapping (AudioMapping (as->channels (), MAX_DCP_AUDIO_CHANNELS));
94                 }
95         }
96
97         c = node->node_children ("Filter");
98         for (list<cxml::NodePtr>::iterator i = c.begin(); i != c.end(); ++i) {
99                 Filter const * f = Filter::from_id ((*i)->content ());
100                 if (f) {
101                         _filters.push_back (f);
102                 } else {
103                         notes.push_back (String::compose (_("DCP-o-matic no longer supports the `%1' filter, so it has been turned off."), (*i)->content()));
104                 }
105         }
106
107         optional<ContentTime::Type> const f = node->optional_number_child<ContentTime::Type> ("FirstVideo");
108         if (f) {
109                 _first_video = ContentTime (f.get ());
110         }
111
112         _color_range = static_cast<AVColorRange> (node->optional_number_child<int>("ColorRange").get_value_or (AVCOL_RANGE_UNSPECIFIED));
113         _color_primaries = static_cast<AVColorPrimaries> (node->optional_number_child<int>("ColorPrimaries").get_value_or (AVCOL_PRI_UNSPECIFIED));
114         _color_trc = static_cast<AVColorTransferCharacteristic> (
115                 node->optional_number_child<int>("ColorTransferCharacteristic").get_value_or (AVCOL_TRC_UNSPECIFIED)
116                 );
117         _colorspace = static_cast<AVColorSpace> (node->optional_number_child<int>("Colorspace").get_value_or (AVCOL_SPC_UNSPECIFIED));
118         _bits_per_pixel = node->optional_number_child<int> ("BitsPerPixel");
119
120 }
121
122 FFmpegContent::FFmpegContent (shared_ptr<const Film> film, vector<boost::shared_ptr<Content> > c)
123         : Content (film, c)
124 {
125         video.reset (new VideoContent (this, c));
126         audio.reset (new AudioContent (this, c));
127         subtitle.reset (new SubtitleContent (this, c));
128
129         shared_ptr<FFmpegContent> ref = dynamic_pointer_cast<FFmpegContent> (c[0]);
130         DCPOMATIC_ASSERT (ref);
131
132         for (size_t i = 0; i < c.size(); ++i) {
133                 shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (c[i]);
134                 if (fc->subtitle->use() && *(fc->_subtitle_stream.get()) != *(ref->_subtitle_stream.get())) {
135                         throw JoinError (_("Content to be joined must use the same subtitle stream."));
136                 }
137         }
138
139         /* XXX: should probably check that more of the stuff below is the same in *this and ref */
140
141         _subtitle_streams = ref->subtitle_streams ();
142         _subtitle_stream = ref->subtitle_stream ();
143         _first_video = ref->_first_video;
144         _filters = ref->_filters;
145         _color_range = ref->_color_range;
146         _color_primaries = ref->_color_primaries;
147         _color_trc = ref->_color_trc;
148         _colorspace = ref->_colorspace;
149         _bits_per_pixel = ref->_bits_per_pixel;
150 }
151
152 void
153 FFmpegContent::as_xml (xmlpp::Node* node) const
154 {
155         node->add_child("Type")->add_child_text ("FFmpeg");
156         Content::as_xml (node);
157
158         if (video) {
159                 video->as_xml (node);
160         }
161
162         if (audio) {
163                 audio->as_xml (node);
164
165                 BOOST_FOREACH (AudioStreamPtr i, audio->streams ()) {
166                         shared_ptr<FFmpegAudioStream> f = dynamic_pointer_cast<FFmpegAudioStream> (i);
167                         DCPOMATIC_ASSERT (f);
168                         f->as_xml (node->add_child("AudioStream"));
169                 }
170         }
171
172         if (subtitle) {
173                 subtitle->as_xml (node);
174         }
175
176         boost::mutex::scoped_lock lm (_mutex);
177
178         for (vector<shared_ptr<FFmpegSubtitleStream> >::const_iterator i = _subtitle_streams.begin(); i != _subtitle_streams.end(); ++i) {
179                 xmlpp::Node* t = node->add_child("SubtitleStream");
180                 if (_subtitle_stream && *i == _subtitle_stream) {
181                         t->add_child("Selected")->add_child_text("1");
182                 }
183                 (*i)->as_xml (t);
184         }
185
186         for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
187                 node->add_child("Filter")->add_child_text ((*i)->id ());
188         }
189
190         if (_first_video) {
191                 node->add_child("FirstVideo")->add_child_text (raw_convert<string> (_first_video.get().get()));
192         }
193
194         node->add_child("ColorRange")->add_child_text (raw_convert<string> (_color_range));
195         node->add_child("ColorPrimaries")->add_child_text (raw_convert<string> (_color_primaries));
196         node->add_child("ColorTransferCharacteristic")->add_child_text (raw_convert<string> (_color_trc));
197         node->add_child("Colorspace")->add_child_text (raw_convert<string> (_colorspace));
198         if (_bits_per_pixel) {
199                 node->add_child("BitsPerPixel")->add_child_text (raw_convert<string> (_bits_per_pixel.get ()));
200         }
201 }
202
203 void
204 FFmpegContent::examine (shared_ptr<Job> job)
205 {
206         job->set_progress_unknown ();
207
208         Content::examine (job);
209
210         shared_ptr<FFmpegExaminer> examiner (new FFmpegExaminer (shared_from_this (), job));
211
212         if (examiner->has_video ()) {
213                 video.reset (new VideoContent (this));
214                 video->take_from_examiner (examiner);
215                 set_default_colour_conversion ();
216         }
217
218         {
219                 boost::mutex::scoped_lock lm (_mutex);
220
221                 if (examiner->has_video ()) {
222                         _first_video = examiner->first_video ();
223                         _color_range = examiner->color_range ();
224                         _color_primaries = examiner->color_primaries ();
225                         _color_trc = examiner->color_trc ();
226                         _colorspace = examiner->colorspace ();
227                         _bits_per_pixel = examiner->bits_per_pixel ();
228                 }
229
230                 if (!examiner->audio_streams().empty ()) {
231                         audio.reset (new AudioContent (this));
232
233                         BOOST_FOREACH (shared_ptr<FFmpegAudioStream> i, examiner->audio_streams ()) {
234                                 audio->add_stream (i);
235                         }
236
237                         AudioStreamPtr as = audio->streams().front();
238                         AudioMapping m = as->mapping ();
239                         film()->make_audio_mapping_default (m);
240                         as->set_mapping (m);
241                 }
242
243                 _subtitle_streams = examiner->subtitle_streams ();
244                 if (!_subtitle_streams.empty ()) {
245                         subtitle.reset (new SubtitleContent (this));
246                         _subtitle_stream = _subtitle_streams.front ();
247                 }
248
249         }
250
251         signal_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
252         signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
253 }
254
255 string
256 FFmpegContent::summary () const
257 {
258         if (video && audio) {
259                 return String::compose (_("%1 [movie]"), path_summary ());
260         } else if (video) {
261                 return String::compose (_("%1 [video]"), path_summary ());
262         } else if (audio) {
263                 return String::compose (_("%1 [audio]"), path_summary ());
264         }
265
266         return path_summary ();
267 }
268
269 string
270 FFmpegContent::technical_summary () const
271 {
272         string as = "";
273         BOOST_FOREACH (shared_ptr<FFmpegAudioStream> i, ffmpeg_audio_streams ()) {
274                 as += i->technical_summary () + " " ;
275         }
276
277         if (as.empty ()) {
278                 as = "none";
279         }
280
281         string ss = "none";
282         if (_subtitle_stream) {
283                 ss = _subtitle_stream->technical_summary ();
284         }
285
286         string filt = Filter::ffmpeg_string (_filters);
287
288         string s = Content::technical_summary ();
289
290         if (video) {
291                 s += " - " + video->technical_summary ();
292         }
293
294         if (audio) {
295                 s += " - " + audio->technical_summary ();
296         }
297
298         return s + String::compose (
299                 "ffmpeg: audio %1 subtitle %2 filters %3", as, ss, filt
300                 );
301 }
302
303 void
304 FFmpegContent::set_subtitle_stream (shared_ptr<FFmpegSubtitleStream> s)
305 {
306         {
307                 boost::mutex::scoped_lock lm (_mutex);
308                 _subtitle_stream = s;
309         }
310
311         signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
312 }
313
314 bool
315 operator== (FFmpegStream const & a, FFmpegStream const & b)
316 {
317         return a._id == b._id;
318 }
319
320 bool
321 operator!= (FFmpegStream const & a, FFmpegStream const & b)
322 {
323         return a._id != b._id;
324 }
325
326 DCPTime
327 FFmpegContent::full_length () const
328 {
329         FrameRateChange const frc (active_video_frame_rate (), film()->video_frame_rate ());
330         if (video) {
331                 return DCPTime::from_frames (llrint (video->length_after_3d_combine() * frc.factor()), film()->video_frame_rate());
332         }
333
334         DCPOMATIC_ASSERT (audio);
335
336         DCPTime longest;
337         BOOST_FOREACH (AudioStreamPtr i, audio->streams ()) {
338                 longest = max (longest, DCPTime::from_frames (llrint (i->length() / frc.speed_up), i->frame_rate()));
339         }
340
341         return longest;
342 }
343
344 void
345 FFmpegContent::set_filters (vector<Filter const *> const & filters)
346 {
347         {
348                 boost::mutex::scoped_lock lm (_mutex);
349                 _filters = filters;
350         }
351
352         signal_changed (FFmpegContentProperty::FILTERS);
353 }
354
355 string
356 FFmpegContent::identifier () const
357 {
358         SafeStringStream s;
359
360         s << Content::identifier();
361
362         if (video) {
363                 s << "_" << video->identifier();
364         }
365
366         if (subtitle) {
367                 s << "_" << subtitle->identifier();
368         }
369
370         boost::mutex::scoped_lock lm (_mutex);
371
372         if (_subtitle_stream) {
373                 s << "_" << _subtitle_stream->identifier ();
374         }
375
376         for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
377                 s << "_" << (*i)->id ();
378         }
379
380         return s.str ();
381 }
382
383 list<ContentTimePeriod>
384 FFmpegContent::image_subtitles_during (ContentTimePeriod period, bool starting) const
385 {
386         shared_ptr<FFmpegSubtitleStream> stream = subtitle_stream ();
387         if (!stream) {
388                 return list<ContentTimePeriod> ();
389         }
390
391         return stream->image_subtitles_during (period, starting);
392 }
393
394 list<ContentTimePeriod>
395 FFmpegContent::text_subtitles_during (ContentTimePeriod period, bool starting) const
396 {
397         shared_ptr<FFmpegSubtitleStream> stream = subtitle_stream ();
398         if (!stream) {
399                 return list<ContentTimePeriod> ();
400         }
401
402         return stream->text_subtitles_during (period, starting);
403 }
404
405 void
406 FFmpegContent::set_default_colour_conversion ()
407 {
408         DCPOMATIC_ASSERT (video);
409
410         dcp::Size const s = video->size ();
411
412         boost::mutex::scoped_lock lm (_mutex);
413
414         if (s.width < 1080) {
415                 video->set_colour_conversion (PresetColourConversion::from_id ("rec601").conversion);
416         } else {
417                 video->set_colour_conversion (PresetColourConversion::from_id ("rec709").conversion);
418         }
419 }
420
421 void
422 FFmpegContent::add_properties (list<UserProperty>& p) const
423 {
424         Content::add_properties (p);
425
426         if (video) {
427                 video->add_properties (p);
428
429                 if (_bits_per_pixel) {
430                         int const sub = 219 * pow (2, _bits_per_pixel.get() - 8);
431                         int const total = pow (2, _bits_per_pixel.get());
432
433                         switch (_color_range) {
434                         case AVCOL_RANGE_UNSPECIFIED:
435                                 /// TRANSLATORS: this means that the range of pixel values used in this
436                                 /// file is unknown (not specified in the file).
437                                 p.push_back (UserProperty (_("Video"), _("Colour range"), _("Unspecified")));
438                                 break;
439                         case AVCOL_RANGE_MPEG:
440                                 /// TRANSLATORS: this means that the range of pixel values used in this
441                                 /// file is limited, so that not all possible values are valid.
442                                 p.push_back (
443                                         UserProperty (
444                                                 _("Video"), _("Colour range"), String::compose (_("Limited (%1-%2)"), (total - sub) / 2, (total + sub) / 2)
445                                                 )
446                                         );
447                                 break;
448                         case AVCOL_RANGE_JPEG:
449                                 /// TRANSLATORS: this means that the range of pixel values used in this
450                                 /// file is full, so that all possible pixel values are valid.
451                                 p.push_back (UserProperty (_("Video"), _("Colour range"), String::compose (_("Full (0-%1)"), total)));
452                                 break;
453                         default:
454                                 DCPOMATIC_ASSERT (false);
455                         }
456                 } else {
457                         switch (_color_range) {
458                         case AVCOL_RANGE_UNSPECIFIED:
459                                 /// TRANSLATORS: this means that the range of pixel values used in this
460                                 /// file is unknown (not specified in the file).
461                                 p.push_back (UserProperty (_("Video"), _("Colour range"), _("Unspecified")));
462                                 break;
463                         case AVCOL_RANGE_MPEG:
464                                 /// TRANSLATORS: this means that the range of pixel values used in this
465                                 /// file is limited, so that not all possible values are valid.
466                                 p.push_back (UserProperty (_("Video"), _("Colour range"), _("Limited")));
467                                 break;
468                         case AVCOL_RANGE_JPEG:
469                                 /// TRANSLATORS: this means that the range of pixel values used in this
470                                 /// file is full, so that all possible pixel values are valid.
471                                 p.push_back (UserProperty (_("Video"), _("Colour range"), _("Full")));
472                                 break;
473                         default:
474                                 DCPOMATIC_ASSERT (false);
475                         }
476                 }
477
478                 char const * primaries[] = {
479                         _("Unspecified"),
480                         _("BT709"),
481                         _("Unspecified"),
482                         _("Unspecified"),
483                         _("BT470M"),
484                         _("BT470BG"),
485                         _("SMPTE 170M (BT601)"),
486                         _("SMPTE 240M"),
487                         _("Film"),
488                         _("BT2020"),
489                         _("SMPTE ST 428-1 (CIE 1931 XYZ)")
490                 };
491
492                 DCPOMATIC_ASSERT (AVCOL_PRI_NB <= 11);
493                 p.push_back (UserProperty (_("Video"), _("Colour primaries"), primaries[_color_primaries]));
494
495                 char const * transfers[] = {
496                         _("Unspecified"),
497                         _("BT709"),
498                         _("Unspecified"),
499                         _("Unspecified"),
500                         _("Gamma 22 (BT470M)"),
501                         _("Gamma 28 (BT470BG)"),
502                         _("SMPTE 170M (BT601)"),
503                         _("SMPTE 240M"),
504                         _("Linear"),
505                         _("Logarithmic (100:1 range)"),
506                         _("Logarithmic (316:1 range)"),
507                         _("IEC61966-2-4"),
508                         _("BT1361 extended colour gamut"),
509                         _("IEC61966-2-1 (sRGB or sYCC)"),
510                         _("BT2020 for a 10-bit system"),
511                         _("BT2020 for a 12-bit system"),
512                         _("SMPTE ST 2084 for 10, 12, 14 and 16 bit systems"),
513                         _("SMPTE ST 428-1")
514                 };
515
516                 DCPOMATIC_ASSERT (AVCOL_TRC_NB <= 18);
517                 p.push_back (UserProperty (_("Video"), _("Colour transfer characteristic"), transfers[_color_trc]));
518
519                 char const * spaces[] = {
520                         _("RGB / sRGB (IEC61966-2-1)"),
521                         _("BT709"),
522                         _("Unspecified"),
523                         _("Unspecified"),
524                         _("FCC"),
525                         _("BT470BG (BT601-6)"),
526                         _("SMPTE 170M (BT601-6)"),
527                         _("SMPTE 240M"),
528                         _("YCOCG"),
529                         _("BT2020 non-constant luminance"),
530                         _("BT2020 constant luminance"),
531                 };
532
533                 DCPOMATIC_ASSERT (AVCOL_SPC_NB == 11);
534                 p.push_back (UserProperty (_("Video"), _("Colourspace"), spaces[_colorspace]));
535
536                 if (_bits_per_pixel) {
537                         p.push_back (UserProperty (_("Video"), _("Bits per pixel"), raw_convert<string> (_bits_per_pixel.get ())));
538                 }
539         }
540
541         if (audio) {
542                 audio->add_properties (p);
543         }
544 }
545
546 /** Our subtitle streams have colour maps, which can be changed, but
547  *  they have no way of signalling that change.  As a hack, we have this
548  *  method which callers can use when they've modified one of our subtitle
549  *  streams.
550  */
551 void
552 FFmpegContent::signal_subtitle_stream_changed ()
553 {
554         signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
555 }
556
557 vector<shared_ptr<FFmpegAudioStream> >
558 FFmpegContent::ffmpeg_audio_streams () const
559 {
560         vector<shared_ptr<FFmpegAudioStream> > fa;
561
562         if (audio) {
563                 BOOST_FOREACH (AudioStreamPtr i, audio->streams()) {
564                         fa.push_back (dynamic_pointer_cast<FFmpegAudioStream> (i));
565                 }
566         }
567
568         return fa;
569 }