Allow content parts to not be preset in XML.
[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(...) 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, shared_ptr<const Film> film)
67         : ContentPart (parent, film)
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, shared_ptr<const Film> film, 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, film, node, version));
86 }
87
88 VideoContent::VideoContent (Content* parent, shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
89         : ContentPart (parent, film)
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, shared_ptr<const Film> film, vector<shared_ptr<Content> > c)
133         : ContentPart (parent, film)
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         shared_ptr<const Film> film = _film.lock ();
226         DCPOMATIC_ASSERT (film);
227         LOG_GENERAL ("Video length obtained from header as %1 frames", _length);
228
229         if (d->video_frame_rate()) {
230                 _parent->set_video_frame_rate (d->video_frame_rate().get());
231         }
232
233         _parent->signal_changed (VideoContentProperty::SIZE);
234         _parent->signal_changed (VideoContentProperty::SCALE);
235         _parent->signal_changed (ContentProperty::LENGTH);
236 }
237
238 /** @return string which includes everything about how this content looks */
239 string
240 VideoContent::identifier () const
241 {
242         SafeStringStream s;
243         s << crop().left
244           << "_" << crop().right
245           << "_" << crop().top
246           << "_" << crop().bottom
247           << "_" << scale().id()
248           << "_" << _fade_in
249           << "_" << _fade_out;
250
251         if (colour_conversion()) {
252                 s << "_" << colour_conversion().get().identifier ();
253         }
254
255         return s.str ();
256 }
257
258 string
259 VideoContent::technical_summary () const
260 {
261         string s = String::compose (
262                 N_("video: length %1 frames, size %2x%3"),
263                 length_after_3d_combine(),
264                 size().width,
265                 size().height
266                 );
267
268         if (sample_aspect_ratio ()) {
269                 s += String::compose (N_(", sample aspect ratio %1"), (sample_aspect_ratio().get ()));
270         }
271
272         return s;
273 }
274
275 dcp::Size
276 VideoContent::size_after_3d_split () const
277 {
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:
284                 return s;
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);
289         }
290
291         DCPOMATIC_ASSERT (false);
292 }
293
294 /** @return Video size after 3D split and crop */
295 dcp::Size
296 VideoContent::size_after_crop () const
297 {
298         return crop().apply (size_after_3d_split ());
299 }
300
301 void
302 VideoContent::scale_and_crop_to_fit_width ()
303 {
304         shared_ptr<const Film> film = _film.lock ();
305         DCPOMATIC_ASSERT (film);
306         set_scale (VideoContentScale (film->container ()));
307
308         int const crop = max (0, int (size().height - double (film->frame_size().height) * size().width / film->frame_size().width));
309         set_left_crop (0);
310         set_right_crop (0);
311         set_top_crop (crop / 2);
312         set_bottom_crop (crop / 2);
313 }
314
315 void
316 VideoContent::scale_and_crop_to_fit_height ()
317 {
318         shared_ptr<const Film> film = _film.lock ();
319         DCPOMATIC_ASSERT (film);
320         set_scale (VideoContentScale (film->container ()));
321
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);
325         set_top_crop (0);
326         set_bottom_crop (0);
327 }
328
329 /** @param f Frame index within the whole (untrimmed) content */
330 optional<double>
331 VideoContent::fade (Frame f) const
332 {
333         DCPOMATIC_ASSERT (f >= 0);
334
335         shared_ptr<const Film> film = _film.lock ();
336         DCPOMATIC_ASSERT (film);
337
338         double const vfr = _parent->active_video_frame_rate ();
339
340         Frame const ts = _parent->trim_start().frames_round(vfr);
341         if ((f - ts) < fade_in()) {
342                 return double (f - ts) / fade_in();
343         }
344
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();
348         }
349
350         return optional<double> ();
351 }
352
353 string
354 VideoContent::processing_description () const
355 {
356         /* stringstream is OK here as this string is just for presentation to the user */
357         SafeStringStream d;
358
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
364                         );
365
366
367                 double ratio = size_after_3d_split().ratio ();
368
369                 if (sample_aspect_ratio ()) {
370                         d << ", " << _("pixel aspect ratio") << " " << fixed << setprecision(2) << sample_aspect_ratio().get () << ":1";
371                         ratio *= sample_aspect_ratio().get ();
372                 }
373
374                 d << "\n" << _("Display aspect ratio") << " " << fixed << setprecision(2) << ratio << ":1\n";
375         }
376
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
382                         );
383
384                 d << " (" << fixed << setprecision(2) << cropped.ratio () << ":1)\n";
385         }
386
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);
391
392         if (scaled != size_after_crop ()) {
393                 d << String::compose (
394                         _("Scaled to %1x%2"),
395                         scaled.width, scaled.height
396                         );
397
398                 d << " (" << fixed << setprecision(2) << scaled.ratio() << ":1)\n";
399         }
400
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
406                         );
407
408                 d << " (" << fixed << setprecision(2) << container_size.ratio () << ":1)\n";
409         }
410
411         return d.str ();
412 }
413
414 void
415 VideoContent::add_properties (list<UserProperty>& p) const
416 {
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)));
419 }
420
421 void
422 VideoContent::set_length (Frame len)
423 {
424         maybe_set (_length, len, ContentProperty::LENGTH);
425 }
426
427 void
428 VideoContent::set_left_crop (int c)
429 {
430         maybe_set (_crop.left, c, VideoContentProperty::CROP);
431 }
432
433 void
434 VideoContent::set_right_crop (int c)
435 {
436         maybe_set (_crop.right, c, VideoContentProperty::CROP);
437 }
438
439 void
440 VideoContent::set_top_crop (int c)
441 {
442         maybe_set (_crop.top, c, VideoContentProperty::CROP);
443 }
444
445 void
446 VideoContent::set_bottom_crop (int c)
447 {
448         maybe_set (_crop.bottom, c, VideoContentProperty::CROP);
449 }
450
451 void
452 VideoContent::set_scale (VideoContentScale s)
453 {
454         maybe_set (_scale, s, VideoContentProperty::SCALE);
455 }
456
457 void
458 VideoContent::set_frame_type (VideoFrameType t)
459 {
460         maybe_set (_frame_type, t, VideoContentProperty::FRAME_TYPE);
461 }
462
463 void
464 VideoContent::unset_colour_conversion ()
465 {
466         maybe_set (_colour_conversion, boost::optional<ColourConversion> (), VideoContentProperty::COLOUR_CONVERSION);
467 }
468
469 void
470 VideoContent::set_colour_conversion (ColourConversion c)
471 {
472         maybe_set (_colour_conversion, c, VideoContentProperty::COLOUR_CONVERSION);
473 }
474
475 void
476 VideoContent::set_fade_in (Frame t)
477 {
478         maybe_set (_fade_in, t, VideoContentProperty::FADE_IN);
479 }
480
481 void
482 VideoContent::set_fade_out (Frame t)
483 {
484         maybe_set (_fade_out, t, VideoContentProperty::FADE_OUT);
485 }