Take some stuff out of VideoContent into ContentPart.
[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 VIDEO_SIZE;
42         static int const VIDEO_FRAME_RATE;
43         static int const VIDEO_FRAME_TYPE;
44         static int const VIDEO_CROP;
45         static int const VIDEO_SCALE;
46         static int const COLOUR_CONVERSION;
47         static int const VIDEO_FADE_IN;
48         static int const VIDEO_FADE_OUT;
49 };
50
51 class VideoContent : public ContentPart, public boost::enable_shared_from_this<VideoContent>
52 {
53 public:
54         VideoContent (Content* parent, boost::shared_ptr<const Film>);
55         VideoContent (Content* parent, boost::shared_ptr<const Film>, cxml::ConstNodePtr, int);
56         VideoContent (Content* parent, boost::shared_ptr<const Film>, std::vector<boost::shared_ptr<Content> >);
57
58         void as_xml (xmlpp::Node *) const;
59         std::string technical_summary () const;
60         std::string identifier () const;
61
62         void set_default_colour_conversion ();
63
64         Frame video_length () const {
65                 boost::mutex::scoped_lock lm (_mutex);
66                 return _video_length;
67         }
68
69         Frame video_length_after_3d_combine () const {
70                 boost::mutex::scoped_lock lm (_mutex);
71                 if (_video_frame_type == VIDEO_FRAME_TYPE_3D_ALTERNATE) {
72                         return _video_length / 2;
73                 }
74
75                 return _video_length;
76         }
77
78         dcp::Size video_size () const {
79                 boost::mutex::scoped_lock lm (_mutex);
80                 return _video_size;
81         }
82
83         double video_frame_rate () const;
84
85         /** @return true if this content has a specific video frame rate, false
86          *  if it should use the DCP's rate.
87          */
88         bool has_own_video_frame_rate () const {
89                 boost::mutex::scoped_lock lm (_mutex);
90                 return static_cast<bool>(_video_frame_rate);
91         }
92
93         void set_video_frame_type (VideoFrameType);
94         void set_video_frame_rate (double);
95
96         void set_left_crop (int);
97         void set_right_crop (int);
98         void set_top_crop (int);
99         void set_bottom_crop (int);
100
101         void set_scale (VideoContentScale);
102         void unset_colour_conversion ();
103         void set_colour_conversion (ColourConversion);
104
105         void set_fade_in (Frame);
106         void set_fade_out (Frame);
107
108         VideoFrameType video_frame_type () const {
109                 boost::mutex::scoped_lock lm (_mutex);
110                 return _video_frame_type;
111         }
112
113         Crop crop () const {
114                 boost::mutex::scoped_lock lm (_mutex);
115                 return _crop;
116         }
117
118         int left_crop () const {
119                 boost::mutex::scoped_lock lm (_mutex);
120                 return _crop.left;
121         }
122
123         int right_crop () const {
124                 boost::mutex::scoped_lock lm (_mutex);
125                 return _crop.right;
126         }
127
128         int top_crop () const {
129                 boost::mutex::scoped_lock lm (_mutex);
130                 return _crop.top;
131         }
132
133         int bottom_crop () const {
134                 boost::mutex::scoped_lock lm (_mutex);
135                 return _crop.bottom;
136         }
137
138         /** @return Description of how to scale this content (if indeed it should be scaled) */
139         VideoContentScale scale () const {
140                 boost::mutex::scoped_lock lm (_mutex);
141                 return _scale;
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         dcp::Size video_size_after_3d_split () const;
170         dcp::Size video_size_after_crop () const;
171
172         ContentTime dcp_time_to_content_time (DCPTime) const;
173
174         boost::optional<double> fade (Frame) const;
175
176         void scale_and_crop_to_fit_width ();
177         void scale_and_crop_to_fit_height ();
178
179         std::string processing_description () const;
180
181         void set_video_length (Frame);
182
183         void take_from_video_examiner (boost::shared_ptr<VideoExaminer>);
184         void add_properties (std::list<UserProperty> &) const;
185
186 private:
187
188         Frame _video_length;
189         /** Video frame rate, or not set if this content should use the DCP's frame rate */
190         boost::optional<double> _video_frame_rate;
191         boost::optional<ColourConversion> _colour_conversion;
192
193         friend struct ffmpeg_pts_offset_test;
194         friend struct best_dcp_frame_rate_test_single;
195         friend struct best_dcp_frame_rate_test_double;
196         friend struct audio_sampling_rate_test;
197
198         void setup_default_colour_conversion ();
199
200         dcp::Size _video_size;
201         VideoFrameType _video_frame_type;
202         Crop _crop;
203         VideoContentScale _scale;
204         /** Sample aspect ratio obtained from the content file's header,
205             if there is one.
206         */
207         boost::optional<double> _sample_aspect_ratio;
208         bool _yuv;
209         Frame _fade_in;
210         Frame _fade_out;
211 };
212
213 #endif