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)
105 shared_ptr<VideoContent> ref = dynamic_pointer_cast<VideoContent> (c[0]);
108 for (size_t i = 0; i < c.size(); ++i) {
109 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (c[i]);
111 if (vc->video_size() != ref->video_size()) {
112 throw JoinError (_("Content to be joined must have the same picture size."));
115 if (vc->video_frame_rate() != ref->video_frame_rate()) {
116 throw JoinError (_("Content to be joined must have the same video frame rate."));
119 if (vc->video_frame_type() != ref->video_frame_type()) {
120 throw JoinError (_("Content to be joined must have the same video frame type."));
123 if (vc->crop() != ref->crop()) {
124 throw JoinError (_("Content to be joined must have the same crop."));
127 if (vc->ratio() != ref->ratio()) {
128 throw JoinError (_("Content to be joined must have the same ratio."));
131 if (vc->colour_conversion() != ref->colour_conversion()) {
132 throw JoinError (_("Content to be joined must have the same colour conversion."));
136 _video_size = ref->video_size ();
137 _video_frame_rate = ref->video_frame_rate ();
138 _video_frame_type = ref->video_frame_type ();
139 _crop = ref->crop ();
140 _ratio = ref->ratio ();
141 _colour_conversion = ref->colour_conversion ();
145 VideoContent::as_xml (xmlpp::Node* node) const
147 boost::mutex::scoped_lock lm (_mutex);
148 node->add_child("VideoLength")->add_child_text (lexical_cast<string> (_video_length));
149 node->add_child("VideoWidth")->add_child_text (lexical_cast<string> (_video_size.width));
150 node->add_child("VideoHeight")->add_child_text (lexical_cast<string> (_video_size.height));
151 node->add_child("VideoFrameRate")->add_child_text (lexical_cast<string> (_video_frame_rate));
152 node->add_child("VideoFrameType")->add_child_text (lexical_cast<string> (static_cast<int> (_video_frame_type)));
153 node->add_child("LeftCrop")->add_child_text (boost::lexical_cast<string> (_crop.left));
154 node->add_child("RightCrop")->add_child_text (boost::lexical_cast<string> (_crop.right));
155 node->add_child("TopCrop")->add_child_text (boost::lexical_cast<string> (_crop.top));
156 node->add_child("BottomCrop")->add_child_text (boost::lexical_cast<string> (_crop.bottom));
158 node->add_child("Ratio")->add_child_text (_ratio->id ());
160 _colour_conversion.as_xml (node->add_child("ColourConversion"));
164 VideoContent::setup_default_colour_conversion ()
166 _colour_conversion = PresetColourConversion (_("sRGB"), 2.4, true, libdcp::colour_matrix::srgb_to_xyz, 2.6).conversion;
170 VideoContent::take_from_video_examiner (shared_ptr<VideoExaminer> d)
172 /* These examiner calls could call other content methods which take a lock on the mutex */
173 libdcp::Size const vs = d->video_size ();
174 float const vfr = d->video_frame_rate ();
177 boost::mutex::scoped_lock lm (_mutex);
179 _video_frame_rate = vfr;
182 signal_changed (VideoContentProperty::VIDEO_SIZE);
183 signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
188 VideoContent::information () const
190 if (video_size().width == 0 || video_size().height == 0) {
196 s << String::compose (
197 _("%1x%2 pixels (%3:1)"),
200 setprecision (3), video_size().ratio ()
207 VideoContent::set_left_crop (int c)
210 boost::mutex::scoped_lock lm (_mutex);
212 if (_crop.left == c) {
219 signal_changed (VideoContentProperty::VIDEO_CROP);
223 VideoContent::set_right_crop (int c)
226 boost::mutex::scoped_lock lm (_mutex);
227 if (_crop.right == c) {
234 signal_changed (VideoContentProperty::VIDEO_CROP);
238 VideoContent::set_top_crop (int c)
241 boost::mutex::scoped_lock lm (_mutex);
242 if (_crop.top == c) {
249 signal_changed (VideoContentProperty::VIDEO_CROP);
253 VideoContent::set_bottom_crop (int c)
256 boost::mutex::scoped_lock lm (_mutex);
257 if (_crop.bottom == c) {
264 signal_changed (VideoContentProperty::VIDEO_CROP);
268 VideoContent::set_ratio (Ratio const * r)
271 boost::mutex::scoped_lock lm (_mutex);
279 signal_changed (VideoContentProperty::VIDEO_RATIO);
282 /** @return string which includes everything about how this content looks */
284 VideoContent::identifier () const
287 s << Content::identifier()
288 << "_" << crop().left
289 << "_" << crop().right
291 << "_" << crop().bottom
292 << "_" << colour_conversion().identifier ();
295 s << "_" << ratio()->id ();
302 VideoContent::set_video_frame_type (VideoFrameType t)
305 boost::mutex::scoped_lock lm (_mutex);
306 _video_frame_type = t;
309 signal_changed (VideoContentProperty::VIDEO_FRAME_TYPE);
313 VideoContent::technical_summary () const
315 return String::compose ("video: length %1, size %2x%3, rate %4", video_length(), video_size().width, video_size().height, video_frame_rate());
319 VideoContent::video_size_after_3d_split () const
321 libdcp::Size const s = video_size ();
322 switch (video_frame_type ()) {
323 case VIDEO_FRAME_TYPE_2D:
325 case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
326 return libdcp::Size (s.width / 2, s.height);
333 VideoContent::set_colour_conversion (ColourConversion c)
336 boost::mutex::scoped_lock lm (_mutex);
337 _colour_conversion = c;
340 signal_changed (VideoContentProperty::COLOUR_CONVERSION);
343 /** @return Video size after 3D split and crop */
345 VideoContent::video_size_after_crop () const
347 return crop().apply (video_size_after_3d_split ());
350 /** @param t A time offset from the start of this piece of content.
351 * @return Corresponding frame index.
354 VideoContent::time_to_content_video_frames (Time t) const
356 shared_ptr<const Film> film = _film.lock ();
359 FrameRateConversion frc (video_frame_rate(), film->video_frame_rate());
361 /* Here we are converting from time (in the DCP) to a frame number in the content.
362 Hence we need to use the DCP's frame rate and the double/skip correction, not
365 return t * film->video_frame_rate() / (frc.factor() * TIME_HZ);