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
//! Video frame types.
use oximedia_core::{PixelFormat, Rational, Timestamp};
/// Decoded video frame.
#[derive(Clone, Debug)]
pub struct VideoFrame {
/// Pixel format.
pub format: PixelFormat,
/// Frame width in pixels.
pub width: u32,
/// Frame height in pixels.
pub height: u32,
/// Plane data.
pub planes: Vec<Plane>,
/// Presentation timestamp.
pub timestamp: Timestamp,
/// Frame type (I/P/B).
pub frame_type: FrameType,
/// Color information.
pub color_info: ColorInfo,
/// Frame is corrupt (concealment applied).
pub corrupt: bool,
}
impl VideoFrame {
/// Create a new video frame.
#[must_use]
pub fn new(format: PixelFormat, width: u32, height: u32) -> Self {
Self {
format,
width,
height,
planes: Vec::new(),
timestamp: Timestamp::new(0, Rational::new(1, 1000)),
frame_type: FrameType::Key,
color_info: ColorInfo::default(),
corrupt: false,
}
}
/// Allocate planes for the frame format.
pub fn allocate(&mut self) {
let plane_count = self.format.plane_count();
self.planes.clear();
for i in 0..plane_count {
let (width, height) = self.plane_dimensions(i as usize);
let stride = width as usize;
let size = stride * height as usize;
let data = vec![0u8; size];
self.planes.push(Plane {
data,
stride,
width,
height,
});
}
}
/// Get plane dimensions.
#[must_use]
pub fn plane_dimensions(&self, plane_index: usize) -> (u32, u32) {
let (h_ratio, v_ratio) = self.format.chroma_subsampling();
if plane_index == 0 {
// Luma plane
(self.width, self.height)
} else {
// Chroma planes - use ratio directly for division
let chroma_width = self.width.div_ceil(h_ratio);
let chroma_height = self.height.div_ceil(v_ratio);
(chroma_width, chroma_height)
}
}
/// Get total frame size in bytes.
#[must_use]
pub fn size_bytes(&self) -> usize {
self.planes.iter().map(|p| p.data.len()).sum()
}
/// Check if frame is a keyframe.
#[must_use]
pub fn is_keyframe(&self) -> bool {
self.frame_type == FrameType::Key
}
/// Get reference to plane by index.
#[must_use]
pub fn plane(&self, index: usize) -> &Plane {
&self.planes[index]
}
/// Get mutable reference to plane by index.
#[must_use]
pub fn plane_mut(&mut self, index: usize) -> &mut Plane {
&mut self.planes[index]
}
}
/// Single plane of video data.
#[derive(Clone, Debug)]
pub struct Plane {
/// Pixel data.
pub data: Vec<u8>,
/// Row stride in bytes.
pub stride: usize,
/// Plane width in pixels.
pub width: u32,
/// Plane height in pixels.
pub height: u32,
}
impl Plane {
/// Create a new plane.
#[must_use]
pub fn new(data: Vec<u8>, stride: usize) -> Self {
Self {
data,
stride,
width: 0,
height: 0,
}
}
/// Create a new plane with width and height.
#[must_use]
pub fn with_dimensions(data: Vec<u8>, stride: usize, width: u32, height: u32) -> Self {
Self {
data,
stride,
width,
height,
}
}
/// Get row at given y coordinate.
#[must_use]
pub fn row(&self, y: usize) -> &[u8] {
let start = y * self.stride;
let end = start + self.stride;
if end <= self.data.len() {
&self.data[start..end]
} else {
&[]
}
}
/// Get plane width.
#[must_use]
pub const fn width(&self) -> u32 {
self.width
}
/// Get plane height.
#[must_use]
pub const fn height(&self) -> u32 {
self.height
}
/// Get plane stride.
#[must_use]
pub const fn stride(&self) -> usize {
self.stride
}
/// Get immutable reference to plane data.
#[must_use]
pub fn data(&self) -> &[u8] {
&self.data
}
/// Get mutable reference to plane data.
#[must_use]
pub fn data_mut(&mut self) -> &mut [u8] {
&mut self.data
}
}
/// Frame type.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum FrameType {
/// Keyframe (I-frame) - can be decoded independently.
#[default]
Key,
/// Inter frame (P-frame) - references previous frames.
Inter,
/// Bidirectional frame (B-frame) - references past and future.
BiDir,
/// Switch frame - allows stream switching.
Switch,
}
impl FrameType {
/// Check if this is a reference frame.
#[must_use]
pub fn is_reference(&self) -> bool {
matches!(self, Self::Key | Self::Inter)
}
}
/// Color information for video frames.
#[derive(Clone, Copy, Debug, Default)]
pub struct ColorInfo {
/// Color primaries.
pub primaries: ColorPrimaries,
/// Transfer characteristics.
pub transfer: TransferCharacteristics,
/// Matrix coefficients.
pub matrix: MatrixCoefficients,
/// Full range (0-255) vs limited range (16-235).
pub full_range: bool,
}
/// Color primaries (ITU-T H.273).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ColorPrimaries {
/// BT.709 (sRGB, HD).
#[default]
Bt709 = 1,
/// Unspecified.
Unspecified = 2,
/// BT.470M (obsolete NTSC).
Bt470M = 4,
/// BT.470BG (PAL/SECAM).
Bt470Bg = 5,
/// SMPTE 170M (NTSC).
Smpte170M = 6,
/// SMPTE 240M.
Smpte240M = 7,
/// Generic film.
Film = 8,
/// BT.2020 (UHD).
Bt2020 = 9,
/// SMPTE ST 428-1.
Smpte428 = 10,
/// SMPTE RP 431-2.
Smpte431 = 11,
/// SMPTE EG 432-1 (P3).
Smpte432 = 12,
/// EBU Tech 3213-E.
Ebu3213 = 22,
}
/// Transfer characteristics (ITU-T H.273).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TransferCharacteristics {
/// BT.709.
#[default]
Bt709 = 1,
/// Unspecified.
Unspecified = 2,
/// BT.470M.
Bt470M = 4,
/// BT.470BG.
Bt470Bg = 5,
/// SMPTE 170M.
Smpte170M = 6,
/// SMPTE 240M.
Smpte240M = 7,
/// Linear.
Linear = 8,
/// Logarithmic 100:1.
Log100 = 9,
/// Logarithmic 100*sqrt(10):1.
Log316 = 10,
/// IEC 61966-2-4.
Iec619662_4 = 11,
/// BT.1361.
Bt1361 = 12,
/// sRGB/sYCC.
Srgb = 13,
/// BT.2020 10-bit.
Bt202010 = 14,
/// BT.2020 12-bit.
Bt202012 = 15,
/// SMPTE ST 2084 (PQ/HDR10).
Smpte2084 = 16,
/// SMPTE ST 428-1.
Smpte428 = 17,
/// ARIB STD-B67 (HLG).
AribStdB67 = 18,
}
/// Matrix coefficients (ITU-T H.273).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum MatrixCoefficients {
/// Identity (RGB).
Identity = 0,
/// BT.709.
#[default]
Bt709 = 1,
/// Unspecified.
Unspecified = 2,
/// FCC.
Fcc = 4,
/// BT.470BG.
Bt470Bg = 5,
/// SMPTE 170M.
Smpte170M = 6,
/// SMPTE 240M.
Smpte240M = 7,
/// `YCgCo`.
Ycgco = 8,
/// BT.2020 non-constant.
Bt2020Ncl = 9,
/// BT.2020 constant.
Bt2020Cl = 10,
/// SMPTE 2085.
Smpte2085 = 11,
/// Chromaticity-derived non-constant.
ChromaDerivedNcl = 12,
/// Chromaticity-derived constant.
ChromaDerivedCl = 13,
/// `ICtCp`.
Ictcp = 14,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_video_frame_new() {
let frame = VideoFrame::new(PixelFormat::Yuv420p, 1920, 1080);
assert_eq!(frame.width, 1920);
assert_eq!(frame.height, 1080);
assert_eq!(frame.format, PixelFormat::Yuv420p);
}
#[test]
fn test_plane_dimensions() {
let frame = VideoFrame::new(PixelFormat::Yuv420p, 1920, 1080);
assert_eq!(frame.plane_dimensions(0), (1920, 1080));
assert_eq!(frame.plane_dimensions(1), (960, 540));
assert_eq!(frame.plane_dimensions(2), (960, 540));
}
#[test]
fn test_frame_allocate() {
let mut frame = VideoFrame::new(PixelFormat::Yuv420p, 1920, 1080);
frame.allocate();
assert_eq!(frame.planes.len(), 3);
// Y: 1920 * 1080, U: 960 * 540, V: 960 * 540
assert_eq!(frame.planes[0].data.len(), 1920 * 1080);
assert_eq!(frame.planes[1].data.len(), 960 * 540);
}
#[test]
fn test_frame_type() {
assert!(FrameType::Key.is_reference());
assert!(FrameType::Inter.is_reference());
assert!(!FrameType::BiDir.is_reference());
}
}