fce90ad7bded1913f8bf0a93a5d62a93f85e32df
[dcpomatic.git] / src / lib / video_content.cc
1 /*
2     Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include "video_content.h"
21 #include "content.h"
22 #include "video_examiner.h"
23 #include "compose.hpp"
24 #include "ratio.h"
25 #include "config.h"
26 #include "colour_conversion.h"
27 #include "util.h"
28 #include "film.h"
29 #include "exceptions.h"
30 #include "frame_rate_change.h"
31 #include "log.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>
37 #include <iomanip>
38 #include <iostream>
39
40 #include "i18n.h"
41
42 #define LOG_GENERAL(...) _parent->film()->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL);
43
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;
51
52 using std::string;
53 using std::setprecision;
54 using std::cout;
55 using std::vector;
56 using std::min;
57 using std::max;
58 using std::fixed;
59 using std::setprecision;
60 using std::list;
61 using std::pair;
62 using boost::shared_ptr;
63 using boost::optional;
64 using boost::dynamic_pointer_cast;
65
66 VideoContent::VideoContent (Content* parent)
67         : ContentPart (parent)
68         , _length (0)
69         , _frame_type (VIDEO_FRAME_TYPE_2D)
70         , _scale (VideoContentScale (Ratio::from_id ("178")))
71         , _yuv (true)
72         , _fade_in (0)
73         , _fade_out (0)
74 {
75
76 }
77
78 shared_ptr<VideoContent>
79 VideoContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version)
80 {
81         if (!node->optional_number_child<int> ("VideoWidth")) {
82                 return shared_ptr<VideoContent> ();
83         }
84
85         return shared_ptr<VideoContent> (new VideoContent (parent, node, version));
86 }
87
88 VideoContent::VideoContent (Content* parent, cxml::ConstNodePtr node, int version)
89         : ContentPart (parent)
90 {
91         _size.width = node->number_child<int> ("VideoWidth");
92         _size.height = node->number_child<int> ("VideoHeight");
93
94         /* Backwards compatibility */
95         optional<double> r = node->optional_number_child<double>("VideoFrameRate");
96         if (r) {
97                 _parent->set_video_frame_rate (r.get ());
98         }
99
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");
107
108         if (version <= 7) {
109                 optional<string> r = node->optional_string_child ("Ratio");
110                 if (r) {
111                         _scale = VideoContentScale (Ratio::from_id (r.get ()));
112                 }
113         } else {
114                 _scale = VideoContentScale (node->node_child ("Scale"));
115         }
116
117
118         if (node->optional_node_child ("ColourConversion")) {
119                 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"), version);
120         }
121
122         _yuv = node->optional_bool_child("YUV").get_value_or (true);
123
124         if (version >= 32) {
125                 _fade_in = node->number_child<Frame> ("FadeIn");
126                 _fade_out = node->number_child<Frame> ("FadeOut");
127         } else {
128                 _fade_in = _fade_out = 0;
129         }
130 }
131
132 VideoContent::VideoContent (Content* parent, vector<shared_ptr<Content> > c)
133         : ContentPart (parent)
134         , _length (0)
135         , _yuv (false)
136 {
137         shared_ptr<VideoContent> ref = c[0]->video;
138         DCPOMATIC_ASSERT (ref);
139
140         for (size_t i = 1; i < c.size(); ++i) {
141
142                 if (c[i]->video->size() != ref->size()) {
143                         throw JoinError (_("Content to be joined must have the same picture size."));
144                 }
145
146                 if (c[i]->video->frame_type() != ref->frame_type()) {
147                         throw JoinError (_("Content to be joined must have the same video frame type."));
148                 }
149
150                 if (c[i]->video->crop() != ref->crop()) {
151                         throw JoinError (_("Content to be joined must have the same crop."));
152                 }
153
154                 if (c[i]->video->scale() != ref->scale()) {
155                         throw JoinError (_("Content to be joined must have the same scale setting."));
156                 }
157
158                 if (c[i]->video->colour_conversion() != ref->colour_conversion()) {
159                         throw JoinError (_("Content to be joined must have the same colour conversion."));
160                 }
161
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."));
164                 }
165
166                 _length += c[i]->video->length ();
167
168                 if (c[i]->video->yuv ()) {
169                         _yuv = true;
170                 }
171         }
172
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 ();
180 }
181
182 void
183 VideoContent::as_xml (xmlpp::Node* node) const
184 {
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 ()));
192         }
193         _crop.as_xml (node);
194         _scale.as_xml (node->add_child("Scale"));
195         if (_colour_conversion) {
196                 _colour_conversion.get().as_xml (node->add_child("ColourConversion"));
197         }
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));
201 }
202
203 void
204 VideoContent::take_from_examiner (shared_ptr<VideoExaminer> d)
205 {
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 ();
211
212         {
213                 boost::mutex::scoped_lock lm (_mutex);
214                 _size = vs;
215                 _length = vl;
216                 _sample_aspect_ratio = ar;
217                 _yuv = yuv;
218
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)
222                         );
223         }
224
225         LOG_GENERAL ("Video length obtained from header as %1 frames", _length);
226
227         if (d->video_frame_rate()) {
228                 _parent->set_video_frame_rate (d->video_frame_rate().get());
229         }
230
231         _parent->signal_changed (VideoContentProperty::SIZE);
232         _parent->signal_changed (VideoContentProperty::SCALE);
233         _parent->signal_changed (ContentProperty::LENGTH);
234 }
235
236 /** @return string which includes everything about how this content looks */
237 string
238 VideoContent::identifier () const
239 {
240         SafeStringStream s;
241         s << crop().left
242           << "_" << crop().right
243           << "_" << crop().top
244           << "_" << crop().bottom
245           << "_" << scale().id()
246           << "_" << _fade_in
247           << "_" << _fade_out;
248
249         if (colour_conversion()) {
250                 s << "_" << colour_conversion().get().identifier ();
251         }
252
253         return s.str ();
254 }
255
256 string
257 VideoContent::technical_summary () const
258 {
259         string s = String::compose (
260                 N_("video: length %1 frames, size %2x%3"),
261                 length_after_3d_combine(),
262                 size().width,
263                 size().height
264                 );
265
266         if (sample_aspect_ratio ()) {
267                 s += String::compose (N_(", sample aspect ratio %1"), (sample_aspect_ratio().get ()));
268         }
269
270         return s;
271 }
272
273 dcp::Size
274 VideoContent::size_after_3d_split () const
275 {
276         dcp::Size const s = size ();
277         switch (frame_type ()) {
278         case VIDEO_FRAME_TYPE_2D:
279         case VIDEO_FRAME_TYPE_3D_ALTERNATE:
280         case VIDEO_FRAME_TYPE_3D_LEFT:
281         case VIDEO_FRAME_TYPE_3D_RIGHT:
282                 return s;
283         case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
284                 return dcp::Size (s.width / 2, s.height);
285         case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
286                 return dcp::Size (s.width, s.height / 2);
287         }
288
289         DCPOMATIC_ASSERT (false);
290 }
291
292 /** @return Video size after 3D split and crop */
293 dcp::Size
294 VideoContent::size_after_crop () const
295 {
296         return crop().apply (size_after_3d_split ());
297 }
298
299 void
300 VideoContent::scale_and_crop_to_fit_width ()
301 {
302         shared_ptr<const Film> film = _parent->film ();
303         set_scale (VideoContentScale (film->container ()));
304
305         int const crop = max (0, int (size().height - double (film->frame_size().height) * size().width / film->frame_size().width));
306         set_left_crop (0);
307         set_right_crop (0);
308         set_top_crop (crop / 2);
309         set_bottom_crop (crop / 2);
310 }
311
312 void
313 VideoContent::scale_and_crop_to_fit_height ()
314 {
315         shared_ptr<const Film> film = _parent->film ();
316         set_scale (VideoContentScale (film->container ()));
317
318         int const crop = max (0, int (size().width - double (film->frame_size().width) * size().height / film->frame_size().height));
319         set_left_crop (crop / 2);
320         set_right_crop (crop / 2);
321         set_top_crop (0);
322         set_bottom_crop (0);
323 }
324
325 /** @param f Frame index within the whole (untrimmed) content */
326 optional<double>
327 VideoContent::fade (Frame f) const
328 {
329         DCPOMATIC_ASSERT (f >= 0);
330
331         shared_ptr<const Film> film = _parent->film ();
332
333         double const vfr = _parent->active_video_frame_rate ();
334
335         Frame const ts = _parent->trim_start().frames_round(vfr);
336         if ((f - ts) < fade_in()) {
337                 return double (f - ts) / fade_in();
338         }
339
340         Frame fade_out_start = length() - _parent->trim_end().frames_round(vfr) - fade_out();
341         if (f >= fade_out_start) {
342                 return 1 - double (f - fade_out_start) / fade_out();
343         }
344
345         return optional<double> ();
346 }
347
348 string
349 VideoContent::processing_description () const
350 {
351         /* stringstream is OK here as this string is just for presentation to the user */
352         SafeStringStream d;
353
354         if (size().width && size().height) {
355                 d << String::compose (
356                         _("Content video is %1x%2"),
357                         size_after_3d_split().width,
358                         size_after_3d_split().height
359                         );
360
361
362                 double ratio = size_after_3d_split().ratio ();
363
364                 if (sample_aspect_ratio ()) {
365                         d << ", " << _("pixel aspect ratio") << " " << fixed << setprecision(2) << sample_aspect_ratio().get () << ":1";
366                         ratio *= sample_aspect_ratio().get ();
367                 }
368
369                 d << "\n" << _("Display aspect ratio") << " " << fixed << setprecision(2) << ratio << ":1\n";
370         }
371
372         if ((crop().left || crop().right || crop().top || crop().bottom) && size() != dcp::Size (0, 0)) {
373                 dcp::Size cropped = size_after_crop ();
374                 d << String::compose (
375                         _("Cropped to %1x%2"),
376                         cropped.width, cropped.height
377                         );
378
379                 d << " (" << fixed << setprecision(2) << cropped.ratio () << ":1)\n";
380         }
381
382         shared_ptr<const Film> film = _parent->film ();
383         dcp::Size const container_size = film->frame_size ();
384         dcp::Size const scaled = scale().size (shared_from_this(), container_size, container_size);
385
386         if (scaled != size_after_crop ()) {
387                 d << String::compose (
388                         _("Scaled to %1x%2"),
389                         scaled.width, scaled.height
390                         );
391
392                 d << " (" << fixed << setprecision(2) << scaled.ratio() << ":1)\n";
393         }
394
395         if (scaled != container_size) {
396                 d << String::compose (
397                         _("Padded with black to fit container %1 (%2x%3)"),
398                         film->container()->nickname (),
399                         container_size.width, container_size.height
400                         );
401
402                 d << " (" << fixed << setprecision(2) << container_size.ratio () << ":1)\n";
403         }
404
405         return d.str ();
406 }
407
408 void
409 VideoContent::add_properties (list<UserProperty>& p) const
410 {
411         p.push_back (UserProperty (_("Video"), _("Length"), raw_convert<string> (length ()), _("video frames")));
412         p.push_back (UserProperty (_("Video"), _("Size"), raw_convert<string> (size().width) + "x" + raw_convert<string> (size().height)));
413 }
414
415 void
416 VideoContent::set_length (Frame len)
417 {
418         maybe_set (_length, len, ContentProperty::LENGTH);
419 }
420
421 void
422 VideoContent::set_left_crop (int c)
423 {
424         maybe_set (_crop.left, c, VideoContentProperty::CROP);
425 }
426
427 void
428 VideoContent::set_right_crop (int c)
429 {
430         maybe_set (_crop.right, c, VideoContentProperty::CROP);
431 }
432
433 void
434 VideoContent::set_top_crop (int c)
435 {
436         maybe_set (_crop.top, c, VideoContentProperty::CROP);
437 }
438
439 void
440 VideoContent::set_bottom_crop (int c)
441 {
442         maybe_set (_crop.bottom, c, VideoContentProperty::CROP);
443 }
444
445 void
446 VideoContent::set_scale (VideoContentScale s)
447 {
448         maybe_set (_scale, s, VideoContentProperty::SCALE);
449 }
450
451 void
452 VideoContent::set_frame_type (VideoFrameType t)
453 {
454         maybe_set (_frame_type, t, VideoContentProperty::FRAME_TYPE);
455 }
456
457 void
458 VideoContent::unset_colour_conversion ()
459 {
460         maybe_set (_colour_conversion, boost::optional<ColourConversion> (), VideoContentProperty::COLOUR_CONVERSION);
461 }
462
463 void
464 VideoContent::set_colour_conversion (ColourConversion c)
465 {
466         maybe_set (_colour_conversion, c, VideoContentProperty::COLOUR_CONVERSION);
467 }
468
469 void
470 VideoContent::set_fade_in (Frame t)
471 {
472         maybe_set (_fade_in, t, VideoContentProperty::FADE_IN);
473 }
474
475 void
476 VideoContent::set_fade_out (Frame t)
477 {
478         maybe_set (_fade_out, t, VideoContentProperty::FADE_OUT);
479 }