# raspberrypi/libcamera architecture

> Architecture diagram of the GitHub repository raspberrypi/libcamera, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/raspberrypi/libcamera
- Repository: https://github.com/raspberrypi/libcamera

## Overview

##### Project’s Main Components
- Core Library Functionality – Represented by the “include/libcamera” and “src/libcamera” directories, the core component is responsible for camera management, request scheduling, hardware abstraction, sensor interfacing, and supporting modules such as the software ISP and V4L2 device handling.
- IPA (Image Processing Algorithms) Modules – Under “src/ipa” (with several submodules like ipu3, mali-c55, rkisp1, rpi, simple, vimc) and matching include files, these algorithms process raw image data; they are designed as plugins and are tied into the core image pipeline.
- Platform/Hardware-Specific Layers – There are dedicated directories (for example, “src/android” and “include/android”) that show how libcamera extends or adapts camera functionality to Android systems (matching Android HAL APIs) as well as support for specific architectures (e.g., Raspberry Pi under “rpi” in IPA and “src/apps”).
- External Interfaces and Integration – The “src/gstreamer” directory supplies a GStreamer plugin, enabling streaming and multimedia pipelines. In addition, the Python layer (“src/py”) offers bindings and utilities for scripting and rapid development.
- Applications and Utilities – A set of camera applications exist (e.g., “src/apps/cam”, “src/apps/qcam”) that act as clients to the libcamera core. These illustrate how the core library is deployed in real-world scenarios.
- Build and Testing Infrastructure – The project uses Meson as its build system (see “meson.build”, “meson_options.txt”) and includes extensive testing components (found in “test”) to exercise its many layers.

##### Component Relationships and Interactions
- The core library manages camera hardware through abstraction layers (with common interfaces exposed through header files and the “src/libcamera” implementation). It provides APIs for camera control as well as communication with lower-level drivers (e.g., V4L2).
- The Android-specific components integrate with the core but also provide additional HAL layers so that the system can interact with Android’s camera stack.
- External clients (apps, GStreamer, Python bindings) interface with the core library, creating a clear separation between the camera operation engine and end-user applications.
- The build system orchestrates the integration of all these components using Meson and Ninja, which should be noted if including a “deployment/build” pipeline segment.

##### Architectural Patterns and Design Principles
- Modularity – The project is clearly modular, with each major function (hardware abstraction, image pipeline processing, application interfaces) implemented in segregated directories.
- Extensibility – With multiple IPA modules and multiple platform-specific adaptations (such as for rpi, ipu3, and Android), the architecture emphasizes plug-in extensibility.
- Clean Abstraction Layers – The headers in “include/libcamera” and “include/android” define clear APIs, while internal details reside in “src”. This separation is akin to an MVC or layered design where the view (applications) interacts through a controller (API) that in turn interacts with the device (model).
- Inter-Process Communication – For IPA modules and potentially some of the proxy components, there is a use of IPC constructs (e.g., “ipc_pipe”, “ipc_unixsocket”) which should be represented to show data/control flowing between processes.

##### Relevant External Technologies
- C++ (core language used), Meson/Ninja for building.
- Libraries: V4L2, GStreamer, Qt (for qcam), Python3 and pybind11 (for the Python bindings) and optionally Android libraries.
- External modules for image processing algorithms and media bus formats (see the “linux” directory with headers).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Sensor
    Sensor["Camera Sensor (Hardware)"]:::sensor

    %% Core Camera Library Subgraph
    subgraph "Core Camera Library"
        CoreLibInclude["Headers (include/libcamera)"]:::core
        CoreLibSrc["Implementation (src/libcamera)"]:::core
    end

    %% IPA Modules Subgraph
    subgraph "IPA Modules"
        IPAAlgo["Algorithms (src/ipa)"]:::ipa
        IPAHeaders["Headers (include/libcamera/ipa)"]:::ipa
    end

    %% Platform Specific Layers Subgraph
    subgraph "Platform Specific Layers"
        AndroidAdapter["Adapter (src/android)"]:::android
        AndroidHeaders["Headers (include/android)"]:::android
    end

    %% External Interfaces Subgraph
    subgraph "External Interfaces"
        GStreamer["GStreamer Plugin (src/gstreamer)"]:::external
        PythonBindings["Python Bindings (src/py)"]:::external
        Applications["Applications (src/apps)"]:::external
    end

    %% Build & Testing Infrastructure Subgraph
    subgraph "Build & Testing Infrastructure"
        MesonBuild["Meson Build (meson.build)"]:::build
        MesonOptions["Meson Options (meson_options.txt)"]:::build
        TestSuite["Test Suite (test)"]:::build
    end

    %% Data Flow Connections
    Sensor -->|"captures"| CoreLibInclude
    CoreLibInclude ---|"linked"| CoreLibSrc
    CoreLibInclude -->|"processes"| IPAAlgo
    CoreLibInclude -->|"interfaces"| AndroidAdapter
    CoreLibInclude -->|"provides_API"| GStreamer
    CoreLibInclude -->|"provides_API"| PythonBindings
    CoreLibInclude -->|"provides_API"| Applications

    IPAAlgo ---|"coupled"| IPAHeaders
    IPAAlgo -->|"enhances_data"| GStreamer
    IPAAlgo -->|"enhances_data"| PythonBindings
    IPAAlgo -->|"enhances_data"| Applications

    %% Build Infrastructure Dotted Connections
    MesonBuild -.-> CoreLibInclude
    MesonBuild -.-> IPAAlgo
    MesonBuild -.-> AndroidAdapter
    MesonBuild -.-> GStreamer
    MesonBuild -.-> PythonBindings
    MesonBuild -.-> Applications

    TestSuite -.-> CoreLibSrc
    TestSuite -.-> IPAHeaders
    TestSuite -.-> AndroidHeaders

    %% Styles for each category
    classDef sensor fill:#ccf,stroke:#333,stroke-width:2px;
    classDef core fill:#f9c,stroke:#333,stroke-width:2px;
    classDef ipa fill:#bbf,stroke:#333,stroke-width:2px;
    classDef android fill:#cfc,stroke:#333,stroke-width:2px;
    classDef external fill:#ffc,stroke:#333,stroke-width:2px;
    classDef build fill:#fcc,stroke:#333,stroke-width:2px;

    %% Click Events
    click CoreLibInclude "https://github.com/raspberrypi/libcamera/tree/main/include/libcamera"
    click CoreLibSrc "https://github.com/raspberrypi/libcamera/tree/main/src/libcamera"
    click IPAAlgo "https://github.com/raspberrypi/libcamera/tree/main/src/ipa"
    click IPAHeaders "https://github.com/raspberrypi/libcamera/tree/main/include/libcamera/ipa"
    click AndroidAdapter "https://github.com/raspberrypi/libcamera/tree/main/src/android"
    click AndroidHeaders "https://github.com/raspberrypi/libcamera/tree/main/include/android"
    click GStreamer "https://github.com/raspberrypi/libcamera/tree/main/src/gstreamer"
    click PythonBindings "https://github.com/raspberrypi/libcamera/tree/main/src/py"
    click Applications "https://github.com/raspberrypi/libcamera/tree/main/src/apps"
    click MesonBuild "https://github.com/raspberrypi/libcamera/blob/main/meson.build"
    click MesonOptions "https://github.com/raspberrypi/libcamera/blob/main/meson_options.txt"
    click TestSuite "https://github.com/raspberrypi/libcamera/tree/main/test"
```
