No-op; fix GPL address and use the explicit-program-name version.
[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 "safe_stringstream.h"
34 #include "raw_convert.h"
35 #include <libcxml/cxml.h>
36 #include <dcp/colour_matrix.h>
37 #include <libxml++/libxml++.h>
38 #include <iomanip>
39 #include <iostream>
40
41 #include "i18n.h"
42
43 #define LOG_GENERAL(...) _parent->film()->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL);
44
45 int const VideoContentProperty::SIZE      = 0;
46 int const VideoContentProperty::FRAME_TYPE  = 1;
47 int const VideoContentProperty::CROP      = 2;
48 int const VideoContentProperty::SCALE     = 3;
49 int const VideoContentProperty::COLOUR_CONVERSION = 4;
50 int const VideoContentProperty::FADE_IN     = 5;
51 int const VideoContentProperty::FADE_OUT    = 6;
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)
68         : ContentPart (parent)
69         , _length (0)
70         , _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 shared_ptr<VideoContent>
80 VideoContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version)
81 {
82         if (!node->optional_number_child<int> ("VideoWidth")) {
83                 return shared_ptr<VideoContent> ();
84         }
85
86         return shared_ptr<VideoContent> (new VideoContent (parent, node, version));
87 }
88
89 VideoContent::VideoContent (Content* parent, cxml::ConstNodePtr node, int version)
90         : ContentPart (parent)
91 {
92         _size.width = node->number_child<int> ("VideoWidth");
93         _size.height = node->number_child<int> ("VideoHeight");
94
95         /* Backwards compatibility */
96         optional<double> r = node->optional_number_child<double>("VideoFrameRate");
97         if (r) {
98                 _parent->set_video_frame_rate (r.get ());
99         }
100
101         _length = node->number_child<Frame> ("VideoLength");
102         _frame_type = static_cast<VideoFrameType> (node->number_child<int> ("VideoFrameType"));
103         _sample_aspect_ratio = node->optional_number_child<double> ("SampleAspectRatio");
104         _crop.left = node->number_child<int> ("LeftCrop");
105         _crop.right = node->number_child<int> ("RightCrop");
106         _crop.top = node->number_child<int> ("TopCrop");
107         _crop.bottom = node->number_child<int> ("BottomCrop");
108
109         if (version <= 7) {
110                 optional<string> r = node->optional_string_child ("Ratio");
111                 if (r) {
112                         _scale = VideoContentScale (Ratio::from_id (r.get ()));
113                 }
114         } else {
115                 _scale = VideoContentScale (node->node_child ("Scale"));
116         }
117
118
119         if (node->optional_node_child ("ColourConversion")) {
120                 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"), version);
121         }
122
123         _yuv = node->optional_bool_child("YUV").get_value_or (true);
124
125         if (version >= 32) {
126                 _fade_in = node->number_child<Frame> ("FadeIn");
127                 _fade_out = node->number_child<Frame> ("FadeOut");
128         } else {
129                 _fade_in = _fade_out = 0;
130         }
131 }
132
133 VideoContent::VideoContent (Content* parent, vector<shared_ptr<Content> > c)
134         : ContentPart (parent)
135         , _length (0)
136         , _yuv (false)
137 {
138         shared_ptr<VideoContent> ref = c[0]->video;
139         DCPOMATIC_ASSERT (ref);
140
141         for (size_t i = 1; i < c.size(); ++i) {
142
143                 if (c[i]->video->size() != ref->size()) {
144                         throw JoinError (_("Content to be joined must have the same picture size."));
145                 }
146
147                 if (c[i]->video->frame_type() != ref->frame_type()) {
148                         throw JoinError (_("Content to be joined must have the same video frame type."));
149                 }
150
151                 if (c[i]->video->crop() != ref->crop()) {
152                         throw JoinError (_("Content to be joined must have the same crop."));
153                 }
154
155                 if (c[i]->video->scale() != ref->scale()) {
156                         throw JoinError (_("Content to be joined must have the same scale setting."));
157                 }
158
159                 if (c[i]->video->colour_conversion() != ref->colour_conversion()) {
160                         throw JoinError (_("Content to be joined must have the same colour conversion."));
161                 }
162
163                 if (c[i]->video->fade_in() != ref->fade_in() || c[i]->video->fade_out() != ref->fade_out()) {
164                         throw JoinError (_("Content to be joined must have the same fades."));
165                 }
166
167                 _length += c[i]->video->length ();
168
169                 if (c[i]->video->yuv ()) {
170                         _yuv = true;
171                 }
172         }
173
174         _size = ref->size ();
175         _frame_type = ref->frame_type ();
176         _crop = ref->crop ();
177         _scale = ref->scale ();
178         _colour_conversion = ref->colour_conversion ();
179         _fade_in = ref->fade_in ();
180         _fade_out = ref->fade_out ();
181 }
182
183 void
184 VideoContent::as_xml (xmlpp::Node* node) const
185 {
186         boost::mutex::scoped_lock lm (_mutex);
187         node->add_child("VideoLength")->add_child_text (raw_convert<string> (_length));
188         node->add_child("VideoWidth")->add_child_text (raw_convert<string> (_size.width));
189         node->add_child("VideoHeight")->add_child_text (raw_convert<string> (_size.height));
190         node->add_child("VideoFrameType")->add_child_text (raw_convert<string> (static_cast<int> (_frame_type)));
191         if (_sample_aspect_ratio) {
192                 node->add_child("SampleAspectRatio")->add_child_text (raw_convert<string> (_sample_aspect_ratio.get ()));
193         }
194         _crop.as_xml (node);
195         _scale.as_xml (node->add_child("Scale"));
196         if (_colour_conversion) {
197                 _colour_conversion.get().as_xml (node->add_child("ColourConversion"));
198         }
199         node->add_child("YUV")->add_child_text (_yuv ? "1" : "0");
200         node->add_child("FadeIn")->add_child_text (raw_convert<string> (_fade_in));
201         node->add_child("FadeOut")->add_child_text (raw_convert<string> (_fade_out));
202 }
203
204 void
205 VideoContent::take_from_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         Frame vl = d->video_length ();
210         optional<double> const ar = d->sample_aspect_ratio ();
211         bool const yuv = d->yuv ();
212
213         {
214                 boost::mutex::scoped_lock lm (_mutex);
215                 _size = vs;
216                 _length = vl;
217                 _sample_aspect_ratio = ar;
218                 _yuv = yuv;
219
220                 /* Guess correct scale from size and sample aspect ratio */
221                 _scale = VideoContentScale (
222                         Ratio::nearest_from_ratio (double (_size.width) * ar.get_value_or (1) / _size.height)
223                         );
224         }
225
226         LOG_GENERAL ("Video length obtained from header as %1 frames", _length);
227
228         if (d->video_frame_rate()) {
229                 _parent->set_video_frame_rate (d->video_frame_rate().get());
230         }
231
232         _parent->signal_changed (VideoContentProperty::SIZE);
233         _parent->signal_changed (VideoContentProperty::SCALE);
234         _parent->signal_changed (ContentProperty::LENGTH);
235 }
236
237 /** @return string which includes everything about how this content looks */
238 string
239 VideoContent::identifier () const
240 {
241         SafeStringStream s;
242         s << crop().left
243           << "_" << crop().right
244           << "_" << crop().top
245           << "_" << crop().bottom
246           << "_" << scale().id()
247           << "_" << _fade_in
248           << "_" << _fade_out;
249
250         if (colour_conversion()) {
251                 s << "_" << colour_conversion().get().identifier ();
252         }
253
254         return s.str ();
255 }
256
257 string
258 VideoContent::technical_summary () const
259 {
260         string s = String::compose (
261                 N_("video: length %1 frames, size %2x%3"),
262                 length_after_3d_combine(),
263                 size().width,
264                 size().height
265                 );
266
267         if (sample_aspect_ratio ()) {
268                 s += String::compose (N_(", sample aspect ratio %1"), (sample_aspect_ratio().get ()));
269         }
270
271         return s;
272 }
273
274 dcp::Size
275 VideoContent::size_after_3d_split () const
276 {
277         dcp::Size const s = size ();
278         switch (frame_type ()) {
279         case VIDEO_FRAME_TYPE_2D:
280         case VIDEO_FRAME_TYPE_3D_ALTERNATE:
281         case VIDEO_FRAME_TYPE_3D_LEFT:
282         case VIDEO_FRAME_TYPE_3D_RIGHT:
283                 return s;
284         case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
285                 return dcp::Size (s.width / 2, s.height);
286         case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
287                 return dcp::Size (s.width, s.height / 2);
288         }
289
290         DCPOMATIC_ASSERT (false);
291 }
292
293 /** @return Video size after 3D split and crop */
294 dcp::Size
295 VideoContent::size_after_crop () const
296 {
297         return crop().apply (size_after_3d_split ());
298 }
299
300 void
301 VideoContent::scale_and_crop_to_fit_width ()
302 {
303         shared_ptr<const Film> film = _parent->film ();
304         set_scale (VideoContentScale (film->container ()));
305
306         int const crop = max (0, int (size().height - double (film->frame_size().height) * size().width / film->frame_size().width));
307         set_left_crop (0);
308         set_right_crop (0);
309         set_top_crop (crop / 2);
310         set_bottom_crop (crop / 2);
311 }
312
313 void
314 VideoContent::scale_and_crop_to_fit_height ()
315 {
316         shared_ptr<const Film> film = _parent->film ();
317         set_scale (VideoContentScale (film->container ()));
318
319         int const crop = max (0, int (size().width - double (film->frame_size().width) * size().height / film->frame_size().height));
320         set_left_crop (crop / 2);
321         set_right_crop (crop / 2);
322         set_top_crop (0);
323         set_bottom_crop (0);
324 }
325
326 /** @param f Frame index within the whole (untrimmed) content */
327 optional<double>
328 VideoContent::fade (Frame f) const
329 {
330         DCPOMATIC_ASSERT (f >= 0);
331
332         shared_ptr<const Film> film = _parent->film ();
333
334         double const vfr = _parent->active_video_frame_rate ();
335
336         Frame const ts = _parent->trim_start().frames_round(vfr);
337         if ((f - ts) < fade_in()) {
338                 return double (f - ts) / fade_in();
339         }
340
341         Frame fade_out_start = length() - _parent->trim_end().frames_round(vfr) - fade_out();
342         if (f >= fade_out_start) {
343                 return 1 - double (f - fade_out_start) / fade_out();
344         }
345
346         return optional<double> ();
347 }
348
349 string
350 VideoContent::processing_description () const
351 {
352         /* stringstream is OK here as this string is just for presentation to the user */
353         SafeStringStream d;
354
355         if (size().width && size().height) {
356                 d << String::compose (
357                         _("Content video is %1x%2"),
358                         size_after_3d_split().width,
359                         size_after_3d_split().height
360                         );
361
362
363                 double ratio = size_after_3d_split().ratio ();
364
365                 if (sample_aspect_ratio ()) {
366                         d << ", " << _("pixel aspect ratio") << " " << fixed << setprecision(2) << sample_aspect_ratio().get () << ":1";
367                         ratio *= sample_aspect_ratio().get ();
368                 }
369
370                 d << "\n" << _("Display aspect ratio") << " " << fixed << setprecision(2) << ratio << ":1\n";
371         }
372
373         if ((crop().left || crop().right || crop().top || crop().bottom) && size() != dcp::Size (0, 0)) {
374                 dcp::Size cropped = size_after_crop ();
375                 d << String::compose (
376                         _("Cropped to %1x%2"),
377                         cropped.width, cropped.height
378                         );
379
380                 d << " (" << fixed << setprecision(2) << cropped.ratio () << ":1)\n";
381         }
382
383         shared_ptr<const Film> film = _parent->film ();
384         dcp::Size const container_size = film->frame_size ();
385         dcp::Size const scaled = scale().size (shared_from_this(), container_size, container_size);
386
387         if (scaled != size_after_crop ()) {
388                 d << String::compose (
389                         _("Scaled to %1x%2"),
390                         scaled.width, scaled.height
391                         );
392
393                 d << " (" << fixed << setprecision(2) << scaled.ratio() << ":1)\n";
394         }
395
396         if (scaled != container_size) {
397                 d << String::compose (
398                         _("Padded with black to fit container %1 (%2x%3)"),
399                         film->container()->nickname (),
400                         container_size.width, container_size.height
401                         );
402
403                 d << " (" << fixed << setprecision(2) << container_size.ratio () << ":1)\n";
404         }
405
406         return d.str ();
407 }
408
409 void
410 VideoContent::add_properties (list<UserProperty>& p) const
411 {
412         p.push_back (UserProperty (_("Video"), _("Length"), raw_convert<string> (length ()), _("video frames")));
413         p.push_back (UserProperty (_("Video"), _("Size"), raw_convert<string> (size().width) + "x" + raw_convert<string> (size().height)));
414 }
415
416 void
417 VideoContent::set_length (Frame len)
418 {
419         maybe_set (_length, len, ContentProperty::LENGTH);
420 }
421
422 void
423 VideoContent::set_left_crop (int c)
424 {
425         maybe_set (_crop.left, c, VideoContentProperty::CROP);
426 }
427
428 void
429 VideoContent::set_right_crop (int c)
430 {
431         maybe_set (_crop.right, c, VideoContentProperty::CROP);
432 }
433
434 void
435 VideoContent::set_top_crop (int c)
436 {
437         maybe_set (_crop.top, c, VideoContentProperty::CROP);
438 }
439
440 void
441 VideoContent::set_bottom_crop (int c)
442 {
443         maybe_set (_crop.bottom, c, VideoContentProperty::CROP);
444 }
445
446 void
447 VideoContent::set_scale (VideoContentScale s)
448 {
449         maybe_set (_scale, s, VideoContentProperty::SCALE);
450 }
451
452 void
453 VideoContent::set_frame_type (VideoFrameType t)
454 {
455         maybe_set (_frame_type, t, VideoContentProperty::FRAME_TYPE);
456 }
457
458 void
459 VideoContent::unset_colour_conversion ()
460 {
461         maybe_set (_colour_conversion, boost::optional<ColourConversion> (), VideoContentProperty::COLOUR_CONVERSION);
462 }
463
464 void
465 VideoContent::set_colour_conversion (ColourConversion c)
466 {
467         maybe_set (_colour_conversion, c, VideoContentProperty::COLOUR_CONVERSION);
468 }
469
470 void
471 VideoContent::set_fade_in (Frame t)
472 {
473         maybe_set (_fade_in, t, VideoContentProperty::FADE_IN);
474 }
475
476 void
477 VideoContent::set_fade_out (Frame t)
478 {
479         maybe_set (_fade_out, t, VideoContentProperty::FADE_OUT);
480 }