still265 0.6.1

Pure-Rust HEVC/H.265 still-image encoder with BPG and HEIC support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
//! Centralized StillSearch environment gates.
//!
//! Keep StillSearch-only env parsing here so experimental knobs are discoverable
//! without grepping the search implementation. These are process-global and are
//! cached on first use; set them before starting an encode.
//!
//! Each function accepts a template fallback value so the search code reads from
//! the effort template by default but a developer can still override via env var.

use std::sync::OnceLock;

use crate::rdoq::RdoqPricingMode;

/// Set to any value to collect `EncodeStats::stillsearch_ledger_ns` bucket
/// timings in addition to call counts.
pub(super) const PROFILE: &str = "BPG_STILLSEARCH_PROFILE";

/// `0` restores the old behavior: every rough-shortlisted luma mode gets full
/// recursive TU search. Any other value / unset enables the x265-style cheap
/// luma-only first pass followed by full TU search for one winner.
pub(super) const LUMA_CHEAP: &str = "BPG_STILLSEARCH_LUMA_CHEAP";

/// Diagnostic RD lambda multiplier. Values below 1.0 spend more bits for lower
/// distortion; values above 1.0 save bits. Defaults to 1.0.
pub(super) const LAMBDA_SCALE: &str = "BPG_STILLSEARCH_LAMBDA_SCALE";

/// Experimental SSIM-RD-style candidate cost. This is deliberately separate
/// from AQ: it only changes mode/TU/CBF ranking by adding a source-normalized
/// residual-energy term to the normal SSE+rate cost. Off by default.
pub(super) const SSIM_RD: &str = "BPG_STILLSEARCH_SSIM_RD";

/// Use RDOQ, not hard quantization, during search/materialization trials.
/// x265 placebo (`-m9`) has rdoq-level=2 enabled during RD search, while
/// still265 historically ran RDOQ only as a final winner recode. `1` forces
/// RDOQ trials everywhere, `0` forces hard quant everywhere; unset (or
/// `effort`) defers to the effort template's per-stage `TrialQuant` policy
/// (Rdoq on slow/placebo, HardQuant on fast). Measured 2026-07-02:
/// −2.8% Y / −2.7% RGB BD-rate on the small native set at +46% encode time.
pub(super) const RDOQ_SEARCH: &str = "BPG_STILLSEARCH_RDOQ_SEARCH";

/// One-pass exact evaluation: `decide_tt` (Phase 1) quantizes with the effort
/// policy's trial quant (RDOQ on slow/placebo), and the Phase-2 RDOQ re-run of
/// exact candidates is skipped as redundant. This halves exact-stage work and
/// explores TU splits under RDOQ (closer to x265 rdoq-level 2, which never
/// evaluates a candidate twice). `0` restores the two-phase hard-quant→RDOQ
/// flow. On by default.
pub(super) const RDOQ_ONE_PASS: &str = "BPG_STILLSEARCH_RDOQ_ONE_PASS";

/// Experimental post-RDOQ residual-pricing model:
/// `frozen|running|hybrid|exact-cleanup|bounded-trellis`, or `0|1|2|3|4`.
/// Defaults to `frozen`, preserving the current single-scan RDOQ output.
pub(super) const RDOQ_PRICING: &str = "BPG_RDOQ_PRICING";

/// Beam width for `BPG_RDOQ_PRICING=bounded-trellis` (default 4, clamped
/// internally by the optimizer).
pub(super) const RDOQ_TRELLIS_BEAM: &str = "BPG_RDOQ_TRELLIS_BEAM";

/// Diagnostic: disable winner-final RDOQ recode and commit hard-quantized
/// search coefficients. x265 rdoq-level=0 A/B is a useful quant/RDO parity
/// reference; this isolates still265's final RDOQ contribution.
pub(super) const FINAL_RDOQ: &str = "BPG_STILLSEARCH_FINAL_RDOQ";

/// Diagnostic only: sample RDOQ-coded block residual pricing rows. Set
/// `BPG_STILLSEARCH_RDOQ_AUDIT=1` and optionally
/// `BPG_STILLSEARCH_RDOQ_AUDIT_MOD=N` (default 1024; 1 prints every block).
pub(super) const RDOQ_AUDIT: &str = "BPG_STILLSEARCH_RDOQ_AUDIT";
pub(super) const RDOQ_AUDIT_MOD: &str = "BPG_STILLSEARCH_RDOQ_AUDIT_MOD";

/// Single-block forensic dump target. `BPG_STILL_DUMP=x,y,log2,c_idx` makes
/// `eval_component_8_from_src_pred` print the full residual pipeline (source,
/// prediction, residual, forward coeffs, quantised levels, dequant, inverse
/// residual, recon, distortion) for every candidate mode whose block geometry
/// matches, so it can be diffed coefficient-by-coefficient against x265. The
/// fourth field (c_idx) is optional and defaults to 0 (luma).
pub(super) const STILL_DUMP: &str = "BPG_STILL_DUMP";

/// Diagnostic transform-tree split controls. These target the observed x265
/// parity gap where C emits many more luma TUs, especially 4x4 TUs in textured
/// regions. All are off by default.
pub(super) const TU_MIN_SPLIT_LOG2: &str = "BPG_STILLSEARCH_TU_MIN_SPLIT_LOG2";
pub(super) const TU_SPLIT_BIAS_PER_PX: &str = "BPG_STILLSEARCH_TU_SPLIT_BIAS_PER_PX";
pub(super) const TU_4X4_BIAS_PER_PX: &str = "BPG_STILLSEARCH_TU_4X4_BIAS_PER_PX";
pub(super) const TU_FORCE_SPLIT_LOG2: &str = "BPG_STILLSEARCH_TU_FORCE_SPLIT_LOG2";

/// Targeted 8x8->4x4 TU split admission. Unlike the global min-split/bias
/// probes, this only admits extra 4x4 split evaluation when local evidence says
/// the 8x8 leaf is a poor fit.
pub(super) const TU_TARGET_4X4: &str = "BPG_STILLSEARCH_TU_TARGET_4X4";
pub(super) const TU_TARGET_VAR_MIN: &str = "BPG_STILLSEARCH_TU_TARGET_VAR_MIN";
pub(super) const TU_TARGET_COST_MIN: &str = "BPG_STILLSEARCH_TU_TARGET_COST_MIN";
pub(super) const TU_TARGET_MEAN_RANGE_MIN: &str = "BPG_STILLSEARCH_TU_TARGET_MEAN_RANGE_MIN";

