8. Compression. 2 Video and Audio Compression Video and Audio files are very large. Unless we...

29
8. Compression 8. Compression

Transcript of 8. Compression. 2 Video and Audio Compression Video and Audio files are very large. Unless we...

8. Compression8. Compression

22

Video and Audio Compression Video and Audio Compression

Video and Audio files are very large. Unless we Video and Audio files are very large. Unless we develop and maintain very high bandwidth develop and maintain very high bandwidth networks (Gigabytes per second or more) we have networks (Gigabytes per second or more) we have to compress the data. to compress the data.

Relying on higher bandwidths is not a good option - Relying on higher bandwidths is not a good option - M25 Syndrome: Traffic needs ever increases and M25 Syndrome: Traffic needs ever increases and will adapt to swamp current limit whatever this is. will adapt to swamp current limit whatever this is.

Compression becomes part of the representation or Compression becomes part of the representation or codingcoding scheme which have become popular scheme which have become popular audio, image and video formats. audio, image and video formats.

33

What is Compression?What is Compression?

Compression basically employs redundancy in the data: Compression basically employs redundancy in the data: Temporal - in 1D data, 1D signals, Audio etc. Temporal - in 1D data, 1D signals, Audio etc. Spatial - correlation between neighbouring pixels or data Spatial - correlation between neighbouring pixels or data

items items Spectral - correlation between colour or luminescence Spectral - correlation between colour or luminescence

components. This uses the frequency domain to exploit components. This uses the frequency domain to exploit relationships between frequency of change in data. relationships between frequency of change in data.

psycho-visual - exploit perceptual properties of the human psycho-visual - exploit perceptual properties of the human visual system. visual system.

44

Compression can be categorised in Compression can be categorised in two broad ways: two broad ways:

Lossless Compression Lossless Compression where data is compressed and can be reconstituted where data is compressed and can be reconstituted

(uncompressed) without loss of detail or information. (uncompressed) without loss of detail or information. These are referred to as bit-preserving or reversible These are referred to as bit-preserving or reversible compression systems also. compression systems also.

Lossy Compression Lossy Compression where the aim is to obtain the best possible where the aim is to obtain the best possible fidelityfidelity for a for a

given bit-rate or minimizing the bit-rate to achieve a given bit-rate or minimizing the bit-rate to achieve a given fidelity measure. Video and audio compression given fidelity measure. Video and audio compression techniques are most suited to this form of compression. techniques are most suited to this form of compression.

55

If an image is compressed it clearly needs to be If an image is compressed it clearly needs to be uncompressed (decoded) before it can uncompressed (decoded) before it can viewed/listened to. Some processing of data viewed/listened to. Some processing of data may be possible in encoded form however. may be possible in encoded form however.

Lossless compression frequently involves some Lossless compression frequently involves some form of form of entropy encodingentropy encoding and are based in and are based in information theoretic techniques (see next fig.) information theoretic techniques (see next fig.)

Lossy compression use source encoding Lossy compression use source encoding techniques that may involve transform encoding, techniques that may involve transform encoding, differential encoding or vector quantisation (see differential encoding or vector quantisation (see next fig.).next fig.).

66

77

Lossless Compression Algorithms Lossless Compression Algorithms (Repetitive Sequence Suppression) (Repetitive Sequence Suppression)

Simple Repetition Suppression Simple Repetition Suppression If in a sequence a series on If in a sequence a series on nn successive tokens appears successive tokens appears

we can replace these with a token and a count number of we can replace these with a token and a count number of occurrences. We usually need to have a special occurrences. We usually need to have a special flagflag to to denote when the repeated token appears denote when the repeated token appears

For Example For Example 89400000000000000000000000000000000 89400000000000000000000000000000000

can be replaced with can be replaced with 894f32 894f32 where f is the flag for zero. where f is the flag for zero.

Compression savings depend on the content of the data. Compression savings depend on the content of the data.

88

Applications of this simple compression Applications of this simple compression technique include: technique include:

