e843e1e168c3fbb71e561279b9babe2debf6b376
[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_examiner.h"
23 #include "compose.hpp"
24 #include "job.h"
25 #include "util.h"
26 #include "filter.h"
27 #include "film.h"
28 #include "log.h"
29 #include "exceptions.h"
30
31 #include "i18n.h"
32
33 using std::string;
34 using std::stringstream;
35 using std::vector;
36 using std::list;
37 using std::cout;
38 using std::pair;
39 using boost::shared_ptr;
40 using boost::lexical_cast;
41 using boost::dynamic_pointer_cast;
42
43 int const FFmpegContentProperty::SUBTITLE_STREAMS = 100;
44 int const FFmpegContentProperty::SUBTITLE_STREAM = 101;
45 int const FFmpegContentProperty::AUDIO_STREAMS = 102;
46 int const FFmpegContentProperty::AUDIO_STREAM = 103;
47 int const FFmpegContentProperty::FILTERS = 104;
48
49 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, boost::filesystem::path p)
50         : Content (f, p)
51         , VideoContent (f, p)
52         , AudioContent (f, p)
53         , SubtitleContent (f, p)
54 {
55
56 }
57
58 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node)
59         : Content (f, node)
60         , VideoContent (f, node)
61         , AudioContent (f, node)
62         , SubtitleContent (f, node)
63 {
64         list<cxml::NodePtr> c = node->node_children ("SubtitleStream");
65         for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
66                 _subtitle_streams.push_back (shared_ptr<FFmpegSubtitleStream> (new FFmpegSubtitleStream (*i)));
67                 if ((*i)->optional_number_child<int> ("Selected")) {
68                         _subtitle_stream = _subtitle_streams.back ();
69                 }
70         }
71
72         c = node->node_children ("AudioStream");
73         for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
74                 _audio_streams.push_back (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream (*i)));
75                 if ((*i)->optional_number_child<int> ("Selected")) {
76                         _audio_stream = _audio_streams.back ();
77                 }
78         }
79
80         c = node->node_children ("Filter");
81         for (list<cxml::NodePtr>::iterator i = c.begin(); i != c.end(); ++i) {
82                 _filters.push_back (Filter::from_id ((*i)->content ()));
83         }
84
85         _first_video = node->optional_number_child<double> ("FirstVideo");
86 }
87
88 FFmpegContent::FFmpegContent (shared_ptr<const Film> f, vector<boost::shared_ptr<Content> > c)
89         : Content (f, c)
90         , VideoContent (f, c)
91         , AudioContent (f, c)
92         , SubtitleContent (f, c)
93 {
94         shared_ptr<FFmpegContent> ref = dynamic_pointer_cast<FFmpegContent> (c[0]);
95         assert (ref);
96         
97         for (size_t i = 0; i < c.size(); ++i) {
98                 shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (c[i]);
99                 if (*(fc->_subtitle_stream.get()) != *(ref->_subtitle_stream.get())) {
100                         throw JoinError (_("Content to be joined must use the same subtitle stream."));
101                 }
102
103                 if (*(fc->_audio_stream.get()) != *(ref->_audio_stream.get())) {
104                         throw JoinError (_("Content to be joined must use the same audio stream."));
105                 }
106         }
107
108         _subtitle_streams = ref->subtitle_streams ();
109         _subtitle_stream = ref->subtitle_stream ();
110         _audio_streams = ref->audio_streams ();
111         _audio_stream = ref->audio_stream ();
112         _first_video = ref->_first_video;
113 }
114
115 void
116 FFmpegContent::as_xml (xmlpp::Node* node) const
117 {
118         node->add_child("Type")->add_child_text ("FFmpeg");
119         Content::as_xml (node);
120         VideoContent::as_xml (node);
121         AudioContent::as_xml (node);
122         SubtitleContent::as_xml (node);
123
124         boost::mutex::scoped_lock lm (_mutex);
125
126         for (vector<shared_ptr<FFmpegSubtitleStream> >::const_iterator i = _subtitle_streams.begin(); i != _subtitle_streams.end(); ++i) {
127                 xmlpp::Node* t = node->add_child("SubtitleStream");
128                 if (_subtitle_stream && *i == _subtitle_stream) {
129                         t->add_child("Selected")->add_child_text("1");
130                 }
131                 (*i)->as_xml (t);
132         }
133
134         for (vector<shared_ptr<FFmpegAudioStream> >::const_iterator i = _audio_streams.begin(); i != _audio_streams.end(); ++i) {
135                 xmlpp::Node* t = node->add_child("AudioStream");
136                 if (_audio_stream && *i == _audio_stream) {
137                         t->add_child("Selected")->add_child_text("1");
138                 }
139                 (*i)->as_xml (t);
140         }
141
142         for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
143                 node->add_child("Filter")->add_child_text ((*i)->id ());
144         }
145
146         if (_first_video) {
147                 node->add_child("FirstVideo")->add_child_text (lexical_cast<string> (_first_video.get ()));
148         }
149 }
150
151 void
152 FFmpegContent::examine (shared_ptr<Job> job)
153 {
154         job->set_progress_unknown ();
155
156         Content::examine (job);
157
158         shared_ptr<const Film> film = _film.lock ();
159         assert (film);
160
161         shared_ptr<FFmpegExaminer> examiner (new FFmpegExaminer (shared_from_this ()));
162
163         VideoContent::Frame video_length = 0;
164         video_length = examiner->video_length ();
165         film->log()->log (String::compose ("Video length obtained from header as %1 frames", video_length));
166
167         {
168                 boost::mutex::scoped_lock lm (_mutex);
169
170                 _video_length = video_length;
171
172                 _subtitle_streams = examiner->subtitle_streams ();
173                 if (!_subtitle_streams.empty ()) {
174                         _subtitle_stream = _subtitle_streams.front ();
175                 }
176                 
177                 _audio_streams = examiner->audio_streams ();
178                 if (!_audio_streams.empty ()) {
179                         _audio_stream = _audio_streams.front ();
180                 }
181
182                 _first_video = examiner->first_video ();
183         }
184
185         take_from_video_examiner (examiner);
186
187         signal_changed (ContentProperty::LENGTH);
188         signal_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
189         signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
190         signal_changed (FFmpegContentProperty::AUDIO_STREAMS);
191         signal_changed (FFmpegContentProperty::AUDIO_STREAM);
192         signal_changed (AudioContentProperty::AUDIO_CHANNELS);
193 }
194
195 string
196 FFmpegContent::summary () const
197 {
198         /* Get the string() here so that the name does not have quotes around it */
199         return String::compose (_("%1 [movie]"), path_summary ());
200 }
201
202 string
203 FFmpegContent::technical_summary () const
204 {
205         string as = "none";
206         if (_audio_stream) {
207                 as = String::compose ("id %1", _audio_stream->id);
208         }
209
210         string ss = "none";
211         if (_subtitle_stream) {
212                 ss = String::compose ("id %1", _subtitle_stream->id);
213         }
214
215         pair<string, string> filt = Filter::ffmpeg_strings (_filters);
216         
217         return Content::technical_summary() + " - "
218                 + VideoContent::technical_summary() + " - "
219                 + AudioContent::technical_summary() + " - "
220                 + String::compose (
221                         "ffmpeg: audio %1, subtitle %2, filters %3 %4", as, ss, filt.first, filt.second
222                         );
223 }
224
225 string
226 FFmpegContent::information () const
227 {
228         if (video_length() == 0 || video_frame_rate() == 0) {
229                 return "";
230         }
231         
232         stringstream s;
233         
234         s << String::compose (_("%1 frames; %2 frames per second"), video_length(), video_frame_rate()) << "\n";
235         s << VideoContent::information ();
236
237         return s.str ();
238 }
239
240 void
241 FFmpegContent::set_subtitle_stream (shared_ptr<FFmpegSubtitleStream> s)
242 {
243         {
244                 boost::mutex::scoped_lock lm (_mutex);
245                 _subtitle_stream = s;
246         }
247
248         signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
249 }
250
251 void
252 FFmpegContent::set_audio_stream (shared_ptr<FFmpegAudioStream> s)
253 {
254         {
255                 boost::mutex::scoped_lock lm (_mutex);
256                 _audio_stream = s;
257         }
258
259         signal_changed (FFmpegContentProperty::AUDIO_STREAM);
260 }
261
262 AudioContent::Frame
263 FFmpegContent::audio_length () const
264 {
265         int const cafr = content_audio_frame_rate ();
266         int const vfr  = video_frame_rate ();
267         VideoContent::Frame const vl = video_length ();
268
269         boost::mutex::scoped_lock lm (_mutex);
270         if (!_audio_stream) {
271                 return 0;
272         }
273         
274         return video_frames_to_audio_frames (vl, cafr, vfr);
275 }
276
277 int
278 FFmpegContent::audio_channels () const
279 {
280         boost::mutex::scoped_lock lm (_mutex);
281         
282         if (!_audio_stream) {
283                 return 0;
284         }
285
286         return _audio_stream->channels;
287 }
288
289 int
290 FFmpegContent::content_audio_frame_rate () const
291 {
292         boost::mutex::scoped_lock lm (_mutex);
293
294         if (!_audio_stream) {
295                 return 0;
296         }
297
298         return _audio_stream->frame_rate;
299 }
300
301 int
302 FFmpegContent::output_audio_frame_rate () const
303 {
304         shared_ptr<const Film> film = _film.lock ();
305         assert (film);
306         
307         /* Resample to a DCI-approved sample rate */
308         double t = dcp_audio_frame_rate (content_audio_frame_rate ());
309
310         FrameRateConversion frc (video_frame_rate(), film->video_frame_rate());
311
312         /* Compensate if the DCP is being run at a different frame rate
313            to the source; that is, if the video is run such that it will
314            look different in the DCP compared to the source (slower or faster).
315            skip/repeat doesn't come into effect here.
316         */
317
318         if (frc.change_speed) {
319                 t *= video_frame_rate() * frc.factor() / film->video_frame_rate();
320         }
321
322         return rint (t);
323 }
324
325 bool
326 operator== (FFmpegSubtitleStream const & a, FFmpegSubtitleStream const & b)
327 {
328         return a.id == b.id;
329 }
330
331 bool
332 operator!= (FFmpegSubtitleStream const & a, FFmpegSubtitleStream const & b)
333 {
334         return a.id != b.id;
335 }
336
337 bool
338 operator== (FFmpegAudioStream const & a, FFmpegAudioStream const & b)
339 {
340         return a.id == b.id;
341 }
342
343 bool
344 operator!= (FFmpegAudioStream const & a, FFmpegAudioStream const & b)
345 {
346         return a.id != b.id;
347 }
348
349 FFmpegAudioStream::FFmpegAudioStream (shared_ptr<const cxml::Node> node)
350         : mapping (node->node_child ("Mapping"))
351 {
352         name = node->string_child ("Name");
353         id = node->number_child<int> ("Id");
354         frame_rate = node->number_child<int> ("FrameRate");
355         channels = node->number_child<int64_t> ("Channels");
356         first_audio = node->optional_number_child<double> ("FirstAudio");
357 }
358
359 void
360 FFmpegAudioStream::as_xml (xmlpp::Node* root) const
361 {
362         root->add_child("Name")->add_child_text (name);
363         root->add_child("Id")->add_child_text (lexical_cast<string> (id));
364         root->add_child("FrameRate")->add_child_text (lexical_cast<string> (frame_rate));
365         root->add_child("Channels")->add_child_text (lexical_cast<string> (channels));
366         if (first_audio) {
367                 root->add_child("FirstAudio")->add_child_text (lexical_cast<string> (first_audio.get ()));
368         }
369         mapping.as_xml (root->add_child("Mapping"));
370 }
371
372 /** Construct a SubtitleStream from a value returned from to_string().
373  *  @param t String returned from to_string().
374  *  @param v State file version.
375  */
376 FFmpegSubtitleStream::FFmpegSubtitleStream (shared_ptr<const cxml::Node> node)
377 {
378         name = node->string_child ("Name");
379         id = node->number_child<int> ("Id");
380 }
381
382 void
383 FFmpegSubtitleStream::as_xml (xmlpp::Node* root) const
384 {
385         root->add_child("Name")->add_child_text (name);
386         root->add_child("Id")->add_child_text (lexical_cast<string> (id));
387 }
388
389 Time
390 FFmpegContent::full_length () const
391 {
392         shared_ptr<const Film> film = _film.lock ();
393         assert (film);
394         
395         FrameRateConversion frc (video_frame_rate (), film->video_frame_rate ());
396         return video_length() * frc.factor() * TIME_HZ / film->video_frame_rate ();
397 }
398
399 AudioMapping
400 FFmpegContent::audio_mapping () const
401 {
402         boost::mutex::scoped_lock lm (_mutex);
403
404         if (!_audio_stream) {
405                 return AudioMapping ();
406         }
407
408         return _audio_stream->mapping;
409 }
410
411 void
412 FFmpegContent::set_filters (vector<Filter const *> const & filters)
413 {
414         {
415                 boost::mutex::scoped_lock lm (_mutex);
416                 _filters = filters;
417         }
418
419         signal_changed (FFmpegContentProperty::FILTERS);
420 }
421
422 void
423 FFmpegContent::set_audio_mapping (AudioMapping m)
424 {
425         audio_stream()->mapping = m;
426         signal_changed (AudioContentProperty::AUDIO_MAPPING);
427 }
428
429 string
430 FFmpegContent::identifier () const
431 {
432         stringstream s;
433
434         s << VideoContent::identifier();
435
436         boost::mutex::scoped_lock lm (_mutex);
437
438         if (_subtitle_stream) {
439                 s << "_" << _subtitle_stream->id;
440         }
441
442         for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
443                 s << "_" << (*i)->id ();
444         }
445
446         return s.str ();
447 }
448