Allow content parts to not be preset in XML.
[dcpomatic.git] / src / lib / video_content.h
1 /*
2     Copyright (C) 2013-2016 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 #ifndef DCPOMATIC_VIDEO_CONTENT_H
21 #define DCPOMATIC_VIDEO_CONTENT_H
22
23 #include "colour_conversion.h"
24 #include "video_content_scale.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 SIZE;
42         static int const FRAME_TYPE;
43         static int const CROP;
44         static int const SCALE;
45         static int const COLOUR_CONVERSION;
46         static int const FADE_IN;
47         static int const FADE_OUT;
48 };
49
50 class VideoContent : public ContentPart, public boost::enable_shared_from_this<VideoContent>
51 {
52 public:
53         VideoContent (Content* parent, boost::shared_ptr<const Film>);
54         VideoContent (Content* parent, boost::shared_ptr<const Film>, std::vector<boost::shared_ptr<Content> >);
55
56         void as_xml (xmlpp::Node *) const;
57         std::string technical_summary () const;
58         std::string identifier () const;
59
60         Frame length () const {
61                 boost::mutex::scoped_lock lm (_mutex);
62                 return _length;
63         }
64
65         Frame length_after_3d_combine () const {
66                 boost::mutex::scoped_lock lm (_mutex);
67                 if (_frame_type == VIDEO_FRAME_TYPE_3D_ALTERNATE) {
68                         return _length / 2;
69                 }
70
71                 return _length;
72         }
73
74         dcp::Size size () const {
75                 boost::mutex::scoped_lock lm (_mutex);
76                 return _size;
77         }
78
79         void set_frame_type (VideoFrameType);
80
81         void set_left_crop (int);
82         void set_right_crop (int);
83         void set_top_crop (int);
84         void set_bottom_crop (int);
85
86         void set_scale (VideoContentScale);
87         void unset_colour_conversion ();
88         void set_colour_conversion (ColourConversion);
89
90         void set_fade_in (Frame);
91         void set_fade_out (Frame);
92
93         VideoFrameType frame_type () const {
94                 boost::mutex::scoped_lock lm (_mutex);
95                 return _frame_type;
96         }
97
98         Crop crop () const {
99                 boost::mutex::scoped_lock lm (_mutex);
100                 return _crop;
101         }
102
103         int left_crop () const {
104                 boost::mutex::scoped_lock lm (_mutex);
105                 return _crop.left;
106         }
107
108         int right_crop () const {
109                 boost::mutex::scoped_lock lm (_mutex);
110                 return _crop.right;
111         }
112
113         int top_crop () const {
114                 boost::mutex::scoped_lock lm (_mutex);
115                 return _crop.top;
116         }
117
118         int bottom_crop () const {
119                 boost::mutex::scoped_lock lm (_mutex);
120                 return _crop.bottom;
121         }
122
123         /** @return Description of how to scale this content (if indeed it should be scaled) */
124         VideoContentScale scale () const {
125                 boost::mutex::scoped_lock lm (_mutex);
126                 return _scale;
127         }
128
129         boost::optional<ColourConversion> colour_conversion () const {
130                 boost::mutex::scoped_lock lm (_mutex);
131                 return _colour_conversion;
132         }
133
134         boost::optional<double> sample_aspect_ratio () const {
135                 boost::mutex::scoped_lock lm (_mutex);
136                 return _sample_aspect_ratio;
137         }
138
139         bool yuv () const {
140                 boost::mutex::scoped_lock lm (_mutex);
141                 return _yuv;
142         }
143
144         Frame fade_in () const {
145                 boost::mutex::scoped_lock lm (_mutex);
146                 return _fade_in;
147         }
148
149         Frame fade_out () const {
150                 boost::mutex::scoped_lock lm (_mutex);
151                 return _fade_out;
152         }
153
154         dcp::Size size_after_3d_split () const;
155         dcp::Size size_after_crop () const;
156
157         boost::optional<double> fade (Frame) const;
158
159         void scale_and_crop_to_fit_width ();
160         void scale_and_crop_to_fit_height ();
161
162         std::string processing_description () const;
163
164         void set_length (Frame);
165
166         void take_from_examiner (boost::shared_ptr<VideoExaminer>);
167         void add_properties (std::list<UserProperty> &) const;
168
169         static boost::shared_ptr<VideoContent> from_xml (Content* parent, boost::shared_ptr<const Film>, cxml::ConstNodePtr, int);
170
171 private:
172
173         friend struct ffmpeg_pts_offset_test;
174         friend struct best_dcp_frame_rate_test_single;
175         friend struct best_dcp_frame_rate_test_double;
176         friend struct audio_sampling_rate_test;
177
178         VideoContent (Content* parent, boost::shared_ptr<const Film>, cxml::ConstNodePtr, int);
179         void setup_default_colour_conversion ();
180
181         Frame _length;
182         boost::optional<ColourConversion> _colour_conversion;
183         dcp::Size _size;
184         VideoFrameType _frame_type;
185         Crop _crop;
186         VideoContentScale _scale;
187         /** Sample aspect ratio obtained from the content file's header, if there is one */
188         boost::optional<double> _sample_aspect_ratio;
189         bool _yuv;
190         Frame _fade_in;
191         Frame _fade_out;
192 };
193
194 #endif