/// Diagnostic CU split controls. These increase the number of luma CUs/TUs to
/// probe the x265 gap where bpgenc emits many more small partitions.
pub(super) const CU_SPLIT_BIAS_PER_PX: &str = "BPG_STILLSEARCH_CU_SPLIT_BIAS_PER_PX";
pub(super) const CU_FORCE_SPLIT_LOG2: &str = "BPG_STILLSEARCH_CU_FORCE_SPLIT_LOG2";

/// Decision-identical CU split branch-and-bound: abort remaining split
/// children once the partial split cost already exceeds the leaf-first bound
/// (the split can no longer win the leaf-vs-split compare). `0` disables.
pub(super) const CU_SPLIT_BOUND: &str = "BPG_STILLSEARCH_CU_SPLIT_BOUND";

/// Same branch-and-bound at the transform-tree level (`decide_tt` leaf-first
/// split evaluation). `0` disables.
pub(super) const TU_SPLIT_BOUND: &str = "BPG_STILLSEARCH_TU_SPLIT_BOUND";

/// Diagnostic PartNxN controls. PartNxN creates four 4x4 luma PUs/TUs inside
/// an 8x8 CU, matching one path by which x265 emits many more 4x4 luma TUs.
pub(super) const NXN_BIAS_PER_PX: &str = "BPG_STILLSEARCH_NXN_BIAS_PER_PX";
pub(super) const NXN_FORCE: &str = "BPG_STILLSEARCH_NXN_FORCE";

/// Multiplier for the experimental SSIM-RD residual-energy term.
pub(super) const SSIM_RD_WEIGHT: &str = "BPG_STILLSEARCH_SSIM_RD_WEIGHT";

/// Sweep-script overrides for the psy-rd / psy-rdoq config values
/// ([`crate::StillHevcConfig::psy_rd`] / `psy_rdoq`). When set, the env value
/// replaces the config value; both are clamped to their sane ranges
/// (psy-rd 0..=5, psy-rdoq 0..=50) on resolve.
pub(in crate::encoder) const PSY_RD: &str = "BPG_PSY_RD";
pub(in crate::encoder) const PSY_RDOQ: &str = "BPG_PSY_RDOQ";

/// Residual syntax pricing mode used only by the luma-cheap first pass.
/// Defaults to `exact`. Set to `skip` or `0` to rank cheap luma candidates
/// without exact residual syntax bits; the selected winner is still remeasured
/// with exact pricing in the full pass.
pub(super) const LUMA_CHEAP_RESIDUAL_PRICE: &str = "BPG_STILLSEARCH_LUMA_CHEAP_RESIDUAL_PRICE";

/// Experimental: when set (`1`/`on`), the cheap-pass exact residual pricing uses
/// the static-context (frozen-state, x265 `EstBitsSbac`-style) bit estimate
/// instead of the evolving-CABAC re-encode. A/B knob for the residual-pricing
/// speed/quality lever; off by default.
pub(super) const CHEAP_BITS_STATIC: &str = "BPG_STILLSEARCH_CHEAP_BITS_STATIC";

/// Skip PartNxN for 8×8 CUs whose rough SATD (best mode, from RoughLuma) is
/// below this threshold. A rough SATD of T means the block's best-mode
/// prediction error is < T / 64 per pixel on average.
/// Default 1000: smooth blocks skip NxN (PSNR is unchanged or slightly better
/// for smooth content; saves ~3s on a 12MP encode). Set to 0 to disable.
pub(super) const NXN_SKIP_SATD: &str = "BPG_STILLSEARCH_NXN_SKIP_SATD";

/// x265-parity CABAC context evolution during StillSearch RD pricing.
///
/// x265 chains entropy contexts through the coding tree during analysis: each
/// child CU is priced from the previous sibling's post-coding contexts
/// (`compressIntraCU`'s `nextContext`), `codeIntraLumaQT` evolves the coder
/// TU-to-TU with per-depth save/restore, and the RDOQ rate model reads those
/// evolved states. still265 historically priced every trial from a context
/// frozen at CTU entry (`price_base`), overestimating residual bits mid-CTU by
/// a measured 12–37%.
///
/// Values: `1`/unset = full (search + finalize), `0`/`off` = disabled
/// (byte-identical to the frozen-context behavior), `search` = search-time
/// chaining only, `finalize` = decoder-order final-RDOQ contexts only.
pub(super) const CTX_EVOLVE: &str = "BPG_STILLSEARCH_CTX_EVOLVE";

/// x265-parity CU-header bit pricing: include `intra_chroma_pred_mode` bits
/// for every CU leaf and `part_mode` bits for every 8x8 CU in the leaf RD
/// cost, matching x265 `checkIntra`'s full-syntax `totalBits`. Historically
/// these were only priced inside the 8x8 2Nx2N-vs-NxN comparison, so CU
/// splits (4 headers vs 1) were under-priced. `0` restores the old pricing.
pub(super) const CU_HEADER_BITS: &str = "BPG_STILLSEARCH_CU_HEADER_BITS";

/// x265-parity CU-level chroma intra mode search (`estIntraPredChromaQT`):
/// full-RD evaluation of the five allowed chroma modes
/// {planar, vertical, horizontal, DC, DM} for each CU leaf, instead of
/// always signalling DM. `0` disables (DM-only, the historical behavior).
pub(super) const CHROMA_MODE_SEARCH: &str = "BPG_STILLSEARCH_CHROMA_MODES";

