2 Copyright (C) 2013-2014 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 <libcxml/cxml.h>
33 #include <dcp/colour_matrix.h>
34 #include <dcp/raw_convert.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;
62 using dcp::raw_convert;
64 VideoContent::VideoContent (shared_ptr<const Film> f)
67 , _video_frame_rate (0)
68 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
69 , _scale (VideoContentScale (Ratio::from_id ("178")))
71 set_default_colour_conversion (false);
74 VideoContent::VideoContent (shared_ptr<const Film> f, DCPTime s, ContentTime len)
77 , _video_frame_rate (0)
78 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
79 , _scale (VideoContentScale (Ratio::from_id ("178")))
81 set_default_colour_conversion (false);
84 VideoContent::VideoContent (shared_ptr<const Film> f, boost::filesystem::path p)
87 , _video_frame_rate (0)
88 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
89 , _scale (VideoContentScale (Ratio::from_id ("178")))
91 set_default_colour_conversion (false);
94 VideoContent::VideoContent (shared_ptr<const Film> f, cxml::ConstNodePtr node, int version)
97 _video_size.width = node->number_child<int> ("VideoWidth");
98 _video_size.height = node->number_child<int> ("VideoHeight");
99 _video_frame_rate = node->number_child<float> ("VideoFrameRate");
102 /* DCP-o-matic 1.0 branch */
103 _video_length = ContentTime::from_frames (node->number_child<int64_t> ("VideoLength"), _video_frame_rate);
105 _video_length = ContentTime (node->number_child<ContentTime::Type> ("VideoLength"));
108 _video_frame_type = static_cast<VideoFrameType> (node->number_child<int> ("VideoFrameType"));
109 _sample_aspect_ratio = node->optional_number_child<float> ("SampleAspectRatio");
110 _crop.left = node->number_child<int> ("LeftCrop");
111 _crop.right = node->number_child<int> ("RightCrop");
112 _crop.top = node->number_child<int> ("TopCrop");
113 _crop.bottom = node->number_child<int> ("BottomCrop");
116 optional<string> r = node->optional_string_child ("Ratio");
118 _scale = VideoContentScale (Ratio::from_id (r.get ()));
121 _scale = VideoContentScale (node->node_child ("Scale"));
125 if (node->optional_node_child ("ColourConversion")) {
126 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"));
129 _fade_in = ContentTime (node->number_child<int64_t> ("FadeIn"));
130 _fade_out = ContentTime (node->number_child<int64_t> ("FadeOut"));
134 VideoContent::VideoContent (shared_ptr<const Film> f, vector<shared_ptr<Content> > c)
138 shared_ptr<VideoContent> ref = dynamic_pointer_cast<VideoContent> (c[0]);
139 DCPOMATIC_ASSERT (ref);
141 for (size_t i = 0; i < c.size(); ++i) {
142 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (c[i]);
144 if (vc->video_size() != ref->video_size()) {
145 throw JoinError (_("Content to be joined must have the same picture size."));
148 if (vc->video_frame_rate() != ref->video_frame_rate()) {
149 throw JoinError (_("Content to be joined must have the same video frame rate."));
152 if (vc->video_frame_type() != ref->video_frame_type()) {
153 throw JoinError (_("Content to be joined must have the same video frame type."));
156 if (vc->crop() != ref->crop()) {
157 throw JoinError (_("Content to be joined must have the same crop."));
160 if (vc->scale() != ref->scale()) {
161 throw JoinError (_("Content to be joined must have the same scale setting."));
164 if (vc->colour_conversion() != ref->colour_conversion()) {
165 throw JoinError (_("Content to be joined must have the same colour conversion."));
168 if (vc->fade_in() != ref->fade_in() || vc->fade_out() != ref->fade_out()) {
169 throw JoinError (_("Content to be joined must have the same fades."));
172 _video_length += vc->video_length ();
175 _video_size = ref->video_size ();
176 _video_frame_rate = ref->video_frame_rate ();
177 _video_frame_type = ref->video_frame_type ();
178 _crop = ref->crop ();
179 _scale = ref->scale ();
180 _colour_conversion = ref->colour_conversion ();
181 _fade_in = ref->fade_in ();
182 _fade_out = ref->fade_out ();
186 VideoContent::as_xml (xmlpp::Node* node) const
188 boost::mutex::scoped_lock lm (_mutex);
189 node->add_child("VideoLength")->add_child_text (raw_convert<string> (_video_length.get ()));
190 node->add_child("VideoWidth")->add_child_text (raw_convert<string> (_video_size.width));
191 node->add_child("VideoHeight")->add_child_text (raw_convert<string> (_video_size.height));
192 node->add_child("VideoFrameRate")->add_child_text (raw_convert<string> (_video_frame_rate));
193 node->add_child("VideoFrameType")->add_child_text (raw_convert<string> (static_cast<int> (_video_frame_type)));
194 if (_sample_aspect_ratio) {
195 node->add_child("SampleAspectRatio")->add_child_text (raw_convert<string> (_sample_aspect_ratio.get ()));
198 _scale.as_xml (node->add_child("Scale"));
199 if (_colour_conversion) {
200 _colour_conversion.get().as_xml (node->add_child("ColourConversion"));
202 node->add_child("FadeIn")->add_child_text (raw_convert<string> (_fade_in.get ()));
203 node->add_child("FadeOut")->add_child_text (raw_convert<string> (_fade_out.get ()));
207 VideoContent::set_default_colour_conversion (bool signal)
210 boost::mutex::scoped_lock lm (_mutex);
211 _colour_conversion = ColourConversion (dcp::ColourConversion::srgb_to_xyz);
215 signal_changed (VideoContentProperty::COLOUR_CONVERSION);
220 VideoContent::take_from_video_examiner (shared_ptr<VideoExaminer> d)
222 /* These examiner calls could call other content methods which take a lock on the mutex */
223 dcp::Size const vs = d->video_size ();
224 optional<float> const vfr = d->video_frame_rate ();
225 ContentTime vl = d->video_length ();
226 optional<float> const ar = d->sample_aspect_ratio ();
229 boost::mutex::scoped_lock lm (_mutex);
231 /* Default video frame rate to 24fps if the examiner doesn't know */
232 _video_frame_rate = vfr.get_value_or (24);
234 _sample_aspect_ratio = ar;
236 /* Guess correct scale from size and sample aspect ratio */
237 _scale = VideoContentScale (
238 Ratio::nearest_from_ratio (float (_video_size.width) * ar.get_value_or (1) / _video_size.height)
242 shared_ptr<const Film> film = _film.lock ();
243 DCPOMATIC_ASSERT (film);
244 LOG_GENERAL ("Video length obtained from header as %1 frames", _video_length.frames (_video_frame_rate));
246 signal_changed (VideoContentProperty::VIDEO_SIZE);
247 signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
248 signal_changed (VideoContentProperty::VIDEO_SCALE);
249 signal_changed (ContentProperty::LENGTH);
253 VideoContent::set_left_crop (int c)
256 boost::mutex::scoped_lock lm (_mutex);
258 if (_crop.left == c) {
265 signal_changed (VideoContentProperty::VIDEO_CROP);
269 VideoContent::set_right_crop (int c)
272 boost::mutex::scoped_lock lm (_mutex);
273 if (_crop.right == c) {
280 signal_changed (VideoContentProperty::VIDEO_CROP);
284 VideoContent::set_top_crop (int c)
287 boost::mutex::scoped_lock lm (_mutex);
288 if (_crop.top == c) {
295 signal_changed (VideoContentProperty::VIDEO_CROP);
299 VideoContent::set_bottom_crop (int c)
302 boost::mutex::scoped_lock lm (_mutex);
303 if (_crop.bottom == c) {
310 signal_changed (VideoContentProperty::VIDEO_CROP);
314 VideoContent::set_scale (VideoContentScale s)
317 boost::mutex::scoped_lock lm (_mutex);
325 signal_changed (VideoContentProperty::VIDEO_SCALE);
328 /** @return string which includes everything about how this content looks */
330 VideoContent::identifier () const
333 s << Content::identifier()
334 << "_" << crop().left
335 << "_" << crop().right
337 << "_" << crop().bottom
338 << "_" << scale().id();
340 if (colour_conversion()) {
341 s << "_" << colour_conversion().get().identifier ();
348 VideoContent::set_video_frame_type (VideoFrameType t)
351 boost::mutex::scoped_lock lm (_mutex);
352 _video_frame_type = t;
355 signal_changed (VideoContentProperty::VIDEO_FRAME_TYPE);
359 VideoContent::technical_summary () const
361 string s = String::compose (
362 "video: length %1, size %2x%3, rate %4",
363 video_length_after_3d_combine().seconds(),
369 if (sample_aspect_ratio ()) {
370 s += String::compose (_(", sample aspect ratio %1"), (sample_aspect_ratio().get ()));
377 VideoContent::video_size_after_3d_split () const
379 dcp::Size const s = video_size ();
380 switch (video_frame_type ()) {
381 case VIDEO_FRAME_TYPE_2D:
382 case VIDEO_FRAME_TYPE_3D_ALTERNATE:
383 case VIDEO_FRAME_TYPE_3D_LEFT:
384 case VIDEO_FRAME_TYPE_3D_RIGHT:
386 case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
387 return dcp::Size (s.width / 2, s.height);
388 case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
389 return dcp::Size (s.width, s.height / 2);
392 DCPOMATIC_ASSERT (false);
396 VideoContent::unset_colour_conversion ()
399 boost::mutex::scoped_lock lm (_mutex);
400 _colour_conversion = boost::optional<ColourConversion> ();
403 signal_changed (VideoContentProperty::COLOUR_CONVERSION);
407 VideoContent::set_colour_conversion (ColourConversion c)
410 boost::mutex::scoped_lock lm (_mutex);
411 _colour_conversion = c;
414 signal_changed (VideoContentProperty::COLOUR_CONVERSION);
418 VideoContent::set_fade_in (ContentTime t)
421 boost::mutex::scoped_lock lm (_mutex);
425 signal_changed (VideoContentProperty::VIDEO_FADE_IN);
429 VideoContent::set_fade_out (ContentTime t)
432 boost::mutex::scoped_lock lm (_mutex);
436 signal_changed (VideoContentProperty::VIDEO_FADE_OUT);
439 /** @return Video size after 3D split and crop */
441 VideoContent::video_size_after_crop () const
443 return crop().apply (video_size_after_3d_split ());
446 /** @param t A time offset from the start of this piece of content.
447 * @return Corresponding time with respect to the content.
450 VideoContent::dcp_time_to_content_time (DCPTime t) const
452 shared_ptr<const Film> film = _film.lock ();
453 DCPOMATIC_ASSERT (film);
454 return ContentTime (t, FrameRateChange (video_frame_rate(), film->video_frame_rate()));
458 VideoContent::scale_and_crop_to_fit_width ()
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().height - double (film->frame_size().height) * video_size().width / film->frame_size().width));
466 set_top_crop (crop / 2);
467 set_bottom_crop (crop / 2);
471 VideoContent::scale_and_crop_to_fit_height ()
473 shared_ptr<const Film> film = _film.lock ();
474 DCPOMATIC_ASSERT (film);
476 set_scale (VideoContentScale (film->container ()));
478 int const crop = max (0, int (video_size().width - double (film->frame_size().width) * video_size().height / film->frame_size().height));
479 set_left_crop (crop / 2);
480 set_right_crop (crop / 2);
484 VideoContent::set_video_frame_rate (float r)
487 boost::mutex::scoped_lock lm (_mutex);
488 if (_video_frame_rate == r) {
492 _video_frame_rate = r;
495 signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
499 VideoContent::fade (VideoFrame f) const
501 DCPOMATIC_ASSERT (f >= 0);
503 if (f < fade_in().frames (video_frame_rate ())) {
504 return float (f) / _fade_in.frames (video_frame_rate ());
507 VideoFrame fade_out_start = ContentTime (video_length() - fade_out()).frames (video_frame_rate ());
508 if (f >= fade_out_start) {
509 return 1 - float (f - fade_out_start) / fade_out().frames (video_frame_rate ());
512 return optional<float> ();
516 VideoContent::processing_description () const
518 /* stringstream is OK here as this string is just for presentation to the user */
521 if (video_size().width && video_size().height) {
522 d << String::compose (
523 _("Content video is %1x%2"),
524 video_size_after_3d_split().width,
525 video_size_after_3d_split().height
528 d << " (" << fixed << setprecision(2) << video_size_after_3d_split().ratio() << ":1)\n";
531 if ((crop().left || crop().right || crop().top || crop().bottom) && video_size() != dcp::Size (0, 0)) {
532 dcp::Size cropped = video_size_after_crop ();
533 d << String::compose (
534 _("Cropped to %1x%2"),
535 cropped.width, cropped.height
538 d << " (" << fixed << setprecision(2) << cropped.ratio () << ":1)\n";
541 shared_ptr<const Film> film = _film.lock ();
542 DCPOMATIC_ASSERT (film);
544 dcp::Size const container_size = film->frame_size ();
545 dcp::Size const scaled = scale().size (dynamic_pointer_cast<const VideoContent> (shared_from_this ()), container_size, container_size, 1);
547 if (scaled != video_size_after_crop ()) {
548 d << String::compose (
549 _("Scaled to %1x%2"),
550 scaled.width, scaled.height
553 d << " (" << fixed << setprecision(2) << scaled.ratio() << ":1)\n";
556 if (scaled != container_size) {
557 d << String::compose (
558 _("Padded with black to %1x%2"),
559 container_size.width, container_size.height
562 d << " (" << fixed << setprecision(2) << container_size.ratio () << ":1)\n";
565 d << _("Content frame rate");
566 d << " " << fixed << setprecision(4) << video_frame_rate() << "\n";
568 FrameRateChange frc (video_frame_rate(), film->video_frame_rate ());
569 d << frc.description () << "\n";