threers-codec 0.1.0

Pure-Rust media codecs: H.264/HEVC/VP9 encoders, H.264/HEVC/VP9/AV1 decoders, AAC/MP3/Opus/Vorbis/FLAC/WAV decoders, AAC-LC and MP3 encoders, MP4/WebM/Matroska/Ogg, GIF/APNG, captions, and VideoToolbox acceleration on macOS
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
//! VAAPI on a machine that has a VA driver.
//!
//! The VAAPI decoder must reproduce the native decoder's planes exactly: the
//! native decoder supplies the reference lists and picture order the GPU
//! decodes with, and decoding is deterministic, so any difference is a bug in
//! how they were handed over. The streams come from x264 with the features
//! that exercise that hand-over: B-frames and their lists, CAVLC and CABAC,
//! 8×8 transforms with scaling matrices, weighted prediction, multiple
//! slices, long-term references and field coding.
//!
//! Skipped without a VA driver; set `THREERS_REQUIRE_VAAPI=1` to fail
//! instead, so a run on the GPU machine cannot pass by skipping.

/// The backend exists on Linux only; elsewhere this suite says so rather
/// than passing with no tests.
#[cfg(not(target_os = "linux"))]
#[test]
fn vaapi_is_linux_only() {
    eprintln!("SKIP: VAAPI is built on Linux only");
}

#[cfg(target_os = "linux")]
mod va {
    use std::process::Command;

    use threers_codec::decode::{Backend, DecodedFrame, Decoder, NativeDecoder, Picture};
    use threers_codec::mp4_demux::demux_video;
    use threers_codec::vaapi::{self, VaapiDecoder};

    fn gpu() -> bool {
        if vaapi::available() {
            return true;
        }
        assert!(
            std::env::var_os("THREERS_REQUIRE_VAAPI").is_none(),
            "THREERS_REQUIRE_VAAPI is set but no VA driver decodes H.264 here"
        );
        eprintln!("SKIP: no VA driver");
        false
    }

    /// An x264 stream of a lavfi pattern; `None` when ffmpeg cannot make it.
    fn x264(name: &str, src: &str, params: &str) -> Option<Vec<u8>> {
        encode(
            name,
            src,
            &[
                "-pix_fmt",
                "yuv420p",
                "-c:v",
                "libx264",
                "-x264-params",
                params,
            ],
        )
    }

    /// An x265 stream; `pix` picks 8- or 10-bit.
    fn x265(name: &str, src: &str, pix: &str, params: &str) -> Option<Vec<u8>> {
        let params = format!("log-level=error:{params}");
        encode(
            name,
            src,
            &[
                "-pix_fmt",
                pix,
                "-c:v",
                "libx265",
                "-tag:v",
                "hvc1",
                "-x265-params",
                &params,
            ],
        )
    }

    /// A libvpx VP9 stream in WebM.
    fn vpx(name: &str, src: &str, args: &[&str]) -> Option<Vec<u8>> {
        let mut all = vec!["-c:v", "libvpx-vp9"];
        all.extend_from_slice(args);
        encode(&format!("{name}.webm"), src, &all)
    }

    fn encode(name: &str, src: &str, args: &[&str]) -> Option<Vec<u8>> {
        let file = if name.contains('.') {
            name.to_string()
        } else {
            format!("{name}.mp4")
        };
        let out = std::env::temp_dir().join(format!("threers_va_{}_{file}", std::process::id()));
        let ok = Command::new("ffmpeg")
            .args([
                "-v",
                "error",
                "-y",
                "-f",
                "lavfi",
                "-i",
                src,
                "-frames:v",
                "30",
            ])
            .args(args)
            .arg(&out)
            .status()
            .map(|s| s.success())
            .unwrap_or(false);
        let data = ok.then(|| std::fs::read(&out).ok()).flatten();
        let _ = std::fs::remove_file(&out);
        if data.is_none() {
            eprintln!("SKIP {name}: ffmpeg/libx264 could not make it");
        }
        data
    }

    fn same(a: &DecodedFrame, b: &DecodedFrame) -> bool {
        (a.width, a.height, a.bit_depth) == (b.width, b.height, b.bit_depth)
            && a.y == b.y
            && a.u == b.u
            && a.v == b.v
    }

