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 <libdcp/raw_convert.h>
25 #include "ffmpeg_content.h"
26 #include "ffmpeg_examiner.h"
27 #include "compose.hpp"
33 #include "exceptions.h"
38 using std::stringstream;
43 using boost::shared_ptr;
44 using boost::dynamic_pointer_cast;
45 using libdcp::raw_convert;
47 int const FFmpegContentProperty::SUBTITLE_STREAMS = 100;
48 int const FFmpegContentProperty::SUBTITLE_STREAM = 101;
49 int const FFmpegContentProperty::AUDIO_STREAMS = 102;
50 int const FFmpegContentProperty::AUDIO_STREAM = 103;
51 int const FFmpegContentProperty::FILTERS = 104;
53 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, boost::filesystem::path p)
57 , SubtitleContent (f, p)
62 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, cxml::ConstNodePtr node, int version, list<string>& notes)
64 , VideoContent (f, node, version)
65 , AudioContent (f, node)
66 , SubtitleContent (f, node, version)
68 list<cxml::NodePtr> c = node->node_children ("SubtitleStream");
69 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
70 _subtitle_streams.push_back (shared_ptr<FFmpegSubtitleStream> (new FFmpegSubtitleStream (*i)));
71 if ((*i)->optional_number_child<int> ("Selected")) {
72 _subtitle_stream = _subtitle_streams.back ();
76 c = node->node_children ("AudioStream");
77 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
78 _audio_streams.push_back (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream (*i, version)));
79 if ((*i)->optional_number_child<int> ("Selected")) {
80 _audio_stream = _audio_streams.back ();
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]);
106 for (size_t i = 0; i < c.size(); ++i) {
107 shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (c[i]);
108 if (f->with_subtitles() && *(fc->_subtitle_stream.get()) != *(ref->_subtitle_stream.get())) {
109 throw JoinError (_("Content to be joined must use the same subtitle stream."));
112 if (*(fc->_audio_stream.get()) != *(ref->_audio_stream.get())) {
113 throw JoinError (_("Content to be joined must use the same audio stream."));
117 _subtitle_streams = ref->subtitle_streams ();
118 _subtitle_stream = ref->subtitle_stream ();
119 _audio_streams = ref->audio_streams ();
120 _audio_stream = ref->audio_stream ();
121 _first_video = ref->_first_video;
125 FFmpegContent::as_xml (xmlpp::Node* node) const
127 node->add_child("Type")->add_child_text ("FFmpeg");
128 Content::as_xml (node);
129 VideoContent::as_xml (node);
130 AudioContent::as_xml (node);
131 SubtitleContent::as_xml (node);
133 boost::mutex::scoped_lock lm (_mutex);
135 for (vector<shared_ptr<FFmpegSubtitleStream> >::const_iterator i = _subtitle_streams.begin(); i != _subtitle_streams.end(); ++i) {
136 xmlpp::Node* t = node->add_child("SubtitleStream");
137 if (_subtitle_stream && *i == _subtitle_stream) {
138 t->add_child("Selected")->add_child_text("1");
143 for (vector<shared_ptr<FFmpegAudioStream> >::const_iterator i = _audio_streams.begin(); i != _audio_streams.end(); ++i) {
144 xmlpp::Node* t = node->add_child("AudioStream");
145 if (_audio_stream && *i == _audio_stream) {
146 t->add_child("Selected")->add_child_text("1");
151 for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
152 node->add_child("Filter")->add_child_text ((*i)->id ());
156 node->add_child("FirstVideo")->add_child_text (raw_convert<string> (_first_video.get().get()));
161 FFmpegContent::examine (shared_ptr<Job> job)
163 job->set_progress_unknown ();
165 Content::examine (job);
167 shared_ptr<FFmpegExaminer> examiner (new FFmpegExaminer (shared_from_this ()));
168 take_from_video_examiner (examiner);
170 ContentTime video_length = examiner->video_length ();
172 shared_ptr<const Film> film = _film.lock ();
174 film->log()->log (String::compose ("Video length obtained from header as %1 frames", video_length.frames (video_frame_rate ())));
177 boost::mutex::scoped_lock lm (_mutex);
179 _video_length = video_length;
181 _subtitle_streams = examiner->subtitle_streams ();
182 if (!_subtitle_streams.empty ()) {
183 _subtitle_stream = _subtitle_streams.front ();
186 _audio_streams = examiner->audio_streams ();
187 if (!_audio_streams.empty ()) {
188 _audio_stream = _audio_streams.front ();
191 _first_video = examiner->first_video ();
194 signal_changed (ContentProperty::LENGTH);
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;
315 FFmpegStream::FFmpegStream (cxml::ConstNodePtr node)
316 : name (node->string_child ("Name"))
317 , _id (node->number_child<int> ("Id"))
323 FFmpegStream::as_xml (xmlpp::Node* root) const
325 root->add_child("Name")->add_child_text (name);
326 root->add_child("Id")->add_child_text (raw_convert<string> (_id));
329 FFmpegAudioStream::FFmpegAudioStream (cxml::ConstNodePtr node, int version)
330 : FFmpegStream (node)
331 , mapping (node->node_child ("Mapping"), version)
333 frame_rate = node->number_child<int> ("FrameRate");
334 channels = node->number_child<int64_t> ("Channels");
335 first_audio = node->optional_number_child<double> ("FirstAudio");
339 FFmpegAudioStream::as_xml (xmlpp::Node* root) const
341 FFmpegStream::as_xml (root);
342 root->add_child("FrameRate")->add_child_text (raw_convert<string> (frame_rate));
343 root->add_child("Channels")->add_child_text (raw_convert<string> (channels));
345 root->add_child("FirstAudio")->add_child_text (raw_convert<string> (first_audio.get().get()));
347 mapping.as_xml (root->add_child("Mapping"));
351 FFmpegStream::uses_index (AVFormatContext const * fc, int index) const
354 while (i < fc->nb_streams) {
355 if (fc->streams[i]->id == _id) {
356 return int (i) == index;
365 FFmpegStream::stream (AVFormatContext const * fc) const
368 while (i < fc->nb_streams) {
369 if (fc->streams[i]->id == _id) {
370 return fc->streams[i];
379 /** Construct a SubtitleStream from a value returned from to_string().
380 * @param t String returned from to_string().
381 * @param v State file version.
383 FFmpegSubtitleStream::FFmpegSubtitleStream (cxml::ConstNodePtr node)
384 : FFmpegStream (node)
390 FFmpegSubtitleStream::as_xml (xmlpp::Node* root) const
392 FFmpegStream::as_xml (root);
396 FFmpegContent::full_length () const
398 shared_ptr<const Film> film = _film.lock ();
400 return DCPTime (video_length_after_3d_combine(), FrameRateChange (video_frame_rate (), film->video_frame_rate ()));
404 FFmpegContent::audio_mapping () const
406 boost::mutex::scoped_lock lm (_mutex);
408 if (!_audio_stream) {
409 return AudioMapping ();
412 return _audio_stream->mapping;
416 FFmpegContent::set_filters (vector<Filter const *> const & filters)
419 boost::mutex::scoped_lock lm (_mutex);
423 signal_changed (FFmpegContentProperty::FILTERS);
427 FFmpegContent::set_audio_mapping (AudioMapping m)
429 audio_stream()->mapping = m;
430 AudioContent::set_audio_mapping (m);
434 FFmpegContent::identifier () const
438 s << VideoContent::identifier();
440 boost::mutex::scoped_lock lm (_mutex);
442 if (_subtitle_stream) {
443 s << "_" << _subtitle_stream->identifier ();
446 for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
447 s << "_" << (*i)->id ();
453 boost::filesystem::path
454 FFmpegContent::audio_analysis_path () const
456 shared_ptr<const Film> film = _film.lock ();
458 return boost::filesystem::path ();
461 /* We need to include the stream ID in this path so that we get different
462 analyses for each stream.
465 boost::filesystem::path p = film->audio_analysis_dir ();
466 string name = digest ();
467 if (audio_stream ()) {
468 name += "_" + audio_stream()->identifier ();