You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

树莓派3B上libcamera返回帧数据mmap失败问题求助

树莓派3B上libcamera帧数据mmap失败问题排查

在树莓派3B上使用libcamera进行相机开发时,libcamera返回的帧数据由原始文件描述符(fd)、大小(size)和偏移量(offset)组成。按照官方文档要求,需要通过mmap访问这些帧数据,但执行mmap时会偶尔失败(通常在成功1-2次后触发)。

代码片段

void copy_plane_to_texture(const libcamera::FrameBuffer::Plane &plane, int w, int h, GLuint texture)
{
    glBindTexture(GL_TEXTURE_2D, texture);
    auto *addr = (unsigned char *)mmap(nullptr, plane.length, PROT_READ, MAP_PRIVATE, plane.fd.get(), plane.offset);
    if (addr == MAP_FAILED)
    {
        clog << "mmap failed for plane fd " << plane.fd.get() << " size " << plane.length << " offset " << plane.offset << ": " << strerror(errno) << endl;
        abort();
    }
    clog << "plane fd " << plane.fd.get() << " size " << plane.length << " offset " << plane.offset << " mapped to address " << std::hex << size_t(addr) << std::dec << endl;
    glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA, GL_UNSIGNED_BYTE, addr);
    glFinish(); // wait until copying is really finished
    if (munmap(addr, plane.length) != 0)
    {
        clog << "munmap failed on address " << std::hex << size_t(addr) << ": " << strerror(errno) << endl;
        abort();
    }
}

典型输出日志

plane fd 18 size 921600 offset 0 mapped to address 6b51e000
plane fd 18 size 230400 offset 921600 mapped to address 6b5c6000
mmap failed for plane fd 18 size 230400 offset 1152000: Invalid argument

错误原因参考

根据man 2 mmap,EINVAL错误仅在以下三种情况触发:

  • addr、length或offset不符合要求(例如过大,或未按页边界对齐)。
  • (Linux 2.6.12起)length为0。
  • flags未指定MAP_PRIVATE或MAP_SHARED,或同时指定了两者。

目前可以排除length为0和flags参数错误的情况,剩余可能是length或offset无效,但这些参数均由libcamera提供,无法手动修改。请问该如何解决这个错误?


内容的提问来源于stack exchange,提问作者jiandingzhe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 15:57:01