ff-decode 0.13.1

Video and audio decoding - the Rust way
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
//! Basic VideoDecoder tests covering decoder creation, stream info,
//! frame decoding, decoder state, thread configuration, and error handling.

use std::path::PathBuf;
use std::time::Duration;

mod fixtures;
use fixtures::*;

use ff_decode::{HardwareAccel, VideoDecoder};
use ff_format::PixelFormat;

// ============================================================================
// Basic Decoder Creation Tests
// ============================================================================

#[test]
fn test_decoder_opens_successfully() {
    let result = create_decoder();
    assert!(
        result.is_ok(),
        "Failed to open video file: {:?}",
        result.err()
    );
}

#[test]
fn test_decoder_nonexistent_file() {
    let path = assets_dir().join("nonexistent-file.mp4");
    let result = VideoDecoder::open(&path)
        .hardware_accel(HardwareAccel::None)
        .build();

    assert!(result.is_err(), "Opening nonexistent file should fail");
}

// ============================================================================
// Stream Information Tests
// ============================================================================

#[test]
fn test_decoder_stream_info() {
    let decoder = create_decoder().expect("Failed to create decoder");
    let info = decoder.stream_info();

    // Verify basic properties
    assert!(info.width() > 0, "Video width should be positive");
    assert!(info.height() > 0, "Video height should be positive");

    // Frame rate should be valid
    let fps = info.frame_rate();
    let fps_value = fps.num() as f64 / fps.den() as f64;
    assert!(
        fps_value > 0.0 && fps_value < 120.0,
        "Frame rate {} seems unusual",
        fps_value
    );
}

#[test]
fn test_decoder_stream_info_pixel_format() {
    let decoder = create_decoder().expect("Failed to create decoder");
    let info = decoder.stream_info();

    // Pixel format should be a known format
    let format = info.pixel_format();
    assert!(
        !matches!(format, PixelFormat::Other(_)),
        "Pixel format should be a known format, got {:?}",
        format
    );
}

#[test]
fn test_decoder_stream_info_codec() {
    let decoder = create_decoder().expect("Failed to create decoder");
    let info = decoder.stream_info();

    // Codec should be set
    let codec = info.codec();
    assert!(
        codec != ff_format::codec::VideoCodec::Unknown,
        "Video codec should be known"
    );
}

#[test]
fn test_decoder_stream_info_duration() {
    let decoder = create_decoder().expect("Failed to create decoder");
    let info = decoder.stream_info();

    // Duration should be present and reasonable
    if let Some(duration) = info.duration() {
        assert!(duration > Duration::ZERO, "Duration should be positive");
        assert!(
            duration < Duration::from_secs(3600),
            "Duration seems unreasonably long for a test file"
        );
    }
}

// ============================================================================
// Frame Decoding Tests
// ============================================================================

#[test]
fn test_decode_first_frame() {
    let mut decoder = create_decoder().expect("Failed to create decoder");

    // Decode first frame
    let result = decoder.decode_one();
    assert!(
        result.is_ok(),
        "Failed to decode first frame: {:?}",
        result.err()
    );

    let frame_opt = result.unwrap();
    assert!(frame_opt.is_some(), "First frame should be Some");

    let frame = frame_opt.unwrap();

    // Verify frame properties
    let info = decoder.stream_info();
    assert_eq!(
        frame.width(),
        info.width(),
        "Frame width should match stream info"
    );
    assert_eq!(
        frame.height(),
        info.height(),
        "Frame height should match stream info"
    );
}

#[test]
fn test_decode_multiple_frames() {
    let mut decoder = create_decoder().expect("Failed to create decoder");

    // Decode first 10 frames
    let mut frame_count = 0;
    for i in 0..10 {
        let result = decoder.decode_one();
        assert!(
            result.is_ok(),
            "Failed to decode frame {}: {:?}",
            i,
            result.err()
        );

        if let Some(frame) = result.unwrap() {
            frame_count += 1;

            // Verify frame is valid
            assert!(
                frame.width() > 0,
                "Frame {} width should be positive",
                frame_count
            );
            assert!(
                frame.height() > 0,
                "Frame {} height should be positive",
                frame_count
            );
            assert!(
                !frame.planes().is_empty(),
                "Frame {} should have planes",
                frame_count
            );
        } else {
            break;
        }
    }

    assert!(frame_count > 0, "Should decode at least one frame");
    assert_eq!(frame_count, 10, "Should decode 10 frames");
}

