2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG.
\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
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
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
21 #include "ogl_consumer.h"
\r
23 #include <GL/glew.h>
\r
24 #include <SFML/Window.hpp>
\r
26 #include <common/diagnostics/graph.h>
\r
27 #include <common/gl/gl_check.h>
\r
28 #include <common/log/log.h>
\r
29 #include <common/memory/safe_ptr.h>
\r
30 #include <common/memory/memcpy.h>
\r
31 #include <common/memory/memshfl.h>
\r
32 #include <common/utility/timer.h>
\r
33 #include <common/utility/string.h>
\r
35 #include <ffmpeg/producer/filter/filter.h>
\r
37 #include <core/video_format.h>
\r
38 #include <core/mixer/read_frame.h>
\r
39 #include <core/consumer/frame_consumer.h>
\r
41 #include <boost/timer.hpp>
\r
42 #include <boost/circular_buffer.hpp>
\r
43 #include <boost/foreach.hpp>
\r
44 #include <boost/thread.hpp>
\r
46 #include <tbb/atomic.h>
\r
47 #include <tbb/concurrent_queue.h>
\r
49 #include <boost/assign.hpp>
\r
51 #include <algorithm>
\r
54 #if defined(_MSC_VER)
\r
55 #pragma warning (push)
\r
56 #pragma warning (disable : 4244)
\r
60 #define __STDC_CONSTANT_MACROS
\r
61 #define __STDC_LIMIT_MACROS
\r
62 #include <libavcodec/avcodec.h>
\r
63 #include <libavutil/imgutils.h>
\r
65 #if defined(_MSC_VER)
\r
66 #pragma warning (pop)
\r
69 namespace caspar { namespace ogl {
\r
79 struct configuration
\r
82 size_t screen_index;
\r
85 bool auto_deinterlace;
\r
93 , auto_deinterlace(true)
\r
99 struct ogl_consumer : boost::noncopyable
\r
101 const configuration config_;
\r
102 core::video_format_desc format_desc_;
\r
103 int channel_index_;
\r
107 std::vector<GLuint> pbos_;
\r
111 unsigned int screen_x_;
\r
112 unsigned int screen_y_;
\r
113 unsigned int screen_width_;
\r
114 unsigned int screen_height_;
\r
115 size_t square_width_;
\r
116 size_t square_height_;
\r
118 sf::Window window_;
\r
120 safe_ptr<diagnostics::graph> graph_;
\r
121 boost::timer perf_timer_;
\r
122 boost::timer tick_timer_;
\r
124 tbb::concurrent_bounded_queue<safe_ptr<core::read_frame>> frame_buffer_;
\r
126 boost::thread thread_;
\r
127 tbb::atomic<bool> is_running_;
\r
130 ffmpeg::filter filter_;
\r
132 ogl_consumer(const configuration& config, const core::video_format_desc& format_desc, int channel_index, int sub_index)
\r
134 , format_desc_(format_desc)
\r
135 , channel_index_(channel_index)
\r
136 , sub_index_(sub_index)
\r
139 , screen_width_(format_desc.width)
\r
140 , screen_height_(format_desc.height)
\r
141 , square_width_(format_desc.square_width)
\r
142 , square_height_(format_desc.square_height)
\r
143 , filter_(format_desc.field_mode == core::field_mode::progressive || !config.auto_deinterlace ? L"" : L"YADIF=0:-1", boost::assign::list_of(PIX_FMT_BGRA))
\r
145 frame_buffer_.set_capacity(2);
\r
147 graph_->add_guide("tick-time", 0.5);
\r
148 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));
\r
149 graph_->set_color("frame-time", diagnostics::color(0.1f, 1.0f, 0.1f));
\r
150 graph_->set_color("dropped-frame", diagnostics::color(0.3f, 0.6f, 0.3f));
\r
151 graph_->set_text(print());
\r
152 diagnostics::register_graph(graph_);
\r
154 DISPLAY_DEVICE d_device = {sizeof(d_device), 0};
\r
155 std::vector<DISPLAY_DEVICE> displayDevices;
\r
156 for(int n = 0; EnumDisplayDevices(NULL, n, &d_device, NULL); ++n)
\r
157 displayDevices.push_back(d_device);
\r
159 if(config_.screen_index >= displayDevices.size())
\r
160 BOOST_THROW_EXCEPTION(out_of_range() << arg_name_info("screen_index_") << msg_info(narrow(print())));
\r
162 DEVMODE devmode = {};
\r
163 if(!EnumDisplaySettings(displayDevices[config_.screen_index].DeviceName, ENUM_CURRENT_SETTINGS, &devmode))
\r
164 BOOST_THROW_EXCEPTION(invalid_operation() << arg_name_info("screen_index") << msg_info(narrow(print()) + " EnumDisplaySettings"));
\r
166 screen_x_ = devmode.dmPosition.x;
\r
167 screen_y_ = devmode.dmPosition.y;
\r
168 screen_width_ = config_.windowed ? square_width_ : devmode.dmPelsWidth;
\r
169 screen_height_ = config_.windowed ? square_height_ : devmode.dmPelsHeight;
\r
171 is_running_ = true;
\r
172 thread_ = boost::thread([this]{run();});
\r
177 is_running_ = false;
\r
178 frame_buffer_.try_push(make_safe<core::read_frame>());
\r
184 if(!GLEW_VERSION_2_1)
\r
185 BOOST_THROW_EXCEPTION(not_supported() << msg_info("Missing OpenGL 2.1 support."));
\r
187 window_.Create(sf::VideoMode(screen_width_, screen_height_, 32), narrow(print()), config_.windowed ? sf::Style::Resize | sf::Style::Close : sf::Style::Fullscreen);
\r
188 window_.ShowMouseCursor(false);
\r
189 window_.SetPosition(screen_x_, screen_y_);
\r
190 window_.SetSize(screen_width_, screen_height_);
\r
191 window_.SetActive();
\r
192 GL(glEnable(GL_TEXTURE_2D));
\r
193 GL(glDisable(GL_DEPTH_TEST));
\r
194 GL(glClearColor(0.0, 0.0, 0.0, 0.0));
\r
195 GL(glViewport(0, 0, format_desc_.width, format_desc_.height));
\r
196 GL(glLoadIdentity());
\r
198 calculate_aspect();
\r
200 GL(glGenTextures(1, &texture_));
\r
201 GL(glBindTexture(GL_TEXTURE_2D, texture_));
\r
202 GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
\r
203 GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
\r
204 GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP));
\r
205 GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP));
\r
206 GL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, format_desc_.width, format_desc_.height, 0, GL_BGRA, GL_UNSIGNED_BYTE, 0));
\r
207 GL(glBindTexture(GL_TEXTURE_2D, 0));
\r
209 GL(glGenBuffers(2, pbos_.data()));
\r
211 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pbos_[0]);
\r
212 glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, format_desc_.size, 0, GL_STREAM_DRAW_ARB);
\r
213 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pbos_[1]);
\r
214 glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, format_desc_.size, 0, GL_STREAM_DRAW_ARB);
\r
215 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
\r
217 CASPAR_LOG(info) << print() << " Successfully Initialized.";
\r
223 glDeleteTextures(1, &texture_);
\r
225 BOOST_FOREACH(auto& pbo, pbos_)
\r
228 glDeleteBuffers(1, &pbo);
\r
244 while(window_.GetEvent(e))
\r
246 if(e.Type == sf::Event::Resized)
\r
247 calculate_aspect();
\r
248 else if(e.Type == sf::Event::Closed)
\r
249 is_running_ = false;
\r
252 safe_ptr<core::read_frame> frame;
\r
253 frame_buffer_.pop(frame);
\r
255 perf_timer_.restart();
\r
257 graph_->update_value("frame-time", perf_timer_.elapsed()*format_desc_.fps*0.5);
\r
261 graph_->update_value("tick-time", tick_timer_.elapsed()*format_desc_.fps*0.5);
\r
262 tick_timer_.restart();
\r
266 CASPAR_LOG_CURRENT_EXCEPTION();
\r
267 is_running_ = false;
\r
275 CASPAR_LOG_CURRENT_EXCEPTION();
\r
279 safe_ptr<AVFrame> get_av_frame()
\r
281 safe_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);
\r
282 avcodec_get_frame_defaults(av_frame.get());
\r
284 av_frame->linesize[0] = format_desc_.width*4;
\r
285 av_frame->format = PIX_FMT_BGRA;
\r
286 av_frame->width = format_desc_.width;
\r
287 av_frame->height = format_desc_.height;
\r
288 av_frame->interlaced_frame = format_desc_.field_mode != core::field_mode::progressive;
\r
289 av_frame->top_field_first = format_desc_.field_mode == core::field_mode::upper ? 1 : 0;
\r
294 void render(const safe_ptr<core::read_frame>& frame)
\r
296 if(static_cast<size_t>(frame->image_data().size()) != format_desc_.size)
\r
299 auto av_frame = get_av_frame();
\r
300 av_frame->data[0] = const_cast<uint8_t*>(frame->image_data().begin());
\r
302 filter_.push(av_frame);
\r
303 auto frames = filter_.poll_all();
\r
308 av_frame = frames[0];
\r
310 if(av_frame->linesize[0] != static_cast<int>(format_desc_.width*4))
\r
312 const uint8_t *src_data[4] = {0};
\r
313 memcpy(const_cast<uint8_t**>(&src_data[0]), av_frame->data, 4);
\r
314 const int src_linesizes[4] = {0};
\r
315 memcpy(const_cast<int*>(&src_linesizes[0]), av_frame->linesize, 4);
\r
317 auto av_frame2 = get_av_frame();
\r
318 av_image_alloc(av_frame2->data, av_frame2->linesize, av_frame2->width, av_frame2->height, PIX_FMT_BGRA, 16);
\r
319 av_frame = safe_ptr<AVFrame>(av_frame2.get(), [=](AVFrame*)
\r
321 av_freep(&av_frame2->data[0]);
\r
324 av_image_copy(av_frame2->data, av_frame2->linesize, src_data, src_linesizes, PIX_FMT_BGRA, av_frame2->width, av_frame2->height);
\r
327 glBindTexture(GL_TEXTURE_2D, texture_);
\r
329 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbos_[0]);
\r
330 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, format_desc_.width, format_desc_.height, GL_BGRA, GL_UNSIGNED_BYTE, 0);
\r
332 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbos_[1]);
\r
333 glBufferData(GL_PIXEL_UNPACK_BUFFER, format_desc_.size, 0, GL_STREAM_DRAW);
\r
335 auto ptr = glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
\r
338 if(config_.key_only)
\r
339 fast_memshfl(reinterpret_cast<char*>(ptr), av_frame->data[0], frame->image_data().size(), 0x0F0F0F0F, 0x0B0B0B0B, 0x07070707, 0x03030303);
\r
341 fast_memcpy(reinterpret_cast<char*>(ptr), av_frame->data[0], frame->image_data().size());
\r
343 glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER); // release the mapped buffer
\r
346 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
\r
348 GL(glClear(GL_COLOR_BUFFER_BIT));
\r
350 glTexCoord2f(0.0f, 1.0f); glVertex2f(-width_, -height_);
\r
351 glTexCoord2f(1.0f, 1.0f); glVertex2f( width_, -height_);
\r
352 glTexCoord2f(1.0f, 0.0f); glVertex2f( width_, height_);
\r
353 glTexCoord2f(0.0f, 0.0f); glVertex2f(-width_, height_);
\r
356 glBindTexture(GL_TEXTURE_2D, 0);
\r
358 std::rotate(pbos_.begin(), pbos_.begin() + 1, pbos_.end());
\r
361 bool send(const safe_ptr<core::read_frame>& frame)
\r
363 if(!frame_buffer_.try_push(frame))
\r
364 graph_->add_tag("dropped-frame");
\r
365 return is_running_;
\r
368 std::wstring print() const
\r
370 return config_.name + L"[" + boost::lexical_cast<std::wstring>(channel_index_) + L"-" + boost::lexical_cast<std::wstring>(sub_index_) + L"|" + format_desc_.name + L"]";
\r
373 void calculate_aspect()
\r
375 if(config_.windowed)
\r
377 screen_height_ = window_.GetHeight();
\r
378 screen_width_ = window_.GetWidth();
\r
381 GL(glViewport(0, 0, screen_width_, screen_height_));
\r
383 std::pair<float, float> target_ratio = None();
\r
384 if(config_.stretch == fill)
\r
385 target_ratio = Fill();
\r
386 else if(config_.stretch == uniform)
\r
387 target_ratio = Uniform();
\r
388 else if(config_.stretch == uniform_to_fill)
\r
389 target_ratio = UniformToFill();
\r
391 width_ = target_ratio.first;
\r
392 height_ = target_ratio.second;
\r
395 std::pair<float, float> None()
\r
397 float width = static_cast<float>(square_width_)/static_cast<float>(screen_width_);
\r
398 float height = static_cast<float>(square_height_)/static_cast<float>(screen_height_);
\r
400 return std::make_pair(width, height);
\r
403 std::pair<float, float> Uniform()
\r
405 float aspect = static_cast<float>(square_width_)/static_cast<float>(square_height_);
\r
406 float width = std::min(1.0f, static_cast<float>(screen_height_)*aspect/static_cast<float>(screen_width_));
\r
407 float height = static_cast<float>(screen_width_*width)/static_cast<float>(screen_height_*aspect);
\r
409 return std::make_pair(width, height);
\r
412 std::pair<float, float> Fill()
\r
414 return std::make_pair(1.0f, 1.0f);
\r
417 std::pair<float, float> UniformToFill()
\r
419 float wr = static_cast<float>(square_width_)/static_cast<float>(screen_width_);
\r
420 float hr = static_cast<float>(square_height_)/static_cast<float>(screen_height_);
\r
421 float r_inv = 1.0f/std::min(wr, hr);
\r
423 float width = wr*r_inv;
\r
424 float height = hr*r_inv;
\r
426 return std::make_pair(width, height);
\r
431 struct ogl_consumer_proxy : public core::frame_consumer
\r
433 const configuration config_;
\r
434 std::unique_ptr<ogl_consumer> consumer_;
\r
438 ogl_consumer_proxy(const configuration& config)
\r
439 : config_(config){}
\r
441 ~ogl_consumer_proxy()
\r
445 auto str = print();
\r
447 CASPAR_LOG(info) << str << L" Successfully Uninitialized.";
\r
453 virtual void initialize(const core::video_format_desc& format_desc, int channel_index, int sub_index) override
\r
456 consumer_.reset(new ogl_consumer(config_, format_desc, channel_index, sub_index));
\r
457 CASPAR_LOG(info) << print() << L" Successfully Initialized.";
\r
460 virtual bool send(const safe_ptr<core::read_frame>& frame) override
\r
462 return consumer_->send(frame);
\r
465 virtual std::wstring print() const override
\r
467 return consumer_ ? consumer_->print() : L"[ogl_consumer]";
\r
470 virtual bool has_synchronization_clock() const override
\r
475 virtual size_t buffer_depth() const override
\r
481 safe_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>& params)
\r
483 if(params.size() < 1 || params[0] != L"SCREEN")
\r
484 return core::frame_consumer::empty();
\r
486 configuration config;
\r
488 if(params.size() > 1)
\r
489 config.screen_index = lexical_cast_or_default<int>(params[2], config.screen_index);
\r
491 if(params.size() > 2)
\r
492 config.windowed = lexical_cast_or_default<bool>(params[3], config.windowed);
\r
494 config.key_only = std::find(params.begin(), params.end(), L"KEY_ONLY") != params.end();
\r
496 return make_safe<ogl_consumer_proxy>(config);
\r
499 safe_ptr<core::frame_consumer> create_consumer(const boost::property_tree::ptree& ptree)
\r
501 configuration config;
\r
502 config.name = widen(ptree.get("name", narrow(config.name)));
\r
503 config.screen_index = ptree.get("device", config.screen_index+1)-1;
\r
504 config.windowed = ptree.get("windowed", config.windowed);
\r
505 config.key_only = ptree.get("key-only", config.key_only);
\r
506 config.auto_deinterlace = ptree.get("auto-deinterlace", config.auto_deinterlace);
\r
508 auto stretch_str = ptree.get("stretch", "default");
\r
509 if(stretch_str == "uniform")
\r
510 config.stretch = stretch::uniform;
\r
511 else if(stretch_str == "uniform_to_fill")
\r
512 config.stretch = stretch::uniform_to_fill;
\r
514 return make_safe<ogl_consumer_proxy>(config);
\r