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