    /// The video track of a WebM and its frames' bytes, as an MP4 track
    /// would describe them.
    fn webm(data: &[u8]) -> (threers_codec::mp4_demux::VideoTrack, Vec<Vec<u8>>) {
        use threers_codec::mkv_demux::{Mkv, TrackKind};
        use threers_codec::mp4_demux::{Sample, TrackCodec, VideoTrack};
        let mkv = Mkv::parse(data).unwrap();
        let t = mkv
            .tracks
            .iter()
            .find(|t| t.kind == TrackKind::Video)
            .unwrap();
        let frames: Vec<_> = mkv.frames.iter().filter(|f| f.track == t.number).collect();
        let samples = frames
            .iter()
            .enumerate()
            .map(|(i, f)| Sample {
                offset: 0,
                size: f.data.len() as u32,
                duration: 1,
                dts: i as i64,
                pts: f.pts,
                sync: f.keyframe,
            })
            .collect();
        let codec = if t.codec_id == "V_VP9" {
            TrackCodec::Vp9
        } else {
            TrackCodec::Av1
        };
        let track = VideoTrack {
            codec,
            width: t.width,
            height: t.height,
            config: t.codec_private.clone(),
            timescale: 1000,
            samples,
        };
        (track, frames.iter().map(|f| f.data.clone()).collect())
    }

    /// The picture as the hardware left it: NV12 or P010, never planes the
    /// CPU converted; except AV1 film grain (a stream named `…_grain`),
    /// which the CPU adds, so planes. Then as planes, to compare.
    fn packed(name: &str, picture: Option<Picture>) -> Option<DecodedFrame> {
        picture.map(|p| {
            let grain = name.ends_with("_grain");
            assert_eq!(
                matches!(p, Picture::Packed(_)),
                !grain,
                "{name}: VAAPI handed back {}",
                if grain {
                    "a picture without its film grain"
                } else {
                    "planes, not the surface's NV12/P010"
                }
            );
            p.into_decoded()
        })
    }

    /// [`compare`] for a WebM.
    fn compare_webm(name: &str, data: &[u8]) {
        let (track, frames) = webm(data);
        let mut hw = VaapiDecoder::new(&track)
            .unwrap_or_else(|e| panic!("{name}: VAAPI would not open: {e}"));
        let mut sw = NativeDecoder::new(&track).unwrap();
        for (s, bytes) in track.samples.iter().zip(&frames) {
            hw.push(bytes, s.pts, s.dts, 1)
                .unwrap_or_else(|e| panic!("{name}: VAAPI: {e}"));
            sw.push(bytes, s.pts, s.dts, 1).unwrap();
        }
        for s in &track.samples {
            let (a, b) = (packed(name, hw.take_picture(s.pts)), sw.take_frame(s.pts));
            match (a, b) {
                (Some(a), Some(b)) => assert!(
                    same(&a, &b),
                    "{name}: picture at pts {} differs ({}-bit)",
                    s.pts,
                    a.bit_depth
                ),
                (None, None) => {}
                (a, b) => panic!(
                    "{name}: at pts {} VAAPI has {} and native {}",
                    s.pts,
                    a.is_some(),
                    b.is_some()
                ),
            }
        }
        eprintln!(
            "{name}: {} frames identical to the native decoder",
            frames.len()
        );
    }

    /// Decode `data` with VAAPI and natively; every picture must match.
    fn compare(name: &str, data: &[u8]) {
        let track = demux_video(data).unwrap();
        let mut hw = VaapiDecoder::new(&track)
            .unwrap_or_else(|e| panic!("{name}: VAAPI would not open: {e}"));
        let mut sw = NativeDecoder::new(&track).unwrap();
        for s in &track.samples {
            let bytes = &data[s.offset as usize..][..s.size as usize];
            hw.push(bytes, s.pts, s.dts, s.duration as i64)
                .unwrap_or_else(|e| panic!("{name}: VAAPI: {e}"));
            sw.push(bytes, s.pts, s.dts, s.duration as i64).unwrap();
        }
        for i in track.presentation_order() {
            let pts = track.samples[i].pts;
            let a = packed(name, hw.take_picture(pts))
                .unwrap_or_else(|| panic!("{name}: VAAPI has no picture at {pts}"));
            let b = sw.take_frame(pts).unwrap();
            assert!(
                same(&a, &b),
                "{name}: picture at pts {pts} differs ({}-bit)",
                a.bit_depth
            );
        }
        eprintln!(
            "{name}: {} pictures identical to the native decoder",
            track.samples.len()
        );
    }

