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