2 Copyright (C) 2013-2015 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 "ffmpeg_examiner.h"
22 #include "ffmpeg_subtitle_stream.h"
23 #include "ffmpeg_audio_stream.h"
24 #include "compose.hpp"
30 #include "exceptions.h"
31 #include "frame_rate_change.h"
32 #include "safe_stringstream.h"
33 #include "raw_convert.h"
34 #include <libcxml/cxml.h>
36 #include <libavformat/avformat.h>
38 #include <boost/foreach.hpp>
42 #define LOG_GENERAL(...) film->log()->log (String::compose (__VA_ARGS__), Log::TYPE_GENERAL);
49 using boost::shared_ptr;
50 using boost::dynamic_pointer_cast;
52 int const FFmpegContentProperty::SUBTITLE_STREAMS = 100;
53 int const FFmpegContentProperty::SUBTITLE_STREAM = 101;
54 int const FFmpegContentProperty::FILTERS = 102;
56 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, boost::filesystem::path p)
60 , SubtitleContent (f, p)
65 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, cxml::ConstNodePtr node, int version, list<string>& notes)
67 , VideoContent (f, node, version)
68 , AudioContent (f, node)
69 , SubtitleContent (f, node, version)
71 list<cxml::NodePtr> c = node->node_children ("SubtitleStream");
72 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
73 _subtitle_streams.push_back (shared_ptr<FFmpegSubtitleStream> (new FFmpegSubtitleStream (*i)));
74 if ((*i)->optional_number_child<int> ("Selected")) {
75 _subtitle_stream = _subtitle_streams.back ();
79 c = node->node_children ("AudioStream");
80 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
81 _audio_streams.push_back (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream (*i, version)));
84 c = node->node_children ("Filter");
85 for (list<cxml::NodePtr>::iterator i = c.begin(); i != c.end(); ++i) {
86 Filter const * f = Filter::from_id ((*i)->content ());
88 _filters.push_back (f);
90 notes.push_back (String::compose (_("DCP-o-matic no longer supports the `%1' filter, so it has been turned off."), (*i)->content()));
94 _first_video = node->optional_number_child<double> ("FirstVideo");
97 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, vector<boost::shared_ptr<Content> > c)
100 , AudioContent (f, c)
101 , SubtitleContent (f, c)
103 shared_ptr<FFmpegContent> ref = dynamic_pointer_cast<FFmpegContent> (c[0]);
104 DCPOMATIC_ASSERT (ref);
106 for (size_t i = 0; i < c.size(); ++i) {
107 shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (c[i]);
108 if (fc->use_subtitles() && *(fc->_subtitle_stream.get()) != *(ref->_subtitle_stream.get())) {
109 throw JoinError (_("Content to be joined must use the same subtitle stream."));
113 _subtitle_streams = ref->subtitle_streams ();
114 _subtitle_stream = ref->subtitle_stream ();
115 _audio_streams = ref->ffmpeg_audio_streams ();
116 _first_video = ref->_first_video;
120 FFmpegContent::as_xml (xmlpp::Node* node) const
122 node->add_child("Type")->add_child_text ("FFmpeg");
123 Content::as_xml (node);
124 VideoContent::as_xml (node);
125 AudioContent::as_xml (node);
126 SubtitleContent::as_xml (node);
128 boost::mutex::scoped_lock lm (_mutex);
130 for (vector<shared_ptr<FFmpegSubtitleStream> >::const_iterator i = _subtitle_streams.begin(); i != _subtitle_streams.end(); ++i) {
131 xmlpp::Node* t = node->add_child("SubtitleStream");
132 if (_subtitle_stream && *i == _subtitle_stream) {
133 t->add_child("Selected")->add_child_text("1");
138 for (vector<shared_ptr<FFmpegAudioStream> >::const_iterator i = _audio_streams.begin(); i != _audio_streams.end(); ++i) {
139 (*i)->as_xml (node->add_child("AudioStream"));
142 for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
143 node->add_child("Filter")->add_child_text ((*i)->id ());
147 node->add_child("FirstVideo")->add_child_text (raw_convert<string> (_first_video.get().get()));
152 FFmpegContent::examine (shared_ptr<Job> job)
154 job->set_progress_unknown ();
156 Content::examine (job);
158 shared_ptr<FFmpegExaminer> examiner (new FFmpegExaminer (shared_from_this (), job));
159 take_from_video_examiner (examiner);
161 shared_ptr<const Film> film = _film.lock ();
162 DCPOMATIC_ASSERT (film);
165 boost::mutex::scoped_lock lm (_mutex);
167 _subtitle_streams = examiner->subtitle_streams ();
168 if (!_subtitle_streams.empty ()) {
169 _subtitle_stream = _subtitle_streams.front ();
172 _audio_streams = examiner->audio_streams ();
174 if (!_audio_streams.empty ()) {
175 AudioMapping m = _audio_streams.front()->mapping ();
176 film->make_audio_mapping_default (m);
177 _audio_streams.front()->set_mapping (m);
180 _first_video = examiner->first_video ();
183 signal_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
184 signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
185 signal_changed (AudioContentProperty::AUDIO_STREAMS);
189 FFmpegContent::summary () const
191 /* Get the string() here so that the name does not have quotes around it */
192 return String::compose (_("%1 [movie]"), path_summary ());
196 FFmpegContent::technical_summary () const
199 BOOST_FOREACH (shared_ptr<FFmpegAudioStream> i, ffmpeg_audio_streams ()) {
200 as += i->technical_summary () + " " ;
208 if (_subtitle_stream) {
209 ss = _subtitle_stream->technical_summary ();
212 string filt = Filter::ffmpeg_string (_filters);
214 return Content::technical_summary() + " - "
215 + VideoContent::technical_summary() + " - "
216 + AudioContent::technical_summary() + " - "
218 "ffmpeg: audio %1 subtitle %2 filters %3", as, ss, filt
223 FFmpegContent::set_subtitle_stream (shared_ptr<FFmpegSubtitleStream> s)
226 boost::mutex::scoped_lock lm (_mutex);
227 _subtitle_stream = s;
230 signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
234 operator== (FFmpegStream const & a, FFmpegStream const & b)
236 return a._id == b._id;
240 operator!= (FFmpegStream const & a, FFmpegStream const & b)
242 return a._id != b._id;
246 FFmpegContent::full_length () const
248 shared_ptr<const Film> film = _film.lock ();
249 DCPOMATIC_ASSERT (film);
250 FrameRateChange const frc (video_frame_rate (), film->video_frame_rate ());
251 return DCPTime::from_frames (rint (video_length_after_3d_combine() * frc.factor()), film->video_frame_rate());
255 FFmpegContent::set_filters (vector<Filter const *> const & filters)
258 boost::mutex::scoped_lock lm (_mutex);
262 signal_changed (FFmpegContentProperty::FILTERS);
266 FFmpegContent::identifier () const
270 s << VideoContent::identifier();
272 boost::mutex::scoped_lock lm (_mutex);
274 if (_subtitle_stream) {
275 s << "_" << _subtitle_stream->identifier ();
278 for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
279 s << "_" << (*i)->id ();
285 list<ContentTimePeriod>
286 FFmpegContent::subtitles_during (ContentTimePeriod period, bool starting) const
288 shared_ptr<FFmpegSubtitleStream> stream = subtitle_stream ();
290 return list<ContentTimePeriod> ();
293 return stream->subtitles_during (period, starting);
297 FFmpegContent::has_subtitles () const
299 return !subtitle_streams().empty ();
303 FFmpegContent::set_default_colour_conversion ()
305 dcp::Size const s = video_size ();
307 boost::mutex::scoped_lock lm (_mutex);
309 if (s.width < 1080) {
310 _colour_conversion = PresetColourConversion::from_id ("rec601").conversion;
312 _colour_conversion = PresetColourConversion::from_id ("rec709").conversion;
316 vector<AudioStreamPtr>
317 FFmpegContent::audio_streams () const
319 boost::mutex::scoped_lock lm (_mutex);
321 vector<AudioStreamPtr> s;
322 copy (_audio_streams.begin(), _audio_streams.end(), back_inserter (s));