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