mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2023-10-10 13:36:59 +02:00
348e0c1ee8
Former-commit-id: b56a72681edbf442a28fb3106a4ca445b6e6aaa8
94 lines
2.6 KiB
C++
94 lines
2.6 KiB
C++
|
|
// STL includes
|
|
#include <cassert>
|
|
#include <iostream>
|
|
|
|
// Linux includes
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
#include <sys/ioctl.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/types.h>
|
|
|
|
// Local includes
|
|
#include "AmlogicGrabber.h"
|
|
|
|
// Flags copied from 'linux/amlogic/amports/amvideocap.h' at https://github.com/codesnake/linux-amlogic/
|
|
#define AMVIDEOCAP_IOC_MAGIC 'V'
|
|
#define AMVIDEOCAP_IOW_SET_WANTFRAME_WIDTH _IOW(AMVIDEOCAP_IOC_MAGIC, 0x02, int)
|
|
#define AMVIDEOCAP_IOW_SET_WANTFRAME_HEIGHT _IOW(AMVIDEOCAP_IOC_MAGIC, 0x03, int)
|
|
|
|
AmlogicGrabber::AmlogicGrabber(const unsigned width, const unsigned height) :
|
|
_width(width),
|
|
_height(height),
|
|
_amlogicCaptureDev(-1)
|
|
{
|
|
}
|
|
|
|
AmlogicGrabber::~AmlogicGrabber()
|
|
{
|
|
if (_amlogicCaptureDev != -1)
|
|
{
|
|
if (close(_amlogicCaptureDev) == -1)
|
|
{
|
|
std::cerr << "[" << __PRETTY_FUNCTION__ << "] Failed to close AMLOGIC device (" << errno << ")" << std::endl;
|
|
}
|
|
_amlogicCaptureDev = -1;
|
|
}
|
|
}
|
|
|
|
void AmlogicGrabber::setVideoMode(const VideoMode videoMode)
|
|
{
|
|
switch (videoMode) {
|
|
case VIDEO_3DSBS:
|
|
//vc_dispmanx_rect_set(&_rectangle, 0, 0, _width/2, _height);
|
|
break;
|
|
case VIDEO_3DTAB:
|
|
//vc_dispmanx_rect_set(&_rectangle, 0, 0, _width, _height/2);
|
|
break;
|
|
case VIDEO_2D:
|
|
default:
|
|
//vc_dispmanx_rect_set(&_rectangle, 0, 0, _width, _height);
|
|
break;
|
|
}
|
|
}
|
|
|
|
void AmlogicGrabber::grabFrame(Image<ColorRgb> & image)
|
|
{
|
|
// resize the given image if needed
|
|
if (image.width() != _width || image.height() != _height)
|
|
{
|
|
image.resize(_width, _height);
|
|
}
|
|
|
|
_amlogicCaptureDev = open("/dev/amvideocap0", O_RDONLY, 0);
|
|
if (_amlogicCaptureDev == -1)
|
|
{
|
|
std::cerr << "[" << __PRETTY_FUNCTION__ << "] Failed to open the AMLOGIC device (" << errno << ")" << std::endl;
|
|
return;
|
|
}
|
|
|
|
if (ioctl(_amlogicCaptureDev, AMVIDEOCAP_IOW_SET_WANTFRAME_WIDTH, _width) == -1 ||
|
|
ioctl(_amlogicCaptureDev, AMVIDEOCAP_IOW_SET_WANTFRAME_HEIGHT, _height) == -1)
|
|
{
|
|
// Failed to configure frame width
|
|
std::cerr << "[" << __PRETTY_FUNCTION__ << "] Failed to configure capture size (" << errno << ")" << std::endl;
|
|
return;
|
|
}
|
|
|
|
std::cout << "AMLOGIC grabber created (size " << _width << "x" << _height << ")" << std::endl;
|
|
// Read the snapshot into the memory
|
|
void * image_ptr = image.memptr();
|
|
const size_t bytesToRead = _width * _height * sizeof(ColorRgb);
|
|
const size_t bytesRead = pread(_amlogicCaptureDev, image_ptr, bytesToRead, 0);
|
|
if (bytesToRead != bytesRead)
|
|
{
|
|
// Read of snapshot failed
|
|
std::cerr << "[" << __PRETTY_FUNCTION__ << "] Capture failed to grab entire image [bytesToRead(" << bytesToRead << ") != bytesRead(" << bytesRead << ")]" << std::endl;
|
|
}
|
|
|
|
close(_amlogicCaptureDev);
|
|
_amlogicCaptureDev = -1;
|
|
}
|