Innocuous build fixes.
[dcpomatic.git] / src / lib / ffmpeg_content.cc
1 /*
2     Copyright (C) 2013-2016 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 "ffmpeg_content.h"
21 #include "video_content.h"
22 #include "ffmpeg_examiner.h"
23 #include "ffmpeg_subtitle_stream.h"
24 #include "ffmpeg_audio_stream.h"
25 #include "compose.hpp"
26 #include "job.h"
27 #include "util.h"
28 #include "filter.h"
29 #include "film.h"
30 #include "log.h"
31 #include "exceptions.h"
32 #include "frame_rate_change.h"
33 #include "safe_stringstream.h"
34 #include "raw_convert.h"
35 #include <libcxml/cxml.h>
36 extern "C" {
37 #include <libavformat/avformat.h>
38 #include <libavutil/pixdesc.h>
39 }
40 #include <libxml++/libxml++.h>
41 #include <boost/foreach.hpp>
42 #include <iostream>
43
44 #include "i18n.h"
45
46 #define LOG_GENERAL(...) film->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL);
47
48 using std::string;
49 using std::vector;
50 using std::list;
51 using std::cout;
52 using std::pair;
53 using std::make_pair;
54 using boost::shared_ptr;
55 using boost::dynamic_pointer_cast;
56 using boost::optional;
57
58 int const FFmpegContentProperty::SUBTITLE_STREAMS = 100;
59 int const FFmpegContentProperty::SUBTITLE_STREAM = 101;
60 int const FFmpegContentProperty::FILTERS = 102;
61
62 FFmpegContent::FFmpegContent (shared_ptr<const Film> film, boost::filesystem::path p)
63         : Content (film, p)
64         , AudioContent (film, p)
65         , SubtitleContent (film, p)
66         , video (new VideoContent (film))
67 {
68         set_default_colour_conversion ();
69 }
70
71 FFmpegContent::FFmpegContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version, list<string>& notes)
72         : Content (film, node)
73         , AudioContent (film, node)
74         , SubtitleContent (film, node, version)
75         , video (new VideoContent (film, node, version))
76 {
77         list<cxml::NodePtr> c = node->node_children ("SubtitleStream");
78         for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
79                 _subtitle_streams.push_back (shared_ptr<FFmpegSubtitleStream> (new FFmpegSubtitleStream (*i, version)));
80                 if ((*i)->optional_number_child<int> ("Selected")) {
81                         _subtitle_stream = _subtitle_streams.back ();
82                 }
83         }
84
85         c = node->node_children ("AudioStream");
86         for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
87                 _audio_streams.push_back (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream (*i, version)));
88                 if (version < 11 && !(*i)->optional_node_child ("Selected")) {
89                         /* This is an old file and this stream is not selected, so un-map it */
90                         _audio_streams.back()->set_mapping (AudioMapping (_audio_streams.back()->channels (), MAX_DCP_AUDIO_CHANNELS));
91                 }
92         }
93
94         c = node->node_children ("Filter");
95         for (list<cxml::NodePtr>::iterator i = c.begin(); i != c.end(); ++i) {
96                 Filter const * f = Filter::from_id ((*i)->content ());
97                 if (f) {
98                         _filters.push_back (f);
99                 } else {
100                         notes.push_back (String::compose (_("DCP-o-matic no longer supports the `%1' filter, so it has been turned off."), (*i)->content()));
101                 }
102         }
103
104         optional<ContentTime::Type> const f = node->optional_number_child<ContentTime::Type> ("FirstVideo");
105         if (f) {
106                 _first_video = ContentTime (f.get ());
107         }
108
109         _color_range = static_cast<AVColorRange> (node->optional_number_child<int>("ColorRange").get_value_or (AVCOL_RANGE_UNSPECIFIED));
110         _color_primaries = static_cast<AVColorPrimaries> (node->optional_number_child<int>("ColorPrimaries").get_value_or (AVCOL_PRI_UNSPECIFIED));
111         _color_trc = static_cast<AVColorTransferCharacteristic> (
112                 node->optional_number_child<int>("ColorTransferCharacteristic").get_value_or (AVCOL_TRC_UNSPECIFIED)
113                 );
114         _colorspace = static_cast<AVColorSpace> (node->optional_number_child<int>("Colorspace").get_value_or (AVCOL_SPC_UNSPECIFIED));
115         _bits_per_pixel = node->optional_number_child<int> ("BitsPerPixel");
116
117 }
118
119 FFmpegContent::FFmpegContent (shared_ptr<const Film> film, vector<boost::shared_ptr<Content> > c)
120         : Content (film, c)
121         , AudioContent (film, c)
122         , SubtitleContent (film, c)
123         , video (new VideoContent (film, c))
124 {
125         shared_ptr<FFmpegContent> ref = dynamic_pointer_cast<FFmpegContent> (c[0]);
126         DCPOMATIC_ASSERT (ref);
127
128         for (size_t i = 0; i < c.size(); ++i) {
129                 shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (c[i]);
130                 if (fc->use_subtitles() && *(fc->_subtitle_stream.get()) != *(ref->_subtitle_stream.get())) {
131                         throw JoinError (_("Content to be joined must use the same subtitle stream."));
132                 }
133         }
134
135         /* XXX: should probably check that more of the stuff below is the same in *this and ref */
136
137         _subtitle_streams = ref->subtitle_streams ();
138         _subtitle_stream = ref->subtitle_stream ();
139         _audio_streams = ref->ffmpeg_audio_streams ();
140         _first_video = ref->_first_video;
141         _filters = ref->_filters;
142         _color_range = ref->_color_range;
143         _color_primaries = ref->_color_primaries;
144         _color_trc = ref->_color_trc;
145         _colorspace = ref->_colorspace;
146         _bits_per_pixel = ref->_bits_per_pixel;
147 }
148
149 void
150 FFmpegContent::as_xml (xmlpp::Node* node) const
151 {
152         node->add_child("Type")->add_child_text ("FFmpeg");
153         Content::as_xml (node);
154         video->as_xml (node);
155         AudioContent::as_xml (node);
156         SubtitleContent::as_xml (node);
157
158         boost::mutex::scoped_lock lm (_mutex);
159
160         for (vector<shared_ptr<FFmpegSubtitleStream> >::const_iterator i = _subtitle_streams.begin(); i != _subtitle_streams.end(); ++i) {
161                 xmlpp::Node* t = node->add_child("SubtitleStream");
162                 if (_subtitle_stream && *i == _subtitle_stream) {
163                         t->add_child("Selected")->add_child_text("1");
164                 }
165                 (*i)->as_xml (t);
166         }
167
168         for (vector<shared_ptr<FFmpegAudioStream> >::const_iterator i = _audio_streams.begin(); i != _audio_streams.end(); ++i) {
169                 (*i)->as_xml (node->add_child("AudioStream"));
170         }
171
172         for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
173                 node->add_child("Filter")->add_child_text ((*i)->id ());
174         }
175
176         if (_first_video) {
177                 node->add_child("FirstVideo")->add_child_text (raw_convert<string> (_first_video.get().get()));
178         }
179
180         node->add_child("ColorRange")->add_child_text (raw_convert<string> (_color_range));
181         node->add_child("ColorPrimaries")->add_child_text (raw_convert<string> (_color_primaries));
182         node->add_child("ColorTransferCharacteristic")->add_child_text (raw_convert<string> (_color_trc));
183         node->add_child("Colorspace")->add_child_text (raw_convert<string> (_colorspace));
184         if (_bits_per_pixel) {
185                 node->add_child("BitsPerPixel")->add_child_text (raw_convert<string> (_bits_per_pixel.get ()));
186         }
187 }
188
189 void
190 FFmpegContent::examine (shared_ptr<Job> job)
191 {
192         job->set_progress_unknown ();
193
194         Content::examine (job);
195
196         shared_ptr<FFmpegExaminer> examiner (new FFmpegExaminer (shared_from_this (), job));
197         take_from_video_examiner (examiner);
198         set_default_colour_conversion ();
199
200         {
201                 boost::mutex::scoped_lock lm (_mutex);
202
203                 _subtitle_streams = examiner->subtitle_streams ();
204                 if (!_subtitle_streams.empty ()) {
205                         _subtitle_stream = _subtitle_streams.front ();
206                 }
207
208                 _audio_streams = examiner->audio_streams ();
209
210                 if (!_audio_streams.empty ()) {
211                         AudioMapping m = _audio_streams.front()->mapping ();
212                         film()->make_audio_mapping_default (m);
213                         _audio_streams.front()->set_mapping (m);
214                 }
215
216                 _first_video = examiner->first_video ();
217
218                 _color_range = examiner->color_range ();
219                 _color_primaries = examiner->color_primaries ();
220                 _color_trc = examiner->color_trc ();
221                 _colorspace = examiner->colorspace ();
222                 _bits_per_pixel = examiner->bits_per_pixel ();
223         }
224
225         signal_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
226         signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
227         signal_changed (AudioContentProperty::AUDIO_STREAMS);
228 }
229
230 string
231 FFmpegContent::summary () const
232 {
233         /* Get the string() here so that the name does not have quotes around it */
234         return String::compose (_("%1 [movie]"), path_summary ());
235 }
236
237 string
238 FFmpegContent::technical_summary () const
239 {
240         string as = "";
241         BOOST_FOREACH (shared_ptr<FFmpegAudioStream> i, ffmpeg_audio_streams ()) {
242                 as += i->technical_summary () + " " ;
243         }
244
245         if (as.empty ()) {
246                 as = "none";
247         }
248
249         string ss = "none";
250         if (_subtitle_stream) {
251                 ss = _subtitle_stream->technical_summary ();
252         }
253
254         string filt = Filter::ffmpeg_string (_filters);
255
256         return Content::technical_summary() + " - "
257                 + video->technical_summary() + " - "
258                 + AudioContent::technical_summary() + " - "
259                 + String::compose (
260                         "ffmpeg: audio %1 subtitle %2 filters %3", as, ss, filt
261                         );
262 }
263
264 void
265 FFmpegContent::set_subtitle_stream (shared_ptr<FFmpegSubtitleStream> s)
266 {
267         {
268                 boost::mutex::scoped_lock lm (_mutex);
269                 _subtitle_stream = s;
270         }
271
272         signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
273 }
274
275 bool
276 operator== (FFmpegStream const & a, FFmpegStream const & b)
277 {
278         return a._id == b._id;
279 }
280
281 bool
282 operator!= (FFmpegStream const & a, FFmpegStream const & b)
283 {
284         return a._id != b._id;
285 }
286
287 DCPTime
288 FFmpegContent::full_length () const
289 {
290         FrameRateChange const frc (video_frame_rate (), film()->video_frame_rate ());
291         return DCPTime::from_frames (llrint (video_length_after_3d_combine() * frc.factor()), film()->video_frame_rate());
292 }
293
294 void
295 FFmpegContent::set_filters (vector<Filter const *> const & filters)
296 {
297         {
298                 boost::mutex::scoped_lock lm (_mutex);
299                 _filters = filters;
300         }
301
302         signal_changed (FFmpegContentProperty::FILTERS);
303 }
304
305 string
306 FFmpegContent::identifier () const
307 {
308         SafeStringStream s;
309
310         s << Content::identifier() << "_"
311           << video->identifier() << "_"
312           << SubtitleContent::identifier();
313
314         boost::mutex::scoped_lock lm (_mutex);
315
316         if (_subtitle_stream) {
317                 s << "_" << _subtitle_stream->identifier ();
318         }
319
320         for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
321                 s << "_" << (*i)->id ();
322         }
323
324         return s.str ();
325 }
326
327 list<ContentTimePeriod>
328 FFmpegContent::image_subtitles_during (ContentTimePeriod period, bool starting) const
329 {
330         shared_ptr<FFmpegSubtitleStream> stream = subtitle_stream ();
331         if (!stream) {
332                 return list<ContentTimePeriod> ();
333         }
334
335         return stream->image_subtitles_during (period, starting);
336 }
337
338 list<ContentTimePeriod>
339 FFmpegContent::text_subtitles_during (ContentTimePeriod period, bool starting) const
340 {
341         shared_ptr<FFmpegSubtitleStream> stream = subtitle_stream ();
342         if (!stream) {
343                 return list<ContentTimePeriod> ();
344         }
345
346         return stream->text_subtitles_during (period, starting);
347 }
348
349 bool
350 FFmpegContent::has_image_subtitles () const
351 {
352         BOOST_FOREACH (shared_ptr<FFmpegSubtitleStream> i, subtitle_streams()) {
353                 if (i->has_image_subtitles()) {
354                         return true;
355                 }
356         }
357
358         return false;
359 }
360
361 bool
362 FFmpegContent::has_text_subtitles () const
363 {
364         BOOST_FOREACH (shared_ptr<FFmpegSubtitleStream> i, subtitle_streams()) {
365                 if (i->has_text_subtitles()) {
366                         return true;
367                 }
368         }
369
370         return false;
371 }
372
373 void
374 FFmpegContent::set_default_colour_conversion ()
375 {
376         dcp::Size const s = video_size ();
377
378         boost::mutex::scoped_lock lm (_mutex);
379
380         if (s.width < 1080) {
381                 _colour_conversion = PresetColourConversion::from_id ("rec601").conversion;
382         } else {
383                 _colour_conversion = PresetColourConversion::from_id ("rec709").conversion;
384         }
385 }
386
387 vector<AudioStreamPtr>
388 FFmpegContent::audio_streams () const
389 {
390         boost::mutex::scoped_lock lm (_mutex);
391
392         vector<AudioStreamPtr> s;
393         copy (_audio_streams.begin(), _audio_streams.end(), back_inserter (s));
394         return s;
395 }
396
397 void
398 FFmpegContent::add_properties (list<UserProperty>& p) const
399 {
400         Content::add_properties (p);
401         video->add_properties (p);
402         AudioContent::add_properties (p);
403
404         if (_bits_per_pixel) {
405                 int const sub = 219 * pow (2, _bits_per_pixel.get() - 8);
406                 int const total = pow (2, _bits_per_pixel.get());
407
408                 switch (_color_range) {
409                 case AVCOL_RANGE_UNSPECIFIED:
410                         /// TRANSLATORS: this means that the range of pixel values used in this
411                         /// file is unknown (not specified in the file).
412                         p.push_back (UserProperty (_("Video"), _("Colour range"), _("Unspecified")));
413                         break;
414                 case AVCOL_RANGE_MPEG:
415                         /// TRANSLATORS: this means that the range of pixel values used in this
416                         /// file is limited, so that not all possible values are valid.
417                         p.push_back (
418                                 UserProperty (
419                                         _("Video"), _("Colour range"), String::compose (_("Limited (%1-%2)"), (total - sub) / 2, (total + sub) / 2)
420                                         )
421                                 );
422                         break;
423                 case AVCOL_RANGE_JPEG:
424                         /// TRANSLATORS: this means that the range of pixel values used in this
425                         /// file is full, so that all possible pixel values are valid.
426                         p.push_back (UserProperty (_("Video"), _("Colour range"), String::compose (_("Full (0-%1)"), total)));
427                         break;
428                 default:
429                         DCPOMATIC_ASSERT (false);
430                 }
431         } else {
432                 switch (_color_range) {
433                 case AVCOL_RANGE_UNSPECIFIED:
434                         /// TRANSLATORS: this means that the range of pixel values used in this
435                         /// file is unknown (not specified in the file).
436                         p.push_back (UserProperty (_("Video"), _("Colour range"), _("Unspecified")));
437                         break;
438                 case AVCOL_RANGE_MPEG:
439                         /// TRANSLATORS: this means that the range of pixel values used in this
440                         /// file is limited, so that not all possible values are valid.
441                         p.push_back (UserProperty (_("Video"), _("Colour range"), _("Limited")));
442                         break;
443                 case AVCOL_RANGE_JPEG:
444                         /// TRANSLATORS: this means that the range of pixel values used in this
445                         /// file is full, so that all possible pixel values are valid.
446                         p.push_back (UserProperty (_("Video"), _("Colour range"), _("Full")));
447                         break;
448                 default:
449                         DCPOMATIC_ASSERT (false);
450                 }
451         }
452
453         char const * primaries[] = {
454                 _("Unspecified"),
455                 _("BT709"),
456                 _("Unspecified"),
457                 _("Unspecified"),
458                 _("BT470M"),
459                 _("BT470BG"),
460                 _("SMPTE 170M (BT601)"),
461                 _("SMPTE 240M"),
462                 _("Film"),
463                 _("BT2020"),
464                 _("SMPTE ST 428-1 (CIE 1931 XYZ)")
465         };
466
467         DCPOMATIC_ASSERT (AVCOL_PRI_NB == 11);
468         p.push_back (UserProperty (_("Video"), _("Colour primaries"), primaries[_color_primaries]));
469
470         char const * transfers[] = {
471                 _("Unspecified"),
472                 _("BT709"),
473                 _("Unspecified"),
474                 _("Unspecified"),
475                 _("Gamma 22 (BT470M)"),
476                 _("Gamma 28 (BT470BG)"),
477                 _("SMPTE 170M (BT601)"),
478                 _("SMPTE 240M"),
479                 _("Linear"),
480                 _("Logarithmic (100:1 range)"),
481                 _("Logarithmic (316:1 range)"),
482                 _("IEC61966-2-4"),
483                 _("BT1361 extended colour gamut"),
484                 _("IEC61966-2-1 (sRGB or sYCC)"),
485                 _("BT2020 for a 10-bit system"),
486                 _("BT2020 for a 12-bit system"),
487                 _("SMPTE ST 2084 for 10, 12, 14 and 16 bit systems"),
488                 _("SMPTE ST 428-1")
489         };
490
491         DCPOMATIC_ASSERT (AVCOL_TRC_NB == 18);
492         p.push_back (UserProperty (_("Video"), _("Colour transfer characteristic"), transfers[_color_trc]));
493
494         char const * spaces[] = {
495                 _("RGB / sRGB (IEC61966-2-1)"),
496                 _("BT709"),
497                 _("Unspecified"),
498                 _("Unspecified"),
499                 _("FCC"),
500                 _("BT470BG (BT601-6)"),
501                 _("SMPTE 170M (BT601-6)"),
502                 _("SMPTE 240M"),
503                 _("YCOCG"),
504                 _("BT2020 non-constant luminance"),
505                 _("BT2020 constant luminance"),
506         };
507
508         DCPOMATIC_ASSERT (AVCOL_SPC_NB == 11);
509         p.push_back (UserProperty (_("Video"), _("Colourspace"), spaces[_colorspace]));
510
511         if (_bits_per_pixel) {
512                 p.push_back (UserProperty (_("Video"), _("Bits per pixel"), raw_convert<string> (_bits_per_pixel.get ())));
513         }
514 }
515
516 /** Our subtitle streams have colour maps, which can be changed, but
517  *  they have no way of signalling that change.  As a hack, we have this
518  *  method which callers can use when they've modified one of our subtitle
519  *  streams.
520  */
521 void
522 FFmpegContent::signal_subtitle_stream_changed ()
523 {
524         signal_changed (FFmpegContentProperty::SUBTITLE_STREAM);
525 }
526
527 double
528 FFmpegContent::subtitle_video_frame_rate () const
529 {
530         return video->video_frame_rate ();
531 }