2 Copyright (C) 2013-2014 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.
21 #include <libavformat/avformat.h>
23 #include <libcxml/cxml.h>
24 #include <dcp/raw_convert.h>
25 #include "ffmpeg_content.h"
26 #include "ffmpeg_examiner.h"
27 #include "ffmpeg_subtitle_stream.h"
28 #include "ffmpeg_audio_stream.h"
29 #include "compose.hpp"
35 #include "exceptions.h"
36 #include "frame_rate_change.h"
40 #define LOG_GENERAL(...) film->log()->log (String::compose (__VA_ARGS__), Log::TYPE_GENERAL);
43 using std::stringstream;
48 using boost::shared_ptr;
49 using boost::dynamic_pointer_cast;
50 using dcp::raw_convert;
52 int const FFmpegContentProperty::SUBTITLE_STREAMS = 100;
53 int const FFmpegContentProperty::SUBTITLE_STREAM = 101;
54 int const FFmpegContentProperty::AUDIO_STREAMS = 102;
55 int const FFmpegContentProperty::AUDIO_STREAM = 103;
56 int const FFmpegContentProperty::FILTERS = 104;
58 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, boost::filesystem::path p)
62 , SubtitleContent (f, p)
67 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, cxml::ConstNodePtr node, int version, list<string>& notes)
69 , VideoContent (f, node, version)
70 , AudioContent (f, node)
71 , SubtitleContent (f, node, version)
73 list<cxml::NodePtr> c = node->node_children ("SubtitleStream");
74 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
75 _subtitle_streams.push_back (shared_ptr<FFmpegSubtitleStream> (new FFmpegSubtitleStream (*i)));
76 if ((*i)->optional_number_child<int> ("Selected")) {
77 _subtitle_stream = _subtitle_streams.back ();
81 c = node->node_children ("AudioStream");
82 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
83 _audio_streams.push_back (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream (*i, version)));
84 if ((*i)->optional_number_child<int> ("Selected")) {
85 _audio_stream = _audio_streams.back ();
89 c = node->node_children ("Filter");
90 for (list<cxml::NodePtr>::iterator i = c.begin(); i != c.end(); ++i) {
91 Filter const * f = Filter::from_id ((*i)->content ());
93 _filters.push_back (f);
95 notes.push_back (String::compose (_("DCP-o-matic no longer supports the `%1' filter, so it has been turned off."), (*i)->content()));
99 _first_video = node->optional_number_child<double> ("FirstVideo");
102 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, vector<boost::shared_ptr<Content> > c)
104 , VideoContent (f, c)
105 , AudioContent (f, c)
106 , SubtitleContent (f, c)
108 shared_ptr<FFmpegContent> ref = dynamic_pointer_cast<FFmpegContent> (c[0]);
111 for (size_t i = 0; i < c.size(); ++i) {
112 shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (c[i]);
113 if (fc->subtitle_use() && *(fc->_subtitle_stream.get()) != *(ref->_subtitle_stream.get())) {
114 throw JoinError (_("Content to be joined must use the same subtitle stream."));
117 if (*(fc->_audio_stream.get()) != *(ref->_audio_stream.get())) {
118 throw JoinError (_("Content to be joined must use the same audio stream."));
122 _subtitle_streams = ref->subtitle_streams ();
123 _subtitle_stream = ref->subtitle_stream ();
124 _audio_streams = ref->audio_streams ();
125 _audio_stream = ref->audio_stream ();
126 _first_video = ref->_first_video;
130 FFmpegContent::as_xml (xmlpp::Node* node) const
132 node->add_child("Type")->add_child_text ("FFmpeg");
133 Content::as_xml (node);
134 VideoContent::as_xml (node);
135 AudioContent::as_xml (node);
136 SubtitleContent::as_xml (node);
138 boost::mutex::scoped_lock lm (_mutex);
140 for (vector<shared_ptr<FFmpegSubtitleStream> >::const_iterator i = _subtitle_streams.begin(); i != _subtitle_streams.end(); ++i) {
141 xmlpp::Node* t = node->add_child("SubtitleStream");
142 if (_subtitle_stream && *i == _subtitle_stream) {
143 t->add_child("Selected")->add_child_text("1");
148 for (vector<shared_ptr<FFmpegAudioStream> >::const_iterator i = _audio_streams.begin(); i != _audio_streams.end(); ++i) {
149 xmlpp::Node* t = node->add_child("AudioStream");
150 if (_audio_stream && *i == _audio_stream) {
151 t->add_child("Selected")->add_child_text("1");
156 for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
157 node->add_child("Filter")->add_child_text ((*i)->id ());
161 node->add_child("FirstVideo")->add_child_text (raw_convert<string> (_first_video.get().get()));
166 FFmpegContent::examine (shared_ptr<Job> job)
168 job->set_progress_unknown ();
170 Content::examine (job);
172 shared_ptr<FFmpegExaminer> examiner (new FFmpegExaminer (shared_from_this ()));
173 take_from_video_examiner (examiner);
175 shared_ptr<const Film> film = _film.lock ();
177 LOG_GENERAL ("Video length obtained from header as %1 frames", video_length.frames (video_frame_rate ()));
180 boost::mutex::scoped_lock lm (_mutex);
182 _subtitle_streams = examiner->subtitle_streams ();
183 if (!_subtitle_streams.empty ()) {
184 _subtitle_stream = _subtitle_streams.front ();
187 _audio_streams = examiner->audio_streams ();
188 if (!_audio_streams.empty ()) {
189 _audio_stream = _audio_streams.front ();
192 _first_video = examiner->first_video ();
195 signal_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
196 signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
197 signal_changed (FFmpegContentProperty::AUDIO_STREAMS);
198 signal_changed (FFmpegContentProperty::AUDIO_STREAM);
199 signal_changed (AudioContentProperty::AUDIO_CHANNELS);
203 FFmpegContent::summary () const
205 /* Get the string() here so that the name does not have quotes around it */
206 return String::compose (_("%1 [movie]"), path_summary ());
210 FFmpegContent::technical_summary () const
214 as = _audio_stream->technical_summary ();
218 if (_subtitle_stream) {
219 ss = _subtitle_stream->technical_summary ();
222 string filt = Filter::ffmpeg_string (_filters);
224 return Content::technical_summary() + " - "
225 + VideoContent::technical_summary() + " - "
226 + AudioContent::technical_summary() + " - "
228 "ffmpeg: audio %1, subtitle %2, filters %3", as, ss, filt
233 FFmpegContent::information () const
235 if (video_length() == ContentTime (0) || video_frame_rate() == 0) {
241 s << String::compose (_("%1 frames; %2 frames per second"), video_length_after_3d_combine().frames (video_frame_rate()), video_frame_rate()) << "\n";
242 s << VideoContent::information ();
248 FFmpegContent::set_subtitle_stream (shared_ptr<FFmpegSubtitleStream> s)
251 boost::mutex::scoped_lock lm (_mutex);
252 _subtitle_stream = s;
255 signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
259 FFmpegContent::set_audio_stream (shared_ptr<FFmpegAudioStream> s)
262 boost::mutex::scoped_lock lm (_mutex);
266 signal_changed (FFmpegContentProperty::AUDIO_STREAM);
270 FFmpegContent::audio_length () const
272 if (!audio_stream ()) {
273 return ContentTime ();
276 return video_length ();
280 FFmpegContent::audio_channels () const
282 boost::mutex::scoped_lock lm (_mutex);
284 if (!_audio_stream) {
288 return _audio_stream->channels;
292 FFmpegContent::audio_frame_rate () const
294 boost::mutex::scoped_lock lm (_mutex);
296 if (!_audio_stream) {
300 return _audio_stream->frame_rate;
304 operator== (FFmpegStream const & a, FFmpegStream const & b)
306 return a._id == b._id;
310 operator!= (FFmpegStream const & a, FFmpegStream const & b)
312 return a._id != b._id;
316 FFmpegContent::full_length () const
318 shared_ptr<const Film> film = _film.lock ();
320 return DCPTime (video_length_after_3d_combine(), FrameRateChange (video_frame_rate (), film->video_frame_rate ()));
324 FFmpegContent::audio_mapping () const
326 boost::mutex::scoped_lock lm (_mutex);
328 if (!_audio_stream) {
329 return AudioMapping ();
332 return _audio_stream->mapping;
336 FFmpegContent::set_filters (vector<Filter const *> const & filters)
339 boost::mutex::scoped_lock lm (_mutex);
343 signal_changed (FFmpegContentProperty::FILTERS);
347 FFmpegContent::set_audio_mapping (AudioMapping m)
349 audio_stream()->mapping = m;
350 AudioContent::set_audio_mapping (m);
354 FFmpegContent::identifier () const
358 s << VideoContent::identifier();
360 boost::mutex::scoped_lock lm (_mutex);
362 if (_subtitle_stream) {
363 s << "_" << _subtitle_stream->identifier ();
366 for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
367 s << "_" << (*i)->id ();
373 boost::filesystem::path
374 FFmpegContent::audio_analysis_path () const
376 shared_ptr<const Film> film = _film.lock ();
378 return boost::filesystem::path ();
381 /* We need to include the stream ID in this path so that we get different
382 analyses for each stream.
385 boost::filesystem::path p = film->audio_analysis_dir ();
386 string name = digest ();
387 if (audio_stream ()) {
388 name += "_" + audio_stream()->identifier ();
394 list<ContentTimePeriod>
395 FFmpegContent::subtitles_during (ContentTimePeriod period, bool starting) const
397 list<ContentTimePeriod> d;
399 shared_ptr<FFmpegSubtitleStream> stream = subtitle_stream ();
404 /* XXX: inefficient */
405 for (vector<ContentTimePeriod>::const_iterator i = stream->periods.begin(); i != stream->periods.end(); ++i) {
406 if ((starting && period.contains (i->from)) || (!starting && period.overlaps (*i))) {