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