3

I use v4l2 to get video frame from camera using streaming io and need to do some calculation on the frame.

However, accessing the frame memory is 10 times slower than allocating a malloc'ed memory.

I guess the mmap'ed frame memory is not cacheable by cpu.

Here is the test code.

//mmap video buffers
struct v4l2_buffer buf;
CLEAR(buf);
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = i;
if (-1 == xioctl(_fdCamera, VIDIOC_QUERYBUF, &buf)) {
    ERROR("VIDIOC_QUERYBUF");
    goto error_querybuf;
}
_buffers[i].start = mmap(NULL, buf.length,
PROT_READ | PROT_WRITE,
MAP_SHARED, _fdCamera, buf.m.offset);


//use mmap'ed buffer to do some calculation
u16* frame=_buffers[0].start;

u64 sum=0;
u16* p=frame;
time[0]=GetMicrosecond64();
while(p!=frame+PIXELS){
    sum+=*p;
    p++;
}
time[1]=GetMicrosecond64();
printf("mmap:sum %lld,time %lld\n",sum, time[1] - time[0]);

//use a copy of data to do some calculation
u16* frame_copy=(u16*)malloc(PIXELS*2);
memcpy(frame_copy,frame,PIXELS*2);
sum=0;
p=frame_copy;
time[0]=GetMicrosecond64();
while(p!=frame_copy+PIXELS){
    sum+=*p;
    p++;
}
time[1]=GetMicrosecond64();
printf("malloc:sum %lld,time %lld\n",sum, time[1] - time[0]);

update:

I use s5pv210 with linux-2.6.35.The fimc_dev.c indicates the mmaped is uncached.

How to make the frame buffer memory support both DMA and cache?

static inline int fimc_mmap_cap(struct file *filp, struct vm_area_struct *vma)
{
    struct fimc_prv_data *prv_data =
                (struct fimc_prv_data *)filp->private_data;
    struct fimc_control *ctrl = prv_data->ctrl;
    u32 size = vma->vm_end - vma->vm_start;
    u32 pfn, idx = vma->vm_pgoff;

    vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
    vma->vm_flags |= VM_RESERVED;

    /*
     * page frame number of the address for a source frame
     * to be stored at.
     */
    pfn = __phys_to_pfn(ctrl->cap->bufs[idx].base[0]);

    if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED)) {
        fimc_err("%s: writable mapping must be shared\n", __func__);
        return -EINVAL;
    }

    if (remap_pfn_range(vma, vma->vm_start, pfn, size, vma->vm_page_prot)) {
        fimc_err("%s: mmap fail\n", __func__);
        return -EINVAL;
    }

    return 0;
}
Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
duwu891229
  • 31
  • 4
  • It makes no sense to cache a camera frame buffer. Imagine what happens if you do. The camera continues to update the frame buffer as the scene it's seeing changes, but the cache is never updated. You would have to invalidate the cache every X milliseconds to keep frame updates coming in. That would be bad. Imstead, memcpy or DMA away a copy of the framebuffer, and do all the calculations on the copy. – n. m. could be an AI Jun 16 '16 at 05:25

0 Answers0