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")))
76 set_default_colour_conversion ();
79 VideoContent::VideoContent (shared_ptr<const Film> film, DCPTime s, Frame len)
82 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
83 , _scale (VideoContentScale (Ratio::from_id ("178")))
88 set_default_colour_conversion ();
91 VideoContent::VideoContent (shared_ptr<const Film> film, boost::filesystem::path p)
94 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
95 , _scale (VideoContentScale (Ratio::from_id ("178")))
100 set_default_colour_conversion ();
103 VideoContent::VideoContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
104 : Content (film, node)
106 _video_size.width = node->number_child<int> ("VideoWidth");
107 _video_size.height = node->number_child<int> ("VideoHeight");
108 _video_frame_rate = node->optional_number_child<double> ("VideoFrameRate");
109 _video_length = node->number_child<Frame> ("VideoLength");
110 _video_frame_type = static_cast<VideoFrameType> (node->number_child<int> ("VideoFrameType"));
111 _sample_aspect_ratio = node->optional_number_child<double> ("SampleAspectRatio");
112 _crop.left = node->number_child<int> ("LeftCrop");
113 _crop.right = node->number_child<int> ("RightCrop");
114 _crop.top = node->number_child<int> ("TopCrop");
115 _crop.bottom = node->number_child<int> ("BottomCrop");
118 optional<string> r = node->optional_string_child ("Ratio");
120 _scale = VideoContentScale (Ratio::from_id (r.get ()));
123 _scale = VideoContentScale (node->node_child ("Scale"));
127 if (node->optional_node_child ("ColourConversion")) {
128 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"), version);
131 _yuv = node->optional_bool_child("YUV").get_value_or (true);
134 _fade_in = node->number_child<Frame> ("FadeIn");
135 _fade_out = node->number_child<Frame> ("FadeOut");
137 _fade_in = _fade_out = 0;
141 VideoContent::VideoContent (shared_ptr<const Film> film, vector<shared_ptr<Content> > c)
146 shared_ptr<VideoContent> ref = dynamic_pointer_cast<VideoContent> (c[0]);
147 DCPOMATIC_ASSERT (ref);
149 for (size_t i = 0; i < c.size(); ++i) {
150 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (c[i]);
152 if (vc->video_size() != ref->video_size()) {
153 throw JoinError (_("Content to be joined must have the same picture size."));
156 if (vc->video_frame_rate() != ref->video_frame_rate()) {
157 throw JoinError (_("Content to be joined must have the same video frame rate."));
160 if (vc->video_frame_type() != ref->video_frame_type()) {
161 throw JoinError (_("Content to be joined must have the same video frame type."));
164 if (vc->crop() != ref->crop()) {
165 throw JoinError (_("Content to be joined must have the same crop."));
168 if (vc->scale() != ref->scale()) {
169 throw JoinError (_("Content to be joined must have the same scale setting."));
172 if (vc->colour_conversion() != ref->colour_conversion()) {
173 throw JoinError (_("Content to be joined must have the same colour conversion."));
176 if (vc->fade_in() != ref->fade_in() || vc->fade_out() != ref->fade_out()) {
177 throw JoinError (_("Content to be joined must have the same fades."));
180 _video_length += vc->video_length ();
187 _video_size = ref->video_size ();
188 _video_frame_rate = ref->video_frame_rate ();
189 _video_frame_type = ref->video_frame_type ();
190 _crop = ref->crop ();
191 _scale = ref->scale ();
192 _colour_conversion = ref->colour_conversion ();
193 _fade_in = ref->fade_in ();
194 _fade_out = ref->fade_out ();
198 VideoContent::as_xml (xmlpp::Node* node) const
200 boost::mutex::scoped_lock lm (_mutex);
201 node->add_child("VideoLength")->add_child_text (raw_convert<string> (_video_length));
202 node->add_child("VideoWidth")->add_child_text (raw_convert<string> (_video_size.width));
203 node->add_child("VideoHeight")->add_child_text (raw_convert<string> (_video_size.height));
204 if (_video_frame_rate) {
205 node->add_child("VideoFrameRate")->add_child_text (raw_convert<string> (_video_frame_rate.get()));
207 node->add_child("VideoFrameType")->add_child_text (raw_convert<string> (static_cast<int> (_video_frame_type)));
208 if (_sample_aspect_ratio) {
209 node->add_child("SampleAspectRatio")->add_child_text (raw_convert<string> (_sample_aspect_ratio.get ()));
212 _scale.as_xml (node->add_child("Scale"));
213 if (_colour_conversion) {
214 _colour_conversion.get().as_xml (node->add_child("ColourConversion"));
216 node->add_child("YUV")->add_child_text (_yuv ? "1" : "0");
217 node->add_child("FadeIn")->add_child_text (raw_convert<string> (_fade_in));
218 node->add_child("FadeOut")->add_child_text (raw_convert<string> (_fade_out));
222 VideoContent::set_default_colour_conversion ()
224 /* If there's no better offer we'll use Rec. 709 */
225 boost::mutex::scoped_lock lm (_mutex);
226 _colour_conversion = PresetColourConversion::from_id ("rec709").conversion;
230 VideoContent::take_from_video_examiner (shared_ptr<VideoExaminer> d)
232 /* These examiner calls could call other content methods which take a lock on the mutex */
233 dcp::Size const vs = d->video_size ();
234 optional<double> const vfr = d->video_frame_rate ();
235 Frame vl = d->video_length ();
236 optional<double> const ar = d->sample_aspect_ratio ();
237 bool const yuv = d->yuv ();
240 boost::mutex::scoped_lock lm (_mutex);
242 _video_frame_rate = vfr;
244 _sample_aspect_ratio = ar;
247 /* Guess correct scale from size and sample aspect ratio */
248 _scale = VideoContentScale (
249 Ratio::nearest_from_ratio (double (_video_size.width) * ar.get_value_or (1) / _video_size.height)
253 LOG_GENERAL ("Video length obtained from header as %1 frames", _video_length);
255 set_default_colour_conversion ();
257 signal_changed (VideoContentProperty::VIDEO_SIZE);
258 signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
259 signal_changed (VideoContentProperty::VIDEO_SCALE);
260 signal_changed (ContentProperty::LENGTH);
264 VideoContent::set_left_crop (int c)
267 boost::mutex::scoped_lock lm (_mutex);
269 if (_crop.left == c) {
276 signal_changed (VideoContentProperty::VIDEO_CROP);
280 VideoContent::set_right_crop (int c)
283 boost::mutex::scoped_lock lm (_mutex);
284 if (_crop.right == c) {
291 signal_changed (VideoContentProperty::VIDEO_CROP);
295 VideoContent::set_top_crop (int c)
298 boost::mutex::scoped_lock lm (_mutex);
299 if (_crop.top == c) {
306 signal_changed (VideoContentProperty::VIDEO_CROP);
310 VideoContent::set_bottom_crop (int c)
313 boost::mutex::scoped_lock lm (_mutex);
314 if (_crop.bottom == c) {
321 signal_changed (VideoContentProperty::VIDEO_CROP);
325 VideoContent::set_scale (VideoContentScale s)
328 boost::mutex::scoped_lock lm (_mutex);
336 signal_changed (VideoContentProperty::VIDEO_SCALE);
339 /** @return string which includes everything about how this content looks */
341 VideoContent::identifier () const
344 s << Content::identifier()
345 << "_" << crop().left
346 << "_" << crop().right
348 << "_" << crop().bottom
349 << "_" << scale().id()
353 if (colour_conversion()) {
354 s << "_" << colour_conversion().get().identifier ();
361 VideoContent::set_video_frame_type (VideoFrameType t)
364 boost::mutex::scoped_lock lm (_mutex);
365 _video_frame_type = t;
368 signal_changed (VideoContentProperty::VIDEO_FRAME_TYPE);
372 VideoContent::technical_summary () const
374 string s = String::compose (
375 N_("video: length %1 frames, size %2x%3, rate %4"),
376 video_length_after_3d_combine(),
382 if (sample_aspect_ratio ()) {
383 s += String::compose (N_(", sample aspect ratio %1"), (sample_aspect_ratio().get ()));
390 VideoContent::video_size_after_3d_split () const
392 dcp::Size const s = video_size ();
393 switch (video_frame_type ()) {
394 case VIDEO_FRAME_TYPE_2D:
395 case VIDEO_FRAME_TYPE_3D_ALTERNATE:
396 case VIDEO_FRAME_TYPE_3D_LEFT:
397 case VIDEO_FRAME_TYPE_3D_RIGHT:
399 case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
400 return dcp::Size (s.width / 2, s.height);
401 case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
402 return dcp::Size (s.width, s.height / 2);
405 DCPOMATIC_ASSERT (false);
409 VideoContent::unset_colour_conversion ()
412 boost::mutex::scoped_lock lm (_mutex);
413 _colour_conversion = boost::optional<ColourConversion> ();
416 signal_changed (VideoContentProperty::COLOUR_CONVERSION);
420 VideoContent::set_colour_conversion (ColourConversion c)
423 boost::mutex::scoped_lock lm (_mutex);
424 _colour_conversion = c;
427 signal_changed (VideoContentProperty::COLOUR_CONVERSION);
431 VideoContent::set_fade_in (Frame t)
434 boost::mutex::scoped_lock lm (_mutex);
438 signal_changed (VideoContentProperty::VIDEO_FADE_IN);
442 VideoContent::set_fade_out (Frame t)
445 boost::mutex::scoped_lock lm (_mutex);
449 signal_changed (VideoContentProperty::VIDEO_FADE_OUT);
452 /** @return Video size after 3D split and crop */
454 VideoContent::video_size_after_crop () const
456 return crop().apply (video_size_after_3d_split ());
460 VideoContent::scale_and_crop_to_fit_width ()
462 set_scale (VideoContentScale (film()->container ()));
464 int const crop = max (0, int (video_size().height - double (film()->frame_size().height) * video_size().width / film()->frame_size().width));
465 set_top_crop (crop / 2);
466 set_bottom_crop (crop / 2);
470 VideoContent::scale_and_crop_to_fit_height ()
472 set_scale (VideoContentScale (film()->container ()));
474 int const crop = max (0, int (video_size().width - double (film()->frame_size().width) * video_size().height / film()->frame_size().height));
475 set_left_crop (crop / 2);
476 set_right_crop (crop / 2);
480 VideoContent::set_video_frame_rate (double r)
483 boost::mutex::scoped_lock lm (_mutex);
484 if (_video_frame_rate == r) {
488 _video_frame_rate = r;
491 signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
495 VideoContent::fade (Frame f) const
497 DCPOMATIC_ASSERT (f >= 0);
500 return double (f) / fade_in();
503 Frame fade_out_start = video_length() - fade_out();
504 if (f >= fade_out_start) {
505 return 1 - double (f - fade_out_start) / fade_out();
508 return optional<double> ();
512 VideoContent::processing_description () const
514 /* stringstream is OK here as this string is just for presentation to the user */
517 if (video_size().width && video_size().height) {
518 d << String::compose (
519 _("Content video is %1x%2"),
520 video_size_after_3d_split().width,
521 video_size_after_3d_split().height
525 double ratio = video_size_after_3d_split().ratio ();
527 if (sample_aspect_ratio ()) {
528 d << ", " << _("pixel aspect ratio") << " " << fixed << setprecision(2) << sample_aspect_ratio().get () << ":1";
529 ratio *= sample_aspect_ratio().get ();
532 d << "\n" << _("Display aspect ratio") << " " << fixed << setprecision(2) << ratio << ":1\n";
535 if ((crop().left || crop().right || crop().top || crop().bottom) && video_size() != dcp::Size (0, 0)) {
536 dcp::Size cropped = video_size_after_crop ();
537 d << String::compose (
538 _("Cropped to %1x%2"),
539 cropped.width, cropped.height
542 d << " (" << fixed << setprecision(2) << cropped.ratio () << ":1)\n";
545 dcp::Size const container_size = film()->frame_size ();
546 dcp::Size const scaled = scale().size (dynamic_pointer_cast<const VideoContent> (shared_from_this ()), container_size, container_size);
548 if (scaled != video_size_after_crop ()) {
549 d << String::compose (
550 _("Scaled to %1x%2"),
551 scaled.width, scaled.height
554 d << " (" << fixed << setprecision(2) << scaled.ratio() << ":1)\n";
557 if (scaled != container_size) {
558 d << String::compose (
559 _("Padded with black to fit container %1 (%2x%3)"),
560 film()->container()->nickname (),
561 container_size.width, container_size.height
564 d << " (" << fixed << setprecision(2) << container_size.ratio () << ":1)\n";
567 d << _("Content frame rate");
568 d << " " << fixed << setprecision(4) << video_frame_rate() << "\n";
570 FrameRateChange frc (video_frame_rate(), film()->video_frame_rate ());
571 d << frc.description () << "\n";
577 VideoContent::add_properties (list<pair<string, string> >& p) const
579 p.push_back (make_pair (_("Video length"), raw_convert<string> (video_length ()) + " " + _("video frames")));
580 p.push_back (make_pair (_("Video size"), raw_convert<string> (video_size().width) + "x" + raw_convert<string> (video_size().height)));
581 p.push_back (make_pair (_("Video frame rate"), raw_convert<string> (video_frame_rate()) + " " + _("frames per second")));
585 VideoContent::reel_split_points () const
588 /* XXX: this is questionable; perhaps the position should be forced to be on a frame boundary */
589 t.push_back (position().round_up (film()->video_frame_rate()));
594 VideoContent::video_frame_rate () const
596 boost::mutex::scoped_lock lm (_mutex);
597 return _video_frame_rate.get_value_or (film()->video_frame_rate ());