/// Chroma-mode search optimizer knobs. The mode search gate above still
/// controls whether chroma modes are searched at all.
///
/// `CHROMA_ROUGH=0` restores exhaustive exact RD over all non-DM modes.
/// `CHROMA_ROUGH_K` is the number of non-DM modes promoted from the rough SATD
/// pass to exact RD (1..=4; default from the effort template).
/// `CHROMA_SKIP_SATD` is a per-chroma-sample rough-SATD threshold below which
/// DM is kept immediately (0 disables; default from the effort template).
/// `CHROMA_REUSE_DM=0` restores the old behavior of rebuilding DM winners
/// instead of replaying the already-materialized DM residual contexts.
pub(super) const CHROMA_ROUGH: &str = "BPG_STILLSEARCH_CHROMA_ROUGH";
pub(super) const CHROMA_ROUGH_K: &str = "BPG_STILLSEARCH_CHROMA_ROUGH_K";
pub(super) const CHROMA_SKIP_SATD: &str = "BPG_STILLSEARCH_CHROMA_SKIP_SATD";
pub(super) const CHROMA_REUSE_DM: &str = "BPG_STILLSEARCH_CHROMA_REUSE_DM";

/// Rough-pass audit: when set, logs per-CU statistics about rough vs exact
/// ranking and angular-mode inclusion to stderr.  Sampled to avoid flooding
/// large images; the sampling stride is controlled by `ROUGH_AUDIT_MOD`.
pub(super) const ROUGH_AUDIT: &str = "BPG_STILLSEARCH_ROUGH_AUDIT";

/// When set, the rough luma search uses scalar `predict_into` for angular
/// modes instead of the batched `predict_all_angular_into_u16` primitive.
/// Slower, but isolates whether the batched angular predictor path is
/// numerically biased.
pub(super) const SCALAR_ANGULAR_ROUGH: &str = "BPG_STILLSEARCH_SCALAR_ANGULAR_ROUGH";

/// Rough-stage mode-bit cost weight (default 1.0).  Set to 0 to disable mode
/// bits in rough ranking, isolating whether planar/DC signalling advantage
/// is suppressing angular candidates too early.
pub(super) const ROUGH_MODE_BIT_WEIGHT: &str = "BPG_STILLSEARCH_ROUGH_MODE_BIT_WEIGHT";

/// Diagnostic x265shape RMD admission window. x265 uses 1.25. Larger values
/// are not canonical; they test whether Still265's current rough cost model is
/// losing oracle winners before simple RDO can rank them.
pub(super) const X265_RMD_WINDOW: &str = "BPG_STILLSEARCH_X265_RMD_WINDOW";

/// Diagnostic x265shape RMD candidate cap. x265 placebo uses
/// `2 + rdLevel + (depth >> 1)`. Larger values are not canonical; they test
/// whether Still265 rough ranking is pushing oracle winners below x265's cap.
pub(super) const X265_RMD_CAP: &str = "BPG_STILLSEARCH_X265_RMD_CAP";

/// Diagnostic only: when set alongside the luma oracle, print one `RMD_AUDIT:`
/// line per oracle sample comparing Still265's fractional mode-bit RMD rank
/// with x265-like integer `bitsCodeBin()` RMD ranks, including carry brackets.
pub(super) const X265_RMD_AUDIT: &str = "BPG_STILLSEARCH_X265_RMD_AUDIT";

/// Diagnostic only: use the no-carry integer x265 `calcRdSADCost` approximation
/// for x265shape RMD candidate admission. This is not the default because
/// x265's real `bitsCodeBin()` depends on the current fractional CABAC carry.
pub(super) const X265_RMD_INTEGER_COST: &str = "BPG_STILLSEARCH_X265_RMD_INTEGER_COST";

/// Diagnostic x265shape exact remeasure expansion. Default top=1 matches x265shape;
/// higher values remeasure multiple simple-RDO-ranked modes with split-enabled
/// exact search, optionally gated by variance and close-cost margin.
pub(super) const X265SHAPE_EXACT_TOP: &str = "BPG_STILLSEARCH_X265SHAPE_EXACT_TOP";
pub(super) const X265SHAPE_EXACT_MARGIN: &str = "BPG_STILLSEARCH_X265SHAPE_EXACT_MARGIN";
pub(super) const X265SHAPE_EXACT_VAR_MIN: &str = "BPG_STILLSEARCH_X265SHAPE_EXACT_VAR_MIN";

/// Diagnostic only: when set alongside the luma oracle, print one
/// `MISS_AUDIT:` line per sampled oracle miss with RMD admission and
/// simple-RDO ranking context.
pub(super) const LUMA_MISS_AUDIT: &str = "BPG_STILLSEARCH_LUMA_MISS_AUDIT";

/// Oracle diagnostic: when set, sampled CUs get full `decide_tt()` run on the
/// entire shortlist AND on all 35 modes, reporting the true-best winner.
/// Output is CSV-compatible lines on stderr bearing the `ORACLE:` prefix.
pub(super) const LUMA_ORACLE: &str = "BPG_STILLSEARCH_LUMA_ORACLE";

/// Sampling modulus for the luma oracle (default 64).  Every N-th CU is
/// sampled; higher values are less noisy but converge more slowly.
pub(super) const LUMA_ORACLE_MOD: &str = "BPG_STILLSEARCH_LUMA_ORACLE_MOD";

#[inline]
pub(super) fn profile_enabled() -> bool {
    static ENABLED: OnceLock<bool> = OnceLock::new();
    *ENABLED.get_or_init(|| std::env::var_os(PROFILE).is_some())
}

#[derive(Clone, Copy, PartialEq, Eq)]
struct CtxEvolve {
    search: bool,
    finalize: bool,
}

#[inline]
fn ctx_evolve() -> CtxEvolve {
    static VALUE: OnceLock<CtxEvolve> = OnceLock::new();
    *VALUE.get_or_init(|| {
        let raw = std::env::var(CTX_EVOLVE).unwrap_or_default();
        match raw.trim().to_ascii_lowercase().as_str() {
            "0" | "off" | "none" => CtxEvolve {
                search: false,
                finalize: false,
            },
            "search" => CtxEvolve {
                search: true,
                finalize: false,
            },
            "finalize" => CtxEvolve {
                search: false,
                finalize: true,
            },
            _ => CtxEvolve {
                search: true,
                finalize: true,
            },
        }
    })
}

