Unit 04 - Compression Flashcards
Why does compression matter?
People generate a ton of data.
A very good digital photo = about 12 MegaPixels (MP)
12 MP x 3 bytes per pixel = 36 MB
… which is already a lot
But with video, we have to multiply each frame size by 24 or 30 frames per second
So why does compression matter?
Basically, compression enables us to store data more efficiently
What are some common media types?
Compression is widely used across media types.
Some common ones:
Images: jpeg, png, gif
Videos: mpeg 1, 2, 4, 7 (21)
Audio: mp3, m4a
Data files: rar, zip (lzw)
What are some fundamental principles about compression?
Compression relies on both spatial coherence and temporal coherence.
What is spatial coherence?
Similarity with neighbour across space
What is temporal coherence?
Similarity with neighbour over time
Spatial
Our visual system is more sensitive to differences in light than colour, so we need to store less chroma information than luminance.
Chroma: intensity of colour
Luminance: intensity of light
Spatial continued
Spatial coherence reduces redundancy through chroma subsampling.
Divide the image into macroblocks to reduce file size.
Spatial continued. What are consequences of high compression?
Consequence of high
compression:
Compression artifacts
(noticeably distorted)
Temporal is also known as?
Inter-frame
Temporal
AKA inter-frame
Reduce redundancy between frames, which often have lots in common.
Instead of storing data for every pixel, each macroblock gets instructions on how to change from their current state using keyframes.
Temporal continued. What is a keyframe?
Keyframe: Defines the starting and ending points of a smooth transition
Lossless vs lossy?
Lossless compression: output data is identical to input data
Lossy compression: output data is not identical to input data
-Acceptable for data that is only viewed or heard
What is lossless commonly used for?
Lossless commonly used for:
Sensitive documents
Confidential info
PNG, RAW, GIF, BMP files
What is lossy commonly used for?
JPEG
MPEG, MP3
Higher compression = ____ files = ___ loss
Higher compression = smaller files = more loss
Lower compression = ____ files = ____ loss
Lower compression = bigger files = less loss
Encoding. Vocabulary
Character: a basic data unit in the input stream that forms words and phrases (e.g. English latin letter a, Chinese ideograph 請)
Strings: sequences of characters
char letter = ‘a’;
string word = ‘apple’;
Another word for encoding?
Compression
Another word for decoding?
Decompression
What is codeword?
Data elements used to represent input characters or character strings
Codetable
Codetable: stores the mapping between display value and stored (data) values
Codetables are like dictionaries for encoded messages
Compression ratio
Compression ratio: the relative reduction in size produced by a data compression algorithm
(higher = better)
How do you calculate the compression ratio?
Compression ratio = uncompressed size / compressed size
Example - mp3: 32-megabyte/3-megabyte = 10.66 or 10:1
What is the compression process?
The encoder takes strings as input and uses a codetable to decide on which codewords to produce
Input Data Stream -> Encoder -> Data storage or transmission -> Decoder -> Output data stream
The decoder takes codewords as input and uses the same codetable to decide on which characters to produce to recreate the string
Codetables
Encoder and decoder pass the encoded data as a series of codewords, along with the codetable.
The codetable can be passed explicitly or implicitly, by:
-sending it across
-agreeing on it beforehand (hard wired)
-recreate it from the codewords (clever!)
Symmetry
Symmetric compression:
time to encode = time to decode
Asymmetric compression:
time to encode < decoding
Symmetric algorithms are consequently often used for live activities, like streaming media
Asymmetrical algorithms are useful for backing up or exporting.
What is Entropy Encoding?
The measurement of the entropy of a thing tells you how much information you need to describe it.
Example:
You can describe hhhhhhhh with just 9*h – it has low entropy
The string arghbdqwplsk!? requires
more information to describe – it has higher entropy
Entropy encoding is lossless &
independent of the specific
characteristics of the medium being
compressed.
Common examples of entropy
encoding:
● Run-length
● Huffman
● Lempel Ziv Welch
Run-Length Encoding (RLE)
Run-length encoding (RLE) is a simple early & simple compression method (used by MacPaint & fax machines)
● Leverages repeating data
(called runs)
● Data are represented by a count of the run length along with the original data (e.g. AAAABB => 4A2B)
What are the steps for run-length encoding?
- Count until there is a change in data
- Then start the next count
- Repeat - while moving cell-by-cell: left-right, top-bottom
Run-length encoding
Run-length encoding is lossless and has fixed-length codewords
-Best for images with solid backgrounds (e.g. cartoons)
-Less effective for natural images (e.g. photos) and English text, where runs are short
Run-length encoding is almost always a part of a larger compression system, like JPEG
Huffman encoding
While run-length codewords are fixed length, Huffman codewords are variable length
● Length of codewords is inversely
proportional to frequency
● In variable length compression one code cannot be the prefix of another
● A binary tree structure guarantees this
How to do Huffman coding
There are two major parts in Huffman Encoding
- Build a Huffman Tree from input characters
- Traverse the Huffman Tree and assign codes to characters
Huffman coding Example
I’m going to encode the string: this is my example
● 18 characters
● 1 character = 8 bits
● 18 characters * 8 bits = 144 bits
● My string is 144 bits before compression
● Let’s see if we can improve on that!
● Characters include: letters, punctuation, spaces
Huffman coding example continued
- Build a tree
A. Create a leaf node for each unique character (*case
sensitive) and build a min heap (a list) of all leaf nodes.
○ I’ll be attempting to encode the 18-character phrase:
this is my example
○ Go through your encoding string and add a row every time you
encounter a unique character
○ Add a tick mark beside it every time you encounter it (including
the first)
○ Sum up the ticks for each row and write that number in a new
column (Count)
Char Freq Count
t | 1
h | 1
i || 2
s || 2
_ ||| 3
m || 2
y | 1
e || 2
x | 1
a | 1
p | 1
l | 1
Huffman coding example continued
- Build a tree
A. …
○ Add up ALL the counts & make sure that total = the number
of characters in your string.
○ Sort the table by the count column (descending), so your
table has most frequent characters at the top.
Char Freq Count
_ ||| 3
i || 2
s || 2
m || 2
e || 2
t | 1
h | 1
y | 1
x | 1
a | 1
p | 1
l | 1