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"
31 #include "exceptions.h"
35 int const VideoContentProperty::VIDEO_SIZE = 0;
36 int const VideoContentProperty::VIDEO_FRAME_RATE = 1;
37 int const VideoContentProperty::VIDEO_FRAME_TYPE = 2;
38 int const VideoContentProperty::VIDEO_CROP = 3;
39 int const VideoContentProperty::VIDEO_RATIO = 4;
40 int const VideoContentProperty::COLOUR_CONVERSION = 5;
43 using std::stringstream;
44 using std::setprecision;
47 using boost::shared_ptr;
48 using boost::lexical_cast;
49 using boost::optional;
50 using boost::dynamic_pointer_cast;
52 VideoContent::VideoContent (shared_ptr<const Film> f)
55 , _video_frame_rate (0)
56 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
57 , _ratio (Ratio::from_id ("185"))
59 setup_default_colour_conversion ();
62 VideoContent::VideoContent (shared_ptr<const Film> f, Time s, VideoContent::Frame len)
65 , _video_frame_rate (0)
66 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
67 , _ratio (Ratio::from_id ("185"))
69 setup_default_colour_conversion ();
72 VideoContent::VideoContent (shared_ptr<const Film> f, boost::filesystem::path p)
75 , _video_frame_rate (0)
76 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
77 , _ratio (Ratio::from_id ("185"))
79 setup_default_colour_conversion ();
82 VideoContent::VideoContent (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node)
86 _video_length = node->number_child<VideoContent::Frame> ("VideoLength");
87 _video_size.width = node->number_child<int> ("VideoWidth");
88 _video_size.height = node->number_child<int> ("VideoHeight");
89 _video_frame_rate = node->number_child<float> ("VideoFrameRate");
90 _video_frame_type = static_cast<VideoFrameType> (node->number_child<int> ("VideoFrameType"));
91 _crop.left = node->number_child<int> ("LeftCrop");
92 _crop.right = node->number_child<int> ("RightCrop");
93 _crop.top = node->number_child<int> ("TopCrop");
94 _crop.bottom = node->number_child<int> ("BottomCrop");
95 optional<string> r = node->optional_string_child ("Ratio");
97 _ratio = Ratio::from_id (r.get ());
99 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"));
102 VideoContent::VideoContent (shared_ptr<const Film> f, vector<shared_ptr<Content> > c)
106 shared_ptr<VideoContent> ref = dynamic_pointer_cast<VideoContent> (c[0]);
109 for (size_t i = 0; i < c.size(); ++i) {
110 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (c[i]);
112 if (vc->video_size() != ref->video_size()) {
113 throw JoinError (_("Content to be joined must have the same picture size."));
116 if (vc->video_frame_rate() != ref->video_frame_rate()) {
117 throw JoinError (_("Content to be joined must have the same video frame rate."));
120 if (vc->video_frame_type() != ref->video_frame_type()) {
121 throw JoinError (_("Content to be joined must have the same video frame type."));
124 if (vc->crop() != ref->crop()) {
125 throw JoinError (_("Content to be joined must have the same crop."));
128 if (vc->ratio() != ref->ratio()) {
129 throw JoinError (_("Content to be joined must have the same ratio."));
132 if (vc->colour_conversion() != ref->colour_conversion()) {
133 throw JoinError (_("Content to be joined must have the same colour conversion."));
136 _video_length += vc->video_length ();
139 _video_size = ref->video_size ();
140 _video_frame_rate = ref->video_frame_rate ();
141 _video_frame_type = ref->video_frame_type ();
142 _crop = ref->crop ();
143 _ratio = ref->ratio ();
144 _colour_conversion = ref->colour_conversion ();
148 VideoContent::as_xml (xmlpp::Node* node) const
150 boost::mutex::scoped_lock lm (_mutex);
151 node->add_child("VideoLength")->add_child_text (lexical_cast<string> (_video_length));
152 node->add_child("VideoWidth")->add_child_text (lexical_cast<string> (_video_size.width));
153 node->add_child("VideoHeight")->add_child_text (lexical_cast<string> (_video_size.height));
154 node->add_child("VideoFrameRate")->add_child_text (lexical_cast<string> (_video_frame_rate));
155 node->add_child("VideoFrameType")->add_child_text (lexical_cast<string> (static_cast<int> (_video_frame_type)));
156 node->add_child("LeftCrop")->add_child_text (boost::lexical_cast<string> (_crop.left));
157 node->add_child("RightCrop")->add_child_text (boost::lexical_cast<string> (_crop.right));
158 node->add_child("TopCrop")->add_child_text (boost::lexical_cast<string> (_crop.top));
159 node->add_child("BottomCrop")->add_child_text (boost::lexical_cast<string> (_crop.bottom));
161 node->add_child("Ratio")->add_child_text (_ratio->id ());
163 _colour_conversion.as_xml (node->add_child("ColourConversion"));
167 VideoContent::setup_default_colour_conversion ()
169 _colour_conversion = PresetColourConversion (_("sRGB"), 2.4, true, libdcp::colour_matrix::srgb_to_xyz, 2.6).conversion;
173 VideoContent::take_from_video_examiner (shared_ptr<VideoExaminer> d)
175 /* These examiner calls could call other content methods which take a lock on the mutex */
176 libdcp::Size const vs = d->video_size ();
177 float const vfr = d->video_frame_rate ();
180 boost::mutex::scoped_lock lm (_mutex);
182 _video_frame_rate = vfr;
185 signal_changed (VideoContentProperty::VIDEO_SIZE);
186 signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
191 VideoContent::information () const
193 if (video_size().width == 0 || video_size().height == 0) {
199 s << String::compose (
200 _("%1x%2 pixels (%3:1)"),
203 setprecision (3), video_size().ratio ()
210 VideoContent::set_left_crop (int c)
213 boost::mutex::scoped_lock lm (_mutex);
215 if (_crop.left == c) {
222 signal_changed (VideoContentProperty::VIDEO_CROP);
226 VideoContent::set_right_crop (int c)
229 boost::mutex::scoped_lock lm (_mutex);
230 if (_crop.right == c) {
237 signal_changed (VideoContentProperty::VIDEO_CROP);
241 VideoContent::set_top_crop (int c)
244 boost::mutex::scoped_lock lm (_mutex);
245 if (_crop.top == c) {
252 signal_changed (VideoContentProperty::VIDEO_CROP);
256 VideoContent::set_bottom_crop (int c)
259 boost::mutex::scoped_lock lm (_mutex);
260 if (_crop.bottom == c) {
267 signal_changed (VideoContentProperty::VIDEO_CROP);
271 VideoContent::set_ratio (Ratio const * r)
274 boost::mutex::scoped_lock lm (_mutex);
282 signal_changed (VideoContentProperty::VIDEO_RATIO);
285 /** @return string which includes everything about how this content looks */
287 VideoContent::identifier () const
290 s << Content::identifier()
291 << "_" << crop().left
292 << "_" << crop().right
294 << "_" << crop().bottom
295 << "_" << colour_conversion().identifier ();
298 s << "_" << ratio()->id ();
305 VideoContent::set_video_frame_type (VideoFrameType t)
308 boost::mutex::scoped_lock lm (_mutex);
309 _video_frame_type = t;
312 signal_changed (VideoContentProperty::VIDEO_FRAME_TYPE);
316 VideoContent::technical_summary () const
318 return String::compose ("video: length %1, size %2x%3, rate %4", video_length(), video_size().width, video_size().height, video_frame_rate());
322 VideoContent::video_size_after_3d_split () const
324 libdcp::Size const s = video_size ();
325 switch (video_frame_type ()) {
326 case VIDEO_FRAME_TYPE_2D:
328 case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
329 return libdcp::Size (s.width / 2, s.height);
336 VideoContent::set_colour_conversion (ColourConversion c)
339 boost::mutex::scoped_lock lm (_mutex);
340 _colour_conversion = c;
343 signal_changed (VideoContentProperty::COLOUR_CONVERSION);
346 /** @return Video size after 3D split and crop */
348 VideoContent::video_size_after_crop () const
350 return crop().apply (video_size_after_3d_split ());
353 /** @param t A time offset from the start of this piece of content.
354 * @return Corresponding frame index.
357 VideoContent::time_to_content_video_frames (Time t) const
359 shared_ptr<const Film> film = _film.lock ();
362 FrameRateConversion frc (video_frame_rate(), film->video_frame_rate());
364 /* Here we are converting from time (in the DCP) to a frame number in the content.
365 Hence we need to use the DCP's frame rate and the double/skip correction, not
368 return t * film->video_frame_rate() / (frc.factor() * TIME_HZ);