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