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