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, boost::filesystem::path p)
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, cxml::ConstNodePtr node, int version)
91 : Content (film, node)
93 _video_size.width = node->number_child<int> ("VideoWidth");
94 _video_size.height = node->number_child<int> ("VideoHeight");
95 _video_frame_rate = node->optional_number_child<double> ("VideoFrameRate");
96 _video_length = node->number_child<Frame> ("VideoLength");
97 _video_frame_type = static_cast<VideoFrameType> (node->number_child<int> ("VideoFrameType"));
98 _sample_aspect_ratio = node->optional_number_child<double> ("SampleAspectRatio");
99 _crop.left = node->number_child<int> ("LeftCrop");
100 _crop.right = node->number_child<int> ("RightCrop");
101 _crop.top = node->number_child<int> ("TopCrop");
102 _crop.bottom = node->number_child<int> ("BottomCrop");
105 optional<string> r = node->optional_string_child ("Ratio");
107 _scale = VideoContentScale (Ratio::from_id (r.get ()));
110 _scale = VideoContentScale (node->node_child ("Scale"));
114 if (node->optional_node_child ("ColourConversion")) {
115 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"), version);
118 _yuv = node->optional_bool_child("YUV").get_value_or (true);
121 _fade_in = node->number_child<Frame> ("FadeIn");
122 _fade_out = node->number_child<Frame> ("FadeOut");
124 _fade_in = _fade_out = 0;
128 VideoContent::VideoContent (shared_ptr<const Film> film, vector<shared_ptr<Content> > c)
133 shared_ptr<VideoContent> ref = dynamic_pointer_cast<VideoContent> (c[0]);
134 DCPOMATIC_ASSERT (ref);
136 for (size_t i = 0; i < c.size(); ++i) {
137 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (c[i]);
139 if (vc->video_size() != ref->video_size()) {
140 throw JoinError (_("Content to be joined must have the same picture size."));
143 if (vc->video_frame_rate() != ref->video_frame_rate()) {
144 throw JoinError (_("Content to be joined must have the same video frame rate."));
147 if (vc->video_frame_type() != ref->video_frame_type()) {
148 throw JoinError (_("Content to be joined must have the same video frame type."));
151 if (vc->crop() != ref->crop()) {
152 throw JoinError (_("Content to be joined must have the same crop."));
155 if (vc->scale() != ref->scale()) {
156 throw JoinError (_("Content to be joined must have the same scale setting."));
159 if (vc->colour_conversion() != ref->colour_conversion()) {
160 throw JoinError (_("Content to be joined must have the same colour conversion."));
163 if (vc->fade_in() != ref->fade_in() || vc->fade_out() != ref->fade_out()) {
164 throw JoinError (_("Content to be joined must have the same fades."));
167 _video_length += vc->video_length ();
174 _video_size = ref->video_size ();
175 _video_frame_rate = ref->video_frame_rate ();
176 _video_frame_type = ref->video_frame_type ();
177 _crop = ref->crop ();
178 _scale = ref->scale ();
179 _colour_conversion = ref->colour_conversion ();
180 _fade_in = ref->fade_in ();
181 _fade_out = ref->fade_out ();
185 VideoContent::as_xml (xmlpp::Node* node) const
187 boost::mutex::scoped_lock lm (_mutex);
188 node->add_child("VideoLength")->add_child_text (raw_convert<string> (_video_length));
189 node->add_child("VideoWidth")->add_child_text (raw_convert<string> (_video_size.width));
190 node->add_child("VideoHeight")->add_child_text (raw_convert<string> (_video_size.height));
191 if (_video_frame_rate) {
192 node->add_child("VideoFrameRate")->add_child_text (raw_convert<string> (_video_frame_rate.get()));
194 node->add_child("VideoFrameType")->add_child_text (raw_convert<string> (static_cast<int> (_video_frame_type)));
195 if (_sample_aspect_ratio) {
196 node->add_child("SampleAspectRatio")->add_child_text (raw_convert<string> (_sample_aspect_ratio.get ()));
199 _scale.as_xml (node->add_child("Scale"));
200 if (_colour_conversion) {
201 _colour_conversion.get().as_xml (node->add_child("ColourConversion"));
203 node->add_child("YUV")->add_child_text (_yuv ? "1" : "0");
204 node->add_child("FadeIn")->add_child_text (raw_convert<string> (_fade_in));
205 node->add_child("FadeOut")->add_child_text (raw_convert<string> (_fade_out));
209 VideoContent::set_default_colour_conversion ()
211 /* If there's no better offer we'll use Rec. 709 */
212 boost::mutex::scoped_lock lm (_mutex);
213 _colour_conversion = PresetColourConversion::from_id ("rec709").conversion;
217 VideoContent::take_from_video_examiner (shared_ptr<VideoExaminer> d)
219 /* These examiner calls could call other content methods which take a lock on the mutex */
220 dcp::Size const vs = d->video_size ();
221 optional<double> const vfr = d->video_frame_rate ();
222 Frame vl = d->video_length ();
223 optional<double> const ar = d->sample_aspect_ratio ();
224 bool const yuv = d->yuv ();
227 boost::mutex::scoped_lock lm (_mutex);
229 _video_frame_rate = vfr;
231 _sample_aspect_ratio = ar;
234 /* Guess correct scale from size and sample aspect ratio */
235 _scale = VideoContentScale (
236 Ratio::nearest_from_ratio (double (_video_size.width) * ar.get_value_or (1) / _video_size.height)
240 LOG_GENERAL ("Video length obtained from header as %1 frames", _video_length);
242 set_default_colour_conversion ();
244 signal_changed (VideoContentProperty::VIDEO_SIZE);
245 signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
246 signal_changed (VideoContentProperty::VIDEO_SCALE);
247 signal_changed (ContentProperty::LENGTH);
251 VideoContent::set_left_crop (int c)
254 boost::mutex::scoped_lock lm (_mutex);
256 if (_crop.left == c) {
263 signal_changed (VideoContentProperty::VIDEO_CROP);
267 VideoContent::set_right_crop (int c)
270 boost::mutex::scoped_lock lm (_mutex);
271 if (_crop.right == c) {
278 signal_changed (VideoContentProperty::VIDEO_CROP);
282 VideoContent::set_top_crop (int c)
285 boost::mutex::scoped_lock lm (_mutex);
286 if (_crop.top == c) {
293 signal_changed (VideoContentProperty::VIDEO_CROP);
297 VideoContent::set_bottom_crop (int c)
300 boost::mutex::scoped_lock lm (_mutex);
301 if (_crop.bottom == c) {
308 signal_changed (VideoContentProperty::VIDEO_CROP);
312 VideoContent::set_scale (VideoContentScale s)
315 boost::mutex::scoped_lock lm (_mutex);
323 signal_changed (VideoContentProperty::VIDEO_SCALE);
326 /** @return string which includes everything about how this content looks */
328 VideoContent::identifier () const
331 s << Content::identifier()
332 << "_" << crop().left
333 << "_" << crop().right
335 << "_" << crop().bottom
336 << "_" << scale().id()
340 if (colour_conversion()) {
341 s << "_" << colour_conversion().get().identifier ();
348 VideoContent::set_video_frame_type (VideoFrameType t)
351 boost::mutex::scoped_lock lm (_mutex);
352 _video_frame_type = t;
355 signal_changed (VideoContentProperty::VIDEO_FRAME_TYPE);
359 VideoContent::technical_summary () const
361 string s = String::compose (
362 N_("video: length %1 frames, size %2x%3, rate %4"),
363 video_length_after_3d_combine(),
369 if (sample_aspect_ratio ()) {
370 s += String::compose (N_(", sample aspect ratio %1"), (sample_aspect_ratio().get ()));
377 VideoContent::video_size_after_3d_split () const
379 dcp::Size const s = video_size ();
380 switch (video_frame_type ()) {
381 case VIDEO_FRAME_TYPE_2D:
382 case VIDEO_FRAME_TYPE_3D_ALTERNATE:
383 case VIDEO_FRAME_TYPE_3D_LEFT:
384 case VIDEO_FRAME_TYPE_3D_RIGHT:
386 case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
387 return dcp::Size (s.width / 2, s.height);
388 case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
389 return dcp::Size (s.width, s.height / 2);
392 DCPOMATIC_ASSERT (false);
396 VideoContent::unset_colour_conversion ()
399 boost::mutex::scoped_lock lm (_mutex);
400 _colour_conversion = boost::optional<ColourConversion> ();
403 signal_changed (VideoContentProperty::COLOUR_CONVERSION);
407 VideoContent::set_colour_conversion (ColourConversion c)
410 boost::mutex::scoped_lock lm (_mutex);
411 _colour_conversion = c;
414 signal_changed (VideoContentProperty::COLOUR_CONVERSION);
418 VideoContent::set_fade_in (Frame t)
421 boost::mutex::scoped_lock lm (_mutex);
425 signal_changed (VideoContentProperty::VIDEO_FADE_IN);
429 VideoContent::set_fade_out (Frame t)
432 boost::mutex::scoped_lock lm (_mutex);
436 signal_changed (VideoContentProperty::VIDEO_FADE_OUT);
439 /** @return Video size after 3D split and crop */
441 VideoContent::video_size_after_crop () const
443 return crop().apply (video_size_after_3d_split ());
447 VideoContent::scale_and_crop_to_fit_width ()
449 set_scale (VideoContentScale (film()->container ()));
451 int const crop = max (0, int (video_size().height - double (film()->frame_size().height) * video_size().width / film()->frame_size().width));
454 set_top_crop (crop / 2);
455 set_bottom_crop (crop / 2);
459 VideoContent::scale_and_crop_to_fit_height ()
461 set_scale (VideoContentScale (film()->container ()));
463 int const crop = max (0, int (video_size().width - double (film()->frame_size().width) * video_size().height / film()->frame_size().height));
464 set_left_crop (crop / 2);
465 set_right_crop (crop / 2);
471 VideoContent::set_video_frame_rate (double r)
474 boost::mutex::scoped_lock lm (_mutex);
475 if (_video_frame_rate == r) {
479 _video_frame_rate = r;
482 signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
485 /** @param f Frame index within the whole (untrimmed) content */
487 VideoContent::fade (Frame f) const
489 DCPOMATIC_ASSERT (f >= 0);
491 Frame const ts = trim_start().frames_round(video_frame_rate());
492 if ((f - ts) < fade_in()) {
493 return double (f - ts) / fade_in();
496 Frame fade_out_start = video_length() - trim_end().frames_round(video_frame_rate()) - fade_out();
497 if (f >= fade_out_start) {
498 return 1 - double (f - fade_out_start) / fade_out();
501 return optional<double> ();
505 VideoContent::processing_description () const
507 /* stringstream is OK here as this string is just for presentation to the user */
510 if (video_size().width && video_size().height) {
511 d << String::compose (
512 _("Content video is %1x%2"),
513 video_size_after_3d_split().width,
514 video_size_after_3d_split().height
518 double ratio = video_size_after_3d_split().ratio ();
520 if (sample_aspect_ratio ()) {
521 d << ", " << _("pixel aspect ratio") << " " << fixed << setprecision(2) << sample_aspect_ratio().get () << ":1";
522 ratio *= sample_aspect_ratio().get ();
525 d << "\n" << _("Display aspect ratio") << " " << fixed << setprecision(2) << ratio << ":1\n";
528 if ((crop().left || crop().right || crop().top || crop().bottom) && video_size() != dcp::Size (0, 0)) {
529 dcp::Size cropped = video_size_after_crop ();
530 d << String::compose (
531 _("Cropped to %1x%2"),
532 cropped.width, cropped.height
535 d << " (" << fixed << setprecision(2) << cropped.ratio () << ":1)\n";
538 dcp::Size const container_size = film()->frame_size ();
539 dcp::Size const scaled = scale().size (dynamic_pointer_cast<const VideoContent> (shared_from_this ()), container_size, container_size);
541 if (scaled != video_size_after_crop ()) {
542 d << String::compose (
543 _("Scaled to %1x%2"),
544 scaled.width, scaled.height
547 d << " (" << fixed << setprecision(2) << scaled.ratio() << ":1)\n";
550 if (scaled != container_size) {
551 d << String::compose (
552 _("Padded with black to fit container %1 (%2x%3)"),
553 film()->container()->nickname (),
554 container_size.width, container_size.height
557 d << " (" << fixed << setprecision(2) << container_size.ratio () << ":1)\n";
560 d << _("Content frame rate");
561 d << " " << fixed << setprecision(4) << video_frame_rate() << "\n";
563 FrameRateChange frc (video_frame_rate(), film()->video_frame_rate ());
564 d << frc.description () << "\n";
570 VideoContent::add_properties (list<UserProperty>& p) const
572 p.push_back (UserProperty (_("Video"), _("Length"), raw_convert<string> (video_length ()), _("video frames")));
573 p.push_back (UserProperty (_("Video"), _("Size"), raw_convert<string> (video_size().width) + "x" + raw_convert<string> (video_size().height)));
574 p.push_back (UserProperty (_("Video"), _("Frame rate"), raw_convert<string> (video_frame_rate(), 5), _("frames per second")));
578 VideoContent::video_frame_rate () const
580 boost::mutex::scoped_lock lm (_mutex);
581 return _video_frame_rate.get_value_or (film()->video_frame_rate ());