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/>.
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 std::shared_ptr;
65 using std::make_shared;
66 using boost::optional;
67 using std::dynamic_pointer_cast;
68 using dcp::raw_convert;
69 using namespace dcpomatic;
71 VideoContent::VideoContent (Content* parent)
72 : ContentPart (parent)
75 , _frame_type (VideoFrameType::TWO_D)
79 , _range (VideoRange::FULL)
84 shared_ptr<VideoContent>
85 VideoContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version)
87 if (!node->optional_number_child<int> ("VideoWidth")) {
91 return make_shared<VideoContent>(parent, node, version);
94 VideoContent::VideoContent (Content* parent, cxml::ConstNodePtr node, int version)
95 : ContentPart (parent)
97 _size.width = node->number_child<int> ("VideoWidth");
98 _size.height = node->number_child<int> ("VideoHeight");
100 /* Backwards compatibility */
101 auto r = node->optional_number_child<double>("VideoFrameRate");
103 _parent->set_video_frame_rate (r.get ());
106 _use = node->optional_bool_child("Use").get_value_or(true);
107 _length = node->number_child<Frame> ("VideoLength");
110 /* Snapshot of the VideoFrameType enum at version 34 */
111 switch (node->number_child<int> ("VideoFrameType")) {
113 _frame_type = VideoFrameType::TWO_D;
116 _frame_type = VideoFrameType::THREE_D_LEFT_RIGHT;
119 _frame_type = VideoFrameType::THREE_D_TOP_BOTTOM;
122 _frame_type = VideoFrameType::THREE_D_ALTERNATE;
125 _frame_type = VideoFrameType::THREE_D_LEFT;
128 _frame_type = VideoFrameType::THREE_D_RIGHT;
132 _frame_type = string_to_video_frame_type (node->string_child ("VideoFrameType"));
135 _sample_aspect_ratio = node->optional_number_child<double> ("SampleAspectRatio");
136 _crop.left = node->number_child<int> ("LeftCrop");
137 _crop.right = node->number_child<int> ("RightCrop");
138 _crop.top = node->number_child<int> ("TopCrop");
139 _crop.bottom = node->number_child<int> ("BottomCrop");
142 auto r = node->optional_string_child ("Ratio");
144 _legacy_ratio = Ratio::from_id(r.get())->ratio();
146 } else if (version <= 37) {
147 auto ratio = node->node_child("Scale")->optional_string_child("Ratio");
149 _legacy_ratio = Ratio::from_id(ratio.get())->ratio();
151 auto scale = node->node_child("Scale")->optional_bool_child("Scale");
154 /* This is what we used to call "no stretch" */
155 _legacy_ratio = _size.ratio();
157 /* This is what we used to call "no scale" */
158 _custom_size = _size;
163 _custom_ratio = node->optional_number_child<float>("CustomRatio");
164 if (node->optional_number_child<int>("CustomWidth")) {
165 _custom_size = dcp::Size (node->number_child<int>("CustomWidth"), node->number_child<int>("CustomHeight"));
169 if (node->optional_node_child ("ColourConversion")) {
170 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"), version);
173 _yuv = node->optional_bool_child("YUV").get_value_or (true);
176 _fade_in = node->number_child<Frame> ("FadeIn");
177 _fade_out = node->number_child<Frame> ("FadeOut");
179 _fade_in = _fade_out = 0;
182 _range = VideoRange::FULL;
183 if (node->optional_string_child("Range").get_value_or("full") == "video") {
184 _range = VideoRange::VIDEO;
188 VideoContent::VideoContent (Content* parent, vector<shared_ptr<Content> > c)
189 : ContentPart (parent)
193 auto ref = c[0]->video;
194 DCPOMATIC_ASSERT (ref);
196 for (size_t i = 1; i < c.size(); ++i) {
198 if (c[i]->video->use() != ref->use()) {
199 throw JoinError (_("Content to be joined must have all its video used or not used."));
202 if (c[i]->video->size() != ref->size()) {
203 throw JoinError (_("Content to be joined must have the same picture size."));
206 if (c[i]->video->frame_type() != ref->frame_type()) {
207 throw JoinError (_("Content to be joined must have the same video frame type."));
210 if (c[i]->video->crop() != ref->crop()) {
211 throw JoinError (_("Content to be joined must have the same crop."));
214 if (c[i]->video->custom_ratio() != ref->custom_ratio()) {
215 throw JoinError (_("Content to be joined must have the same custom ratio setting."));
218 if (c[i]->video->custom_size() != ref->custom_size()) {
219 throw JoinError (_("Content to be joined must have the same custom size setting."));
222 if (c[i]->video->colour_conversion() != ref->colour_conversion()) {
223 throw JoinError (_("Content to be joined must have the same colour conversion."));
226 if (c[i]->video->fade_in() != ref->fade_in() || c[i]->video->fade_out() != ref->fade_out()) {
227 throw JoinError (_("Content to be joined must have the same fades."));
230 _length += c[i]->video->length ();
232 if (c[i]->video->yuv ()) {
238 _size = ref->size ();
239 _frame_type = ref->frame_type ();
240 _crop = ref->crop ();
241 _custom_ratio = ref->custom_ratio ();
242 _colour_conversion = ref->colour_conversion ();
243 _fade_in = ref->fade_in ();
244 _fade_out = ref->fade_out ();
245 _range = ref->range ();
249 VideoContent::as_xml (xmlpp::Node* node) const
251 boost::mutex::scoped_lock lm (_mutex);
252 node->add_child("Use")->add_child_text (_use ? "1" : "0");
253 node->add_child("VideoLength")->add_child_text (raw_convert<string> (_length));
254 node->add_child("VideoWidth")->add_child_text (raw_convert<string> (_size.width));
255 node->add_child("VideoHeight")->add_child_text (raw_convert<string> (_size.height));
256 node->add_child("VideoFrameType")->add_child_text (video_frame_type_to_string (_frame_type));
257 if (_sample_aspect_ratio) {
258 node->add_child("SampleAspectRatio")->add_child_text (raw_convert<string> (_sample_aspect_ratio.get ()));
262 node->add_child("CustomRatio")->add_child_text(raw_convert<string>(*_custom_ratio));
265 node->add_child("CustomWidth")->add_child_text(raw_convert<string>(_custom_size->width));
266 node->add_child("CustomHeight")->add_child_text(raw_convert<string>(_custom_size->height));
268 if (_colour_conversion) {
269 _colour_conversion.get().as_xml (node->add_child("ColourConversion"));
271 node->add_child("YUV")->add_child_text (_yuv ? "1" : "0");
272 node->add_child("FadeIn")->add_child_text (raw_convert<string> (_fade_in));
273 node->add_child("FadeOut")->add_child_text (raw_convert<string> (_fade_out));
274 node->add_child("Range")->add_child_text(_range == VideoRange::FULL ? "full" : "video");
278 VideoContent::take_from_examiner (shared_ptr<VideoExaminer> d)
280 /* These examiner calls could call other content methods which take a lock on the mutex */
281 auto const vs = d->video_size ();
282 auto vl = d->video_length ();
283 auto const ar = d->sample_aspect_ratio ();
284 auto const yuv = d->yuv ();
285 auto const range = d->range ();
287 ContentChangeSignaller cc1 (_parent, VideoContentProperty::SIZE);
288 ContentChangeSignaller cc2 (_parent, VideoContentProperty::SCALE);
289 ContentChangeSignaller cc3 (_parent, ContentProperty::LENGTH);
290 ContentChangeSignaller cc4 (_parent, VideoContentProperty::RANGE);
293 boost::mutex::scoped_lock lm (_mutex);
296 _sample_aspect_ratio = ar;
301 LOG_GENERAL ("Video length obtained from header as %1 frames", _length);
303 if (d->video_frame_rate()) {
304 _parent->set_video_frame_rate (d->video_frame_rate().get());
308 /** @return string which includes everything about how this content looks */
310 VideoContent::identifier () const
314 buffer, sizeof(buffer), "%d_%d_%d_%d_%d_%f_%d_%d%" PRId64 "_%" PRId64 "_%d",
320 _custom_ratio.get_value_or(0),
321 _custom_size ? _custom_size->width : 0,
322 _custom_size ? _custom_size->height : 0,
325 _range == VideoRange::FULL ? 0 : 1
330 if (colour_conversion()) {
331 s += "_" + colour_conversion().get().identifier ();
338 VideoContent::technical_summary () const
340 string s = String::compose (
341 N_("video: length %1 frames, size %2x%3"),
342 length_after_3d_combine(),
347 if (sample_aspect_ratio ()) {
348 s += String::compose (N_(", sample aspect ratio %1"), (sample_aspect_ratio().get ()));
355 VideoContent::size_after_3d_split () const
357 auto const s = size ();
358 switch (frame_type ()) {
359 case VideoFrameType::TWO_D:
360 case VideoFrameType::THREE_D:
361 case VideoFrameType::THREE_D_ALTERNATE:
362 case VideoFrameType::THREE_D_LEFT:
363 case VideoFrameType::THREE_D_RIGHT:
365 case VideoFrameType::THREE_D_LEFT_RIGHT:
366 return dcp::Size (s.width / 2, s.height);
367 case VideoFrameType::THREE_D_TOP_BOTTOM:
368 return dcp::Size (s.width, s.height / 2);
371 DCPOMATIC_ASSERT (false);
374 /** @return Video size after 3D split and crop */
376 VideoContent::size_after_crop () const
378 return crop().apply (size_after_3d_split ());
382 /** @param f Frame index within the whole (untrimmed) content.
383 * @return Fade factor (between 0 and 1) or unset if there is no fade.
386 VideoContent::fade (shared_ptr<const Film> film, Frame f) const
388 DCPOMATIC_ASSERT (f >= 0);
390 double const vfr = _parent->active_video_frame_rate(film);
392 auto const ts = _parent->trim_start().frames_round(vfr);
393 if ((f - ts) < fade_in()) {
394 return double (f - ts) / fade_in();
397 auto fade_out_start = length() - _parent->trim_end().frames_round(vfr) - fade_out();
398 if (f >= fade_out_start) {
399 return 1 - double (f - fade_out_start) / fade_out();
402 return optional<double> ();
406 VideoContent::processing_description (shared_ptr<const Film> film)
411 if (size().width && size().height) {
412 d += String::compose (
413 _("Content video is %1x%2"),
414 size_after_3d_split().width,
415 size_after_3d_split().height
419 double ratio = size_after_3d_split().ratio ();
421 if (sample_aspect_ratio ()) {
422 snprintf (buffer, sizeof(buffer), _(", pixel aspect ratio %.2f:1"), sample_aspect_ratio().get());
424 ratio *= sample_aspect_ratio().get ();
427 snprintf (buffer, sizeof(buffer), _("\nDisplay aspect ratio %.2f:1"), ratio);
431 if ((crop().left || crop().right || crop().top || crop().bottom) && size() != dcp::Size (0, 0)) {
432 dcp::Size cropped = size_after_crop ();
433 d += String::compose (
434 _("\nCropped to %1x%2"),
435 cropped.width, cropped.height
438 snprintf (buffer, sizeof(buffer), " (%.2f:1)", cropped.ratio());
442 auto const container_size = film->frame_size ();
443 auto const scaled = scaled_size (container_size);
445 if (scaled != size_after_crop ()) {
446 d += String::compose (
447 _("\nScaled to %1x%2"),
448 scaled.width, scaled.height
451 snprintf (buffer, sizeof(buffer), _(" (%.2f:1)"), scaled.ratio());
455 if (scaled != container_size) {
456 d += String::compose (
457 _("\nPadded with black to fit container %1 (%2x%3)"),
458 film->container()->container_nickname (),
459 container_size.width, container_size.height
462 snprintf (buffer, sizeof(buffer), _(" (%.2f:1)"), container_size.ratio());
466 if (_parent->video_frame_rate()) {
467 double const vfr = _parent->video_frame_rate().get ();
469 snprintf (buffer, sizeof(buffer), _("\nContent frame rate %.4f\n"), vfr);
472 FrameRateChange frc (vfr, film->video_frame_rate ());
473 d += frc.description ();
480 VideoContent::add_properties (list<UserProperty>& p) const
482 p.push_back (UserProperty (UserProperty::VIDEO, _("Length"), length (), _("video frames")));
483 p.push_back (UserProperty (UserProperty::VIDEO, _("Size"), String::compose ("%1x%2", size().width, size().height)));
487 VideoContent::set_length (Frame len)
489 maybe_set (_length, len, ContentProperty::LENGTH);
493 VideoContent::set_left_crop (int c)
495 maybe_set (_crop.left, c, VideoContentProperty::CROP);
499 VideoContent::set_right_crop (int c)
501 maybe_set (_crop.right, c, VideoContentProperty::CROP);
505 VideoContent::set_top_crop (int c)
507 maybe_set (_crop.top, c, VideoContentProperty::CROP);
511 VideoContent::set_bottom_crop (int c)
513 maybe_set (_crop.bottom, c, VideoContentProperty::CROP);
518 VideoContent::set_frame_type (VideoFrameType t)
520 maybe_set (_frame_type, t, VideoContentProperty::FRAME_TYPE);
524 VideoContent::unset_colour_conversion ()
526 maybe_set (_colour_conversion, boost::optional<ColourConversion> (), VideoContentProperty::COLOUR_CONVERSION);
530 VideoContent::set_colour_conversion (ColourConversion c)
532 maybe_set (_colour_conversion, c, VideoContentProperty::COLOUR_CONVERSION);
536 VideoContent::set_fade_in (Frame t)
538 maybe_set (_fade_in, t, VideoContentProperty::FADE_IN);
542 VideoContent::set_fade_out (Frame t)
544 maybe_set (_fade_out, t, VideoContentProperty::FADE_OUT);
548 VideoContent::set_range (VideoRange r)
550 maybe_set (_range, r, VideoContentProperty::RANGE);
554 VideoContent::set_use (bool u)
556 maybe_set (_use, u, VideoContentProperty::USE);
560 VideoContent::take_settings_from (shared_ptr<const VideoContent> c)
562 if (c->_colour_conversion) {
563 set_colour_conversion (c->_colour_conversion.get());
565 unset_colour_conversion ();
568 set_frame_type (c->_frame_type);
569 set_left_crop (c->_crop.left);
570 set_right_crop (c->_crop.right);
571 set_top_crop (c->_crop.top);
572 set_bottom_crop (c->_crop.bottom);
573 set_custom_ratio (c->_custom_ratio);
574 set_custom_size (c->_custom_size);
575 set_fade_in (c->_fade_in);
576 set_fade_out (c->_fade_out);
580 VideoContent::modify_position (shared_ptr<const Film> film, DCPTime& pos) const
582 pos = pos.round (film->video_frame_rate());
586 VideoContent::modify_trim_start (ContentTime& trim) const
588 if (_parent->video_frame_rate()) {
589 trim = trim.round (_parent->video_frame_rate().get());
594 /** @param film_container The size of the container for the DCP that we are working on */
596 VideoContent::scaled_size (dcp::Size film_container)
599 return fit_ratio_within(*_custom_ratio, film_container);
603 return *_custom_size;
606 auto size = size_after_crop ();
607 size.width *= _sample_aspect_ratio.get_value_or(1);
609 /* This is what we will return unless there is any legacy stuff to take into account */
610 auto auto_size = fit_ratio_within (size.ratio(), film_container);
613 if (fit_ratio_within(*_legacy_ratio, film_container) != auto_size) {
614 _custom_ratio = *_legacy_ratio;
615 _legacy_ratio = optional<float>();
616 return fit_ratio_within(*_custom_ratio, film_container);
618 _legacy_ratio = boost::optional<float>();
626 VideoContent::set_custom_ratio (optional<float> ratio)
628 maybe_set (_custom_ratio, ratio, VideoContentProperty::CUSTOM_RATIO);
633 VideoContent::set_custom_size (optional<dcp::Size> size)
635 maybe_set (_custom_size, size, VideoContentProperty::CUSTOM_SIZE);