Skip to content

Commit 8e7ded1

Browse files
Merge pull request #2 from rafaelcaricio/quality-enhancements
Phase 8: DCT transforms, intra/inter prediction, and quality enhancements
2 parents 66ad23c + 48903e1 commit 8e7ded1

19 files changed

Lines changed: 6231 additions & 203 deletions

CLAUDE.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,9 @@ Current phases: 1 [DONE] → 2+3 [DONE] → 4 [DONE] → 5 [DONE] → 6 [DONE]
4747
- base_q_idx = 128, qctx = 3, DC dequant = 140
4848
- MSAC: precarry buffer approach, bytes NOT XOR'd (carry resolved in finalize)
4949
- CDF format: cdf[i] = 32768 - cumulative_prob, last element = adaptation counter
50+
51+
### Phase 9 Reconstruction Drift Investigation
52+
- Root cause: intra prediction reference pixel handling bugs (NOT transform/MSAC/dequant)
53+
- MSAC, dequant tables, DCT implementations, scan orders, CDF updates all verified correct
54+
- Bisection showed DC/V/H/SMOOTH modes produce 0 diff; PAETH and directional modes cause drift
55+
- Key bugs: PAETH top_left pixel read from wrong location (z-order traversal issue), directional modes missing edge_flags-based extended reference pixel availability, frame-edge pixels must replicate last available pixel instead of falling back to 128
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Intra Prediction Modes — Visual Quality Enhancement
2+
3+
## Goal
4+
5+
Replace the single DC_PRED mode with per-pixel prediction using 7 intra modes (DC, V, H, SMOOTH, SMOOTH_V, SMOOTH_H, PAETH) with SAD-based mode selection. This is the single highest-impact quality improvement for keyframes.
6+
7+
## Current State
8+
9+
- All intra blocks use `dc_prediction()` which returns a **single u8 value** for the entire block
10+
- Residual = `source_pixel - flat_dc_value` for every pixel
11+
- Mode is hardcoded to 0 (DC_PRED) in the bitstream
12+
- Natural images with edges, gradients, and textures produce large residuals
13+
14+
## Design
15+
16+
### Prediction Functions
17+
18+
Each function takes reconstructed neighbor pixels and produces a full prediction block:
19+
20+
- **DC_PRED**: Average of above + left neighbors, fill entire block (existing behavior, but per-pixel output)
21+
- **V_PRED**: Copy above row into every row of the block
22+
- **H_PRED**: Copy left column into every column of the block
23+
- **SMOOTH_PRED**: Weighted average blending toward above-right and below-left corners
24+
- **SMOOTH_V_PRED**: Vertical-only smooth (blend top to bottom)
25+
- **SMOOTH_H_PRED**: Horizontal-only smooth (blend left to right)
26+
- **PAETH_PRED**: For each pixel, pick closest of (above, left, top-left) based on gradient
27+
28+
### Mode Selection
29+
30+
For each block, try all 7 modes and pick the one with lowest SAD (Sum of Absolute Differences) against the source pixels. SAD is chosen over SATD because it's fast and doesn't require a transform.
31+
32+
### Bitstream Encoding
33+
34+
- `kf_y_mode[above_mode][left_mode]` — encode chosen Y mode (0-12, using AV1 mode indices)
35+
- `uv_mode[cfl_idx][y_mode]` — encode chosen UV mode (initially same as Y)
36+
- Mode context arrays: track above/left modes per MI column/row
37+
38+
### AV1 Mode Index Mapping
39+
40+
| Mode | AV1 Index |
41+
|------|-----------|
42+
| DC_PRED | 0 |
43+
| V_PRED | 1 |
44+
| H_PRED | 2 |
45+
| SMOOTH_PRED | 9 |
46+
| SMOOTH_V_PRED | 10 |
47+
| SMOOTH_H_PRED | 11 |
48+
| PAETH_PRED | 12 |
49+
50+
Directional modes (3-8) are not included in this phase.
51+
52+
### Files Changed
53+
54+
- `src/tile.rs` — prediction functions, mode selection, encode_block, encode_skip_block, context tracking
55+
- `src/cdf.rs` — possibly adjust mode CDF context indexing (kf_y_mode already has 13x13 context)
56+
57+
### What This Does NOT Change
58+
59+
- Transform type (stays DCT_DCT)
60+
- Partition decisions (stays variance-based skip + forced 8x8 split)
61+
- Inter frames (stays GLOBALMV)
62+
- Chroma prediction (UV follows Y mode, no CFL)
63+
- Frame/sequence headers (no new flags needed)
64+
65+
### Validation
66+
67+
- All existing tests must pass (solid colors still pick DC_PRED)
68+
- Encode gradient/edge test patterns, decode with dav1d
69+
- Measure PSNR improvement on real image content

0 commit comments

Comments
 (0)