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
use super::{
  GeometryOverflow, InsufficientPlane, InsufficientStride, WidthAlignment, WidthOverflow,
  ZeroDimension,
};
use derive_more::{IsVariant, TryUnwrap, Unwrap};
use thiserror::Error;

// ============================================================
// Tier 3 — Packed YUV 4:2:2 8-bit source-side frames (Ship 10)
// ============================================================
//
// Three formats share the same layout: a single plane of 8-bit
// bytes, 2 pixels per 4-byte block, horizontal chroma 2:1
// subsampling. They differ only in byte ordering inside each block:
//
// - YUYV422 (YUY2)  — `[Y0, U0, Y1, V0]` per 2-pixel block
// - UYVY422 (UYVY)  — `[U0, Y0, V0, Y1]` per 2-pixel block
// - YVYU422 (YVYU)  — `[Y0, V0, Y1, U0]` per 2-pixel block
//
// Stride is in bytes and must be ≥ `2 * width`; width must be even
// (one chroma sample serves two adjacent Y pixels). The `try_new`
// constructors share an enum shape with the existing `Tier 6`
// packed-RGB frame types.

/// Errors returned by [`Yuyv422Frame::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 Yuyv422FrameError {
  /// `width` or `height` was zero.
  #[error(transparent)]
  ZeroDimension(ZeroDimension),

  /// `width` was odd. Packed YUV 4:2:2 pairs two Y samples per
  /// chroma pair, so each 2-pixel block needs exactly 4 bytes —
  /// odd widths can't form a complete final block.
  #[error(transparent)]
  WidthAlignment(WidthAlignment),

  /// `stride < 2 * width`. Each row needs `2 * width` bytes
  /// (4 bytes per 2-pixel block).
  #[error(transparent)]
  InsufficientStride(InsufficientStride),

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

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

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

/// A validated packed **YUYV422** frame (`AV_PIX_FMT_YUYV422`,
/// also known as YUY2). Single plane, 4 bytes per 2-pixel block,
/// byte order `Y0, U0, Y1, V0` — Y in even byte positions, U/V in
/// odd positions with U preceding V.
///
/// `stride` is in **bytes** (≥ `2 * width`). `width` must be even.
#[derive(Debug, Clone, Copy)]
pub struct Yuyv422Frame<'a> {
  yuyv: &'a [u8],
  width: u32,
  height: u32,
  stride: u32,
}

impl<'a> Yuyv422Frame<'a> {
  /// Constructs a new [`Yuyv422Frame`], validating dimensions and
  /// plane length.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn try_new(
    yuyv: &'a [u8],
    width: u32,
    height: u32,
    stride: u32,
  ) -> Result<Self, Yuyv422FrameError> {
    if width == 0 || height == 0 {
      return Err(Yuyv422FrameError::ZeroDimension(ZeroDimension::new(
        width, height,
      )));
    }
    if width & 1 != 0 {
      return Err(Yuyv422FrameError::WidthAlignment(WidthAlignment::odd(
        width as usize,
      )));
    }
    let min_stride = match width.checked_mul(2) {
      Some(v) => v,
      None => return Err(Yuyv422FrameError::WidthOverflow(WidthOverflow::new(width))),
    };
    if stride < min_stride {
      return Err(Yuyv422FrameError::InsufficientStride(
        InsufficientStride::new(stride, min_stride),
      ));
    }
    let plane_min = match (stride as usize).checked_mul(height as usize) {
      Some(v) => v,
      None => {
        return Err(Yuyv422FrameError::GeometryOverflow(GeometryOverflow::new(
          stride, height,
        )));
      }
    };
    if yuyv.len() < plane_min {
      return Err(Yuyv422FrameError::InsufficientPlane(
        InsufficientPlane::new(plane_min, yuyv.len()),
      ));
    }
    Ok(Self {
      yuyv,
      width,
      height,
      stride,
    })
  }

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

  /// Packed YUYV plane bytes (`Y0, U0, Y1, V0, Y2, U2, Y3, V2, …`).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn yuyv(&self) -> &'a [u8] {
    self.yuyv
  }
  /// Frame width in pixels (even).
  #[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
  }
  /// Byte stride (`>= 2 * width`).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn stride(&self) -> u32 {
    self.stride
  }
}

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

  /// `width` was odd.
  #[error(transparent)]
  WidthAlignment(WidthAlignment),

  /// `stride < 2 * width`.
  #[error(transparent)]
  InsufficientStride(InsufficientStride),

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

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

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

/// A validated packed **UYVY422** frame (`AV_PIX_FMT_UYVY422`).
/// Single plane, 4 bytes per 2-pixel block, byte order
/// `U0, Y0, V0, Y1` — Y in odd byte positions, U/V in even
/// positions. The de-facto SDI capture format on Apple
/// QuickTime / VideoToolbox 8-bit paths.
///
/// `stride` is in **bytes** (≥ `2 * width`). `width` must be even.
#[derive(Debug, Clone, Copy)]
pub struct Uyvy422Frame<'a> {
  uyvy: &'a [u8],
  width: u32,
  height: u32,
  stride: u32,
}

