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
// Copyright (c) 2017-2025, The rav1e contributors. All rights reserved
//
// This source code is subject to the terms of the BSD 2 Clause License and
// the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
// was not distributed with this source code in the LICENSE file, you can
// obtain it at www.aomedia.org/license/software. If the Alliance for Open
// Media Patent License 1.0 was not distributed with this source code in the
// PATENTS file, you can obtain it at www.aomedia.org/license/patent.
//! Pixel data type abstractions.
//!
//! This module defines the [`Pixel`] trait, which abstracts over the pixel data types
//! used throughout the library. This allows the same code to work with both 8-bit
//! (`u8`) and high bit-depth (`u16`) pixel data.
//!
//! # Supported Pixel Types
//!
//! - `u8`: For 8-bit pixel data
//! - `u16`: For 9-16 bit pixel data (high bit-depth)
//!
//! The type used must match the bit depth specified when creating frames:
//! - 8-bit frames must use `u8`
//! - 9-16 bit frames must use `u16`
//!
//! # Working with `T: Pixel`
//!
//! It is often necessary to convert between an abstract pixel and their concrete
//! integer representation in order to work with them, for example to implement an
//! algorithm operating on video frames.
//!
//! For this purpose, `Pixel` is a supertrait of some conversions from the standard
//! library, namely:
//! - `Into<u16>` to convert any pixel into a `u16`,
//! - `TryInto<u8>` to try to convert any pixel into a `u8` (not possible if the pixel value
//! is outside of the range representable by `u8`),
//! - `From<u8>` to convert any `u8` into a Pixel containing the given value and
//! - `TryFrom<u16>` to try to convert any `u16` into a `T` (not possible if the source value
//! is outside of the range representable by `T`).
//!
//! For example, to sum all pixels in a row:
//!
//! ```
//! use v_frame::frame::Frame;
//! use v_frame::pixel::Pixel;
//!
//! pub fn summed_rows<T: Pixel>(frame: &Frame<T>) -> Vec<u64> {
//! frame.y_plane
//! .rows()
//! // row is &[T] here
//! .map(|row| {
//! row.iter()
//! .map(|&pix| pix.into()) // convert T -> u16
//! .map(|pix| u64::from(pix)) // widen so sum doesn't overflow
//! .sum()
//! })
//! .collect()
//! }
//! ```
//!
//! Note that Pixel only provides one `Into<T>` implementation so it is not necessary to
//! explicitly specify the wanted type (`<T as Into<u16>>::into(pix)`).
//!
//! Here's a somewhat contrived example of how to convert integers into pixels:
//!
//! ```
//! use v_frame::frame::Frame;
//! use v_frame::pixel::Pixel;
//!
//! fn random_value() -> i32 {
//! // chosen by a fair dice roll
//! 4
//! }
//!
//! fn clamp_to_range(value: i32, bit_depth: u8) -> u16 {
//! assert!(
//! bit_depth >= 8 && bit_depth <= 16,
//! "only bit depths 8-16 are supported"
//! );
//! let bit_depth_max = (1i32 << bit_depth) - 1;
//!
//! if value < 0 {
//! 0
//! } else if value > bit_depth_max {
//! bit_depth_max as u16
//! } else {
//! value as u16
//! }
//! }
//!
//! pub fn change_some_rows<T: Pixel>(frame: &mut Frame<T>, bit_depth: u8) {
//! for pix in frame.y_plane.pixels_mut() {
//! let old_val = i32::from((*pix).into());
//!
//! let new_val = clamp_to_range(old_val + random_value(), bit_depth);
//! if size_of::<T>() == 1 {
//! *pix = T::from(new_val as u8)
//! } else {
//! *pix = T::try_from(new_val).expect("T is u16");
//! }
//! }
//! }
//! ```
//!
//! Again, `From<u8>` and `TryFrom<u16>` are the only implementations for the respective traits
//! so it's not necessary to specify which one is being used.
use ;
use Hash;
/// A trait for types that can be used as pixel data.
///
/// This trait abstracts over the pixel data types supported by the library,
/// currently `u8` for 8-bit data and `u16` for high bit-depth (9-16 bit) data.
///
/// All frame and plane types are generic over `T: Pixel`, allowing the same
/// data structures and algorithms to work with both standard and high bit-depth
/// video content.
///
/// See the [module documentation][crate::pixel] for more details.
///
/// # Type Safety
///
/// The library enforces correct type usage through validation:
/// - Frames with 8-bit depth can only be created with `T = u8`
/// - Frames with 9-16 bit depth can only be created with `T = u16`
///
/// Attempting to create a frame with a mismatched type will result in
/// [`FrameError::DataTypeMismatch`][crate::frame::FrameError::DataTypeMismatch].
///
/// # Safety
///
/// All implementing types must be valid if represented by an all-zero byte-pattern,
/// i.e. using [`std::mem::zeroed`] must __not__ cause undefined behavior for
/// implementing types.
pub unsafe
/// Pixel implementation for 8-bit video data.
// SAFETY: u8 is valid if represented by a zeroed byte.
unsafe
/// Pixel implementation for high bit-depth (9-16 bit) video data.
// SAFETY: u16 is valid if represented by zeroed bytes.
unsafe