| 1 |
/* Copyright (C) 2008 Vincent Penquerc'h. |
| 2 |
This file is part of the Kate codec library. |
| 3 |
Written by Vincent Penquerc'h. |
| 4 |
|
| 5 |
Use, distribution and reproduction of this library is governed |
| 6 |
by a BSD style source license included with this source in the |
| 7 |
file 'COPYING'. Please read these terms before distributing. */ |
| 8 |
|
| 9 |
|
| 10 |
/** |
| 11 |
|
| 12 |
\page format_32v Format - variable length integer |
| 13 |
|
| 14 |
Small integers are often needed, and the Kate bitstream format has |
| 15 |
a way to store them using a fraction of a byte to save space. |
| 16 |
This format is simple on purpose, and is not optimal. However, it |
| 17 |
allows easy compression of bitstreams using lots of small integers. |
| 18 |
All 32 bit integers may be represented by this encoding. |
| 19 |
|
| 20 |
A variable length integer is stored in the following way: |
| 21 |
|
| 22 |
- if the integer is between 0 and 14, it is stored as 4 bits |
| 23 |
directly representing the integer's value. |
| 24 |
- otherwise, it is stored as a first 4 bit value of 15, followed |
| 25 |
by a single bit for its sign (1 if negative, 0 if positive), |
| 26 |
then a 5 bit number representing one less than the number of bits |
| 27 |
needed to represent the absolute value of that number, followed by |
| 28 |
the absolute value of the number itself on the number of bits needed |
| 29 |
to represent it. |
| 30 |
|
| 31 |
A few examples: |
| 32 |
|
| 33 |
- 0 is stored as 0000 (4 bits) |
| 34 |
|
| 35 |
- -1 is stored as 1111 1 00001 1 (11 bits) |
| 36 |
|
| 37 |
- 7 is stored as 0111 (4 bits) |
| 38 |
|
| 39 |
- 17 is stored as 1111 0 00100 10001 (15 bits) |
| 40 |
|
| 41 |
- -3 is stored as 1111 1 00010 11 (12 bits) |
| 42 |
|
| 43 |
- 1000 is stored as 1111 0 01001 11 1110 1000 (20 bits) |
| 44 |
|
| 45 |
- 842389532 is stored as 1111 0 11101 0011 0010 0011 0101 1101 1000 0001 1100 (42 bits) |
| 46 |
|
| 47 |
|
| 48 |
Simple alterations would allow even more compression for typical |
| 49 |
numbers (eg, offsetting the numbers to allow -1 (a common occurence) |
| 50 |
to be stored on 4 bits, and noticing that the first bit of the |
| 51 |
bitstream stored last for large numbers is always 1), but are left |
| 52 |
for a possible future bitstream change as this would mean either an |
| 53 |
incompatible change, or a use for very few values, making the change |
| 54 |
almost pointless. |
| 55 |
|
| 56 |
|
| 57 |
*/ |
| 58 |
|