#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::print_stderr,
reason = "test modules may unwrap / print"
)]
use super::*;
use mediaway_common::{CodecKind, Rational};
fn tiny_hevc_cfg(width: u32, height: u32) -> VideoEncoderConfig {
tiny_hevc_gop_cfg(width, height, 1)
}
fn tiny_hevc_gop_cfg(width: u32, height: u32, gop_size: u32) -> VideoEncoderConfig {
VideoEncoderConfig {
codec: CodecKind::Hevc,
width,
height,
time_base: Rational::new(1, 30),
bitrate_bps: 500_000,
pixel_format: PixelFormat::Nv12,
color_range: mediaway_common::ColorRange::Video,
input: VideoInputPreference::CpuUploadOk,
gpu_device: None,
gop_size,
rate_control: None,
intra_refresh_period: None,
}
}
#[test]
fn validate_accepts_cb_aligned_hevc_cpu_upload_config() {
assert!(validate(&tiny_hevc_cfg(64, 64)).is_ok());
}
#[test]
fn validate_rejects_non_hevc_codec() {
let mut cfg = tiny_hevc_cfg(64, 64);
cfg.codec = CodecKind::H264;
assert_eq!(validate(&cfg), Err(EncodeError::Unsupported));
}
#[test]
fn validate_rejects_zero_dimensions() {
let cfg = tiny_hevc_cfg(0, 64);
assert_eq!(validate(&cfg), Err(EncodeError::InvalidInput));
}
#[test]
fn validate_rejects_non_8_pixel_aligned_dimensions() {
let cfg = tiny_hevc_cfg(65, 64);
assert_eq!(validate(&cfg), Err(EncodeError::Unsupported));
}
#[test]
fn validate_accepts_8_pixel_aligned_non_16_aligned_dimensions() {
let cfg = tiny_hevc_cfg(72, 56);
assert!(validate(&cfg).is_ok());
}
#[test]
fn validate_rejects_non_nv12_pixel_format() {
let mut cfg = tiny_hevc_cfg(64, 64);
cfg.pixel_format = PixelFormat::Bgra8;
assert_eq!(validate(&cfg), Err(EncodeError::Unsupported));
}
#[test]
fn validate_rejects_zero_timebase_denominator() {
let mut cfg = tiny_hevc_cfg(64, 64);
cfg.time_base = Rational::new(1, 0);
assert_eq!(validate(&cfg), Err(EncodeError::InvalidInput));
}
#[test]
fn validate_rejects_zero_gop_size() {
let cfg = tiny_hevc_gop_cfg(64, 64, 0);
assert_eq!(validate(&cfg), Err(EncodeError::InvalidInput));
}
#[test]
fn validate_accepts_gop_size_greater_than_one() {
let cfg = tiny_hevc_gop_cfg(64, 64, 3);
assert!(validate(&cfg).is_ok());
}
#[test]
fn ctu_count_rounds_up_to_ctu_grid() {
assert_eq!(ctu_count(64, 64), 4);
assert_eq!(ctu_count(72, 56), 6);
}
#[test]
fn stream_info_from_config_carries_geometry_and_timebase() {
let cfg = tiny_hevc_cfg(64, 32);
let info = stream_info_from(&cfg);
assert!(matches!(
info,
StreamInfo::Video {
codec: CodecKind::Hevc,
..
}
));
if let StreamInfo::Video {
time_base,
geometry,
..
} = info
{
assert_eq!(time_base, cfg.time_base);
assert_eq!(geometry.width, 64);
assert_eq!(geometry.height, 32);
}
}
fn black_nv12_frame(pts: i64, width: u32, height: u32) -> VideoFrame {
let nv12_len = (width * height + (width * height) / 2) as usize;
VideoFrame {
pts,
duration: 1,
width,
height,
format: PixelFormat::Nv12,
storage: VideoFrameStorage::Cpu {
data: Bytes::from(vec![0u8; nv12_len]),
},
}
}
#[test]
fn vaapi_open_and_encode_or_skip_without_hw() {
let cfg = tiny_hevc_cfg(64, 64);
let mut enc = match VaapiHevcVideoEncoder::open(&cfg) {
Ok(e) => e,
Err(e) => {
eprintln!("skip: VaapiHevcVideoEncoder::open failed ({e:?}) — no VA-API display?");
return;
}
};
let frame = black_nv12_frame(0, 64, 64);
if let Err(e) = enc.push_frame(&frame) {
eprintln!("skip: push_frame failed ({e:?}) — no usable VA-API encode session?");
return;
}
let _ = enc.flush();
let mut packets = 0usize;
while let Ok(Some(p)) = enc.poll_packet() {
assert!(!p.payload.is_empty());
assert!(p.is_keyframe);
packets += 1;
}
eprintln!("vaapi hevc cpu-upload packets={packets}");
}
#[test]
fn vaapi_gop_cadence_or_skip_without_hw() {
const GOP_SIZE: u32 = 3;
let cfg = tiny_hevc_gop_cfg(64, 64, GOP_SIZE);
let mut enc = match VaapiHevcVideoEncoder::open(&cfg) {
Ok(e) => e,
Err(e) => {
eprintln!("skip: VaapiHevcVideoEncoder::open failed ({e:?}) — no VA-API display?");
return;
}
};
let mut is_keyframe = Vec::new();
for pts in 0..7i64 {
let frame = black_nv12_frame(pts, 64, 64);
if let Err(e) = enc.push_frame(&frame) {
eprintln!("skip: push_frame failed ({e:?}) — no usable VA-API encode session?");
return;
}
match enc.poll_packet() {
Ok(Some(p)) => is_keyframe.push(p.is_keyframe),
Ok(None) => eprintln!("skip: no packet produced for pts={pts}"),
Err(e) => {
eprintln!("skip: poll_packet failed ({e:?})");
return;
}
}
}
let _ = enc.flush();
if is_keyframe.iter().all(|&kf| kf) {
eprintln!(
"vaapi hevc gop cadence: driver may report VAConfigAttribEncMaxRefFrames unsupported \
for VAProfileHEVCMain, degrading to all-IDR — skipping cadence assertion"
);
return;
}
assert_eq!(
is_keyframe,
vec![true, false, false, true, false, false, true],
"expected I P P I P P I cadence for gop_size={GOP_SIZE}"
);
}