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