5a2bef1a08fa7835069a7bd3f3d951ede9d03bac
[dcpomatic.git] / src / lib / video_content.cc
1 /*
2     Copyright (C) 2013-2015 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 "video_content.h"
21 #include "video_examiner.h"
22 #include "compose.hpp"
23 #include "ratio.h"
24 #include "config.h"
25 #include "colour_conversion.h"
26 #include "util.h"
27 #include "film.h"
28 #include "exceptions.h"
29 #include "frame_rate_change.h"
30 #include "log.h"
31 #include "safe_stringstream.h"
32 #include "raw_convert.h"
33 #include <libcxml/cxml.h>
34 #include <dcp/colour_matrix.h>
35 #include <libxml++/libxml++.h>
36 #include <iomanip>
37 #include <iostream>
38
39 #include "i18n.h"
40
41 #define LOG_GENERAL(...) film->log()->log (String::compose (__VA_ARGS__), Log::TYPE_GENERAL);
42
43 int const VideoContentProperty::VIDEO_SIZE        = 0;
44 int const VideoContentProperty::VIDEO_FRAME_RATE  = 1;
45 int const VideoContentProperty::VIDEO_FRAME_TYPE  = 2;
46 int const VideoContentProperty::VIDEO_CROP        = 3;
47 int const VideoContentProperty::VIDEO_SCALE       = 4;
48 int const VideoContentProperty::COLOUR_CONVERSION = 5;
49 int const VideoContentProperty::VIDEO_FADE_IN     = 6;
50 int const VideoContentProperty::VIDEO_FADE_OUT    = 7;
51
52 using std::string;
53 using std::setprecision;
54 using std::cout;
55 using std::vector;
56 using std::min;
57 using std::max;
58 using std::stringstream;
59 using std::fixed;
60 using std::setprecision;
61 using std::list;
62 using std::pair;
63 using boost::shared_ptr;
64 using boost::optional;
65 using boost::dynamic_pointer_cast;
66
67 VideoContent::VideoContent (shared_ptr<const Film> film)
68         : Content (film)
69         , _video_length (0)
70         , _video_frame_rate (0)
71         , _video_frame_type (VIDEO_FRAME_TYPE_2D)
72         , _scale (VideoContentScale (Ratio::from_id ("178")))
73         , _fade_in (0)
74         , _fade_out (0)
75 {
76         set_default_colour_conversion ();
77 }
78
79 VideoContent::VideoContent (shared_ptr<const Film> film, DCPTime s, Frame len)
80         : Content (film, s)
81         , _video_length (len)
82         , _video_frame_rate (0)
83         , _video_frame_type (VIDEO_FRAME_TYPE_2D)
84         , _scale (VideoContentScale (Ratio::from_id ("178")))
85         , _fade_in (0)
86         , _fade_out (0)
87 {
88         set_default_colour_conversion ();
89 }
90
91 VideoContent::VideoContent (shared_ptr<const Film> film, boost::filesystem::path p)
92         : Content (film, p)
93         , _video_length (0)
94         , _video_frame_rate (0)
95         , _video_frame_type (VIDEO_FRAME_TYPE_2D)
96         , _scale (VideoContentScale (Ratio::from_id ("178")))
97         , _fade_in (0)
98         , _fade_out (0)
99 {
100         set_default_colour_conversion ();
101 }
102
103 VideoContent::VideoContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
104         : Content (film, node)
105 {
106         _video_size.width = node->number_child<int> ("VideoWidth");
107         _video_size.height = node->number_child<int> ("VideoHeight");
108         _video_frame_rate = node->number_child<double> ("VideoFrameRate");
109         _video_length = node->number_child<Frame> ("VideoLength");
110         _video_frame_type = static_cast<VideoFrameType> (node->number_child<int> ("VideoFrameType"));
111         _sample_aspect_ratio = node->optional_number_child<double> ("SampleAspectRatio");
112         _crop.left = node->number_child<int> ("LeftCrop");
113         _crop.right = node->number_child<int> ("RightCrop");
114         _crop.top = node->number_child<int> ("TopCrop");
115         _crop.bottom = node->number_child<int> ("BottomCrop");
116
117         if (version <= 7) {
118                 optional<string> r = node->optional_string_child ("Ratio");
119                 if (r) {
120                         _scale = VideoContentScale (Ratio::from_id (r.get ()));
121                 }
122         } else {
123                 _scale = VideoContentScale (node->node_child ("Scale"));
124         }
125
126
127         if (node->optional_node_child ("ColourConversion")) {
128                 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"), version);
129         }
130         if (version >= 32) {
131                 _fade_in = node->number_child<Frame> ("FadeIn");
132                 _fade_out = node->number_child<Frame> ("FadeOut");
133         } else {
134                 _fade_in = _fade_out = 0;
135         }
136 }
137
138 VideoContent::VideoContent (shared_ptr<const Film> film, vector<shared_ptr<Content> > c)
139         : Content (film, c)
140         , _video_length (0)
141 {
142         shared_ptr<VideoContent> ref = dynamic_pointer_cast<VideoContent> (c[0]);
143         DCPOMATIC_ASSERT (ref);
144
145         for (size_t i = 0; i < c.size(); ++i) {
146                 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (c[i]);
147
148                 if (vc->video_size() != ref->video_size()) {
149                         throw JoinError (_("Content to be joined must have the same picture size."));
150                 }
151
152                 if (vc->video_frame_rate() != ref->video_frame_rate()) {
153                         throw JoinError (_("Content to be joined must have the same video frame rate."));
154                 }
155
156                 if (vc->video_frame_type() != ref->video_frame_type()) {
157                         throw JoinError (_("Content to be joined must have the same video frame type."));
158                 }
159
160                 if (vc->crop() != ref->crop()) {
161                         throw JoinError (_("Content to be joined must have the same crop."));
162                 }
163
164                 if (vc->scale() != ref->scale()) {
165                         throw JoinError (_("Content to be joined must have the same scale setting."));
166                 }
167
168                 if (vc->colour_conversion() != ref->colour_conversion()) {
169                         throw JoinError (_("Content to be joined must have the same colour conversion."));
170                 }
171
172                 if (vc->fade_in() != ref->fade_in() || vc->fade_out() != ref->fade_out()) {
173                         throw JoinError (_("Content to be joined must have the same fades."));
174                 }
175
176                 _video_length += vc->video_length ();
177         }
178
179         _video_size = ref->video_size ();
180         _video_frame_rate = ref->video_frame_rate ();
181         _video_frame_type = ref->video_frame_type ();
182         _crop = ref->crop ();
183         _scale = ref->scale ();
184         _colour_conversion = ref->colour_conversion ();
185         _fade_in = ref->fade_in ();
186         _fade_out = ref->fade_out ();
187 }
188
189 void
190 VideoContent::as_xml (xmlpp::Node* node) const
191 {
192         boost::mutex::scoped_lock lm (_mutex);
193         node->add_child("VideoLength")->add_child_text (raw_convert<string> (_video_length));
194         node->add_child("VideoWidth")->add_child_text (raw_convert<string> (_video_size.width));
195         node->add_child("VideoHeight")->add_child_text (raw_convert<string> (_video_size.height));
196         node->add_child("VideoFrameRate")->add_child_text (raw_convert<string> (_video_frame_rate));
197         node->add_child("VideoFrameType")->add_child_text (raw_convert<string> (static_cast<int> (_video_frame_type)));
198         if (_sample_aspect_ratio) {
199                 node->add_child("SampleAspectRatio")->add_child_text (raw_convert<string> (_sample_aspect_ratio.get ()));
200         }
201         _crop.as_xml (node);
202         _scale.as_xml (node->add_child("Scale"));
203         if (_colour_conversion) {
204                 _colour_conversion.get().as_xml (node->add_child("ColourConversion"));
205         }
206         node->add_child("FadeIn")->add_child_text (raw_convert<string> (_fade_in));
207         node->add_child("FadeOut")->add_child_text (raw_convert<string> (_fade_out));
208 }
209
210 void
211 VideoContent::set_default_colour_conversion ()
212 {
213         /* If there's no better offer we'll use Rec. 709 */
214         boost::mutex::scoped_lock lm (_mutex);
215         _colour_conversion = PresetColourConversion::from_id ("rec709").conversion;
216 }
217
218 void
219 VideoContent::take_from_video_examiner (shared_ptr<VideoExaminer> d)
220 {
221         /* These examiner calls could call other content methods which take a lock on the mutex */
222         dcp::Size const vs = d->video_size ();
223         optional<double> const vfr = d->video_frame_rate ();
224         Frame vl = d->video_length ();
225         optional<double> const ar = d->sample_aspect_ratio ();
226
227         {
228                 boost::mutex::scoped_lock lm (_mutex);
229                 _video_size = vs;
230                 /* Default video frame rate to 24fps if the examiner doesn't know */
231                 _video_frame_rate = vfr.get_value_or (24);
232                 _video_length = vl;
233                 _sample_aspect_ratio = ar;
234
235                 /* Guess correct scale from size and sample aspect ratio */
236                 _scale = VideoContentScale (
237                         Ratio::nearest_from_ratio (double (_video_size.width) * ar.get_value_or (1) / _video_size.height)
238                         );
239         }
240
241         shared_ptr<const Film> film = _film.lock ();
242         DCPOMATIC_ASSERT (film);
243         LOG_GENERAL ("Video length obtained from header as %1 frames", _video_length);
244
245         set_default_colour_conversion ();
246
247         signal_changed (VideoContentProperty::VIDEO_SIZE);
248         signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
249         signal_changed (VideoContentProperty::VIDEO_SCALE);
250         signal_changed (ContentProperty::LENGTH);
251 }
252
253 void
254 VideoContent::set_left_crop (int c)
255 {
256         {
257                 boost::mutex::scoped_lock lm (_mutex);
258
259                 if (_crop.left == c) {
260                         return;
261                 }
262
263                 _crop.left = c;
264         }
265
266         signal_changed (VideoContentProperty::VIDEO_CROP);
267 }
268
269 void
270 VideoContent::set_right_crop (int c)
271 {
272         {
273                 boost::mutex::scoped_lock lm (_mutex);
274                 if (_crop.right == c) {
275                         return;
276                 }
277
278                 _crop.right = c;
279         }
280
281         signal_changed (VideoContentProperty::VIDEO_CROP);
282 }
283
284 void
285 VideoContent::set_top_crop (int c)
286 {
287         {
288                 boost::mutex::scoped_lock lm (_mutex);
289                 if (_crop.top == c) {
290                         return;
291                 }
292
293                 _crop.top = c;
294         }
295
296         signal_changed (VideoContentProperty::VIDEO_CROP);
297 }
298
299 void
300 VideoContent::set_bottom_crop (int c)
301 {
302         {
303                 boost::mutex::scoped_lock lm (_mutex);
304                 if (_crop.bottom == c) {
305                         return;
306                 }
307
308                 _crop.bottom = c;
309         }
310
311         signal_changed (VideoContentProperty::VIDEO_CROP);
312 }
313
314 void
315 VideoContent::set_scale (VideoContentScale s)
316 {
317         {
318                 boost::mutex::scoped_lock lm (_mutex);
319                 if (_scale == s) {
320                         return;
321                 }
322
323                 _scale = s;
324         }
325
326         signal_changed (VideoContentProperty::VIDEO_SCALE);
327 }
328
329 /** @return string which includes everything about how this content looks */
330 string
331 VideoContent::identifier () const
332 {
333         SafeStringStream s;
334         s << Content::identifier()
335           << "_" << crop().left
336           << "_" << crop().right
337           << "_" << crop().top
338           << "_" << crop().bottom
339           << "_" << scale().id()
340           << "_" << _fade_in
341           << "_" << _fade_out;
342
343         if (colour_conversion()) {
344                 s << "_" << colour_conversion().get().identifier ();
345         }
346
347         return s.str ();
348 }
349
350 void
351 VideoContent::set_video_frame_type (VideoFrameType t)
352 {
353         {
354                 boost::mutex::scoped_lock lm (_mutex);
355                 _video_frame_type = t;
356         }
357
358         signal_changed (VideoContentProperty::VIDEO_FRAME_TYPE);
359 }
360
361 string
362 VideoContent::technical_summary () const
363 {
364         string s = String::compose (
365                 N_("video: length %1 frames, size %2x%3, rate %4"),
366                 video_length_after_3d_combine(),
367                 video_size().width,
368                 video_size().height,
369                 video_frame_rate()
370                 );
371
372         if (sample_aspect_ratio ()) {
373                 s += String::compose (N_(", sample aspect ratio %1"), (sample_aspect_ratio().get ()));
374         }
375
376         return s;
377 }
378
379 dcp::Size
380 VideoContent::video_size_after_3d_split () const
381 {
382         dcp::Size const s = video_size ();
383         switch (video_frame_type ()) {
384         case VIDEO_FRAME_TYPE_2D:
385         case VIDEO_FRAME_TYPE_3D_ALTERNATE:
386         case VIDEO_FRAME_TYPE_3D_LEFT:
387         case VIDEO_FRAME_TYPE_3D_RIGHT:
388                 return s;
389         case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
390                 return dcp::Size (s.width / 2, s.height);
391         case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
392                 return dcp::Size (s.width, s.height / 2);
393         }
394
395         DCPOMATIC_ASSERT (false);
396 }
397
398 void
399 VideoContent::unset_colour_conversion ()
400 {
401         {
402                 boost::mutex::scoped_lock lm (_mutex);
403                 _colour_conversion = boost::optional<ColourConversion> ();
404         }
405
406         signal_changed (VideoContentProperty::COLOUR_CONVERSION);
407 }
408
409 void
410 VideoContent::set_colour_conversion (ColourConversion c)
411 {
412         {
413                 boost::mutex::scoped_lock lm (_mutex);
414                 _colour_conversion = c;
415         }
416
417         signal_changed (VideoContentProperty::COLOUR_CONVERSION);
418 }
419
420 void
421 VideoContent::set_fade_in (Frame t)
422 {
423         {
424                 boost::mutex::scoped_lock lm (_mutex);
425                 _fade_in = t;
426         }
427
428         signal_changed (VideoContentProperty::VIDEO_FADE_IN);
429 }
430
431 void
432 VideoContent::set_fade_out (Frame t)
433 {
434         {
435                 boost::mutex::scoped_lock lm (_mutex);
436                 _fade_out = t;
437         }
438
439         signal_changed (VideoContentProperty::VIDEO_FADE_OUT);
440 }
441
442 /** @return Video size after 3D split and crop */
443 dcp::Size
444 VideoContent::video_size_after_crop () const
445 {
446         return crop().apply (video_size_after_3d_split ());
447 }
448
449 void
450 VideoContent::scale_and_crop_to_fit_width ()
451 {
452         shared_ptr<const Film> film = _film.lock ();
453         DCPOMATIC_ASSERT (film);
454
455         set_scale (VideoContentScale (film->container ()));
456
457         int const crop = max (0, int (video_size().height - double (film->frame_size().height) * video_size().width / film->frame_size().width));
458         set_top_crop (crop / 2);
459         set_bottom_crop (crop / 2);
460 }
461
462 void
463 VideoContent::scale_and_crop_to_fit_height ()
464 {
465         shared_ptr<const Film> film = _film.lock ();
466         DCPOMATIC_ASSERT (film);
467
468         set_scale (VideoContentScale (film->container ()));
469
470         int const crop = max (0, int (video_size().width - double (film->frame_size().width) * video_size().height / film->frame_size().height));
471         set_left_crop (crop / 2);
472         set_right_crop (crop / 2);
473 }
474
475 void
476 VideoContent::set_video_frame_rate (double r)
477 {
478         {
479                 boost::mutex::scoped_lock lm (_mutex);
480                 if (_video_frame_rate == r) {
481                         return;
482                 }
483
484                 _video_frame_rate = r;
485         }
486
487         signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
488 }
489
490 optional<double>
491 VideoContent::fade (Frame f) const
492 {
493         DCPOMATIC_ASSERT (f >= 0);
494
495         if (f < fade_in()) {
496                 return double (f) / fade_in();
497         }
498
499         Frame fade_out_start = video_length() - fade_out();
500         if (f >= fade_out_start) {
501                 return 1 - double (f - fade_out_start) / fade_out();
502         }
503
504         return optional<double> ();
505 }
506
507 string
508 VideoContent::processing_description () const
509 {
510         /* stringstream is OK here as this string is just for presentation to the user */
511         stringstream d;
512
513         if (video_size().width && video_size().height) {
514                 d << String::compose (
515                         _("Content video is %1x%2"),
516                         video_size_after_3d_split().width,
517                         video_size_after_3d_split().height
518                         );
519
520
521                 double ratio = video_size_after_3d_split().ratio ();
522
523                 if (sample_aspect_ratio ()) {
524                         d << ", " << _("pixel aspect ratio") << " " << fixed << setprecision(2) << sample_aspect_ratio().get () << ":1";
525                         ratio *= sample_aspect_ratio().get ();
526                 }
527
528                 d << "\n" << _("Display aspect ratio") << " " << fixed << setprecision(2) << ratio << ":1\n";
529         }
530
531         if ((crop().left || crop().right || crop().top || crop().bottom) && video_size() != dcp::Size (0, 0)) {
532                 dcp::Size cropped = video_size_after_crop ();
533                 d << String::compose (
534                         _("Cropped to %1x%2"),
535                         cropped.width, cropped.height
536                         );
537
538                 d << " (" << fixed << setprecision(2) << cropped.ratio () << ":1)\n";
539         }
540
541         shared_ptr<const Film> film = _film.lock ();
542         DCPOMATIC_ASSERT (film);
543
544         dcp::Size const container_size = film->frame_size ();
545         dcp::Size const scaled = scale().size (dynamic_pointer_cast<const VideoContent> (shared_from_this ()), container_size, container_size);
546
547         if (scaled != video_size_after_crop ()) {
548                 d << String::compose (
549                         _("Scaled to %1x%2"),
550                         scaled.width, scaled.height
551                         );
552
553                 d << " (" << fixed << setprecision(2) << scaled.ratio() << ":1)\n";
554         }
555
556         if (scaled != container_size) {
557                 d << String::compose (
558                         _("Padded with black to fit container %1 (%2x%3)"),
559                         film->container()->nickname (),
560                         container_size.width, container_size.height
561                         );
562
563                 d << " (" << fixed << setprecision(2) << container_size.ratio () << ":1)\n";
564         }
565
566         d << _("Content frame rate");
567         d << " " << fixed << setprecision(4) << video_frame_rate() << "\n";
568
569         FrameRateChange frc (video_frame_rate(), film->video_frame_rate ());
570         d << frc.description () << "\n";
571
572         return d.str ();
573 }
574
575 void
576 VideoContent::add_properties (list<pair<string, string> >& p) const
577 {
578         p.push_back (make_pair (_("Video length"), raw_convert<string> (video_length ()) + " " + _("video frames")));
579         p.push_back (make_pair (_("Video size"), raw_convert<string> (video_size().width) + "x" + raw_convert<string> (video_size().height)));
580         p.push_back (make_pair (_("Video frame rate"), raw_convert<string> (video_frame_rate()) + " " + _("frames per second")));
581 }