Extract common code out into kdm_for_screen()
[dcpomatic.git] / src / lib / video_content.cc
1 /*
2     Copyright (C) 2013-2019 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "video_content.h"
22 #include "content.h"
23 #include "video_examiner.h"
24 #include "compose.hpp"
25 #include "ratio.h"
26 #include "config.h"
27 #include "colour_conversion.h"
28 #include "util.h"
29 #include "film.h"
30 #include "exceptions.h"
31 #include "frame_rate_change.h"
32 #include "log.h"
33 #include "dcpomatic_log.h"
34 #include <dcp/raw_convert.h>
35 #include <libcxml/cxml.h>
36 #include <libxml++/libxml++.h>
37 #include <iomanip>
38 #include <iostream>
39
40 #include "i18n.h"
41
42 int const VideoContentProperty::USE               = 0;
43 int const VideoContentProperty::SIZE              = 1;
44 int const VideoContentProperty::FRAME_TYPE        = 2;
45 int const VideoContentProperty::CROP              = 3;
46 int const VideoContentProperty::SCALE             = 4;
47 int const VideoContentProperty::COLOUR_CONVERSION = 5;
48 int const VideoContentProperty::FADE_IN           = 6;
49 int const VideoContentProperty::FADE_OUT          = 7;
50 int const VideoContentProperty::RANGE             = 8;
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::fixed;
59 using std::setprecision;
60 using std::list;
61 using std::pair;
62 using boost::shared_ptr;
63 using boost::optional;
64 using boost::dynamic_pointer_cast;
65 using dcp::raw_convert;
66 using namespace dcpomatic;
67
68 VideoContent::VideoContent (Content* parent)
69         : ContentPart (parent)
70         , _use (true)
71         , _length (0)
72         , _frame_type (VIDEO_FRAME_TYPE_2D)
73         , _scale (VideoContentScale (Ratio::from_id ("178")))
74         , _yuv (true)
75         , _fade_in (0)
76         , _fade_out (0)
77         , _range (VIDEO_RANGE_FULL)
78 {
79
80 }
81
82 shared_ptr<VideoContent>
83 VideoContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version)
84 {
85         if (!node->optional_number_child<int> ("VideoWidth")) {
86                 return shared_ptr<VideoContent> ();
87         }
88
89         return shared_ptr<VideoContent> (new VideoContent (parent, node, version));
90 }
91
92 VideoContent::VideoContent (Content* parent, cxml::ConstNodePtr node, int version)
93         : ContentPart (parent)
94 {
95         _size.width = node->number_child<int> ("VideoWidth");
96         _size.height = node->number_child<int> ("VideoHeight");
97
98         /* Backwards compatibility */
99         optional<double> r = node->optional_number_child<double>("VideoFrameRate");
100         if (r) {
101                 _parent->set_video_frame_rate (r.get ());
102         }
103
104         _use = node->optional_bool_child("Use").get_value_or(true);
105         _length = node->number_child<Frame> ("VideoLength");
106
107         if (version <= 34) {
108                 /* Snapshot of the VideoFrameType enum at version 34 */
109                 switch (node->number_child<int> ("VideoFrameType")) {
110                 case 0:
111                         _frame_type = VIDEO_FRAME_TYPE_2D;
112                         break;
113                 case 1:
114                         _frame_type = VIDEO_FRAME_TYPE_3D_LEFT_RIGHT;
115                         break;
116                 case 2:
117                         _frame_type = VIDEO_FRAME_TYPE_3D_TOP_BOTTOM;
118                         break;
119                 case 3:
120                         _frame_type = VIDEO_FRAME_TYPE_3D_ALTERNATE;
121                         break;
122                 case 4:
123                         _frame_type = VIDEO_FRAME_TYPE_3D_LEFT;
124                         break;
125                 case 5:
126                         _frame_type = VIDEO_FRAME_TYPE_3D_RIGHT;
127                         break;
128                 }
129         } else {
130                 _frame_type = string_to_video_frame_type (node->string_child ("VideoFrameType"));
131         }
132
133         _sample_aspect_ratio = node->optional_number_child<double> ("SampleAspectRatio");
134         _crop.left = node->number_child<int> ("LeftCrop");
135         _crop.right = node->number_child<int> ("RightCrop");
136         _crop.top = node->number_child<int> ("TopCrop");
137         _crop.bottom = node->number_child<int> ("BottomCrop");
138
139         if (version <= 7) {
140                 optional<string> r = node->optional_string_child ("Ratio");
141                 if (r) {
142                         _scale = VideoContentScale (Ratio::from_id (r.get ()));
143                 }
144         } else {
145                 _scale = VideoContentScale (node->node_child ("Scale"));
146         }
147
148         if (node->optional_node_child ("ColourConversion")) {
149                 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"), version);
150         }
151
152         _yuv = node->optional_bool_child("YUV").get_value_or (true);
153
154         if (version >= 32) {
155                 _fade_in = node->number_child<Frame> ("FadeIn");
156                 _fade_out = node->number_child<Frame> ("FadeOut");
157         } else {
158                 _fade_in = _fade_out = 0;
159         }
160
161         _range = VIDEO_RANGE_FULL;
162         if (node->optional_string_child("Range").get_value_or("full") == "video") {
163                 _range = VIDEO_RANGE_VIDEO;
164         }
165 }
166
167 VideoContent::VideoContent (Content* parent, vector<shared_ptr<Content> > c)
168         : ContentPart (parent)
169         , _length (0)
170         , _yuv (false)
171 {
172         shared_ptr<VideoContent> ref = c[0]->video;
173         DCPOMATIC_ASSERT (ref);
174
175         for (size_t i = 1; i < c.size(); ++i) {
176
177                 if (c[i]->video->use() != ref->use()) {
178                         throw JoinError (_("Content to be joined must have all its video used or not used."));
179                 }
180
181                 if (c[i]->video->size() != ref->size()) {
182                         throw JoinError (_("Content to be joined must have the same picture size."));
183                 }
184
185                 if (c[i]->video->frame_type() != ref->frame_type()) {
186                         throw JoinError (_("Content to be joined must have the same video frame type."));
187                 }
188
189                 if (c[i]->video->crop() != ref->crop()) {
190                         throw JoinError (_("Content to be joined must have the same crop."));
191                 }
192
193                 if (c[i]->video->scale() != ref->scale()) {
194                         throw JoinError (_("Content to be joined must have the same scale setting."));
195                 }
196
197                 if (c[i]->video->colour_conversion() != ref->colour_conversion()) {
198                         throw JoinError (_("Content to be joined must have the same colour conversion."));
199                 }
200
201                 if (c[i]->video->fade_in() != ref->fade_in() || c[i]->video->fade_out() != ref->fade_out()) {
202                         throw JoinError (_("Content to be joined must have the same fades."));
203                 }
204
205                 _length += c[i]->video->length ();
206
207                 if (c[i]->video->yuv ()) {
208                         _yuv = true;
209                 }
210         }
211
212         _use = ref->use ();
213         _size = ref->size ();
214         _frame_type = ref->frame_type ();
215         _crop = ref->crop ();
216         _scale = ref->scale ();
217         _colour_conversion = ref->colour_conversion ();
218         _fade_in = ref->fade_in ();
219         _fade_out = ref->fade_out ();
220         _range = ref->range ();
221 }
222
223 void
224 VideoContent::as_xml (xmlpp::Node* node) const
225 {
226         boost::mutex::scoped_lock lm (_mutex);
227         node->add_child("Use")->add_child_text (_use ? "1" : "0");
228         node->add_child("VideoLength")->add_child_text (raw_convert<string> (_length));
229         node->add_child("VideoWidth")->add_child_text (raw_convert<string> (_size.width));
230         node->add_child("VideoHeight")->add_child_text (raw_convert<string> (_size.height));
231         node->add_child("VideoFrameType")->add_child_text (video_frame_type_to_string (_frame_type));
232         if (_sample_aspect_ratio) {
233                 node->add_child("SampleAspectRatio")->add_child_text (raw_convert<string> (_sample_aspect_ratio.get ()));
234         }
235         _crop.as_xml (node);
236         _scale.as_xml (node->add_child("Scale"));
237         if (_colour_conversion) {
238                 _colour_conversion.get().as_xml (node->add_child("ColourConversion"));
239         }
240         node->add_child("YUV")->add_child_text (_yuv ? "1" : "0");
241         node->add_child("FadeIn")->add_child_text (raw_convert<string> (_fade_in));
242         node->add_child("FadeOut")->add_child_text (raw_convert<string> (_fade_out));
243         node->add_child("Range")->add_child_text(_range == VIDEO_RANGE_FULL ? "full" : "video");
244 }
245
246 void
247 VideoContent::take_from_examiner (shared_ptr<VideoExaminer> d)
248 {
249         /* These examiner calls could call other content methods which take a lock on the mutex */
250         dcp::Size const vs = d->video_size ();
251         Frame vl = d->video_length ();
252         optional<double> const ar = d->sample_aspect_ratio ();
253         bool const yuv = d->yuv ();
254         VideoRange const range = d->range ();
255
256         ChangeSignaller<Content> cc1 (_parent, VideoContentProperty::SIZE);
257         ChangeSignaller<Content> cc2 (_parent, VideoContentProperty::SCALE);
258         ChangeSignaller<Content> cc3 (_parent, ContentProperty::LENGTH);
259         ChangeSignaller<Content> cc4 (_parent, VideoContentProperty::RANGE);
260
261         {
262                 boost::mutex::scoped_lock lm (_mutex);
263                 _size = vs;
264                 _length = vl;
265                 _sample_aspect_ratio = ar;
266                 _yuv = yuv;
267                 _range = range;
268
269                 if (Config::instance()->default_scale_to ()) {
270                         _scale = VideoContentScale (Config::instance()->default_scale_to ());
271                 } else {
272                         /* Guess correct scale from size and sample aspect ratio */
273                         _scale = VideoContentScale (
274                                 Ratio::nearest_from_ratio (double (_size.width) * ar.get_value_or (1) / _size.height)
275                                 );
276                 }
277         }
278
279         LOG_GENERAL ("Video length obtained from header as %1 frames", _length);
280
281         if (d->video_frame_rate()) {
282                 _parent->set_video_frame_rate (d->video_frame_rate().get());
283         }
284 }
285
286 /** @return string which includes everything about how this content looks */
287 string
288 VideoContent::identifier () const
289 {
290         char buffer[256];
291         snprintf (
292                 buffer, sizeof(buffer), "%d_%d_%d_%d_%d_%s_%" PRId64 "_%" PRId64 "_%d",
293                 (_use ? 1 : 0),
294                 crop().left,
295                 crop().right,
296                 crop().top,
297                 crop().bottom,
298                 scale().id().c_str(),
299                 _fade_in,
300                 _fade_out,
301                 _range == VIDEO_RANGE_FULL ? 0 : 1
302                 );
303
304         string s (buffer);
305
306         if (colour_conversion()) {
307                 s += "_" + colour_conversion().get().identifier ();
308         }
309
310         return s;
311 }
312
313 string
314 VideoContent::technical_summary () const
315 {
316         string s = String::compose (
317                 N_("video: length %1 frames, size %2x%3"),
318                 length_after_3d_combine(),
319                 size().width,
320                 size().height
321                 );
322
323         if (sample_aspect_ratio ()) {
324                 s += String::compose (N_(", sample aspect ratio %1"), (sample_aspect_ratio().get ()));
325         }
326
327         return s;
328 }
329
330 dcp::Size
331 VideoContent::size_after_3d_split () const
332 {
333         dcp::Size const s = size ();
334         switch (frame_type ()) {
335         case VIDEO_FRAME_TYPE_2D:
336         case VIDEO_FRAME_TYPE_3D:
337         case VIDEO_FRAME_TYPE_3D_ALTERNATE:
338         case VIDEO_FRAME_TYPE_3D_LEFT:
339         case VIDEO_FRAME_TYPE_3D_RIGHT:
340                 return s;
341         case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
342                 return dcp::Size (s.width / 2, s.height);
343         case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
344                 return dcp::Size (s.width, s.height / 2);
345         }
346
347         DCPOMATIC_ASSERT (false);
348 }
349
350 /** @return Video size after 3D split and crop */
351 dcp::Size
352 VideoContent::size_after_crop () const
353 {
354         return crop().apply (size_after_3d_split ());
355 }
356
357 void
358 VideoContent::scale_and_crop_to_fit_width (shared_ptr<const Film> film)
359 {
360         set_scale (VideoContentScale(film->container()));
361
362         int const crop = max (0, int (size().height - double (film->frame_size().height) * size().width / film->frame_size().width));
363         set_left_crop (0);
364         set_right_crop (0);
365         set_top_crop (crop / 2);
366         set_bottom_crop (crop / 2);
367 }
368
369 void
370 VideoContent::scale_and_crop_to_fit_height (shared_ptr<const Film> film)
371 {
372         set_scale (VideoContentScale(film->container()));
373
374         int const crop = max (0, int (size().width - double (film->frame_size().width) * size().height / film->frame_size().height));
375         set_left_crop (crop / 2);
376         set_right_crop (crop / 2);
377         set_top_crop (0);
378         set_bottom_crop (0);
379 }
380
381 /** @param f Frame index within the whole (untrimmed) content.
382  *  @return Fade factor (between 0 and 1) or unset if there is no fade.
383  */
384 optional<double>
385 VideoContent::fade (shared_ptr<const Film> film, Frame f) const
386 {
387         DCPOMATIC_ASSERT (f >= 0);
388
389         double const vfr = _parent->active_video_frame_rate(film);
390
391         Frame const ts = _parent->trim_start().frames_round(vfr);
392         if ((f - ts) < fade_in()) {
393                 return double (f - ts) / fade_in();
394         }
395
396         Frame fade_out_start = length() - _parent->trim_end().frames_round(vfr) - fade_out();
397         if (f >= fade_out_start) {
398                 return 1 - double (f - fade_out_start) / fade_out();
399         }
400
401         return optional<double> ();
402 }
403
404 string
405 VideoContent::processing_description (shared_ptr<const Film> film) const
406 {
407         string d;
408         char buffer[256];
409
410         if (size().width && size().height) {
411                 d += String::compose (
412                         _("Content video is %1x%2"),
413                         size_after_3d_split().width,
414                         size_after_3d_split().height
415                         );
416
417
418                 double ratio = size_after_3d_split().ratio ();
419
420                 if (sample_aspect_ratio ()) {
421                         snprintf (buffer, sizeof(buffer), _(", pixel aspect ratio %.2f:1"), sample_aspect_ratio().get());
422                         d += buffer;
423                         ratio *= sample_aspect_ratio().get ();
424                 }
425
426                 snprintf (buffer, sizeof(buffer), _("\nDisplay aspect ratio %.2f:1"), ratio);
427                 d += buffer;
428         }
429
430         if ((crop().left || crop().right || crop().top || crop().bottom) && size() != dcp::Size (0, 0)) {
431                 dcp::Size cropped = size_after_crop ();
432                 d += String::compose (
433                         _("\nCropped to %1x%2"),
434                         cropped.width, cropped.height
435                         );
436
437                 snprintf (buffer, sizeof(buffer), " (%.2f:1)", cropped.ratio());
438                 d += buffer;
439         }
440
441         dcp::Size const container_size = film->frame_size ();
442         dcp::Size const scaled = scale().size (shared_from_this(), container_size, container_size);
443
444         if (scaled != size_after_crop ()) {
445                 d += String::compose (
446                         _("\nScaled to %1x%2"),
447                         scaled.width, scaled.height
448                         );
449
450                 snprintf (buffer, sizeof(buffer), _(" (%.2f:1)"), scaled.ratio());
451                 d += buffer;
452         }
453
454         if (scaled != container_size) {
455                 d += String::compose (
456                         _("\nPadded with black to fit container %1 (%2x%3)"),
457                         film->container()->container_nickname (),
458                         container_size.width, container_size.height
459                         );
460
461                 snprintf (buffer, sizeof(buffer), _(" (%.2f:1)"), container_size.ratio());
462                 d += buffer;
463         }
464
465         if (_parent->video_frame_rate()) {
466                 double const vfr = _parent->video_frame_rate().get ();
467
468                 snprintf (buffer, sizeof(buffer), _("\nContent frame rate %.4f\n"), vfr);
469                 d += buffer;
470
471                 FrameRateChange frc (vfr, film->video_frame_rate ());
472                 d += frc.description ();
473         }
474
475         return d;
476 }
477
478 void
479 VideoContent::add_properties (list<UserProperty>& p) const
480 {
481         p.push_back (UserProperty (UserProperty::VIDEO, _("Length"), length (), _("video frames")));
482         p.push_back (UserProperty (UserProperty::VIDEO, _("Size"), String::compose ("%1x%2", size().width, size().height)));
483 }
484
485 void
486 VideoContent::set_length (Frame len)
487 {
488         maybe_set (_length, len, ContentProperty::LENGTH);
489 }
490
491 void
492 VideoContent::set_left_crop (int c)
493 {
494         maybe_set (_crop.left, c, VideoContentProperty::CROP);
495 }
496
497 void
498 VideoContent::set_right_crop (int c)
499 {
500         maybe_set (_crop.right, c, VideoContentProperty::CROP);
501 }
502
503 void
504 VideoContent::set_top_crop (int c)
505 {
506         maybe_set (_crop.top, c, VideoContentProperty::CROP);
507 }
508
509 void
510 VideoContent::set_bottom_crop (int c)
511 {
512         maybe_set (_crop.bottom, c, VideoContentProperty::CROP);
513 }
514
515 void
516 VideoContent::set_scale (VideoContentScale s)
517 {
518         maybe_set (_scale, s, VideoContentProperty::SCALE);
519 }
520
521 void
522 VideoContent::set_frame_type (VideoFrameType t)
523 {
524         maybe_set (_frame_type, t, VideoContentProperty::FRAME_TYPE);
525 }
526
527 void
528 VideoContent::unset_colour_conversion ()
529 {
530         maybe_set (_colour_conversion, boost::optional<ColourConversion> (), VideoContentProperty::COLOUR_CONVERSION);
531 }
532
533 void
534 VideoContent::set_colour_conversion (ColourConversion c)
535 {
536         maybe_set (_colour_conversion, c, VideoContentProperty::COLOUR_CONVERSION);
537 }
538
539 void
540 VideoContent::set_fade_in (Frame t)
541 {
542         maybe_set (_fade_in, t, VideoContentProperty::FADE_IN);
543 }
544
545 void
546 VideoContent::set_fade_out (Frame t)
547 {
548         maybe_set (_fade_out, t, VideoContentProperty::FADE_OUT);
549 }
550
551 void
552 VideoContent::set_range (VideoRange r)
553 {
554         maybe_set (_range, r, VideoContentProperty::RANGE);
555 }
556
557 void
558 VideoContent::set_use (bool u)
559 {
560         maybe_set (_use, u, VideoContentProperty::USE);
561 }
562
563 void
564 VideoContent::take_settings_from (shared_ptr<const VideoContent> c)
565 {
566         if (c->_colour_conversion) {
567                 set_colour_conversion (c->_colour_conversion.get());
568         } else {
569                 unset_colour_conversion ();
570         }
571         set_use (c->_use);
572         set_frame_type (c->_frame_type);
573         set_left_crop (c->_crop.left);
574         set_right_crop (c->_crop.right);
575         set_top_crop (c->_crop.top);
576         set_bottom_crop (c->_crop.bottom);
577         set_scale (c->_scale);
578         set_fade_in (c->_fade_in);
579         set_fade_out (c->_fade_out);
580 }
581
582 void
583 VideoContent::modify_position (shared_ptr<const Film> film, DCPTime& pos) const
584 {
585         pos = pos.round (film->video_frame_rate());
586 }
587
588 void
589 VideoContent::modify_trim_start (ContentTime& trim) const
590 {
591         if (_parent->video_frame_rate()) {
592                 trim = trim.round (_parent->video_frame_rate().get());
593         }
594 }