8b0ec4c997827619738cc56f62218c4797fa2219
[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 "video_content.h"
23 #include "video_examiner.h"
24 #include "ratio.h"
25 #include "compose.hpp"
26 #include "config.h"
27 #include "colour_conversion.h"
28
29 #include "i18n.h"
30
31 int const VideoContentProperty::VIDEO_SIZE        = 0;
32 int const VideoContentProperty::VIDEO_FRAME_RATE  = 1;
33 int const VideoContentProperty::VIDEO_FRAME_TYPE  = 2;
34 int const VideoContentProperty::VIDEO_CROP        = 3;
35 int const VideoContentProperty::VIDEO_RATIO       = 4;
36 int const VideoContentProperty::COLOUR_CONVERSION = 5;
37
38 using std::string;
39 using std::stringstream;
40 using std::setprecision;
41 using std::cout;
42 using boost::shared_ptr;
43 using boost::lexical_cast;
44 using boost::optional;
45
46 VideoContent::VideoContent (shared_ptr<const Film> f, Time s, VideoContent::Frame len)
47         : Content (f, s)
48         , _video_length (len)
49         , _video_frame_rate (0)
50         , _video_frame_type (VIDEO_FRAME_TYPE_2D)
51         , _ratio (Ratio::from_id ("185"))
52         , _colour_conversion (Config::instance()->colour_conversions().front().conversion)
53 {
54
55 }
56
57 VideoContent::VideoContent (shared_ptr<const Film> f, boost::filesystem::path p)
58         : Content (f, p)
59         , _video_length (0)
60         , _video_frame_rate (0)
61         , _video_frame_type (VIDEO_FRAME_TYPE_2D)
62         , _ratio (Ratio::from_id ("185"))
63         , _colour_conversion (Config::instance()->colour_conversions().front().conversion)
64 {
65
66 }
67
68 VideoContent::VideoContent (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node)
69         : Content (f, node)
70 {
71         _video_length = node->number_child<VideoContent::Frame> ("VideoLength");
72         _video_size.width = node->number_child<int> ("VideoWidth");
73         _video_size.height = node->number_child<int> ("VideoHeight");
74         _video_frame_rate = node->number_child<float> ("VideoFrameRate");
75         _video_frame_type = static_cast<VideoFrameType> (node->number_child<int> ("VideoFrameType"));
76         _crop.left = node->number_child<int> ("LeftCrop");
77         _crop.right = node->number_child<int> ("RightCrop");
78         _crop.top = node->number_child<int> ("TopCrop");
79         _crop.bottom = node->number_child<int> ("BottomCrop");
80         optional<string> r = node->optional_string_child ("Ratio");
81         if (r) {
82                 _ratio = Ratio::from_id (r.get ());
83         }
84         _colour_conversion = ColourConversion (node->node_child ("ColourConversion"));
85 }
86
87 void
88 VideoContent::as_xml (xmlpp::Node* node) const
89 {
90         boost::mutex::scoped_lock lm (_mutex);
91         node->add_child("VideoLength")->add_child_text (lexical_cast<string> (_video_length));
92         node->add_child("VideoWidth")->add_child_text (lexical_cast<string> (_video_size.width));
93         node->add_child("VideoHeight")->add_child_text (lexical_cast<string> (_video_size.height));
94         node->add_child("VideoFrameRate")->add_child_text (lexical_cast<string> (_video_frame_rate));
95         node->add_child("VideoFrameType")->add_child_text (lexical_cast<string> (static_cast<int> (_video_frame_type)));
96         node->add_child("LeftCrop")->add_child_text (boost::lexical_cast<string> (_crop.left));
97         node->add_child("RightCrop")->add_child_text (boost::lexical_cast<string> (_crop.right));
98         node->add_child("TopCrop")->add_child_text (boost::lexical_cast<string> (_crop.top));
99         node->add_child("BottomCrop")->add_child_text (boost::lexical_cast<string> (_crop.bottom));
100         if (_ratio) {
101                 node->add_child("Ratio")->add_child_text (_ratio->id ());
102         }
103         _colour_conversion.as_xml (node->add_child("ColourConversion"));
104 }
105
106 void
107 VideoContent::take_from_video_examiner (shared_ptr<VideoExaminer> d)
108 {
109         /* These examiner calls could call other content methods which take a lock on the mutex */
110         libdcp::Size const vs = d->video_size ();
111         float const vfr = d->video_frame_rate ();
112         
113         {
114                 boost::mutex::scoped_lock lm (_mutex);
115                 _video_size = vs;
116                 _video_frame_rate = vfr;
117         }
118         
119         signal_changed (VideoContentProperty::VIDEO_SIZE);
120         signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
121 }
122
123
124 string
125 VideoContent::information () const
126 {
127         if (video_size().width == 0 || video_size().height == 0) {
128                 return "";
129         }
130         
131         stringstream s;
132
133         s << String::compose (
134                 _("%1x%2 pixels (%3:1)"),
135                 video_size().width,
136                 video_size().height,
137                 setprecision (3), float (video_size().width) / video_size().height
138                 );
139         
140         return s.str ();
141 }
142
143 void
144 VideoContent::set_left_crop (int c)
145 {
146         {
147                 boost::mutex::scoped_lock lm (_mutex);
148                 
149                 if (_crop.left == c) {
150                         return;
151                 }
152                 
153                 _crop.left = c;
154         }
155         
156         signal_changed (VideoContentProperty::VIDEO_CROP);
157 }
158
159 void
160 VideoContent::set_right_crop (int c)
161 {
162         {
163                 boost::mutex::scoped_lock lm (_mutex);
164                 if (_crop.right == c) {
165                         return;
166                 }
167                 
168                 _crop.right = c;
169         }
170         
171         signal_changed (VideoContentProperty::VIDEO_CROP);
172 }
173
174 void
175 VideoContent::set_top_crop (int c)
176 {
177         {
178                 boost::mutex::scoped_lock lm (_mutex);
179                 if (_crop.top == c) {
180                         return;
181                 }
182                 
183                 _crop.top = c;
184         }
185         
186         signal_changed (VideoContentProperty::VIDEO_CROP);
187 }
188
189 void
190 VideoContent::set_bottom_crop (int c)
191 {
192         {
193                 boost::mutex::scoped_lock lm (_mutex);
194                 if (_crop.bottom == c) {
195                         return;
196                 }
197                 
198                 _crop.bottom = c;
199         }
200
201         signal_changed (VideoContentProperty::VIDEO_CROP);
202 }
203
204 void
205 VideoContent::set_ratio (Ratio const * r)
206 {
207         {
208                 boost::mutex::scoped_lock lm (_mutex);
209                 if (_ratio == r) {
210                         return;
211                 }
212
213                 _ratio = r;
214         }
215
216         signal_changed (VideoContentProperty::VIDEO_RATIO);
217 }
218
219 /** @return string which includes everything about how this content looks */
220 string
221 VideoContent::identifier () const
222 {
223         stringstream s;
224         s << Content::digest()
225           << "_" << crop().left
226           << "_" << crop().right
227           << "_" << crop().top
228           << "_" << crop().bottom;
229
230         if (ratio()) {
231                 s << "_" << ratio()->id ();
232         }
233
234         return s.str ();
235 }
236
237 void
238 VideoContent::set_video_frame_type (VideoFrameType t)
239 {
240         {
241                 boost::mutex::scoped_lock lm (_mutex);
242                 _video_frame_type = t;
243         }
244
245         signal_changed (VideoContentProperty::VIDEO_FRAME_TYPE);
246 }
247
248 string
249 VideoContent::technical_summary () const
250 {
251         return String::compose ("video: length %1, size %2x%3, rate %4", video_length(), video_size().width, video_size().height, video_frame_rate());
252 }
253
254 libdcp::Size
255 VideoContent::video_size_after_3d_split () const
256 {
257         libdcp::Size const s = video_size ();
258         switch (video_frame_type ()) {
259         case VIDEO_FRAME_TYPE_2D:
260                 return s;
261         case VIDEO_FRAME_TYPE_3D_LEFT_RIGHT:
262                 return libdcp::Size (s.width / 2, s.height);
263         }
264
265         assert (false);
266 }
267
268 void
269 VideoContent::set_colour_conversion (ColourConversion c)
270 {
271         {
272                 boost::mutex::scoped_lock lm (_mutex);
273                 _colour_conversion = c;
274         }
275
276         signal_changed (VideoContentProperty::COLOUR_CONVERSION);
277 }