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