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