---
title: Streaming image pipelines
heading: Processing video one pixel per clock with line buffers instead of frame buffers
eyebrow: S3 · Lesson 32 · Streaming image pipelines
xp: 50
---

::: hook
A 1080p camera at 60 frames per second delivers 124 million pixels a second, and a filter that looks at each pixel's 3 × 3 neighborhood needs nine pixels for every output. Storing whole frames in DRAM and reading neighborhoods back would need gigabytes per second of bandwidth. Hardware does it differently: pixels arrive in raster order, one per clock, and a few **line buffers** in on-chip memory hold just the previous rows, so every neighborhood is available as the new pixel arrives. Lesson 31's DSP structures, applied in two dimensions with this memory trick, are how cameras, displays and machine-vision chips process video in real time.
:::

::: play
- The on-chip memory a $k \times k$ filter's line buffers need, $(k - 1) \times W \times b$ bits for image width $W$ and $b$ bits per pixel, against image width, for 8-bit pixels.
- A frame buffer for the same image needs $W \times H \times b$ bits: hundreds of times more. The readout compares them for a 1080-row image.
:::

```sim plot
view: graph
x: W = 320..7680 log
y: M = 1..1e6 log
slider: k = 3..11 step 2 = 3
plot: (k - 1)*W*8/8/1024 | label: line buffers, KB
plot: W*1080*8/8/1024 | dashed | color: ch2 | label: frame buffer, KB
readout: line buffers at 1920 wide, KB = (k - 1)*1920/1024
readout: ratio of frame to line buffers at 1080 rows = 1080/(k - 1)
```

::: predict
## A 3 × 3 filter processes 1920-pixel-wide, 8-bit video. How much on-chip memory do its line buffers need?

- [ ] 6 MB
- [ ] 2 KB
- [x] About 3.75 KB
- [ ] 17 KB

::: reveal
Two full lines of 1920 bytes: 3840 bytes, 3.75 KB, which fits in a single block RAM or two. The third row is the incoming pixel stream itself. A frame buffer would need 1920 × 1080 bytes, 2 MB, and the bandwidth to read it back.
:::
:::

::: nameit
## Raster streams

Video arrives as a stream: pixels left to right along a row, rows top to bottom, one pixel per **pixel clock**, with blanking intervals between rows and frames. 1080p60 has 1920 × 1080 active pixels at 60 frames per second, 124.4 megapixels per second, carried on a 148.5 MHz pixel clock because of the blanking: 2200 × 1125 total per frame.

## Line buffers

A $k \times k$ window needs the current pixel and the $k - 1$ rows above it. Each row is held in a FIFO-like line buffer of $W$ pixels; as a pixel arrives, it enters the first buffer and the oldest pixel of each buffer moves to the next. Tapping the buffers' outputs gives one column of the window per clock, and a shift register of $k$ columns holds the whole window. Memory: $(k - 1)Wb$ bits. Latency: about $(k - 1)/2$ rows plus a few pixels, so the output is a small fraction of a frame behind the input.

## Throughput and arithmetic

At one pixel per clock, a general $k \times k$ convolution needs $k^2$ multiplies every clock. Separable kernels (a Gaussian, a box) split into a $k$-tap horizontal and a $k$-tap vertical pass, $2k$ multiplies. At 4K60, 498 megapixels per second, designs process two or four pixels per clock to keep the clock rate reasonable.

## Edges and frames

At the image's borders the window hangs off the edge. Hardware replicates or mirrors the edge pixels, or marks the output invalid, using row and column counters driven by the stream's sync signals. Operations that need more than a few rows, such as rotations or large resizes, need a frame buffer in DRAM after all, and its bandwidth: $W \times H \times b \times f$ for each pass.
:::

::: example
## Size the line buffers for a 3 × 3 filter on 1920 × 1080, 8-bit video, and for a 5 × 5 filter on 3840 × 2160, 10-bit video.

::: solution
1. **1080p, 3 × 3.** Two lines of 1920 pixels at 8 bits:
   $$2 \times 1920 \times 8 = 30\,720\ \text{bits}$$
   which is 3.75 KB.
2. **4K, 5 × 5.** Four lines of 3840 pixels at 10 bits:
   $$4 \times 3840 \times 10 = 153\,600\ \text{bits}$$
   which is 18.75 KB. Both fit comfortably in on-chip RAM.
3. **Against a full frame.** A full 4K frame at 10 bits is 83 Mbit, 10.4 MB: line buffering saves a factor of 540.

**Answer:** 30 720 bits (3.75 KB) for the 1080p filter and 153 600 bits (18.75 KB) for the 4K one.
:::
:::