/// Search-time CABAC context chaining (CU/TU sibling-to-sibling pricing).
#[inline]
pub(super) fn ctx_evolve_search() -> bool {
    ctx_evolve().search
}

/// Decoder-order evolving contexts for the winner-only RDOQ finalize pass.
#[inline]
pub(super) fn ctx_evolve_finalize() -> bool {
    ctx_evolve().finalize
}

/// Full CU-header (`part_mode` + `intra_chroma_pred_mode`) bit pricing in CU
/// leaf RD costs (x265 `checkIntra` parity). Default on.
#[inline]
pub(super) fn cu_header_bits_enabled() -> bool {
    static ENABLED: OnceLock<bool> = OnceLock::new();
    *ENABLED.get_or_init(|| {
        std::env::var(CU_HEADER_BITS)
            .map(|v| v.trim() != "0")
            .unwrap_or(true)
    })
}

/// CU-level full-RD chroma intra mode search (x265 `estIntraPredChromaQT`
/// parity). Default on.
#[inline]
pub(super) fn chroma_mode_search_enabled() -> bool {
    static ENABLED: OnceLock<bool> = OnceLock::new();
    *ENABLED.get_or_init(|| {
        std::env::var(CHROMA_MODE_SEARCH)
            .map(|v| v.trim() != "0")
            .unwrap_or(true)
    })
}

/// Enable SATD rough pre-selection for chroma mode search. Default on.
#[inline]
pub(super) fn chroma_rough_enabled() -> bool {
    static ENABLED: OnceLock<bool> = OnceLock::new();
    *ENABLED.get_or_init(|| {
        std::env::var(CHROMA_ROUGH)
            .map(|v| v.trim() != "0")
            .unwrap_or(true)
    })
}

/// Number of non-DM chroma modes to exact-evaluate after rough ranking.
#[inline]
pub(super) fn chroma_rough_k(template_k: u8) -> usize {
    static VALUE: OnceLock<Option<usize>> = OnceLock::new();
    let env_val = *VALUE.get_or_init(|| {
        std::env::var(CHROMA_ROUGH_K)
            .ok()
            .and_then(|v| v.trim().parse::<usize>().ok())
            .map(|v| v.clamp(1, 4))
    });
    env_val.unwrap_or(usize::from(template_k.clamp(1, 4)))
}

/// Per-sample SATD threshold below which chroma search keeps DM immediately.
#[inline]
pub(super) fn chroma_skip_satd(template_threshold: f64) -> f64 {
    static VALUE: OnceLock<Option<f64>> = OnceLock::new();
    let env_val = *VALUE.get_or_init(|| {
        std::env::var(CHROMA_SKIP_SATD)
            .ok()
            .and_then(|v| v.trim().parse::<f64>().ok())
            .filter(|v| v.is_finite() && *v >= 0.0)
    });
    env_val.unwrap_or(template_threshold.max(0.0))
}

/// Reuse the already-materialized DM chroma blocks for the baseline cost and
/// avoid a full DM rebuild when DM wins. Default on.
#[inline]
pub(super) fn chroma_reuse_dm_enabled() -> bool {
    static ENABLED: OnceLock<bool> = OnceLock::new();
    *ENABLED.get_or_init(|| {
        std::env::var(CHROMA_REUSE_DM)
            .map(|v| v.trim() != "0")
            .unwrap_or(true)
    })
}

#[inline]
pub(super) fn x265_rmd_audit_enabled() -> bool {
    static ENABLED: OnceLock<bool> = OnceLock::new();
    *ENABLED.get_or_init(|| std::env::var_os(X265_RMD_AUDIT).is_some())
}

#[inline]
pub(super) fn x265_rmd_integer_cost_enabled() -> bool {
    static ENABLED: OnceLock<bool> = OnceLock::new();
    *ENABLED.get_or_init(|| std::env::var_os(X265_RMD_INTEGER_COST).is_some())
}

#[inline]
pub(super) fn rdoq_audit_sample(x0: u32, y0: u32, log2_size: u8, c_idx: u8, mode: u8) -> bool {
    static ENABLED: OnceLock<bool> = OnceLock::new();
    if !*ENABLED.get_or_init(|| std::env::var_os(RDOQ_AUDIT).is_some()) {
        return false;
    }

    static MOD: OnceLock<u64> = OnceLock::new();
    let modulus = *MOD.get_or_init(|| {
        std::env::var(RDOQ_AUDIT_MOD)
            .ok()
            .and_then(|v| v.trim().parse::<u64>().ok())
            .filter(|&v| v > 0)
            .unwrap_or(1024)
    });
    if modulus <= 1 {
        return true;
    }

    // Stable integer mix: enough to spread neighboring blocks/modes without
    // pulling in a RNG or process-global state.
    let mut h = u64::from(x0 / 4).wrapping_mul(0x9E37_79B1);
    h ^= u64::from(y0 / 4).wrapping_mul(0x85EB_CA77);
    h ^= u64::from(log2_size).wrapping_mul(0xC2B2_AE3D);
    h ^= u64::from(c_idx).wrapping_mul(0x27D4_EB2F);
    h ^= u64::from(mode).wrapping_mul(0x1656_67B1);
    h % modulus == 0
}

#[inline]
pub(super) fn x265shape_exact_top() -> usize {
    static VALUE: OnceLock<usize> = OnceLock::new();
    *VALUE.get_or_init(|| {
        std::env::var(X265SHAPE_EXACT_TOP)
            .ok()
            .and_then(|v| v.trim().parse::<usize>().ok())
            .filter(|&v| v >= 1)
            .unwrap_or(1)
    })
}

#[inline]
pub(super) fn x265shape_exact_margin() -> f64 {
    static VALUE: OnceLock<f64> = OnceLock::new();
    *VALUE.get_or_init(|| {
        std::env::var(X265SHAPE_EXACT_MARGIN)
            .ok()
            .and_then(|v| v.trim().parse::<f64>().ok())
            .filter(|v| v.is_finite() && *v >= 1.0)
            .unwrap_or(f64::INFINITY)
    })
}

