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;
49 using boost::shared_ptr;
50 using boost::optional;
51 using boost::dynamic_pointer_cast;
52 using libdcp::raw_convert;
54 vector<VideoContentScale> VideoContentScale::_scales;
56 VideoContent::VideoContent (shared_ptr<const Film> f)
59 , _video_frame_rate (0)
60 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
61 , _scale (Ratio::from_id ("185"))
63 setup_default_colour_conversion ();
66 VideoContent::VideoContent (shared_ptr<const Film> f, Time s, VideoContent::Frame len)
69 , _video_frame_rate (0)
70 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
71 , _scale (Ratio::from_id ("185"))
73 setup_default_colour_conversion ();
76 VideoContent::VideoContent (shared_ptr<const Film> f, boost::filesystem::path p)
79 , _video_frame_rate (0)
80 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
81 , _scale (Ratio::from_id ("185"))
83 setup_default_colour_conversion ();
86 VideoContent::VideoContent (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node, int version)
89 _video_length = node->number_child<VideoContent::Frame> ("VideoLength");
90 _video_size.width = node->number_child<int> ("VideoWidth");
91 _video_size.height = node->number_child<int> ("VideoHeight");
92 _video_frame_rate = node->number_child<float> ("VideoFrameRate");
93 _video_frame_type = static_cast<VideoFrameType> (node->number_child<int> ("VideoFrameType"));
94 _crop.left = node->number_child<int> ("LeftCrop");
95 _crop.right = node->number_child<int> ("RightCrop");
96 _crop.top = node->number_child<int> ("TopCrop");
97 _crop.bottom = node->number_child<int> ("BottomCrop");
100 optional<string> r = node->optional_string_child ("Ratio");
102 _scale = VideoContentScale (Ratio::from_id (r.get ()));
105 _scale = VideoContentScale (node->node_child ("Scale"));
108 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"));
111 VideoContent::VideoContent (shared_ptr<const Film> f, vector<shared_ptr<Content> > c)
115 shared_ptr<VideoContent> ref = dynamic_pointer_cast<VideoContent> (c[0]);
118 for (size_t i = 0; i < c.size(); ++i) {
119 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (c[i]);
121 if (vc->video_size() != ref->video_size()) {
122 throw JoinError (_("Content to be joined must have the same picture size."));
125 if (vc->video_frame_rate() != ref->video_frame_rate()) {
126 throw JoinError (_("Content to be joined must have the same video frame rate."));
129 if (vc->video_frame_type() != ref->video_frame_type()) {
130 throw JoinError (_("Content to be joined must have the same video frame type."));
133 if (vc->crop() != ref->crop()) {
134 throw JoinError (_("Content to be joined must have the same crop."));
137 if (vc->scale() != ref->scale()) {
138 throw JoinError (_("Content to be joined must have the same scale setting."));
141 if (vc->colour_conversion() != ref->colour_conversion()) {
142 throw JoinError (_("Content to be joined must have the same colour conversion."));
145 _video_length += vc->video_length ();
148 _video_size = ref->video_size ();
149 _video_frame_rate = ref->video_frame_rate ();
150 _video_frame_type = ref->video_frame_type ();
151 _crop = ref->crop ();
152 _scale = ref->scale ();
153 _colour_conversion = ref->colour_conversion ();
157 VideoContent::as_xml (xmlpp::Node* node) const
159 boost::mutex::scoped_lock lm (_mutex);
160 node->add_child("VideoLength")->add_child_text (raw_convert<string> (_video_length));
161 node->add_child("VideoWidth")->add_child_text (raw_convert<string> (_video_size.width));
162 node->add_child("VideoHeight")->add_child_text (raw_convert<string> (_video_size.height));
163 node->add_child("VideoFrameRate")->add_child_text (raw_convert<string> (_video_frame_rate));
164 node->add_child("VideoFrameType")->add_child_text (raw_convert<string> (static_cast<int> (_video_frame_type)));
166 _scale.as_xml (node->add_child("Scale"));
167 _colour_conversion.as_xml (node->add_child("ColourConversion"));
171 VideoContent::setup_default_colour_conversion ()
173 _colour_conversion = PresetColourConversion (_("sRGB"), 2.4, true, libdcp::colour_matrix::srgb_to_xyz, 2.6).conversion;
177 VideoContent::take_from_video_examiner (shared_ptr<VideoExaminer> d)
179 /* These examiner calls could call other content methods which take a lock on the mutex */
180 libdcp::Size const vs = d->video_size ();
181 float const vfr = d->video_frame_rate ();
184 boost::mutex::scoped_lock lm (_mutex);
186 _video_frame_rate = vfr;
189 signal_changed (VideoContentProperty::VIDEO_SIZE);
190 signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
195 VideoContent::information () const
197 if (video_size().width == 0 || video_size().height == 0) {
203 s << String::compose (
204 _("%1x%2 pixels (%3:1)"),
207 setprecision (3), video_size().ratio ()
214 VideoContent::set_left_crop (int c)
217 boost::mutex::scoped_lock lm (_mutex);
219 if (_crop.left == c) {
226 signal_changed (VideoContentProperty::VIDEO_CROP);
230 VideoContent::set_right_crop (int c)
233 boost::mutex::scoped_lock lm (_mutex);
234 if (_crop.right == c) {
241 signal_changed (VideoContentProperty::VIDEO_CROP);
245 VideoContent::set_top_crop (int c)
248 boost::mutex::scoped_lock lm (_mutex);
249 if (_crop.top == c) {
256 signal_changed (VideoContentProperty::VIDEO_CROP);
260 VideoContent::set_bottom_crop (int c)
263 boost::mutex::scoped_lock lm (_mutex);
264 if (_crop.bottom == c) {
271 signal_changed (VideoContentProperty::VIDEO_CROP);
275 VideoContent::set_scale (VideoContentScale s)
278 boost::mutex::scoped_lock lm (_mutex);
286 signal_changed (VideoContentProperty::VIDEO_SCALE);
289 /** @return string which includes everything about how this content looks */
291 VideoContent::identifier () const
294 s << Content::identifier()
295 << "_" << crop().left
296 << "_" << crop().right
298 << "_" << crop().bottom
299 << "_" << scale().id()
300 << "_" << colour_conversion().identifier ();
306 VideoContent::set_video_frame_type (VideoFrameType t)
309 boost::mutex::scoped_lock lm (_mutex);
310 _video_frame_type = t;
313 signal_changed (VideoContentProperty::VIDEO_FRAME_TYPE);
317 VideoContent::technical_summary () const
319 return String::compose (
320 "video: length %1, size %2x%3, rate %4",
321 video_length_after_3d_combine(), video_size().width, video_size().height, video_frame_rate()
326 VideoContent::video_size_after_3d_split () const
328 libdcp::Size const s = video_size ();
329 switch (video_frame_type ()) {
330 case VIDEO_FRAME_TYPE_2D:
331 case VIDEO_FRAME_TYPE_3D_ALTERNATE:
332 case VIDEO_FRAME_TYPE_3D_LEFT:
333 case VIDEO_FRAME_TYPE_3D_RIGHT:
335 case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
336 return libdcp::Size (s.width / 2, s.height);
337 case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
338 return libdcp::Size (s.width, s.height / 2);
345 VideoContent::set_colour_conversion (ColourConversion c)
348 boost::mutex::scoped_lock lm (_mutex);
349 _colour_conversion = c;
352 signal_changed (VideoContentProperty::COLOUR_CONVERSION);
355 /** @return Video size after 3D split and crop */
357 VideoContent::video_size_after_crop () const
359 return crop().apply (video_size_after_3d_split ());
362 /** @param t A time offset from the start of this piece of content.
363 * @return Corresponding frame index.
366 VideoContent::time_to_content_video_frames (Time t) const
368 shared_ptr<const Film> film = _film.lock ();
371 FrameRateChange frc (video_frame_rate(), film->video_frame_rate());
373 /* Here we are converting from time (in the DCP) to a frame number in the content.
374 Hence we need to use the DCP's frame rate and the double/skip correction, not
377 return t * film->video_frame_rate() / (frc.factor() * TIME_HZ);
380 VideoContentScale::VideoContentScale (Ratio const * r)
387 VideoContentScale::VideoContentScale ()
394 VideoContentScale::VideoContentScale (bool scale)
401 VideoContentScale::VideoContentScale (shared_ptr<cxml::Node> node)
405 optional<string> r = node->optional_string_child ("Ratio");
407 _ratio = Ratio::from_id (r.get ());
409 _scale = node->bool_child ("Scale");
414 VideoContentScale::as_xml (xmlpp::Node* node) const
417 node->add_child("Ratio")->add_child_text (_ratio->id ());
419 node->add_child("Scale")->add_child_text (_scale ? "1" : "0");
424 VideoContentScale::id () const
429 s << _ratio->id () << "_";
431 s << (_scale ? "S1" : "S0");
438 VideoContentScale::name () const
441 return _ratio->nickname ();
445 return _("No stretch");
448 return _("No scale");
451 /** @param display_container Size of the container that we are displaying this content in.
452 * @param film_container The size of the film's image.
455 VideoContentScale::size (shared_ptr<const VideoContent> c, libdcp::Size display_container, libdcp::Size film_container) const
458 return fit_ratio_within (_ratio->ratio (), display_container);
461 libdcp::Size const ac = c->video_size_after_crop ();
463 /* Force scale if the film_container is smaller than the content's image */
464 if (_scale || film_container.width < ac.width || film_container.height < ac.height) {
465 return fit_ratio_within (ac.ratio (), display_container);
468 /* Scale the image so that it will be in the right place in film_container, even if display_container is a
471 return libdcp::Size (
472 c->video_size().width * float(display_container.width) / film_container.width,
473 c->video_size().height * float(display_container.height) / film_container.height
478 VideoContentScale::setup_scales ()
480 vector<Ratio const *> ratios = Ratio::all ();
481 for (vector<Ratio const *>::const_iterator i = ratios.begin(); i != ratios.end(); ++i) {
482 _scales.push_back (VideoContentScale (*i));
485 _scales.push_back (VideoContentScale (true));
486 _scales.push_back (VideoContentScale (false));
490 operator== (VideoContentScale const & a, VideoContentScale const & b)
492 return (a.ratio() == b.ratio() && a.scale() == b.scale());
496 operator!= (VideoContentScale const & a, VideoContentScale const & b)
498 return (a.ratio() != b.ratio() || a.scale() != b.scale());