::: example
## A frame buffer for 1080p60 RGB at 24 bits per pixel is written once and read once per frame. What memory bandwidth does it need?

::: solution
1. **One frame.**
   $$1920 \times 1080 \times 3 = 6.22\ \text{MB}$$
2. **Bandwidth.** Each frame is written and read, 60 times a second:
   $$2 \times 6.22 \times 60 = 746\ \text{MB/s}$$
3. **What it costs.** That is a sizable share of a DDR interface, and the latency of DRAM accesses must be hidden with FIFOs. A streaming design that avoids the frame buffer needs none of it.

**Answer:** 746 MB/s.
:::
:::

::: example
## A 7 × 7 Gaussian blur runs on 4K60 video. Compare the multiplies per second for a direct 2D convolution and a separable one.

::: solution
1. **Pixel rate.**
   $$3840 \times 2160 \times 60 = 498 \times 10^6$$
   pixels per second.
2. **Direct.** 49 multiplies per pixel, 24.4 billion per second.
3. **Separable.** 14 per pixel, 7.0 billion per second.
4. **Why it works.** Gaussian kernels are separable, so the second is exact, and the 3.5× saving in DSP blocks is why designers check for separability first.

**Answer:** 24.4 billion multiplies a second direct, against 7.0 billion separable.
:::
:::

::: check
given: k = 3 | 5 | 7; W = 640 | 1920 | 3840; b = 8 | 10 | 12
answer: (k - 1)*W*b/8/1024
unit: KB
tolerance: 0.5%

## How much line-buffer memory does a {k} × {k} filter need on a {W}-pixel-wide image with {b}-bit pixels, in KB (1024 bytes)?

::: right
Right. $(k - 1)Wb$ bits = {answer}.
:::

::: misses
- [k*W*b/8/1024] Only $k - 1$ rows are stored; the current row arrives live.
- [(k - 1)*W*b/1024] Convert bits to bytes first.
- [any] Not yet. $(k - 1) \times W \times b$ bits.
:::
:::

::: check
given: W = 1280 | 1920 | 3840; H = 720 | 1080 | 2160; f = 30 | 60 | 120
answer: W*H*f/1e6
unit: MHz
tolerance: 0.5%

## A stream carries {W} × {H} active pixels at {f} frames per second. How many megapixels per second does a one-pixel-per-clock pipeline process (ignoring blanking)?

::: right
Right. {answer} megapixels per second.
:::

::: misses
- [W*H/1e6] Multiply by the frame rate.
- [any] Not yet. $W \times H \times f$.
:::
:::

::: check
kind: parts
given: k = 3 | 5 | 9; p = 124 | 498

## A {k} × {k} separable filter processes {p} million pixels per second.

::: part
answer: k^2*p*1e6/1e9
tolerance: 0.5%

### How many billion multiplies per second would a direct 2D convolution need?

::: misses
- [2*k*p*1e6/1e9] That's the separable version.
- [any] Not yet. $k^2$ per pixel.
:::
:::

::: part
answer: 2*k*p*1e6/1e9
tolerance: 0.5%

### And the separable version?

::: misses
- [k^2*p*1e6/1e9] That's the direct version.
- [k*p*1e6/1e9] Two passes: horizontal and vertical, $k$ each.
- [any] Not yet. $2k$ per pixel.
:::
:::

::: right
Right. The saving grows with the kernel size: $k/2$ times fewer multiplies.
:::
:::

::: check
given: W = 1920 | 3840; H = 1080 | 2160; B = 2 | 3 | 4; f = 30 | 60
answer: 2*W*H*B*f/1e6
unit: MB/s
tolerance: 0.5%

## A frame buffer holds {W} × {H} images at {B} bytes per pixel, written and read once per frame at {f} frames per second. What bandwidth does it need?

::: right
Right. Write plus read: {answer}.
:::

::: misses
- [W*H*B*f/1e6] Count both the write and the read.
- [any] Not yet. $2WHBf$.
:::
:::

::: check
given: k = 3 | 5 | 7 | 9; W = 1920 | 3840; t = 2200 | 4400
answer: (k - 1)/2*t
tolerance: 0.5%

## A {k} × {k} window pipeline on a {W}-wide image has a total line time (with blanking) of {t} pixel clocks. About how many clocks does its output lag the input, counting the $(k - 1)/2$ rows of delay to the window's center?

::: right
Right. About $(k - 1)/2$ lines, {answer} clocks, plus a few for the pipeline itself.
:::

