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
//! StillSearch depth dispatch and CTU entrypoint.

use crate::contexts::Contexts;
use crate::encoder::Encoder;
use crate::encoder::syntax::CuNode;

use super::canvas::{CanvasOverlay, ReconBackground};
use super::emit;
use super::ledger::{StillSearchLedger, WorkBucket};
use super::source::{CtuSource8, CtuSource16, CtuSourceCache};
use super::workspace::CtuWorkspace;

pub(in crate::encoder) struct StillSearch {
    imp: StillSearchImpl,
}

enum StillSearchImpl {
    EightBit(StillSearchDepth<CtuSource8>),
    HighBit(StillSearchDepth<CtuSource16>),
}

pub(super) struct StillSearchDepth<S> {
    pub(super) workspace: CtuWorkspace,
    pub(super) source: S,
    pub(super) overlay: CanvasOverlay,
    /// True while building from [`ReconBackground::Strips`]: the worker owns
    /// no reconstruction frame, so out-of-canvas reference reads must resolve
    /// to UNINIT (they cannot happen for in-CTU blocks — debug-asserted)
    /// and the final canvas content is published by the caller instead of
    /// committed to `state.frame`.
    pub(super) frameless: bool,
}

impl StillSearch {
    pub(in crate::encoder) fn new(bit_depth: u8) -> Self {
        let imp = if bit_depth == 8 {
            StillSearchImpl::EightBit(StillSearchDepth::default())
        } else {
            StillSearchImpl::HighBit(StillSearchDepth::default())
        };
        Self { imp }
    }

    pub(in crate::encoder) fn build_ctu(
        &mut self,
        state: &mut Encoder<'_>,
        price_ctx: &Contexts,
        x0: u32,
        y0: u32,
        log2_cb_size: u8,
        ct_depth: u8,
        background: ReconBackground<'_>,
    ) -> CuNode {
        match &mut self.imp {
            StillSearchImpl::EightBit(search) => {
                search.build_ctu(state, price_ctx, x0, y0, log2_cb_size, ct_depth, background)
            }
            StillSearchImpl::HighBit(search) => {
                search.build_ctu(state, price_ctx, x0, y0, log2_cb_size, ct_depth, background)
            }
        }
    }

    /// The finalize-generation canvas content of the last
    /// [`build_ctu`](Self::build_ctu) under [`ReconBackground::Strips`] —
    /// what the frameless WPP worker publishes to the master sink.
    pub(in crate::encoder) fn canvas(&self) -> &CanvasOverlay {
        match &self.imp {
            StillSearchImpl::EightBit(search) => &search.overlay,
            StillSearchImpl::HighBit(search) => &search.overlay,
        }
    }
}

impl<S> Default for StillSearchDepth<S>
where
    S: Default,
{
    fn default() -> Self {
        Self {
            workspace: CtuWorkspace::default(),
            source: S::default(),
            overlay: CanvasOverlay::default(),
            frameless: false,
        }
    }
}

