Small clean up to video examiner use.
[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<ContentTime::Type> ("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         ContentTime vl = d->video_length ();
192         
193         {
194                 boost::mutex::scoped_lock lm (_mutex);
195                 _video_size = vs;
196                 _video_frame_rate = vfr;
197                 _video_length = vl;
198         }
199         
200         signal_changed (VideoContentProperty::VIDEO_SIZE);
201         signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
202         signal_changed (ContentProperty::LENGTH);
203 }
204
205
206 string
207 VideoContent::information () const
208 {
209         if (video_size().width == 0 || video_size().height == 0) {
210                 return "";
211         }
212         
213         stringstream s;
214
215         s << String::compose (
216                 _("%1x%2 pixels (%3:1)"),
217                 video_size().width,
218                 video_size().height,
219                 setprecision (3), video_size().ratio ()
220                 );
221         
222         return s.str ();
223 }
224
225 void
226 VideoContent::set_left_crop (int c)
227 {
228         {
229                 boost::mutex::scoped_lock lm (_mutex);
230                 
231                 if (_crop.left == c) {
232                         return;
233                 }
234                 
235                 _crop.left = c;
236         }
237         
238         signal_changed (VideoContentProperty::VIDEO_CROP);
239 }
240
241 void
242 VideoContent::set_right_crop (int c)
243 {
244         {
245                 boost::mutex::scoped_lock lm (_mutex);
246                 if (_crop.right == c) {
247                         return;
248                 }
249                 
250                 _crop.right = c;
251         }
252         
253         signal_changed (VideoContentProperty::VIDEO_CROP);
254 }
255
256 void
257 VideoContent::set_top_crop (int c)
258 {
259         {
260                 boost::mutex::scoped_lock lm (_mutex);
261                 if (_crop.top == c) {
262                         return;
263                 }
264                 
265                 _crop.top = c;
266         }
267         
268         signal_changed (VideoContentProperty::VIDEO_CROP);
269 }
270
271 void
272 VideoContent::set_bottom_crop (int c)
273 {
274         {
275                 boost::mutex::scoped_lock lm (_mutex);
276                 if (_crop.bottom == c) {
277                         return;
278                 }
279                 
280                 _crop.bottom = c;
281         }
282
283         signal_changed (VideoContentProperty::VIDEO_CROP);
284 }
285
286 void
287 VideoContent::set_scale (VideoContentScale s)
288 {
289         {
290                 boost::mutex::scoped_lock lm (_mutex);
291                 if (_scale == s) {
292                         return;
293                 }
294
295                 _scale = s;
296         }
297
298         signal_changed (VideoContentProperty::VIDEO_SCALE);
299 }
300
301 /** @return string which includes everything about how this content looks */
302 string
303 VideoContent::identifier () const
304 {
305         stringstream s;
306         s << Content::identifier()
307           << "_" << crop().left
308           << "_" << crop().right
309           << "_" << crop().top
310           << "_" << crop().bottom
311           << "_" << scale().id()
312           << "_" << colour_conversion().identifier ();
313
314         return s.str ();
315 }
316
317 void
318 VideoContent::set_video_frame_type (VideoFrameType t)
319 {
320         {
321                 boost::mutex::scoped_lock lm (_mutex);
322                 _video_frame_type = t;
323         }
324
325         signal_changed (VideoContentProperty::VIDEO_FRAME_TYPE);
326 }
327
328 string
329 VideoContent::technical_summary () const
330 {
331         return String::compose (
332                 "video: length %1, size %2x%3, rate %4",
333                 video_length_after_3d_combine().seconds(),
334                 video_size().width,
335                 video_size().height,
336                 video_frame_rate()
337                 );
338 }
339
340 dcp::Size
341 VideoContent::video_size_after_3d_split () const
342 {
343         dcp::Size const s = video_size ();
344         switch (video_frame_type ()) {
345         case VIDEO_FRAME_TYPE_2D:
346         case VIDEO_FRAME_TYPE_3D_ALTERNATE:
347         case VIDEO_FRAME_TYPE_3D_LEFT:
348         case VIDEO_FRAME_TYPE_3D_RIGHT:
349                 return s;
350         case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
351                 return dcp::Size (s.width / 2, s.height);
352         case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
353                 return dcp::Size (s.width, s.height / 2);
354         }
355
356         assert (false);
357 }
358
359 void
360 VideoContent::set_colour_conversion (ColourConversion c)
361 {
362         {
363                 boost::mutex::scoped_lock lm (_mutex);
364                 _colour_conversion = c;
365         }
366
367         signal_changed (VideoContentProperty::COLOUR_CONVERSION);
368 }
369
370 /** @return Video size after 3D split and crop */
371 dcp::Size
372 VideoContent::video_size_after_crop () const
373 {
374         return crop().apply (video_size_after_3d_split ());
375 }
376
377 /** @param t A time offset from the start of this piece of content.
378  *  @return Corresponding time with respect to the content.
379  */
380 ContentTime
381 VideoContent::dcp_time_to_content_time (DCPTime t) const
382 {
383         shared_ptr<const Film> film = _film.lock ();
384         assert (film);
385         return ContentTime (t, FrameRateChange (video_frame_rate(), film->video_frame_rate()));
386 }
387
388 void
389 VideoContent::scale_and_crop_to_fit_width ()
390 {
391         shared_ptr<const Film> film = _film.lock ();
392         assert (film);
393
394         set_scale (VideoContentScale (film->container ()));
395
396         int const crop = max (0, int (video_size().height - double (film->frame_size().height) * video_size().width / film->frame_size().width));
397         set_top_crop (crop / 2);
398         set_bottom_crop (crop / 2);
399 }
400
401 void
402 VideoContent::scale_and_crop_to_fit_height ()
403 {
404         shared_ptr<const Film> film = _film.lock ();
405         assert (film);
406
407         set_scale (VideoContentScale (film->container ()));
408
409         int const crop = max (0, int (video_size().width - double (film->frame_size().width) * video_size().height / film->frame_size().height));
410         set_left_crop (crop / 2);
411         set_right_crop (crop / 2);
412 }
413
414 void
415 VideoContent::set_video_frame_rate (float r)
416 {
417         {
418                 boost::mutex::scoped_lock lm (_mutex);
419                 if (_video_frame_rate == r) {
420                         return;
421                 }
422                 
423                 _video_frame_rate = r;
424         }
425         
426         signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
427 }
428
429 VideoContentScale::VideoContentScale (Ratio const * r)
430         : _ratio (r)
431         , _scale (true)
432 {
433
434 }
435
436 VideoContentScale::VideoContentScale ()
437         : _ratio (0)
438         , _scale (false)
439 {
440
441 }
442
443 VideoContentScale::VideoContentScale (bool scale)
444         : _ratio (0)
445         , _scale (scale)
446 {
447
448 }
449
450 VideoContentScale::VideoContentScale (cxml::NodePtr node)
451         : _ratio (0)
452         , _scale (true)
453 {
454         optional<string> r = node->optional_string_child ("Ratio");
455         if (r) {
456                 _ratio = Ratio::from_id (r.get ());
457         } else {
458                 _scale = node->bool_child ("Scale");
459         }
460 }
461
462 void
463 VideoContentScale::as_xml (xmlpp::Node* node) const
464 {
465         if (_ratio) {
466                 node->add_child("Ratio")->add_child_text (_ratio->id ());
467         } else {
468                 node->add_child("Scale")->add_child_text (_scale ? "1" : "0");
469         }
470 }
471
472 string
473 VideoContentScale::id () const
474 {
475         stringstream s;
476         
477         if (_ratio) {
478                 s << _ratio->id () << "_";
479         } else {
480                 s << (_scale ? "S1" : "S0");
481         }
482         
483         return s.str ();
484 }
485
486 string
487 VideoContentScale::name () const
488 {
489         if (_ratio) {
490                 return _ratio->nickname ();
491         }
492
493         if (_scale) {
494                 return _("No stretch");
495         }
496
497         return _("No scale");
498 }
499
500 /** @param display_container Size of the container that we are displaying this content in.
501  *  @param film_container The size of the film's image.
502  */
503 dcp::Size
504 VideoContentScale::size (shared_ptr<const VideoContent> c, dcp::Size display_container, dcp::Size film_container) const
505 {
506         if (_ratio) {
507                 return fit_ratio_within (_ratio->ratio (), display_container);
508         }
509
510         dcp::Size const ac = c->video_size_after_crop ();
511
512         /* Force scale if the film_container is smaller than the content's image */
513         if (_scale || film_container.width < ac.width || film_container.height < ac.height) {
514                 return fit_ratio_within (ac.ratio (), display_container);
515         }
516
517         /* Scale the image so that it will be in the right place in film_container, even if display_container is a
518            different size.
519         */
520         return dcp::Size (
521                 c->video_size().width  * float(display_container.width)  / film_container.width,
522                 c->video_size().height * float(display_container.height) / film_container.height
523                 );
524 }
525
526 void
527 VideoContentScale::setup_scales ()
528 {
529         vector<Ratio const *> ratios = Ratio::all ();
530         for (vector<Ratio const *>::const_iterator i = ratios.begin(); i != ratios.end(); ++i) {
531                 _scales.push_back (VideoContentScale (*i));
532         }
533
534         _scales.push_back (VideoContentScale (true));
535         _scales.push_back (VideoContentScale (false));
536 }
537
538 bool
539 operator== (VideoContentScale const & a, VideoContentScale const & b)
540 {
541         return (a.ratio() == b.ratio() && a.scale() == b.scale());
542 }
543
544 bool
545 operator!= (VideoContentScale const & a, VideoContentScale const & b)
546 {
547         return (a.ratio() != b.ratio() || a.scale() != b.scale());
548 }