# C API: Headless rendering and video out

A device of the library's own, with no window and no display: the best GPU, or a software rasteriser (lavapipe,
llvmpipe) where there is none. Pictures are straight RGBA8, row 0 at the top. Every call runs on the ctx's owner thread
and blocks until its pictures are read back. The guide, with the device order, the formats, the sample and the
measured gamerpics a second, is [Headless rendering and video](../../guides/headless.md).

Structs, each with `size` first (set by its `_defaults`; a smaller `size` is `SONA_E_ARGUMENT`): `sona_headless_options`
(64 bytes: `device` `SONA_DEVICE_AUTO`/`GPU`/`SOFTWARE`, `backend` 0/`VULKAN`/`GL`, `adapter` -1, `flags`
`SONA_HEADLESS_VALIDATION`, `batch_in_flight` 8 (at most 32), `wait_seconds` 60), `sona_headless_info` (440 bytes:
`backend`, `software`, `type`, `vendor_id`, `device_id`, `name[256]`, `driver[128]`), `sona_render_options` (104
bytes, 100 with 4-byte pointers: `width` and `height` 256, `preset` FULL, `yaw_deg`, `pitch_deg`, `zoom` 1,
`turn_deg`, `time`, `draw`, `flags` 0, `clip` NULL), `sona_sequence_options` (52 bytes: `frames` 0, `fps` 30,
`turn_deg`, `flags` `SONA_SEQUENCE_ORBIT`) and `sona_video_options` (80 bytes, 76: `format`, `width`, `height`, `fps`
30, `loops` 0, `quality` 0, `background` 0xff000000, `flags` `SONA_VIDEO_OPAQUE` / `SONA_VIDEO_NO_DITHER`, `colours`
0, `ffmpeg` NULL). The handles `sona_headless` and `sona_video` are opaque.

## sona_headless_defaults

