2 Copyright (C) 2013 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.
20 #include <libcxml/cxml.h>
21 #include "ffmpeg_content.h"
22 #include "ffmpeg_examiner.h"
23 #include "compose.hpp"
29 #include "exceptions.h"
34 using std::stringstream;
39 using boost::shared_ptr;
40 using boost::lexical_cast;
41 using boost::dynamic_pointer_cast;
43 int const FFmpegContentProperty::SUBTITLE_STREAMS = 100;
44 int const FFmpegContentProperty::SUBTITLE_STREAM = 101;
45 int const FFmpegContentProperty::AUDIO_STREAMS = 102;
46 int const FFmpegContentProperty::AUDIO_STREAM = 103;
47 int const FFmpegContentProperty::FILTERS = 104;
49 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, boost::filesystem::path p)
53 , SubtitleContent (f, p)
58 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node)
60 , VideoContent (f, node)
61 , AudioContent (f, node)
62 , SubtitleContent (f, node)
64 list<cxml::NodePtr> c = node->node_children ("SubtitleStream");
65 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
66 _subtitle_streams.push_back (shared_ptr<FFmpegSubtitleStream> (new FFmpegSubtitleStream (*i)));
67 if ((*i)->optional_number_child<int> ("Selected")) {
68 _subtitle_stream = _subtitle_streams.back ();
72 c = node->node_children ("AudioStream");
73 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
74 _audio_streams.push_back (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream (*i)));
75 if ((*i)->optional_number_child<int> ("Selected")) {
76 _audio_stream = _audio_streams.back ();
80 c = node->node_children ("Filter");
81 for (list<cxml::NodePtr>::iterator i = c.begin(); i != c.end(); ++i) {
82 _filters.push_back (Filter::from_id ((*i)->content ()));
85 _first_video = node->optional_number_child<double> ("FirstVideo");
88 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, vector<boost::shared_ptr<Content> > c)
92 , SubtitleContent (f, c)
94 shared_ptr<FFmpegContent> ref = dynamic_pointer_cast<FFmpegContent> (c[0]);
97 for (size_t i = 0; i < c.size(); ++i) {
98 shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (c[i]);
99 if (*(fc->_subtitle_stream.get()) != *(ref->_subtitle_stream.get())) {
100 throw JoinError (_("Content to be joined must use the same subtitle stream."));
103 if (*(fc->_audio_stream.get()) != *(ref->_audio_stream.get())) {
104 throw JoinError (_("Content to be joined must use the same audio stream."));
108 _subtitle_streams = ref->subtitle_streams ();
109 _subtitle_stream = ref->subtitle_stream ();
110 _audio_streams = ref->audio_streams ();
111 _audio_stream = ref->audio_stream ();
112 _first_video = ref->_first_video;
116 FFmpegContent::as_xml (xmlpp::Node* node) const
118 node->add_child("Type")->add_child_text ("FFmpeg");
119 Content::as_xml (node);
120 VideoContent::as_xml (node);
121 AudioContent::as_xml (node);
122 SubtitleContent::as_xml (node);
124 boost::mutex::scoped_lock lm (_mutex);
126 for (vector<shared_ptr<FFmpegSubtitleStream> >::const_iterator i = _subtitle_streams.begin(); i != _subtitle_streams.end(); ++i) {
127 xmlpp::Node* t = node->add_child("SubtitleStream");
128 if (_subtitle_stream && *i == _subtitle_stream) {
129 t->add_child("Selected")->add_child_text("1");
134 for (vector<shared_ptr<FFmpegAudioStream> >::const_iterator i = _audio_streams.begin(); i != _audio_streams.end(); ++i) {
135 xmlpp::Node* t = node->add_child("AudioStream");
136 if (_audio_stream && *i == _audio_stream) {
137 t->add_child("Selected")->add_child_text("1");
142 for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
143 node->add_child("Filter")->add_child_text ((*i)->id ());
147 node->add_child("FirstVideo")->add_child_text (lexical_cast<string> (_first_video.get ()));
152 FFmpegContent::examine (shared_ptr<Job> job)
154 job->set_progress_unknown ();
156 Content::examine (job);
158 shared_ptr<const Film> film = _film.lock ();
161 shared_ptr<FFmpegExaminer> examiner (new FFmpegExaminer (shared_from_this ()));
163 VideoContent::Frame video_length = 0;
164 video_length = examiner->video_length ();
165 film->log()->log (String::compose ("Video length obtained from header as %1 frames", video_length));
168 boost::mutex::scoped_lock lm (_mutex);
170 _video_length = video_length;
172 _subtitle_streams = examiner->subtitle_streams ();
173 if (!_subtitle_streams.empty ()) {
174 _subtitle_stream = _subtitle_streams.front ();
177 _audio_streams = examiner->audio_streams ();
178 if (!_audio_streams.empty ()) {
179 _audio_stream = _audio_streams.front ();
182 _first_video = examiner->first_video ();
185 take_from_video_examiner (examiner);
187 signal_changed (ContentProperty::LENGTH);
188 signal_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
189 signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
190 signal_changed (FFmpegContentProperty::AUDIO_STREAMS);
191 signal_changed (FFmpegContentProperty::AUDIO_STREAM);
192 signal_changed (AudioContentProperty::AUDIO_CHANNELS);
196 FFmpegContent::summary () const
198 /* Get the string() here so that the name does not have quotes around it */
199 return String::compose (_("%1 [movie]"), path_summary ());
203 FFmpegContent::technical_summary () const
207 as = String::compose ("id %1", _audio_stream->id);
211 if (_subtitle_stream) {
212 ss = String::compose ("id %1", _subtitle_stream->id);
215 pair<string, string> filt = Filter::ffmpeg_strings (_filters);
217 return Content::technical_summary() + " - "
218 + VideoContent::technical_summary() + " - "
219 + AudioContent::technical_summary() + " - "
221 "ffmpeg: audio %1, subtitle %2, filters %3 %4", as, ss, filt.first, filt.second
226 FFmpegContent::information () const
228 if (video_length() == 0 || video_frame_rate() == 0) {
234 s << String::compose (_("%1 frames; %2 frames per second"), video_length(), video_frame_rate()) << "\n";
235 s << VideoContent::information ();
241 FFmpegContent::set_subtitle_stream (shared_ptr<FFmpegSubtitleStream> s)
244 boost::mutex::scoped_lock lm (_mutex);
245 _subtitle_stream = s;
248 signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
252 FFmpegContent::set_audio_stream (shared_ptr<FFmpegAudioStream> s)
255 boost::mutex::scoped_lock lm (_mutex);
259 signal_changed (FFmpegContentProperty::AUDIO_STREAM);
263 FFmpegContent::audio_length () const
265 int const cafr = content_audio_frame_rate ();
266 int const vfr = video_frame_rate ();
267 VideoContent::Frame const vl = video_length ();
269 boost::mutex::scoped_lock lm (_mutex);
270 if (!_audio_stream) {
274 return video_frames_to_audio_frames (vl, cafr, vfr);
278 FFmpegContent::audio_channels () const
280 boost::mutex::scoped_lock lm (_mutex);
282 if (!_audio_stream) {
286 return _audio_stream->channels;
290 FFmpegContent::content_audio_frame_rate () const
292 boost::mutex::scoped_lock lm (_mutex);
294 if (!_audio_stream) {
298 return _audio_stream->frame_rate;
302 FFmpegContent::output_audio_frame_rate () const
304 shared_ptr<const Film> film = _film.lock ();
307 /* Resample to a DCI-approved sample rate */
308 double t = dcp_audio_frame_rate (content_audio_frame_rate ());
310 FrameRateConversion frc (video_frame_rate(), film->video_frame_rate());
312 /* Compensate if the DCP is being run at a different frame rate
313 to the source; that is, if the video is run such that it will
314 look different in the DCP compared to the source (slower or faster).
315 skip/repeat doesn't come into effect here.
318 if (frc.change_speed) {
319 t *= video_frame_rate() * frc.factor() / film->video_frame_rate();
326 operator== (FFmpegSubtitleStream const & a, FFmpegSubtitleStream const & b)
332 operator!= (FFmpegSubtitleStream const & a, FFmpegSubtitleStream const & b)
338 operator== (FFmpegAudioStream const & a, FFmpegAudioStream const & b)
344 operator!= (FFmpegAudioStream const & a, FFmpegAudioStream const & b)
349 FFmpegAudioStream::FFmpegAudioStream (shared_ptr<const cxml::Node> node)
350 : mapping (node->node_child ("Mapping"))
352 name = node->string_child ("Name");
353 id = node->number_child<int> ("Id");
354 frame_rate = node->number_child<int> ("FrameRate");
355 channels = node->number_child<int64_t> ("Channels");
356 first_audio = node->optional_number_child<double> ("FirstAudio");
360 FFmpegAudioStream::as_xml (xmlpp::Node* root) const
362 root->add_child("Name")->add_child_text (name);
363 root->add_child("Id")->add_child_text (lexical_cast<string> (id));
364 root->add_child("FrameRate")->add_child_text (lexical_cast<string> (frame_rate));
365 root->add_child("Channels")->add_child_text (lexical_cast<string> (channels));
367 root->add_child("FirstAudio")->add_child_text (lexical_cast<string> (first_audio.get ()));
369 mapping.as_xml (root->add_child("Mapping"));
372 /** Construct a SubtitleStream from a value returned from to_string().
373 * @param t String returned from to_string().
374 * @param v State file version.
376 FFmpegSubtitleStream::FFmpegSubtitleStream (shared_ptr<const cxml::Node> node)
378 name = node->string_child ("Name");
379 id = node->number_child<int> ("Id");
383 FFmpegSubtitleStream::as_xml (xmlpp::Node* root) const
385 root->add_child("Name")->add_child_text (name);
386 root->add_child("Id")->add_child_text (lexical_cast<string> (id));
390 FFmpegContent::full_length () const
392 shared_ptr<const Film> film = _film.lock ();
395 FrameRateConversion frc (video_frame_rate (), film->video_frame_rate ());
396 return video_length() * frc.factor() * TIME_HZ / film->video_frame_rate ();
400 FFmpegContent::audio_mapping () const
402 boost::mutex::scoped_lock lm (_mutex);
404 if (!_audio_stream) {
405 return AudioMapping ();
408 return _audio_stream->mapping;
412 FFmpegContent::set_filters (vector<Filter const *> const & filters)
415 boost::mutex::scoped_lock lm (_mutex);
419 signal_changed (FFmpegContentProperty::FILTERS);
423 FFmpegContent::set_audio_mapping (AudioMapping m)
425 audio_stream()->mapping = m;
426 signal_changed (AudioContentProperty::AUDIO_MAPPING);
430 FFmpegContent::identifier () const
434 s << VideoContent::identifier();
436 boost::mutex::scoped_lock lm (_mutex);
438 if (_subtitle_stream) {
439 s << "_" << _subtitle_stream->id;
442 for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
443 s << "_" << (*i)->id ();