> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/bluenviron/gortsplib/llms.txt
> Use this file to discover all available pages before exploring further.

# pkg/format

> RTP payload format types with encoders and decoders.

Import path: `github.com/bluenviron/gortsplib/v5/pkg/format`

## Format interface

Every RTP payload format implements the `Format` interface:

```go theme={null}
type Format interface {
    // ClockRate returns the RTP clock rate in Hz.
    ClockRate() int

    // PayloadType returns the RTP payload type number.
    PayloadType() uint8

    // RTPMap returns the rtpmap SDP attribute value (e.g. "H264/90000").
    RTPMap() string

    // FMTP returns the fmtp SDP attribute as a key-value map.
    FMTP() map[string]string

    // PTSEqualsDTS reports whether PTS equals DTS for the given RTP packet.
    // Used to determine whether a packet carries a random-access point.
    PTSEqualsDTS(*rtp.Packet) bool

    // Codec returns a human-readable codec name.
    // Deprecated: arbitrary. Will be removed in the next version.
    Codec() string
}
```

### Method descriptions

<ParamField path="ClockRate" type="int">
  The RTP clock rate in Hz used for timestamp calculations. For example, `90000` for video
  formats and `48000` for Opus audio.
</ParamField>

<ParamField path="PayloadType" type="uint8">
  The RTP payload type number (0–127). Dynamic payload types are in the range 96–127.
  Some formats use static payload type numbers (e.g., PCMU is 0, PCMA is 8).
</ParamField>

<ParamField path="RTPMap" type="string">
  The `a=rtpmap` SDP attribute value. Returns an empty string for formats with static
  payload types where rtpmap is optional.
</ParamField>

<ParamField path="FMTP" type="map[string]string">
  The `a=fmtp` attribute as a parsed key-value map. Returns an empty map when no
  format parameters are needed.
</ParamField>

<ParamField path="PTSEqualsDTS" type="bool">
  Reports whether the presentation timestamp equals the decode timestamp for the given
  packet. Returns `true` on random-access points (e.g., H.264 IDR frames).
</ParamField>

***

## Format implementations

| Struct | Codec | Static payload type | Dynamic (96–127) | Encoder pkg | Decoder pkg |
| - | - | - | - | - | - |
| `H264` | H.264 / AVC | 35 (allowed) | Yes | `rtph264` | `rtph264` |
| `H265` | H.265 / HEVC | — | Yes | `rtph265` | `rtph265` |
| `AV1` | AV1 | — | Yes | `rtpav1` | `rtpav1` |
| `VP9` | VP9 | — | Yes | `rtpvp9` | `rtpvp9` |
| `VP8` | VP8 | — | Yes | `rtpvp8` | `rtpvp8` |
| `MPEG4Video` | MPEG-4 Part 2 video | — | Yes | — | — |
| `MPEG1Video` | MPEG-1/2 video | 32 | No | `rtpmpeg1video` | `rtpmpeg1video` |
| `MJPEG` | Motion JPEG | 26 | No | `rtpmjpeg` | `rtpmjpeg` |
| `Opus` | Opus / MultiOpus | — | Yes | `rtpsimpleaudio` | `rtpsimpleaudio` |
| `MPEG4Audio` | MPEG-4 Audio (Generic) | — | Yes | `rtpmpeg4audio` | `rtpmpeg4audio` |
| `MPEG4AudioLATM` | MPEG-4 Audio LATM | — | Yes | — | — |
| `Vorbis` | Vorbis | — | Yes | — | — |
| `AC3` | AC-3 | — | Yes | `rtpac3` | `rtpac3` |
| `G711` | G.711 PCMU/PCMA | 0, 8 | Yes | `rtpsimpleaudio` | `rtpsimpleaudio` |
| `G722` | G.722 | 9 | No | `rtpsimpleaudio` | `rtpsimpleaudio` |
| `G726` | G.726 (16/24/32/40 kbps) | — | Yes | — | — |
| `LPCM` | Linear PCM (L8/L16/L24) | 10, 11 | Yes | `rtplpcm` | `rtplpcm` |
| `MPEG1Audio` | MPEG-1/2 audio | 14 | No | `rtpmpeg1audio` | `rtpmpeg1audio` |
| `Speex` | Speex | — | Yes | — | — |
| `KLV` | SMPTE 336M KLV | — | Yes | `rtpklv` | `rtpklv` |
| `MPEGTS` | MPEG-TS | 33 | No | `rtpmpegts` | `rtpmpegts` |
| `Generic` | Unknown / fallback | any | any | — | — |