impl<S> StillSearchDepth<S>
where
    S: CtuSourceCache,
{
    pub(super) fn build_ctu(
        &mut self,
        state: &mut Encoder<'_>,
        price_ctx: &Contexts,
        x0: u32,
        y0: u32,
        log2_cb_size: u8,
        ct_depth: u8,
        background: ReconBackground<'_>,
    ) -> CuNode {
        self.workspace.reset();
        self.workspace.set_price_context(price_ctx);
        self.source.reset_from_ctu(state, x0, y0, log2_cb_size);
        self.frameless = matches!(background, ReconBackground::Strips { .. });
        match background {
            ReconBackground::Frame => self.overlay.reset_from_ctu(&state.frame, x0, y0),
            ReconBackground::Strips { strips, left } => {
                self.overlay
                    .reset_from_strips(strips, left, state.frame.chroma_format, x0, y0)
            }
        }

        let (plan, _cost) = self.decide_cu(state, x0, y0, log2_cb_size, ct_depth);

        // Winner-only RDOQ finalize: discard the hard-quant trial recon, then
        // re-code the chosen plan in decoder order with RDOQ, rebuilding coeffs/
        // recon/CBFs. Search decided the structure; this only refines coding.
        self.overlay.clear();
        // Re-seed the evolving trial context from the CTU-entry writer state
        // for the decoder-order walk. With `ctx_evolve_finalize`, each
        // finalized block commits its syntax so the final RDOQ rate model
        // prices from per-block contexts close to the real writer's (x265
        // parity) instead of frozen CTU-entry states.
        let prev_commit = self.workspace.commit_ctx;
        let prev_skip = self.workspace.commit_ctx_skip_chroma;
        self.workspace.price_cur = self.workspace.price_base.clone();
        self.workspace.commit_ctx = super::env::ctx_evolve_finalize();
        self.workspace.commit_ctx_skip_chroma = false;
        let plan = self.finalize_cu(state, plan, x0, y0, log2_cb_size);
        self.workspace.commit_ctx = prev_commit;
        self.workspace.commit_ctx_skip_chroma = prev_skip;

        let final_commit_timer = StillSearchLedger::start_timer();
        if !self.frameless {
            self.overlay.commit_to_frame(&mut state.frame);
        }
        self.workspace
            .ledger
            .finish_timer(WorkBucket::FinalCommit, final_commit_timer);
        if !self.frameless {
            // Frameless builds keep the finalize-generation canvas resident:
            // the caller publishes it to the master sink, and the next
            // `reset_from_strips` self-captures its right edge as the left
            // border column.
            self.overlay.clear();
        }
        self.workspace.ledger.bump(WorkBucket::FinalCommit);
        // One plan->syntax materialization per CTU (emit::cu_node below).
        self.workspace.ledger.bump(WorkBucket::Writer);
        state.stats.final_rdoq_blocks += self.workspace.ledger.calls(WorkBucket::Rdoq);
        let writer_timer = StillSearchLedger::start_timer();
        let cu = emit::cu_node(plan, &self.workspace.coeffs);
        self.workspace
            .ledger
            .finish_timer(WorkBucket::Writer, writer_timer);
        self.workspace
            .ledger
            .merge_into(&mut state.stats.stillsearch_ledger);
        self.workspace
            .ledger
            .merge_wall_ns_into(&mut state.stats.stillsearch_ledger_ns);
        state.stats.substage_border_ns = state
            .stats
            .substage_border_ns
            .saturating_add(self.workspace.substage.border_ns);
        state.stats.substage_predict_ns = state
            .stats
            .substage_predict_ns
            .saturating_add(self.workspace.substage.predict_ns);
        state.stats.substage_forward_xform_ns = state
            .stats
            .substage_forward_xform_ns
            .saturating_add(self.workspace.substage.forward_xform_ns);
        state.stats.substage_quant_ns = state
            .stats
            .substage_quant_ns
            .saturating_add(self.workspace.substage.quant_ns);
        state.stats.substage_recon_dist_ns = state
            .stats
            .substage_recon_dist_ns
            .saturating_add(self.workspace.substage.recon_dist_ns);
        state.stats.substage_residual_price_ns = state
            .stats
            .substage_residual_price_ns
            .saturating_add(self.workspace.substage.residual_price_ns);
        state.stats.substage_calls += self.workspace.substage.calls;
        state.stats.tu_split_early_terminations += self.workspace.tu_split_early_terminations;
        state.stats.tu_split_bound_aborts += self.workspace.tu_split_bound_aborts;
        state.stats.root_tu_reuse_hits += self.workspace.root_tu_reuse_hits;
        for i in 0..7 {
            state.stats.tu_leaf_by_log2[i] += self.workspace.tu_leaf_by_log2[i];
            state.stats.tu_split_by_log2[i] += self.workspace.tu_split_by_log2[i];
        }
        cu
    }
}

impl<S> StillSearchDepth<S>
where
    S: CtuSourceCache,
{
    /// Predict a block into caller-owned `dst` (`size*size`, stride `size`),
    /// reading the committed frame immutably with overlay-first reference
    /// samples and tile-boundary clamping. Never mutates the shared frame.
    pub(super) fn predict_into(
        &self,
        state: &Encoder<'_>,
        x0: u32,
        y0: u32,
        log2_size: u8,
        c_idx: u8,
        mode: crate::internal::bpg_hevc_decode::hevc::slice::IntraPredMode,
        dst: &mut [u16],
    ) {
        let size = 1usize << log2_size;
        let tile_bounds = state.tile_clamp_bounds(x0, y0, c_idx);
        let overlay = &self.overlay;
        let frameless = self.frameless;
        crate::internal::bpg_hevc_decode::hevc::intra::predict_intra_into_with_reader(
            &state.frame,
            x0,
            y0,
            log2_size,
            mode,
            c_idx,
            true,
            dst,
            size,
            |c, rx, ry| {
                if let Some((tx0, ty0, tx1, ty1)) = tile_bounds {
                    if rx < tx0 || rx >= tx1 || ry < ty0 || ry >= ty1 {
                        return Some(crate::internal::bpg_hevc_decode::hevc::UNINIT_SAMPLE);
                    }
                }
                match overlay.sample(c, rx, ry) {
                    Some(v) => Some(v),
                    // In-CTU reference reads always land in the canvas; a
                    // frameless worker has no frame to fall back to, so an
                    // out-of-canvas read resolves to UNINIT (= unavailable).
                    None if frameless => {
                        debug_assert!(false, "frameless out-of-canvas read c={c} ({rx},{ry})");
                        Some(crate::internal::bpg_hevc_decode::hevc::UNINIT_SAMPLE)
                    }
                    None => None,
                }
            },
        );
    }
}

