2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG (www.casparcg.com).
\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
19 * Author: Robert Nagy, ronag89@gmail.com
\r
22 #include "screen_consumer.h"
\r
24 #include <GL/glew.h>
\r
25 #include <SFML/Window.hpp>
\r
27 #include <common/diagnostics/graph.h>
\r
28 #include <common/gl/gl_check.h>
\r
29 #include <common/log.h>
\r
30 #include <common/spl/memory.h>
\r
31 #include <common/memory/memshfl.h>
\r
32 #include <common/utf.h>
\r
34 #include <ffmpeg/producer/filter/filter.h>
\r
36 #include <core/video_format.h>
\r
37 #include <core/frame/data_frame.h>
\r
38 #include <core/consumer/frame_consumer.h>
\r
40 #include <boost/timer.hpp>
\r
41 #include <boost/circular_buffer.hpp>
\r
42 #include <boost/foreach.hpp>
\r
43 #include <boost/thread.hpp>
\r
44 #include <boost/property_tree/ptree.hpp>
\r
46 #include <tbb/atomic.h>
\r
47 #include <tbb/concurrent_queue.h>
\r
48 #include <tbb/parallel_for.h>
\r
50 #include <boost/assign.hpp>
\r
54 #include <algorithm>
\r
57 #if defined(_MSC_VER)
\r
58 #pragma warning (push)
\r
59 #pragma warning (disable : 4244)
\r
63 #define __STDC_CONSTANT_MACROS
\r
64 #define __STDC_LIMIT_MACROS
\r
65 #include <libavcodec/avcodec.h>
\r
66 #include <libavutil/imgutils.h>
\r
68 #if defined(_MSC_VER)
\r
69 #pragma warning (pop)
\r
72 namespace caspar { namespace screen {
\r
82 struct configuration
\r
95 bool auto_deinterlace;
\r
97 aspect_ratio aspect;
\r
105 , auto_deinterlace(true)
\r
107 , aspect(aspect_invalid)
\r
113 struct screen_consumer : boost::noncopyable
\r
115 const configuration config_;
\r
116 core::video_format_desc format_desc_;
\r
117 int channel_index_;
\r
120 std::vector<GLuint> pbos_;
\r
124 unsigned int screen_x_;
\r
125 unsigned int screen_y_;
\r
126 unsigned int screen_width_;
\r
127 unsigned int screen_height_;
\r
129 int square_height_;
\r
131 sf::Window window_;
\r
133 spl::shared_ptr<diagnostics::graph> graph_;
\r
134 boost::timer perf_timer_;
\r
135 boost::timer tick_timer_;
\r
137 tbb::concurrent_bounded_queue<std::shared_ptr<const core::data_frame>> frame_buffer_;
\r
139 boost::thread thread_;
\r
140 tbb::atomic<bool> is_running_;
\r
142 ffmpeg::filter filter_;
\r
144 screen_consumer(const configuration& config, const core::video_format_desc& format_desc, int channel_index)
\r
146 , format_desc_(format_desc)
\r
147 , channel_index_(channel_index)
\r
150 , screen_width_(format_desc.width)
\r
151 , screen_height_(format_desc.height)
\r
152 , square_width_(format_desc.square_width)
\r
153 , square_height_(format_desc.square_height)
\r
154 , 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
156 if(format_desc_.format == core::video_format::ntsc && config_.aspect == configuration::aspect_4_3)
\r
158 // Use default values which are 4:3.
\r
162 if(config_.aspect == configuration::aspect_16_9)
\r
163 square_width_ = (format_desc.height*16)/9;
\r
164 else if(config_.aspect == configuration::aspect_4_3)
\r
165 square_width_ = (format_desc.height*4)/3;
\r
168 frame_buffer_.set_capacity(2);
\r
170 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));
\r
171 graph_->set_color("frame-time", diagnostics::color(0.1f, 1.0f, 0.1f));
\r
172 graph_->set_color("dropped-frame", diagnostics::color(0.3f, 0.6f, 0.3f));
\r
173 graph_->set_text(print());
\r
174 diagnostics::register_graph(graph_);
\r
176 DISPLAY_DEVICE d_device = {sizeof(d_device), 0};
\r
177 std::vector<DISPLAY_DEVICE> displayDevices;
\r
178 for(int n = 0; EnumDisplayDevices(NULL, n, &d_device, NULL); ++n)
\r
179 displayDevices.push_back(d_device);
\r
181 if(config_.screen_index >= displayDevices.size())
\r
182 CASPAR_LOG(warning) << print() << L" Invalid screen-index: " << config_.screen_index;
\r
184 DEVMODE devmode = {};
\r
185 if(!EnumDisplaySettings(displayDevices[config_.screen_index].DeviceName, ENUM_CURRENT_SETTINGS, &devmode))
\r
186 CASPAR_LOG(warning) << print() << L" Could not find display settings for screen-index: " << config_.screen_index;
\r
188 screen_x_ = devmode.dmPosition.x;
\r
189 screen_y_ = devmode.dmPosition.y;
\r
190 screen_width_ = config_.windowed ? square_width_ : devmode.dmPelsWidth;
\r
191 screen_height_ = config_.windowed ? square_height_ : devmode.dmPelsHeight;
\r
193 is_running_ = true;
\r
194 thread_ = boost::thread([this]{run();});
\r
199 is_running_ = false;
\r
200 frame_buffer_.try_push(core::data_frame::empty());
\r
206 if(!GLEW_VERSION_2_1)
\r
207 BOOST_THROW_EXCEPTION(not_supported() << msg_info("Missing OpenGL 2.1 support."));
\r
209 window_.Create(sf::VideoMode(screen_width_, screen_height_, 32), u8(print()), config_.windowed ? sf::Style::Resize | sf::Style::Close : sf::Style::Fullscreen);
\r
210 window_.ShowMouseCursor(false);
\r
211 window_.SetPosition(screen_x_, screen_y_);
\r
212 window_.SetSize(screen_width_, screen_height_);
\r
213 window_.SetActive();
\r
215 GL(glEnable(GL_TEXTURE_2D));
\r
216 GL(glDisable(GL_DEPTH_TEST));
\r
217 GL(glClearColor(0.0, 0.0, 0.0, 0.0));
\r
218 GL(glViewport(0, 0, format_desc_.width, format_desc_.height));
\r
219 GL(glLoadIdentity());
\r
221 calculate_aspect();
\r
223 GL(glGenTextures(1, &texture_));
\r
224 GL(glBindTexture(GL_TEXTURE_2D, texture_));
\r
225 GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
\r
226 GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
\r
227 GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP));
\r
228 GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP));
\r
229 GL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, format_desc_.width, format_desc_.height, 0, GL_BGRA, GL_UNSIGNED_BYTE, 0));
\r
230 GL(glBindTexture(GL_TEXTURE_2D, 0));
\r
232 GL(glGenBuffers(2, pbos_.data()));
\r
234 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pbos_[0]);
\r
235 glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, format_desc_.size, 0, GL_STREAM_DRAW_ARB);
\r
236 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pbos_[1]);
\r
237 glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, format_desc_.size, 0, GL_STREAM_DRAW_ARB);
\r
238 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
\r
240 auto wglSwapIntervalEXT = reinterpret_cast<void(APIENTRY*)(int)>(wglGetProcAddress("wglSwapIntervalEXT"));
\r
241 if(wglSwapIntervalEXT)
\r
245 wglSwapIntervalEXT(1);
\r
246 CASPAR_LOG(info) << print() << " Enabled vsync.";
\r
249 wglSwapIntervalEXT(0);
\r
252 CASPAR_LOG(info) << print() << " Successfully Initialized.";
\r
258 glDeleteTextures(1, &texture_);
\r
260 BOOST_FOREACH(auto& pbo, pbos_)
\r
263 glDeleteBuffers(1, &pbo);
\r
278 while(window_.GetEvent(e))
\r
280 if(e.Type == sf::Event::Resized)
\r
281 calculate_aspect();
\r
282 else if(e.Type == sf::Event::Closed)
\r
283 is_running_ = false;
\r
286 std::shared_ptr<const core::data_frame> frame;
\r
287 frame_buffer_.pop(frame);
\r
289 perf_timer_.restart();
\r
290 render(spl::make_shared_ptr(frame));
\r
291 graph_->set_value("frame-time", perf_timer_.elapsed()*format_desc_.fps*0.5);
\r
295 graph_->set_value("tick-time", tick_timer_.elapsed()*format_desc_.fps*0.5);
\r
296 tick_timer_.restart();
\r
300 CASPAR_LOG_CURRENT_EXCEPTION();
\r
301 is_running_ = false;
\r
309 CASPAR_LOG_CURRENT_EXCEPTION();
\r
313 spl::shared_ptr<AVFrame> get_av_frame()
\r
315 spl::shared_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);
\r
316 avcodec_get_frame_defaults(av_frame.get());
\r
318 av_frame->linesize[0] = format_desc_.width*4;
\r
319 av_frame->format = PIX_FMT_BGRA;
\r
320 av_frame->width = format_desc_.width;
\r
321 av_frame->height = format_desc_.height;
\r
322 av_frame->interlaced_frame = format_desc_.field_mode != core::field_mode::progressive;
\r
323 av_frame->top_field_first = format_desc_.field_mode == core::field_mode::upper ? 1 : 0;
\r
328 void render(const spl::shared_ptr<const core::data_frame>& frame)
\r
330 if(static_cast<int>(frame->image_data().size()) != format_desc_.size)
\r
333 auto av_frame = get_av_frame();
\r
334 av_frame->data[0] = const_cast<uint8_t*>(frame->image_data().begin());
\r
336 filter_.push(av_frame);
\r
337 auto frames = filter_.poll_all();
\r
342 av_frame = frames[0];
\r
344 GL(glBindTexture(GL_TEXTURE_2D, texture_));
\r
346 GL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbos_[0]));
\r
347 GL(glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, format_desc_.width, format_desc_.height, GL_BGRA, GL_UNSIGNED_BYTE, 0));
\r
349 GL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbos_[1]));
\r
350 GL(glBufferData(GL_PIXEL_UNPACK_BUFFER, format_desc_.size, 0, GL_STREAM_DRAW));
\r
352 auto ptr = reinterpret_cast<char*>(GL2(glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY)));
\r
355 if(config_.key_only)
\r
357 tbb::parallel_for(tbb::blocked_range<int>(0, format_desc_.height), [&](const tbb::blocked_range<int>& r)
\r
359 for(int n = r.begin(); n != r.end(); ++n)
\r
360 aligned_memshfl(ptr+n*format_desc_.width*4, av_frame->data[0]+n*av_frame->linesize[0], format_desc_.width*4, 0x0F0F0F0F, 0x0B0B0B0B, 0x07070707, 0x03030303);
\r
365 tbb::parallel_for(tbb::blocked_range<int>(0, format_desc_.height), [&](const tbb::blocked_range<int>& r)
\r
367 for(int n = r.begin(); n != r.end(); ++n)
\r
368 A_memcpy(ptr+n*format_desc_.width*4, av_frame->data[0]+n*av_frame->linesize[0], format_desc_.width*4);
\r
372 GL(glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER)); // release the mapped buffer
\r
375 GL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
\r
377 GL(glClear(GL_COLOR_BUFFER_BIT));
\r
379 glTexCoord2f(0.0f, 1.0f); glVertex2f(-width_, -height_);
\r
380 glTexCoord2f(1.0f, 1.0f); glVertex2f( width_, -height_);
\r
381 glTexCoord2f(1.0f, 0.0f); glVertex2f( width_, height_);
\r
382 glTexCoord2f(0.0f, 0.0f); glVertex2f(-width_, height_);
\r
385 GL(glBindTexture(GL_TEXTURE_2D, 0));
\r
387 std::rotate(pbos_.begin(), pbos_.begin() + 1, pbos_.end());
\r
390 bool send(const spl::shared_ptr<const core::data_frame>& frame)
\r
392 if(!frame_buffer_.try_push(frame))
\r
393 graph_->set_tag("dropped-frame");
\r
394 return is_running_;
\r
397 std::wstring print() const
\r
399 return config_.name + L"[" + boost::lexical_cast<std::wstring>(channel_index_) + L"|" + format_desc_.name + L"]";
\r
402 void calculate_aspect()
\r
404 if(config_.windowed)
\r
406 screen_height_ = window_.GetHeight();
\r
407 screen_width_ = window_.GetWidth();
\r
410 GL(glViewport(0, 0, screen_width_, screen_height_));
\r
412 std::pair<float, float> target_ratio = None();
\r
413 if(config_.stretch == fill)
\r
414 target_ratio = Fill();
\r
415 else if(config_.stretch == uniform)
\r
416 target_ratio = Uniform();
\r
417 else if(config_.stretch == uniform_to_fill)
\r
418 target_ratio = UniformToFill();
\r
420 width_ = target_ratio.first;
\r
421 height_ = target_ratio.second;
\r
424 std::pair<float, float> None()
\r
426 float width = static_cast<float>(square_width_)/static_cast<float>(screen_width_);
\r
427 float height = static_cast<float>(square_height_)/static_cast<float>(screen_height_);
\r
429 return std::make_pair(width, height);
\r
432 std::pair<float, float> Uniform()
\r
434 float aspect = static_cast<float>(square_width_)/static_cast<float>(square_height_);
\r
435 float width = std::min(1.0f, static_cast<float>(screen_height_)*aspect/static_cast<float>(screen_width_));
\r
436 float height = static_cast<float>(screen_width_*width)/static_cast<float>(screen_height_*aspect);
\r
438 return std::make_pair(width, height);
\r
441 std::pair<float, float> Fill()
\r
443 return std::make_pair(1.0f, 1.0f);
\r
446 std::pair<float, float> UniformToFill()
\r
448 float wr = static_cast<float>(square_width_)/static_cast<float>(screen_width_);
\r
449 float hr = static_cast<float>(square_height_)/static_cast<float>(screen_height_);
\r
450 float r_inv = 1.0f/std::min(wr, hr);
\r
452 float width = wr*r_inv;
\r
453 float height = hr*r_inv;
\r
455 return std::make_pair(width, height);
\r
460 struct screen_consumer_proxy : public core::frame_consumer
\r
462 const configuration config_;
\r
463 std::unique_ptr<screen_consumer> consumer_;
\r
467 screen_consumer_proxy(const configuration& config)
\r
468 : config_(config){}
\r
472 virtual void initialize(const core::video_format_desc& format_desc, int channel_index) override
\r
475 consumer_.reset(new screen_consumer(config_, format_desc, channel_index));
\r
478 virtual bool send(const spl::shared_ptr<const core::data_frame>& frame) override
\r
480 return consumer_->send(frame);
\r
483 virtual std::wstring print() const override
\r
485 return consumer_ ? consumer_->print() : L"[screen_consumer]";
\r
488 virtual boost::property_tree::wptree info() const override
\r
490 boost::property_tree::wptree info;
\r
491 info.add(L"type", L"ogl-consumer");
\r
492 info.add(L"key-only", config_.key_only);
\r
493 info.add(L"windowed", config_.windowed);
\r
494 info.add(L"auto-deinterlace", config_.auto_deinterlace);
\r
498 virtual bool has_synchronization_clock() const override
\r
503 virtual int buffer_depth() const override
\r
508 virtual int index() const override
\r
514 spl::shared_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>& params)
\r
516 if(params.size() < 1 || params[0] != L"SCREEN")
\r
517 return core::frame_consumer::empty();
\r
519 configuration config;
\r
521 auto device_it = std::find(params.begin(), params.end(), L"DEVICE");
\r
522 if(device_it != params.end() && ++device_it != params.end())
\r
523 config.screen_index = boost::lexical_cast<int>(*device_it);
\r
525 config.key_only = std::find(params.begin(), params.end(), L"WINDOWED") != params.end();
\r
526 config.key_only = std::find(params.begin(), params.end(), L"KEY_ONLY") != params.end();
\r
528 auto name_it = std::find(params.begin(), params.end(), L"NAME");
\r
529 if(name_it != params.end() && ++name_it != params.end())
\r
530 config.name = *name_it;
\r
532 return spl::make_shared<screen_consumer_proxy>(config);
\r
535 spl::shared_ptr<core::frame_consumer> create_consumer(const boost::property_tree::wptree& ptree)
\r
537 configuration config;
\r
538 config.name = ptree.get(L"name", config.name);
\r
539 config.screen_index = ptree.get(L"device", config.screen_index+1)-1;
\r
540 config.windowed = ptree.get(L"windowed", config.windowed);
\r
541 config.key_only = ptree.get(L"key-only", config.key_only);
\r
542 config.auto_deinterlace = ptree.get(L"auto-deinterlace", config.auto_deinterlace);
\r
543 config.vsync = ptree.get(L"vsync", config.vsync);
\r
545 auto stretch_str = ptree.get(L"stretch", L"default");
\r
546 if(stretch_str == L"uniform")
\r
547 config.stretch = stretch::uniform;
\r
548 else if(stretch_str == L"uniform_to_fill")
\r
549 config.stretch = stretch::uniform_to_fill;
\r
551 auto aspect_str = ptree.get(L"aspect-ratio", L"default");
\r
552 if(aspect_str == L"16:9")
\r
553 config.aspect = configuration::aspect_16_9;
\r
554 else if(aspect_str == L"4:3")
\r
555 config.aspect = configuration::aspect_4_3;
\r
557 return spl::make_shared<screen_consumer_proxy>(config);
\r