    #[test]
    fn vaapi_h264_matches_the_native_decoder() {
        if !gpu() {
            return;
        }
        const SRC: &str = "testsrc2=size=640x360:rate=25";
        const MOVING: &str = "testsrc2=size=640x360:rate=25,rotate=a=t/2:c=black";
        let cases = [
            ("baseline", SRC, "profile=baseline:cabac=0:bframes=0"),
            ("main_b", SRC, "bframes=3:b-pyramid=normal"),
            ("high_8x8_cqm", MOVING, "bframes=2:8x8dct=1:cqm=jvt:ref=4"),
            (
                "weighted",
                "testsrc2=size=640x360:rate=25,fade=in:0:30",
                "bframes=2:weightp=2:weightb=1",
            ),
            ("slices", MOVING, "slices=4:bframes=2"),
            ("cavlc_b", MOVING, "cabac=0:bframes=2"),
            ("interlaced", MOVING, "interlaced=1:bframes=2"),
        ];
        let mut ran = 0;
        for (name, src, params) in cases {
            let Some(data) = x264(name, src, params) else {
                continue;
            };
            compare(name, &data);
            ran += 1;
        }
        assert!(ran > 0, "no stream could be made");
    }

    #[test]
    fn vaapi_hevc_matches_the_native_decoder() {
        if !gpu() {
            return;
        }
        const SRC: &str = "testsrc2=size=640x360:rate=25";
        const MOVING: &str = "testsrc2=size=640x360:rate=25,rotate=a=t/2:c=black";
        let cases = [
            ("hevc_b", SRC, "yuv420p", "bframes=3:b-pyramid=1"),
            ("hevc_moving", MOVING, "yuv420p", "bframes=4:ref=4"),
            ("hevc10", MOVING, "yuv420p10le", "bframes=2"),
            ("hevc_wpp", MOVING, "yuv420p", "wpp=1:bframes=2"),
            ("hevc_tiles", MOVING, "yuv420p", "wpp=0:bframes=2:tiles=2x2"),
            ("hevc_slices", MOVING, "yuv420p", "slices=3:bframes=2"),
            (
                "hevc_scaling",
                MOVING,
                "yuv420p",
                "scaling-list=default:bframes=2",
            ),
            (
                "hevc_weighted",
                "testsrc2=size=640x360:rate=25,fade=in:0:30",
                "yuv420p",
                "weightp=1:weightb=1:bframes=2",
            ),
            (
                "hevc_odd_size",
                "testsrc2=size=636x358:rate=25",
                "yuv420p",
                "bframes=2",
            ),
        ];
        let mut ran = 0;
        for (name, src, pix, params) in cases {
            let Some(data) = x265(name, src, pix, params) else {
                continue;
            };
            compare(name, &data);
            ran += 1;
        }
        assert!(ran > 0, "no stream could be made");
    }

    #[test]
    fn vaapi_vp9_matches_the_native_decoder() {
        if !gpu() {
            return;
        }
        if VaapiDecoder::new(
            &webm(
                &match vpx("probe", "testsrc2=size=320x240:rate=25", &["-b:v", "300k"]) {
                    Some(d) => d,
                    None => return,
                },
            )
            .0,
        )
        .is_err()
        {
            // A driver need not do every codec (Intel's free iHD build has no AV1).
            eprintln!("SKIP: the VA driver here does not decode VP9");
            return;
        }
        const MOVING: &str = "testsrc2=size=640x360:rate=25,rotate=a=t/2:c=black";
        let cases: [(&str, &[&str]); 6] = [
            // Alt-ref frames: hidden frames packed into superframes.
            (
                "vp9_altref",
                &["-b:v", "800k", "-auto-alt-ref", "1", "-lag-in-frames", "16"],
            ),
            (
                "vp9_tiles",
                &["-b:v", "800k", "-tile-columns", "2", "-threads", "4"],
            ),
            (
                "vp9_error_resilient",
                &["-b:v", "600k", "-error-resilient", "1"],
            ),
            ("vp9_segments", &["-b:v", "600k", "-aq-mode", "3"]),
            (
                "vp9_10bit",
                &["-b:v", "800k", "-pix_fmt", "yuv420p10le", "-profile:v", "2"],
            ),
            ("vp9_lossless", &["-lossless", "1"]),
        ];
        let mut ran = 0;
        for (name, args) in cases {
            let Some(data) = vpx(name, MOVING, args) else {
                continue;
            };
            compare_webm(name, &data);
            ran += 1;
        }
        assert!(ran > 0, "no stream could be made");
    }