::: misses
- [(k - 1)*t] The output pixel is the window's center: half the stored rows.
- [(k - 1)/2*W] A line takes {t} clocks including blanking, not {W}.
- [any] Not yet. Center row delay × line time.
:::
:::

::: check
kind: choice

## Which operation can't be done with a few line buffers in a raster-order streaming pipeline?

- [x] Rotating the image by 90°
- [ ] A 5 × 5 sharpening filter
- [ ] Converting RGB to YCbCr
- [ ] Detecting edges with a Sobel operator

::: right
Right. A 90° rotation turns rows into columns: the first output row needs the first pixel of every input row, the last of which arrives at the end of the frame. It needs a frame buffer.
:::

::: misses
- [2] A 5 × 5 window needs four line buffers.
- [3] Color conversion is per pixel: no buffers at all.
- [4] Sobel uses 3 × 3 windows: two line buffers.
:::
:::

::: check
kind: python
given: H = 6 | 9; W = 8 | 13; seed = 1 | 2

## Model a streaming 3 × 3 box filter. Generate an image `img = numpy.random.default_rng(seed).integers(0, 256, (H, W))` with `H` = {H}, `W` = {W}. Write `stream_box(img)`, which reads pixels in raster order one at a time, keeps two line buffers (Python lists) of the previous rows and a 3-column window, and outputs the integer sum of each fully valid 3 × 3 window (no padding), returning an `(H - 2) × (W - 2)` array. Set `out` and `ref`, the same sums computed directly with slicing, and `match = bool((out == ref).all())`.

```python starter
import numpy as np
img = np.random.default_rng(seed).integers(0, 256, (H, W))
out = np.zeros((H - 2, W - 2), dtype=int)
ref = out
match = True
```

```python solution
import numpy as np
img = np.random.default_rng(seed).integers(0, 256, (H, W))
def stream_box(img):
    H, W = img.shape
    lb1 = [0] * W   # row r-1
    lb2 = [0] * W   # row r-2
    out = np.zeros((H - 2, W - 2), dtype=int)
    for r in range(H):
        win = []
        for c in range(W):
            p = int(img[r, c])
            col = (lb2[c], lb1[c], p)
            lb2[c], lb1[c] = lb1[c], p
            win.append(col)
            if len(win) > 3:
                win.pop(0)
            if r >= 2 and c >= 2:
                out[r - 2, c - 2] = sum(sum(x) for x in win)
    return out
out = stream_box(img)
ref = sum(img[i:H - 2 + i, j:W - 2 + j] for i in range(3) for j in range(3))
match = bool((out == ref).all())
```

```python tests
import numpy as np
img = np.random.default_rng(seed).integers(0, 256, (H, W))
r = sum(img[i:H - 2 + i, j:W - 2 + j] for i in range(3) for j in range(3))
assert out.shape == (H - 2, W - 2) and (out == r).all(), "The streamed sums must equal the direct 3×3 sums."
assert match is True, "match should confirm the two agree."
```

::: right
Right. Two line buffers and a three-column window reproduce the direct computation exactly, touching each pixel once, as hardware must.
:::
:::

::: explain
## A camera team wants to add lens-distortion correction, which moves each output pixel's source by up to 40 rows, to a streaming pipeline that has only small line buffers. Explain the memory implications and options.

::: model
Distortion correction reads, for each output pixel, a source pixel at a position that can be up to 40 rows away from the output row, so the pipeline must hold at least about 80 rows (40 above and below) of the input in memory, or more if the displacement can be one-sided. With $W$ pixels per row and $b$ bits each, that's $80Wb$ bits: about 1.2 Mbit for 1920-wide 8-bit video, much more than a 3 × 3 filter's two lines but still far less than a frame, and feasible in on-chip RAM on a large FPGA or SoC. Alternatives are to store the frame in DRAM and fetch the needed regions with a cache, at the cost of bandwidth and latency, or to correct in tiles. The required number of buffered rows is set by the largest vertical displacement, which the lens design determines.
:::
:::

::: practice

::: check
given: k = 3 | 11; W = 1024 | 4096
answer: (k - 1)*W*8/8/1024
unit: KB
tolerance: 0.5%

## How many KB of line buffers does a {k} × {k} filter need on {W}-wide 8-bit images?

::: misses
- [k*W/1024] Only $k - 1$ rows are buffered.
- [any] $(k - 1)W$ bytes.
:::
:::

::: check
given: k = 3 | 5 | 7 | 9 | 11 | 15
answer: k/2
tolerance: 0.5%

## By what factor does a separable {k} × {k} filter cut the multiplies per pixel?

::: misses
- [k] It's $k^2/(2k)$.
- [any] $k^2/2k = k/2$.
:::
:::
:::
