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