Chapter 1.
3 — File Compression
Cambridge AS & A Level Computer Science | Syllabus 9618
NOT IN SYLLABUS — skip this
Section 1.2.4 Video — the book itself says this section 'extends beyond the syllabus'.
You do NOT need to learn about frame rates, DV cameras, or motion JPEG for the exam.
Extension Activities 1C and 1D in the book are also optional extras — not examined.
Key Terms
Lossless file compression Compression method where the original file can be fully
restored (reconstructed) after decompression.
Lossy file compression Compression method where parts of the original file cannot
be recovered during decompression — some original detail
is permanently lost.
JPEG Joint Photographic Expert Group. A lossy compression
format for images, based on the human eye's inability to
spot certain colour changes and hues.
MP3 / MP4 File compression formats used for music (MP3) and
multimedia including video and photos (MP4).
Audio compression Method used to reduce the size of a sound file using
perceptual music shaping.
Perceptual music shaping Removes sounds outside the normal human hearing range
(and quieter sounds masked by louder ones) during
compression.
Bit rate Number of bits per second that can be transmitted over a
network; determines MP3 sound quality (80–320 kilobits
per second).
Run-length encoding (RLE) A lossless compression technique that encodes a run of
identical data items as two values: (count, data item).
Effective for text and image files.
Lossless vs Lossy Compression
File compression is used to reduce file size to save storage space or reduce transmission time. The two main types are:
Lossless Compression Lossy Compression
All original data is preserved. Some data is permanently removed.
The original file can be fully reconstructed after The original file CANNOT be reconstructed after
decompression. decompression.
Essential for files where any data loss would be disastrous The algorithm decides which parts of the file are important
(e.g. spreadsheets, databases). and which can be discarded.
Example technique: Run-Length Encoding (RLE). Example formats: MP3 (audio), JPEG (images).
EXAM TIP
When the exam asks you to define lossless or lossy, always include TWO things:
(1) what happens to the data during compression
(2) whether the original can be reconstructed or not.
Example good answer: 'Lossless compression — no data is lost and the original file can be
fully reconstructed after decompression.'
Always give an example file format (RLE for lossless; MP3 or JPEG for lossy).
1.3.1 File Compression Applications
MPEG-3 (MP3) — Audio Compression
MP3 uses audio compression to convert music and other sounds into an MP3 file format.
● Reduces normal music file size by about 90% (e.g. 80 MB on CD → 8 MB as MP3).
● MP3 is a
MP3 is a lossy format — once compressed, the original cannot be put back together.
How it achieves 90% reduction without much quality loss:
● Uses
Uses perceptual music shaping:
○ Removes frequencies that are outside the normal human hearing range.
○ If two sounds are played at the same time, only the louder one can be heard by the ear — the softer
sound is eliminated.
Sound quality depends on the bit rate (bits per second used when creating the file):
● Bit rates range from 80 to 320 kilobits per second.
● Usually 200 kilobits/sec or higher gives sound quality close to a normal CD.
EXAM TIP
If asked 'how does MP3 reduce file size while keeping most of the quality?' — you must
name 'perceptual music shaping' and give BOTH rules:
1. Removes frequencies outside the human hearing range.
2. When two sounds play at the same time, the softer sound is eliminated.
Just saying 'removes sounds humans cannot hear' will only get 1 mark out of 2.
IMPORTANT
MP3 is LOSSY — once compressed, the original music file CANNOT be recovered.
Do not say 'lossless' for MP3 — this is a very common mistake.
MPEG-4 (MP4)
MP4 is slightly different to MP3. It allows the storage of:
● Music, videos, photos and animation — not just sound.
● Videos can be streamed over the internet using MP4 without losing discernible quality.
JPEG — Photographic (Bit-map) Image Compression
JPEG uses lossy compression for photographic images.
● Once compressed, the original file can NO longer be reconstructed.
● JPEG reduces a raw bit-map image by a factor of between 5 and 15, depending on quality.
EXAM TIP
JPEG is lossy. If the exam asks 'what type of compression does JPEG use?' — answer lossy.
If asked why JPEG is lossy: because once the algorithm compresses the image, the original
file cannot be constructed again.
Run-Length Encoding (RLE)
RLE is a lossless compression technique.
It reduces the size of a string of adjacent, identical data (such as repeated colours in an image).
How RLE works
A repeating string is encoded into two values:
1st value — the count: number of identical data items in the run.
2nd value — the data item code: e.g. ASCII code of the character, or pixel colour code.
RLE is only effective where there is a long run of repeated units/bits.
IMPORTANT
RLE always needs TWO values per run — count AND the code. Not just the count.
RLE is NOT effective on data that does not have long runs of identical values.
RLE is LOSSLESS — the original data can always be reconstructed from the encoded values.
RLE Example — Text
Example: text string 'aaaaabbbbccddddd' (16 bytes uncompressed)
Each character needs 1 byte. Using ASCII codes:
aaaaa bbbb cc ddddd
05 97 04 98 02 99 05 100
RLE stores: 05 97 04 98 02 99 05 100 = 8 bytes
Result: file size halved (8 bytes instead of 16 bytes).
Flag System for Non-Repeating Data
A problem occurs with strings like 'cdcdcdcdcd' where compression is not effective. A flag (value 255) is used:
● 255 means: 'what follows is a RLE run' — the next two bytes are (count, code).
● Without a flag: the next byte(s) are taken at face value — e.g. 01 99 means one character with ASCII code 99.
This allows both repeated and non-repeated data to be stored efficiently in the same compressed file.
RLE Example — Black & White Image
Example: 8 × 8 grid of the letter 'F' (W = white = 1, B = black = 0)
Uncompressed: 64 bytes (one byte per square).
Compressed RLE: 9W 6B 2W 1B 7W 1B 7W 5B 3W 1B 7W 1B 7W 1B 6W
= 30 values = 30 bytes
File size reduced from 64 bytes to 30 bytes.
RLE Example — Coloured Image
Example: 8 × 8 coloured image (4 colours, each defined by RGB — 3 bytes per pixel)
Uncompressed: 8 × 8 × 3 = 192 bytes.
After RLE: 92 values = 92 bytes.
File size reduced by approximately 52%.
Note: real-world reductions may be smaller because a file header also needs storing.
1.3.2 General Methods of Compressing Files
The techniques above (MP3, JPEG, RLE) are for specific file types. There are also general methods to reduce file size
without using a dedicated lossy/lossless algorithm:
File Type Ways to Reduce File Size
• Reduce the sampling rate used
Movie files • Reduce the sampling resolution
• Reduce the frame rate
• Crop the image
Image files • Decrease the colour / bit depth
• Reduce the image resolution
EXAM TIP
Exams often ask you to 'state two ways to reduce the size of a movie file' or 'an image file'.
Keep the two lists separate — do NOT mix movie and image methods.
Movie: sampling rate / sampling resolution / frame rate.
Image: crop / colour depth / resolution.
One word per method is enough — no need to explain unless the question says 'describe'.
Quick Summary
Remember
Lossless = no data lost, original fully recoverable. Example: RLE.
Lossy = some data permanently removed. Examples: MP3 (audio), JPEG (images).
RLE encodes as (count, code). Only good when there are long runs of identical data.
MP3 uses perceptual music shaping: removes inaudible frequencies and masked sounds.
Bit rate (80–320 kbps) controls MP3 quality — higher bit rate = better quality = larger file.
General methods: movie files → reduce sampling rate / resolution / frame rate; image files → crop / reduce colour
depth / reduce resolution.