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 <dcp/raw_convert.h>
24 #include "video_content.h"
25 #include "video_examiner.h"
26 #include "compose.hpp"
29 #include "colour_conversion.h"
32 #include "exceptions.h"
33 #include "frame_rate_change.h"
38 #define LOG_GENERAL(...) film->log()->log (String::compose (__VA_ARGS__), Log::TYPE_GENERAL);
40 int const VideoContentProperty::VIDEO_SIZE = 0;
41 int const VideoContentProperty::VIDEO_FRAME_RATE = 1;
42 int const VideoContentProperty::VIDEO_FRAME_TYPE = 2;
43 int const VideoContentProperty::VIDEO_CROP = 3;
44 int const VideoContentProperty::VIDEO_SCALE = 4;
45 int const VideoContentProperty::COLOUR_CONVERSION = 5;
48 using std::stringstream;
49 using std::setprecision;
54 using boost::shared_ptr;
55 using boost::optional;
56 using boost::dynamic_pointer_cast;
57 using dcp::raw_convert;
59 vector<VideoContentScale> VideoContentScale::_scales;
61 VideoContent::VideoContent (shared_ptr<const Film> f)
64 , _video_frame_rate (0)
65 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
66 , _scale (Ratio::from_id ("185"))
68 setup_default_colour_conversion ();
71 VideoContent::VideoContent (shared_ptr<const Film> f, DCPTime s, ContentTime len)
74 , _video_frame_rate (0)
75 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
76 , _scale (Ratio::from_id ("185"))
78 setup_default_colour_conversion ();
81 VideoContent::VideoContent (shared_ptr<const Film> f, boost::filesystem::path p)
84 , _video_frame_rate (0)
85 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
86 , _scale (Ratio::from_id ("185"))
88 setup_default_colour_conversion ();
91 VideoContent::VideoContent (shared_ptr<const Film> f, cxml::ConstNodePtr node, int version)
94 _video_size.width = node->number_child<int> ("VideoWidth");
95 _video_size.height = node->number_child<int> ("VideoHeight");
96 _video_frame_rate = node->number_child<float> ("VideoFrameRate");
99 /* DCP-o-matic 1.0 branch */
100 _video_length = ContentTime::from_frames (node->number_child<int64_t> ("VideoLength"), _video_frame_rate);
102 _video_length = ContentTime (node->number_child<ContentTime::Type> ("VideoLength"));
105 _video_frame_type = static_cast<VideoFrameType> (node->number_child<int> ("VideoFrameType"));
106 _crop.left = node->number_child<int> ("LeftCrop");
107 _crop.right = node->number_child<int> ("RightCrop");
108 _crop.top = node->number_child<int> ("TopCrop");
109 _crop.bottom = node->number_child<int> ("BottomCrop");
112 optional<string> r = node->optional_string_child ("Ratio");
114 _scale = VideoContentScale (Ratio::from_id (r.get ()));
117 _scale = VideoContentScale (node->node_child ("Scale"));
120 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"));
123 VideoContent::VideoContent (shared_ptr<const Film> f, vector<shared_ptr<Content> > c)
127 shared_ptr<VideoContent> ref = dynamic_pointer_cast<VideoContent> (c[0]);
130 for (size_t i = 0; i < c.size(); ++i) {
131 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (c[i]);
133 if (vc->video_size() != ref->video_size()) {
134 throw JoinError (_("Content to be joined must have the same picture size."));
137 if (vc->video_frame_rate() != ref->video_frame_rate()) {
138 throw JoinError (_("Content to be joined must have the same video frame rate."));
141 if (vc->video_frame_type() != ref->video_frame_type()) {
142 throw JoinError (_("Content to be joined must have the same video frame type."));
145 if (vc->crop() != ref->crop()) {
146 throw JoinError (_("Content to be joined must have the same crop."));
149 if (vc->scale() != ref->scale()) {
150 throw JoinError (_("Content to be joined must have the same scale setting."));
153 if (vc->colour_conversion() != ref->colour_conversion()) {
154 throw JoinError (_("Content to be joined must have the same colour conversion."));
157 _video_length += vc->video_length ();
160 _video_size = ref->video_size ();
161 _video_frame_rate = ref->video_frame_rate ();
162 _video_frame_type = ref->video_frame_type ();
163 _crop = ref->crop ();
164 _scale = ref->scale ();
165 _colour_conversion = ref->colour_conversion ();
169 VideoContent::as_xml (xmlpp::Node* node) const
171 boost::mutex::scoped_lock lm (_mutex);
172 node->add_child("VideoLength")->add_child_text (raw_convert<string> (_video_length.get ()));
173 node->add_child("VideoWidth")->add_child_text (raw_convert<string> (_video_size.width));
174 node->add_child("VideoHeight")->add_child_text (raw_convert<string> (_video_size.height));
175 node->add_child("VideoFrameRate")->add_child_text (raw_convert<string> (_video_frame_rate));
176 node->add_child("VideoFrameType")->add_child_text (raw_convert<string> (static_cast<int> (_video_frame_type)));
178 _scale.as_xml (node->add_child("Scale"));
179 _colour_conversion.as_xml (node->add_child("ColourConversion"));
183 VideoContent::setup_default_colour_conversion ()
185 _colour_conversion = PresetColourConversion (_("sRGB"), 2.4, true, dcp::colour_matrix::srgb_to_xyz, 2.6).conversion;
189 VideoContent::take_from_video_examiner (shared_ptr<VideoExaminer> d)
191 /* These examiner calls could call other content methods which take a lock on the mutex */
192 dcp::Size const vs = d->video_size ();
193 float const vfr = d->video_frame_rate ();
194 ContentTime vl = d->video_length ();
197 boost::mutex::scoped_lock lm (_mutex);
199 _video_frame_rate = vfr;
203 shared_ptr<const Film> film = _film.lock ();
205 LOG_GENERAL ("Video length obtained from header as %1 frames", _video_length.frames (_video_frame_rate));
207 signal_changed (VideoContentProperty::VIDEO_SIZE);
208 signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
209 signal_changed (ContentProperty::LENGTH);
214 VideoContent::information () const
216 if (video_size().width == 0 || video_size().height == 0) {
222 s << String::compose (
223 _("%1x%2 pixels (%3:1)"),
226 setprecision (3), video_size().ratio ()
233 VideoContent::set_left_crop (int c)
236 boost::mutex::scoped_lock lm (_mutex);
238 if (_crop.left == c) {
245 signal_changed (VideoContentProperty::VIDEO_CROP);
249 VideoContent::set_right_crop (int c)
252 boost::mutex::scoped_lock lm (_mutex);
253 if (_crop.right == c) {
260 signal_changed (VideoContentProperty::VIDEO_CROP);
264 VideoContent::set_top_crop (int c)
267 boost::mutex::scoped_lock lm (_mutex);
268 if (_crop.top == c) {
275 signal_changed (VideoContentProperty::VIDEO_CROP);
279 VideoContent::set_bottom_crop (int c)
282 boost::mutex::scoped_lock lm (_mutex);
283 if (_crop.bottom == c) {
290 signal_changed (VideoContentProperty::VIDEO_CROP);
294 VideoContent::set_scale (VideoContentScale s)
297 boost::mutex::scoped_lock lm (_mutex);
305 signal_changed (VideoContentProperty::VIDEO_SCALE);
308 /** @return string which includes everything about how this content looks */
310 VideoContent::identifier () const
313 s << Content::identifier()
314 << "_" << crop().left
315 << "_" << crop().right
317 << "_" << crop().bottom
318 << "_" << scale().id()
319 << "_" << colour_conversion().identifier ();
325 VideoContent::set_video_frame_type (VideoFrameType t)
328 boost::mutex::scoped_lock lm (_mutex);
329 _video_frame_type = t;
332 signal_changed (VideoContentProperty::VIDEO_FRAME_TYPE);
336 VideoContent::technical_summary () const
338 return String::compose (
339 "video: length %1, size %2x%3, rate %4",
340 video_length_after_3d_combine().seconds(),
348 VideoContent::video_size_after_3d_split () const
350 dcp::Size const s = video_size ();
351 switch (video_frame_type ()) {
352 case VIDEO_FRAME_TYPE_2D:
353 case VIDEO_FRAME_TYPE_3D_ALTERNATE:
354 case VIDEO_FRAME_TYPE_3D_LEFT:
355 case VIDEO_FRAME_TYPE_3D_RIGHT:
357 case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
358 return dcp::Size (s.width / 2, s.height);
359 case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
360 return dcp::Size (s.width, s.height / 2);
367 VideoContent::set_colour_conversion (ColourConversion c)
370 boost::mutex::scoped_lock lm (_mutex);
371 _colour_conversion = c;
374 signal_changed (VideoContentProperty::COLOUR_CONVERSION);
377 /** @return Video size after 3D split and crop */
379 VideoContent::video_size_after_crop () const
381 return crop().apply (video_size_after_3d_split ());
384 /** @param t A time offset from the start of this piece of content.
385 * @return Corresponding time with respect to the content.
388 VideoContent::dcp_time_to_content_time (DCPTime t) const
390 shared_ptr<const Film> film = _film.lock ();
392 return ContentTime (t, FrameRateChange (video_frame_rate(), film->video_frame_rate()));
396 VideoContent::scale_and_crop_to_fit_width ()
398 shared_ptr<const Film> film = _film.lock ();
401 set_scale (VideoContentScale (film->container ()));
403 int const crop = max (0, int (video_size().height - double (film->frame_size().height) * video_size().width / film->frame_size().width));
404 set_top_crop (crop / 2);
405 set_bottom_crop (crop / 2);
409 VideoContent::scale_and_crop_to_fit_height ()
411 shared_ptr<const Film> film = _film.lock ();
414 set_scale (VideoContentScale (film->container ()));
416 int const crop = max (0, int (video_size().width - double (film->frame_size().width) * video_size().height / film->frame_size().height));
417 set_left_crop (crop / 2);
418 set_right_crop (crop / 2);
422 VideoContent::set_video_frame_rate (float r)
425 boost::mutex::scoped_lock lm (_mutex);
426 if (_video_frame_rate == r) {
430 _video_frame_rate = r;
433 signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
436 VideoContentScale::VideoContentScale (Ratio const * r)
443 VideoContentScale::VideoContentScale ()
450 VideoContentScale::VideoContentScale (bool scale)
457 VideoContentScale::VideoContentScale (cxml::NodePtr node)
461 optional<string> r = node->optional_string_child ("Ratio");
463 _ratio = Ratio::from_id (r.get ());
465 _scale = node->bool_child ("Scale");
470 VideoContentScale::as_xml (xmlpp::Node* node) const
473 node->add_child("Ratio")->add_child_text (_ratio->id ());
475 node->add_child("Scale")->add_child_text (_scale ? "1" : "0");
480 VideoContentScale::id () const
485 s << _ratio->id () << "_";
487 s << (_scale ? "S1" : "S0");
494 VideoContentScale::name () const
497 return _ratio->nickname ();
501 return _("No stretch");
504 return _("No scale");
507 /** @param display_container Size of the container that we are displaying this content in.
508 * @param film_container The size of the film's image.
511 VideoContentScale::size (shared_ptr<const VideoContent> c, dcp::Size display_container, dcp::Size film_container) const
514 return fit_ratio_within (_ratio->ratio (), display_container);
517 dcp::Size const ac = c->video_size_after_crop ();
519 /* Force scale if the film_container is smaller than the content's image */
520 if (_scale || film_container.width < ac.width || film_container.height < ac.height) {
521 return fit_ratio_within (ac.ratio (), display_container);
524 /* Scale the image so that it will be in the right place in film_container, even if display_container is a
528 c->video_size().width * float(display_container.width) / film_container.width,
529 c->video_size().height * float(display_container.height) / film_container.height
534 VideoContentScale::setup_scales ()
536 vector<Ratio const *> ratios = Ratio::all ();
537 for (vector<Ratio const *>::const_iterator i = ratios.begin(); i != ratios.end(); ++i) {
538 _scales.push_back (VideoContentScale (*i));
541 _scales.push_back (VideoContentScale (true));
542 _scales.push_back (VideoContentScale (false));
546 operator== (VideoContentScale const & a, VideoContentScale const & b)
548 return (a.ratio() == b.ratio() && a.scale() == b.scale());
552 operator!= (VideoContentScale const & a, VideoContentScale const & b)
554 return (a.ratio() != b.ratio() || a.scale() != b.scale());