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, shared_ptr<const cxml::Node> 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 ()));
161 FFmpegContent::examine (shared_ptr<Job> job)
163 job->set_progress_unknown ();
165 Content::examine (job);
167 shared_ptr<const Film> film = _film.lock ();
170 shared_ptr<FFmpegExaminer> examiner (new FFmpegExaminer (shared_from_this ()));
172 VideoContent::Frame video_length = 0;
173 video_length = examiner->video_length ();
174 film->log()->log (String::compose ("Video length obtained from header as %1 frames", video_length));
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 take_from_video_examiner (examiner);
196 signal_changed (ContentProperty::LENGTH);
197 signal_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
198 signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
199 signal_changed (FFmpegContentProperty::AUDIO_STREAMS);
200 signal_changed (FFmpegContentProperty::AUDIO_STREAM);
201 signal_changed (AudioContentProperty::AUDIO_CHANNELS);
205 FFmpegContent::summary () const
207 /* Get the string() here so that the name does not have quotes around it */
208 return String::compose (_("%1 [movie]"), path_summary ());
212 FFmpegContent::technical_summary () const
216 as = _audio_stream->technical_summary ();
220 if (_subtitle_stream) {
221 ss = _subtitle_stream->technical_summary ();
224 string filt = Filter::ffmpeg_string (_filters);
226 return Content::technical_summary() + " - "
227 + VideoContent::technical_summary() + " - "
228 + AudioContent::technical_summary() + " - "
230 "ffmpeg: audio %1, subtitle %2, filters %3", as, ss, filt
235 FFmpegContent::information () const
237 if (video_length() == 0 || video_frame_rate() == 0) {
243 s << String::compose (_("%1 frames; %2 frames per second"), video_length_after_3d_combine(), video_frame_rate()) << "\n";
244 s << VideoContent::information ();
250 FFmpegContent::set_subtitle_stream (shared_ptr<FFmpegSubtitleStream> s)
253 boost::mutex::scoped_lock lm (_mutex);
254 _subtitle_stream = s;
257 signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
261 FFmpegContent::set_audio_stream (shared_ptr<FFmpegAudioStream> s)
264 boost::mutex::scoped_lock lm (_mutex);
268 signal_changed (FFmpegContentProperty::AUDIO_STREAM);
272 FFmpegContent::audio_length () const
274 int const cafr = content_audio_frame_rate ();
275 float const vfr = video_frame_rate ();
276 VideoContent::Frame const vl = video_length_after_3d_combine ();
278 boost::mutex::scoped_lock lm (_mutex);
279 if (!_audio_stream) {
283 return video_frames_to_audio_frames (vl, cafr, vfr);
287 FFmpegContent::audio_channels () const
289 boost::mutex::scoped_lock lm (_mutex);
291 if (!_audio_stream) {
295 return _audio_stream->channels;
299 FFmpegContent::content_audio_frame_rate () const
301 boost::mutex::scoped_lock lm (_mutex);
303 if (!_audio_stream) {
307 return _audio_stream->frame_rate;
311 FFmpegContent::output_audio_frame_rate () const
313 shared_ptr<const Film> film = _film.lock ();
316 /* Resample to a DCI-approved sample rate */
317 double t = dcp_audio_frame_rate (content_audio_frame_rate ());
319 FrameRateConversion frc (video_frame_rate(), film->video_frame_rate());
321 /* Compensate if the DCP is being run at a different frame rate
322 to the source; that is, if the video is run such that it will
323 look different in the DCP compared to the source (slower or faster).
324 skip/repeat doesn't come into effect here.
327 if (frc.change_speed) {
328 t *= video_frame_rate() * frc.factor() / film->video_frame_rate();
335 operator== (FFmpegStream const & a, FFmpegStream const & b)
337 return a._id == b._id;
341 operator!= (FFmpegStream const & a, FFmpegStream const & b)
343 return a._id != b._id;
346 FFmpegStream::FFmpegStream (shared_ptr<const cxml::Node> node)
347 : name (node->string_child ("Name"))
348 , _id (node->number_child<int> ("Id"))
354 FFmpegStream::as_xml (xmlpp::Node* root) const
356 root->add_child("Name")->add_child_text (name);
357 root->add_child("Id")->add_child_text (raw_convert<string> (_id));
360 FFmpegAudioStream::FFmpegAudioStream (shared_ptr<const cxml::Node> node, int version)
361 : FFmpegStream (node)
362 , mapping (node->node_child ("Mapping"), version)
364 frame_rate = node->number_child<int> ("FrameRate");
365 channels = node->number_child<int64_t> ("Channels");
366 first_audio = node->optional_number_child<double> ("FirstAudio");
370 FFmpegAudioStream::as_xml (xmlpp::Node* root) const
372 FFmpegStream::as_xml (root);
373 root->add_child("FrameRate")->add_child_text (raw_convert<string> (frame_rate));
374 root->add_child("Channels")->add_child_text (raw_convert<string> (channels));
376 root->add_child("FirstAudio")->add_child_text (raw_convert<string> (first_audio.get ()));
378 mapping.as_xml (root->add_child("Mapping"));
382 FFmpegStream::uses_index (AVFormatContext const * fc, int index) const
385 while (i < fc->nb_streams) {
386 if (fc->streams[i]->id == _id) {
387 return int (i) == index;
396 FFmpegStream::stream (AVFormatContext const * fc) const
399 while (i < fc->nb_streams) {
400 if (fc->streams[i]->id == _id) {
401 return fc->streams[i];
410 /** Construct a SubtitleStream from a value returned from to_string().
411 * @param t String returned from to_string().
412 * @param v State file version.
414 FFmpegSubtitleStream::FFmpegSubtitleStream (shared_ptr<const cxml::Node> node)
415 : FFmpegStream (node)
421 FFmpegSubtitleStream::as_xml (xmlpp::Node* root) const
423 FFmpegStream::as_xml (root);
427 FFmpegContent::full_length () const
429 shared_ptr<const Film> film = _film.lock ();
432 FrameRateConversion frc (video_frame_rate (), film->video_frame_rate ());
433 return video_length_after_3d_combine() * frc.factor() * TIME_HZ / film->video_frame_rate ();
437 FFmpegContent::audio_mapping () const
439 boost::mutex::scoped_lock lm (_mutex);
441 if (!_audio_stream) {
442 return AudioMapping ();
445 return _audio_stream->mapping;
449 FFmpegContent::set_filters (vector<Filter const *> const & filters)
452 boost::mutex::scoped_lock lm (_mutex);
456 signal_changed (FFmpegContentProperty::FILTERS);
460 FFmpegContent::set_audio_mapping (AudioMapping m)
462 audio_stream()->mapping = m;
463 signal_changed (AudioContentProperty::AUDIO_MAPPING);
467 FFmpegContent::identifier () const
471 s << VideoContent::identifier();
473 boost::mutex::scoped_lock lm (_mutex);
475 if (_subtitle_stream) {
476 s << "_" << _subtitle_stream->identifier ();
479 for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
480 s << "_" << (*i)->id ();
486 boost::filesystem::path
487 FFmpegContent::audio_analysis_path () const
489 shared_ptr<const Film> film = _film.lock ();
491 return boost::filesystem::path ();
494 /* We need to include the stream ID in this path so that we get different
495 analyses for each stream.
498 boost::filesystem::path p = film->audio_analysis_dir ();
499 string name = digest ();
500 if (audio_stream ()) {
501 name += "_" + audio_stream()->identifier ();