Skip to content
This repository was archived by the owner on May 6, 2021. It is now read-only.

Commit fdb9c1c

Browse files
committed
Added code for amlogic grabber.
1 parent b5be5f6 commit fdb9c1c

File tree

7 files changed

+375
-22
lines changed

7 files changed

+375
-22
lines changed

CMakeLists.txt

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,40 @@ cmake_minimum_required(VERSION 2.8)
77
#set(CMAKE_TOOLCHAIN_FILE /opt/raspberrypi/Toolchain-RaspberryPi.cmake)
88

99
# set the build options
10+
option(ENABLE_AMLOGIC "Enable the AMLOGIC video grabber" OFF)
11+
message(STATUS "ENABLE_AMLOGIC = " ${ENABLE_AMLOGIC})
12+
1013
option(ENABLE_DISPMANX "Enable the RPi dispmanx grabber" ON)
1114
message(STATUS "ENABLE_DISPMANX = " ${ENABLE_DISPMANX})
1215

16+
option(ENABLE_FB "Enable the framebuffer grabber" OFF)
17+
message(STATUS "ENABLE_FB = " ${ENABLE_FB})
18+
19+
option(ENABLE_OSX "Enable the osx grabber" OFF)
20+
message(STATUS "ENABLE_OSX = " ${ENABLE_OSX})
21+
22+
option(ENABLE_PROTOBUF "Enable PROTOBUF server" ON)
23+
message(STATUS "ENABLE_PROTOBUF = " ${ENABLE_PROTOBUF})
24+
1325
option(ENABLE_SPIDEV "Enable the SPIDEV device" ON)
1426
message(STATUS "ENABLE_SPIDEV = " ${ENABLE_SPIDEV})
1527

16-
option(ENABLE_WS2812BPWM "Enable the WS2812b-PWM device" OFF)
17-
message(STATUS "ENABLE_WS2812BPWM = " ${ENABLE_WS2812BPWM})
28+
option(ENABLE_TINKERFORGE "Enable the TINKERFORGE device" ON)
29+
message(STATUS "ENABLE_TINKERFORGE = " ${ENABLE_TINKERFORGE})
1830

1931
option(ENABLE_V4L2 "Enable the V4L2 grabber" ON)
2032
message(STATUS "ENABLE_V4L2 = " ${ENABLE_V4L2})
2133

34+
option(ENABLE_WS2812BPWM "Enable the WS2812b-PWM device" OFF)
35+
message(STATUS "ENABLE_WS2812BPWM = " ${ENABLE_WS2812BPWM})
36+
2237
option(ENABLE_X11 "Enable the X11 grabber" OFF)
2338
message(STATUS "ENABLE_X11 = " ${ENABLE_X11})
2439

25-
option(ENABLE_TINKERFORGE "Enable the TINKERFORGE device" ON)
26-
message(STATUS "ENABLE_TINKERFORGE = " ${ENABLE_TINKERFORGE})
27-
28-
option(ENABLE_PROTOBUF "Enable PROTOBUF server" ON)
29-
message(STATUS "ENABLE_PROTOBUF = " ${ENABLE_PROTOBUF})
30-
3140
if(ENABLE_V4L2 AND NOT ENABLE_PROTOBUF)
3241
message(FATAL_ERROR "V4L2 grabber requires PROTOBUF. Disable V4L2 or enable PROTOBUF")
3342
endif(ENABLE_V4L2 AND NOT ENABLE_PROTOBUF)
3443

35-
option(ENABLE_FB "Enable the framebuffer grabber" OFF)
36-
message(STATUS "ENABLE_FB = " ${ENABLE_FB})
37-
38-
option(ENABLE_OSX "Enable the osx grabber" OFF)
39-
message(STATUS "ENABLE_OSX = " ${ENABLE_OSX})
40-
4144
if(ENABLE_FB AND ENABLE_DISPMANX)
4245
message(FATAL_ERROR "dispmanx grabber and framebuffer grabber cannot be used at the same time")
4346
endif(ENABLE_FB AND ENABLE_DISPMANX)
@@ -89,10 +92,6 @@ find_package(Qt4 COMPONENTS QtCore QtGui QtNetwork REQUIRED QUIET)
8992
find_package(libusb-1.0 REQUIRED)
9093
find_package(Threads REQUIRED)
9194

92-
if (ENABLE_TINKERFORGE)
93-
#find_package(libtinkerforge-1.0 REQUIRED)
94-
endif (ENABLE_TINKERFORGE)
95-
9695
include(${QT_USE_FILE})
9796
add_definitions(${QT_DEFINITIONS})
9897
# TODO[TvdZ]: This linking directory should only be added if we are cross compiling

