2 Copyright (C) 2013-2015 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.
20 #include "video_content.h"
21 #include "video_examiner.h"
22 #include "compose.hpp"
25 #include "colour_conversion.h"
28 #include "exceptions.h"
29 #include "frame_rate_change.h"
31 #include "safe_stringstream.h"
32 #include "raw_convert.h"
33 #include <libcxml/cxml.h>
34 #include <dcp/colour_matrix.h>
35 #include <libxml++/libxml++.h>
41 #define LOG_GENERAL(...) film->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL);
43 int const VideoContentProperty::VIDEO_SIZE = 0;
44 int const VideoContentProperty::VIDEO_FRAME_RATE = 1;
45 int const VideoContentProperty::VIDEO_FRAME_TYPE = 2;
46 int const VideoContentProperty::VIDEO_CROP = 3;
47 int const VideoContentProperty::VIDEO_SCALE = 4;
48 int const VideoContentProperty::COLOUR_CONVERSION = 5;
49 int const VideoContentProperty::VIDEO_FADE_IN = 6;
50 int const VideoContentProperty::VIDEO_FADE_OUT = 7;
53 using std::setprecision;
58 using std::stringstream;
60 using std::setprecision;
63 using boost::shared_ptr;
64 using boost::optional;
65 using boost::dynamic_pointer_cast;
67 VideoContent::VideoContent (shared_ptr<const Film> film)
70 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
71 , _scale (VideoContentScale (Ratio::from_id ("178")))
75 set_default_colour_conversion ();
78 VideoContent::VideoContent (shared_ptr<const Film> film, DCPTime s, Frame len)
81 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
82 , _scale (VideoContentScale (Ratio::from_id ("178")))
86 set_default_colour_conversion ();
89 VideoContent::VideoContent (shared_ptr<const Film> film, boost::filesystem::path p)
92 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
93 , _scale (VideoContentScale (Ratio::from_id ("178")))
97 set_default_colour_conversion ();
100 VideoContent::VideoContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
101 : Content (film, node)
103 _video_size.width = node->number_child<int> ("VideoWidth");
104 _video_size.height = node->number_child<int> ("VideoHeight");
105 _video_frame_rate = node->optional_number_child<double> ("VideoFrameRate");
106 _video_length = node->number_child<Frame> ("VideoLength");
107 _video_frame_type = static_cast<VideoFrameType> (node->number_child<int> ("VideoFrameType"));
108 _sample_aspect_ratio = node->optional_number_child<double> ("SampleAspectRatio");
109 _crop.left = node->number_child<int> ("LeftCrop");
110 _crop.right = node->number_child<int> ("RightCrop");
111 _crop.top = node->number_child<int> ("TopCrop");
112 _crop.bottom = node->number_child<int> ("BottomCrop");
115 optional<string> r = node->optional_string_child ("Ratio");
117 _scale = VideoContentScale (Ratio::from_id (r.get ()));
120 _scale = VideoContentScale (node->node_child ("Scale"));
124 if (node->optional_node_child ("ColourConversion")) {
125 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"), version);
128 _fade_in = node->number_child<Frame> ("FadeIn");
129 _fade_out = node->number_child<Frame> ("FadeOut");
131 _fade_in = _fade_out = 0;
135 VideoContent::VideoContent (shared_ptr<const Film> film, vector<shared_ptr<Content> > c)
139 shared_ptr<VideoContent> ref = dynamic_pointer_cast<VideoContent> (c[0]);
140 DCPOMATIC_ASSERT (ref);
142 for (size_t i = 0; i < c.size(); ++i) {
143 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (c[i]);
145 if (vc->video_size() != ref->video_size()) {
146 throw JoinError (_("Content to be joined must have the same picture size."));
149 if (vc->video_frame_rate() != ref->video_frame_rate()) {
150 throw JoinError (_("Content to be joined must have the same video frame rate."));
153 if (vc->video_frame_type() != ref->video_frame_type()) {
154 throw JoinError (_("Content to be joined must have the same video frame type."));
157 if (vc->crop() != ref->crop()) {
158 throw JoinError (_("Content to be joined must have the same crop."));
161 if (vc->scale() != ref->scale()) {
162 throw JoinError (_("Content to be joined must have the same scale setting."));
165 if (vc->colour_conversion() != ref->colour_conversion()) {
166 throw JoinError (_("Content to be joined must have the same colour conversion."));
169 if (vc->fade_in() != ref->fade_in() || vc->fade_out() != ref->fade_out()) {
170 throw JoinError (_("Content to be joined must have the same fades."));
173 _video_length += vc->video_length ();
176 _video_size = ref->video_size ();
177 _video_frame_rate = ref->video_frame_rate ();
178 _video_frame_type = ref->video_frame_type ();
179 _crop = ref->crop ();
180 _scale = ref->scale ();
181 _colour_conversion = ref->colour_conversion ();
182 _fade_in = ref->fade_in ();
183 _fade_out = ref->fade_out ();
187 VideoContent::as_xml (xmlpp::Node* node) const
189 boost::mutex::scoped_lock lm (_mutex);
190 node->add_child("VideoLength")->add_child_text (raw_convert<string> (_video_length));
191 node->add_child("VideoWidth")->add_child_text (raw_convert<string> (_video_size.width));
192 node->add_child("VideoHeight")->add_child_text (raw_convert<string> (_video_size.height));
193 if (_video_frame_rate) {
194 node->add_child("VideoFrameRate")->add_child_text (raw_convert<string> (_video_frame_rate.get()));
196 node->add_child("VideoFrameType")->add_child_text (raw_convert<string> (static_cast<int> (_video_frame_type)));
197 if (_sample_aspect_ratio) {
198 node->add_child("SampleAspectRatio")->add_child_text (raw_convert<string> (_sample_aspect_ratio.get ()));
201 _scale.as_xml (node->add_child("Scale"));
202 if (_colour_conversion) {
203 _colour_conversion.get().as_xml (node->add_child("ColourConversion"));
205 node->add_child("FadeIn")->add_child_text (raw_convert<string> (_fade_in));
206 node->add_child("FadeOut")->add_child_text (raw_convert<string> (_fade_out));
210 VideoContent::set_default_colour_conversion ()
212 /* If there's no better offer we'll use Rec. 709 */
213 boost::mutex::scoped_lock lm (_mutex);
214 _colour_conversion = PresetColourConversion::from_id ("rec709").conversion;
218 VideoContent::take_from_video_examiner (shared_ptr<VideoExaminer> d)
220 /* These examiner calls could call other content methods which take a lock on the mutex */
221 dcp::Size const vs = d->video_size ();
222 optional<double> const vfr = d->video_frame_rate ();
223 Frame vl = d->video_length ();
224 optional<double> const ar = d->sample_aspect_ratio ();
227 boost::mutex::scoped_lock lm (_mutex);
229 _video_frame_rate = vfr;
231 _sample_aspect_ratio = ar;
233 /* Guess correct scale from size and sample aspect ratio */
234 _scale = VideoContentScale (
235 Ratio::nearest_from_ratio (double (_video_size.width) * ar.get_value_or (1) / _video_size.height)
239 shared_ptr<const Film> film = _film.lock ();
240 DCPOMATIC_ASSERT (film);
241 LOG_GENERAL ("Video length obtained from header as %1 frames", _video_length);
243 set_default_colour_conversion ();
245 signal_changed (VideoContentProperty::VIDEO_SIZE);
246 signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
247 signal_changed (VideoContentProperty::VIDEO_SCALE);
248 signal_changed (ContentProperty::LENGTH);
252 VideoContent::set_left_crop (int c)
255 boost::mutex::scoped_lock lm (_mutex);
257 if (_crop.left == c) {
264 signal_changed (VideoContentProperty::VIDEO_CROP);
268 VideoContent::set_right_crop (int c)
271 boost::mutex::scoped_lock lm (_mutex);
272 if (_crop.right == c) {
279 signal_changed (VideoContentProperty::VIDEO_CROP);
283 VideoContent::set_top_crop (int c)
286 boost::mutex::scoped_lock lm (_mutex);
287 if (_crop.top == c) {
294 signal_changed (VideoContentProperty::VIDEO_CROP);
298 VideoContent::set_bottom_crop (int c)
301 boost::mutex::scoped_lock lm (_mutex);
302 if (_crop.bottom == c) {
309 signal_changed (VideoContentProperty::VIDEO_CROP);
313 VideoContent::set_scale (VideoContentScale s)
316 boost::mutex::scoped_lock lm (_mutex);
324 signal_changed (VideoContentProperty::VIDEO_SCALE);
327 /** @return string which includes everything about how this content looks */
329 VideoContent::identifier () const
332 s << Content::identifier()
333 << "_" << crop().left
334 << "_" << crop().right
336 << "_" << crop().bottom
337 << "_" << scale().id()
341 if (colour_conversion()) {
342 s << "_" << colour_conversion().get().identifier ();
349 VideoContent::set_video_frame_type (VideoFrameType t)
352 boost::mutex::scoped_lock lm (_mutex);
353 _video_frame_type = t;
356 signal_changed (VideoContentProperty::VIDEO_FRAME_TYPE);
360 VideoContent::technical_summary () const
362 string s = String::compose (
363 N_("video: length %1 frames, size %2x%3, rate %4"),
364 video_length_after_3d_combine(),
370 if (sample_aspect_ratio ()) {
371 s += String::compose (N_(", sample aspect ratio %1"), (sample_aspect_ratio().get ()));
378 VideoContent::video_size_after_3d_split () const
380 dcp::Size const s = video_size ();
381 switch (video_frame_type ()) {
382 case VIDEO_FRAME_TYPE_2D:
383 case VIDEO_FRAME_TYPE_3D_ALTERNATE:
384 case VIDEO_FRAME_TYPE_3D_LEFT:
385 case VIDEO_FRAME_TYPE_3D_RIGHT:
387 case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
388 return dcp::Size (s.width / 2, s.height);
389 case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
390 return dcp::Size (s.width, s.height / 2);
393 DCPOMATIC_ASSERT (false);
397 VideoContent::unset_colour_conversion ()
400 boost::mutex::scoped_lock lm (_mutex);
401 _colour_conversion = boost::optional<ColourConversion> ();
404 signal_changed (VideoContentProperty::COLOUR_CONVERSION);
408 VideoContent::set_colour_conversion (ColourConversion c)
411 boost::mutex::scoped_lock lm (_mutex);
412 _colour_conversion = c;
415 signal_changed (VideoContentProperty::COLOUR_CONVERSION);
419 VideoContent::set_fade_in (Frame t)
422 boost::mutex::scoped_lock lm (_mutex);
426 signal_changed (VideoContentProperty::VIDEO_FADE_IN);
430 VideoContent::set_fade_out (Frame t)
433 boost::mutex::scoped_lock lm (_mutex);
437 signal_changed (VideoContentProperty::VIDEO_FADE_OUT);
440 /** @return Video size after 3D split and crop */
442 VideoContent::video_size_after_crop () const
444 return crop().apply (video_size_after_3d_split ());
448 VideoContent::scale_and_crop_to_fit_width ()
450 shared_ptr<const Film> film = _film.lock ();
451 DCPOMATIC_ASSERT (film);
453 set_scale (VideoContentScale (film->container ()));
455 int const crop = max (0, int (video_size().height - double (film->frame_size().height) * video_size().width / film->frame_size().width));
456 set_top_crop (crop / 2);
457 set_bottom_crop (crop / 2);
461 VideoContent::scale_and_crop_to_fit_height ()
463 shared_ptr<const Film> film = _film.lock ();
464 DCPOMATIC_ASSERT (film);
466 set_scale (VideoContentScale (film->container ()));
468 int const crop = max (0, int (video_size().width - double (film->frame_size().width) * video_size().height / film->frame_size().height));
469 set_left_crop (crop / 2);
470 set_right_crop (crop / 2);
474 VideoContent::set_video_frame_rate (double r)
477 boost::mutex::scoped_lock lm (_mutex);
478 if (_video_frame_rate == r) {
482 _video_frame_rate = r;
485 signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
489 VideoContent::fade (Frame f) const
491 DCPOMATIC_ASSERT (f >= 0);
494 return double (f) / fade_in();
497 Frame fade_out_start = video_length() - fade_out();
498 if (f >= fade_out_start) {
499 return 1 - double (f - fade_out_start) / fade_out();
502 return optional<double> ();
506 VideoContent::processing_description () const
508 /* stringstream is OK here as this string is just for presentation to the user */
511 if (video_size().width && video_size().height) {
512 d << String::compose (
513 _("Content video is %1x%2"),
514 video_size_after_3d_split().width,
515 video_size_after_3d_split().height
519 double ratio = video_size_after_3d_split().ratio ();
521 if (sample_aspect_ratio ()) {
522 d << ", " << _("pixel aspect ratio") << " " << fixed << setprecision(2) << sample_aspect_ratio().get () << ":1";
523 ratio *= sample_aspect_ratio().get ();
526 d << "\n" << _("Display aspect ratio") << " " << fixed << setprecision(2) << ratio << ":1\n";
529 if ((crop().left || crop().right || crop().top || crop().bottom) && video_size() != dcp::Size (0, 0)) {
530 dcp::Size cropped = video_size_after_crop ();
531 d << String::compose (
532 _("Cropped to %1x%2"),
533 cropped.width, cropped.height
536 d << " (" << fixed << setprecision(2) << cropped.ratio () << ":1)\n";
539 shared_ptr<const Film> film = _film.lock ();
540 DCPOMATIC_ASSERT (film);
542 dcp::Size const container_size = film->frame_size ();
543 dcp::Size const scaled = scale().size (dynamic_pointer_cast<const VideoContent> (shared_from_this ()), container_size, container_size);
545 if (scaled != video_size_after_crop ()) {
546 d << String::compose (
547 _("Scaled to %1x%2"),
548 scaled.width, scaled.height
551 d << " (" << fixed << setprecision(2) << scaled.ratio() << ":1)\n";
554 if (scaled != container_size) {
555 d << String::compose (
556 _("Padded with black to fit container %1 (%2x%3)"),
557 film->container()->nickname (),
558 container_size.width, container_size.height
561 d << " (" << fixed << setprecision(2) << container_size.ratio () << ":1)\n";
564 d << _("Content frame rate");
565 d << " " << fixed << setprecision(4) << video_frame_rate() << "\n";
567 FrameRateChange frc (video_frame_rate(), film->video_frame_rate ());
568 d << frc.description () << "\n";
574 VideoContent::add_properties (list<pair<string, string> >& p) const
576 p.push_back (make_pair (_("Video length"), raw_convert<string> (video_length ()) + " " + _("video frames")));
577 p.push_back (make_pair (_("Video size"), raw_convert<string> (video_size().width) + "x" + raw_convert<string> (video_size().height)));
578 p.push_back (make_pair (_("Video frame rate"), raw_convert<string> (video_frame_rate()) + " " + _("frames per second")));
582 VideoContent::reel_split_points () const
585 shared_ptr<const Film> film = _film.lock ();
586 DCPOMATIC_ASSERT (film);
587 /* XXX: this is questionable; perhaps the position should be forced to be on a frame boundary */
588 t.push_back (position().round_up (film->video_frame_rate()));
593 VideoContent::video_frame_rate () const
595 boost::shared_ptr<const Film> film = _film.lock ();
596 DCPOMATIC_ASSERT (film);
598 boost::mutex::scoped_lock lm (_mutex);
599 return _video_frame_rate.get_value_or (film->video_frame_rate ());