Make terminate_threads() less likely to leave _threads containing invalid pointers.
[dcpomatic.git] / src / lib / video_content.cc
1 /*
2     Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "video_content.h"
22 #include "content.h"
23 #include "video_examiner.h"
24 #include "compose.hpp"
25 #include "ratio.h"
26 #include "config.h"
27 #include "colour_conversion.h"
28 #include "util.h"
29 #include "film.h"
30 #include "exceptions.h"
31 #include "frame_rate_change.h"
32 #include "log.h"
33 #include "dcpomatic_log.h"
34 #include <dcp/raw_convert.h>
35 #include <libcxml/cxml.h>
36 #include <libxml++/libxml++.h>
37 #include <iomanip>
38 #include <iostream>
39
40 #include "i18n.h"
41
42 int const VideoContentProperty::SIZE      = 0;
43 int const VideoContentProperty::FRAME_TYPE  = 1;
44 int const VideoContentProperty::CROP      = 2;
45 int const VideoContentProperty::SCALE     = 3;
46 int const VideoContentProperty::COLOUR_CONVERSION = 4;
47 int const VideoContentProperty::FADE_IN     = 5;
48 int const VideoContentProperty::FADE_OUT    = 6;
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::fixed;
57 using std::setprecision;
58 using std::list;
59 using std::pair;
60 using boost::shared_ptr;
61 using boost::optional;
62 using boost::dynamic_pointer_cast;
63 using dcp::raw_convert;
64
65 VideoContent::VideoContent (Content* parent)
66         : ContentPart (parent)
67         , _length (0)
68         , _frame_type (VIDEO_FRAME_TYPE_2D)
69         , _scale (VideoContentScale (Ratio::from_id ("178")))
70         , _yuv (true)
71         , _fade_in (0)
72         , _fade_out (0)
73 {
74
75 }
76
77 shared_ptr<VideoContent>
78 VideoContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version)
79 {
80         if (!node->optional_number_child<int> ("VideoWidth")) {
81                 return shared_ptr<VideoContent> ();
82         }
83
84         return shared_ptr<VideoContent> (new VideoContent (parent, node, version));
85 }
86
87 VideoContent::VideoContent (Content* parent, cxml::ConstNodePtr node, int version)
88         : ContentPart (parent)
89 {
90         _size.width = node->number_child<int> ("VideoWidth");
91         _size.height = node->number_child<int> ("VideoHeight");
92
93         /* Backwards compatibility */
94         optional<double> r = node->optional_number_child<double>("VideoFrameRate");
95         if (r) {
96                 _parent->set_video_frame_rate (r.get ());
97         }
98
99         _length = node->number_child<Frame> ("VideoLength");
100
101         if (version <= 34) {
102                 /* Snapshot of the VideoFrameType enum at version 34 */
103                 switch (node->number_child<int> ("VideoFrameType")) {
104                 case 0:
105                         _frame_type = VIDEO_FRAME_TYPE_2D;
106                         break;
107                 case 1:
108                         _frame_type = VIDEO_FRAME_TYPE_3D_LEFT_RIGHT;
109                         break;
110                 case 2:
111                         _frame_type = VIDEO_FRAME_TYPE_3D_TOP_BOTTOM;
112                         break;
113                 case 3:
114                         _frame_type = VIDEO_FRAME_TYPE_3D_ALTERNATE;
115                         break;
116                 case 4:
117                         _frame_type = VIDEO_FRAME_TYPE_3D_LEFT;
118                         break;
119                 case 5:
120                         _frame_type = VIDEO_FRAME_TYPE_3D_RIGHT;
121                         break;
122                 }
123         } else {
124                 _frame_type = string_to_video_frame_type (node->string_child ("VideoFrameType"));
125         }
126
127         _sample_aspect_ratio = node->optional_number_child<double> ("SampleAspectRatio");
128         _crop.left = node->number_child<int> ("LeftCrop");
129         _crop.right = node->number_child<int> ("RightCrop");
130         _crop.top = node->number_child<int> ("TopCrop");
131         _crop.bottom = node->number_child<int> ("BottomCrop");
132
133         if (version <= 7) {
134                 optional<string> r = node->optional_string_child ("Ratio");
135                 if (r) {
136                         _scale = VideoContentScale (Ratio::from_id (r.get ()));
137                 }
138         } else {
139                 _scale = VideoContentScale (node->node_child ("Scale"));
140         }
141
142         if (node->optional_node_child ("ColourConversion")) {
143                 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"), version);
144         }
145
146         _yuv = node->optional_bool_child("YUV").get_value_or (true);
147
148         if (version >= 32) {
149                 _fade_in = node->number_child<Frame> ("FadeIn");
150                 _fade_out = node->number_child<Frame> ("FadeOut");
151         } else {
152                 _fade_in = _fade_out = 0;
153         }
154 }
155
156 VideoContent::VideoContent (Content* parent, vector<shared_ptr<Content> > c)
157         : ContentPart (parent)
158         , _length (0)
159         , _yuv (false)
160 {
161         shared_ptr<VideoContent> ref = c[0]->video;
162         DCPOMATIC_ASSERT (ref);
163
164         for (size_t i = 1; i < c.size(); ++i) {
165
166                 if (c[i]->video->size() != ref->size()) {
167                         throw JoinError (_("Content to be joined must have the same picture size."));
168                 }
169
170                 if (c[i]->video->frame_type() != ref->frame_type()) {
171                         throw JoinError (_("Content to be joined must have the same video frame type."));
172                 }
173
174                 if (c[i]->video->crop() != ref->crop()) {
175                         throw JoinError (_("Content to be joined must have the same crop."));
176                 }
177
178                 if (c[i]->video->scale() != ref->scale()) {
179                         throw JoinError (_("Content to be joined must have the same scale setting."));
180                 }
181
182                 if (c[i]->video->colour_conversion() != ref->colour_conversion()) {
183                         throw JoinError (_("Content to be joined must have the same colour conversion."));
184                 }
185
186                 if (c[i]->video->fade_in() != ref->fade_in() || c[i]->video->fade_out() != ref->fade_out()) {
187                         throw JoinError (_("Content to be joined must have the same fades."));
188                 }
189
190                 _length += c[i]->video->length ();
191
192                 if (c[i]->video->yuv ()) {
193                         _yuv = true;
194                 }
195         }
196
197         _size = ref->size ();
198         _frame_type = ref->frame_type ();
199         _crop = ref->crop ();
200         _scale = ref->scale ();
201         _colour_conversion = ref->colour_conversion ();
202         _fade_in = ref->fade_in ();
203         _fade_out = ref->fade_out ();
204 }
205
206 void
207 VideoContent::as_xml (xmlpp::Node* node) const
208 {
209         boost::mutex::scoped_lock lm (_mutex);
210         node->add_child("VideoLength")->add_child_text (raw_convert<string> (_length));
211         node->add_child("VideoWidth")->add_child_text (raw_convert<string> (_size.width));
212         node->add_child("VideoHeight")->add_child_text (raw_convert<string> (_size.height));
213         node->add_child("VideoFrameType")->add_child_text (video_frame_type_to_string (_frame_type));
214         if (_sample_aspect_ratio) {
215                 node->add_child("SampleAspectRatio")->add_child_text (raw_convert<string> (_sample_aspect_ratio.get ()));
216         }
217         _crop.as_xml (node);
218         _scale.as_xml (node->add_child("Scale"));
219         if (_colour_conversion) {
220                 _colour_conversion.get().as_xml (node->add_child("ColourConversion"));
221         }
222         node->add_child("YUV")->add_child_text (_yuv ? "1" : "0");
223         node->add_child("FadeIn")->add_child_text (raw_convert<string> (_fade_in));
224         node->add_child("FadeOut")->add_child_text (raw_convert<string> (_fade_out));
225 }
226
227 void
228 VideoContent::take_from_examiner (shared_ptr<VideoExaminer> d)
229 {
230         /* These examiner calls could call other content methods which take a lock on the mutex */
231         dcp::Size const vs = d->video_size ();
232         Frame vl = d->video_length ();
233         optional<double> const ar = d->sample_aspect_ratio ();
234         bool const yuv = d->yuv ();
235
236         ChangeSignaller<Content> cc1 (_parent, VideoContentProperty::SIZE);
237         ChangeSignaller<Content> cc2 (_parent, VideoContentProperty::SCALE);
238         ChangeSignaller<Content> cc3 (_parent, ContentProperty::LENGTH);
239
240         {
241                 boost::mutex::scoped_lock lm (_mutex);
242                 _size = vs;
243                 _length = vl;
244                 _sample_aspect_ratio = ar;
245                 _yuv = yuv;
246
247                 if (Config::instance()->default_scale_to ()) {
248                         _scale = VideoContentScale (Config::instance()->default_scale_to ());
249                 } else {
250                         /* Guess correct scale from size and sample aspect ratio */
251                         _scale = VideoContentScale (
252                                 Ratio::nearest_from_ratio (double (_size.width) * ar.get_value_or (1) / _size.height)
253                                 );
254                 }
255         }
256
257         LOG_GENERAL ("Video length obtained from header as %1 frames", _length);
258
259         if (d->video_frame_rate()) {
260                 _parent->set_video_frame_rate (d->video_frame_rate().get());
261         }
262 }
263
264 /** @return string which includes everything about how this content looks */
265 string
266 VideoContent::identifier () const
267 {
268         char buffer[256];
269         snprintf (
270                 buffer, sizeof(buffer), "%d_%d_%d_%d_%s_%" PRId64 "_%" PRId64,
271                 crop().left,
272                 crop().right,
273                 crop().top,
274                 crop().bottom,
275                 scale().id().c_str(),
276                 _fade_in,
277                 _fade_out
278                 );
279
280         string s (buffer);
281
282         if (colour_conversion()) {
283                 s += "_" + colour_conversion().get().identifier ();
284         }
285
286         return s;
287 }
288
289 string
290 VideoContent::technical_summary () const
291 {
292         string s = String::compose (
293                 N_("video: length %1 frames, size %2x%3"),
294                 length_after_3d_combine(),
295                 size().width,
296                 size().height
297                 );
298
299         if (sample_aspect_ratio ()) {
300                 s += String::compose (N_(", sample aspect ratio %1"), (sample_aspect_ratio().get ()));
301         }
302
303         return s;
304 }
305
306 dcp::Size
307 VideoContent::size_after_3d_split () const
308 {
309         dcp::Size const s = size ();
310         switch (frame_type ()) {
311         case VIDEO_FRAME_TYPE_2D:
312         case VIDEO_FRAME_TYPE_3D:
313         case VIDEO_FRAME_TYPE_3D_ALTERNATE:
314         case VIDEO_FRAME_TYPE_3D_LEFT:
315         case VIDEO_FRAME_TYPE_3D_RIGHT:
316                 return s;
317         case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
318                 return dcp::Size (s.width / 2, s.height);
319         case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
320                 return dcp::Size (s.width, s.height / 2);
321         }
322
323         DCPOMATIC_ASSERT (false);
324 }
325
326 /** @return Video size after 3D split and crop */
327 dcp::Size
328 VideoContent::size_after_crop () const
329 {
330         return crop().apply (size_after_3d_split ());
331 }
332
333 void
334 VideoContent::scale_and_crop_to_fit_width (shared_ptr<const Film> film)
335 {
336         set_scale (VideoContentScale(film->container()));
337
338         int const crop = max (0, int (size().height - double (film->frame_size().height) * size().width / film->frame_size().width));
339         set_left_crop (0);
340         set_right_crop (0);
341         set_top_crop (crop / 2);
342         set_bottom_crop (crop / 2);
343 }
344
345 void
346 VideoContent::scale_and_crop_to_fit_height (shared_ptr<const Film> film)
347 {
348         set_scale (VideoContentScale(film->container()));
349
350         int const crop = max (0, int (size().width - double (film->frame_size().width) * size().height / film->frame_size().height));
351         set_left_crop (crop / 2);
352         set_right_crop (crop / 2);
353         set_top_crop (0);
354         set_bottom_crop (0);
355 }
356
357 /** @param f Frame index within the whole (untrimmed) content.
358  *  @return Fade factor (between 0 and 1) or unset if there is no fade.
359  */
360 optional<double>
361 VideoContent::fade (shared_ptr<const Film> film, Frame f) const
362 {
363         DCPOMATIC_ASSERT (f >= 0);
364
365         double const vfr = _parent->active_video_frame_rate(film);
366
367         Frame const ts = _parent->trim_start().frames_round(vfr);
368         if ((f - ts) < fade_in()) {
369                 return double (f - ts) / fade_in();
370         }
371
372         Frame fade_out_start = length() - _parent->trim_end().frames_round(vfr) - fade_out();
373         if (f >= fade_out_start) {
374                 return 1 - double (f - fade_out_start) / fade_out();
375         }
376
377         return optional<double> ();
378 }
379
380 string
381 VideoContent::processing_description (shared_ptr<const Film> film) const
382 {
383         string d;
384         char buffer[256];
385
386         if (size().width && size().height) {
387                 d += String::compose (
388                         _("Content video is %1x%2"),
389                         size_after_3d_split().width,
390                         size_after_3d_split().height
391                         );
392
393
394                 double ratio = size_after_3d_split().ratio ();
395
396                 if (sample_aspect_ratio ()) {
397                         snprintf (buffer, sizeof(buffer), _(", pixel aspect ratio %.2f:1"), sample_aspect_ratio().get());
398                         d += buffer;
399                         ratio *= sample_aspect_ratio().get ();
400                 }
401
402                 snprintf (buffer, sizeof(buffer), _("\nDisplay aspect ratio %.2f:1"), ratio);
403                 d += buffer;
404         }
405
406         if ((crop().left || crop().right || crop().top || crop().bottom) && size() != dcp::Size (0, 0)) {
407                 dcp::Size cropped = size_after_crop ();
408                 d += String::compose (
409                         _("\nCropped to %1x%2"),
410                         cropped.width, cropped.height
411                         );
412
413                 snprintf (buffer, sizeof(buffer), " (%.2f:1)", cropped.ratio());
414                 d += buffer;
415         }
416
417         dcp::Size const container_size = film->frame_size ();
418         dcp::Size const scaled = scale().size (shared_from_this(), container_size, container_size);
419
420         if (scaled != size_after_crop ()) {
421                 d += String::compose (
422                         _("\nScaled to %1x%2"),
423                         scaled.width, scaled.height
424                         );
425
426                 snprintf (buffer, sizeof(buffer), _(" (%.2f:1)"), scaled.ratio());
427                 d += buffer;
428         }
429
430         if (scaled != container_size) {
431                 d += String::compose (
432                         _("\nPadded with black to fit container %1 (%2x%3)"),
433                         film->container()->container_nickname (),
434                         container_size.width, container_size.height
435                         );
436
437                 snprintf (buffer, sizeof(buffer), _(" (%.2f:1)"), container_size.ratio());
438                 d += buffer;
439         }
440
441         if (_parent->video_frame_rate()) {
442                 double const vfr = _parent->video_frame_rate().get ();
443
444                 snprintf (buffer, sizeof(buffer), _("\nContent frame rate %.4f\n"), vfr);
445                 d += buffer;
446
447                 FrameRateChange frc (vfr, film->video_frame_rate ());
448                 d += frc.description ();
449         }
450
451         return d;
452 }
453
454 void
455 VideoContent::add_properties (list<UserProperty>& p) const
456 {
457         p.push_back (UserProperty (UserProperty::VIDEO, _("Length"), length (), _("video frames")));
458         p.push_back (UserProperty (UserProperty::VIDEO, _("Size"), String::compose ("%1x%2", size().width, size().height)));
459 }
460
461 void
462 VideoContent::set_length (Frame len)
463 {
464         maybe_set (_length, len, ContentProperty::LENGTH);
465 }
466
467 void
468 VideoContent::set_left_crop (int c)
469 {
470         maybe_set (_crop.left, c, VideoContentProperty::CROP);
471 }
472
473 void
474 VideoContent::set_right_crop (int c)
475 {
476         maybe_set (_crop.right, c, VideoContentProperty::CROP);
477 }
478
479 void
480 VideoContent::set_top_crop (int c)
481 {
482         maybe_set (_crop.top, c, VideoContentProperty::CROP);
483 }
484
485 void
486 VideoContent::set_bottom_crop (int c)
487 {
488         maybe_set (_crop.bottom, c, VideoContentProperty::CROP);
489 }
490
491 void
492 VideoContent::set_scale (VideoContentScale s)
493 {
494         maybe_set (_scale, s, VideoContentProperty::SCALE);
495 }
496
497 void
498 VideoContent::set_frame_type (VideoFrameType t)
499 {
500         maybe_set (_frame_type, t, VideoContentProperty::FRAME_TYPE);
501 }
502
503 void
504 VideoContent::unset_colour_conversion ()
505 {
506         maybe_set (_colour_conversion, boost::optional<ColourConversion> (), VideoContentProperty::COLOUR_CONVERSION);
507 }
508
509 void
510 VideoContent::set_colour_conversion (ColourConversion c)
511 {
512         maybe_set (_colour_conversion, c, VideoContentProperty::COLOUR_CONVERSION);
513 }
514
515 void
516 VideoContent::set_fade_in (Frame t)
517 {
518         maybe_set (_fade_in, t, VideoContentProperty::FADE_IN);
519 }
520
521 void
522 VideoContent::set_fade_out (Frame t)
523 {
524         maybe_set (_fade_out, t, VideoContentProperty::FADE_OUT);
525 }
526
527 void
528 VideoContent::take_settings_from (shared_ptr<const VideoContent> c)
529 {
530         if (c->_colour_conversion) {
531                 set_colour_conversion (c->_colour_conversion.get());
532         } else {
533                 unset_colour_conversion ();
534         }
535         set_frame_type (c->_frame_type);
536         set_left_crop (c->_crop.left);
537         set_right_crop (c->_crop.right);
538         set_top_crop (c->_crop.top);
539         set_bottom_crop (c->_crop.bottom);
540         set_scale (c->_scale);
541         set_fade_in (c->_fade_in);
542         set_fade_out (c->_fade_out);
543 }
544
545 void
546 VideoContent::modify_position (shared_ptr<const Film> film, DCPTime& pos) const
547 {
548         pos = pos.round (film->video_frame_rate());
549 }
550
551 void
552 VideoContent::modify_trim_start (ContentTime& trim) const
553 {
554         if (_parent->video_frame_rate()) {
555                 trim = trim.round (_parent->video_frame_rate().get());
556         }
557 }