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_decoder.h"
23 #include "compose.hpp"
32 using std::stringstream;
36 using boost::shared_ptr;
37 using boost::lexical_cast;
39 int const FFmpegContentProperty::SUBTITLE_STREAMS = 100;
40 int const FFmpegContentProperty::SUBTITLE_STREAM = 101;
41 int const FFmpegContentProperty::AUDIO_STREAMS = 102;
42 int const FFmpegContentProperty::AUDIO_STREAM = 103;
43 int const FFmpegContentProperty::FILTERS = 104;
45 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, boost::filesystem::path p)
53 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node)
55 , VideoContent (f, node)
56 , AudioContent (f, 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 (shared_ptr<FFmpegSubtitleStream> (new 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 (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream (*i)));
69 if ((*i)->optional_number_child<int> ("Selected")) {
70 _audio_stream = _audio_streams.back ();
74 c = node->node_children ("Filter");
75 for (list<shared_ptr<cxml::Node> >::iterator i = c.begin(); i != c.end(); ++i) {
76 _filters.push_back (Filter::from_id ((*i)->content ()));
80 FFmpegContent::FFmpegContent (FFmpegContent const & o)
84 , _subtitle_streams (o._subtitle_streams)
85 , _subtitle_stream (o._subtitle_stream)
86 , _audio_streams (o._audio_streams)
87 , _audio_stream (o._audio_stream)
93 FFmpegContent::as_xml (xmlpp::Node* node) const
95 node->add_child("Type")->add_child_text ("FFmpeg");
96 Content::as_xml (node);
97 VideoContent::as_xml (node);
98 AudioContent::as_xml (node);
100 boost::mutex::scoped_lock lm (_mutex);
102 for (vector<shared_ptr<FFmpegSubtitleStream> >::const_iterator i = _subtitle_streams.begin(); i != _subtitle_streams.end(); ++i) {
103 xmlpp::Node* t = node->add_child("SubtitleStream");
104 if (_subtitle_stream && *i == _subtitle_stream) {
105 t->add_child("Selected")->add_child_text("1");
110 for (vector<shared_ptr<FFmpegAudioStream> >::const_iterator i = _audio_streams.begin(); i != _audio_streams.end(); ++i) {
111 xmlpp::Node* t = node->add_child("AudioStream");
112 if (_audio_stream && *i == _audio_stream) {
113 t->add_child("Selected")->add_child_text("1");
118 for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
119 node->add_child("Filter")->add_child_text ((*i)->id ());
124 FFmpegContent::examine (shared_ptr<Job> job)
126 job->set_progress_unknown ();
128 Content::examine (job);
130 shared_ptr<const Film> film = _film.lock ();
133 shared_ptr<FFmpegDecoder> decoder (new FFmpegDecoder (film, shared_from_this (), true, false));
135 ContentVideoFrame video_length = 0;
136 video_length = decoder->video_length ();
137 film->log()->log (String::compose ("Video length obtained from header as %1 frames", decoder->video_length ()));
140 boost::mutex::scoped_lock lm (_mutex);
142 _video_length = video_length;
144 _subtitle_streams = decoder->subtitle_streams ();
145 if (!_subtitle_streams.empty ()) {
146 _subtitle_stream = _subtitle_streams.front ();
149 _audio_streams = decoder->audio_streams ();
150 if (!_audio_streams.empty ()) {
151 _audio_stream = _audio_streams.front ();
155 take_from_video_decoder (decoder);
157 signal_changed (ContentProperty::LENGTH);
158 signal_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
159 signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
160 signal_changed (FFmpegContentProperty::AUDIO_STREAMS);
161 signal_changed (FFmpegContentProperty::AUDIO_STREAM);
162 signal_changed (AudioContentProperty::AUDIO_CHANNELS);
166 FFmpegContent::summary () const
168 return String::compose (_("Movie: %1"), file().filename().string());
172 FFmpegContent::information () const
174 if (video_length() == 0 || video_frame_rate() == 0) {
180 s << String::compose (_("%1 frames; %2 frames per second"), video_length(), video_frame_rate()) << "\n";
181 s << VideoContent::information ();
187 FFmpegContent::set_subtitle_stream (shared_ptr<FFmpegSubtitleStream> s)
190 boost::mutex::scoped_lock lm (_mutex);
191 _subtitle_stream = s;
194 signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
198 FFmpegContent::set_audio_stream (shared_ptr<FFmpegAudioStream> s)
201 boost::mutex::scoped_lock lm (_mutex);
205 signal_changed (FFmpegContentProperty::AUDIO_STREAM);
209 FFmpegContent::audio_length () const
211 int const cafr = content_audio_frame_rate ();
212 int const vfr = video_frame_rate ();
213 ContentVideoFrame const vl = video_length ();
215 boost::mutex::scoped_lock lm (_mutex);
216 if (!_audio_stream) {
220 return video_frames_to_audio_frames (vl, cafr, vfr);
224 FFmpegContent::audio_channels () const
226 boost::mutex::scoped_lock lm (_mutex);
228 if (!_audio_stream) {
232 return _audio_stream->channels;
236 FFmpegContent::content_audio_frame_rate () const
238 boost::mutex::scoped_lock lm (_mutex);
240 if (!_audio_stream) {
244 return _audio_stream->frame_rate;
248 FFmpegContent::output_audio_frame_rate () const
250 shared_ptr<const Film> film = _film.lock ();
253 /* Resample to a DCI-approved sample rate */
254 double t = dcp_audio_frame_rate (content_audio_frame_rate ());
256 FrameRateConversion frc (video_frame_rate(), film->dcp_video_frame_rate());
258 /* Compensate if the DCP is being run at a different frame rate
259 to the source; that is, if the video is run such that it will
260 look different in the DCP compared to the source (slower or faster).
261 skip/repeat doesn't come into effect here.
264 if (frc.change_speed) {
265 t *= video_frame_rate() * frc.factor() / film->dcp_video_frame_rate();
272 operator== (FFmpegSubtitleStream const & a, FFmpegSubtitleStream const & b)
278 operator== (FFmpegAudioStream const & a, FFmpegAudioStream const & b)
283 FFmpegAudioStream::FFmpegAudioStream (shared_ptr<const cxml::Node> node)
285 name = node->string_child ("Name");
286 id = node->number_child<int> ("Id");
287 frame_rate = node->number_child<int> ("FrameRate");
288 channels = node->number_child<int64_t> ("Channels");
289 mapping = AudioMapping (node->node_child ("Mapping"));
293 FFmpegAudioStream::as_xml (xmlpp::Node* root) const
295 root->add_child("Name")->add_child_text (name);
296 root->add_child("Id")->add_child_text (lexical_cast<string> (id));
297 root->add_child("FrameRate")->add_child_text (lexical_cast<string> (frame_rate));
298 root->add_child("Channels")->add_child_text (lexical_cast<string> (channels));
299 mapping.as_xml (root->add_child("Mapping"));
302 /** Construct a SubtitleStream from a value returned from to_string().
303 * @param t String returned from to_string().
304 * @param v State file version.
306 FFmpegSubtitleStream::FFmpegSubtitleStream (shared_ptr<const cxml::Node> node)
308 name = node->string_child ("Name");
309 id = node->number_child<int> ("Id");
313 FFmpegSubtitleStream::as_xml (xmlpp::Node* root) const
315 root->add_child("Name")->add_child_text (name);
316 root->add_child("Id")->add_child_text (lexical_cast<string> (id));
320 FFmpegContent::clone () const
322 return shared_ptr<Content> (new FFmpegContent (*this));
326 FFmpegContent::length () const
328 shared_ptr<const Film> film = _film.lock ();
331 FrameRateConversion frc (video_frame_rate (), film->dcp_video_frame_rate ());
332 return video_length() * frc.factor() * TIME_HZ / film->dcp_video_frame_rate ();
336 FFmpegContent::audio_mapping () const
338 boost::mutex::scoped_lock lm (_mutex);
340 if (!_audio_stream) {
341 return AudioMapping ();
344 return _audio_stream->mapping;
348 FFmpegContent::set_filters (vector<Filter const *> const & filters)
351 boost::mutex::scoped_lock lm (_mutex);
355 signal_changed (FFmpegContentProperty::FILTERS);
359 FFmpegContent::set_audio_mapping (AudioMapping m)
361 audio_stream()->mapping = m;
362 signal_changed (AudioContentProperty::AUDIO_MAPPING);