Reasonably straightforward stuff; main things are adding
[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::VIDEO_SIZE        = 0;
45 int const VideoContentProperty::VIDEO_FRAME_RATE  = 1;
46 int const VideoContentProperty::VIDEO_FRAME_TYPE  = 2;
47 int const VideoContentProperty::VIDEO_CROP        = 3;
48 int const VideoContentProperty::VIDEO_SCALE       = 4;
49 int const VideoContentProperty::COLOUR_CONVERSION = 5;
50 int const VideoContentProperty::VIDEO_FADE_IN     = 6;
51 int const VideoContentProperty::VIDEO_FADE_OUT    = 7;
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, shared_ptr<const Film> film)
68         : _parent (parent)
69         , _film (film)
70         , _video_length (0)
71         , _video_frame_type (VIDEO_FRAME_TYPE_2D)
72         , _scale (VideoContentScale (Ratio::from_id ("178")))
73         , _yuv (true)
74         , _fade_in (0)
75         , _fade_out (0)
76 {
77
78 }
79
80 VideoContent::VideoContent (Content* parent, shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
81         : _parent (parent)
82         , _film (film)
83 {
84         _video_size.width = node->number_child<int> ("VideoWidth");
85         _video_size.height = node->number_child<int> ("VideoHeight");
86         _video_frame_rate = node->optional_number_child<double> ("VideoFrameRate");
87         _video_length = node->number_child<Frame> ("VideoLength");
88         _video_frame_type = static_cast<VideoFrameType> (node->number_child<int> ("VideoFrameType"));
89         _sample_aspect_ratio = node->optional_number_child<double> ("SampleAspectRatio");
90         _crop.left = node->number_child<int> ("LeftCrop");
91         _crop.right = node->number_child<int> ("RightCrop");
92         _crop.top = node->number_child<int> ("TopCrop");
93         _crop.bottom = node->number_child<int> ("BottomCrop");
94
95         if (version <= 7) {
96                 optional<string> r = node->optional_string_child ("Ratio");
97                 if (r) {
98                         _scale = VideoContentScale (Ratio::from_id (r.get ()));
99                 }
100         } else {
101                 _scale = VideoContentScale (node->node_child ("Scale"));
102         }
103
104
105         if (node->optional_node_child ("ColourConversion")) {
106                 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"), version);
107         }
108
109         _yuv = node->optional_bool_child("YUV").get_value_or (true);
110
111         if (version >= 32) {
112                 _fade_in = node->number_child<Frame> ("FadeIn");
113                 _fade_out = node->number_child<Frame> ("FadeOut");
114         } else {
115                 _fade_in = _fade_out = 0;
116         }
117 }
118
119 VideoContent::VideoContent (Content* parent, shared_ptr<const Film> film, vector<shared_ptr<Content> > c)
120         : _parent (parent)
121         , _film (film)
122         , _video_length (0)
123         , _yuv (false)
124 {
125         shared_ptr<VideoContent> ref = dynamic_pointer_cast<VideoContent> (c[0]);
126         DCPOMATIC_ASSERT (ref);
127
128         for (size_t i = 0; i < c.size(); ++i) {
129                 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (c[i]);
130
131                 if (vc->video_size() != ref->video_size()) {
132                         throw JoinError (_("Content to be joined must have the same picture size."));
133                 }
134
135                 if (vc->video_frame_rate() != ref->video_frame_rate()) {
136                         throw JoinError (_("Content to be joined must have the same video frame rate."));
137                 }
138
139                 if (vc->video_frame_type() != ref->video_frame_type()) {
140                         throw JoinError (_("Content to be joined must have the same video frame type."));
141                 }
142
143                 if (vc->crop() != ref->crop()) {
144                         throw JoinError (_("Content to be joined must have the same crop."));
145                 }
146
147                 if (vc->scale() != ref->scale()) {
148                         throw JoinError (_("Content to be joined must have the same scale setting."));
149                 }
150
151                 if (vc->colour_conversion() != ref->colour_conversion()) {
152                         throw JoinError (_("Content to be joined must have the same colour conversion."));
153                 }
154
155                 if (vc->fade_in() != ref->fade_in() || vc->fade_out() != ref->fade_out()) {
156                         throw JoinError (_("Content to be joined must have the same fades."));
157                 }
158
159                 _video_length += vc->video_length ();
160
161                 if (vc->yuv ()) {
162                         _yuv = true;
163                 }
164         }
165
166         _video_size = ref->video_size ();
167         _video_frame_rate = ref->video_frame_rate ();
168         _video_frame_type = ref->video_frame_type ();
169         _crop = ref->crop ();
170         _scale = ref->scale ();
171         _colour_conversion = ref->colour_conversion ();
172         _fade_in = ref->fade_in ();
173         _fade_out = ref->fade_out ();
174 }
175
176 void
177 VideoContent::as_xml (xmlpp::Node* node) const
178 {
179         boost::mutex::scoped_lock lm (_mutex);
180         node->add_child("VideoLength")->add_child_text (raw_convert<string> (_video_length));
181         node->add_child("VideoWidth")->add_child_text (raw_convert<string> (_video_size.width));
182         node->add_child("VideoHeight")->add_child_text (raw_convert<string> (_video_size.height));
183         if (_video_frame_rate) {
184                 node->add_child("VideoFrameRate")->add_child_text (raw_convert<string> (_video_frame_rate.get()));
185         }
186         node->add_child("VideoFrameType")->add_child_text (raw_convert<string> (static_cast<int> (_video_frame_type)));
187         if (_sample_aspect_ratio) {
188                 node->add_child("SampleAspectRatio")->add_child_text (raw_convert<string> (_sample_aspect_ratio.get ()));
189         }
190         _crop.as_xml (node);
191         _scale.as_xml (node->add_child("Scale"));
192         if (_colour_conversion) {
193                 _colour_conversion.get().as_xml (node->add_child("ColourConversion"));
194         }
195         node->add_child("YUV")->add_child_text (_yuv ? "1" : "0");
196         node->add_child("FadeIn")->add_child_text (raw_convert<string> (_fade_in));
197         node->add_child("FadeOut")->add_child_text (raw_convert<string> (_fade_out));
198 }
199
200 void
201 VideoContent::set_default_colour_conversion ()
202 {
203         /* If there's no better offer we'll use Rec. 709 */
204         boost::mutex::scoped_lock lm (_mutex);
205         _colour_conversion = PresetColourConversion::from_id ("rec709").conversion;
206 }
207
208 void
209 VideoContent::take_from_video_examiner (shared_ptr<VideoExaminer> d)
210 {
211         /* These examiner calls could call other content methods which take a lock on the mutex */
212         dcp::Size const vs = d->video_size ();
213         optional<double> const vfr = d->video_frame_rate ();
214         Frame vl = d->video_length ();
215         optional<double> const ar = d->sample_aspect_ratio ();
216         bool const yuv = d->yuv ();
217
218         {
219                 boost::mutex::scoped_lock lm (_mutex);
220                 _video_size = vs;
221                 _video_frame_rate = vfr;
222                 _video_length = vl;
223                 _sample_aspect_ratio = ar;
224                 _yuv = yuv;
225
226                 /* Guess correct scale from size and sample aspect ratio */
227                 _scale = VideoContentScale (
228                         Ratio::nearest_from_ratio (double (_video_size.width) * ar.get_value_or (1) / _video_size.height)
229                         );
230         }
231
232         shared_ptr<const Film> film = _film.lock ();
233         DCPOMATIC_ASSERT (film);
234         LOG_GENERAL ("Video length obtained from header as %1 frames", _video_length);
235
236         _parent->signal_changed (VideoContentProperty::VIDEO_SIZE);
237         _parent->signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
238         _parent->signal_changed (VideoContentProperty::VIDEO_SCALE);
239         _parent->signal_changed (ContentProperty::LENGTH);
240 }
241
242 void
243 VideoContent::set_left_crop (int c)
244 {
245         {
246                 boost::mutex::scoped_lock lm (_mutex);
247
248                 if (_crop.left == c) {
249                         return;
250                 }
251
252                 _crop.left = c;
253         }
254
255         _parent->signal_changed (VideoContentProperty::VIDEO_CROP);
256 }
257
258 void
259 VideoContent::set_right_crop (int c)
260 {
261         {
262                 boost::mutex::scoped_lock lm (_mutex);
263                 if (_crop.right == c) {
264                         return;
265                 }
266
267                 _crop.right = c;
268         }
269
270         _parent->signal_changed (VideoContentProperty::VIDEO_CROP);
271 }
272
273 void
274 VideoContent::set_top_crop (int c)
275 {
276         {
277                 boost::mutex::scoped_lock lm (_mutex);
278                 if (_crop.top == c) {
279                         return;
280                 }
281
282                 _crop.top = c;
283         }
284
285         _parent->signal_changed (VideoContentProperty::VIDEO_CROP);
286 }
287
288 void
289 VideoContent::set_bottom_crop (int c)
290 {
291         {
292                 boost::mutex::scoped_lock lm (_mutex);
293                 if (_crop.bottom == c) {
294                         return;
295                 }
296
297                 _crop.bottom = c;
298         }
299
300         _parent->signal_changed (VideoContentProperty::VIDEO_CROP);
301 }
302
303 void
304 VideoContent::set_scale (VideoContentScale s)
305 {
306         {
307                 boost::mutex::scoped_lock lm (_mutex);
308                 if (_scale == s) {
309                         return;
310                 }
311
312                 _scale = s;
313         }
314
315         _parent->signal_changed (VideoContentProperty::VIDEO_SCALE);
316 }
317
318 /** @return string which includes everything about how this content looks */
319 string
320 VideoContent::identifier () const
321 {
322         SafeStringStream s;
323         s << crop().left
324           << "_" << crop().right
325           << "_" << crop().top
326           << "_" << crop().bottom
327           << "_" << scale().id()
328           << "_" << _fade_in
329           << "_" << _fade_out;
330
331         if (colour_conversion()) {
332                 s << "_" << colour_conversion().get().identifier ();
333         }
334
335         return s.str ();
336 }
337
338 void
339 VideoContent::set_video_frame_type (VideoFrameType t)
340 {
341         {
342                 boost::mutex::scoped_lock lm (_mutex);
343                 _video_frame_type = t;
344         }
345
346         _parent->signal_changed (VideoContentProperty::VIDEO_FRAME_TYPE);
347 }
348
349 string
350 VideoContent::technical_summary () const
351 {
352         string s = String::compose (
353                 N_("video: length %1 frames, size %2x%3, rate %4"),
354                 video_length_after_3d_combine(),
355                 video_size().width,
356                 video_size().height,
357                 video_frame_rate()
358                 );
359
360         if (sample_aspect_ratio ()) {
361                 s += String::compose (N_(", sample aspect ratio %1"), (sample_aspect_ratio().get ()));
362         }
363
364         return s;
365 }
366
367 dcp::Size
368 VideoContent::video_size_after_3d_split () const
369 {
370         dcp::Size const s = video_size ();
371         switch (video_frame_type ()) {
372         case VIDEO_FRAME_TYPE_2D:
373         case VIDEO_FRAME_TYPE_3D_ALTERNATE:
374         case VIDEO_FRAME_TYPE_3D_LEFT:
375         case VIDEO_FRAME_TYPE_3D_RIGHT:
376                 return s;
377         case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
378                 return dcp::Size (s.width / 2, s.height);
379         case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
380                 return dcp::Size (s.width, s.height / 2);
381         }
382
383         DCPOMATIC_ASSERT (false);
384 }
385
386 void
387 VideoContent::unset_colour_conversion ()
388 {
389         {
390                 boost::mutex::scoped_lock lm (_mutex);
391                 _colour_conversion = boost::optional<ColourConversion> ();
392         }
393
394         _parent->signal_changed (VideoContentProperty::COLOUR_CONVERSION);
395 }
396
397 void
398 VideoContent::set_colour_conversion (ColourConversion c)
399 {
400         {
401                 boost::mutex::scoped_lock lm (_mutex);
402                 _colour_conversion = c;
403         }
404
405         _parent->signal_changed (VideoContentProperty::COLOUR_CONVERSION);
406 }
407
408 void
409 VideoContent::set_fade_in (Frame t)
410 {
411         {
412                 boost::mutex::scoped_lock lm (_mutex);
413                 _fade_in = t;
414         }
415
416         _parent->signal_changed (VideoContentProperty::VIDEO_FADE_IN);
417 }
418
419 void
420 VideoContent::set_fade_out (Frame t)
421 {
422         {
423                 boost::mutex::scoped_lock lm (_mutex);
424                 _fade_out = t;
425         }
426
427         _parent->signal_changed (VideoContentProperty::VIDEO_FADE_OUT);
428 }
429
430 /** @return Video size after 3D split and crop */
431 dcp::Size
432 VideoContent::video_size_after_crop () const
433 {
434         return crop().apply (video_size_after_3d_split ());
435 }
436
437 void
438 VideoContent::scale_and_crop_to_fit_width ()
439 {
440         shared_ptr<const Film> film = _film.lock ();
441         DCPOMATIC_ASSERT (film);
442         set_scale (VideoContentScale (film->container ()));
443
444         int const crop = max (0, int (video_size().height - double (film->frame_size().height) * video_size().width / film->frame_size().width));
445         set_left_crop (0);
446         set_right_crop (0);
447         set_top_crop (crop / 2);
448         set_bottom_crop (crop / 2);
449 }
450
451 void
452 VideoContent::scale_and_crop_to_fit_height ()
453 {
454         shared_ptr<const Film> film = _film.lock ();
455         DCPOMATIC_ASSERT (film);
456         set_scale (VideoContentScale (film->container ()));
457
458         int const crop = max (0, int (video_size().width - double (film->frame_size().width) * video_size().height / film->frame_size().height));
459         set_left_crop (crop / 2);
460         set_right_crop (crop / 2);
461         set_top_crop (0);
462         set_bottom_crop (0);
463 }
464
465 void
466 VideoContent::set_video_frame_rate (double r)
467 {
468         {
469                 boost::mutex::scoped_lock lm (_mutex);
470                 if (_video_frame_rate == r) {
471                         return;
472                 }
473
474                 _video_frame_rate = r;
475         }
476
477         _parent->signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
478 }
479
480 /** @param f Frame index within the whole (untrimmed) content */
481 optional<double>
482 VideoContent::fade (Frame f) const
483 {
484         DCPOMATIC_ASSERT (f >= 0);
485
486         Frame const ts = _parent->trim_start().frames_round(video_frame_rate());
487         if ((f - ts) < fade_in()) {
488                 return double (f - ts) / fade_in();
489         }
490
491         Frame fade_out_start = video_length() - _parent->trim_end().frames_round(video_frame_rate()) - fade_out();
492         if (f >= fade_out_start) {
493                 return 1 - double (f - fade_out_start) / fade_out();
494         }
495
496         return optional<double> ();
497 }
498
499 string
500 VideoContent::processing_description () const
501 {
502         /* stringstream is OK here as this string is just for presentation to the user */
503         SafeStringStream d;
504
505         if (video_size().width && video_size().height) {
506                 d << String::compose (
507                         _("Content video is %1x%2"),
508                         video_size_after_3d_split().width,
509                         video_size_after_3d_split().height
510                         );
511
512
513                 double ratio = video_size_after_3d_split().ratio ();
514
515                 if (sample_aspect_ratio ()) {
516                         d << ", " << _("pixel aspect ratio") << " " << fixed << setprecision(2) << sample_aspect_ratio().get () << ":1";
517                         ratio *= sample_aspect_ratio().get ();
518                 }
519
520                 d << "\n" << _("Display aspect ratio") << " " << fixed << setprecision(2) << ratio << ":1\n";
521         }
522
523         if ((crop().left || crop().right || crop().top || crop().bottom) && video_size() != dcp::Size (0, 0)) {
524                 dcp::Size cropped = video_size_after_crop ();
525                 d << String::compose (
526                         _("Cropped to %1x%2"),
527                         cropped.width, cropped.height
528                         );
529
530                 d << " (" << fixed << setprecision(2) << cropped.ratio () << ":1)\n";
531         }
532
533         shared_ptr<const Film> film = _film.lock ();
534         DCPOMATIC_ASSERT (film);
535         dcp::Size const container_size = film->frame_size ();
536         dcp::Size const scaled = scale().size (shared_from_this(), container_size, container_size);
537
538         if (scaled != video_size_after_crop ()) {
539                 d << String::compose (
540                         _("Scaled to %1x%2"),
541                         scaled.width, scaled.height
542                         );
543
544                 d << " (" << fixed << setprecision(2) << scaled.ratio() << ":1)\n";
545         }
546
547         if (scaled != container_size) {
548                 d << String::compose (
549                         _("Padded with black to fit container %1 (%2x%3)"),
550                         film->container()->nickname (),
551                         container_size.width, container_size.height
552                         );
553
554                 d << " (" << fixed << setprecision(2) << container_size.ratio () << ":1)\n";
555         }
556
557         d << _("Content frame rate");
558         d << " " << fixed << setprecision(4) << video_frame_rate() << "\n";
559
560         FrameRateChange frc (video_frame_rate(), film->video_frame_rate ());
561         d << frc.description () << "\n";
562
563         return d.str ();
564 }
565
566 void
567 VideoContent::add_properties (list<UserProperty>& p) const
568 {
569         p.push_back (UserProperty (_("Video"), _("Length"), raw_convert<string> (video_length ()), _("video frames")));
570         p.push_back (UserProperty (_("Video"), _("Size"), raw_convert<string> (video_size().width) + "x" + raw_convert<string> (video_size().height)));
571         p.push_back (UserProperty (_("Video"), _("Frame rate"), raw_convert<string> (video_frame_rate(), 5), _("frames per second")));
572 }
573
574 double
575 VideoContent::video_frame_rate () const
576 {
577         boost::mutex::scoped_lock lm (_mutex);
578         shared_ptr<const Film> film = _film.lock ();
579         DCPOMATIC_ASSERT (film);
580         return _video_frame_rate.get_value_or (film->video_frame_rate ());
581 }
582
583 void
584 VideoContent::set_video_length (Frame len)
585 {
586         {
587                 boost::mutex::scoped_lock lm (_mutex);
588                 _video_length = len;
589         }
590
591         _parent->signal_changed (ContentProperty::LENGTH);
592 }