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