videoframe 0.2.0

A common vocabulary of pixel-format and color-metadata types for video processing pipelines.
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
//! Packed 16-bit RGB/BGR and RGBA/BGRA source frames:
//! - `AV_PIX_FMT_RGB48{LE,BE}`  → [`Rgb48Frame`]  (R, G, B; stride in u16 elements ≥ 3 × width)
//! - `AV_PIX_FMT_BGR48{LE,BE}`  → [`Bgr48Frame`]  (B, G, R; stride in u16 elements ≥ 3 × width)
//! - `AV_PIX_FMT_RGBA64{LE,BE}` → [`Rgba64Frame`] (R, G, B, A; stride in u16 elements ≥ 4 × width)
//! - `AV_PIX_FMT_BGRA64{LE,BE}` → [`Bgra64Frame`] (B, G, R, A; stride in u16 elements ≥ 4 × width)
//!
//! # Endian contract — `<const BE: bool = false>`
//!
//! Each frame type carries a `<const BE: bool>` parameter that defaults to
//! `false` (LE-encoded bytes). The parameter encodes the **byte order of the
//! plane bytes**, matching the FFmpeg `*LE` / `*BE` pixel-format suffix in the
//! format name:
//!
//! - `BE = false` (`Rgb48Frame<'_, false>` aka [`Rgb48LeFrame`]) — plane bytes
//!   are LE-encoded, matching `AV_PIX_FMT_RGB48LE`. On a little-endian host
//!   (every CI runner today) LE bytes _are_ host-native, so `&[u16]` is also a
//!   host-native u16 slice; on a big-endian host the bytes have to be
//!   byte-swapped back to host-native before arithmetic.
//! - `BE = true` (`Rgb48Frame<'_, true>` aka [`Rgb48BeFrame`]) — plane bytes
//!   are BE-encoded, matching `AV_PIX_FMT_RGB48BE`. On a little-endian host
//!   the bytes are byte-swapped before arithmetic; on a big-endian host they
//!   are host-native.
//!
//! Downstream row kernels handle the byte-swap (or no-op) under the hood —
//! callers do **not** pre-swap. The `BE` parameter on `Frame` propagates
//! through the walker (`rgb48_to::<BE>(...)`) into the sinker dispatch
//! (`MixedSinker<Rgb48<BE>>`), which monomorphizes the kernel call as
//! `rgb48_to_*_row_endian::<BE>(...)`.
//!
//! Stride is in **u16 elements** (not bytes). Callers holding a raw FFmpeg
//! byte buffer should cast via `bytemuck::cast_slice` (which checks alignment
//! at runtime) and divide `linesize[0]` by 2 before constructing. Direct
//! pointer casts to `&[u16]` are undefined behaviour if the byte buffer is
//! not 2-byte aligned.

use super::{
  GeometryOverflow, InsufficientPlane, InsufficientStride, WidthOverflow, ZeroDimension,
};
use derive_more::{IsVariant, TryUnwrap, Unwrap};
use thiserror::Error;

// ---- Rgb48Frame --------------------------------------------------------------

/// Errors returned by [`Rgb48Frame::try_new`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, IsVariant, TryUnwrap, Unwrap, Error)]
#[non_exhaustive]
#[unwrap(ref, ref_mut)]
#[try_unwrap(ref, ref_mut)]
pub enum Rgb48FrameError {
  /// `width` or `height` was zero.
  #[error(transparent)]
  ZeroDimension(ZeroDimension),

  /// `stride < 3 * width` (in u16 elements).
  #[error(transparent)]
  InsufficientStride(InsufficientStride),

  /// Plane is shorter than `stride * height` u16 elements.
  #[error(transparent)]
  InsufficientPlane(InsufficientPlane),

  /// `stride * height` overflows `usize`.
  #[error(transparent)]
  GeometryOverflow(GeometryOverflow),

  /// `3 * width` overflows `u32`.
  #[error(transparent)]
  WidthOverflow(WidthOverflow),
}

