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_size.width = node->number_child<int> ("VideoWidth");
89 _video_size.height = node->number_child<int> ("VideoHeight");
90 _video_frame_rate = node->number_child<float> ("VideoFrameRate");
93 /* DCP-o-matic 1.0 branch */
94 _video_length = ContentTime::from_frames (node->number_child<int64_t> ("VideoLength"), _video_frame_rate);
96 _video_length = ContentTime (node->number_child<int64_t> ("VideoLength"));
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 _video_frame_rate = ref->video_frame_rate ();
156 _video_frame_type = ref->video_frame_type ();
157 _crop = ref->crop ();
158 _scale = ref->scale ();
159 _colour_conversion = ref->colour_conversion ();
163 VideoContent::as_xml (xmlpp::Node* node) const
165 boost::mutex::scoped_lock lm (_mutex);
166 node->add_child("VideoLength")->add_child_text (raw_convert<string> (_video_length.get ()));
167 node->add_child("VideoWidth")->add_child_text (raw_convert<string> (_video_size.width));
168 node->add_child("VideoHeight")->add_child_text (raw_convert<string> (_video_size.height));
169 node->add_child("VideoFrameRate")->add_child_text (raw_convert<string> (_video_frame_rate));
170 node->add_child("VideoFrameType")->add_child_text (raw_convert<string> (static_cast<int> (_video_frame_type)));
172 _scale.as_xml (node->add_child("Scale"));
173 _colour_conversion.as_xml (node->add_child("ColourConversion"));
177 VideoContent::setup_default_colour_conversion ()
179 _colour_conversion = PresetColourConversion (_("sRGB"), 2.4, true, dcp::colour_matrix::srgb_to_xyz, 2.6).conversion;
183 VideoContent::take_from_video_examiner (shared_ptr<VideoExaminer> d)
185 /* These examiner calls could call other content methods which take a lock on the mutex */
186 dcp::Size const vs = d->video_size ();
187 float const vfr = d->video_frame_rate ();
190 boost::mutex::scoped_lock lm (_mutex);
192 _video_frame_rate = vfr;
195 signal_changed (VideoContentProperty::VIDEO_SIZE);
196 signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
201 VideoContent::information () const
203 if (video_size().width == 0 || video_size().height == 0) {
209 s << String::compose (
210 _("%1x%2 pixels (%3:1)"),
213 setprecision (3), video_size().ratio ()
220 VideoContent::set_left_crop (int c)
223 boost::mutex::scoped_lock lm (_mutex);
225 if (_crop.left == c) {
232 signal_changed (VideoContentProperty::VIDEO_CROP);
236 VideoContent::set_right_crop (int c)
239 boost::mutex::scoped_lock lm (_mutex);
240 if (_crop.right == c) {
247 signal_changed (VideoContentProperty::VIDEO_CROP);
251 VideoContent::set_top_crop (int c)
254 boost::mutex::scoped_lock lm (_mutex);
255 if (_crop.top == c) {
262 signal_changed (VideoContentProperty::VIDEO_CROP);
266 VideoContent::set_bottom_crop (int c)
269 boost::mutex::scoped_lock lm (_mutex);
270 if (_crop.bottom == c) {
277 signal_changed (VideoContentProperty::VIDEO_CROP);
281 VideoContent::set_scale (VideoContentScale s)
284 boost::mutex::scoped_lock lm (_mutex);
292 signal_changed (VideoContentProperty::VIDEO_SCALE);
295 /** @return string which includes everything about how this content looks */
297 VideoContent::identifier () const
300 s << Content::identifier()
301 << "_" << crop().left
302 << "_" << crop().right
304 << "_" << crop().bottom
305 << "_" << scale().id()
306 << "_" << colour_conversion().identifier ();
312 VideoContent::set_video_frame_type (VideoFrameType t)
315 boost::mutex::scoped_lock lm (_mutex);
316 _video_frame_type = t;
319 signal_changed (VideoContentProperty::VIDEO_FRAME_TYPE);
323 VideoContent::technical_summary () const
325 return String::compose (
326 "video: length %1, size %2x%3, rate %4",
327 video_length_after_3d_combine().seconds(),
335 VideoContent::video_size_after_3d_split () const
337 dcp::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 dcp::Size (s.width / 2, s.height);
346 case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
347 return dcp::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 time with respect to the content.
375 VideoContent::dcp_time_to_content_time (DCPTime t) const
377 shared_ptr<const Film> film = _film.lock ();
379 return ContentTime (t, FrameRateChange (video_frame_rate(), film->video_frame_rate()));
382 VideoContentScale::VideoContentScale (Ratio const * r)
389 VideoContentScale::VideoContentScale ()
396 VideoContentScale::VideoContentScale (bool scale)
403 VideoContentScale::VideoContentScale (cxml::NodePtr node)
407 optional<string> r = node->optional_string_child ("Ratio");
409 _ratio = Ratio::from_id (r.get ());
411 _scale = node->bool_child ("Scale");
416 VideoContentScale::as_xml (xmlpp::Node* node) const
419 node->add_child("Ratio")->add_child_text (_ratio->id ());
421 node->add_child("Scale")->add_child_text (_scale ? "1" : "0");
426 VideoContentScale::id () const
431 s << _ratio->id () << "_";
433 s << (_scale ? "S1" : "S0");
440 VideoContentScale::name () const
443 return _ratio->nickname ();
447 return _("No stretch");
450 return _("No scale");
453 /** @param display_container Size of the container that we are displaying this content in.
454 * @param film_container The size of the film's image.
457 VideoContentScale::size (shared_ptr<const VideoContent> c, dcp::Size display_container, dcp::Size film_container) const
460 return fit_ratio_within (_ratio->ratio (), display_container);
463 dcp::Size const ac = c->video_size_after_crop ();
465 /* Force scale if the film_container is smaller than the content's image */
466 if (_scale || film_container.width < ac.width || film_container.height < ac.height) {
467 return fit_ratio_within (ac.ratio (), display_container);
470 /* Scale the image so that it will be in the right place in film_container, even if display_container is a
474 c->video_size().width * float(display_container.width) / film_container.width,
475 c->video_size().height * float(display_container.height) / film_container.height
480 VideoContentScale::setup_scales ()
482 vector<Ratio const *> ratios = Ratio::all ();
483 for (vector<Ratio const *>::const_iterator i = ratios.begin(); i != ratios.end(); ++i) {
484 _scales.push_back (VideoContentScale (*i));
487 _scales.push_back (VideoContentScale (true));
488 _scales.push_back (VideoContentScale (false));
492 operator== (VideoContentScale const & a, VideoContentScale const & b)
494 return (a.ratio() == b.ratio() && a.scale() == b.scale());
498 operator!= (VideoContentScale const & a, VideoContentScale const & b)
500 return (a.ratio() != b.ratio() || a.scale() != b.scale());