```c
void sona_headless_defaults(sona_headless_options *o);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `o` | `sona_headless_options *` | The [`sona_headless_options`](../fields.md#sona_headless_options) to fill with the defaults | required |

**Returns** Nothing.

**Example**:

```c
sona_headless_options x_headless_options;
memset(&x_headless_options, 0, sizeof x_headless_options);
x_headless_options.size = sizeof x_headless_options;
sona_headless_defaults(&x_headless_options);
```

Fills the defaults above. NULL does nothing.

## sona_headless_devices

```c
uint32_t sona_headless_devices(const sona_headless_options *o, sona_headless_info *out, uint32_t cap);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `o` | `const sona_headless_options *` | The options, a [`sona_headless_options`](../fields.md#sona_headless_options) | `NULL`: the defaults |
| `out` | `sona_headless_info *` | Receives the [`sona_headless_info`](../fields.md#sona_headless_info) | `NULL` allowed (below) |
| `cap` | `uint32_t` | How many entries `out` holds | `NULL` allowed (below) |

**Returns** `uint32_t`.

**Example**:

```c
sona_headless_options x_headless_options;
memset(&x_headless_options, 0, sizeof x_headless_options);
x_headless_options.size = sizeof x_headless_options;
sona_headless_info x_headless_info2;
memset(&x_headless_info2, 0, sizeof x_headless_info2);
x_headless_info2.size = sizeof x_headless_info2;
uint32_t x_got = sona_headless_devices(&x_headless_options, &x_headless_info2, 1);
printf("sona_headless_devices: %lld\n", (long long)x_got);
```

The devices a headless renderer can open with `o`'s backend: Vulkan devices in the loader's order, then EGL devices.
Writes up to `cap` (`out` may be NULL) and returns how many there are. NULL `o`: the defaults; bad options: 0.

## sona_headless_open

```c
sona_result sona_headless_open(sona_ctx *ctx, const sona_headless_options *o, sona_headless **out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `sona_ctx *` | The context | required |
| `o` | `const sona_headless_options *` | The options, a [`sona_headless_options`](../fields.md#sona_headless_options) | `NULL`: the defaults |
| `out` | `sona_headless **` | Receives the new headless renderer | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_NOT_READY`, `SONA_E_UNSUPPORTED`, `SONA_E_MEMORY` ([what each means](../../troubleshooting.md#error-codes)).

**Example** (also in [Headless rendering and video out](../../guides/headless.md)):

```c
sona_headless *h;
sona_headless_open(ctx, NULL, &h);                 /* best GPU, else lavapipe or llvmpipe */
sona_render_options ro;
sona_render_defaults(&ro);                         /* 256 x 256, full body, transparent */
ro.preset = SONA_PRESET_BUST;
uint8_t *rgba = malloc(256 * 256 * 4);
sona_headless_render_manifest(h, manifest, &ro, rgba);   /* waits for the avatar, draws it, removes it */
```

Makes the device and a renderer on it, waiting up to `wait_seconds` for the ctx's shading data. NULL `o`: the defaults.
`SONA_E_UNSUPPORTED` when no device fits (the message says what was tried), `SONA_E_ARGUMENT` for bad options,
`SONA_E_NOT_READY` when the shading data did not load in time.

## sona_headless_close

```c
void sona_headless_close(sona_headless *h);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `h` | `sona_headless *` | The headless renderer | required |

**Returns** Nothing.

Frees the renderer and the device. NULL is fine. Close it before the ctx.

## sona_headless_info_get

```c
sona_result sona_headless_info_get(const sona_headless *h, sona_headless_info *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `h` | `const sona_headless *` | The headless renderer | required |
| `out` | `sona_headless_info *` | Receives the [`sona_headless_info`](../fields.md#sona_headless_info) | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT` ([what each means](../../troubleshooting.md#error-codes)).

The device it drew on. `SONA_E_ARGUMENT` for NULL arguments.

## sona_headless_renderer

```c
sona_renderer *sona_headless_renderer(const sona_headless *h);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `h` | `const sona_headless *` | The headless renderer | required |

**Returns** `sona_renderer *`.

Its renderer, for `sona_renderer_set_quality` and the like. Owned by `h`; do not destroy it.

## sona_render_defaults

```c
void sona_render_defaults(sona_render_options *o);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `o` | `sona_render_options *` | The [`sona_render_options`](../fields.md#sona_render_options) to fill with the defaults | required |

**Returns** Nothing.

**Example** (also in [Headless rendering and video out](../../guides/headless.md)):

```c
sona_headless *h;
sona_headless_open(ctx, NULL, &h);                 /* best GPU, else lavapipe or llvmpipe */
sona_render_options ro;
sona_render_defaults(&ro);                         /* 256 x 256, full body, transparent */
ro.preset = SONA_PRESET_BUST;
uint8_t *rgba = malloc(256 * 256 * 4);
sona_headless_render_manifest(h, manifest, &ro, rgba);   /* waits for the avatar, draws it, removes it */
```

256 x 256, `SONA_PRESET_FULL`, zoom 1, transparent, no clip. NULL does nothing.

## sona_headless_render

```c
sona_result sona_headless_render(sona_headless *h, sona_avatar *a, const sona_render_options *o, uint8_t *rgba_out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `h` | `sona_headless *` | The headless renderer | required |
| `a` | `sona_avatar *` | The avatar | required |
| `o` | `const sona_render_options *` | The options, a [`sona_render_options`](../fields.md#sona_render_options) | required |
| `rgba_out` | `uint8_t *` | Receives straight RGBA8, row 0 at the top: `width * height * 4` bytes | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_DESTROYED`, `SONA_E_NOT_READY`, `SONA_E_MEMORY`, `SONA_E_CONTEXT`, `SONA_E_READBACK` ([what each means](../../troubleshooting.md#error-codes)).

One picture of an avatar of the ctx, alone, after waiting for it to be READY. With `clip` set it plays that clip from
`time` (as `sona_avatar_play`, from then on) and is drawn at `time`; without, it is drawn as its clock stands. Its
placement, clock and pose are as before when the call returns. `rgba_out` holds `width * height * 4` bytes.
`SONA_E_ARGUMENT` for bad options, an item preview, an avatar of another ctx or a clip it does not have;
`SONA_E_NOT_READY` after `wait_seconds`; its own failure code when it FAILED.

## sona_headless_render_manifest

```c
sona_result sona_headless_render_manifest(sona_headless *h, const uint8_t manifest[SONA_MANIFEST_BYTES],
                                          const sona_render_options *o, uint8_t *rgba_out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `h` | `sona_headless *` | The headless renderer | required |
| `manifest` | `const uint8_t [SONA_MANIFEST_BYTES]` | The manifest's 1,000 bytes | required |
| `o` | `const sona_render_options *` | The options, a [`sona_render_options`](../fields.md#sona_render_options) | required |
| `rgba_out` | `uint8_t *` | Receives straight RGBA8, row 0 at the top: `width * height * 4` bytes | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_DESTROYED`, `SONA_E_NOT_READY`, `SONA_E_LIMIT`, `SONA_E_MEMORY`, `SONA_E_MANIFEST`, `SONA_E_CONTEXT` ([what each means](../../troubleshooting.md#error-codes)).

**Example** (also in [Headless rendering and video out](../../guides/headless.md)):

```c
sona_headless *h;
sona_headless_open(ctx, NULL, &h);                 /* best GPU, else lavapipe or llvmpipe */
sona_render_options ro;
sona_render_defaults(&ro);                         /* 256 x 256, full body, transparent */
ro.preset = SONA_PRESET_BUST;
uint8_t *rgba = malloc(256 * 256 * 4);
sona_headless_render_manifest(h, manifest, &ro, rgba);   /* waits for the avatar, draws it, removes it */
```

Adds the avatar (hidden, standing still or playing `clip` from `time`), draws it and removes it. Errors as above;
`SONA_E_MANIFEST` for a manifest that cannot be built, `SONA_E_LIMIT` when the ctx is full.

## sona_headless_batch

```c
sona_result sona_headless_batch(sona_headless *h, const uint8_t *manifests, uint32_t count,
                                const sona_render_options *o, sona_picture_fn fn, void *user);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `h` | `sona_headless *` | The headless renderer | required |
| `manifests` | `const uint8_t *` | The manifests, 1,000 bytes each, one after the other | required |
| `count` | `uint32_t` | How many entries the array holds | - |
| `o` | `const sona_render_options *` | The options, a [`sona_render_options`](../fields.md#sona_render_options) | required |
| `fn` | `sona_picture_fn` | Called with each picture as it is made | - |
| `user` | `void *` | Passed to the callback as it is | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_DESTROYED`, `SONA_E_NOT_READY`, `SONA_E_LIMIT`, `SONA_E_MEMORY`, `SONA_E_CONTEXT` ([what each means](../../troubleshooting.md#error-codes)).

`count` manifests, one after another, to pictures. `fn(user, index, result, rgba, width, height)` runs once for each,
in index order; `rgba` is NULL unless `result` is `SONA_OK` and lives until `fn` returns. `batch_in_flight` avatars
build ahead; each is removed once drawn. A failing manifest gives its code and the batch goes on. Returns `SONA_OK`, or
the error that stopped it (`SONA_E_CONTEXT`, `SONA_E_MEMORY`, a ctx-wide error). `SONA_E_ARGUMENT` for NULL `fn`.

## sona_sequence_defaults

```c
void sona_sequence_defaults(sona_sequence_options *o);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `o` | `sona_sequence_options *` | The [`sona_sequence_options`](../fields.md#sona_sequence_options) to fill with the defaults | required |

**Returns** Nothing.

**Example** (also in [Headless rendering and video out](../../guides/headless.md)):

```c
sona_sequence_options so;
sona_sequence_defaults(&so);
so.turn_deg = 360;
so.frames = 36;
sona_video_options vo;
sona_video_defaults(&vo);
vo.width = ro.width; vo.height = ro.height; vo.fps = 18;
sona_video *v;
sona_video_open(ctx, "turntable.gif", &vo, &v);
sona_headless_record(h, avatar, &ro, &so, v);
sona_video_finish(v, NULL, NULL);
```

`frames` 0, `fps` 30, no turn. NULL does nothing.

## sona_headless_sequence

```c
sona_result sona_headless_sequence(sona_headless *h, sona_avatar *a, const sona_render_options *r,
                                   const sona_sequence_options *s, sona_picture_fn fn, void *user);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `h` | `sona_headless *` | The headless renderer | required |
| `a` | `sona_avatar *` | The avatar | required |
| `r` | `const sona_render_options *` | The options, a [`sona_render_options`](../fields.md#sona_render_options) | required |
| `s` | `const sona_sequence_options *` | The options, a [`sona_sequence_options`](../fields.md#sona_sequence_options) | required |
| `fn` | `sona_picture_fn` | Called with each picture as it is made | - |
| `user` | `void *` | Passed to the callback as it is | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_NOT_READY`, `SONA_E_MEMORY` ([what each means](../../troubleshooting.md#error-codes)).

Frame `i` is the clip at its start + `i / fps` (the render options' `time` with a clip, else the avatar's clock) and the
turn `turn_deg * i / frames` (of the avatar, or of the camera with `SONA_SEQUENCE_ORBIT`). `frames` 0: the clip's
length at `fps`, rounded; 36 when nothing plays and `turn_deg` is set; else 1. `fn` gets every frame in order. The same
inputs give the same pixels every run. The avatar's clock and placement are put back after.

## sona_headless_record

```c
sona_result sona_headless_record(sona_headless *h, sona_avatar *a, const sona_render_options *r,
                                 const sona_sequence_options *s, sona_video *v);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `h` | `sona_headless *` | The headless renderer | required |
| `a` | `sona_avatar *` | The avatar | required |
| `r` | `const sona_render_options *` | The options, a [`sona_render_options`](../fields.md#sona_render_options) | required |
| `s` | `const sona_sequence_options *` | The options, a [`sona_sequence_options`](../fields.md#sona_sequence_options) | required |
| `v` | `sona_video *` | The video writer | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_NOT_READY`, `SONA_E_MEMORY` ([what each means](../../troubleshooting.md#error-codes)).

**Example** (also in [Headless rendering and video out](../../guides/headless.md)):

```c
sona_sequence_options so;
sona_sequence_defaults(&so);
so.turn_deg = 360;
so.frames = 36;
sona_video_options vo;
sona_video_defaults(&vo);
vo.width = ro.width; vo.height = ro.height; vo.fps = 18;
sona_video *v;
sona_video_open(ctx, "turntable.gif", &vo, &v);
sona_headless_record(h, avatar, &ro, &so, v);
sona_video_finish(v, NULL, NULL);
```

`sona_headless_sequence` into a video. `SONA_E_ARGUMENT` when the video's size is not the render size; a video
error stops it.

## sona_video_defaults

```c
void sona_video_defaults(sona_video_options *o);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `o` | `sona_video_options *` | The [`sona_video_options`](../fields.md#sona_video_options) to fill with the defaults | required |

**Returns** Nothing.

**Example** (also in [Headless rendering and video out](../../guides/headless.md)):

```c
sona_sequence_options so;
sona_sequence_defaults(&so);
so.turn_deg = 360;
so.frames = 36;
sona_video_options vo;
sona_video_defaults(&vo);
vo.width = ro.width; vo.height = ro.height; vo.fps = 18;
sona_video *v;
sona_video_open(ctx, "turntable.gif", &vo, &v);
sona_headless_record(h, avatar, &ro, &so, v);
sona_video_finish(v, NULL, NULL);
```

The defaults are `format` from the path, a frame rate of 30 fps, opaque black background, every other field 0. Set `width` and `height`.

## sona_video_open

```c
sona_result sona_video_open(sona_ctx *ctx, const char *path, const sona_video_options *o, sona_video **out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `sona_ctx *` | The context | required |
| `path` | `const char *` | A file path | required |
| `o` | `const sona_video_options *` | The options, a [`sona_video_options`](../fields.md#sona_video_options) | required |
| `out` | `sona_video **` | Receives the new video writer | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_UNSUPPORTED`, `SONA_E_MEMORY`, `SONA_E_IO` ([what each means](../../troubleshooting.md#error-codes)).

**Example** (also in [Headless rendering and video out](../../guides/headless.md)):

```c
sona_sequence_options so;
sona_sequence_defaults(&so);
so.turn_deg = 360;
so.frames = 36;
sona_video_options vo;
sona_video_defaults(&vo);
vo.width = ro.width; vo.height = ro.height; vo.fps = 18;
sona_video *v;
sona_video_open(ctx, "turntable.gif", &vo, &v);
sona_headless_record(h, avatar, &ro, &so, v);
sona_video_finish(v, NULL, NULL);
```

Starts a PNG sequence (the path holds one `%d`, `%u` or `%0Nd`), a GIF, or an MP4 or WebM through ffmpeg (started here).
NULL `path` keeps a GIF, MP4 or WebM in memory. `SONA_E_ARGUMENT` for bad options, a PNG path without one number,
or a NULL path with no format; `SONA_E_UNSUPPORTED` when ffmpeg cannot be run or has no fitting encoder (the message
says which); `SONA_E_IO` when the file cannot be made.

## sona_video_frame

```c
sona_result sona_video_frame(sona_video *v, const uint8_t *rgba);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `v` | `sona_video *` | The video writer | required |
| `rgba` | `const uint8_t *` | Straight RGBA8, row 0 at the top, `width * height * 4` bytes | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_MEMORY`, `SONA_E_IO` ([what each means](../../troubleshooting.md#error-codes)).

One frame of `width * height` straight RGBA8, row 0 at the top. `SONA_E_IO` when the file or ffmpeg failed; a GIF or ffmpeg
error sticks.

## sona_video_finish

```c
sona_result sona_video_finish(sona_video *v, uint8_t **out, size_t *out_len);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `v` | `sona_video *` | The video writer | required |
| `out` | `uint8_t **` | Receives the bytes; free them with `sona_free` | required |
| `out_len` | `size_t *` | Receives how many bytes `*out` holds | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_MEMORY`, `SONA_E_IO` ([what each means](../../troubleshooting.md#error-codes)).

**Example** (also in [Headless rendering and video out](../../guides/headless.md)):

```c
sona_sequence_options so;
sona_sequence_defaults(&so);
so.turn_deg = 360;
so.frames = 36;
sona_video_options vo;
sona_video_defaults(&vo);
vo.width = ro.width; vo.height = ro.height; vo.fps = 18;
sona_video *v;
sona_video_open(ctx, "turntable.gif", &vo, &v);
sona_headless_record(h, avatar, &ro, &so, v);
sona_video_finish(v, NULL, NULL);
```

Ends the file and frees `v`, also on error. For an in-memory video `*out` and `*out_len` (may be NULL) get the bytes,
freed with `sona_free`. `SONA_E_ARGUMENT` for a video with no frames; `SONA_E_IO` with ffmpeg's last line when it
failed.

## sona_video_formats

```c
uint32_t sona_video_formats(const char *ffmpeg);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ffmpeg` | `const char *` | The ffmpeg program; NULL: `ffmpeg` on the path | `NULL`: `ffmpeg` on the path |

**Returns** `uint32_t`.

**Example**:

```c
uint32_t x_got = sona_video_formats("example");
printf("sona_video_formats: %lld\n", (long long)x_got);
```

Bit `1u << format` for each format this machine can write: PNG and GIF always, MP4 and WebM when that ffmpeg (NULL:
`$SONA_FFMPEG`, else `ffmpeg` on the PATH) runs and has an encoder for them.