/// A validated packed **RGB48** frame (`AV_PIX_FMT_RGB48{LE,BE}`) — three
/// `u16` samples per pixel in `R, G, B` order.
///
/// The `<const BE: bool>` parameter selects the plane byte order: `false`
/// (default) → LE-encoded bytes (`AV_PIX_FMT_RGB48LE`), `true` → BE-encoded
/// bytes (`AV_PIX_FMT_RGB48BE`). Downstream row kernels handle the byte-swap
/// (or no-op) under the hood — callers do **not** pre-swap.
///
/// `stride` is in **u16 elements** (≥ `3 * width`). Callers holding byte
/// buffers from FFmpeg should cast via `bytemuck::cast_slice` and divide
/// `linesize[0]` by 2 before constructing.
///
/// # Aliases
/// - [`Rgb48LeFrame`] = `Rgb48Frame<'a, false>` — explicit LE.
/// - [`Rgb48BeFrame`] = `Rgb48Frame<'a, true>` — explicit BE.
#[derive(Debug, Clone, Copy)]
pub struct Rgb48Frame<'a, const BE: bool = false> {
  rgb48: &'a [u16],
  width: u32,
  height: u32,
  stride: u32,
}

/// LE-encoded `Rgb48Frame` (`AV_PIX_FMT_RGB48LE`). Equivalent to the default
/// `Rgb48Frame<'a>`; provided as an explicit alias for callers who want to
/// document the endianness at the type level.
pub type Rgb48LeFrame<'a> = Rgb48Frame<'a, false>;

/// BE-encoded `Rgb48Frame` (`AV_PIX_FMT_RGB48BE`). Plane bytes are
/// big-endian-encoded `u16` samples; downstream row kernels byte-swap under
/// the hood.
pub type Rgb48BeFrame<'a> = Rgb48Frame<'a, true>;

impl<'a, const BE: bool> Rgb48Frame<'a, BE> {
  /// Constructs a new [`Rgb48Frame`], validating dimensions and plane length.
  ///
  /// The `<const BE: bool>` parameter selects whether the supplied `rgb48`
  /// slice is interpreted as LE-encoded bytes (`BE = false`, default) or
  /// BE-encoded bytes (`BE = true`). The byte-swap is performed inside the
  /// row kernels — this constructor does no I/O on the bytes.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn try_new(
    rgb48: &'a [u16],
    width: u32,
    height: u32,
    stride: u32,
  ) -> Result<Self, Rgb48FrameError> {
    if width == 0 || height == 0 {
      return Err(Rgb48FrameError::ZeroDimension(ZeroDimension::new(
        width, height,
      )));
    }
    let min_stride = match width.checked_mul(3) {
      Some(v) => v,
      None => return Err(Rgb48FrameError::WidthOverflow(WidthOverflow::new(width))),
    };
    if stride < min_stride {
      return Err(Rgb48FrameError::InsufficientStride(
        InsufficientStride::new(stride, min_stride),
      ));
    }
    let plane_min = match (stride as usize).checked_mul(height as usize) {
      Some(v) => v,
      None => {
        return Err(Rgb48FrameError::GeometryOverflow(GeometryOverflow::new(
          stride, height,
        )));
      }
    };
    if rgb48.len() < plane_min {
      return Err(Rgb48FrameError::InsufficientPlane(InsufficientPlane::new(
        plane_min,
        rgb48.len(),
      )));
    }
    Ok(Self {
      rgb48,
      width,
      height,
      stride,
    })
  }

  /// Constructs a new [`Rgb48Frame`], panicking on invalid inputs.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn new(rgb48: &'a [u16], width: u32, height: u32, stride: u32) -> Self {
    match Self::try_new(rgb48, width, height, stride) {
      Ok(f) => f,
      Err(_) => panic!("invalid Rgb48Frame dimensions or plane length"),
    }
  }

  /// Packed RGB48 plane — `width * 3` u16 elements per row (`R, G, B` per pixel).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn rgb48(&self) -> &'a [u16] {
    self.rgb48
  }
  /// Frame width in pixels.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn width(&self) -> u32 {
    self.width
  }
  /// Frame height in pixels.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn height(&self) -> u32 {
    self.height
  }
  /// Stride in u16 elements (≥ `3 * width`).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn stride(&self) -> u32 {
    self.stride
  }
  /// Returns the compile-time BE flag — `true` if plane bytes are BE-encoded
  /// (`AV_PIX_FMT_RGB48BE`), `false` if LE-encoded (`AV_PIX_FMT_RGB48LE`).
  /// Runtime mirror of the `<const BE: bool>` type parameter.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn is_be(&self) -> bool {
    BE
  }
}

// ---- Bgr48Frame --------------------------------------------------------------

/// Errors returned by [`Bgr48Frame::try_new`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, IsVariant, TryUnwrap, Unwrap, Error)]
#[non_exhaustive]
#[unwrap(ref, ref_mut)]
#[try_unwrap(ref, ref_mut)]
pub enum Bgr48FrameError {
  /// `width` or `height` was zero.
  #[error(transparent)]
  ZeroDimension(ZeroDimension),

