Change video content scaling so that it either:
[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 #include <boost/weak_ptr.hpp>
31 #include <boost/enable_shared_from_this.hpp>
32
33 class VideoExaminer;
34 class Ratio;
35 class Film;
36 class Content;
37
38 class VideoContentProperty
39 {
40 public:
41         static int const USE;
42         static int const SIZE;
43         static int const FRAME_TYPE;
44         static int const CROP;
45         static int const SCALE;
46         static int const COLOUR_CONVERSION;
47         static int const FADE_IN;
48         static int const FADE_OUT;
49         static int const RANGE;
50         static int const CUSTOM_RATIO;
51         static int const CUSTOM_SIZE;
52 };
53
54 class VideoContent : public ContentPart, public boost::enable_shared_from_this<VideoContent>
55 {
56 public:
57         explicit VideoContent (Content* parent);
58         VideoContent (Content* parent, std::vector<boost::shared_ptr<Content> >);
59
60         void as_xml (xmlpp::Node *) const;
61         std::string technical_summary () const;
62         std::string identifier () const;
63         void take_settings_from (boost::shared_ptr<const VideoContent> c);
64
65         Frame length () const {
66                 boost::mutex::scoped_lock lm (_mutex);
67                 return _length;
68         }
69
70         Frame length_after_3d_combine () const {
71                 boost::mutex::scoped_lock lm (_mutex);
72                 if (_frame_type == VIDEO_FRAME_TYPE_3D_ALTERNATE) {
73                         return _length / 2;
74                 }
75
76                 return _length;
77         }
78
79         dcp::Size size () const {
80                 boost::mutex::scoped_lock lm (_mutex);
81                 return _size;
82         }
83
84         void set_frame_type (VideoFrameType);
85
86         void set_left_crop (int);
87         void set_right_crop (int);
88         void set_top_crop (int);
89         void set_bottom_crop (int);
90
91         void set_custom_ratio (boost::optional<float> ratio);
92         void set_custom_size (boost::optional<dcp::Size> size);
93
94         void unset_colour_conversion ();
95         void set_colour_conversion (ColourConversion);
96
97         void set_fade_in (Frame);
98         void set_fade_out (Frame);
99
100         void set_range (VideoRange);
101         void set_use (bool);
102
103         VideoFrameType frame_type () const {
104                 boost::mutex::scoped_lock lm (_mutex);
105                 return _frame_type;
106         }
107
108         Crop crop () const {
109                 boost::mutex::scoped_lock lm (_mutex);
110                 return _crop;
111         }
112
113         int left_crop () const {
114                 boost::mutex::scoped_lock lm (_mutex);
115                 return _crop.left;
116         }
117
118         int right_crop () const {
119                 boost::mutex::scoped_lock lm (_mutex);
120                 return _crop.right;
121         }
122
123         int top_crop () const {
124                 boost::mutex::scoped_lock lm (_mutex);
125                 return _crop.top;
126         }
127
128         int bottom_crop () const {
129                 boost::mutex::scoped_lock lm (_mutex);
130                 return _crop.bottom;
131         }
132
133
134         boost::optional<float> custom_ratio () const {
135                 boost::mutex::scoped_lock lm (_mutex);
136                 return _custom_ratio;
137         }
138
139
140         boost::optional<dcp::Size> custom_size () const {
141                 boost::mutex::scoped_lock lm (_mutex);
142                 return _custom_size;
143         }
144
145
146         boost::optional<ColourConversion> colour_conversion () const {
147                 boost::mutex::scoped_lock lm (_mutex);
148                 return _colour_conversion;
149         }
150
151         boost::optional<double> sample_aspect_ratio () const {
152                 boost::mutex::scoped_lock lm (_mutex);
153                 return _sample_aspect_ratio;
154         }
155
156         bool yuv () const {
157                 boost::mutex::scoped_lock lm (_mutex);
158                 return _yuv;
159         }
160
161         Frame fade_in () const {
162                 boost::mutex::scoped_lock lm (_mutex);
163                 return _fade_in;
164         }
165
166         Frame fade_out () const {
167                 boost::mutex::scoped_lock lm (_mutex);
168                 return _fade_out;
169         }
170
171         VideoRange range () const {
172                 boost::mutex::scoped_lock lm (_mutex);
173                 return _range;
174         }
175
176         bool use () const {
177                 boost::mutex::scoped_lock lm (_mutex);
178                 return _use;
179         }
180
181         /* XXX: names for these? */
182         dcp::Size size_after_3d_split () const;
183         dcp::Size size_after_crop () const;
184         dcp::Size scaled_size (dcp::Size container_size);
185
186         boost::optional<double> fade (boost::shared_ptr<const Film> film, Frame) const;
187
188         std::string processing_description (boost::shared_ptr<const Film> film);
189
190         void set_length (Frame);
191
192         void take_from_examiner (boost::shared_ptr<VideoExaminer>);
193         void add_properties (std::list<UserProperty> &) const;
194
195         void modify_position (boost::shared_ptr<const Film> film, dcpomatic::DCPTime& pos) const;
196         void modify_trim_start (dcpomatic::ContentTime& pos) const;
197
198         static boost::shared_ptr<VideoContent> from_xml (Content* parent, cxml::ConstNodePtr, int);
199
200 private:
201
202         friend struct ffmpeg_pts_offset_test;
203         friend struct best_dcp_frame_rate_test_single;
204         friend struct best_dcp_frame_rate_test_double;
205         friend struct audio_sampling_rate_test;
206         friend struct scaled_size_test1;
207         friend struct scaled_size_test2;
208         friend struct scaled_size_legacy_test;
209
210         VideoContent (Content* parent, cxml::ConstNodePtr, int);
211         void setup_default_colour_conversion ();
212
213         bool _use;
214         Frame _length;
215         boost::optional<ColourConversion> _colour_conversion;
216         dcp::Size _size;
217         VideoFrameType _frame_type;
218         Crop _crop;
219         /** ratio to scale cropped image to (or none to guess); i.e. if set, scale to _custom_ratio:1 */
220         boost::optional<float> _custom_ratio;
221         /** size to scale cropped image to; only used if _custom_ratio is none */
222         boost::optional<dcp::Size> _custom_size;
223         /** ratio obtained from an older metadata file; will be used to set up
224          *  _custom_{ratio,size} (or not, if not required) on the first call to
225          *  scaled_size()
226          */
227         boost::optional<float> _legacy_ratio;
228         /** Sample aspect ratio obtained from the content file's header, if there is one */
229         boost::optional<double> _sample_aspect_ratio;
230         bool _yuv;
231         /** fade in time in content frames */
232         Frame _fade_in;
233         /** fade out time in content frames */
234         Frame _fade_out;
235         VideoRange _range;
236 };
237
238 #endif