]> git.sesse.net Git - casparcg/blob - core/frame/frame_transform.cpp
* Merged MIXER CROP, MIXER ANCHOR, MIXER ROTATION and MIXER PERSPECTIVE from 2.0
[casparcg] / core / frame / frame_transform.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG 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 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG 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 CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "../StdAfx.h"
23
24 #include "frame_transform.h"
25
26 #include <boost/range/algorithm/equal.hpp>
27 #include <boost/thread.hpp>
28
29 namespace caspar { namespace core {
30
31 // image_transform
32
33 template<typename Rect>
34 void transform_rect(Rect& self, const Rect& other)
35 {
36         self.ul[0] += other.ul[0];
37         self.ul[1] += other.ul[1];
38         self.lr[0] *= other.lr[0];
39         self.lr[1] *= other.lr[1];
40 }
41
42 void transform_corners(corners& self, const corners& other)
43 {
44         transform_rect(self, other);
45
46         self.ur[0] *= other.ur[0];
47         self.ur[1] += other.ur[1];
48         self.ll[0] += other.ll[0];
49         self.ll[1] *= other.ll[1];
50
51         // TODO: figure out the math to compose perspective transforms correctly.
52 }
53
54 image_transform& image_transform::operator*=(const image_transform &other)
55 {
56         opacity                                 *= other.opacity;       
57         brightness                              *= other.brightness;
58         contrast                                *= other.contrast;
59         saturation                              *= other.saturation;
60
61         // TODO: can this be done in any way without knowing the aspect ratio of the
62         // actual video mode? Thread local to the rescue
63         auto aspect_ratio                = detail::get_current_aspect_ratio();
64         aspect_ratio                    *= fill_scale[0] / fill_scale[1];
65
66         boost::array<double, 2> rotated;
67
68         auto orig_x                              = other.fill_translation[0];
69         auto orig_y                              = other.fill_translation[1] / aspect_ratio;
70         rotated[0]                               = orig_x * std::cos(angle) - orig_y * std::sin(angle);
71         rotated[1]                               = orig_x * std::sin(angle) + orig_y * std::cos(angle);
72         rotated[1]                              *= aspect_ratio;
73
74         anchor[0]                               += other.anchor[0] * fill_scale[0];
75         anchor[1]                               += other.anchor[1] * fill_scale[1];
76         fill_translation[0]             += rotated[0] * fill_scale[0];
77         fill_translation[1]             += rotated[1] * fill_scale[1];
78         fill_scale[0]                   *= other.fill_scale[0];
79         fill_scale[1]                   *= other.fill_scale[1];
80         clip_translation[0]             += other.clip_translation[0] * clip_scale[0];
81         clip_translation[1]             += other.clip_translation[1] * clip_scale[1];
82         clip_scale[0]                   *= other.clip_scale[0];
83         clip_scale[1]                   *= other.clip_scale[1];
84         angle                                   += other.angle;
85
86         transform_rect(crop, other.crop);
87         transform_corners(perspective, other.perspective);
88
89         levels.min_input                 = std::max(levels.min_input,  other.levels.min_input);
90         levels.max_input                 = std::min(levels.max_input,  other.levels.max_input); 
91         levels.min_output                = std::max(levels.min_output, other.levels.min_output);
92         levels.max_output                = std::min(levels.max_output, other.levels.max_output);
93         levels.gamma                    *= other.levels.gamma;
94         field_mode                               = field_mode & other.field_mode;
95         is_key                                  |= other.is_key;
96         is_mix                                  |= other.is_mix;
97         is_still                                |= other.is_still;
98
99         return *this;
100 }
101
102 image_transform image_transform::operator*(const image_transform &other) const
103 {
104         return image_transform(*this) *= other;
105 }
106
107 double do_tween(double time, double source, double dest, double duration, const tweener& tween)
108 {
109         return tween(time, source, dest - source, duration);
110 };
111
112 template<typename Rect>
113 void do_tween_rectangle(const Rect& source, const Rect& dest, Rect& out, double time, double duration, const tweener& tweener)
114 {
115         out.ul[0] = do_tween(time, source.ul[0], dest.ul[0], duration, tweener);
116         out.ul[1] = do_tween(time, source.ul[1], dest.ul[1], duration, tweener);
117         out.lr[0] = do_tween(time, source.lr[0], dest.lr[0], duration, tweener);
118         out.lr[1] = do_tween(time, source.lr[1], dest.lr[1], duration, tweener);
119 }
120
121 void do_tween_corners(const corners& source, const corners& dest, corners& out, double time, double duration, const tweener& tweener)
122 {
123         do_tween_rectangle(source, dest, out, time, duration, tweener);
124
125         out.ur[0] = do_tween(time, source.ur[0], dest.ur[0], duration, tweener);
126         out.ur[1] = do_tween(time, source.ur[1], dest.ur[1], duration, tweener);
127         out.ll[0] = do_tween(time, source.ll[0], dest.ll[0], duration, tweener);
128         out.ll[1] = do_tween(time, source.ll[1], dest.ll[1], duration, tweener);
129 };
130
131 image_transform image_transform::tween(double time, const image_transform& source, const image_transform& dest, double duration, const tweener& tween)
132 {       
133         image_transform result; 
134
135         result.brightness                       = do_tween(time, source.brightness,                             dest.brightness,                        duration, tween);
136         result.contrast                         = do_tween(time, source.contrast,                               dest.contrast,                          duration, tween);
137         result.saturation                       = do_tween(time, source.saturation,                             dest.saturation,                        duration, tween);
138         result.opacity                          = do_tween(time, source.opacity,                                dest.opacity,                           duration, tween);
139         result.anchor[0]                        = do_tween(time, source.anchor[0],                              dest.anchor[0],                         duration, tween);
140         result.anchor[1]                        = do_tween(time, source.anchor[1],                              dest.anchor[1],                         duration, tween);
141         result.fill_translation[0]      = do_tween(time, source.fill_translation[0],    dest.fill_translation[0],       duration, tween);
142         result.fill_translation[1]      = do_tween(time, source.fill_translation[1],    dest.fill_translation[1],       duration, tween);
143         result.fill_scale[0]            = do_tween(time, source.fill_scale[0],                  dest.fill_scale[0],                     duration, tween);
144         result.fill_scale[1]            = do_tween(time, source.fill_scale[1],                  dest.fill_scale[1],                     duration, tween);
145         result.clip_translation[0]      = do_tween(time, source.clip_translation[0],    dest.clip_translation[0],       duration, tween);
146         result.clip_translation[1]      = do_tween(time, source.clip_translation[1],    dest.clip_translation[1],       duration, tween);
147         result.clip_scale[0]            = do_tween(time, source.clip_scale[0],                  dest.clip_scale[0],                     duration, tween);
148         result.clip_scale[1]            = do_tween(time, source.clip_scale[1],                  dest.clip_scale[1],                     duration, tween);
149         result.angle                            = do_tween(time, source.angle,                                  dest.angle,                                     duration, tween);
150         result.levels.max_input         = do_tween(time, source.levels.max_input,               dest.levels.max_input,          duration, tween);
151         result.levels.min_input         = do_tween(time, source.levels.min_input,               dest.levels.min_input,          duration, tween);
152         result.levels.max_output        = do_tween(time, source.levels.max_output,              dest.levels.max_output,         duration, tween);
153         result.levels.min_output        = do_tween(time, source.levels.min_output,              dest.levels.min_output,         duration, tween);
154         result.levels.gamma                     = do_tween(time, source.levels.gamma,                   dest.levels.gamma,                      duration, tween);
155         result.field_mode                       = source.field_mode & dest.field_mode;
156         result.is_key                           = source.is_key | dest.is_key;
157         result.is_mix                           = source.is_mix | dest.is_mix;
158         result.is_still                         = source.is_still | dest.is_still;
159
160         do_tween_rectangle(source.crop, dest.crop, result.crop, time, duration, tween);
161         do_tween_corners(source.perspective, dest.perspective, result.perspective, time, duration, tween);
162
163         return result;
164 }
165
166 bool eq(double lhs, double rhs)
167 {
168         return std::abs(lhs - rhs) < 5e-8;
169 };
170
171 bool operator==(const corners& lhs, const corners& rhs)
172 {
173         return
174                 boost::range::equal(lhs.ul, rhs.ul, eq) &&
175                 boost::range::equal(lhs.ur, rhs.ur, eq) &&
176                 boost::range::equal(lhs.lr, rhs.lr, eq) &&
177                 boost::range::equal(lhs.ll, rhs.ll, eq);
178 }
179
180 bool operator==(const rectangle& lhs, const rectangle& rhs)
181 {
182         return
183                 boost::range::equal(lhs.ul, rhs.ul, eq) &&
184                 boost::range::equal(lhs.lr, rhs.lr, eq);
185 }
186
187 bool operator==(const image_transform& lhs, const image_transform& rhs)
188 {
189         return 
190                 eq(lhs.opacity, rhs.opacity) &&
191                 eq(lhs.contrast, rhs.contrast) &&
192                 eq(lhs.brightness, rhs.brightness) &&
193                 eq(lhs.saturation, rhs.saturation) &&
194                 boost::range::equal(lhs.anchor, rhs.anchor, eq) &&
195                 boost::range::equal(lhs.fill_translation, rhs.fill_translation, eq) &&
196                 boost::range::equal(lhs.fill_scale, rhs.fill_scale, eq) &&
197                 boost::range::equal(lhs.clip_translation, rhs.clip_translation, eq) &&
198                 boost::range::equal(lhs.clip_scale, rhs.clip_scale, eq) &&
199                 eq(lhs.angle, rhs.angle) &&
200                 lhs.field_mode == rhs.field_mode &&
201                 lhs.is_key == rhs.is_key &&
202                 lhs.is_mix == rhs.is_mix &&
203                 lhs.is_still == rhs.is_still &&
204                 lhs.crop == rhs.crop &&
205                 lhs.perspective == rhs.perspective;
206 }
207
208 bool operator!=(const image_transform& lhs, const image_transform& rhs)
209 {
210         return !(lhs == rhs);
211 }
212
213 // audio_transform
214
215 audio_transform& audio_transform::operator*=(const audio_transform &other)
216 {
217         volume   *= other.volume;       
218         is_still |= other.is_still;
219         return *this;
220 }
221
222 audio_transform audio_transform::operator*(const audio_transform &other) const
223 {
224         return audio_transform(*this) *= other;
225 }
226
227 audio_transform audio_transform::tween(double time, const audio_transform& source, const audio_transform& dest, double duration, const tweener& tween)
228 {       
229         audio_transform result;
230         result.is_still                 = source.is_still | dest.is_still;
231         result.volume                   = do_tween(time, source.volume,                         dest.volume,                    duration, tween);
232         
233         return result;
234 }
235
236 bool operator==(const audio_transform& lhs, const audio_transform& rhs)
237 {
238         return eq(lhs.volume, rhs.volume) && lhs.is_still == rhs.is_still;
239 }
240
241 bool operator!=(const audio_transform& lhs, const audio_transform& rhs)
242 {
243         return !(lhs == rhs);
244 }
245
246 // frame_transform
247 frame_transform::frame_transform()
248 {
249 }
250
251 frame_transform& frame_transform::operator*=(const frame_transform &other)
252 {
253         image_transform *= other.image_transform;
254         audio_transform *= other.audio_transform;
255         return *this;
256 }
257
258 frame_transform frame_transform::operator*(const frame_transform &other) const
259 {
260         return frame_transform(*this) *= other;
261 }
262
263 frame_transform frame_transform::tween(double time, const frame_transform& source, const frame_transform& dest, double duration, const tweener& tween)
264 {
265         frame_transform result;
266         result.image_transform = image_transform::tween(time, source.image_transform, dest.image_transform, duration, tween);
267         result.audio_transform = audio_transform::tween(time, source.audio_transform, dest.audio_transform, duration, tween);
268         return result;
269 }
270
271 bool operator==(const frame_transform& lhs, const frame_transform& rhs)
272 {
273         return  lhs.image_transform == rhs.image_transform && 
274                         lhs.audio_transform == rhs.audio_transform;
275 }
276
277 bool operator!=(const frame_transform& lhs, const frame_transform& rhs)
278 {
279         return !(lhs == rhs);
280 }
281
282 namespace detail {
283
284 boost::thread_specific_ptr<double>& get_thread_local_aspect_ratio()
285 {
286         static boost::thread_specific_ptr<double> aspect_ratio;
287
288         if (!aspect_ratio.get())
289                 aspect_ratio.reset(new double(1.0));
290
291         return aspect_ratio;
292 }
293
294 void set_current_aspect_ratio(double aspect_ratio)
295 {
296         *get_thread_local_aspect_ratio() = aspect_ratio;
297 }
298
299 double get_current_aspect_ratio()
300 {
301         return *get_thread_local_aspect_ratio();
302 }
303
304 }}}