Use SafeStringStream instead of std::stringstream to try to fix random crashes on...
[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 #include "frame_rate_change.h"
34 #include "safe_stringstream.h"
35
36 #include "i18n.h"
37
38 int const VideoContentProperty::VIDEO_SIZE        = 0;
39 int const VideoContentProperty::VIDEO_FRAME_RATE  = 1;
40 int const VideoContentProperty::VIDEO_FRAME_TYPE  = 2;
41 int const VideoContentProperty::VIDEO_CROP        = 3;
42 int const VideoContentProperty::VIDEO_SCALE       = 4;
43 int const VideoContentProperty::COLOUR_CONVERSION = 5;
44
45 using std::string;
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 libdcp::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         , _original_video_frame_rate (0)
62         , _video_frame_rate (0)
63         , _video_frame_type (VIDEO_FRAME_TYPE_2D)
64         , _scale (Config::instance()->default_scale ())
65 {
66         setup_default_colour_conversion ();
67 }
68
69 VideoContent::VideoContent (shared_ptr<const Film> f, Time s, VideoContent::Frame len)
70         : Content (f, s)
71         , _video_length (len)
72         , _original_video_frame_rate (0)
73         , _video_frame_rate (0)
74         , _video_frame_type (VIDEO_FRAME_TYPE_2D)
75         , _scale (Config::instance()->default_scale ())
76 {
77         setup_default_colour_conversion ();
78 }
79
80 VideoContent::VideoContent (shared_ptr<const Film> f, boost::filesystem::path p)
81         : Content (f, p)
82         , _video_length (0)
83         , _original_video_frame_rate (0)
84         , _video_frame_rate (0)
85         , _video_frame_type (VIDEO_FRAME_TYPE_2D)
86         , _scale (Config::instance()->default_scale ())
87 {
88         setup_default_colour_conversion ();
89 }
90
91 VideoContent::VideoContent (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node, int version)
92         : Content (f, node)
93 {
94         _video_length = node->number_child<VideoContent::Frame> ("VideoLength");
95         _video_size.width = node->number_child<int> ("VideoWidth");
96         _video_size.height = node->number_child<int> ("VideoHeight");
97         _video_frame_rate = node->number_child<float> ("VideoFrameRate");
98         _original_video_frame_rate = node->optional_number_child<float> ("OriginalVideoFrameRate").get_value_or (_video_frame_rate);
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         _original_video_frame_rate = ref->original_video_frame_rate ();
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));
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("OriginalVideoFrameRate")->add_child_text (raw_convert<string> (_original_video_frame_rate));
172         node->add_child("VideoFrameType")->add_child_text (raw_convert<string> (static_cast<int> (_video_frame_type)));
173         _crop.as_xml (node);
174         _scale.as_xml (node->add_child("Scale"));
175         _colour_conversion.as_xml (node->add_child("ColourConversion"));
176 }
177
178 void
179 VideoContent::setup_default_colour_conversion ()
180 {
181         _colour_conversion = PresetColourConversion (_("sRGB"), 2.4, true, libdcp::colour_matrix::srgb_to_xyz, 2.6).conversion;
182 }
183
184 void
185 VideoContent::take_from_video_examiner (shared_ptr<VideoExaminer> d)
186 {
187         /* These examiner calls could call other content methods which take a lock on the mutex */
188         libdcp::Size const vs = d->video_size ();
189         float const vfr = d->video_frame_rate ();
190         
191         {
192                 boost::mutex::scoped_lock lm (_mutex);
193                 _video_size = vs;
194                 _video_frame_rate = vfr;
195                 _original_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         SafeStringStream 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         SafeStringStream 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(), video_size().width, video_size().height, video_frame_rate()
331                 );
332 }
333
334 libdcp::Size
335 VideoContent::video_size_after_3d_split () const
336 {
337         libdcp::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 libdcp::Size (s.width / 2, s.height);
346         case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
347                 return libdcp::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 libdcp::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 frame index.
373  */
374 VideoContent::Frame
375 VideoContent::time_to_content_video_frames (Time t) const
376 {
377         shared_ptr<const Film> film = _film.lock ();
378         assert (film);
379         
380         FrameRateChange frc (video_frame_rate(), film->video_frame_rate());
381
382         /* Here we are converting from time (in the DCP) to a frame number in the content.
383            Hence we need to use the DCP's frame rate and the double/skip correction, not
384            the source's rate.
385         */
386         return t * film->video_frame_rate() / (frc.factor() * TIME_HZ);
387 }
388
389 void
390 VideoContent::scale_and_crop_to_fit_width ()
391 {
392         shared_ptr<const Film> film = _film.lock ();
393         assert (film);
394
395         set_scale (VideoContentScale (film->container ()));
396
397         int const crop = max (0, int (video_size().height - double (film->frame_size().height) * video_size().width / film->frame_size().width));
398         set_top_crop (crop / 2);
399         set_bottom_crop (crop / 2);
400 }
401
402 void
403 VideoContent::scale_and_crop_to_fit_height ()
404 {
405         shared_ptr<const Film> film = _film.lock ();
406         assert (film);
407
408         set_scale (VideoContentScale (film->container ()));
409
410         int const crop = max (0, int (video_size().width - double (film->frame_size().width) * video_size().height / film->frame_size().height));
411         set_left_crop (crop / 2);
412         set_right_crop (crop / 2);
413 }
414
415 void
416 VideoContent::set_video_frame_rate (float r)
417 {
418         {
419                 boost::mutex::scoped_lock lm (_mutex);
420                 if (_video_frame_rate == r) {
421                         return;
422                 }
423                 
424                 _video_frame_rate = r;
425         }
426         
427         signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
428 }
429
430 VideoContentScale::VideoContentScale (Ratio const * r)
431         : _ratio (r)
432         , _scale (true)
433 {
434
435 }
436
437 VideoContentScale::VideoContentScale ()
438         : _ratio (0)
439         , _scale (false)
440 {
441
442 }
443
444 VideoContentScale::VideoContentScale (bool scale)
445         : _ratio (0)
446         , _scale (scale)
447 {
448
449 }
450
451 VideoContentScale::VideoContentScale (shared_ptr<cxml::Node> node)
452         : _ratio (0)
453         , _scale (true)
454 {
455         optional<string> r = node->optional_string_child ("Ratio");
456         if (r) {
457                 _ratio = Ratio::from_id (r.get ());
458         } else {
459                 _scale = node->bool_child ("Scale");
460         }
461 }
462
463 void
464 VideoContentScale::as_xml (xmlpp::Node* node) const
465 {
466         if (_ratio) {
467                 node->add_child("Ratio")->add_child_text (_ratio->id ());
468         } else {
469                 node->add_child("Scale")->add_child_text (_scale ? "1" : "0");
470         }
471 }
472
473 string
474 VideoContentScale::id () const
475 {
476         SafeStringStream s;
477         
478         if (_ratio) {
479                 s << _ratio->id () << "_";
480         } else {
481                 s << (_scale ? "S1" : "S0");
482         }
483         
484         return s.str ();
485 }
486
487 string
488 VideoContentScale::name () const
489 {
490         if (_ratio) {
491                 return _ratio->nickname ();
492         }
493
494         if (_scale) {
495                 return _("No stretch");
496         }
497
498         return _("No scale");
499 }
500
501 /** @param display_container Size of the container that we are displaying this content in.
502  *  @param film_container The size of the film's image.
503  */
504 libdcp::Size
505 VideoContentScale::size (shared_ptr<const VideoContent> c, libdcp::Size display_container, libdcp::Size film_container) const
506 {
507         if (_ratio) {
508                 return fit_ratio_within (_ratio->ratio (), display_container);
509         }
510
511         libdcp::Size const ac = c->video_size_after_crop ();
512
513         /* Force scale if the film_container is smaller than the content's image */
514         if (_scale || film_container.width < ac.width || film_container.height < ac.height) {
515                 return fit_ratio_within (ac.ratio (), display_container);
516         }
517
518         /* Scale the image so that it will be in the right place in film_container, even if display_container is a
519            different size.
520         */
521         return libdcp::Size (
522                 c->video_size().width  * float(display_container.width)  / film_container.width,
523                 c->video_size().height * float(display_container.height) / film_container.height
524                 );
525 }
526
527 void
528 VideoContentScale::setup_scales ()
529 {
530         vector<Ratio const *> ratios = Ratio::all ();
531         for (vector<Ratio const *>::const_iterator i = ratios.begin(); i != ratios.end(); ++i) {
532                 _scales.push_back (VideoContentScale (*i));
533         }
534
535         _scales.push_back (VideoContentScale (true));
536         _scales.push_back (VideoContentScale (false));
537 }
538
539 bool
540 operator== (VideoContentScale const & a, VideoContentScale const & b)
541 {
542         return (a.ratio() == b.ratio() && a.scale() == b.scale());
543 }
544
545 bool
546 operator!= (VideoContentScale const & a, VideoContentScale const & b)
547 {
548         return (a.ratio() != b.ratio() || a.scale() != b.scale());
549 }