3 MM Compression

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 35

Chapter Three

Multimedia Data Compression

1
The Need for Compression
• Take, for example, a video signal with resolution
320x240 pixels and 256 (8 bits) colors, 30 frames per
second
• Raw bit rate = 320x240x8x30
= 18,432,000 bits
= 2,304,000 bytes = 2.3 MB

• A 90 minute movie would take 2.3x60x90 MB = 12.44


GB

• Without compression, data storage and transmission


would pose serious problems!
2
Multimedia Data Compression
•Data compression is about finding ways to reduce the
number of bits or bytes used to store or transmit the
content of multimedia data.
–It is the process of encoding information using fewer bits
–For example, the ZIP file format, which provides compression,
also acts as an archiver, storing many source files in a single
destination output file.
•As with any communication, compressed data
communication only works when both the sender and
receiver of the information understand the encoding
scheme.
–Thus, compressed data can only be understood if the decoding
method is known by the receiver. 3
Is compression useful?
• Compression is useful because it helps reduce the
consumption of expensive resources, such as hard disk
space or transmission bandwidth.
–save storage space requirement: handy for storing files as they
take up less room.
–speed up document transmission time: convenient for transferring
files across the Internet, as smaller files transfer faster.
• On the downside, compressed data must be decompressed
to be used, and this extra processing may be detrimental to
some applications.
–For instance, a compression scheme for video may require
expensive hardware for the video to be decompressed fast
enough to be viewed as it's being decompressed
–The option of decompressing the video in full before watching it
may be inconvenient, and requires storage space for the
decompressed video.
4
Trade offs in Data Compression
The design of data compression schemes therefore
involves trade-offs among various factors, including
• the degree of compression
– To what extent the data should be compressed?
• the amount of distortion introduced
– To what extent quality loss is tolerated?
• the computational resources(winzip/winrar) required
to compress and uncompress the data.
– Do we have enough memory required for compressing and
uncompressing the data?

5
Types of Compression
Lossless Compression Lossy Compression

M M

Compress without Loss Compress with loss

m m
M’  M

Uncompress Uncompress

M M’

M = Multimedia data Transmitted


6
Types of Compression
• Lossless Compression
– Lossless compression can recover exact original data after compression.
– It is used mainly for compressing database records, spreadsheets, texts,
executable programs, etc., where exact replication of the original is essential &
changing even a single bit cannot be tolerated.
– Examples:
1. Run Length Encoding,
2. Lempel Ziv (LZ),
3. Huffman Coding.
• Lossy Compression
– Result in a certain loss of accuracy in exchange for a substantial increase in
compression.
– For visual & audio data, some loss of quality can be tolerated without losing the
essential nature of the data where losses outside visual or aural perception can be
tolerated.
• By taking advantage of the limitations of the human sensory system, a great
deal of space can be saved while producing an output which is nearly
indistinguishable from the original.
• In audio compression, for instance, non-audible (or less audible) components
of the signal are removed.
• Lossy compression is used for:
–image compression in digital cameras, to increase storage capacities with minimal
degradation of picture quality
–audio compression for Internet telephony & CD ripping, which is decoded by
audio players. 7
–video compression in DVDs with MPEG format.
Data Compression

Raw image takes about 6M 24k bytes with jpeg, Q=50


