2 Copyright (C) 2013 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 "video_content.h"
24 #include "video_examiner.h"
26 #include "compose.hpp"
28 #include "colour_conversion.h"
34 int const VideoContentProperty::VIDEO_SIZE = 0;
35 int const VideoContentProperty::VIDEO_FRAME_RATE = 1;
36 int const VideoContentProperty::VIDEO_FRAME_TYPE = 2;
37 int const VideoContentProperty::VIDEO_CROP = 3;
38 int const VideoContentProperty::VIDEO_RATIO = 4;
39 int const VideoContentProperty::COLOUR_CONVERSION = 5;
42 using std::stringstream;
43 using std::setprecision;
45 using boost::shared_ptr;
46 using boost::lexical_cast;
47 using boost::optional;
49 VideoContent::VideoContent (shared_ptr<const Film> f)
52 , _video_frame_rate (0)
53 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
54 , _ratio (Ratio::from_id ("185"))
56 setup_default_colour_conversion ();
59 VideoContent::VideoContent (shared_ptr<const Film> f, Time s, VideoContent::Frame len)
62 , _video_frame_rate (0)
63 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
64 , _ratio (Ratio::from_id ("185"))
66 setup_default_colour_conversion ();
69 VideoContent::VideoContent (shared_ptr<const Film> f, boost::filesystem::path p)
72 , _video_frame_rate (0)
73 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
74 , _ratio (Ratio::from_id ("185"))
76 setup_default_colour_conversion ();
79 VideoContent::VideoContent (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node)
83 _video_length = node->number_child<VideoContent::Frame> ("VideoLength");
84 _video_size.width = node->number_child<int> ("VideoWidth");
85 _video_size.height = node->number_child<int> ("VideoHeight");
86 _video_frame_rate = node->number_child<float> ("VideoFrameRate");
87 _video_frame_type = static_cast<VideoFrameType> (node->number_child<int> ("VideoFrameType"));
88 _crop.left = node->number_child<int> ("LeftCrop");
89 _crop.right = node->number_child<int> ("RightCrop");
90 _crop.top = node->number_child<int> ("TopCrop");
91 _crop.bottom = node->number_child<int> ("BottomCrop");
92 optional<string> r = node->optional_string_child ("Ratio");
94 _ratio = Ratio::from_id (r.get ());
96 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"));
100 VideoContent::as_xml (xmlpp::Node* node) const
102 boost::mutex::scoped_lock lm (_mutex);
103 node->add_child("VideoLength")->add_child_text (lexical_cast<string> (_video_length));
104 node->add_child("VideoWidth")->add_child_text (lexical_cast<string> (_video_size.width));
105 node->add_child("VideoHeight")->add_child_text (lexical_cast<string> (_video_size.height));
106 node->add_child("VideoFrameRate")->add_child_text (lexical_cast<string> (_video_frame_rate));
107 node->add_child("VideoFrameType")->add_child_text (lexical_cast<string> (static_cast<int> (_video_frame_type)));
108 node->add_child("LeftCrop")->add_child_text (boost::lexical_cast<string> (_crop.left));
109 node->add_child("RightCrop")->add_child_text (boost::lexical_cast<string> (_crop.right));
110 node->add_child("TopCrop")->add_child_text (boost::lexical_cast<string> (_crop.top));
111 node->add_child("BottomCrop")->add_child_text (boost::lexical_cast<string> (_crop.bottom));
113 node->add_child("Ratio")->add_child_text (_ratio->id ());
115 _colour_conversion.as_xml (node->add_child("ColourConversion"));
119 VideoContent::setup_default_colour_conversion ()
121 _colour_conversion = PresetColourConversion (_("sRGB"), 2.4, true, libdcp::colour_matrix::srgb_to_xyz, 2.6).conversion;
125 VideoContent::take_from_video_examiner (shared_ptr<VideoExaminer> d)
127 /* These examiner calls could call other content methods which take a lock on the mutex */
128 libdcp::Size const vs = d->video_size ();
129 float const vfr = d->video_frame_rate ();
132 boost::mutex::scoped_lock lm (_mutex);
134 _video_frame_rate = vfr;
137 signal_changed (VideoContentProperty::VIDEO_SIZE);
138 signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
143 VideoContent::information () const
145 if (video_size().width == 0 || video_size().height == 0) {
151 s << String::compose (
152 _("%1x%2 pixels (%3:1)"),
155 setprecision (3), video_size().ratio ()
162 VideoContent::set_left_crop (int c)
165 boost::mutex::scoped_lock lm (_mutex);
167 if (_crop.left == c) {
174 signal_changed (VideoContentProperty::VIDEO_CROP);
178 VideoContent::set_right_crop (int c)
181 boost::mutex::scoped_lock lm (_mutex);
182 if (_crop.right == c) {
189 signal_changed (VideoContentProperty::VIDEO_CROP);
193 VideoContent::set_top_crop (int c)
196 boost::mutex::scoped_lock lm (_mutex);
197 if (_crop.top == c) {
204 signal_changed (VideoContentProperty::VIDEO_CROP);
208 VideoContent::set_bottom_crop (int c)
211 boost::mutex::scoped_lock lm (_mutex);
212 if (_crop.bottom == c) {
219 signal_changed (VideoContentProperty::VIDEO_CROP);
223 VideoContent::set_ratio (Ratio const * r)
226 boost::mutex::scoped_lock lm (_mutex);
234 signal_changed (VideoContentProperty::VIDEO_RATIO);
237 /** @return string which includes everything about how this content looks */
239 VideoContent::identifier () const
242 s << Content::identifier()
243 << "_" << crop().left
244 << "_" << crop().right
246 << "_" << crop().bottom
247 << "_" << colour_conversion().identifier ();
250 s << "_" << ratio()->id ();
257 VideoContent::set_video_frame_type (VideoFrameType t)
260 boost::mutex::scoped_lock lm (_mutex);
261 _video_frame_type = t;
264 signal_changed (VideoContentProperty::VIDEO_FRAME_TYPE);
268 VideoContent::technical_summary () const
270 return String::compose ("video: length %1, size %2x%3, rate %4", video_length(), video_size().width, video_size().height, video_frame_rate());
274 VideoContent::video_size_after_3d_split () const
276 libdcp::Size const s = video_size ();
277 switch (video_frame_type ()) {
278 case VIDEO_FRAME_TYPE_2D:
280 case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
281 return libdcp::Size (s.width / 2, s.height);
288 VideoContent::set_colour_conversion (ColourConversion c)
291 boost::mutex::scoped_lock lm (_mutex);
292 _colour_conversion = c;
295 signal_changed (VideoContentProperty::COLOUR_CONVERSION);
298 /** @return Video size after 3D split and crop */
300 VideoContent::video_size_after_crop () const
302 return crop().apply (video_size_after_3d_split ());
305 /** @param t A time offset from the start of this piece of content.
306 * @return Corresponding frame index.
309 VideoContent::time_to_content_video_frames (Time t) const
311 shared_ptr<const Film> film = _film.lock ();
314 FrameRateConversion frc (video_frame_rate(), film->video_frame_rate());
316 /* Here we are converting from time (in the DCP) to a frame number in the content.
317 Hence we need to use the DCP's frame rate and the double/skip correction, not
320 return t * film->video_frame_rate() / (frc.factor() * TIME_HZ);