impl<S> StillSearchDepth<S>
where
    S: CtuSourceCache,
{
    /// 8-bit prediction wrapper. The decoder primitive is still u16-oriented;
    /// this narrows immediately into caller-owned u8 storage so the rest of the
    /// 8-bit analysis path (distortion, residual, recon, overlay) stays u8.
    pub(super) fn predict_into_u8(
        &self,
        state: &Encoder<'_>,
        x0: u32,
        y0: u32,
        log2_size: u8,
        c_idx: u8,
        mode: crate::internal::bpg_hevc_decode::hevc::slice::IntraPredMode,
        dst: &mut [u8],
        tmp_u16: &mut Vec<u16>,
    ) {
        let size = 1usize << log2_size;
        let n = size * size;
        tmp_u16.clear();
        tmp_u16.resize(n, 0);
        self.predict_into(state, x0, y0, log2_size, c_idx, mode, tmp_u16);
        debug_assert!(dst.len() >= n);
        crate::primitives::narrow_u16_to_u8(tmp_u16, dst, n);
    }

    /// Build the intra reference borders (unfiltered + filtered, plus the center
    /// index and bit depth) for one block, reading overlay-first with
    /// tile-boundary clamping. These borders are *identical* across all candidate
    /// intra modes for a block as long as the overlay is unchanged, so callers
    /// that try many modes (cheap/exact ranking) should build once via this and
    /// reuse with [`predict_exact_from_refs_u8`] instead of rebuilding per mode.
    pub(super) fn build_intra_refs_for_block(
        &self,
        state: &Encoder<'_>,
        x0: u32,
        y0: u32,
        log2_size: u8,
        c_idx: u8,
    ) -> IntraRefs {
        let tile_bounds = state.tile_clamp_bounds(x0, y0, c_idx);
        let (uf, ft, center, bit_depth) =
            self.build_intra_refs_bulk_overlay(&state.frame, x0, y0, log2_size, c_idx, tile_bounds);
        IntraRefs {
            uf,
            ft,
            center,
            bit_depth,
        }
    }

    /// Predict one block from prebuilt [`IntraRefs`] into `dst`. Pure function of
    /// the refs + mode; does no overlay reads.
    #[inline]
    pub(super) fn predict_exact_from_refs_u8(
        &self,
        refs: &IntraRefs,
        log2_size: u8,
        c_idx: u8,
        mode: crate::internal::bpg_hevc_decode::hevc::slice::IntraPredMode,
        dst: &mut [u8],
    ) {
        let mode_u8 = mode.as_u8();
        let size = 1usize << log2_size;
        let border = if crate::internal::bpg_hevc_decode::hevc::intra::reference_filter_applies(
            size, mode_u8,
        ) {
            &refs.ft
        } else {
            &refs.uf
        };
        match mode {
            crate::internal::bpg_hevc_decode::hevc::slice::IntraPredMode::Planar => {
                crate::primitives::pred_planar_u8(
                    dst,
                    border,
                    refs.center,
                    log2_size,
                    c_idx,
                    refs.bit_depth,
                )
            }
            crate::internal::bpg_hevc_decode::hevc::slice::IntraPredMode::Dc => {
                crate::primitives::pred_dc_u8(
                    dst,
                    border,
                    refs.center,
                    log2_size,
                    c_idx,
                    refs.bit_depth,
                )
            }
            _ => crate::primitives::pred_angular_u8(
                dst,
                border,
                refs.center,
                log2_size,
                c_idx,
                mode_u8,
                refs.bit_depth,
            ),
        }
    }

    #[allow(clippy::type_complexity)]
    pub(super) fn build_intra_refs_bulk_overlay(
        &self,
        frame: &crate::internal::bpg_hevc_decode::DecodedFrame,
        x0: u32,
        y0: u32,
        log2_size: u8,
        c_idx: u8,
        tile_bounds: Option<(u32, u32, u32, u32)>,
    ) -> (
        [i32; crate::internal::bpg_hevc_decode::hevc::intra::INTRA_BORDER_LEN],
        [i32; crate::internal::bpg_hevc_decode::hevc::intra::INTRA_BORDER_LEN],
        usize,
        u8,
    ) {
        use crate::internal::bpg_hevc_decode::hevc::UNINIT_SAMPLE;
        use crate::internal::bpg_hevc_decode::hevc::intra::{
            INTRA_BORDER_CENTER, INTRA_BORDER_LEN, MAX_INTRA_PRED_BLOCK_SIZE,
            apply_reference_filter,
        };

        let size = 1u32 << log2_size;
        let bit_depth = frame.bit_depth;
        let center = INTRA_BORDER_CENTER;
        let default_val = 1i32 << (bit_depth - 1);
        let (plane, stride) = frame.plane(c_idx);
        let (frame_w, frame_h) = frame.component_dims(c_idx);
        let avail_left = x0 > 0;
        let avail_top = y0 > 0;
        let mut unfiltered = [0i32; INTRA_BORDER_LEN];
        let mut avail = [false; 4 * MAX_INTRA_PRED_BLOCK_SIZE + 1];
        let mut avail_count = 0u32;
        let total_samples = 4 * size + 1;

        #[inline]
        fn in_tile(tile: Option<(u32, u32, u32, u32)>, x: u32, y: u32) -> bool {
            tile.is_none_or(|(tx0, ty0, tx1, ty1)| x >= tx0 && x < tx1 && y >= ty0 && y < ty1)
        }

        #[inline]
        fn read_plane_sample(plane: &[u16], stride: usize, x: u32, y: u32) -> u16 {
            plane
                .get(y as usize * stride + x as usize)
                .copied()
                .unwrap_or(0)
        }

        #[inline]
        fn maybe_set(
            border: &mut [i32],
            avail: &mut [bool],
            avail_count: &mut u32,
            idx: usize,
            raw: u16,
        ) -> bool {
            if raw != UNINIT_SAMPLE {
                border[idx] = raw as i32;
                avail[idx] = true;
                *avail_count += 1;
                true
            } else {
                false
            }
        }

        let frameless = self.frameless;
        let read_one = |rx: u32, ry: u32| -> u16 {
            if !in_tile(tile_bounds, rx, ry) {
                return UNINIT_SAMPLE;
            }
            self.overlay.sample(c_idx, rx, ry).unwrap_or_else(|| {
                debug_assert!(
                    !frameless,
                    "frameless out-of-canvas read c={c_idx} ({rx},{ry})"
                );
                read_plane_sample(plane, stride, rx, ry)
            })
        };

        // Top-left corner, with the same top/left fallback order as the decoder
        // helper. Tile-clamped samples are treated as unavailable.
        let mut corner_avail = false;
        if avail_left && avail_top {
            corner_avail = maybe_set(
                &mut unfiltered,
                &mut avail,
                &mut avail_count,
                center,
                read_one(x0 - 1, y0 - 1),
            );
        }
        if !corner_avail && avail_top {
            corner_avail = maybe_set(
                &mut unfiltered,
                &mut avail,
                &mut avail_count,
                center,
                read_one(x0, y0 - 1),
            );
        }
        if !corner_avail && avail_left {
            corner_avail = maybe_set(
                &mut unfiltered,
                &mut avail,
                &mut avail_count,
                center,
                read_one(x0 - 1, y0),
            );
        }
        if !corner_avail {
            unfiltered[center] = default_val;
        }

        // Top + above-right: start from the committed frame row in one pass,
        // then overlay all intersecting patches with a single patch-stack scan.
        if avail_top {
            let top_count = (2 * size).min(frame_w.saturating_sub(x0)) as usize;
            let mut row = [UNINIT_SAMPLE; 2 * MAX_INTRA_PRED_BLOCK_SIZE];
            if top_count > 0 {
                // Canvas fast path: one contiguous merged read. Fallback:
                // frame-initialize, then overlay the patch stack.
                if !self
                    .overlay
                    .read_row_merged(c_idx, y0 - 1, x0, &mut row[..top_count])
                {
                    debug_assert!(!frameless, "frameless out-of-canvas top-row read");
                    let row_base = (y0 - 1) as usize * stride + x0 as usize;
                    for (i, dst) in row[..top_count].iter_mut().enumerate() {
                        *dst = plane.get(row_base + i).copied().unwrap_or(0);
                    }
                    self.overlay
                        .overlay_row_samples(c_idx, y0 - 1, x0, &mut row[..top_count]);
                }
                for (i, &raw) in row[..top_count].iter().enumerate() {
                    let rx = x0 + i as u32;
                    if in_tile(tile_bounds, rx, y0 - 1) {
                        maybe_set(
                            &mut unfiltered,
                            &mut avail,
                            &mut avail_count,
                            center + 1 + i,
                            raw,
                        );
                    }
                }
            }
        }

        // Left + below-left: same bulk-overlay path for the reference column.
        if avail_left {
            let left_count = (2 * size).min(frame_h.saturating_sub(y0)) as usize;
            let mut col = [UNINIT_SAMPLE; 2 * MAX_INTRA_PRED_BLOCK_SIZE];
            if left_count > 0 {
                if !self
                    .overlay
                    .read_col_merged(c_idx, x0 - 1, y0, &mut col[..left_count])
                {
                    debug_assert!(!frameless, "frameless out-of-canvas left-col read");
                    let left_x = (x0 - 1) as usize;
                    for (i, dst) in col[..left_count].iter_mut().enumerate() {
                        let plane_idx = (y0 as usize + i) * stride + left_x;
                        *dst = plane.get(plane_idx).copied().unwrap_or(0);
                    }
                    self.overlay
                        .overlay_col_samples(c_idx, x0 - 1, y0, &mut col[..left_count]);
                }
                for (i, &raw) in col[..left_count].iter().enumerate() {
                    let ry = y0 + i as u32;
                    if in_tile(tile_bounds, x0 - 1, ry) {
                        maybe_set(
                            &mut unfiltered,
                            &mut avail,
                            &mut avail_count,
                            center - 1 - i,
                            raw,
                        );
                    }
                }
            }
        }

        if avail_count < total_samples {
            substitute_unavailable_refs(
                &mut unfiltered,
                &avail,
                center,
                size as usize,
                default_val,
            );
        }

        let mut filtered = unfiltered;
        if (c_idx == 0 || frame.chroma_format == 3) && size > 4 {
            apply_reference_filter(
                &mut filtered,
                center,
                size as usize,
                c_idx,
                true,
                bit_depth as usize,
            );
        }

        (unfiltered, filtered, center, bit_depth)
    }
}

