A few test fixups.
[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 <libcxml/cxml.h>
21 #include "video_content.h"
22 #include "video_decoder.h"
23 #include "ratio.h"
24
25 #include "i18n.h"
26
27 int const VideoContentProperty::VIDEO_SIZE       = 0;
28 int const VideoContentProperty::VIDEO_FRAME_RATE = 1;
29 int const VideoContentProperty::VIDEO_CROP       = 2;
30 int const VideoContentProperty::VIDEO_RATIO      = 3;
31
32 using std::string;
33 using std::stringstream;
34 using std::setprecision;
35 using boost::shared_ptr;
36 using boost::lexical_cast;
37 using boost::optional;
38
39 VideoContent::VideoContent (shared_ptr<const Film> f, Time s, ContentVideoFrame len)
40         : Content (f, s)
41         , _video_length (len)
42         , _ratio (0)
43 {
44
45 }
46
47 VideoContent::VideoContent (shared_ptr<const Film> f, boost::filesystem::path p)
48         : Content (f, p)
49         , _video_length (0)
50         , _ratio (0)
51 {
52
53 }
54
55 VideoContent::VideoContent (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node)
56         : Content (f, node)
57 {
58         _video_length = node->number_child<ContentVideoFrame> ("VideoLength");
59         _video_size.width = node->number_child<int> ("VideoWidth");
60         _video_size.height = node->number_child<int> ("VideoHeight");
61         _video_frame_rate = node->number_child<float> ("VideoFrameRate");
62         _crop.left = node->number_child<int> ("LeftCrop");
63         _crop.right = node->number_child<int> ("RightCrop");
64         _crop.top = node->number_child<int> ("TopCrop");
65         _crop.bottom = node->number_child<int> ("BottomCrop");
66         optional<string> r = node->optional_string_child ("Ratio");
67         if (r) {
68                 _ratio = Ratio::from_id (r.get ());
69         }
70 }
71
72 VideoContent::VideoContent (VideoContent const & o)
73         : Content (o)
74         , _video_length (o._video_length)
75         , _video_size (o._video_size)
76         , _video_frame_rate (o._video_frame_rate)
77         , _ratio (o._ratio)
78 {
79
80 }
81
82 void
83 VideoContent::as_xml (xmlpp::Node* node) const
84 {
85         boost::mutex::scoped_lock lm (_mutex);
86         node->add_child("VideoLength")->add_child_text (lexical_cast<string> (_video_length));
87         node->add_child("VideoWidth")->add_child_text (lexical_cast<string> (_video_size.width));
88         node->add_child("VideoHeight")->add_child_text (lexical_cast<string> (_video_size.height));
89         node->add_child("VideoFrameRate")->add_child_text (lexical_cast<string> (_video_frame_rate));
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_decoder (shared_ptr<VideoDecoder> d)
101 {
102         /* These decoder 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_crop (Crop c)
138 {
139         {
140                 boost::mutex::scoped_lock lm (_mutex);
141                 _crop = c;
142         }
143         signal_changed (VideoContentProperty::VIDEO_CROP);
144 }
145
146 void
147 VideoContent::set_left_crop (int c)
148 {
149         {
150                 boost::mutex::scoped_lock lm (_mutex);
151                 
152                 if (_crop.left == c) {
153                         return;
154                 }
155                 
156                 _crop.left = c;
157         }
158         
159         signal_changed (VideoContentProperty::VIDEO_CROP);
160 }
161
162 void
163 VideoContent::set_right_crop (int c)
164 {
165         {
166                 boost::mutex::scoped_lock lm (_mutex);
167                 if (_crop.right == c) {
168                         return;
169                 }
170                 
171                 _crop.right = c;
172         }
173         
174         signal_changed (VideoContentProperty::VIDEO_CROP);
175 }
176
177 void
178 VideoContent::set_top_crop (int c)
179 {
180         {
181                 boost::mutex::scoped_lock lm (_mutex);
182                 if (_crop.top == c) {
183                         return;
184                 }
185                 
186                 _crop.top = c;
187         }
188         
189         signal_changed (VideoContentProperty::VIDEO_CROP);
190 }
191
192 void
193 VideoContent::set_bottom_crop (int c)
194 {
195         {
196                 boost::mutex::scoped_lock lm (_mutex);
197                 if (_crop.bottom == c) {
198                         return;
199                 }
200                 
201                 _crop.bottom = c;
202         }
203
204         signal_changed (VideoContentProperty::VIDEO_CROP);
205 }
206
207 void
208 VideoContent::set_ratio (Ratio const * r)
209 {
210         {
211                 boost::mutex::scoped_lock lm (_mutex);
212                 if (_ratio == r) {
213                         return;
214                 }
215
216                 _ratio = r;
217         }
218
219         signal_changed (VideoContentProperty::VIDEO_RATIO);
220 }