2 Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "video_content.h"
21 #include "video_examiner.h"
22 #include "compose.hpp"
25 #include "colour_conversion.h"
28 #include "exceptions.h"
29 #include "frame_rate_change.h"
31 #include "safe_stringstream.h"
32 #include "raw_convert.h"
33 #include <libcxml/cxml.h>
34 #include <dcp/colour_matrix.h>
39 #define LOG_GENERAL(...) film->log()->log (String::compose (__VA_ARGS__), Log::TYPE_GENERAL);
41 int const VideoContentProperty::VIDEO_SIZE = 0;
42 int const VideoContentProperty::VIDEO_FRAME_RATE = 1;
43 int const VideoContentProperty::VIDEO_FRAME_TYPE = 2;
44 int const VideoContentProperty::VIDEO_CROP = 3;
45 int const VideoContentProperty::VIDEO_SCALE = 4;
46 int const VideoContentProperty::COLOUR_CONVERSION = 5;
47 int const VideoContentProperty::VIDEO_FADE_IN = 6;
48 int const VideoContentProperty::VIDEO_FADE_OUT = 7;
51 using std::setprecision;
56 using std::stringstream;
58 using std::setprecision;
59 using boost::shared_ptr;
60 using boost::optional;
61 using boost::dynamic_pointer_cast;
63 VideoContent::VideoContent (shared_ptr<const Film> f)
66 , _video_frame_rate (0)
67 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
68 , _scale (VideoContentScale (Ratio::from_id ("178")))
70 set_default_colour_conversion ();
73 VideoContent::VideoContent (shared_ptr<const Film> f, DCPTime s, ContentTime len)
76 , _video_frame_rate (0)
77 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
78 , _scale (VideoContentScale (Ratio::from_id ("178")))
80 set_default_colour_conversion ();
83 VideoContent::VideoContent (shared_ptr<const Film> f, boost::filesystem::path p)
86 , _video_frame_rate (0)
87 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
88 , _scale (VideoContentScale (Ratio::from_id ("178")))
90 set_default_colour_conversion ();
93 VideoContent::VideoContent (shared_ptr<const Film> f, cxml::ConstNodePtr node, int version)
96 _video_size.width = node->number_child<int> ("VideoWidth");
97 _video_size.height = node->number_child<int> ("VideoHeight");
98 _video_frame_rate = node->number_child<float> ("VideoFrameRate");
101 /* DCP-o-matic 1.0 branch */
102 _video_length = ContentTime::from_frames (node->number_child<int64_t> ("VideoLength"), _video_frame_rate);
104 _video_length = ContentTime (node->number_child<ContentTime::Type> ("VideoLength"));
107 _video_frame_type = static_cast<VideoFrameType> (node->number_child<int> ("VideoFrameType"));
108 _sample_aspect_ratio = node->optional_number_child<float> ("SampleAspectRatio");
109 _crop.left = node->number_child<int> ("LeftCrop");
110 _crop.right = node->number_child<int> ("RightCrop");
111 _crop.top = node->number_child<int> ("TopCrop");
112 _crop.bottom = node->number_child<int> ("BottomCrop");
115 optional<string> r = node->optional_string_child ("Ratio");
117 _scale = VideoContentScale (Ratio::from_id (r.get ()));
120 _scale = VideoContentScale (node->node_child ("Scale"));
124 if (node->optional_node_child ("ColourConversion")) {
125 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"), version);
128 _fade_in = ContentTime (node->number_child<int64_t> ("FadeIn"));
129 _fade_out = ContentTime (node->number_child<int64_t> ("FadeOut"));
133 VideoContent::VideoContent (shared_ptr<const Film> f, vector<shared_ptr<Content> > c)
137 shared_ptr<VideoContent> ref = dynamic_pointer_cast<VideoContent> (c[0]);
138 DCPOMATIC_ASSERT (ref);
140 for (size_t i = 0; i < c.size(); ++i) {
141 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (c[i]);
143 if (vc->video_size() != ref->video_size()) {
144 throw JoinError (_("Content to be joined must have the same picture size."));
147 if (vc->video_frame_rate() != ref->video_frame_rate()) {
148 throw JoinError (_("Content to be joined must have the same video frame rate."));
151 if (vc->video_frame_type() != ref->video_frame_type()) {
152 throw JoinError (_("Content to be joined must have the same video frame type."));
155 if (vc->crop() != ref->crop()) {
156 throw JoinError (_("Content to be joined must have the same crop."));
159 if (vc->scale() != ref->scale()) {
160 throw JoinError (_("Content to be joined must have the same scale setting."));
163 if (vc->colour_conversion() != ref->colour_conversion()) {
164 throw JoinError (_("Content to be joined must have the same colour conversion."));
167 if (vc->fade_in() != ref->fade_in() || vc->fade_out() != ref->fade_out()) {
168 throw JoinError (_("Content to be joined must have the same fades."));
171 _video_length += vc->video_length ();
174 _video_size = ref->video_size ();
175 _video_frame_rate = ref->video_frame_rate ();
176 _video_frame_type = ref->video_frame_type ();
177 _crop = ref->crop ();
178 _scale = ref->scale ();
179 _colour_conversion = ref->colour_conversion ();
180 _fade_in = ref->fade_in ();
181 _fade_out = ref->fade_out ();
185 VideoContent::as_xml (xmlpp::Node* node) const
187 boost::mutex::scoped_lock lm (_mutex);
188 node->add_child("VideoLength")->add_child_text (raw_convert<string> (_video_length.get ()));
189 node->add_child("VideoWidth")->add_child_text (raw_convert<string> (_video_size.width));
190 node->add_child("VideoHeight")->add_child_text (raw_convert<string> (_video_size.height));
191 node->add_child("VideoFrameRate")->add_child_text (raw_convert<string> (_video_frame_rate));
192 node->add_child("VideoFrameType")->add_child_text (raw_convert<string> (static_cast<int> (_video_frame_type)));
193 if (_sample_aspect_ratio) {
194 node->add_child("SampleAspectRatio")->add_child_text (raw_convert<string> (_sample_aspect_ratio.get ()));
197 _scale.as_xml (node->add_child("Scale"));
198 if (_colour_conversion) {
199 _colour_conversion.get().as_xml (node->add_child("ColourConversion"));
201 node->add_child("FadeIn")->add_child_text (raw_convert<string> (_fade_in.get ()));
202 node->add_child("FadeOut")->add_child_text (raw_convert<string> (_fade_out.get ()));
206 VideoContent::set_default_colour_conversion ()
208 /* If there's no better offer we'll use Rec. 709 */
209 boost::mutex::scoped_lock lm (_mutex);
210 _colour_conversion = PresetColourConversion::from_id ("rec709").conversion;
214 VideoContent::take_from_video_examiner (shared_ptr<VideoExaminer> d)
216 /* These examiner calls could call other content methods which take a lock on the mutex */
217 dcp::Size const vs = d->video_size ();
218 optional<float> const vfr = d->video_frame_rate ();
219 ContentTime vl = d->video_length ();
220 optional<float> const ar = d->sample_aspect_ratio ();
223 boost::mutex::scoped_lock lm (_mutex);
225 /* Default video frame rate to 24fps if the examiner doesn't know */
226 _video_frame_rate = vfr.get_value_or (24);
228 _sample_aspect_ratio = ar;
230 /* Guess correct scale from size and sample aspect ratio */
231 _scale = VideoContentScale (
232 Ratio::nearest_from_ratio (float (_video_size.width) * ar.get_value_or (1) / _video_size.height)
236 shared_ptr<const Film> film = _film.lock ();
237 DCPOMATIC_ASSERT (film);
238 LOG_GENERAL ("Video length obtained from header as %1 frames", _video_length.frames (_video_frame_rate));
240 set_default_colour_conversion ();
242 signal_changed (VideoContentProperty::VIDEO_SIZE);
243 signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
244 signal_changed (VideoContentProperty::VIDEO_SCALE);
245 signal_changed (ContentProperty::LENGTH);
249 VideoContent::set_left_crop (int c)
252 boost::mutex::scoped_lock lm (_mutex);
254 if (_crop.left == c) {
261 signal_changed (VideoContentProperty::VIDEO_CROP);
265 VideoContent::set_right_crop (int c)
268 boost::mutex::scoped_lock lm (_mutex);
269 if (_crop.right == c) {
276 signal_changed (VideoContentProperty::VIDEO_CROP);
280 VideoContent::set_top_crop (int c)
283 boost::mutex::scoped_lock lm (_mutex);
284 if (_crop.top == c) {
291 signal_changed (VideoContentProperty::VIDEO_CROP);
295 VideoContent::set_bottom_crop (int c)
298 boost::mutex::scoped_lock lm (_mutex);
299 if (_crop.bottom == c) {
306 signal_changed (VideoContentProperty::VIDEO_CROP);
310 VideoContent::set_scale (VideoContentScale s)
313 boost::mutex::scoped_lock lm (_mutex);
321 signal_changed (VideoContentProperty::VIDEO_SCALE);
324 /** @return string which includes everything about how this content looks */
326 VideoContent::identifier () const
329 s << Content::identifier()
330 << "_" << crop().left
331 << "_" << crop().right
333 << "_" << crop().bottom
334 << "_" << scale().id();
336 if (colour_conversion()) {
337 s << "_" << colour_conversion().get().identifier ();
344 VideoContent::set_video_frame_type (VideoFrameType t)
347 boost::mutex::scoped_lock lm (_mutex);
348 _video_frame_type = t;
351 signal_changed (VideoContentProperty::VIDEO_FRAME_TYPE);
355 VideoContent::technical_summary () const
357 string s = String::compose (
358 N_("video: length %1, size %2x%3, rate %4"),
359 video_length_after_3d_combine().seconds(),
365 if (sample_aspect_ratio ()) {
366 s += String::compose (N_(", sample aspect ratio %1"), (sample_aspect_ratio().get ()));
373 VideoContent::video_size_after_3d_split () const
375 dcp::Size const s = video_size ();
376 switch (video_frame_type ()) {
377 case VIDEO_FRAME_TYPE_2D:
378 case VIDEO_FRAME_TYPE_3D_ALTERNATE:
379 case VIDEO_FRAME_TYPE_3D_LEFT:
380 case VIDEO_FRAME_TYPE_3D_RIGHT:
382 case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
383 return dcp::Size (s.width / 2, s.height);
384 case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
385 return dcp::Size (s.width, s.height / 2);
388 DCPOMATIC_ASSERT (false);
392 VideoContent::unset_colour_conversion (bool signal)
395 boost::mutex::scoped_lock lm (_mutex);
396 _colour_conversion = boost::optional<ColourConversion> ();
400 signal_changed (VideoContentProperty::COLOUR_CONVERSION);
405 VideoContent::set_colour_conversion (ColourConversion c)
408 boost::mutex::scoped_lock lm (_mutex);
409 _colour_conversion = c;
412 signal_changed (VideoContentProperty::COLOUR_CONVERSION);
416 VideoContent::set_fade_in (ContentTime t)
419 boost::mutex::scoped_lock lm (_mutex);
423 signal_changed (VideoContentProperty::VIDEO_FADE_IN);
427 VideoContent::set_fade_out (ContentTime t)
430 boost::mutex::scoped_lock lm (_mutex);
434 signal_changed (VideoContentProperty::VIDEO_FADE_OUT);
437 /** @return Video size after 3D split and crop */
439 VideoContent::video_size_after_crop () const
441 return crop().apply (video_size_after_3d_split ());
445 VideoContent::scale_and_crop_to_fit_width ()
447 shared_ptr<const Film> film = _film.lock ();
448 DCPOMATIC_ASSERT (film);
450 set_scale (VideoContentScale (film->container ()));
452 int const crop = max (0, int (video_size().height - double (film->frame_size().height) * video_size().width / film->frame_size().width));
453 set_top_crop (crop / 2);
454 set_bottom_crop (crop / 2);
458 VideoContent::scale_and_crop_to_fit_height ()
460 shared_ptr<const Film> film = _film.lock ();
461 DCPOMATIC_ASSERT (film);
463 set_scale (VideoContentScale (film->container ()));
465 int const crop = max (0, int (video_size().width - double (film->frame_size().width) * video_size().height / film->frame_size().height));
466 set_left_crop (crop / 2);
467 set_right_crop (crop / 2);
471 VideoContent::set_video_frame_rate (float r)
474 boost::mutex::scoped_lock lm (_mutex);
475 if (_video_frame_rate == r) {
479 _video_frame_rate = r;
482 signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
486 VideoContent::fade (VideoFrame f) const
488 DCPOMATIC_ASSERT (f >= 0);
490 if (f < fade_in().frames (video_frame_rate ())) {
491 return float (f) / _fade_in.frames (video_frame_rate ());
494 VideoFrame fade_out_start = ContentTime (video_length() - fade_out()).frames (video_frame_rate ());
495 if (f >= fade_out_start) {
496 return 1 - float (f - fade_out_start) / fade_out().frames (video_frame_rate ());
499 return optional<float> ();
503 VideoContent::processing_description () const
505 /* stringstream is OK here as this string is just for presentation to the user */
508 if (video_size().width && video_size().height) {
509 d << String::compose (
510 _("Content video is %1x%2"),
511 video_size_after_3d_split().width,
512 video_size_after_3d_split().height
516 float ratio = video_size_after_3d_split().ratio ();
518 if (sample_aspect_ratio ()) {
519 d << ", " << _("pixel aspect ratio") << " " << fixed << setprecision(2) << sample_aspect_ratio().get () << ":1";
520 ratio *= sample_aspect_ratio().get ();
523 d << "\n" << _("Display aspect ratio") << " " << fixed << setprecision(2) << ratio << ":1\n";
526 if ((crop().left || crop().right || crop().top || crop().bottom) && video_size() != dcp::Size (0, 0)) {
527 dcp::Size cropped = video_size_after_crop ();
528 d << String::compose (
529 _("Cropped to %1x%2"),
530 cropped.width, cropped.height
533 d << " (" << fixed << setprecision(2) << cropped.ratio () << ":1)\n";
536 shared_ptr<const Film> film = _film.lock ();
537 DCPOMATIC_ASSERT (film);
539 dcp::Size const container_size = film->frame_size ();
540 dcp::Size const scaled = scale().size (dynamic_pointer_cast<const VideoContent> (shared_from_this ()), container_size, container_size);
542 if (scaled != video_size_after_crop ()) {
543 d << String::compose (
544 _("Scaled to %1x%2"),
545 scaled.width, scaled.height
548 d << " (" << fixed << setprecision(2) << scaled.ratio() << ":1)\n";
551 if (scaled != container_size) {
552 d << String::compose (
553 _("Padded with black to fit container %1 (%2x%3)"),
554 film->container()->nickname (),
555 container_size.width, container_size.height
558 d << " (" << fixed << setprecision(2) << container_size.ratio () << ":1)\n";
561 d << _("Content frame rate");
562 d << " " << fixed << setprecision(4) << video_frame_rate() << "\n";
564 FrameRateChange frc (video_frame_rate(), film->video_frame_rate ());
565 d << frc.description () << "\n";