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