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