2 Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
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"
33 #include "exceptions.h"
34 #include "frame_rate_change.h"
35 #include "text_content.h"
36 #include <dcp/raw_convert.h>
37 #include <libcxml/cxml.h>
39 #include <libavformat/avformat.h>
40 #include <libavutil/pixdesc.h>
42 #include <libxml++/libxml++.h>
43 #include <boost/foreach.hpp>
55 using boost::shared_ptr;
56 using boost::dynamic_pointer_cast;
57 using boost::optional;
58 using dcp::raw_convert;
59 using namespace dcpomatic;
61 int const FFmpegContentProperty::SUBTITLE_STREAMS = 100;
62 int const FFmpegContentProperty::SUBTITLE_STREAM = 101;
63 int const FFmpegContentProperty::FILTERS = 102;
64 int const FFmpegContentProperty::KDM = 103;
66 FFmpegContent::FFmpegContent (boost::filesystem::path p)
68 #ifdef DCPOMATIC_VARIANT_SWAROOP
77 get_optional_enum (cxml::ConstNodePtr node, string name)
79 optional<int> const v = node->optional_number_child<int>(name);
83 return static_cast<T>(*v);
86 FFmpegContent::FFmpegContent (cxml::ConstNodePtr node, int version, list<string>& notes)
89 video = VideoContent::from_xml (this, node, version);
90 audio = AudioContent::from_xml (this, node, version);
91 text = TextContent::from_xml (this, node, version);
93 list<cxml::NodePtr> c = node->node_children ("SubtitleStream");
94 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
95 _subtitle_streams.push_back (shared_ptr<FFmpegSubtitleStream> (new FFmpegSubtitleStream (*i, version)));
96 if ((*i)->optional_number_child<int> ("Selected")) {
97 _subtitle_stream = _subtitle_streams.back ();
101 c = node->node_children ("AudioStream");
102 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
103 shared_ptr<FFmpegAudioStream> as (new FFmpegAudioStream (*i, version));
104 audio->add_stream (as);
105 if (version < 11 && !(*i)->optional_node_child ("Selected")) {
106 /* This is an old file and this stream is not selected, so un-map it */
107 as->set_mapping (AudioMapping (as->channels (), MAX_DCP_AUDIO_CHANNELS));
111 c = node->node_children ("Filter");
112 for (list<cxml::NodePtr>::iterator i = c.begin(); i != c.end(); ++i) {
113 Filter const * f = Filter::from_id ((*i)->content ());
115 _filters.push_back (f);
117 notes.push_back (String::compose (_("DCP-o-matic no longer supports the `%1' filter, so it has been turned off."), (*i)->content()));
121 optional<ContentTime::Type> const f = node->optional_number_child<ContentTime::Type> ("FirstVideo");
123 _first_video = ContentTime (f.get ());
126 _color_range = get_optional_enum<AVColorRange>(node, "ColorRange");
127 _color_primaries = get_optional_enum<AVColorPrimaries>(node, "ColorPrimaries");
128 _color_trc = get_optional_enum<AVColorTransferCharacteristic>(node, "ColorTransferCharacteristic");
129 _colorspace = get_optional_enum<AVColorSpace>(node, "Colorspace");
130 _bits_per_pixel = node->optional_number_child<int> ("BitsPerPixel");
131 #ifdef DCPOMATIC_VARIANT_SWAROOP
132 _encrypted = node->optional_bool_child("Encrypted").get_value_or(false);
136 FFmpegContent::FFmpegContent (vector<shared_ptr<Content> > c)
139 vector<shared_ptr<Content> >::const_iterator i = c.begin ();
141 bool need_video = false;
142 bool need_audio = false;
143 bool need_text = false;
146 need_video = static_cast<bool> ((*i)->video);
147 need_audio = static_cast<bool> ((*i)->audio);
148 need_text = !(*i)->text.empty();
151 while (i != c.end ()) {
152 if (need_video != static_cast<bool> ((*i)->video)) {
153 throw JoinError (_("Content to be joined must all have or not have video"));
155 if (need_audio != static_cast<bool> ((*i)->audio)) {
156 throw JoinError (_("Content to be joined must all have or not have audio"));
158 if (need_text != !(*i)->text.empty()) {
159 throw JoinError (_("Content to be joined must all have or not have subtitles or captions"));
165 video.reset (new VideoContent (this, c));
168 audio.reset (new AudioContent (this, c));
171 text.push_back (shared_ptr<TextContent> (new TextContent (this, c)));
174 shared_ptr<FFmpegContent> ref = dynamic_pointer_cast<FFmpegContent> (c[0]);
175 DCPOMATIC_ASSERT (ref);
177 for (size_t i = 0; i < c.size(); ++i) {
178 shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (c[i]);
179 if (fc->only_text() && fc->only_text()->use() && *(fc->_subtitle_stream.get()) != *(ref->_subtitle_stream.get())) {
180 throw JoinError (_("Content to be joined must use the same subtitle stream."));
184 /* XXX: should probably check that more of the stuff below is the same in *this and ref */
186 _subtitle_streams = ref->subtitle_streams ();
187 _subtitle_stream = ref->subtitle_stream ();
188 _first_video = ref->_first_video;
189 _filters = ref->_filters;
190 _color_range = ref->_color_range;
191 _color_primaries = ref->_color_primaries;
192 _color_trc = ref->_color_trc;
193 _colorspace = ref->_colorspace;
194 _bits_per_pixel = ref->_bits_per_pixel;
195 #ifdef DCPOMATIC_VARIANT_SWAROOP
196 _encrypted = ref->_encrypted;
201 FFmpegContent::as_xml (xmlpp::Node* node, bool with_paths) const
203 node->add_child("Type")->add_child_text ("FFmpeg");
204 Content::as_xml (node, with_paths);
207 video->as_xml (node);
211 audio->as_xml (node);
213 BOOST_FOREACH (AudioStreamPtr i, audio->streams ()) {
214 shared_ptr<FFmpegAudioStream> f = dynamic_pointer_cast<FFmpegAudioStream> (i);
215 DCPOMATIC_ASSERT (f);
216 f->as_xml (node->add_child("AudioStream"));
221 only_text()->as_xml (node);
224 boost::mutex::scoped_lock lm (_mutex);
226 for (vector<shared_ptr<FFmpegSubtitleStream> >::const_iterator i = _subtitle_streams.begin(); i != _subtitle_streams.end(); ++i) {
227 xmlpp::Node* t = node->add_child("SubtitleStream");
228 if (_subtitle_stream && *i == _subtitle_stream) {
229 t->add_child("Selected")->add_child_text("1");
234 for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
235 node->add_child("Filter")->add_child_text ((*i)->id ());
239 node->add_child("FirstVideo")->add_child_text (raw_convert<string> (_first_video.get().get()));
243 node->add_child("ColorRange")->add_child_text (raw_convert<string> (static_cast<int> (*_color_range)));
245 if (_color_primaries) {
246 node->add_child("ColorPrimaries")->add_child_text (raw_convert<string> (static_cast<int> (*_color_primaries)));
249 node->add_child("ColorTransferCharacteristic")->add_child_text (raw_convert<string> (static_cast<int> (*_color_trc)));
252 node->add_child("Colorspace")->add_child_text (raw_convert<string> (static_cast<int> (*_colorspace)));
254 if (_bits_per_pixel) {
255 node->add_child("BitsPerPixel")->add_child_text (raw_convert<string> (*_bits_per_pixel));
257 #ifdef DCPOMATIC_VARIANT_SWAROOP
259 node->add_child("Encypted")->add_child_text ("1");
265 FFmpegContent::examine (shared_ptr<const Film> film, shared_ptr<Job> job)
267 ChangeSignaller<Content> cc1 (this, FFmpegContentProperty::SUBTITLE_STREAMS);
268 ChangeSignaller<Content> cc2 (this, FFmpegContentProperty::SUBTITLE_STREAM);
271 job->set_progress_unknown ();
274 Content::examine (film, job);
276 shared_ptr<FFmpegExaminer> examiner (new FFmpegExaminer (shared_from_this (), job));
278 if (examiner->has_video ()) {
279 video.reset (new VideoContent (this));
280 video->take_from_examiner (examiner);
283 boost::filesystem::path first_path = path (0);
286 boost::mutex::scoped_lock lm (_mutex);
288 if (examiner->has_video ()) {
289 _first_video = examiner->first_video ();
290 _color_range = examiner->color_range ();
291 _color_primaries = examiner->color_primaries ();
292 _color_trc = examiner->color_trc ();
293 _colorspace = examiner->colorspace ();
294 _bits_per_pixel = examiner->bits_per_pixel ();
296 if (examiner->rotation()) {
297 double rot = *examiner->rotation ();
298 if (fabs (rot - 180) < 1.0) {
299 _filters.push_back (Filter::from_id ("vflip"));
300 _filters.push_back (Filter::from_id ("hflip"));
301 } else if (fabs (rot - 90) < 1.0) {
302 _filters.push_back (Filter::from_id ("90clock"));
303 } else if (fabs (rot - 270) < 1.0) {
304 _filters.push_back (Filter::from_id ("90anticlock"));
309 if (!examiner->audio_streams().empty ()) {
310 audio.reset (new AudioContent (this));
312 BOOST_FOREACH (shared_ptr<FFmpegAudioStream> i, examiner->audio_streams ()) {
313 audio->add_stream (i);
316 AudioStreamPtr as = audio->streams().front();
317 AudioMapping m = as->mapping ();
318 m.make_default (film ? film->audio_processor() : 0, first_path);
322 _subtitle_streams = examiner->subtitle_streams ();
323 if (!_subtitle_streams.empty ()) {
325 text.push_back (shared_ptr<TextContent> (new TextContent (this, TEXT_OPEN_SUBTITLE, TEXT_UNKNOWN)));
326 _subtitle_stream = _subtitle_streams.front ();
329 #ifdef DCPOMATIC_VARIANT_SWAROOP
330 _encrypted = first_path.extension() == ".ecinema";
334 if (examiner->has_video ()) {
335 set_default_colour_conversion ();
338 #ifdef DCPOMATIC_VARIANT_SWAROOP
339 _id = examiner->id ();
344 FFmpegContent::summary () const
346 if (video && audio) {
347 return String::compose (_("%1 [movie]"), path_summary ());
349 return String::compose (_("%1 [video]"), path_summary ());
351 return String::compose (_("%1 [audio]"), path_summary ());
354 return path_summary ();
358 FFmpegContent::technical_summary () const
361 BOOST_FOREACH (shared_ptr<FFmpegAudioStream> i, ffmpeg_audio_streams ()) {
362 as += i->technical_summary () + " " ;
370 if (_subtitle_stream) {
371 ss = _subtitle_stream->technical_summary ();
374 string filt = Filter::ffmpeg_string (_filters);
376 string s = Content::technical_summary ();
379 s += " - " + video->technical_summary ();
383 s += " - " + audio->technical_summary ();
386 return s + String::compose (
387 "ffmpeg: audio %1 subtitle %2 filters %3", as, ss, filt
392 FFmpegContent::set_subtitle_stream (shared_ptr<FFmpegSubtitleStream> s)
394 ChangeSignaller<Content> cc (this, FFmpegContentProperty::SUBTITLE_STREAM);
397 boost::mutex::scoped_lock lm (_mutex);
398 _subtitle_stream = s;
403 operator== (FFmpegStream const & a, FFmpegStream const & b)
405 return a._id == b._id;
409 operator!= (FFmpegStream const & a, FFmpegStream const & b)
411 return a._id != b._id;
415 FFmpegContent::full_length (shared_ptr<const Film> film) const
417 FrameRateChange const frc (film, shared_from_this());
419 return DCPTime::from_frames (llrint (video->length_after_3d_combine() * frc.factor()), film->video_frame_rate());
424 BOOST_FOREACH (AudioStreamPtr i, audio->streams()) {
425 longest = max (longest, DCPTime::from_frames(llrint(i->length() / frc.speed_up), i->frame_rate()));
430 /* XXX: subtitle content? */
436 FFmpegContent::approximate_length () const
439 return DCPTime::from_frames (video->length_after_3d_combine(), 24);
442 DCPOMATIC_ASSERT (audio);
445 BOOST_FOREACH (AudioStreamPtr i, audio->streams ()) {
446 longest = max (longest, Frame(llrint(i->length())));
449 return DCPTime::from_frames (longest, 24);
453 FFmpegContent::set_filters (vector<Filter const *> const & filters)
455 ChangeSignaller<Content> cc (this, FFmpegContentProperty::FILTERS);
458 boost::mutex::scoped_lock lm (_mutex);
464 FFmpegContent::identifier () const
466 string s = Content::identifier();
469 s += "_" + video->identifier();
472 if (only_text() && only_text()->use() && only_text()->burn()) {
473 s += "_" + only_text()->identifier();
476 boost::mutex::scoped_lock lm (_mutex);
478 if (_subtitle_stream) {
479 s += "_" + _subtitle_stream->identifier ();
482 for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
483 s += "_" + (*i)->id ();
490 FFmpegContent::set_default_colour_conversion ()
492 DCPOMATIC_ASSERT (video);
494 dcp::Size const s = video->size ();
496 boost::mutex::scoped_lock lm (_mutex);
498 switch (_colorspace.get_value_or(AVCOL_SPC_UNSPECIFIED)) {
500 video->set_colour_conversion (PresetColourConversion::from_id ("srgb").conversion);
502 case AVCOL_SPC_BT709:
503 video->set_colour_conversion (PresetColourConversion::from_id ("rec709").conversion);
505 case AVCOL_SPC_BT470BG:
506 case AVCOL_SPC_SMPTE170M:
507 case AVCOL_SPC_SMPTE240M:
508 video->set_colour_conversion (PresetColourConversion::from_id ("rec601").conversion);
510 case AVCOL_SPC_BT2020_CL:
511 case AVCOL_SPC_BT2020_NCL:
512 video->set_colour_conversion (PresetColourConversion::from_id ("rec2020").conversion);
515 if (s.width < 1080) {
516 video->set_colour_conversion (PresetColourConversion::from_id ("rec601").conversion);
518 video->set_colour_conversion (PresetColourConversion::from_id ("rec709").conversion);
525 FFmpegContent::add_properties (shared_ptr<const Film> film, list<UserProperty>& p) const
527 Content::add_properties (film, p);
530 video->add_properties (p);
532 if (_bits_per_pixel) {
533 int const sub = 219 * pow (2, _bits_per_pixel.get() - 8);
534 int const total = pow (2, _bits_per_pixel.get());
536 switch (_color_range.get_value_or(AVCOL_RANGE_UNSPECIFIED)) {
537 case AVCOL_RANGE_UNSPECIFIED:
538 /// TRANSLATORS: this means that the range of pixel values used in this
539 /// file is unknown (not specified in the file).
540 p.push_back (UserProperty (UserProperty::VIDEO, _("Colour range"), _("Unspecified")));
542 case AVCOL_RANGE_MPEG:
543 /// TRANSLATORS: this means that the range of pixel values used in this
544 /// file is limited, so that not all possible values are valid.
547 UserProperty::VIDEO, _("Colour range"), String::compose (_("Limited (%1-%2)"), (total - sub) / 2, (total + sub) / 2)
551 case AVCOL_RANGE_JPEG:
552 /// TRANSLATORS: this means that the range of pixel values used in this
553 /// file is full, so that all possible pixel values are valid.
554 p.push_back (UserProperty (UserProperty::VIDEO, _("Colour range"), String::compose (_("Full (0-%1)"), total)));
557 DCPOMATIC_ASSERT (false);
560 switch (_color_range.get_value_or(AVCOL_RANGE_UNSPECIFIED)) {
561 case AVCOL_RANGE_UNSPECIFIED:
562 /// TRANSLATORS: this means that the range of pixel values used in this
563 /// file is unknown (not specified in the file).
564 p.push_back (UserProperty (UserProperty::VIDEO, _("Colour range"), _("Unspecified")));
566 case AVCOL_RANGE_MPEG:
567 /// TRANSLATORS: this means that the range of pixel values used in this
568 /// file is limited, so that not all possible values are valid.
569 p.push_back (UserProperty (UserProperty::VIDEO, _("Colour range"), _("Limited")));
571 case AVCOL_RANGE_JPEG:
572 /// TRANSLATORS: this means that the range of pixel values used in this
573 /// file is full, so that all possible pixel values are valid.
574 p.push_back (UserProperty (UserProperty::VIDEO, _("Colour range"), _("Full")));
577 DCPOMATIC_ASSERT (false);
581 char const * primaries[] = {
588 _("SMPTE 170M (BT601)"),
592 _("SMPTE ST 428-1 (CIE 1931 XYZ)"),
593 _("SMPTE ST 431-2 (2011)"),
594 _("SMPTE ST 432-1 D65 (2010)"), // 12
607 DCPOMATIC_ASSERT (AVCOL_PRI_NB <= 23);
608 p.push_back (UserProperty (UserProperty::VIDEO, _("Colour primaries"), primaries[_color_primaries.get_value_or(AVCOL_PRI_UNSPECIFIED)]));
610 char const * transfers[] = {
615 _("Gamma 22 (BT470M)"),
616 _("Gamma 28 (BT470BG)"),
617 _("SMPTE 170M (BT601)"),
620 _("Logarithmic (100:1 range)"),
621 _("Logarithmic (316:1 range)"),
623 _("BT1361 extended colour gamut"),
624 _("IEC61966-2-1 (sRGB or sYCC)"),
625 _("BT2020 for a 10-bit system"),
626 _("BT2020 for a 12-bit system"),
627 _("SMPTE ST 2084 for 10, 12, 14 and 16 bit systems"),
629 _("ARIB STD-B67 ('Hybrid log-gamma')")
632 DCPOMATIC_ASSERT (AVCOL_TRC_NB <= 19);
633 p.push_back (UserProperty (UserProperty::VIDEO, _("Colour transfer characteristic"), transfers[_color_trc.get_value_or(AVCOL_TRC_UNSPECIFIED)]));
635 char const * spaces[] = {
636 _("RGB / sRGB (IEC61966-2-1)"),
641 _("BT470BG (BT601-6)"),
642 _("SMPTE 170M (BT601-6)"),
645 _("BT2020 non-constant luminance"),
646 _("BT2020 constant luminance"),
647 _("SMPTE 2085, Y'D'zD'x"),
648 _("Chroma-derived non-constant luminance"),
649 _("Chroma-derived constant luminance"),
653 DCPOMATIC_ASSERT (AVCOL_SPC_NB == 15);
654 p.push_back (UserProperty (UserProperty::VIDEO, _("Colourspace"), spaces[_colorspace.get_value_or(AVCOL_SPC_UNSPECIFIED)]));
656 if (_bits_per_pixel) {
657 p.push_back (UserProperty (UserProperty::VIDEO, _("Bits per pixel"), *_bits_per_pixel));
662 audio->add_properties (film, p);
666 /** Our subtitle streams have colour maps, which can be changed, but
667 * they have no way of signalling that change. As a hack, we have this
668 * method which callers can use when they've modified one of our subtitle
672 FFmpegContent::signal_subtitle_stream_changed ()
674 /* XXX: this is too late; really it should be before the change */
675 ChangeSignaller<Content> cc (this, FFmpegContentProperty::SUBTITLE_STREAM);
678 vector<shared_ptr<FFmpegAudioStream> >
679 FFmpegContent::ffmpeg_audio_streams () const
681 vector<shared_ptr<FFmpegAudioStream> > fa;
684 BOOST_FOREACH (AudioStreamPtr i, audio->streams()) {
685 fa.push_back (dynamic_pointer_cast<FFmpegAudioStream> (i));
693 FFmpegContent::take_settings_from (shared_ptr<const Content> c)
695 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (c);
700 Content::take_settings_from (c);
701 _filters = fc->_filters;
704 #ifdef DCPOMATIC_VARIANT_SWAROOP
706 FFmpegContent::add_kdm (EncryptedECinemaKDM kdm)
708 ChangeSignaller<Content> cc (this, FFmpegContentProperty::KDM);
709 boost::mutex::scoped_lock lm (_mutex);