bytes (without header
information) 8
Lossy and Lossless Compression
•Lossless compression does not lose any data in the
compression process.
–Lossless compression is possible because most real-world data has
statistical redundancy. It packs data into a smaller file size by
using a kind of internal shorthand to signify redundant data. If an
original file is 1.5MB, this technique can reduce up to half of the
original size.
–For example, in English text, the letter 'e' is more common than the
letter 'z', and the probability that the letter 'q' will be followed by the
letter 'z' is very small.
–WinZip use lossless compression. For this reason zip software is
popular for compressing program & data files.
•Lossless compression has advantages and disadvantages.
–The advantage is that the compressed file will decompress to an
exact duplicate of the original file, mirroring its quality.
–The disadvantage is that the compression ratio is not all that high,
precisely because no data is lost.
•To get a higher compression ratio (i.e. to reduce a file
9
significantly beyond 50%) you must use lossy compression.
Lossy and Lossless Compression
•Lossy compression will strip a file of some of its
redundant data. Because of this data loss, only certain
applications are fit for lossy compression, like
graphics, audio, and video.
–Lossy compression necessarily reduces the quality of the file
to arrive at the resulting highly compressed size.
•Lossy data compression will be guided by research &
experiment on how people perceive the multimedia
data in question.
–For example, the human eye is more sensitive to subtle
variations in luminance (i.e. brightness) than it is to
variations in color.
–JPEG image compression works in part by "rounding off"
some of this less-important information.
10
Lossless vs. Lossy compression
•Lossless & lossy compression have become part
of our every day vocabulary due to the
popularity of MP3 music file, JPEG image file,
MPEG video file, …
–A sound file in WAV format, converted to a MP3 file
will lose much data as MP3 employs a lossy
compression; resulting in a file much smaller so that
several dozen MP3 files can fit on a single storage
device, vs. a handful of WAV files. However, the
sound quality of the MP3 file will be slightly lower
than the original WAV. Have you noticed that?
•To compress video, graphics or audio, it is our
personal choice and good results depend heavily
on the quality of the original file. 11
Example: Lossless vs. Lossy Compression
• An example of lossless vs. lossy compression is the
following string:
–25.888888888
• This string can be compressed as: 25.98
• Interpreted as, "twenty five point 9 eights", the original string is
perfectly recreated, just written in a smaller form.
• In a lossy system it can be compressed as: 26
–In which case, the original data is lost, at the benefit of a smaller
file size

• The two simplest compression techniques are:


Zero length suppression & run length encoding.
–The above is a very simple example of run-length encoding,
12
Run length encoding compression
technique
• Data often contains sequences of identical bytes. By
replacing these repeated byte sequences with the number
of occurrences, a substantial reduction of data can be
achieved.
• In Run-length encoding, large runs of consecutive
identical data values are replaced by a simple code with
the data value and length of the run, i.e.
(dataValue, LengthOfTheRun)

• This encoding scheme tries to tally occurrence of data


