std::shared_ptr
[dcpomatic.git] / src / lib / video_content.h
1 /*
2     Copyright (C) 2013-2020 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #ifndef DCPOMATIC_VIDEO_CONTENT_H
22 #define DCPOMATIC_VIDEO_CONTENT_H
23
24 #include "colour_conversion.h"
25 #include "dcpomatic_time.h"
26 #include "user_property.h"
27 #include "types.h"
28 #include "content_part.h"
29 #include <boost/thread/mutex.hpp>
30
31 class VideoExaminer;
32 class Ratio;
33 class Film;
34 class Content;
35
36 class VideoContentProperty
37 {
38 public:
39         static int const USE;
40         static int const SIZE;
41         static int const FRAME_TYPE;
42         static int const CROP;
43         static int const SCALE;
44         static int const COLOUR_CONVERSION;
45         static int const FADE_IN;
46         static int const FADE_OUT;
47         static int const RANGE;
48         static int const CUSTOM_RATIO;
49         static int const CUSTOM_SIZE;
50 };
51
52 class VideoContent : public ContentPart, public std::enable_shared_from_this<VideoContent>
53 {
54 public:
55         explicit VideoContent (Content* parent);
56         VideoContent (Content* parent, std::vector<std::shared_ptr<Content> >);
57
58         void as_xml (xmlpp::Node *) const;
59         std::string technical_summary () const;
60         std::string identifier () const;
61         void take_settings_from (std::shared_ptr<const VideoContent> c);
62
63         Frame length () const {
64                 boost::mutex::scoped_lock lm (_mutex);
65                 return _length;
66         }
67
68         Frame length_after_3d_combine () const {
69                 boost::mutex::scoped_lock lm (_mutex);
70                 if (_frame_type == VIDEO_FRAME_TYPE_3D_ALTERNATE) {
71                         return _length / 2;
72                 }
73
74                 return _length;
75         }
76
77         dcp::Size size () const {
78                 boost::mutex::scoped_lock lm (_mutex);
79                 return _size;
80         }
81
82         void set_frame_type (VideoFrameType);
83
84         void set_left_crop (int);
85         void set_right_crop (int);
86         void set_top_crop (int);
87         void set_bottom_crop (int);
88
89         void set_custom_ratio (boost::optional<float> ratio);
90         void set_custom_size (boost::optional<dcp::Size> size);
91
92         void unset_colour_conversion ();
93         void set_colour_conversion (ColourConversion);
94
95         void set_fade_in (Frame);
96         void set_fade_out (Frame);
97
98         void set_range (VideoRange);
99         void set_use (bool);
100
101         VideoFrameType frame_type () const {
102                 boost::mutex::scoped_lock lm (_mutex);
103                 return _frame_type;
104         }
105
106         Crop crop () const {
107                 boost::mutex::scoped_lock lm (_mutex);
108                 return _crop;
109         }
110
111         int left_crop () const {
112                 boost::mutex::scoped_lock lm (_mutex);
113                 return _crop.left;
114         }
115
116         int right_crop () const {
117                 boost::mutex::scoped_lock lm (_mutex);
118                 return _crop.right;
119         }
120
121         int top_crop () const {
122                 boost::mutex::scoped_lock lm (_mutex);
123                 return _crop.top;
124         }
125
126         int bottom_crop () const {
127                 boost::mutex::scoped_lock lm (_mutex);
128                 return _crop.bottom;
129         }
130
131
132         boost::optional<float> custom_ratio () const {
133                 boost::mutex::scoped_lock lm (_mutex);
134                 return _custom_ratio;
135         }
136
137
138         boost::optional<dcp::Size> custom_size () const {
139                 boost::mutex::scoped_lock lm (_mutex);
140                 return _custom_size;
141         }
142
143
144         boost::optional<ColourConversion> colour_conversion () const {
145                 boost::mutex::scoped_lock lm (_mutex);
146                 return _colour_conversion;
147         }
148
149         boost::optional<double> sample_aspect_ratio () const {
150                 boost::mutex::scoped_lock lm (_mutex);
151                 return _sample_aspect_ratio;
152         }
153
154         bool yuv () const {
155                 boost::mutex::scoped_lock lm (_mutex);
156                 return _yuv;
157         }
158
159         Frame fade_in () const {
160                 boost::mutex::scoped_lock lm (_mutex);
161                 return _fade_in;
162         }
163
164         Frame fade_out () const {
165                 boost::mutex::scoped_lock lm (_mutex);
166                 return _fade_out;
167         }
168
169         VideoRange range () const {
170                 boost::mutex::scoped_lock lm (_mutex);
171                 return _range;
172         }
173
174         bool use () const {
175                 boost::mutex::scoped_lock lm (_mutex);
176                 return _use;
177         }
178
179         /* XXX: names for these? */
180         dcp::Size size_after_3d_split () const;
181         dcp::Size size_after_crop () const;
182         dcp::Size scaled_size (dcp::Size container_size);
183
184         boost::optional<double> fade (std::shared_ptr<const Film> film, Frame) const;
185
186         std::string processing_description (std::shared_ptr<const Film> film);
187
188         void set_length (Frame);
189
190         void take_from_examiner (std::shared_ptr<VideoExaminer>);
191         void add_properties (std::list<UserProperty> &) const;
192
193         void modify_position (std::shared_ptr<const Film> film, dcpomatic::DCPTime& pos) const;
194         void modify_trim_start (dcpomatic::ContentTime& pos) const;
195
196         static std::shared_ptr<VideoContent> from_xml (Content* parent, cxml::ConstNodePtr, int);
197
198 private:
199
200         friend struct ffmpeg_pts_offset_test;
201         friend struct best_dcp_frame_rate_test_single;
202         friend struct best_dcp_frame_rate_test_double;
203         friend struct audio_sampling_rate_test;
204         friend struct scaled_size_test1;
205         friend struct scaled_size_test2;
206         friend struct scaled_size_legacy_test;
207
208         VideoContent (Content* parent, cxml::ConstNodePtr, int);
209         void setup_default_colour_conversion ();
210
211         bool _use;
212         Frame _length;
213         boost::optional<ColourConversion> _colour_conversion;
214         dcp::Size _size;
215         VideoFrameType _frame_type;
216         Crop _crop;
217         /** ratio to scale cropped image to (or none to guess); i.e. if set, scale to _custom_ratio:1 */
218         boost::optional<float> _custom_ratio;
219         /** size to scale cropped image to; only used if _custom_ratio is none */
220         boost::optional<dcp::Size> _custom_size;
221         /** ratio obtained from an older metadata file; will be used to set up
222          *  _custom_{ratio,size} (or not, if not required) on the first call to
223          *  scaled_size()
224          */
225         boost::optional<float> _legacy_ratio;
226         /** Sample aspect ratio obtained from the content file's header, if there is one */
227         boost::optional<double> _sample_aspect_ratio;
228         bool _yuv;
229         /** fade in time in content frames */
230         Frame _fade_in;
231         /** fade out time in content frames */
232         Frame _fade_out;
233         VideoRange _range;
234 };
235
236 #endif