]> git.sesse.net Git - casparcg/blob - core/frame/frame_transform.h
bf2cdb35f550a12e299a90b6c05f70446482d751
[casparcg] / core / frame / frame_transform.h
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 #pragma once
23
24 #include <common/tweener.h>
25 #include <common/env.h>
26
27 #include <core/video_format.h>
28 #include <core/mixer/image/blend_modes.h>
29
30 #include <boost/array.hpp>
31 #include <boost/property_tree/ptree.hpp>
32
33 namespace caspar { namespace core {
34
35 struct chroma
36 {
37         enum class type
38         {
39                 none,
40                 green,
41                 blue
42         };
43
44         type    key                     = type::none;
45         double  threshold       = 0.0;
46         double  softness        = 0.0;
47         double  spill           = 0.0;
48 };
49
50 struct levels final
51 {
52         double min_input        = 0.0;
53         double max_input        = 1.0;
54         double gamma            = 1.0;
55         double min_output       = 0.0;
56         double max_output       = 1.0;
57 };
58
59 struct corners final
60 {
61         boost::array<double, 2> ul = boost::array<double, 2> { { 0.0, 0.0 } };
62         boost::array<double, 2> ur = boost::array<double, 2> { { 1.0, 0.0 } };
63         boost::array<double, 2> lr = boost::array<double, 2> { { 1.0, 1.0 } };
64         boost::array<double, 2> ll = boost::array<double, 2> { { 0.0, 1.0 } };
65 };
66
67 struct rectangle final
68 {
69         boost::array<double, 2> ul = boost::array<double, 2> { { 0.0, 0.0 } };
70         boost::array<double, 2> lr = boost::array<double, 2> { { 1.0, 1.0 } };
71 };
72
73 struct image_transform final
74 {
75         double                                  opacity                         = 1.0;
76         double                                  contrast                        = 1.0;
77         double                                  brightness                      = 1.0;
78         double                                  saturation                      = 1.0;
79
80         // A bug in VS 2013 prevents us from writing:
81         // boost::array<double, 2> fill_translation = { { 0.0, 0.0 } };
82         // See http://blogs.msdn.com/b/vcblog/archive/2014/08/19/the-future-of-non-static-data-member-initialization.aspx
83         boost::array<double, 2> anchor                          = boost::array<double, 2> { { 0.0, 0.0 } };
84         boost::array<double, 2> fill_translation        = boost::array<double, 2> { { 0.0, 0.0 } };
85         boost::array<double, 2> fill_scale                      = boost::array<double, 2> { { 1.0, 1.0 } };
86         boost::array<double, 2> clip_translation        = boost::array<double, 2> { { 0.0, 0.0 } };
87         boost::array<double, 2> clip_scale                      = boost::array<double, 2> { { 1.0, 1.0 } };
88         double                                  angle                           = 0.0;
89         rectangle                               crop;
90         corners                                 perspective;
91         core::levels                    levels;
92         core::chroma                    chroma;
93
94         core::field_mode                field_mode                      = core::field_mode::progressive;
95         bool                                    is_key                          = false;
96         bool                                    is_mix                          = false;
97         bool                                    is_still                        = false;
98         bool                                    use_mipmap                      = false;
99         core::blend_mode                blend_mode                      = core::blend_mode::normal;
100         int                                             layer_depth                     = 0;
101         
102         image_transform& operator*=(const image_transform &other);
103         image_transform operator*(const image_transform &other) const;
104
105         static image_transform tween(double time, const image_transform& source, const image_transform& dest, double duration, const tweener& tween);
106 };
107
108 bool operator==(const image_transform& lhs, const image_transform& rhs);
109 bool operator!=(const image_transform& lhs, const image_transform& rhs);
110
111 struct audio_transform final
112 {
113         double  volume          = 1.0;
114         bool    is_still        = false;
115         
116         audio_transform& operator*=(const audio_transform &other);
117         audio_transform operator*(const audio_transform &other) const;
118
119         static audio_transform tween(double time, const audio_transform& source, const audio_transform& dest, double duration, const tweener& tween);
120 };
121
122 bool operator==(const audio_transform& lhs, const audio_transform& rhs);
123 bool operator!=(const audio_transform& lhs, const audio_transform& rhs);
124
125 //__declspec(align(16)) 
126 struct frame_transform final
127 {
128 public:
129         frame_transform();
130         
131         core::image_transform image_transform;
132         core::audio_transform audio_transform;
133
134         //char padding[(sizeof(core::image_transform) + sizeof(core::audio_transform)) % 16];
135         
136         frame_transform& operator*=(const frame_transform &other);
137         frame_transform operator*(const frame_transform &other) const;
138
139         static frame_transform tween(double time, const frame_transform& source, const frame_transform& dest, double duration, const tweener& tween);
140 };
141
142 bool operator==(const frame_transform& lhs, const frame_transform& rhs);
143 bool operator!=(const frame_transform& lhs, const frame_transform& rhs);
144
145 class tweened_transform
146 {
147         frame_transform source_;
148         frame_transform dest_;
149         int duration_;
150         int time_;
151         tweener tweener_;
152 public: 
153         tweened_transform()
154                 : duration_(0)
155                 , time_(0)
156         {
157                 dest_.image_transform.use_mipmap = env::properties().get(L"configuration.mixer.mipmapping_default_on", false);
158         }
159
160         tweened_transform(const frame_transform& source, const frame_transform& dest, int duration, const tweener& tween)
161                 : source_(source)
162                 , dest_(dest)
163                 , duration_(duration)
164                 , time_(0)
165                 , tweener_(tween)
166         {
167         }
168
169         const frame_transform& dest() const
170         {
171                 return dest_;
172         }
173         
174         frame_transform fetch()
175         {
176                 return time_ == duration_ ? dest_ : frame_transform::tween(static_cast<double>(time_), source_, dest_, static_cast<double>(duration_), tweener_);
177         }
178
179         frame_transform fetch_and_tick(int num)
180         {                                               
181                 time_ = std::min(time_+num, duration_);
182                 return fetch();
183         }
184 };
185
186 chroma::type get_chroma_mode(const std::wstring& str);
187 std::wstring get_chroma_mode(chroma::type type);
188
189 namespace detail {
190
191 void set_current_aspect_ratio(double aspect_ratio);
192 double get_current_aspect_ratio();
193
194 }}}