|
| 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