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"
34 #include "frame_rate_change.h"
38 #define LOG_GENERAL(...) film->log()->log (String::compose (__VA_ARGS__), Log::TYPE_GENERAL);
41 using std::stringstream;
46 using boost::shared_ptr;
47 using boost::dynamic_pointer_cast;
48 using libdcp::raw_convert;
50 int const FFmpegContentProperty::SUBTITLE_STREAMS = 100;
51 int const FFmpegContentProperty::SUBTITLE_STREAM = 101;
52 int const FFmpegContentProperty::AUDIO_STREAMS = 102;
53 int const FFmpegContentProperty::AUDIO_STREAM = 103;
54 int const FFmpegContentProperty::FILTERS = 104;
56 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, boost::filesystem::path p)
60 , SubtitleContent (f, p)
65 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node, int version, list<string>& notes)
67 , VideoContent (f, node, version)
68 , AudioContent (f, node)
69 , SubtitleContent (f, node, version)
71 list<cxml::NodePtr> c = node->node_children ("SubtitleStream");
72 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
73 _subtitle_streams.push_back (shared_ptr<FFmpegSubtitleStream> (new FFmpegSubtitleStream (*i)));
74 if ((*i)->optional_number_child<int> ("Selected")) {
75 _subtitle_stream = _subtitle_streams.back ();
79 c = node->node_children ("AudioStream");
80 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
81 _audio_streams.push_back (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream (*i, version)));
82 if ((*i)->optional_number_child<int> ("Selected")) {
83 _audio_stream = _audio_streams.back ();
87 c = node->node_children ("Filter");
88 for (list<cxml::NodePtr>::iterator i = c.begin(); i != c.end(); ++i) {
89 Filter const * f = Filter::from_id ((*i)->content ());
91 _filters.push_back (f);
93 notes.push_back (String::compose (_("DCP-o-matic no longer supports the `%1' filter, so it has been turned off."), (*i)->content()));
97 _first_video = node->optional_number_child<double> ("FirstVideo");
100 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, vector<boost::shared_ptr<Content> > c)
102 , VideoContent (f, c)
103 , AudioContent (f, c)
104 , SubtitleContent (f, c)
106 shared_ptr<FFmpegContent> ref = dynamic_pointer_cast<FFmpegContent> (c[0]);
109 for (size_t i = 0; i < c.size(); ++i) {
110 shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (c[i]);
111 if (f->with_subtitles() && *(fc->_subtitle_stream.get()) != *(ref->_subtitle_stream.get())) {
112 throw JoinError (_("Content to be joined must use the same subtitle stream."));
115 if (*(fc->_audio_stream.get()) != *(ref->_audio_stream.get())) {
116 throw JoinError (_("Content to be joined must use the same audio stream."));
120 _subtitle_streams = ref->subtitle_streams ();
121 _subtitle_stream = ref->subtitle_stream ();
122 _audio_streams = ref->audio_streams ();
123 _audio_stream = ref->audio_stream ();
124 _first_video = ref->_first_video;
128 FFmpegContent::as_xml (xmlpp::Node* node) const
130 node->add_child("Type")->add_child_text ("FFmpeg");
131 Content::as_xml (node);
132 VideoContent::as_xml (node);
133 AudioContent::as_xml (node);
134 SubtitleContent::as_xml (node);
136 boost::mutex::scoped_lock lm (_mutex);
138 for (vector<shared_ptr<FFmpegSubtitleStream> >::const_iterator i = _subtitle_streams.begin(); i != _subtitle_streams.end(); ++i) {
139 xmlpp::Node* t = node->add_child("SubtitleStream");
140 if (_subtitle_stream && *i == _subtitle_stream) {
141 t->add_child("Selected")->add_child_text("1");
146 for (vector<shared_ptr<FFmpegAudioStream> >::const_iterator i = _audio_streams.begin(); i != _audio_streams.end(); ++i) {
147 xmlpp::Node* t = node->add_child("AudioStream");
148 if (_audio_stream && *i == _audio_stream) {
149 t->add_child("Selected")->add_child_text("1");
154 for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
155 node->add_child("Filter")->add_child_text ((*i)->id ());
159 node->add_child("FirstVideo")->add_child_text (raw_convert<string> (_first_video.get ()));
164 FFmpegContent::examine (shared_ptr<Job> job)
166 job->set_progress_unknown ();
168 Content::examine (job);
170 shared_ptr<const Film> film = _film.lock ();
173 shared_ptr<FFmpegExaminer> examiner (new FFmpegExaminer (shared_from_this ()));
175 VideoContent::Frame video_length = 0;
176 video_length = examiner->video_length ();
177 LOG_GENERAL ("Video length obtained from header as %1 frames", video_length);
180 boost::mutex::scoped_lock lm (_mutex);
182 _video_length = video_length;
184 _subtitle_streams = examiner->subtitle_streams ();
185 if (!_subtitle_streams.empty ()) {
186 _subtitle_stream = _subtitle_streams.front ();
189 _audio_streams = examiner->audio_streams ();
190 if (!_audio_streams.empty ()) {
191 _audio_stream = _audio_streams.front ();
194 _first_video = examiner->first_video ();
197 take_from_video_examiner (examiner);
199 signal_changed (ContentProperty::LENGTH);
200 signal_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
201 signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
202 signal_changed (FFmpegContentProperty::AUDIO_STREAMS);
203 signal_changed (FFmpegContentProperty::AUDIO_STREAM);
204 signal_changed (AudioContentProperty::AUDIO_CHANNELS);
208 FFmpegContent::summary () const
210 /* Get the string() here so that the name does not have quotes around it */
211 return String::compose (_("%1 [movie]"), path_summary ());
215 FFmpegContent::technical_summary () const
219 as = _audio_stream->technical_summary ();
223 if (_subtitle_stream) {
224 ss = _subtitle_stream->technical_summary ();
227 string filt = Filter::ffmpeg_string (_filters);
229 return Content::technical_summary() + " - "
230 + VideoContent::technical_summary() + " - "
231 + AudioContent::technical_summary() + " - "
233 "ffmpeg: audio %1, subtitle %2, filters %3", as, ss, filt
238 FFmpegContent::information () const
240 if (video_length() == 0 || video_frame_rate() == 0) {
246 s << String::compose (_("%1 frames; %2 frames per second"), video_length_after_3d_combine(), video_frame_rate()) << "\n";
247 s << VideoContent::information ();
253 FFmpegContent::set_subtitle_stream (shared_ptr<FFmpegSubtitleStream> s)
256 boost::mutex::scoped_lock lm (_mutex);
257 _subtitle_stream = s;
260 signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
264 FFmpegContent::set_audio_stream (shared_ptr<FFmpegAudioStream> s)
267 boost::mutex::scoped_lock lm (_mutex);
271 signal_changed (FFmpegContentProperty::AUDIO_STREAM);
275 FFmpegContent::audio_length () const
277 int const cafr = content_audio_frame_rate ();
278 float const vfr = video_frame_rate ();
279 VideoContent::Frame const vl = video_length_after_3d_combine ();
281 boost::mutex::scoped_lock lm (_mutex);
282 if (!_audio_stream) {
286 return video_frames_to_audio_frames (vl, cafr, vfr);
290 FFmpegContent::audio_channels () const
292 boost::mutex::scoped_lock lm (_mutex);
294 if (!_audio_stream) {
298 return _audio_stream->channels;
302 FFmpegContent::content_audio_frame_rate () const
304 boost::mutex::scoped_lock lm (_mutex);
306 if (!_audio_stream) {
310 return _audio_stream->frame_rate;
314 operator== (FFmpegStream const & a, FFmpegStream const & b)
316 return a._id == b._id;
320 operator!= (FFmpegStream const & a, FFmpegStream const & b)
322 return a._id != b._id;
325 FFmpegStream::FFmpegStream (shared_ptr<const cxml::Node> node)
326 : name (node->string_child ("Name"))
327 , _id (node->number_child<int> ("Id"))
333 FFmpegStream::as_xml (xmlpp::Node* root) const
335 root->add_child("Name")->add_child_text (name);
336 root->add_child("Id")->add_child_text (raw_convert<string> (_id));
339 FFmpegAudioStream::FFmpegAudioStream (shared_ptr<const cxml::Node> node, int version)
340 : FFmpegStream (node)
341 , mapping (node->node_child ("Mapping"), version)
343 frame_rate = node->number_child<int> ("FrameRate");
344 channels = node->number_child<int64_t> ("Channels");
345 first_audio = node->optional_number_child<double> ("FirstAudio");
349 FFmpegAudioStream::as_xml (xmlpp::Node* root) const
351 FFmpegStream::as_xml (root);
352 root->add_child("FrameRate")->add_child_text (raw_convert<string> (frame_rate));
353 root->add_child("Channels")->add_child_text (raw_convert<string> (channels));
355 root->add_child("FirstAudio")->add_child_text (raw_convert<string> (first_audio.get ()));
357 mapping.as_xml (root->add_child("Mapping"));
361 FFmpegStream::uses_index (AVFormatContext const * fc, int index) const
364 while (i < fc->nb_streams) {
365 if (fc->streams[i]->id == _id) {
366 return int (i) == index;
375 FFmpegStream::stream (AVFormatContext const * fc) const
378 while (i < fc->nb_streams) {
379 if (fc->streams[i]->id == _id) {
380 return fc->streams[i];
389 /** Construct a SubtitleStream from a value returned from to_string().
390 * @param t String returned from to_string().
391 * @param v State file version.
393 FFmpegSubtitleStream::FFmpegSubtitleStream (shared_ptr<const cxml::Node> node)
394 : FFmpegStream (node)
400 FFmpegSubtitleStream::as_xml (xmlpp::Node* root) const
402 FFmpegStream::as_xml (root);
406 FFmpegContent::full_length () const
408 shared_ptr<const Film> film = _film.lock ();
411 FrameRateChange frc (video_frame_rate (), film->video_frame_rate ());
412 return video_length_after_3d_combine() * frc.factor() * TIME_HZ / film->video_frame_rate ();
416 FFmpegContent::audio_mapping () const
418 boost::mutex::scoped_lock lm (_mutex);
420 if (!_audio_stream) {
421 return AudioMapping ();
424 return _audio_stream->mapping;
428 FFmpegContent::set_filters (vector<Filter const *> const & filters)
431 boost::mutex::scoped_lock lm (_mutex);
435 signal_changed (FFmpegContentProperty::FILTERS);
439 FFmpegContent::set_audio_mapping (AudioMapping m)
441 audio_stream()->mapping = m;
442 signal_changed (AudioContentProperty::AUDIO_MAPPING);
446 FFmpegContent::identifier () const
450 s << VideoContent::identifier();
452 boost::mutex::scoped_lock lm (_mutex);
454 if (_subtitle_stream) {
455 s << "_" << _subtitle_stream->identifier ();
458 for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
459 s << "_" << (*i)->id ();
465 boost::filesystem::path
466 FFmpegContent::audio_analysis_path () const
468 shared_ptr<const Film> film = _film.lock ();
470 return boost::filesystem::path ();
473 /* We need to include the stream ID in this path so that we get different
474 analyses for each stream.
477 boost::filesystem::path p = film->audio_analysis_dir ();
478 string name = digest ();
479 if (audio_stream ()) {
480 name += "_" + audio_stream()->identifier ();