include/grabber/AmlogicWrapper.h

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#pragma once
2+
3+
// QT includes
4+
#include <QObject>
5+
#include <QTimer>
6+
7+
// Utils includes
8+
#include <utils/Image.h>
9+
#include <utils/ColorRgb.h>
10+
#include <utils/ColorRgba.h>
11+
#include <utils/GrabbingMode.h>
12+
#include <utils/VideoMode.h>
13+
14+
// Forward class declaration
15+
class AmlogicGrabber;
16+
class Hyperion;
17+
class ImageProcessor;
18+
19+
///
20+
/// The DispmanxWrapper uses an instance of the DispmanxFrameGrabber to obtain ImageRgb's from the
21+
/// displayed content. This ImageRgb is processed to a ColorRgb for each led and commmited to the
22+
/// attached Hyperion.
23+
///
24+
class AmlogicWrapper : public QObject
25+
{
26+
Q_OBJECT
27+
public:
28+
///
29+
/// Constructs the dispmanx frame grabber with a specified grab size and update rate.
30+
///
31+
/// @param[in] grabWidth The width of the grabbed image [pixels]
32+
/// @param[in] grabHeight The height of the grabbed images [pixels]
33+
/// @param[in] updateRate_Hz The image grab rate [Hz]
34+
/// @param[in] hyperion The instance of Hyperion used to write the led values
35+
///
36+
AmlogicWrapper(const unsigned grabWidth, const unsigned grabHeight, const unsigned updateRate_Hz, Hyperion * hyperion);
37+
38+
///
39+
/// Destructor of this dispmanx frame grabber. Releases any claimed resources.
40+
///
41+
virtual ~AmlogicWrapper();
42+
43+
public slots:
44+
///
45+
/// Starts the grabber wich produces led values with the specified update rate
46+
///
47+
void start();
48+
49+
///
50+
/// Performs a single frame grab and computes the led-colors
51+
///
52+
void action();
53+
54+
///
55+
/// Stops the grabber
56+
///
57+
void stop();
58+
59+
///
60+
/// Set the grabbing mode
61+
/// @param[in] mode The new grabbing mode
62+
///
63+
void setGrabbingMode(const GrabbingMode mode);
64+
65+
///
66+
/// Set the video mode (2D/3D)
67+
/// @param[in] mode The new video mode
68+
///
69+
void setVideoMode(const VideoMode videoMode);
70+
71+
private:
72+
/// The update rate [Hz]
73+
const int _updateInterval_ms;
74+
/// The timeout of the led colors [ms]
75+
const int _timeout_ms;
76+
/// The priority of the led colors
77+
const int _priority;
78+
79+
/// The timer for generating events with the specified update rate
80+
QTimer _timer;
81+
82+
/// The image used for grabbing frames
83+
Image<ColorRgb> _image;
84+
/// The actual grabber
85+
AmlogicGrabber * _frameGrabber;
86+
/// The processor for transforming images to led colors
87+
ImageProcessor * _processor;
88+
89+
/// The list with computed led colors
90+
std::vector<ColorRgb> _ledColors;
91+
92+
/// Pointer to Hyperion for writing led values
93+
Hyperion * _hyperion;
94+
};

libsrc/grabber/CMakeLists.txt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
if (ENABLE_AMLOGIC)
2+
add_subdirectory(amlogic)
3+
endif (ENABLE_AMLOGIC)
4+
15
if (ENABLE_DISPMANX)
26
add_subdirectory(dispmanx)
37
endif (ENABLE_DISPMANX)
@@ -6,14 +10,14 @@ if (ENABLE_FB)
610
add_subdirectory(framebuffer)
711
endif (ENABLE_FB)
812

13+
if (ENABLE_OSX)
14+
add_subdirectory(osx)
15+
endif()
16+
917
if (ENABLE_V4L2)
1018
add_subdirectory(v4l2)
1119
endif (ENABLE_V4L2)
1220

