]> git.sesse.net Git - casparcg/blob - accelerator/ogl/image/image_mixer.cpp
* Merged MIXER CROP, MIXER ANCHOR, MIXER ROTATION and MIXER PERSPECTIVE from 2.0
[casparcg] / accelerator / ogl / image / image_mixer.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 #include "../../StdAfx.h"
22
23 #include "image_mixer.h"
24
25 #include "image_kernel.h"
26
27 #include "../util/device.h"
28 #include "../util/buffer.h"
29 #include "../util/texture.h"
30
31 #include <common/gl/gl_check.h>
32 #include <common/future.h>
33 #include <common/array.h>
34 #include <common/linq.h>
35
36 #include <core/frame/frame.h>
37 #include <core/frame/frame_transform.h>
38 #include <core/frame/pixel_format.h>
39 #include <core/video_format.h>
40 #include <core/frame/geometry.h>
41
42 #include <asmlib.h>
43
44 #include <GL/glew.h>
45
46 #include <boost/range/algorithm_ext/erase.hpp>
47 #include <boost/thread/future.hpp>
48
49 #include <algorithm>
50 #include <vector>
51
52 namespace caspar { namespace accelerator { namespace ogl {
53                 
54 typedef std::shared_future<std::shared_ptr<texture>> future_texture;
55
56 struct item
57 {
58         core::pixel_format_desc         pix_desc        = core::pixel_format::invalid;
59         std::vector<future_texture>     textures;
60         core::image_transform           transform;
61         core::frame_geometry            geometry        = core::frame_geometry::get_default();
62 };
63
64 struct layer
65 {
66         std::vector<item>       items;
67         core::blend_mode        blend_mode;
68
69         layer()
70                 : blend_mode(core::blend_mode::normal)
71         {
72         }
73
74         layer(std::vector<item> items, core::blend_mode blend_mode)
75                 : items(std::move(items))
76                 , blend_mode(blend_mode)
77         {
78         }
79 };
80
81 std::size_t get_max_video_format_size()
82 {
83         auto format_size = [](core::video_format format) { return core::video_format_desc(format).size; };
84         return cpplinq::from(enum_constants<core::video_format>())
85                 .select(std::ref(format_size))
86                 .max();
87 }
88
89 class image_renderer
90 {
91         spl::shared_ptr<device> ogl_;
92         image_kernel                    kernel_;
93 public:
94         image_renderer(const spl::shared_ptr<device>& ogl, bool blend_modes_wanted)
95                 : ogl_(ogl)
96                 , kernel_(ogl_, blend_modes_wanted)
97         {
98         }
99         
100         std::future<array<const std::uint8_t>> operator()(std::vector<layer> layers, const core::video_format_desc& format_desc)
101         {       
102                 if(layers.empty())
103                 { // Bypass GPU with empty frame.
104                         static const cache_aligned_vector<uint8_t> buffer(get_max_video_format_size(), 0);
105                         return make_ready_future(array<const std::uint8_t>(buffer.data(), format_desc.size, true));
106                 }
107
108                 if(format_desc.field_mode != core::field_mode::progressive)
109                 { // Remove jitter from still.
110                         for (auto& layer : layers)
111                         {       
112                                 // Remove first field stills.
113                                 boost::range::remove_erase_if(layer.items, [&](const item& item)
114                                 {
115                                         return item.transform.is_still && item.transform.field_mode == format_desc.field_mode; // only us last field for stills.
116                                 });
117                 
118                                 // Stills are progressive
119                                 for (auto& item : layer.items)
120                                 {
121                                         if(item.transform.is_still)
122                                                 item.transform.field_mode = core::field_mode::progressive;
123                                 }
124                         }
125                 }
126
127                 return flatten(ogl_->begin_invoke([=]() mutable -> std::shared_future<array<const std::uint8_t>>
128                 {
129                         auto target_texture = ogl_->create_texture(format_desc.width, format_desc.height, 4);
130
131                         if (format_desc.field_mode != core::field_mode::progressive)
132                         {
133                                 draw(target_texture, layers, format_desc, core::field_mode::upper);
134                                 draw(target_texture, std::move(layers), format_desc, core::field_mode::lower);
135                         }
136                         else
137                                 draw(target_texture, std::move(layers), format_desc, core::field_mode::progressive);
138
139                         return ogl_->copy_async(target_texture);
140                 }));
141         }
142
143 private:        
144         
145         void draw(spl::shared_ptr<texture>&                     target_texture, 
146                           std::vector<layer>                            layers, 
147                           const core::video_format_desc&        format_desc,
148                           core::field_mode                                      field_mode)
149         {
150                 std::shared_ptr<texture> layer_key_texture;
151
152                 for (auto& layer : layers)
153                         draw(target_texture, std::move(layer), layer_key_texture, format_desc, field_mode);
154         }
155
156         void draw(spl::shared_ptr<texture>&                     target_texture,
157                           layer                                                         layer, 
158                           std::shared_ptr<texture>&                     layer_key_texture,
159                           const core::video_format_desc&        format_desc,
160                           core::field_mode                                      field_mode)
161         {                       
162                 // REMOVED: This is done in frame_muxer. 
163                 // Fix frames
164                 //BOOST_FOREACH(auto& item, layer.items)                
165                 //{
166                         //if(std::abs(item.transform.fill_scale[1]-1.0) > 1.0/target_texture->height() ||
167                         //   std::abs(item.transform.fill_translation[1]) > 1.0/target_texture->height())               
168                         //      CASPAR_LOG(warning) << L"[image_mixer] Frame should be deinterlaced. Send FILTER DEINTERLACE_BOB when creating producer.";      
169
170                         //if(item.pix_desc.planes.at(0).height == 480) // NTSC DV
171                         //{
172                         //      item.transform.fill_translation[1] += 2.0/static_cast<double>(format_desc.height);
173                         //      item.transform.fill_scale[1] *= 1.0 - 6.0*1.0/static_cast<double>(format_desc.height);
174                         //}
175         
176                         //// Fix field-order if needed
177                         //if(item.field_mode == core::field_mode::lower && format_desc.field_mode == core::field_mode::upper)
178                         //      item.transform.fill_translation[1] += 1.0/static_cast<double>(format_desc.height);
179                         //else if(item.field_mode == core::field_mode::upper && format_desc.field_mode == core::field_mode::lower)
180                         //      item.transform.fill_translation[1] -= 1.0/static_cast<double>(format_desc.height);
181                 //}
182
183                 // Mask out fields
184                 for (auto& item : layer.items)                          
185                         item.transform.field_mode &= field_mode;
186                 
187                 // Remove empty items.
188                 boost::range::remove_erase_if(layer.items, [&](const item& item)
189                 {
190                         return item.transform.field_mode == core::field_mode::empty;
191                 });
192                 
193                 if(layer.items.empty())
194                         return;
195
196                 std::shared_ptr<texture> local_key_texture;
197                 std::shared_ptr<texture> local_mix_texture;
198                                 
199                 if(layer.blend_mode != core::blend_mode::normal)
200                 {
201                         auto layer_texture = ogl_->create_texture(target_texture->width(), target_texture->height(), 4);
202
203                         for (auto& item : layer.items)
204                                 draw(layer_texture, std::move(item), layer_key_texture, local_key_texture, local_mix_texture, format_desc);
205                 
206                         draw(layer_texture, std::move(local_mix_texture), core::blend_mode::normal);                                                    
207                         draw(target_texture, std::move(layer_texture), layer.blend_mode);
208                 }
209                 else // fast path
210                 {
211                         for (auto& item : layer.items)          
212                                 draw(target_texture, std::move(item), layer_key_texture, local_key_texture, local_mix_texture, format_desc);
213                                         
214                         draw(target_texture, std::move(local_mix_texture), core::blend_mode::normal);
215                 }                                       
216
217                 layer_key_texture = std::move(local_key_texture);
218         }
219
220         void draw(spl::shared_ptr<texture>& target_texture, 
221                           item item, 
222                       std::shared_ptr<texture>& layer_key_texture, 
223                           std::shared_ptr<texture>& local_key_texture, 
224                           std::shared_ptr<texture>& local_mix_texture,
225                           const core::video_format_desc& format_desc)
226         {                       
227                 draw_params draw_params;
228                 draw_params.pix_desc            = std::move(item.pix_desc);
229                 draw_params.transform           = std::move(item.transform);
230                 draw_params.geometry            = item.geometry;
231                 draw_params.aspect_ratio        = static_cast<double>(format_desc.square_width) / static_cast<double>(format_desc.square_height);
232
233                 for (auto& future_texture : item.textures)
234                         draw_params.textures.push_back(spl::make_shared_ptr(future_texture.get()));
235
236                 if(item.transform.is_key)
237                 {
238                         local_key_texture = local_key_texture ? local_key_texture : ogl_->create_texture(target_texture->width(), target_texture->height(), 1);
239
240                         draw_params.background  = local_key_texture;
241                         draw_params.local_key   = nullptr;
242                         draw_params.layer_key   = nullptr;
243
244                         kernel_.draw(std::move(draw_params));
245                 }
246                 else if(item.transform.is_mix)
247                 {
248                         local_mix_texture = local_mix_texture ? local_mix_texture : ogl_->create_texture(target_texture->width(), target_texture->height(), 4);
249
250                         draw_params.background  = local_mix_texture;
251                         draw_params.local_key   = std::move(local_key_texture);
252                         draw_params.layer_key   = layer_key_texture;
253
254                         draw_params.keyer               = keyer::additive;
255
256                         kernel_.draw(std::move(draw_params));
257                 }
258                 else
259                 {
260                         draw(target_texture, std::move(local_mix_texture), core::blend_mode::normal);
261                         
262                         draw_params.background  = target_texture;
263                         draw_params.local_key   = std::move(local_key_texture);
264                         draw_params.layer_key   = layer_key_texture;
265
266                         kernel_.draw(std::move(draw_params));
267                 }       
268         }
269
270         void draw(spl::shared_ptr<texture>&      target_texture, 
271                           std::shared_ptr<texture>&& source_buffer, 
272                           core::blend_mode                       blend_mode = core::blend_mode::normal)
273         {
274                 if(!source_buffer)
275                         return;
276
277                 draw_params draw_params;
278                 draw_params.pix_desc.format     = core::pixel_format::bgra;
279                 draw_params.pix_desc.planes     = { core::pixel_format_desc::plane(source_buffer->width(), source_buffer->height(), 4) };
280                 draw_params.textures            = { spl::make_shared_ptr(source_buffer) };
281                 draw_params.transform           = core::image_transform();
282                 draw_params.blend_mode          = blend_mode;
283                 draw_params.background          = target_texture;
284                 draw_params.geometry            = core::frame_geometry::get_default();
285
286                 kernel_.draw(std::move(draw_params));
287         }
288 };
289                 
290 struct image_mixer::impl : public core::frame_factory
291 {       
292         spl::shared_ptr<device>                         ogl_;
293         image_renderer                                          renderer_;
294         std::vector<core::image_transform>      transform_stack_;
295         std::vector<layer>                                      layers_; // layer/stream/items
296 public:
297         impl(const spl::shared_ptr<device>& ogl, bool blend_modes_wanted) 
298                 : ogl_(ogl)
299                 , renderer_(ogl, blend_modes_wanted)
300                 , transform_stack_(1)   
301         {
302                 CASPAR_LOG(info) << L"Initialized OpenGL Accelerated GPU Image Mixer";
303         }
304
305         void begin_layer(core::blend_mode blend_mode)
306         {
307                 layers_.push_back(layer(std::vector<item>(), blend_mode));
308         }
309                 
310         void push(const core::frame_transform& transform)
311         {
312                 transform_stack_.push_back(transform_stack_.back()*transform.image_transform);
313         }
314                 
315         void visit(const core::const_frame& frame)
316         {                       
317                 if(frame.pixel_format_desc().format == core::pixel_format::invalid)
318                         return;
319
320                 if(frame.pixel_format_desc().planes.empty())
321                         return;
322
323                 if(transform_stack_.back().field_mode == core::field_mode::empty)
324                         return;
325
326                 item item;
327                 item.pix_desc   = frame.pixel_format_desc();
328                 item.transform  = transform_stack_.back();
329                 item.geometry   = frame.geometry();
330                 
331                 // NOTE: Once we have copied the arrays they are no longer valid for reading!!! Check for alternative solution e.g. transfer with AMD_pinned_memory.
332                 for(int n = 0; n < static_cast<int>(item.pix_desc.planes.size()); ++n)
333                         item.textures.push_back(ogl_->copy_async(frame.image_data(n), item.pix_desc.planes[n].width, item.pix_desc.planes[n].height, item.pix_desc.planes[n].stride));
334                 
335                 layers_.back().items.push_back(item);
336         }
337
338         void pop()
339         {
340                 transform_stack_.pop_back();
341         }
342
343         void end_layer()
344         {               
345         }
346         
347         std::future<array<const std::uint8_t>> render(const core::video_format_desc& format_desc)
348         {
349                 return renderer_(std::move(layers_), format_desc);
350         }
351         
352         core::mutable_frame create_frame(const void* tag, const core::pixel_format_desc& desc) override
353         {
354                 std::vector<array<std::uint8_t>> buffers;
355                 for (auto& plane : desc.planes)         
356                         buffers.push_back(ogl_->create_array(plane.size));              
357
358                 return core::mutable_frame(std::move(buffers), core::audio_buffer(), tag, desc);
359         }
360 };
361
362 image_mixer::image_mixer(const spl::shared_ptr<device>& ogl, bool blend_modes_wanted) : impl_(new impl(ogl, blend_modes_wanted)){}
363 image_mixer::~image_mixer(){}
364 void image_mixer::push(const core::frame_transform& transform){impl_->push(transform);}
365 void image_mixer::visit(const core::const_frame& frame){impl_->visit(frame);}
366 void image_mixer::pop(){impl_->pop();}
367 std::future<array<const std::uint8_t>> image_mixer::operator()(const core::video_format_desc& format_desc){return impl_->render(format_desc);}
368 void image_mixer::begin_layer(core::blend_mode blend_mode){impl_->begin_layer(blend_mode);}
369 void image_mixer::end_layer(){impl_->end_layer();}
370 core::mutable_frame image_mixer::create_frame(const void* tag, const core::pixel_format_desc& desc) {return impl_->create_frame(tag, desc);}
371
372 }}}