v_frame 0.5.2

Video Frame data structures, originally part of rav1e
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
// Copyright (c) 2018-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.

#![allow(clippy::unwrap_used, reason = "test file")]

use super::*;
use crate::chroma::ChromaSubsampling;

#[test]
fn basic_8bit_frame() {
    let width = NonZeroUsize::new(1920).unwrap();
    let height = NonZeroUsize::new(1080).unwrap();
    let bit_depth = NonZeroU8::new(8).unwrap();

    let frame = FrameBuilder::new(width, height, ChromaSubsampling::Yuv420, bit_depth)
        .build::<u8>()
        .unwrap();

    assert_eq!(frame.y_plane.width().get(), 1920);
    assert_eq!(frame.y_plane.height().get(), 1080);
    assert_eq!(frame.bit_depth.get(), 8);
    assert_eq!(frame.subsampling, ChromaSubsampling::Yuv420);
}

#[test]
fn basic_10bit_frame() {
    let width = NonZeroUsize::new(3840).unwrap();
    let height = NonZeroUsize::new(2160).unwrap();
    let bit_depth = NonZeroU8::new(10).unwrap();

    let frame = FrameBuilder::new(width, height, ChromaSubsampling::Yuv420, bit_depth)
        .build::<u16>()
        .unwrap();

    assert_eq!(frame.y_plane.width().get(), 3840);
    assert_eq!(frame.y_plane.height().get(), 2160);
    assert_eq!(frame.bit_depth.get(), 10);
}

#[test]
fn yuv420_chroma_dimensions() {
    let width = NonZeroUsize::new(1920).unwrap();
    let height = NonZeroUsize::new(1080).unwrap();
    let bit_depth = NonZeroU8::new(8).unwrap();

    let frame = FrameBuilder::new(width, height, ChromaSubsampling::Yuv420, bit_depth)
        .build::<u8>()
        .unwrap();

    let u_plane = frame.u_plane.as_ref().unwrap();
    let v_plane = frame.v_plane.as_ref().unwrap();

    // YUV420 has chroma planes at half width and half height
    assert_eq!(u_plane.width().get(), 960);
    assert_eq!(u_plane.height().get(), 540);
    assert_eq!(v_plane.width().get(), 960);
    assert_eq!(v_plane.height().get(), 540);
}

#[test]
fn yuv422_chroma_dimensions() {
    let width = NonZeroUsize::new(1920).unwrap();
    let height = NonZeroUsize::new(1080).unwrap();
    let bit_depth = NonZeroU8::new(8).unwrap();

    let frame = FrameBuilder::new(width, height, ChromaSubsampling::Yuv422, bit_depth)
        .build::<u8>()
        .unwrap();

    let u_plane = frame.u_plane.as_ref().unwrap();
    let v_plane = frame.v_plane.as_ref().unwrap();

    // YUV422 has chroma planes at half width and full height
    assert_eq!(u_plane.width().get(), 960);
    assert_eq!(u_plane.height().get(), 1080);
    assert_eq!(v_plane.width().get(), 960);
    assert_eq!(v_plane.height().get(), 1080);
}

#[test]
fn yuv444_chroma_dimensions() {
    let width = NonZeroUsize::new(1920).unwrap();
    let height = NonZeroUsize::new(1080).unwrap();
    let bit_depth = NonZeroU8::new(8).unwrap();

    let frame = FrameBuilder::new(width, height, ChromaSubsampling::Yuv444, bit_depth)
        .build::<u8>()
        .unwrap();

    let u_plane = frame.u_plane.as_ref().unwrap();
    let v_plane = frame.v_plane.as_ref().unwrap();

    // YUV444 has chroma planes at full resolution
    assert_eq!(u_plane.width().get(), 1920);
    assert_eq!(u_plane.height().get(), 1080);
    assert_eq!(v_plane.width().get(), 1920);
    assert_eq!(v_plane.height().get(), 1080);
}

#[test]
fn monochrome_no_chroma_planes() {
    let width = NonZeroUsize::new(1920).unwrap();
    let height = NonZeroUsize::new(1080).unwrap();
    let bit_depth = NonZeroU8::new(8).unwrap();

    let frame = FrameBuilder::new(width, height, ChromaSubsampling::Monochrome, bit_depth)
        .build::<u8>()
        .unwrap();

    assert_eq!(frame.y_plane.width().get(), 1920);
    assert_eq!(frame.y_plane.height().get(), 1080);
    assert!(frame.u_plane.is_none());
    assert!(frame.v_plane.is_none());
    assert_eq!(frame.subsampling, ChromaSubsampling::Monochrome);
}

#[test]
fn unsupported_bit_depth_too_low() {
    let width = NonZeroUsize::new(1920).unwrap();
    let height = NonZeroUsize::new(1080).unwrap();
    let bit_depth = NonZeroU8::new(7).unwrap();

    let result =
        FrameBuilder::new(width, height, ChromaSubsampling::Yuv420, bit_depth).build::<u8>();

    assert!(matches!(
        result,
        Err(Error::UnsupportedBitDepth { found: 7 })
    ));
}

