864fa11aa87eb36262dcb95e7499dea64b514ef7
[dcpomatic.git] / src / lib / video_content.h
1 /*
2     Copyright (C) 2013-2021 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
22 #ifndef DCPOMATIC_VIDEO_CONTENT_H
23 #define DCPOMATIC_VIDEO_CONTENT_H
24
25
26 #include "colour_conversion.h"
27 #include "content_part.h"
28 #include "crop.h"
29 #include "dcpomatic_time.h"
30 #include "pixel_quanta.h"
31 #include "user_property.h"
32 #include "video_frame_type.h"
33 #include "video_range.h"
34 #include "video_type.h"
35 #include "types.h"
36 #include <dcp/language_tag.h>
37 #include <boost/thread/mutex.hpp>
38
39
40 class VideoExaminer;
41 class Ratio;
42 class Film;
43 class Content;
44
45
46 class VideoContentProperty
47 {
48 public:
49         static int const USE;
50         static int const SIZE;
51         static int const FRAME_TYPE;
52         static int const CROP;
53         static int const COLOUR_CONVERSION;
54         static int const FADE_IN;
55         static int const FADE_OUT;
56         static int const RANGE;
57         static int const CUSTOM_RATIO;
58         static int const CUSTOM_SIZE;
59         static int const BURNT_SUBTITLE_LANGUAGE;
60         static int const TYPE;
61 };
62
63
64 class VideoContent : public ContentPart, public std::enable_shared_from_this<VideoContent>
65 {
66 public:
67         explicit VideoContent (Content* parent);
68         VideoContent (Content* parent, cxml::ConstNodePtr node, int version, VideoRange video_range_hint);
69         VideoContent (Content* parent, std::vector<std::shared_ptr<Content>>);
70
71         void as_xml (xmlpp::Node *) const;
72         std::string technical_summary () const;
73         std::string identifier () const;
74         void take_settings_from (std::shared_ptr<const VideoContent> c);
75
76         Frame length () const {
77                 boost::mutex::scoped_lock lm (_mutex);
78                 return _length;
79         }
80
81         Frame length_after_3d_combine () const {
82                 boost::mutex::scoped_lock lm (_mutex);
83                 if (_frame_type == VideoFrameType::THREE_D_ALTERNATE) {
84                         return _length / 2;
85                 }
86
87                 return _length;
88         }
89
90         dcp::Size size () const {
91                 boost::mutex::scoped_lock lm (_mutex);
92                 return _size;
93         }
94
95         void set_type(VideoType type);
96         void set_frame_type (VideoFrameType);
97
98         void set_crop (Crop crop);
99         void set_left_crop (int);
100         void set_right_crop (int);
101         void set_top_crop (int);
102         void set_bottom_crop (int);
103
104         void set_custom_ratio (boost::optional<float> ratio);
105         void set_custom_size (boost::optional<dcp::Size> size);
106
107         void unset_colour_conversion ();
108         void set_colour_conversion (ColourConversion);
109
110         void set_fade_in (Frame);
111         void set_fade_out (Frame);
112
113         void set_range (VideoRange);
114         void set_use (bool);
115
116         void set_burnt_subtitle_language (boost::optional<dcp::LanguageTag> language);
117
118         VideoType type() const {
119                 boost::mutex::scoped_lock lm (_mutex);
120                 return _type;
121         }
122
123         VideoFrameType frame_type () const {
124                 boost::mutex::scoped_lock lm (_mutex);
125                 return _frame_type;
126         }
127
128         Crop actual_crop () const;
129
130         Crop requested_crop () const {
131                 boost::mutex::scoped_lock lm (_mutex);
132                 return _crop;
133         }
134
135         int requested_left_crop () const {
136                 boost::mutex::scoped_lock lm (_mutex);
137                 return _crop.left;
138         }
139
140         int requested_right_crop () const {
141                 boost::mutex::scoped_lock lm (_mutex);
142                 return _crop.right;
143         }
144
145         int requested_top_crop () const {
146                 boost::mutex::scoped_lock lm (_mutex);
147                 return _crop.top;
148         }
149
150         int requested_bottom_crop () const {
151                 boost::mutex::scoped_lock lm (_mutex);
152                 return _crop.bottom;
153         }
154
155
156         boost::optional<float> custom_ratio () const {
157                 boost::mutex::scoped_lock lm (_mutex);
158                 return _custom_ratio;
159         }
160
161
162         boost::optional<dcp::Size> custom_size () const {
163                 boost::mutex::scoped_lock lm (_mutex);
164                 return _custom_size;
165         }
166
167
168         boost::optional<ColourConversion> colour_conversion () const {
169                 boost::mutex::scoped_lock lm (_mutex);
170                 return _colour_conversion;
171         }
172
173         boost::optional<double> sample_aspect_ratio () const {
174                 boost::mutex::scoped_lock lm (_mutex);
175                 return _sample_aspect_ratio;
176         }
177
178         bool yuv () const {
179                 boost::mutex::scoped_lock lm (_mutex);
180                 return _yuv;
181         }
182
183         Frame fade_in () const {
184                 boost::mutex::scoped_lock lm (_mutex);
185                 return _fade_in;
186         }
187
188         Frame fade_out () const {
189                 boost::mutex::scoped_lock lm (_mutex);
190                 return _fade_out;
191         }
192
193         VideoRange range () const {
194                 boost::mutex::scoped_lock lm (_mutex);
195                 return _range;
196         }
197
198         PixelQuanta pixel_quanta () const {
199                 boost::mutex::scoped_lock lm (_mutex);
200                 return _pixel_quanta;
201         }
202
203         bool use () const {
204                 boost::mutex::scoped_lock lm (_mutex);
205                 return _use;
206         }
207
208         boost::optional<dcp::LanguageTag> burnt_subtitle_language () const {
209                 boost::mutex::scoped_lock lm (_mutex);
210                 return _burnt_subtitle_language;
211         }
212
213
214         /* XXX: names for these? */
215         dcp::Size size_after_3d_split () const;
216         dcp::Size size_after_crop () const;
217         dcp::Size scaled_size (dcp::Size container_size);
218
219         boost::optional<double> fade(std::shared_ptr<const Film> film, dcpomatic::ContentTime time) const;
220
221         std::string processing_description (std::shared_ptr<const Film> film);
222
223         void set_length (Frame);
224
225         void take_from_examiner(std::shared_ptr<const Film> film, std::shared_ptr<VideoExaminer>);
226         void add_properties (std::list<UserProperty> &) const;
227
228         void modify_position (std::shared_ptr<const Film> film, dcpomatic::DCPTime& pos) const;
229         void modify_trim_start (dcpomatic::ContentTime& pos) const;
230
231         static std::shared_ptr<VideoContent> from_xml (Content* parent, cxml::ConstNodePtr node, int version, VideoRange video_range_hint);
232
233 private:
234
235         friend struct ffmpeg_pts_offset_test;
236         friend struct best_dcp_frame_rate_test_single;
237         friend struct best_dcp_frame_rate_test_double;
238         friend struct audio_sampling_rate_test;
239         friend struct scaled_size_test1;
240         friend struct scaled_size_test2;
241         friend struct scaled_size_legacy_test;
242
243         void setup_default_colour_conversion ();
244
245         bool _use;
246         Frame _length;
247         boost::optional<ColourConversion> _colour_conversion;
248         dcp::Size _size;
249         VideoType _type;
250         VideoFrameType _frame_type;
251         Crop _crop;
252         /** ratio to scale cropped image to (or none to guess); i.e. if set, scale to _custom_ratio:1 */
253         boost::optional<float> _custom_ratio;
254         /** size to scale cropped image to; only used if _custom_ratio is none */
255         boost::optional<dcp::Size> _custom_size;
256         /** ratio obtained from an older metadata file; will be used to set up
257          *  _custom_{ratio,size} (or not, if not required) on the first call to
258          *  scaled_size()
259          */
260         boost::optional<float> _legacy_ratio;
261         /** Sample aspect ratio obtained from the content file's header, if there is one */
262         boost::optional<double> _sample_aspect_ratio;
263         bool _yuv;
264         /** fade in time in content frames */
265         Frame _fade_in;
266         /** fade out time in content frames */
267         Frame _fade_out;
268         VideoRange _range;
269         PixelQuanta _pixel_quanta;
270         boost::optional<dcp::LanguageTag> _burnt_subtitle_language;
271 };
272
273
274 #endif