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"
37 int const VideoContentProperty::VIDEO_SIZE = 0;
38 int const VideoContentProperty::VIDEO_FRAME_RATE = 1;
39 int const VideoContentProperty::VIDEO_FRAME_TYPE = 2;
40 int const VideoContentProperty::VIDEO_CROP = 3;
41 int const VideoContentProperty::VIDEO_SCALE = 4;
42 int const VideoContentProperty::COLOUR_CONVERSION = 5;
45 using std::stringstream;
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 , _video_frame_rate (0)
62 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
63 , _scale (Ratio::from_id ("185"))
65 setup_default_colour_conversion ();
68 VideoContent::VideoContent (shared_ptr<const Film> f, Time s, VideoContent::Frame len)
71 , _video_frame_rate (0)
72 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
73 , _scale (Ratio::from_id ("185"))
75 setup_default_colour_conversion ();
78 VideoContent::VideoContent (shared_ptr<const Film> f, boost::filesystem::path p)
81 , _video_frame_rate (0)
82 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
83 , _scale (Ratio::from_id ("185"))
85 setup_default_colour_conversion ();
88 VideoContent::VideoContent (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node, int version)
91 _video_length = node->number_child<VideoContent::Frame> ("VideoLength");
92 _video_size.width = node->number_child<int> ("VideoWidth");
93 _video_size.height = node->number_child<int> ("VideoHeight");
94 _video_frame_rate = node->number_child<float> ("VideoFrameRate");
95 _video_frame_type = static_cast<VideoFrameType> (node->number_child<int> ("VideoFrameType"));
96 _crop.left = node->number_child<int> ("LeftCrop");
97 _crop.right = node->number_child<int> ("RightCrop");
98 _crop.top = node->number_child<int> ("TopCrop");
99 _crop.bottom = node->number_child<int> ("BottomCrop");
102 optional<string> r = node->optional_string_child ("Ratio");
104 _scale = VideoContentScale (Ratio::from_id (r.get ()));
107 _scale = VideoContentScale (node->node_child ("Scale"));
110 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"));
113 VideoContent::VideoContent (shared_ptr<const Film> f, vector<shared_ptr<Content> > c)
117 shared_ptr<VideoContent> ref = dynamic_pointer_cast<VideoContent> (c[0]);
120 for (size_t i = 0; i < c.size(); ++i) {
121 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (c[i]);
123 if (vc->video_size() != ref->video_size()) {
124 throw JoinError (_("Content to be joined must have the same picture size."));
127 if (vc->video_frame_rate() != ref->video_frame_rate()) {
128 throw JoinError (_("Content to be joined must have the same video frame rate."));
131 if (vc->video_frame_type() != ref->video_frame_type()) {
132 throw JoinError (_("Content to be joined must have the same video frame type."));
135 if (vc->crop() != ref->crop()) {
136 throw JoinError (_("Content to be joined must have the same crop."));
139 if (vc->scale() != ref->scale()) {
140 throw JoinError (_("Content to be joined must have the same scale setting."));
143 if (vc->colour_conversion() != ref->colour_conversion()) {
144 throw JoinError (_("Content to be joined must have the same colour conversion."));
147 _video_length += vc->video_length ();
150 _video_size = ref->video_size ();
151 _video_frame_rate = ref->video_frame_rate ();
152 _video_frame_type = ref->video_frame_type ();
153 _crop = ref->crop ();
154 _scale = ref->scale ();
155 _colour_conversion = ref->colour_conversion ();
159 VideoContent::as_xml (xmlpp::Node* node) const
161 boost::mutex::scoped_lock lm (_mutex);
162 node->add_child("VideoLength")->add_child_text (raw_convert<string> (_video_length));
163 node->add_child("VideoWidth")->add_child_text (raw_convert<string> (_video_size.width));
164 node->add_child("VideoHeight")->add_child_text (raw_convert<string> (_video_size.height));
165 node->add_child("VideoFrameRate")->add_child_text (raw_convert<string> (_video_frame_rate));
166 node->add_child("VideoFrameType")->add_child_text (raw_convert<string> (static_cast<int> (_video_frame_type)));
168 _scale.as_xml (node->add_child("Scale"));
169 _colour_conversion.as_xml (node->add_child("ColourConversion"));
173 VideoContent::setup_default_colour_conversion ()
175 _colour_conversion = PresetColourConversion (_("sRGB"), 2.4, true, libdcp::colour_matrix::srgb_to_xyz, 2.6).conversion;
179 VideoContent::take_from_video_examiner (shared_ptr<VideoExaminer> d)
181 /* These examiner calls could call other content methods which take a lock on the mutex */
182 libdcp::Size const vs = d->video_size ();
183 float const vfr = d->video_frame_rate ();
186 boost::mutex::scoped_lock lm (_mutex);
188 _video_frame_rate = vfr;
191 signal_changed (VideoContentProperty::VIDEO_SIZE);
192 signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
197 VideoContent::information () const
199 if (video_size().width == 0 || video_size().height == 0) {
205 s << String::compose (
206 _("%1x%2 pixels (%3:1)"),
209 setprecision (3), video_size().ratio ()
216 VideoContent::set_left_crop (int c)
219 boost::mutex::scoped_lock lm (_mutex);
221 if (_crop.left == c) {
228 signal_changed (VideoContentProperty::VIDEO_CROP);
232 VideoContent::set_right_crop (int c)
235 boost::mutex::scoped_lock lm (_mutex);
236 if (_crop.right == c) {
243 signal_changed (VideoContentProperty::VIDEO_CROP);
247 VideoContent::set_top_crop (int c)
250 boost::mutex::scoped_lock lm (_mutex);
251 if (_crop.top == c) {
258 signal_changed (VideoContentProperty::VIDEO_CROP);
262 VideoContent::set_bottom_crop (int c)
265 boost::mutex::scoped_lock lm (_mutex);
266 if (_crop.bottom == c) {
273 signal_changed (VideoContentProperty::VIDEO_CROP);
277 VideoContent::set_scale (VideoContentScale s)
280 boost::mutex::scoped_lock lm (_mutex);
288 signal_changed (VideoContentProperty::VIDEO_SCALE);
291 /** @return string which includes everything about how this content looks */
293 VideoContent::identifier () const
296 s << Content::identifier()
297 << "_" << crop().left
298 << "_" << crop().right
300 << "_" << crop().bottom
301 << "_" << scale().id()
302 << "_" << colour_conversion().identifier ();
308 VideoContent::set_video_frame_type (VideoFrameType t)
311 boost::mutex::scoped_lock lm (_mutex);
312 _video_frame_type = t;
315 signal_changed (VideoContentProperty::VIDEO_FRAME_TYPE);
319 VideoContent::technical_summary () const
321 return String::compose (
322 "video: length %1, size %2x%3, rate %4",
323 video_length_after_3d_combine(), video_size().width, video_size().height, video_frame_rate()
328 VideoContent::video_size_after_3d_split () const
330 libdcp::Size const s = video_size ();
331 switch (video_frame_type ()) {
332 case VIDEO_FRAME_TYPE_2D:
333 case VIDEO_FRAME_TYPE_3D_ALTERNATE:
334 case VIDEO_FRAME_TYPE_3D_LEFT:
335 case VIDEO_FRAME_TYPE_3D_RIGHT:
337 case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
338 return libdcp::Size (s.width / 2, s.height);
339 case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
340 return libdcp::Size (s.width, s.height / 2);
347 VideoContent::set_colour_conversion (ColourConversion c)
350 boost::mutex::scoped_lock lm (_mutex);
351 _colour_conversion = c;
354 signal_changed (VideoContentProperty::COLOUR_CONVERSION);
357 /** @return Video size after 3D split and crop */
359 VideoContent::video_size_after_crop () const
361 return crop().apply (video_size_after_3d_split ());
364 /** @param t A time offset from the start of this piece of content.
365 * @return Corresponding frame index.
368 VideoContent::time_to_content_video_frames (Time t) const
370 shared_ptr<const Film> film = _film.lock ();
373 FrameRateChange frc (video_frame_rate(), film->video_frame_rate());
375 /* Here we are converting from time (in the DCP) to a frame number in the content.
376 Hence we need to use the DCP's frame rate and the double/skip correction, not
379 return t * film->video_frame_rate() / (frc.factor() * TIME_HZ);
383 VideoContent::scale_and_crop_to_fit_width ()
385 shared_ptr<const Film> film = _film.lock ();
388 set_scale (VideoContentScale (film->container ()));
390 int const crop = max (0, int (video_size().height - double (film->frame_size().height) * video_size().width / film->frame_size().width));
391 set_top_crop (crop / 2);
392 set_bottom_crop (crop / 2);
396 VideoContent::scale_and_crop_to_fit_height ()
398 shared_ptr<const Film> film = _film.lock ();
401 set_scale (VideoContentScale (film->container ()));
403 int const crop = max (0, int (video_size().width - double (film->frame_size().width) * video_size().height / film->frame_size().height));
404 set_left_crop (crop / 2);
405 set_right_crop (crop / 2);
408 VideoContentScale::VideoContentScale (Ratio const * r)
415 VideoContentScale::VideoContentScale ()
422 VideoContentScale::VideoContentScale (bool scale)
429 VideoContentScale::VideoContentScale (shared_ptr<cxml::Node> node)
433 optional<string> r = node->optional_string_child ("Ratio");
435 _ratio = Ratio::from_id (r.get ());
437 _scale = node->bool_child ("Scale");
442 VideoContentScale::as_xml (xmlpp::Node* node) const
445 node->add_child("Ratio")->add_child_text (_ratio->id ());
447 node->add_child("Scale")->add_child_text (_scale ? "1" : "0");
452 VideoContentScale::id () const
457 s << _ratio->id () << "_";
459 s << (_scale ? "S1" : "S0");
466 VideoContentScale::name () const
469 return _ratio->nickname ();
473 return _("No stretch");
476 return _("No scale");
479 /** @param display_container Size of the container that we are displaying this content in.
480 * @param film_container The size of the film's image.
483 VideoContentScale::size (shared_ptr<const VideoContent> c, libdcp::Size display_container, libdcp::Size film_container) const
486 return fit_ratio_within (_ratio->ratio (), display_container);
489 libdcp::Size const ac = c->video_size_after_crop ();
491 /* Force scale if the film_container is smaller than the content's image */
492 if (_scale || film_container.width < ac.width || film_container.height < ac.height) {
493 return fit_ratio_within (ac.ratio (), display_container);
496 /* Scale the image so that it will be in the right place in film_container, even if display_container is a
499 return libdcp::Size (
500 c->video_size().width * float(display_container.width) / film_container.width,
501 c->video_size().height * float(display_container.height) / film_container.height
506 VideoContentScale::setup_scales ()
508 vector<Ratio const *> ratios = Ratio::all ();
509 for (vector<Ratio const *>::const_iterator i = ratios.begin(); i != ratios.end(); ++i) {
510 _scales.push_back (VideoContentScale (*i));
513 _scales.push_back (VideoContentScale (true));
514 _scales.push_back (VideoContentScale (false));
518 operator== (VideoContentScale const & a, VideoContentScale const & b)
520 return (a.ratio() == b.ratio() && a.scale() == b.scale());
524 operator!= (VideoContentScale const & a, VideoContentScale const & b)
526 return (a.ratio() != b.ratio() || a.scale() != b.scale());