forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

av1split.plan.md374 lines, 24.9 KiBHistoryRawDownload
1# av1split: module split plan for crates/ec-av1
2
3Lane: `av1splitplan` (analysis only, no code changed). Base: `main` @ 382b318b.
4Source of the requirement: `DONE.md` (no source file over 5,000 lines before further AV1 work).
5Line numbers below are as of base 382b318b. They shift after every step, so each step also
6names its region by first and last item.
7
8## 0. Size table (crates/ec-av1, all .rs under src/, examples/; no tests/ or benches/ dir exists)
9
10| File | Lines | Over 5,000 |
11|---|---:|---|
12| src/decode.rs | 61,650 | yes |
13| src/stream.rs | 57,178 | yes |
14| src/encode.rs | 23,771 | yes |
15| src/tile.rs | 11,516 | yes |
16| src/qm.rs | 7,046 | yes |
17| src/encoder.rs | 6,503 | yes |
18| src/cdf.rs | 5,519 | yes |
19| src/mvstack.rs | 3,820 | no |
20| all other src/*.rs | < 3,600 | no |
21| examples/*.rs (largest decode_probe.rs) | 532 | no |
22
23Seven files need splitting: decode, stream, encode, tile, qm, encoder, cdf.
24
25## 1. Cross-cutting constraints (apply to every step)
26
27**1a. Source-scanning tests read the files by path.** These break when a file moves or
28shrinks, so they must be repointed before any move:
29- `src/gate_coverage.rs`: `include_str!("stream.rs")` at 548, 1076, 1993, 2488, 2583, 2663;
30 and `("stream.rs", include_str!("stream.rs"))`, `("decode.rs", ...)`, `("encode.rs", ...)` at 1584-1586.
31- `src/decode.rs` itself: `include_str!("decode.rs")` at 13697, 13796, 31180, 31850, 57769, 58221, 58912, 60134.
32- `src/stack_budget.rs` `FILES` table, lines 445-~500: one `include_str!` per src file (the
33 `every_by_value_module_is_scanned` test requires that new modules are listed).
34- `refusal_inventory::pins_refusal(src, ..)` consumes the text returned by those includes.
35Total: about 70 `include_str!` sites in gate_coverage.rs, stack_budget.rs and decode.rs.
36
37Fix (Step P0, before any move): add one test helper that concatenates every `*.rs` under a
38module directory (`env!("CARGO_MANIFEST_DIR")/src/<name>/` plus `<name>.rs` if it exists) and
39replace each `include_str!("X.rs")` with that helper's output. The helper must cover the
40new `decode/`, `stream/`, `encode/`, `tile/`, `qm/`, `cdf/`, `encoder/` directories. The same
41step adds the new files to `stack_budget::FILES`. Check the 5 probes that use
42`ec_av1::decode::` / `ec_av1::encode::` (examples/*.rs, tools/ec-bench, crates/ec-hw/tests/gpu.rs):
43public paths must not change.
44
45**1b. File-to-directory rename.** `decode.rs` -> `decode/mod.rs` (same for stream, encode, tile, qm,
46encoder, cdf). `lib.rs` keeps `pub mod decode;`, so paths stay the same.
47
48**1c. Inline `mod x { ... }` -> `mod x;` with a file.** Child modules see the parent's private
49items, so moving a test module to a file needs no visibility change. Do this first; it is the
50cheapest large move.
51
52**1d. Production code moved to a sibling module.** Every item it uses from another new sibling
53must go from private to `pub(super)`. Every item that was `pub` must be re-exported with
54`pub use <child>::*;` (or an explicit path) in `mod.rs`, so `crate::decode::X` and
55`ec_av1::decode::X` keep working. Items are listed under section 4.
56
57**1e. Imports.** Each new module needs `use super::*;` (or the explicit names) plus
58`use crate::hits::{hit, hit_do};` where it uses the hit macros.
59
60**1f. Macros.** `decode.rs` has no top-level `macro_rules!`. Its three `istep!` definitions
61(12148, 20844, 26638) are function-local, so they need no ordering or `#[macro_use]`. `hit!` and
62`hit_do!` come from `crate::hits` (`#[macro_export]`-free path import). Nothing in ec-av1 needs
63`#[macro_use]`. stream.rs and encode.rs have no top-level macros either.
64
65**1g. Order of moves.** Within a file, extract regions from the bottom up (highest line number
66first). Then the line ranges of regions not yet moved stay valid. Do every step with the file
67compiling: `cargo check -p ec-av1` plus the source-scan tests.
68
69## 2. decode.rs (61,650 lines)
70
71### 2a. Top-level map (approximate end = next item start - 1)
72
73| Lines | Region | Notes |
74|---|---|---|
75| 1-~357 | imports, FrameCtx (216-~222 impls), core structs | |
76| ~357-8492 | partition/seg/palette/recon helpers, read_coeffs fns | 3,624 lines are hit-counter fns (section 2c) |
77| 8493-9129 | `read_coeffs`, `read_coeffs_rect` | |
78| 9241-11564 | `struct Neighbours` and `impl Neighbours` (2,072 lines) | one impl, private state |
79| 11565-12083 | filter-intra, palette index map helpers | |
80| 12084-16934 | `read_intra_mode_rect`, `decode_rect_split` (1,080), `decode_intra_rect_in_inter`, `decode_intrabc_rect`, `decode_intrabc_128rect`, `decode_block_rect`, `decode_leaf_rect` | 4,850 lines, tight |
81| 16935-20788 | `decode_block_rect4` (788), `decode_rect4_16_intrabc`, `decode_intrabc_owned_rect`, `decode_rect4_16_strip` (989), `decode_block_rect64` (703) | |
82| 20789-24729 | `read_intra_mode`, `read_angle_delta`, CFL consts/fns, `impl PlaneBuf` (378), `read_plane`, `decode_block` (1,448) | |
83| 24730-26796 | `decode_block_128rect` (491), `decode_leaf8` (1,257), `read_intra_mode_sub8` | |
84| 26797-29818 | `sub8_leaf_chroma444` (689), `sub8_leaf_chroma422` (447), `decode_leaf_split4` (689), `decode_leaf_rect8` (1,158) | |
85| 29819-31336 | CDEF consts and fns (`CDEF_DIRECTIONS`, `cdef_find_dir`, `cdef_adjust_strength`, `cdef_filter_block*`), `mod cdef_simd` (30215-30469, inline) | |
86| 31337-33053 | `read_block_tx_size` (204), `read_inter_rect_chroma` (208), `INTER_BLOCK_SHAPES`, `rect_inter_residual_supported`, `edge_params_body` | |
87| 33054-34521 | `deblock_thresholds`, `sclamp`, `round_pow2`, `pooled`, `mod deblock_simd` (33383-33827, inline, 445 lines) | |
88| 34522-36564 | `replay_filters`, `replay_final`, `replay_inner` (257), `cdef_band` (212) | |
89| 36565-39260 | `decode_key_frame_tile_lr`, `decode_key_frame_tiles_lr`, `decode_key_frame_tile_with_cdfs` (2,696) | |
90| 39261-42377 | `switchable_interp_ctx`, `resolve_interp_filter`, `intra_inter_ctx`, `ref_chroma_shape`, `ref_planes`, `overlappable_above`, `obmc_run` (41605-41815), `read_compound_ref_frames`, `read_inter_compound_mode`, `assign_compound_mv`, `build_gm_mv_table` | 3,116 lines |
91| 41528-~41600 | `#[cfg(test)] mod obmc_skip_chroma_tests` (inline) | |
92| 42378-48099 | `fn decode_inter_block` (5,722 lines, one function) | **over 5k as a single function**, see 2d |
93| 48100-51108 | `decode_inter_sub8_split4` (994), `decode_intra_sub8_leaf` (874), `decode_inter_sub8_rect2` (1,141) | 3,009 lines |
94| 51109-54360 | `grid_stamp_rect`, `decode_inter_block8` (2,935) | |
95| 54361-57728 | `decode_inter_frame_tile`, `decode_inter_frame_tile_lr`, `decode_inter_frame_tiles_lr`, `decode_inter_frame_tile_with_cdfs` | 3,368 lines |
96| 57728-57729+ | `#[cfg(test)] mod tests` (57729-61558, 3,830 lines), `#[cfg(test)] mod tx4x8_table_tests` (61559-61650) | |
97
98### 2b. Test modules
99- `mod tests` 57729-61558 (3,830 lines, `use super::*`) -> `decode/tests.rs`, via `#[cfg(test)] mod tests;` (rule 1c). Under 5k, so no further split.
100- `mod tx4x8_table_tests` 61559-61650 -> `decode/tx4x8_table_tests.rs`, or fold into tests.rs.
101- `mod obmc_skip_chroma_tests` 41528-~41600 -> `decode/obmc_skip_chroma_tests.rs`.
102- Scattered `#[test]` fns inside production regions (13661-14548 block, 7999, 8244, 13753, 34356-34380). Leave them with their region. They are small.
103- Test helpers at column 0 (`#[cfg(test)] fn` at 7999, 8244, 13753) stay with their region.
104
105### 2c. Hit-counter / census functions (cross-cutting)
106- 318 top-level items whose names match `hits|counter|census|_hit|reset_` (about 4,255 lines). They are
107 spread through 1-8492 (3,624), 20789-24729 (257), 29819-34521 (97+156) and 41605-42377 (82).
108- They are instrumentation (`hit!` calls). Move them all to `decode/counters.rs` by name, and set
109 `pub(super)` (they are called from production modules). The `mod.rs` re-exports them with `pub use counters::*;`.
110- Also: `tile.rs` has the same pattern (`COMPOUND_*_HITS` statics and `take_*_hits` fns, 1720-1762).
111 Move those into a shared module when the tile split runs.
112
113### 2d. Blocker: `decode_inter_block` is 5,722 lines in one function
114A file cap does not help if one function is over 5k. Before its file can move, the body must be
115split into helper functions, with the phase blocks (mode read, MV/ref setup, prediction, residual,
116recon) pulled out one by one. This is a behaviour-preserving refactor. Gate it with the decode
117pins: `ec-av1` tests (decode pixel-exact tests and `gate_coverage`). Plan steps D9a/D9b below.
118
119## 3. stream.rs (57,178 lines)
120
121- Production 1-2328 (about 2.3k): `decode_stream`, `decode_stream_with`, `decode_stream_with_threads`
122 (649-1383), `SeqFlags`, `RefSnapshot`, `FrameOutput`, `decode_frame` (1546-2307), `Reorder`, `SlotPromise`, plus
123 about 85 counter fns (71-390, 2308-2328). No split needed: `stream.rs` -> `stream/mod.rs` only (optionally move the counters).
124- `#[cfg(test)] pub(crate) mod tests` 2329-57178 (54,849 lines, 397 `#[test]` fns plus about 140
125 helper fns / statics / structs).
126
127### 3a. Test chunks (cut at ` #[test]` boundaries, all under 5k)
128| Chunk | Lines | Anchor (first item) |
129|---|---|---|
130| T1 | 2329-6884 | `pub(crate) mod tests`, `test_card` (2334) |
131| T2 | 6885-11454 | `decode_stream_matches_the_tile_path_on_a_key_frame` |
132| T3 | 11455-16256 | `a_420_lossless_alt_ref_1to4_strip_witness...` |
133| T4 | 16257-20952 | `a_16x4_intrabc_pair_strip_decodes_pixel_exact` |
134| T5 | 20953-25719 | `a_real_aomenc_10bit_intra_tools_stream_decodes_pixel_exact` |
135| T6 | 25720-30250 | `a_real_aomenc_inter_sequence_with_32x32_level_1to4_strips...` |
136| T7 | 30251-34861 | `a_real_aomenc_stream_with_reference_select_reads_comp_mode_correctly` |
137| T8 | 34862-39404 | `a_real_aomenc_10bit_stream_with_a_32_level_ab_partition...` |
138| T9 | 39405-44038 | `real_aomenc_film_grain_streams_decode_pixel_exact` |
139| T10 | 44039-48708 | `a_422_muchunk_iis_128root_grad128_comp_stream_is_exact...` |
140| T11 | 48709-53366 | `the_chroma_rect_gates_excluded_seed_46_decodes_pixel_exact` |
141| T12 | 53367-57178 | `a_lossless_444_10bit_inter_stream...` (`FrameFacts` at 53604) |
142
143### 3b. Shared helpers
144Helpers used across chunks (`test_card`, `panned_test_card`, `gop_round_trips`, `read_pin`,
145`stream_bit_depth`, `chroma_shape`, `assert_420_oracle_stream`, `panic_message`, `pack_rawvideo`,
146`assert_rawvideo_matches`, `fnv1a64`, `have_ffmpeg`, `ffmpeg_decode_*`, `libaom_encode*`,
147`encode_aomenc_stream*`, `census_attempt`, `*_gate` fns, `GATE_COUNTER_LOCK`,
148`lock_gate_counters`) are not `#[test]`. They sit inside the chunks above. Step S2 moves all of
149them into `stream/tests/support.rs` as `pub(super)`. The `gate` fns (intra_rect_in_inter_gate
15022228, tx_select_inter_gate 22525, ..., edge32_gate 44519, run_multi_tile_gate 47907) are helpers too.
151
152Fix to `use`: each chunk file starts with `use super::super::*;` and `use super::support::*;`
153(`super` = `stream::tests`). Where a test calls `crate::stream::decode_stream` directly, nothing changes.
154
155## 4. Cross-cutting private items that must go pub(super)/pub(crate)
156
157decode.rs (found by the planned module boundaries):
158- Structs: `FrameCtx` (216), `TxParams`, `Residual`, `Grid` (Deref at 3521), `WaveQueue`, `WaveGuard`,
159 `ReconInline`, `PlaneBuf` (impl at 22459 and 39547), `PlaneGuard`, `Neighbours` (9241), `TxClass` (7858),
160 `ObmcPlan`, `ReducedTxRestore`, `ReducedRestore`, `PipeSync`, `PipeGuard` (35524-35535).
161- Fns used by several regions: `unsupported` (7743), `ss_x`, `ss_y`, `sb_mi_cur`, `intra_inter_ctx`,
162 `switchable_interp_ctx`, `read_intra_mode`, `read_plane`, `read_coeffs`, `read_block_tx_size`,
163 `decode_block`, `decode_leaf8`, `decode_block_rect4`, `decode_inter_block`, `decode_intra_sub8_leaf`,
164 `cdef_band`, `replay_filters`, `obmc_run`, `obmc_skip_chroma_above`, `whole_plane`.
165- Consts used across: `CDEF_*` (28661-29965), `INTER_BLOCK_SHAPES`, `UV_CFL_PRED`, `MI`, `SUB`,
166 `SB_MI`, etc. The `use super::{CDEF_BORDER, ...}` lines in `mod cdef_simd` / `mod deblock_simd` show which ones.
167- `cdef_simd` and `deblock_simd` (inline mods, `#[allow(unsafe_code)]`) move with their attributes to files
168 `decode/cdef_simd.rs` and `decode/deblock_simd.rs`. Their `use super::{...}` list needs `pub(super)` on the consts it imports.
169
170stream.rs: `Reorder`, `SlotValue`, `SlotPromise`, `PromiseState`, `LeafResult`, `collect_one`, `SeqFlags`,
171`RefSnapshot`, `FrameOutput`, `stored_cdfs_for`, `decode_frame`, the `FRAMES_DISPATCHED` static (487), and the
172`final_dump_prefix` / `set_final_dump_prefix` pair (57-66). Only the test chunks move, so these stay in
173`stream/mod.rs` and need no visibility change. The test files reach them through `super::super::*`.
174
175encode.rs: `Plane` (2653), `CoefCtx`/`CoefCtxMap` (2562/2585), `At` (3782), `Reach` (4037), `Ibc` (6094),
176`Search` (8495), `Trial` (8720), `VertAbGuard` (3908), `EncodedSequence` (15713), and the `SPLIT_CENSUS` static (5004).
177
178tile.rs: `BlockCoeffs`, `PaletteY`, `Coeff`, `write_palette_syntax`, `write_inter_frame_leaf`, `write_compound_block` (1782), `palette_uv_side`.
179
180## 5. Module layouts and steps
181
182Module sizes below are approximate; the executor re-counts with `wc -l` before each step.
183
184### 5a. decode.rs -> decode/
185Proposed layout (every module under 5k):
186
187| Module | Source lines (base) | ~Lines | Depends on |
188|---|---|---:|---|
189| decode/mod.rs | 1-8492 minus counters (~4,850 left) + `pub use` re-exports | 4,850 | (top of tree) |
190| decode/counters.rs | 318 hit/census items (2c) | 4,255 | `crate::hits` |
191| decode/coeffs.rs | 8493-12083 (`read_coeffs*`, `Neighbours`, filter-intra, palette index) | 3,590 | mod.rs types |
192| decode/rect.rs | 12084-16934 | 4,850 | coeffs, mod.rs |
193| decode/rect4.rs | 16935-20788 | 3,850 | rect, block |
194| decode/block.rs | 20789-24729 minus counters (~3,680) | 3,680 | coeffs, rect, leaf8 |
195| decode/leaf8.rs | 24730-26796 | 2,070 | block, coeffs |
196| decode/leaf_sub8.rs | 26797-29818 | 3,020 | leaf8, block |
197| decode/cdef.rs | 29819-31336 + cdef_simd (30215-30469) | 1,520 | mod.rs consts |
198| decode/inter_shapes.rs | 31337-33053 | 1,720 | coeffs (read_coeffs), mod.rs |
199| decode/deblock.rs | 33054-34521 + deblock_simd (33383-33827) | 1,470 | mod.rs |
200| decode/replay.rs | 34522-36564 minus counters (~1,890) | 1,890 | cdef, deblock |
201| decode/key_tile.rs | 36565-39260 | 2,700 | block, leaf8, replay, deblock, cdef |
202| decode/inter_ctx.rs | 39261-42377 (ctx fns, ref fns, obmc_run) | 3,120 | mod.rs, counters |
203| decode/inter_block.rs | 42378-48099 (after 2d refactor, split across 2 files) | 5,722 -> ~2 x 2.9k | inter_ctx, block |
204| decode/inter_sub8.rs | 48100-51108 | 3,010 | inter_block, block |
205| decode/inter_block8.rs | 51109-54360 | 3,250 | inter_block, inter_sub8 |
206| decode/frame_tile.rs | 54361-57728 | 3,370 | key_tile, inter_block8, replay |
207| decode/tests.rs | 57729-61558 (inline mod tests) | 3,830 | everything (via `super::*`) |
208| decode/tx4x8_table_tests.rs | 61559-61650 | 90 | |
209| decode/obmc_skip_chroma_tests.rs | 41528-~41600 | 75 | inter_ctx |
210
211Dependency rule: no module depends on a module that comes later in the table, except through
212`mod.rs` re-exports. Check with `cargo check` after each step.
213
214### 5b. stream.rs -> stream/
215- stream/mod.rs: production 1-2328 (about 2.3k). No split.
216- stream/tests/mod.rs: `mod support; mod t01; ... mod t12;`
217- stream/tests/support.rs: all non-`#[test]` helpers (section 3b), about 1.5k lines. Estimated; the exact figure comes from step S2.
218- stream/tests/t01.rs ... t12.rs: chunks T1-T12 (about 4.5k each, minus the helpers moved out).
219
220### 5c. encode.rs -> encode/ (23,771 lines)
221Production 1-15867 (15.9k) plus tests 15868-23771 (7.9k).
222
223| Module | Source lines | ~Lines |
224|---|---|---:|
225| encode/mod.rs | 1-2548 (Picture 1576, Encoded 1902, CdfSnapshot 1894) | 2,550 |
226| encode/coef.rs | 2549-5003 (CoefCtx 2562, Plane 2653, At 3782) | 2,460 |
227| encode/split.rs | 5004-8334 (SPLIT_CENSUS 5004, `split_census_on`, `code_square_inter`) | 3,330 |
228| encode/inter_search.rs | 8335-13021 (`record_mi*`, Ibc 6094 may move here, Search 8495, `search_inter_block` 10164-11894) | 4,690 |
229| encode/frame.rs | 13022-15866 (TPL_STRENGTH, `deltaq_res_log2`, `encode_inter_frame`, `encode_sequence_with_ctx` 15713) | 2,850 |
230| encode/tests.rs (A) | 15868-19800 (first half of tests) | 3,930 |
231| encode/tests_b.rs (B) | 19801-23771 (second half, cut at a `#[test]` boundary) | 3,970 |
232
233### 5d. tile.rs -> tile/ (11,516 lines)
234| Module | Source lines | ~Lines |
235|---|---|---:|
236| tile/mod.rs | 1-2509 (includes compound hit statics 1720-1762 unless moved to a counters module) | 2,510 |
237| tile/coeff_syntax.rs | 2510-5008 (`BlockCoeffs`, `PaletteY`, `Coeff`, `write_palette_syntax`) | 2,500 |
238| tile/palette.rs | 5009-8394 (`palette_uv_side`, `PALETTE_HITS` static 5009, `sb_coeff_inter_frame_tile_cdfs`) | 3,390 |
239| tile/leaf.rs | 8395-8957 (`write_inter_frame_leaf`, `write_inter_frame_leaf8`) | 560 |
240| tile/tests.rs | 8958-11516 (inline `mod tests`) | 2,560 |
241
242### 5e. qm.rs -> qm/ (7,046 lines)
243Production is one static literal: `static IWT: [[[u8; 3344]; 2]; 15] = [ ... ]` (139-6937, 6,799
244lines, 15 `// qm level k` blocks). It has to become per-level constants:
245- qm/mod.rs: `NUM_QM_LEVELS`, `AOM_QM_BITS`, `Iqm`, `adjusted`, `shape_range`, `iwt_matrix`, `dqv_at`, and
246 `static IWT: [[[u8;3344];2];15] = [IWT_L0, ..., IWT_L14];`.
247- qm/iwt_a.rs: `const IWT_L0..IWT_L4: [[u8; 3344]; 2]` (levels 0-4, about 2.3k).
248- qm/iwt_b.rs: levels 5-9 (about 2.3k). qm/iwt_c.rs: levels 10-14 (about 2.3k).
249- qm/tests.rs: 6938-7046 (`mod tests`).
250
251### 5f. encoder.rs -> encoder/ (6,503 lines)
252- encoder/mod.rs: 1-2557 (2,556 lines; `Av1Encoder` impl 1506).
253- encoder/tests.rs: 2558-6503 inline `mod tests` (3,946 lines).
254
255### 5g. cdf.rs (5,519 lines)
256- cdf.rs stays at 1-3724 (3,724 lines) and 3850-5519 becomes cdf/tables_tail.rs (1,670 lines, const tables, `pub use` back).
257- `#[cfg(test)] mod tests` 3725-3849 (124 lines) -> cdf/tests.rs.
258
259## 6. Ordered extraction steps
260
261Each step moves at most 6k lines, leaves the crate compiling, and is verified with `cargo check -p ec-av1 --all-targets`
262and `cargo test -p ec-av1 --lib -- --list` (the count must not change). Test runs happen on the
263server per the orchestration doc.
264
265### Phase 0 (prerequisite, all files)
266- **P0.** Add the directory-aware source reader in the test helper. Replace the ~70 `include_str!`
267 sites in gate_coverage.rs, stack_budget.rs, decode.rs (section 1a). Add the new file names to `stack_budget::FILES`.
268 Moves 0 lines. Makes the later steps safe.
269
270### decode.rs
271- **D1.** `git mv src/decode.rs src/decode/mod.rs`. 0 lines moved.
272- **D2.** Move `mod tests` 57729-61558 to `decode/tests.rs` and `mod tx4x8_table_tests` 61559-61650 to
273 `decode/tx4x8_table_tests.rs`. 3,920 lines moved. Also `mod obmc_skip_chroma_tests` 41528-~41600 to a file (75 lines).
274- **D3.** Move the 318 counter items (2c) to `decode/counters.rs` (about 4.3k). They are spread across
275 the file, so the step is a name-based script. Set `pub(super)`, add `pub use counters::*;` in mod.rs.
276- **D4.** Move `mod cdef_simd` 30215-30469 and `mod deblock_simd` 33383-33827 to files (700 lines). Make the
277 consts they import `pub(super)`.
278- **D5.** Move 54361-57728 (`decode_inter_frame_tile*`) to `decode/frame_tile.rs` (3,370 lines). The step must
279 import `key_tile` names. It moves first because nothing below depends on it.
280- **D6.** Move 51109-54360 (`grid_stamp_rect`, `decode_inter_block8`) to `decode/inter_block8.rs` (3,250 lines).
281- **D7.** Move 48100-51108 (`decode_inter_sub8_split4`, `decode_intra_sub8_leaf`, `decode_inter_sub8_rect2`)
282 to `decode/inter_sub8.rs` (3,010 lines).
283- **D8a.** (Blocker) Refactor `decode_inter_block` (42378-48099, 5,722 lines) in place: extract its phase blocks
284 into named helper fns in the same file, until no single fn is over 5k. Gate: decode pins and gate_coverage.
285 Moves 0 lines to new files.
286- **D8b.** Move the refactored `decode_inter_block` and its helpers to `decode/inter_block.rs`, split into two
287 files if needed (each under 5k). About 5.7k moved; within the 6k limit.
288- **D9.** Move 39261-42377 (ctx/ref fns, `obmc_run`) to `decode/inter_ctx.rs` (3,120 lines).
289- **D10.** Move 36565-39260 (`decode_key_frame_tile*`) to `decode/key_tile.rs` (2,700 lines).
290- **D11.** Move 34522-36564 (`replay_*`, `cdef_band`), counters removed, to `decode/replay.rs` (1,890 lines).
291- **D12.** Move 33054-34521 (`deblock_thresholds`, `sclamp`, `round_pow2`, `pooled`) to `decode/deblock.rs` (1,470 lines).
292- **D13.** Move 31337-33053 (`read_block_tx_size`, `read_inter_rect_chroma`, `INTER_BLOCK_SHAPES`, ...) to
293 `decode/inter_shapes.rs` (1,720 lines).
294- **D14.** Move 29819-31336 (`CDEF_*` consts, `cdef_find_dir`, `cdef_adjust_strength`, `cdef_filter_block*`) to
295 `decode/cdef.rs` (1,520 lines).
296- **D15.** Move 26797-29818 (`sub8_leaf_chroma444/422`, `decode_leaf_split4`, `decode_leaf_rect8`) to
297 `decode/leaf_sub8.rs` (3,020 lines).
298- **D16.** Move 24730-26796 (`decode_block_128rect`, `decode_leaf8`, `read_intra_mode_sub8`) to `decode/leaf8.rs` (2,070 lines).
299- **D17.** Move 20789-24729 (`read_intra_mode`, CFL, `PlaneBuf` impl, `read_plane`, `decode_block`, counters removed) to
300 `decode/block.rs` (3,680 lines).
301- **D18.** Move 16935-20788 (`decode_block_rect4`, `decode_rect4_16_*`, `decode_intrabc_owned_rect`,
302 `decode_block_rect64`) to `decode/rect4.rs` (3,850 lines).
303- **D19.** Move 12084-16934 (`read_intra_mode_rect`, `decode_rect_split`, `decode_intra_rect_in_inter`,
304 `decode_intrabc_rect`, `decode_intrabc_128rect`, `decode_block_rect`, `decode_leaf_rect`) to `decode/rect.rs` (4,850 lines,
305 tight; if it is over, move `decode_intrabc_*` to its own file).
306- **D20.** Move 8493-12083 (`read_coeffs`, `read_coeffs_rect`, `Neighbours` struct and impl, filter-intra and palette index helpers)
307 to `decode/coeffs.rs` (3,590 lines).
308- **D21.** Final check: `decode/mod.rs` should be 1-~8492 minus counters, about 4,850 lines, and under 5k.
309 Confirm every `pub` item of the old decode.rs is re-exported.
310
311Decode total: 22 steps (D1-D21, with D8 split into D8a and D8b). Largest single move: D8b (about 5.7k).
312
313### stream.rs
314- **S1.** `git mv src/stream.rs src/stream/mod.rs` (0 lines moved).
315- **S2.** Move every non-`#[test]` helper in `stream::tests` (section 3b) to `stream/tests/support.rs` as `pub(super)`.
316 Create `stream/tests/mod.rs` with `mod support;`. About 1.5k lines moved. (Lines are name-based; re-measure.)
317- **S3.** Move T1 (`stream` tests chunk 2329-6884 minus helpers, about 4.0k) to `stream/tests/t01.rs`.
318- **S4-S14.** One step per chunk: T2 (6885-11454), T3 (11455-16256), T4 (16257-20952), T5 (20953-25719),
319 T6 (25720-30250), T7 (30251-34861), T8 (34862-39404), T9 (39405-44038), T10 (44039-48708),
320 T11 (48709-53366), T12 (53367-57178). Each moves about 4.6k lines.
321- **S15.** Remove the now-empty `#[cfg(test)] pub(crate) mod tests` header and confirm `stream/mod.rs` is about 2.3k lines.
322
323Stream total: 15 steps (S1-S15), each under 5k moved.
324
325### encode.rs
326- **E1.** `git mv` to `encode/mod.rs` (0 lines).
327- **E2.** Move `mod tests` 15868-19800 to `encode/tests.rs` (about 3.9k).
328- **E3.** Move 19801-23771 to `encode/tests_b.rs` (about 4.0k).
329- **E4.** Move 13022-15866 (`TPL_STRENGTH`, `encode_inter_frame`, `EncodedSequence`, `encode_sequence_with_ctx`) to `encode/frame.rs` (2,850).
330- **E5.** Move 8335-13021 (`record_mi*`, `Search`, `search_inter_block`) to `encode/inter_search.rs` (4,690).
331- **E6.** Move 5004-8334 (`SPLIT_CENSUS`, `split_census_on`, `code_square_inter`) to `encode/split.rs` (3,330).
332- **E7.** Move 2549-5003 (`CoefCtx`, `Plane`, `At`, `Reach`) to `encode/coef.rs` (2,460).
333- **E8.** Check that `encode/mod.rs` is about 2.5k lines, and under 5k.
334
335Encode total: 8 steps.
336
337### tile.rs
338- **T1.** `git mv` to `tile/mod.rs`.
339- **T2.** Move `mod tests` 8958-11516 to `tile/tests.rs` (2,560).
340- **T3.** Move 5009-8394 (`palette_uv_side`, `PALETTE_HITS`, `sb_coeff_inter_frame_tile_cdfs`) to `tile/palette.rs` (3,390).
341- **T4.** Move 8395-8957 (`write_inter_frame_leaf*`) to `tile/leaf.rs` (560).
342- **T5.** Move 2510-5008 (`BlockCoeffs`, `PaletteY`, `Coeff`, `write_palette_syntax`) to `tile/coeff_syntax.rs` (2,500).
343
344Tile total: 5 steps.
345
346### encoder.rs
347- **N1.** Move `mod tests` 2558-6503 to `encoder/tests.rs` (3,946). Keep `encoder.rs` as `encoder/mod.rs` (2,556 lines).
348
349Encoder total: 1 step (plus the rename).
350
351### qm.rs
352- **Q1.** Move `mod tests` 6938-7046 to `qm/tests.rs`.
353- **Q2.** Convert `static IWT` levels 0-4 (about 2.3k lines) into `qm/iwt_a.rs` as consts, and make the outer
354 `static IWT` in `qm/mod.rs` reference them.
355- **Q3.** Levels 5-9 to `qm/iwt_b.rs`.
356- **Q4.** Levels 10-14 to `qm/iwt_c.rs`. Confirm `qm/mod.rs` is under 1k.
357
358qm total: 4 steps. Q2-Q4 must keep the same bytes: check the output of `iwt_matrix` with an existing
359pinned test before and after.
360
361### cdf.rs
362- **C1.** Move 3725-3849 (`mod tests`) to `cdf/tests.rs`.
363- **C2.** Move 3850-5519 (const tables) to `cdf/tables_tail.rs` (1,670), `pub use` back into `cdf/mod.rs`.
364
365cdf total: 2 steps.
366
367## 7. Summary
368
369- Files needing a split: 7 (decode, stream, encode, tile, qm, encoder, cdf).
370- Steps: 1 prerequisite (P0) + decode 22 + stream 15 + encode 8 + tile 5 + encoder 1 + qm 4 + cdf 2 = 58 steps.
371- Blocker: `decode_inter_block` (5,722 lines, one fn) needs a behaviour-preserving refactor (D8a) before its file move.
372- Risk: about 70 `include_str!` source scanners (P0 must land first).
373- Risk: `tile.rs` / `decode.rs` / `encode.rs` counter statics and `take_*_hits` fns are referenced from
374 tests in other modules; keep the names and re-exports.