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;
59 using std::setprecision;
62 using boost::shared_ptr;
63 using boost::optional;
64 using boost::dynamic_pointer_cast;
66 VideoContent::VideoContent (shared_ptr<const Film> film)
69 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
70 , _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")))
87 set_default_colour_conversion ();
90 VideoContent::VideoContent (shared_ptr<const Film> film, boost::filesystem::path p)
93 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
94 , _scale (VideoContentScale (Ratio::from_id ("178")))
99 set_default_colour_conversion ();
102 VideoContent::VideoContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
103 : Content (film, node)
105 _video_size.width = node->number_child<int> ("VideoWidth");
106 _video_size.height = node->number_child<int> ("VideoHeight");
107 _video_frame_rate = node->optional_number_child<double> ("VideoFrameRate");
108 _video_length = node->number_child<Frame> ("VideoLength");
109 _video_frame_type = static_cast<VideoFrameType> (node->number_child<int> ("VideoFrameType"));
110 _sample_aspect_ratio = node->optional_number_child<double> ("SampleAspectRatio");
111 _crop.left = node->number_child<int> ("LeftCrop");
112 _crop.right = node->number_child<int> ("RightCrop");
113 _crop.top = node->number_child<int> ("TopCrop");
114 _crop.bottom = node->number_child<int> ("BottomCrop");
117 optional<string> r = node->optional_string_child ("Ratio");
119 _scale = VideoContentScale (Ratio::from_id (r.get ()));
122 _scale = VideoContentScale (node->node_child ("Scale"));
126 if (node->optional_node_child ("ColourConversion")) {
127 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"), version);
130 _yuv = node->optional_bool_child("YUV").get_value_or (true);
133 _fade_in = node->number_child<Frame> ("FadeIn");
134 _fade_out = node->number_child<Frame> ("FadeOut");
136 _fade_in = _fade_out = 0;
140 VideoContent::VideoContent (shared_ptr<const Film> film, vector<shared_ptr<Content> > c)
145 shared_ptr<VideoContent> ref = dynamic_pointer_cast<VideoContent> (c[0]);
146 DCPOMATIC_ASSERT (ref);
148 for (size_t i = 0; i < c.size(); ++i) {
149 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (c[i]);
151 if (vc->video_size() != ref->video_size()) {
152 throw JoinError (_("Content to be joined must have the same picture size."));
155 if (vc->video_frame_rate() != ref->video_frame_rate()) {
156 throw JoinError (_("Content to be joined must have the same video frame rate."));
159 if (vc->video_frame_type() != ref->video_frame_type()) {
160 throw JoinError (_("Content to be joined must have the same video frame type."));
163 if (vc->crop() != ref->crop()) {
164 throw JoinError (_("Content to be joined must have the same crop."));
167 if (vc->scale() != ref->scale()) {
168 throw JoinError (_("Content to be joined must have the same scale setting."));
171 if (vc->colour_conversion() != ref->colour_conversion()) {
172 throw JoinError (_("Content to be joined must have the same colour conversion."));
175 if (vc->fade_in() != ref->fade_in() || vc->fade_out() != ref->fade_out()) {
176 throw JoinError (_("Content to be joined must have the same fades."));
179 _video_length += vc->video_length ();
186 _video_size = ref->video_size ();
187 _video_frame_rate = ref->video_frame_rate ();
188 _video_frame_type = ref->video_frame_type ();
189 _crop = ref->crop ();
190 _scale = ref->scale ();
191 _colour_conversion = ref->colour_conversion ();
192 _fade_in = ref->fade_in ();
193 _fade_out = ref->fade_out ();
197 VideoContent::as_xml (xmlpp::Node* node) const
199 boost::mutex::scoped_lock lm (_mutex);
200 node->add_child("VideoLength")->add_child_text (raw_convert<string> (_video_length));
201 node->add_child("VideoWidth")->add_child_text (raw_convert<string> (_video_size.width));
202 node->add_child("VideoHeight")->add_child_text (raw_convert<string> (_video_size.height));
203 if (_video_frame_rate) {
204 node->add_child("VideoFrameRate")->add_child_text (raw_convert<string> (_video_frame_rate.get()));
206 node->add_child("VideoFrameType")->add_child_text (raw_convert<string> (static_cast<int> (_video_frame_type)));
207 if (_sample_aspect_ratio) {
208 node->add_child("SampleAspectRatio")->add_child_text (raw_convert<string> (_sample_aspect_ratio.get ()));
211 _scale.as_xml (node->add_child("Scale"));
212 if (_colour_conversion) {
213 _colour_conversion.get().as_xml (node->add_child("ColourConversion"));
215 node->add_child("YUV")->add_child_text (_yuv ? "1" : "0");
216 node->add_child("FadeIn")->add_child_text (raw_convert<string> (_fade_in));
217 node->add_child("FadeOut")->add_child_text (raw_convert<string> (_fade_out));
221 VideoContent::set_default_colour_conversion ()
223 /* If there's no better offer we'll use Rec. 709 */
224 boost::mutex::scoped_lock lm (_mutex);
225 _colour_conversion = PresetColourConversion::from_id ("rec709").conversion;
229 VideoContent::take_from_video_examiner (shared_ptr<VideoExaminer> d)
231 /* These examiner calls could call other content methods which take a lock on the mutex */
232 dcp::Size const vs = d->video_size ();
233 optional<double> const vfr = d->video_frame_rate ();
234 Frame vl = d->video_length ();
235 optional<double> const ar = d->sample_aspect_ratio ();
236 bool const yuv = d->yuv ();
239 boost::mutex::scoped_lock lm (_mutex);
241 _video_frame_rate = vfr;
243 _sample_aspect_ratio = ar;
246 /* Guess correct scale from size and sample aspect ratio */
247 _scale = VideoContentScale (
248 Ratio::nearest_from_ratio (double (_video_size.width) * ar.get_value_or (1) / _video_size.height)
252 LOG_GENERAL ("Video length obtained from header as %1 frames", _video_length);
254 set_default_colour_conversion ();
256 signal_changed (VideoContentProperty::VIDEO_SIZE);
257 signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
258 signal_changed (VideoContentProperty::VIDEO_SCALE);
259 signal_changed (ContentProperty::LENGTH);
263 VideoContent::set_left_crop (int c)
266 boost::mutex::scoped_lock lm (_mutex);
268 if (_crop.left == c) {
275 signal_changed (VideoContentProperty::VIDEO_CROP);
279 VideoContent::set_right_crop (int c)
282 boost::mutex::scoped_lock lm (_mutex);
283 if (_crop.right == c) {
290 signal_changed (VideoContentProperty::VIDEO_CROP);
294 VideoContent::set_top_crop (int c)
297 boost::mutex::scoped_lock lm (_mutex);
298 if (_crop.top == c) {
305 signal_changed (VideoContentProperty::VIDEO_CROP);
309 VideoContent::set_bottom_crop (int c)
312 boost::mutex::scoped_lock lm (_mutex);
313 if (_crop.bottom == c) {
320 signal_changed (VideoContentProperty::VIDEO_CROP);
324 VideoContent::set_scale (VideoContentScale s)
327 boost::mutex::scoped_lock lm (_mutex);
335 signal_changed (VideoContentProperty::VIDEO_SCALE);
338 /** @return string which includes everything about how this content looks */
340 VideoContent::identifier () const
343 s << Content::identifier()
344 << "_" << crop().left
345 << "_" << crop().right
347 << "_" << crop().bottom
348 << "_" << scale().id()
352 if (colour_conversion()) {
353 s << "_" << colour_conversion().get().identifier ();
360 VideoContent::set_video_frame_type (VideoFrameType t)
363 boost::mutex::scoped_lock lm (_mutex);
364 _video_frame_type = t;
367 signal_changed (VideoContentProperty::VIDEO_FRAME_TYPE);
371 VideoContent::technical_summary () const
373 string s = String::compose (
374 N_("video: length %1 frames, size %2x%3, rate %4"),
375 video_length_after_3d_combine(),
381 if (sample_aspect_ratio ()) {
382 s += String::compose (N_(", sample aspect ratio %1"), (sample_aspect_ratio().get ()));
389 VideoContent::video_size_after_3d_split () const
391 dcp::Size const s = video_size ();
392 switch (video_frame_type ()) {
393 case VIDEO_FRAME_TYPE_2D:
394 case VIDEO_FRAME_TYPE_3D_ALTERNATE:
395 case VIDEO_FRAME_TYPE_3D_LEFT:
396 case VIDEO_FRAME_TYPE_3D_RIGHT:
398 case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
399 return dcp::Size (s.width / 2, s.height);
400 case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
401 return dcp::Size (s.width, s.height / 2);
404 DCPOMATIC_ASSERT (false);
408 VideoContent::unset_colour_conversion ()
411 boost::mutex::scoped_lock lm (_mutex);
412 _colour_conversion = boost::optional<ColourConversion> ();
415 signal_changed (VideoContentProperty::COLOUR_CONVERSION);
419 VideoContent::set_colour_conversion (ColourConversion c)
422 boost::mutex::scoped_lock lm (_mutex);
423 _colour_conversion = c;
426 signal_changed (VideoContentProperty::COLOUR_CONVERSION);
430 VideoContent::set_fade_in (Frame t)
433 boost::mutex::scoped_lock lm (_mutex);
437 signal_changed (VideoContentProperty::VIDEO_FADE_IN);
441 VideoContent::set_fade_out (Frame t)
444 boost::mutex::scoped_lock lm (_mutex);
448 signal_changed (VideoContentProperty::VIDEO_FADE_OUT);
451 /** @return Video size after 3D split and crop */
453 VideoContent::video_size_after_crop () const
455 return crop().apply (video_size_after_3d_split ());
459 VideoContent::scale_and_crop_to_fit_width ()
461 set_scale (VideoContentScale (film()->container ()));
463 int const crop = max (0, int (video_size().height - double (film()->frame_size().height) * video_size().width / film()->frame_size().width));
466 set_top_crop (crop / 2);
467 set_bottom_crop (crop / 2);
471 VideoContent::scale_and_crop_to_fit_height ()
473 set_scale (VideoContentScale (film()->container ()));
475 int const crop = max (0, int (video_size().width - double (film()->frame_size().width) * video_size().height / film()->frame_size().height));
476 set_left_crop (crop / 2);
477 set_right_crop (crop / 2);
483 VideoContent::set_video_frame_rate (double r)
486 boost::mutex::scoped_lock lm (_mutex);
487 if (_video_frame_rate == r) {
491 _video_frame_rate = r;
494 signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
497 /** @param f Frame index within the whole (untrimmed) content */
499 VideoContent::fade (Frame f) const
501 DCPOMATIC_ASSERT (f >= 0);
503 Frame const ts = trim_start().frames_round(video_frame_rate());
504 if ((f - ts) < fade_in()) {
505 return double (f - ts) / fade_in();
508 Frame fade_out_start = video_length() - trim_end().frames_round(video_frame_rate()) - fade_out();
509 if (f >= fade_out_start) {
510 return 1 - double (f - fade_out_start) / fade_out();
513 return optional<double> ();
517 VideoContent::processing_description () const
519 /* stringstream is OK here as this string is just for presentation to the user */
522 if (video_size().width && video_size().height) {
523 d << String::compose (
524 _("Content video is %1x%2"),
525 video_size_after_3d_split().width,
526 video_size_after_3d_split().height
530 double ratio = video_size_after_3d_split().ratio ();
532 if (sample_aspect_ratio ()) {
533 d << ", " << _("pixel aspect ratio") << " " << fixed << setprecision(2) << sample_aspect_ratio().get () << ":1";
534 ratio *= sample_aspect_ratio().get ();
537 d << "\n" << _("Display aspect ratio") << " " << fixed << setprecision(2) << ratio << ":1\n";
540 if ((crop().left || crop().right || crop().top || crop().bottom) && video_size() != dcp::Size (0, 0)) {
541 dcp::Size cropped = video_size_after_crop ();
542 d << String::compose (
543 _("Cropped to %1x%2"),
544 cropped.width, cropped.height
547 d << " (" << fixed << setprecision(2) << cropped.ratio () << ":1)\n";
550 dcp::Size const container_size = film()->frame_size ();
551 dcp::Size const scaled = scale().size (dynamic_pointer_cast<const VideoContent> (shared_from_this ()), container_size, container_size);
553 if (scaled != video_size_after_crop ()) {
554 d << String::compose (
555 _("Scaled to %1x%2"),
556 scaled.width, scaled.height
559 d << " (" << fixed << setprecision(2) << scaled.ratio() << ":1)\n";
562 if (scaled != container_size) {
563 d << String::compose (
564 _("Padded with black to fit container %1 (%2x%3)"),
565 film()->container()->nickname (),
566 container_size.width, container_size.height
569 d << " (" << fixed << setprecision(2) << container_size.ratio () << ":1)\n";
572 d << _("Content frame rate");
573 d << " " << fixed << setprecision(4) << video_frame_rate() << "\n";
575 FrameRateChange frc (video_frame_rate(), film()->video_frame_rate ());
576 d << frc.description () << "\n";
582 VideoContent::add_properties (list<UserProperty>& p) const
584 p.push_back (UserProperty (_("Video"), _("Length"), raw_convert<string> (video_length ()), _("video frames")));
585 p.push_back (UserProperty (_("Video"), _("Size"), raw_convert<string> (video_size().width) + "x" + raw_convert<string> (video_size().height)));
586 p.push_back (UserProperty (_("Video"), _("Frame rate"), raw_convert<string> (video_frame_rate(), 5), _("frames per second")));
590 VideoContent::video_frame_rate () const
592 boost::mutex::scoped_lock lm (_mutex);
593 return _video_frame_rate.get_value_or (film()->video_frame_rate ());