ff-decode 0.13.0

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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
//! Integration tests for [`ImageDecoder`] and [`ImageDecoderBuilder`].
//!
//! The real assets `assets/img/hello-triangle.png` and
//! `assets/img/hello-triangle.jpg` are used for happy-path tests.
//! If FFmpeg is unavailable or the file cannot be opened the tests skip
//! gracefully via `println!("Skipping: …"); return;`.

#![allow(clippy::unwrap_used)]

mod fixtures;

use std::path::PathBuf;

use ff_decode::{ImageDecoder, ImageDecoderBuilder};

fn png_path() -> PathBuf {
    fixtures::assets_dir().join("img/hello-triangle.png")
}

fn jpeg_path() -> PathBuf {
    fixtures::test_jpeg_path()
}

// ── crate-root exports ────────────────────────────────────────────────────────

/// `ImageDecoder` and `ImageDecoderBuilder` must be importable from the crate
/// root without any extra path segments.
#[test]
fn crate_root_should_export_image_decoder_types() {
    // If these types are not re-exported the test won't compile.
    let _: fn(&str) -> ImageDecoderBuilder = |p| ImageDecoder::open(p);
}

// ── open() / build() — error cases ───────────────────────────────────────────

#[test]
fn open_missing_file_should_return_file_not_found_error() {
    let path = PathBuf::from("/nonexistent/path/does_not_exist.png");
    let result = ImageDecoder::open(&path).build();
    assert!(
        matches!(result, Err(ff_decode::DecodeError::FileNotFound { .. })),
        "expected FileNotFound"
    );
}

#[test]
fn open_audio_only_file_should_return_error() {
    // An audio-only file has no video stream — open must not succeed silently.
    let path = fixtures::test_audio_path();
    if !path.exists() {
        println!("Skipping: audio asset not found");
        return;
    }
    let result = ImageDecoder::open(&path).build();
    assert!(
        result.is_err(),
        "opening an audio-only file should fail, got Ok"
    );
}

// ── open() / build() — happy path ────────────────────────────────────────────

#[test]
fn open_png_should_succeed() {
    match ImageDecoder::open(png_path()).build() {
        Ok(_) => {}
        Err(e) => {
            println!("Skipping: {e}");
        }
    }
}

#[test]
fn open_png_should_report_positive_width() {
    let decoder = match ImageDecoder::open(png_path()).build() {
        Ok(d) => d,
        Err(e) => {
            println!("Skipping: {e}");
            return;
        }
    };
    assert!(decoder.width() > 0, "width must be positive after open");
}

#[test]
fn open_png_should_report_positive_height() {
    let decoder = match ImageDecoder::open(png_path()).build() {
        Ok(d) => d,
        Err(e) => {
            println!("Skipping: {e}");
            return;
        }
    };
    assert!(decoder.height() > 0, "height must be positive after open");
}

#[test]
fn open_returns_builder_then_build_opens_file() {
    // Verify the two-step builder API: open() → builder, build() → decoder.
    let builder = ImageDecoder::open(png_path());
    match builder.build() {
        Ok(decoder) => assert!(decoder.width() > 0),
        Err(e) => println!("Skipping: {e}"),
    }
}

// ── decode() — consuming convenience API ──────────────────────────────────────

#[test]
fn decode_png_should_return_video_frame() {
    let decoder = match ImageDecoder::open(png_path()).build() {
        Ok(d) => d,
        Err(e) => {
            println!("Skipping: {e}");
            return;
        }
    };
    match decoder.decode() {
        Ok(_) => {}
        Err(e) => println!("Skipping: {e}"),
    }
}

#[test]
fn decode_png_frame_width_should_match_open_width() {
    let decoder = match ImageDecoder::open(png_path()).build() {
        Ok(d) => d,
        Err(e) => {
            println!("Skipping: {e}");
            return;
        }
    };
    let expected = decoder.width();
    let frame = match decoder.decode() {
        Ok(f) => f,
        Err(e) => {
            println!("Skipping: {e}");
            return;
        }
    };
    assert_eq!(
        frame.width(),
        expected,
        "frame width must match reported width"
    );
}

#[test]
fn decode_png_frame_height_should_match_open_height() {
    let decoder = match ImageDecoder::open(png_path()).build() {
        Ok(d) => d,
        Err(e) => {
            println!("Skipping: {e}");
            return;
        }
    };
    let expected = decoder.height();
    let frame = match decoder.decode() {
        Ok(f) => f,
        Err(e) => {
            println!("Skipping: {e}");
            return;
        }
    };
    assert_eq!(
        frame.height(),
        expected,
        "frame height must match reported height"
    );
}

#[test]
fn decode_png_frame_should_be_key_frame() {
    // Images are always key frames — decoder_inner hard-codes `true`.
    let decoder = match ImageDecoder::open(png_path()).build() {
        Ok(d) => d,
        Err(e) => {
            println!("Skipping: {e}");
            return;
        }
    };
    let frame = match decoder.decode() {
        Ok(f) => f,
        Err(e) => {
            println!("Skipping: {e}");
            return;
        }
    };
    assert!(
        frame.is_key_frame(),
        "decoded image must be marked as a key frame"
    );
}