    #[test]
    fn vaapi_av1_matches_the_native_decoder() {
        if !gpu() {
            return;
        }
        let probe = |args: &[&str]| encode("av1_probe.webm", "testsrc2=size=320x240:rate=25", args);
        let Some(p) = probe(&["-c:v", "libsvtav1", "-preset", "10"]) else {
            return;
        };
        if VaapiDecoder::new(&webm(&p).0).is_err() {
            // A driver need not do every codec (Intel's free iHD build has no AV1).
            eprintln!("SKIP: the VA driver here does not decode AV1");
            return;
        }
        const MOVING: &str = "testsrc2=size=640x360:rate=25,rotate=a=t/2:c=black";
        let svt = |extra: &'static str| -> Vec<&'static str> {
            let mut v = vec!["-c:v", "libsvtav1", "-preset", "8"];
            if !extra.is_empty() {
                v.extend(["-svtav1-params", extra]);
            }
            v
        };
        let cases: Vec<(&str, &str, Vec<&str>)> = vec![
            ("av1_svt", MOVING, svt("")),
            (
                "av1_svt_10bit",
                MOVING,
                [svt(""), vec!["-pix_fmt", "yuv420p10le"]].concat(),
            ),
            ("av1_svt_grain", MOVING, svt("film-grain=8")),
            ("av1_svt_tiles", MOVING, svt("tile-columns=2:tile-rows=1")),
            (
                "av1_svt_screen",
                "testsrc=size=640x360:rate=25",
                svt("scm=1"),
            ),
            (
                "av1_aom",
                MOVING,
                vec![
                    "-c:v",
                    "libaom-av1",
                    "-cpu-used",
                    "8",
                    "-b:v",
                    "600k",
                    "-lag-in-frames",
                    "10",
                ],
            ),
        ];
        let mut ran = 0;
        for (name, src, args) in cases {
            let Some(data) = encode(&format!("{name}.webm"), src, &args) else {
                continue;
            };
            compare_webm(name, &data);
            ran += 1;
        }
        assert!(ran > 0, "no stream could be made");
        // The conformance vectors, when they are here.
        let dir =
            std::env::var("THREERS_AV1_CONFORMANCE").unwrap_or_else(|_| "/var/tmp/av1conf".into());
        let Ok(rd) = std::fs::read_dir(&dir) else {
            return;
        };
        let mut paths: Vec<_> = rd
            .flatten()
            .map(|e| e.path())
            .filter(|p| p.extension().is_some_and(|x| x == "ivf"))
            .collect();
        paths.sort();
        for p in paths {
            let data = std::fs::read(&p).unwrap();
            let (_, _, _, _, packets) = threers_codec::mkv_demux::parse_ivf(&data).unwrap();
            let name = p.file_name().unwrap().to_string_lossy().into_owned();
            use threers_codec::mp4_demux::{Sample, TrackCodec, VideoTrack};
            let first = threers_codec::decode::av1::Av1Decoder::new()
                .decode(&packets[0].1)
                .ok()
                .and_then(|f| f.into_iter().next());
            let (w, h) = first.map_or((0, 0), |f| (f.pic.w as u32, f.pic.h as u32));
            let samples = packets
                .iter()
                .enumerate()
                .map(|(i, _)| Sample {
                    offset: 0,
                    size: 0,
                    duration: 1,
                    dts: i as i64,
                    pts: i as i64,
                    sync: i == 0,
                })
                .collect();
            let track = VideoTrack {
                codec: TrackCodec::Av1,
                width: w,
                height: h,
                config: Vec::new(),
                timescale: 1000,
                samples,
            };
            let mut hw = match VaapiDecoder::new(&track) {
                Ok(d) => d,
                Err(e) => {
                    eprintln!("{name}: VAAPI refuses it ({e})");
                    continue;
                }
            };
            let mut sw = NativeDecoder::new(&track).unwrap();
            let mut n = 0;
            for (i, (_, pkt)) in packets.iter().enumerate() {
                if let Err(e) = hw.push(pkt, i as i64, i as i64, 1) {
                    eprintln!("{name}: VAAPI refuses frame {i} ({e})");
                    break;
                }
                sw.push(pkt, i as i64, i as i64, 1).unwrap();
                if let (Some(a), Some(b)) = (hw.take_frame(i as i64), sw.take_frame(i as i64)) {
                    assert!(same(&a, &b), "{name}: frame {i} differs");
                    n += 1;
                }
            }
            eprintln!("{name}: {n} frames identical to the native decoder");
        }
    }

