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