chapter 3: multimedia data compression
TRANSCRIPT
Chapter 3:Multimedia Data
Compression•Compression with loss and lossless
•Huffman coding
•Entropy coding
•Adaptive coding
•Dictionary-based coding(LZW)
1chapter3: Multimedia Compression
Data Compression
• Branch of information theory
– minimize amount of information to be transmitted
• Transform a sequence of characters into a new string of bits
– same information content
– length as short as possible
chapter3: Multimedia Compression
2
Why Compress
• Raw data are huge.
• Audio:CD quality music44.1kHz*16bit*2 channel=1.4Mbps
• Video:near-DVD quality true color animation640px*480px*30fps*24bit=220Mbps
• Impractical in storage and bandwidth
3chapter3: Multimedia Compression
chapter3: Multimedia Compression 4
CompressionGraphic file formats can be regarded as being of three types.
• The first type stores graphics uncompressed.
– Windows BMP (.bmp) files are an example of this sort offormat.
• The second type is called "non-lossy“ or “lossless”compression formats.
– Most graphic formats use lossless compression - the GIFformats are among them.
• The third type of bitmapped graphic file formats is called"lossy" compression.
– the details are what prevent areas from being all the samecolor, and as such from responding well to compression.
– perhaps too subtle to be discernable by your eye
Lossless is not enough!
• The best lossless audio and imagecompression ratio is normally a half
• Lossy audio compression like mp3 or oggachieve 1/20 ratio while remainacceptable quality, and 1/5 ratio forperfect quality
• Lossy video compression reduce a film to1/300 size
5chapter3: Multimedia Compression
Lossy Compression
• Massively reduce information we don’tnotice
• Highly content specific
• Psychology
6chapter3: Multimedia Compression
Lossy Audio Compression
• Frequency domain
• Quantization– The importance varies in bands
– Higher frequency, larger quantum
• Psychoacoustics– Pitch resolution of ear is only 2Hz without
beating
– Threshold of hearing varies in bands
– Simultaneous and temporal masking effect
7chapter3: Multimedia Compression
Lossy Image Compression
• Frequency domain
– Discrete Cosine Transform (in Jpeg)
– Discrete Wavelet Transform (in J2k)
• Quantization
– Reduce less important data
Transform QuantizationEntropy
Coding
Image
data
Output
data
8chapter3: Multimedia Compression
Broad Classification• Entropy Coding (statistical)
– lossless; independent of data characteristics
– e.g. RLE( Run Length Encoding), Huffman, LZW, Arithmetic coding
• Source Coding– lossy; may consider semantics of the data
– depends on characteristics of the data
– e.g. DCT, DPCM, ADPCM, color model transform
• Hybrid Coding (used by most multimedia systems)– combine entropy with source encoding
– e.g., JPEG-2000, H.264, MPEG-2, MPEG-4, MPEG-7
chapter3: Multimedia Compression
9
Huffman Coding• Huffman codes can be used to compress
information– Like WinZip – although WinZip doesn’t use the
Huffman algorithm
– JPEGs do use Huffman as part of their compressionprocess
• The basic idea is that instead of storing eachcharacter in a file as an 8-bit ASCII value, we willinstead store the more frequently occurringcharacters using fewer bits and less frequentlyoccurring characters using more bits– On average this should decrease the filesize (usually ½)
10chapter3: Multimedia Compression
Huffman Coding
• As an example, lets take the string:
“duke blue devils”
• We first to a frequency count of the characters:• e:3, d:2, u:2, l:2, space:2, k:1, b:1, v:1, i:1, s:1
• Next we use a Greedy algorithm to build up a Huffman Tree
– We start with nodes for each character
e,3 d,2 u,2 l,2 sp,2 k,1 b,1 v,1 i,1 s,1
11chapter3: Multimedia Compression
Huffman Coding
• We then pick the nodes with the smallestfrequency and combine them together toform a new node– The selection of these nodes is the Greedy part
• The two selected nodes are removed fromthe set, but replace by the combined node
• This continues until we have only 1 nodeleft in the set
12chapter3: Multimedia Compression
Huffman Coding
e,3 d,2 u,2 l,2 sp,2 k,1 b,1 v,1 i,1 s,1
13chapter3: Multimedia Compression
Huffman Coding
e,3 d,2 u,2 l,2 sp,2 k,1 b,1 v,1
i,1 s,1
2
14chapter3: Multimedia Compression
Huffman Coding
e,3 d,2 u,2 l,2 sp,2 k,1
b,1 v,1 i,1 s,1
22
15chapter3: Multimedia Compression
Huffman Coding
e,3 d,2 u,2 l,2 sp,2
k,1 i,1 s,1
2
b,1 v,1
2
3
16chapter3: Multimedia Compression
Huffman Coding
e,3 d,2 u,2
l,2 sp,2 k,1 i,1 s,1
2
b,1 v,1
2
34
17chapter3: Multimedia Compression
Huffman Coding
e,3
d,2 u,2 l,2 sp,2 k,1 i,1 s,1
2
b,1 v,1
2
344
18chapter3: Multimedia Compression
Huffman Coding
e,3
d,2 u,2 l,2 sp,2
k,1i,1 s,1
2
b,1 v,1
2
3
44 5
19chapter3: Multimedia Compression
Huffman Coding
e,3
d,2 u,2
l,2 sp,2
k,1i,1 s,1
2
b,1 v,1
2
3
4
4
57
20chapter3: Multimedia Compression
Huffman Coding
e,3
d,2 u,2 l,2 sp,2
k,1i,1 s,1
2
b,1 v,1
2
3
44 5
7 9
21chapter3: Multimedia Compression
Huffman Coding
e,3
d,2 u,2 l,2 sp,2
k,1i,1 s,1
2
b,1 v,1
2
3
44 5
7 9
16
22chapter3: Multimedia Compression
Huffman Coding
• Now we assign codes to the tree byplacing a 0 on every left branch and a 1 onevery right branch
• A traversal of the tree from root to leafgive the Huffman code for that particularleaf character
• Note that no code is the prefix of anothercode
23chapter3: Multimedia Compression
Huffman Coding
e,3
d,2 u,2 l,2 sp,2
k,1i,1 s,1
2
b,1 v,1
2
3
44 5
7 9
16
e 00
d 010
u 011
l 100
sp 101
i 1100
s 1101
k 1110
b 11110
v 11111
24chapter3: Multimedia Compression
Huffman Coding
• These codes are then used to encode the string
• Thus, “duke blue devils” turns into:010 011 1110 00 101 11110 100 011 00 101 010 00 11111 1100 100 1101
• When grouped into 8-bit bytes:01001111 10001011 11101000 11001010 10001111 11100100 1101xxxx
• Thus it takes 7 bytes of space compared to 16characters * 1 byte/char = 16 bytesuncompressed
25chapter3: Multimedia Compression
Huffman Coding• Uncompressing works by reading in the file bit
by bit– Start at the root of the tree– If a 0 is read, head left– If a 1 is read, head right– When a leaf is reached decode that character and start
over again at the root of the tree
• Thus, we need to save Huffman tableinformation as a header in the compressed file– Doesn’t add a significant amount of size to the file for
large files (which are the ones you want to compressanyway)
– Or we could use a fixed universal set ofcodes/freqencies
26chapter3: Multimedia Compression
Entropy Coding Algorithms
(Content Dependent Coding)
• Run-length Encoding (RLE)
– Replaces sequence of the same consecutive bytes with number of occurrences
– Number of occurrences is indicated by a special flag (e.g., !)
– Example:
• abcccccccccdeffffggg (20 Bytes)
• abc!9def!4ggg (13 bytes)
chapter3: Multimedia Compression
27
Variations of RLE (Zero-suppression
technique)
• Assumes that only one symbol appearsoften (blank)
• Replace blank sequence by M-byte and abyte with number of blanks in sequence
– Example: M3, M4, M14,…
• Some other definitions are possible
– Example:
• M4 = 8 blanks, M5 = 16 blanks, M4M5=24 blanks
chapter3: Multimedia Compression
28
29
Adaptive Coding
Motivations:– The previous algorithms (Huffman) require the statistical
knowledge which is often not available (e.g., live audio, video).
– Even when it is available, it could be a heavy overhead.
– Higher-order models incur more overhead. For example, a 255entry probability table would be required for a 0-order model. Anorder-1 model would require 255 such probability tables. (Aorder-1 model will consider probabilities of occurrences of 2symbols)
The solution is to use adaptive algorithms. AdaptiveHuffman Coding is one such mechanism that we willstudy.
The idea of “adaptiveness” is however applicable to otheradaptive compression algorithms.
chapter3: Multimedia Compression
30
Adaptive Coding
ENCODER
Initialize_model();
do {
c = getc( input );
encode( c, output );
update_model( c );
} while ( c != eof)
DECODER
Initialize_model();
while ( c = decode (input)) != eof) {
putc( c, output)
update_model( c );
}
r The key is that, both encoder and decoder use exactly the same initialize_model and update_model routines.
chapter3: Multimedia Compression
31
The Sibling Property
The node numbers will be assigned in such a waythat:
1. A node with a higher weight will have a higher nodenumber
2. A parent node will always have a higher node numberthan its children.
In a nutshell, the sibling property requires that thenodes (internal and leaf) are arranged in order ofincreasing weights.
The update procedure swaps nodes in violation ofthe sibling property.– The identification of nodes in violation of the sibling
property is achieved by using the notion of a block.
– All nodes that have the same weight are said to belongto one block
chapter3: Multimedia Compression
32
Flowchart of the update procedure
START
First appearance of symbol
Go to symbol external node
Node number max
in block?
Incrementnode weight
Switch node withhighest numbered
node in block
Is thisthe root
node?
Go toparent node
STOP
NYT gives birthTo new NYT and
external node
Increment weight of external node
and old NYT node; Adjust node
numbers
Go to oldNYT node
Yes
No
No
Yes
Yes
No
The Huffman tree is
initialized with a single
node, known as the Not-
Yet-Transmitted (NYT) or
escape code. This code
will be sent every time
that a new character,
which is not in the tree,
is encountered, followed
by the ASCII encoding of
the character. This allows
for the de-compressor
to distinguish between a
code and a new
character. Also, the
procedure creates a new
node for the character chapter3: Multimedia Compression
33
Example
BW=2#1
CW=2#2
DW=2#3
W=2#4
W=4#5
W=6#6
EW=10#7
Root W=16#8
Counts:(number of occurrences)
B:2C:2D:2E:10
Example Huffman tree after some symbols have been processed in accordance
with the sibling property
NYT
NYT
#0
Initial Huffman Tree
#0
chapter3: Multimedia Compression
34
Example
W=1#2
BW=2#3
CW=2#4
DW=2#5
W=2+1#6
W=4#7
W=6+1#8
EW=10#9
Root W=16+1
#10
Counts:(number of occurrences)
A:1B:2C:2D:2E:10
A Huffman tree after first appearance of symbol A
AW=1#1
NYT
#0
chapter3: Multimedia Compression
35
Increment
BW=2#3
CW=2#4
DW=2#5
W=3+1#6
W=4#7
W=7+1#8
EW=10#9
Root W=17+1
#10
Counts:A:1+1
B:2C:2D:2E:10
An increment in the count for A propagates up to the root
W=1+1#2
AW=1+1
#1
NYT
#0
chapter3: Multimedia Compression
36
Swapping
BW=2#3
CW=2#4
DW=2#5
W=4#6
W=4#7
W=8#8
EW=10#9
Root W=18#10
Counts:A:2+1
B:2C:2D:2E:10
BW=2#3
CW=2#4
AW=2+1
#5
W=4#6
W=4+1#7
W=8+1#8
EW=10#9
Root W=18+1
#10
Counts:A:3B:2C:2D:2E:10
Swap nodes 1 and 5
Another increment in the count for A results in swap
W=2#2
AW=2#1
NYT
W=2#2
DW=2#1
NYT
#0
#0chapter3: Multimedia Compression
37
Swapping … contd.
BW=2#3
CW=2#4
AW=3+1
#5
W=4#6
W=5+1#7
W=9+1#8
EW=10#9
Root W=19+1
#10
Counts:A:3+1
B:2C:2D:2E:10
Another increment in the count for A propagates up
W=2#2
DW=2#1
NYT
#0
chapter3: Multimedia Compression
38
Swapping … contd.
BW=2#3
CW=2#4
AW=4#5
W=4#6
W=6#7
W=10#8
EW=10#9
Root W=20#10
Counts:A:4+1
B:2C:2D:2E:10
Swap nodes 5 and 6
Another increment in the count for A causes swap of sub-tree
W=2#2
DW=2#1
NYT
#0
chapter3: Multimedia Compression
39
Swapping … contd.
CW=2#4
W=6#7
W=10#8
EW=10#9
Root W=20#10
Counts:A:4+1
B:2C:2D:2E:10
BW=2#3
W=4#5
AW=4+1
#6
Swap nodes 8 and 9
Further swapping needed to fix the tree
W=2#2
DW=2#1
NYT
#0
chapter3: Multimedia Compression
40
Swapping … contd.
CW=2#4
W=6#7
W=10+1#9
Root W=20+1
#10
Counts:A:5B:2C:2D:2E:10
BW=2#3
W=4#5
AW=5#6
EW=10#8
W=2#2
DW=2#1
NYT
#0
chapter3: Multimedia Compression
Lempel-Ziv-Welch (LZW) Compression Algorithm
Introduction to the LZW Algorithm
Example 1: Encoding using LZW
Example 2: Decoding using LZW
LZW: Concluding Notes
41chapter3: Multimedia Compression
Introduction to LZW
As mentioned earlier, static coding schemes requiresome knowledge about the data before encoding takesplace.
Universal coding schemes, like LZW, do not requireadvance knowledge and can build such knowledge on-the-fly.
LZW is the foremost technique for general purpose datacompression due to its simplicity and versatility.
It is the basis of many PC utilities that claim to “doublethe capacity of your hard drive”
LZW compression uses a code table, with 4096 as acommon choice for the number of table entries.
42chapter3: Multimedia Compression
Introduction to LZW (cont'd)
Codes 0-255 in the code table are always assigned torepresent single bytes from the input file.
When encoding begins the code table contains only thefirst 256 entries, with the remainder of the table beingblanks.
Compression is achieved by using codes 256 through4095 to represent sequences of bytes.
As the encoding continues, LZW identifies repeatedsequences in the data, and adds them to the code table.
Decoding is achieved by taking each code from thecompressed file, and translating it through the code tableto find what character or characters it represents.
43chapter3: Multimedia Compression
LZW Encoding Algorithm
1 Initialize table with single character strings
2 P = first input character
3 WHILE not end of input stream
4 C = next input character
5 IF P + C is in the string table
6 P = P + C
7 ELSE
8 output the code for P
9 add P + C to the string table
10 P = C
11 END WHILE
12 output code for P
44chapter3: Multimedia Compression
Example 1: Compression using LZW
Example 1: Use the LZW algorithm to compress the string
BABAABAAA
45chapter3: Multimedia Compression
Example 1: LZW Compression Step 1
BABAABAAA P=A
C=empty
STRING TABLEENCODER OUTPUT
stringcodewordrepresentingoutput code
BA256B66
46chapter3: Multimedia Compression
Example 1: LZW Compression Step 2
BABAABAAA P=B
C=empty
STRING TABLEENCODER OUTPUT
stringcodewordrepresentingoutput code
BA256B66
AB257A65
47chapter3: Multimedia Compression
Example 1: LZW Compression Step 3
BABAABAAA P=A
C=empty
STRING TABLEENCODER OUTPUT
stringcodewordrepresentingoutput code
BA256B66
AB257A65
BAA258BA256
48chapter3: Multimedia Compression
Example 1: LZW Compression Step 4
BABAABAAA P=A
C=empty
STRING TABLEENCODER OUTPUT
stringcodewordrepresentingoutput code
BA256B66
AB257A65
BAA258BA256
ABA259AB257
49chapter3: Multimedia Compression
Example 1: LZW Compression Step 5
BABAABAAA P=A
C=A
STRING TABLEENCODER OUTPUT
stringcodewordrepresentingoutput code
BA256B66
AB257A65
BAA258BA256
ABA259AB257
AA260A65
50chapter3: Multimedia Compression
Example 1: LZW Compression Step 6
BABAABAAA P=AA
C=empty
STRING TABLEENCODER OUTPUT
stringcodewordrepresentingoutput code
BA256B66
AB257A65
BAA258BA256
ABA259AB257
AA260A65
AA260
51chapter3: Multimedia Compression
LZW Decompression
The LZW decompressor creates the same string tableduring decompression.
It starts with the first 256 table entries initialized tosingle characters.
The string table is updated for each character in theinput stream, except the first one.
Decoding achieved by reading codes and translatingthem through the code table being built.
52chapter3: Multimedia Compression
LZW Decompression Algorithm
1 Initialize table with single character strings
2 OLD = first input code
3 output translation of OLD
4 WHILE not end of input stream
5 NEW = next input code
6 IF NEW is not in the string table
7 S = translation of OLD
8 S = S + C
9 ELSE
10 S = translation of NEW
11 output S
12 C = first character of S
13 OLD + C to the string table
14 OLD = NEW
15 END WHILE
53chapter3: Multimedia Compression
Example 2: LZW Decompression 1
Example 2: Use LZW to decompress the output sequence of
Example 1:
<66><65><256><257><65><260>.
54chapter3: Multimedia Compression
Example 2: LZW Decompression Step 1
<66><65><256><257><65><260> Old = 65 S = A
New = 66 C = A
STRING TABLEENCODER OUTPUT
stringcodewordstring
B
BA256A
55chapter3: Multimedia Compression
Example 2: LZW Decompression Step 2
<66><65><256><257><65><260> Old = 256 S = BA
New = 256 C = B
STRING TABLEENCODER OUTPUT
stringcodewordstring
B
BA256A
AB257BA
56chapter3: Multimedia Compression
Example 2: LZW Decompression Step 3
<66><65><256><257><65><260> Old = 257 S = AB
New = 257 C = A
STRING TABLEENCODER OUTPUT
stringcodewordstring
B
BA256A
AB257BA
BAA258AB
57chapter3: Multimedia Compression
Example 2: LZW Decompression Step 4
<66><65><256><257><65><260> Old = 65 S = A
New = 65 C = A
STRING TABLEENCODER OUTPUT
stringcodewordstring
B
BA256A
AB257BA
BAA258AB
ABA259A
58chapter3: Multimedia Compression
Example 2: LZW Decompression Step 5
<66><65><256><257><65><260> Old = 260 S = AA
New = 260 C = A
STRING TABLEENCODER OUTPUT
stringcodewordstring
B
BA256A
AB257BA
BAA258AB
ABA259A
AA260AA
59chapter3: Multimedia Compression
LZW: Some Notes
This algorithm compresses repetitive sequences of datawell.
Since the codewords are 12 bits, any single encodedcharacter will expand the data size rather than reduce it.
In this example, 72 bits are represented with 72 bits ofdata. After a reasonable string table is built, compressionimproves dramatically.
Advantages of LZW over Huffman: LZW requires no prior information about the input data stream. LZW can compress the input stream in one single pass. Another advantage of LZW its simplicity, allowing fast
execution.
60chapter3: Multimedia Compression
LZW: Limitations
What happens when the dictionary gets too large (i.e., when all the4096 locations have been used)?
Here are some options usually implemented:
Simply forget about adding any more entries and use the tableas is.
Throw the dictionary away when it reaches a certain size.
Throw the dictionary away when it is no longer effective atcompression.
Clear entries 256-4095 and start building the dictionary again.
Some clever schemes rebuild a string table from the last Ninput characters.
61chapter3: Multimedia Compression