  /// `stride < 3 * width` (in u16 elements).
  #[error(transparent)]
  InsufficientStride(InsufficientStride),

  /// Plane is shorter than `stride * height` u16 elements.
  #[error(transparent)]
  InsufficientPlane(InsufficientPlane),

  /// `stride * height` overflows `usize`.
  #[error(transparent)]
  GeometryOverflow(GeometryOverflow),

  /// `3 * width` overflows `u32`.
  #[error(transparent)]
  WidthOverflow(WidthOverflow),
}

/// A validated packed **BGR48** frame (`AV_PIX_FMT_BGR48{LE,BE}`) — three
/// `u16` samples per pixel in `B, G, R` order. Channel order is reversed
/// relative to [`Rgb48Frame`]; stride convention and element type are
/// identical.
///
/// The `<const BE: bool>` parameter selects the plane byte order; see
/// [`Rgb48Frame`] for the full contract.
///
/// # Aliases
/// - [`Bgr48LeFrame`] = `Bgr48Frame<'a, false>`.
/// - [`Bgr48BeFrame`] = `Bgr48Frame<'a, true>`.
#[derive(Debug, Clone, Copy)]
pub struct Bgr48Frame<'a, const BE: bool = false> {
  bgr48: &'a [u16],
  width: u32,
  height: u32,
  stride: u32,
}

/// LE-encoded `Bgr48Frame` (`AV_PIX_FMT_BGR48LE`).
pub type Bgr48LeFrame<'a> = Bgr48Frame<'a, false>;

/// BE-encoded `Bgr48Frame` (`AV_PIX_FMT_BGR48BE`).
pub type Bgr48BeFrame<'a> = Bgr48Frame<'a, true>;

impl<'a, const BE: bool> Bgr48Frame<'a, BE> {
  /// Constructs a new [`Bgr48Frame`], validating dimensions and plane length.
  /// `<const BE: bool>` selects LE (`false`, default) vs BE (`true`) plane
  /// byte order; row kernels perform the byte-swap.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn try_new(
    bgr48: &'a [u16],
    width: u32,
    height: u32,
    stride: u32,
  ) -> Result<Self, Bgr48FrameError> {
    if width == 0 || height == 0 {
      return Err(Bgr48FrameError::ZeroDimension(ZeroDimension::new(
        width, height,
      )));
    }
    let min_stride = match width.checked_mul(3) {
      Some(v) => v,
      None => return Err(Bgr48FrameError::WidthOverflow(WidthOverflow::new(width))),
    };
    if stride < min_stride {
      return Err(Bgr48FrameError::InsufficientStride(
        InsufficientStride::new(stride, min_stride),
      ));
    }
    let plane_min = match (stride as usize).checked_mul(height as usize) {
      Some(v) => v,
      None => {
        return Err(Bgr48FrameError::GeometryOverflow(GeometryOverflow::new(
          stride, height,
        )));
      }
    };
    if bgr48.len() < plane_min {
      return Err(Bgr48FrameError::InsufficientPlane(InsufficientPlane::new(
        plane_min,
        bgr48.len(),
      )));
    }
    Ok(Self {
      bgr48,
      width,
      height,
      stride,
    })
  }

  /// Constructs a new [`Bgr48Frame`], panicking on invalid inputs.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn new(bgr48: &'a [u16], width: u32, height: u32, stride: u32) -> Self {
    match Self::try_new(bgr48, width, height, stride) {
      Ok(f) => f,
      Err(_) => panic!("invalid Bgr48Frame dimensions or plane length"),
    }
  }

  /// Packed BGR48 plane — `width * 3` u16 elements per row (`B, G, R` per pixel).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn bgr48(&self) -> &'a [u16] {
    self.bgr48
  }
  /// Frame width in pixels.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn width(&self) -> u32 {
    self.width
  }
  /// Frame height in pixels.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn height(&self) -> u32 {
    self.height
  }
  /// Stride in u16 elements (≥ `3 * width`).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn stride(&self) -> u32 {
    self.stride
  }
  /// Runtime mirror of the `<const BE: bool>` type parameter.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn is_be(&self) -> bool {
    BE
  }
}

// ---- Rgba64Frame -------------------------------------------------------------

/// Errors returned by [`Rgba64Frame::try_new`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, IsVariant, TryUnwrap, Unwrap, Error)]
#[non_exhaustive]
#[unwrap(ref, ref_mut)]
#[try_unwrap(ref, ref_mut)]
pub enum Rgba64FrameError {
  /// `width` or `height` was zero.
  #[error(transparent)]
  ZeroDimension(ZeroDimension),

