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