# `BB.Camera.Format`
[🔗](https://github.com/beam-bots/bb_camera/blob/main/lib/bb/camera/format.ex#L5)

Which media formats `bb_camera` will carry: raw pixels, and nothing else.

`BB.Camera.Hub` keeps exactly one frame per camera and drops anything staler.
That policy is lossless for frames that stand alone and destructive for
frames that reference earlier ones, so the hub only carries frames that
stand alone. It goes further than that and only carries *raw* frames: a
driver decodes whatever its device produces - MJPEG, H.264, anything - before
handing a frame over. Every consumer then gets pixels it can use directly,
rather than each one decoding the same frame for itself, and a codec whose
frames depend on each other is the driver's problem rather than the hub's.

A format is `{:raw, pixel_format}`, where `pixel_format` is one of
`Membrane.RawVideo`'s - `:I420`, `:NV12`, `:YUY2`, `:RGB` and so on - so a
frame can be handed to a Membrane pipeline without translating anything.

## Packing

A raw frame's `data` is tightly packed: planes laid end to end in their
conventional order, rows with no padding between them. Its size is exactly
`Membrane.RawVideo.frame_size/3` for its format and dimensions. Hardware that
pads rows to an alignment has to have that padding stripped by its driver.

# `pixel_format`

```elixir
@type pixel_format() :: Membrane.RawVideo.pixel_format()
```

# `t`

```elixir
@type t() :: {:raw, pixel_format()}
```

# `assert`

```elixir
@spec assert(t(), term()) ::
  :ok | {:error, BB.Error.Invalid.Camera.UnsupportedFormat.t()}
```

Check a camera's format against what a caller asserted it would be.

`nil` asserts nothing.

# `frame_size`

```elixir
@spec frame_size(term(), pos_integer(), pos_integer()) ::
  {:ok, pos_integer()} | {:error, BB.Error.Invalid.Camera.UnsupportedFormat.t()}
```

The size in bytes of one frame in `format` at `width` x `height`, checking
along the way that this package will carry it.

Returns `{:error, %UnsupportedFormat{}}` with a `reason` of:

- `:encoded` - anything other than `{:raw, pixel_format}`. Decode it in the
  driver.
- `:unknown_pixel_format` - a raw format `Membrane.RawVideo` doesn't name.
- `:invalid_dimensions` - dimensions the pixel format can't take, such as an
  odd width for chroma-subsampled `:I420`.

# `independent_frames?`

```elixir
@spec independent_frames?(term()) :: boolean()
```

Whether every frame in this format decodes without reference to any other.

True of every raw format, and of nothing this package carries besides.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