  /// `stride < 4 * width` (in u16 elements).
  #[error(transparent)]
  InsufficientStride(InsufficientStride),

  /// Plane is shorter than `stride * height` u16 elements.
  #[error(transparent)]
  InsufficientPlane(InsufficientPlane),

  /// `stride * height` overflows `usize`.
  #[error(transparent)]
  GeometryOverflow(GeometryOverflow),

  /// `4 * width` overflows `u32`.
  #[error(transparent)]
  WidthOverflow(WidthOverflow),
}

/// A validated packed **RGBA64** frame (`AV_PIX_FMT_RGBA64{LE,BE}`) — four
/// `u16` samples per pixel in `R, G, B, A` order. The alpha channel is real
/// (not padding) and is passed through by `with_rgba` / `with_rgba_u16`.
///
/// The `<const BE: bool>` parameter selects the plane byte order; see
/// [`Rgb48Frame`] for the full contract.
///
/// # Aliases
/// - [`Rgba64LeFrame`] = `Rgba64Frame<'a, false>`.
/// - [`Rgba64BeFrame`] = `Rgba64Frame<'a, true>`.
#[derive(Debug, Clone, Copy)]
pub struct Rgba64Frame<'a, const BE: bool = false> {
  rgba64: &'a [u16],
  width: u32,
  height: u32,
  stride: u32,
}

/// LE-encoded `Rgba64Frame` (`AV_PIX_FMT_RGBA64LE`).
pub type Rgba64LeFrame<'a> = Rgba64Frame<'a, false>;

/// BE-encoded `Rgba64Frame` (`AV_PIX_FMT_RGBA64BE`).
pub type Rgba64BeFrame<'a> = Rgba64Frame<'a, true>;

impl<'a, const BE: bool> Rgba64Frame<'a, BE> {
  /// Constructs a new [`Rgba64Frame`], validating dimensions and plane length.
  /// `<const BE: bool>` selects LE (`false`, default) vs BE (`true`) plane
  /// byte order; row kernels perform the byte-swap.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn try_new(
    rgba64: &'a [u16],
    width: u32,
    height: u32,
    stride: u32,
  ) -> Result<Self, Rgba64FrameError> {
    if width == 0 || height == 0 {
      return Err(Rgba64FrameError::ZeroDimension(ZeroDimension::new(
        width, height,
      )));
    }
    let min_stride = match width.checked_mul(4) {
      Some(v) => v,
      None => return Err(Rgba64FrameError::WidthOverflow(WidthOverflow::new(width))),
    };
    if stride < min_stride {
      return Err(Rgba64FrameError::InsufficientStride(
        InsufficientStride::new(stride, min_stride),
      ));
    }
    let plane_min = match (stride as usize).checked_mul(height as usize) {
      Some(v) => v,
      None => {
        return Err(Rgba64FrameError::GeometryOverflow(GeometryOverflow::new(
          stride, height,
        )));
      }
    };
    if rgba64.len() < plane_min {
      return Err(Rgba64FrameError::InsufficientPlane(InsufficientPlane::new(
        plane_min,
        rgba64.len(),
      )));
    }
    Ok(Self {
      rgba64,
      width,
      height,
      stride,
    })
  }

  /// Constructs a new [`Rgba64Frame`], panicking on invalid inputs.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn new(rgba64: &'a [u16], width: u32, height: u32, stride: u32) -> Self {
    match Self::try_new(rgba64, width, height, stride) {
      Ok(f) => f,
      Err(_) => panic!("invalid Rgba64Frame dimensions or plane length"),
    }
  }

  /// Packed RGBA64 plane — `width * 4` u16 elements per row (`R, G, B, A` per pixel).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn rgba64(&self) -> &'a [u16] {
    self.rgba64
  }
  /// Frame width in pixels.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn width(&self) -> u32 {
    self.width
  }
  /// Frame height in pixels.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn height(&self) -> u32 {
    self.height
  }
  /// Stride in u16 elements (≥ `4 * width`).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn stride(&self) -> u32 {
    self.stride
  }
  /// Runtime mirror of the `<const BE: bool>` type parameter.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn is_be(&self) -> bool {
    BE
  }
}

// ---- Bgra64Frame -------------------------------------------------------------

