MOQT requires a variable-length integer encoding with the following properties:
-
The encoded length can be determined from the first encoded byte.
-
The range of 1 byte values is as large as possible.
-
All 64 bit numbers can be encoded.
The variable-length integer encoding uses the number of leading 1 bits of the first byte to indicate the length of the encoding in bytes. The remaining bits after the first 0 and subsequent bytes, if any, represent the integer value, encoded in network byte order.
Integers are encoded in 1 to 9 bytes and can encode up to 64 bit unsigned integers. The following table summarizes the encoding properties.
| Leading Bits | Length (bytes) | Usable Bits | Range |
|---|---|---|---|
| 0 | 1 | 7 | 0-127 |
| 10 | 2 | 14 | 0-16383 |
| 110 | 3 | 21 | 0-2097151 |
| 1110 | 4 | 28 | 0-268435455 |
| 11110 | 5 | 35 | 0-34359738367 |
| 111110 | 6 | 42 | 0-4398046511103 |
| 1111110 | 7 | 49 | 0-562949953421311 |
| 11111110 | 8 | 56 | 0-72057594037927935 |
| 11111111 | 9 | 64 | 0-18446744073709551615 |
The following table contains some example encodings:
| Byte Sequence | Decimal Value |
|---|---|
| 0x25 | 37 |
| 0x8025 | 37 |
| 0xbbbd | 15,293 |
| 0xed7f3e7d | 226,442,877 |
| 0xfaa1a0e403d8 | 2,893,212,287,960 |
| 0xfc8998abc66bc0 | 151,288,809,941,952 |
| 0xfefa318fa8e3ca11 | 70,423,237,261,249,041 |
| 0xffffffffffffffffff | 18,446,744,073,709,551,615 |
Variable-length integers do not need to be encoded using the minimum number of
bytes; any encoding length that can represent the value is valid. Note that, as
a result, the same numeric value can be represented by more than one byte
sequence. For example, the value 0 can be encoded as 0x00, 0x8000,
0xc00000, or any longer form.
- x (vi64):
-
Indicates that x holds an integer value using the variable-length encoding as described above.