Does v4l2 camera capture with mmap ring buffer make sense for tracking application
Asked Answered
D

0

6

I'm working on a v4l2 API for capturing images from a raw sensor on embedded platform. My capture routine is related to the example on [1]. The proposed method for streaming is using mmaped buffers as a ringbuffer.
For initialization, buffers (default = 4 buffers) are requested using ioctl with VIDIOC_REQBUFS identifier. Subsequently, they are queued using VIDIOC_QBUF. The entire streaming procedure is described here [2]. As soon as streaming starts, the driver fills the queued buffers with data. The timestamp of v4l2_buffer struct indicates the time of first byte captured which in my case results in a time interval of approximately 8.3 ms (=120fps) between buffers. So far so good.
Now what I would expect of a ring buffer is that new captures automatically overwrite older ones in a circular fashion. But this is not what happens. Only when a buffer is queued again (VIDIOC_QBUF) after it has been dequeued (VIDIOC_DQBUF) and processed (demosaic, tracking step,..), a new frame is assigned to a buffer. If I do meet the timing condition (processing < 8.3 ms) I don't get the latest captured frame when dequeuing but the oldest captured one (according to FIFO), so the one of 3x8.3 ms before the current one. If the timing condition is not met the time span gets even larger, as the buffers are not overwritten.

So I have several questions:
1. Does it even make sense for this tracking application to have a ring buffer as I don't really need history of frames? I certainly doubt that, but by using the proposed mmap method drivers mostly require a minimum amount of buffers to be requested.
2. Should a seperate thread continously DQBUF and QBUF to accomplish the buffer overwrite? How could this be accomplished?
3. As a workaround one could probably dequeue and requeue all buffers on every capture, but this doesn't sound right. Is there someone with more experience in real time capture and streaming who can point to the "proper" way to go?
4. Also currently, I'm doing the preprocessing step (demosaicing) between DQBUF and QBUF and the tracking step afterwards. Should the tracking step also be executed before QBUF is called again?

So the main code basically performs Capture() and Track() subsequently in a while loop. The Capture routine looks as follows:

cv::Mat v4l2Camera::Capture( size_t timeout ) { 

    fd_set fds;
    FD_ZERO(&fds);
    FD_SET(mFD, &fds);

    struct timeval tv;

    tv.tv_sec  = 0;
    tv.tv_usec = 0;

    const bool threaded = true; //false;

    // proper register settings
    this->format2registerSetting();
    //

    if( timeout > 0 )
    {
        tv.tv_sec  = timeout / 1000;
        tv.tv_usec = (timeout - (tv.tv_sec * 1000)) * 1000;
    }

    //
    const int result = select(mFD + 1, &fds, NULL, NULL, &tv);


    if( result == -1 ) 
    {
        //if (EINTR == errno)
        printf("v4l2 -- select() failed (errno=%i) (%s)\n", errno, strerror(errno));
        return cv::Mat();
    }
    else if( result == 0 )
    {
    if( timeout > 0 )
        printf("v4l2 -- select() timed out...\n");  
        return cv::Mat(); // timeout, not necessarily an error      (TRY_AGAIN)
    }

    // dequeue input buffer from V4L2
    struct v4l2_buffer buf;
    memset(&buf, 0, sizeof(v4l2_buffer));

    buf.type   = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    buf.memory = V4L2_MEMORY_MMAP;  //V4L2_MEMORY_USERPTR;

    if( xioctl(mFD, VIDIOC_DQBUF, &buf) < 0 )
    {
        printf("v4l2 -- ioctl(VIDIOC_DQBUF) failed (errno=%i) (%s)\n", errno, strerror(errno));
        return cv::Mat();
    }

    if( buf.index >= mBufferCountMMap )
    {
        printf("v4l2 -- invalid mmap buffer index (%u)\n", buf.index);
        return cv::Mat();
    }

    // emit ringbuffer entry
    printf("v4l2 -- recieved %ux%u video frame (index=%u)\n", mWidth, mHeight, (uint32_t)buf.index);

    void* image_ptr = mBuffersMMap[buf.index].ptr;

    // frame processing (& tracking step)
    cv::Mat demosaic_mat = demosaic(image_ptr,mSize,mDepth,1);

    // re-queue buffer to V4L2
    if( xioctl(mFD, VIDIOC_QBUF, &buf) < 0 )
        printf("v4l2 -- ioctl(VIDIOC_QBUF) failed (errno=%i) (%s)\n", errno, strerror(errno));

    return demosaic_mat;
}

As my knowledge is limited regarding capturing and streaming video I appreciate any help.

Diary answered 13/4, 2017 at 9:0 Comment(2)
Were you able to get a solution for this?Johannessen
Did you solve this yet ?Vengeance

© 2022 - 2024 — McMap. All rights reserved.