7834cb76e825cc1b6987f469de747d3661029977
[dcpomatic.git] / src / lib / ffmpeg_content.cc
1 /*
2     Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include <libcxml/cxml.h>
21 #include "ffmpeg_content.h"
22 #include "ffmpeg_decoder.h"
23 #include "compose.hpp"
24 #include "job.h"
25 #include "util.h"
26 #include "log.h"
27
28 #include "i18n.h"
29
30 using std::string;
31 using std::stringstream;
32 using std::vector;
33 using std::list;
34 using boost::shared_ptr;
35 using boost::lexical_cast;
36
37 int const FFmpegContentProperty::SUBTITLE_STREAMS = 100;
38 int const FFmpegContentProperty::SUBTITLE_STREAM = 101;
39 int const FFmpegContentProperty::AUDIO_STREAMS = 102;
40 int const FFmpegContentProperty::AUDIO_STREAM = 103;
41
42 FFmpegContent::FFmpegContent (boost::filesystem::path f)
43         : Content (f)
44         , VideoContent (f)
45         , AudioContent (f)
46 {
47
48 }
49
50 FFmpegContent::FFmpegContent (shared_ptr<const cxml::Node> node)
51         : Content (node)
52         , VideoContent (node)
53         , AudioContent (node)
54 {
55         list<shared_ptr<cxml::Node> > c = node->node_children ("SubtitleStream");
56         for (list<shared_ptr<cxml::Node> >::const_iterator i = c.begin(); i != c.end(); ++i) {
57                 _subtitle_streams.push_back (FFmpegSubtitleStream (*i));
58                 if ((*i)->optional_number_child<int> ("Selected")) {
59                         _subtitle_stream = _subtitle_streams.back ();
60                 }
61         }
62
63         c = node->node_children ("AudioStream");
64         for (list<shared_ptr<cxml::Node> >::const_iterator i = c.begin(); i != c.end(); ++i) {
65                 _audio_streams.push_back (FFmpegAudioStream (*i));
66                 if ((*i)->optional_number_child<int> ("Selected")) {
67                         _audio_stream = _audio_streams.back ();
68                 }
69         }
70 }
71
72 FFmpegContent::FFmpegContent (FFmpegContent const & o)
73         : Content (o)
74         , VideoContent (o)
75         , AudioContent (o)
76         , boost::enable_shared_from_this<FFmpegContent> (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)
81 {
82
83 }
84
85 void
86 FFmpegContent::as_xml (xmlpp::Node* node) const
87 {
88         node->add_child("Type")->add_child_text ("FFmpeg");
89         Content::as_xml (node);
90         VideoContent::as_xml (node);
91
92         boost::mutex::scoped_lock lm (_mutex);
93
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");
98                 }
99                 i->as_xml (t);
100         }
101
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");
106                 }
107                 i->as_xml (t);
108         }
109 }
110
111 void
112 FFmpegContent::examine (shared_ptr<Film> film, shared_ptr<Job> job, bool quick)
113 {
114         job->set_progress_unknown ();
115
116         Content::examine (film, job, quick);
117
118         shared_ptr<FFmpegDecoder> decoder (new FFmpegDecoder (film, shared_from_this (), true, false, false, true));
119
120         ContentVideoFrame video_length = 0;
121         if (quick) {
122                 video_length = decoder->video_length ();
123                 film->log()->log (String::compose ("Video length obtained from header as %1 frames", decoder->video_length ()));
124         } else {
125                 while (!decoder->pass ()) {
126                         /* keep going */
127                 }
128
129                 video_length = decoder->video_frame ();
130                 film->log()->log (String::compose ("Video length examined as %1 frames", decoder->video_frame ()));
131         }
132
133         {
134                 boost::mutex::scoped_lock lm (_mutex);
135
136                 _video_length = video_length;
137
138                 _subtitle_streams = decoder->subtitle_streams ();
139                 if (!_subtitle_streams.empty ()) {
140                         _subtitle_stream = _subtitle_streams.front ();
141                 }
142                 
143                 _audio_streams = decoder->audio_streams ();
144                 if (!_audio_streams.empty ()) {
145                         _audio_stream = _audio_streams.front ();
146                 }
147         }
148
149         take_from_video_decoder (decoder);
150
151         Changed (VideoContentProperty::VIDEO_LENGTH);
152         Changed (FFmpegContentProperty::SUBTITLE_STREAMS);
153         Changed (FFmpegContentProperty::SUBTITLE_STREAM);
154         Changed (FFmpegContentProperty::AUDIO_STREAMS);
155         Changed (FFmpegContentProperty::AUDIO_STREAM);
156 }
157
158 string
159 FFmpegContent::summary () const
160 {
161         return String::compose (_("Movie: %1"), file().filename ());
162 }
163
164 string
165 FFmpegContent::information () const
166 {
167         if (video_length() == 0 || video_frame_rate() == 0) {
168                 return "";
169         }
170         
171         stringstream s;
172         
173         s << String::compose (_("%1 frames; %2 frames per second"), video_length(), video_frame_rate()) << "\n";
174         s << VideoContent::information ();
175
176         return s.str ();
177 }
178
179 void
180 FFmpegContent::set_subtitle_stream (FFmpegSubtitleStream s)
181 {
182         {
183                 boost::mutex::scoped_lock lm (_mutex);
184                 _subtitle_stream = s;
185         }
186
187         Changed (FFmpegContentProperty::SUBTITLE_STREAM);
188 }
189
190 void
191 FFmpegContent::set_audio_stream (FFmpegAudioStream s)
192 {
193         {
194                 boost::mutex::scoped_lock lm (_mutex);
195                 _audio_stream = s;
196         }
197
198         Changed (FFmpegContentProperty::AUDIO_STREAM);
199 }
200
201 ContentAudioFrame
202 FFmpegContent::audio_length () const
203 {
204         if (!_audio_stream) {
205                 return 0;
206         }
207         
208         return video_frames_to_audio_frames (_video_length, audio_frame_rate(), video_frame_rate());
209 }
210
211 int
212 FFmpegContent::audio_channels () const
213 {
214         if (!_audio_stream) {
215                 return 0;
216         }
217
218         return _audio_stream->channels ();
219 }
220
221 int
222 FFmpegContent::audio_frame_rate () const
223 {
224         if (!_audio_stream) {
225                 return 0;
226         }
227
228         return _audio_stream->frame_rate;
229 }
230
231 int64_t
232 FFmpegContent::audio_channel_layout () const
233 {
234         if (!_audio_stream) {
235                 return 0;
236         }
237
238         return _audio_stream->channel_layout;
239 }
240         
241 bool
242 operator== (FFmpegSubtitleStream const & a, FFmpegSubtitleStream const & b)
243 {
244         return a.id == b.id;
245 }
246
247 bool
248 operator== (FFmpegAudioStream const & a, FFmpegAudioStream const & b)
249 {
250         return a.id == b.id;
251 }
252
253 FFmpegAudioStream::FFmpegAudioStream (shared_ptr<const cxml::Node> node)
254 {
255         name = node->string_child ("Name");
256         id = node->number_child<int> ("Id");
257         frame_rate = node->number_child<int> ("FrameRate");
258         channel_layout = node->number_child<int64_t> ("ChannelLayout");
259 }
260
261 void
262 FFmpegAudioStream::as_xml (xmlpp::Node* root) const
263 {
264         root->add_child("Name")->add_child_text (name);
265         root->add_child("Id")->add_child_text (lexical_cast<string> (id));
266         root->add_child("FrameRate")->add_child_text (lexical_cast<string> (frame_rate));
267         root->add_child("ChannelLayout")->add_child_text (lexical_cast<string> (channel_layout));
268 }
269
270 /** Construct a SubtitleStream from a value returned from to_string().
271  *  @param t String returned from to_string().
272  *  @param v State file version.
273  */
274 FFmpegSubtitleStream::FFmpegSubtitleStream (shared_ptr<const cxml::Node> node)
275 {
276         name = node->string_child ("Name");
277         id = node->number_child<int> ("Id");
278 }
279
280 void
281 FFmpegSubtitleStream::as_xml (xmlpp::Node* root) const
282 {
283         root->add_child("Name")->add_child_text (name);
284         root->add_child("Id")->add_child_text (lexical_cast<string> (id));
285 }
286
287 shared_ptr<Content>
288 FFmpegContent::clone () const
289 {
290         return shared_ptr<Content> (new FFmpegContent (*this));
291 }