Fix some stringstreams that had crept in.
[dcpomatic.git] / src / lib / video_content.cc
1 /*
2     Copyright (C) 2013-2015 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 "video_content.h"
21 #include "video_examiner.h"
22 #include "compose.hpp"
23 #include "ratio.h"
24 #include "config.h"
25 #include "colour_conversion.h"
26 #include "util.h"
27 #include "film.h"
28 #include "exceptions.h"
29 #include "frame_rate_change.h"
30 #include "log.h"
31 #include "safe_stringstream.h"
32 #include "raw_convert.h"
33 #include <libcxml/cxml.h>
34 #include <dcp/colour_matrix.h>
35 #include <libxml++/libxml++.h>
36 #include <iomanip>
37 #include <iostream>
38
39 #include "i18n.h"
40
41 #define LOG_GENERAL(...) film()->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL);
42
43 int const VideoContentProperty::VIDEO_SIZE        = 0;
44 int const VideoContentProperty::VIDEO_FRAME_RATE  = 1;
45 int const VideoContentProperty::VIDEO_FRAME_TYPE  = 2;
46 int const VideoContentProperty::VIDEO_CROP        = 3;
47 int const VideoContentProperty::VIDEO_SCALE       = 4;
48 int const VideoContentProperty::COLOUR_CONVERSION = 5;
49 int const VideoContentProperty::VIDEO_FADE_IN     = 6;
50 int const VideoContentProperty::VIDEO_FADE_OUT    = 7;
51
52 using std::string;
53 using std::setprecision;
54 using std::cout;
55 using std::vector;
56 using std::min;
57 using std::max;
58 using std::fixed;
59 using std::setprecision;
60 using std::list;
61 using std::pair;
62 using boost::shared_ptr;
63 using boost::optional;
64 using boost::dynamic_pointer_cast;
65
66 VideoContent::VideoContent (shared_ptr<const Film> film)
67         : Content (film)
68         , _video_length (0)
69         , _video_frame_type (VIDEO_FRAME_TYPE_2D)
70         , _scale (VideoContentScale (Ratio::from_id ("178")))
71         , _yuv (true)
72         , _fade_in (0)
73         , _fade_out (0)
74 {
75         set_default_colour_conversion ();
76 }
77
78 VideoContent::VideoContent (shared_ptr<const Film> film, DCPTime s, Frame len)
79         : Content (film, s)
80         , _video_length (len)
81         , _video_frame_type (VIDEO_FRAME_TYPE_2D)
82         , _scale (VideoContentScale (Ratio::from_id ("178")))
83         , _yuv (true)
84         , _fade_in (0)
85         , _fade_out (0)
86 {
87         set_default_colour_conversion ();
88 }
89
90 VideoContent::VideoContent (shared_ptr<const Film> film, boost::filesystem::path p)
91         : Content (film, p)
92         , _video_length (0)
93         , _video_frame_type (VIDEO_FRAME_TYPE_2D)
94         , _scale (VideoContentScale (Ratio::from_id ("178")))
95         , _yuv (true)
96         , _fade_in (0)
97         , _fade_out (0)
98 {
99         set_default_colour_conversion ();
100 }
101
102 VideoContent::VideoContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
103         : Content (film, node)
104 {
105         _video_size.width = node->number_child<int> ("VideoWidth");
106         _video_size.height = node->number_child<int> ("VideoHeight");
107         _video_frame_rate = node->optional_number_child<double> ("VideoFrameRate");
108         _video_length = node->number_child<Frame> ("VideoLength");
109         _video_frame_type = static_cast<VideoFrameType> (node->number_child<int> ("VideoFrameType"));
110         _sample_aspect_ratio = node->optional_number_child<double> ("SampleAspectRatio");
111         _crop.left = node->number_child<int> ("LeftCrop");
112         _crop.right = node->number_child<int> ("RightCrop");
113         _crop.top = node->number_child<int> ("TopCrop");
114         _crop.bottom = node->number_child<int> ("BottomCrop");
115
116         if (version <= 7) {
117                 optional<string> r = node->optional_string_child ("Ratio");
118                 if (r) {
119                         _scale = VideoContentScale (Ratio::from_id (r.get ()));
120                 }
121         } else {
122                 _scale = VideoContentScale (node->node_child ("Scale"));
123         }
124
125
126         if (node->optional_node_child ("ColourConversion")) {
127                 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"), version);
128         }
129
130         _yuv = node->optional_bool_child("YUV").get_value_or (true);
131
132         if (version >= 32) {
133                 _fade_in = node->number_child<Frame> ("FadeIn");
134                 _fade_out = node->number_child<Frame> ("FadeOut");
135         } else {
136                 _fade_in = _fade_out = 0;
137         }
138 }
139
140 VideoContent::VideoContent (shared_ptr<const Film> film, vector<shared_ptr<Content> > c)
141         : Content (film, c)
142         , _video_length (0)
143         , _yuv (false)
144 {
145         shared_ptr<VideoContent> ref = dynamic_pointer_cast<VideoContent> (c[0]);
146         DCPOMATIC_ASSERT (ref);
147
148         for (size_t i = 0; i < c.size(); ++i) {
149                 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (c[i]);
150
151                 if (vc->video_size() != ref->video_size()) {
152                         throw JoinError (_("Content to be joined must have the same picture size."));
153                 }
154
155                 if (vc->video_frame_rate() != ref->video_frame_rate()) {
156                         throw JoinError (_("Content to be joined must have the same video frame rate."));
157                 }
158
159                 if (vc->video_frame_type() != ref->video_frame_type()) {
160                         throw JoinError (_("Content to be joined must have the same video frame type."));
161                 }
162
163                 if (vc->crop() != ref->crop()) {
164                         throw JoinError (_("Content to be joined must have the same crop."));
165                 }
166
167                 if (vc->scale() != ref->scale()) {
168                         throw JoinError (_("Content to be joined must have the same scale setting."));
169                 }
170
171                 if (vc->colour_conversion() != ref->colour_conversion()) {
172                         throw JoinError (_("Content to be joined must have the same colour conversion."));
173                 }
174
175                 if (vc->fade_in() != ref->fade_in() || vc->fade_out() != ref->fade_out()) {
176                         throw JoinError (_("Content to be joined must have the same fades."));
177                 }
178
179                 _video_length += vc->video_length ();
180
181                 if (vc->yuv ()) {
182                         _yuv = true;
183                 }
184         }
185
186         _video_size = ref->video_size ();
187         _video_frame_rate = ref->video_frame_rate ();
188         _video_frame_type = ref->video_frame_type ();
189         _crop = ref->crop ();
190         _scale = ref->scale ();
191         _colour_conversion = ref->colour_conversion ();
192         _fade_in = ref->fade_in ();
193         _fade_out = ref->fade_out ();
194 }
195
196 void
197 VideoContent::as_xml (xmlpp::Node* node) const
198 {
199         boost::mutex::scoped_lock lm (_mutex);
200         node->add_child("VideoLength")->add_child_text (raw_convert<string> (_video_length));
201         node->add_child("VideoWidth")->add_child_text (raw_convert<string> (_video_size.width));
202         node->add_child("VideoHeight")->add_child_text (raw_convert<string> (_video_size.height));
203         if (_video_frame_rate) {
204                 node->add_child("VideoFrameRate")->add_child_text (raw_convert<string> (_video_frame_rate.get()));
205         }
206         node->add_child("VideoFrameType")->add_child_text (raw_convert<string> (static_cast<int> (_video_frame_type)));
207         if (_sample_aspect_ratio) {
208                 node->add_child("SampleAspectRatio")->add_child_text (raw_convert<string> (_sample_aspect_ratio.get ()));
209         }
210         _crop.as_xml (node);
211         _scale.as_xml (node->add_child("Scale"));
212         if (_colour_conversion) {
213                 _colour_conversion.get().as_xml (node->add_child("ColourConversion"));
214         }
215         node->add_child("YUV")->add_child_text (_yuv ? "1" : "0");
216         node->add_child("FadeIn")->add_child_text (raw_convert<string> (_fade_in));
217         node->add_child("FadeOut")->add_child_text (raw_convert<string> (_fade_out));
218 }
219
220 void
221 VideoContent::set_default_colour_conversion ()
222 {
223         /* If there's no better offer we'll use Rec. 709 */
224         boost::mutex::scoped_lock lm (_mutex);
225         _colour_conversion = PresetColourConversion::from_id ("rec709").conversion;
226 }
227
228 void
229 VideoContent::take_from_video_examiner (shared_ptr<VideoExaminer> d)
230 {
231         /* These examiner calls could call other content methods which take a lock on the mutex */
232         dcp::Size const vs = d->video_size ();
233         optional<double> const vfr = d->video_frame_rate ();
234         Frame vl = d->video_length ();
235         optional<double> const ar = d->sample_aspect_ratio ();
236         bool const yuv = d->yuv ();
237
238         {
239                 boost::mutex::scoped_lock lm (_mutex);
240                 _video_size = vs;
241                 _video_frame_rate = vfr;
242                 _video_length = vl;
243                 _sample_aspect_ratio = ar;
244                 _yuv = yuv;
245
246                 /* Guess correct scale from size and sample aspect ratio */
247                 _scale = VideoContentScale (
248                         Ratio::nearest_from_ratio (double (_video_size.width) * ar.get_value_or (1) / _video_size.height)
249                         );
250         }
251
252         LOG_GENERAL ("Video length obtained from header as %1 frames", _video_length);
253
254         set_default_colour_conversion ();
255
256         signal_changed (VideoContentProperty::VIDEO_SIZE);
257         signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
258         signal_changed (VideoContentProperty::VIDEO_SCALE);
259         signal_changed (ContentProperty::LENGTH);
260 }
261
262 void
263 VideoContent::set_left_crop (int c)
264 {
265         {
266                 boost::mutex::scoped_lock lm (_mutex);
267
268                 if (_crop.left == c) {
269                         return;
270                 }
271
272                 _crop.left = c;
273         }
274
275         signal_changed (VideoContentProperty::VIDEO_CROP);
276 }
277
278 void
279 VideoContent::set_right_crop (int c)
280 {
281         {
282                 boost::mutex::scoped_lock lm (_mutex);
283                 if (_crop.right == c) {
284                         return;
285                 }
286
287                 _crop.right = c;
288         }
289
290         signal_changed (VideoContentProperty::VIDEO_CROP);
291 }
292
293 void
294 VideoContent::set_top_crop (int c)
295 {
296         {
297                 boost::mutex::scoped_lock lm (_mutex);
298                 if (_crop.top == c) {
299                         return;
300                 }
301
302                 _crop.top = c;
303         }
304
305         signal_changed (VideoContentProperty::VIDEO_CROP);
306 }
307
308 void
309 VideoContent::set_bottom_crop (int c)
310 {
311         {
312                 boost::mutex::scoped_lock lm (_mutex);
313                 if (_crop.bottom == c) {
314                         return;
315                 }
316
317                 _crop.bottom = c;
318         }
319
320         signal_changed (VideoContentProperty::VIDEO_CROP);
321 }
322
323 void
324 VideoContent::set_scale (VideoContentScale s)
325 {
326         {
327                 boost::mutex::scoped_lock lm (_mutex);
328                 if (_scale == s) {
329                         return;
330                 }
331
332                 _scale = s;
333         }
334
335         signal_changed (VideoContentProperty::VIDEO_SCALE);
336 }
337
338 /** @return string which includes everything about how this content looks */
339 string
340 VideoContent::identifier () const
341 {
342         SafeStringStream s;
343         s << Content::identifier()
344           << "_" << crop().left
345           << "_" << crop().right
346           << "_" << crop().top
347           << "_" << crop().bottom
348           << "_" << scale().id()
349           << "_" << _fade_in
350           << "_" << _fade_out;
351
352         if (colour_conversion()) {
353                 s << "_" << colour_conversion().get().identifier ();
354         }
355
356         return s.str ();
357 }
358
359 void
360 VideoContent::set_video_frame_type (VideoFrameType t)
361 {
362         {
363                 boost::mutex::scoped_lock lm (_mutex);
364                 _video_frame_type = t;
365         }
366
367         signal_changed (VideoContentProperty::VIDEO_FRAME_TYPE);
368 }
369
370 string
371 VideoContent::technical_summary () const
372 {
373         string s = String::compose (
374                 N_("video: length %1 frames, size %2x%3, rate %4"),
375                 video_length_after_3d_combine(),
376                 video_size().width,
377                 video_size().height,
378                 video_frame_rate()
379                 );
380
381         if (sample_aspect_ratio ()) {
382                 s += String::compose (N_(", sample aspect ratio %1"), (sample_aspect_ratio().get ()));
383         }
384
385         return s;
386 }
387
388 dcp::Size
389 VideoContent::video_size_after_3d_split () const
390 {
391         dcp::Size const s = video_size ();
392         switch (video_frame_type ()) {
393         case VIDEO_FRAME_TYPE_2D:
394         case VIDEO_FRAME_TYPE_3D_ALTERNATE:
395         case VIDEO_FRAME_TYPE_3D_LEFT:
396         case VIDEO_FRAME_TYPE_3D_RIGHT:
397                 return s;
398         case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
399                 return dcp::Size (s.width / 2, s.height);
400         case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
401                 return dcp::Size (s.width, s.height / 2);
402         }
403
404         DCPOMATIC_ASSERT (false);
405 }
406
407 void
408 VideoContent::unset_colour_conversion ()
409 {
410         {
411                 boost::mutex::scoped_lock lm (_mutex);
412                 _colour_conversion = boost::optional<ColourConversion> ();
413         }
414
415         signal_changed (VideoContentProperty::COLOUR_CONVERSION);
416 }
417
418 void
419 VideoContent::set_colour_conversion (ColourConversion c)
420 {
421         {
422                 boost::mutex::scoped_lock lm (_mutex);
423                 _colour_conversion = c;
424         }
425
426         signal_changed (VideoContentProperty::COLOUR_CONVERSION);
427 }
428
429 void
430 VideoContent::set_fade_in (Frame t)
431 {
432         {
433                 boost::mutex::scoped_lock lm (_mutex);
434                 _fade_in = t;
435         }
436
437         signal_changed (VideoContentProperty::VIDEO_FADE_IN);
438 }
439
440 void
441 VideoContent::set_fade_out (Frame t)
442 {
443         {
444                 boost::mutex::scoped_lock lm (_mutex);
445                 _fade_out = t;
446         }
447
448         signal_changed (VideoContentProperty::VIDEO_FADE_OUT);
449 }
450
451 /** @return Video size after 3D split and crop */
452 dcp::Size
453 VideoContent::video_size_after_crop () const
454 {
455         return crop().apply (video_size_after_3d_split ());
456 }
457
458 void
459 VideoContent::scale_and_crop_to_fit_width ()
460 {
461         set_scale (VideoContentScale (film()->container ()));
462
463         int const crop = max (0, int (video_size().height - double (film()->frame_size().height) * video_size().width / film()->frame_size().width));
464         set_top_crop (crop / 2);
465         set_bottom_crop (crop / 2);
466 }
467
468 void
469 VideoContent::scale_and_crop_to_fit_height ()
470 {
471         set_scale (VideoContentScale (film()->container ()));
472
473         int const crop = max (0, int (video_size().width - double (film()->frame_size().width) * video_size().height / film()->frame_size().height));
474         set_left_crop (crop / 2);
475         set_right_crop (crop / 2);
476 }
477
478 void
479 VideoContent::set_video_frame_rate (double r)
480 {
481         {
482                 boost::mutex::scoped_lock lm (_mutex);
483                 if (_video_frame_rate == r) {
484                         return;
485                 }
486
487                 _video_frame_rate = r;
488         }
489
490         signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
491 }
492
493 optional<double>
494 VideoContent::fade (Frame f) const
495 {
496         DCPOMATIC_ASSERT (f >= 0);
497
498         if (f < fade_in()) {
499                 return double (f) / fade_in();
500         }
501
502         Frame fade_out_start = video_length() - fade_out();
503         if (f >= fade_out_start) {
504                 return 1 - double (f - fade_out_start) / fade_out();
505         }
506
507         return optional<double> ();
508 }
509
510 string
511 VideoContent::processing_description () const
512 {
513         /* stringstream is OK here as this string is just for presentation to the user */
514         SafeStringStream d;
515
516         if (video_size().width && video_size().height) {
517                 d << String::compose (
518                         _("Content video is %1x%2"),
519                         video_size_after_3d_split().width,
520                         video_size_after_3d_split().height
521                         );
522
523
524                 double ratio = video_size_after_3d_split().ratio ();
525
526                 if (sample_aspect_ratio ()) {
527                         d << ", " << _("pixel aspect ratio") << " " << fixed << setprecision(2) << sample_aspect_ratio().get () << ":1";
528                         ratio *= sample_aspect_ratio().get ();
529                 }
530
531                 d << "\n" << _("Display aspect ratio") << " " << fixed << setprecision(2) << ratio << ":1\n";
532         }
533
534         if ((crop().left || crop().right || crop().top || crop().bottom) && video_size() != dcp::Size (0, 0)) {
535                 dcp::Size cropped = video_size_after_crop ();
536                 d << String::compose (
537                         _("Cropped to %1x%2"),
538                         cropped.width, cropped.height
539                         );
540
541                 d << " (" << fixed << setprecision(2) << cropped.ratio () << ":1)\n";
542         }
543
544         dcp::Size const container_size = film()->frame_size ();
545         dcp::Size const scaled = scale().size (dynamic_pointer_cast<const VideoContent> (shared_from_this ()), container_size, container_size);
546
547         if (scaled != video_size_after_crop ()) {
548                 d << String::compose (
549                         _("Scaled to %1x%2"),
550                         scaled.width, scaled.height
551                         );
552
553                 d << " (" << fixed << setprecision(2) << scaled.ratio() << ":1)\n";
554         }
555
556         if (scaled != container_size) {
557                 d << String::compose (
558                         _("Padded with black to fit container %1 (%2x%3)"),
559                         film()->container()->nickname (),
560                         container_size.width, container_size.height
561                         );
562
563                 d << " (" << fixed << setprecision(2) << container_size.ratio () << ":1)\n";
564         }
565
566         d << _("Content frame rate");
567         d << " " << fixed << setprecision(4) << video_frame_rate() << "\n";
568
569         FrameRateChange frc (video_frame_rate(), film()->video_frame_rate ());
570         d << frc.description () << "\n";
571
572         return d.str ();
573 }
574
575 void
576 VideoContent::add_properties (list<pair<string, string> >& p) const
577 {
578         p.push_back (make_pair (_("Video length"), raw_convert<string> (video_length ()) + " " + _("video frames")));
579         p.push_back (make_pair (_("Video size"), raw_convert<string> (video_size().width) + "x" + raw_convert<string> (video_size().height)));
580         p.push_back (make_pair (_("Video frame rate"), raw_convert<string> (video_frame_rate()) + " " + _("frames per second")));
581 }
582
583 list<DCPTime>
584 VideoContent::reel_split_points () const
585 {
586         list<DCPTime> t;
587         /* XXX: this is questionable; perhaps the position should be forced to be on a frame boundary */
588         t.push_back (position().round_up (film()->video_frame_rate()));
589         return t;
590 }
591
592 double
593 VideoContent::video_frame_rate () const
594 {
595         boost::mutex::scoped_lock lm (_mutex);
596         return _video_frame_rate.get_value_or (film()->video_frame_rate ());
597 }