# lane hevcmisc: ec-h265 intra-only encode quality gate and Dolby Vision profile 8 decode Base: main f5eb855b. Two DONE.md items for ec-h265. ## 1. Encode: intra-only BD-PSNR vs x265 intra (DONE.md "intra-only mode") Criterion: intra-only mode at BD-PSNR >= -1.0 dB vs x265 intra (`-x265-params keyint=1`, preset veryfast). Measured with `scripts/hevc-bdrate.sh` at GOP 1 (keyint=1, min-keyint=1, scenecut=0, bframes=0). Clips are generated by `scripts/hevc-intra-bd.sh` (ffmpeg lavfi, no copyrighted media). 30 frames, QP 22/27/32/37, host c. The anchor now runs with `-tune psnr` (`X265_TUNE`, default `psnr`). The table also gives the x265 default-tune result for comparison. ### Anchor tuning (-tune psnr) psy-rd and aq trade PSNR for perceptual detail, and ec-h265 has neither. So `-tune psnr` is the matched anchor for a BD-PSNR comparison, and it is now the default. On this data it changes almost nothing. On 352x288 testsrc2 at QP 27, x265 default tuning gives 248294 bytes (Y 50.538 dB), and `-tune psnr` gives 247391 bytes (Y 50.468 dB). The BD results are the same to 0.02 %. ### Results | clip | anchor | BD-PSNR YUV (dB) | BD-PSNR Y (dB) | BD-rate YUV | BD-rate Y | |---|---|---|---|---|---| | 352x288 testsrc2 | x265 -tune psnr | +7.63 | +7.93 | -30.28 % | -31.20 % | | 352x288 testsrc2 | x265 default | +7.65 | +7.95 | -30.26 % | -31.15 % | | 1280x720 testsrc2 | x265 -tune psnr | +0.67 | +0.88 | -4.58 % | -5.99 % | | 1280x720 testsrc2 | x265 default | +0.73 | +0.94 | -4.96 % | -6.27 % | | 1280x720 natural (synthetic stand-in) | x265 -tune psnr | +0.18 | +0.19 | +43.01 % | +52.21 % | | 1280x720 natural (synthetic stand-in) | x265 default | +0.20 | +0.20 | +41.39 % | +51.56 % | Criterion (BD-PSNR >= -1.0 dB, all clips, both anchors): **MET**. No quality change was needed. ### Raw points, 352x288 testsrc2, QP 27 (one second, 30 frames: kbps = bytes x 8 / 1000) | encoder | bytes | kbps | PSNR-Y | PSNR-U | PSNR-V | PSNR-YUV (6:1:1) | |---|---|---|---|---|---|---| | x265 -preset veryfast -tune psnr | 247391 | 1979.13 | 50.4677 | 47.3297 | 47.3660 | 49.6877 | | x265 -preset veryfast (default tune) | 248294 | 1986.35 | 50.5380 | 47.3850 | 47.4277 | 49.7551 | | ec-h265 (hevc_gop, keyint 1) | 155185 | 1241.48 | 47.1493 | 43.1110 | 43.3397 | 46.1683 | PSNR is the per-frame mean of ffmpeg's `psnr` filter (what `scripts/ffmpeg-psnr.sh` prints). ffmpeg's summary line (from the global MSE) gives 50.38 and 47.16 for the two x265 decodes and ec, which differs from the mean by up to 0.1 dB. The table uses the per-frame mean. The full QP sweep is in the log of each run below. Commands (the source is `testsrc2-352x288.yuv`, generated with `ffmpeg -f lavfi -i testsrc2=size=352x288:rate=30 -frames:v 30 -pix_fmt yuv420p -f rawvideo`): ``` # x265 encode (anchor; the default-tune row omits -tune psnr) ffmpeg -v error -y -f rawvideo -pix_fmt yuv420p -s 352x288 -r 30 -i testsrc2-352x288.yuv \ -c:v libx265 -preset veryfast -tune psnr -qp 27 \ -x265-params bframes=0:keyint=1:min-keyint=1:scenecut=0:log-level=error -f hevc x265.hevc # x265 decode, then PSNR against the source ffmpeg -v error -y -i x265.hevc -f rawvideo -pix_fmt yuv420p x265.yuv ffmpeg -v error -f rawvideo -pix_fmt yuv420p -s 352x288 -i x265.yuv \ -f rawvideo -pix_fmt yuv420p -s 352x288 -i testsrc2-352x288.yuv \ -lavfi "psnr=stats_file=st-x265.txt" -f null - # ec-h265 encode (keyint 1, QP 27), reconstruction, then PSNR hevc_gop encode testsrc2-352x288.yuv 352 288 1 27 ec.hevc ec.recon.yuv ffmpeg -v error -y -i ec.hevc -f rawvideo -pix_fmt yuv420p ec.yuv cmp ec.yuv ec.recon.yuv # identical: ec's decode equals its reconstruction ffmpeg -v error -f rawvideo -pix_fmt yuv420p -s 352x288 -i ec.yuv \ -f rawvideo -pix_fmt yuv420p -s 352x288 -i testsrc2-352x288.yuv \ -lavfi "psnr=stats_file=st-ec.txt" -f null - ``` ### Checks on the measurement - **PSNR is not misaligned.** ffmpeg's `psnr` filter on each decode against the source gives the same per-plane means as the script (x265 Y 50.5380 / U 47.3850 / V 47.4277 default tune; ec Y 47.1493 / U 43.1110 / V 43.3397). The frame counts are 30 each, and `cmp` shows ec's decode equals its reconstruction. - **Pixel format and range** are yuv420p (tv range) on both sides, the same source file for both encoders. - **Bytes and kbps** come from the encoded file sizes (`stat -c %s`), not from a shared file. The 30-frame clip lasts 1 s at 30 fps. - **QP mapping.** ffmpeg's libx265 `-qp N` runs CQP-N, but x265 reports `Avg QP` of N-3 (intra offset), so x265's `-qp 27` codes at QP 24 on average. ec's QP is literal. BD-rate and BD-PSNR compare the curves, not matched QP, so this does not bias the comparison. - **x265 bytes change little with QP.** x265 at QP 12/22/27/32/37/42 gives 428791/292367/247391/205823/166524/137608 bytes, which is a 3x spread over 30 QP steps. ec's spread over its QP range is larger (see the table). This is the likeliest source of the large BD values, and it is what the x265 points say. I did not find a measurement error. - **Preset does not change the x265 curve here.** At QP 27 with `-tune psnr`, veryfast, medium and slow give 247391, 247307 and 242411 bytes. - **The BD-PSNR number follows from the points, not from the fit.** The rate overlap is 166524 to 198308 bytes. Linear interpolation of the two curves in log-rate gives +7.89 dB over that overlap, close to the cubic fit's +7.93. At ec's 155185 bytes (47.15 dB), x265 needs about 219 kB for the same PSNR, which is the -29 % BD-rate. The result is "ec is about 30 % smaller than x265 veryfast intra at equal PSNR on this clip", which is surprising, and I cannot explain it from the measurement. A stronger x265 preset does not move it. ### Notes - Intra-only does not use the fast IDR preset. `Encoder::new` applies `gop_fast_idr` only when `keyint > 1`, so keyint=1 runs the full intra tool set. No encoder source changed, so the GOP/CQP byte output is unchanged, and `scripts/hevc-gop-roundtrip.sh` and `scripts/hevc-rc.sh` were not re-run. - The natural clip is a generated stand-in (mandelbrot blended with testsrc2, plus noise). Its BD-PSNR is small and positive, but its BD-rate is +43 %. The R-D curves barely overlap (PSNR 34 to 40 dB), so the BD-rate there is unstable. BD-rate is not the intra criterion. - Running `scripts/hevc-intra-bd.sh` on all three clips takes about 10 minutes, which can exceed a 595 s foreground call. Run it detached on one server, or one clip at a time. Logs (host c): `~/hevcmisc-bd352.log` (tune psnr), `~/hevcmisc-bd352-default.log`, `~/hevcmisc-bd720.log`, `~/hevcmisc-bdnat.log`, `~/hevcmisc-bd720-default.log`, `~/hevcmisc-bdnat-default.log`. ## 2. Decode: Dolby Vision profile 8 base layer, RPU NAL units (62) ignored New test: `crates/ec-h265/tests/dolby_vision_p8.rs`, test `dolby_vision_p8_base_layer_decodes_and_rpu_is_ignored`. - Base layer: ffmpeg libx265 (`preset veryfast`, `keyint=4`, `bframes=0`, `scenecut=0`) of `testsrc2` 352x288, 12 frames. Generated in the test into `std::env::temp_dir()`. No copyrighted media. - Injection: a type 62 NAL unit with arbitrary escaped payload (zero runs and start-code-like bytes) is placed in front of every VCL NAL unit, each with a 4-byte start code. The test asserts that the count of type 62 NAL units equals the VCL count. - Assertions: - ec-h265 decodes the injected stream to the same 12 pictures as the clean stream, byte for byte. - ffmpeg decodes the injected stream to the same bytes as ec-h265. - Skips with a message when ffmpeg is absent, and fails under `EC_REQUIRE_FIXTURES`. - Not done: the dvcC/dvvC box. ec-h265 is an Annex B decoder with no mp4 path, so there is nothing to assert against. The existing `dec_meta.rs` test (`dolby_vision_nal_units_change_nothing`) also covers type 62/63 injection, against the JCTVC vectors when they are present. ## Verify - `cargo test --release -p ec-h265 --test dolby_vision_p8 -- --nocapture` (host c): `1 passed; 0 failed`. - `cargo clippy -p ec-h265 --all-targets -- -D warnings` (host a): exit 0. - `EC_H265_REF_YUV_DIR=$HOME/refyuv/hevcinter cargo test --release -p ec-h265 --test conformance_intra` (host a): `total: 9203 pictures checked, 9203 exact, 147/147 streams accepted`, 2 passed. - `cargo test --release -p ec-h265` (host a, detached): EXIT=0, 18 test binaries, all `ok`. - The new test file is about 170 lines, and no file is over 5000 lines. ## Changed files - `scripts/hevc-bdrate.sh`: anchor runs with `-tune psnr` by default (`X265_TUNE`); point files now carry psnr_u, psnr_v and kbps columns (BD fit reads only the first three). - `scripts/hevc-intra-bd.sh` (new): intra-only BD wrapper over `scripts/hevc-bdrate.sh`; takes a clip name or `all`. - `crates/ec-h265/tests/dolby_vision_p8.rs` (new): Dolby Vision profile 8 base-layer test. - `lanes/hevcmisc.report.md` (this file). ## MET / NOT MET - Encode intra-only BD-PSNR >= -1.0 dB vs x265 intra: **MET** on all three clips, with either anchor. - Decode DV profile 8 base layer bit-exact with RPU 62 ignored, vs ffmpeg: **MET** (mp4 dvcC/dvvC not covered). - Conformance host a 9203/9203 and 147/147: **MET**. ## Left - The natural clip is synthetic. A real natural clip is needed to judge the intra BD-rate. - The large intra BD-PSNR on testsrc2 (+7.6 dB at 352x288) is what the x265 points give, but it is surprising. I have not found a measurement error. An independent intra reference would settle it. - The dvcC/dvvC box has no mp4 path to test here.