Skip to content

letterbox.h

Aspect-preserving resize with constant padding, on the SP500 DSP.

This is the "letterbox" step that prepares a camera frame as a fixed-size model input. The source is scaled by r = min(dst_w/src_w, dst_h/src_h) so it fits the destination without distortion, and the remaining border is filled with a constant value.

The applied scale and the top-left padding are returned so detections can be mapped back to source coordinates:

src_x = (dst_x - result.pad_x) / result.scale;
src_y = (dst_y - result.pad_y) / result.scale;

Frames are 8-bit interleaved HWC with no row padding: a row is exactly width * channels bytes.

Example

sp500_dsp_open(0);

sp500_letterbox_params p;
sp500_letterbox_params_init(&p);
p.src_w = 1920; p.src_h = 1080;
p.dst_w = 640;  p.dst_h = 640;

sp500_buffer src = sp500_buffer_wrap(frame, 1920 * 1080 * 3);
sp500_buffer dst = sp500_buffer_alloc(640 * 640 * 3, SP500_ALIGN_NPU);

sp500_letterbox_result r;
if(sp500_letterbox(&src, &p, &dst, &r) == SP500_OK)
{
    // dst.data holds the 640x640x3 model input; r.scale / r.pad_x / r.pad_y
    // map detections back to the 1920x1080 frame.
}
sp500_buffer_release(&dst);

Data structures

Name Description
sp500_letterbox_params Letterbox geometry and options.
sp500_letterbox_result Values the DSP computed for a letterbox call.

Enumerations

(anonymous enum)

enum

Interpolation used by the resize, for sp500_letterbox_params::interp.

Value Description
SP500_RESIZE_NEAREST = 0 nearest neighbour
SP500_RESIZE_BILINEAR = 1 bilinear (the default)
SP500_RESIZE_BICUBIC = 2 bicubic, a = -0.75
SP500_RESIZE_LANCZOS = 3 Lanczos4, 8 tap

Functions

sp500_letterbox_params_init

void sp500_letterbox_params_init(sp500_letterbox_params *params)

Fill params with the defaults documented above.

sp500_letterbox

sp500_status sp500_letterbox(const sp500_buffer *src, const sp500_letterbox_params *params, sp500_buffer *dst, sp500_letterbox_result *result)

Letterbox an 8-bit HWC frame on the DSP.

Parameters

  • src — Source frame; at least src_w * src_h * channels bytes.
  • params — Geometry and options; see sp500_letterbox_params.
  • dst — Destination frame; at least dst_w * dst_h * channels bytes. Allocating it with sp500_buffer_alloc() lets the NPU consume the model input directly, with no copy.
  • result — Receives the scale and padding. May be NULL if you do not need to map coordinates back.

Returns

SP500_OK on success. SP500_ERR_INVALID_ARG for a bad parameter, SP500_ERR_BUFFER_TOO_SMALL for an undersized frame, SP500_ERR_UNSUPPORTED for an unknown interpolation, SP500_ERR_NOT_READY if sp500_dsp_open() has not succeeded, SP500_ERR_TIMEOUT or SP500_ERR_DSP if the DSP call failed. result is only meaningful when the call returned SP500_OK.