KLV (Key-Length-Value) is a data encoding standard, often used to embed information in video feeds. Items are encoded into Key-Length-Value triplets, where key identifies the data, length specifies the data's length, and 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. Due to KLV's large degree of interoperability, it has also been adopted by the Motion Imagery Standards Board.
In a binary stream of data, a KLV set is broken down in the following fashion, with all integer-interpretation being Big Endian:
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 in a separate specification document you would agree on a 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 more KLV sets with smaller keys.
Following the bytes for the Key are bytes for the Length field which will tell you 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 pretty straightforward: make an unsigned integer out of 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 in the length field does not have the high bit set (0x80), then that single byte represents an integer between 0 and 127 and indicates the number of Value bytes that immediately follows. If the high bit is set, then the lower seven bits indicate how many bytes follow that themselves make up a length field.
For example if the first byte of a BER length field is binary 10000010, that would indicate that the next two bytes make up an integer that then indicates how many Value bytes follow. Therefore a total of three bytes were taken up to specify a length.
The remaining bytes are the Value field, and its contents can be whatever you like, including a chain of more KLV sets, as is often the case.
In the following example, the four bytes represent a KLV set where the key is one byte, the length field is one byte (or possibly BER - you cannot tell from the example), and the value is two bytes: a zero and a three. In your application you would have previously agreed to a) use one-byte keys and b) use one-byte length encoding. Also presumably the key value "42" would mean something to you, perhaps it indicates that the value bytes 0x00 and 0x03 are an integer representing the value of your bicycle's odometer.
- Type-length-value, the type of data-encoding of which KLV is an instance
- KLVLib - A C Library for KLV file I/O
- Purchase The KLV Standard (SMPTE 336M-2007)
- ITU version of the KLV specification (ITU-R Recommendation BT.1563) (free)
- A Java KLV library in the Public Domain
- A Commercial Implementation of the Codec
- A Commercial Implementation of the KLV encoder/decoder lib (C++)