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 "ffmpeg_content.h"
25 #include "ffmpeg_examiner.h"
26 #include "compose.hpp"
32 #include "exceptions.h"
37 using std::stringstream;
42 using boost::shared_ptr;
43 using boost::lexical_cast;
44 using boost::dynamic_pointer_cast;
46 int const FFmpegContentProperty::SUBTITLE_STREAMS = 100;
47 int const FFmpegContentProperty::SUBTITLE_STREAM = 101;
48 int const FFmpegContentProperty::AUDIO_STREAMS = 102;
49 int const FFmpegContentProperty::AUDIO_STREAM = 103;
50 int const FFmpegContentProperty::FILTERS = 104;
52 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, boost::filesystem::path p)
56 , SubtitleContent (f, p)
61 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node, int version, list<string>& notes)
63 , VideoContent (f, node, version)
64 , AudioContent (f, node)
65 , SubtitleContent (f, node, version)
67 list<cxml::NodePtr> c = node->node_children ("SubtitleStream");
68 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
69 _subtitle_streams.push_back (shared_ptr<FFmpegSubtitleStream> (new FFmpegSubtitleStream (*i)));
70 if ((*i)->optional_number_child<int> ("Selected")) {
71 _subtitle_stream = _subtitle_streams.back ();
75 c = node->node_children ("AudioStream");
76 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
77 _audio_streams.push_back (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream (*i, version)));
78 if ((*i)->optional_number_child<int> ("Selected")) {
79 _audio_stream = _audio_streams.back ();
83 c = node->node_children ("Filter");
84 for (list<cxml::NodePtr>::iterator i = c.begin(); i != c.end(); ++i) {
85 Filter const * f = Filter::from_id ((*i)->content ());
87 _filters.push_back (f);
89 notes.push_back (String::compose (_("DCP-o-matic no longer supports the `%1' filter, so it has been turned off."), (*i)->content()));
93 _first_video = node->optional_number_child<double> ("FirstVideo");
96 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, vector<boost::shared_ptr<Content> > c)
100 , SubtitleContent (f, c)
102 shared_ptr<FFmpegContent> ref = dynamic_pointer_cast<FFmpegContent> (c[0]);
105 for (size_t i = 0; i < c.size(); ++i) {
106 shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (c[i]);
107 if (f->with_subtitles() && *(fc->_subtitle_stream.get()) != *(ref->_subtitle_stream.get())) {
108 throw JoinError (_("Content to be joined must use the same subtitle stream."));
111 if (*(fc->_audio_stream.get()) != *(ref->_audio_stream.get())) {
112 throw JoinError (_("Content to be joined must use the same audio stream."));
116 _subtitle_streams = ref->subtitle_streams ();
117 _subtitle_stream = ref->subtitle_stream ();
118 _audio_streams = ref->audio_streams ();
119 _audio_stream = ref->audio_stream ();
120 _first_video = ref->_first_video;
124 FFmpegContent::as_xml (xmlpp::Node* node) const
126 node->add_child("Type")->add_child_text ("FFmpeg");
127 Content::as_xml (node);
128 VideoContent::as_xml (node);
129 AudioContent::as_xml (node);
130 SubtitleContent::as_xml (node);
132 boost::mutex::scoped_lock lm (_mutex);
134 for (vector<shared_ptr<FFmpegSubtitleStream> >::const_iterator i = _subtitle_streams.begin(); i != _subtitle_streams.end(); ++i) {
135 xmlpp::Node* t = node->add_child("SubtitleStream");
136 if (_subtitle_stream && *i == _subtitle_stream) {
137 t->add_child("Selected")->add_child_text("1");
142 for (vector<shared_ptr<FFmpegAudioStream> >::const_iterator i = _audio_streams.begin(); i != _audio_streams.end(); ++i) {
143 xmlpp::Node* t = node->add_child("AudioStream");
144 if (_audio_stream && *i == _audio_stream) {
145 t->add_child("Selected")->add_child_text("1");
150 for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
151 node->add_child("Filter")->add_child_text ((*i)->id ());
155 node->add_child("FirstVideo")->add_child_text (lexical_cast<string> (_first_video.get().get()));
160 FFmpegContent::examine (shared_ptr<Job> job)
162 job->set_progress_unknown ();
164 Content::examine (job);
166 shared_ptr<FFmpegExaminer> examiner (new FFmpegExaminer (shared_from_this ()));
167 take_from_video_examiner (examiner);
169 ContentTime video_length = examiner->video_length ();
171 shared_ptr<const Film> film = _film.lock ();
173 film->log()->log (String::compose ("Video length obtained from header as %1 frames", video_length.frames (video_frame_rate ())));
176 boost::mutex::scoped_lock lm (_mutex);
178 _video_length = video_length;
180 _subtitle_streams = examiner->subtitle_streams ();
181 if (!_subtitle_streams.empty ()) {
182 _subtitle_stream = _subtitle_streams.front ();
185 _audio_streams = examiner->audio_streams ();
186 if (!_audio_streams.empty ()) {
187 _audio_stream = _audio_streams.front ();
190 _first_video = examiner->first_video ();
193 signal_changed (ContentProperty::LENGTH);
194 signal_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
195 signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
196 signal_changed (FFmpegContentProperty::AUDIO_STREAMS);
197 signal_changed (FFmpegContentProperty::AUDIO_STREAM);
198 signal_changed (AudioContentProperty::AUDIO_CHANNELS);
202 FFmpegContent::summary () const
204 /* Get the string() here so that the name does not have quotes around it */
205 return String::compose (_("%1 [movie]"), path_summary ());
209 FFmpegContent::technical_summary () const
213 as = _audio_stream->technical_summary ();
217 if (_subtitle_stream) {
218 ss = _subtitle_stream->technical_summary ();
221 string filt = Filter::ffmpeg_string (_filters);
223 return Content::technical_summary() + " - "
224 + VideoContent::technical_summary() + " - "
225 + AudioContent::technical_summary() + " - "
227 "ffmpeg: audio %1, subtitle %2, filters %3", as, ss, filt
232 FFmpegContent::information () const
234 if (video_length() == ContentTime (0) || video_frame_rate() == 0) {
240 s << String::compose (_("%1 frames; %2 frames per second"), video_length_after_3d_combine().frames (video_frame_rate()), video_frame_rate()) << "\n";
241 s << VideoContent::information ();
247 FFmpegContent::set_subtitle_stream (shared_ptr<FFmpegSubtitleStream> s)
250 boost::mutex::scoped_lock lm (_mutex);
251 _subtitle_stream = s;
254 signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
258 FFmpegContent::set_audio_stream (shared_ptr<FFmpegAudioStream> s)
261 boost::mutex::scoped_lock lm (_mutex);
265 signal_changed (FFmpegContentProperty::AUDIO_STREAM);
269 FFmpegContent::audio_length () const
271 if (!audio_stream ()) {
272 return ContentTime ();
275 return video_length ();
279 FFmpegContent::audio_channels () const
281 boost::mutex::scoped_lock lm (_mutex);
283 if (!_audio_stream) {
287 return _audio_stream->channels;
291 FFmpegContent::audio_frame_rate () const
293 boost::mutex::scoped_lock lm (_mutex);
295 if (!_audio_stream) {
299 return _audio_stream->frame_rate;
303 operator== (FFmpegStream const & a, FFmpegStream const & b)
305 return a._id == b._id;
309 operator!= (FFmpegStream const & a, FFmpegStream const & b)
311 return a._id != b._id;
314 FFmpegStream::FFmpegStream (shared_ptr<const cxml::Node> node)
315 : name (node->string_child ("Name"))
316 , _id (node->number_child<int> ("Id"))
322 FFmpegStream::as_xml (xmlpp::Node* root) const
324 root->add_child("Name")->add_child_text (name);
325 root->add_child("Id")->add_child_text (lexical_cast<string> (_id));
328 FFmpegAudioStream::FFmpegAudioStream (shared_ptr<const cxml::Node> node, int version)
329 : FFmpegStream (node)
330 , mapping (node->node_child ("Mapping"), version)
332 frame_rate = node->number_child<int> ("FrameRate");
333 channels = node->number_child<int64_t> ("Channels");
334 first_audio = node->optional_number_child<double> ("FirstAudio");
338 FFmpegAudioStream::as_xml (xmlpp::Node* root) const
340 FFmpegStream::as_xml (root);
341 root->add_child("FrameRate")->add_child_text (lexical_cast<string> (frame_rate));
342 root->add_child("Channels")->add_child_text (lexical_cast<string> (channels));
344 root->add_child("FirstAudio")->add_child_text (lexical_cast<string> (first_audio.get().get()));
346 mapping.as_xml (root->add_child("Mapping"));
350 FFmpegStream::uses_index (AVFormatContext const * fc, int index) const
353 while (i < fc->nb_streams) {
354 if (fc->streams[i]->id == _id) {
355 return int (i) == index;
364 FFmpegStream::stream (AVFormatContext const * fc) const
367 while (i < fc->nb_streams) {
368 if (fc->streams[i]->id == _id) {
369 return fc->streams[i];
378 /** Construct a SubtitleStream from a value returned from to_string().
379 * @param t String returned from to_string().
380 * @param v State file version.
382 FFmpegSubtitleStream::FFmpegSubtitleStream (shared_ptr<const cxml::Node> node)
383 : FFmpegStream (node)
389 FFmpegSubtitleStream::as_xml (xmlpp::Node* root) const
391 FFmpegStream::as_xml (root);
395 FFmpegContent::full_length () const
397 shared_ptr<const Film> film = _film.lock ();
399 return DCPTime (video_length_after_3d_combine(), FrameRateChange (video_frame_rate (), film->video_frame_rate ()));
403 FFmpegContent::audio_mapping () const
405 boost::mutex::scoped_lock lm (_mutex);
407 if (!_audio_stream) {
408 return AudioMapping ();
411 return _audio_stream->mapping;
415 FFmpegContent::set_filters (vector<Filter const *> const & filters)
418 boost::mutex::scoped_lock lm (_mutex);
422 signal_changed (FFmpegContentProperty::FILTERS);
426 FFmpegContent::set_audio_mapping (AudioMapping m)
428 audio_stream()->mapping = m;
429 AudioContent::set_audio_mapping (m);
433 FFmpegContent::identifier () const
437 s << VideoContent::identifier();
439 boost::mutex::scoped_lock lm (_mutex);
441 if (_subtitle_stream) {
442 s << "_" << _subtitle_stream->identifier ();
445 for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
446 s << "_" << (*i)->id ();
452 boost::filesystem::path
453 FFmpegContent::audio_analysis_path () const
455 shared_ptr<const Film> film = _film.lock ();
457 return boost::filesystem::path ();
460 /* We need to include the stream ID in this path so that we get different
461 analyses for each stream.
464 boost::filesystem::path p = film->audio_analysis_dir ();
465 string name = digest ();
466 if (audio_stream ()) {
467 name += "_" + audio_stream()->identifier ();