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"
37 #define LOG_GENERAL(...) film->log()->log (String::compose (__VA_ARGS__), Log::TYPE_GENERAL);
40 using std::stringstream;
45 using boost::shared_ptr;
46 using boost::dynamic_pointer_cast;
47 using libdcp::raw_convert;
49 int const FFmpegContentProperty::SUBTITLE_STREAMS = 100;
50 int const FFmpegContentProperty::SUBTITLE_STREAM = 101;
51 int const FFmpegContentProperty::AUDIO_STREAMS = 102;
52 int const FFmpegContentProperty::AUDIO_STREAM = 103;
53 int const FFmpegContentProperty::FILTERS = 104;
55 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, boost::filesystem::path p)
59 , SubtitleContent (f, p)
64 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node, int version, list<string>& notes)
66 , VideoContent (f, node, version)
67 , AudioContent (f, node)
68 , SubtitleContent (f, node, version)
70 list<cxml::NodePtr> c = node->node_children ("SubtitleStream");
71 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
72 _subtitle_streams.push_back (shared_ptr<FFmpegSubtitleStream> (new FFmpegSubtitleStream (*i)));
73 if ((*i)->optional_number_child<int> ("Selected")) {
74 _subtitle_stream = _subtitle_streams.back ();
78 c = node->node_children ("AudioStream");
79 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
80 _audio_streams.push_back (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream (*i, version)));
81 if ((*i)->optional_number_child<int> ("Selected")) {
82 _audio_stream = _audio_streams.back ();
86 c = node->node_children ("Filter");
87 for (list<cxml::NodePtr>::iterator i = c.begin(); i != c.end(); ++i) {
88 Filter const * f = Filter::from_id ((*i)->content ());
90 _filters.push_back (f);
92 notes.push_back (String::compose (_("DCP-o-matic no longer supports the `%1' filter, so it has been turned off."), (*i)->content()));
96 _first_video = node->optional_number_child<double> ("FirstVideo");
99 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, vector<boost::shared_ptr<Content> > c)
101 , VideoContent (f, c)
102 , AudioContent (f, c)
103 , SubtitleContent (f, c)
105 shared_ptr<FFmpegContent> ref = dynamic_pointer_cast<FFmpegContent> (c[0]);
108 for (size_t i = 0; i < c.size(); ++i) {
109 shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (c[i]);
110 if (f->with_subtitles() && *(fc->_subtitle_stream.get()) != *(ref->_subtitle_stream.get())) {
111 throw JoinError (_("Content to be joined must use the same subtitle stream."));
114 if (*(fc->_audio_stream.get()) != *(ref->_audio_stream.get())) {
115 throw JoinError (_("Content to be joined must use the same audio stream."));
119 _subtitle_streams = ref->subtitle_streams ();
120 _subtitle_stream = ref->subtitle_stream ();
121 _audio_streams = ref->audio_streams ();
122 _audio_stream = ref->audio_stream ();
123 _first_video = ref->_first_video;
127 FFmpegContent::as_xml (xmlpp::Node* node) const
129 node->add_child("Type")->add_child_text ("FFmpeg");
130 Content::as_xml (node);
131 VideoContent::as_xml (node);
132 AudioContent::as_xml (node);
133 SubtitleContent::as_xml (node);
135 boost::mutex::scoped_lock lm (_mutex);
137 for (vector<shared_ptr<FFmpegSubtitleStream> >::const_iterator i = _subtitle_streams.begin(); i != _subtitle_streams.end(); ++i) {
138 xmlpp::Node* t = node->add_child("SubtitleStream");
139 if (_subtitle_stream && *i == _subtitle_stream) {
140 t->add_child("Selected")->add_child_text("1");
145 for (vector<shared_ptr<FFmpegAudioStream> >::const_iterator i = _audio_streams.begin(); i != _audio_streams.end(); ++i) {
146 xmlpp::Node* t = node->add_child("AudioStream");
147 if (_audio_stream && *i == _audio_stream) {
148 t->add_child("Selected")->add_child_text("1");
153 for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
154 node->add_child("Filter")->add_child_text ((*i)->id ());
158 node->add_child("FirstVideo")->add_child_text (raw_convert<string> (_first_video.get ()));
163 FFmpegContent::examine (shared_ptr<Job> job)
165 job->set_progress_unknown ();
167 Content::examine (job);
169 shared_ptr<const Film> film = _film.lock ();
172 shared_ptr<FFmpegExaminer> examiner (new FFmpegExaminer (shared_from_this ()));
174 VideoContent::Frame video_length = 0;
175 video_length = examiner->video_length ();
176 LOG_GENERAL ("Video length obtained from header as %1 frames", video_length);
179 boost::mutex::scoped_lock lm (_mutex);
181 _video_length = video_length;
183 _subtitle_streams = examiner->subtitle_streams ();
184 if (!_subtitle_streams.empty ()) {
185 _subtitle_stream = _subtitle_streams.front ();
188 _audio_streams = examiner->audio_streams ();
189 if (!_audio_streams.empty ()) {
190 _audio_stream = _audio_streams.front ();
193 _first_video = examiner->first_video ();
196 take_from_video_examiner (examiner);
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() == 0 || video_frame_rate() == 0) {
245 s << String::compose (_("%1 frames; %2 frames per second"), video_length_after_3d_combine(), 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 int const cafr = content_audio_frame_rate ();
277 float const vfr = video_frame_rate ();
278 VideoContent::Frame const vl = video_length_after_3d_combine ();
280 boost::mutex::scoped_lock lm (_mutex);
281 if (!_audio_stream) {
285 return video_frames_to_audio_frames (vl, cafr, vfr);
289 FFmpegContent::audio_channels () const
291 boost::mutex::scoped_lock lm (_mutex);
293 if (!_audio_stream) {
297 return _audio_stream->channels;
301 FFmpegContent::content_audio_frame_rate () const
303 boost::mutex::scoped_lock lm (_mutex);
305 if (!_audio_stream) {
309 return _audio_stream->frame_rate;
313 FFmpegContent::output_audio_frame_rate () const
315 shared_ptr<const Film> film = _film.lock ();
318 /* Resample to a DCI-approved sample rate */
319 double t = dcp_audio_frame_rate (content_audio_frame_rate ());
321 FrameRateConversion frc (video_frame_rate(), film->video_frame_rate());
323 /* Compensate if the DCP is being run at a different frame rate
324 to the source; that is, if the video is run such that it will
325 look different in the DCP compared to the source (slower or faster).
326 skip/repeat doesn't come into effect here.
329 if (frc.change_speed) {
330 t *= video_frame_rate() * frc.factor() / film->video_frame_rate();
337 operator== (FFmpegStream const & a, FFmpegStream const & b)
339 return a._id == b._id;
343 operator!= (FFmpegStream const & a, FFmpegStream const & b)
345 return a._id != b._id;
348 FFmpegStream::FFmpegStream (shared_ptr<const cxml::Node> node)
349 : name (node->string_child ("Name"))
350 , _id (node->number_child<int> ("Id"))
356 FFmpegStream::as_xml (xmlpp::Node* root) const
358 root->add_child("Name")->add_child_text (name);
359 root->add_child("Id")->add_child_text (raw_convert<string> (_id));
362 FFmpegAudioStream::FFmpegAudioStream (shared_ptr<const cxml::Node> node, int version)
363 : FFmpegStream (node)
364 , mapping (node->node_child ("Mapping"), version)
366 frame_rate = node->number_child<int> ("FrameRate");
367 channels = node->number_child<int64_t> ("Channels");
368 first_audio = node->optional_number_child<double> ("FirstAudio");
372 FFmpegAudioStream::as_xml (xmlpp::Node* root) const
374 FFmpegStream::as_xml (root);
375 root->add_child("FrameRate")->add_child_text (raw_convert<string> (frame_rate));
376 root->add_child("Channels")->add_child_text (raw_convert<string> (channels));
378 root->add_child("FirstAudio")->add_child_text (raw_convert<string> (first_audio.get ()));
380 mapping.as_xml (root->add_child("Mapping"));
384 FFmpegStream::uses_index (AVFormatContext const * fc, int index) const
387 while (i < fc->nb_streams) {
388 if (fc->streams[i]->id == _id) {
389 return int (i) == index;
398 FFmpegStream::stream (AVFormatContext const * fc) const
401 while (i < fc->nb_streams) {
402 if (fc->streams[i]->id == _id) {
403 return fc->streams[i];
412 /** Construct a SubtitleStream from a value returned from to_string().
413 * @param t String returned from to_string().
414 * @param v State file version.
416 FFmpegSubtitleStream::FFmpegSubtitleStream (shared_ptr<const cxml::Node> node)
417 : FFmpegStream (node)
423 FFmpegSubtitleStream::as_xml (xmlpp::Node* root) const
425 FFmpegStream::as_xml (root);
429 FFmpegContent::full_length () const
431 shared_ptr<const Film> film = _film.lock ();
434 FrameRateConversion frc (video_frame_rate (), film->video_frame_rate ());
435 return video_length_after_3d_combine() * frc.factor() * TIME_HZ / film->video_frame_rate ();
439 FFmpegContent::audio_mapping () const
441 boost::mutex::scoped_lock lm (_mutex);
443 if (!_audio_stream) {
444 return AudioMapping ();
447 return _audio_stream->mapping;
451 FFmpegContent::set_filters (vector<Filter const *> const & filters)
454 boost::mutex::scoped_lock lm (_mutex);
458 signal_changed (FFmpegContentProperty::FILTERS);
462 FFmpegContent::set_audio_mapping (AudioMapping m)
464 audio_stream()->mapping = m;
465 signal_changed (AudioContentProperty::AUDIO_MAPPING);
469 FFmpegContent::identifier () const
473 s << VideoContent::identifier();
475 boost::mutex::scoped_lock lm (_mutex);
477 if (_subtitle_stream) {
478 s << "_" << _subtitle_stream->identifier ();
481 for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
482 s << "_" << (*i)->id ();
488 boost::filesystem::path
489 FFmpegContent::audio_analysis_path () const
491 shared_ptr<const Film> film = _film.lock ();
493 return boost::filesystem::path ();
496 /* We need to include the stream ID in this path so that we get different
497 analyses for each stream.
500 boost::filesystem::path p = film->audio_analysis_dir ();
501 string name = digest ();
502 if (audio_stream ()) {
503 name += "_" + audio_stream()->identifier ();