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