Remove join function; the code is long and I don't think anybody
[dcpomatic.git] / src / lib / video_content.cc
1 /*
2     Copyright (C) 2013-2018 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 <dcp/raw_convert.h>
34 #include <libcxml/cxml.h>
35 #include <libxml++/libxml++.h>
36 #include <iomanip>
37 #include <iostream>
38
39 #include "i18n.h"
40
41 #define LOG_GENERAL(...) _parent->film()->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL);
42
43 int const VideoContentProperty::SIZE      = 0;
44 int const VideoContentProperty::FRAME_TYPE  = 1;
45 int const VideoContentProperty::CROP      = 2;
46 int const VideoContentProperty::SCALE     = 3;
47 int const VideoContentProperty::COLOUR_CONVERSION = 4;
48 int const VideoContentProperty::FADE_IN     = 5;
49 int const VideoContentProperty::FADE_OUT    = 6;
50
51 using std::string;
52 using std::setprecision;
53 using std::cout;
54 using std::vector;
55 using std::min;
56 using std::max;
57 using std::fixed;
58 using std::setprecision;
59 using std::list;
60 using std::pair;
61 using boost::shared_ptr;
62 using boost::optional;
63 using boost::dynamic_pointer_cast;
64 using dcp::raw_convert;
65
66 VideoContent::VideoContent (Content* parent)
67         : ContentPart (parent)
68         , _length (0)
69         , _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 shared_ptr<VideoContent>
79 VideoContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version)
80 {
81         if (!node->optional_number_child<int> ("VideoWidth")) {
82                 return shared_ptr<VideoContent> ();
83         }
84
85         return shared_ptr<VideoContent> (new VideoContent (parent, node, version));
86 }
87
88 VideoContent::VideoContent (Content* parent, cxml::ConstNodePtr node, int version)
89         : ContentPart (parent)
90 {
91         _size.width = node->number_child<int> ("VideoWidth");
92         _size.height = node->number_child<int> ("VideoHeight");
93
94         /* Backwards compatibility */
95         optional<double> r = node->optional_number_child<double>("VideoFrameRate");
96         if (r) {
97                 _parent->set_video_frame_rate (r.get ());
98         }
99
100         _length = node->number_child<Frame> ("VideoLength");
101
102         if (version <= 34) {
103                 /* Snapshot of the VideoFrameType enum at version 34 */
104                 switch (node->number_child<int> ("VideoFrameType")) {
105                 case 0:
106                         _frame_type = VIDEO_FRAME_TYPE_2D;
107                         break;
108                 case 1:
109                         _frame_type = VIDEO_FRAME_TYPE_3D_LEFT_RIGHT;
110                         break;
111                 case 2:
112                         _frame_type = VIDEO_FRAME_TYPE_3D_TOP_BOTTOM;
113                         break;
114                 case 3:
115                         _frame_type = VIDEO_FRAME_TYPE_3D_ALTERNATE;
116                         break;
117                 case 4:
118                         _frame_type = VIDEO_FRAME_TYPE_3D_LEFT;
119                         break;
120                 case 5:
121                         _frame_type = VIDEO_FRAME_TYPE_3D_RIGHT;
122                         break;
123                 }
124         } else {
125                 _frame_type = string_to_video_frame_type (node->string_child ("VideoFrameType"));
126         }
127
128         _sample_aspect_ratio = node->optional_number_child<double> ("SampleAspectRatio");
129         _crop.left = node->number_child<int> ("LeftCrop");
130         _crop.right = node->number_child<int> ("RightCrop");
131         _crop.top = node->number_child<int> ("TopCrop");
132         _crop.bottom = node->number_child<int> ("BottomCrop");
133
134         if (version <= 7) {
135                 optional<string> r = node->optional_string_child ("Ratio");
136                 if (r) {
137                         _scale = VideoContentScale (Ratio::from_id (r.get ()));
138                 }
139         } else {
140                 _scale = VideoContentScale (node->node_child ("Scale"));
141         }
142
143         if (node->optional_node_child ("ColourConversion")) {
144                 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"), version);
145         }
146
147         _yuv = node->optional_bool_child("YUV").get_value_or (true);
148
149         if (version >= 32) {
150                 _fade_in = node->number_child<Frame> ("FadeIn");
151                 _fade_out = node->number_child<Frame> ("FadeOut");
152         } else {
153                 _fade_in = _fade_out = 0;
154         }
155 }
156
157 void
158 VideoContent::as_xml (xmlpp::Node* node) const
159 {
160         boost::mutex::scoped_lock lm (_mutex);
161         node->add_child("VideoLength")->add_child_text (raw_convert<string> (_length));
162         node->add_child("VideoWidth")->add_child_text (raw_convert<string> (_size.width));
163         node->add_child("VideoHeight")->add_child_text (raw_convert<string> (_size.height));
164         node->add_child("VideoFrameType")->add_child_text (video_frame_type_to_string (_frame_type));
165         if (_sample_aspect_ratio) {
166                 node->add_child("SampleAspectRatio")->add_child_text (raw_convert<string> (_sample_aspect_ratio.get ()));
167         }
168         _crop.as_xml (node);
169         _scale.as_xml (node->add_child("Scale"));
170         if (_colour_conversion) {
171                 _colour_conversion.get().as_xml (node->add_child("ColourConversion"));
172         }
173         node->add_child("YUV")->add_child_text (_yuv ? "1" : "0");
174         node->add_child("FadeIn")->add_child_text (raw_convert<string> (_fade_in));
175         node->add_child("FadeOut")->add_child_text (raw_convert<string> (_fade_out));
176 }
177
178 void
179 VideoContent::take_from_examiner (shared_ptr<VideoExaminer> d)
180 {
181         /* These examiner calls could call other content methods which take a lock on the mutex */
182         dcp::Size const vs = d->video_size ();
183         Frame vl = d->video_length ();
184         optional<double> const ar = d->sample_aspect_ratio ();
185         bool const yuv = d->yuv ();
186
187         {
188                 boost::mutex::scoped_lock lm (_mutex);
189                 _size = vs;
190                 _length = vl;
191                 _sample_aspect_ratio = ar;
192                 _yuv = yuv;
193
194                 if (Config::instance()->default_scale_to ()) {
195                         _scale = VideoContentScale (Config::instance()->default_scale_to ());
196                 } else {
197                         /* Guess correct scale from size and sample aspect ratio */
198                         _scale = VideoContentScale (
199                                 Ratio::nearest_from_ratio (double (_size.width) * ar.get_value_or (1) / _size.height)
200                                 );
201                 }
202         }
203
204         LOG_GENERAL ("Video length obtained from header as %1 frames", _length);
205
206         if (d->video_frame_rate()) {
207                 _parent->set_video_frame_rate (d->video_frame_rate().get());
208         }
209
210         _parent->signal_changed (VideoContentProperty::SIZE);
211         _parent->signal_changed (VideoContentProperty::SCALE);
212         _parent->signal_changed (ContentProperty::LENGTH);
213 }
214
215 /** @return string which includes everything about how this content looks */
216 string
217 VideoContent::identifier () const
218 {
219         char buffer[256];
220         snprintf (
221                 buffer, sizeof(buffer), "%d_%d_%d_%d_%s_%" PRId64 "_%" PRId64,
222                 crop().left,
223                 crop().right,
224                 crop().top,
225                 crop().bottom,
226                 scale().id().c_str(),
227                 _fade_in,
228                 _fade_out
229                 );
230
231         string s (buffer);
232
233         if (colour_conversion()) {
234                 s += "_" + colour_conversion().get().identifier ();
235         }
236
237         return s;
238 }
239
240 string
241 VideoContent::technical_summary () const
242 {
243         string s = String::compose (
244                 N_("video: length %1 frames, size %2x%3"),
245                 length_after_3d_combine(),
246                 size().width,
247                 size().height
248                 );
249
250         if (sample_aspect_ratio ()) {
251                 s += String::compose (N_(", sample aspect ratio %1"), (sample_aspect_ratio().get ()));
252         }
253
254         return s;
255 }
256
257 dcp::Size
258 VideoContent::size_after_3d_split () const
259 {
260         dcp::Size const s = size ();
261         switch (frame_type ()) {
262         case VIDEO_FRAME_TYPE_2D:
263         case VIDEO_FRAME_TYPE_3D:
264         case VIDEO_FRAME_TYPE_3D_ALTERNATE:
265         case VIDEO_FRAME_TYPE_3D_LEFT:
266         case VIDEO_FRAME_TYPE_3D_RIGHT:
267                 return s;
268         case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
269                 return dcp::Size (s.width / 2, s.height);
270         case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
271                 return dcp::Size (s.width, s.height / 2);
272         }
273
274         DCPOMATIC_ASSERT (false);
275 }
276
277 /** @return Video size after 3D split and crop */
278 dcp::Size
279 VideoContent::size_after_crop () const
280 {
281         return crop().apply (size_after_3d_split ());
282 }
283
284 void
285 VideoContent::scale_and_crop_to_fit_width ()
286 {
287         shared_ptr<const Film> film = _parent->film ();
288         set_scale (VideoContentScale (film->container ()));
289
290         int const crop = max (0, int (size().height - double (film->frame_size().height) * size().width / film->frame_size().width));
291         set_left_crop (0);
292         set_right_crop (0);
293         set_top_crop (crop / 2);
294         set_bottom_crop (crop / 2);
295 }
296
297 void
298 VideoContent::scale_and_crop_to_fit_height ()
299 {
300         shared_ptr<const Film> film = _parent->film ();
301         set_scale (VideoContentScale (film->container ()));
302
303         int const crop = max (0, int (size().width - double (film->frame_size().width) * size().height / film->frame_size().height));
304         set_left_crop (crop / 2);
305         set_right_crop (crop / 2);
306         set_top_crop (0);
307         set_bottom_crop (0);
308 }
309
310 /** @param f Frame index within the whole (untrimmed) content */
311 optional<double>
312 VideoContent::fade (Frame f) const
313 {
314         DCPOMATIC_ASSERT (f >= 0);
315
316         shared_ptr<const Film> film = _parent->film ();
317
318         double const vfr = _parent->active_video_frame_rate ();
319
320         Frame const ts = _parent->trim_start().frames_round(vfr);
321         if ((f - ts) < fade_in()) {
322                 return double (f - ts) / fade_in();
323         }
324
325         Frame fade_out_start = length() - _parent->trim_end().frames_round(vfr) - fade_out();
326         if (f >= fade_out_start) {
327                 return 1 - double (f - fade_out_start) / fade_out();
328         }
329
330         return optional<double> ();
331 }
332
333 string
334 VideoContent::processing_description () const
335 {
336         string d;
337         char buffer[256];
338
339         if (size().width && size().height) {
340                 d += String::compose (
341                         _("Content video is %1x%2"),
342                         size_after_3d_split().width,
343                         size_after_3d_split().height
344                         );
345
346
347                 double ratio = size_after_3d_split().ratio ();
348
349                 if (sample_aspect_ratio ()) {
350                         snprintf (buffer, sizeof(buffer), _(", pixel aspect ratio %.2f:1"), sample_aspect_ratio().get());
351                         d += buffer;
352                         ratio *= sample_aspect_ratio().get ();
353                 }
354
355                 snprintf (buffer, sizeof(buffer), _("\nDisplay aspect ratio %.2f:1"), ratio);
356                 d += buffer;
357         }
358
359         if ((crop().left || crop().right || crop().top || crop().bottom) && size() != dcp::Size (0, 0)) {
360                 dcp::Size cropped = size_after_crop ();
361                 d += String::compose (
362                         _("\nCropped to %1x%2"),
363                         cropped.width, cropped.height
364                         );
365
366                 snprintf (buffer, sizeof(buffer), " (%.2f:1)", cropped.ratio());
367                 d += buffer;
368         }
369
370         shared_ptr<const Film> film = _parent->film ();
371         dcp::Size const container_size = film->frame_size ();
372         dcp::Size const scaled = scale().size (shared_from_this(), container_size, container_size);
373
374         if (scaled != size_after_crop ()) {
375                 d += String::compose (
376                         _("\nScaled to %1x%2"),
377                         scaled.width, scaled.height
378                         );
379
380                 snprintf (buffer, sizeof(buffer), _(" (%.2f:1)"), scaled.ratio());
381                 d += buffer;
382         }
383
384         if (scaled != container_size) {
385                 d += String::compose (
386                         _("\nPadded with black to fit container %1 (%2x%3)"),
387                         film->container()->container_nickname (),
388                         container_size.width, container_size.height
389                         );
390
391                 snprintf (buffer, sizeof(buffer), _(" (%.2f:1)"), container_size.ratio());
392                 d += buffer;
393         }
394
395         if (_parent->video_frame_rate()) {
396                 double const vfr = _parent->video_frame_rate().get ();
397
398                 snprintf (buffer, sizeof(buffer), _("\nContent frame rate %.4f\n"), vfr);
399                 d += buffer;
400
401                 FrameRateChange frc (vfr, film->video_frame_rate ());
402                 d += frc.description ();
403         }
404
405         return d;
406 }
407
408 void
409 VideoContent::add_properties (list<UserProperty>& p) const
410 {
411         p.push_back (UserProperty (UserProperty::VIDEO, _("Length"), length (), _("video frames")));
412         p.push_back (UserProperty (UserProperty::VIDEO, _("Size"), String::compose ("%1x%2", size().width, size().height)));
413 }
414
415 void
416 VideoContent::set_length (Frame len)
417 {
418         maybe_set (_length, len, ContentProperty::LENGTH);
419 }
420
421 void
422 VideoContent::set_left_crop (int c)
423 {
424         maybe_set (_crop.left, c, VideoContentProperty::CROP);
425 }
426
427 void
428 VideoContent::set_right_crop (int c)
429 {
430         maybe_set (_crop.right, c, VideoContentProperty::CROP);
431 }
432
433 void
434 VideoContent::set_top_crop (int c)
435 {
436         maybe_set (_crop.top, c, VideoContentProperty::CROP);
437 }
438
439 void
440 VideoContent::set_bottom_crop (int c)
441 {
442         maybe_set (_crop.bottom, c, VideoContentProperty::CROP);
443 }
444
445 void
446 VideoContent::set_scale (VideoContentScale s)
447 {
448         maybe_set (_scale, s, VideoContentProperty::SCALE);
449 }
450
451 void
452 VideoContent::set_frame_type (VideoFrameType t)
453 {
454         maybe_set (_frame_type, t, VideoContentProperty::FRAME_TYPE);
455 }
456
457 void
458 VideoContent::unset_colour_conversion ()
459 {
460         maybe_set (_colour_conversion, boost::optional<ColourConversion> (), VideoContentProperty::COLOUR_CONVERSION);
461 }
462
463 void
464 VideoContent::set_colour_conversion (ColourConversion c)
465 {
466         maybe_set (_colour_conversion, c, VideoContentProperty::COLOUR_CONVERSION);
467 }
468
469 void
470 VideoContent::set_fade_in (Frame t)
471 {
472         maybe_set (_fade_in, t, VideoContentProperty::FADE_IN);
473 }
474
475 void
476 VideoContent::set_fade_out (Frame t)
477 {
478         maybe_set (_fade_out, t, VideoContentProperty::FADE_OUT);
479 }
480
481 void
482 VideoContent::take_settings_from (shared_ptr<const VideoContent> c)
483 {
484         if (c->_colour_conversion) {
485                 set_colour_conversion (c->_colour_conversion.get());
486         } else {
487                 unset_colour_conversion ();
488         }
489         set_frame_type (c->_frame_type);
490         set_left_crop (c->_crop.left);
491         set_right_crop (c->_crop.right);
492         set_top_crop (c->_crop.top);
493         set_bottom_crop (c->_crop.bottom);
494         set_scale (c->_scale);
495         set_fade_in (c->_fade_in);
496         set_fade_out (c->_fade_out);
497 }
498
499 void
500 VideoContent::modify_position (DCPTime& pos) const
501 {
502         pos = pos.round (_parent->film()->video_frame_rate());
503 }
504
505 void
506 VideoContent::modify_trim_start (ContentTime& trim) const
507 {
508         if (_parent->video_frame_rate()) {
509                 trim = trim.round (_parent->video_frame_rate().get());
510         }
511 }