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