Quantisation and JPEG/MPEG standards
Quantisation reduces continuous-valued image or transform coefficients to discrete levels (introducing distortion) and is the core lossy step in JPEG and MPEG where transform coding, subsampling and entropy coding are combined to give efficient image/video compression.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Engineers use quantisation and the JPEG/MPEG standards when they need to store or transmit images and video with a controlled trade-off between file size (bitrate) and visual quality. Quantisation is the step that makes lossy compression possible; JPEG is the dominant standard for still images and MPEG families (MPEG-1, MPEG-2, MPEG-4, etc.) form the basis of most digital video systems.
Key ideas
Start from first principles.
Signal amplitude quantisation: a quantiser is a mapping that converts a continuous (or high-precision discrete) value x into one of a finite set of output values (levels) q_i. A scalar quantiser works on one sample at a time; a vector quantiser maps vectors (blocks) to codewords.
Quantiser definition (formal): define a partition of the real line into disjoint intervals R_i (decision regions). A scalar quantiser Q is Q(x) = q_i for x in R_i, where q_i is the reconstruction level for region R_i. Common constructions: uniform quantiser (equal-width intervals) and non-uniform quantiser (variable-width intervals, e.g., Lloyd–Max design).
Midrise and midtread: two uniform scalar quantiser variants. For step size Δ, a midrise quantiser has reconstruction levels at ±(Δ/2), ±(3Δ/2), ... (no level at zero); midtread has a level at zero (0, ±Δ, ±2Δ ...). Exams may ask you to draw decision thresholds and reconstruction points: draw an x-axis, mark intervals of width Δ, label thresholds at kΔ and levels at centers.
Quantisation error: the error e = x − Q(x). For a uniform quantiser with step Δ, if we assume the input is approximately uniformly distributed inside each cell and the error is independent of the signal, the variance (mean squared error) of quantisation error = Δ^2/12. Derive by integrating e^2 over one cell of width Δ and dividing by Δ.
Rate–distortion trade-off: fewer quantisation levels → larger Δ on average → larger distortion (MSE) but fewer bits required after entropy coding. Entropy coding (lossless) such as Huffman or arithmetic coding follows quantisation to exploit symbol probabilities.
Transform coding (why): many image samples are correlated. Apply an orthogonal transform (typically the 2D Discrete Cosine Transform (DCT)) to decorrelate and concentrate energy into a few coefficients. Quantise transform coefficients more coarsely for high-frequency coefficients (which the eye tolerates more) and finely for low-frequency coefficients.
JPEG baseline pipeline (lossy, the common case) — list of steps in order; in an exam draw a block diagram with arrows for each step and brief labels:
- Color space conversion: from RGB to YCbCr (separates luminance Y and chrominance Cb, Cr). Define conversion if required.
- Chrominance subsampling (optional): e.g., 4:4:4 (no subsampling), 4:2:2, 4:2:0 (commonly used). Explain that chroma is sampled less because human vision is less sensitive to colour detail than luminance.
- Block splitting: split Y, Cb, Cr into 8×8 blocks (JPEG baseline). Draw an 8×8 square and label rows and columns. JPEG also level-shifts samples by subtracting 128 from each 8×8 block before the DCT (and adds 128 back after the inverse DCT).
- 2D DCT on each 8×8 block (separable: apply 1D DCT on rows then columns).
- Quantisation: divide each DCT coefficient by an integer quantisation step (from quant table), round to integer. Different quant tables for luminance and chrominance are used.
- Zig-zag scan: order coefficients from low to high frequency so that runs of zeros appear together.
- Run-length encoding (RLE) for AC coefficients (encode (run, value) pairs), and special End-Of-Block (EOB) code for trailing zeros.
- Entropy coding: Huffman coding (in baseline JPEG) or arithmetic coding (in some modes) of DC differences and AC run/value symbols.
JPEG modes and options: baseline JPEG = 8×8 DCT, quantisation, Huffman coding (lossy); there are also lossless JPEG modes and progressive JPEG which send coefficients in multiple scans.
MPEG video coding concepts (high level): compression uses intra-frame (I-frame) and inter-frame techniques.
- Macroblock: processing unit (commonly 16×16 luma samples with associated chroma, though chroma arrangement depends on subsampling). In exams say: draw a 16×16 square and annotate it as macroblock containing four 8×8 luma blocks and corresponding chroma blocks depending on subsampling.
- I-frame (intra): coded like a still image (DCT + quantisation + entropy coding) – no temporal prediction.
- P-frame (predicted): coded using motion-compensated prediction from previous reference frame(s) plus a residual coded with transform+quantisation.
- B-frame (bidirectionally predicted): uses prediction from past and/or future frames (introduces higher compression but more delay). In MPEG‑1/2, B‑frames are not used as references for other frames.
- Motion estimation and compensation: find a motion vector for each block that best predicts the block from a reference frame; encode motion vector and the transform-coded residual.
- GOP (group of pictures): sequence pattern of I, P, and B frames; draw a timeline showing I ... B P B ... etc.
Distortion measures: MSE (mean squared error) and PSNR (peak signal-to-noise ratio, in dB). For 8-bit images PSNR uses MAX_I=255.
Where numeric tables or exact MPEG motion-vector precisions are needed, consult the standard; none are required for these notes.
Formulas
Q(x) = q_i for x ∈ R_i — Q(x): quantiser output; q_i: reconstruction level; R_i: decision interval. Applies to scalar quantisers.
e = x - Q(x) — quantisation error; units same as x (e.g., digital counts or radiance).
σ_q^2 = Δ^2 / 12 — variance of quantisation error for a uniform quantiser with step Δ under the assumption that the error is uniformly distributed in [−Δ/2, Δ/2]; units = (units of x)^2; applies when cell-wise distribution is uniform or approximately uniform.
X[k] = α(k) · Σ_{n=0}^{N-1} x[n] · cos(π(2n+1)k/(2N)) — 1-D DCT (type II). α(0)=√(1/N), α(k)=√(2/N) for k>0. X[k] has same units as x.
2D-DCT: X[u,v] = α(u)α(v) Σ_{x=0}^{N-1} Σ_{y=0}^{N-1} f[x,y] cos(π(2x+1)u/(2N)) cos(π(2y+1)v/(2N)) — separable 2D DCT used on N×N image blocks (N=8 in JPEG baseline).
MSE = (1/(MN)) Σ_{i=1}^{M} Σ_{j=1}^{N} (f[i,j] - f_hat[i,j])^2 — mean squared error of reconstructed image f_hat compared to original f; units = (units of pixel values)^2.
PSNR = 10 · log10( MAX_I^2 / MSE ) — PSNR in decibels (dB); MAX_I is maximum possible pixel value (e.g., 255 for 8-bit images).
q = round( X / Δ ) · Δ — simple uniform quantisation of coefficient X with step Δ (reconstruction level form); used frequently in examples (round to nearest multiple).
Worked examples
Example 1 — scalar uniform quantisation (numerical)
Given samples x = [12, 47, 93] (units: gray-level counts), use a uniform quantiser that rounds to the nearest multiple of Δ = 10. Compute quantised values, errors and MSE.
- Formula:
q = round(x/Δ)·Δ. - Compute per sample:
- x1=12 → q1=round(12/10)·10 = 1·10 = 10; error e1 = 12−10 = 2; e1^2 = 4.
- x2=47 → q2=round(47/10)·10 = 5·10 = 50; error e2 = 47−50 = −3; e2^2 = 9.
- x3=93 → q3=round(93/10)·10 = 9·10 = 90; error e3 = 3; e3^2 = 9.
- MSE = (4 + 9 + 9) / 3 = 22 / 3 = 7.333 (units: gray-level^2).
Final answer: quantised values [10, 50, 90], MSE = 7.333.
Example 2 — 1-D DCT (N=4) and quantisation
Input vector x = [10, 20, 30, 40]. Compute orthonormal DCT-II coefficients and quantise with Δ = 10.
- Use
X[k] = α(k) Σ_{n=0}^{3} x[n] cos(π(2n+1)k/(2·4))with α(0)=0.5, α(k>0)=√(2/4)=0.70710678. - Compute:
- X[0] = 0.5·(10+20+30+40) = 0.5·100 = 50.
- X[1] ≈ 0.70710678·(10·0.9238795 + 20·0.3826834 + 30·(−0.3826834) + 40·(−0.9238795)) ≈ −22.315.
- X[2] = 0.70710678·(10·0.7071068 + 20·(−0.7071068) + 30·(−0.7071068) + 40·0.7071068) = 0.
- X[3] ≈ 0.70710678·(10·0.3826834 + 20·(−0.9238795) + 30·0.9238795 + 40·(−0.3826834)) ≈ −1.586.
- Quantise:
q = round(X/10)·10→ q ≈ [50, −20, 0, 0].
This illustrates energy compaction (X[0] large, some higher coefficients small) and how quantisation reduces precision (and may set some coefficients to zero), which leads to compression after entropy coding.
Final answer: DCT coefficients ≈ [50, −22.315, 0, −1.586], quantised to [50, −20, 0, 0].
Example 3 — JPEG zig-zag and run-length idea (theory/model answer)
Given an 8×8 quantised block, you are asked in the exam to show zig-zag ordering and RLE. Model answer steps:
- Draw an 8×8 matrix and mark the DC coefficient at top-left (u=v=0).
- Sketch the zig-zag path: start at (0,0), go to (0,1), (1,0), (2,0), (1,1), (0,2), ... covering increasing frequency diagonals.
- Convert matrix into a 1-D sequence using that path and then perform RLE by encoding runs of zeros as (run, value) pairs and using EOB for trailing zeros.
State that the DC term is usually coded as the difference from previous block DC and that AC run/values are Huffman-coded.
Common mistakes
- In DCT calculations, forgetting the α(k) normalization factors (gives wrong energy scaling).
- Describing JPEG but forgetting the zig-zag + RLE + entropy-coding sequence (order matters in answers).
- In MPEG descriptions, mixing up the roles of I, P and B frames (I is intra, P uses past prediction, B may use past and future).
- Not specifying block / macroblock sizes or subsampling patterns when asked (explain 8×8 for JPEG baseline; macroblock typically 16×16 luma in many MPEG systems).
For your exam
Question types you will see: definitions (define quantiser, DCT, PSNR), short notes (compare midrise vs midtread, steps of JPEG), derivations (derive Δ^2/12 for uniform quantiser, derive DCT separability), numericals (quantise sample data, compute MSE/PSNR, small DCT calculation), and diagrams (JPEG pipeline block diagram, DCT coefficient layout, zig-zag path, GOP timeline for MPEG).
What full-marks answers include:
- Clear definitions of bolded terms.
- Correct diagrams annotated (8×8 block for JPEG, macroblock for MPEG) and ordered steps.
- For derivations show assumptions (e.g., uniform distribution in cell), show integrals or summations step-by-step.
- For numericals carry units, show each intermediate arithmetic step and final value.
Quick check
- What is the common formula for uniform quantiser error variance?
- Name the transform used in baseline JPEG and why it is used.
- In MPEG coding, what are the main roles of I, P and B frames?
Answers: 1) Δ^2/12 (under uniform-in-cell assumption). 2) 2‑D Discrete Cosine Transform (DCT) — used to decorrelate samples and concentrate energy for efficient quantisation. 3) I = intra-coded reference frame; P = predicted from past reference frames (uses motion compensation); B = bidirectionally predicted from past and/or future frames (higher compression, needs references).
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?