1 /* -*- c-basic-offset: 8; default-tab-width: 8; -*- */
4 Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <libcxml/cxml.h>
23 #include "ffmpeg_content.h"
24 #include "ffmpeg_decoder.h"
25 #include "compose.hpp"
33 using std::stringstream;
37 using boost::shared_ptr;
38 using boost::lexical_cast;
40 int const FFmpegContentProperty::SUBTITLE_STREAMS = 100;
41 int const FFmpegContentProperty::SUBTITLE_STREAM = 101;
42 int const FFmpegContentProperty::AUDIO_STREAMS = 102;
43 int const FFmpegContentProperty::AUDIO_STREAM = 103;
45 FFmpegContent::FFmpegContent (boost::filesystem::path f)
53 FFmpegContent::FFmpegContent (shared_ptr<const cxml::Node> node)
58 list<shared_ptr<cxml::Node> > c = node->node_children ("SubtitleStream");
59 for (list<shared_ptr<cxml::Node> >::const_iterator i = c.begin(); i != c.end(); ++i) {
60 _subtitle_streams.push_back (FFmpegSubtitleStream (*i));
61 if ((*i)->optional_number_child<int> ("Selected")) {
62 _subtitle_stream = _subtitle_streams.back ();
66 c = node->node_children ("AudioStream");
67 for (list<shared_ptr<cxml::Node> >::const_iterator i = c.begin(); i != c.end(); ++i) {
68 _audio_streams.push_back (FFmpegAudioStream (*i));
69 if ((*i)->optional_number_child<int> ("Selected")) {
70 _audio_stream = _audio_streams.back ();
75 FFmpegContent::FFmpegContent (FFmpegContent const & o)
79 , _subtitle_streams (o._subtitle_streams)
80 , _subtitle_stream (o._subtitle_stream)
81 , _audio_streams (o._audio_streams)
82 , _audio_stream (o._audio_stream)
88 FFmpegContent::as_xml (xmlpp::Node* node) const
90 node->add_child("Type")->add_child_text ("FFmpeg");
91 Content::as_xml (node);
92 VideoContent::as_xml (node);
93 AudioContent::as_xml (node);
95 boost::mutex::scoped_lock lm (_mutex);
97 for (vector<FFmpegSubtitleStream>::const_iterator i = _subtitle_streams.begin(); i != _subtitle_streams.end(); ++i) {
98 xmlpp::Node* t = node->add_child("SubtitleStream");
99 if (_subtitle_stream && *i == _subtitle_stream.get()) {
100 t->add_child("Selected")->add_child_text("1");
105 for (vector<FFmpegAudioStream>::const_iterator i = _audio_streams.begin(); i != _audio_streams.end(); ++i) {
106 xmlpp::Node* t = node->add_child("AudioStream");
107 if (_audio_stream && *i == _audio_stream.get()) {
108 t->add_child("Selected")->add_child_text("1");
115 FFmpegContent::examine (shared_ptr<Film> film, shared_ptr<Job> job, bool quick)
117 job->set_progress_unknown ();
119 Content::examine (film, job, quick);
121 shared_ptr<FFmpegDecoder> decoder (new FFmpegDecoder (film, shared_from_this (), true, false, false));
123 ContentVideoFrame video_length = 0;
125 video_length = decoder->video_length ();
126 film->log()->log (String::compose ("Video length obtained from header as %1 frames", decoder->video_length ()));
128 while (!decoder->pass ()) {
132 video_length = decoder->video_frame ();
133 film->log()->log (String::compose ("Video length examined as %1 frames", decoder->video_frame ()));
137 boost::mutex::scoped_lock lm (_mutex);
139 _video_length = video_length;
141 _subtitle_streams = decoder->subtitle_streams ();
142 if (!_subtitle_streams.empty ()) {
143 _subtitle_stream = _subtitle_streams.front ();
146 _audio_streams = decoder->audio_streams ();
147 if (!_audio_streams.empty ()) {
148 _audio_stream = _audio_streams.front ();
152 take_from_video_decoder (decoder);
154 signal_changed (VideoContentProperty::VIDEO_LENGTH);
155 signal_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
156 signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
157 signal_changed (FFmpegContentProperty::AUDIO_STREAMS);
158 signal_changed (FFmpegContentProperty::AUDIO_STREAM);
159 signal_changed (AudioContentProperty::AUDIO_CHANNELS);
163 FFmpegContent::summary () const
165 return String::compose (_("Movie: %1"), file().filename().string());
169 FFmpegContent::information () const
171 if (video_length() == 0 || video_frame_rate() == 0) {
177 s << String::compose (_("%1 frames; %2 frames per second"), video_length(), video_frame_rate()) << "\n";
178 s << VideoContent::information ();
184 FFmpegContent::set_subtitle_stream (FFmpegSubtitleStream s)
187 boost::mutex::scoped_lock lm (_mutex);
188 _subtitle_stream = s;
191 signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
195 FFmpegContent::set_audio_stream (FFmpegAudioStream s)
198 boost::mutex::scoped_lock lm (_mutex);
202 signal_changed (FFmpegContentProperty::AUDIO_STREAM);
206 FFmpegContent::audio_length () const
208 if (!_audio_stream) {
212 return video_frames_to_audio_frames (_video_length, content_audio_frame_rate(), video_frame_rate());
216 FFmpegContent::audio_channels () const
218 if (!_audio_stream) {
222 return _audio_stream->channels;
226 FFmpegContent::content_audio_frame_rate () const
228 if (!_audio_stream) {
232 return _audio_stream->frame_rate;
236 FFmpegContent::output_audio_frame_rate (shared_ptr<const Film> film) const
238 /* Resample to a DCI-approved sample rate */
239 double t = dcp_audio_frame_rate (content_audio_frame_rate ());
241 FrameRateConversion frc (video_frame_rate(), film->dcp_video_frame_rate());
243 /* Compensate if the DCP is being run at a different frame rate
244 to the source; that is, if the video is run such that it will
245 look different in the DCP compared to the source (slower or faster).
246 skip/repeat doesn't come into effect here.
249 if (frc.change_speed) {
250 t *= video_frame_rate() * frc.factor() / film->dcp_video_frame_rate();
257 operator== (FFmpegSubtitleStream const & a, FFmpegSubtitleStream const & b)
263 operator== (FFmpegAudioStream const & a, FFmpegAudioStream const & b)
268 FFmpegAudioStream::FFmpegAudioStream (shared_ptr<const cxml::Node> node)
269 : mapping (node->node_child ("Mapping"))
271 name = node->string_child ("Name");
272 id = node->number_child<int> ("Id");
273 frame_rate = node->number_child<int> ("FrameRate");
274 channels = node->number_child<int64_t> ("Channels");
278 FFmpegAudioStream::as_xml (xmlpp::Node* root) const
280 root->add_child("Name")->add_child_text (name);
281 root->add_child("Id")->add_child_text (lexical_cast<string> (id));
282 root->add_child("FrameRate")->add_child_text (lexical_cast<string> (frame_rate));
283 root->add_child("Channels")->add_child_text (lexical_cast<string> (channels));
284 mapping.as_xml (root->add_child("Mapping"));
287 /** Construct a SubtitleStream from a value returned from to_string().
288 * @param t String returned from to_string().
289 * @param v State file version.
291 FFmpegSubtitleStream::FFmpegSubtitleStream (shared_ptr<const cxml::Node> node)
293 name = node->string_child ("Name");
294 id = node->number_child<int> ("Id");
298 FFmpegSubtitleStream::as_xml (xmlpp::Node* root) const
300 root->add_child("Name")->add_child_text (name);
301 root->add_child("Id")->add_child_text (lexical_cast<string> (id));
305 FFmpegContent::clone () const
307 return shared_ptr<Content> (new FFmpegContent (*this));
311 FFmpegContent::length (shared_ptr<const Film> film) const
313 FrameRateConversion frc (video_frame_rate (), film->dcp_video_frame_rate ());
314 return video_length() * frc.factor() * TIME_HZ / film->dcp_video_frame_rate ();
318 FFmpegContent::audio_mapping () const
320 if (!_audio_stream) {
321 return AudioMapping ();
324 return _audio_stream->mapping;