Skip to content
Search

KLV

lastmod 2023-09-25created 2023-01-201 edits

KLV (Key-Length-Value) is a data encoding standard often used to embed information in video feeds. The standard uses a type-length-value encoding scheme. Items are encoded as Key-Length-Value triplets, where the key identifies the data, the length specifies the data's length, and the value is the data itself. It is defined in SMPTE 336M-2007 (Data Encoding Protocol Using Key-Length-Value), approved by the Society of Motion Picture and Television Engineers. Because of KLV's high degree of interoperability, it has also been adopted by the Motion Imagery Standards Board.

Byte packing

In a binary data stream, a KLV set is broken down in the following fashion, with all integer interpretation being big-endian.

Key field

The first few bytes are the key, much like a key in a standard hash table data structure. Keys can be 1, 2, 4, or 16 bytes in length. Presumably, a separate specification document would agree on the key length for a given application. Sixteen-byte keys are usually reserved for use as globally registered unique identifiers, and the value portion of such a packet usually contains a series of further KLV sets with smaller keys.

Length field

The bytes of the length field, which follow the bytes of the key, tell how many bytes follow the length field and make up the value portion. There are four kinds of encoding for the length field: 1-byte, 2-byte, 4-byte, and Basic Encoding Rules (BER). The 1-, 2-, and 4-byte variants are very simple: build an unsigned integer from the bytes, and that integer is the number of bytes that follow.

BER length encoding is a bit more complicated but the most flexible. If the first byte of the length field does not have the high bit set (0x80), that single byte represents an integer between 0 and 127 and indicates the number of value bytes that immediately follow. If the high bit is set, the lower seven bits indicate how many bytes make up the length field.

For example, if the first byte of a BER length field is binary 10000010, the next two bytes make up an integer that indicates how many Value bytes follow after them. A total of three bytes were therefore used to specify the length.

Value field

The remaining bytes are the value field, whose contents can be whatever you want, including, as is often the case, a chain of more KLV sets.

Example

In the following example, the four bytes represent a KLV set with a 1-byte key and a 1-byte length field (or BER; it cannot be told from the example), and the value is two bytes (0 and 3). The application would have previously agreed on a) using 1-byte keys and b) using 1-byte length encoding. Presumably the key value "42" also means something to you; perhaps it indicates that the value bytes 0x00 and 0x03 are an integer representing a bicycle's odometer reading.

Key

Length

Value

42

2

0

3

See also

Favorite site

author Zer0 sitemap