edith_codecs
Clone
0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits
lane/hwrc2: intra-only rate overshoot and H.264 frame_num after IDR (ec-hw)
Base: main at 9acef7ad. Branch lane/hwrc2, worktree ~/.cache/wt/hwrc2. Local GPU (radeonsi,
/dev/dri/renderD128), -j4, CARGO_TARGET_DIR=~/.cache/tgt-hwrc2, TMPDIR=~/.cache/tmp-hwrc2.
Fixtures linked with scripts/link-fixtures.sh; gates run with EC_REQUIRE_FIXTURES=1.
1. Intra-only (GOP 1) rate overshoot
Cause
The HRD buffer. rate_control_buffers sized the buffer at one rate window (1000 ms of the request) for every
GOP. For GOP 1 that buffer lets radeonsi's CBR loop spend far above the request: every picture is an IDR, so
nothing later in the GOP pulls the budget back.
Measured, 720p testsrc2, 240 pictures, HEVC, GOP 1, request 3 Mb/s, buffer size varied:
| buffer | delivered | error |
|---|---|---|
| 1000 ms (old) | 7631 kb/s | +154% |
| 300 ms | 4206 kb/s (1 Mb/s request) | +321% |
| 100 ms | 8569 kb/s | +186% |
| 33 ms | 3285 kb/s | +9.5% |
| 20 ms | 3020 kb/s | +0.7% |
| 10 ms | 3020 kb/s | +0.7% |
| 5 ms | 3020 kb/s | +0.7% |
Not the cause (measured, same clip, HEVC GOP 1, 1 Mb/s request, old buffer): window_size 1000 vs 33 ms (identical
output, the driver ignores it in CBR), min/max QP 1..51 (identical), initial QP 1 or 51 (identical). Fixed QP 51
gives 922 kb/s, so the content is not a floor at 1 Mb/s for HEVC. For H.264 the floor is real: fixed QP 51 gives
1445 kb/s, so a 1 Mb/s request is below what H.264 can code at this clip (see the table below).
Buffer size per GOP (HEVC, requested 1/3/8 Mb/s; delivered vs request):
| GOP | buffer | 1 Mb/s | 3 Mb/s | 8 Mb/s |
|---|---|---|---|---|
| 1 | 1000 ms (old) | +321% | +154% | +50% |
| 1 | 20 ms | +2.0% | +0.7% | +0.3% |
| 2 | 1000 ms | +9.4% | ||
| 2 | 20 ms | +0.3% | ||
| 5 | 1000 ms | +4.3% | ||
| 5 | 20 ms | +0.1% | ||
| 10 | 1000 ms | +1.8% | ||
| 10 | 20 ms | +0.1% | ||
| 30 | 1000 ms | +1.1% | +0.9% | +0.9% |
| 30 | 20 ms | +0.0% (40.1 dB vs 43.5 dB at 1000 ms, 3 Mb/s) |
A 20 ms buffer at GOP 30 is exact on the rate but costs 3.4 dB at the same rate, so GOP 30 keeps the full window.
Fix
hrd_buffer_ms(gop_size) in crates/ec-hw/src/enc/mod.rs: 20 ms for a GOP under 10 pictures, the 1000 ms rate
window otherwise. The same HRD buffer is still "full at the start". The rate window constant is now named
RATE_WINDOW_MS. RateControl.window_size is unchanged (the driver ignores it in CBR, as measured).
Gate: crates/ec-hw/tests/intra_rate.rs
Each encode runs in a child process of the test binary (see section 3). (cbr_lands_within_five_percent_on_intra_only_and_gop_30)
testsrc2 1280x720 30 fps, 240 pictures (8 s), CBR, GOP 1 and 30, requests 1, 3, 8 Mb/s, three codecs. Each
encode runs in its own process (see section 3). "floor" is the same clip at the coarsest quantiser (QP 51 for
H.264/HEVC, base_q_idx 255 for AV1, in constant-QP mode). A point below the floor passes when it lands within 5%
of the floor, marked "(at QP 51)".
Final run (cargo test -p ec-hw --release --test intra_rate -- --nocapture), PSNR from ffmpeg psnr:
| codec | GOP | request | delivered | error | floor | PSNR |
|---|---|---|---|---|---|---|
| H.264 | 1 | 1000 kb/s | 1446 kb/s | +44.6% | 1445 kb/s (at QP 51) | 31.43 dB |
| H.264 | 1 | 3000 kb/s | 3008 kb/s | +0.3% | 1445 | 34.87 dB |
| H.264 | 1 | 8000 kb/s | 8008 kb/s | +0.1% | 1445 | 44.16 dB |
| H.264 | 30 | 1000 kb/s | 1010 kb/s | +1.0% | 593 | 37.48 dB |
| H.264 | 30 | 3000 kb/s | 3033 kb/s | +1.1% | 593 | 44.04 dB |
| H.264 | 30 | 8000 kb/s | 8166 kb/s | +2.1% | 593 | 57.23 dB |
| HEVC | 1 | 1000 kb/s | 1059 kb/s | +5.9% | 1010 kb/s (at QP 51, +4.9%) | 31.84 dB |
| HEVC | 1 | 3000 kb/s | 3020 kb/s | +0.7% | 1010 | 34.74 dB |
| HEVC | 1 | 8000 kb/s | 8020 kb/s | +0.3% | 1010 | 42.53 dB |
| HEVC | 30 | 1000 kb/s | 1011 kb/s | +1.1% | 86 | 38.09 dB |
| HEVC | 30 | 3000 kb/s | 3027 kb/s | +0.9% | 86 | 43.46 dB |
| HEVC | 30 | 8000 kb/s | 8070 kb/s | +0.9% | 86 | 55.15 dB |
| AV1 | 1 | 1000 kb/s | 1004 kb/s | +0.4% | 386 | 31.47 dB |
| AV1 | 1 | 3000 kb/s | 3004 kb/s | +0.1% | 386 | 36.45 dB |
| AV1 | 1 | 8000 kb/s | 8004 kb/s | +0.0% | 386 | 43.89 dB |
| AV1 | 30 | 1000 kb/s | 1010 kb/s | +1.0% | 73 | 36.60 dB |
| AV1 | 30 | 3000 kb/s | 3034 kb/s | +1.1% | 73 | 42.58 dB |
| AV1 | 30 | 8000 kb/s | 8007 kb/s | +0.1% | 73 | 57.56 dB |
Result: test passes (1 passed). The H.264 1 Mb/s GOP 1 point is not reachable: 1445 kb/s is H.264's QP 51
stream for this clip. The HEVC 1 Mb/s GOP 1 point sits 4.9% above its QP 51 floor on the final run (earlier runs
of the same point gave +2.0%; see the finding below).
Reference: ffmpeg *_vaapi on the same driver (-g 1 or -g 30, -b:v, -rc_mode CBR, 240 frames, 720p testsrc2)
| codec | GOP | request 1M | request 3M | request 8M |
|---|---|---|---|---|
| h264_vaapi | 1 | 5377 kb/s (+438%) | 9058 kb/s (+202%) | 12692 kb/s (+59%) |
| h264_vaapi | 30 | 995 kb/s (-0.5%) | 2944 kb/s (-1.9%) | 7912 kb/s (-1.1%) |
| hevc_vaapi | 1 | 4172 kb/s (+317%) | 7583 kb/s (+153%) | 11876 kb/s (+48%) |
| hevc_vaapi | 30 | 999 kb/s (-0.1%) | 2939 kb/s (-2.0%) | 7877 kb/s (-1.6%) |
| av1_vaapi | 1 | 1642 kb/s (+64%) | 4098 kb/s (+37%) | 9847 kb/s (+23%) |
| av1_vaapi | 30 | 979 kb/s (-2.1%) | 2941 kb/s (-2.0%) | 7861 kb/s (-1.7%) |
So ffmpeg's vaapi encoders do not reach +/-5% at GOP 1 on this driver (they overshoot as ec-hw did before this lane). The ec-hw path now lands within the +/-5% gate on every point except H.264 at 1 Mb/s, where the request is below what H.264 can code at QP 51 on this clip (ffmpeg's H.264 at that request is 5.4 Mb/s).
2. H.264 frame_num and POC across IDR
Fix (crates/ec-hw/src/enc/h264.rs, crates/ec-hw/src/enc/mod.rs)
frame_numof a non-IDR picture isgop_position % 16, wheregop_positioncounts from the last IDR (it wascoded_count % 16, which never restarted). The IDR hasframe_num0.- The reference picture's
frame_numis(frame_num + 16 - 1) % 16. The oldframe_num.wrapping_sub(1)on au16gave 65535 forframe_num0 after a wrap. - POC type 2:
TopFieldOrderCnt = 2 * gop_position, the reference's ispoc - 2. Both restart at the IDR (were2 * coded_count). idr_pic_idalternates between consecutive IDRs (7.4.3). It was(gop_position / gop) % 2, which is 0 for every IDR, so GOP 1 wrote the same id on every picture. The encoder now counts IDRs (Encoder::idr_count).Encoder::coded_countand its counter were removed (no other user).
Tests: crates/ec-hw/tests/idr_counting.rs (new, 3 tests, all pass)
h264_frame_num_and_idr_pic_id_restart_at_every_idr: 60 pictures, IDR every 7; every slice header read with ffmpegtrace_headers:frame_numequals(t % 7) % 16for all 60 slices (IDR 0 at t = 0, 7, ..., 56),idr_pic_idis0,1,0,1,...over the 9 IDRs, no POC lsb is written (type 2). Decoded withffmpeg -v error(empty stderr), every picture decodes in order (PSNR >= 30 dB against its source), andffmpeg -v debugprints no line with "gap" (ffmpeg's message isFrame num gap %d %d).h264_intra_only_stream_is_one_idr_per_picture: GOP 1, 12 pictures:frame_num0 everywhere,idr_pic_idalternates on every picture, decodes clean.hevc_poc_lsb_restarts_at_every_idr: same stream shape in HEVC:slice_pic_order_cnt_lsbequalst % 7for the non-IDR slices and no lsb is written for IDRs; decodes in order.
The H.264 test's expected values change at the first IDR: the old code writes frame_num 7 at picture 7 and the test
expects 0. The old-code run of this test was not repeated after the change (main's h264.rs calls the
coded_count that this lane removes). The pre-fix evidence is hwgaps' measurement: frame_num 13 after an IDR at
picture 60 with a 16 modulus.
edith-side
The engine-hw slice_fix::SliceCounters::fix_au repair is in edith's tree, not this repo. It can be removed
once edith is on this commit; that change is not made here.
3. Finding: driver state carries across encodes in one process (not per display)
The first run of the pair failed cbr_lands_within_five_percent_on_intra_only_and_gop_30 at HEVC GOP 1, 1 Mb/s:
+5.2% against a coarsest-quantiser floor that itself varied between processes (983 to 1049 kb/s), so the point
sat on the floor-band edge. Measured:
| encodes before the point, same process | HEVC fixed QP 51 stream (kB) |
|---|---|
| none (fresh process, every time) | 922 |
| H.264 QP 51 first | 964 |
| H.264 GOP 30 then H.264 GOP 1 first | 1003 |
| same, each encode on a fresh VA display | 960, 1010, 957 |
Rate-controlled HEVC GOP 1 at 1 Mb/s, same pattern: 1020 kb/s alone, 1033 to 1118 kb/s after other encodes in the
process. Opening a fresh Display per encode does not isolate it. The effect is process-wide: no static or
cache in ec-va or ec-hw holds it, so it sits below them (most likely Mesa's per-device winsys, which libva shares
across displays in one process; not confirmed).
Decision: Encoder does not open its own display, and does not reset RC state by default. Neither isolates the
state, so neither would change the result. The caller can only isolate by process.
Gate fix: intra_rate.rs re-runs its own test binary once per encode (one_encode_in_its_own_process), so each
measurement starts from the same driver state. The points and the +/-5% gate are unchanged. Ten back-to-back runs
of idr_counting and intra_rate: all 10 passed; HEVC GOP 1 at 1 Mb/s read 1020 kb/s every run.
Gates
cargo clippy --no-deps -p ec-hw -p ec-va --lib --tests -- -D warnings: exit 0. Theec-av1warnings printed in the log are a dependency's and are not errors under this gate.EC_REQUIRE_FIXTURES=1 cargo test -p ec-hw -p ec-va --release -- --skip av1_encode_agrees --skip av1_10bit --skip bd_row --skip av1_encode_speed --skip real_library --skip long_decode: exit 0.- ec-hw lib: 17 passed
- ec-hw
tests/gpu.rs: 33 passed, 8 filtered (skipped by the command) - ec-hw
tests/idr_counting.rs(new): 3 passed - ec-hw
tests/intra_rate.rs(new): 1 passed - ec-va lib: 8 passed; ec-va
tests/gpu.rs: 14 passed; plus 1 + 2 + 4 from the remaining ec-va targets - The existing
cbr_lands_within_five_percent_of_the_request(GOP 30, 240 frames) passes with the new buffer rule for GOP 30 (unchanged).
Left
- The H.264 1 Mb/s GOP 1 request is below what this clip can code in H.264 (floor 1445 kb/s). Not fixable in the rate controller; the test accepts it at the floor.
- The 20 ms buffer at GOP 30 costs 3.4 dB; GOP 30 keeps the 1000 ms window. The threshold of 10 pictures is measured at GOP 1, 2, 5, 10 and 30 on HEVC only; H.264 and AV1 were measured at GOP 1 and 30.
- The cross-encoder carry-over (section 3) is a driver effect; not changed in ec-hw.
- The edith
slice_fixrepair removal (section 2). - Test files: the scratch
rc_probeused for the sweeps was deleted before commit.