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