#[test]
fn unsupported_bit_depth_too_high() {
    let width = NonZeroUsize::new(1920).unwrap();
    let height = NonZeroUsize::new(1080).unwrap();
    let bit_depth = NonZeroU8::new(17).unwrap();

    let result =
        FrameBuilder::new(width, height, ChromaSubsampling::Yuv420, bit_depth).build::<u16>();

    assert!(matches!(
        result,
        Err(Error::UnsupportedBitDepth { found: 17 })
    ));
}

#[test]
fn data_type_mismatch_u8_with_10bit() {
    let width = NonZeroUsize::new(1920).unwrap();
    let height = NonZeroUsize::new(1080).unwrap();
    let bit_depth = NonZeroU8::new(10).unwrap();

    let result =
        FrameBuilder::new(width, height, ChromaSubsampling::Yuv420, bit_depth).build::<u8>();

    assert!(matches!(result, Err(Error::DataTypeMismatch)));
}

#[test]
fn data_type_mismatch_u16_with_8bit() {
    let width = NonZeroUsize::new(1920).unwrap();
    let height = NonZeroUsize::new(1080).unwrap();
    let bit_depth = NonZeroU8::new(8).unwrap();

    let result =
        FrameBuilder::new(width, height, ChromaSubsampling::Yuv420, bit_depth).build::<u16>();

    assert!(matches!(result, Err(Error::DataTypeMismatch)));
}

#[test]
fn yuv420_odd_width_resolution_error() {
    let width = NonZeroUsize::new(1921).unwrap(); // odd width
    let height = NonZeroUsize::new(1080).unwrap();
    let bit_depth = NonZeroU8::new(8).unwrap();

    let result =
        FrameBuilder::new(width, height, ChromaSubsampling::Yuv420, bit_depth).build::<u8>();

    assert!(matches!(result, Err(Error::UnsupportedResolution)));
}

#[test]
fn yuv420_odd_height_resolution_error() {
    let width = NonZeroUsize::new(1920).unwrap();
    let height = NonZeroUsize::new(1081).unwrap(); // odd height
    let bit_depth = NonZeroU8::new(8).unwrap();

    let result =
        FrameBuilder::new(width, height, ChromaSubsampling::Yuv420, bit_depth).build::<u8>();

    assert!(matches!(result, Err(Error::UnsupportedResolution)));
}

#[test]
fn yuv422_odd_width_resolution_error() {
    let width = NonZeroUsize::new(1921).unwrap(); // odd width
    let height = NonZeroUsize::new(1080).unwrap();
    let bit_depth = NonZeroU8::new(8).unwrap();

    let result =
        FrameBuilder::new(width, height, ChromaSubsampling::Yuv422, bit_depth).build::<u8>();

    assert!(matches!(result, Err(Error::UnsupportedResolution)));
}

#[test]
fn frame_with_luma_padding() {
    let width = NonZeroUsize::new(1920).unwrap();
    let height = NonZeroUsize::new(1080).unwrap();
    let bit_depth = NonZeroU8::new(8).unwrap();

    let frame = FrameBuilder::new(width, height, ChromaSubsampling::Yuv420, bit_depth)
        .luma_padding_left(16)
        .luma_padding_right(16)
        .luma_padding_top(16)
        .luma_padding_bottom(16)
        .build::<u8>()
        .unwrap();

    // Visible dimensions should remain unchanged
    assert_eq!(frame.y_plane.width().get(), 1920);
    assert_eq!(frame.y_plane.height().get(), 1080);
}

#[test]
fn chroma_padding_derived_from_luma_yuv420() {
    let width = NonZeroUsize::new(1920).unwrap();
    let height = NonZeroUsize::new(1080).unwrap();
    let bit_depth = NonZeroU8::new(8).unwrap();

    let frame = FrameBuilder::new(width, height, ChromaSubsampling::Yuv420, bit_depth)
        .luma_padding_left(16)
        .luma_padding_right(16)
        .luma_padding_top(16)
        .luma_padding_bottom(16)
        .build::<u8>()
        .unwrap();

    // For YUV420, chroma padding should be half of luma padding
    let u_plane = frame.u_plane.as_ref().unwrap();
    assert_eq!(u_plane.width().get(), 960); // chroma width
    assert_eq!(u_plane.height().get(), 540); // chroma height
}

#[test]
fn padding_not_aligned_to_subsampling_yuv420() {
    let width = NonZeroUsize::new(1920).unwrap();
    let height = NonZeroUsize::new(1080).unwrap();
    let bit_depth = NonZeroU8::new(8).unwrap();

    let result = FrameBuilder::new(width, height, ChromaSubsampling::Yuv420, bit_depth)
        // Not divisible by 2 for YUV420
        .luma_padding_left(15)
        .build::<u8>();

    assert!(matches!(result, Err(Error::UnsupportedResolution)));
}