value (Xi) along with its run length, i.e.(Xi ,
Length_of_Xi)
13
Run-length Encoding (RLE)
• It compress data by storing runs of data (that is, sequences in which
the same data value occurs in many consecutive data elements) as a
single data value & count.
– This method is useful on data that contains many such runs. Otherwise, It is not
recommended for use with files that don't have many runs as it could potentially
double the file size.
• For example, consider the following image with long runs of white
pixels (W) and short runs of black pixels (B).
WWWWWWWWWWBWWWWWWWWWBBBWWWWWWWWWWWW
• If we apply the run-length encoding (RLE) data compression
algorithm, the compressed code is :
10W1B9W3B12W (Interpreted as ten W's, one B, nine W's, three
B's, …)
• Run-length encoding performs lossless data compression.
• It is used in fax machines (combined with Modified Huffman
coding). It is relatively efficient because faxed documents are mostly
white space, with occasional interruptions of black. 14
Lossless vs. Lossy compression
• Generally, the difference between the two compression
technique is that:
– Lossless compression schemes are reversible so that the
original data can be reconstructed,
– Lossy schemes accept some loss of data in order to achieve
higher compression.

• These lossy data compression methods typically offer a


three-way tradeoff between
– Computer resource requirement (compression speed, memory
consumption)
– compressed data size and
– quality loss.
15
Common compression methods
• Statistical methods:
–It requires prior information about the occurrence of symbols
E.g. Huffman coding and Entropy coding
•Estimate probabilities of symbols, code one symbol at a time, shorter
codes for symbols with high probabilities

• Dictionary-based coding
–The previous algorithms (both entropy and Huffman) require the
statistical knowledge which is often not available (e.g., live audio,
video).
–Dictionary based coding, such as Lempel-Ziv (LZ) compression
techniques do not require prior information to compress strings.
•Rather, replace symbols with a pointer to dictionary entries

16
Common Compression Techniques
• Compression techniques are classified into Static and
adaptive (dynamic) encodings.
1. Static coding requires two passes: one pass to compute
probabilities (or frequencies) and determine the mapping, &
a second pass to encode.
• Examples: Huffman Coding, Entropy encoding(Shanon-fano)
2. Adaptive coding:
–It adapts to localized changes in the characteristics of the data, and
don't require a first pass over the data to calculate a probability
model. All of the adaptive methods are one-pass methods; only one
scan of the message is required.
–The cost paid for these advantages is that the encoder & decoder
must be complex to keep their states synchronized, & more
computational power is needed to keep adapting the
encoder/decoder state.
–Examples: Lempel-Ziv encoding 17
Compression model
• Almost all data compression methods involve the use of a
model, a prediction of the composition of the data.
–When the data matches the prediction made by the model, the
encoder can usually transmit the content of the data at a lower
information cost, by making reference to the model.
–In most methods the model is separate, and because both the
encoder and the decoder need to use the model, it must be
transmitted with the data.
• In adaptive coding, the encoder and decoder are instead
equipped with identical rules about how they will alter their
models in response to the actual content of the data
–both start with a blank slate, meaning that no initial model needs to
be transmitted.
–As the data is transmitted, both encoder and decoder adapt their
models, so that unless the character of the data changes radically,
the model becomes better-adapted to the data it's handling and
compresses it more efficiently. 18
Huffman coding
• Developed in 1950s by David Huffman,
widely used for text compression,
multimedia codec and message 0 1
transmission
D4
0 1
• The problem: Given a set of n symbols
and their weights (or frequencies), 1 D3
0
construct a tree structure (a binary tree
for binary code) with the objective of D1 D2
reducing memory space and decoding
time per symbol. Code of:
D1 = 000
• For instance, Huffman coding is
constructed based on frequency of D2 = 001
occurrence of letters in text documents D3 = 01
D4 = 1
19
Huffman coding
• The Model could determine raw probabilities of each
symbol occurring anywhere in the input stream.
pi = # of occurrences of Si
Total # of Symbols

• The output of the Huffman encoder is determined by the


Model (probabilities).
–The higher the probability of occurrence of the symbol, the
shorter the code assigned to that symbol and vice versa.
–This will enable to easily control the most frequently occurring
symbols in in a data and also reduce the time taken during
decoding each symbols.

20
How to construct Huffman coding
• Step 1: Create forest of trees for each symbol, t1, t2,… tn
• Step 2: Sort forest of trees according to falling
probabilities of symbol occurrence
• Step 3: WHILE more than one tree exist DO
–Merge two trees t1 and t2 with least probabilities p1 and p2
–Label their root with sum p1 + p2
–Associate binary code: 1 with the right branch and 0 with the left
branch
• Step 4: Create a unique codeword for each symbol by
traversing the tree from the root to the leaf.
–Concatenate all encountered 0s and 1s together during traversal
• The resulting tree has a prob. of 1 in its root and symbols
in its leaf node. 21
Example
• Consider a 7-symbol alphabet given in the following table
to construct the Huffman coding.

Symbol Probability
a 0.05
• The Huffman encoding
b 0.05
algorithm picks each time two
c 0.1 symbols (with the smallest
d 0.2 frequency) to combine
e 0.3
f 0.2
g 0.1
22
Huffman code tree

• Using the Huffman coding a table can be constructed by


working down the tree, left to right. This gives the binary
equivalents for each symbol in terms of 1s and 0s.
• What is the Huffman binary representation for ‘café’? 23
Word level Exercise
• Given text: “for each rose, a rose is a rose”
– Construct the Huffman coding

24
Entropy encoding
According to Shannon, the entropy of an information source S is
defined as: H(S i )   ( log 2 (1/p i ))
i

–log 2 (1/pi ) indicates the amount of information contained in


symbol Si, i.e., the number of bits needed to code symbol Si.
• Example 1: What is the entropy of a gray-scale image with
uniform distribution of gray-level intensity?
–The entropy of the image, H(S)= Σi (1/256 log 2 (1/1/256))= 8
bits, which indicates that 8 bits are needed to code each gray level
• Example 2: What is the entropy of a source with M symbols
where each symbol is equally likely?
• Entropy, H(S) = log2 M
• Example 3: How about an image in which half of the pixels are
white and half are black?
• Entropy, H(S) = 1
25
Entropy Encoding
• Entropy is a measure of how much information is encoded
in a message. Higher the entropy, higher the information
content.
–The units (in coding theory) of entropy are bits per symbol. It is
determined by the base of the logarithm:
2: binary (bit);
10: decimal (digit).

• Example: If the probability of the character ‘e’ appearing


in this slide is 1/16, compute the information content of
this character?
– H(S) = 4 bits.
–So, the character string “eeeee” has a total content of 20 bits (in
contrast the use of an 8-bit ASCII coding result in 40 bits to
represent “eeeee”). 26
The Shannon-Fano Encoding Algorithm
1.Calculate the frequency of each of the symbols in the list.
2.Sort the list in (decreasing) order of frequencies.
3.Divide the list into two half’s, with the total frequency
counts of each half being as close as possible to each
other.
4.The right half is assigned a code of 1 and the left half
with a code of 0.
5.Recursively apply steps 3 and 4 to each of the halves,
until each symbol has become a corresponding code leaf
on the tree. That is, treat each split as a list and apply
splitting and code assigning till you are left with lists of
single elements.
6.Generate code word for each symbol 27
The Shannon-Fano Encoding Algorithm
• Example: Given five symbols A to E with their frequencies being
15, 7, 6, 6 & 5; encode them using Shannon-Fano entropy encoding
Symbol A B C D E
Count 15 7 6 6 5
0 0 1 1 1
0 1 0 1 1
0 1

28
Exercise
• Given the following symbols and their corresponding
frequency of occurrence, find an optimal binary code for
compression:
Character: a b c d e t

Frequency: 16 5 12 17 10 25

a. Using the Huffman algorithm


b. Using Entropy coding scheme

29
Lempel-Ziv compression
•The problem with Huffman coding is that it requires
knowledge about the data before encoding takes place.
–Huffman coding requires frequencies of symbol occurrence
before codeword is assigned to symbols

•Lempel-Ziv compression:
–Not rely on previous knowledge about the data
–Rather builds this knowledge in the course of data
transmission/data storage
–Lempel-Ziv algorithm (called LZ) uses a table of code-words
created during data transmission;
•each time it replaces strings of characters with a reference to a
previous occurrence of the string.
31
Lempel-Ziv Compression Algorithm
• The multi-symbol patterns are of the form: C0C1 . . . Cn-1
Cn. The prefix of a pattern consists of all the pattern
symbols except the last: C0C1 . . . Cn-1

Lempel-Ziv Output: there are three options in assigning a


code to each symbol in the list
• If one-symbol pattern is not in dictionary, assign (0, symbol)
• If multi-symbol pattern is in dictionary, assign
(dictionaryPrefixIndex, lastPatternSymbol)
• If the last input symbol or the last pattern is in the dictionary,
asign (dictionaryPrefixIndex, )
32
Example: LZ Compression
Encode (i.e., compress) the string ABBCBCABABCAABCAAB
using the LZ algorithm.

The compressed message is: (0,A)(0,B)(2,C)(3,A)(2,A)(4,A)(6,B)


• Note: The above is just a representation, the commas and
33
parentheses are not transmitted
Example: Compute Number of bits transmitted
• Consider the string ABBCBCABABCAABCAAB given in example
2 (previous slide) and compute the number of bits transmitted:
Number of bits = Total No. of characters * 8 = 18 * 8 = 144 bits
• The compressed string consists of codewords and the corresponding
codeword index as shown below:
Codeword: (0, A) (0, B) (2, C) (3, A) (2, A) (4, A) (6, B)
Codeword index: 1 2 3 4 5 6 7
• Each code word consists of a character and an integer:
– The character is represented by 8 bits
– The number of bits n required to represent the integer part of the codeword with
index i is given by:

Codeword (0, A) (0, B) (2, C) (3, A) (2, A) (4, A) (6, B)


Index 1 2 3 4 5 6 7
Bits: (1 + 8) + (1 + 8) + (2 + 8) + (2 + 8) + (3 + 8) + (3 + 8) + (3 + 8) = 71 bits
• The actual compressed message is: 0A0B10C11A010A100A110B
– where each character is replaced by its binary 8-bit ASCII code. 34
Example: Decompression
Decode (i.e., decompress) the sequence (0, A) (0, B) (2, C) (3, A)
(2, A) (4, A) (6, B)

The decompressed message is:


ABBCBCABABCAABCAAB
35
Exercise
Encode (i.e., compress) the following strings using the
Lempel-Ziv algorithm.

1. Aaababbbaaabaaaaaaabaabb
2. ABBCBCABABCAABCAAB
3. SATATASACITASA.

36

You might also like