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