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.
21 #include <libcxml/cxml.h>
22 #include <libdcp/colour_matrix.h>
23 #include <libdcp/raw_convert.h>
24 #include "video_content.h"
25 #include "video_examiner.h"
26 #include "compose.hpp"
29 #include "colour_conversion.h"
32 #include "exceptions.h"
33 #include "frame_rate_change.h"
34 #include "safe_stringstream.h"
38 int const VideoContentProperty::VIDEO_SIZE = 0;
39 int const VideoContentProperty::VIDEO_FRAME_RATE = 1;
40 int const VideoContentProperty::VIDEO_FRAME_TYPE = 2;
41 int const VideoContentProperty::VIDEO_CROP = 3;
42 int const VideoContentProperty::VIDEO_SCALE = 4;
43 int const VideoContentProperty::COLOUR_CONVERSION = 5;
46 using std::setprecision;
51 using boost::shared_ptr;
52 using boost::optional;
53 using boost::dynamic_pointer_cast;
54 using libdcp::raw_convert;
56 vector<VideoContentScale> VideoContentScale::_scales;
58 VideoContent::VideoContent (shared_ptr<const Film> f)
61 , _original_video_frame_rate (0)
62 , _video_frame_rate (0)
63 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
64 , _scale (Config::instance()->default_scale ())
66 setup_default_colour_conversion ();
69 VideoContent::VideoContent (shared_ptr<const Film> f, Time s, VideoContent::Frame len)
72 , _original_video_frame_rate (0)
73 , _video_frame_rate (0)
74 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
75 , _scale (Config::instance()->default_scale ())
77 setup_default_colour_conversion ();
80 VideoContent::VideoContent (shared_ptr<const Film> f, boost::filesystem::path p)
83 , _original_video_frame_rate (0)
84 , _video_frame_rate (0)
85 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
86 , _scale (Config::instance()->default_scale ())
88 setup_default_colour_conversion ();
91 VideoContent::VideoContent (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node, int version)
94 _video_length = node->number_child<VideoContent::Frame> ("VideoLength");
95 _video_size.width = node->number_child<int> ("VideoWidth");
96 _video_size.height = node->number_child<int> ("VideoHeight");
97 _video_frame_rate = node->number_child<float> ("VideoFrameRate");
98 _original_video_frame_rate = node->optional_number_child<float> ("OriginalVideoFrameRate").get_value_or (_video_frame_rate);
99 _video_frame_type = static_cast<VideoFrameType> (node->number_child<int> ("VideoFrameType"));
100 _crop.left = node->number_child<int> ("LeftCrop");
101 _crop.right = node->number_child<int> ("RightCrop");
102 _crop.top = node->number_child<int> ("TopCrop");
103 _crop.bottom = node->number_child<int> ("BottomCrop");
106 optional<string> r = node->optional_string_child ("Ratio");
108 _scale = VideoContentScale (Ratio::from_id (r.get ()));
111 _scale = VideoContentScale (node->node_child ("Scale"));
114 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"));
117 VideoContent::VideoContent (shared_ptr<const Film> f, vector<shared_ptr<Content> > c)
121 shared_ptr<VideoContent> ref = dynamic_pointer_cast<VideoContent> (c[0]);
124 for (size_t i = 0; i < c.size(); ++i) {
125 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (c[i]);
127 if (vc->video_size() != ref->video_size()) {
128 throw JoinError (_("Content to be joined must have the same picture size."));
131 if (vc->video_frame_rate() != ref->video_frame_rate()) {
132 throw JoinError (_("Content to be joined must have the same video frame rate."));
135 if (vc->video_frame_type() != ref->video_frame_type()) {
136 throw JoinError (_("Content to be joined must have the same video frame type."));
139 if (vc->crop() != ref->crop()) {
140 throw JoinError (_("Content to be joined must have the same crop."));
143 if (vc->scale() != ref->scale()) {
144 throw JoinError (_("Content to be joined must have the same scale setting."));
147 if (vc->colour_conversion() != ref->colour_conversion()) {
148 throw JoinError (_("Content to be joined must have the same colour conversion."));
151 _video_length += vc->video_length ();
154 _video_size = ref->video_size ();
155 _original_video_frame_rate = ref->original_video_frame_rate ();
156 _video_frame_rate = ref->video_frame_rate ();
157 _video_frame_type = ref->video_frame_type ();
158 _crop = ref->crop ();
159 _scale = ref->scale ();
160 _colour_conversion = ref->colour_conversion ();
164 VideoContent::as_xml (xmlpp::Node* node) const
166 boost::mutex::scoped_lock lm (_mutex);
167 node->add_child("VideoLength")->add_child_text (raw_convert<string> (_video_length));
168 node->add_child("VideoWidth")->add_child_text (raw_convert<string> (_video_size.width));
169 node->add_child("VideoHeight")->add_child_text (raw_convert<string> (_video_size.height));
170 node->add_child("VideoFrameRate")->add_child_text (raw_convert<string> (_video_frame_rate));
171 node->add_child("OriginalVideoFrameRate")->add_child_text (raw_convert<string> (_original_video_frame_rate));
172 node->add_child("VideoFrameType")->add_child_text (raw_convert<string> (static_cast<int> (_video_frame_type)));
174 _scale.as_xml (node->add_child("Scale"));
175 _colour_conversion.as_xml (node->add_child("ColourConversion"));
179 VideoContent::setup_default_colour_conversion ()
181 _colour_conversion = PresetColourConversion (_("sRGB"), 2.4, true, libdcp::colour_matrix::srgb_to_xyz, 2.6).conversion;
185 VideoContent::take_from_video_examiner (shared_ptr<VideoExaminer> d)
187 /* These examiner calls could call other content methods which take a lock on the mutex */
188 libdcp::Size const vs = d->video_size ();
189 float const vfr = d->video_frame_rate ();
192 boost::mutex::scoped_lock lm (_mutex);
194 _video_frame_rate = vfr;
195 _original_video_frame_rate = vfr;
198 signal_changed (VideoContentProperty::VIDEO_SIZE);
199 signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
204 VideoContent::information () const
206 if (video_size().width == 0 || video_size().height == 0) {
212 s << String::compose (
213 _("%1x%2 pixels (%3:1)"),
216 setprecision (3), video_size().ratio ()
223 VideoContent::set_left_crop (int c)
226 boost::mutex::scoped_lock lm (_mutex);
228 if (_crop.left == c) {
235 signal_changed (VideoContentProperty::VIDEO_CROP);
239 VideoContent::set_right_crop (int c)
242 boost::mutex::scoped_lock lm (_mutex);
243 if (_crop.right == c) {
250 signal_changed (VideoContentProperty::VIDEO_CROP);
254 VideoContent::set_top_crop (int c)
257 boost::mutex::scoped_lock lm (_mutex);
258 if (_crop.top == c) {
265 signal_changed (VideoContentProperty::VIDEO_CROP);
269 VideoContent::set_bottom_crop (int c)
272 boost::mutex::scoped_lock lm (_mutex);
273 if (_crop.bottom == c) {
280 signal_changed (VideoContentProperty::VIDEO_CROP);
284 VideoContent::set_scale (VideoContentScale s)
287 boost::mutex::scoped_lock lm (_mutex);
295 signal_changed (VideoContentProperty::VIDEO_SCALE);
298 /** @return string which includes everything about how this content looks */
300 VideoContent::identifier () const
303 s << Content::identifier()
304 << "_" << crop().left
305 << "_" << crop().right
307 << "_" << crop().bottom
308 << "_" << scale().id()
309 << "_" << colour_conversion().identifier ();
315 VideoContent::set_video_frame_type (VideoFrameType t)
318 boost::mutex::scoped_lock lm (_mutex);
319 _video_frame_type = t;
322 signal_changed (VideoContentProperty::VIDEO_FRAME_TYPE);
326 VideoContent::technical_summary () const
328 return String::compose (
329 "video: length %1, size %2x%3, rate %4",
330 video_length_after_3d_combine(), video_size().width, video_size().height, video_frame_rate()
335 VideoContent::video_size_after_3d_split () const
337 libdcp::Size const s = video_size ();
338 switch (video_frame_type ()) {
339 case VIDEO_FRAME_TYPE_2D:
340 case VIDEO_FRAME_TYPE_3D_ALTERNATE:
341 case VIDEO_FRAME_TYPE_3D_LEFT:
342 case VIDEO_FRAME_TYPE_3D_RIGHT:
344 case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
345 return libdcp::Size (s.width / 2, s.height);
346 case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
347 return libdcp::Size (s.width, s.height / 2);
354 VideoContent::set_colour_conversion (ColourConversion c)
357 boost::mutex::scoped_lock lm (_mutex);
358 _colour_conversion = c;
361 signal_changed (VideoContentProperty::COLOUR_CONVERSION);
364 /** @return Video size after 3D split and crop */
366 VideoContent::video_size_after_crop () const
368 return crop().apply (video_size_after_3d_split ());
371 /** @param t A time offset from the start of this piece of content.
372 * @return Corresponding frame index.
375 VideoContent::time_to_content_video_frames (Time t) const
377 shared_ptr<const Film> film = _film.lock ();
380 FrameRateChange frc (video_frame_rate(), film->video_frame_rate());
382 /* Here we are converting from time (in the DCP) to a frame number in the content.
383 Hence we need to use the DCP's frame rate and the double/skip correction, not
386 return t * film->video_frame_rate() / (frc.factor() * TIME_HZ);
390 VideoContent::scale_and_crop_to_fit_width ()
392 shared_ptr<const Film> film = _film.lock ();
395 set_scale (VideoContentScale (film->container ()));
397 int const crop = max (0, int (video_size().height - double (film->frame_size().height) * video_size().width / film->frame_size().width));
398 set_top_crop (crop / 2);
399 set_bottom_crop (crop / 2);
403 VideoContent::scale_and_crop_to_fit_height ()
405 shared_ptr<const Film> film = _film.lock ();
408 set_scale (VideoContentScale (film->container ()));
410 int const crop = max (0, int (video_size().width - double (film->frame_size().width) * video_size().height / film->frame_size().height));
411 set_left_crop (crop / 2);
412 set_right_crop (crop / 2);
416 VideoContent::set_video_frame_rate (float r)
419 boost::mutex::scoped_lock lm (_mutex);
420 if (_video_frame_rate == r) {
424 _video_frame_rate = r;
427 signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
430 VideoContentScale::VideoContentScale (Ratio const * r)
437 VideoContentScale::VideoContentScale ()
444 VideoContentScale::VideoContentScale (bool scale)
451 VideoContentScale::VideoContentScale (shared_ptr<cxml::Node> node)
455 optional<string> r = node->optional_string_child ("Ratio");
457 _ratio = Ratio::from_id (r.get ());
459 _scale = node->bool_child ("Scale");
464 VideoContentScale::as_xml (xmlpp::Node* node) const
467 node->add_child("Ratio")->add_child_text (_ratio->id ());
469 node->add_child("Scale")->add_child_text (_scale ? "1" : "0");
474 VideoContentScale::id () const
481 s << (_scale ? "S1" : "S0");
488 VideoContentScale::name () const
491 return _ratio->nickname ();
495 return _("No stretch");
498 return _("No scale");
502 VideoContentScale::from_id (string id)
504 Ratio const * r = Ratio::from_id (id);
506 return VideoContentScale (r);
510 return VideoContentScale (false);
513 return VideoContentScale (true);
516 /** @param display_container Size of the container that we are displaying this content in.
517 * @param film_container The size of the film's image.
520 VideoContentScale::size (shared_ptr<const VideoContent> c, libdcp::Size display_container, libdcp::Size film_container) const
523 return fit_ratio_within (_ratio->ratio (), display_container);
526 libdcp::Size const ac = c->video_size_after_crop ();
528 /* Force scale if the film_container is smaller than the content's image */
529 if (_scale || film_container.width < ac.width || film_container.height < ac.height) {
530 return fit_ratio_within (ac.ratio (), display_container);
533 /* Scale the image so that it will be in the right place in film_container, even if display_container is a
536 return libdcp::Size (
537 c->video_size().width * float(display_container.width) / film_container.width,
538 c->video_size().height * float(display_container.height) / film_container.height
543 VideoContentScale::setup_scales ()
545 vector<Ratio const *> ratios = Ratio::all ();
546 for (vector<Ratio const *>::const_iterator i = ratios.begin(); i != ratios.end(); ++i) {
547 _scales.push_back (VideoContentScale (*i));
550 _scales.push_back (VideoContentScale (true));
551 _scales.push_back (VideoContentScale (false));
555 operator== (VideoContentScale const & a, VideoContentScale const & b)
557 return (a.ratio() == b.ratio() && a.scale() == b.scale());
561 operator!= (VideoContentScale const & a, VideoContentScale const & b)
563 return (a.ratio() != b.ratio() || a.scale() != b.scale());