ca4ed8a9f019bcd9525997f779e76b5c33822af8
[dcpomatic.git] / src / lib / video_content.cc
1 /*
2     Copyright (C) 2013 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 "video_content.h"
24 #include "video_examiner.h"
25 #include "ratio.h"
26 #include "compose.hpp"
27 #include "config.h"
28 #include "colour_conversion.h"
29 #include "util.h"
30 #include "film.h"
31 #include "exceptions.h"
32
33 #include "i18n.h"
34
35 int const VideoContentProperty::VIDEO_SIZE        = 0;
36 int const VideoContentProperty::VIDEO_FRAME_RATE  = 1;
37 int const VideoContentProperty::VIDEO_FRAME_TYPE  = 2;
38 int const VideoContentProperty::VIDEO_CROP        = 3;
39 int const VideoContentProperty::VIDEO_RATIO       = 4;
40 int const VideoContentProperty::COLOUR_CONVERSION = 5;
41
42 using std::string;
43 using std::stringstream;
44 using std::setprecision;
45 using std::cout;
46 using std::vector;
47 using boost::shared_ptr;
48 using boost::lexical_cast;
49 using boost::optional;
50 using boost::dynamic_pointer_cast;
51
52 VideoContent::VideoContent (shared_ptr<const Film> f)
53         : Content (f)
54         , _video_length (0)
55         , _video_frame_rate (0)
56         , _video_frame_type (VIDEO_FRAME_TYPE_2D)
57         , _ratio (Ratio::from_id ("185"))
58 {
59         setup_default_colour_conversion ();
60 }
61
62 VideoContent::VideoContent (shared_ptr<const Film> f, Time s, VideoContent::Frame len)
63         : Content (f, s)
64         , _video_length (len)
65         , _video_frame_rate (0)
66         , _video_frame_type (VIDEO_FRAME_TYPE_2D)
67         , _ratio (Ratio::from_id ("185"))
68 {
69         setup_default_colour_conversion ();
70 }
71
72 VideoContent::VideoContent (shared_ptr<const Film> f, boost::filesystem::path p)
73         : Content (f, p)
74         , _video_length (0)
75         , _video_frame_rate (0)
76         , _video_frame_type (VIDEO_FRAME_TYPE_2D)
77         , _ratio (Ratio::from_id ("185"))
78 {
79         setup_default_colour_conversion ();
80 }
81
82 VideoContent::VideoContent (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node)
83         : Content (f, node)
84         , _ratio (0)
85 {
86         _video_length = node->number_child<VideoContent::Frame> ("VideoLength");
87         _video_size.width = node->number_child<int> ("VideoWidth");
88         _video_size.height = node->number_child<int> ("VideoHeight");
89         _video_frame_rate = node->number_child<float> ("VideoFrameRate");
90         _video_frame_type = static_cast<VideoFrameType> (node->number_child<int> ("VideoFrameType"));
91         _crop.left = node->number_child<int> ("LeftCrop");
92         _crop.right = node->number_child<int> ("RightCrop");
93         _crop.top = node->number_child<int> ("TopCrop");
94         _crop.bottom = node->number_child<int> ("BottomCrop");
95         optional<string> r = node->optional_string_child ("Ratio");
96         if (r) {
97                 _ratio = Ratio::from_id (r.get ());
98         }
99         _colour_conversion = ColourConversion (node->node_child ("ColourConversion"));
100 }
101
102 VideoContent::VideoContent (shared_ptr<const Film> f, vector<shared_ptr<Content> > c)
103         : Content (f, c)
104 {
105         shared_ptr<VideoContent> ref = dynamic_pointer_cast<VideoContent> (c[0]);
106         assert (ref);
107         
108         for (size_t i = 0; i < c.size(); ++i) {
109                 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (c[i]);
110
111                 if (vc->video_size() != ref->video_size()) {
112                         throw JoinError (_("Content to be joined must have the same picture size."));
113                 }
114
115                 if (vc->video_frame_rate() != ref->video_frame_rate()) {
116                         throw JoinError (_("Content to be joined must have the same video frame rate."));
117                 }
118
119                 if (vc->video_frame_type() != ref->video_frame_type()) {
120                         throw JoinError (_("Content to be joined must have the same video frame type."));
121                 }
122
123                 if (vc->crop() != ref->crop()) {
124                         throw JoinError (_("Content to be joined must have the same crop."));
125                 }
126
127                 if (vc->ratio() != ref->ratio()) {
128                         throw JoinError (_("Content to be joined must have the same ratio."));
129                 }
130
131                 if (vc->colour_conversion() != ref->colour_conversion()) {
132                         throw JoinError (_("Content to be joined must have the same colour conversion."));
133                 }
134         }
135
136         _video_size = ref->video_size ();
137         _video_frame_rate = ref->video_frame_rate ();
138         _video_frame_type = ref->video_frame_type ();
139         _crop = ref->crop ();
140         _ratio = ref->ratio ();
141         _colour_conversion = ref->colour_conversion ();
142 }
143
144 void
145 VideoContent::as_xml (xmlpp::Node* node) const
146 {
147         boost::mutex::scoped_lock lm (_mutex);
148         node->add_child("VideoLength")->add_child_text (lexical_cast<string> (_video_length));
149         node->add_child("VideoWidth")->add_child_text (lexical_cast<string> (_video_size.width));
150         node->add_child("VideoHeight")->add_child_text (lexical_cast<string> (_video_size.height));
151         node->add_child("VideoFrameRate")->add_child_text (lexical_cast<string> (_video_frame_rate));
152         node->add_child("VideoFrameType")->add_child_text (lexical_cast<string> (static_cast<int> (_video_frame_type)));
153         node->add_child("LeftCrop")->add_child_text (boost::lexical_cast<string> (_crop.left));
154         node->add_child("RightCrop")->add_child_text (boost::lexical_cast<string> (_crop.right));
155         node->add_child("TopCrop")->add_child_text (boost::lexical_cast<string> (_crop.top));
156         node->add_child("BottomCrop")->add_child_text (boost::lexical_cast<string> (_crop.bottom));
157         if (_ratio) {
158                 node->add_child("Ratio")->add_child_text (_ratio->id ());
159         }
160         _colour_conversion.as_xml (node->add_child("ColourConversion"));
161 }
162
163 void
164 VideoContent::setup_default_colour_conversion ()
165 {
166         _colour_conversion = PresetColourConversion (_("sRGB"), 2.4, true, libdcp::colour_matrix::srgb_to_xyz, 2.6).conversion;
167 }
168
169 void
170 VideoContent::take_from_video_examiner (shared_ptr<VideoExaminer> d)
171 {
172         /* These examiner calls could call other content methods which take a lock on the mutex */
173         libdcp::Size const vs = d->video_size ();
174         float const vfr = d->video_frame_rate ();
175         
176         {
177                 boost::mutex::scoped_lock lm (_mutex);
178                 _video_size = vs;
179                 _video_frame_rate = vfr;
180         }
181         
182         signal_changed (VideoContentProperty::VIDEO_SIZE);
183         signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
184 }
185
186
187 string
188 VideoContent::information () const
189 {
190         if (video_size().width == 0 || video_size().height == 0) {
191                 return "";
192         }
193         
194         stringstream s;
195
196         s << String::compose (
197                 _("%1x%2 pixels (%3:1)"),
198                 video_size().width,
199                 video_size().height,
200                 setprecision (3), video_size().ratio ()
201                 );
202         
203         return s.str ();
204 }
205
206 void
207 VideoContent::set_left_crop (int c)
208 {
209         {
210                 boost::mutex::scoped_lock lm (_mutex);
211                 
212                 if (_crop.left == c) {
213                         return;
214                 }
215                 
216                 _crop.left = c;
217         }
218         
219         signal_changed (VideoContentProperty::VIDEO_CROP);
220 }
221
222 void
223 VideoContent::set_right_crop (int c)
224 {
225         {
226                 boost::mutex::scoped_lock lm (_mutex);
227                 if (_crop.right == c) {
228                         return;
229                 }
230                 
231                 _crop.right = c;
232         }
233         
234         signal_changed (VideoContentProperty::VIDEO_CROP);
235 }
236
237 void
238 VideoContent::set_top_crop (int c)
239 {
240         {
241                 boost::mutex::scoped_lock lm (_mutex);
242                 if (_crop.top == c) {
243                         return;
244                 }
245                 
246                 _crop.top = c;
247         }
248         
249         signal_changed (VideoContentProperty::VIDEO_CROP);
250 }
251
252 void
253 VideoContent::set_bottom_crop (int c)
254 {
255         {
256                 boost::mutex::scoped_lock lm (_mutex);
257                 if (_crop.bottom == c) {
258                         return;
259                 }
260                 
261                 _crop.bottom = c;
262         }
263
264         signal_changed (VideoContentProperty::VIDEO_CROP);
265 }
266
267 void
268 VideoContent::set_ratio (Ratio const * r)
269 {
270         {
271                 boost::mutex::scoped_lock lm (_mutex);
272                 if (_ratio == r) {
273                         return;
274                 }
275
276                 _ratio = r;
277         }
278
279         signal_changed (VideoContentProperty::VIDEO_RATIO);
280 }
281
282 /** @return string which includes everything about how this content looks */
283 string
284 VideoContent::identifier () const
285 {
286         stringstream s;
287         s << Content::identifier()
288           << "_" << crop().left
289           << "_" << crop().right
290           << "_" << crop().top
291           << "_" << crop().bottom
292           << "_" << colour_conversion().identifier ();
293
294         if (ratio()) {
295                 s << "_" << ratio()->id ();
296         }
297
298         return s.str ();
299 }
300
301 void
302 VideoContent::set_video_frame_type (VideoFrameType t)
303 {
304         {
305                 boost::mutex::scoped_lock lm (_mutex);
306                 _video_frame_type = t;
307         }
308
309         signal_changed (VideoContentProperty::VIDEO_FRAME_TYPE);
310 }
311
312 string
313 VideoContent::technical_summary () const
314 {
315         return String::compose ("video: length %1, size %2x%3, rate %4", video_length(), video_size().width, video_size().height, video_frame_rate());
316 }
317
318 libdcp::Size
319 VideoContent::video_size_after_3d_split () const
320 {
321         libdcp::Size const s = video_size ();
322         switch (video_frame_type ()) {
323         case VIDEO_FRAME_TYPE_2D:
324                 return s;
325         case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
326                 return libdcp::Size (s.width / 2, s.height);
327         }
328
329         assert (false);
330 }
331
332 void
333 VideoContent::set_colour_conversion (ColourConversion c)
334 {
335         {
336                 boost::mutex::scoped_lock lm (_mutex);
337                 _colour_conversion = c;
338         }
339
340         signal_changed (VideoContentProperty::COLOUR_CONVERSION);
341 }
342
343 /** @return Video size after 3D split and crop */
344 libdcp::Size
345 VideoContent::video_size_after_crop () const
346 {
347         return crop().apply (video_size_after_3d_split ());
348 }
349
350 /** @param t A time offset from the start of this piece of content.
351  *  @return Corresponding frame index.
352  */
353 VideoContent::Frame
354 VideoContent::time_to_content_video_frames (Time t) const
355 {
356         shared_ptr<const Film> film = _film.lock ();
357         assert (film);
358         
359         FrameRateConversion frc (video_frame_rate(), film->video_frame_rate());
360
361         /* Here we are converting from time (in the DCP) to a frame number in the content.
362            Hence we need to use the DCP's frame rate and the double/skip correction, not
363            the source's rate.
364         */
365         return t * film->video_frame_rate() / (frc.factor() * TIME_HZ);
366 }