#[inline]
pub(super) fn x265shape_exact_var_min() -> f64 {
    static VALUE: OnceLock<f64> = OnceLock::new();
    *VALUE.get_or_init(|| {
        std::env::var(X265SHAPE_EXACT_VAR_MIN)
            .ok()
            .and_then(|v| v.trim().parse::<f64>().ok())
            .filter(|v| v.is_finite() && *v >= 0.0)
            .unwrap_or(0.0)
    })
}

#[inline]
pub(super) fn luma_miss_audit_enabled() -> bool {
    static ENABLED: OnceLock<bool> = OnceLock::new();
    *ENABLED.get_or_init(|| std::env::var_os(LUMA_MISS_AUDIT).is_some())
}

/// Returns `true` if luma cheap pass is enabled.
/// Falls back to `template_enabled` when env var is not set.
#[inline]
pub(super) fn luma_cheap_enabled(template_enabled: bool) -> bool {
    static ENV: OnceLock<Option<bool>> = OnceLock::new();
    let env_val = *ENV.get_or_init(|| std::env::var(LUMA_CHEAP).ok().map(|v| v.trim() != "0"));
    env_val.unwrap_or(template_enabled)
}

/// RD lambda multiplier used by [`super::price::rd_lambda`].
#[inline]
pub(super) fn lambda_scale() -> f64 {
    static SCALE: OnceLock<f64> = OnceLock::new();
    *SCALE.get_or_init(|| {
        std::env::var(LAMBDA_SCALE)
            .ok()
            .and_then(|v| v.trim().parse::<f64>().ok())
            .filter(|v| v.is_finite() && *v > 0.0)
            .unwrap_or(1.0)
    })
}

/// Target CTU `(x,y)` pixel origin for candidate-cost dumps (`BPG_DUMP_CTU="x,y"`,
/// same env as the patched x265 `BPGCAND` dump). `None` when unset/malformed.
pub(super) fn dump_ctu_target() -> Option<(u32, u32)> {
    static V: OnceLock<Option<(u32, u32)>> = OnceLock::new();
    *V.get_or_init(|| {
        let s = std::env::var("BPG_DUMP_CTU").ok()?;
        let (a, b) = s.split_once(',')?;
        Some((a.trim().parse().ok()?, b.trim().parse().ok()?))
    })
}

/// Diagnostic scale on the SEARCH-side residual-bit estimate
/// (`BPG_RESIDUAL_BITS_SCALE`, default 1.0). Applies only to trial RD pricing,
/// never to emitted bits. <1 approximates the within-CTU CABAC context drift
/// (mid-CTU blocks code ~12-37% cheaper than the frozen CTU-entry `price_base`
/// estimates), to test whether pricing residual closer to its real evolving-
/// context cost shifts textured leaf/split/CBF decisions toward more coding.
pub(super) fn residual_bits_search_scale() -> f64 {
    static SCALE: OnceLock<f64> = OnceLock::new();
    *SCALE.get_or_init(|| {
        std::env::var("BPG_RESIDUAL_BITS_SCALE")
            .ok()
            .and_then(|v| v.trim().parse::<f64>().ok())
            .filter(|v| v.is_finite() && *v > 0.0)
            .unwrap_or(1.0)
    })
}

/// Parsed `BPG_STILL_DUMP` forensic target: `(x, y, log2_size, c_idx)`.
/// Cached; `None` when unset or malformed (the hot path pays one branch).
#[inline]
pub(super) fn dump_target() -> Option<(u32, u32, u8, u8)> {
    static VALUE: OnceLock<Option<(u32, u32, u8, u8)>> = OnceLock::new();
    *VALUE.get_or_init(|| {
        let raw = std::env::var(STILL_DUMP).ok()?;
        let mut it = raw.split(',').map(|s| s.trim());
        let x = it.next()?.parse::<u32>().ok()?;
        let y = it.next()?.parse::<u32>().ok()?;
        let log2 = it.next()?.parse::<u8>().ok()?;
        let c_idx = it.next().and_then(|s| s.parse::<u8>().ok()).unwrap_or(0);
        Some((x, y, log2, c_idx))
    })
}

/// Override the smallest TU log2 size that may be reached by optional splits.
/// `2` enables 8x8 -> 4x4 split evaluation; default policy usually stops at 8x8.
#[inline]
pub(super) fn tu_min_split_log2_override() -> Option<u8> {
    static VALUE: OnceLock<Option<u8>> = OnceLock::new();
    *VALUE.get_or_init(|| {
        std::env::var(TU_MIN_SPLIT_LOG2)
            .ok()
            .and_then(|v| v.trim().parse::<u8>().ok())
            .filter(|&v| (2..=5).contains(&v))
    })
}

/// Subtract this many RD-cost units per luma pixel from split candidates.
#[inline]
pub(super) fn tu_split_bias_per_px() -> f64 {
    static VALUE: OnceLock<f64> = OnceLock::new();
    *VALUE.get_or_init(|| {
        std::env::var(TU_SPLIT_BIAS_PER_PX)
            .ok()
            .and_then(|v| v.trim().parse::<f64>().ok())
            .filter(|v| v.is_finite())
            .unwrap_or(0.0)
    })
}

/// Extra split bias for 8x8 -> 4x4 split decisions.
#[inline]
pub(super) fn tu_4x4_bias_per_px() -> f64 {
    static VALUE: OnceLock<f64> = OnceLock::new();
    *VALUE.get_or_init(|| {
        std::env::var(TU_4X4_BIAS_PER_PX)
            .ok()
            .and_then(|v| v.trim().parse::<f64>().ok())
            .filter(|v| v.is_finite())
            .unwrap_or(0.0)
    })
}

/// Force optional TU splits at the given parent log2 size (for example `3`
/// forces legal 8x8 -> 4x4 splits).
#[inline]
pub(super) fn tu_force_split_log2() -> Option<u8> {
    static VALUE: OnceLock<Option<u8>> = OnceLock::new();
    *VALUE.get_or_init(|| {
        std::env::var(TU_FORCE_SPLIT_LOG2)
            .ok()
            .and_then(|v| v.trim().parse::<u8>().ok())
            .filter(|&v| (3..=6).contains(&v))
    })
}

