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