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