Suppression of zero's in a file (Suppression of zero's in a file (Zero Zero Length SuppressionLength Suppression) ) – Silence in audio data, Pauses in conversation Silence in audio data, Pauses in conversation

etc.etc. – Bitmaps Bitmaps – Blanks in text or program source files Blanks in text or program source files – Backgrounds in images Backgrounds in images

other regular image or data tokens other regular image or data tokens

99

Run-length Encoding Run-length Encoding

This encoding method is frequently applied to This encoding method is frequently applied to images (or pixels in a scan line). It is a small images (or pixels in a scan line). It is a small compression component used in JPEG compression component used in JPEG compression. compression.

In this instance, sequences of image elements XIn this instance, sequences of image elements X11, ,

XX22, …, X, …, Xnn are mapped to pairs (c are mapped to pairs (c11, l, l11), (c), (c11, L, L22), …, ), …,

(c(cnn, l, lnn) where ) where ccii represent image intensity or colour represent image intensity or colour

and and llii the length of the the length of the iith run of pixels (Not th run of pixels (Not

dissimilar to zero length suppression above). dissimilar to zero length suppression above).

1010

For example: For example: Original Sequence: Original Sequence:

111122233333311112222111122233333311112222 can be encoded as: can be encoded as: (1,4),(2,3),(3,6),(1,4),(2,4) (1,4),(2,3),(3,6),(1,4),(2,4)

The savings are dependent on the data. In the The savings are dependent on the data. In the worst case (Random Noise) encoding is more worst case (Random Noise) encoding is more heavy than original file: 2*integer rather 1* heavy than original file: 2*integer rather 1* integer if data is represented as integers. integer if data is represented as integers.

1111

Lossless Compression Algorithms Lossless Compression Algorithms (Pattern Substitution) (Pattern Substitution)

This is a simple form of statistical encoding. This is a simple form of statistical encoding.

Here we substitute a frequently repeating Here we substitute a frequently repeating pattern(s) with a code. The code is shorter pattern(s) with a code. The code is shorter than the pattern giving us compression. than the pattern giving us compression.