***

## Encoder and decoder pattern

Formats that carry complex framing (e.g., H.264 NAL unit packetization) provide
`CreateEncoder()` and `CreateDecoder()` factory methods. These return codec-specific
types from sub-packages under `pkg/format/rtp<codec>`.

### Encoding

```go theme={null}
encode, err := forma.CreateEncoder()
if err != nil { ... }

// Encode an access unit ([][]byte for H.264) into RTP packets
pkts, err := enc.Encode(accessUnit)
if err != nil { ... }

for _, pkt := range pkts {
    // send pkt over the network
}
```

### Decoding

```go theme={null}
dec, err := forma.CreateDecoder()
if err != nil { ... }

// Decode a single RTP packet into an access unit
au, pts, err := dec.Decode(pkt)
if err != nil { ... }
// use au ([][]byte for H.264)
```

***

## H264 struct

`H264` is the RTP format for the H.264 codec (RFC 6184).

```go theme={null}
type H264 struct {
    PayloadTyp        uint8
    SPS               []byte
    PPS               []byte
    PacketizationMode int
}
```

<ParamField path="PayloadTyp" type="uint8" required>
  RTP payload type number. Typically `96` for dynamic assignment. The field name is
  `PayloadTyp` (not `PayloadType`) to avoid a collision with the interface method.
</ParamField>

<ParamField path="SPS" type="[]byte">
  H.264 Sequence Parameter Set NAL unit (without Annex B start code). Serialized
  into the `sprop-parameter-sets` fmtp attribute.
</ParamField>

<ParamField path="PPS" type="[]byte">
  H.264 Picture Parameter Set NAL unit (without Annex B start code). Serialized
  alongside `SPS` in `sprop-parameter-sets`.
</ParamField>

<ParamField path="PacketizationMode" type="int">
  H.264 packetization mode (`0` = single NAL unit mode, `1` = non-interleaved mode).
  Serialized as the `packetization-mode` fmtp attribute. Defaults to `0`.
</ParamField>

### H264 methods

| Method | Signature | Description |
| - | - | - |
| `CreateEncoder` | `() (*rtph264.Encoder, error)` | Returns an H.264 RTP encoder initialized with the format's payload type and packetization mode. |
| `CreateDecoder` | `() (*rtph264.Decoder, error)` | Returns an H.264 RTP decoder. |
| `SafeSetParams` | `(sps []byte, pps []byte)` | Thread-safe setter for SPS and PPS parameters. |
| `SafeParams` | `() ([]byte, []byte)` | Thread-safe getter for SPS and PPS parameters. |

***

## Example with H264

```go theme={null}
import (
    "github.com/bluenviron/gortsplib/v5/pkg/format"
    "github.com/bluenviron/gortsplib/v5/pkg/format/rtph264"
)

// Declare the format
forma := &format.H264{
    PayloadTyp:        96,
    PacketizationMode: 1,
}

// Encoding
enc, err := forma.CreateEncoder()
if err != nil { ... }

// accessUnit is a [][]byte of H.264 NAL units
pkts, err := enc.Encode(accessUnit)
if err != nil { ... }

// Decoding
dec, err := forma.CreateDecoder()
if err != nil { ... }

au, pts, err := dec.Decode(pkt)
if err != nil { ... }
```

***

## Codec-specific encoder/decoder packages

Each sub-package under `pkg/format/rtp<codec>` exposes `Encoder` and `Decoder` structs
with codec-specific options:

| Sub-package | Codec |
| - | - |
| `pkg/format/rtph264` | H.264 |
| `pkg/format/rtph265` | H.265 |
| `pkg/format/rtpav1` | AV1 |
| `pkg/format/rtpvp8` | VP8 |
| `pkg/format/rtpvp9` | VP9 |
| `pkg/format/rtpmpeg4audio` | MPEG-4 Audio |
| `pkg/format/rtpac3` | AC-3 |
| `pkg/format/rtplpcm` | Linear PCM |
| `pkg/format/rtpklv` | KLV |
| `pkg/format/rtpmjpeg` | Motion JPEG |
| `pkg/format/rtpmpeg1audio` | MPEG-1 Audio |
| `pkg/format/rtpmpeg1video` | MPEG-1 Video |
| `pkg/format/rtpmpegts` | MPEG-TS |
| `pkg/format/rtpsimpleaudio` | G.711, G.722, Opus |

<Note>
  The `rtpfragmented` sub-package provides a generic fragmented-unit encoder/decoder
  used internally by several codec packages.
</Note>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.