Added frame buffer grabber support

Former-commit-id: a73767a1abb2cac977492bd8c6cc593fb21840e1
This commit is contained in:
Gamadril
2015-01-18 00:04:45 +01:00
parent 391ccf4738
commit c89e1fcefa
11 changed files with 453 additions and 2 deletions

View File

@@ -0,0 +1,35 @@
# Find the BCM-package (VC control)
# find_package(BCM REQUIRED)
# include_directories(${BCM_INCLUDE_DIRS})
# Define the current source locations
SET(CURRENT_HEADER_DIR ${CMAKE_SOURCE_DIR}/include/grabber)
SET(CURRENT_SOURCE_DIR ${CMAKE_SOURCE_DIR}/libsrc/grabber/framebuffer)
# Group the headers that go through the MOC compiler
SET(FramebufferGrabberQT_HEADERS
${CURRENT_HEADER_DIR}/FramebufferWrapper.h
)
SET(FramebufferGrabberHEADERS
${CURRENT_SOURCE_DIR}/FramebufferFrameGrabber.h
)
SET(FramebufferGrabberSOURCES
${CURRENT_SOURCE_DIR}/FramebufferWrapper.cpp
${CURRENT_SOURCE_DIR}/FramebufferFrameGrabber.cpp
)
QT4_WRAP_CPP(FramebufferGrabberHEADERS_MOC ${FramebufferGrabberQT_HEADERS})
add_library(framebuffer-grabber
${FramebufferGrabberHEADERS}
${FramebufferGrabberQT_HEADERS}
${FramebufferGrabberHEADERS_MOC}
${FramebufferGrabberSOURCES}
)
target_link_libraries(framebuffer-grabber
hyperion
${QT_LIBRARIES})

View File

@@ -0,0 +1,123 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <math.h>
// STL includes
#include <cassert>
#include <iostream>
// Local includes
#include "FramebufferFrameGrabber.h"
FramebufferFrameGrabber::FramebufferFrameGrabber(const std::string & device, const unsigned width, const unsigned height) :
_fbfd(0),
_fbp(0),
_fbDevice(device),
_width(width),
_height(height),
_xScale(1),
_yScale(1)
{
int result;
struct fb_var_screeninfo vinfo;
// Check if the framebuffer device can be opened and display the current resolution
_fbfd = open(_fbDevice.c_str(), O_RDONLY);
assert(_fbfd > 0);
// get variable screen information
result = ioctl (_fbfd, FBIOGET_VSCREENINFO, &vinfo);
assert(result == 0);
std::cout << "Framebuffer opened with resolution: " << vinfo.xres << "x" << vinfo.yres << "@" << vinfo.bits_per_pixel << "bit" << std::endl;
close(_fbfd);
}
FramebufferFrameGrabber::~FramebufferFrameGrabber()
{
}
void FramebufferFrameGrabber::setVideoMode(const VideoMode videoMode)
{
switch (videoMode) {
case VIDEO_3DSBS:
_xScale = 2;
_yScale = 1;
break;
case VIDEO_3DTAB:
_xScale = 1;
_yScale = 2;
break;
case VIDEO_2D:
default:
_xScale = 1;
_yScale = 1;
break;
}
}
void FramebufferFrameGrabber::grabFrame(Image<ColorRgba> & image)
{
struct fb_var_screeninfo vinfo;
unsigned capSize, px, py, index, bytesPerPixel;
float x_scale, y_scale;
/* resize the given image if needed */
if (image.width() != _width || image.height() != _height)
{
image.resize(_width, _height);
}
/* Open the framebuffer device */
_fbfd = open(_fbDevice.c_str(), O_RDONLY);
/* get variable screen information */
ioctl (_fbfd, FBIOGET_VSCREENINFO, &vinfo);
bytesPerPixel = vinfo.bits_per_pixel / 8;
capSize = vinfo.xres * vinfo.yres * bytesPerPixel;
/* map the device to memory */
_fbp = (unsigned char*)mmap(0, capSize, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, _fbfd, 0);
/* nearest neighbor downscaling */
x_scale = (vinfo.xres / float(_xScale)) / float(_width);
y_scale = (vinfo.yres / float(_yScale)) / float(_height);
ColorRgba *pPixel = image.memptr();
for (unsigned i=0; i < _height; i++) {
for (unsigned j=0; j < _width; j++) {
px = floor(j * x_scale);
py = floor(i * y_scale);
index = (py * vinfo.xres + px) * bytesPerPixel;
if (vinfo.bits_per_pixel == 16) {
pPixel->blue = (_fbp[index] & 0x1f) << 3;
pPixel->green = (((_fbp[index + 1] & 0x7) << 3) | (_fbp[index] & 0xE0) >> 5) << 2;
pPixel->red = (_fbp[index + 1] & 0xF8);
pPixel->alpha = 255;
} else if(vinfo.bits_per_pixel == 24) {
pPixel->blue = _fbp[index];
pPixel->green = _fbp[index + 1];
pPixel->red = _fbp[index + 2];
pPixel->alpha = 255;
} else if(vinfo.bits_per_pixel == 32) {
pPixel->blue = _fbp[index];
pPixel->green = _fbp[index + 1];
pPixel->red = _fbp[index + 2];
pPixel->alpha = _fbp[index + 3];
}
pPixel++;
}
}
munmap(_fbp, capSize);
close(_fbfd);
}