/// Subtract this many RD-cost units per luma pixel from CU split candidates.
#[inline]
pub(super) fn cu_split_bias_per_px() -> f64 {
    static VALUE: OnceLock<f64> = OnceLock::new();
    *VALUE.get_or_init(|| {
        std::env::var(CU_SPLIT_BIAS_PER_PX)
            .ok()
            .and_then(|v| v.trim().parse::<f64>().ok())
            .filter(|v| v.is_finite())
            .unwrap_or(0.0)
    })
}

/// Leaf-first CU split branch-and-bound abort. Default on; decision- and
/// byte-identical to full evaluation (only skips children that cannot change
/// the leaf-vs-split winner).
#[inline]
pub(super) fn cu_split_bound_enabled() -> bool {
    static ENABLED: OnceLock<bool> = OnceLock::new();
    *ENABLED.get_or_init(|| {
        std::env::var(CU_SPLIT_BOUND)
            .map(|v| v.trim() != "0")
            .unwrap_or(true)
    })
}

/// Descent-termination instrumentation: when set, `decide_cu` logs one
/// `DSC` line per decided CU (rough score, leaf total, split total, outcome)
/// to stderr for offline bound calibration. Diagnostic only.
#[inline]
pub(super) fn descent_log_enabled() -> bool {
    static ENABLED: OnceLock<bool> = OnceLock::new();
    *ENABLED.get_or_init(|| std::env::var_os("BPG_STILLSEARCH_DESCENT_LOG").is_some())
}

/// Override for the effort template's CU descent gate thresholds
/// (`CuSearchPolicy::descent_gate_t16/t32`). `BPG_STILLSEARCH_DESCENT_GATE`
/// accepts `t16,t32` (floats), or `off`/`0` to disable the gate.
#[inline]
pub(super) fn descent_gate_override() -> Option<(f64, f64)> {
    static VALUE: OnceLock<Option<(f64, f64)>> = OnceLock::new();
    *VALUE.get_or_init(|| {
        let raw = std::env::var("BPG_STILLSEARCH_DESCENT_GATE").ok()?;
        let v = raw.trim();
        if matches!(v, "0" | "off" | "false" | "none" | "disable" | "disabled") {
            return Some((0.0, 0.0));
        }
        let (a, b) = v.split_once(',')?;
        let t16 = a.trim().parse::<f64>().ok().filter(|t| t.is_finite())?;
        let t32 = b.trim().parse::<f64>().ok().filter(|t| t.is_finite())?;
        Some((t16, t32))
    })
}

/// Leaf-first TU split branch-and-bound abort. Default on; decision- and
/// byte-identical to full evaluation (only skips TU children that cannot
/// change the leaf-vs-split winner).
#[inline]
pub(super) fn tu_split_bound_enabled() -> bool {
    static ENABLED: OnceLock<bool> = OnceLock::new();
    *ENABLED.get_or_init(|| {
        std::env::var(TU_SPLIT_BOUND)
            .map(|v| v.trim() != "0")
            .unwrap_or(true)
    })
}

/// Force CU splits at the given parent log2 size (for example `4` forces
/// legal 16x16 -> 8x8 CU splits). When unset, use the effort template's
/// default. Set to `0`/`off`/`false`/`none` to disable a template default.
#[inline]
pub(super) fn cu_force_split_log2(default: Option<u8>) -> Option<u8> {
    static OVERRIDE: OnceLock<Option<Option<u8>>> = OnceLock::new();
    match *OVERRIDE.get_or_init(|| {
        let Ok(raw) = std::env::var(CU_FORCE_SPLIT_LOG2) else {
            return None;
        };
        let v = raw.trim();
        if matches!(v, "0" | "off" | "false" | "none" | "disable" | "disabled") {
            return Some(None);
        }
        v.parse::<u8>()
            .ok()
            .filter(|&v| (4..=6).contains(&v))
            .map(Some)
    }) {
        Some(v) => v,
        None => default,
    }
}

/// Subtract this many RD-cost units per luma pixel from legal PartNxN candidates.
#[inline]
pub(super) fn nxn_bias_per_px() -> f64 {
    static VALUE: OnceLock<f64> = OnceLock::new();
    *VALUE.get_or_init(|| {
        std::env::var(NXN_BIAS_PER_PX)
            .ok()
            .and_then(|v| v.trim().parse::<f64>().ok())
            .filter(|v| v.is_finite())
            .unwrap_or(0.0)
    })
}

/// Force legal PartNxN candidates to win after evaluation. Diagnostic only.
#[inline]
pub(super) fn nxn_force_enabled() -> bool {
    static VALUE: OnceLock<bool> = OnceLock::new();
    *VALUE.get_or_init(|| {
        std::env::var(NXN_FORCE)
            .ok()
            .map(|v| {
                let v = v.trim().to_ascii_lowercase();
                !(v == "0" || v == "off" || v == "none" || v.is_empty())
            })
            .unwrap_or(false)
    })
}

/// Enable targeted 8x8->4x4 TU split admission.
#[inline]
pub(super) fn tu_target_4x4_enabled() -> bool {
    static VALUE: OnceLock<bool> = OnceLock::new();
    *VALUE.get_or_init(|| {
        std::env::var(TU_TARGET_4X4)
            .ok()
            .map(|v| {
                let v = v.trim().to_ascii_lowercase();
                !(v == "0" || v == "off" || v == "none" || v.is_empty())
            })
            .unwrap_or(false)
    })
}

#[inline]
pub(super) fn tu_target_var_min() -> f64 {
    static VALUE: OnceLock<f64> = OnceLock::new();
    *VALUE.get_or_init(|| {
        std::env::var(TU_TARGET_VAR_MIN)
            .ok()
            .and_then(|v| v.trim().parse::<f64>().ok())
            .filter(|v| v.is_finite() && *v >= 0.0)
            .unwrap_or(200.0)
    })
}

#[inline]
pub(super) fn tu_target_cost_min() -> f64 {
    static VALUE: OnceLock<f64> = OnceLock::new();
    *VALUE.get_or_init(|| {
        std::env::var(TU_TARGET_COST_MIN)
            .ok()
            .and_then(|v| v.trim().parse::<f64>().ok())
            .filter(|v| v.is_finite() && *v >= 0.0)
            .unwrap_or(50.0)
    })
}

