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