]> git.sesse.net Git - casparcg/commitdiff
2.0.0.2: More missing files.
authorronag <ronag@362d55ac-95cf-4e76-9f9a-cbaa9c17b72d>
Mon, 17 Jan 2011 18:06:18 +0000 (18:06 +0000)
committerronag <ronag@362d55ac-95cf-4e76-9f9a-cbaa9c17b72d>
Mon, 17 Jan 2011 18:06:18 +0000 (18:06 +0000)
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches/2.0.0.2@380 362d55ac-95cf-4e76-9f9a-cbaa9c17b72d

23 files changed:
core/mixer/audio/audio_mixer.cpp [new file with mode: 0644]
core/mixer/audio/audio_mixer.h [new file with mode: 0644]
core/mixer/frame/draw_frame.cpp [new file with mode: 0644]
core/mixer/frame/draw_frame.h [new file with mode: 0644]
core/mixer/frame/pixel_format.h [new file with mode: 0644]
core/mixer/frame/read_frame.cpp [new file with mode: 0644]
core/mixer/frame/read_frame.h [new file with mode: 0644]
core/mixer/frame/write_frame.cpp [new file with mode: 0644]
core/mixer/frame/write_frame.h [new file with mode: 0644]
core/mixer/frame_factory.h [new file with mode: 0644]
core/mixer/frame_mixer_device.cpp [new file with mode: 0644]
core/mixer/frame_mixer_device.h [new file with mode: 0644]
core/mixer/fwd.h [new file with mode: 0644]
core/mixer/gpu/device_buffer.cpp [new file with mode: 0644]
core/mixer/gpu/device_buffer.h [new file with mode: 0644]
core/mixer/gpu/host_buffer.cpp [new file with mode: 0644]
core/mixer/gpu/host_buffer.h [new file with mode: 0644]
core/mixer/gpu/ogl_device.cpp [new file with mode: 0644]
core/mixer/gpu/ogl_device.h [new file with mode: 0644]
core/mixer/image/image_kernel.cpp [new file with mode: 0644]
core/mixer/image/image_kernel.h [new file with mode: 0644]
core/mixer/image/image_mixer.cpp [new file with mode: 0644]
core/mixer/image/image_mixer.h [new file with mode: 0644]