fn substitute_unavailable_refs(
    border: &mut [i32],
    avail: &[bool],
    center: usize,
    size: usize,
    default_val: i32,
) {
    let mut first_avail_val = None;
    for i in (0..(2 * size)).rev() {
        let idx = center - 1 - i;
        if avail[idx] {
            first_avail_val = Some(border[idx]);
            break;
        }
    }
    if first_avail_val.is_none() && avail[center] {
        first_avail_val = Some(border[center]);
    }
    if first_avail_val.is_none() {
        for i in 0..(2 * size) {
            let idx = center + 1 + i;
            if avail[idx] {
                first_avail_val = Some(border[idx]);
                break;
            }
        }
    }

    let mut current = first_avail_val.unwrap_or(default_val);
    for i in (0..(2 * size)).rev() {
        let idx = center - 1 - i;
        if avail[idx] {
            current = border[idx];
        } else {
            border[idx] = current;
        }
    }
    if avail[center] {
        current = border[center];
    } else {
        border[center] = current;
    }
    for i in 0..(2 * size) {
        let idx = center + 1 + i;
        if avail[idx] {
            current = border[idx];
        } else {
            border[idx] = current;
        }
    }
}

/// Prebuilt intra reference borders for one block, reusable across all candidate
/// modes while the overlay is unchanged. The borders are fixed-size stack arrays
/// (no heap), so copying/holding one is cheap.
#[derive(Clone, Copy)]
pub(super) struct IntraRefs {
    pub(super) uf: [i32; crate::internal::bpg_hevc_decode::hevc::intra::INTRA_BORDER_LEN],
    pub(super) ft: [i32; crate::internal::bpg_hevc_decode::hevc::intra::INTRA_BORDER_LEN],
    pub(super) center: usize,
    pub(super) bit_depth: u8,
}