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"
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 dcp::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, DCPTime s, ContentTime 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, cxml::ConstNodePtr node, int version)
91 _video_size.width = node->number_child<int> ("VideoWidth");
92 _video_size.height = node->number_child<int> ("VideoHeight");
93 _video_frame_rate = node->number_child<float> ("VideoFrameRate");
96 /* DCP-o-matic 1.0 branch */
97 _video_length = ContentTime::from_frames (node->number_child<int64_t> ("VideoLength"), _video_frame_rate);
99 _video_length = ContentTime (node->number_child<int64_t> ("VideoLength"));
102 _video_frame_type = static_cast<VideoFrameType> (node->number_child<int> ("VideoFrameType"));
103 _crop.left = node->number_child<int> ("LeftCrop");
104 _crop.right = node->number_child<int> ("RightCrop");
105 _crop.top = node->number_child<int> ("TopCrop");
106 _crop.bottom = node->number_child<int> ("BottomCrop");
109 optional<string> r = node->optional_string_child ("Ratio");
111 _scale = VideoContentScale (Ratio::from_id (r.get ()));
114 _scale = VideoContentScale (node->node_child ("Scale"));
117 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"));
120 VideoContent::VideoContent (shared_ptr<const Film> f, vector<shared_ptr<Content> > c)
124 shared_ptr<VideoContent> ref = dynamic_pointer_cast<VideoContent> (c[0]);
127 for (size_t i = 0; i < c.size(); ++i) {
128 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (c[i]);
130 if (vc->video_size() != ref->video_size()) {
131 throw JoinError (_("Content to be joined must have the same picture size."));
134 if (vc->video_frame_rate() != ref->video_frame_rate()) {
135 throw JoinError (_("Content to be joined must have the same video frame rate."));
138 if (vc->video_frame_type() != ref->video_frame_type()) {
139 throw JoinError (_("Content to be joined must have the same video frame type."));
142 if (vc->crop() != ref->crop()) {
143 throw JoinError (_("Content to be joined must have the same crop."));
146 if (vc->scale() != ref->scale()) {
147 throw JoinError (_("Content to be joined must have the same scale setting."));
150 if (vc->colour_conversion() != ref->colour_conversion()) {
151 throw JoinError (_("Content to be joined must have the same colour conversion."));
154 _video_length += vc->video_length ();
157 _video_size = ref->video_size ();
158 _video_frame_rate = ref->video_frame_rate ();
159 _video_frame_type = ref->video_frame_type ();
160 _crop = ref->crop ();
161 _scale = ref->scale ();
162 _colour_conversion = ref->colour_conversion ();
166 VideoContent::as_xml (xmlpp::Node* node) const
168 boost::mutex::scoped_lock lm (_mutex);
169 node->add_child("VideoLength")->add_child_text (raw_convert<string> (_video_length.get ()));
170 node->add_child("VideoWidth")->add_child_text (raw_convert<string> (_video_size.width));
171 node->add_child("VideoHeight")->add_child_text (raw_convert<string> (_video_size.height));
172 node->add_child("VideoFrameRate")->add_child_text (raw_convert<string> (_video_frame_rate));
173 node->add_child("VideoFrameType")->add_child_text (raw_convert<string> (static_cast<int> (_video_frame_type)));
175 _scale.as_xml (node->add_child("Scale"));
176 _colour_conversion.as_xml (node->add_child("ColourConversion"));
180 VideoContent::setup_default_colour_conversion ()
182 _colour_conversion = PresetColourConversion (_("sRGB"), 2.4, true, dcp::colour_matrix::srgb_to_xyz, 2.6).conversion;
186 VideoContent::take_from_video_examiner (shared_ptr<VideoExaminer> d)
188 /* These examiner calls could call other content methods which take a lock on the mutex */
189 dcp::Size const vs = d->video_size ();
190 float const vfr = d->video_frame_rate ();
193 boost::mutex::scoped_lock lm (_mutex);
195 _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().seconds(),
338 VideoContent::video_size_after_3d_split () const
340 dcp::Size const s = video_size ();
341 switch (video_frame_type ()) {
342 case VIDEO_FRAME_TYPE_2D:
343 case VIDEO_FRAME_TYPE_3D_ALTERNATE:
344 case VIDEO_FRAME_TYPE_3D_LEFT:
345 case VIDEO_FRAME_TYPE_3D_RIGHT:
347 case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
348 return dcp::Size (s.width / 2, s.height);
349 case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
350 return dcp::Size (s.width, s.height / 2);
357 VideoContent::set_colour_conversion (ColourConversion c)
360 boost::mutex::scoped_lock lm (_mutex);
361 _colour_conversion = c;
364 signal_changed (VideoContentProperty::COLOUR_CONVERSION);
367 /** @return Video size after 3D split and crop */
369 VideoContent::video_size_after_crop () const
371 return crop().apply (video_size_after_3d_split ());
374 /** @param t A time offset from the start of this piece of content.
375 * @return Corresponding time with respect to the content.
378 VideoContent::dcp_time_to_content_time (DCPTime t) const
380 shared_ptr<const Film> film = _film.lock ();
382 return ContentTime (t, FrameRateChange (video_frame_rate(), film->video_frame_rate()));
386 VideoContent::scale_and_crop_to_fit_width ()
388 shared_ptr<const Film> film = _film.lock ();
391 set_scale (VideoContentScale (film->container ()));
393 int const crop = max (0, int (video_size().height - double (film->frame_size().height) * video_size().width / film->frame_size().width));
394 set_top_crop (crop / 2);
395 set_bottom_crop (crop / 2);
399 VideoContent::scale_and_crop_to_fit_height ()
401 shared_ptr<const Film> film = _film.lock ();
404 set_scale (VideoContentScale (film->container ()));
406 int const crop = max (0, int (video_size().width - double (film->frame_size().width) * video_size().height / film->frame_size().height));
407 set_left_crop (crop / 2);
408 set_right_crop (crop / 2);
412 VideoContent::set_video_frame_rate (float r)
415 boost::mutex::scoped_lock lm (_mutex);
416 if (_video_frame_rate == r) {
420 _video_frame_rate = r;
423 signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
426 VideoContentScale::VideoContentScale (Ratio const * r)
433 VideoContentScale::VideoContentScale ()
440 VideoContentScale::VideoContentScale (bool scale)
447 VideoContentScale::VideoContentScale (cxml::NodePtr node)
451 optional<string> r = node->optional_string_child ("Ratio");
453 _ratio = Ratio::from_id (r.get ());
455 _scale = node->bool_child ("Scale");
460 VideoContentScale::as_xml (xmlpp::Node* node) const
463 node->add_child("Ratio")->add_child_text (_ratio->id ());
465 node->add_child("Scale")->add_child_text (_scale ? "1" : "0");
470 VideoContentScale::id () const
475 s << _ratio->id () << "_";
477 s << (_scale ? "S1" : "S0");
484 VideoContentScale::name () const
487 return _ratio->nickname ();
491 return _("No stretch");
494 return _("No scale");
497 /** @param display_container Size of the container that we are displaying this content in.
498 * @param film_container The size of the film's image.
501 VideoContentScale::size (shared_ptr<const VideoContent> c, dcp::Size display_container, dcp::Size film_container) const
504 return fit_ratio_within (_ratio->ratio (), display_container);
507 dcp::Size const ac = c->video_size_after_crop ();
509 /* Force scale if the film_container is smaller than the content's image */
510 if (_scale || film_container.width < ac.width || film_container.height < ac.height) {
511 return fit_ratio_within (ac.ratio (), display_container);
514 /* Scale the image so that it will be in the right place in film_container, even if display_container is a
518 c->video_size().width * float(display_container.width) / film_container.width,
519 c->video_size().height * float(display_container.height) / film_container.height
524 VideoContentScale::setup_scales ()
526 vector<Ratio const *> ratios = Ratio::all ();
527 for (vector<Ratio const *>::const_iterator i = ratios.begin(); i != ratios.end(); ++i) {
528 _scales.push_back (VideoContentScale (*i));
531 _scales.push_back (VideoContentScale (true));
532 _scales.push_back (VideoContentScale (false));
536 operator== (VideoContentScale const & a, VideoContentScale const & b)
538 return (a.ratio() == b.ratio() && a.scale() == b.scale());
542 operator!= (VideoContentScale const & a, VideoContentScale const & b)
544 return (a.ratio() != b.ratio() || a.scale() != b.scale());