diff --git a/core/mixer/audio/audio_mixer.cpp b/core/mixer/audio/audio_mixer.cpp
new file mode 100644 (file)
index 0000000..a8b68ad
--- /dev/null
@@ -0,0 +1,72 @@
+#include "../../stdafx.h"\r
+\r
+#include "audio_mixer.h"\r
+\r
+namespace caspar { namespace core {\r
+       \r
+audio_transform& audio_transform::operator*=(const audio_transform &other) \r
+{\r
+       gain *= other.gain;\r
+       return *this;\r
+}\r
+\r
+const audio_transform audio_transform::operator*(const audio_transform &other) const \r
+{\r
+       return audio_transform(*this) *= other;\r
+}\r
+\r
+struct audio_mixer::implementation\r
+{\r
+       std::vector<short> audio_data_;\r
+       std::stack<audio_transform> transform_stack_;\r
+\r
+public:\r
+       implementation()\r
+       {\r
+               transform_stack_.push(audio_transform());\r
+       }\r
+\r
+       void begin(const audio_transform& transform)\r
+       {\r
+               transform_stack_.push(transform_stack_.top()*transform);\r
+       }\r
+\r
+       void process(const std::vector<short>& audio_data)\r
+       {               \r
+               if(audio_data_.empty())\r
+                       audio_data_.resize(audio_data.size(), 0);\r
+\r
+               tbb::parallel_for\r
+               (\r
+                       tbb::blocked_range<size_t>(0, audio_data.size()),\r
+                       [&](const tbb::blocked_range<size_t>& r)\r
+                       {\r
+                               for(size_t n = r.begin(); n < r.end(); ++n)\r
+                               {\r
+                                       int sample = static_cast<int>(audio_data[n]);\r
+                                       sample = (static_cast<int>(transform_stack_.top().gain*8192.0)*sample)/8192;\r
+                                       audio_data_[n] = static_cast<short>((static_cast<int>(audio_data_[n]) + sample) & 0xFFFF);\r
+                               }\r
+                       }\r
+               );\r
+       }\r
+\r
+       void end()\r
+       {\r
+               transform_stack_.pop();\r
+       }\r
+\r
+       std::vector<short> begin_pass()\r
+       {\r
+               return std::move(audio_data_);\r
+       }\r
+};\r
+\r
+audio_mixer::audio_mixer() : impl_(new implementation()){}\r
+void audio_mixer::begin(const audio_transform& transform){impl_->begin(transform);}\r
+void audio_mixer::process(const std::vector<short>& audio_data){impl_->process(audio_data);}\r
+void audio_mixer::end(){impl_->end();}\r
+std::vector<short> audio_mixer::begin_pass(){return impl_->begin_pass();}      \r
+void audio_mixer::end_pass(){}\r
+\r
+}}
\ No newline at end of file
diff --git a/core/mixer/audio/audio_mixer.h b/core/mixer/audio/audio_mixer.h
new file mode 100644 (file)
index 0000000..7801e7b
--- /dev/null
@@ -0,0 +1,31 @@
+#pragma once\r
+\r
+namespace caspar { namespace core {\r
+\r
+struct audio_transform\r
+{\r
+       audio_transform() : gain(1.0){}\r
+       double gain;\r
+\r
+       audio_transform& operator*=(const audio_transform &other);\r
+       const audio_transform operator*(const audio_transform &other) const;\r
+};\r
+\r
+class audio_mixer\r
+{\r
+public:\r
+       audio_mixer();\r
+\r
+       void begin(const audio_transform& transform);\r
+       void process(const std::vector<short>& audio_data);\r
+       void end();\r
+       \r
+       std::vector<short> begin_pass();\r
+       void end_pass();\r
+\r
+private:\r
+       struct implementation;\r
+       std::shared_ptr<implementation> impl_;\r
+};\r
+\r
+}}
\ No newline at end of file
diff --git a/core/mixer/frame/draw_frame.cpp b/core/mixer/frame/draw_frame.cpp
new file mode 100644 (file)
index 0000000..3ec376b
--- /dev/null
@@ -0,0 +1,121 @@
+#include "../../StdAfx.h"\r
+\r
+#include "draw_frame.h"\r
+\r
+#include "../image/image_mixer.h"\r
+#include "../audio/audio_mixer.h"\r
+#include "pixel_format.h"\r
+#include "../../video_format.h"\r
+\r
+#include <boost/range/algorithm.hpp>\r
+\r
+namespace caspar { namespace core {\r
+                                                                                                                                                                                                                                                                                                               \r
+struct draw_frame::implementation\r
+{              \r
+       std::vector<safe_ptr<draw_frame>> frames_;\r
+\r
+       image_transform image_transform_;       \r
+       audio_transform audio_transform_;\r
+\r
+public:\r
+       implementation(const std::vector<safe_ptr<draw_frame>>& frames) \r
+               : frames_(frames){}\r
+       implementation(std::vector<safe_ptr<draw_frame>>&& frames) \r
+               : frames_(std::move(frames)){}\r
+       \r
+       void process_image(image_mixer& mixer)\r
+       {\r
+               mixer.begin(image_transform_);\r
+               BOOST_FOREACH(auto frame, frames_)\r
+                       frame->process_image(mixer);\r
+               mixer.end();\r
+       }\r
+\r
+       void process_audio(audio_mixer& mixer)\r
+       {\r
+               mixer.begin(audio_transform_);\r
+               BOOST_FOREACH(auto frame, frames_)\r
+                       frame->process_audio(mixer);\r
+               mixer.end();\r
+       }       \r
+};\r
+       \r
+draw_frame::draw_frame() : impl_(new implementation(std::vector<safe_ptr<draw_frame>>())){}\r
+draw_frame::draw_frame(const std::vector<safe_ptr<draw_frame>>& frames) : impl_(new implementation(frames)){}\r
+draw_frame::draw_frame(std::vector<safe_ptr<draw_frame>>&& frames) : impl_(new implementation(std::move(frames))){}\r
+draw_frame::draw_frame(const draw_frame& other) : impl_(new implementation(*other.impl_)){}\r
+draw_frame::draw_frame(const safe_ptr<draw_frame>& frame)\r
+{\r
+       std::vector<safe_ptr<draw_frame>> frames;\r
+       frames.push_back(frame);\r
+       impl_.reset(new implementation(std::move(frames)));\r
+}\r
+draw_frame::draw_frame(safe_ptr<draw_frame>&& frame)\r
+{\r
+       std::vector<safe_ptr<draw_frame>> frames;\r
+       frames.push_back(std::move(frame));\r
+       impl_.reset(new implementation(std::move(frames)));\r
+}\r
+draw_frame::draw_frame(const safe_ptr<draw_frame>& frame1, const safe_ptr<draw_frame>& frame2)\r
+{\r
+       std::vector<safe_ptr<draw_frame>> frames;\r
+       frames.push_back(frame1);\r
+       frames.push_back(frame2);\r
+       impl_.reset(new implementation(std::move(frames)));\r
+}\r
+draw_frame::draw_frame(safe_ptr<draw_frame>&& frame1, safe_ptr<draw_frame>&& frame2)\r
+{\r
+       std::vector<safe_ptr<draw_frame>> frames;\r
+       frames.push_back(std::move(frame1));\r
+       frames.push_back(std::move(frame2));\r
+       impl_.reset(new implementation(std::move(frames)));\r
+}\r
+\r
+void draw_frame::swap(draw_frame& other){impl_.swap(other.impl_);}\r
+draw_frame& draw_frame::operator=(const draw_frame& other)\r
+{\r
+       draw_frame temp(other);\r
+       temp.swap(*this);\r
+       return *this;\r
+}\r
+draw_frame::draw_frame(draw_frame&& other) : impl_(std::move(other.impl_)){}\r
+draw_frame& draw_frame::operator=(draw_frame&& other)\r
+{\r
+       draw_frame temp(std::move(other));\r
+       temp.swap(*this);\r
+       return *this;\r
+}\r
+void draw_frame::process_image(image_mixer& mixer){impl_->process_image(mixer);}\r
+void draw_frame::process_audio(audio_mixer& mixer){impl_->process_audio(mixer);}\r
+\r
+const image_transform& draw_frame::get_image_transform() const { return impl_->image_transform_;}\r
+image_transform& draw_frame::get_image_transform() { return impl_->image_transform_;}\r
+const audio_transform& draw_frame::get_audio_transform() const { return impl_->audio_transform_;}\r
+audio_transform& draw_frame::get_audio_transform() { return impl_->audio_transform_;}\r
+\r
+safe_ptr<draw_frame> draw_frame::interlace(const safe_ptr<draw_frame>& frame1, const safe_ptr<draw_frame>& frame2, video_mode::type mode)\r
+{                      \r
+       if(frame1 == frame2 || mode == video_mode::progressive)\r
+               return frame1;\r
+\r
+       auto my_frame1 = make_safe<draw_frame>(frame1);\r
+       auto my_frame2 = make_safe<draw_frame>(frame2);\r
+       if(mode == video_mode::upper)\r
+       {\r
+               my_frame1->get_image_transform().mode = video_mode::upper;      \r
+               my_frame2->get_image_transform().mode = video_mode::lower;      \r
+       }\r
+       else\r
+       {\r
+               my_frame1->get_image_transform().mode = video_mode::lower;      \r
+               my_frame2->get_image_transform().mode = video_mode::upper;      \r
+       }\r
+\r
+       std::vector<safe_ptr<draw_frame>> frames;\r
+       frames.push_back(my_frame1);\r
+       frames.push_back(my_frame2);\r
+       return make_safe<draw_frame>(frames);\r
+}\r
+\r
+}}
\ No newline at end of file
diff --git a/core/mixer/frame/draw_frame.h b/core/mixer/frame/draw_frame.h
new file mode 100644 (file)
index 0000000..994fc67
--- /dev/null
@@ -0,0 +1,71 @@
+#pragma once\r
+\r
+#include "../fwd.h"\r
+\r
+#include "draw_frame.h"\r
+\r
+#include "../../video_format.h"\r
+\r
+#include <boost/noncopyable.hpp>\r
+#include <boost/range/iterator_range.hpp>\r
+\r
+#include <memory>\r
+#include <array>\r
+#include <vector>\r
+\r
+namespace caspar { namespace core {\r
+\r
+struct image_transform;\r
+struct audio_transform;\r
+               \r
+class draw_frame\r
+{\r
+public:\r
+       draw_frame();   \r
+       draw_frame(safe_ptr<write_frame>&& image, std::vector<short>&& audio);\r
+       draw_frame(const safe_ptr<draw_frame>& frame);\r
+       draw_frame(safe_ptr<draw_frame>&& frame);\r
+       draw_frame(const std::vector<safe_ptr<draw_frame>>& frames);\r
+       draw_frame(std::vector<safe_ptr<draw_frame>>&& frame);\r
+       draw_frame(const safe_ptr<draw_frame>& frame1, const safe_ptr<draw_frame>& frame2);\r
+       draw_frame(safe_ptr<draw_frame>&& frame1, safe_ptr<draw_frame>&& frame2);\r
+\r
+       void swap(draw_frame& other);\r
+       \r
+       draw_frame(const draw_frame& other);\r
+       draw_frame(draw_frame&& other);\r
+\r
+       draw_frame& operator=(const draw_frame& other);\r
+       draw_frame& operator=(draw_frame&& other);\r
+       \r
+       const image_transform& get_image_transform() const;\r
+       image_transform& get_image_transform();\r
+\r
+       const audio_transform& get_audio_transform() const;\r
+       audio_transform& get_audio_transform();\r
+               \r
+       static safe_ptr<draw_frame> interlace(const safe_ptr<draw_frame>& frame1, const safe_ptr<draw_frame>& frame2, video_mode::type mode);\r
+               \r
+       static safe_ptr<draw_frame> eof()\r
+       {\r
+               struct eof_frame : public draw_frame{};\r
+               static safe_ptr<draw_frame> frame = make_safe<eof_frame>();\r
+               return frame;\r
+       }\r
+\r
+       static safe_ptr<draw_frame> empty()\r
+       {\r
+               struct empty_frame : public draw_frame{};\r
+               static safe_ptr<draw_frame> frame = make_safe<empty_frame>();\r
+               return frame;\r
+       }\r
+\r
+       virtual void process_image(image_mixer& mixer);\r
+       virtual void process_audio(audio_mixer& mixer);\r
+       \r
+private:\r
+       struct implementation;\r
+       std::shared_ptr<implementation> impl_;\r
+};\r
+\r
+}}
\ No newline at end of file
diff --git a/core/mixer/frame/pixel_format.h b/core/mixer/frame/pixel_format.h
new file mode 100644 (file)
index 0000000..f23d128
--- /dev/null
@@ -0,0 +1,53 @@
+#pragma once\r
+\r
+#include <vector>\r
+\r
+namespace caspar { namespace core {\r
+               \r
+struct pixel_format\r
+{\r
+       enum type\r
+       {\r
+               bgra,\r
+               rgba,\r
+               argb,\r
+               abgr,\r
+               ycbcr,\r
+               ycbcra,\r
+               count,\r
+               invalid\r
+       };\r
+};\r
+\r
+struct pixel_format_desc\r
+{\r
+       struct plane\r
+       {\r
+               size_t linesize;\r
+               size_t width;\r
+               size_t height;\r
+               size_t size;\r
+               size_t channels;\r
+\r
+               plane() \r
+                       : linesize(0)\r
+                       , width(0)\r
+                       , height(0)\r
+                       , size(0)\r
+                       , channels(0){}\r
+\r
+               plane(size_t width, size_t height, size_t channels)\r
+                       : linesize(width*channels)\r
+                       , width(width)\r
+                       , height(height)\r
+                       , size(width*height*channels)\r
+                       , channels(channels){}\r
+       };\r
+\r
+       pixel_format_desc() : pix_fmt(pixel_format::invalid){}\r
+       \r
+       pixel_format::type pix_fmt;\r
+       std::vector<plane> planes;\r
+};\r
+\r
+}}
\ No newline at end of file
diff --git a/core/mixer/frame/read_frame.cpp b/core/mixer/frame/read_frame.cpp
new file mode 100644 (file)
index 0000000..bf1d260
--- /dev/null
@@ -0,0 +1,39 @@
+#include "../../StdAfx.h"\r
+\r
+#include "read_frame.h"\r
+\r
+#include "../gpu/host_buffer.h"\r
+\r
+#include <common/gl/gl_check.h>\r
+\r
+namespace caspar { namespace core {\r
+                                                                                                                                                                                                                                                                                                                       \r
+struct read_frame::implementation : boost::noncopyable\r
+{\r
+       safe_ptr<const host_buffer> image_data_;\r
+       std::vector<short> audio_data_;\r
+\r
+public:\r
+       implementation(safe_ptr<const host_buffer>&& image_data, std::vector<short>&& audio_data) \r
+               : image_data_(std::move(image_data))\r
+               , audio_data_(std::move(audio_data)){}  \r
+};\r
+\r
+read_frame::read_frame(){}\r
+read_frame::read_frame(safe_ptr<const host_buffer>&& image_data, std::vector<short>&& audio_data) : impl_(new implementation(std::move(image_data), std::move(audio_data))){}\r
+\r
+const boost::iterator_range<const unsigned char*> read_frame::image_data() const\r
+{\r
+       if(!impl_ || !impl_->image_data_->data())\r
+               return boost::iterator_range<const unsigned char*>();\r
+       auto ptr = static_cast<const unsigned char*>(impl_->image_data_->data());\r
+       return boost::iterator_range<const unsigned char*>(ptr, ptr + impl_->image_data_->size());\r
+}\r
+const boost::iterator_range<const short*> read_frame::audio_data() const\r
+{\r
+       if(!impl_)\r
+               return boost::iterator_range<const short*>();\r
+       return boost::iterator_range<const short*>(impl_->audio_data_.data(), impl_->audio_data_.data() + impl_->audio_data_.size());\r
+}\r
+\r
+}}
\ No newline at end of file
diff --git a/core/mixer/frame/read_frame.h b/core/mixer/frame/read_frame.h
new file mode 100644 (file)
index 0000000..6e34431
--- /dev/null
@@ -0,0 +1,29 @@
+#pragma once\r
+\r
+#include "../gpu/host_buffer.h"        \r
+\r
+#include <boost/noncopyable.hpp>\r
+#include <boost/range/iterator_range.hpp>\r
+\r
+#include <memory>\r
+#include <vector>\r
+\r
+#include <common/memory/safe_ptr.h>\r
+\r
+namespace caspar { namespace core {\r
+       \r
+class read_frame\r
+{\r
+public:\r
+       read_frame();\r
+       read_frame(safe_ptr<const host_buffer>&& image_data, std::vector<short>&& audio_data);\r
+\r
+       const boost::iterator_range<const unsigned char*> image_data() const;\r
+       const boost::iterator_range<const short*> audio_data() const;\r
+       \r
+private:\r
+       struct implementation;\r
+       std::shared_ptr<implementation> impl_;\r
+};\r
+\r
+}}
\ No newline at end of file
diff --git a/core/mixer/frame/write_frame.cpp b/core/mixer/frame/write_frame.cpp
new file mode 100644 (file)
index 0000000..6ed77b8
--- /dev/null
@@ -0,0 +1,79 @@
+#include "../../StdAfx.h"\r
+\r
+#include "write_frame.h"\r
+\r
+#include "draw_frame.h"\r
+#include "pixel_format.h"\r
+\r
+#include "../image/image_mixer.h"\r
+#include "../audio/audio_mixer.h"\r
+\r
+#include "../gpu/host_buffer.h"\r
+\r
+#include <common/gl/gl_check.h>\r
+\r
+#include <boost/range/algorithm.hpp>\r
+\r
+namespace caspar { namespace core {\r
+                                                                                                                                                                                                                                                                                                                       \r
+struct write_frame::implementation : boost::noncopyable\r
+{                              \r
+       write_frame& self_;\r
+       std::vector<safe_ptr<host_buffer>> buffers_;\r
+       std::vector<short> audio_data_;\r
+       const pixel_format_desc desc_;\r
+\r
+public:\r
+       implementation(write_frame& self, const pixel_format_desc& desc, std::vector<safe_ptr<host_buffer>> buffers) \r
+               : self_(self)\r
+               , desc_(desc)\r
+               , buffers_(buffers){}\r
+       \r
+       void process_image(image_mixer& mixer)\r
+       {\r
+               mixer.begin(self_.get_image_transform());\r
+               mixer.process(desc_, buffers_);\r
+               mixer.end();\r
+       }\r
+\r
+       void process_audio(audio_mixer& mixer)\r
+       {\r
+               mixer.begin(self_.get_audio_transform());\r
+               mixer.process(audio_data_);\r
+               mixer.end();\r
+       }\r
+\r
+       boost::iterator_range<unsigned char*> image_data(size_t index)\r
+       {\r
+               if(index >= buffers_.size() || !buffers_[index]->data())\r
+                       return boost::iterator_range<const unsigned char*>();\r
+               auto ptr = static_cast<unsigned char*>(buffers_[index]->data());\r
+               return boost::iterator_range<unsigned char*>(ptr, ptr+buffers_[index]->size());\r
+       }\r
+       const boost::iterator_range<const unsigned char*> image_data(size_t index) const\r
+       {\r
+               if(index >= buffers_.size() || !buffers_[index]->data())\r
+                       return boost::iterator_range<const unsigned char*>();\r
+               auto ptr = static_cast<const unsigned char*>(buffers_[index]->data());\r
+               return boost::iterator_range<const unsigned char*>(ptr, ptr+buffers_[index]->size());\r
+       }\r
+};\r
+\r
+#ifdef _MSC_VER\r
+#pragma warning(disable : 4355) // 'this' : used in base member initializer list\r
+#endif\r
+       \r
+write_frame::write_frame(const pixel_format_desc& desc, std::vector<safe_ptr<host_buffer>> buffers) : impl_(new implementation(*this, desc, buffers)){}\r
+write_frame::write_frame(write_frame&& other) : impl_(std::move(other.impl_)){}\r
+void write_frame::swap(write_frame& other){impl_.swap(other.impl_);}\r
+write_frame& write_frame::operator=(write_frame&& other)\r
+{\r
+       write_frame temp(std::move(other));\r
+       temp.swap(*this);\r
+       return *this;\r
+}\r
+void write_frame::process_image(image_mixer& mixer){impl_->process_image(mixer);}\r
+void write_frame::process_audio(audio_mixer& mixer){impl_->process_audio(mixer);}\r
+boost::iterator_range<unsigned char*> write_frame::image_data(size_t index){return impl_->image_data(index);}\r
+std::vector<short>& write_frame::audio_data() { return impl_->audio_data_; }\r
+}}
\ No newline at end of file
diff --git a/core/mixer/frame/write_frame.h b/core/mixer/frame/write_frame.h
new file mode 100644 (file)
index 0000000..f6a8605
--- /dev/null
@@ -0,0 +1,43 @@
+#pragma once\r
+\r
+#include "../fwd.h"\r
+\r
+#include "draw_frame.h"\r
+\r
+#include "../gpu/host_buffer.h"\r
+\r
+#include "../../video_format.h"\r
+\r
+#include <boost/noncopyable.hpp>\r
+#include <boost/range/iterator_range.hpp>\r
+\r
+#include <memory>\r
+#include <vector>\r
+\r
+namespace caspar { namespace core {\r
+       \r
+struct pixel_format_desc;\r
+\r
+class write_frame : public draw_frame, boost::noncopyable\r
+{\r
+public:        \r
+       explicit write_frame(const pixel_format_desc& desc, std::vector<safe_ptr<host_buffer>> buffers);\r
+       write_frame(write_frame&& other);\r
+       write_frame& operator=(write_frame&& other);\r
+       \r
+       void swap(write_frame& other);\r
+               \r
+       boost::iterator_range<unsigned char*> image_data(size_t index = 0);     \r
+       std::vector<short>& audio_data();\r
+\r
+       virtual void process_image(image_mixer& mixer);\r
+       virtual void process_audio(audio_mixer& mixer);\r
+       \r
+private:\r
+       struct implementation;\r
+       std::shared_ptr<implementation> impl_;\r
+};\r
+typedef std::shared_ptr<write_frame> write_frame_impl_ptr;\r
+\r
+\r
+}}
\ No newline at end of file
diff --git a/core/mixer/frame_factory.h b/core/mixer/frame_factory.h
new file mode 100644 (file)
index 0000000..c6dccf5
--- /dev/null
@@ -0,0 +1,40 @@
+/*\r
+* copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
+*\r
+*  This file is part of CasparCG.\r
+*\r
+*    CasparCG is free software: you can redistribute it and/or modify\r
+*    it under the terms of the GNU General Public License as published by\r
+*    the Free Software Foundation, either version 3 of the License, or\r
+*    (at your option) any later version.\r
+*\r
+*    CasparCG is distributed in the hope that it will be useful,\r
+*    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+*    GNU General Public License for more details.\r
+\r
+*    You should have received a copy of the GNU General Public License\r
+*    along with CasparCG.  If not, see <http://www.gnu.org/licenses/>.\r
+*\r
+*/\r
+#pragma once\r
+\r
+#include "fwd.h"\r
+\r
+#include "frame/write_frame.h"\r
+#include "frame/pixel_format.h"\r
+\r
+#include <common/memory/safe_ptr.h>\r
+\r
+namespace caspar { namespace core {\r
+       \r
+struct frame_factory : boost::noncopyable\r
+{\r
+       virtual safe_ptr<write_frame> create_frame(const pixel_format_desc& desc) = 0;\r
+       virtual safe_ptr<write_frame> create_frame(size_t width, size_t height, pixel_format::type pix_fmt = pixel_format::bgra) = 0;           \r
+       virtual safe_ptr<write_frame> create_frame(pixel_format::type pix_fmt = pixel_format::bgra) = 0;\r
+       \r
+       virtual const video_format_desc& get_video_format_desc() const = 0; // nothrow\r
+};\r
+\r
+}}
\ No newline at end of file
diff --git a/core/mixer/frame_mixer_device.cpp b/core/mixer/frame_mixer_device.cpp
new file mode 100644 (file)
index 0000000..5ba4ac9
--- /dev/null
@@ -0,0 +1,95 @@
+#include "../StdAfx.h"\r
+\r
+#include "frame_mixer_device.h"\r
+\r
+#include "audio/audio_mixer.h"\r
+#include "image/image_mixer.h"\r
+\r
+#include "frame/write_frame.h"\r
+#include "frame/read_frame.h"\r
+\r
+#include "../video_format.h"\r
+\r
+#include <common/exception/exceptions.h>\r
+#include <common/concurrency/executor.h>\r
+#include <common/gl/gl_check.h>\r
+\r
+#include <tbb/concurrent_queue.h>\r
+#include <tbb/concurrent_unordered_map.h>\r
+\r
+#include <boost/range/algorithm.hpp>\r
+\r
+namespace caspar { namespace core {\r
+       \r
+struct frame_mixer_device::implementation : boost::noncopyable\r
+{              \r
+       const video_format_desc format_desc_;\r
+\r
+       audio_mixer     audio_mixer_;\r
+       image_mixer image_mixer_;\r
+\r
+       output_func output_;\r
+\r
+       executor executor_;\r
+\r
+public:\r
+       implementation(const video_format_desc& format_desc, const output_func& output) \r
+               : format_desc_(format_desc)\r
+               , image_mixer_(format_desc)\r
+               , output_(output)\r
+       {\r
+               executor_.start();\r
+               executor_.set_capacity(2);\r
+       }\r
+               \r
+       ~implementation()\r
+       {\r
+               CASPAR_LOG(info) << "Shutting down mixer-device.";\r
+       }\r
+\r
+       void send(const safe_ptr<draw_frame>& frame)\r
+       {                       \r
+               executor_.begin_invoke([=]\r
+               {\r
+                       auto image = image_mixer_.begin_pass();\r
+                       frame->process_image(image_mixer_);\r
+                       image_mixer_.end_pass();\r
+\r
+                       auto audio = audio_mixer_.begin_pass();\r
+                       frame->process_audio(audio_mixer_);\r
+                       audio_mixer_.end_pass();\r
+\r
+                       output_(make_safe<const read_frame>(std::move(image.get()), std::move(audio)));\r
+               });\r
+       }\r
+               \r
+       safe_ptr<write_frame> create_frame(const pixel_format_desc& desc)\r
+       {\r
+               return make_safe<write_frame>(desc, image_mixer_.create_buffers(desc));\r
+       }\r
+};\r
+       \r
+frame_mixer_device::frame_mixer_device(const video_format_desc& format_desc, const output_func& output) : impl_(new implementation(format_desc, output)){}\r
+frame_mixer_device::frame_mixer_device(frame_mixer_device&& other) : impl_(std::move(other.impl_)){}\r
+void frame_mixer_device::send(const safe_ptr<draw_frame>& frame){impl_->send(frame);}\r
+const video_format_desc& frame_mixer_device::get_video_format_desc() const { return impl_->format_desc_; }\r
+safe_ptr<write_frame> frame_mixer_device::create_frame(const pixel_format_desc& desc){ return impl_->create_frame(desc); }             \r
+safe_ptr<write_frame> frame_mixer_device::create_frame(size_t width, size_t height, pixel_format::type pix_fmt)\r
+{\r
+       // Create bgra frame\r
+       pixel_format_desc desc;\r
+       desc.pix_fmt = pix_fmt;\r
+       desc.planes.push_back(pixel_format_desc::plane(width, height, 4));\r
+       return create_frame(desc);\r
+}\r
+                       \r
+safe_ptr<write_frame> frame_mixer_device::create_frame(pixel_format::type pix_fmt)\r
+{\r
+       // Create bgra frame with output resolution\r
+       pixel_format_desc desc;\r
+       desc.pix_fmt = pix_fmt;\r
+       desc.planes.push_back(pixel_format_desc::plane(get_video_format_desc().width, get_video_format_desc().height, 4));\r
+       return create_frame(desc);\r
+}\r
+\r
+}}
\ No newline at end of file
diff --git a/core/mixer/frame_mixer_device.h b/core/mixer/frame_mixer_device.h
new file mode 100644 (file)
index 0000000..4648792
--- /dev/null
@@ -0,0 +1,57 @@
+/*\r
+* copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
+*\r
+*  This file is part of CasparCG.\r
+*\r
+*    CasparCG is free software: you can redistribute it and/or modify\r
+*    it under the terms of the GNU General Public License as published by\r
+*    the Free Software Foundation, either version 3 of the License, or\r
+*    (at your option) any later version.\r
+*\r
+*    CasparCG is distributed in the hope that it will be useful,\r
+*    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+*    GNU General Public License for more details.\r
+\r
+*    You should have received a copy of the GNU General Public License\r
+*    along with CasparCG.  If not, see <http://www.gnu.org/licenses/>.\r
+*\r
+*/\r
+#pragma once\r
+\r
+#include "fwd.h"\r
+\r
+#include "frame_factory.h"\r
+\r
+#include "frame/write_frame.h"\r
+#include "frame/pixel_format.h"\r
+\r
+#include <common/memory/safe_ptr.h>\r
+\r
+#include <functional>\r
+\r
+namespace caspar { namespace core {\r
+       \r
+struct video_format;\r
+\r
+class frame_mixer_device : public frame_factory\r
+{\r
+public:\r
+       typedef std::function<void(const safe_ptr<const read_frame>&)> output_func;\r
+\r
+       frame_mixer_device(const video_format_desc& format_desc, const output_func& output);\r
+       frame_mixer_device(frame_mixer_device&& other); // nothrow\r
+               \r
+       void send(const safe_ptr<draw_frame>& frame); // nothrow\r
+               \r
+       safe_ptr<write_frame> create_frame(const pixel_format_desc& desc);              \r
+       safe_ptr<write_frame> create_frame(size_t width, size_t height, pixel_format::type pix_fmt = pixel_format::bgra);                       \r
+       safe_ptr<write_frame> create_frame(pixel_format::type pix_fmt = pixel_format::bgra);\r
+       \r
+       const video_format_desc& get_video_format_desc() const; // nothrow\r
+private:\r
+       struct implementation;\r
+       safe_ptr<implementation> impl_;\r
+};\r
+\r
+}}
\ No newline at end of file
diff --git a/core/mixer/fwd.h b/core/mixer/fwd.h
new file mode 100644 (file)
index 0000000..209971c
--- /dev/null
@@ -0,0 +1,18 @@
+#pragma once\r
+\r
+#include <memory>\r
+\r
+namespace caspar { namespace core {\r
+       \r
+class read_frame;\r
+class write_frame;\r
+class draw_frame;\r
+\r
+class image_mixer;\r
+struct frame_factory;\r
+class audio_mixer;\r
+class frame_mixer_device;\r
+\r
+struct pixel_format_desc;\r
+\r
+}}
\ No newline at end of file
diff --git a/core/mixer/gpu/device_buffer.cpp b/core/mixer/gpu/device_buffer.cpp
new file mode 100644 (file)
index 0000000..7755274
--- /dev/null
@@ -0,0 +1,87 @@
+#include "../../StdAfx.h"\r
+\r
+#include "device_buffer.h"\r
+\r
+#include <common/gl/gl_check.h>\r
+\r
+namespace caspar { namespace core {\r
+       \r
+GLenum FORMAT[] = {0, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_BGR, GL_BGRA};\r
+GLenum INTERNAL_FORMAT[] = {0, GL_LUMINANCE8, GL_LUMINANCE8_ALPHA8, GL_RGB8, GL_RGBA8};        \r
+\r
+struct device_buffer::implementation : boost::noncopyable\r
+{\r
+       GLuint id_;\r
+\r
+       const size_t width_;\r
+       const size_t height_;\r
+       const size_t stride_;\r
+\r
+public:\r
+       implementation(size_t width, size_t height, size_t stride) \r
+               : width_(width)\r
+               , height_(height)\r
+               , stride_(stride)\r
+       {       \r
+               GL(glGenTextures(1, &id_));\r
+               GL(glBindTexture(GL_TEXTURE_2D, id_));\r
+               GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));\r
+               GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));\r
+               GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));\r
+               GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));\r
+               GL(glTexImage2D(GL_TEXTURE_2D, 0, INTERNAL_FORMAT[stride_], width_, height_, 0, FORMAT[stride_], GL_UNSIGNED_BYTE, NULL));\r
+               GL(glBindTexture(GL_TEXTURE_2D, 0));    \r
+       }       \r
+\r
+       ~implementation()\r
+       {\r
+               GL(glDeleteTextures(1, &id_));\r
+       }\r
+       \r
+       void bind()\r
+       {\r
+               GL(glBindTexture(GL_TEXTURE_2D, id_));\r
+       }\r
+\r
+       void unbind()\r
+       {\r
+               GL(glBindTexture(GL_TEXTURE_2D, 0));\r
+       }\r
+\r
+       void read(host_buffer& source)\r
+       {\r
+               GL(glBindTexture(GL_TEXTURE_2D, id_));\r
+               source.unmap();\r
+               source.bind();\r
+               GL(glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width_, height_, FORMAT[stride_], GL_UNSIGNED_BYTE, NULL));\r
+               source.unbind();\r
+               GL(glBindTexture(GL_TEXTURE_2D, 0));\r
+       }\r
+       \r
+       void write(host_buffer& target)\r
+       {\r
+               GL(glBindTexture(GL_TEXTURE_2D, id_));\r
+               target.unmap();\r
+               target.bind();\r
+               GL(glReadPixels(0, 0, width_, height_, FORMAT[stride_], GL_UNSIGNED_BYTE, NULL));\r
+               target.unbind();\r
+               GL(glBindTexture(GL_TEXTURE_2D, 0));\r
+       }\r
+\r
+       void attach(int index)\r
+       {\r
+               GL(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + index, GL_TEXTURE_2D, id_, 0));\r
+       }\r
+};\r
+\r
+device_buffer::device_buffer(size_t width, size_t height, size_t stride) : impl_(new implementation(width, height, stride)){}\r
+size_t device_buffer::stride() const { return impl_->stride_; }\r
+size_t device_buffer::width() const { return impl_->width_; }\r
+size_t device_buffer::height() const { return impl_->height_; }\r
+void device_buffer::attach(int index){impl_->attach(index);}\r
+void device_buffer::bind(){impl_->bind();}\r
+void device_buffer::unbind(){impl_->unbind();}\r
+void device_buffer::read(host_buffer& source){impl_->read(source);}\r
+void device_buffer::write(host_buffer& target){impl_->write(target);}\r
+\r
+}}
\ No newline at end of file
diff --git a/core/mixer/gpu/device_buffer.h b/core/mixer/gpu/device_buffer.h
new file mode 100644 (file)
index 0000000..1c47156
--- /dev/null
@@ -0,0 +1,32 @@
+#pragma once\r
+\r
+#include "../gpu/host_buffer.h"\r
+\r
+#include <boost/noncopyable.hpp>\r
+\r
+#include <memory>\r
+\r
+namespace caspar { namespace core {\r
+       \r
+class device_buffer : boost::noncopyable\r
+{\r
+public:\r
+       device_buffer(size_t width, size_t height, size_t stride);\r
+       \r
+       size_t stride() const;  \r
+       size_t width() const;\r
+       size_t height() const;\r
+               \r
+       void bind();\r
+       void unbind();\r
+\r
+       void attach(int index);\r
+       void read(host_buffer& source);\r
+       void write(host_buffer& target);\r
+       \r
+private:\r
+       struct implementation;\r
+       std::shared_ptr<implementation> impl_;\r
+};\r
+\r
+}}
\ No newline at end of file
diff --git a/core/mixer/gpu/host_buffer.cpp b/core/mixer/gpu/host_buffer.cpp
new file mode 100644 (file)
index 0000000..17b5c7e
--- /dev/null
@@ -0,0 +1,86 @@
+#include "../../StdAfx.h"\r
+\r
+#include "../gpu/host_buffer.h"\r
+\r
+#include <common/gl/gl_check.h>\r
+\r
+namespace caspar { namespace core {\r
+                                                                                                                                                                                                                                                                                                                       \r
+struct host_buffer::implementation : boost::noncopyable\r
+{      \r
+       GLuint pbo_;\r
+\r
+       const size_t size_;\r
+\r
+       void* data_;\r
+       GLenum usage_;\r
+       GLenum target_;\r
+\r
+public:\r
+       implementation(size_t size, usage_t usage) \r
+               : size_(size)\r
+               , data_(nullptr)\r
+               , pbo_(0)\r
+               , target_(usage == write_only ? GL_PIXEL_UNPACK_BUFFER : GL_PIXEL_PACK_BUFFER)\r
+               , usage_(usage == write_only ? GL_STREAM_DRAW : GL_STREAM_READ)\r
+       {\r
+               GL(glGenBuffers(1, &pbo_));\r
+               GL(glBindBuffer(target_, pbo_));\r
+               GL(glBufferData(target_, size_, NULL, usage_)); \r
+               GL(glBindBuffer(target_, 0));\r
+\r
+               if(!pbo_)\r
+                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to allocate buffer."));\r
+               CASPAR_LOG(trace) << "[host_buffer] Allocated.";\r
+       }       \r
+\r
+       ~implementation()\r
+       {\r
+               GL(glDeleteBuffers(1, &pbo_));\r
+       }\r
+\r
+       void map()\r
+       {\r
+               if(data_)\r
+                       return;\r
+               \r
+               GL(glBindBuffer(target_, pbo_));\r
+               data_ = glMapBuffer(target_, usage_ == GL_STREAM_DRAW ? GL_WRITE_ONLY : GL_READ_ONLY);  \r
+               GL(glBindBuffer(target_, 0)); \r
+               if(!data_)\r
+                       BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Failed to map target_ OpenGL Pixel Buffer Object."));\r
+       }\r
+\r
+       void unmap()\r
+       {\r
+               if(!data_)\r
+                       return;\r
+               \r
+               GL(glBindBuffer(target_, pbo_));\r
+               GL(glUnmapBuffer(target_));     \r
+               data_ = nullptr;                \r
+               GL(glBindBuffer(target_, 0));\r
+       }\r
+\r
+       void bind()\r
+       {\r
+               GL(glBindBuffer(target_, pbo_));\r
+       }\r
+\r
+       void unbind()\r
+       {\r
+               GL(glBindBuffer(target_, 0));\r
+       }\r
+};\r
+\r
+host_buffer::host_buffer(size_t size, usage_t usage) : impl_(new implementation(size, usage)){}\r
+const void* host_buffer::data() const {return impl_->data_;}\r
+void* host_buffer::data() {return impl_->data_;}\r
+void host_buffer::map(){impl_->map();}\r
+void host_buffer::unmap(){impl_->unmap();}\r
+void host_buffer::bind(){impl_->bind();}\r
+void host_buffer::unbind(){impl_->unbind();}\r
+\r
+size_t host_buffer::size() const { return impl_->size_; }\r
+\r
+}}
\ No newline at end of file
diff --git a/core/mixer/gpu/host_buffer.h b/core/mixer/gpu/host_buffer.h
new file mode 100644 (file)
index 0000000..24b9e11
--- /dev/null
@@ -0,0 +1,32 @@
+#pragma once\r
+\r
+#include <boost/noncopyable.hpp>\r
+\r
+#include <memory>\r
+\r
+namespace caspar { namespace core {\r
+               \r
+class host_buffer : boost::noncopyable\r
+{\r
+public:\r
+       enum usage_t\r
+       {\r
+               write_only,\r
+               read_only\r
+       };\r
+       host_buffer(size_t size, usage_t usage);\r
+       \r
+       const void* data() const;\r
+       void* data();\r
+       size_t size() const;    \r
+       \r
+       void bind();\r
+       void unbind();\r
+       void unmap();\r
+       void map();\r
+private:\r
+       struct implementation;\r
+       std::shared_ptr<implementation> impl_;\r
+};\r
+\r
+}}
\ No newline at end of file
diff --git a/core/mixer/gpu/ogl_device.cpp b/core/mixer/gpu/ogl_device.cpp
new file mode 100644 (file)
index 0000000..bfbdd04
--- /dev/null
@@ -0,0 +1,64 @@
+#include "../../stdafx.h"\r
+\r
+#include "ogl_device.h"\r
+\r
+#include <Glee.h>\r
+#include <SFML/Window.hpp>\r
+\r
+namespace caspar { namespace core {\r
+\r
+ogl_device::ogl_device()\r
+{\r
+       executor_.start();\r
+       invoke([=]\r
+       {\r
+               context_.reset(new sf::Context());\r
+               context_->SetActive(true);\r
+       });\r
+}\r
+                               \r
+safe_ptr<device_buffer> ogl_device::create_device_buffer(size_t width, size_t height, size_t stride)\r
+{\r
+       auto pool = &device_pools_[stride][((width << 16) & 0xFFFF0000) | (height & 0x0000FFFF)];\r
+       std::shared_ptr<device_buffer> buffer;\r
+       if(!pool->try_pop(buffer))              \r
+       {\r
+               executor_.invoke([&]\r
+               {\r
+                       buffer = std::make_shared<device_buffer>(width, height, stride);\r
+               });     \r
+       }\r
+               \r
+       return safe_ptr<device_buffer>(buffer.get(), [=](device_buffer*){pool->push(buffer);});\r
+}\r
+       \r
+safe_ptr<host_buffer> ogl_device::create_host_buffer(size_t size, host_buffer::usage_t usage)\r
+{\r
+       auto pool = &host_pools_[usage][size];\r
+       std::shared_ptr<host_buffer> buffer;\r
+       if(!pool->try_pop(buffer))\r
+       {\r
+               executor_.invoke([&]\r
+               {\r
+                       buffer = std::make_shared<host_buffer>(size, usage);\r
+                       if(usage == host_buffer::write_only)\r
+                               buffer->map();\r
+                       else\r
+                               buffer->unmap();\r
+               });     \r
+       }\r
+\r
+       return safe_ptr<host_buffer>(buffer.get(), [=](host_buffer*)\r
+       {\r
+               executor_.begin_invoke([=]\r
+               {\r
+                       if(usage == host_buffer::write_only)\r
+                               buffer->map();\r
+                       else\r
+                               buffer->unmap();\r
+                       pool->push(buffer);\r
+               });\r
+       });\r
+}\r
+\r
+}}
\ No newline at end of file
diff --git a/core/mixer/gpu/ogl_device.h b/core/mixer/gpu/ogl_device.h
new file mode 100644 (file)
index 0000000..94c1e54
--- /dev/null
@@ -0,0 +1,46 @@
+#pragma once\r
+\r
+#include "host_buffer.h"\r
+#include "device_buffer.h"\r
+\r
+#include <common/concurrency/executor.h>\r
+#include <common/memory/safe_ptr.h>\r
+\r
+#include <tbb/concurrent_unordered_map.h>\r
+#include <tbb/concurrent_queue.h>\r
+\r
+#include <boost/thread/future.hpp>\r
+\r
+#include <array>\r
+\r
+namespace caspar { namespace core {\r
+\r
+class ogl_device\r
+{      \r
+public:        \r
+       ogl_device();\r
+\r
+       template<typename Func>\r
+       auto begin_invoke(Func&& func) -> boost::unique_future<decltype(func())> // noexcept\r
+       {                       \r
+               return executor_.begin_invoke(std::forward<Func>(func));\r
+       }\r
+       \r
+       template<typename Func>\r
+       auto invoke(Func&& func) -> decltype(func())\r
+       {\r
+               return executor_.invoke(std::forward<Func>(func));\r
+       }\r
+               \r
+       safe_ptr<device_buffer> create_device_buffer(size_t width, size_t height, size_t stride);\r
+       safe_ptr<host_buffer> create_host_buffer(size_t size, host_buffer::usage_t usage);\r
+\r
+private:\r
+       executor executor_;\r
+       std::unique_ptr<sf::Context> context_;\r
+       \r
+       std::array<tbb::concurrent_unordered_map<size_t, tbb::concurrent_bounded_queue<std::shared_ptr<device_buffer>>>, 4> device_pools_;\r
+       std::array<tbb::concurrent_unordered_map<size_t, tbb::concurrent_bounded_queue<std::shared_ptr<host_buffer>>>, 2> host_pools_;\r
+};\r
+\r
+}}
\ No newline at end of file
diff --git a/core/mixer/image/image_kernel.cpp b/core/mixer/image/image_kernel.cpp
new file mode 100644 (file)
index 0000000..bf8d493
--- /dev/null
@@ -0,0 +1,250 @@
+#include "../../StdAfx.h"\r
+\r
+#include "image_kernel.h"\r
+\r
+#include "image_mixer.h"\r
+\r
+#include <common/exception/exceptions.h>\r
+#include <common/gl/gl_check.h>\r
+\r
+#include <Glee.h>\r
+\r
+#include <boost/noncopyable.hpp>\r
+\r
+#include <unordered_map>\r
+\r
+namespace caspar { namespace core {\r
+\r
+class shader_program\r
+{\r
+       GLuint program_;\r
+public:\r
+\r
+       shader_program() : program_(0) {}\r
+       shader_program(const std::string& vertex_source_str, const std::string& fragment_source_str) : program_(0)\r
+       {\r
+               GLint success;\r
+       \r
+               const char* vertex_source = vertex_source_str.c_str();\r
+                                               \r
+               auto vertex_shader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);\r
+                                       \r
+               GL(glShaderSourceARB(vertex_shader, 1, &vertex_source, NULL));\r
+               GL(glCompileShaderARB(vertex_shader));\r
+\r
+               GL(glGetObjectParameterivARB(vertex_shader, GL_OBJECT_COMPILE_STATUS_ARB, &success));\r
+               if (success == GL_FALSE)\r
+               {\r
+                       char info[2048];\r
+                       GL(glGetInfoLogARB(vertex_shader, sizeof(info), 0, info));\r
+                       GL(glDeleteObjectARB(vertex_shader));\r
+                       std::stringstream str;\r
+                       str << "Failed to compile vertex shader:" << std::endl << info << std::endl;\r
+                       BOOST_THROW_EXCEPTION(gl::gl_error() << msg_info(str.str()));\r
+               }\r
+                       \r
+               const char* fragment_source = fragment_source_str.c_str();\r
+                                               \r
+               auto fragmemt_shader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);\r
+                                       \r
+               GL(glShaderSourceARB(fragmemt_shader, 1, &fragment_source, NULL));\r
+               GL(glCompileShaderARB(fragmemt_shader));\r
+\r
+               GL(glGetObjectParameterivARB(fragmemt_shader, GL_OBJECT_COMPILE_STATUS_ARB, &success));\r
+               if (success == GL_FALSE)\r
+               {\r
+                       char info[2048];\r
+                       GL(glGetInfoLogARB(fragmemt_shader, sizeof(info), 0, info));\r
+                       GL(glDeleteObjectARB(fragmemt_shader));\r
+                       std::stringstream str;\r
+                       str << "Failed to compile fragment shader:" << std::endl << info << std::endl;\r
+                       BOOST_THROW_EXCEPTION(gl::gl_error() << msg_info(str.str()));\r
+               }\r
+                       \r
+               program_ = glCreateProgramObjectARB();\r
+                       \r
+               GL(glAttachObjectARB(program_, vertex_shader));\r
+               GL(glAttachObjectARB(program_, fragmemt_shader));\r
+\r
+               GL(glLinkProgramARB(program_));\r
+                       \r
+               GL(glDeleteObjectARB(vertex_shader));\r
+               GL(glDeleteObjectARB(fragmemt_shader));\r
+\r
+               GL(glGetObjectParameterivARB(program_, GL_OBJECT_LINK_STATUS_ARB, &success));\r
+               if (success == GL_FALSE)\r
+               {\r
+                       char info[2048];\r
+                       GL(glGetInfoLogARB(program_, sizeof(info), 0, info));\r
+                       GL(glDeleteObjectARB(program_));\r
+                       std::stringstream str;\r
+                       str << "Failed to link shader program:" << std::endl << info << std::endl;\r
+                       BOOST_THROW_EXCEPTION(gl::gl_error() << msg_info(str.str()));\r
+               }\r
+               GL(glUseProgramObjectARB(program_));\r
+               glUniform1i(glGetUniformLocation(program_, "plane[0]"), 0);\r
+               glUniform1i(glGetUniformLocation(program_, "plane[1]"), 1);\r
+               glUniform1i(glGetUniformLocation(program_, "plane[2]"), 2);\r
+               glUniform1i(glGetUniformLocation(program_, "plane[3]"), 3);\r
+       }\r
+\r
+       GLint get_location(const char* name)\r
+       {\r
+               GLint loc = glGetUniformLocation(program_, name);\r
+               return loc;\r
+       }\r
+\r
+       shader_program& operator=(shader_program&& other) \r
+       {\r
+               program_ = other.program_; \r
+               other.program_ = 0; \r
+               return *this;\r
+       }\r
+\r
+       ~shader_program()\r
+       {\r
+               glDeleteProgram(program_);\r
+       }\r
+\r
+       void use()\r
+       {       \r
+               GL(glUseProgramObjectARB(program_));            \r
+       }\r
+};\r
+\r
+GLubyte progressive_pattern[] = {\r
+       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,\r
+       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,\r
+       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,\r
+       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xFF, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};\r
+       \r
+GLubyte upper_pattern[] = {\r
+       0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,\r
+       0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,\r
+       0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,\r
+       0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00};\r
+               \r
+GLubyte lower_pattern[] = {\r
+       0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, \r
+       0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,\r
+       0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,\r
+       0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff};\r
+\r
+struct image_kernel::implementation\r
+{      \r
+       std::unordered_map<pixel_format::type, shader_program> shaders_;\r
+\r
+public:\r
+       std::unordered_map<pixel_format::type, shader_program>& shaders()\r
+       {\r
+               GL(glEnable(GL_POLYGON_STIPPLE));\r
+               GL(glEnable(GL_BLEND));\r
+               GL(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));  \r
+\r
+               if(shaders_.empty())\r
+               {\r
+               std::string common_vertex = \r
+                       "void main()                                                                                                                    "\r
+                       "{                                                                                                                                              "\r
+                       "       gl_TexCoord[0] = gl_MultiTexCoord0;                                                                     "\r
+                       "       gl_FrontColor = gl_Color;                                                                                       "\r
+                       "       gl_Position = ftransform();                                                                                     "\r
+                       "}                                                                                                                                              ";\r
+\r
+               std::string common_fragment = \r
+                       "uniform sampler2D      plane[4];                                                                                       "\r
+                       "uniform float          color_gain;                                                                                     "\r
+                                                                                                                                                               \r
+                       // NOTE: YCbCr, ITU-R, http://www.intersil.com/data/an/an9717.pdf               \r
+                       // TODO: Support for more yuv formats might be needed.                                  \r
+                       "vec4 ycbcra_to_bgra(float y, float cb, float cr, float a)                              "\r
+                       "{                                                                                                                                              "\r
+                       "       cb -= 0.5;                                                                                                                      "\r
+                       "       cr -= 0.5;                                                                                                                      "\r
+                       "       y = 1.164*(y-0.0625);                                                                                           "\r
+                       "                                                                                                                                               "\r
+                       "       vec4 color;                                                                                                                     "\r
+                       "       color.r = y + 1.596 * cr;                                                                                       "\r
+                       "       color.g = y - 0.813 * cr - 0.337633 * cb;                                                       "\r
+                       "       color.b = y + 2.017 * cb;                                                                                       "\r
+                       "       color.a = a;                                                                                                            "\r
+                       "                                                                                                                                               "\r
+                       "       return color;                                                                                                           "\r
+                       "}                                                                                                                                              "                       \r
+                       "                                                                                                                                               ";\r
+                       \r
+               shaders_[pixel_format::abgr] = shader_program(common_vertex, common_fragment +\r
+\r
+                       "void main()                                                                                                                    "\r
+                       "{                                                                                                                                              "\r
+                       "       vec4 abgr = texture2D(plane[0], gl_TexCoord[0].st);                                     "\r
+                       "       gl_FragColor = abgr.argb * color_gain;                                                          "\r
+                       "}                                                                                                                                              ");\r
+               \r
+               shaders_[pixel_format::argb]= shader_program(common_vertex, common_fragment +\r
+\r
+                       "void main()                                                                                                                    "       \r
+                       "{                                                                                                                                              "\r
+                       "       vec4 argb = texture2D(plane[0], gl_TexCoord[0].st);                                     "\r
+                       "       gl_FragColor = argb.grab * gl_Color * color_gain;                                       "\r
+                       "}                                                                                                                                              ");\r
+               \r
+               shaders_[pixel_format::bgra]= shader_program(common_vertex, common_fragment +\r
+\r
+                       "void main()                                                                                                                    "\r
+                       "{                                                                                                                                              "\r
+                       "       vec4 bgra = texture2D(plane[0], gl_TexCoord[0].st);                                     "\r
+                       "       gl_FragColor = bgra.rgba * gl_Color * color_gain;                                       "\r
+                       "}                                                                                                                                              ");\r
+               \r
+               shaders_[pixel_format::rgba] = shader_program(common_vertex, common_fragment +\r
+\r
+                       "void main()                                                                                                                    "\r
+                       "{                                                                                                                                              "\r
+                       "       vec4 rgba = texture2D(plane[0], gl_TexCoord[0].st);                                     "\r
+                       "       gl_FragColor = rgba.bgra * gl_Color * color_gain;                                       "\r
+                       "}                                                                                                                                              ");\r
+               \r
+               shaders_[pixel_format::ycbcr] = shader_program(common_vertex, common_fragment +\r
+\r
+                       "void main()                                                                                                                    "\r
+                       "{                                                                                                                                              "\r
+                       "       float y  = texture2D(plane[0], gl_TexCoord[0].st).r;"\r
+                       "       float cb = texture2D(plane[1], gl_TexCoord[0].st).r;"\r
+                       "       float cr = texture2D(plane[2], gl_TexCoord[0].st).r;"\r
+                       "       float a = 1.0;                                                                                                          "       \r
+                       "       gl_FragColor = ycbcra_to_bgra(y, cb, cr, a) * gl_Color * color_gain;"\r
+                       "}                                                                                                                                              ");\r
+               \r
+               shaders_[pixel_format::ycbcra] = shader_program(common_vertex, common_fragment +\r
+\r
+                       "void main()                                                                                                                    "\r
+                       "{                                                                                                                                              "\r
+                       "       float y  = texture2D(plane[0], gl_TexCoord[0].st).r;"\r
+                       "       float cb = texture2D(plane[1], gl_TexCoord[0].st).r;"\r
+                       "       float cr = texture2D(plane[2], gl_TexCoord[0].st).r;"\r
+                       "       float a  = texture2D(plane[3], gl_TexCoord[0].st).r;"\r
+                       "       gl_FragColor = ycbcra_to_bgra(y, cb, cr, a) * gl_Color * color_gain;"\r
+                       "}                                                                                                                                              ");\r
+               }\r
+               return shaders_;\r
+       }\r
+};\r
+\r
+image_kernel::image_kernel() : impl_(new implementation()){}\r
+\r
+void image_kernel::apply(pixel_format::type pix_fmt, const image_transform& transform)\r
+{\r
+       impl_->shaders()[pix_fmt].use();\r
+\r
+       GL(glUniform1f(impl_->shaders()[pix_fmt].get_location("color_gain"), static_cast<GLfloat>(transform.gain)));\r
+\r
+       if(transform.mode == video_mode::upper)\r
+               glPolygonStipple(upper_pattern);\r
+       else if(transform.mode == video_mode::lower)\r
+               glPolygonStipple(lower_pattern);\r
+       else\r
+               glPolygonStipple(progressive_pattern);\r
+}\r
+\r
+}}
\ No newline at end of file
diff --git a/core/mixer/image/image_kernel.h b/core/mixer/image/image_kernel.h
new file mode 100644 (file)
index 0000000..523319d
--- /dev/null
@@ -0,0 +1,23 @@
+#pragma once\r
+\r
+#include "../../video_format.h"\r
+#include "../frame/pixel_format.h"\r
+\r
+#include <memory>\r
+\r
+namespace caspar { namespace core {\r
+\r
+struct image_transform;\r
+\r
+class image_kernel\r
+{\r
+public:\r
+       image_kernel();\r
+       void apply(pixel_format::type pix_fmt, const image_transform& mode);\r
+\r
+private:\r
+       struct implementation;\r
+       std::shared_ptr<implementation> impl_;\r
+};\r
+\r
+}}
\ No newline at end of file
diff --git a/core/mixer/image/image_mixer.cpp b/core/mixer/image/image_mixer.cpp
new file mode 100644 (file)
index 0000000..c016729
--- /dev/null
@@ -0,0 +1,168 @@
+#include "../../StdAfx.h"\r
+\r
+#include "image_mixer.h"\r
+#include "image_kernel.h"\r
+\r
+#include "../gpu/ogl_device.h"\r
+#include "../gpu/host_buffer.h"\r
+#include "../gpu/device_buffer.h"\r
+\r
+#include <common/exception/exceptions.h>\r
+#include <common/gl/gl_check.h>\r
+#include <common/concurrency/executor.h>\r
+\r
+#include <Glee.h>\r
+#include <SFML/Window/Context.hpp>\r
+#include <unordered_map>\r
+\r
+namespace caspar { namespace core {\r
+               \r
+image_transform& image_transform::operator*=(const image_transform &other)\r
+{\r
+       alpha *= other.alpha;\r
+       mode = other.mode;\r
+       gain *= other.gain;\r
+       uv.get<0>() += other.uv.get<0>();\r
+       uv.get<1>() += other.uv.get<1>();\r
+       uv.get<2>() += other.uv.get<2>();\r
+       uv.get<3>() += other.uv.get<3>();\r
+       pos.get<0>() += other.pos.get<0>();\r
+       pos.get<1>() += other.pos.get<1>();\r
+       return *this;\r
+}\r
+\r
+const image_transform image_transform::operator*(const image_transform &other) const\r
+{\r
+       return image_transform(*this) *= other;\r
+}\r
+\r
+struct image_mixer::implementation : boost::noncopyable\r
+{                      \r
+       const video_format_desc format_desc_;\r
+       \r
+       std::stack<image_transform> transform_stack_;\r
+\r
+       GLuint fbo_;\r
+       std::array<std::shared_ptr<device_buffer>, 2> render_targets_;\r
+\r
+       std::shared_ptr<host_buffer> reading_;\r
+\r
+       image_kernel kernel_;\r
+\r
+       ogl_device context_;\r
+\r
+public:\r
+       implementation(const video_format_desc& format_desc) \r
+               : format_desc_(format_desc)\r
+       {\r
+               context_.begin_invoke([=]\r
+               {\r
+                       transform_stack_.push(image_transform());\r
+                       transform_stack_.top().mode = video_mode::progressive;\r
+                       transform_stack_.top().uv = boost::make_tuple(0.0, 1.0, 1.0, 0.0);\r
+\r
+                       GL(glEnable(GL_TEXTURE_2D));\r
+                       GL(glDisable(GL_DEPTH_TEST));           \r
+\r
+                       render_targets_[0] = context_.create_device_buffer(format_desc.width, format_desc.height, 4);\r
+                       render_targets_[1] = context_.create_device_buffer(format_desc.width, format_desc.height, 4);\r
+                       \r
+                       GL(glGenFramebuffers(1, &fbo_));                \r
+                       GL(glBindFramebuffer(GL_FRAMEBUFFER_EXT, fbo_));\r
+                       GL(glReadBuffer(GL_COLOR_ATTACHMENT0_EXT));\r
+\r
+                       reading_ = context_.create_host_buffer(format_desc_.size, host_buffer::read_only);\r
+               });\r
+       }\r
+\r
+       ~implementation()\r
+       {\r
+               glDeleteFramebuffersEXT(1, &fbo_);\r
+       }\r
+\r
+       void begin(const image_transform& transform)\r
+       {\r
+               transform_stack_.push(transform_stack_.top()*transform);\r
+       }\r
+               \r
+       void render(const pixel_format_desc& desc, std::vector<safe_ptr<host_buffer>>& buffers)\r
+       {\r
+               auto transform = transform_stack_.top();\r
+               context_.begin_invoke([=]\r
+               {\r
+                       GL(glLoadIdentity());\r
+                       GL(glTranslated(transform.pos.get<0>()*2.0, transform.pos.get<1>()*2.0, 0.0));\r
+                       GL(glColor4d(1.0, 1.0, 1.0, transform.alpha));\r
+                       GL(glViewport(0, 0, format_desc_.width, format_desc_.height));\r
+                       kernel_.apply(desc.pix_fmt, transform);\r
+\r
+                       std::vector<safe_ptr<device_buffer>> device_buffers;\r
+                       for(size_t n = 0; n < buffers.size(); ++n)\r
+                       {\r
+                               auto texture = context_.create_device_buffer(desc.planes[n].width, desc.planes[n].height, desc.planes[n].channels);\r
+                               texture->read(*buffers[n]);\r
+                               device_buffers.push_back(texture);\r
+                       }\r
+\r
+                       for(size_t n = 0; n < buffers.size(); ++n)\r
+                       {\r
+                               glActiveTexture(GL_TEXTURE0+n);\r
+                               device_buffers[n]->bind();\r
+                       }\r
+\r
+                       auto t = transform;\r
+                       glBegin(GL_QUADS);\r
+                               glTexCoord2d(t.uv.get<0>(), t.uv.get<3>()); glVertex2d(-1.0, -1.0);\r
+                               glTexCoord2d(t.uv.get<2>(), t.uv.get<3>()); glVertex2d( 1.0, -1.0);\r
+                               glTexCoord2d(t.uv.get<2>(), t.uv.get<1>()); glVertex2d( 1.0,  1.0);\r
+                               glTexCoord2d(t.uv.get<0>(), t.uv.get<1>()); glVertex2d(-1.0,  1.0);\r
+                       glEnd();\r
+               });\r
+       }\r
+\r
+       void end()\r
+       {\r
+               transform_stack_.pop();\r
+       }\r
+\r
+       boost::unique_future<safe_ptr<const host_buffer>> begin_pass()\r
+       {\r
+               return context_.begin_invoke([=]() -> safe_ptr<const host_buffer>\r
+               {\r
+                       reading_->map();\r
+                       render_targets_[0]->attach(0);\r
+                       GL(glClear(GL_COLOR_BUFFER_BIT));\r
+                       return safe_ptr<const host_buffer>(reading_);\r
+               });\r
+       }\r
+\r
+       void end_pass()\r
+       {\r
+               context_.begin_invoke([=]\r
+               {\r
+                       reading_ = context_.create_host_buffer(format_desc_.size, host_buffer::read_only);\r
+                       render_targets_[0]->write(*reading_);\r
+                       std::rotate(render_targets_.begin(), render_targets_.begin() + 1, render_targets_.end());\r
+               });\r
+       }\r
+               \r
+       std::vector<safe_ptr<host_buffer>> create_buffers(const pixel_format_desc& format)\r
+       {\r
+               std::vector<safe_ptr<host_buffer>> buffers;\r
+               std::transform(format.planes.begin(), format.planes.end(), std::back_inserter(buffers), [&](const pixel_format_desc::plane& plane) -> safe_ptr<host_buffer>\r
+               {\r
+                       return context_.create_host_buffer(plane.size, host_buffer::write_only);\r
+               });\r
+               return buffers;\r
+       }\r
+};\r
+\r
+image_mixer::image_mixer(const video_format_desc& format_desc) : impl_(new implementation(format_desc)){}\r
+void image_mixer::begin(const image_transform& transform) {    impl_->begin(transform);}\r
+void image_mixer::process(const pixel_format_desc& desc, std::vector<safe_ptr<host_buffer>>& buffers){ impl_->render(desc, buffers);}\r
+void image_mixer::end(){impl_->end();}\r
+boost::unique_future<safe_ptr<const host_buffer>> image_mixer::begin_pass(){   return impl_->begin_pass();}\r
+void image_mixer::end_pass(){impl_->end_pass();}\r
+std::vector<safe_ptr<host_buffer>> image_mixer::create_buffers(const pixel_format_desc& format){return impl_->create_buffers(format);}\r
+\r
+}}
\ No newline at end of file
diff --git a/core/mixer/image/image_mixer.h b/core/mixer/image/image_mixer.h
new file mode 100644 (file)
index 0000000..bdd2f5c
--- /dev/null
@@ -0,0 +1,50 @@
+#pragma once\r
+\r
+#include "../gpu/host_buffer.h"\r
+\r
+#include "../../video_format.h"\r
+\r
+#include <boost/tuple/tuple.hpp>\r
+#include <boost/thread/future.hpp>\r
+#include <boost/noncopyable.hpp>\r
+\r
+#include <memory>\r
+#include <array>\r
+\r
+namespace caspar { namespace core {\r
+\r
+struct pixel_format_desc;\r
+       \r
+struct image_transform \r
+{\r
+       image_transform() : alpha(1.0), pos(boost::make_tuple(0.0, 0.0)), gain(1.0), uv(boost::make_tuple(0.0, 0.0, 0.0, 0.0)), mode(video_mode::invalid){}\r
+       double alpha;\r
+       double gain;\r
+       boost::tuple<double, double> pos;\r
+       boost::tuple<double, double, double, double> uv;\r
+       video_mode::type mode; \r
+\r
+       image_transform& operator*=(const image_transform &other);\r
+       const image_transform operator*(const image_transform &other) const;\r
+};\r
+\r
+class image_mixer : boost::noncopyable\r
+{\r
+public:\r
+       image_mixer(const video_format_desc& format_desc);\r
+\r
+       void begin(const image_transform& transform);\r
+       void process(const pixel_format_desc& desc, std::vector<safe_ptr<host_buffer>>& buffers);\r
+       void end();\r
+\r
+       boost::unique_future<safe_ptr<const host_buffer>> begin_pass();\r
+       void end_pass();\r
+\r
+       std::vector<safe_ptr<host_buffer>> create_buffers(const pixel_format_desc& format);\r
+\r
+private:\r
+       struct implementation;\r
+       std::shared_ptr<implementation> impl_;\r
+};\r
+\r
+}}
\ No newline at end of file