#[test]
fn decode_png_frame_should_have_at_least_one_plane() {
    let decoder = match ImageDecoder::open(png_path()).build() {
        Ok(d) => d,
        Err(e) => {
            println!("Skipping: {e}");
            return;
        }
    };
    let frame = match decoder.decode() {
        Ok(f) => f,
        Err(e) => {
            println!("Skipping: {e}");
            return;
        }
    };
    assert!(
        frame.num_planes() > 0,
        "decoded frame must have at least one plane"
    );
}

#[test]
fn decode_png_frame_planes_should_be_non_empty() {
    let decoder = match ImageDecoder::open(png_path()).build() {
        Ok(d) => d,
        Err(e) => {
            println!("Skipping: {e}");
            return;
        }
    };
    let frame = match decoder.decode() {
        Ok(f) => f,
        Err(e) => {
            println!("Skipping: {e}");
            return;
        }
    };
    for (i, plane) in frame.planes().iter().enumerate() {
        assert!(!plane.is_empty(), "plane {i} must contain pixel data");
    }
}

#[test]
fn decode_png_frame_total_size_should_be_positive() {
    let decoder = match ImageDecoder::open(png_path()).build() {
        Ok(d) => d,
        Err(e) => {
            println!("Skipping: {e}");
            return;
        }
    };
    let frame = match decoder.decode() {
        Ok(f) => f,
        Err(e) => {
            println!("Skipping: {e}");
            return;
        }
    };
    assert!(
        frame.total_size() > 0,
        "decoded frame must have a positive total byte size"
    );
}

#[test]
fn decode_png_frame_pixel_format_should_be_supported() {
    use ff_format::PixelFormat;
    let supported = [
        PixelFormat::Yuv420p,
        PixelFormat::Yuv422p,
        PixelFormat::Yuv444p,
        PixelFormat::Rgb24,
        PixelFormat::Bgr24,
        PixelFormat::Rgba,
        PixelFormat::Bgra,
        PixelFormat::Gray8,
    ];
    let decoder = match ImageDecoder::open(png_path()).build() {
        Ok(d) => d,
        Err(e) => {
            println!("Skipping: {e}");
            return;
        }
    };
    let frame = match decoder.decode() {
        Ok(f) => f,
        Err(e) => {
            println!("Skipping: {e}");
            return;
        }
    };
    assert!(
        supported.contains(&frame.format()),
        "pixel format {:?} must be one of the supported formats",
        frame.format()
    );
}

// ── decode_one() — mutable incremental API ────────────────────────────────────

#[test]
fn decode_one_first_call_should_return_some_frame() {
    let mut decoder = match ImageDecoder::open(png_path()).build() {
        Ok(d) => d,
        Err(e) => {
            println!("Skipping: {e}");
            return;
        }
    };
    let result = decoder.decode_one();
    assert!(
        matches!(result, Ok(Some(_))),
        "first decode_one must return Ok(Some(frame)), got {result:?}"
    );
}

#[test]
fn decode_one_second_call_should_return_none() {
    let mut decoder = match ImageDecoder::open(png_path()).build() {
        Ok(d) => d,
        Err(e) => {
            println!("Skipping: {e}");
            return;
        }
    };
    let _ = decoder.decode_one();
    let result = decoder.decode_one();
    assert!(
        matches!(result, Ok(None)),
        "second decode_one must return Ok(None), got {result:?}"
    );
}

#[test]
fn decode_one_frame_dimensions_should_match_reported_dimensions() {
    let mut decoder = match ImageDecoder::open(png_path()).build() {
        Ok(d) => d,
        Err(e) => {
            println!("Skipping: {e}");
            return;
        }
    };
    let w = decoder.width();
    let h = decoder.height();
    let frame = match decoder.decode_one() {
        Ok(Some(f)) => f,
        Ok(None) => {
            println!("Skipping: no frame returned");
            return;
        }
        Err(e) => {
            println!("Skipping: {e}");
            return;
        }
    };
    assert_eq!(frame.width(), w);
    assert_eq!(frame.height(), h);
}

// ── frames() — iterator API ───────────────────────────────────────────────────

#[test]
fn frames_iterator_should_yield_exactly_one_frame() {
    let mut decoder = match ImageDecoder::open(png_path()).build() {
        Ok(d) => d,
        Err(e) => {
            println!("Skipping: {e}");
            return;
        }
    };
    let frames: Vec<_> = decoder.frames().collect();
    assert_eq!(
        frames.len(),
        1,
        "frames() must yield exactly one frame for a still image"
    );
    assert!(frames[0].is_ok(), "the single frame must be Ok");
}

