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_rate (0)
71 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
72 , _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_rate (0)
83 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
84 , _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_rate (0)
95 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
96 , _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->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 _fade_in = node->number_child<Frame> ("FadeIn");
132 _fade_out = node->number_child<Frame> ("FadeOut");
134 _fade_in = _fade_out = 0;
138 VideoContent::VideoContent (shared_ptr<const Film> film, vector<shared_ptr<Content> > c)
142 shared_ptr<VideoContent> ref = dynamic_pointer_cast<VideoContent> (c[0]);
143 DCPOMATIC_ASSERT (ref);
145 for (size_t i = 0; i < c.size(); ++i) {
146 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (c[i]);
148 if (vc->video_size() != ref->video_size()) {
149 throw JoinError (_("Content to be joined must have the same picture size."));
152 if (vc->video_frame_rate() != ref->video_frame_rate()) {
153 throw JoinError (_("Content to be joined must have the same video frame rate."));
156 if (vc->video_frame_type() != ref->video_frame_type()) {
157 throw JoinError (_("Content to be joined must have the same video frame type."));
160 if (vc->crop() != ref->crop()) {
161 throw JoinError (_("Content to be joined must have the same crop."));
164 if (vc->scale() != ref->scale()) {
165 throw JoinError (_("Content to be joined must have the same scale setting."));
168 if (vc->colour_conversion() != ref->colour_conversion()) {
169 throw JoinError (_("Content to be joined must have the same colour conversion."));
172 if (vc->fade_in() != ref->fade_in() || vc->fade_out() != ref->fade_out()) {
173 throw JoinError (_("Content to be joined must have the same fades."));
176 _video_length += vc->video_length ();
179 _video_size = ref->video_size ();
180 _video_frame_rate = ref->video_frame_rate ();
181 _video_frame_type = ref->video_frame_type ();
182 _crop = ref->crop ();
183 _scale = ref->scale ();
184 _colour_conversion = ref->colour_conversion ();
185 _fade_in = ref->fade_in ();
186 _fade_out = ref->fade_out ();
190 VideoContent::as_xml (xmlpp::Node* node) const
192 boost::mutex::scoped_lock lm (_mutex);
193 node->add_child("VideoLength")->add_child_text (raw_convert<string> (_video_length));
194 node->add_child("VideoWidth")->add_child_text (raw_convert<string> (_video_size.width));
195 node->add_child("VideoHeight")->add_child_text (raw_convert<string> (_video_size.height));
196 node->add_child("VideoFrameRate")->add_child_text (raw_convert<string> (_video_frame_rate));
197 node->add_child("VideoFrameType")->add_child_text (raw_convert<string> (static_cast<int> (_video_frame_type)));
198 if (_sample_aspect_ratio) {
199 node->add_child("SampleAspectRatio")->add_child_text (raw_convert<string> (_sample_aspect_ratio.get ()));
202 _scale.as_xml (node->add_child("Scale"));
203 if (_colour_conversion) {
204 _colour_conversion.get().as_xml (node->add_child("ColourConversion"));
206 node->add_child("FadeIn")->add_child_text (raw_convert<string> (_fade_in));
207 node->add_child("FadeOut")->add_child_text (raw_convert<string> (_fade_out));
211 VideoContent::set_default_colour_conversion ()
213 /* If there's no better offer we'll use Rec. 709 */
214 boost::mutex::scoped_lock lm (_mutex);
215 _colour_conversion = PresetColourConversion::from_id ("rec709").conversion;
219 VideoContent::take_from_video_examiner (shared_ptr<VideoExaminer> d)
221 /* These examiner calls could call other content methods which take a lock on the mutex */
222 dcp::Size const vs = d->video_size ();
223 optional<double> const vfr = d->video_frame_rate ();
224 Frame vl = d->video_length ();
225 optional<double> const ar = d->sample_aspect_ratio ();
228 boost::mutex::scoped_lock lm (_mutex);
230 /* Default video frame rate to 24fps if the examiner doesn't know */
231 _video_frame_rate = vfr.get_value_or (24);
233 _sample_aspect_ratio = ar;
235 /* Guess correct scale from size and sample aspect ratio */
236 _scale = VideoContentScale (
237 Ratio::nearest_from_ratio (double (_video_size.width) * ar.get_value_or (1) / _video_size.height)
241 shared_ptr<const Film> film = _film.lock ();
242 DCPOMATIC_ASSERT (film);
243 LOG_GENERAL ("Video length obtained from header as %1 frames", _video_length);
245 set_default_colour_conversion ();
247 signal_changed (VideoContentProperty::VIDEO_SIZE);
248 signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
249 signal_changed (VideoContentProperty::VIDEO_SCALE);
250 signal_changed (ContentProperty::LENGTH);
254 VideoContent::set_left_crop (int c)
257 boost::mutex::scoped_lock lm (_mutex);
259 if (_crop.left == c) {
266 signal_changed (VideoContentProperty::VIDEO_CROP);
270 VideoContent::set_right_crop (int c)
273 boost::mutex::scoped_lock lm (_mutex);
274 if (_crop.right == c) {
281 signal_changed (VideoContentProperty::VIDEO_CROP);
285 VideoContent::set_top_crop (int c)
288 boost::mutex::scoped_lock lm (_mutex);
289 if (_crop.top == c) {
296 signal_changed (VideoContentProperty::VIDEO_CROP);
300 VideoContent::set_bottom_crop (int c)
303 boost::mutex::scoped_lock lm (_mutex);
304 if (_crop.bottom == c) {
311 signal_changed (VideoContentProperty::VIDEO_CROP);
315 VideoContent::set_scale (VideoContentScale s)
318 boost::mutex::scoped_lock lm (_mutex);
326 signal_changed (VideoContentProperty::VIDEO_SCALE);
329 /** @return string which includes everything about how this content looks */
331 VideoContent::identifier () const
334 s << Content::identifier()
335 << "_" << crop().left
336 << "_" << crop().right
338 << "_" << crop().bottom
339 << "_" << scale().id()
343 if (colour_conversion()) {
344 s << "_" << colour_conversion().get().identifier ();
351 VideoContent::set_video_frame_type (VideoFrameType t)
354 boost::mutex::scoped_lock lm (_mutex);
355 _video_frame_type = t;
358 signal_changed (VideoContentProperty::VIDEO_FRAME_TYPE);
362 VideoContent::technical_summary () const
364 string s = String::compose (
365 N_("video: length %1 frames, size %2x%3, rate %4"),
366 video_length_after_3d_combine(),
372 if (sample_aspect_ratio ()) {
373 s += String::compose (N_(", sample aspect ratio %1"), (sample_aspect_ratio().get ()));
380 VideoContent::video_size_after_3d_split () const
382 dcp::Size const s = video_size ();
383 switch (video_frame_type ()) {
384 case VIDEO_FRAME_TYPE_2D:
385 case VIDEO_FRAME_TYPE_3D_ALTERNATE:
386 case VIDEO_FRAME_TYPE_3D_LEFT:
387 case VIDEO_FRAME_TYPE_3D_RIGHT:
389 case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
390 return dcp::Size (s.width / 2, s.height);
391 case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
392 return dcp::Size (s.width, s.height / 2);
395 DCPOMATIC_ASSERT (false);
399 VideoContent::unset_colour_conversion ()
402 boost::mutex::scoped_lock lm (_mutex);
403 _colour_conversion = boost::optional<ColourConversion> ();
406 signal_changed (VideoContentProperty::COLOUR_CONVERSION);
410 VideoContent::set_colour_conversion (ColourConversion c)
413 boost::mutex::scoped_lock lm (_mutex);
414 _colour_conversion = c;
417 signal_changed (VideoContentProperty::COLOUR_CONVERSION);
421 VideoContent::set_fade_in (Frame t)
424 boost::mutex::scoped_lock lm (_mutex);
428 signal_changed (VideoContentProperty::VIDEO_FADE_IN);
432 VideoContent::set_fade_out (Frame t)
435 boost::mutex::scoped_lock lm (_mutex);
439 signal_changed (VideoContentProperty::VIDEO_FADE_OUT);
442 /** @return Video size after 3D split and crop */
444 VideoContent::video_size_after_crop () const
446 return crop().apply (video_size_after_3d_split ());
450 VideoContent::scale_and_crop_to_fit_width ()
452 shared_ptr<const Film> film = _film.lock ();
453 DCPOMATIC_ASSERT (film);
455 set_scale (VideoContentScale (film->container ()));
457 int const crop = max (0, int (video_size().height - double (film->frame_size().height) * video_size().width / film->frame_size().width));
458 set_top_crop (crop / 2);
459 set_bottom_crop (crop / 2);
463 VideoContent::scale_and_crop_to_fit_height ()
465 shared_ptr<const Film> film = _film.lock ();
466 DCPOMATIC_ASSERT (film);
468 set_scale (VideoContentScale (film->container ()));
470 int const crop = max (0, int (video_size().width - double (film->frame_size().width) * video_size().height / film->frame_size().height));
471 set_left_crop (crop / 2);
472 set_right_crop (crop / 2);
476 VideoContent::set_video_frame_rate (double r)
479 boost::mutex::scoped_lock lm (_mutex);
480 if (_video_frame_rate == r) {
484 _video_frame_rate = r;
487 signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
491 VideoContent::fade (Frame f) const
493 DCPOMATIC_ASSERT (f >= 0);
496 return double (f) / fade_in();
499 Frame fade_out_start = video_length() - fade_out();
500 if (f >= fade_out_start) {
501 return 1 - double (f - fade_out_start) / fade_out();
504 return optional<double> ();
508 VideoContent::processing_description () const
510 /* stringstream is OK here as this string is just for presentation to the user */
513 if (video_size().width && video_size().height) {
514 d << String::compose (
515 _("Content video is %1x%2"),
516 video_size_after_3d_split().width,
517 video_size_after_3d_split().height
521 double ratio = video_size_after_3d_split().ratio ();
523 if (sample_aspect_ratio ()) {
524 d << ", " << _("pixel aspect ratio") << " " << fixed << setprecision(2) << sample_aspect_ratio().get () << ":1";
525 ratio *= sample_aspect_ratio().get ();
528 d << "\n" << _("Display aspect ratio") << " " << fixed << setprecision(2) << ratio << ":1\n";
531 if ((crop().left || crop().right || crop().top || crop().bottom) && video_size() != dcp::Size (0, 0)) {
532 dcp::Size cropped = video_size_after_crop ();
533 d << String::compose (
534 _("Cropped to %1x%2"),
535 cropped.width, cropped.height
538 d << " (" << fixed << setprecision(2) << cropped.ratio () << ":1)\n";
541 shared_ptr<const Film> film = _film.lock ();
542 DCPOMATIC_ASSERT (film);
544 dcp::Size const container_size = film->frame_size ();
545 dcp::Size const scaled = scale().size (dynamic_pointer_cast<const VideoContent> (shared_from_this ()), container_size, container_size);
547 if (scaled != video_size_after_crop ()) {
548 d << String::compose (
549 _("Scaled to %1x%2"),
550 scaled.width, scaled.height
553 d << " (" << fixed << setprecision(2) << scaled.ratio() << ":1)\n";
556 if (scaled != container_size) {
557 d << String::compose (
558 _("Padded with black to fit container %1 (%2x%3)"),
559 film->container()->nickname (),
560 container_size.width, container_size.height
563 d << " (" << fixed << setprecision(2) << container_size.ratio () << ":1)\n";
566 d << _("Content frame rate");
567 d << " " << fixed << setprecision(4) << video_frame_rate() << "\n";
569 FrameRateChange frc (video_frame_rate(), film->video_frame_rate ());
570 d << frc.description () << "\n";
576 VideoContent::add_properties (list<pair<string, string> >& p) const
578 p.push_back (make_pair (_("Video length"), raw_convert<string> (video_length ()) + " " + _("video frames")));
579 p.push_back (make_pair (_("Video size"), raw_convert<string> (video_size().width) + "x" + raw_convert<string> (video_size().height)));
580 p.push_back (make_pair (_("Video frame rate"), raw_convert<string> (video_frame_rate()) + " " + _("frames per second")));