A simple Pattern Substitution scheme could A simple Pattern Substitution scheme could employ predefined code (for example employ predefined code (for example replace all occurrences of `The' with the replace all occurrences of `The' with the code '&'). code '&').

1212

More typically tokens are assigned to according to More typically tokens are assigned to according to frequency of occurrence of patterns: frequency of occurrence of patterns:

Count occurrence of tokens Count occurrence of tokens Sort in Descending order Sort in Descending order Assign some symbols to highest count tokens Assign some symbols to highest count tokens

A predefined symbol table may used i.e. assign A predefined symbol table may used i.e. assign code code ii to token to token ii. .

However, it is more usual to dynamically assign However, it is more usual to dynamically assign codes to tokens. The entropy encoding schemes codes to tokens. The entropy encoding schemes below basically attempt to decide the optimum below basically attempt to decide the optimum assignment of codes to achieve the best assignment of codes to achieve the best compression. compression.

1313

Lossless Compression Algorithms Lossless Compression Algorithms (Entropy Encoding) (Entropy Encoding)

Lossless compression frequently involves Lossless compression frequently involves some form of some form of entropy encodingentropy encoding and are and are based in information theoretic techniques, based in information theoretic techniques, Shannon is father of information theory.Shannon is father of information theory.

1414

The Shannon-Fano Algorithm The Shannon-Fano Algorithm

This is a basic information theoretic This is a basic information theoretic algorithm. A simple example will be used to algorithm. A simple example will be used to illustrate the algorithm: illustrate the algorithm:

Symbol A B C D E Symbol A B C D E

Count 15 7 6 6 5 Count 15 7 6 6 5

1515

Encoding for the Shannon-Fano Encoding for the Shannon-Fano Algorithm:Algorithm:

A top-down approach A top-down approach

1. Sort symbols according to their 1. Sort symbols according to their frequencies/probabilities, e.g., ABCDE. frequencies/probabilities, e.g., ABCDE.

2. Recursively divide into two parts, each 2. Recursively divide into two parts, each with approx. same number of counts. with approx. same number of counts.

1616

Huffman Coding Huffman Coding

Huffman coding is based on the frequency of Huffman coding is based on the frequency of occurrence of a data item (pixel in images). occurrence of a data item (pixel in images). The principle is to use a lower number of The principle is to use a lower number of bits to encode the data that occurs more bits to encode the data that occurs more frequently. Codes are stored in a frequently. Codes are stored in a Code Code BookBook which may be constructed for each which may be constructed for each image or a set of images. In all cases the image or a set of images. In all cases the code book plus encoded data must be code book plus encoded data must be transmitted to enable decoding. transmitted to enable decoding.

1717

The Huffman algorithm is now briefly summarised: The Huffman algorithm is now briefly summarised: A bottom-up approach A bottom-up approach 1. Initialization: Put all nodes in an OPEN list, keep it 1. Initialization: Put all nodes in an OPEN list, keep it

sorted at all times (e.g., ABCDE). sorted at all times (e.g., ABCDE). 2. Repeat until the OPEN list has only one node left: 2. Repeat until the OPEN list has only one node left: (a) From OPEN pick two nodes having the lowest (a) From OPEN pick two nodes having the lowest

frequencies/probabilities, create a parent node of frequencies/probabilities, create a parent node of them. them.

(b) Assign the sum of the children's frequencies/ (b) Assign the sum of the children's frequencies/ probabilities to the parent node and insert it into probabilities to the parent node and insert it into OPEN. OPEN.

(c) Assign code 0, 1 to the two branches of the tree, (c) Assign code 0, 1 to the two branches of the tree, and delete the children from OPEN. and delete the children from OPEN.

1818

1919

The following points are worth noting about the The following points are worth noting about the above algorithm: above algorithm:

Decoding for the above two algorithms is Decoding for the above two algorithms is trivial as long as the coding table (the trivial as long as the coding table (the statistics) is sent before the data. (There is a statistics) is sent before the data. (There is a bit overhead for sending this, negligible if the bit overhead for sending this, negligible if the data file is big.) data file is big.)

Unique Prefix PropertyUnique Prefix Property: no code is a prefix : no code is a prefix to any other code (all symbols are at the leaf to any other code (all symbols are at the leaf nodes) -> great for decoder, unambiguous. nodes) -> great for decoder, unambiguous.

If prior statistics are available and accurate, If prior statistics are available and accurate, then Huffman coding is very good. then Huffman coding is very good.

2020

Huffman Coding of Images Huffman Coding of Images

In order to encode images: In order to encode images:

Divide image up into 8x8 blocks Divide image up into 8x8 blocks Each block is a symbol to be coded Each block is a symbol to be coded Compute Huffman codes for set of block Compute Huffman codes for set of block Encode blocks accordinglyEncode blocks accordingly

2121

Adaptive Huffman Coding Adaptive Huffman Coding

The basic Huffman algorithm has been extended, for the The basic Huffman algorithm has been extended, for the following reasons: following reasons:

(a) The previous algorithms require the statistical (a) The previous algorithms require the statistical knowledge which is often not available (e.g., live audio, knowledge which is often not available (e.g., live audio, video). video).

(b) Even when it is available, it could be a heavy overhead (b) Even when it is available, it could be a heavy overhead especially when many tables had to be sent when a non-especially when many tables had to be sent when a non-order0 model is used, i.e. taking into account the impact of order0 model is used, i.e. taking into account the impact of the previous symbol to the probability of the current symbol the previous symbol to the probability of the current symbol (e.g., "qu" often come together, ...). (e.g., "qu" often come together, ...).

The solution is to use adaptive algorithms, e.g. Adaptive The solution is to use adaptive algorithms, e.g. Adaptive Huffman coding (applicable to other adaptive compression Huffman coding (applicable to other adaptive compression algorithms).algorithms).

2222

Arithmetic Coding Arithmetic Coding

Huffman coding and the like use an integer number Huffman coding and the like use an integer number (k) of bits for each symbol, hence k is never less (k) of bits for each symbol, hence k is never less than 1. Sometimes, e.g., when sending a 1-bit than 1. Sometimes, e.g., when sending a 1-bit image, compression becomes impossible. image, compression becomes impossible.

Map all possible length 2, 3 … messages to intervals Map all possible length 2, 3 … messages to intervals in the range [0..1] (in general, need – in the range [0..1] (in general, need – log plog p bits to bits to represent interval of size represent interval of size p)p)..

To encode message, just send enough bits of a To encode message, just send enough bits of a binary fraction that uniquely specifies the interval. binary fraction that uniquely specifies the interval.

2323

Problem: how to determine Problem: how to determine probabilities?probabilities?

Simple idea is to use adaptive model: Simple idea is to use adaptive model:

Start with guess of symbol Start with guess of symbol frequencies. Update frequency with frequencies. Update frequency with each new symbol. each new symbol.

Another idea is to take account of Another idea is to take account of intersymbol probabilities, e.g., intersymbol probabilities, e.g., Prediction by Partial Matching. Prediction by Partial Matching.

2424

Lempel-Ziv-Welch (LZW) Algorithm Lempel-Ziv-Welch (LZW) Algorithm

The LZW algorithm is a very common The LZW algorithm is a very common compression technique. compression technique.

Suppose we want to encode the Oxford Suppose we want to encode the Oxford Concise English dictionary which Concise English dictionary which contains about 159,000 entries. Why not contains about 159,000 entries. Why not just transmit each word as an 18 bit just transmit each word as an 18 bit number? number?

2525

Problems:Problems:

Too many bits, Too many bits, everyone needs a dictionary, everyone needs a dictionary, only works for English text. only works for English text. SolutionSolution: Find a way to build the : Find a way to build the

dictionary adaptively.dictionary adaptively.

2626

Original methods due to Ziv and Original methods due to Ziv and Lempel in 1977 and 1978. Terry Welch Lempel in 1977 and 1978. Terry Welch improved the scheme in 1984 (called improved the scheme in 1984 (called LZW compression). LZW compression).

It is used in UNIX It is used in UNIX compresscompress -1D token -1D token stream (similar to below) stream (similar to below)

It used in GIF compression - 2D It used in GIF compression - 2D window tokens (treat image as with window tokens (treat image as with Huffman Coding Above). Huffman Coding Above).

2727

The LZW Compression Algorithm The LZW Compression Algorithm can summarised as follows: can summarised as follows:

w = NIL; w = NIL; while ( read a character k ) while ( read a character k )

{ { if wk exists in the dictionary if wk exists in the dictionary

w = wk; w = wk; else else

add wk to the dictionary; add wk to the dictionary; output the code for w; output the code for w; w = k; w = k;

} }

2828

The LZW Decompression Algorithm The LZW Decompression Algorithm is as follows: is as follows:

read a character k; read a character k; output k; output k; w = k; w = k; while ( read a character k ) while ( read a character k ) /* k could be a character or a code. */ /* k could be a character or a code. */

{ { entry = dictionary entry for k; entry = dictionary entry for k; output entry; output entry; add w + entry[0] to dictionary; add w + entry[0] to dictionary; w = entry; w = entry;

} }

2929

Entropy Encoding Summary Entropy Encoding Summary

Huffman maps fixed length symbols to variable length Huffman maps fixed length symbols to variable length codes. Optimal only when symbol probabilities are powers codes. Optimal only when symbol probabilities are powers of 2. of 2.

Arithmetic maps entire message to real number range Arithmetic maps entire message to real number range based on statistics. Theoretically optimal for long based on statistics. Theoretically optimal for long messages, but optimality depends on data model. Also can messages, but optimality depends on data model. Also can be CPU/memory intensive. be CPU/memory intensive.

Lempel-Ziv-Welch is a dictionary-based compression Lempel-Ziv-Welch is a dictionary-based compression method. It maps a variable number of symbols to a fixed method. It maps a variable number of symbols to a fixed length code. length code.

Adaptive algorithms do not need a priori estimation of Adaptive algorithms do not need a priori estimation of probabilities, they are more useful in real applications. probabilities, they are more useful in real applications.