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