Split examiner parts off decoder.
[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
26 #include "i18n.h"
27
28 int const VideoContentProperty::VIDEO_SIZE       = 0;
29 int const VideoContentProperty::VIDEO_FRAME_RATE = 1;
30 int const VideoContentProperty::VIDEO_CROP       = 2;
31 int const VideoContentProperty::VIDEO_RATIO      = 3;
32
33 using std::string;
34 using std::stringstream;
35 using std::setprecision;
36 using boost::shared_ptr;
37 using boost::lexical_cast;
38 using boost::optional;
39
40 VideoContent::VideoContent (shared_ptr<const Film> f, Time s, ContentVideoFrame len)
41         : Content (f, s)
42         , _video_length (len)
43         , _video_frame_rate (0)
44         , _ratio (Ratio::from_id ("185"))
45 {
46
47 }
48
49 VideoContent::VideoContent (shared_ptr<const Film> f, boost::filesystem::path p)
50         : Content (f, p)
51         , _video_length (0)
52         , _video_frame_rate (0)
53         , _ratio (Ratio::from_id ("185"))
54 {
55
56 }
57
58 VideoContent::VideoContent (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node)
59         : Content (f, node)
60 {
61         _video_length = node->number_child<ContentVideoFrame> ("VideoLength");
62         _video_size.width = node->number_child<int> ("VideoWidth");
63         _video_size.height = node->number_child<int> ("VideoHeight");
64         _video_frame_rate = node->number_child<float> ("VideoFrameRate");
65         _crop.left = node->number_child<int> ("LeftCrop");
66         _crop.right = node->number_child<int> ("RightCrop");
67         _crop.top = node->number_child<int> ("TopCrop");
68         _crop.bottom = node->number_child<int> ("BottomCrop");
69         optional<string> r = node->optional_string_child ("Ratio");
70         if (r) {
71                 _ratio = Ratio::from_id (r.get ());
72         }
73 }
74
75 VideoContent::VideoContent (VideoContent const & o)
76         : Content (o)
77         , _video_length (o._video_length)
78         , _video_size (o._video_size)
79         , _video_frame_rate (o._video_frame_rate)
80         , _ratio (o._ratio)
81 {
82
83 }
84
85 void
86 VideoContent::as_xml (xmlpp::Node* node) const
87 {
88         boost::mutex::scoped_lock lm (_mutex);
89         node->add_child("VideoLength")->add_child_text (lexical_cast<string> (_video_length));
90         node->add_child("VideoWidth")->add_child_text (lexical_cast<string> (_video_size.width));
91         node->add_child("VideoHeight")->add_child_text (lexical_cast<string> (_video_size.height));
92         node->add_child("VideoFrameRate")->add_child_text (lexical_cast<string> (_video_frame_rate));
93         node->add_child("LeftCrop")->add_child_text (boost::lexical_cast<string> (_crop.left));
94         node->add_child("RightCrop")->add_child_text (boost::lexical_cast<string> (_crop.right));
95         node->add_child("TopCrop")->add_child_text (boost::lexical_cast<string> (_crop.top));
96         node->add_child("BottomCrop")->add_child_text (boost::lexical_cast<string> (_crop.bottom));
97         if (_ratio) {
98                 node->add_child("Ratio")->add_child_text (_ratio->id ());
99         }
100 }
101
102 void
103 VideoContent::take_from_video_examiner (shared_ptr<VideoExaminer> d)
104 {
105         /* These examiner calls could call other content methods which take a lock on the mutex */
106         libdcp::Size const vs = d->video_size ();
107         float const vfr = d->video_frame_rate ();
108         
109         {
110                 boost::mutex::scoped_lock lm (_mutex);
111                 _video_size = vs;
112                 _video_frame_rate = vfr;
113         }
114         
115         signal_changed (VideoContentProperty::VIDEO_SIZE);
116         signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
117 }
118
119
120 string
121 VideoContent::information () const
122 {
123         if (video_size().width == 0 || video_size().height == 0) {
124                 return "";
125         }
126         
127         stringstream s;
128
129         s << String::compose (
130                 _("%1x%2 pixels (%3:1)"),
131                 video_size().width,
132                 video_size().height,
133                 setprecision (3), float (video_size().width) / video_size().height
134                 );
135         
136         return s.str ();
137 }
138
139 void
140 VideoContent::set_crop (Crop c)
141 {
142         {
143                 boost::mutex::scoped_lock lm (_mutex);
144                 _crop = c;
145         }
146         signal_changed (VideoContentProperty::VIDEO_CROP);
147 }
148
149 void
150 VideoContent::set_left_crop (int c)
151 {
152         {
153                 boost::mutex::scoped_lock lm (_mutex);
154                 
155                 if (_crop.left == c) {
156                         return;
157                 }
158                 
159                 _crop.left = c;
160         }
161         
162         signal_changed (VideoContentProperty::VIDEO_CROP);
163 }
164
165 void
166 VideoContent::set_right_crop (int c)
167 {
168         {
169                 boost::mutex::scoped_lock lm (_mutex);
170                 if (_crop.right == c) {
171                         return;
172                 }
173                 
174                 _crop.right = c;
175         }
176         
177         signal_changed (VideoContentProperty::VIDEO_CROP);
178 }
179
180 void
181 VideoContent::set_top_crop (int c)
182 {
183         {
184                 boost::mutex::scoped_lock lm (_mutex);
185                 if (_crop.top == c) {
186                         return;
187                 }
188                 
189                 _crop.top = c;
190         }
191         
192         signal_changed (VideoContentProperty::VIDEO_CROP);
193 }
194
195 void
196 VideoContent::set_bottom_crop (int c)
197 {
198         {
199                 boost::mutex::scoped_lock lm (_mutex);
200                 if (_crop.bottom == c) {
201                         return;
202                 }
203                 
204                 _crop.bottom = c;
205         }
206
207         signal_changed (VideoContentProperty::VIDEO_CROP);
208 }
209
210 void
211 VideoContent::set_ratio (Ratio const * r)
212 {
213         {
214                 boost::mutex::scoped_lock lm (_mutex);
215                 if (_ratio == r) {
216                         return;
217                 }
218
219                 _ratio = r;
220         }
221
222         signal_changed (VideoContentProperty::VIDEO_RATIO);
223 }