Forward-port move of content description generation to lib/ rather than wx/.
[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 (Config::instance()->default_scale ())
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 (Config::instance()->default_scale ())
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 (Config::instance()->default_scale ())
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         _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"));
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         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         _crop.as_xml (node);
194         _scale.as_xml (node->add_child("Scale"));
195         if (_colour_conversion) {
196                 _colour_conversion.get().as_xml (node->add_child("ColourConversion"));
197         }
198         node->add_child("FadeIn")->add_child_text (raw_convert<string> (_fade_in.get ()));
199         node->add_child("FadeOut")->add_child_text (raw_convert<string> (_fade_out.get ()));
200 }
201
202 void
203 VideoContent::set_default_colour_conversion (bool signal)
204 {
205         {
206                 boost::mutex::scoped_lock lm (_mutex);
207                 _colour_conversion = PresetColourConversion (_("sRGB"), 2.4, true, dcp::colour_matrix::srgb_to_xyz, 2.6).conversion;
208         }
209
210         if (signal) {
211                 signal_changed (VideoContentProperty::COLOUR_CONVERSION);
212         }
213 }
214
215 void
216 VideoContent::take_from_video_examiner (shared_ptr<VideoExaminer> d)
217 {
218         /* These examiner calls could call other content methods which take a lock on the mutex */
219         dcp::Size const vs = d->video_size ();
220         float const vfr = d->video_frame_rate ();
221         ContentTime vl = d->video_length ();
222
223         {
224                 boost::mutex::scoped_lock lm (_mutex);
225                 _video_size = vs;
226                 _video_frame_rate = vfr;
227                 _video_length = vl;
228         }
229
230         shared_ptr<const Film> film = _film.lock ();
231         assert (film);
232         LOG_GENERAL ("Video length obtained from header as %1 frames", _video_length.frames (_video_frame_rate));
233         
234         signal_changed (VideoContentProperty::VIDEO_SIZE);
235         signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
236         signal_changed (ContentProperty::LENGTH);
237 }
238
239
240 string
241 VideoContent::information () const
242 {
243         if (video_size().width == 0 || video_size().height == 0) {
244                 return "";
245         }
246         
247         SafeStringStream s;
248
249         s << String::compose (
250                 _("%1x%2 pixels (%3:1)"),
251                 video_size().width,
252                 video_size().height,
253                 setprecision (3), video_size().ratio ()
254                 );
255         
256         return s.str ();
257 }
258
259 void
260 VideoContent::set_left_crop (int c)
261 {
262         {
263                 boost::mutex::scoped_lock lm (_mutex);
264                 
265                 if (_crop.left == c) {
266                         return;
267                 }
268                 
269                 _crop.left = c;
270         }
271         
272         signal_changed (VideoContentProperty::VIDEO_CROP);
273 }
274
275 void
276 VideoContent::set_right_crop (int c)
277 {
278         {
279                 boost::mutex::scoped_lock lm (_mutex);
280                 if (_crop.right == c) {
281                         return;
282                 }
283                 
284                 _crop.right = c;
285         }
286         
287         signal_changed (VideoContentProperty::VIDEO_CROP);
288 }
289
290 void
291 VideoContent::set_top_crop (int c)
292 {
293         {
294                 boost::mutex::scoped_lock lm (_mutex);
295                 if (_crop.top == c) {
296                         return;
297                 }
298                 
299                 _crop.top = c;
300         }
301         
302         signal_changed (VideoContentProperty::VIDEO_CROP);
303 }
304
305 void
306 VideoContent::set_bottom_crop (int c)
307 {
308         {
309                 boost::mutex::scoped_lock lm (_mutex);
310                 if (_crop.bottom == c) {
311                         return;
312                 }
313                 
314                 _crop.bottom = c;
315         }
316
317         signal_changed (VideoContentProperty::VIDEO_CROP);
318 }
319
320 void
321 VideoContent::set_scale (VideoContentScale s)
322 {
323         {
324                 boost::mutex::scoped_lock lm (_mutex);
325                 if (_scale == s) {
326                         return;
327                 }
328
329                 _scale = s;
330         }
331
332         signal_changed (VideoContentProperty::VIDEO_SCALE);
333 }
334
335 /** @return string which includes everything about how this content looks */
336 string
337 VideoContent::identifier () const
338 {
339         SafeStringStream s;
340         s << Content::identifier()
341           << "_" << crop().left
342           << "_" << crop().right
343           << "_" << crop().top
344           << "_" << crop().bottom
345           << "_" << scale().id();
346
347         if (colour_conversion()) {
348                 s << "_" << colour_conversion().get().identifier ();
349         }
350
351         return s.str ();
352 }
353
354 void
355 VideoContent::set_video_frame_type (VideoFrameType t)
356 {
357         {
358                 boost::mutex::scoped_lock lm (_mutex);
359                 _video_frame_type = t;
360         }
361
362         signal_changed (VideoContentProperty::VIDEO_FRAME_TYPE);
363 }
364
365 string
366 VideoContent::technical_summary () const
367 {
368         return String::compose (
369                 "video: length %1, size %2x%3, rate %4",
370                 video_length_after_3d_combine().seconds(),
371                 video_size().width,
372                 video_size().height,
373                 video_frame_rate()
374                 );
375 }
376
377 dcp::Size
378 VideoContent::video_size_after_3d_split () const
379 {
380         dcp::Size const s = video_size ();
381         switch (video_frame_type ()) {
382         case VIDEO_FRAME_TYPE_2D:
383         case VIDEO_FRAME_TYPE_3D_ALTERNATE:
384         case VIDEO_FRAME_TYPE_3D_LEFT:
385         case VIDEO_FRAME_TYPE_3D_RIGHT:
386                 return s;
387         case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
388                 return dcp::Size (s.width / 2, s.height);
389         case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
390                 return dcp::Size (s.width, s.height / 2);
391         }
392
393         assert (false);
394 }
395
396 void
397 VideoContent::unset_colour_conversion ()
398 {
399         {
400                 boost::mutex::scoped_lock lm (_mutex);
401                 _colour_conversion = boost::optional<ColourConversion> ();
402         }
403
404         signal_changed (VideoContentProperty::COLOUR_CONVERSION);
405 }
406
407 void
408 VideoContent::set_colour_conversion (ColourConversion c)
409 {
410         {
411                 boost::mutex::scoped_lock lm (_mutex);
412                 _colour_conversion = c;
413         }
414
415         signal_changed (VideoContentProperty::COLOUR_CONVERSION);
416 }
417
418 void
419 VideoContent::set_fade_in (ContentTime t)
420 {
421         {
422                 boost::mutex::scoped_lock lm (_mutex);
423                 _fade_in = t;
424         }
425
426         signal_changed (VideoContentProperty::VIDEO_FADE_IN);
427 }
428
429 void
430 VideoContent::set_fade_out (ContentTime t)
431 {
432         {
433                 boost::mutex::scoped_lock lm (_mutex);
434                 _fade_out = t;
435         }
436
437         signal_changed (VideoContentProperty::VIDEO_FADE_OUT);
438 }
439
440 /** @return Video size after 3D split and crop */
441 dcp::Size
442 VideoContent::video_size_after_crop () const
443 {
444         return crop().apply (video_size_after_3d_split ());
445 }
446
447 /** @param t A time offset from the start of this piece of content.
448  *  @return Corresponding time with respect to the content.
449  */
450 ContentTime
451 VideoContent::dcp_time_to_content_time (DCPTime t) const
452 {
453         shared_ptr<const Film> film = _film.lock ();
454         assert (film);
455         return ContentTime (t, FrameRateChange (video_frame_rate(), film->video_frame_rate()));
456 }
457
458 void
459 VideoContent::scale_and_crop_to_fit_width ()
460 {
461         shared_ptr<const Film> film = _film.lock ();
462         assert (film);
463
464         set_scale (VideoContentScale (film->container ()));
465
466         int const crop = max (0, int (video_size().height - double (film->frame_size().height) * video_size().width / film->frame_size().width));
467         set_top_crop (crop / 2);
468         set_bottom_crop (crop / 2);
469 }
470
471 void
472 VideoContent::scale_and_crop_to_fit_height ()
473 {
474         shared_ptr<const Film> film = _film.lock ();
475         assert (film);
476
477         set_scale (VideoContentScale (film->container ()));
478
479         int const crop = max (0, int (video_size().width - double (film->frame_size().width) * video_size().height / film->frame_size().height));
480         set_left_crop (crop / 2);
481         set_right_crop (crop / 2);
482 }
483
484 void
485 VideoContent::set_video_frame_rate (float r)
486 {
487         {
488                 boost::mutex::scoped_lock lm (_mutex);
489                 if (_video_frame_rate == r) {
490                         return;
491                 }
492                 
493                 _video_frame_rate = r;
494         }
495         
496         signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
497 }
498
499 optional<float>
500 VideoContent::fade (VideoFrame f) const
501 {
502         assert (f >= 0);
503         
504         if (f < fade_in().frames (video_frame_rate ())) {
505                 return float (f) / _fade_in.frames (video_frame_rate ());
506         }
507
508         VideoFrame fade_out_start = ContentTime (video_length() - fade_out()).frames (video_frame_rate ());
509         if (f >= fade_out_start) {
510                 return 1 - float (f - fade_out_start) / fade_out().frames (video_frame_rate ());
511         }
512
513         return optional<float> ();
514 }
515
516 string
517 VideoContent::processing_description () const
518 {
519         /* stringstream is OK here as this string is just for presentation to the user */
520         stringstream d;
521
522         if (video_size().width && video_size().height) {
523                 d << String::compose (
524                         _("Content video is %1x%2"),
525                         video_size_after_3d_split().width,
526                         video_size_after_3d_split().height
527                         );
528
529                 d << " (" << fixed << setprecision(2) << video_size_after_3d_split().ratio() << ":1)\n";
530         }
531
532         if ((crop().left || crop().right || crop().top || crop().bottom) && video_size() != dcp::Size (0, 0)) {
533                 dcp::Size cropped = video_size_after_crop ();
534                 d << String::compose (
535                         _("Cropped to %1x%2"),
536                         cropped.width, cropped.height
537                         );
538
539                 d << " (" << fixed << setprecision(2) << cropped.ratio () << ":1)\n";
540         }
541
542         shared_ptr<const Film> film = _film.lock ();
543         assert (film);
544
545         dcp::Size const container_size = film->frame_size ();
546         dcp::Size const scaled = scale().size (dynamic_pointer_cast<const VideoContent> (shared_from_this ()), container_size, container_size, 1);
547
548         if (scaled != video_size_after_crop ()) {
549                 d << String::compose (
550                         _("Scaled to %1x%2"),
551                         scaled.width, scaled.height
552                         );
553
554                 d << " (" << fixed << setprecision(2) << scaled.ratio() << ":1)\n";
555         }
556         
557         if (scaled != container_size) {
558                 d << String::compose (
559                         _("Padded with black to %1x%2"),
560                         container_size.width, container_size.height
561                         );
562
563                 d << " (" << fixed << setprecision(2) << container_size.ratio () << ":1)\n";
564         }
565
566         d << _("Content frame rate");
567         d << " " << fixed << setprecision(4) << video_frame_rate() << "\n";
568         
569         FrameRateChange frc (video_frame_rate(), film->video_frame_rate ());
570         d << frc.description () << "\n";
571
572         return d.str ();
573 }