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"
32 int const VideoContentProperty::VIDEO_SIZE = 0;
33 int const VideoContentProperty::VIDEO_FRAME_RATE = 1;
34 int const VideoContentProperty::VIDEO_FRAME_TYPE = 2;
35 int const VideoContentProperty::VIDEO_CROP = 3;
36 int const VideoContentProperty::VIDEO_RATIO = 4;
37 int const VideoContentProperty::COLOUR_CONVERSION = 5;
40 using std::stringstream;
41 using std::setprecision;
43 using boost::shared_ptr;
44 using boost::lexical_cast;
45 using boost::optional;
47 VideoContent::VideoContent (shared_ptr<const Film> f, Time s, VideoContent::Frame len)
50 , _video_frame_rate (0)
51 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
52 , _ratio (Ratio::from_id ("185"))
54 setup_default_colour_conversion ();
57 VideoContent::VideoContent (shared_ptr<const Film> f, boost::filesystem::path p)
60 , _video_frame_rate (0)
61 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
62 , _ratio (Ratio::from_id ("185"))
64 setup_default_colour_conversion ();
67 VideoContent::VideoContent (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node)
70 _video_length = node->number_child<VideoContent::Frame> ("VideoLength");
71 _video_size.width = node->number_child<int> ("VideoWidth");
72 _video_size.height = node->number_child<int> ("VideoHeight");
73 _video_frame_rate = node->number_child<float> ("VideoFrameRate");
74 _video_frame_type = static_cast<VideoFrameType> (node->number_child<int> ("VideoFrameType"));
75 _crop.left = node->number_child<int> ("LeftCrop");
76 _crop.right = node->number_child<int> ("RightCrop");
77 _crop.top = node->number_child<int> ("TopCrop");
78 _crop.bottom = node->number_child<int> ("BottomCrop");
79 optional<string> r = node->optional_string_child ("Ratio");
81 _ratio = Ratio::from_id (r.get ());
83 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"));
87 VideoContent::as_xml (xmlpp::Node* node) const
89 boost::mutex::scoped_lock lm (_mutex);
90 node->add_child("VideoLength")->add_child_text (lexical_cast<string> (_video_length));
91 node->add_child("VideoWidth")->add_child_text (lexical_cast<string> (_video_size.width));
92 node->add_child("VideoHeight")->add_child_text (lexical_cast<string> (_video_size.height));
93 node->add_child("VideoFrameRate")->add_child_text (lexical_cast<string> (_video_frame_rate));
94 node->add_child("VideoFrameType")->add_child_text (lexical_cast<string> (static_cast<int> (_video_frame_type)));
95 node->add_child("LeftCrop")->add_child_text (boost::lexical_cast<string> (_crop.left));
96 node->add_child("RightCrop")->add_child_text (boost::lexical_cast<string> (_crop.right));
97 node->add_child("TopCrop")->add_child_text (boost::lexical_cast<string> (_crop.top));
98 node->add_child("BottomCrop")->add_child_text (boost::lexical_cast<string> (_crop.bottom));
100 node->add_child("Ratio")->add_child_text (_ratio->id ());
102 _colour_conversion.as_xml (node->add_child("ColourConversion"));
106 VideoContent::setup_default_colour_conversion ()
108 _colour_conversion = PresetColourConversion (_("sRGB"), 2.4, true, libdcp::colour_matrix::srgb_to_xyz, 2.6).conversion;
112 VideoContent::take_from_video_examiner (shared_ptr<VideoExaminer> d)
114 /* These examiner calls could call other content methods which take a lock on the mutex */
115 libdcp::Size const vs = d->video_size ();
116 float const vfr = d->video_frame_rate ();
119 boost::mutex::scoped_lock lm (_mutex);
121 _video_frame_rate = vfr;
124 signal_changed (VideoContentProperty::VIDEO_SIZE);
125 signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
130 VideoContent::information () const
132 if (video_size().width == 0 || video_size().height == 0) {
138 s << String::compose (
139 _("%1x%2 pixels (%3:1)"),
142 setprecision (3), float (video_size().width) / video_size().height
149 VideoContent::set_left_crop (int c)
152 boost::mutex::scoped_lock lm (_mutex);
154 if (_crop.left == c) {
161 signal_changed (VideoContentProperty::VIDEO_CROP);
165 VideoContent::set_right_crop (int c)
168 boost::mutex::scoped_lock lm (_mutex);
169 if (_crop.right == c) {
176 signal_changed (VideoContentProperty::VIDEO_CROP);
180 VideoContent::set_top_crop (int c)
183 boost::mutex::scoped_lock lm (_mutex);
184 if (_crop.top == c) {
191 signal_changed (VideoContentProperty::VIDEO_CROP);
195 VideoContent::set_bottom_crop (int c)
198 boost::mutex::scoped_lock lm (_mutex);
199 if (_crop.bottom == c) {
206 signal_changed (VideoContentProperty::VIDEO_CROP);
210 VideoContent::set_ratio (Ratio const * r)
213 boost::mutex::scoped_lock lm (_mutex);
221 signal_changed (VideoContentProperty::VIDEO_RATIO);
224 /** @return string which includes everything about how this content looks */
226 VideoContent::identifier () const
229 s << Content::digest()
230 << "_" << crop().left
231 << "_" << crop().right
233 << "_" << crop().bottom
234 << "_" << colour_conversion().identifier ();
237 s << "_" << ratio()->id ();
244 VideoContent::set_video_frame_type (VideoFrameType t)
247 boost::mutex::scoped_lock lm (_mutex);
248 _video_frame_type = t;
251 signal_changed (VideoContentProperty::VIDEO_FRAME_TYPE);
255 VideoContent::technical_summary () const
257 return String::compose ("video: length %1, size %2x%3, rate %4", video_length(), video_size().width, video_size().height, video_frame_rate());
261 VideoContent::video_size_after_3d_split () const
263 libdcp::Size const s = video_size ();
264 switch (video_frame_type ()) {
265 case VIDEO_FRAME_TYPE_2D:
267 case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
268 return libdcp::Size (s.width / 2, s.height);
275 VideoContent::set_colour_conversion (ColourConversion c)
278 boost::mutex::scoped_lock lm (_mutex);
279 _colour_conversion = c;
282 signal_changed (VideoContentProperty::COLOUR_CONVERSION);