#[test]
fn padding_not_aligned_to_subsampling_yuv422() {
    let width = NonZeroUsize::new(1920).unwrap();
    let height = NonZeroUsize::new(1080).unwrap();
    let bit_depth = NonZeroU8::new(8).unwrap();

    let result = FrameBuilder::new(width, height, ChromaSubsampling::Yuv422, bit_depth)
        // Not divisible by 2 for YUV422
        .luma_padding_left(15)
        .build::<u8>();

    assert!(matches!(result, Err(Error::UnsupportedResolution)));
}

#[test]
fn yuv444_padding_any_value() {
    let width = NonZeroUsize::new(1920).unwrap();
    let height = NonZeroUsize::new(1080).unwrap();
    let bit_depth = NonZeroU8::new(8).unwrap();

    let result = FrameBuilder::new(width, height, ChromaSubsampling::Yuv444, bit_depth)
        // Any value works for YUV444
        .luma_padding_left(15)
        .build::<u8>();

    assert!(result.is_ok());
}

#[test]
fn monochrome_padding_any_value() {
    let width = NonZeroUsize::new(1920).unwrap();
    let height = NonZeroUsize::new(1080).unwrap();
    let bit_depth = NonZeroU8::new(8).unwrap();

    let result = FrameBuilder::new(width, height, ChromaSubsampling::Monochrome, bit_depth)
        .luma_padding_left(15)
        .luma_padding_right(17)
        .luma_padding_top(13)
        .luma_padding_bottom(19)
        .build::<u8>();

    assert!(result.is_ok());
}

#[test]
fn frame_clone() {
    let width = NonZeroUsize::new(640).unwrap();
    let height = NonZeroUsize::new(480).unwrap();
    let bit_depth = NonZeroU8::new(8).unwrap();

    let frame = FrameBuilder::new(width, height, ChromaSubsampling::Yuv420, bit_depth)
        .build::<u8>()
        .unwrap();

    let cloned_frame = frame.clone();

    assert_eq!(cloned_frame.y_plane.width(), frame.y_plane.width());
    assert_eq!(cloned_frame.y_plane.height(), frame.y_plane.height());
    assert_eq!(cloned_frame.bit_depth, frame.bit_depth);
    assert_eq!(cloned_frame.subsampling, frame.subsampling);
}

#[test]
fn all_supported_bit_depths() {
    let width = NonZeroUsize::new(640).unwrap();
    let height = NonZeroUsize::new(480).unwrap();

    // Test 8-bit with u8
    let bit_depth_8 = NonZeroU8::new(8).unwrap();
    assert!(
        FrameBuilder::new(width, height, ChromaSubsampling::Yuv420, bit_depth_8)
            .build::<u8>()
            .is_ok()
    );

    // Test 9-16 bit with u16
    for depth in 9..=16 {
        let bit_depth = NonZeroU8::new(depth).unwrap();
        assert!(
            FrameBuilder::new(width, height, ChromaSubsampling::Yuv420, bit_depth)
                .build::<u16>()
                .is_ok()
        );
    }
}

#[test]
fn small_resolution() {
    let width = NonZeroUsize::new(2).unwrap();
    let height = NonZeroUsize::new(2).unwrap();
    let bit_depth = NonZeroU8::new(8).unwrap();

    let frame = FrameBuilder::new(width, height, ChromaSubsampling::Yuv420, bit_depth)
        .build::<u8>()
        .unwrap();

    assert_eq!(frame.y_plane.width().get(), 2);
    assert_eq!(frame.y_plane.height().get(), 2);

    let u_plane = frame.u_plane.as_ref().unwrap();
    assert_eq!(u_plane.width().get(), 1);
    assert_eq!(u_plane.height().get(), 1);
}

#[test]
fn builder_setters() {
    let width = NonZeroUsize::new(1920).unwrap();
    let height = NonZeroUsize::new(1080).unwrap();
    let bit_depth = NonZeroU8::new(8).unwrap();

    let frame = FrameBuilder::new(width, height, ChromaSubsampling::Yuv420, bit_depth)
        .luma_padding_left(8)
        .luma_padding_right(8)
        .luma_padding_top(8)
        .luma_padding_bottom(8)
        .build::<u8>()
        .unwrap();
    assert!(frame.y_plane.width().get() == 1920);
}

#[test]
fn asymmetric_padding() {
    let width = NonZeroUsize::new(1920).unwrap();
    let height = NonZeroUsize::new(1080).unwrap();
    let bit_depth = NonZeroU8::new(8).unwrap();

    let frame = FrameBuilder::new(width, height, ChromaSubsampling::Yuv420, bit_depth)
        .luma_padding_left(8)
        .luma_padding_right(16)
        .luma_padding_top(4)
        .luma_padding_bottom(12)
        .build::<u8>()
        .unwrap();

    // Visible dimensions should remain unchanged
    assert_eq!(frame.y_plane.width().get(), 1920);
    assert_eq!(frame.y_plane.height().get(), 1080);
}