2 Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "ffmpeg_content.h"
21 #include "video_content.h"
22 #include "ffmpeg_examiner.h"
23 #include "ffmpeg_subtitle_stream.h"
24 #include "ffmpeg_audio_stream.h"
25 #include "compose.hpp"
31 #include "exceptions.h"
32 #include "frame_rate_change.h"
33 #include "safe_stringstream.h"
34 #include "raw_convert.h"
35 #include <libcxml/cxml.h>
37 #include <libavformat/avformat.h>
38 #include <libavutil/pixdesc.h>
40 #include <libxml++/libxml++.h>
41 #include <boost/foreach.hpp>
46 #define LOG_GENERAL(...) film->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL);
54 using boost::shared_ptr;
55 using boost::dynamic_pointer_cast;
56 using boost::optional;
58 int const FFmpegContentProperty::SUBTITLE_STREAMS = 100;
59 int const FFmpegContentProperty::SUBTITLE_STREAM = 101;
60 int const FFmpegContentProperty::FILTERS = 102;
62 FFmpegContent::FFmpegContent (shared_ptr<const Film> film, boost::filesystem::path p)
64 , AudioContent (film, p)
65 , SubtitleContent (film, p)
66 , video (new VideoContent (film))
68 set_default_colour_conversion ();
71 FFmpegContent::FFmpegContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version, list<string>& notes)
72 : Content (film, node)
73 , AudioContent (film, node)
74 , SubtitleContent (film, node, version)
75 , video (new VideoContent (film, node, version))
77 list<cxml::NodePtr> c = node->node_children ("SubtitleStream");
78 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
79 _subtitle_streams.push_back (shared_ptr<FFmpegSubtitleStream> (new FFmpegSubtitleStream (*i, version)));
80 if ((*i)->optional_number_child<int> ("Selected")) {
81 _subtitle_stream = _subtitle_streams.back ();
85 c = node->node_children ("AudioStream");
86 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
87 _audio_streams.push_back (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream (*i, version)));
88 if (version < 11 && !(*i)->optional_node_child ("Selected")) {
89 /* This is an old file and this stream is not selected, so un-map it */
90 _audio_streams.back()->set_mapping (AudioMapping (_audio_streams.back()->channels (), MAX_DCP_AUDIO_CHANNELS));
94 c = node->node_children ("Filter");
95 for (list<cxml::NodePtr>::iterator i = c.begin(); i != c.end(); ++i) {
96 Filter const * f = Filter::from_id ((*i)->content ());
98 _filters.push_back (f);
100 notes.push_back (String::compose (_("DCP-o-matic no longer supports the `%1' filter, so it has been turned off."), (*i)->content()));
104 optional<ContentTime::Type> const f = node->optional_number_child<ContentTime::Type> ("FirstVideo");
106 _first_video = ContentTime (f.get ());
109 _color_range = static_cast<AVColorRange> (node->optional_number_child<int>("ColorRange").get_value_or (AVCOL_RANGE_UNSPECIFIED));
110 _color_primaries = static_cast<AVColorPrimaries> (node->optional_number_child<int>("ColorPrimaries").get_value_or (AVCOL_PRI_UNSPECIFIED));
111 _color_trc = static_cast<AVColorTransferCharacteristic> (
112 node->optional_number_child<int>("ColorTransferCharacteristic").get_value_or (AVCOL_TRC_UNSPECIFIED)
114 _colorspace = static_cast<AVColorSpace> (node->optional_number_child<int>("Colorspace").get_value_or (AVCOL_SPC_UNSPECIFIED));
115 _bits_per_pixel = node->optional_number_child<int> ("BitsPerPixel");
119 FFmpegContent::FFmpegContent (shared_ptr<const Film> film, vector<boost::shared_ptr<Content> > c)
121 , AudioContent (film, c)
122 , SubtitleContent (film, c)
123 , video (new VideoContent (film, c))
125 shared_ptr<FFmpegContent> ref = dynamic_pointer_cast<FFmpegContent> (c[0]);
126 DCPOMATIC_ASSERT (ref);
128 for (size_t i = 0; i < c.size(); ++i) {
129 shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (c[i]);
130 if (fc->use_subtitles() && *(fc->_subtitle_stream.get()) != *(ref->_subtitle_stream.get())) {
131 throw JoinError (_("Content to be joined must use the same subtitle stream."));
135 /* XXX: should probably check that more of the stuff below is the same in *this and ref */
137 _subtitle_streams = ref->subtitle_streams ();
138 _subtitle_stream = ref->subtitle_stream ();
139 _audio_streams = ref->ffmpeg_audio_streams ();
140 _first_video = ref->_first_video;
141 _filters = ref->_filters;
142 _color_range = ref->_color_range;
143 _color_primaries = ref->_color_primaries;
144 _color_trc = ref->_color_trc;
145 _colorspace = ref->_colorspace;
146 _bits_per_pixel = ref->_bits_per_pixel;
150 FFmpegContent::as_xml (xmlpp::Node* node) const
152 node->add_child("Type")->add_child_text ("FFmpeg");
153 Content::as_xml (node);
154 video->as_xml (node);
155 AudioContent::as_xml (node);
156 SubtitleContent::as_xml (node);
158 boost::mutex::scoped_lock lm (_mutex);
160 for (vector<shared_ptr<FFmpegSubtitleStream> >::const_iterator i = _subtitle_streams.begin(); i != _subtitle_streams.end(); ++i) {
161 xmlpp::Node* t = node->add_child("SubtitleStream");
162 if (_subtitle_stream && *i == _subtitle_stream) {
163 t->add_child("Selected")->add_child_text("1");
168 for (vector<shared_ptr<FFmpegAudioStream> >::const_iterator i = _audio_streams.begin(); i != _audio_streams.end(); ++i) {
169 (*i)->as_xml (node->add_child("AudioStream"));
172 for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
173 node->add_child("Filter")->add_child_text ((*i)->id ());
177 node->add_child("FirstVideo")->add_child_text (raw_convert<string> (_first_video.get().get()));
180 node->add_child("ColorRange")->add_child_text (raw_convert<string> (_color_range));
181 node->add_child("ColorPrimaries")->add_child_text (raw_convert<string> (_color_primaries));
182 node->add_child("ColorTransferCharacteristic")->add_child_text (raw_convert<string> (_color_trc));
183 node->add_child("Colorspace")->add_child_text (raw_convert<string> (_colorspace));
184 if (_bits_per_pixel) {
185 node->add_child("BitsPerPixel")->add_child_text (raw_convert<string> (_bits_per_pixel.get ()));
190 FFmpegContent::examine (shared_ptr<Job> job)
192 job->set_progress_unknown ();
194 Content::examine (job);
196 shared_ptr<FFmpegExaminer> examiner (new FFmpegExaminer (shared_from_this (), job));
197 take_from_video_examiner (examiner);
198 set_default_colour_conversion ();
201 boost::mutex::scoped_lock lm (_mutex);
203 _subtitle_streams = examiner->subtitle_streams ();
204 if (!_subtitle_streams.empty ()) {
205 _subtitle_stream = _subtitle_streams.front ();
208 _audio_streams = examiner->audio_streams ();
210 if (!_audio_streams.empty ()) {
211 AudioMapping m = _audio_streams.front()->mapping ();
212 film()->make_audio_mapping_default (m);
213 _audio_streams.front()->set_mapping (m);
216 _first_video = examiner->first_video ();
218 _color_range = examiner->color_range ();
219 _color_primaries = examiner->color_primaries ();
220 _color_trc = examiner->color_trc ();
221 _colorspace = examiner->colorspace ();
222 _bits_per_pixel = examiner->bits_per_pixel ();
225 signal_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
226 signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
227 signal_changed (AudioContentProperty::AUDIO_STREAMS);
231 FFmpegContent::summary () const
233 /* Get the string() here so that the name does not have quotes around it */
234 return String::compose (_("%1 [movie]"), path_summary ());
238 FFmpegContent::technical_summary () const
241 BOOST_FOREACH (shared_ptr<FFmpegAudioStream> i, ffmpeg_audio_streams ()) {
242 as += i->technical_summary () + " " ;
250 if (_subtitle_stream) {
251 ss = _subtitle_stream->technical_summary ();
254 string filt = Filter::ffmpeg_string (_filters);
256 return Content::technical_summary() + " - "
257 + video->technical_summary() + " - "
258 + AudioContent::technical_summary() + " - "
260 "ffmpeg: audio %1 subtitle %2 filters %3", as, ss, filt
265 FFmpegContent::set_subtitle_stream (shared_ptr<FFmpegSubtitleStream> s)
268 boost::mutex::scoped_lock lm (_mutex);
269 _subtitle_stream = s;
272 signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
276 operator== (FFmpegStream const & a, FFmpegStream const & b)
278 return a._id == b._id;
282 operator!= (FFmpegStream const & a, FFmpegStream const & b)
284 return a._id != b._id;
288 FFmpegContent::full_length () const
290 FrameRateChange const frc (video_frame_rate (), film()->video_frame_rate ());
291 return DCPTime::from_frames (llrint (video_length_after_3d_combine() * frc.factor()), film()->video_frame_rate());
295 FFmpegContent::set_filters (vector<Filter const *> const & filters)
298 boost::mutex::scoped_lock lm (_mutex);
302 signal_changed (FFmpegContentProperty::FILTERS);
306 FFmpegContent::identifier () const
310 s << Content::identifier() << "_"
311 << video->identifier() << "_"
312 << SubtitleContent::identifier();
314 boost::mutex::scoped_lock lm (_mutex);
316 if (_subtitle_stream) {
317 s << "_" << _subtitle_stream->identifier ();
320 for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
321 s << "_" << (*i)->id ();
327 list<ContentTimePeriod>
328 FFmpegContent::image_subtitles_during (ContentTimePeriod period, bool starting) const
330 shared_ptr<FFmpegSubtitleStream> stream = subtitle_stream ();
332 return list<ContentTimePeriod> ();
335 return stream->image_subtitles_during (period, starting);
338 list<ContentTimePeriod>
339 FFmpegContent::text_subtitles_during (ContentTimePeriod period, bool starting) const
341 shared_ptr<FFmpegSubtitleStream> stream = subtitle_stream ();
343 return list<ContentTimePeriod> ();
346 return stream->text_subtitles_during (period, starting);
350 FFmpegContent::has_image_subtitles () const
352 BOOST_FOREACH (shared_ptr<FFmpegSubtitleStream> i, subtitle_streams()) {
353 if (i->has_image_subtitles()) {
362 FFmpegContent::has_text_subtitles () const
364 BOOST_FOREACH (shared_ptr<FFmpegSubtitleStream> i, subtitle_streams()) {
365 if (i->has_text_subtitles()) {
374 FFmpegContent::set_default_colour_conversion ()
376 dcp::Size const s = video_size ();
378 boost::mutex::scoped_lock lm (_mutex);
380 if (s.width < 1080) {
381 _colour_conversion = PresetColourConversion::from_id ("rec601").conversion;
383 _colour_conversion = PresetColourConversion::from_id ("rec709").conversion;
387 vector<AudioStreamPtr>
388 FFmpegContent::audio_streams () const
390 boost::mutex::scoped_lock lm (_mutex);
392 vector<AudioStreamPtr> s;
393 copy (_audio_streams.begin(), _audio_streams.end(), back_inserter (s));
398 FFmpegContent::add_properties (list<UserProperty>& p) const
400 Content::add_properties (p);
401 video->add_properties (p);
402 AudioContent::add_properties (p);
404 if (_bits_per_pixel) {
405 int const sub = 219 * pow (2, _bits_per_pixel.get() - 8);
406 int const total = pow (2, _bits_per_pixel.get());
408 switch (_color_range) {
409 case AVCOL_RANGE_UNSPECIFIED:
410 /// TRANSLATORS: this means that the range of pixel values used in this
411 /// file is unknown (not specified in the file).
412 p.push_back (UserProperty (_("Video"), _("Colour range"), _("Unspecified")));
414 case AVCOL_RANGE_MPEG:
415 /// TRANSLATORS: this means that the range of pixel values used in this
416 /// file is limited, so that not all possible values are valid.
419 _("Video"), _("Colour range"), String::compose (_("Limited (%1-%2)"), (total - sub) / 2, (total + sub) / 2)
423 case AVCOL_RANGE_JPEG:
424 /// TRANSLATORS: this means that the range of pixel values used in this
425 /// file is full, so that all possible pixel values are valid.
426 p.push_back (UserProperty (_("Video"), _("Colour range"), String::compose (_("Full (0-%1)"), total)));
429 DCPOMATIC_ASSERT (false);
432 switch (_color_range) {
433 case AVCOL_RANGE_UNSPECIFIED:
434 /// TRANSLATORS: this means that the range of pixel values used in this
435 /// file is unknown (not specified in the file).
436 p.push_back (UserProperty (_("Video"), _("Colour range"), _("Unspecified")));
438 case AVCOL_RANGE_MPEG:
439 /// TRANSLATORS: this means that the range of pixel values used in this
440 /// file is limited, so that not all possible values are valid.
441 p.push_back (UserProperty (_("Video"), _("Colour range"), _("Limited")));
443 case AVCOL_RANGE_JPEG:
444 /// TRANSLATORS: this means that the range of pixel values used in this
445 /// file is full, so that all possible pixel values are valid.
446 p.push_back (UserProperty (_("Video"), _("Colour range"), _("Full")));
449 DCPOMATIC_ASSERT (false);
453 char const * primaries[] = {
460 _("SMPTE 170M (BT601)"),
464 _("SMPTE ST 428-1 (CIE 1931 XYZ)")
467 DCPOMATIC_ASSERT (AVCOL_PRI_NB == 11);
468 p.push_back (UserProperty (_("Video"), _("Colour primaries"), primaries[_color_primaries]));
470 char const * transfers[] = {
475 _("Gamma 22 (BT470M)"),
476 _("Gamma 28 (BT470BG)"),
477 _("SMPTE 170M (BT601)"),
480 _("Logarithmic (100:1 range)"),
481 _("Logarithmic (316:1 range)"),
483 _("BT1361 extended colour gamut"),
484 _("IEC61966-2-1 (sRGB or sYCC)"),
485 _("BT2020 for a 10-bit system"),
486 _("BT2020 for a 12-bit system"),
487 _("SMPTE ST 2084 for 10, 12, 14 and 16 bit systems"),
491 DCPOMATIC_ASSERT (AVCOL_TRC_NB == 18);
492 p.push_back (UserProperty (_("Video"), _("Colour transfer characteristic"), transfers[_color_trc]));
494 char const * spaces[] = {
495 _("RGB / sRGB (IEC61966-2-1)"),
500 _("BT470BG (BT601-6)"),
501 _("SMPTE 170M (BT601-6)"),
504 _("BT2020 non-constant luminance"),
505 _("BT2020 constant luminance"),
508 DCPOMATIC_ASSERT (AVCOL_SPC_NB == 11);
509 p.push_back (UserProperty (_("Video"), _("Colourspace"), spaces[_colorspace]));
511 if (_bits_per_pixel) {
512 p.push_back (UserProperty (_("Video"), _("Bits per pixel"), raw_convert<string> (_bits_per_pixel.get ())));
516 /** Our subtitle streams have colour maps, which can be changed, but
517 * they have no way of signalling that change. As a hack, we have this
518 * method which callers can use when they've modified one of our subtitle
522 FFmpegContent::signal_subtitle_stream_changed ()
524 signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
528 FFmpegContent::subtitle_video_frame_rate () const
530 return video->video_frame_rate ();