/// Errors returned by [`Bgra64Frame::try_new`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, IsVariant, TryUnwrap, Unwrap, Error)]
#[non_exhaustive]
#[unwrap(ref, ref_mut)]
#[try_unwrap(ref, ref_mut)]
pub enum Bgra64FrameError {
  /// `width` or `height` was zero.
  #[error(transparent)]
  ZeroDimension(ZeroDimension),

  /// `stride < 4 * width` (in u16 elements).
  #[error(transparent)]
  InsufficientStride(InsufficientStride),

  /// Plane is shorter than `stride * height` u16 elements.
  #[error(transparent)]
  InsufficientPlane(InsufficientPlane),

  /// `stride * height` overflows `usize`.
  #[error(transparent)]
  GeometryOverflow(GeometryOverflow),

  /// `4 * width` overflows `u32`.
  #[error(transparent)]
  WidthOverflow(WidthOverflow),
}

/// A validated packed **BGRA64** frame (`AV_PIX_FMT_BGRA64{LE,BE}`) — four
/// `u16` samples per pixel in `B, G, R, A` order. Channel order is reversed
/// on the first three elements relative to [`Rgba64Frame`]; alpha at position
/// 3 is real and is passed through by `with_rgba` / `with_rgba_u16`.
///
/// The `<const BE: bool>` parameter selects the plane byte order; see
/// [`Rgb48Frame`] for the full contract.
///
/// # Aliases
/// - [`Bgra64LeFrame`] = `Bgra64Frame<'a, false>`.
/// - [`Bgra64BeFrame`] = `Bgra64Frame<'a, true>`.
#[derive(Debug, Clone, Copy)]
pub struct Bgra64Frame<'a, const BE: bool = false> {
  bgra64: &'a [u16],
  width: u32,
  height: u32,
  stride: u32,
}

/// LE-encoded `Bgra64Frame` (`AV_PIX_FMT_BGRA64LE`).
pub type Bgra64LeFrame<'a> = Bgra64Frame<'a, false>;

/// BE-encoded `Bgra64Frame` (`AV_PIX_FMT_BGRA64BE`).
pub type Bgra64BeFrame<'a> = Bgra64Frame<'a, true>;

impl<'a, const BE: bool> Bgra64Frame<'a, BE> {
  /// Constructs a new [`Bgra64Frame`], validating dimensions and plane length.
  /// `<const BE: bool>` selects LE (`false`, default) vs BE (`true`) plane
  /// byte order; row kernels perform the byte-swap.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn try_new(
    bgra64: &'a [u16],
    width: u32,
    height: u32,
    stride: u32,
  ) -> Result<Self, Bgra64FrameError> {
    if width == 0 || height == 0 {
      return Err(Bgra64FrameError::ZeroDimension(ZeroDimension::new(
        width, height,
      )));
    }
    let min_stride = match width.checked_mul(4) {
      Some(v) => v,
      None => return Err(Bgra64FrameError::WidthOverflow(WidthOverflow::new(width))),
    };
    if stride < min_stride {
      return Err(Bgra64FrameError::InsufficientStride(
        InsufficientStride::new(stride, min_stride),
      ));
    }
    let plane_min = match (stride as usize).checked_mul(height as usize) {
      Some(v) => v,
      None => {
        return Err(Bgra64FrameError::GeometryOverflow(GeometryOverflow::new(
          stride, height,
        )));
      }
    };
    if bgra64.len() < plane_min {
      return Err(Bgra64FrameError::InsufficientPlane(InsufficientPlane::new(
        plane_min,
        bgra64.len(),
      )));
    }
    Ok(Self {
      bgra64,
      width,
      height,
      stride,
    })
  }

  /// Constructs a new [`Bgra64Frame`], panicking on invalid inputs.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn new(bgra64: &'a [u16], width: u32, height: u32, stride: u32) -> Self {
    match Self::try_new(bgra64, width, height, stride) {
      Ok(f) => f,
      Err(_) => panic!("invalid Bgra64Frame dimensions or plane length"),
    }
  }

  /// Packed BGRA64 plane — `width * 4` u16 elements per row (`B, G, R, A` per pixel).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn bgra64(&self) -> &'a [u16] {
    self.bgra64
  }
  /// Frame width in pixels.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn width(&self) -> u32 {
    self.width
  }
  /// Frame height in pixels.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn height(&self) -> u32 {
    self.height
  }
  /// Stride in u16 elements (≥ `4 * width`).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn stride(&self) -> u32 {
    self.stride
  }
  /// Runtime mirror of the `<const BE: bool>` type parameter.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn is_be(&self) -> bool {
    BE
  }
}