#[inline]
pub(super) fn tu_target_mean_range_min() -> f64 {
    static VALUE: OnceLock<f64> = OnceLock::new();
    *VALUE.get_or_init(|| {
        std::env::var(TU_TARGET_MEAN_RANGE_MIN)
            .ok()
            .and_then(|v| v.trim().parse::<f64>().ok())
            .filter(|v| v.is_finite() && *v >= 0.0)
            .unwrap_or(16.0)
    })
}

/// True unless `BPG_STILLSEARCH_FINAL_RDOQ=0/off/none` disables final RDOQ.
#[inline]
pub(super) fn final_rdoq_enabled() -> bool {
    static ENABLED: OnceLock<bool> = OnceLock::new();
    *ENABLED.get_or_init(|| {
        std::env::var(FINAL_RDOQ)
            .ok()
            .map(|v| {
                let v = v.trim().to_ascii_lowercase();
                !(v == "0" || v == "off" || v == "none")
            })
            .unwrap_or(true)
    })
}

/// True unless `BPG_STILLSEARCH_RDOQ_ONE_PASS=0/off/none` restores the
/// two-phase (hard-quant Phase 1 + RDOQ Phase 2) exact evaluation.
#[inline]
pub(super) fn rdoq_one_pass_enabled() -> bool {
    static ENABLED: OnceLock<bool> = OnceLock::new();
    *ENABLED.get_or_init(|| {
        std::env::var(RDOQ_ONE_PASS)
            .ok()
            .map(|v| {
                let v = v.trim().to_ascii_lowercase();
                !(v == "0" || v == "off" || v == "none")
            })
            .unwrap_or(true)
    })
}

/// Root-TU dedup: reuse the cheap stage's winning-mode root-leaf luma
/// evaluation as the winner materialization's no-split arm instead of
/// re-running transform/RDOQ/pricing (audit §10.13). Byte-identical by
/// construction when every captured input matches; any mismatch falls back
/// to a fresh evaluation.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum RootTuReuseMode {
    Off,
    On,
    /// Capture and match as in `On`, but run the fresh evaluation anyway and
    /// panic if the captured outcome differs — the validation gate.
    Verify,
}

/// `BPG_STILLSEARCH_ROOT_TU_REUSE`: `0`/`off` disables, `verify` enables the
/// recompute-and-assert mode, anything else (default, unset) enables reuse.
pub(super) fn root_tu_reuse_mode() -> RootTuReuseMode {
    static MODE: OnceLock<RootTuReuseMode> = OnceLock::new();
    *MODE.get_or_init(|| {
        match std::env::var("BPG_STILLSEARCH_ROOT_TU_REUSE")
            .unwrap_or_default()
            .trim()
            .to_ascii_lowercase()
            .as_str()
        {
            "0" | "off" | "none" | "disable" | "disabled" => RootTuReuseMode::Off,
            "verify" | "check" => RootTuReuseMode::Verify,
            _ => RootTuReuseMode::On,
        }
    })
}

/// RDOQ-in-search override: `Some(true)` forces RDOQ trials at every search
/// decision site, `Some(false)` forces hard quant, `None` (unset or `effort`)
/// defers to the effort template's per-stage [`crate::effort::TrialQuant`].
#[inline]
pub(super) fn rdoq_search_override() -> Option<bool> {
    static OVERRIDE: OnceLock<Option<bool>> = OnceLock::new();
    *OVERRIDE.get_or_init(|| {
        let v = std::env::var(RDOQ_SEARCH).ok()?;
        let v = v.trim().to_ascii_lowercase();
        if v.is_empty() || v == "effort" {
            return None;
        }
        Some(!(v == "0" || v == "off" || v == "none"))
    })
}

/// Post-RDOQ pricing experiment mode. See [`RDOQ_PRICING`].
#[inline]
pub(super) fn rdoq_pricing_mode() -> RdoqPricingMode {
    static MODE: OnceLock<RdoqPricingMode> = OnceLock::new();
    *MODE.get_or_init(|| {
        let raw = std::env::var(RDOQ_PRICING).ok();
        match raw
            .as_deref()
            .map(str::trim)
            .map(str::to_ascii_lowercase)
            .as_deref()
        {
            Some("1" | "running" | "run") => RdoqPricingMode::Running,
            Some("2" | "hybrid") => RdoqPricingMode::Hybrid,
            Some("3" | "exact" | "exact-cleanup" | "cleanup") => RdoqPricingMode::ExactCleanup,
            Some("4" | "trellis" | "bounded-trellis" | "bounded_trellis") => {
                RdoqPricingMode::BoundedTrellis
            }
            _ => RdoqPricingMode::Frozen,
        }
    })
}

/// Beam width for `BPG_RDOQ_PRICING=bounded-trellis`.
#[inline]
pub(super) fn rdoq_trellis_beam() -> usize {
    static BEAM: OnceLock<usize> = OnceLock::new();
    *BEAM.get_or_init(|| {
        std::env::var(RDOQ_TRELLIS_BEAM)
            .ok()
            .and_then(|v| v.trim().parse::<usize>().ok())
            .filter(|&v| v > 0)
            .unwrap_or(4)
    })
}

/// Resolved psy-rd strength: `BPG_PSY_RD` overrides `config_value` when set,
/// then the result is clamped to 0..=5 (x265's sane range).
#[inline]
pub(in crate::encoder) fn psy_rd(config_value: f32) -> f32 {
    static ENV: OnceLock<Option<f32>> = OnceLock::new();
    let env_val = *ENV.get_or_init(|| {
        std::env::var(PSY_RD)
            .ok()
            .and_then(|v| v.trim().parse::<f32>().ok())
            .filter(|v| v.is_finite())
    });
    env_val.unwrap_or(config_value).clamp(0.0, 5.0)
}

