Merge master.
[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         , _video_length (0)
105 {
106         shared_ptr<VideoContent> ref = dynamic_pointer_cast<VideoContent> (c[0]);
107         assert (ref);
108
109         for (size_t i = 0; i < c.size(); ++i) {
110                 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (c[i]);
111
112                 if (vc->video_size() != ref->video_size()) {
113                         throw JoinError (_("Content to be joined must have the same picture size."));
114                 }
115
116                 if (vc->video_frame_rate() != ref->video_frame_rate()) {
117                         throw JoinError (_("Content to be joined must have the same video frame rate."));
118                 }
119
120                 if (vc->video_frame_type() != ref->video_frame_type()) {
121                         throw JoinError (_("Content to be joined must have the same video frame type."));
122                 }
123
124                 if (vc->crop() != ref->crop()) {
125                         throw JoinError (_("Content to be joined must have the same crop."));
126                 }
127
128                 if (vc->ratio() != ref->ratio()) {
129                         throw JoinError (_("Content to be joined must have the same ratio."));
130                 }
131
132                 if (vc->colour_conversion() != ref->colour_conversion()) {
133                         throw JoinError (_("Content to be joined must have the same colour conversion."));
134                 }
135
136                 _video_length += vc->video_length ();
137         }
138
139         _video_size = ref->video_size ();
140         _video_frame_rate = ref->video_frame_rate ();
141         _video_frame_type = ref->video_frame_type ();
142         _crop = ref->crop ();
143         _ratio = ref->ratio ();
144         _colour_conversion = ref->colour_conversion ();
145 }
146
147 void
148 VideoContent::as_xml (xmlpp::Node* node) const
149 {
150         boost::mutex::scoped_lock lm (_mutex);
151         node->add_child("VideoLength")->add_child_text (lexical_cast<string> (_video_length));
152         node->add_child("VideoWidth")->add_child_text (lexical_cast<string> (_video_size.width));
153         node->add_child("VideoHeight")->add_child_text (lexical_cast<string> (_video_size.height));
154         node->add_child("VideoFrameRate")->add_child_text (lexical_cast<string> (_video_frame_rate));
155         node->add_child("VideoFrameType")->add_child_text (lexical_cast<string> (static_cast<int> (_video_frame_type)));
156         node->add_child("LeftCrop")->add_child_text (boost::lexical_cast<string> (_crop.left));
157         node->add_child("RightCrop")->add_child_text (boost::lexical_cast<string> (_crop.right));
158         node->add_child("TopCrop")->add_child_text (boost::lexical_cast<string> (_crop.top));
159         node->add_child("BottomCrop")->add_child_text (boost::lexical_cast<string> (_crop.bottom));
160         if (_ratio) {
161                 node->add_child("Ratio")->add_child_text (_ratio->id ());
162         }
163         _colour_conversion.as_xml (node->add_child("ColourConversion"));
164 }
165
166 void
167 VideoContent::setup_default_colour_conversion ()
168 {
169         _colour_conversion = PresetColourConversion (_("sRGB"), 2.4, true, libdcp::colour_matrix::srgb_to_xyz, 2.6).conversion;
170 }
171
172 void
173 VideoContent::take_from_video_examiner (shared_ptr<VideoExaminer> d)
174 {
175         /* These examiner calls could call other content methods which take a lock on the mutex */
176         libdcp::Size const vs = d->video_size ();
177         float const vfr = d->video_frame_rate ();
178         
179         {
180                 boost::mutex::scoped_lock lm (_mutex);
181                 _video_size = vs;
182                 _video_frame_rate = vfr;
183         }
184         
185         signal_changed (VideoContentProperty::VIDEO_SIZE);
186         signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
187 }
188
189
190 string
191 VideoContent::information () const
192 {
193         if (video_size().width == 0 || video_size().height == 0) {
194                 return "";
195         }
196         
197         stringstream s;
198
199         s << String::compose (
200                 _("%1x%2 pixels (%3:1)"),
201                 video_size().width,
202                 video_size().height,
203                 setprecision (3), video_size().ratio ()
204                 );
205         
206         return s.str ();
207 }
208
209 void
210 VideoContent::set_left_crop (int c)
211 {
212         {
213                 boost::mutex::scoped_lock lm (_mutex);
214                 
215                 if (_crop.left == c) {
216                         return;
217                 }
218                 
219                 _crop.left = c;
220         }
221         
222         signal_changed (VideoContentProperty::VIDEO_CROP);
223 }
224
225 void
226 VideoContent::set_right_crop (int c)
227 {
228         {
229                 boost::mutex::scoped_lock lm (_mutex);
230                 if (_crop.right == c) {
231                         return;
232                 }
233                 
234                 _crop.right = c;
235         }
236         
237         signal_changed (VideoContentProperty::VIDEO_CROP);
238 }
239
240 void
241 VideoContent::set_top_crop (int c)
242 {
243         {
244                 boost::mutex::scoped_lock lm (_mutex);
245                 if (_crop.top == c) {
246                         return;
247                 }
248                 
249                 _crop.top = c;
250         }
251         
252         signal_changed (VideoContentProperty::VIDEO_CROP);
253 }
254
255 void
256 VideoContent::set_bottom_crop (int c)
257 {
258         {
259                 boost::mutex::scoped_lock lm (_mutex);
260                 if (_crop.bottom == c) {
261                         return;
262                 }
263                 
264                 _crop.bottom = c;
265         }
266
267         signal_changed (VideoContentProperty::VIDEO_CROP);
268 }
269
270 void
271 VideoContent::set_ratio (Ratio const * r)
272 {
273         {
274                 boost::mutex::scoped_lock lm (_mutex);
275                 if (_ratio == r) {
276                         return;
277                 }
278
279                 _ratio = r;
280         }
281
282         signal_changed (VideoContentProperty::VIDEO_RATIO);
283 }
284
285 /** @return string which includes everything about how this content looks */
286 string
287 VideoContent::identifier () const
288 {
289         stringstream s;
290         s << Content::identifier()
291           << "_" << crop().left
292           << "_" << crop().right
293           << "_" << crop().top
294           << "_" << crop().bottom
295           << "_" << colour_conversion().identifier ();
296
297         if (ratio()) {
298                 s << "_" << ratio()->id ();
299         }
300
301         return s.str ();
302 }
303
304 void
305 VideoContent::set_video_frame_type (VideoFrameType t)
306 {
307         {
308                 boost::mutex::scoped_lock lm (_mutex);
309                 _video_frame_type = t;
310         }
311
312         signal_changed (VideoContentProperty::VIDEO_FRAME_TYPE);
313 }
314
315 string
316 VideoContent::technical_summary () const
317 {
318         return String::compose ("video: length %1, size %2x%3, rate %4", video_length(), video_size().width, video_size().height, video_frame_rate());
319 }
320
321 libdcp::Size
322 VideoContent::video_size_after_3d_split () const
323 {
324         libdcp::Size const s = video_size ();
325         switch (video_frame_type ()) {
326         case VIDEO_FRAME_TYPE_2D:
327                 return s;
328         case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
329                 return libdcp::Size (s.width / 2, s.height);
330         }
331
332         assert (false);
333 }
334
335 void
336 VideoContent::set_colour_conversion (ColourConversion c)
337 {
338         {
339                 boost::mutex::scoped_lock lm (_mutex);
340                 _colour_conversion = c;
341         }
342
343         signal_changed (VideoContentProperty::COLOUR_CONVERSION);
344 }
345
346 /** @return Video size after 3D split and crop */
347 libdcp::Size
348 VideoContent::video_size_after_crop () const
349 {
350         return crop().apply (video_size_after_3d_split ());
351 }
352
353 /** @param t A time offset from the start of this piece of content.
354  *  @return Corresponding frame index.
355  */
356 VideoContent::Frame
357 VideoContent::time_to_content_video_frames (Time t) const
358 {
359         shared_ptr<const Film> film = _film.lock ();
360         assert (film);
361         
362         FrameRateConversion frc (video_frame_rate(), film->video_frame_rate());
363
364         /* Here we are converting from time (in the DCP) to a frame number in the content.
365            Hence we need to use the DCP's frame rate and the double/skip correction, not
366            the source's rate.
367         */
368         return t * film->video_frame_rate() / (frc.factor() * TIME_HZ);
369 }