Replace incorrect uses of raw_convert with a new locale_convert.
[dcpomatic.git] / src / lib / video_content.cc
1 /*
2     Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "video_content.h"
22 #include "content.h"
23 #include "video_examiner.h"
24 #include "compose.hpp"
25 #include "ratio.h"
26 #include "config.h"
27 #include "colour_conversion.h"
28 #include "util.h"
29 #include "film.h"
30 #include "exceptions.h"
31 #include "frame_rate_change.h"
32 #include "log.h"
33 #include "locale_convert.h"
34 #include <dcp/raw_convert.h>
35 #include <libcxml/cxml.h>
36 #include <dcp/colour_matrix.h>
37 #include <libxml++/libxml++.h>
38 #include <iomanip>
39 #include <iostream>
40
41 #include "i18n.h"
42
43 #define LOG_GENERAL(...) _parent->film()->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL);
44
45 int const VideoContentProperty::SIZE      = 0;
46 int const VideoContentProperty::FRAME_TYPE  = 1;
47 int const VideoContentProperty::CROP      = 2;
48 int const VideoContentProperty::SCALE     = 3;
49 int const VideoContentProperty::COLOUR_CONVERSION = 4;
50 int const VideoContentProperty::FADE_IN     = 5;
51 int const VideoContentProperty::FADE_OUT    = 6;
52
53 using std::string;
54 using std::setprecision;
55 using std::cout;
56 using std::vector;
57 using std::min;
58 using std::max;
59 using std::fixed;
60 using std::setprecision;
61 using std::list;
62 using std::pair;
63 using boost::shared_ptr;
64 using boost::optional;
65 using boost::dynamic_pointer_cast;
66 using dcp::raw_convert;
67
68 VideoContent::VideoContent (Content* parent)
69         : ContentPart (parent)
70         , _length (0)
71         , _frame_type (VIDEO_FRAME_TYPE_2D)
72         , _scale (VideoContentScale (Ratio::from_id ("178")))
73         , _yuv (true)
74         , _fade_in (0)
75         , _fade_out (0)
76 {
77
78 }
79
80 shared_ptr<VideoContent>
81 VideoContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version)
82 {
83         if (!node->optional_number_child<int> ("VideoWidth")) {
84                 return shared_ptr<VideoContent> ();
85         }
86
87         return shared_ptr<VideoContent> (new VideoContent (parent, node, version));
88 }
89
90 VideoContent::VideoContent (Content* parent, cxml::ConstNodePtr node, int version)
91         : ContentPart (parent)
92 {
93         _size.width = node->number_child<int> ("VideoWidth");
94         _size.height = node->number_child<int> ("VideoHeight");
95
96         /* Backwards compatibility */
97         optional<double> r = node->optional_number_child<double>("VideoFrameRate");
98         if (r) {
99                 _parent->set_video_frame_rate (r.get ());
100         }
101
102         _length = node->number_child<Frame> ("VideoLength");
103
104         if (version <= 34) {
105                 /* Snapshot of the VideoFrameType enum at version 34 */
106                 switch (node->number_child<int> ("VideoFrameType")) {
107                 case 0:
108                         _frame_type = VIDEO_FRAME_TYPE_2D;
109                         break;
110                 case 1:
111                         _frame_type = VIDEO_FRAME_TYPE_3D_LEFT_RIGHT;
112                         break;
113                 case 2:
114                         _frame_type = VIDEO_FRAME_TYPE_3D_TOP_BOTTOM;
115                         break;
116                 case 3:
117                         _frame_type = VIDEO_FRAME_TYPE_3D_ALTERNATE;
118                         break;
119                 case 4:
120                         _frame_type = VIDEO_FRAME_TYPE_3D_LEFT;
121                         break;
122                 case 5:
123                         _frame_type = VIDEO_FRAME_TYPE_3D_RIGHT;
124                         break;
125                 }
126         } else {
127                 _frame_type = string_to_video_frame_type (node->string_child ("VideoFrameType"));
128         }
129
130         _sample_aspect_ratio = node->optional_number_child<double> ("SampleAspectRatio");
131         _crop.left = node->number_child<int> ("LeftCrop");
132         _crop.right = node->number_child<int> ("RightCrop");
133         _crop.top = node->number_child<int> ("TopCrop");
134         _crop.bottom = node->number_child<int> ("BottomCrop");
135
136         if (version <= 7) {
137                 optional<string> r = node->optional_string_child ("Ratio");
138                 if (r) {
139                         _scale = VideoContentScale (Ratio::from_id (r.get ()));
140                 }
141         } else {
142                 _scale = VideoContentScale (node->node_child ("Scale"));
143         }
144
145
146         if (node->optional_node_child ("ColourConversion")) {
147                 _colour_conversion = ColourConversion (node->node_child ("ColourConversion"), version);
148         }
149
150         _yuv = node->optional_bool_child("YUV").get_value_or (true);
151
152         if (version >= 32) {
153                 _fade_in = node->number_child<Frame> ("FadeIn");
154                 _fade_out = node->number_child<Frame> ("FadeOut");
155         } else {
156                 _fade_in = _fade_out = 0;
157         }
158 }
159
160 VideoContent::VideoContent (Content* parent, vector<shared_ptr<Content> > c)
161         : ContentPart (parent)
162         , _length (0)
163         , _yuv (false)
164 {
165         shared_ptr<VideoContent> ref = c[0]->video;
166         DCPOMATIC_ASSERT (ref);
167
168         for (size_t i = 1; i < c.size(); ++i) {
169
170                 if (c[i]->video->size() != ref->size()) {
171                         throw JoinError (_("Content to be joined must have the same picture size."));
172                 }
173
174                 if (c[i]->video->frame_type() != ref->frame_type()) {
175                         throw JoinError (_("Content to be joined must have the same video frame type."));
176                 }
177
178                 if (c[i]->video->crop() != ref->crop()) {
179                         throw JoinError (_("Content to be joined must have the same crop."));
180                 }
181
182                 if (c[i]->video->scale() != ref->scale()) {
183                         throw JoinError (_("Content to be joined must have the same scale setting."));
184                 }
185
186                 if (c[i]->video->colour_conversion() != ref->colour_conversion()) {
187                         throw JoinError (_("Content to be joined must have the same colour conversion."));
188                 }
189
190                 if (c[i]->video->fade_in() != ref->fade_in() || c[i]->video->fade_out() != ref->fade_out()) {
191                         throw JoinError (_("Content to be joined must have the same fades."));
192                 }
193
194                 _length += c[i]->video->length ();
195
196                 if (c[i]->video->yuv ()) {
197                         _yuv = true;
198                 }
199         }
200
201         _size = ref->size ();
202         _frame_type = ref->frame_type ();
203         _crop = ref->crop ();
204         _scale = ref->scale ();
205         _colour_conversion = ref->colour_conversion ();
206         _fade_in = ref->fade_in ();
207         _fade_out = ref->fade_out ();
208 }
209
210 void
211 VideoContent::as_xml (xmlpp::Node* node) const
212 {
213         boost::mutex::scoped_lock lm (_mutex);
214         node->add_child("VideoLength")->add_child_text (raw_convert<string> (_length));
215         node->add_child("VideoWidth")->add_child_text (raw_convert<string> (_size.width));
216         node->add_child("VideoHeight")->add_child_text (raw_convert<string> (_size.height));
217         node->add_child("VideoFrameType")->add_child_text (video_frame_type_to_string (_frame_type));
218         if (_sample_aspect_ratio) {
219                 node->add_child("SampleAspectRatio")->add_child_text (raw_convert<string> (_sample_aspect_ratio.get ()));
220         }
221         _crop.as_xml (node);
222         _scale.as_xml (node->add_child("Scale"));
223         if (_colour_conversion) {
224                 _colour_conversion.get().as_xml (node->add_child("ColourConversion"));
225         }
226         node->add_child("YUV")->add_child_text (_yuv ? "1" : "0");
227         node->add_child("FadeIn")->add_child_text (raw_convert<string> (_fade_in));
228         node->add_child("FadeOut")->add_child_text (raw_convert<string> (_fade_out));
229 }
230
231 void
232 VideoContent::take_from_examiner (shared_ptr<VideoExaminer> d)
233 {
234         /* These examiner calls could call other content methods which take a lock on the mutex */
235         dcp::Size const vs = d->video_size ();
236         Frame vl = d->video_length ();
237         optional<double> const ar = d->sample_aspect_ratio ();
238         bool const yuv = d->yuv ();
239
240         {
241                 boost::mutex::scoped_lock lm (_mutex);
242                 _size = vs;
243                 _length = vl;
244                 _sample_aspect_ratio = ar;
245                 _yuv = yuv;
246
247                 /* Guess correct scale from size and sample aspect ratio */
248                 _scale = VideoContentScale (
249                         Ratio::nearest_from_ratio (double (_size.width) * ar.get_value_or (1) / _size.height)
250                         );
251         }
252
253         LOG_GENERAL ("Video length obtained from header as %1 frames", _length);
254
255         if (d->video_frame_rate()) {
256                 _parent->set_video_frame_rate (d->video_frame_rate().get());
257         }
258
259         _parent->signal_changed (VideoContentProperty::SIZE);
260         _parent->signal_changed (VideoContentProperty::SCALE);
261         _parent->signal_changed (ContentProperty::LENGTH);
262 }
263
264 /** @return string which includes everything about how this content looks */
265 string
266 VideoContent::identifier () const
267 {
268         char buffer[256];
269         snprintf (
270                 buffer, sizeof(buffer), "%d_%d_%d_%d_%s_%" PRId64 "_%" PRId64,
271                 crop().left,
272                 crop().right,
273                 crop().top,
274                 crop().bottom,
275                 scale().id().c_str(),
276                 _fade_in,
277                 _fade_out
278                 );
279
280         string s (buffer);
281
282         if (colour_conversion()) {
283                 s += "_" + colour_conversion().get().identifier ();
284         }
285
286         return s;
287 }
288
289 string
290 VideoContent::technical_summary () const
291 {
292         string s = String::compose (
293                 N_("video: length %1 frames, size %2x%3"),
294                 length_after_3d_combine(),
295                 size().width,
296                 size().height
297                 );
298
299         if (sample_aspect_ratio ()) {
300                 s += String::compose (N_(", sample aspect ratio %1"), (sample_aspect_ratio().get ()));
301         }
302
303         return s;
304 }
305
306 dcp::Size
307 VideoContent::size_after_3d_split () const
308 {
309         dcp::Size const s = size ();
310         switch (frame_type ()) {
311         case VIDEO_FRAME_TYPE_2D:
312         case VIDEO_FRAME_TYPE_3D:
313         case VIDEO_FRAME_TYPE_3D_ALTERNATE:
314         case VIDEO_FRAME_TYPE_3D_LEFT:
315         case VIDEO_FRAME_TYPE_3D_RIGHT:
316                 return s;
317         case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
318                 return dcp::Size (s.width / 2, s.height);
319         case VIDEO_FRAME_TYPE_3D_TOP_BOTTOM:
320                 return dcp::Size (s.width, s.height / 2);
321         }
322
323         DCPOMATIC_ASSERT (false);
324 }
325
326 /** @return Video size after 3D split and crop */
327 dcp::Size
328 VideoContent::size_after_crop () const
329 {
330         return crop().apply (size_after_3d_split ());
331 }
332
333 void
334 VideoContent::scale_and_crop_to_fit_width ()
335 {
336         shared_ptr<const Film> film = _parent->film ();
337         set_scale (VideoContentScale (film->container ()));
338
339         int const crop = max (0, int (size().height - double (film->frame_size().height) * size().width / film->frame_size().width));
340         set_left_crop (0);
341         set_right_crop (0);
342         set_top_crop (crop / 2);
343         set_bottom_crop (crop / 2);
344 }
345
346 void
347 VideoContent::scale_and_crop_to_fit_height ()
348 {
349         shared_ptr<const Film> film = _parent->film ();
350         set_scale (VideoContentScale (film->container ()));
351
352         int const crop = max (0, int (size().width - double (film->frame_size().width) * size().height / film->frame_size().height));
353         set_left_crop (crop / 2);
354         set_right_crop (crop / 2);
355         set_top_crop (0);
356         set_bottom_crop (0);
357 }
358
359 /** @param f Frame index within the whole (untrimmed) content */
360 optional<double>
361 VideoContent::fade (Frame f) const
362 {
363         DCPOMATIC_ASSERT (f >= 0);
364
365         shared_ptr<const Film> film = _parent->film ();
366
367         double const vfr = _parent->active_video_frame_rate ();
368
369         Frame const ts = _parent->trim_start().frames_round(vfr);
370         if ((f - ts) < fade_in()) {
371                 return double (f - ts) / fade_in();
372         }
373
374         Frame fade_out_start = length() - _parent->trim_end().frames_round(vfr) - fade_out();
375         if (f >= fade_out_start) {
376                 return 1 - double (f - fade_out_start) / fade_out();
377         }
378
379         return optional<double> ();
380 }
381
382 string
383 VideoContent::processing_description () const
384 {
385         string d;
386         char buffer[256];
387
388         if (size().width && size().height) {
389                 d += String::compose (
390                         _("Content video is %1x%2"),
391                         size_after_3d_split().width,
392                         size_after_3d_split().height
393                         );
394
395
396                 double ratio = size_after_3d_split().ratio ();
397
398                 if (sample_aspect_ratio ()) {
399                         snprintf (buffer, sizeof(buffer), _(", pixel aspect ratio %.2f:1"), sample_aspect_ratio().get());
400                         d += buffer;
401                         ratio *= sample_aspect_ratio().get ();
402                 }
403
404                 snprintf (buffer, sizeof(buffer), _("\nDisplay aspect ratio %.2f:1"), ratio);
405                 d += buffer;
406         }
407
408         if ((crop().left || crop().right || crop().top || crop().bottom) && size() != dcp::Size (0, 0)) {
409                 dcp::Size cropped = size_after_crop ();
410                 d += String::compose (
411                         _("Cropped to %1x%2"),
412                         cropped.width, cropped.height
413                         );
414
415                 snprintf (buffer, sizeof(buffer), " (%.2f:1)\n", cropped.ratio());
416                 d += buffer;
417         }
418
419         shared_ptr<const Film> film = _parent->film ();
420         dcp::Size const container_size = film->frame_size ();
421         dcp::Size const scaled = scale().size (shared_from_this(), container_size, container_size);
422
423         if (scaled != size_after_crop ()) {
424                 d += String::compose (
425                         _("Scaled to %1x%2"),
426                         scaled.width, scaled.height
427                         );
428
429                 snprintf (buffer, sizeof(buffer), _(" (%.2f:1)\n"), scaled.ratio());
430                 d += buffer;
431         }
432
433         if (scaled != container_size) {
434                 d += String::compose (
435                         _("Padded with black to fit container %1 (%2x%3)"),
436                         film->container()->nickname (),
437                         container_size.width, container_size.height
438                         );
439
440                 snprintf (buffer, sizeof(buffer), _(" (%.2f:1)\n"), container_size.ratio());
441                 d += buffer;
442         }
443
444         if (_parent->video_frame_rate()) {
445                 double const vfr = _parent->video_frame_rate().get ();
446
447                 snprintf (buffer, sizeof(buffer), _("Content frame rate %.4f\n"), vfr);
448                 d += buffer;
449
450                 FrameRateChange frc (vfr, film->video_frame_rate ());
451                 d += frc.description () + "\n";
452         }
453
454         return d;
455 }
456
457 void
458 VideoContent::add_properties (list<UserProperty>& p) const
459 {
460         p.push_back (UserProperty (UserProperty::VIDEO, _("Length"), length (), _("video frames")));
461         p.push_back (UserProperty (UserProperty::VIDEO, _("Size"), size().width + "x" + size().height));
462 }
463
464 void
465 VideoContent::set_length (Frame len)
466 {
467         maybe_set (_length, len, ContentProperty::LENGTH);
468 }
469
470 void
471 VideoContent::set_left_crop (int c)
472 {
473         maybe_set (_crop.left, c, VideoContentProperty::CROP);
474 }
475
476 void
477 VideoContent::set_right_crop (int c)
478 {
479         maybe_set (_crop.right, c, VideoContentProperty::CROP);
480 }
481
482 void
483 VideoContent::set_top_crop (int c)
484 {
485         maybe_set (_crop.top, c, VideoContentProperty::CROP);
486 }
487
488 void
489 VideoContent::set_bottom_crop (int c)
490 {
491         maybe_set (_crop.bottom, c, VideoContentProperty::CROP);
492 }
493
494 void
495 VideoContent::set_scale (VideoContentScale s)
496 {
497         maybe_set (_scale, s, VideoContentProperty::SCALE);
498 }
499
500 void
501 VideoContent::set_frame_type (VideoFrameType t)
502 {
503         maybe_set (_frame_type, t, VideoContentProperty::FRAME_TYPE);
504 }
505
506 void
507 VideoContent::unset_colour_conversion ()
508 {
509         maybe_set (_colour_conversion, boost::optional<ColourConversion> (), VideoContentProperty::COLOUR_CONVERSION);
510 }
511
512 void
513 VideoContent::set_colour_conversion (ColourConversion c)
514 {
515         maybe_set (_colour_conversion, c, VideoContentProperty::COLOUR_CONVERSION);
516 }
517
518 void
519 VideoContent::set_fade_in (Frame t)
520 {
521         maybe_set (_fade_in, t, VideoContentProperty::FADE_IN);
522 }
523
524 void
525 VideoContent::set_fade_out (Frame t)
526 {
527         maybe_set (_fade_out, t, VideoContentProperty::FADE_OUT);
528 }