Added framerate decimation

Former-commit-id: 8f7eb09978aef5364cbab1f5dfe12afbf60e6cac
This commit is contained in:
johan 2014-01-12 14:18:27 +01:00
parent 558ad6a11f
commit 421ec6926f
3 changed files with 163 additions and 149 deletions

View File

@ -19,13 +19,16 @@
#define CLEAR(x) memset(&(x), 0, sizeof(x))
V4L2Grabber::V4L2Grabber(const std::string &device, int input, VideoStandard videoStandard, double fps) :
V4L2Grabber::V4L2Grabber(const std::string &device, int input, VideoStandard videoStandard, int frameDecimation, int pixelDecimation) :
_deviceName(device),
_ioMethod(IO_METHOD_MMAP),
_fileDescriptor(-1),
_buffers(),
_width(0),
_height(0)
_height(0),
_frameDecimation(frameDecimation),
_pixelDecimation(pixelDecimation),
_currentFrame(0)
{
open_device();
init_device(videoStandard, input);
@ -42,23 +45,24 @@ void V4L2Grabber::start()
start_capturing();
}
void V4L2Grabber::capture()
void V4L2Grabber::capture(int frameCount)
{
int count = 1;
while (count-- > 0) {
for (;;) {
for (int count = 0; count < frameCount || frameCount < 0; ++count)
{
for (;;)
{
// the set of file descriptors for select
fd_set fds;
struct timeval tv;
int r;
FD_ZERO(&fds);
FD_SET(_fileDescriptor, &fds);
/* Timeout. */
// timeout
struct timeval tv;
tv.tv_sec = 2;
tv.tv_usec = 0;
r = select(_fileDescriptor + 1, &fds, NULL, NULL, &tv);
// block until data is available
int r = select(_fileDescriptor + 1, &fds, NULL, NULL, &tv);
if (-1 == r)
{
@ -545,6 +549,10 @@ int V4L2Grabber::read_frame()
void V4L2Grabber::process_image(const void *p, int size)
{
if (++_currentFrame >= _frameDecimation)
{
// We do want a new frame...
if (size != 2*_width*_height)
{
std::cout << "Frame too small: " << size << " != " << (2*_width*_height) << std::endl;
@ -552,6 +560,8 @@ void V4L2Grabber::process_image(const void *p, int size)
else
{
process_image(reinterpret_cast<const uint8_t *>(p));
_currentFrame = 0; // restart counting
}
}
}

View File

@ -15,12 +15,12 @@ public:
};
public:
V4L2Grabber(const std::string & device, int input, VideoStandard videoStandard, double fps);
V4L2Grabber(const std::string & device, int input, VideoStandard videoStandard, int frameDecimation, int pixelDecimation);
virtual ~V4L2Grabber();
void start();
void capture();
void capture(int frameCount = -1);
void stop();
@ -73,4 +73,8 @@ private:
int _width;
int _height;
const int _frameDecimation;
const int _pixelDecimation;
int _currentFrame;
};

View File

@ -40,9 +40,9 @@ int main(int argc, char** argv)
return 1;
}
V4L2Grabber grabber("/dev/video0", 0, V4L2Grabber::PAL, 10.0);
V4L2Grabber grabber("/dev/video0", 0, V4L2Grabber::PAL, 25, 8);
grabber.start();
grabber.capture();
grabber.capture(250);
grabber.stop();
return 0;