2 Copyright (C) 2013-2021 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/>.
22 #include "colour_conversion.h"
23 #include "compose.hpp"
26 #include "dcpomatic_log.h"
27 #include "exceptions.h"
29 #include "frame_rate_change.h"
33 #include "video_content.h"
34 #include "video_examiner.h"
35 #include <dcp/raw_convert.h>
36 #include <libcxml/cxml.h>
37 #include <libxml++/libxml++.h>
44 int const VideoContentProperty::USE = 0;
45 int const VideoContentProperty::SIZE = 1;
46 int const VideoContentProperty::FRAME_TYPE = 2;
47 int const VideoContentProperty::CROP = 3;
48 int const VideoContentProperty::COLOUR_CONVERSION = 4;
49 int const VideoContentProperty::FADE_IN = 5;
50 int const VideoContentProperty::FADE_OUT = 6;
51 int const VideoContentProperty::RANGE = 7;
52 int const VideoContentProperty::CUSTOM_RATIO = 8;
53 int const VideoContentProperty::CUSTOM_SIZE = 9;
54 int const VideoContentProperty::BURNT_SUBTITLE_LANGUAGE = 10;
58 using std::dynamic_pointer_cast;
61 using std::make_shared;
65 using std::setprecision;
66 using std::setprecision;
67 using std::shared_ptr;
70 using boost::optional;
71 using dcp::raw_convert;
72 using namespace dcpomatic;
75 VideoContent::VideoContent (Content* parent)
76 : ContentPart (parent)
79 , _frame_type (VideoFrameType::TWO_D)
83 , _range (VideoRange::FULL)
89 shared_ptr<VideoContent>
90 VideoContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version)
92 if (!node->optional_number_child<int> ("VideoWidth")) {
96 return make_shared<VideoContent>(parent, node, version);
100 VideoContent::VideoContent (Content* parent, cxml::ConstNodePtr node, int version)
101 : ContentPart (parent)
103 _size.width = node->number_child<int> ("VideoWidth");
104 _size.height = node->number_child<int> ("VideoHeight");
106 /* Backwards compatibility */
107 auto r = node->optional_number_child<double>("VideoFrameRate");
109 _parent->set_video_frame_rate (r.get ());
112 _use = node->optional_bool_child("Use").get_value_or(true);
113 _length = node->number_child<Frame> ("VideoLength");
116 /* Snapshot of the VideoFrameType enum at version 34 */
117 switch (node->number_child<int> ("VideoFrameType")) {
119 _frame_type = VideoFrameType::TWO_D;
122 _frame_type = VideoFrameType::THREE_D_LEFT_RIGHT;
125 _frame_type = VideoFrameType::THREE_D_TOP_BOTTOM;
128 _frame_type = VideoFrameType::THREE_D_ALTERNATE;
131 _frame_type = VideoFrameType::THREE_D_LEFT;
134 _frame_type = VideoFrameType::THREE_D_RIGHT;
138 _frame_type = string_to_video_frame_type (node->string_child ("VideoFrameType"));
141 _sample_aspect_ratio = node->optional_number_child<double> ("SampleAspectRatio");
142 _crop.left = node->number_child<int> ("LeftCrop");
143 _crop.right = node->number_child<int> ("RightCrop");
144 _crop.top = node->number_child<int> ("TopCrop");
145 _crop.bottom = node->number_child<int> ("BottomCrop");
148 auto r = node->optional_string_child ("Ratio");
150 _legacy_ratio = Ratio::from_id(r.get())->ratio();
152 } else if (version <= 37) {
153 auto ratio = node->node_child("Scale")->optional_string_child("Ratio");
155 _legacy_ratio = Ratio::from_id(ratio.get())->ratio();
157 auto scale = node->node_child("Scale")->optional_bool_child("Scale");
160 /* This is what we used to call "no stretch" */
161 _legacy_ratio = _size.ratio();
163 /* This is what we used to call "no scale" */
164 _custom_size = _size;
169 _custom_ratio = node->optional_number_child<float>("CustomRatio");
170 if (node->optional_number_child<int>("CustomWidth")) {
171 _custom_size = dcp::Size (node->number_child<int>("CustomWidth"), node->number_child<int>("CustomHeight"));
175 if (node->optional_node_child ("ColourConversion")) {
176 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"), version);
179 _yuv = node->optional_bool_child("YUV").get_value_or (true);
182 _fade_in = node->optional_number_child<Frame>("VideoFadeIn").get_value_or(node->number_child<Frame>("FadeIn"));
183 _fade_out = node->optional_number_child<Frame>("VideoFadeOut").get_value_or(node->number_child<Frame>("FadeOut"));
185 _fade_in = _fade_out = 0;
188 _range = VideoRange::FULL;
189 if (node->optional_string_child("Range").get_value_or("full") == "video") {
190 _range = VideoRange::VIDEO;
193 if (auto pixel_quanta = node->optional_node_child("PixelQuanta")) {
194 _pixel_quanta = PixelQuanta(pixel_quanta);
197 auto burnt = node->optional_string_child("BurntSubtitleLanguage");
199 _burnt_subtitle_language = dcp::LanguageTag (*burnt);
205 VideoContent::VideoContent (Content* parent, vector<shared_ptr<Content>> c)
206 : ContentPart (parent)
210 auto ref = c[0]->video;
211 DCPOMATIC_ASSERT (ref);
213 for (size_t i = 1; i < c.size(); ++i) {
215 if (c[i]->video->use() != ref->use()) {
216 throw JoinError (_("Content to be joined must have all its video used or not used."));
219 if (c[i]->video->size() != ref->size()) {
220 throw JoinError (_("Content to be joined must have the same picture size."));
223 if (c[i]->video->frame_type() != ref->frame_type()) {
224 throw JoinError (_("Content to be joined must have the same video frame type."));
227 if (c[i]->video->requested_crop() != ref->requested_crop()) {
228 throw JoinError (_("Content to be joined must have the same crop."));
231 if (c[i]->video->custom_ratio() != ref->custom_ratio()) {
232 throw JoinError (_("Content to be joined must have the same custom ratio setting."));
235 if (c[i]->video->custom_size() != ref->custom_size()) {
236 throw JoinError (_("Content to be joined must have the same custom size setting."));
239 if (c[i]->video->colour_conversion() != ref->colour_conversion()) {
240 throw JoinError (_("Content to be joined must have the same colour conversion."));
243 if (c[i]->video->fade_in() != ref->fade_in() || c[i]->video->fade_out() != ref->fade_out()) {
244 throw JoinError (_("Content to be joined must have the same fades."));
247 if (c[i]->video->burnt_subtitle_language() != ref->burnt_subtitle_language()) {
248 throw JoinError (_("Content to be joined must have the same burnt subtitle language."));
251 _length += c[i]->video->length ();
253 if (c[i]->video->yuv ()) {
257 _pixel_quanta = max(_pixel_quanta, c[i]->video->_pixel_quanta);
261 _size = ref->size ();
262 _frame_type = ref->frame_type ();
263 _crop = ref->requested_crop ();
264 _custom_ratio = ref->custom_ratio ();
265 _colour_conversion = ref->colour_conversion ();
266 _fade_in = ref->fade_in ();
267 _fade_out = ref->fade_out ();
268 _range = ref->range ();
269 _burnt_subtitle_language = ref->burnt_subtitle_language ();
274 VideoContent::as_xml (xmlpp::Node* node) const
276 boost::mutex::scoped_lock lm (_mutex);
277 node->add_child("Use")->add_child_text (_use ? "1" : "0");
278 node->add_child("VideoLength")->add_child_text (raw_convert<string> (_length));
279 node->add_child("VideoWidth")->add_child_text (raw_convert<string> (_size.width));
280 node->add_child("VideoHeight")->add_child_text (raw_convert<string> (_size.height));
281 node->add_child("VideoFrameType")->add_child_text (video_frame_type_to_string (_frame_type));
282 if (_sample_aspect_ratio) {
283 node->add_child("SampleAspectRatio")->add_child_text (raw_convert<string> (_sample_aspect_ratio.get ()));
287 node->add_child("CustomRatio")->add_child_text(raw_convert<string>(*_custom_ratio));
290 node->add_child("CustomWidth")->add_child_text(raw_convert<string>(_custom_size->width));
291 node->add_child("CustomHeight")->add_child_text(raw_convert<string>(_custom_size->height));
293 if (_colour_conversion) {
294 _colour_conversion.get().as_xml (node->add_child("ColourConversion"));
296 node->add_child("YUV")->add_child_text (_yuv ? "1" : "0");
297 node->add_child("VideoFadeIn")->add_child_text(raw_convert<string>(_fade_in));
298 node->add_child("VideoFadeOut")->add_child_text(raw_convert<string>(_fade_out));
299 node->add_child("Range")->add_child_text(_range == VideoRange::FULL ? "full" : "video");
300 _pixel_quanta.as_xml(node->add_child("PixelQuanta"));
301 if (_burnt_subtitle_language) {
302 node->add_child("BurntSubtitleLanguage")->add_child_text(_burnt_subtitle_language->to_string());
308 VideoContent::take_from_examiner (shared_ptr<VideoExaminer> d)
310 /* These examiner calls could call other content methods which take a lock on the mutex */
311 auto const vs = d->video_size ();
312 auto vl = d->video_length ();
313 auto const ar = d->sample_aspect_ratio ();
314 auto const yuv = d->yuv ();
315 auto const range = d->range ();
316 auto const pixel_quanta = d->pixel_quanta ();
318 ContentChangeSignaller cc1 (_parent, VideoContentProperty::SIZE);
319 ContentChangeSignaller cc2 (_parent, ContentProperty::LENGTH);
320 ContentChangeSignaller cc3 (_parent, VideoContentProperty::RANGE);
323 boost::mutex::scoped_lock lm (_mutex);
326 _sample_aspect_ratio = ar;
329 _pixel_quanta = pixel_quanta;
332 LOG_GENERAL ("Video length obtained from header as %1 frames", _length);
334 if (d->video_frame_rate()) {
335 _parent->set_video_frame_rate (d->video_frame_rate().get());
340 /** @return string which includes everything about how this content looks */
342 VideoContent::identifier () const
345 auto const crop = actual_crop();
347 buffer, sizeof(buffer), "%d_%d_%d_%d_%d_%f_%d_%d%" PRId64 "_%" PRId64 "_%d",
353 _custom_ratio.get_value_or(0),
354 _custom_size ? _custom_size->width : 0,
355 _custom_size ? _custom_size->height : 0,
358 _range == VideoRange::FULL ? 0 : 1
363 if (colour_conversion()) {
364 s += "_" + colour_conversion().get().identifier ();
372 VideoContent::technical_summary () const
374 string s = String::compose (
375 N_("video: length %1 frames, size %2x%3"),
376 length_after_3d_combine(),
381 if (sample_aspect_ratio ()) {
382 s += String::compose (N_(", sample aspect ratio %1"), (sample_aspect_ratio().get ()));
390 VideoContent::size_after_3d_split () const
392 auto const s = size ();
393 switch (frame_type ()) {
394 case VideoFrameType::TWO_D:
395 case VideoFrameType::THREE_D:
396 case VideoFrameType::THREE_D_ALTERNATE:
397 case VideoFrameType::THREE_D_LEFT:
398 case VideoFrameType::THREE_D_RIGHT:
400 case VideoFrameType::THREE_D_LEFT_RIGHT:
401 return dcp::Size (s.width / 2, s.height);
402 case VideoFrameType::THREE_D_TOP_BOTTOM:
403 return dcp::Size (s.width, s.height / 2);
406 DCPOMATIC_ASSERT (false);
410 /** @return Video size after 3D split and crop */
412 VideoContent::size_after_crop () const
414 return actual_crop().apply(size_after_3d_split());
418 /** @param f Frame index within the whole (untrimmed) content.
419 * @return Fade factor (between 0 and 1) or unset if there is no fade.
422 VideoContent::fade (shared_ptr<const Film> film, Frame f) const
424 DCPOMATIC_ASSERT (f >= 0);
426 double const vfr = _parent->active_video_frame_rate(film);
428 auto const ts = _parent->trim_start().frames_round(vfr);
429 if ((f - ts) < fade_in()) {
430 return double (f - ts) / fade_in();
433 auto fade_out_start = length() - _parent->trim_end().frames_round(vfr) - fade_out();
434 if (f >= fade_out_start) {
435 return 1 - double (f - fade_out_start) / fade_out();
443 VideoContent::processing_description (shared_ptr<const Film> film)
448 if (size().width && size().height) {
449 d += String::compose (
450 _("Content video is %1x%2"),
451 size_after_3d_split().width,
452 size_after_3d_split().height
456 double ratio = size_after_3d_split().ratio ();
458 if (sample_aspect_ratio ()) {
459 snprintf (buffer, sizeof(buffer), _(", pixel aspect ratio %.2f:1"), sample_aspect_ratio().get());
461 ratio *= sample_aspect_ratio().get ();
464 snprintf (buffer, sizeof(buffer), _("\nDisplay aspect ratio %.2f:1"), ratio);
468 auto const crop = actual_crop();
470 if ((crop.left || crop.right || crop.top || crop.bottom) && size() != dcp::Size(0, 0)) {
471 auto const cropped = size_after_crop ();
472 d += String::compose (
473 _("\nCropped to %1x%2"),
474 cropped.width, cropped.height
477 snprintf (buffer, sizeof(buffer), " (%.2f:1)", cropped.ratio());
481 auto const container_size = film->frame_size ();
482 auto const scaled = scaled_size (container_size);
484 if (scaled != size_after_crop ()) {
485 d += String::compose (
486 _("\nScaled to %1x%2"),
487 scaled.width, scaled.height
490 snprintf (buffer, sizeof(buffer), _(" (%.2f:1)"), scaled.ratio());
494 if (scaled != container_size) {
495 d += String::compose (
496 _("\nPadded with black to fit container %1 (%2x%3)"),
497 film->container()->container_nickname (),
498 container_size.width, container_size.height
501 snprintf (buffer, sizeof(buffer), _(" (%.2f:1)"), container_size.ratio());
505 if (_parent->video_frame_rate()) {
506 double const vfr = _parent->video_frame_rate().get ();
508 snprintf (buffer, sizeof(buffer), _("\nContent frame rate %.4f\n"), vfr);
511 FrameRateChange frc (vfr, film->video_frame_rate ());
512 d += frc.description ();
520 VideoContent::add_properties (list<UserProperty>& p) const
522 p.push_back (UserProperty (UserProperty::VIDEO, _("Length"), length (), _("video frames")));
523 p.push_back (UserProperty (UserProperty::VIDEO, _("Size"), String::compose ("%1x%2", size().width, size().height)));
528 VideoContent::set_length (Frame len)
530 maybe_set (_length, len, ContentProperty::LENGTH);
535 VideoContent::set_crop (Crop c)
537 maybe_set (_crop, c, VideoContentProperty::CROP);
541 VideoContent::set_left_crop (int c)
543 maybe_set (_crop.left, c, VideoContentProperty::CROP);
548 VideoContent::set_right_crop (int c)
550 maybe_set (_crop.right, c, VideoContentProperty::CROP);
555 VideoContent::set_top_crop (int c)
557 maybe_set (_crop.top, c, VideoContentProperty::CROP);
562 VideoContent::set_bottom_crop (int c)
564 maybe_set (_crop.bottom, c, VideoContentProperty::CROP);
569 VideoContent::set_frame_type (VideoFrameType t)
571 maybe_set (_frame_type, t, VideoContentProperty::FRAME_TYPE);
576 VideoContent::unset_colour_conversion ()
578 maybe_set (_colour_conversion, boost::optional<ColourConversion> (), VideoContentProperty::COLOUR_CONVERSION);
583 VideoContent::set_colour_conversion (ColourConversion c)
585 maybe_set (_colour_conversion, c, VideoContentProperty::COLOUR_CONVERSION);
590 VideoContent::set_fade_in (Frame t)
592 maybe_set (_fade_in, t, VideoContentProperty::FADE_IN);
597 VideoContent::set_fade_out (Frame t)
599 maybe_set (_fade_out, t, VideoContentProperty::FADE_OUT);
604 VideoContent::set_range (VideoRange r)
606 maybe_set (_range, r, VideoContentProperty::RANGE);
611 VideoContent::set_use (bool u)
613 maybe_set (_use, u, VideoContentProperty::USE);
618 VideoContent::set_burnt_subtitle_language (boost::optional<dcp::LanguageTag> language)
620 maybe_set (_burnt_subtitle_language, language, VideoContentProperty::BURNT_SUBTITLE_LANGUAGE);
625 VideoContent::take_settings_from (shared_ptr<const VideoContent> c)
627 if (c->_colour_conversion) {
628 set_colour_conversion (c->_colour_conversion.get());
630 unset_colour_conversion ();
633 set_frame_type (c->_frame_type);
634 set_left_crop (c->_crop.left);
635 set_right_crop (c->_crop.right);
636 set_top_crop (c->_crop.top);
637 set_bottom_crop (c->_crop.bottom);
638 set_custom_ratio (c->_custom_ratio);
639 set_custom_size (c->_custom_size);
640 set_fade_in (c->_fade_in);
641 set_fade_out (c->_fade_out);
642 set_burnt_subtitle_language (c->_burnt_subtitle_language);
647 VideoContent::modify_position (shared_ptr<const Film> film, DCPTime& pos) const
649 pos = pos.round (film->video_frame_rate());
654 VideoContent::modify_trim_start (ContentTime& trim) const
656 if (_parent->video_frame_rate()) {
657 trim = trim.round (_parent->video_frame_rate().get());
662 /** @param film_container The size of the container for the DCP that we are working on */
664 VideoContent::scaled_size (dcp::Size film_container)
667 return fit_ratio_within(*_custom_ratio, film_container);
671 return *_custom_size;
674 auto size = size_after_crop ();
675 size.width *= _sample_aspect_ratio.get_value_or(1);
677 /* This is what we will return unless there is any legacy stuff to take into account */
678 auto auto_size = fit_ratio_within (size.ratio(), film_container);
681 if (fit_ratio_within(*_legacy_ratio, film_container) != auto_size) {
682 _custom_ratio = *_legacy_ratio;
683 _legacy_ratio = optional<float>();
684 return fit_ratio_within(*_custom_ratio, film_container);
686 _legacy_ratio = boost::optional<float>();
689 return _pixel_quanta.round (auto_size);
694 VideoContent::set_custom_ratio (optional<float> ratio)
696 maybe_set (_custom_ratio, ratio, VideoContentProperty::CUSTOM_RATIO);
701 VideoContent::set_custom_size (optional<dcp::Size> size)
703 maybe_set (_custom_size, size, VideoContentProperty::CUSTOM_SIZE);
708 VideoContent::actual_crop () const
711 _pixel_quanta.round_x(_crop.left),
712 _pixel_quanta.round_x(_crop.right),
713 _pixel_quanta.round_y(_crop.top),
714 _pixel_quanta.round_y(_crop.bottom)