impl<'a> Uyvy422Frame<'a> {
  /// Constructs a new [`Uyvy422Frame`], validating dimensions and
  /// plane length.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn try_new(
    uyvy: &'a [u8],
    width: u32,
    height: u32,
    stride: u32,
  ) -> Result<Self, Uyvy422FrameError> {
    if width == 0 || height == 0 {
      return Err(Uyvy422FrameError::ZeroDimension(ZeroDimension::new(
        width, height,
      )));
    }
    if width & 1 != 0 {
      return Err(Uyvy422FrameError::WidthAlignment(WidthAlignment::odd(
        width as usize,
      )));
    }
    let min_stride = match width.checked_mul(2) {
      Some(v) => v,
      None => return Err(Uyvy422FrameError::WidthOverflow(WidthOverflow::new(width))),
    };
    if stride < min_stride {
      return Err(Uyvy422FrameError::InsufficientStride(
        InsufficientStride::new(stride, min_stride),
      ));
    }
    let plane_min = match (stride as usize).checked_mul(height as usize) {
      Some(v) => v,
      None => {
        return Err(Uyvy422FrameError::GeometryOverflow(GeometryOverflow::new(
          stride, height,
        )));
      }
    };
    if uyvy.len() < plane_min {
      return Err(Uyvy422FrameError::InsufficientPlane(
        InsufficientPlane::new(plane_min, uyvy.len()),
      ));
    }
    Ok(Self {
      uyvy,
      width,
      height,
      stride,
    })
  }

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

  /// Packed UYVY plane bytes (`U0, Y0, V0, Y1, U2, Y2, V2, Y3, …`).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn uyvy(&self) -> &'a [u8] {
    self.uyvy
  }
  /// Frame width in pixels (even).
  #[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
  }
  /// Byte stride (`>= 2 * width`).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn stride(&self) -> u32 {
    self.stride
  }
}

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

  /// `width` was odd.
  #[error(transparent)]
  WidthAlignment(WidthAlignment),

  /// `stride < 2 * width`.
  #[error(transparent)]
  InsufficientStride(InsufficientStride),

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

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

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

/// A validated packed **YVYU422** frame (`AV_PIX_FMT_YVYU422`).
/// Single plane, 4 bytes per 2-pixel block, byte order
/// `Y0, V0, Y1, U0` — same Y positions as YUYV but with V/U
/// swapped relative to YUYV (V precedes U). Common on Android
/// camera HAL outputs.
///
/// `stride` is in **bytes** (≥ `2 * width`). `width` must be even.
#[derive(Debug, Clone, Copy)]
pub struct Yvyu422Frame<'a> {
  yvyu: &'a [u8],
  width: u32,
  height: u32,
  stride: u32,
}

impl<'a> Yvyu422Frame<'a> {
  /// Constructs a new [`Yvyu422Frame`], validating dimensions and
  /// plane length.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn try_new(
    yvyu: &'a [u8],
    width: u32,
    height: u32,
    stride: u32,
  ) -> Result<Self, Yvyu422FrameError> {
    if width == 0 || height == 0 {
      return Err(Yvyu422FrameError::ZeroDimension(ZeroDimension::new(
        width, height,
      )));
    }
    if width & 1 != 0 {
      return Err(Yvyu422FrameError::WidthAlignment(WidthAlignment::odd(
        width as usize,
      )));
    }
    let min_stride = match width.checked_mul(2) {
      Some(v) => v,
      None => return Err(Yvyu422FrameError::WidthOverflow(WidthOverflow::new(width))),
    };
    if stride < min_stride {
      return Err(Yvyu422FrameError::InsufficientStride(
        InsufficientStride::new(stride, min_stride),
      ));
    }
    let plane_min = match (stride as usize).checked_mul(height as usize) {
      Some(v) => v,
      None => {
        return Err(Yvyu422FrameError::GeometryOverflow(GeometryOverflow::new(
          stride, height,
        )));
      }
    };
    if yvyu.len() < plane_min {
      return Err(Yvyu422FrameError::InsufficientPlane(
        InsufficientPlane::new(plane_min, yvyu.len()),
      ));
    }
    Ok(Self {
      yvyu,
      width,
      height,
      stride,
    })
  }

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

  /// Packed YVYU plane bytes (`Y0, V0, Y1, U0, Y2, V2, Y3, U2, …`).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn yvyu(&self) -> &'a [u8] {
    self.yvyu
  }
  /// Frame width in pixels (even).
  #[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
  }
  /// Byte stride (`>= 2 * width`).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn stride(&self) -> u32 {
    self.stride
  }
}