2 Copyright (C) 2013-2016 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"
22 #include "video_examiner.h"
23 #include "compose.hpp"
26 #include "colour_conversion.h"
29 #include "exceptions.h"
30 #include "frame_rate_change.h"
32 #include "safe_stringstream.h"
33 #include "raw_convert.h"
34 #include <libcxml/cxml.h>
35 #include <dcp/colour_matrix.h>
36 #include <libxml++/libxml++.h>
42 #define LOG_GENERAL(...) film->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL);
44 int const VideoContentProperty::VIDEO_SIZE = 0;
45 int const VideoContentProperty::VIDEO_FRAME_RATE = 1;
46 int const VideoContentProperty::VIDEO_FRAME_TYPE = 2;
47 int const VideoContentProperty::VIDEO_CROP = 3;
48 int const VideoContentProperty::VIDEO_SCALE = 4;
49 int const VideoContentProperty::COLOUR_CONVERSION = 5;
50 int const VideoContentProperty::VIDEO_FADE_IN = 6;
51 int const VideoContentProperty::VIDEO_FADE_OUT = 7;
54 using std::setprecision;
60 using std::setprecision;
63 using boost::shared_ptr;
64 using boost::optional;
65 using boost::dynamic_pointer_cast;
67 VideoContent::VideoContent (Content* parent, shared_ptr<const Film> film)
68 : ContentPart (parent, film)
70 , _video_frame_type (VIDEO_FRAME_TYPE_2D)
71 , _scale (VideoContentScale (Ratio::from_id ("178")))
79 VideoContent::VideoContent (Content* parent, shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
80 : ContentPart (parent, film)
82 _video_size.width = node->number_child<int> ("VideoWidth");
83 _video_size.height = node->number_child<int> ("VideoHeight");
84 _video_frame_rate = node->optional_number_child<double> ("VideoFrameRate");
85 _video_length = node->number_child<Frame> ("VideoLength");
86 _video_frame_type = static_cast<VideoFrameType> (node->number_child<int> ("VideoFrameType"));
87 _sample_aspect_ratio = node->optional_number_child<double> ("SampleAspectRatio");
88 _crop.left = node->number_child<int> ("LeftCrop");
89 _crop.right = node->number_child<int> ("RightCrop");
90 _crop.top = node->number_child<int> ("TopCrop");
91 _crop.bottom = node->number_child<int> ("BottomCrop");
94 optional<string> r = node->optional_string_child ("Ratio");
96 _scale = VideoContentScale (Ratio::from_id (r.get ()));
99 _scale = VideoContentScale (node->node_child ("Scale"));
103 if (node->optional_node_child ("ColourConversion")) {
104 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"), version);
107 _yuv = node->optional_bool_child("YUV").get_value_or (true);
110 _fade_in = node->number_child<Frame> ("FadeIn");
111 _fade_out = node->number_child<Frame> ("FadeOut");
113 _fade_in = _fade_out = 0;
117 VideoContent::VideoContent (Content* parent, shared_ptr<const Film> film, vector<shared_ptr<Content> > c)
118 : ContentPart (parent, film)
122 shared_ptr<VideoContent> ref = c[0]->video;
123 DCPOMATIC_ASSERT (ref);
125 for (size_t i = 1; i < c.size(); ++i) {
127 if (c[i]->video->video_size() != ref->video_size()) {
128 throw JoinError (_("Content to be joined must have the same picture size."));
131 if (c[i]->video->video_frame_rate() != ref->video_frame_rate()) {
132 throw JoinError (_("Content to be joined must have the same video frame rate."));
135 if (c[i]->video->video_frame_type() != ref->video_frame_type()) {
136 throw JoinError (_("Content to be joined must have the same video frame type."));
139 if (c[i]->video->crop() != ref->crop()) {
140 throw JoinError (_("Content to be joined must have the same crop."));
143 if (c[i]->video->scale() != ref->scale()) {
144 throw JoinError (_("Content to be joined must have the same scale setting."));
147 if (c[i]->video->colour_conversion() != ref->colour_conversion()) {
148 throw JoinError (_("Content to be joined must have the same colour conversion."));
151 if (c[i]->video->fade_in() != ref->fade_in() || c[i]->video->fade_out() != ref->fade_out()) {
152 throw JoinError (_("Content to be joined must have the same fades."));
155 _video_length += c[i]->video->video_length ();
157 if (c[i]->video->yuv ()) {
162 _video_size = ref->video_size ();
163 _video_frame_rate = ref->video_frame_rate ();
164 _video_frame_type = ref->video_frame_type ();
165 _crop = ref->crop ();
166 _scale = ref->scale ();
167 _colour_conversion = ref->colour_conversion ();
168 _fade_in = ref->fade_in ();
169 _fade_out = ref->fade_out ();
173 VideoContent::as_xml (xmlpp::Node* node) const
175 boost::mutex::scoped_lock lm (_mutex);
176 node->add_child("VideoLength")->add_child_text (raw_convert<string> (_video_length));
177 node->add_child("VideoWidth")->add_child_text (raw_convert<string> (_video_size.width));
178 node->add_child("VideoHeight")->add_child_text (raw_convert<string> (_video_size.height));
179 if (_video_frame_rate) {
180 node->add_child("VideoFrameRate")->add_child_text (raw_convert<string> (_video_frame_rate.get()));
182 node->add_child("VideoFrameType")->add_child_text (raw_convert<string> (static_cast<int> (_video_frame_type)));
183 if (_sample_aspect_ratio) {
184 node->add_child("SampleAspectRatio")->add_child_text (raw_convert<string> (_sample_aspect_ratio.get ()));
187 _scale.as_xml (node->add_child("Scale"));
188 if (_colour_conversion) {
189 _colour_conversion.get().as_xml (node->add_child("ColourConversion"));
191 node->add_child("YUV")->add_child_text (_yuv ? "1" : "0");
192 node->add_child("FadeIn")->add_child_text (raw_convert<string> (_fade_in));
193 node->add_child("FadeOut")->add_child_text (raw_convert<string> (_fade_out));
197 VideoContent::set_default_colour_conversion ()
199 /* If there's no better offer we'll use Rec. 709 */
200 boost::mutex::scoped_lock lm (_mutex);
201 _colour_conversion = PresetColourConversion::from_id ("rec709").conversion;
205 VideoContent::take_from_video_examiner (shared_ptr<VideoExaminer> d)
207 /* These examiner calls could call other content methods which take a lock on the mutex */
208 dcp::Size const vs = d->video_size ();
209 optional<double> const vfr = d->video_frame_rate ();
210 Frame vl = d->video_length ();
211 optional<double> const ar = d->sample_aspect_ratio ();
212 bool const yuv = d->yuv ();
215 boost::mutex::scoped_lock lm (_mutex);
217 _video_frame_rate = vfr;
219 _sample_aspect_ratio = ar;
222 /* Guess correct scale from size and sample aspect ratio */
223 _scale = VideoContentScale (
224 Ratio::nearest_from_ratio (double (_video_size.width) * ar.get_value_or (1) / _video_size.height)
228 shared_ptr<const Film> film = _film.lock ();
229 DCPOMATIC_ASSERT (film);
230 LOG_GENERAL ("Video length obtained from header as %1 frames", _video_length);
232 _parent->signal_changed (VideoContentProperty::VIDEO_SIZE);
233 _parent->signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
234 _parent->signal_changed (VideoContentProperty::VIDEO_SCALE);
235 _parent->signal_changed (ContentProperty::LENGTH);
238 /** @return string which includes everything about how this content looks */
240 VideoContent::identifier () const
244 << "_" << crop().right
246 << "_" << crop().bottom
247 << "_" << scale().id()
251 if (colour_conversion()) {
252 s << "_" << colour_conversion().get().identifier ();
259 VideoContent::technical_summary () const
261 string s = String::compose (
262 N_("video: length %1 frames, size %2x%3, rate %4"),
263 video_length_after_3d_combine(),
269 if (sample_aspect_ratio ()) {
270 s += String::compose (N_(", sample aspect ratio %1"), (sample_aspect_ratio().get ()));
277 VideoContent::video_size_after_3d_split () const
279 dcp::Size const s = video_size ();
280 switch (video_frame_type ()) {
281 case VIDEO_FRAME_TYPE_2D:
282 case VIDEO_FRAME_TYPE_3D_ALTERNATE:
283 case VIDEO_FRAME_TYPE_3D_LEFT:
284 case VIDEO_FRAME_TYPE_3D_RIGHT:
286 case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
287 return dcp::Size (s.width / 2, s.height);
288 case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
289 return dcp::Size (s.width, s.height / 2);
292 DCPOMATIC_ASSERT (false);
295 /** @return Video size after 3D split and crop */
297 VideoContent::video_size_after_crop () const
299 return crop().apply (video_size_after_3d_split ());
303 VideoContent::scale_and_crop_to_fit_width ()
305 shared_ptr<const Film> film = _film.lock ();
306 DCPOMATIC_ASSERT (film);
307 set_scale (VideoContentScale (film->container ()));
309 int const crop = max (0, int (video_size().height - double (film->frame_size().height) * video_size().width / film->frame_size().width));
312 set_top_crop (crop / 2);
313 set_bottom_crop (crop / 2);
317 VideoContent::scale_and_crop_to_fit_height ()
319 shared_ptr<const Film> film = _film.lock ();
320 DCPOMATIC_ASSERT (film);
321 set_scale (VideoContentScale (film->container ()));
323 int const crop = max (0, int (video_size().width - double (film->frame_size().width) * video_size().height / film->frame_size().height));
324 set_left_crop (crop / 2);
325 set_right_crop (crop / 2);
330 /** @param f Frame index within the whole (untrimmed) content */
332 VideoContent::fade (Frame f) const
334 DCPOMATIC_ASSERT (f >= 0);
336 Frame const ts = _parent->trim_start().frames_round(video_frame_rate());
337 if ((f - ts) < fade_in()) {
338 return double (f - ts) / fade_in();
341 Frame fade_out_start = video_length() - _parent->trim_end().frames_round(video_frame_rate()) - fade_out();
342 if (f >= fade_out_start) {
343 return 1 - double (f - fade_out_start) / fade_out();
346 return optional<double> ();
350 VideoContent::processing_description () const
352 /* stringstream is OK here as this string is just for presentation to the user */
355 if (video_size().width && video_size().height) {
356 d << String::compose (
357 _("Content video is %1x%2"),
358 video_size_after_3d_split().width,
359 video_size_after_3d_split().height
363 double ratio = video_size_after_3d_split().ratio ();
365 if (sample_aspect_ratio ()) {
366 d << ", " << _("pixel aspect ratio") << " " << fixed << setprecision(2) << sample_aspect_ratio().get () << ":1";
367 ratio *= sample_aspect_ratio().get ();
370 d << "\n" << _("Display aspect ratio") << " " << fixed << setprecision(2) << ratio << ":1\n";
373 if ((crop().left || crop().right || crop().top || crop().bottom) && video_size() != dcp::Size (0, 0)) {
374 dcp::Size cropped = video_size_after_crop ();
375 d << String::compose (
376 _("Cropped to %1x%2"),
377 cropped.width, cropped.height
380 d << " (" << fixed << setprecision(2) << cropped.ratio () << ":1)\n";
383 shared_ptr<const Film> film = _film.lock ();
384 DCPOMATIC_ASSERT (film);
385 dcp::Size const container_size = film->frame_size ();
386 dcp::Size const scaled = scale().size (shared_from_this(), container_size, container_size);
388 if (scaled != video_size_after_crop ()) {
389 d << String::compose (
390 _("Scaled to %1x%2"),
391 scaled.width, scaled.height
394 d << " (" << fixed << setprecision(2) << scaled.ratio() << ":1)\n";
397 if (scaled != container_size) {
398 d << String::compose (
399 _("Padded with black to fit container %1 (%2x%3)"),
400 film->container()->nickname (),
401 container_size.width, container_size.height
404 d << " (" << fixed << setprecision(2) << container_size.ratio () << ":1)\n";
407 d << _("Content frame rate");
408 d << " " << fixed << setprecision(4) << video_frame_rate() << "\n";
410 FrameRateChange frc (video_frame_rate(), film->video_frame_rate ());
411 d << frc.description () << "\n";
417 VideoContent::add_properties (list<UserProperty>& p) const
419 p.push_back (UserProperty (_("Video"), _("Length"), raw_convert<string> (video_length ()), _("video frames")));
420 p.push_back (UserProperty (_("Video"), _("Size"), raw_convert<string> (video_size().width) + "x" + raw_convert<string> (video_size().height)));
421 p.push_back (UserProperty (_("Video"), _("Frame rate"), raw_convert<string> (video_frame_rate(), 5), _("frames per second")));
425 VideoContent::video_frame_rate () const
427 boost::mutex::scoped_lock lm (_mutex);
428 shared_ptr<const Film> film = _film.lock ();
429 DCPOMATIC_ASSERT (film);
430 return _video_frame_rate.get_value_or (film->video_frame_rate ());
434 VideoContent::set_video_length (Frame len)
436 maybe_set (_video_length, len, ContentProperty::LENGTH);
440 VideoContent::set_left_crop (int c)
442 maybe_set (_crop.left, c, VideoContentProperty::VIDEO_CROP);
446 VideoContent::set_right_crop (int c)
448 maybe_set (_crop.right, c, VideoContentProperty::VIDEO_CROP);
452 VideoContent::set_top_crop (int c)
454 maybe_set (_crop.top, c, VideoContentProperty::VIDEO_CROP);
458 VideoContent::set_bottom_crop (int c)
460 maybe_set (_crop.bottom, c, VideoContentProperty::VIDEO_CROP);
464 VideoContent::set_scale (VideoContentScale s)
466 maybe_set (_scale, s, VideoContentProperty::VIDEO_SCALE);
470 VideoContent::set_video_frame_rate (double r)
472 maybe_set (_video_frame_rate, r, VideoContentProperty::VIDEO_FRAME_RATE);
476 VideoContent::set_video_frame_type (VideoFrameType t)
478 maybe_set (_video_frame_type, t, VideoContentProperty::VIDEO_FRAME_TYPE);
482 VideoContent::unset_colour_conversion ()
484 maybe_set (_colour_conversion, boost::optional<ColourConversion> (), VideoContentProperty::COLOUR_CONVERSION);
488 VideoContent::set_colour_conversion (ColourConversion c)
490 maybe_set (_colour_conversion, c, VideoContentProperty::COLOUR_CONVERSION);
494 VideoContent::set_fade_in (Frame t)
496 maybe_set (_fade_in, t, VideoContentProperty::VIDEO_FADE_IN);
500 VideoContent::set_fade_out (Frame t)
502 maybe_set (_fade_out, t, VideoContentProperty::VIDEO_FADE_OUT);