#[test]
fn test_decode_frames_have_data() {
    let mut decoder = create_decoder().expect("Failed to create decoder");

    // Decode first frame
    let frame = decoder
        .decode_one()
        .expect("Failed to decode")
        .expect("First frame should exist");

    // Verify planes have data
    let planes = frame.planes();
    assert!(!planes.is_empty(), "Frame should have at least one plane");

    for (i, plane) in planes.iter().enumerate() {
        assert!(!plane.is_empty(), "Plane {} should not be empty", i);
    }

    // Verify strides
    let strides = frame.strides();
    assert_eq!(
        strides.len(),
        planes.len(),
        "Strides count should match planes count"
    );

    for (i, &stride) in strides.iter().enumerate() {
        assert!(stride > 0, "Stride {} should be positive", i);
    }
}

#[test]
fn test_decode_frame_timestamps() {
    let mut decoder = create_decoder().expect("Failed to create decoder");

    let mut last_pts = None;

    // Decode first few frames and verify timestamps are increasing
    for i in 0..5 {
        let frame = decoder
            .decode_one()
            .expect("Failed to decode")
            .unwrap_or_else(|| panic!("Frame {} should exist", i));

        let timestamp = frame.timestamp();
        let pts = timestamp.pts();

        if let Some(last) = last_pts {
            assert!(
                pts > last,
                "Timestamp should increase: frame {} pts={}, last_pts={}",
                i,
                pts,
                last
            );
        }

        last_pts = Some(pts);
    }
}

// ============================================================================
// Decoder State Tests
// ============================================================================

#[test]
fn test_decoder_is_eof() {
    let mut decoder = create_decoder().expect("Failed to create decoder");

    // Initially should not be EOF
    assert!(!decoder.is_eof(), "Decoder should not be EOF initially");

    // Decode all frames until EOF
    let mut frame_count = 0;
    loop {
        match decoder.decode_one() {
            Ok(Some(_)) => {
                frame_count += 1;
                // Limit to prevent infinite loop
                if frame_count > 100000 {
                    panic!("Too many frames, possible infinite loop");
                }
            }
            Ok(None) => {
                // EOF reached
                break;
            }
            Err(e) => {
                panic!("Decode error: {:?}", e);
            }
        }
    }

    // Should be EOF now
    assert!(
        decoder.is_eof(),
        "Decoder should be EOF after all frames decoded"
    );

    // Further decode_one calls should return None
    let result = decoder
        .decode_one()
        .expect("decode_one should not error at EOF");
    assert!(result.is_none(), "decode_one should return None at EOF");
}

// ============================================================================
// Thread Configuration Tests
// ============================================================================

#[test]
fn test_decoder_with_thread_count() {
    let path = test_video_path();
    let result = VideoDecoder::open(&path)
        .thread_count(4)
        .hardware_accel(HardwareAccel::None)
        .build();

    assert!(
        result.is_ok(),
        "Failed to create decoder with thread_count: {:?}",
        result.err()
    );

    let mut decoder = result.unwrap();
    let frame = decoder
        .decode_one()
        .expect("Failed to decode")
        .expect("First frame should exist");

    assert!(frame.width() > 0, "Frame should be valid with thread_count");
}

#[test]
fn test_decoder_with_zero_threads_uses_auto() {
    let path = test_video_path();
    let result = VideoDecoder::open(&path)
        .thread_count(0) // 0 = auto
        .hardware_accel(HardwareAccel::None)
        .build();

    assert!(
        result.is_ok(),
        "Failed to create decoder with thread_count=0: {:?}",
        result.err()
    );

    let mut decoder = result.unwrap();
    let frame = decoder
        .decode_one()
        .expect("Failed to decode")
        .expect("First frame should exist");

    assert!(
        frame.width() > 0,
        "Frame should be valid with auto thread_count"
    );
}

