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 <dcp/colour_matrix.h>
23 #include "video_content.h"
24 #include "video_examiner.h"
25 #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_SCALE = 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 vector<VideoContentScale> VideoContentScale::_scales;
54 VideoContent::VideoContent (shared_ptr<const Film> f)
57 , _video_frame_rate (0)
58 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
59 , _scale (Ratio::from_id ("185"))
61 setup_default_colour_conversion ();
64 VideoContent::VideoContent (shared_ptr<const Film> f, DCPTime s, ContentTime len)
67 , _video_frame_rate (0)
68 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
69 , _scale (Ratio::from_id ("185"))
71 setup_default_colour_conversion ();
74 VideoContent::VideoContent (shared_ptr<const Film> f, boost::filesystem::path p)
77 , _video_frame_rate (0)
78 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
79 , _scale (Ratio::from_id ("185"))
81 setup_default_colour_conversion ();
84 VideoContent::VideoContent (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node, int version)
87 _video_length = ContentTime (node->number_child<int64_t> ("VideoLength"));
88 _video_size.width = node->number_child<int> ("VideoWidth");
89 _video_size.height = node->number_child<int> ("VideoHeight");
90 _video_frame_rate = node->number_child<float> ("VideoFrameRate");
91 _video_frame_type = static_cast<VideoFrameType> (node->number_child<int> ("VideoFrameType"));
92 _crop.left = node->number_child<int> ("LeftCrop");
93 _crop.right = node->number_child<int> ("RightCrop");
94 _crop.top = node->number_child<int> ("TopCrop");
95 _crop.bottom = node->number_child<int> ("BottomCrop");
98 optional<string> r = node->optional_string_child ("Ratio");
100 _scale = VideoContentScale (Ratio::from_id (r.get ()));
103 _scale = VideoContentScale (node->node_child ("Scale"));
106 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"));
109 VideoContent::VideoContent (shared_ptr<const Film> f, vector<shared_ptr<Content> > c)
113 shared_ptr<VideoContent> ref = dynamic_pointer_cast<VideoContent> (c[0]);
116 for (size_t i = 0; i < c.size(); ++i) {
117 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (c[i]);
119 if (vc->video_size() != ref->video_size()) {
120 throw JoinError (_("Content to be joined must have the same picture size."));
123 if (vc->video_frame_rate() != ref->video_frame_rate()) {
124 throw JoinError (_("Content to be joined must have the same video frame rate."));
127 if (vc->video_frame_type() != ref->video_frame_type()) {
128 throw JoinError (_("Content to be joined must have the same video frame type."));
131 if (vc->crop() != ref->crop()) {
132 throw JoinError (_("Content to be joined must have the same crop."));
135 if (vc->scale() != ref->scale()) {
136 throw JoinError (_("Content to be joined must have the same scale setting."));
139 if (vc->colour_conversion() != ref->colour_conversion()) {
140 throw JoinError (_("Content to be joined must have the same colour conversion."));
143 _video_length += vc->video_length ();
146 _video_size = ref->video_size ();
147 _video_frame_rate = ref->video_frame_rate ();
148 _video_frame_type = ref->video_frame_type ();
149 _crop = ref->crop ();
150 _scale = ref->scale ();
151 _colour_conversion = ref->colour_conversion ();
155 VideoContent::as_xml (xmlpp::Node* node) const
157 boost::mutex::scoped_lock lm (_mutex);
158 node->add_child("VideoLength")->add_child_text (lexical_cast<string> (_video_length.get ()));
159 node->add_child("VideoWidth")->add_child_text (lexical_cast<string> (_video_size.width));
160 node->add_child("VideoHeight")->add_child_text (lexical_cast<string> (_video_size.height));
161 node->add_child("VideoFrameRate")->add_child_text (lexical_cast<string> (_video_frame_rate));
162 node->add_child("VideoFrameType")->add_child_text (lexical_cast<string> (static_cast<int> (_video_frame_type)));
163 node->add_child("LeftCrop")->add_child_text (boost::lexical_cast<string> (_crop.left));
164 node->add_child("RightCrop")->add_child_text (boost::lexical_cast<string> (_crop.right));
165 node->add_child("TopCrop")->add_child_text (boost::lexical_cast<string> (_crop.top));
166 node->add_child("BottomCrop")->add_child_text (boost::lexical_cast<string> (_crop.bottom));
167 _scale.as_xml (node->add_child("Scale"));
168 _colour_conversion.as_xml (node->add_child("ColourConversion"));
172 VideoContent::setup_default_colour_conversion ()
174 _colour_conversion = PresetColourConversion (_("sRGB"), 2.4, true, dcp::colour_matrix::srgb_to_xyz, 2.6).conversion;
178 VideoContent::take_from_video_examiner (shared_ptr<VideoExaminer> d)
180 /* These examiner calls could call other content methods which take a lock on the mutex */
181 dcp::Size const vs = d->video_size ();
182 float const vfr = d->video_frame_rate ();
183 cout << "taking " << vfr << "\n";
186 boost::mutex::scoped_lock lm (_mutex);
188 _video_frame_rate = vfr;
189 cout << "and then " << _video_frame_rate << "\n";
192 signal_changed (VideoContentProperty::VIDEO_SIZE);
193 signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
198 VideoContent::information () const
200 if (video_size().width == 0 || video_size().height == 0) {
206 s << String::compose (
207 _("%1x%2 pixels (%3:1)"),
210 setprecision (3), video_size().ratio ()
217 VideoContent::set_left_crop (int c)
220 boost::mutex::scoped_lock lm (_mutex);
222 if (_crop.left == c) {
229 signal_changed (VideoContentProperty::VIDEO_CROP);
233 VideoContent::set_right_crop (int c)
236 boost::mutex::scoped_lock lm (_mutex);
237 if (_crop.right == c) {
244 signal_changed (VideoContentProperty::VIDEO_CROP);
248 VideoContent::set_top_crop (int c)
251 boost::mutex::scoped_lock lm (_mutex);
252 if (_crop.top == c) {
259 signal_changed (VideoContentProperty::VIDEO_CROP);
263 VideoContent::set_bottom_crop (int c)
266 boost::mutex::scoped_lock lm (_mutex);
267 if (_crop.bottom == c) {
274 signal_changed (VideoContentProperty::VIDEO_CROP);
278 VideoContent::set_scale (VideoContentScale s)
281 boost::mutex::scoped_lock lm (_mutex);
289 signal_changed (VideoContentProperty::VIDEO_SCALE);
292 /** @return string which includes everything about how this content looks */
294 VideoContent::identifier () const
297 s << Content::identifier()
298 << "_" << crop().left
299 << "_" << crop().right
301 << "_" << crop().bottom
302 << "_" << scale().id()
303 << "_" << colour_conversion().identifier ();
309 VideoContent::set_video_frame_type (VideoFrameType t)
312 boost::mutex::scoped_lock lm (_mutex);
313 _video_frame_type = t;
316 signal_changed (VideoContentProperty::VIDEO_FRAME_TYPE);
320 VideoContent::technical_summary () const
322 return String::compose (
323 "video: length %1, size %2x%3, rate %4",
324 video_length().seconds(),
332 VideoContent::video_size_after_3d_split () const
334 dcp::Size const s = video_size ();
335 switch (video_frame_type ()) {
336 case VIDEO_FRAME_TYPE_2D:
338 case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
339 return dcp::Size (s.width / 2, s.height);
340 case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
341 return dcp::Size (s.width, s.height / 2);
348 VideoContent::set_colour_conversion (ColourConversion c)
351 boost::mutex::scoped_lock lm (_mutex);
352 _colour_conversion = c;
355 signal_changed (VideoContentProperty::COLOUR_CONVERSION);
358 /** @return Video size after 3D split and crop */
360 VideoContent::video_size_after_crop () const
362 return crop().apply (video_size_after_3d_split ());
365 /** @param t A time offset from the start of this piece of content.
366 * @return Corresponding time with respect to the content.
369 VideoContent::dcp_time_to_content_time (DCPTime t) const
371 shared_ptr<const Film> film = _film.lock ();
373 return ContentTime (t, FrameRateChange (video_frame_rate(), film->video_frame_rate()));
376 VideoContentScale::VideoContentScale (Ratio const * r)
383 VideoContentScale::VideoContentScale ()
390 VideoContentScale::VideoContentScale (bool scale)
397 VideoContentScale::VideoContentScale (shared_ptr<cxml::Node> node)
401 optional<string> r = node->optional_string_child ("Ratio");
403 _ratio = Ratio::from_id (r.get ());
405 _scale = node->bool_child ("Scale");
410 VideoContentScale::as_xml (xmlpp::Node* node) const
413 node->add_child("Ratio")->add_child_text (_ratio->id ());
415 node->add_child("Scale")->add_child_text (_scale ? "1" : "0");
420 VideoContentScale::id () const
425 s << _ratio->id () << "_";
427 s << (_scale ? "S1" : "S0");
434 VideoContentScale::name () const
437 return _ratio->nickname ();
441 return _("No stretch");
444 return _("No scale");
448 VideoContentScale::size (shared_ptr<const VideoContent> c, dcp::Size container) const
451 return fit_ratio_within (_ratio->ratio (), container);
454 /* Force scale if the container is smaller than the content's image */
455 if (_scale || container.width < c->video_size().width || container.height < c->video_size().height) {
456 return fit_ratio_within (c->video_size().ratio (), container);
459 return c->video_size ();
463 VideoContentScale::setup_scales ()
465 vector<Ratio const *> ratios = Ratio::all ();
466 for (vector<Ratio const *>::const_iterator i = ratios.begin(); i != ratios.end(); ++i) {
467 _scales.push_back (VideoContentScale (*i));
470 _scales.push_back (VideoContentScale (true));
471 _scales.push_back (VideoContentScale (false));
475 operator== (VideoContentScale const & a, VideoContentScale const & b)
477 return (a.ratio() == b.ratio() && a.scale() == b.scale());
481 operator!= (VideoContentScale const & a, VideoContentScale const & b)
483 return (a.ratio() != b.ratio() || a.scale() != b.scale());