Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8015

Camera board • Libcamera with C++ RPi Cam v1.3 - Segfault

$
0
0
I am trying to use libcamera

Code:

sudo apt install libcamera-dev
in C++ with Qt to capture images on a RPi4 with a v1.3 camera. My config and output from running rpicam-hello is shown below.

/boot/firmware/config.txt

Code:

#camera_auto_detect=1#dtoverlay=vc4-kms-v3ddtoverlay=ov5647gpu_mem=128
rpicam-hello --list-cameras

Code:

 $ rpicam-hello --list-camerasAvailable cameras-----------------0 : ov5647 [2592x1944 10-bit GBRG] (/base/soc/i2c0mux/i2c@1/ov5647@36)    Modes: 'SGBRG10_CSI2P' : 640x480 [58.92 fps - (16, 0)/2560x1920 crop]                             1296x972 [46.34 fps - (0, 0)/2592x1944 crop]                             1920x1080 [32.81 fps - (348, 434)/1928x1080 crop]                             2592x1944 [15.63 fps - (0, 0)/2592x1944 crop]
rpicam-hello

Code:

 $ rpicam-hello[18:14:01.761813636] [6678]  INFO Camera camera_manager.cpp:326 libcamera v0.5.1                                                                                                                                                             +100-e53bdf1f[18:14:01.811701960] [6681]  WARN RPiSdn sdn.cpp:40 Using legacy SDN tuning - pl                                                                                                                                                             ease consider moving SDN inside rpi.denoise[18:14:01.813914595] [6681]  INFO RPI vc4.cpp:440 Registered camera /base/soc/i2                                                                                                                                                             c0mux/i2c@1/ov5647@36 to Unicam device /dev/media3 and ISP device /dev/media0[18:14:01.814039465] [6681]  INFO RPI pipeline_base.cpp:1107 Using configuration                                                                                                                                                              file '/usr/share/libcamera/pipeline/rpi/vc4/rpi_apps.yaml'Made DRM preview windowPreview window unavailableMode selection for 1296:972:12:P    SGBRG10_CSI2P,640x480/0 - Score: 3296    SGBRG10_CSI2P,1296x972/0 - Score: 1000    SGBRG10_CSI2P,1920x1080/0 - Score: 1349.67    SGBRG10_CSI2P,2592x1944/0 - Score: 1567Stream configuration adjusted[18:14:02.074541428] [6678]  INFO Camera camera.cpp:1205 configuring streams: (0                                                                                                                                                             ) 1296x972-YUV420/sYCC (1) 1296x972-SGBRG10_CSI2P/RAW[18:14:02.075026314] [6681]  INFO RPI vc4.cpp:615 Sensor: /base/soc/i2c0mux/i2c@                                                                                                                                                             1/ov5647@36 - Selected sensor format: 1296x972-SGBRG10_1X10 - Selected unicam fo                                                                                                                                                             rmat: 1296x972-pGAA#10 (0.00 fps) exp 26977.00 ag 2.00 dg 1.00#11 (30.01 fps) exp 29989.00 ag 2.38 dg 1.01#12 (30.00 fps) exp 29989.00 ag 2.38 dg 1.01#13 (30.01 fps) exp 29989.00 ag 2.31 dg 1.03...
I initialize the class Camera from my main application and then call Camera->open(). My application segfaults soon after queuing the request camera->queueRequest(request.get());. Sometimes it even prints "Request completed" from inside requestCompleted(libcamera::Request *request) first. I don't know what's causing this issue ?

Linking

Code:

INCLUDEPATH += /usr/include/libcamera/LIBS += -lcamera -lcamera-base
Camera.h

Code:

#ifndef CAMERA_H#define CAMERA_H#undef signals#undef slots#undef emit#undef foreach#include <libcamera/libcamera.h>using namespace std;class Camera{public:    Camera();    ~Camera();    bool open(); private:    std::shared_ptr<libcamera::Camera> camera;    std::unique_ptr<libcamera::CameraManager> cm;    std::unique_ptr<libcamera::FrameBufferAllocator> bufferAllocator;    void requestCompleted(libcamera::Request *request);};#endif // CAMERA_H
Camera.cpp

Code:

#include "Camera.h"#include <QDebug>Camera::Camera(){    cm = std::make_unique<libcamera::CameraManager>();    cm->start();}Camera::~Camera(){//Cleanup is here but not shown}bool Camera::open() {    //Get list of cameras    auto cameras = cm->cameras();    if (cameras.empty()){        cout << "ERROR: No cameras.";        cm->stop();        return false;    }    //Find camera    std::string cameraId = cameras[0]->id();    camera = cm->get(cameraId);    if(!camera){        cout << "ERROR: Cannot open camera";        cm->stop();        return false;    }    //Lock camera    camera->acquire();    //Get camera config    std::unique_ptr<libcamera::CameraConfiguration> config = camera->generateConfiguration({libcamera::StreamRole::Viewfinder});    if(!config || config->size() < 1){        cout << "ERROR: Cannot get configuration for camera";        cm->stop();        return false;    }    //Set camera config for preview    config->at(0).size.width = 640;    config->at(0).size.height= 480;    //Validate config    if (config->validate() == libcamera::CameraConfiguration::Invalid) {        cout << "ERROR: Cannot validate configuration for camera" ;        cm->stop();        return false;    }    //Apply config    camera->configure(config.get());    //Allocate framebuffers    bufferAllocator = std::make_unique<libcamera::FrameBufferAllocator>(camera);    for (libcamera::StreamConfiguration &cfg : *config) {        int ret = bufferAllocator->allocate(cfg.stream());        if (ret < 0) {            cout << "ERROR: Cannot allocate buffer for camera";            cm->stop();            return false;        }    }    //Connect to slot for frame preview    camera->requestCompleted.connect(this, &Camera::requestCompleted);    //Start camera    camera->start();    //Queue initial requests    for (libcamera::StreamConfiguration &cfg : *config) {        for (const std::unique_ptr<libcamera::FrameBuffer> &buffer : bufferAllocator->buffers(cfg.stream())){            std::unique_ptr<libcamera::Request> request = camera->createRequest();            if (!request){                cout << "ERROR: Cannot create request for camera";                cm->stop();                return false;            }            int ret = request->addBuffer(cfg.stream(), buffer.get());            if (ret < 0) {                cout << "ERROR: Cannot set buffer for camera";                cm->stop();                return false;            }            camera->queueRequest(request.get());        }    }    return true;}void Camera::requestCompleted(libcamera::Request *request){    qDebug() << "Request completed";        //Process image further and re-use request}
Capture.PNG

Statistics: Posted by RPH — Fri Aug 22, 2025 3:12 pm



Viewing all articles
Browse latest Browse all 8015

Trending Articles