2 Copyright (C) 2013-2016 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 <dcp/raw_convert.h>
34 #include <libcxml/cxml.h>
35 #include <dcp/colour_matrix.h>
36 #include <libxml++/libxml++.h>
42 #define LOG_GENERAL(...) _parent->film()->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL);
44 int const VideoContentProperty::SIZE = 0;
45 int const VideoContentProperty::FRAME_TYPE = 1;
46 int const VideoContentProperty::CROP = 2;
47 int const VideoContentProperty::SCALE = 3;
48 int const VideoContentProperty::COLOUR_CONVERSION = 4;
49 int const VideoContentProperty::FADE_IN = 5;
50 int const VideoContentProperty::FADE_OUT = 6;
53 using std::setprecision;
59 using std::setprecision;
62 using boost::shared_ptr;
63 using boost::optional;
64 using boost::dynamic_pointer_cast;
65 using dcp::raw_convert;
67 VideoContent::VideoContent (Content* parent)
68 : ContentPart (parent)
70 , _frame_type (VIDEO_FRAME_TYPE_2D)
71 , _scale (VideoContentScale (Ratio::from_id ("178")))
79 shared_ptr<VideoContent>
80 VideoContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version)
82 if (!node->optional_number_child<int> ("VideoWidth")) {
83 return shared_ptr<VideoContent> ();
86 return shared_ptr<VideoContent> (new VideoContent (parent, node, version));
89 VideoContent::VideoContent (Content* parent, cxml::ConstNodePtr node, int version)
90 : ContentPart (parent)
92 _size.width = node->number_child<int> ("VideoWidth");
93 _size.height = node->number_child<int> ("VideoHeight");
95 /* Backwards compatibility */
96 optional<double> r = node->optional_number_child<double>("VideoFrameRate");
98 _parent->set_video_frame_rate (r.get ());
101 _length = node->number_child<Frame> ("VideoLength");
104 /* Snapshot of the VideoFrameType enum at version 34 */
105 switch (node->number_child<int> ("VideoFrameType")) {
107 _frame_type = VIDEO_FRAME_TYPE_2D;
110 _frame_type = VIDEO_FRAME_TYPE_3D_LEFT_RIGHT;
113 _frame_type = VIDEO_FRAME_TYPE_3D_TOP_BOTTOM;
116 _frame_type = VIDEO_FRAME_TYPE_3D_ALTERNATE;
119 _frame_type = VIDEO_FRAME_TYPE_3D_LEFT;
122 _frame_type = VIDEO_FRAME_TYPE_3D_RIGHT;
126 _frame_type = string_to_video_frame_type (node->string_child ("VideoFrameType"));
129 _sample_aspect_ratio = node->optional_number_child<double> ("SampleAspectRatio");
130 _crop.left = node->number_child<int> ("LeftCrop");
131 _crop.right = node->number_child<int> ("RightCrop");
132 _crop.top = node->number_child<int> ("TopCrop");
133 _crop.bottom = node->number_child<int> ("BottomCrop");
136 optional<string> r = node->optional_string_child ("Ratio");
138 _scale = VideoContentScale (Ratio::from_id (r.get ()));
141 _scale = VideoContentScale (node->node_child ("Scale"));
145 if (node->optional_node_child ("ColourConversion")) {
146 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"), version);
149 _yuv = node->optional_bool_child("YUV").get_value_or (true);
152 _fade_in = node->number_child<Frame> ("FadeIn");
153 _fade_out = node->number_child<Frame> ("FadeOut");
155 _fade_in = _fade_out = 0;
159 VideoContent::VideoContent (Content* parent, vector<shared_ptr<Content> > c)
160 : ContentPart (parent)
164 shared_ptr<VideoContent> ref = c[0]->video;
165 DCPOMATIC_ASSERT (ref);
167 for (size_t i = 1; i < c.size(); ++i) {
169 if (c[i]->video->size() != ref->size()) {
170 throw JoinError (_("Content to be joined must have the same picture size."));
173 if (c[i]->video->frame_type() != ref->frame_type()) {
174 throw JoinError (_("Content to be joined must have the same video frame type."));
177 if (c[i]->video->crop() != ref->crop()) {
178 throw JoinError (_("Content to be joined must have the same crop."));
181 if (c[i]->video->scale() != ref->scale()) {
182 throw JoinError (_("Content to be joined must have the same scale setting."));
185 if (c[i]->video->colour_conversion() != ref->colour_conversion()) {
186 throw JoinError (_("Content to be joined must have the same colour conversion."));
189 if (c[i]->video->fade_in() != ref->fade_in() || c[i]->video->fade_out() != ref->fade_out()) {
190 throw JoinError (_("Content to be joined must have the same fades."));
193 _length += c[i]->video->length ();
195 if (c[i]->video->yuv ()) {
200 _size = ref->size ();
201 _frame_type = ref->frame_type ();
202 _crop = ref->crop ();
203 _scale = ref->scale ();
204 _colour_conversion = ref->colour_conversion ();
205 _fade_in = ref->fade_in ();
206 _fade_out = ref->fade_out ();
210 VideoContent::as_xml (xmlpp::Node* node) const
212 boost::mutex::scoped_lock lm (_mutex);
213 node->add_child("VideoLength")->add_child_text (raw_convert<string> (_length));
214 node->add_child("VideoWidth")->add_child_text (raw_convert<string> (_size.width));
215 node->add_child("VideoHeight")->add_child_text (raw_convert<string> (_size.height));
216 node->add_child("VideoFrameType")->add_child_text (video_frame_type_to_string (_frame_type));
217 if (_sample_aspect_ratio) {
218 node->add_child("SampleAspectRatio")->add_child_text (raw_convert<string> (_sample_aspect_ratio.get ()));
221 _scale.as_xml (node->add_child("Scale"));
222 if (_colour_conversion) {
223 _colour_conversion.get().as_xml (node->add_child("ColourConversion"));
225 node->add_child("YUV")->add_child_text (_yuv ? "1" : "0");
226 node->add_child("FadeIn")->add_child_text (raw_convert<string> (_fade_in));
227 node->add_child("FadeOut")->add_child_text (raw_convert<string> (_fade_out));
231 VideoContent::take_from_examiner (shared_ptr<VideoExaminer> d)
233 /* These examiner calls could call other content methods which take a lock on the mutex */
234 dcp::Size const vs = d->video_size ();
235 Frame vl = d->video_length ();
236 optional<double> const ar = d->sample_aspect_ratio ();
237 bool const yuv = d->yuv ();
240 boost::mutex::scoped_lock lm (_mutex);
243 _sample_aspect_ratio = ar;
246 /* Guess correct scale from size and sample aspect ratio */
247 _scale = VideoContentScale (
248 Ratio::nearest_from_ratio (double (_size.width) * ar.get_value_or (1) / _size.height)
252 LOG_GENERAL ("Video length obtained from header as %1 frames", _length);
254 if (d->video_frame_rate()) {
255 _parent->set_video_frame_rate (d->video_frame_rate().get());
258 _parent->signal_changed (VideoContentProperty::SIZE);
259 _parent->signal_changed (VideoContentProperty::SCALE);
260 _parent->signal_changed (ContentProperty::LENGTH);
263 /** @return string which includes everything about how this content looks */
265 VideoContent::identifier () const
269 buffer, sizeof(buffer), "%d_%d_%d_%d_%s_%" PRId64 "_%" PRId64,
274 scale().id().c_str(),
281 if (colour_conversion()) {
282 s += "_" + colour_conversion().get().identifier ();
289 VideoContent::technical_summary () const
291 string s = String::compose (
292 N_("video: length %1 frames, size %2x%3"),
293 length_after_3d_combine(),
298 if (sample_aspect_ratio ()) {
299 s += String::compose (N_(", sample aspect ratio %1"), (sample_aspect_ratio().get ()));
306 VideoContent::size_after_3d_split () const
308 dcp::Size const s = size ();
309 switch (frame_type ()) {
310 case VIDEO_FRAME_TYPE_2D:
311 case VIDEO_FRAME_TYPE_3D:
312 case VIDEO_FRAME_TYPE_3D_ALTERNATE:
313 case VIDEO_FRAME_TYPE_3D_LEFT:
314 case VIDEO_FRAME_TYPE_3D_RIGHT:
316 case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
317 return dcp::Size (s.width / 2, s.height);
318 case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
319 return dcp::Size (s.width, s.height / 2);
322 DCPOMATIC_ASSERT (false);
325 /** @return Video size after 3D split and crop */
327 VideoContent::size_after_crop () const
329 return crop().apply (size_after_3d_split ());
333 VideoContent::scale_and_crop_to_fit_width ()
335 shared_ptr<const Film> film = _parent->film ();
336 set_scale (VideoContentScale (film->container ()));
338 int const crop = max (0, int (size().height - double (film->frame_size().height) * size().width / film->frame_size().width));
341 set_top_crop (crop / 2);
342 set_bottom_crop (crop / 2);
346 VideoContent::scale_and_crop_to_fit_height ()
348 shared_ptr<const Film> film = _parent->film ();
349 set_scale (VideoContentScale (film->container ()));
351 int const crop = max (0, int (size().width - double (film->frame_size().width) * size().height / film->frame_size().height));
352 set_left_crop (crop / 2);
353 set_right_crop (crop / 2);
358 /** @param f Frame index within the whole (untrimmed) content */
360 VideoContent::fade (Frame f) const
362 DCPOMATIC_ASSERT (f >= 0);
364 shared_ptr<const Film> film = _parent->film ();
366 double const vfr = _parent->active_video_frame_rate ();
368 Frame const ts = _parent->trim_start().frames_round(vfr);
369 if ((f - ts) < fade_in()) {
370 return double (f - ts) / fade_in();
373 Frame fade_out_start = length() - _parent->trim_end().frames_round(vfr) - fade_out();
374 if (f >= fade_out_start) {
375 return 1 - double (f - fade_out_start) / fade_out();
378 return optional<double> ();
382 VideoContent::processing_description () const
387 if (size().width && size().height) {
388 d += String::compose (
389 _("Content video is %1x%2"),
390 size_after_3d_split().width,
391 size_after_3d_split().height
395 double ratio = size_after_3d_split().ratio ();
397 if (sample_aspect_ratio ()) {
398 snprintf (buffer, sizeof(buffer), _(", pixel aspect ratio %.2f:1"), sample_aspect_ratio().get());
400 ratio *= sample_aspect_ratio().get ();
403 snprintf (buffer, sizeof(buffer), _("\nDisplay aspect ratio %.2f:1"), ratio);
407 if ((crop().left || crop().right || crop().top || crop().bottom) && size() != dcp::Size (0, 0)) {
408 dcp::Size cropped = size_after_crop ();
409 d += String::compose (
410 _("\nCropped to %1x%2"),
411 cropped.width, cropped.height
414 snprintf (buffer, sizeof(buffer), " (%.2f:1)", cropped.ratio());
418 shared_ptr<const Film> film = _parent->film ();
419 dcp::Size const container_size = film->frame_size ();
420 dcp::Size const scaled = scale().size (shared_from_this(), container_size, container_size);
422 if (scaled != size_after_crop ()) {
423 d += String::compose (
424 _("\nScaled to %1x%2"),
425 scaled.width, scaled.height
428 snprintf (buffer, sizeof(buffer), _(" (%.2f:1)"), scaled.ratio());
432 if (scaled != container_size) {
433 d += String::compose (
434 _("\nPadded with black to fit container %1 (%2x%3)"),
435 film->container()->nickname (),
436 container_size.width, container_size.height
439 snprintf (buffer, sizeof(buffer), _(" (%.2f:1)"), container_size.ratio());
443 if (_parent->video_frame_rate()) {
444 double const vfr = _parent->video_frame_rate().get ();
446 snprintf (buffer, sizeof(buffer), _("\nContent frame rate %.4f\n"), vfr);
449 FrameRateChange frc (vfr, film->video_frame_rate ());
450 d += frc.description ();
457 VideoContent::add_properties (list<UserProperty>& p) const
459 p.push_back (UserProperty (UserProperty::VIDEO, _("Length"), length (), _("video frames")));
460 p.push_back (UserProperty (UserProperty::VIDEO, _("Size"), String::compose ("%1x%2", size().width, size().height)));
464 VideoContent::set_length (Frame len)
466 maybe_set (_length, len, ContentProperty::LENGTH);
470 VideoContent::set_left_crop (int c)
472 maybe_set (_crop.left, c, VideoContentProperty::CROP);
476 VideoContent::set_right_crop (int c)
478 maybe_set (_crop.right, c, VideoContentProperty::CROP);
482 VideoContent::set_top_crop (int c)
484 maybe_set (_crop.top, c, VideoContentProperty::CROP);
488 VideoContent::set_bottom_crop (int c)
490 maybe_set (_crop.bottom, c, VideoContentProperty::CROP);
494 VideoContent::set_scale (VideoContentScale s)
496 maybe_set (_scale, s, VideoContentProperty::SCALE);
500 VideoContent::set_frame_type (VideoFrameType t)
502 maybe_set (_frame_type, t, VideoContentProperty::FRAME_TYPE);
506 VideoContent::unset_colour_conversion ()
508 maybe_set (_colour_conversion, boost::optional<ColourConversion> (), VideoContentProperty::COLOUR_CONVERSION);
512 VideoContent::set_colour_conversion (ColourConversion c)
514 maybe_set (_colour_conversion, c, VideoContentProperty::COLOUR_CONVERSION);
518 VideoContent::set_fade_in (Frame t)
520 maybe_set (_fade_in, t, VideoContentProperty::FADE_IN);
524 VideoContent::set_fade_out (Frame t)
526 maybe_set (_fade_out, t, VideoContentProperty::FADE_OUT);
530 VideoContent::use_template (shared_ptr<const VideoContent> c)
532 _colour_conversion = c->_colour_conversion;
533 _frame_type = c->_frame_type;
536 _fade_in = c->_fade_in;
537 _fade_out = c->_fade_out;