2 Copyright (C) 2013-2020 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 #include "video_content.h"
23 #include "video_examiner.h"
24 #include "compose.hpp"
27 #include "colour_conversion.h"
30 #include "exceptions.h"
31 #include "frame_rate_change.h"
33 #include "dcpomatic_log.h"
34 #include <dcp/raw_convert.h>
35 #include <libcxml/cxml.h>
36 #include <libxml++/libxml++.h>
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 int const VideoContentProperty::CUSTOM_RATIO = 9;
52 int const VideoContentProperty::CUSTOM_SIZE = 10;
55 using std::setprecision;
61 using std::setprecision;
64 using boost::shared_ptr;
65 using boost::optional;
66 using boost::dynamic_pointer_cast;
67 using dcp::raw_convert;
68 using namespace dcpomatic;
70 VideoContent::VideoContent (Content* parent)
71 : ContentPart (parent)
74 , _frame_type (VIDEO_FRAME_TYPE_2D)
78 , _range (VIDEO_RANGE_FULL)
83 shared_ptr<VideoContent>
84 VideoContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version)
86 if (!node->optional_number_child<int> ("VideoWidth")) {
87 return shared_ptr<VideoContent> ();
90 return shared_ptr<VideoContent> (new VideoContent (parent, node, version));
93 VideoContent::VideoContent (Content* parent, cxml::ConstNodePtr node, int version)
94 : ContentPart (parent)
96 _size.width = node->number_child<int> ("VideoWidth");
97 _size.height = node->number_child<int> ("VideoHeight");
99 /* Backwards compatibility */
100 optional<double> r = node->optional_number_child<double>("VideoFrameRate");
102 _parent->set_video_frame_rate (r.get ());
105 _use = node->optional_bool_child("Use").get_value_or(true);
106 _length = node->number_child<Frame> ("VideoLength");
109 /* Snapshot of the VideoFrameType enum at version 34 */
110 switch (node->number_child<int> ("VideoFrameType")) {
112 _frame_type = VIDEO_FRAME_TYPE_2D;
115 _frame_type = VIDEO_FRAME_TYPE_3D_LEFT_RIGHT;
118 _frame_type = VIDEO_FRAME_TYPE_3D_TOP_BOTTOM;
121 _frame_type = VIDEO_FRAME_TYPE_3D_ALTERNATE;
124 _frame_type = VIDEO_FRAME_TYPE_3D_LEFT;
127 _frame_type = VIDEO_FRAME_TYPE_3D_RIGHT;
131 _frame_type = string_to_video_frame_type (node->string_child ("VideoFrameType"));
134 _sample_aspect_ratio = node->optional_number_child<double> ("SampleAspectRatio");
135 _crop.left = node->number_child<int> ("LeftCrop");
136 _crop.right = node->number_child<int> ("RightCrop");
137 _crop.top = node->number_child<int> ("TopCrop");
138 _crop.bottom = node->number_child<int> ("BottomCrop");
141 optional<string> r = node->optional_string_child ("Ratio");
143 _legacy_ratio = Ratio::from_id(r.get())->ratio();
145 } else if (version <= 37) {
146 optional<string> ratio = node->node_child("Scale")->optional_string_child("Ratio");
148 _legacy_ratio = Ratio::from_id(ratio.get())->ratio();
150 optional<bool> scale = node->node_child("Scale")->optional_bool_child("Scale");
153 /* This is what we used to call "no stretch" */
154 _legacy_ratio = _size.ratio();
156 /* This is what we used to call "no scale" */
157 _custom_size = _size;
162 _custom_ratio = node->optional_number_child<float>("CustomRatio");
163 if (node->optional_number_child<int>("CustomWidth")) {
164 _custom_size = dcp::Size (node->number_child<int>("CustomWidth"), node->number_child<int>("CustomHeight"));
168 if (node->optional_node_child ("ColourConversion")) {
169 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"), version);
172 _yuv = node->optional_bool_child("YUV").get_value_or (true);
175 _fade_in = node->number_child<Frame> ("FadeIn");
176 _fade_out = node->number_child<Frame> ("FadeOut");
178 _fade_in = _fade_out = 0;
181 _range = VIDEO_RANGE_FULL;
182 if (node->optional_string_child("Range").get_value_or("full") == "video") {
183 _range = VIDEO_RANGE_VIDEO;
187 VideoContent::VideoContent (Content* parent, vector<shared_ptr<Content> > c)
188 : ContentPart (parent)
192 shared_ptr<VideoContent> ref = c[0]->video;
193 DCPOMATIC_ASSERT (ref);
195 for (size_t i = 1; i < c.size(); ++i) {
197 if (c[i]->video->use() != ref->use()) {
198 throw JoinError (_("Content to be joined must have all its video used or not used."));
201 if (c[i]->video->size() != ref->size()) {
202 throw JoinError (_("Content to be joined must have the same picture size."));
205 if (c[i]->video->frame_type() != ref->frame_type()) {
206 throw JoinError (_("Content to be joined must have the same video frame type."));
209 if (c[i]->video->crop() != ref->crop()) {
210 throw JoinError (_("Content to be joined must have the same crop."));
213 if (c[i]->video->custom_ratio() != ref->custom_ratio()) {
214 throw JoinError (_("Content to be joined must have the same custom ratio setting."));
217 if (c[i]->video->custom_size() != ref->custom_size()) {
218 throw JoinError (_("Content to be joined must have the same custom size setting."));
221 if (c[i]->video->colour_conversion() != ref->colour_conversion()) {
222 throw JoinError (_("Content to be joined must have the same colour conversion."));
225 if (c[i]->video->fade_in() != ref->fade_in() || c[i]->video->fade_out() != ref->fade_out()) {
226 throw JoinError (_("Content to be joined must have the same fades."));
229 _length += c[i]->video->length ();
231 if (c[i]->video->yuv ()) {
237 _size = ref->size ();
238 _frame_type = ref->frame_type ();
239 _crop = ref->crop ();
240 _custom_ratio = ref->custom_ratio ();
241 _colour_conversion = ref->colour_conversion ();
242 _fade_in = ref->fade_in ();
243 _fade_out = ref->fade_out ();
244 _range = ref->range ();
248 VideoContent::as_xml (xmlpp::Node* node) const
250 boost::mutex::scoped_lock lm (_mutex);
251 node->add_child("Use")->add_child_text (_use ? "1" : "0");
252 node->add_child("VideoLength")->add_child_text (raw_convert<string> (_length));
253 node->add_child("VideoWidth")->add_child_text (raw_convert<string> (_size.width));
254 node->add_child("VideoHeight")->add_child_text (raw_convert<string> (_size.height));
255 node->add_child("VideoFrameType")->add_child_text (video_frame_type_to_string (_frame_type));
256 if (_sample_aspect_ratio) {
257 node->add_child("SampleAspectRatio")->add_child_text (raw_convert<string> (_sample_aspect_ratio.get ()));
261 node->add_child("CustomRatio")->add_child_text(raw_convert<string>(*_custom_ratio));
264 node->add_child("CustomWidth")->add_child_text(raw_convert<string>(_custom_size->width));
265 node->add_child("CustomHeight")->add_child_text(raw_convert<string>(_custom_size->height));
267 if (_colour_conversion) {
268 _colour_conversion.get().as_xml (node->add_child("ColourConversion"));
270 node->add_child("YUV")->add_child_text (_yuv ? "1" : "0");
271 node->add_child("FadeIn")->add_child_text (raw_convert<string> (_fade_in));
272 node->add_child("FadeOut")->add_child_text (raw_convert<string> (_fade_out));
273 node->add_child("Range")->add_child_text(_range == VIDEO_RANGE_FULL ? "full" : "video");
277 VideoContent::take_from_examiner (shared_ptr<VideoExaminer> d)
279 /* These examiner calls could call other content methods which take a lock on the mutex */
280 dcp::Size const vs = d->video_size ();
281 Frame vl = d->video_length ();
282 optional<double> const ar = d->sample_aspect_ratio ();
283 bool const yuv = d->yuv ();
284 VideoRange const range = d->range ();
286 ChangeSignaller<Content> cc1 (_parent, VideoContentProperty::SIZE);
287 ChangeSignaller<Content> cc2 (_parent, VideoContentProperty::SCALE);
288 ChangeSignaller<Content> cc3 (_parent, ContentProperty::LENGTH);
289 ChangeSignaller<Content> cc4 (_parent, VideoContentProperty::RANGE);
292 boost::mutex::scoped_lock lm (_mutex);
295 _sample_aspect_ratio = ar;
300 LOG_GENERAL ("Video length obtained from header as %1 frames", _length);
302 if (d->video_frame_rate()) {
303 _parent->set_video_frame_rate (d->video_frame_rate().get());
307 /** @return string which includes everything about how this content looks */
309 VideoContent::identifier () const
313 buffer, sizeof(buffer), "%d_%d_%d_%d_%d_%f_%d_%d%" PRId64 "_%" PRId64 "_%d",
319 _custom_ratio.get_value_or(0),
320 _custom_size ? _custom_size->width : 0,
321 _custom_size ? _custom_size->height : 0,
324 _range == VIDEO_RANGE_FULL ? 0 : 1
329 if (colour_conversion()) {
330 s += "_" + colour_conversion().get().identifier ();
337 VideoContent::technical_summary () const
339 string s = String::compose (
340 N_("video: length %1 frames, size %2x%3"),
341 length_after_3d_combine(),
346 if (sample_aspect_ratio ()) {
347 s += String::compose (N_(", sample aspect ratio %1"), (sample_aspect_ratio().get ()));
354 VideoContent::size_after_3d_split () const
356 dcp::Size const s = size ();
357 switch (frame_type ()) {
358 case VIDEO_FRAME_TYPE_2D:
359 case VIDEO_FRAME_TYPE_3D:
360 case VIDEO_FRAME_TYPE_3D_ALTERNATE:
361 case VIDEO_FRAME_TYPE_3D_LEFT:
362 case VIDEO_FRAME_TYPE_3D_RIGHT:
364 case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
365 return dcp::Size (s.width / 2, s.height);
366 case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
367 return dcp::Size (s.width, s.height / 2);
370 DCPOMATIC_ASSERT (false);
373 /** @return Video size after 3D split and crop */
375 VideoContent::size_after_crop () const
377 return crop().apply (size_after_3d_split ());
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.
385 VideoContent::fade (shared_ptr<const Film> film, Frame f) const
387 DCPOMATIC_ASSERT (f >= 0);
389 double const vfr = _parent->active_video_frame_rate(film);
391 Frame const ts = _parent->trim_start().frames_round(vfr);
392 if ((f - ts) < fade_in()) {
393 return double (f - ts) / fade_in();
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();
401 return optional<double> ();
405 VideoContent::processing_description (shared_ptr<const Film> film)
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
418 double ratio = size_after_3d_split().ratio ();
420 if (sample_aspect_ratio ()) {
421 snprintf (buffer, sizeof(buffer), _(", pixel aspect ratio %.2f:1"), sample_aspect_ratio().get());
423 ratio *= sample_aspect_ratio().get ();
426 snprintf (buffer, sizeof(buffer), _("\nDisplay aspect ratio %.2f:1"), ratio);
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
437 snprintf (buffer, sizeof(buffer), " (%.2f:1)", cropped.ratio());
441 dcp::Size const container_size = film->frame_size ();
442 dcp::Size const scaled = scaled_size (container_size);
444 if (scaled != size_after_crop ()) {
445 d += String::compose (
446 _("\nScaled to %1x%2"),
447 scaled.width, scaled.height
450 snprintf (buffer, sizeof(buffer), _(" (%.2f:1)"), scaled.ratio());
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
461 snprintf (buffer, sizeof(buffer), _(" (%.2f:1)"), container_size.ratio());
465 if (_parent->video_frame_rate()) {
466 double const vfr = _parent->video_frame_rate().get ();
468 snprintf (buffer, sizeof(buffer), _("\nContent frame rate %.4f\n"), vfr);
471 FrameRateChange frc (vfr, film->video_frame_rate ());
472 d += frc.description ();
479 VideoContent::add_properties (list<UserProperty>& p) const
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)));
486 VideoContent::set_length (Frame len)
488 maybe_set (_length, len, ContentProperty::LENGTH);
492 VideoContent::set_left_crop (int c)
494 maybe_set (_crop.left, c, VideoContentProperty::CROP);
498 VideoContent::set_right_crop (int c)
500 maybe_set (_crop.right, c, VideoContentProperty::CROP);
504 VideoContent::set_top_crop (int c)
506 maybe_set (_crop.top, c, VideoContentProperty::CROP);
510 VideoContent::set_bottom_crop (int c)
512 maybe_set (_crop.bottom, c, VideoContentProperty::CROP);
517 VideoContent::set_frame_type (VideoFrameType t)
519 maybe_set (_frame_type, t, VideoContentProperty::FRAME_TYPE);
523 VideoContent::unset_colour_conversion ()
525 maybe_set (_colour_conversion, boost::optional<ColourConversion> (), VideoContentProperty::COLOUR_CONVERSION);
529 VideoContent::set_colour_conversion (ColourConversion c)
531 maybe_set (_colour_conversion, c, VideoContentProperty::COLOUR_CONVERSION);
535 VideoContent::set_fade_in (Frame t)
537 maybe_set (_fade_in, t, VideoContentProperty::FADE_IN);
541 VideoContent::set_fade_out (Frame t)
543 maybe_set (_fade_out, t, VideoContentProperty::FADE_OUT);
547 VideoContent::set_range (VideoRange r)
549 maybe_set (_range, r, VideoContentProperty::RANGE);
553 VideoContent::set_use (bool u)
555 maybe_set (_use, u, VideoContentProperty::USE);
559 VideoContent::take_settings_from (shared_ptr<const VideoContent> c)
561 if (c->_colour_conversion) {
562 set_colour_conversion (c->_colour_conversion.get());
564 unset_colour_conversion ();
567 set_frame_type (c->_frame_type);
568 set_left_crop (c->_crop.left);
569 set_right_crop (c->_crop.right);
570 set_top_crop (c->_crop.top);
571 set_bottom_crop (c->_crop.bottom);
572 set_custom_ratio (c->_custom_ratio);
573 set_custom_size (c->_custom_size);
574 set_fade_in (c->_fade_in);
575 set_fade_out (c->_fade_out);
579 VideoContent::modify_position (shared_ptr<const Film> film, DCPTime& pos) const
581 pos = pos.round (film->video_frame_rate());
585 VideoContent::modify_trim_start (ContentTime& trim) const
587 if (_parent->video_frame_rate()) {
588 trim = trim.round (_parent->video_frame_rate().get());
593 /** @param film_container The size of the container for the DCP that we are working on */
595 VideoContent::scaled_size (dcp::Size film_container)
598 return fit_ratio_within(*_custom_ratio, film_container);
602 return *_custom_size;
605 dcp::Size size = size_after_crop ();
606 size.width *= _sample_aspect_ratio.get_value_or(1);
608 /* This is what we will return unless there is any legacy stuff to take into account */
609 dcp::Size auto_size = fit_ratio_within (size.ratio(), film_container);
612 if (fit_ratio_within(*_legacy_ratio, film_container) != auto_size) {
613 _custom_ratio = *_legacy_ratio;
614 _legacy_ratio = optional<float>();
615 return fit_ratio_within(*_custom_ratio, film_container);
617 _legacy_ratio = boost::optional<float>();
625 VideoContent::set_custom_ratio (optional<float> ratio)
627 maybe_set (_custom_ratio, ratio, VideoContentProperty::CUSTOM_RATIO);
632 VideoContent::set_custom_size (optional<dcp::Size> size)
634 maybe_set (_custom_size, size, VideoContentProperty::CUSTOM_SIZE);