]> git.sesse.net Git - casparcg/blob - core/producer/frame/basic_frame.cpp
2.0. mixer: Refactored transforms, merged image and audio transform.
[casparcg] / core / producer / frame / basic_frame.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\r
5 *\r
6 *    CasparCG is free software: you can redistribute it and/or modify\r
7 *    it under the terms of the GNU General Public License as published by\r
8 *    the Free Software Foundation, either version 3 of the License, or\r
9 *    (at your option) any later version.\r
10 *\r
11 *    CasparCG is distributed in the hope that it will be useful,\r
12 *    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 *    GNU General Public License for more details.\r
15 \r
16 *    You should have received a copy of the GNU General Public License\r
17 *    along with CasparCG.  If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 */\r
20 #include "../../stdafx.h"\r
21 \r
22 #include "basic_frame.h"\r
23 \r
24 #include "frame_transform.h"\r
25 #include "../../video_format.h"\r
26 \r
27 #include <boost/foreach.hpp>\r
28 \r
29 namespace caspar { namespace core {\r
30                                                                                                                                                                                                                                                                                                                 \r
31 struct basic_frame::implementation\r
32 {               \r
33         std::vector<safe_ptr<basic_frame>> frames_;\r
34 \r
35         frame_transform frame_transform_;       \r
36         \r
37 public:\r
38         implementation(const std::vector<safe_ptr<basic_frame>>& frames) : frames_(frames) \r
39         {\r
40         }\r
41         implementation(std::vector<safe_ptr<basic_frame>>&& frames) : frames_(std::move(frames))\r
42         {\r
43         }\r
44         implementation(safe_ptr<basic_frame>&& frame) \r
45         {\r
46                 frames_.push_back(std::move(frame));\r
47         }\r
48         implementation(const safe_ptr<basic_frame>& frame)              \r
49         { \r
50                 frames_.push_back(frame);\r
51         }\r
52         \r
53         void accept(basic_frame& self, frame_visitor& visitor)\r
54         {\r
55                 visitor.begin(self);\r
56                 BOOST_FOREACH(auto frame, frames_)\r
57                         frame->accept(visitor);\r
58                 visitor.end();\r
59         }       \r
60 \r
61         std::wstring print() const\r
62         {\r
63                 std::wstring str = L"\tbasic_frame[\n";\r
64                 BOOST_FOREACH(auto& frame, frames_)\r
65                         str += frame->print() + L"\n";\r
66                 str += L"\n]";\r
67                 return str;\r
68         }\r
69 };\r
70         \r
71 basic_frame::basic_frame() : impl_(new implementation(std::vector<safe_ptr<basic_frame>>())){}\r
72 basic_frame::basic_frame(const std::vector<safe_ptr<basic_frame>>& frames) : impl_(new implementation(frames)){}\r
73 basic_frame::basic_frame(const basic_frame& other) : impl_(new implementation(*other.impl_)){}\r
74 basic_frame::basic_frame(std::vector<safe_ptr<basic_frame>>&& frames) : impl_(new implementation(frames)){}\r
75 basic_frame::basic_frame(const safe_ptr<basic_frame>& frame) : impl_(new implementation(frame)){}\r
76 basic_frame::basic_frame(safe_ptr<basic_frame>&& frame)  : impl_(new implementation(std::move(frame))){}\r
77 basic_frame::basic_frame(basic_frame&& other) : impl_(std::move(other.impl_)){}\r
78 basic_frame& basic_frame::operator=(const basic_frame& other)\r
79 {\r
80         basic_frame temp(other);\r
81         temp.swap(*this);\r
82         return *this;\r
83 }\r
84 basic_frame& basic_frame::operator=(basic_frame&& other)\r
85 {\r
86         basic_frame temp(std::move(other));\r
87         temp.swap(*this);\r
88         return *this;\r
89 }\r
90 void basic_frame::swap(basic_frame& other){impl_.swap(other.impl_);}\r
91 \r
92 const frame_transform& basic_frame::get_frame_transform() const { return impl_->frame_transform_;}\r
93 frame_transform& basic_frame::get_frame_transform() { return impl_->frame_transform_;}\r
94 \r
95 std::wstring basic_frame::print() const{return impl_->print();}\r
96 void basic_frame::accept(frame_visitor& visitor){impl_->accept(*this, visitor);}\r
97 \r
98 safe_ptr<basic_frame> basic_frame::interlace(const safe_ptr<basic_frame>& frame1, const safe_ptr<basic_frame>& frame2, field_mode::type mode)\r
99 {                               \r
100         if(frame1 == basic_frame::eof() || frame2 == basic_frame::eof())\r
101                 return basic_frame::eof();\r
102 \r
103         if(frame1 == basic_frame::empty() && frame2 == basic_frame::empty())\r
104                 return basic_frame::empty();\r
105         \r
106         if(frame1 == frame2 || mode == field_mode::progressive)\r
107                 return frame2;\r
108 \r
109         auto my_frame1 = make_safe<basic_frame>(frame1);\r
110         auto my_frame2 = make_safe<basic_frame>(frame2);\r
111         if(mode == field_mode::upper)\r
112         {\r
113                 my_frame1->get_frame_transform().field_mode = field_mode::upper;        \r
114                 my_frame2->get_frame_transform().field_mode = field_mode::lower;        \r
115         }                                                                        \r
116         else                                                             \r
117         {                                                                        \r
118                 my_frame1->get_frame_transform().field_mode = field_mode::lower;        \r
119                 my_frame2->get_frame_transform().field_mode = field_mode::upper;        \r
120         }\r
121 \r
122         std::vector<safe_ptr<basic_frame>> frames;\r
123         frames.push_back(my_frame1);\r
124         frames.push_back(my_frame2);\r
125         return basic_frame(std::move(frames));\r
126 }\r
127 \r
128 safe_ptr<basic_frame> basic_frame::combine(const safe_ptr<basic_frame>& frame1, const safe_ptr<basic_frame>& frame2)\r
129 {       \r
130         if(frame1 == basic_frame::eof() || frame2 == basic_frame::eof())\r
131                 return basic_frame::eof();\r
132         \r
133         if(frame1 == basic_frame::empty() && frame2 == basic_frame::empty())\r
134                 return basic_frame::empty();\r
135 \r
136         std::vector<safe_ptr<basic_frame>> frames;\r
137         frames.push_back(frame1);\r
138         frames.push_back(frame2);\r
139         return basic_frame(std::move(frames));\r
140 }\r
141 \r
142 safe_ptr<basic_frame> basic_frame::fill_and_key(const safe_ptr<basic_frame>& fill, const safe_ptr<basic_frame>& key)\r
143 {       \r
144         if(fill == basic_frame::eof() || key == basic_frame::eof())\r
145                 return basic_frame::eof();\r
146 \r
147         if(fill == basic_frame::empty() || key == basic_frame::empty())\r
148                 return basic_frame::empty();\r
149 \r
150         std::vector<safe_ptr<basic_frame>> frames;\r
151         key->get_frame_transform().is_key = true;\r
152         frames.push_back(key);\r
153         frames.push_back(fill);\r
154         return basic_frame(std::move(frames));\r
155 }\r
156 \r
157 safe_ptr<basic_frame> disable_audio(const safe_ptr<basic_frame>& frame)\r
158 {\r
159         basic_frame frame2 = frame;\r
160         frame2.get_frame_transform().volume = 0.0;\r
161         return std::move(frame2);\r
162 }\r
163         \r
164 }}