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)
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 _filters.push_back (Filter::from_id ((*i)->content ()));
88 _first_video = node->optional_number_child<double> ("FirstVideo");
91 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, vector<boost::shared_ptr<Content> > c)
95 , SubtitleContent (f, c)
97 shared_ptr<FFmpegContent> ref = dynamic_pointer_cast<FFmpegContent> (c[0]);
100 for (size_t i = 0; i < c.size(); ++i) {
101 shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (c[i]);
102 if (f->with_subtitles() && *(fc->_subtitle_stream.get()) != *(ref->_subtitle_stream.get())) {
103 throw JoinError (_("Content to be joined must use the same subtitle stream."));
106 if (*(fc->_audio_stream.get()) != *(ref->_audio_stream.get())) {
107 throw JoinError (_("Content to be joined must use the same audio stream."));
111 _subtitle_streams = ref->subtitle_streams ();
112 _subtitle_stream = ref->subtitle_stream ();
113 _audio_streams = ref->audio_streams ();
114 _audio_stream = ref->audio_stream ();
115 _first_video = ref->_first_video;
119 FFmpegContent::as_xml (xmlpp::Node* node) const
121 node->add_child("Type")->add_child_text ("FFmpeg");
122 Content::as_xml (node);
123 VideoContent::as_xml (node);
124 AudioContent::as_xml (node);
125 SubtitleContent::as_xml (node);
127 boost::mutex::scoped_lock lm (_mutex);
129 for (vector<shared_ptr<FFmpegSubtitleStream> >::const_iterator i = _subtitle_streams.begin(); i != _subtitle_streams.end(); ++i) {
130 xmlpp::Node* t = node->add_child("SubtitleStream");
131 if (_subtitle_stream && *i == _subtitle_stream) {
132 t->add_child("Selected")->add_child_text("1");
137 for (vector<shared_ptr<FFmpegAudioStream> >::const_iterator i = _audio_streams.begin(); i != _audio_streams.end(); ++i) {
138 xmlpp::Node* t = node->add_child("AudioStream");
139 if (_audio_stream && *i == _audio_stream) {
140 t->add_child("Selected")->add_child_text("1");
145 for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
146 node->add_child("Filter")->add_child_text ((*i)->id ());
150 node->add_child("FirstVideo")->add_child_text (lexical_cast<string> (_first_video.get().get()));
155 FFmpegContent::examine (shared_ptr<Job> job)
157 job->set_progress_unknown ();
159 Content::examine (job);
161 shared_ptr<FFmpegExaminer> examiner (new FFmpegExaminer (shared_from_this ()));
162 take_from_video_examiner (examiner);
164 ContentTime video_length = examiner->video_length ();
166 shared_ptr<const Film> film = _film.lock ();
168 film->log()->log (String::compose ("Video length obtained from header as %1 frames", video_length.frames (video_frame_rate ())));
171 boost::mutex::scoped_lock lm (_mutex);
173 _video_length = video_length;
175 _subtitle_streams = examiner->subtitle_streams ();
176 if (!_subtitle_streams.empty ()) {
177 _subtitle_stream = _subtitle_streams.front ();
180 _audio_streams = examiner->audio_streams ();
181 if (!_audio_streams.empty ()) {
182 _audio_stream = _audio_streams.front ();
185 _first_video = examiner->first_video ();
188 signal_changed (ContentProperty::LENGTH);
189 signal_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
190 signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
191 signal_changed (FFmpegContentProperty::AUDIO_STREAMS);
192 signal_changed (FFmpegContentProperty::AUDIO_STREAM);
193 signal_changed (AudioContentProperty::AUDIO_CHANNELS);
197 FFmpegContent::summary () const
199 /* Get the string() here so that the name does not have quotes around it */
200 return String::compose (_("%1 [movie]"), path_summary ());
204 FFmpegContent::technical_summary () const
208 as = _audio_stream->technical_summary ();
212 if (_subtitle_stream) {
213 ss = _subtitle_stream->technical_summary ();
216 pair<string, string> filt = Filter::ffmpeg_strings (_filters);
218 return Content::technical_summary() + " - "
219 + VideoContent::technical_summary() + " - "
220 + AudioContent::technical_summary() + " - "
222 "ffmpeg: audio %1, subtitle %2, filters %3 %4", as, ss, filt.first, filt.second
227 FFmpegContent::information () const
229 if (video_length() == ContentTime (0) || video_frame_rate() == 0) {
235 s << String::compose (_("%1 frames; %2 frames per second"), video_length().frames (video_frame_rate()), video_frame_rate()) << "\n";
236 s << VideoContent::information ();
242 FFmpegContent::set_subtitle_stream (shared_ptr<FFmpegSubtitleStream> s)
245 boost::mutex::scoped_lock lm (_mutex);
246 _subtitle_stream = s;
249 signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
253 FFmpegContent::set_audio_stream (shared_ptr<FFmpegAudioStream> s)
256 boost::mutex::scoped_lock lm (_mutex);
260 signal_changed (FFmpegContentProperty::AUDIO_STREAM);
264 FFmpegContent::audio_length () const
267 boost::mutex::scoped_lock lm (_mutex);
268 if (!_audio_stream) {
269 return ContentTime ();
273 return video_length();
277 FFmpegContent::audio_channels () const
279 boost::mutex::scoped_lock lm (_mutex);
281 if (!_audio_stream) {
285 return _audio_stream->channels;
289 FFmpegContent::content_audio_frame_rate () const
291 boost::mutex::scoped_lock lm (_mutex);
293 if (!_audio_stream) {
297 return _audio_stream->frame_rate;
301 FFmpegContent::output_audio_frame_rate () const
303 shared_ptr<const Film> film = _film.lock ();
306 /* Resample to a DCI-approved sample rate */
307 double t = dcp_audio_frame_rate (content_audio_frame_rate ());
309 FrameRateChange frc (video_frame_rate(), film->video_frame_rate());
311 /* Compensate if the DCP is being run at a different frame rate
312 to the source; that is, if the video is run such that it will
313 look different in the DCP compared to the source (slower or faster).
316 if (frc.change_speed) {
324 operator== (FFmpegStream const & a, FFmpegStream const & b)
326 return a._id == b._id;
330 operator!= (FFmpegStream const & a, FFmpegStream const & b)
332 return a._id != b._id;
335 FFmpegStream::FFmpegStream (shared_ptr<const cxml::Node> node)
336 : name (node->string_child ("Name"))
337 , _id (node->number_child<int> ("Id"))
343 FFmpegStream::as_xml (xmlpp::Node* root) const
345 root->add_child("Name")->add_child_text (name);
346 root->add_child("Id")->add_child_text (lexical_cast<string> (_id));
349 FFmpegAudioStream::FFmpegAudioStream (shared_ptr<const cxml::Node> node, int version)
350 : FFmpegStream (node)
351 , mapping (node->node_child ("Mapping"), version)
353 frame_rate = node->number_child<int> ("FrameRate");
354 channels = node->number_child<int64_t> ("Channels");
355 first_audio = node->optional_number_child<double> ("FirstAudio");
359 FFmpegAudioStream::as_xml (xmlpp::Node* root) const
361 FFmpegStream::as_xml (root);
362 root->add_child("FrameRate")->add_child_text (lexical_cast<string> (frame_rate));
363 root->add_child("Channels")->add_child_text (lexical_cast<string> (channels));
365 root->add_child("FirstAudio")->add_child_text (lexical_cast<string> (first_audio.get().get()));
367 mapping.as_xml (root->add_child("Mapping"));
371 FFmpegStream::uses_index (AVFormatContext const * fc, int index) const
374 while (i < fc->nb_streams) {
375 if (fc->streams[i]->id == _id) {
376 return int (i) == index;
385 FFmpegStream::stream (AVFormatContext const * fc) const
388 while (i < fc->nb_streams) {
389 if (fc->streams[i]->id == _id) {
390 return fc->streams[i];
399 /** Construct a SubtitleStream from a value returned from to_string().
400 * @param t String returned from to_string().
401 * @param v State file version.
403 FFmpegSubtitleStream::FFmpegSubtitleStream (shared_ptr<const cxml::Node> node)
404 : FFmpegStream (node)
410 FFmpegSubtitleStream::as_xml (xmlpp::Node* root) const
412 FFmpegStream::as_xml (root);
416 FFmpegContent::full_length () const
418 shared_ptr<const Film> film = _film.lock ();
420 return DCPTime (video_length(), FrameRateChange (video_frame_rate (), film->video_frame_rate ()));
424 FFmpegContent::audio_mapping () const
426 boost::mutex::scoped_lock lm (_mutex);
428 if (!_audio_stream) {
429 return AudioMapping ();
432 return _audio_stream->mapping;
436 FFmpegContent::set_filters (vector<Filter const *> const & filters)
439 boost::mutex::scoped_lock lm (_mutex);
443 signal_changed (FFmpegContentProperty::FILTERS);
447 FFmpegContent::set_audio_mapping (AudioMapping m)
449 audio_stream()->mapping = m;
450 signal_changed (AudioContentProperty::AUDIO_MAPPING);
454 FFmpegContent::identifier () const
458 s << VideoContent::identifier();
460 boost::mutex::scoped_lock lm (_mutex);
462 if (_subtitle_stream) {
463 s << "_" << _subtitle_stream->identifier ();
466 for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
467 s << "_" << (*i)->id ();