use ec_av1::encode::Picture; use ec_av1::encoder::{Av1Encoder, Colour, EncoderConfig as Av1Config, Pyramid, RateTarget}; use ec_core::color::ContentLight; use ec_core::frame::{PixelFormat, Plane, VideoFrame}; use ec_h264::{Encoder as H264Encoder, EncoderConfig as H264Config, PictureView, Preset as H264Preset}; use ec_h265::encoder::{Encoder as H265Encoder, EncoderConfig as H265Config, Preset as H265Preset}; use ec_h265_syntax::sei::MasteringDisplay; use ec_h265_syntax::vui::{ColourDescription, VideoSignalType}; use ec_prores::{AlphaPlane, EncoderConfig as ProresConfig, ProResEncoder, Profile}; use crate::{error, guard, hash}; const WIDTH: usize = 176; const HEIGHT: usize = 144; const FRAMES: usize = 6; const AV1_FRAMES: usize = 8; const AV1_MODES: [Av1Mode; 4] = [Av1Mode::KeyOnly, Av1Mode::Flat, Av1Mode::Pyramid3, Av1Mode::Pyramid4]; const AV1_RATES: [RateTarget; 2] = [ RateTarget::Quality(40), RateTarget::Bitrate { bits_per_second: 300_000, frames_per_second: 25.0, }, ]; const AV1_TILES: [(u32, u32); 2] = [(0, 0), (1, 1)]; const AV1_SIZES: [(usize, usize); 2] = [(176, 144), (151, 91)]; #[derive(Clone, Copy, Debug)] enum Av1Mode { KeyOnly, Flat, Pyramid3, Pyramid4, } #[derive(Clone, Copy, Debug)] struct Av1Case { mode: Av1Mode, rate: RateTarget, tiles: (u32, u32), screen: bool, w: usize, h: usize, speed: u8, } impl Av1Case { fn name(&self) -> String { let content = if self.screen { "screen" } else { "natural" }; format!( "{:?}-{:?}-tiles{}x{}-{content}-{}x{}-s{}", self.mode, self.rate, self.tiles.0, self.tiles.1, self.w, self.h, self.speed ) } } fn case_name(codec: &str, config: &str, index: usize) -> String { format!("encode_video/{codec}/{config}/{index}") } fn outcome(case: &str, result: Result, String>) { match result { Ok(bytes) => hash(case, &bytes), Err(err) => error(case, err), } } fn clamp8(v: i32) -> u8 { v.clamp(0, 255) as u8 } fn natural_frame(w: usize, h: usize, t: usize) -> Vec { let mut seed = ((w * 1_000_003 + h * 1009 + t * 7919) as u64) | 1; let mut noise = || { seed = seed .wrapping_mul(6364136223846793005) .wrapping_add(1442695040888963407); ((seed >> 33) % 9) as i32 - 4 }; let sq = w / 4; let x0 = (4 * t) % (w - sq); let y0 = (2 * t) % (h - sq); let mut out = Vec::with_capacity(w * h * 3 / 2); for y in 0..h { for x in 0..w { let inside = (x0..x0 + sq).contains(&x) && (y0..y0 + sq).contains(&y); let base: i32 = if inside { 230 } else { (x * 200 / w + y * 40 / h) as i32 }; out.push(clamp8(base + noise())); } } let (cw, ch) = (w.div_ceil(2), h.div_ceil(2)); for _ in 0..ch { for x in 0..cw { out.push(clamp8(64 + (x * 128 / cw) as i32 + noise())); } } for y in 0..ch { for _ in 0..cw { out.push(clamp8(64 + (y * 128 / ch) as i32 + noise())); } } out } fn screen_frame(w: usize, h: usize, t: usize) -> Vec { let mut out = Vec::with_capacity(w * h * 3 / 2); for y in 0..h { for x in 0..w { let v = if (y + t).is_multiple_of(6) { 16 } else if ((x + 2 * t) / 8 + y / 8).is_multiple_of(3) { 235 } else { 81 }; out.push(v); } } let chroma = w.div_ceil(2) * h.div_ceil(2) * 2; out.resize(w * h + chroma, 128); out } fn split(buf: &[u8], w: usize, h: usize) -> (&[u8], &[u8], &[u8]) { let (ys, cs) = (w * h, w.div_ceil(2) * h.div_ceil(2)); (&buf[..ys], &buf[ys..ys + cs], &buf[ys + cs..ys + 2 * cs]) } fn resample(src: &[u8], sw: usize, sh: usize, dw: usize, dh: usize) -> Vec { let mut out = Vec::with_capacity(dw * dh); for y in 0..dh { for x in 0..dw { out.push(src[(y * sh / dh) * sw + x * sw / dw]); } } out } fn to_bytes(samples: &[u8], bits: u32) -> Vec { if bits == 8 { return samples.to_vec(); } samples .iter() .flat_map(|&s| (u16::from(s) << (bits - 8)).to_le_bytes()) .collect() } fn frame_in(format: PixelFormat, w: usize, h: usize, buf: &[u8]) -> Result { let (y, u, v) = split(buf, w, h); let (cw, ch) = (w.div_ceil(2), h.div_ceil(2)); let src = [(y, w, h), (u, cw, ch), (v, cw, ch)]; let bits = format.bits_per_component(); let bytes = format.bytes_per_component(); let mut planes = Vec::new(); for ((s, sw, sh), (row, rows)) in src.iter().zip(format.plane_geometry(w as u32, h as u32)) { let data = to_bytes(&resample(s, *sw, *sh, row / bytes, rows), bits); planes.push(Plane::new(data, row)); } VideoFrame::try_new(format, w as u32, h as u32, planes).map_err(|e| e.to_string()) } fn alpha_plane(w: usize, h: usize, t: usize) -> AlphaPlane { AlphaPlane { width: w as u32, height: h as u32, stride: w, data: (0..w * h).map(|i| ((i * 257 + t * 4099) % 65536) as u16).collect(), } } fn h264_config( preset: H264Preset, cabac: bool, t8: bool, qp: i32, bitrate: u32, threads: usize, ) -> H264Config { let mut cfg = H264Config::new(WIDTH as u32, HEIGHT as u32); cfg.preset = preset; cfg.cabac = cabac; cfg.transform_8x8 = t8; cfg.qp = qp; cfg.bitrate = bitrate; cfg.threads = threads; cfg.gop_size = 4; cfg } fn h264_stream(cfg: H264Config) -> Result, String> { let mut enc = H264Encoder::new(cfg).map_err(|e| e.to_string())?; let mut out = Vec::new(); for t in 0..FRAMES { let buf = natural_frame(WIDTH, HEIGHT, t); let (y, u, v) = split(&buf, WIDTH, HEIGHT); let view = PictureView::i420(WIDTH as u32, HEIGHT as u32, y, u, v); let pic = enc.encode(&view).map_err(|e| e.to_string())?; out.extend_from_slice(&pic.au); } Ok(out) } fn h264() { let mut index = 0; for preset in [H264Preset::Fast, H264Preset::Balanced] { for cabac in [true, false] { for t8 in [true, false] { for (qp, bitrate) in [(20, 0), (36, 0), (26, 400_000)] { for threads in [1, 2, 4] { index += 1; let config = format!( "{preset:?}-cabac{cabac}-t8{t8}-qp{qp}-br{bitrate}-th{threads}" ); let case = case_name("h264", &config, index); guard(&case, || { let cfg = h264_config(preset, cabac, t8, qp, bitrate, threads); outcome(&case, h264_stream(cfg)); }); } } } } } } fn h265_config( preset: H265Preset, bit10: bool, keyint: usize, threads: usize, ctb: usize, hdr: bool, ) -> H265Config { let mut cfg = H265Config::new(WIDTH as u32, HEIGHT as u32); cfg.speed = preset; cfg.bit_depth = if bit10 { 10 } else { 8 }; cfg.keyint = keyint; cfg.threads = threads; cfg.ctb_size = ctb; if hdr { cfg.video_signal_type = Some(VideoSignalType { video_format: 5, video_full_range_flag: false, colour_description: Some(ColourDescription { colour_primaries: 9, transfer_characteristics: 16, matrix_coeffs: 9, }), }); cfg.chroma_sample_loc = Some((2, 2)); cfg.mastering_display = Some(MasteringDisplay { display_primaries: [(13250, 34500), (7500, 3000), (34000, 16000)], white_point: (15635, 16450), max_luminance: 10_000_000, min_luminance: 50, }); cfg.content_light = ContentLight { max_cll: Some(1000.0), max_fall: Some(400.0), ..ContentLight::default() }; } cfg } fn h265_stream(cfg: H265Config) -> Result, String> { let format = if cfg.bit_depth == 10 { PixelFormat::I010 } else { PixelFormat::I420 }; let mut frames = Vec::new(); for t in 0..FRAMES { frames.push(frame_in(format, WIDTH, HEIGHT, &natural_frame(WIDTH, HEIGHT, t))?); } let enc = H265Encoder::new(cfg).map_err(|e| e.to_string())?; let pics = enc.encode_sequence(&frames).map_err(|e| e.to_string())?; Ok(pics.iter().flat_map(|p| p.au.clone()).collect()) } fn h265() { let mut index = 0; for preset in [H265Preset::Default, H265Preset::Fast, H265Preset::Faster] { for bit10 in [false, true] { for keyint in [1, 30] { for threads in [1, 4] { for ctb in [32, 64] { for hdr in [false, true] { index += 1; let config = format!( "{preset:?}-{}bit-keyint{keyint}-th{threads}-ctb{ctb}-hdr{hdr}", if bit10 { 10 } else { 8 } ); let case = case_name("h265", &config, index); guard(&case, || { let cfg = h265_config(preset, bit10, keyint, threads, ctb, hdr); outcome(&case, h265_stream(cfg)); }); } } } } } } } fn pyramid(mode: Av1Mode) -> Pyramid { let (mini_gop, quarter) = match mode { Av1Mode::Pyramid4 => (8, Some(-4)), _ => (4, None), }; Pyramid { mini_gop, mid_q_offset: Some(-8), quarter_q_offset: quarter, ..Pyramid::default() } } fn av1_picture(c: &Av1Case, t: usize) -> Picture { let buf = if c.screen { screen_frame(c.w, c.h, t) } else { natural_frame(c.w, c.h, t) }; let (y, u, v) = split(&buf, c.w, c.h); let wide = |p: &[u8]| p.iter().map(|&s| u16::from(s)).collect::>(); Picture { width: c.w, height: c.h, y: wide(y), u: wide(u), v: wide(v), } } fn av1_stream(c: &Av1Case) -> Result, String> { ec_av1::speed::set_speed(c.speed); let gop = if matches!(c.mode, Av1Mode::KeyOnly) { 1 } else { 8 }; let mut cfg = Av1Config::new(c.w, c.h, 100, gop, Colour::default()); cfg.tile_cols_log2 = c.tiles.0; cfg.tile_rows_log2 = c.tiles.1; let built = match c.mode { Av1Mode::KeyOnly | Av1Mode::Flat => Av1Encoder::with_rate_target(cfg, c.rate), Av1Mode::Pyramid3 | Av1Mode::Pyramid4 => { Av1Encoder::with_pyramid_and_rate_target(cfg, pyramid(c.mode), c.rate) } }; let mut enc = built.map_err(|e| e.to_string())?; let mut out = Vec::new(); for t in 0..AV1_FRAMES { let pic = av1_picture(c, t); for packet in enc.encode_frames(&pic).map_err(|e| e.to_string())? { out.extend_from_slice(&packet.data); } } for packet in enc.flush().map_err(|e| e.to_string())? { out.extend_from_slice(&packet.data); } Ok(out) } fn av1_cases() -> Vec { let mut cases = Vec::new(); for mode in AV1_MODES { for rate in AV1_RATES { for tiles in AV1_TILES { for screen in [false, true] { for (w, h) in AV1_SIZES { cases.push(Av1Case { mode, rate, tiles, screen, w, h, speed: 0, }); } } } } } for mode in AV1_MODES { for speed in 0..=10 { cases.push(Av1Case { mode, rate: AV1_RATES[0], tiles: (1, 1), screen: true, w: AV1_SIZES[1].0, h: AV1_SIZES[1].1, speed, }); if speed > 0 { cases.push(Av1Case { mode, rate: AV1_RATES[0], tiles: (0, 0), screen: false, w: WIDTH, h: HEIGHT, speed, }); } } } cases } fn av1() { for (i, c) in av1_cases().iter().enumerate() { let case = case_name("av1", &c.name(), i + 1); guard(&case, || outcome(&case, av1_stream(c))); } } fn prores_stream( profile: Profile, format: PixelFormat, qscale: Option, threads: usize, alpha_bits: u8, ) -> Result, String> { let mut cfg = ProresConfig::new(profile, WIDTH as u32, HEIGHT as u32); cfg.threads = threads; cfg.qscale = qscale; cfg.alpha_bits = alpha_bits; let enc = ProResEncoder::new(cfg).map_err(|e| e.to_string())?; let mut out = Vec::new(); for t in 0..FRAMES { let frame = frame_in(format, WIDTH, HEIGHT, &natural_frame(WIDTH, HEIGHT, t))?; let alpha = (alpha_bits > 0).then(|| alpha_plane(WIDTH, HEIGHT, t)); let bytes = enc .encode_with_alpha(&frame, alpha.as_ref()) .map_err(|e| e.to_string())?; out.extend_from_slice(&bytes); } Ok(out) } fn prores() { let mut index = 0; for profile in [ Profile::Proxy, Profile::Lt, Profile::Standard, Profile::Hq, Profile::P4444, Profile::P4444Xq, ] { let formats = if profile.is_444() { [PixelFormat::I444, PixelFormat::I412] } else { [PixelFormat::I422, PixelFormat::I210] }; let alphas: &[u8] = if profile.is_444() { &[0, 16] } else { &[0] }; for format in formats { for qscale in [None, Some(8)] { for threads in [1, 4] { for &alpha_bits in alphas { index += 1; let config = format!( "{profile:?}-{format:?}-q{qscale:?}-th{threads}-a{alpha_bits}" ); let case = case_name("prores", &config, index); guard(&case, || { outcome( &case, prores_stream(profile, format, qscale, threads, alpha_bits), ); }); } } } } } } pub fn run() { h264(); h265(); av1(); prores(); }