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