View File

@@ -0,0 +1,61 @@
#pragma once
// Utils includes
#include <utils/Image.h>
#include <utils/ColorRgba.h>
#include <utils/VideoMode.h>
///
/// The FramebufferFrameGrabber is used for creating snapshots of the display (screenshots)
///
class FramebufferFrameGrabber
{
public:
///
/// Construct a FramebufferFrameGrabber that will capture snapshots with specified dimensions.
///
/// @param[in] device The framebuffer device name/path
/// @param[in] width The width of the captured screenshot
/// @param[in] height The heigth of the captured screenshot
///
FramebufferFrameGrabber(const std::string & device, const unsigned width, const unsigned height);
~FramebufferFrameGrabber();
///
/// Set the video mode (2D/3D)
/// @param[in] mode The new video mode
///
void setVideoMode(const VideoMode videoMode);
///
/// Captures a single snapshot of the display and writes the data to the given image. The
/// provided image should have the same dimensions as the configured values (_width and
/// _height)
///
/// @param[out] image The snapped screenshot (should be initialized with correct width and
/// height)
///
void grabFrame(Image<ColorRgba> & image);
private:
/// Framebuffer file descriptor
int _fbfd;
/// Pointer to framebuffer
unsigned char * _fbp;
/// Framebuffer device e.g. /dev/fb0
const std::string _fbDevice;
/// With of the captured snapshot [pixels]
const unsigned _width;
/// Height of the captured snapshot [pixels]
const unsigned _height;
/// width scale factor for 3D image processing
float _xScale;
/// height scale factor for 3D image processing
float _yScale;
};

View File

@@ -0,0 +1,79 @@
// Hyperion includes
#include <hyperion/Hyperion.h>
#include <hyperion/ImageProcessorFactory.h>
#include <hyperion/ImageProcessor.h>
// Framebuffer grabber includes
#include <grabber/FramebufferWrapper.h>
#include "FramebufferFrameGrabber.h"
FramebufferWrapper::FramebufferWrapper(const std::string & device, const unsigned grabWidth, const unsigned grabHeight, const unsigned updateRate_Hz, Hyperion * hyperion) :
_updateInterval_ms(1000/updateRate_Hz),
_timeout_ms(2 * _updateInterval_ms),
_priority(1000),
_timer(),
_image(grabWidth, grabHeight),
_frameGrabber(new FramebufferFrameGrabber(device, grabWidth, grabHeight)),
_processor(ImageProcessorFactory::getInstance().newImageProcessor()),
_ledColors(hyperion->getLedCount(), ColorRgb{0,0,0}),
_hyperion(hyperion)
{
// Configure the timer to generate events every n milliseconds
_timer.setInterval(_updateInterval_ms);
_timer.setSingleShot(false);
_processor->setSize(grabWidth, grabHeight);
// Connect the QTimer to this
QObject::connect(&_timer, SIGNAL(timeout()), this, SLOT(action()));
}
FramebufferWrapper::~FramebufferWrapper()
{
// Cleanup used resources (ImageProcessor and FrameGrabber)
delete _processor;
delete _frameGrabber;
}
void FramebufferWrapper::start()
{
// Start the timer with the pre configured interval
_timer.start();
}
void FramebufferWrapper::action()
{
// Grab frame into the allocated image
_frameGrabber->grabFrame(_image);
_processor->process(_image, _ledColors);
_hyperion->setColors(_priority, _ledColors, _timeout_ms);
}
void FramebufferWrapper::stop()
{
// Stop the timer, effectivly stopping the process
_timer.stop();
}
void FramebufferWrapper::setGrabbingMode(const GrabbingMode mode)
{
switch (mode)
{
case GRABBINGMODE_VIDEO:
case GRABBINGMODE_AUDIO:
case GRABBINGMODE_PHOTO:
case GRABBINGMODE_MENU:
case GRABBINGMODE_INVALID:
start();
break;
case GRABBINGMODE_OFF:
stop();
break;
}
}
void FramebufferWrapper::setVideoMode(const VideoMode mode)
{
_frameGrabber->setVideoMode(mode);
}