amlogic refactoring (#464)

* - grabber auto off when not set as active prio
- join aml and fb - on aml platform both grabbers are needed, so they joind in one module and share one prio. user don't the the nasty magic behind
- aml: preparation for direct ge2d access

* just save it, in the middle of ge2d impl

* fix compile issues

* now grabbing works basicly

* add 3d support for ge2d

* next step, we got some video from aml

* switch back to rgba

* remove unfinished ge2d stuff

* commit missing changes

* some urgent fixes, needs some beautifying, but it works now

* fixes and refctoring
This commit is contained in:
redPanther
2017-09-01 08:50:37 +02:00
committed by GitHub
parent 8644672296
commit 2aa4df92a9
24 changed files with 291 additions and 142 deletions

View File

@@ -1,8 +1,8 @@
// STL includes
#include <algorithm>
#include <cassert>
#include <iostream>
#include <QFile>
// Linux includes
#include <errno.h>
@@ -15,137 +15,157 @@
// Local includes
#include <utils/Logger.h>
#include <grabber/AmlogicGrabber.h>
#include "Amvideocap.h"
// Flags copied from 'include/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)
#if HAVE_AML_HEADER
#include <amcodec/amports/amstream.h>
#else
// Flags copied from 'include/linux/amlogic/amports/amvstream.h' at https://github.com/codesnake/linux-amlogic
#define AMSTREAM_IOC_MAGIC 'S'
#define AMSTREAM_IOC_GET_VIDEO_DISABLE _IOR(AMSTREAM_IOC_MAGIC, 0x48, unsigned long)
#endif
#define VIDEO_DEVICE "/dev/amvideo"
#define CAPTURE_DEVICE "/dev/amvideocap0"
#include <iostream>
AmlogicGrabber::AmlogicGrabber(const unsigned width, const unsigned height)
: Grabber("AMLOGICGRABBER", qMax(160u, width), qMax(160u, height)) // Minimum required width or height is 160
, _amlogicCaptureDev(-1)
, _captureDev(-1)
, _videoDev(-1)
, _lastError(0)
, _fbGrabber("/dev/fb0",width,height)
, _grabbingModeNotification(0)
{
Debug(_log, "constructed(%d x %d)",_width,_height);
}
AmlogicGrabber::~AmlogicGrabber()
{
if (_amlogicCaptureDev != -1)
closeDev(_captureDev);
closeDev(_videoDev);
}
bool AmlogicGrabber::openDev(int &fd, const char* dev, int flags)
{
if (fd<0)
{
if (close(_amlogicCaptureDev) == -1)
{
Error(_log, "Failed to close device (%d - %s)", errno, strerror(errno));
}
_amlogicCaptureDev = -1;
fd = open(dev, flags);
}
return fd >= 0;
}
void AmlogicGrabber::closeDev(int &fd)
{
if (fd >= 0)
{
close(fd);
fd = -1;
}
}
bool AmlogicGrabber::isVideoPlaying()
{
const QString videoDevice = "/dev/amvideo";
if(!QFile::exists(VIDEO_DEVICE)) return false;
// Open the video device
int video_fd = open(QSTRING_CSTR(videoDevice), O_RDONLY);
if (video_fd < 0)
int videoDisabled = 1;
if (!openDev(_videoDev, VIDEO_DEVICE, O_RDWR))
{
Error(_log, "Failed to open video device(%s): %d - %s", QSTRING_CSTR(videoDevice), errno, strerror(errno));
Error(_log, "Failed to open video device(%s): %d - %s", VIDEO_DEVICE, errno, strerror(errno));
return false;
}
// Check the video disabled flag
int videoDisabled;
if (ioctl(video_fd, AMSTREAM_IOC_GET_VIDEO_DISABLE, &videoDisabled) == -1)
else
{
Error(_log, "Failed to retrieve video state from device: %d - %s", errno, strerror(errno));
close(video_fd);
return false;
// Check the video disabled flag
if(ioctl(_videoDev, AMSTREAM_IOC_GET_VIDEO_DISABLE, &videoDisabled) < 0)
{
Error(_log, "Failed to retrieve video state from device: %d - %s", errno, strerror(errno));
closeDev(_videoDev);
return false;
}
}
// Make sure to close the device after use
close(video_fd);
return videoDisabled == 0;
}
int AmlogicGrabber::grabFrame(Image<ColorRgb> & image)
{
// TODO crop resulting image accroding member _videoMode
// TODO add croping
if (!_enabled) return 0;
image.resize(_width,_height);
// Make sure video is playing, else there is nothing to grab
if (!isVideoPlaying())
if (isVideoPlaying())
{
if (_grabbingModeNotification!=1)
{
Info(_log, "VPU mode");
_grabbingModeNotification = 1;
_lastError = 0;
}
if (QFile::exists(CAPTURE_DEVICE))
{
grabFrame_amvideocap(image);
}
}
else
{
if (_grabbingModeNotification!=2)
{
Info( _log, "FB mode");
_grabbingModeNotification = 2;
_lastError = 0;
}
_fbGrabber.grabFrame(image);
}
closeDev(_videoDev);
return 0;
}
int AmlogicGrabber::grabFrame_amvideocap(Image<ColorRgb> & image)
{
// If the device is not open, attempt to open it
if (! openDev(_captureDev, CAPTURE_DEVICE, O_RDWR))
{
ErrorIf( _lastError != 1, _log,"Failed to open the AMLOGIC device (%d - %s):", errno, strerror(errno));
_lastError = 1;
return -1;
}
// If the device is not open, attempt to open it
if (_amlogicCaptureDev == -1)
long r1 = ioctl(_captureDev, AMVIDEOCAP_IOW_SET_WANTFRAME_WIDTH, _width);
long r2 = ioctl(_captureDev, AMVIDEOCAP_IOW_SET_WANTFRAME_HEIGHT, _height);
if (r1<0 || r2<0 || _height==0 || _width==0)
{
_amlogicCaptureDev = open("/dev/amvideocap0", O_RDONLY, 0);
// If the device is still not open, there is something wrong
if (_amlogicCaptureDev == -1)
{
Error(_log,"Failed to open the AMLOGIC device (%d - %s):", errno, strerror(errno));
return -1;
}
}
if (ioctl(_amlogicCaptureDev, AMVIDEOCAP_IOW_SET_WANTFRAME_WIDTH, _width) == -1 ||
ioctl(_amlogicCaptureDev, AMVIDEOCAP_IOW_SET_WANTFRAME_HEIGHT, _height) == -1)
{
// Failed to configure frame width
Error(_log,"Failed to configure capture size (%d - %s)", errno, strerror(errno));
close(_amlogicCaptureDev);
_amlogicCaptureDev = -1;
ErrorIf(_lastError != 2,_log,"Failed to configure capture size (%d - %s)", errno, strerror(errno));
closeDev(_captureDev);
_lastError = 2;
return -1;
}
// Read the snapshot into the memory
void * image_ptr = _image.memptr();
const ssize_t bytesToRead = _width * _height * sizeof(ColorBgr);
image.resize(_width, _height);
_image_bgr.resize(_width, _height);
const ssize_t bytesToRead = _image_bgr.size();
void * image_ptr = _image_bgr.memptr();
const ssize_t bytesRead = pread(_captureDev, image_ptr, bytesToRead, 0);
const ssize_t bytesRead = pread(_amlogicCaptureDev, image_ptr, bytesToRead, 0);
if (bytesRead == -1)
if (bytesRead < 0)
{
Error(_log,"Read of device failed: %d - %s", errno, strerror(errno));
close(_amlogicCaptureDev);
_amlogicCaptureDev = -1;
ErrorIf(_lastError != 3, _log,"Read of device failed: %d - %s", errno, strerror(errno));
closeDev(_captureDev);
_lastError = 3;
return -1;
}
else if (bytesToRead != bytesRead)
{
// Read of snapshot failed
Error(_log,"Capture failed to grab entire image [bytesToRead(%d) != bytesRead(%d)]", bytesToRead, bytesRead);
close(_amlogicCaptureDev);
_amlogicCaptureDev = -1;
ErrorIf(_lastError != 4, _log,"Capture failed to grab entire image [bytesToRead(%d) != bytesRead(%d)]", bytesToRead, bytesRead);
closeDev(_captureDev);
return -1;
}
// For now we always close the device now and again
static int readCnt = 0;
++readCnt;
if (readCnt > 20)
{
close(_amlogicCaptureDev);
_amlogicCaptureDev = -1;
readCnt = 0;
}
closeDev(_captureDev);
_imageResampler.setHorizontalPixelDecimation(_width);
_imageResampler.setVerticalPixelDecimation(_height);
_imageResampler.processImage((const uint8_t*)image_ptr, _width, _height, 3, PIXELFORMAT_BGR24, image);
_imageResampler.processImage((const uint8_t*)image_ptr, _width, _height, _width*3, PIXELFORMAT_BGR24, image);
_lastError = 0;
return 0;
}

View File

@@ -0,0 +1,80 @@
#pragma once
#include <exception>
//#include <linux/videodev2.h>
#define AMVIDEOCAP_IOC_MAGIC 'V'
#define CAP_FLAG_AT_CURRENT 0
#define CAP_FLAG_AT_TIME_WINDOW 1
#define CAP_FLAG_AT_END 2
/*
format see linux/ge2d/ge2d.h
like:
GE2D_FORMAT_S24_RGB
*/
#define GE2D_ENDIAN_SHIFT 24
#define GE2D_LITTLE_ENDIAN (1 << GE2D_ENDIAN_SHIFT)
#define GE2D_COLOR_MAP_SHIFT 20
#define GE2D_COLOR_MAP_BGR888 (5 << GE2D_COLOR_MAP_SHIFT)
#define GE2D_COLOR_MAP_RGB888 (0 << GE2D_COLOR_MAP_SHIFT)
#define GE2D_FMT_S24_RGB (GE2D_LITTLE_ENDIAN|0x00200) /* 10_00_0_00_0_00 */
#define GE2D_FORMAT_S24_BGR (GE2D_FMT_S24_RGB | GE2D_COLOR_MAP_BGR888)
#define GE2D_FORMAT_S24_RGB (GE2D_FMT_S24_RGB | GE2D_COLOR_MAP_RGB888)
#define AMVIDEOCAP_IOW_SET_WANTFRAME_FORMAT _IOW(AMVIDEOCAP_IOC_MAGIC, 0x01, int)
#define AMVIDEOCAP_IOW_SET_WANTFRAME_WIDTH _IOW(AMVIDEOCAP_IOC_MAGIC, 0x02, int)
#define AMVIDEOCAP_IOW_SET_WANTFRAME_HEIGHT _IOW(AMVIDEOCAP_IOC_MAGIC, 0x03, int)
#define AMVIDEOCAP_IOW_SET_WANTFRAME_TIMESTAMP_MS _IOW(AMVIDEOCAP_IOC_MAGIC, 0x04, unsigned long)
#define AMVIDEOCAP_IOW_SET_WANTFRAME_WAIT_MAX_MS _IOW(AMVIDEOCAP_IOC_MAGIC, 0x05, unsigned long)
#define AMVIDEOCAP_IOW_SET_WANTFRAME_AT_FLAGS _IOW(AMVIDEOCAP_IOC_MAGIC, 0x06, int)
#define AMVIDEOCAP_IOR_GET_FRAME_FORMAT _IOR(AMVIDEOCAP_IOC_MAGIC, 0x10, int)
#define AMVIDEOCAP_IOR_GET_FRAME_WIDTH _IOR(AMVIDEOCAP_IOC_MAGIC, 0x11, int)
#define AMVIDEOCAP_IOR_GET_FRAME_HEIGHT _IOR(AMVIDEOCAP_IOC_MAGIC, 0x12, int)
#define AMVIDEOCAP_IOR_GET_FRAME_TIMESTAMP_MS _IOR(AMVIDEOCAP_IOC_MAGIC, 0x13, unsigned long)
#define AMVIDEOCAP_IOR_GET_SRCFRAME_FORMAT _IOR(AMVIDEOCAP_IOC_MAGIC, 0x20, int)
#define AMVIDEOCAP_IOR_GET_SRCFRAME_WIDTH _IOR(AMVIDEOCAP_IOC_MAGIC, 0x21, int)
#define AMVIDEOCAP_IOR_GET_SRCFRAME_HEIGHT _IOR(AMVIDEOCAP_IOC_MAGIC, 0x22, int)
#define AMVIDEOCAP_IOR_GET_STATE _IOR(AMVIDEOCAP_IOC_MAGIC, 0x31, int)
#define AMVIDEOCAP_IOW_SET_START_CAPTURE _IOW(AMVIDEOCAP_IOC_MAGIC, 0x32, int)
#define AMVIDEOCAP_IOW_SET_CANCEL_CAPTURE _IOW(AMVIDEOCAP_IOC_MAGIC, 0x33, int)
#define AMVIDEOCAP_IOR_SET_SRC_X _IOR(AMVIDEOCAP_IOC_MAGIC, 0x40, int)
#define AMVIDEOCAP_IOR_SET_SRC_Y _IOR(AMVIDEOCAP_IOC_MAGIC, 0x41, int)
#define AMVIDEOCAP_IOR_SET_SRC_WIDTH _IOR(AMVIDEOCAP_IOC_MAGIC, 0x42, int)
#define AMVIDEOCAP_IOR_SET_SRC_HEIGHT _IOR(AMVIDEOCAP_IOC_MAGIC, 0x43, int)
enum amvideocap_state{
AMVIDEOCAP_STATE_INIT=0,
AMVIDEOCAP_STATE_ON_CAPTURE=200,
AMVIDEOCAP_STATE_FINISHED_CAPTURE=300,
AMVIDEOCAP_STATE_ERROR=0xffff,
};
#define AMVIDEO_MAGIC 'X'
#define AMVIDEO_EXT_GET_CURRENT_VIDEOFRAME _IOR((AMVIDEO_MAGIC), 0x01, int32_t)
#define AMVIDEO_EXT_PUT_CURRENT_VIDEOFRAME _IO((AMVIDEO_MAGIC), 0x02)
#define AMVIDEO_EXT_CURRENT_VIDEOFRAME_GET_GE2D_FORMAT _IOR((AMVIDEO_MAGIC), 0x03, uint32_t)
#define AMVIDEO_EXT_CURRENT_VIDEOFRAME_GET_SIZE _IOR((AMVIDEO_MAGIC), 0x04, uint64_t)
#define AMVIDEO_EXT_CURRENT_VIDEOFRAME_GET_CANVAS0ADDR _IOR((AMVIDEO_MAGIC), 0x05, uint32_t)
#define _A_M 'S'
#define AMSTREAM_IOC_GET_VIDEO_DISABLE _IOR((_A_M), 0x48, int)
#define AMSTREAM_IOC_SET_VIDEO_DISABLE _IOW((_A_M), 0x49, int)
struct Rectangle
{
int X;
int Y;
int Width;
int Height;
};

View File

@@ -6,11 +6,6 @@ SET(CURRENT_SOURCE_DIR ${CMAKE_SOURCE_DIR}/libsrc/grabber/amlogic)
FILE ( GLOB AmlogicSOURCES "${CURRENT_HEADER_DIR}/Amlogic*.h" "${CURRENT_SOURCE_DIR}/*.h" "${CURRENT_SOURCE_DIR}/*.cpp" )
CHECK_INCLUDE_FILES ("amcodec/amports/amstream.h" HAVE_AML_HEADER)
IF (${HAVE_AML_HEADER})
ADD_DEFINITIONS( -DHAVE_AML_HEADER )
ENDIF()
add_library(amlogic-grabber ${AmlogicSOURCES} )
target_link_libraries(amlogic-grabber

View File

@@ -69,6 +69,8 @@ void DispmanxFrameGrabber::setFlags(const int vc_flags)
int DispmanxFrameGrabber::grabFrame(Image<ColorRgb> & image)
{
if (!_enabled) return 0;
int ret;
// vc_dispmanx_resource_read_data doesn't seem to work well

View File

@@ -37,7 +37,7 @@ FramebufferFrameGrabber::FramebufferFrameGrabber(const QString & device, const u
}
else
{
Error(_log, "Display opened with resolution: %dx%d@%dbit", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel);
Info(_log, "Display opened with resolution: %dx%d@%dbit", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel);
}
close(_fbfd);
}
@@ -49,6 +49,8 @@ FramebufferFrameGrabber::~FramebufferFrameGrabber()
int FramebufferFrameGrabber::grabFrame(Image<ColorRgb> & image)
{
if (!_enabled) return 0;
struct fb_var_screeninfo vinfo;
unsigned capSize, bytesPerPixel;
PixelFormat pixelFormat;

View File

@@ -39,6 +39,8 @@ OsxFrameGrabber::~OsxFrameGrabber()
int OsxFrameGrabber::grabFrame(Image<ColorRgb> & image)
{
if (!_enabled) return 0;
CGImageRef dispImage;
CFDataRef imgData;
unsigned char * pImgData;

View File

@@ -86,20 +86,14 @@ void V4L2Wrapper::readError(const char* err)
void V4L2Wrapper::checkSources()
{
QList<int> activePriorities = _hyperion->getActivePriorities();
for (int x : activePriorities)
if ( _hyperion->isCurrentPriority(_priority))
{
if (x < _priority)
{
// found a higher priority source: grabber should be disabled
_grabber.stop();
return;
}
_grabber.start();
}
else
{
_grabber.stop();
}
// no higher priority source was found: grabber should be enabled
_grabber.start();
}
void V4L2Wrapper::action()

View File

@@ -117,6 +117,8 @@ bool X11Grabber::Setup()
int X11Grabber::grabFrame(Image<ColorRgb> & image, bool forceUpdate)
{
if (!_enabled) return 0;
if (forceUpdate)
updateScreenDimensions(forceUpdate);