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