od-bridge
C ABI bridge for od_opencv, designed for Go CGO integration (or any language with C FFI).
Wraps ONNX Runtime inference behind 5 flat C functions and POD (plain old data) structs. The header od_bridge.h is auto-generated by cbindgen on every build.
Table of Contents
- How it works
- Build
- Header generation (cbindgen)
- C API
- Usage from Go (CGO)
- Usage from Rust
- Running examples
- Features
- Memory management
- Why unsafe
- Pre-built binaries
- License
How it works
Caller (Go / C / Python / ...)
-> C ABI (od_bridge.h)
-> od-bridge (Rust)
-> od_opencv
-> ONNX Runtime (CPU or CUDA)
The caller provides raw RGB pixels (uint8, HWC layout). All image preprocessing (resize, normalize, CHW transpose) happens inside ORT, so there is no CPU-side format conversion overhead.
Build
Requires Rust 1.85+ (edition 2024).
# CPU only
# With CUDA execution provider
Output artifacts in target/release/:
| File | Use case |
|---|---|
libod_bridge.so |
Dynamic linking (Linux) |
libod_bridge.a |
Static linking |
libod_bridge.rlib |
Rust-to-Rust dependency |
The C header od_bridge.h is generated in the crate root on every build (see below).
Header generation (cbindgen)
The file od_bridge.h is not maintained by hand. It is regenerated automatically on every cargo build via a build script:
// build.rs
cbindgen inspects #[repr(C)] structs, enums, and extern "C" functions in src/lib.rs and produces a standard C header with typedefs, function prototypes, and doc comments. If you add or change a public FFI symbol, the header updates on the next build.
The cbindgen configuration lives in cbindgen.toml.
C API
Types
// Single detection result (flat POD, safe for memcpy).
typedef struct OdDetection OdDetection;
// Heap-allocated array of detections. Must be freed via od_detections_free().
typedef struct OdDetections OdDetections;
// Error codes.
typedef enum OdError OdError;
Functions
| Function | Feature | Description |
|---|---|---|
od_model_create(path, w, h) |
default | Load ONNX model (CPU). Returns ModelHandle* or NULL. |
od_model_create_cuda(path, w, h) |
cuda |
ONNX model with CUDA execution provider. |
od_model_create_tensorrt(path, w, h) |
tensorrt |
ONNX model with TensorRT execution provider. |
od_model_create_trt(engine_path) |
trt |
Serialized TensorRT engine (no input dims needed). |
od_model_create_rknn(path, num_classes) |
rknn |
RKNN model for Rockchip NPU. |
od_model_free(handle) |
default | Free a model handle. |
od_model_detect(handle, rgb, w, h, conf, nms, out) |
default | Run inference (any backend). Fills OdDetections. |
od_detections_free(detections) |
default | Free detection results. |
See od_bridge.h for full signatures and doc comments.
Usage from Go (CGO)
/*
#cgo LDFLAGS: -L/path/to/od-bridge/target/release -lod_bridge -lm -ldl -lpthread
#cgo CFLAGS: -I/path/to/od-bridge
#include "od_bridge.h"
#include <stdlib.h>
*/
import "C"
import "unsafe"
func main()
At runtime, set LD_LIBRARY_PATH to include the directory with libod_bridge.so:
LD_LIBRARY_PATH=/path/to/od-bridge/target/release
Usage from Rust
The crate also produces an rlib, so it can be used directly from Rust without FFI overhead:
use *;
use CString;
use ptr;
let path = new.unwrap;
let handle = unsafe ;
assert!;
let mut out = OdDetections ;
let rc = unsafe ;
assert_eq!;
// ... use results ...
unsafe
See examples/basics.rs and examples/bench.rs for complete examples.
Running examples
1. Download test data
# YOLOv4-tiny weights and config
# Test image
# COCO class names
All downloaded files (*.weights, *.cfg, *.names, *.jpg) are in .gitignore.
2. Convert to ONNX
The bridge works with ONNX models. Convert darknet weights using darknet2onnx.
Install darknet2onnx following the installation guide.
Convert:
See darknet2onnx README for all options (--opset, --format yolov5|yolov8, macOS/Windows binaries, etc.).
Prepared ONNX file is considered to be ignored by git.
3. Run
# Basic detection
# Benchmark
If everyting is fine they you should see output like:
# Basic run
Image: 768x576, 1327104 bytes
Model loaded: yolov4-tiny.onnx
Detections: 3
[0] class=dog(16) conf=87.3% bbox=(136, 205, 182x337)
[1] class=truck(7) conf=80.8% bbox=(463, 79, 240x91)
[2] class=bicycle(1) conf=61.0% bbox=(72, 100, 505x379)
Done.
# Benchmark run
Model: yolov4-tiny.onnx
Image: 768x576
Warmup: 3 iters
Benchmark: 100 iters
100 iters in 1.63s
avg = 16.35 ms/frame
61.2 FPS
Features
| Feature | Default | C function added | Backend |
|---|---|---|---|
| (none) | yes | od_model_create |
ONNX Runtime, CPU |
cuda |
no | od_model_create_cuda |
ONNX Runtime, CUDA EP |
tensorrt |
no | od_model_create_tensorrt |
ONNX Runtime, TensorRT EP |
trt |
no | od_model_create_trt |
Native TensorRT (serialized .engine file) |
rknn |
no | od_model_create_rknn |
Rockchip RKNN NPU (.rknn file) |
All backends share the same od_model_detect / od_model_free / od_detections_free functions. The ModelHandle dispatches to the correct runtime internally.
Note: od_model_create_trt takes only engine_path (no input dimensions, they are baked into the engine). od_model_create_rknn takes model_path and num_classes instead of input dimensions.
Memory management
od_model_create*allocates aModelHandleon the heap viaBox::into_raw. The caller owns it and must callod_model_freeexactly once.od_model_detectallocates the results array as aBox<[OdDetection]>and passes ownership to the caller via theOdDetectionsstruct. The caller must callod_detections_freeexactly once per successful detect call.- Passing NULL to any
_freefunction is a safe no-op. - Double-free is undefined behavior.
Why unsafe
Every public function in this crate is unsafe extern "C". This is not a design choice, it is a hard requirement of the C ABI: the whole point of the crate is to expose symbols callable from C, Go, Python, or any other language via FFI. Rust's safety guarantees end at the FFI boundary because the compiler cannot verify what the caller does with raw pointers.
Specifically, unsafe is required here for:
extern "C"functions. Rust 2024 edition requires#[unsafe(no_mangle)]and treats allextern "C"fn as unsafe by definition, since the caller is outside Rust's type system.- Raw pointer dereference. The caller passes
*const c_char,*const u8,*mut OdDetections, etc. Rust must trust that these point to valid memory of the correct size. Box::into_raw/Box::from_raw. Heap allocation is transferred across the FFI boundary. The Rust side gives up ownership (into_raw), the C side holds the pointer, and Rust reclaims it later (from_raw). There is no way to express this ownership transfer in safe Rust.slice::from_raw_parts. Building a slice from a raw pixel pointer requires trusting the caller-provided length.
All unsafe operations are confined to the FFI boundary layer. The actual inference logic inside od_opencv is safe Rust. The bridge does the minimum unsafe work needed to convert between C ABI conventions and Rust types.
Pre-built binaries
May be in future there will be pre-built libod_bridge.so / libod_bridge.a for Linux x86_64 in GitHub Releases. This way consumers don't need a Rust toolchain, but I don't have that much time to maintain it right now.