Chapter 8 – Compression Aims: Outline the objectives of compression. Define the key methods used to compress data. Outline methods used to compress images, video and audio. Key factors in reducing the amount of data storage • Getting rid of redundant data. This involves determining the parts of the data that are not required • Identifying irrelevant data. This involves identifying the parts of the data which are perceived to be irrelevant. • Converting the data into a different format. This will typically involve changing the way that the data is processed and stored • Reducing the quality of the data. Often the user does not require the specified quality of the data. RGB 256 240 224 208 192 176 160 144 128 112 96 80 64 48 32 16 0 #FF0000 (255,0,0) #00FF00 (0,255,0) #0000FF (0,0,255) #FFFFFF (255,255,255) #000000 (0,0,0) #6496C8 (100,150,200) #C89664 (200,150,100) #64C896 (100,200,150) Red 256 240 224 208 192 176 160 144 128 112 96 80 64 48 32 16 0 256 240 224 208 192 176 160 144 128 112 96 80 64 48 32 16 0 Green Blue Changes between images (0,0) Previous image (4,0) (0,16) (4,16) Current image Red, Green, Blue Convert Convert to toDigital Digital MPEG-1 MPEG-1 or orMPEG-2 MPEG-2 compression compression Convert Convert to toDigital Digital MPEG MPEGAudio Audio (MP-3) (MP-3) compression compression Convert Convert to toDigital Digital WAV file BMP file JPEG/GIF JPEG/GIF compression compression Compression reduces redundancy in the data MPEG movie MP-3 sound file JPEG/GIF picture file Audio, image and video compression • JPEG/GIF. The JPEG (Joint Photographic Expert Group) compression technique is well matched to what the human eye and the brain perceive. • MPEG. MPEG (Motion Picture Experts Group) uses many techniques to reduce the size of the motion video data. • MPEG (MP-3). The digital storage of audio allows for the data to be compressed. Compression process Animation Text Digital Storage/ Transmission/ Processing Data conversion Compression Data conversion Uncompression 1011101010 Sound Video Sampling ADC Compression/ Data conversion Filter DAC Uncompression/ Data conversion 1101 Digital Storage/ Transmission/ Processing Sampling Ts sample Quantization 1111 1110 1101 1100 1011 1010 1001 1000 0111 0110 0101 0100 0011 0010 0001 0000 1011 1011 1001 0111 Error and number of conversion bits 1 Full Scale Max error . N 2 2 Bits (N) Quantization levels Accuracy (%) 1 2 25 2 4 12.5 3 8 4 16 Bits (N) Quantization levels Accuracy (%) 8 256 0.2 12 4096 0.012 6.25 14 16384 0.003 3.125 16 65536 0.00076 Lossy and lossless compression • • Lossless compression. Where the data, once uncompressed, will be identical to the original uncompressed data. This will obviously be the case with computer-type data, such as data files, computer programs, and so on, as any loss of data may cause the file to be corrupted. Lossy compression. Where the data, once uncompressed, cannot be fully recovered. It normally involves analysing the data and determining which data has little effect on the perceived information. Entropy and source coding • Entropy coding. This does not take into account any of the characteristics of the data and treats all the bits in the same way. As it does not know which parts of the data can be lost, it produces lossless coding. Typical coding techniques are: – Statistical encoding. Analysing the occurrence and patterns of data. – Suppressing repetitive sequences. • Source encoding. This normally takes into account characteristics of the information. Entropy coding Normally, general data compression does not take into account the type of data which is being compressed and is lossless. As it is lossless it can be applied to computer data files, documents, images, and so on. The two main techniques are statistical coding and repetitive sequence suppression. • • Huffman. Huffman coding uses a variable length code for each of the elements within the data. This normally involves analysing the data to determine the probability of its elements. The most probable elements are coded with a few bits and the least probable coded with a greater number of bits. This could be done on a character-by-character basis, in a text file, or could be achieved on a byte-by-byte basis for other files. Lempel-Ziv. Around 1977, Abraham Lempel and Jacob Ziv developed the Lempel–Ziv class of adaptive dictionary data compression techniques (also known as LZ-77 coding), which are now some of the most popular compression techniques. Huffman Letter: No. of occurrences: ‘e’ 57 ‘b’ 12 ‘c’ 3 ‘e’ 57 ‘i’ 51 ‘i’ ‘o’ ‘p’ ‘b’ ‘c’ 51 33 20 12 3 ‘e’ 57 ‘e’ 57 ‘e’ 57 68 ‘i’ 51 ‘i’ 51 ‘i’ 57 ‘e’ 57 [1] ‘o’ 33 ‘o’ 33 ‘p’ 20 ‘p’ 20 [1] ‘b’ 12 [1] ‘c’ 3 [0] ‘o’ 33 15 [0] 35 [1] 108 [1] 68 [0] ‘i’ 51 [0] ‘o’ 33 [0] ‘e’ ‘o’ ‘b’ 11 ‘i’ 10 00 ‘p’ 011 0101 ‘c’ 0100 ‘p’ 20 Huffman example (goals scored) 1 goal [34] 1 goal [34] 1 goal [34] 1 goal [34] 0 goals [21] 0 goals [21] 0 goals [21] [22] 2 goals [15] 2 goals [15] 2 goals [15] 0 goals [21] 3 goals [14] 3 goals [14] 3 goals [14] 1 4 goals [5] 4 goals [5] 1 4 goals [8] 0 [3] 0 5 goals [2] 1 6 goals [1] 0 1 Coding: 0 goals 1 goal 2 goals 3 goals 4 goals 5 goals 6 goals 0 01 11 00 101 1001 10001 10000 [56] [36] 2 goals [15] [36] 1 goal [34] 1 1 goal [34] [22] 0 [22] 1 0 0 goals [21] 2 goals [15] Huffman (cont.) 11000110100100110100 will be decoded as: ‘e’ ‘o’ ‘p’ ‘c’ ‘i’ ‘p’ ‘c’ ‘e’ ‘o’ ‘b’ 11 ‘i’ 10 00 ‘p’ 011 0101 ‘c’ 0100 Lempel-Ziv ‘The receiver#9#3quires a#20#5pt for it. This is automatically sent wh#6#2 it #30#2#47#5ved.’ LZW The Lempel–Ziv–Welsh (LZW) algorithm (also known LZ-78) builds a dictionary of frequently used groups of characters (or 8-bit binary values A simple example is to use a six-character alphabet and a 16-entry dictionary, thus the resulting code word will have 4 bits. If the transmitted message is: ababacdcdaaaaaaef Then the transmitter and receiver would initially add the following to its dictionary: 0000 0010 0100 0110–1111 ‘a’ ‘c’ ‘e’ empty 0001 0011 0101 ‘b’ ‘d’ ‘f’ LZW (cont.) 0000 0010 0100 0110–1111 ‘a’ ‘c’ ‘e’ empty 0001 0011 0101 ‘b’ ‘d’ ‘f’ 0000 0010 0100 0110 ‘a’ ‘c’ ‘e’ ‘ab’ 0001 0011 0101 0111–1111 ‘b’ ‘d’ ‘f’ empty Statistical coding • Statistical encoding is an entropy technique which identifies certain sequences within the data. These ‘patterns’ are then coded so that they have fewer bits. Frequently used patterns are coded with fewer bits than less common patterns. For example, text files normally contain many more ‘e’ characters than ‘z’ characters. Thus the ‘e’ character could be encoded with a few bits and the ‘z’ with many bits a 00000 b 00001 c 00010 d 00011 e 00100 f 00101 g 00110 h 00111 i 01000 j 01001 k 01010 l 01011 m 01100 n 01101 o 01110 p 01111 q 10000 r 10001 s 10010 t 10011 u 10100 v 10101 w 10110 x 10111 y 11000 z 11001 SP 11010 Pure coding (example) Morse coding a 01 b 1000 c 1010 d 100 e 0 f 0010 g 110 h 0000 i 00 j 0111 k 101 l 0100 m 11 n 10 o 111 p 0110 q 1101 r 010 s 000 t 1 u 001 v 0001 w 011 x 1001 y 1011 z 1100 SP 0011 Repetitive character sequence suppression • Repetitive sequence suppression involves representing long runs of a certain bit sequence with a special character. A special bit sequence is then used to represent that character, followed by the number of times it appears in sequence. 8.3200000000000 could be coded as: 8.32F11 where F is a special flag. Source compression • Source compression takes into account the type of information that is being compressed, and is typically used with image, video and audio information. For example the following might be integer values for the samples: 321, 322, 324, 324, 320, 317, 310, 311 This could be coded as difference values as: 321, +1, +2, 0, –4, –3, –7, +1 Image compression File Compression type Max. resolution or colours TIF F Huffman RLE and/or LZW 48-bit colour TIFF (tagged image file format) is typically used to transfer graphics from one computer system to another. It allows high resolutions and colours of up to 48 bits (16 bits for red, green and blue). GIF LZW 65,563 65,536 (24-bit colour, but only 256 displayable colours) Standardized graphics file format which can be read by most graphics packages. It has similar graphics characteristics to PCX files and allows multiple images in a single file and interlaced graphics. JPG JPEG compression (DCT, quantization and Huffman) Depends on the compression Excellent compression technique which produces lossy compression. It normally results in much greater compression than the methods outlined above. Image compression Image with a good deal of repetition Image with a good deal of changes Type Size(B) Compression (%) BMP 308278 100.0 BMP 301584 97.8 BMP, RLE encoded GIF 124304 40.3 GIF, Version 89a, non-interlaced GIF 127849 41.5 GIF, Version 89a, interlaced BMP 750054 100.0 TIF 136276 44.2 TIF, LZW compressed BMP 7832 1.0 BMP, RLE encoded (256 colours) TIF 81106 26.3 TIF, CCITT Group 3, MONOCHROME PCX 31983 4.3 PCX, Version 5 (256 colours) JPG 28271 9.2 JPEG – JFIF Complaint (Standard coding) GIF 4585 0.6 GIF, Version 89a, non-interlaced (256 colours) JPG 26511 8.6 JPEG – JFIF Complaint (Progressive coding) TIF 26072 3.5 TIF, LZW compressed (16.7M colours) JPG 15800 2.1 JPEG (Standard coding, 16.7M colours) JPG 12600 1.7 JPEG (Progressive coding, 16.7M colours) BMP, RBG encoded (640480, 256 colours) Type Size (B) Compression (%) BMP, RBG encoded (500500, 16.7M colours) JPEG • It is a compression technique for grey-scale or colour images and uses a combination of discrete cosine transform, quantization, run-length and Huffman coding. The components are computed from the RGB components: Y = Cb = Cr = 0.299R+0.587G+0.114B 0.1687R–0.3313G+0.5B 0.5R–0.4187G+0.0813B 7 7 (2 y 1)v 1 (2 x 1)u F ( u, v ) C ( u) C ( v ) f ( x , y ) cos cos 4 16 16 x 0 y 0 1 where C ( z ) if z 0 2 or 1 if z 0 MPEG Reference Reference frame frame YUV RGB RGBto to YUV YUV converter converter Images Block Block matching matching YUV DCT DCT transform transform Error terms Quantization Quantization DCT terms Run Run Length Length encoding encoding Quantized DCT terms Huffman Huffman coding coding Zero suppressed Variable length code Segmentation 352 pixels 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 288 pixels I, P and B-frames • • • Intra frame (I-frame). An intra frame, or I-frame, is a complete image and does not require any extra information to be added to it to make it complete. As it is a complete frame, it cannot contain any motion estimation processing. It is typically used as a starting point for other referenced frames, and is usually the first frame to be sent. Predictive frame (P-frame). The predictive frame, or P-frame, uses the preceding I-frame as its reference and has motion estimation processing. Each macroblock in this frame is supplied as referenced to an I-frame as either a vector and difference, or if no match was found, as a completely encoded macroblock (called an intracoded macroblock). The decoder must thus retain all I-frame information to allow the P-frame to be decoded. Bidirectional frame (B-frame). The bidirectional frame, or B-frame, is similar to the P-frame except that it references frames to the nearest preceding or future I- or P-frame. When compressing the data, the motion estimation works on the future frame first, followed by the past frame. If this does not give a good match, an average of the two frames is used. If all else fails, the macroblock can be intracoded. Audio compression Digitized audio signal rate = 44.116 kbps = 705.6kbps Sampler ADC Audio out Parallelto-serial Storage/ Transmission Clock Serial bit stream Filter Audio out DAC Serialto-parallel Phase locked loop (PLL) Clock information Psycho-acoustic model amplitude Actual audio spectrum masking level frequency amplitude Perceived audio spectrum frequency