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::SIZE = 0;
45 int const VideoContentProperty::FRAME_TYPE = 1;
46 int const VideoContentProperty::CROP = 2;
47 int const VideoContentProperty::SCALE = 3;
48 int const VideoContentProperty::COLOUR_CONVERSION = 4;
49 int const VideoContentProperty::FADE_IN = 5;
50 int const VideoContentProperty::FADE_OUT = 6;
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 (Content* parent, shared_ptr<const Film> film)
67 : ContentPart (parent, film)
69 , _frame_type (VIDEO_FRAME_TYPE_2D)
70 , _scale (VideoContentScale (Ratio::from_id ("178")))
78 shared_ptr<VideoContent>
79 VideoContent::from_xml (Content* parent, shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
81 if (!node->optional_number_child<int> ("VideoWidth")) {
82 return shared_ptr<VideoContent> ();
85 return shared_ptr<VideoContent> (new VideoContent (parent, film, node, version));
88 VideoContent::VideoContent (Content* parent, shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
89 : ContentPart (parent, film)
91 _size.width = node->number_child<int> ("VideoWidth");
92 _size.height = node->number_child<int> ("VideoHeight");
94 /* Backwards compatibility */
95 optional<double> r = node->optional_number_child<double>("VideoFrameRate");
97 _parent->set_video_frame_rate (r.get ());
100 _length = node->number_child<Frame> ("VideoLength");
101 _frame_type = static_cast<VideoFrameType> (node->number_child<int> ("VideoFrameType"));
102 _sample_aspect_ratio = node->optional_number_child<double> ("SampleAspectRatio");
103 _crop.left = node->number_child<int> ("LeftCrop");
104 _crop.right = node->number_child<int> ("RightCrop");
105 _crop.top = node->number_child<int> ("TopCrop");
106 _crop.bottom = node->number_child<int> ("BottomCrop");
109 optional<string> r = node->optional_string_child ("Ratio");
111 _scale = VideoContentScale (Ratio::from_id (r.get ()));
114 _scale = VideoContentScale (node->node_child ("Scale"));
118 if (node->optional_node_child ("ColourConversion")) {
119 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"), version);
122 _yuv = node->optional_bool_child("YUV").get_value_or (true);
125 _fade_in = node->number_child<Frame> ("FadeIn");
126 _fade_out = node->number_child<Frame> ("FadeOut");
128 _fade_in = _fade_out = 0;
132 VideoContent::VideoContent (Content* parent, shared_ptr<const Film> film, vector<shared_ptr<Content> > c)
133 : ContentPart (parent, film)
137 shared_ptr<VideoContent> ref = c[0]->video;
138 DCPOMATIC_ASSERT (ref);
140 for (size_t i = 1; i < c.size(); ++i) {
142 if (c[i]->video->size() != ref->size()) {
143 throw JoinError (_("Content to be joined must have the same picture size."));
146 if (c[i]->video->frame_type() != ref->frame_type()) {
147 throw JoinError (_("Content to be joined must have the same video frame type."));
150 if (c[i]->video->crop() != ref->crop()) {
151 throw JoinError (_("Content to be joined must have the same crop."));
154 if (c[i]->video->scale() != ref->scale()) {
155 throw JoinError (_("Content to be joined must have the same scale setting."));
158 if (c[i]->video->colour_conversion() != ref->colour_conversion()) {
159 throw JoinError (_("Content to be joined must have the same colour conversion."));
162 if (c[i]->video->fade_in() != ref->fade_in() || c[i]->video->fade_out() != ref->fade_out()) {
163 throw JoinError (_("Content to be joined must have the same fades."));
166 _length += c[i]->video->length ();
168 if (c[i]->video->yuv ()) {
173 _size = ref->size ();
174 _frame_type = ref->frame_type ();
175 _crop = ref->crop ();
176 _scale = ref->scale ();
177 _colour_conversion = ref->colour_conversion ();
178 _fade_in = ref->fade_in ();
179 _fade_out = ref->fade_out ();
183 VideoContent::as_xml (xmlpp::Node* node) const
185 boost::mutex::scoped_lock lm (_mutex);
186 node->add_child("VideoLength")->add_child_text (raw_convert<string> (_length));
187 node->add_child("VideoWidth")->add_child_text (raw_convert<string> (_size.width));
188 node->add_child("VideoHeight")->add_child_text (raw_convert<string> (_size.height));
189 node->add_child("VideoFrameType")->add_child_text (raw_convert<string> (static_cast<int> (_frame_type)));
190 if (_sample_aspect_ratio) {
191 node->add_child("SampleAspectRatio")->add_child_text (raw_convert<string> (_sample_aspect_ratio.get ()));
194 _scale.as_xml (node->add_child("Scale"));
195 if (_colour_conversion) {
196 _colour_conversion.get().as_xml (node->add_child("ColourConversion"));
198 node->add_child("YUV")->add_child_text (_yuv ? "1" : "0");
199 node->add_child("FadeIn")->add_child_text (raw_convert<string> (_fade_in));
200 node->add_child("FadeOut")->add_child_text (raw_convert<string> (_fade_out));
204 VideoContent::take_from_examiner (shared_ptr<VideoExaminer> d)
206 /* These examiner calls could call other content methods which take a lock on the mutex */
207 dcp::Size const vs = d->video_size ();
208 Frame vl = d->video_length ();
209 optional<double> const ar = d->sample_aspect_ratio ();
210 bool const yuv = d->yuv ();
213 boost::mutex::scoped_lock lm (_mutex);
216 _sample_aspect_ratio = ar;
219 /* Guess correct scale from size and sample aspect ratio */
220 _scale = VideoContentScale (
221 Ratio::nearest_from_ratio (double (_size.width) * ar.get_value_or (1) / _size.height)
225 shared_ptr<const Film> film = _film.lock ();
226 DCPOMATIC_ASSERT (film);
227 LOG_GENERAL ("Video length obtained from header as %1 frames", _length);
229 if (d->video_frame_rate()) {
230 _parent->set_video_frame_rate (d->video_frame_rate().get());
233 _parent->signal_changed (VideoContentProperty::SIZE);
234 _parent->signal_changed (VideoContentProperty::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"),
263 length_after_3d_combine(),
268 if (sample_aspect_ratio ()) {
269 s += String::compose (N_(", sample aspect ratio %1"), (sample_aspect_ratio().get ()));
276 VideoContent::size_after_3d_split () const
278 dcp::Size const s = size ();
279 switch (frame_type ()) {
280 case VIDEO_FRAME_TYPE_2D:
281 case VIDEO_FRAME_TYPE_3D_ALTERNATE:
282 case VIDEO_FRAME_TYPE_3D_LEFT:
283 case VIDEO_FRAME_TYPE_3D_RIGHT:
285 case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
286 return dcp::Size (s.width / 2, s.height);
287 case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
288 return dcp::Size (s.width, s.height / 2);
291 DCPOMATIC_ASSERT (false);
294 /** @return Video size after 3D split and crop */
296 VideoContent::size_after_crop () const
298 return crop().apply (size_after_3d_split ());
302 VideoContent::scale_and_crop_to_fit_width ()
304 shared_ptr<const Film> film = _film.lock ();
305 DCPOMATIC_ASSERT (film);
306 set_scale (VideoContentScale (film->container ()));
308 int const crop = max (0, int (size().height - double (film->frame_size().height) * size().width / film->frame_size().width));
311 set_top_crop (crop / 2);
312 set_bottom_crop (crop / 2);
316 VideoContent::scale_and_crop_to_fit_height ()
318 shared_ptr<const Film> film = _film.lock ();
319 DCPOMATIC_ASSERT (film);
320 set_scale (VideoContentScale (film->container ()));
322 int const crop = max (0, int (size().width - double (film->frame_size().width) * size().height / film->frame_size().height));
323 set_left_crop (crop / 2);
324 set_right_crop (crop / 2);
329 /** @param f Frame index within the whole (untrimmed) content */
331 VideoContent::fade (Frame f) const
333 DCPOMATIC_ASSERT (f >= 0);
335 shared_ptr<const Film> film = _film.lock ();
336 DCPOMATIC_ASSERT (film);
338 double const vfr = _parent->active_video_frame_rate ();
340 Frame const ts = _parent->trim_start().frames_round(vfr);
341 if ((f - ts) < fade_in()) {
342 return double (f - ts) / fade_in();
345 Frame fade_out_start = length() - _parent->trim_end().frames_round(vfr) - fade_out();
346 if (f >= fade_out_start) {
347 return 1 - double (f - fade_out_start) / fade_out();
350 return optional<double> ();
354 VideoContent::processing_description () const
356 /* stringstream is OK here as this string is just for presentation to the user */
359 if (size().width && size().height) {
360 d << String::compose (
361 _("Content video is %1x%2"),
362 size_after_3d_split().width,
363 size_after_3d_split().height
367 double ratio = size_after_3d_split().ratio ();
369 if (sample_aspect_ratio ()) {
370 d << ", " << _("pixel aspect ratio") << " " << fixed << setprecision(2) << sample_aspect_ratio().get () << ":1";
371 ratio *= sample_aspect_ratio().get ();
374 d << "\n" << _("Display aspect ratio") << " " << fixed << setprecision(2) << ratio << ":1\n";
377 if ((crop().left || crop().right || crop().top || crop().bottom) && size() != dcp::Size (0, 0)) {
378 dcp::Size cropped = size_after_crop ();
379 d << String::compose (
380 _("Cropped to %1x%2"),
381 cropped.width, cropped.height
384 d << " (" << fixed << setprecision(2) << cropped.ratio () << ":1)\n";
387 shared_ptr<const Film> film = _film.lock ();
388 DCPOMATIC_ASSERT (film);
389 dcp::Size const container_size = film->frame_size ();
390 dcp::Size const scaled = scale().size (shared_from_this(), container_size, container_size);
392 if (scaled != size_after_crop ()) {
393 d << String::compose (
394 _("Scaled to %1x%2"),
395 scaled.width, scaled.height
398 d << " (" << fixed << setprecision(2) << scaled.ratio() << ":1)\n";
401 if (scaled != container_size) {
402 d << String::compose (
403 _("Padded with black to fit container %1 (%2x%3)"),
404 film->container()->nickname (),
405 container_size.width, container_size.height
408 d << " (" << fixed << setprecision(2) << container_size.ratio () << ":1)\n";
415 VideoContent::add_properties (list<UserProperty>& p) const
417 p.push_back (UserProperty (_("Video"), _("Length"), raw_convert<string> (length ()), _("video frames")));
418 p.push_back (UserProperty (_("Video"), _("Size"), raw_convert<string> (size().width) + "x" + raw_convert<string> (size().height)));
422 VideoContent::set_length (Frame len)
424 maybe_set (_length, len, ContentProperty::LENGTH);
428 VideoContent::set_left_crop (int c)
430 maybe_set (_crop.left, c, VideoContentProperty::CROP);
434 VideoContent::set_right_crop (int c)
436 maybe_set (_crop.right, c, VideoContentProperty::CROP);
440 VideoContent::set_top_crop (int c)
442 maybe_set (_crop.top, c, VideoContentProperty::CROP);
446 VideoContent::set_bottom_crop (int c)
448 maybe_set (_crop.bottom, c, VideoContentProperty::CROP);
452 VideoContent::set_scale (VideoContentScale s)
454 maybe_set (_scale, s, VideoContentProperty::SCALE);
458 VideoContent::set_frame_type (VideoFrameType t)
460 maybe_set (_frame_type, t, VideoContentProperty::FRAME_TYPE);
464 VideoContent::unset_colour_conversion ()
466 maybe_set (_colour_conversion, boost::optional<ColourConversion> (), VideoContentProperty::COLOUR_CONVERSION);
470 VideoContent::set_colour_conversion (ColourConversion c)
472 maybe_set (_colour_conversion, c, VideoContentProperty::COLOUR_CONVERSION);
476 VideoContent::set_fade_in (Frame t)
478 maybe_set (_fade_in, t, VideoContentProperty::FADE_IN);
482 VideoContent::set_fade_out (Frame t)
484 maybe_set (_fade_out, t, VideoContentProperty::FADE_OUT);