/// Resolved psy-rdoq strength: `BPG_PSY_RDOQ` overrides `config_value` when
/// set, then the result is clamped to 0..=50 (x265's sane range).
#[inline]
pub(in crate::encoder) fn psy_rdoq(config_value: f32) -> f32 {
    static ENV: OnceLock<Option<f32>> = OnceLock::new();
    let env_val = *ENV.get_or_init(|| {
        std::env::var(PSY_RDOQ)
            .ok()
            .and_then(|v| v.trim().parse::<f32>().ok())
            .filter(|v| v.is_finite())
    });
    env_val.unwrap_or(config_value).clamp(0.0, 50.0)
}

/// True when experimental SSIM-RD-style candidate ranking is enabled.
#[inline]
pub(super) fn ssim_rd_enabled() -> bool {
    static ENABLED: OnceLock<bool> = OnceLock::new();
    *ENABLED.get_or_init(|| {
        std::env::var(SSIM_RD)
            .ok()
            .map(|v| {
                let v = v.trim().to_ascii_lowercase();
                !(v == "0" || v == "off" || v == "none" || v.is_empty())
            })
            .unwrap_or(false)
    })
}

/// Weight for the experimental SSIM-RD-style residual-energy term.
#[inline]
pub(super) fn ssim_rd_weight() -> f64 {
    static WEIGHT: OnceLock<f64> = OnceLock::new();
    *WEIGHT.get_or_init(|| {
        std::env::var(SSIM_RD_WEIGHT)
            .ok()
            .and_then(|v| v.trim().parse::<f64>().ok())
            .filter(|v| v.is_finite() && *v >= 0.0)
            .unwrap_or(1.0 / 16384.0)
    })
}

/// Residual syntax pricing mode used only by the luma-cheap first pass.
/// Falls back to `template_exact` when env var is not set.
#[inline]
pub(super) fn luma_cheap_residual_price_exact(template_exact: bool) -> bool {
    static ENV: OnceLock<Option<bool>> = OnceLock::new();
    let env_val = *ENV.get_or_init(|| {
        std::env::var(LUMA_CHEAP_RESIDUAL_PRICE).ok().map(|v| {
            let v = v.trim().to_ascii_lowercase();
            !(v == "0" || v == "skip" || v == "none" || v == "off")
        })
    });
    env_val.unwrap_or(template_exact)
}

/// True when the cheap pass should price residual bits with the static-context
/// estimate (frozen-state x265 `EstBitsSbac` model) instead of the evolving
/// CABAC re-encode. Off by default; see [`CHEAP_BITS_STATIC`].
pub(super) fn cheap_bits_static() -> bool {
    static ENV: OnceLock<bool> = OnceLock::new();
    *ENV.get_or_init(|| {
        std::env::var(CHEAP_BITS_STATIC)
            .ok()
            .map(|v| {
                let v = v.trim().to_ascii_lowercase();
                !(v == "0" || v == "off" || v == "none" || v.is_empty())
            })
            .unwrap_or(false)
    })
}

/// Threshold below which PartNxN is skipped for 8×8 CUs.
/// Falls back to `template_val` when env var is not set.
#[inline]
pub(super) fn nxn_skip_satd_threshold(template_val: f64) -> f64 {
    static ENV: OnceLock<Option<f64>> = OnceLock::new();
    let env_val = *ENV.get_or_init(|| {
        std::env::var(NXN_SKIP_SATD)
            .ok()
            .and_then(|s| s.parse().ok())
    });
    env_val.unwrap_or(template_val)
}

/// Returns `true` if the rough-stage audit should be collected.
#[inline]
pub(super) fn rough_audit_enabled() -> bool {
    static ENABLED: OnceLock<bool> = OnceLock::new();
    *ENABLED.get_or_init(|| std::env::var_os(ROUGH_AUDIT).is_some())
}

/// Returns `true` if scalar (non-batched) angular prediction should be used
/// for the rough pass.
#[inline]
pub(super) fn scalar_angular_rough_enabled() -> bool {
    static ENABLED: OnceLock<bool> = OnceLock::new();
    *ENABLED.get_or_init(|| {
        std::env::var(SCALAR_ANGULAR_ROUGH)
            .ok()
            .map(|v| v.trim() != "0")
            .unwrap_or(false)
    })
}

/// Weight applied to the mode-bit cost in rough ranking (default 1.0).
/// Returns `None` when the env var is not set, letting the caller use its
/// default weight.
#[inline]
pub(super) fn rough_mode_bit_weight_override() -> Option<f64> {
    static V: OnceLock<Option<f64>> = OnceLock::new();
    *V.get_or_init(|| {
        std::env::var(ROUGH_MODE_BIT_WEIGHT)
            .ok()
            .and_then(|s| s.parse().ok())
    })
}

/// Returns `true` if the sampled luma oracle diagnostic is enabled.
#[inline]
pub(super) fn luma_oracle_enabled() -> bool {
    static ENABLED: OnceLock<bool> = OnceLock::new();
    *ENABLED.get_or_init(|| std::env::var_os(LUMA_ORACLE).is_some())
}

/// Returns the oracle sampling modulus (default 64).
#[inline]
pub(super) fn luma_oracle_mod() -> u64 {
    static V: OnceLock<u64> = OnceLock::new();
    *V.get_or_init(|| {
        std::env::var(LUMA_ORACLE_MOD)
            .ok()
            .and_then(|s| s.parse().ok())
            .unwrap_or(64)
            .max(1)
    })
}

#[inline]
pub(super) fn x265_rmd_window() -> f64 {
    static V: OnceLock<f64> = OnceLock::new();
    *V.get_or_init(|| parse_env_or(X265_RMD_WINDOW, 1.25_f64).max(1.0))
}

#[inline]
pub(super) fn x265_rmd_cap(default_cap: usize) -> usize {
    static V: OnceLock<Option<usize>> = OnceLock::new();
    V.get_or_init(|| {
        std::env::var(X265_RMD_CAP)
            .ok()
            .and_then(|s| s.trim().parse::<usize>().ok())
            .map(|n| n.clamp(1, 35))
    })
    .unwrap_or(default_cap)
}

fn parse_env_or<T>(key: &str, default: T) -> T
where
    T: std::str::FromStr,
{
    std::env::var(key)
        .ok()
        .and_then(|s| s.trim().parse().ok())
        .unwrap_or(default)
}