1321
if (ENABLE_X11)
1422
add_subdirectory(x11)
1523
endif()
16-
17-
if (ENABLE_OSX)
18-
add_subdirectory(osx)
19-
endif()
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
2+
// STL includes
3+
#include <cassert>
4+
#include <iostream>
5+
6+
// Linux includes
7+
#include <errno.h>
8+
#include <fcntl.h>
9+
#include <unistd.h>
10+
#include <sys/ioctl.h>
11+
#include <sys/stat.h>
12+
#include <sys/types.h>
13+
14+
// Local includes
15+
#include "AmlogicGrabber.h"
16+
17+
// Flags copied from 'linux/amlogic/amports/amvideocap.h' at https://github.com/codesnake/linux-amlogic/
18+
#define AMVIDEOCAP_IOC_MAGIC 'V'
19+
#define AMVIDEOCAP_IOW_SET_WANTFRAME_WIDTH _IOW(AMVIDEOCAP_IOC_MAGIC, 0x02, int)
20+
#define AMVIDEOCAP_IOW_SET_WANTFRAME_HEIGHT _IOW(AMVIDEOCAP_IOC_MAGIC, 0x03, int)
21+
22+
AmlogicGrabber::AmlogicGrabber(const unsigned width, const unsigned height) :
23+
_width(width),
24+
_height(height),
25+
_amlogicCaptureDev(-1)
26+
{
27+
_amlogicCaptureDev = open("/dev/amvideocap0", O_RDONLY, 0);
28+
if (_amlogicCaptureDev == -1)
29+
{
30+
std::cerr << "[" << __PRETTY_FUNCTION__ << "] Failed to open the AMLOGIC device (" << errno << ")" << std::endl;
31+
return;
32+
}
33+
34+
if (ioctl(_amlogicCaptureDev, AMVIDEOCAP_IOW_SET_WANTFRAME_WIDTH, _width) == -1 ||
35+
ioctl(_amlogicCaptureDev, AMVIDEOCAP_IOW_SET_WANTFRAME_HEIGHT, _height) == -1)
36+
{
37+
// Failed to configure frame width
38+
std::cerr << "[" << __PRETTY_FUNCTION__ << "] Failed to configure capture size (" << errno << ")" << std::endl;
39+
}
40+
}
41+
42+
AmlogicGrabber::~AmlogicGrabber()
43+
{
44+
if (_amlogicCaptureDev != -1)
45+
{
46+
if (close(_amlogicCaptureDev) == -1)
47+
{
48+
std::cerr << "[" << __PRETTY_FUNCTION__ << "] Failed to close AMLOGIC device (" << errno << ")" << std::endl;
49+
}
50+
_amlogicCaptureDev = -1;
51+
}
52+
}
53+
54+
void AmlogicGrabber::setVideoMode(const VideoMode videoMode)
55+
{
56+
switch (videoMode) {
57+
case VIDEO_3DSBS:
58+
//vc_dispmanx_rect_set(&_rectangle, 0, 0, _width/2, _height);
59+
break;
60+
case VIDEO_3DTAB:
61+
//vc_dispmanx_rect_set(&_rectangle, 0, 0, _width, _height/2);
62+
break;
63+
case VIDEO_2D:
64+
default:
65+
//vc_dispmanx_rect_set(&_rectangle, 0, 0, _width, _height);
66+
break;
67+
}
68+
}
69+
70+
void AmlogicGrabber::grabFrame(Image<ColorRgb> & image)
71+
{
72+
// resize the given image if needed
73+
if (image.width() != unsigned(_rectangle.width) || image.height() != unsigned(_rectangle.height))
74+
{
75+
image.resize(_rectangle.width, _rectangle.height);
76+
}
77+
78+
// Read the snapshot into the memory
79+
void * image_ptr = image.memptr();
80+
const size_t bytesToRead = _width * _height * sizeof(ColorRgb);
81+
const size_t bytesRead = pread(amlogicCaptureDev, image_ptr, bytesToRead, 0)
82+
if (bytesToRead != bytesRead)
83+
{
84+
// Read of snapshot failed
85+
std::cerr << "[" << __PRETTY_FUNCTION__ << "] Capture failed to grab entire image [bytesToRead(" << bytesToRead << ") != bytesRead(" << bytesRead << ")]" << std::endl;
86+
}
87+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#pragma once
2+
3+
// STL includes
4+
#include <cstdint>
5+
6+
// Utils includes
7+
#include <utils/Image.h>
8+
#include <utils/ColorRgba.h>
9+
#include <utils/VideoMode.h>
10+
11+
///
12+
/// The DispmanxFrameGrabber is used for creating snapshots of the display (screenshots) with a
13+
/// downsized and scaled resolution.
14+
///
15+
class AmlogicGrabber
16+
{
17+
public:
18+
///
19+
/// Construct a AmlogicGrabber that will capture snapshots with specified dimensions.
20+
///
21+
/// @param[in] width The width of the captured screenshot
22+
/// @param[in] height The heigth of the captured screenshot
23+
///
24+
AmlogicGrabber(const unsigned width, const unsigned height);
25+
~AmlogicGrabber();
26+
27+
///
28+
/// Set the video mode (2D/3D)
29+
/// @param[in] mode The new video mode
30+
///
31+
void setVideoMode(const VideoMode videoMode);
32+
33+
///
34+
/// Captures a single snapshot of the display and writes the data to the given image. The
35+
/// provided image should have the same dimensions as the configured values (_width and
36+
/// _height)
37+
///
38+
/// @param[out] image The snapped screenshot (should be initialized with correct width and
39+
/// height)
40+
///
41+
void grabFrame(Image<ColorRgba> & image);
42+
43+
private:
44+
45+
/// With of the captured snapshot [pixels]
46+
const unsigned _width;
47+
/// Height of the captured snapshot [pixels]
48+
const unsigned _height;
49+
50+
/** The snapshot/capture device of the amlogic video chip */
51+
int _amlogicCaptureDev;
52+
};

0 commit comments

Comments
 (0)