9404676808504d912814e70673d9d28e348826bb
[dcpomatic.git] / src / lib / video_content.h
1 /*
2     Copyright (C) 2013-2014 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 "content.h"
24 #include "colour_conversion.h"
25
26 class VideoExaminer;
27 class Ratio;
28
29 class VideoContentProperty
30 {
31 public:
32         static int const VIDEO_SIZE;
33         static int const VIDEO_FRAME_RATE;
34         static int const VIDEO_FRAME_TYPE;
35         static int const VIDEO_CROP;
36         static int const VIDEO_SCALE;
37         static int const COLOUR_CONVERSION;
38 };
39
40 class VideoContentScale
41 {
42 public:
43         VideoContentScale ();
44         VideoContentScale (Ratio const *);
45         VideoContentScale (bool);
46         VideoContentScale (boost::shared_ptr<cxml::Node>);
47
48         libdcp::Size size (boost::shared_ptr<const VideoContent>, libdcp::Size, libdcp::Size) const;
49         std::string id () const;
50         std::string name () const;
51         void as_xml (xmlpp::Node *) const;
52
53         Ratio const * ratio () const {
54                 return _ratio;
55         }
56
57         bool scale () const {
58                 return _scale;
59         }
60
61         static void setup_scales ();
62         static std::vector<VideoContentScale> all () {
63                 return _scales;
64         }
65         static VideoContentScale from_id (std::string id);
66
67 private:
68         /** a ratio to stretch the content to, or 0 for no stretch */
69         Ratio const * _ratio;
70         /** true if we want to scale the content */
71         bool _scale;
72
73         static std::vector<VideoContentScale> _scales;
74 };
75
76 bool operator== (VideoContentScale const & a, VideoContentScale const & b);
77 bool operator!= (VideoContentScale const & a, VideoContentScale const & b);
78
79 class VideoContent : public virtual Content
80 {
81 public:
82         typedef int Frame;
83
84         VideoContent (boost::shared_ptr<const Film>);
85         VideoContent (boost::shared_ptr<const Film>, Time, VideoContent::Frame);
86         VideoContent (boost::shared_ptr<const Film>, boost::filesystem::path);
87         VideoContent (boost::shared_ptr<const Film>, boost::shared_ptr<const cxml::Node>, int);
88         VideoContent (boost::shared_ptr<const Film>, std::vector<boost::shared_ptr<Content> >);
89
90         void as_xml (xmlpp::Node *) const;
91         std::string technical_summary () const;
92         virtual std::string information () const;
93         virtual std::string identifier () const;
94
95         VideoContent::Frame video_length () const {
96                 boost::mutex::scoped_lock lm (_mutex);
97                 return _video_length;
98         }
99
100         VideoContent::Frame video_length_after_3d_combine () const {
101                 boost::mutex::scoped_lock lm (_mutex);
102                 if (_video_frame_type == VIDEO_FRAME_TYPE_3D_ALTERNATE) {
103                         return _video_length / 2;
104                 }
105                 
106                 return _video_length;
107         }
108
109         libdcp::Size video_size () const {
110                 boost::mutex::scoped_lock lm (_mutex);
111                 return _video_size;
112         }
113         
114         float video_frame_rate () const {
115                 boost::mutex::scoped_lock lm (_mutex);
116                 return _video_frame_rate;
117         }
118
119         float original_video_frame_rate () const {
120                 boost::mutex::scoped_lock lm (_mutex);
121                 return _original_video_frame_rate;
122         }
123         
124         void set_video_frame_type (VideoFrameType);
125         void set_video_frame_rate (float);
126
127         void set_left_crop (int);
128         void set_right_crop (int);
129         void set_top_crop (int);
130         void set_bottom_crop (int);
131
132         void set_scale (VideoContentScale);
133         void set_colour_conversion (ColourConversion);
134         
135         VideoFrameType video_frame_type () const {
136                 boost::mutex::scoped_lock lm (_mutex);
137                 return _video_frame_type;
138         }
139
140         Crop crop () const {
141                 boost::mutex::scoped_lock lm (_mutex);
142                 return _crop;
143         }
144
145         int left_crop () const {
146                 boost::mutex::scoped_lock lm (_mutex);
147                 return _crop.left;
148         }
149
150         int right_crop () const {
151                 boost::mutex::scoped_lock lm (_mutex);
152                 return _crop.right;
153         }
154
155         int top_crop () const {
156                 boost::mutex::scoped_lock lm (_mutex);
157                 return _crop.top;
158         }
159
160         int bottom_crop () const {
161                 boost::mutex::scoped_lock lm (_mutex);
162                 return _crop.bottom;
163         }
164
165         /** @return Description of how to scale this content (if indeed it should be scaled) */
166         VideoContentScale scale () const {
167                 boost::mutex::scoped_lock lm (_mutex);
168                 return _scale;
169         }
170
171         ColourConversion colour_conversion () const {
172                 boost::mutex::scoped_lock lm (_mutex);
173                 return _colour_conversion;
174         }
175
176         libdcp::Size video_size_after_3d_split () const;
177         libdcp::Size video_size_after_crop () const;
178
179         VideoContent::Frame time_to_content_video_frames (Time) const;
180
181         void scale_and_crop_to_fit_width ();
182         void scale_and_crop_to_fit_height ();
183
184 protected:
185         void take_from_video_examiner (boost::shared_ptr<VideoExaminer>);
186
187         VideoContent::Frame _video_length;
188         float _original_video_frame_rate;
189         float _video_frame_rate;
190
191 private:
192         friend class ffmpeg_pts_offset_test;
193         friend class best_dcp_frame_rate_test_single;
194         friend class best_dcp_frame_rate_test_double;
195         friend class audio_sampling_rate_test;
196
197         void setup_default_colour_conversion ();
198         
199         libdcp::Size _video_size;
200         VideoFrameType _video_frame_type;
201         Crop _crop;
202         VideoContentScale _scale;
203         ColourConversion _colour_conversion;
204 };
205
206 #endif