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