    #[test]
    fn vaapi_encoder_writes_mp4s_the_native_decoder_reads() {
        if !gpu() {
            return;
        }
        use threers_codec::hw::{EncodeBackend, EncodeOptions, Mp4Encoder};
        if !EncodeBackend::Vaapi.available() {
            // A driver need not do every codec (Intel's free iHD build has no AV1).
            eprintln!("SKIP: the VA driver here does not encode HEVC");
            return;
        }
        // Sizes that are not whole 64×64 CTBs, so the driver must crop.
        for (w, h, options) in [
            (
                640u32,
                360u32,
                EncodeOptions {
                    fps: 30,
                    quality: 0.8,
                    ..Default::default()
                },
            ),
            (
                1920,
                1080,
                EncodeOptions {
                    fps: 30,
                    bitrate: Some(4_000_000),
                    ..Default::default()
                },
            ),
            (
                318,
                238,
                EncodeOptions {
                    fps: 25,
                    quality: 0.7,
                    ..Default::default()
                },
            ),
        ] {
            let frame = |i: u32| -> Vec<u8> {
                (0..w * h)
                    .flat_map(|p| {
                        let (x, y) = (p % w, p / w);
                        [
                            ((x + i * 3) % 256) as u8,
                            (y * 255 / h) as u8,
                            ((x / 4 + y / 4) % 256) as u8,
                            255,
                        ]
                    })
                    .collect()
            };
            let mut enc = Mp4Encoder::with_backend(w, h, options, EncodeBackend::Vaapi).unwrap();
            for i in 0..40 {
                enc.push_rgba(&frame(i)).unwrap();
            }
            let mp4 = enc.finish().unwrap();
            let track = demux_video(&mp4).unwrap();
            assert_eq!((track.width, track.height, track.samples.len()), (w, h, 40));
            let mut dec = NativeDecoder::new(&track).unwrap();
            let mut worst = f64::INFINITY;
            for (i, s) in track.samples.iter().enumerate() {
                dec.push(
                    &mp4[s.offset as usize..][..s.size as usize],
                    s.pts,
                    s.dts,
                    s.duration as i64,
                )
                .unwrap();
                let bgra = dec.take(s.pts).unwrap();
                let src = frame(i as u32);
                let se: f64 = src
                    .as_chunks::<4>()
                    .0
                    .iter()
                    .zip(bgra.as_chunks::<4>().0)
                    .map(|(a, b)| {
                        [(a[0], b[2]), (a[1], b[1]), (a[2], b[0])]
                            .iter()
                            .map(|&(x, y)| (x as f64 - y as f64).powi(2))
                            .sum::<f64>()
                    })
                    .sum();
                worst = worst
                    .min(10.0 * (255f64.powi(2) / (se / (w * h * 3) as f64).max(1e-9)).log10());
            }
            eprintln!(
                "VAAPI {w}x{h} {options:?}: {} bytes, worst PSNR {worst:.1} dB",
                mp4.len()
            );
            assert!(worst > 30.0, "{w}x{h}: {worst:.1} dB");
            let path =
                std::env::temp_dir().join(format!("threers_va_enc_{}_{w}.mp4", std::process::id()));
            std::fs::write(&path, &mp4).unwrap();
            if let Ok(out) = Command::new("ffprobe")
                .args([
                    "-v",
                    "error",
                    "-count_frames",
                    "-select_streams",
                    "v:0",
                    "-show_entries",
                    "stream=codec_name,width,height,nb_read_frames",
                    "-of",
                    "csv=p=0",
                ])
                .arg(&path)
                .output()
            {
                let s = String::from_utf8_lossy(&out.stdout);
                assert_eq!(s.trim(), format!("hevc,{w},{h},40"), "ffprobe");
            }
            let _ = std::fs::remove_file(&path);
        }
    }

    #[test]
    fn decoder_prefers_vaapi_here() {
        if !gpu() {
            return;
        }
        let Some(data) = x264("pref", "testsrc2=size=320x240:rate=25", "bframes=0") else {
            return;
        };
        let track = demux_video(&data).unwrap();
        let d = Decoder::new(&track).unwrap();
        // NVDEC outranks VAAPI where both exist.
        let expect = if Backend::Nvdec.available() {
            Backend::Nvdec
        } else {
            Backend::Vaapi
        };
        assert_eq!(d.backend(), expect);
    }
}