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"
31 using std::stringstream;
35 using boost::shared_ptr;
36 using boost::lexical_cast;
38 int const FFmpegContentProperty::SUBTITLE_STREAMS = 100;
39 int const FFmpegContentProperty::SUBTITLE_STREAM = 101;
40 int const FFmpegContentProperty::AUDIO_STREAMS = 102;
41 int const FFmpegContentProperty::AUDIO_STREAM = 103;
43 FFmpegContent::FFmpegContent (boost::filesystem::path f)
51 FFmpegContent::FFmpegContent (shared_ptr<const cxml::Node> node)
56 list<shared_ptr<cxml::Node> > c = node->node_children ("SubtitleStream");
57 for (list<shared_ptr<cxml::Node> >::const_iterator i = c.begin(); i != c.end(); ++i) {
58 _subtitle_streams.push_back (FFmpegSubtitleStream (*i));
59 if ((*i)->optional_number_child<int> ("Selected")) {
60 _subtitle_stream = _subtitle_streams.back ();
64 c = node->node_children ("AudioStream");
65 for (list<shared_ptr<cxml::Node> >::const_iterator i = c.begin(); i != c.end(); ++i) {
66 _audio_streams.push_back (FFmpegAudioStream (*i));
67 if ((*i)->optional_number_child<int> ("Selected")) {
68 _audio_stream = _audio_streams.back ();
73 FFmpegContent::FFmpegContent (FFmpegContent const & o)
77 , _subtitle_streams (o._subtitle_streams)
78 , _subtitle_stream (o._subtitle_stream)
79 , _audio_streams (o._audio_streams)
80 , _audio_stream (o._audio_stream)
86 FFmpegContent::as_xml (xmlpp::Node* node) const
88 node->add_child("Type")->add_child_text ("FFmpeg");
89 Content::as_xml (node);
90 VideoContent::as_xml (node);
92 boost::mutex::scoped_lock lm (_mutex);
94 for (vector<FFmpegSubtitleStream>::const_iterator i = _subtitle_streams.begin(); i != _subtitle_streams.end(); ++i) {
95 xmlpp::Node* t = node->add_child("SubtitleStream");
96 if (_subtitle_stream && *i == _subtitle_stream.get()) {
97 t->add_child("Selected")->add_child_text("1");
102 for (vector<FFmpegAudioStream>::const_iterator i = _audio_streams.begin(); i != _audio_streams.end(); ++i) {
103 xmlpp::Node* t = node->add_child("AudioStream");
104 if (_audio_stream && *i == _audio_stream.get()) {
105 t->add_child("Selected")->add_child_text("1");
112 FFmpegContent::examine (shared_ptr<Film> film, shared_ptr<Job> job, bool quick)
114 job->set_progress_unknown ();
116 Content::examine (film, job, quick);
118 shared_ptr<FFmpegDecoder> decoder (new FFmpegDecoder (film, shared_from_this (), true, false, false, true));
120 ContentVideoFrame video_length = 0;
122 video_length = decoder->video_length ();
123 film->log()->log (String::compose ("Video length obtained from header as %1 frames", decoder->video_length ()));
125 while (!decoder->pass ()) {
129 video_length = decoder->video_frame ();
130 film->log()->log (String::compose ("Video length examined as %1 frames", decoder->video_frame ()));
134 boost::mutex::scoped_lock lm (_mutex);
136 _video_length = video_length;
138 _subtitle_streams = decoder->subtitle_streams ();
139 if (!_subtitle_streams.empty ()) {
140 _subtitle_stream = _subtitle_streams.front ();
143 _audio_streams = decoder->audio_streams ();
144 if (!_audio_streams.empty ()) {
145 _audio_stream = _audio_streams.front ();
149 take_from_video_decoder (decoder);
151 signal_changed (VideoContentProperty::VIDEO_LENGTH);
152 signal_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
153 signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
154 signal_changed (FFmpegContentProperty::AUDIO_STREAMS);
155 signal_changed (FFmpegContentProperty::AUDIO_STREAM);
156 signal_changed (AudioContentProperty::AUDIO_CHANNELS);
160 FFmpegContent::summary () const
162 return String::compose (_("Movie: %1"), file().filename().string());
166 FFmpegContent::information () const
168 if (video_length() == 0 || video_frame_rate() == 0) {
174 s << String::compose (_("%1 frames; %2 frames per second"), video_length(), video_frame_rate()) << "\n";
175 s << VideoContent::information ();
181 FFmpegContent::set_subtitle_stream (FFmpegSubtitleStream s)
184 boost::mutex::scoped_lock lm (_mutex);
185 _subtitle_stream = s;
188 signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
192 FFmpegContent::set_audio_stream (FFmpegAudioStream s)
195 boost::mutex::scoped_lock lm (_mutex);
199 signal_changed (FFmpegContentProperty::AUDIO_STREAM);
203 FFmpegContent::audio_length () const
205 if (!_audio_stream) {
209 return video_frames_to_audio_frames (_video_length, audio_frame_rate(), video_frame_rate());
213 FFmpegContent::audio_channels () const
215 if (!_audio_stream) {
219 return _audio_stream->channels ();
223 FFmpegContent::audio_frame_rate () const
225 if (!_audio_stream) {
229 return _audio_stream->frame_rate;
233 FFmpegContent::audio_channel_layout () const
235 if (!_audio_stream) {
239 return _audio_stream->channel_layout;
243 operator== (FFmpegSubtitleStream const & a, FFmpegSubtitleStream const & b)
249 operator== (FFmpegAudioStream const & a, FFmpegAudioStream const & b)
254 FFmpegAudioStream::FFmpegAudioStream (shared_ptr<const cxml::Node> node)
256 name = node->string_child ("Name");
257 id = node->number_child<int> ("Id");
258 frame_rate = node->number_child<int> ("FrameRate");
259 channel_layout = node->number_child<int64_t> ("ChannelLayout");
263 FFmpegAudioStream::as_xml (xmlpp::Node* root) const
265 root->add_child("Name")->add_child_text (name);
266 root->add_child("Id")->add_child_text (lexical_cast<string> (id));
267 root->add_child("FrameRate")->add_child_text (lexical_cast<string> (frame_rate));
268 root->add_child("ChannelLayout")->add_child_text (lexical_cast<string> (channel_layout));
271 /** Construct a SubtitleStream from a value returned from to_string().
272 * @param t String returned from to_string().
273 * @param v State file version.
275 FFmpegSubtitleStream::FFmpegSubtitleStream (shared_ptr<const cxml::Node> node)
277 name = node->string_child ("Name");
278 id = node->number_child<int> ("Id");
282 FFmpegSubtitleStream::as_xml (xmlpp::Node* root) const
284 root->add_child("Name")->add_child_text (name);
285 root->add_child("Id")->add_child_text (lexical_cast<string> (id));
289 FFmpegContent::clone () const
291 return shared_ptr<Content> (new FFmpegContent (*this));