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"
39 #define LOG_GENERAL(...) film->log()->log (String::compose (__VA_ARGS__), Log::TYPE_GENERAL);
42 using std::stringstream;
47 using boost::shared_ptr;
48 using boost::dynamic_pointer_cast;
49 using dcp::raw_convert;
51 int const FFmpegContentProperty::SUBTITLE_STREAMS = 100;
52 int const FFmpegContentProperty::SUBTITLE_STREAM = 101;
53 int const FFmpegContentProperty::AUDIO_STREAMS = 102;
54 int const FFmpegContentProperty::AUDIO_STREAM = 103;
55 int const FFmpegContentProperty::FILTERS = 104;
57 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, boost::filesystem::path p)
61 , SubtitleContent (f, p)
66 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, cxml::ConstNodePtr node, int version, list<string>& notes)
68 , VideoContent (f, node, version)
69 , AudioContent (f, node)
70 , SubtitleContent (f, node, version)
72 list<cxml::NodePtr> c = node->node_children ("SubtitleStream");
73 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
74 _subtitle_streams.push_back (shared_ptr<FFmpegSubtitleStream> (new FFmpegSubtitleStream (*i)));
75 if ((*i)->optional_number_child<int> ("Selected")) {
76 _subtitle_stream = _subtitle_streams.back ();
80 c = node->node_children ("AudioStream");
81 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
82 _audio_streams.push_back (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream (*i, version)));
83 if ((*i)->optional_number_child<int> ("Selected")) {
84 _audio_stream = _audio_streams.back ();
88 c = node->node_children ("Filter");
89 for (list<cxml::NodePtr>::iterator i = c.begin(); i != c.end(); ++i) {
90 Filter const * f = Filter::from_id ((*i)->content ());
92 _filters.push_back (f);
94 notes.push_back (String::compose (_("DCP-o-matic no longer supports the `%1' filter, so it has been turned off."), (*i)->content()));
98 _first_video = node->optional_number_child<double> ("FirstVideo");
101 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, vector<boost::shared_ptr<Content> > c)
103 , VideoContent (f, c)
104 , AudioContent (f, c)
105 , SubtitleContent (f, c)
107 shared_ptr<FFmpegContent> ref = dynamic_pointer_cast<FFmpegContent> (c[0]);
110 for (size_t i = 0; i < c.size(); ++i) {
111 shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (c[i]);
112 if (f->with_subtitles() && *(fc->_subtitle_stream.get()) != *(ref->_subtitle_stream.get())) {
113 throw JoinError (_("Content to be joined must use the same subtitle stream."));
116 if (*(fc->_audio_stream.get()) != *(ref->_audio_stream.get())) {
117 throw JoinError (_("Content to be joined must use the same audio stream."));
121 _subtitle_streams = ref->subtitle_streams ();
122 _subtitle_stream = ref->subtitle_stream ();
123 _audio_streams = ref->audio_streams ();
124 _audio_stream = ref->audio_stream ();
125 _first_video = ref->_first_video;
129 FFmpegContent::as_xml (xmlpp::Node* node) const
131 node->add_child("Type")->add_child_text ("FFmpeg");
132 Content::as_xml (node);
133 VideoContent::as_xml (node);
134 AudioContent::as_xml (node);
135 SubtitleContent::as_xml (node);
137 boost::mutex::scoped_lock lm (_mutex);
139 for (vector<shared_ptr<FFmpegSubtitleStream> >::const_iterator i = _subtitle_streams.begin(); i != _subtitle_streams.end(); ++i) {
140 xmlpp::Node* t = node->add_child("SubtitleStream");
141 if (_subtitle_stream && *i == _subtitle_stream) {
142 t->add_child("Selected")->add_child_text("1");
147 for (vector<shared_ptr<FFmpegAudioStream> >::const_iterator i = _audio_streams.begin(); i != _audio_streams.end(); ++i) {
148 xmlpp::Node* t = node->add_child("AudioStream");
149 if (_audio_stream && *i == _audio_stream) {
150 t->add_child("Selected")->add_child_text("1");
155 for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
156 node->add_child("Filter")->add_child_text ((*i)->id ());
160 node->add_child("FirstVideo")->add_child_text (raw_convert<string> (_first_video.get().get()));
165 FFmpegContent::examine (shared_ptr<Job> job)
167 job->set_progress_unknown ();
169 Content::examine (job);
171 shared_ptr<FFmpegExaminer> examiner (new FFmpegExaminer (shared_from_this ()));
172 take_from_video_examiner (examiner);
174 ContentTime video_length = examiner->video_length ();
176 shared_ptr<const Film> film = _film.lock ();
178 LOG_GENERAL ("Video length obtained from header as %1 frames", video_length.frames (video_frame_rate ()));
181 boost::mutex::scoped_lock lm (_mutex);
183 _video_length = video_length;
185 _subtitle_streams = examiner->subtitle_streams ();
186 if (!_subtitle_streams.empty ()) {
187 _subtitle_stream = _subtitle_streams.front ();
190 _audio_streams = examiner->audio_streams ();
191 if (!_audio_streams.empty ()) {
192 _audio_stream = _audio_streams.front ();
195 _first_video = examiner->first_video ();
198 signal_changed (ContentProperty::LENGTH);
199 signal_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
200 signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
201 signal_changed (FFmpegContentProperty::AUDIO_STREAMS);
202 signal_changed (FFmpegContentProperty::AUDIO_STREAM);
203 signal_changed (AudioContentProperty::AUDIO_CHANNELS);
207 FFmpegContent::summary () const
209 /* Get the string() here so that the name does not have quotes around it */
210 return String::compose (_("%1 [movie]"), path_summary ());
214 FFmpegContent::technical_summary () const
218 as = _audio_stream->technical_summary ();
222 if (_subtitle_stream) {
223 ss = _subtitle_stream->technical_summary ();
226 string filt = Filter::ffmpeg_string (_filters);
228 return Content::technical_summary() + " - "
229 + VideoContent::technical_summary() + " - "
230 + AudioContent::technical_summary() + " - "
232 "ffmpeg: audio %1, subtitle %2, filters %3", as, ss, filt
237 FFmpegContent::information () const
239 if (video_length() == ContentTime (0) || video_frame_rate() == 0) {
245 s << String::compose (_("%1 frames; %2 frames per second"), video_length_after_3d_combine().frames (video_frame_rate()), video_frame_rate()) << "\n";
246 s << VideoContent::information ();
252 FFmpegContent::set_subtitle_stream (shared_ptr<FFmpegSubtitleStream> s)
255 boost::mutex::scoped_lock lm (_mutex);
256 _subtitle_stream = s;
259 signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
263 FFmpegContent::set_audio_stream (shared_ptr<FFmpegAudioStream> s)
266 boost::mutex::scoped_lock lm (_mutex);
270 signal_changed (FFmpegContentProperty::AUDIO_STREAM);
274 FFmpegContent::audio_length () const
276 if (!audio_stream ()) {
277 return ContentTime ();
280 return video_length ();
284 FFmpegContent::audio_channels () const
286 boost::mutex::scoped_lock lm (_mutex);
288 if (!_audio_stream) {
292 return _audio_stream->channels;
296 FFmpegContent::audio_frame_rate () const
298 boost::mutex::scoped_lock lm (_mutex);
300 if (!_audio_stream) {
304 return _audio_stream->frame_rate;
308 operator== (FFmpegStream const & a, FFmpegStream const & b)
310 return a._id == b._id;
314 operator!= (FFmpegStream const & a, FFmpegStream const & b)
316 return a._id != b._id;
320 FFmpegContent::full_length () const
322 shared_ptr<const Film> film = _film.lock ();
324 return DCPTime (video_length_after_3d_combine(), FrameRateChange (video_frame_rate (), film->video_frame_rate ()));
328 FFmpegContent::audio_mapping () const
330 boost::mutex::scoped_lock lm (_mutex);
332 if (!_audio_stream) {
333 return AudioMapping ();
336 return _audio_stream->mapping;
340 FFmpegContent::set_filters (vector<Filter const *> const & filters)
343 boost::mutex::scoped_lock lm (_mutex);
347 signal_changed (FFmpegContentProperty::FILTERS);
351 FFmpegContent::set_audio_mapping (AudioMapping m)
353 audio_stream()->mapping = m;
354 AudioContent::set_audio_mapping (m);
358 FFmpegContent::identifier () const
362 s << VideoContent::identifier();
364 boost::mutex::scoped_lock lm (_mutex);
366 if (_subtitle_stream) {
367 s << "_" << _subtitle_stream->identifier ();
370 for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
371 s << "_" << (*i)->id ();
377 boost::filesystem::path
378 FFmpegContent::audio_analysis_path () const
380 shared_ptr<const Film> film = _film.lock ();
382 return boost::filesystem::path ();
385 /* We need to include the stream ID in this path so that we get different
386 analyses for each stream.
389 boost::filesystem::path p = film->audio_analysis_dir ();
390 string name = digest ();
391 if (audio_stream ()) {
392 name += "_" + audio_stream()->identifier ();
399 FFmpegContent::has_subtitle_during (ContentTimePeriod period) const
401 shared_ptr<FFmpegSubtitleStream> stream = subtitle_stream ();
403 /* XXX: inefficient */
404 for (vector<ContentTimePeriod>::const_iterator i = stream->periods.begin(); i != stream->periods.end(); ++i) {
405 if (i->from <= period.to && i->to >= period.from) {