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 <dcp/colour_matrix.h>
23 #include <dcp/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"
36 int const VideoContentProperty::VIDEO_SIZE = 0;
37 int const VideoContentProperty::VIDEO_FRAME_RATE = 1;
38 int const VideoContentProperty::VIDEO_FRAME_TYPE = 2;
39 int const VideoContentProperty::VIDEO_CROP = 3;
40 int const VideoContentProperty::VIDEO_SCALE = 4;
41 int const VideoContentProperty::COLOUR_CONVERSION = 5;
44 using std::stringstream;
45 using std::setprecision;
48 using boost::shared_ptr;
49 using boost::optional;
50 using boost::dynamic_pointer_cast;
51 using dcp::raw_convert;
53 vector<VideoContentScale> VideoContentScale::_scales;
55 VideoContent::VideoContent (shared_ptr<const Film> f)
58 , _video_frame_rate (0)
59 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
60 , _scale (Ratio::from_id ("185"))
62 setup_default_colour_conversion ();
65 VideoContent::VideoContent (shared_ptr<const Film> f, DCPTime s, ContentTime len)
68 , _video_frame_rate (0)
69 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
70 , _scale (Ratio::from_id ("185"))
72 setup_default_colour_conversion ();
75 VideoContent::VideoContent (shared_ptr<const Film> f, boost::filesystem::path p)
78 , _video_frame_rate (0)
79 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
80 , _scale (Ratio::from_id ("185"))
82 setup_default_colour_conversion ();
85 VideoContent::VideoContent (shared_ptr<const Film> f, cxml::ConstNodePtr node, int version)
88 _video_length = ContentTime (node->number_child<int64_t> ("VideoLength"));
89 _video_size.width = node->number_child<int> ("VideoWidth");
90 _video_size.height = node->number_child<int> ("VideoHeight");
91 _video_frame_rate = node->number_child<float> ("VideoFrameRate");
92 _video_frame_type = static_cast<VideoFrameType> (node->number_child<int> ("VideoFrameType"));
93 _crop.left = node->number_child<int> ("LeftCrop");
94 _crop.right = node->number_child<int> ("RightCrop");
95 _crop.top = node->number_child<int> ("TopCrop");
96 _crop.bottom = node->number_child<int> ("BottomCrop");
99 optional<string> r = node->optional_string_child ("Ratio");
101 _scale = VideoContentScale (Ratio::from_id (r.get ()));
104 _scale = VideoContentScale (node->node_child ("Scale"));
107 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"));
110 VideoContent::VideoContent (shared_ptr<const Film> f, vector<shared_ptr<Content> > c)
114 shared_ptr<VideoContent> ref = dynamic_pointer_cast<VideoContent> (c[0]);
117 for (size_t i = 0; i < c.size(); ++i) {
118 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (c[i]);
120 if (vc->video_size() != ref->video_size()) {
121 throw JoinError (_("Content to be joined must have the same picture size."));
124 if (vc->video_frame_rate() != ref->video_frame_rate()) {
125 throw JoinError (_("Content to be joined must have the same video frame rate."));
128 if (vc->video_frame_type() != ref->video_frame_type()) {
129 throw JoinError (_("Content to be joined must have the same video frame type."));
132 if (vc->crop() != ref->crop()) {
133 throw JoinError (_("Content to be joined must have the same crop."));
136 if (vc->scale() != ref->scale()) {
137 throw JoinError (_("Content to be joined must have the same scale setting."));
140 if (vc->colour_conversion() != ref->colour_conversion()) {
141 throw JoinError (_("Content to be joined must have the same colour conversion."));
144 _video_length += vc->video_length ();
147 _video_size = ref->video_size ();
148 _video_frame_rate = ref->video_frame_rate ();
149 _video_frame_type = ref->video_frame_type ();
150 _crop = ref->crop ();
151 _scale = ref->scale ();
152 _colour_conversion = ref->colour_conversion ();
156 VideoContent::as_xml (xmlpp::Node* node) const
158 boost::mutex::scoped_lock lm (_mutex);
159 node->add_child("VideoLength")->add_child_text (raw_convert<string> (_video_length.get ()));
160 node->add_child("VideoWidth")->add_child_text (raw_convert<string> (_video_size.width));
161 node->add_child("VideoHeight")->add_child_text (raw_convert<string> (_video_size.height));
162 node->add_child("VideoFrameRate")->add_child_text (raw_convert<string> (_video_frame_rate));
163 node->add_child("VideoFrameType")->add_child_text (raw_convert<string> (static_cast<int> (_video_frame_type)));
164 node->add_child("LeftCrop")->add_child_text (raw_convert<string> (_crop.left));
165 node->add_child("RightCrop")->add_child_text (raw_convert<string> (_crop.right));
166 node->add_child("TopCrop")->add_child_text (raw_convert<string> (_crop.top));
167 node->add_child("BottomCrop")->add_child_text (raw_convert<string> (_crop.bottom));
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, dcp::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 dcp::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().seconds(),
331 VideoContent::video_size_after_3d_split () const
333 dcp::Size const s = video_size ();
334 switch (video_frame_type ()) {
335 case VIDEO_FRAME_TYPE_2D:
336 case VIDEO_FRAME_TYPE_3D_ALTERNATE:
337 case VIDEO_FRAME_TYPE_3D_LEFT:
338 case VIDEO_FRAME_TYPE_3D_RIGHT:
340 case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
341 return dcp::Size (s.width / 2, s.height);
342 case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
343 return dcp::Size (s.width, s.height / 2);
350 VideoContent::set_colour_conversion (ColourConversion c)
353 boost::mutex::scoped_lock lm (_mutex);
354 _colour_conversion = c;
357 signal_changed (VideoContentProperty::COLOUR_CONVERSION);
360 /** @return Video size after 3D split and crop */
362 VideoContent::video_size_after_crop () const
364 return crop().apply (video_size_after_3d_split ());
367 /** @param t A time offset from the start of this piece of content.
368 * @return Corresponding time with respect to the content.
371 VideoContent::dcp_time_to_content_time (DCPTime t) const
373 shared_ptr<const Film> film = _film.lock ();
375 return ContentTime (t, FrameRateChange (video_frame_rate(), film->video_frame_rate()));
378 VideoContentScale::VideoContentScale (Ratio const * r)
385 VideoContentScale::VideoContentScale ()
392 VideoContentScale::VideoContentScale (bool scale)
399 VideoContentScale::VideoContentScale (cxml::NodePtr node)
403 optional<string> r = node->optional_string_child ("Ratio");
405 _ratio = Ratio::from_id (r.get ());
407 _scale = node->bool_child ("Scale");
412 VideoContentScale::as_xml (xmlpp::Node* node) const
415 node->add_child("Ratio")->add_child_text (_ratio->id ());
417 node->add_child("Scale")->add_child_text (_scale ? "1" : "0");
422 VideoContentScale::id () const
427 s << _ratio->id () << "_";
429 s << (_scale ? "S1" : "S0");
436 VideoContentScale::name () const
439 return _ratio->nickname ();
443 return _("No stretch");
446 return _("No scale");
449 /** @param display_container Size of the container that we are displaying this content in.
450 * @param film_container The size of the film's image.
453 VideoContentScale::size (shared_ptr<const VideoContent> c, dcp::Size display_container, dcp::Size film_container) const
456 return fit_ratio_within (_ratio->ratio (), display_container);
459 dcp::Size const ac = c->video_size_after_crop ();
461 /* Force scale if the film_container is smaller than the content's image */
462 if (_scale || film_container.width < ac.width || film_container.height < ac.height) {
463 return fit_ratio_within (ac.ratio (), display_container);
466 /* Scale the image so that it will be in the right place in film_container, even if display_container is a
470 c->video_size().width * float(display_container.width) / film_container.width,
471 c->video_size().height * float(display_container.height) / film_container.height
476 VideoContentScale::setup_scales ()
478 vector<Ratio const *> ratios = Ratio::all ();
479 for (vector<Ratio const *>::const_iterator i = ratios.begin(); i != ratios.end(); ++i) {
480 _scales.push_back (VideoContentScale (*i));
483 _scales.push_back (VideoContentScale (true));
484 _scales.push_back (VideoContentScale (false));
488 operator== (VideoContentScale const & a, VideoContentScale const & b)
490 return (a.ratio() == b.ratio() && a.scale() == b.scale());
494 operator!= (VideoContentScale const & a, VideoContentScale const & b)
496 return (a.ratio() != b.ratio() || a.scale() != b.scale());