Remove some out-of-date includes.
[dcpomatic.git] / src / lib / video_content.cc
1 /*
2     Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "video_content.h"
22 #include "content.h"
23 #include "video_examiner.h"
24 #include "compose.hpp"
25 #include "ratio.h"
26 #include "config.h"
27 #include "colour_conversion.h"
28 #include "util.h"
29 #include "film.h"
30 #include "exceptions.h"
31 #include "frame_rate_change.h"
32 #include "log.h"
33 #include <dcp/raw_convert.h>
34 #include <libcxml/cxml.h>
35 #include <libxml++/libxml++.h>
36 #include <iomanip>
37 #include <iostream>
38
39 #include "i18n.h"
40
41 #define LOG_GENERAL(...) _parent->film()->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL);
42
43 int const VideoContentProperty::SIZE      = 0;
44 int const VideoContentProperty::FRAME_TYPE  = 1;
45 int const VideoContentProperty::CROP      = 2;
46 int const VideoContentProperty::SCALE     = 3;
47 int const VideoContentProperty::COLOUR_CONVERSION = 4;
48 int const VideoContentProperty::FADE_IN     = 5;
49 int const VideoContentProperty::FADE_OUT    = 6;
50
51 using std::string;
52 using std::setprecision;
53 using std::cout;
54 using std::vector;
55 using std::min;
56 using std::max;
57 using std::fixed;
58 using std::setprecision;
59 using std::list;
60 using std::pair;
61 using boost::shared_ptr;
62 using boost::optional;
63 using boost::dynamic_pointer_cast;
64 using dcp::raw_convert;
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
102         if (version <= 34) {
103                 /* Snapshot of the VideoFrameType enum at version 34 */
104                 switch (node->number_child<int> ("VideoFrameType")) {
105                 case 0:
106                         _frame_type = VIDEO_FRAME_TYPE_2D;
107                         break;
108                 case 1:
109                         _frame_type = VIDEO_FRAME_TYPE_3D_LEFT_RIGHT;
110                         break;
111                 case 2:
112                         _frame_type = VIDEO_FRAME_TYPE_3D_TOP_BOTTOM;
113                         break;
114                 case 3:
115                         _frame_type = VIDEO_FRAME_TYPE_3D_ALTERNATE;
116                         break;
117                 case 4:
118                         _frame_type = VIDEO_FRAME_TYPE_3D_LEFT;
119                         break;
120                 case 5:
121                         _frame_type = VIDEO_FRAME_TYPE_3D_RIGHT;
122                         break;
123                 }
124         } else {
125                 _frame_type = string_to_video_frame_type (node->string_child ("VideoFrameType"));
126         }
127
128         _sample_aspect_ratio = node->optional_number_child<double> ("SampleAspectRatio");
129         _crop.left = node->number_child<int> ("LeftCrop");
130         _crop.right = node->number_child<int> ("RightCrop");
131         _crop.top = node->number_child<int> ("TopCrop");
132         _crop.bottom = node->number_child<int> ("BottomCrop");
133
134         if (version <= 7) {
135                 optional<string> r = node->optional_string_child ("Ratio");
136                 if (r) {
137                         _scale = VideoContentScale (Ratio::from_id (r.get ()));
138                 }
139         } else {
140                 _scale = VideoContentScale (node->node_child ("Scale"));
141         }
142
143         if (node->optional_node_child ("ColourConversion")) {
144                 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"), version);
145         }
146
147         _yuv = node->optional_bool_child("YUV").get_value_or (true);
148
149         if (version >= 32) {
150                 _fade_in = node->number_child<Frame> ("FadeIn");
151                 _fade_out = node->number_child<Frame> ("FadeOut");
152         } else {
153                 _fade_in = _fade_out = 0;
154         }
155 }
156
157 VideoContent::VideoContent (Content* parent, vector<shared_ptr<Content> > c)
158         : ContentPart (parent)
159         , _length (0)
160         , _yuv (false)
161 {
162         shared_ptr<VideoContent> ref = c[0]->video;
163         DCPOMATIC_ASSERT (ref);
164
165         for (size_t i = 1; i < c.size(); ++i) {
166
167                 if (c[i]->video->size() != ref->size()) {
168                         throw JoinError (_("Content to be joined must have the same picture size."));
169                 }
170
171                 if (c[i]->video->frame_type() != ref->frame_type()) {
172                         throw JoinError (_("Content to be joined must have the same video frame type."));
173                 }
174
175                 if (c[i]->video->crop() != ref->crop()) {
176                         throw JoinError (_("Content to be joined must have the same crop."));
177                 }
178
179                 if (c[i]->video->scale() != ref->scale()) {
180                         throw JoinError (_("Content to be joined must have the same scale setting."));
181                 }
182
183                 if (c[i]->video->colour_conversion() != ref->colour_conversion()) {
184                         throw JoinError (_("Content to be joined must have the same colour conversion."));
185                 }
186
187                 if (c[i]->video->fade_in() != ref->fade_in() || c[i]->video->fade_out() != ref->fade_out()) {
188                         throw JoinError (_("Content to be joined must have the same fades."));
189                 }
190
191                 _length += c[i]->video->length ();
192
193                 if (c[i]->video->yuv ()) {
194                         _yuv = true;
195                 }
196         }
197
198         _size = ref->size ();
199         _frame_type = ref->frame_type ();
200         _crop = ref->crop ();
201         _scale = ref->scale ();
202         _colour_conversion = ref->colour_conversion ();
203         _fade_in = ref->fade_in ();
204         _fade_out = ref->fade_out ();
205 }
206
207 void
208 VideoContent::as_xml (xmlpp::Node* node) const
209 {
210         boost::mutex::scoped_lock lm (_mutex);
211         node->add_child("VideoLength")->add_child_text (raw_convert<string> (_length));
212         node->add_child("VideoWidth")->add_child_text (raw_convert<string> (_size.width));
213         node->add_child("VideoHeight")->add_child_text (raw_convert<string> (_size.height));
214         node->add_child("VideoFrameType")->add_child_text (video_frame_type_to_string (_frame_type));
215         if (_sample_aspect_ratio) {
216                 node->add_child("SampleAspectRatio")->add_child_text (raw_convert<string> (_sample_aspect_ratio.get ()));
217         }
218         _crop.as_xml (node);
219         _scale.as_xml (node->add_child("Scale"));
220         if (_colour_conversion) {
221                 _colour_conversion.get().as_xml (node->add_child("ColourConversion"));
222         }
223         node->add_child("YUV")->add_child_text (_yuv ? "1" : "0");
224         node->add_child("FadeIn")->add_child_text (raw_convert<string> (_fade_in));
225         node->add_child("FadeOut")->add_child_text (raw_convert<string> (_fade_out));
226 }
227
228 void
229 VideoContent::take_from_examiner (shared_ptr<VideoExaminer> d)
230 {
231         /* These examiner calls could call other content methods which take a lock on the mutex */
232         dcp::Size const vs = d->video_size ();
233         Frame vl = d->video_length ();
234         optional<double> const ar = d->sample_aspect_ratio ();
235         bool const yuv = d->yuv ();
236
237         {
238                 boost::mutex::scoped_lock lm (_mutex);
239                 _size = vs;
240                 _length = vl;
241                 _sample_aspect_ratio = ar;
242                 _yuv = yuv;
243
244                 /* Guess correct scale from size and sample aspect ratio */
245                 _scale = VideoContentScale (
246                         Ratio::nearest_from_ratio (double (_size.width) * ar.get_value_or (1) / _size.height)
247                         );
248         }
249
250         LOG_GENERAL ("Video length obtained from header as %1 frames", _length);
251
252         if (d->video_frame_rate()) {
253                 _parent->set_video_frame_rate (d->video_frame_rate().get());
254         }
255
256         _parent->signal_changed (VideoContentProperty::SIZE);
257         _parent->signal_changed (VideoContentProperty::SCALE);
258         _parent->signal_changed (ContentProperty::LENGTH);
259 }
260
261 /** @return string which includes everything about how this content looks */
262 string
263 VideoContent::identifier () const
264 {
265         char buffer[256];
266         snprintf (
267                 buffer, sizeof(buffer), "%d_%d_%d_%d_%s_%" PRId64 "_%" PRId64,
268                 crop().left,
269                 crop().right,
270                 crop().top,
271                 crop().bottom,
272                 scale().id().c_str(),
273                 _fade_in,
274                 _fade_out
275                 );
276
277         string s (buffer);
278
279         if (colour_conversion()) {
280                 s += "_" + colour_conversion().get().identifier ();
281         }
282
283         return s;
284 }
285
286 string
287 VideoContent::technical_summary () const
288 {
289         string s = String::compose (
290                 N_("video: length %1 frames, size %2x%3"),
291                 length_after_3d_combine(),
292                 size().width,
293                 size().height
294                 );
295
296         if (sample_aspect_ratio ()) {
297                 s += String::compose (N_(", sample aspect ratio %1"), (sample_aspect_ratio().get ()));
298         }
299
300         return s;
301 }
302
303 dcp::Size
304 VideoContent::size_after_3d_split () const
305 {
306         dcp::Size const s = size ();
307         switch (frame_type ()) {
308         case VIDEO_FRAME_TYPE_2D:
309         case VIDEO_FRAME_TYPE_3D:
310         case VIDEO_FRAME_TYPE_3D_ALTERNATE:
311         case VIDEO_FRAME_TYPE_3D_LEFT:
312         case VIDEO_FRAME_TYPE_3D_RIGHT:
313                 return s;
314         case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
315                 return dcp::Size (s.width / 2, s.height);
316         case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
317                 return dcp::Size (s.width, s.height / 2);
318         }
319
320         DCPOMATIC_ASSERT (false);
321 }
322
323 /** @return Video size after 3D split and crop */
324 dcp::Size
325 VideoContent::size_after_crop () const
326 {
327         return crop().apply (size_after_3d_split ());
328 }
329
330 void
331 VideoContent::scale_and_crop_to_fit_width ()
332 {
333         shared_ptr<const Film> film = _parent->film ();
334         set_scale (VideoContentScale (film->container ()));
335
336         int const crop = max (0, int (size().height - double (film->frame_size().height) * size().width / film->frame_size().width));
337         set_left_crop (0);
338         set_right_crop (0);
339         set_top_crop (crop / 2);
340         set_bottom_crop (crop / 2);
341 }
342
343 void
344 VideoContent::scale_and_crop_to_fit_height ()
345 {
346         shared_ptr<const Film> film = _parent->film ();
347         set_scale (VideoContentScale (film->container ()));
348
349         int const crop = max (0, int (size().width - double (film->frame_size().width) * size().height / film->frame_size().height));
350         set_left_crop (crop / 2);
351         set_right_crop (crop / 2);
352         set_top_crop (0);
353         set_bottom_crop (0);
354 }
355
356 /** @param f Frame index within the whole (untrimmed) content */
357 optional<double>
358 VideoContent::fade (Frame f) const
359 {
360         DCPOMATIC_ASSERT (f >= 0);
361
362         shared_ptr<const Film> film = _parent->film ();
363
364         double const vfr = _parent->active_video_frame_rate ();
365
366         Frame const ts = _parent->trim_start().frames_round(vfr);
367         if ((f - ts) < fade_in()) {
368                 return double (f - ts) / fade_in();
369         }
370
371         Frame fade_out_start = length() - _parent->trim_end().frames_round(vfr) - fade_out();
372         if (f >= fade_out_start) {
373                 return 1 - double (f - fade_out_start) / fade_out();
374         }
375
376         return optional<double> ();
377 }
378
379 string
380 VideoContent::processing_description () const
381 {
382         string d;
383         char buffer[256];
384
385         if (size().width && size().height) {
386                 d += String::compose (
387                         _("Content video is %1x%2"),
388                         size_after_3d_split().width,
389                         size_after_3d_split().height
390                         );
391
392
393                 double ratio = size_after_3d_split().ratio ();
394
395                 if (sample_aspect_ratio ()) {
396                         snprintf (buffer, sizeof(buffer), _(", pixel aspect ratio %.2f:1"), sample_aspect_ratio().get());
397                         d += buffer;
398                         ratio *= sample_aspect_ratio().get ();
399                 }
400
401                 snprintf (buffer, sizeof(buffer), _("\nDisplay aspect ratio %.2f:1"), ratio);
402                 d += buffer;
403         }
404
405         if ((crop().left || crop().right || crop().top || crop().bottom) && size() != dcp::Size (0, 0)) {
406                 dcp::Size cropped = size_after_crop ();
407                 d += String::compose (
408                         _("\nCropped to %1x%2"),
409                         cropped.width, cropped.height
410                         );
411
412                 snprintf (buffer, sizeof(buffer), " (%.2f:1)", cropped.ratio());
413                 d += buffer;
414         }
415
416         shared_ptr<const Film> film = _parent->film ();
417         dcp::Size const container_size = film->frame_size ();
418         dcp::Size const scaled = scale().size (shared_from_this(), container_size, container_size);
419
420         if (scaled != size_after_crop ()) {
421                 d += String::compose (
422                         _("\nScaled to %1x%2"),
423                         scaled.width, scaled.height
424                         );
425
426                 snprintf (buffer, sizeof(buffer), _(" (%.2f:1)"), scaled.ratio());
427                 d += buffer;
428         }
429
430         if (scaled != container_size) {
431                 d += String::compose (
432                         _("\nPadded with black to fit container %1 (%2x%3)"),
433                         film->container()->nickname (),
434                         container_size.width, container_size.height
435                         );
436
437                 snprintf (buffer, sizeof(buffer), _(" (%.2f:1)"), container_size.ratio());
438                 d += buffer;
439         }
440
441         if (_parent->video_frame_rate()) {
442                 double const vfr = _parent->video_frame_rate().get ();
443
444                 snprintf (buffer, sizeof(buffer), _("\nContent frame rate %.4f\n"), vfr);
445                 d += buffer;
446
447                 FrameRateChange frc (vfr, film->video_frame_rate ());
448                 d += frc.description ();
449         }
450
451         return d;
452 }
453
454 void
455 VideoContent::add_properties (list<UserProperty>& p) const
456 {
457         p.push_back (UserProperty (UserProperty::VIDEO, _("Length"), length (), _("video frames")));
458         p.push_back (UserProperty (UserProperty::VIDEO, _("Size"), String::compose ("%1x%2", size().width, size().height)));
459 }
460
461 void
462 VideoContent::set_length (Frame len)
463 {
464         maybe_set (_length, len, ContentProperty::LENGTH);
465 }
466
467 void
468 VideoContent::set_left_crop (int c)
469 {
470         maybe_set (_crop.left, c, VideoContentProperty::CROP);
471 }
472
473 void
474 VideoContent::set_right_crop (int c)
475 {
476         maybe_set (_crop.right, c, VideoContentProperty::CROP);
477 }
478
479 void
480 VideoContent::set_top_crop (int c)
481 {
482         maybe_set (_crop.top, c, VideoContentProperty::CROP);
483 }
484
485 void
486 VideoContent::set_bottom_crop (int c)
487 {
488         maybe_set (_crop.bottom, c, VideoContentProperty::CROP);
489 }
490
491 void
492 VideoContent::set_scale (VideoContentScale s)
493 {
494         maybe_set (_scale, s, VideoContentProperty::SCALE);
495 }
496
497 void
498 VideoContent::set_frame_type (VideoFrameType t)
499 {
500         maybe_set (_frame_type, t, VideoContentProperty::FRAME_TYPE);
501 }
502
503 void
504 VideoContent::unset_colour_conversion ()
505 {
506         maybe_set (_colour_conversion, boost::optional<ColourConversion> (), VideoContentProperty::COLOUR_CONVERSION);
507 }
508
509 void
510 VideoContent::set_colour_conversion (ColourConversion c)
511 {
512         maybe_set (_colour_conversion, c, VideoContentProperty::COLOUR_CONVERSION);
513 }
514
515 void
516 VideoContent::set_fade_in (Frame t)
517 {
518         maybe_set (_fade_in, t, VideoContentProperty::FADE_IN);
519 }
520
521 void
522 VideoContent::set_fade_out (Frame t)
523 {
524         maybe_set (_fade_out, t, VideoContentProperty::FADE_OUT);
525 }
526
527 void
528 VideoContent::use_template (shared_ptr<const VideoContent> c)
529 {
530         _colour_conversion = c->_colour_conversion;
531         _frame_type = c->_frame_type;
532         _crop = c->_crop;
533         _scale = c->_scale;
534         _fade_in = c->_fade_in;
535         _fade_out = c->_fade_out;
536 }