#[test]
fn frames_iterator_second_call_should_yield_nothing() {
    let mut decoder = match ImageDecoder::open(png_path()).build() {
        Ok(d) => d,
        Err(e) => {
            println!("Skipping: {e}");
            return;
        }
    };
    let _ = decoder.frames().collect::<Vec<_>>();
    let second: Vec<_> = decoder.frames().collect();
    assert!(
        second.is_empty(),
        "frames() after image already decoded must yield nothing"
    );
}

#[test]
fn frames_iterator_frame_should_be_key_frame() {
    let mut decoder = match ImageDecoder::open(png_path()).build() {
        Ok(d) => d,
        Err(e) => {
            println!("Skipping: {e}");
            return;
        }
    };
    for result in decoder.frames() {
        let frame = match result {
            Ok(f) => f,
            Err(e) => {
                println!("Skipping: {e}");
                return;
            }
        };
        assert!(
            frame.is_key_frame(),
            "frame from iterator must be a key frame"
        );
    }
}

#[test]
fn frames_iterator_frame_should_have_correct_dimensions() {
    let mut decoder = match ImageDecoder::open(png_path()).build() {
        Ok(d) => d,
        Err(e) => {
            println!("Skipping: {e}");
            return;
        }
    };
    let w = decoder.width();
    let h = decoder.height();
    for result in decoder.frames() {
        let frame = match result {
            Ok(f) => f,
            Err(e) => {
                println!("Skipping: {e}");
                return;
            }
        };
        assert_eq!(frame.width(), w, "iterator frame width must match");
        assert_eq!(frame.height(), h, "iterator frame height must match");
    }
}

// ── Drop safety ───────────────────────────────────────────────────────────────

#[test]
fn decoder_drop_after_decode_should_not_panic() {
    // Drop must handle `inner == None` gracefully after decode_one consumes it.
    let mut decoder = match ImageDecoder::open(png_path()).build() {
        Ok(d) => d,
        Err(e) => {
            println!("Skipping: {e}");
            return;
        }
    };
    let _ = decoder.decode_one();
    // decoder is dropped here
}

#[test]
fn decoder_drop_without_decode_should_not_panic() {
    // Drop must free FFmpeg resources even when decode was never called.
    match ImageDecoder::open(png_path()).build() {
        Ok(_decoder) => {} // dropped here without decoding
        Err(e) => println!("Skipping: {e}"),
    }
}

// ── JPEG decoding ─────────────────────────────────────────────────────────────

#[test]
fn open_jpeg_should_succeed() {
    let path = jpeg_path();
    if !path.exists() {
        println!("Skipping: JPEG asset not found");
        return;
    }
    match ImageDecoder::open(&path).build() {
        Ok(_) => {}
        Err(e) => println!("Skipping: {e}"),
    }
}

#[test]
fn decode_jpeg_frame_should_have_positive_width_and_height() {
    let path = jpeg_path();
    if !path.exists() {
        println!("Skipping: JPEG asset not found");
        return;
    }
    let decoder = match ImageDecoder::open(&path).build() {
        Ok(d) => d,
        Err(e) => {
            println!("Skipping: {e}");
            return;
        }
    };
    assert!(decoder.width() > 0, "JPEG width must be positive");
    assert!(decoder.height() > 0, "JPEG height must be positive");
}

#[test]
fn decode_jpeg_frame_dimensions_should_match_reported_dimensions() {
    let path = jpeg_path();
    if !path.exists() {
        println!("Skipping: JPEG asset not found");
        return;
    }
    let decoder = match ImageDecoder::open(&path).build() {
        Ok(d) => d,
        Err(e) => {
            println!("Skipping: {e}");
            return;
        }
    };
    let expected_w = decoder.width();
    let expected_h = decoder.height();
    let frame = match decoder.decode() {
        Ok(f) => f,
        Err(e) => {
            println!("Skipping: {e}");
            return;
        }
    };
    assert_eq!(
        frame.width(),
        expected_w,
        "decoded JPEG frame width must match reported width"
    );
    assert_eq!(
        frame.height(),
        expected_h,
        "decoded JPEG frame height must match reported height"
    );
}

#[test]
fn decode_jpeg_frame_pixel_format_should_be_supported() {
    use ff_format::PixelFormat;
    let supported = [
        PixelFormat::Yuv420p,
        PixelFormat::Yuv422p,
        PixelFormat::Yuv444p,
        PixelFormat::Rgb24,
        PixelFormat::Bgr24,
        PixelFormat::Rgba,
        PixelFormat::Bgra,
        PixelFormat::Gray8,
    ];
    let path = jpeg_path();
    if !path.exists() {
        println!("Skipping: JPEG asset not found");
        return;
    }
    let decoder = match ImageDecoder::open(&path).build() {
        Ok(d) => d,
        Err(e) => {
            println!("Skipping: {e}");
            return;
        }
    };
    let frame = match decoder.decode() {
        Ok(f) => f,
        Err(e) => {
            println!("Skipping: {e}");
            return;
        }
    };
    assert!(
        supported.contains(&frame.format()),
        "JPEG pixel format {:?} must be one of the supported formats",
        frame.format()
    );
}