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