Merge master.
[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
35 #include "i18n.h"
36
37 int const VideoContentProperty::VIDEO_SIZE        = 0;
38 int const VideoContentProperty::VIDEO_FRAME_RATE  = 1;
39 int const VideoContentProperty::VIDEO_FRAME_TYPE  = 2;
40 int const VideoContentProperty::VIDEO_CROP        = 3;
41 int const VideoContentProperty::VIDEO_SCALE       = 4;
42 int const VideoContentProperty::COLOUR_CONVERSION = 5;
43
44 using std::string;
45 using std::stringstream;
46 using std::setprecision;
47 using std::cout;
48 using std::vector;
49 using std::min;
50 using std::max;
51 using boost::shared_ptr;
52 using boost::optional;
53 using boost::dynamic_pointer_cast;
54 using dcp::raw_convert;
55
56 vector<VideoContentScale> VideoContentScale::_scales;
57
58 VideoContent::VideoContent (shared_ptr<const Film> f)
59         : Content (f)
60         , _video_length (0)
61         , _video_frame_rate (0)
62         , _video_frame_type (VIDEO_FRAME_TYPE_2D)
63         , _scale (Ratio::from_id ("185"))
64 {
65         setup_default_colour_conversion ();
66 }
67
68 VideoContent::VideoContent (shared_ptr<const Film> f, DCPTime s, ContentTime len)
69         : Content (f, s)
70         , _video_length (len)
71         , _video_frame_rate (0)
72         , _video_frame_type (VIDEO_FRAME_TYPE_2D)
73         , _scale (Ratio::from_id ("185"))
74 {
75         setup_default_colour_conversion ();
76 }
77
78 VideoContent::VideoContent (shared_ptr<const Film> f, boost::filesystem::path p)
79         : Content (f, p)
80         , _video_length (0)
81         , _video_frame_rate (0)
82         , _video_frame_type (VIDEO_FRAME_TYPE_2D)
83         , _scale (Ratio::from_id ("185"))
84 {
85         setup_default_colour_conversion ();
86 }
87
88 VideoContent::VideoContent (shared_ptr<const Film> f, cxml::ConstNodePtr node, int version)
89         : Content (f, node)
90 {
91         _video_size.width = node->number_child<int> ("VideoWidth");
92         _video_size.height = node->number_child<int> ("VideoHeight");
93         _video_frame_rate = node->number_child<float> ("VideoFrameRate");
94
95         if (version < 32) {
96                 /* DCP-o-matic 1.0 branch */
97                 _video_length = ContentTime::from_frames (node->number_child<int64_t> ("VideoLength"), _video_frame_rate);
98         } else {
99                 _video_length = ContentTime (node->number_child<int64_t> ("VideoLength"));
100         }
101         
102         _video_frame_type = static_cast<VideoFrameType> (node->number_child<int> ("VideoFrameType"));
103         _crop.left = node->number_child<int> ("LeftCrop");
104         _crop.right = node->number_child<int> ("RightCrop");
105         _crop.top = node->number_child<int> ("TopCrop");
106         _crop.bottom = node->number_child<int> ("BottomCrop");
107
108         if (version <= 7) {
109                 optional<string> r = node->optional_string_child ("Ratio");
110                 if (r) {
111                         _scale = VideoContentScale (Ratio::from_id (r.get ()));
112                 }
113         } else {
114                 _scale = VideoContentScale (node->node_child ("Scale"));
115         }
116         
117         _colour_conversion = ColourConversion (node->node_child ("ColourConversion"));
118 }
119
120 VideoContent::VideoContent (shared_ptr<const Film> f, vector<shared_ptr<Content> > c)
121         : Content (f, c)
122         , _video_length (0)
123 {
124         shared_ptr<VideoContent> ref = dynamic_pointer_cast<VideoContent> (c[0]);
125         assert (ref);
126
127         for (size_t i = 0; i < c.size(); ++i) {
128                 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (c[i]);
129
130                 if (vc->video_size() != ref->video_size()) {
131                         throw JoinError (_("Content to be joined must have the same picture size."));
132                 }
133
134                 if (vc->video_frame_rate() != ref->video_frame_rate()) {
135                         throw JoinError (_("Content to be joined must have the same video frame rate."));
136                 }
137
138                 if (vc->video_frame_type() != ref->video_frame_type()) {
139                         throw JoinError (_("Content to be joined must have the same video frame type."));
140                 }
141
142                 if (vc->crop() != ref->crop()) {
143                         throw JoinError (_("Content to be joined must have the same crop."));
144                 }
145
146                 if (vc->scale() != ref->scale()) {
147                         throw JoinError (_("Content to be joined must have the same scale setting."));
148                 }
149
150                 if (vc->colour_conversion() != ref->colour_conversion()) {
151                         throw JoinError (_("Content to be joined must have the same colour conversion."));
152                 }
153
154                 _video_length += vc->video_length ();
155         }
156
157         _video_size = ref->video_size ();
158         _video_frame_rate = ref->video_frame_rate ();
159         _video_frame_type = ref->video_frame_type ();
160         _crop = ref->crop ();
161         _scale = ref->scale ();
162         _colour_conversion = ref->colour_conversion ();
163 }
164
165 void
166 VideoContent::as_xml (xmlpp::Node* node) const
167 {
168         boost::mutex::scoped_lock lm (_mutex);
169         node->add_child("VideoLength")->add_child_text (raw_convert<string> (_video_length.get ()));
170         node->add_child("VideoWidth")->add_child_text (raw_convert<string> (_video_size.width));
171         node->add_child("VideoHeight")->add_child_text (raw_convert<string> (_video_size.height));
172         node->add_child("VideoFrameRate")->add_child_text (raw_convert<string> (_video_frame_rate));
173         node->add_child("VideoFrameType")->add_child_text (raw_convert<string> (static_cast<int> (_video_frame_type)));
174         _crop.as_xml (node);
175         _scale.as_xml (node->add_child("Scale"));
176         _colour_conversion.as_xml (node->add_child("ColourConversion"));
177 }
178
179 void
180 VideoContent::setup_default_colour_conversion ()
181 {
182         _colour_conversion = PresetColourConversion (_("sRGB"), 2.4, true, dcp::colour_matrix::srgb_to_xyz, 2.6).conversion;
183 }
184
185 void
186 VideoContent::take_from_video_examiner (shared_ptr<VideoExaminer> d)
187 {
188         /* These examiner calls could call other content methods which take a lock on the mutex */
189         dcp::Size const vs = d->video_size ();
190         float const vfr = d->video_frame_rate ();
191         
192         {
193                 boost::mutex::scoped_lock lm (_mutex);
194                 _video_size = vs;
195                 _video_frame_rate = vfr;
196         }
197         
198         signal_changed (VideoContentProperty::VIDEO_SIZE);
199         signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
200 }
201
202
203 string
204 VideoContent::information () const
205 {
206         if (video_size().width == 0 || video_size().height == 0) {
207                 return "";
208         }
209         
210         stringstream s;
211
212         s << String::compose (
213                 _("%1x%2 pixels (%3:1)"),
214                 video_size().width,
215                 video_size().height,
216                 setprecision (3), video_size().ratio ()
217                 );
218         
219         return s.str ();
220 }
221
222 void
223 VideoContent::set_left_crop (int c)
224 {
225         {
226                 boost::mutex::scoped_lock lm (_mutex);
227                 
228                 if (_crop.left == c) {
229                         return;
230                 }
231                 
232                 _crop.left = c;
233         }
234         
235         signal_changed (VideoContentProperty::VIDEO_CROP);
236 }
237
238 void
239 VideoContent::set_right_crop (int c)
240 {
241         {
242                 boost::mutex::scoped_lock lm (_mutex);
243                 if (_crop.right == c) {
244                         return;
245                 }
246                 
247                 _crop.right = c;
248         }
249         
250         signal_changed (VideoContentProperty::VIDEO_CROP);
251 }
252
253 void
254 VideoContent::set_top_crop (int c)
255 {
256         {
257                 boost::mutex::scoped_lock lm (_mutex);
258                 if (_crop.top == c) {
259                         return;
260                 }
261                 
262                 _crop.top = c;
263         }
264         
265         signal_changed (VideoContentProperty::VIDEO_CROP);
266 }
267
268 void
269 VideoContent::set_bottom_crop (int c)
270 {
271         {
272                 boost::mutex::scoped_lock lm (_mutex);
273                 if (_crop.bottom == c) {
274                         return;
275                 }
276                 
277                 _crop.bottom = c;
278         }
279
280         signal_changed (VideoContentProperty::VIDEO_CROP);
281 }
282
283 void
284 VideoContent::set_scale (VideoContentScale s)
285 {
286         {
287                 boost::mutex::scoped_lock lm (_mutex);
288                 if (_scale == s) {
289                         return;
290                 }
291
292                 _scale = s;
293         }
294
295         signal_changed (VideoContentProperty::VIDEO_SCALE);
296 }
297
298 /** @return string which includes everything about how this content looks */
299 string
300 VideoContent::identifier () const
301 {
302         stringstream s;
303         s << Content::identifier()
304           << "_" << crop().left
305           << "_" << crop().right
306           << "_" << crop().top
307           << "_" << crop().bottom
308           << "_" << scale().id()
309           << "_" << colour_conversion().identifier ();
310
311         return s.str ();
312 }
313
314 void
315 VideoContent::set_video_frame_type (VideoFrameType t)
316 {
317         {
318                 boost::mutex::scoped_lock lm (_mutex);
319                 _video_frame_type = t;
320         }
321
322         signal_changed (VideoContentProperty::VIDEO_FRAME_TYPE);
323 }
324
325 string
326 VideoContent::technical_summary () const
327 {
328         return String::compose (
329                 "video: length %1, size %2x%3, rate %4",
330                 video_length_after_3d_combine().seconds(),
331                 video_size().width,
332                 video_size().height,
333                 video_frame_rate()
334                 );
335 }
336
337 dcp::Size
338 VideoContent::video_size_after_3d_split () const
339 {
340         dcp::Size const s = video_size ();
341         switch (video_frame_type ()) {
342         case VIDEO_FRAME_TYPE_2D:
343         case VIDEO_FRAME_TYPE_3D_ALTERNATE:
344         case VIDEO_FRAME_TYPE_3D_LEFT:
345         case VIDEO_FRAME_TYPE_3D_RIGHT:
346                 return s;
347         case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
348                 return dcp::Size (s.width / 2, s.height);
349         case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
350                 return dcp::Size (s.width, s.height / 2);
351         }
352
353         assert (false);
354 }
355
356 void
357 VideoContent::set_colour_conversion (ColourConversion c)
358 {
359         {
360                 boost::mutex::scoped_lock lm (_mutex);
361                 _colour_conversion = c;
362         }
363
364         signal_changed (VideoContentProperty::COLOUR_CONVERSION);
365 }
366
367 /** @return Video size after 3D split and crop */
368 dcp::Size
369 VideoContent::video_size_after_crop () const
370 {
371         return crop().apply (video_size_after_3d_split ());
372 }
373
374 /** @param t A time offset from the start of this piece of content.
375  *  @return Corresponding time with respect to the content.
376  */
377 ContentTime
378 VideoContent::dcp_time_to_content_time (DCPTime t) const
379 {
380         shared_ptr<const Film> film = _film.lock ();
381         assert (film);
382         return ContentTime (t, FrameRateChange (video_frame_rate(), film->video_frame_rate()));
383 }
384
385 void
386 VideoContent::scale_and_crop_to_fit_width ()
387 {
388         shared_ptr<const Film> film = _film.lock ();
389         assert (film);
390
391         set_scale (VideoContentScale (film->container ()));
392
393         int const crop = max (0, int (video_size().height - double (film->frame_size().height) * video_size().width / film->frame_size().width));
394         set_top_crop (crop / 2);
395         set_bottom_crop (crop / 2);
396 }
397
398 void
399 VideoContent::scale_and_crop_to_fit_height ()
400 {
401         shared_ptr<const Film> film = _film.lock ();
402         assert (film);
403
404         set_scale (VideoContentScale (film->container ()));
405
406         int const crop = max (0, int (video_size().width - double (film->frame_size().width) * video_size().height / film->frame_size().height));
407         set_left_crop (crop / 2);
408         set_right_crop (crop / 2);
409 }
410
411 VideoContentScale::VideoContentScale (Ratio const * r)
412         : _ratio (r)
413         , _scale (true)
414 {
415
416 }
417
418 VideoContentScale::VideoContentScale ()
419         : _ratio (0)
420         , _scale (false)
421 {
422
423 }
424
425 VideoContentScale::VideoContentScale (bool scale)
426         : _ratio (0)
427         , _scale (scale)
428 {
429
430 }
431
432 VideoContentScale::VideoContentScale (cxml::NodePtr node)
433         : _ratio (0)
434         , _scale (true)
435 {
436         optional<string> r = node->optional_string_child ("Ratio");
437         if (r) {
438                 _ratio = Ratio::from_id (r.get ());
439         } else {
440                 _scale = node->bool_child ("Scale");
441         }
442 }
443
444 void
445 VideoContentScale::as_xml (xmlpp::Node* node) const
446 {
447         if (_ratio) {
448                 node->add_child("Ratio")->add_child_text (_ratio->id ());
449         } else {
450                 node->add_child("Scale")->add_child_text (_scale ? "1" : "0");
451         }
452 }
453
454 string
455 VideoContentScale::id () const
456 {
457         stringstream s;
458         
459         if (_ratio) {
460                 s << _ratio->id () << "_";
461         } else {
462                 s << (_scale ? "S1" : "S0");
463         }
464         
465         return s.str ();
466 }
467
468 string
469 VideoContentScale::name () const
470 {
471         if (_ratio) {
472                 return _ratio->nickname ();
473         }
474
475         if (_scale) {
476                 return _("No stretch");
477         }
478
479         return _("No scale");
480 }
481
482 /** @param display_container Size of the container that we are displaying this content in.
483  *  @param film_container The size of the film's image.
484  */
485 dcp::Size
486 VideoContentScale::size (shared_ptr<const VideoContent> c, dcp::Size display_container, dcp::Size film_container) const
487 {
488         if (_ratio) {
489                 return fit_ratio_within (_ratio->ratio (), display_container);
490         }
491
492         dcp::Size const ac = c->video_size_after_crop ();
493
494         /* Force scale if the film_container is smaller than the content's image */
495         if (_scale || film_container.width < ac.width || film_container.height < ac.height) {
496                 return fit_ratio_within (ac.ratio (), display_container);
497         }
498
499         /* Scale the image so that it will be in the right place in film_container, even if display_container is a
500            different size.
501         */
502         return dcp::Size (
503                 c->video_size().width  * float(display_container.width)  / film_container.width,
504                 c->video_size().height * float(display_container.height) / film_container.height
505                 );
506 }
507
508 void
509 VideoContentScale::setup_scales ()
510 {
511         vector<Ratio const *> ratios = Ratio::all ();
512         for (vector<Ratio const *>::const_iterator i = ratios.begin(); i != ratios.end(); ++i) {
513                 _scales.push_back (VideoContentScale (*i));
514         }
515
516         _scales.push_back (VideoContentScale (true));
517         _scales.push_back (VideoContentScale (false));
518 }
519
520 bool
521 operator== (VideoContentScale const & a, VideoContentScale const & b)
522 {
523         return (a.ratio() == b.ratio() && a.scale() == b.scale());
524 }
525
526 bool
527 operator!= (VideoContentScale const & a, VideoContentScale const & b)
528 {
529         return (a.ratio() != b.ratio() || a.scale() != b.scale());
530 }