// ============================================================================
// Error Handling Tests
// ============================================================================

#[test]
fn test_decoder_invalid_path() {
    let path = PathBuf::from("/invalid/path/video.mp4");
    let result = VideoDecoder::open(&path)
        .hardware_accel(HardwareAccel::None)
        .build();

    assert!(result.is_err(), "Should fail to open invalid path");
}

// ============================================================================
// Frame Properties Validation Tests
// ============================================================================

#[test]
fn test_frame_dimensions_match_stream_info() {
    let mut decoder = create_decoder().expect("Failed to create decoder");
    let info = decoder.stream_info();

    let expected_width = info.width();
    let expected_height = info.height();

    // Decode several frames and verify dimensions
    for i in 0..5 {
        let frame = decoder
            .decode_one()
            .expect("Failed to decode")
            .unwrap_or_else(|| panic!("Frame {} should exist", i));

        assert_eq!(frame.width(), expected_width, "Frame {} width mismatch", i);
        assert_eq!(
            frame.height(),
            expected_height,
            "Frame {} height mismatch",
            i
        );
    }
}

#[test]
fn test_frame_pixel_format_consistency() {
    let mut decoder = create_decoder().expect("Failed to create decoder");

    let first_frame = decoder
        .decode_one()
        .expect("Failed to decode")
        .expect("First frame should exist");

    let expected_format = first_frame.format();

    // Decode more frames and verify format is consistent
    for i in 1..5 {
        let frame = decoder
            .decode_one()
            .expect("Failed to decode")
            .unwrap_or_else(|| panic!("Frame {} should exist", i));

        assert_eq!(
            frame.format(),
            expected_format,
            "Frame {} pixel format mismatch",
            i
        );
    }
}

#[test]
fn test_frame_data_size_matches_dimensions() {
    let mut decoder = create_decoder().expect("Failed to create decoder");

    let frame = decoder
        .decode_one()
        .expect("Failed to decode")
        .expect("First frame should exist");

    let width = frame.width() as usize;
    let height = frame.height() as usize;

    // Calculate expected data size based on pixel format
    let expected_size = match frame.format() {
        PixelFormat::Rgba | PixelFormat::Bgra => width * height * 4,
        PixelFormat::Rgb24 | PixelFormat::Bgr24 => width * height * 3,
        PixelFormat::Yuv420p => width * height * 3 / 2,
        PixelFormat::Yuv422p => width * height * 2,
        PixelFormat::Yuv444p => width * height * 3,
        PixelFormat::Gray8 => width * height,
        PixelFormat::Nv12 | PixelFormat::Nv21 => width * height * 3 / 2,
        _ => return, // Skip for other formats
    };

    let total_size: usize = frame.planes().iter().map(|p| p.len()).sum();

    assert_eq!(
        total_size, expected_size,
        "Frame data size should match dimensions and format"
    );
}

#[test]
fn test_flush_decoder() {
    let mut decoder = create_decoder().expect("Failed to create decoder");

    // Decode a few frames
    for _ in 0..5 {
        let _ = decoder.decode_one().expect("Failed to decode");
    }

    // Flush the decoder
    decoder.flush();

    // Decoder should not be at EOF after flush
    assert!(!decoder.is_eof(), "Decoder should not be EOF after flush");

    // Should be able to decode frames after flush
    let frame = decoder
        .decode_one()
        .expect("Failed to decode after flush")
        .expect("Frame should exist after flush");

    assert!(frame.width() > 0, "Frame should be valid after flush");
}

#[test]
fn video_stream_info_codec_name_should_not_be_empty() {
    let decoder = create_decoder().expect("Failed to create decoder");
    let info = decoder.stream_info();

    assert!(
        !info.codec_name().is_empty(),
        "codec_name() should not be empty"
    );
}