967522e3645166d8455b834060947d4b673db121
[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 "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
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
144         if (node->optional_node_child ("ColourConversion")) {
145                 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"), version);
146         }
147
148         _yuv = node->optional_bool_child("YUV").get_value_or (true);
149
150         if (version >= 32) {
151                 _fade_in = node->number_child<Frame> ("FadeIn");
152                 _fade_out = node->number_child<Frame> ("FadeOut");
153         } else {
154                 _fade_in = _fade_out = 0;
155         }
156 }
157
158 VideoContent::VideoContent (Content* parent, vector<shared_ptr<Content> > c)
159         : ContentPart (parent)
160         , _length (0)
161         , _yuv (false)
162 {
163         shared_ptr<VideoContent> ref = c[0]->video;
164         DCPOMATIC_ASSERT (ref);
165
166         for (size_t i = 1; i < c.size(); ++i) {
167
168                 if (c[i]->video->size() != ref->size()) {
169                         throw JoinError (_("Content to be joined must have the same picture size."));
170                 }
171
172                 if (c[i]->video->frame_type() != ref->frame_type()) {
173                         throw JoinError (_("Content to be joined must have the same video frame type."));
174                 }
175
176                 if (c[i]->video->crop() != ref->crop()) {
177                         throw JoinError (_("Content to be joined must have the same crop."));
178                 }
179
180                 if (c[i]->video->scale() != ref->scale()) {
181                         throw JoinError (_("Content to be joined must have the same scale setting."));
182                 }
183
184                 if (c[i]->video->colour_conversion() != ref->colour_conversion()) {
185                         throw JoinError (_("Content to be joined must have the same colour conversion."));
186                 }
187
188                 if (c[i]->video->fade_in() != ref->fade_in() || c[i]->video->fade_out() != ref->fade_out()) {
189                         throw JoinError (_("Content to be joined must have the same fades."));
190                 }
191
192                 _length += c[i]->video->length ();
193
194                 if (c[i]->video->yuv ()) {
195                         _yuv = true;
196                 }
197         }
198
199         _size = ref->size ();
200         _frame_type = ref->frame_type ();
201         _crop = ref->crop ();
202         _scale = ref->scale ();
203         _colour_conversion = ref->colour_conversion ();
204         _fade_in = ref->fade_in ();
205         _fade_out = ref->fade_out ();
206 }
207
208 void
209 VideoContent::as_xml (xmlpp::Node* node) const
210 {
211         boost::mutex::scoped_lock lm (_mutex);
212         node->add_child("VideoLength")->add_child_text (raw_convert<string> (_length));
213         node->add_child("VideoWidth")->add_child_text (raw_convert<string> (_size.width));
214         node->add_child("VideoHeight")->add_child_text (raw_convert<string> (_size.height));
215         node->add_child("VideoFrameType")->add_child_text (video_frame_type_to_string (_frame_type));
216         if (_sample_aspect_ratio) {
217                 node->add_child("SampleAspectRatio")->add_child_text (raw_convert<string> (_sample_aspect_ratio.get ()));
218         }
219         _crop.as_xml (node);
220         _scale.as_xml (node->add_child("Scale"));
221         if (_colour_conversion) {
222                 _colour_conversion.get().as_xml (node->add_child("ColourConversion"));
223         }
224         node->add_child("YUV")->add_child_text (_yuv ? "1" : "0");
225         node->add_child("FadeIn")->add_child_text (raw_convert<string> (_fade_in));
226         node->add_child("FadeOut")->add_child_text (raw_convert<string> (_fade_out));
227 }
228
229 void
230 VideoContent::take_from_examiner (shared_ptr<VideoExaminer> d)
231 {
232         /* These examiner calls could call other content methods which take a lock on the mutex */
233         dcp::Size const vs = d->video_size ();
234         Frame vl = d->video_length ();
235         optional<double> const ar = d->sample_aspect_ratio ();
236         bool const yuv = d->yuv ();
237
238         {
239                 boost::mutex::scoped_lock lm (_mutex);
240                 _size = vs;
241                 _length = vl;
242                 _sample_aspect_ratio = ar;
243                 _yuv = yuv;
244
245                 /* Guess correct scale from size and sample aspect ratio */
246                 _scale = VideoContentScale (
247                         Ratio::nearest_from_ratio (double (_size.width) * ar.get_value_or (1) / _size.height)
248                         );
249         }
250
251         LOG_GENERAL ("Video length obtained from header as %1 frames", _length);
252
253         if (d->video_frame_rate()) {
254                 _parent->set_video_frame_rate (d->video_frame_rate().get());
255         }
256
257         _parent->signal_changed (VideoContentProperty::SIZE);
258         _parent->signal_changed (VideoContentProperty::SCALE);
259         _parent->signal_changed (ContentProperty::LENGTH);
260 }
261
262 /** @return string which includes everything about how this content looks */
263 string
264 VideoContent::identifier () const
265 {
266         char buffer[256];
267         snprintf (
268                 buffer, sizeof(buffer), "%d_%d_%d_%d_%s_%" PRId64 "_%" PRId64,
269                 crop().left,
270                 crop().right,
271                 crop().top,
272                 crop().bottom,
273                 scale().id().c_str(),
274                 _fade_in,
275                 _fade_out
276                 );
277
278         string s (buffer);
279
280         if (colour_conversion()) {
281                 s += "_" + colour_conversion().get().identifier ();
282         }
283
284         return s;
285 }
286
287 string
288 VideoContent::technical_summary () const
289 {
290         string s = String::compose (
291                 N_("video: length %1 frames, size %2x%3"),
292                 length_after_3d_combine(),
293                 size().width,
294                 size().height
295                 );
296
297         if (sample_aspect_ratio ()) {
298                 s += String::compose (N_(", sample aspect ratio %1"), (sample_aspect_ratio().get ()));
299         }
300
301         return s;
302 }
303
304 dcp::Size
305 VideoContent::size_after_3d_split () const
306 {
307         dcp::Size const s = size ();
308         switch (frame_type ()) {
309         case VIDEO_FRAME_TYPE_2D:
310         case VIDEO_FRAME_TYPE_3D:
311         case VIDEO_FRAME_TYPE_3D_ALTERNATE:
312         case VIDEO_FRAME_TYPE_3D_LEFT:
313         case VIDEO_FRAME_TYPE_3D_RIGHT:
314                 return s;
315         case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
316                 return dcp::Size (s.width / 2, s.height);
317         case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
318                 return dcp::Size (s.width, s.height / 2);
319         }
320
321         DCPOMATIC_ASSERT (false);
322 }
323
324 /** @return Video size after 3D split and crop */
325 dcp::Size
326 VideoContent::size_after_crop () const
327 {
328         return crop().apply (size_after_3d_split ());
329 }
330
331 void
332 VideoContent::scale_and_crop_to_fit_width ()
333 {
334         shared_ptr<const Film> film = _parent->film ();
335         set_scale (VideoContentScale (film->container ()));
336
337         int const crop = max (0, int (size().height - double (film->frame_size().height) * size().width / film->frame_size().width));
338         set_left_crop (0);
339         set_right_crop (0);
340         set_top_crop (crop / 2);
341         set_bottom_crop (crop / 2);
342 }
343
344 void
345 VideoContent::scale_and_crop_to_fit_height ()
346 {
347         shared_ptr<const Film> film = _parent->film ();
348         set_scale (VideoContentScale (film->container ()));
349
350         int const crop = max (0, int (size().width - double (film->frame_size().width) * size().height / film->frame_size().height));
351         set_left_crop (crop / 2);
352         set_right_crop (crop / 2);
353         set_top_crop (0);
354         set_bottom_crop (0);
355 }
356
357 /** @param f Frame index within the whole (untrimmed) content */
358 optional<double>
359 VideoContent::fade (Frame f) const
360 {
361         DCPOMATIC_ASSERT (f >= 0);
362
363         shared_ptr<const Film> film = _parent->film ();
364
365         double const vfr = _parent->active_video_frame_rate ();
366
367         Frame const ts = _parent->trim_start().frames_round(vfr);
368         if ((f - ts) < fade_in()) {
369                 return double (f - ts) / fade_in();
370         }
371
372         Frame fade_out_start = length() - _parent->trim_end().frames_round(vfr) - fade_out();
373         if (f >= fade_out_start) {
374                 return 1 - double (f - fade_out_start) / fade_out();
375         }
376
377         return optional<double> ();
378 }
379
380 string
381 VideoContent::processing_description () const
382 {
383         string d;
384         char buffer[256];
385
386         if (size().width && size().height) {
387                 d += String::compose (
388                         _("Content video is %1x%2"),
389                         size_after_3d_split().width,
390                         size_after_3d_split().height
391                         );
392
393
394                 double ratio = size_after_3d_split().ratio ();
395
396                 if (sample_aspect_ratio ()) {
397                         snprintf (buffer, sizeof(buffer), _(", pixel aspect ratio %.2f:1"), sample_aspect_ratio().get());
398                         d += buffer;
399                         ratio *= sample_aspect_ratio().get ();
400                 }
401
402                 snprintf (buffer, sizeof(buffer), _("\nDisplay aspect ratio %.2f:1"), ratio);
403                 d += buffer;
404         }
405
406         if ((crop().left || crop().right || crop().top || crop().bottom) && size() != dcp::Size (0, 0)) {
407                 dcp::Size cropped = size_after_crop ();
408                 d += String::compose (
409                         _("Cropped to %1x%2"),
410                         cropped.width, cropped.height
411                         );
412
413                 snprintf (buffer, sizeof(buffer), " (%.2f:1)\n", cropped.ratio());
414                 d += buffer;
415         }
416
417         shared_ptr<const Film> film = _parent->film ();
418         dcp::Size const container_size = film->frame_size ();
419         dcp::Size const scaled = scale().size (shared_from_this(), container_size, container_size);
420
421         if (scaled != size_after_crop ()) {
422                 d += String::compose (
423                         _("Scaled to %1x%2"),
424                         scaled.width, scaled.height
425                         );
426
427                 snprintf (buffer, sizeof(buffer), _(" (%.2f:1)\n"), scaled.ratio());
428                 d += buffer;
429         }
430
431         if (scaled != container_size) {
432                 d += String::compose (
433                         _("Padded with black to fit container %1 (%2x%3)"),
434                         film->container()->nickname (),
435                         container_size.width, container_size.height
436                         );
437
438                 snprintf (buffer, sizeof(buffer), _(" (%.2f:1)\n"), container_size.ratio());
439                 d += buffer;
440         }
441
442         if (_parent->video_frame_rate()) {
443                 double const vfr = _parent->video_frame_rate().get ();
444
445                 snprintf (buffer, sizeof(buffer), _("Content frame rate %.4f\n"), vfr);
446                 d += buffer;
447
448                 FrameRateChange frc (vfr, film->video_frame_rate ());
449                 d += frc.description () + "\n";
450         }
451
452         return d;
453 }
454
455 void
456 VideoContent::add_properties (list<UserProperty>& p) const
457 {
458         p.push_back (UserProperty (UserProperty::VIDEO, _("Length"), raw_convert<string> (length ()), _("video frames")));
459         p.push_back (UserProperty (UserProperty::VIDEO, _("Size"), raw_convert<string> (size().width) + "x" + raw_convert<string> (size().height)));
460 }
461
462 void
463 VideoContent::set_length (Frame len)
464 {
465         maybe_set (_length, len, ContentProperty::LENGTH);
466 }
467
468 void
469 VideoContent::set_left_crop (int c)
470 {
471         maybe_set (_crop.left, c, VideoContentProperty::CROP);
472 }
473
474 void
475 VideoContent::set_right_crop (int c)
476 {
477         maybe_set (_crop.right, c, VideoContentProperty::CROP);
478 }
479
480 void
481 VideoContent::set_top_crop (int c)
482 {
483         maybe_set (_crop.top, c, VideoContentProperty::CROP);
484 }
485
486 void
487 VideoContent::set_bottom_crop (int c)
488 {
489         maybe_set (_crop.bottom, c, VideoContentProperty::CROP);
490 }
491
492 void
493 VideoContent::set_scale (VideoContentScale s)
494 {
495         maybe_set (_scale, s, VideoContentProperty::SCALE);
496 }
497
498 void
499 VideoContent::set_frame_type (VideoFrameType t)
500 {
501         maybe_set (_frame_type, t, VideoContentProperty::FRAME_TYPE);
502 }
503
504 void
505 VideoContent::unset_colour_conversion ()
506 {
507         maybe_set (_colour_conversion, boost::optional<ColourConversion> (), VideoContentProperty::COLOUR_CONVERSION);
508 }
509
510 void
511 VideoContent::set_colour_conversion (ColourConversion c)
512 {
513         maybe_set (_colour_conversion, c, VideoContentProperty::COLOUR_CONVERSION);
514 }
515
516 void
517 VideoContent::set_fade_in (Frame t)
518 {
519         maybe_set (_fade_in, t, VideoContentProperty::FADE_IN);
520 }
521
522 void
523 VideoContent::set_fade_out (Frame t)
524 {
525         maybe_set (_fade_out, t, VideoContentProperty::FADE_OUT);
526 }