Target-bitrate rate mode, per-level rate loop and an EC_AV1_PYRAMID knob on ec-bench
Browse files at this commit4d40670c3fcf6eafd597ee6edd2dcfe3c595aa5cParent: f025ec8- feat
- target-bitrate rate mode, per-level rate loop and an EC_AV1_PYRAMID knob on ec-bench
2 changed files +86 -4
- crates/ec-av1/src/encoder.rs +59 -0
- tools/ec-bench/src/main.rs +27 -4
crates/ec-av1/src/encoder.rsmodified+59 -0
| @@ -1255,6 +1255,65 @@ mod tests { | ||
| 1255 | 1255 | ); |
| 1256 | 1256 | } |
| 1257 | 1257 | |
| 1258 | + /// [`RateTarget::Bitrate`] lands the achieved bitrate within ±10% of the | |
| 1259 | + /// target over 48 frames of a real clip, flat and under a pyramid alike. | |
| 1260 | + /// This is the accuracy claim of the target-bitrate mode: no two-pass, no | |
| 1261 | + /// lookahead, just each frame's size predicted from the previous frame of | |
| 1262 | + /// its own level and one clamped proportional step. | |
| 1263 | + #[test] | |
| 1264 | + fn bitrate_target_lands_within_10_percent_over_48_frames() { | |
| 1265 | + let (width, height, frames) = (640usize, 384usize, 48usize); | |
| 1266 | + let Some(pictures) = h264_clip_frames(width, height, frames) else { | |
| 1267 | + eprintln!("SKIP bitrate_target_lands_within_10_percent_over_48_frames: no ffmpeg/fixture"); | |
| 1268 | + return; | |
| 1269 | + }; | |
| 1270 | + let fps = 24.0; | |
| 1271 | + for bits_per_second in [768_000u32, 1_536_000] { | |
| 1272 | + let config = EncoderConfig { | |
| 1273 | + width, | |
| 1274 | + height, | |
| 1275 | + base_q_idx: 100, | |
| 1276 | + gop: frames, | |
| 1277 | + colour: Colour::Bt709Limited, | |
| 1278 | + }; | |
| 1279 | + let rate = RateTarget::Bitrate { | |
| 1280 | + bits_per_second, | |
| 1281 | + frames_per_second: fps, | |
| 1282 | + }; | |
| 1283 | + for pyramid in [None, Some(Pyramid::default())] { | |
| 1284 | + let mut enc = match pyramid { | |
| 1285 | + None => Av1Encoder::with_rate_target(config, rate).unwrap(), | |
| 1286 | + Some(p) => Av1Encoder::with_pyramid_and_rate_target(config, p, rate).unwrap(), | |
| 1287 | + }; | |
| 1288 | + let mut coded = 0usize; | |
| 1289 | + for picture in &pictures { | |
| 1290 | + for packet in enc.encode_frames(picture).unwrap() { | |
| 1291 | + coded += packet.data.len(); | |
| 1292 | + } | |
| 1293 | + } | |
| 1294 | + for packet in enc.flush().unwrap() { | |
| 1295 | + coded += packet.data.len(); | |
| 1296 | + } | |
| 1297 | + let seconds = frames as f64 / fps; | |
| 1298 | + let achieved = coded as f64 * 8.0 / seconds; | |
| 1299 | + let error = achieved / f64::from(bits_per_second) - 1.0; | |
| 1300 | + eprintln!( | |
| 1301 | + "bitrate {bits_per_second} bps, pyramid {}: {coded} bytes over {seconds:.2}s \ | |
| 1302 | + = {achieved:.0} bps ({:+.1}%)", | |
| 1303 | + pyramid.is_some(), | |
| 1304 | + 100.0 * error, | |
| 1305 | + ); | |
| 1306 | + assert!( | |
| 1307 | + error.abs() <= 0.10, | |
| 1308 | + "achieved {achieved:.0} bps is {:+.1}% off the {bits_per_second} bps target \ | |
| 1309 | + (pyramid {})", | |
| 1310 | + 100.0 * error, | |
| 1311 | + pyramid.is_some(), | |
| 1312 | + ); | |
| 1313 | + } | |
| 1314 | + } | |
| 1315 | + } | |
| 1316 | + | |
| 1258 | 1317 | /// The `BytesPerFrame` controller's own windup bound: no frame's coded |
| 1259 | 1318 | /// `base_q_idx` step exceeds [`RateLoop::STEP_CLAMP`], across a real |
| 1260 | 1319 | /// clip's full range of content (so a scene cut can't be the one frame |
tools/ec-bench/src/main.rsmodified+27 -4
| @@ -13,7 +13,7 @@ use std::process::Command; | ||
| 13 | 13 | use std::time::Instant; |
| 14 | 14 | |
| 15 | 15 | use ec_av1::encode::Picture as Av1Picture; |
| 16 | -use ec_av1::encoder::{Av1Encoder, Colour, EncoderConfig as Av1Config}; | |
| 16 | +use ec_av1::encoder::{Av1Encoder, Colour, EncoderConfig as Av1Config, Level, Pyramid}; | |
| 17 | 17 | use ec_core::frame::{ |
| 18 | 18 | AudioFrame, ChannelLayout, Frame, PixelFormat, Plane, SampleFormat, VideoFrame, |
| 19 | 19 | }; |
| @@ -352,9 +352,26 @@ fn bench_av1_encode(rows: &mut Vec<Row>) { | ||
| 352 | 352 | gop: 10, |
| 353 | 353 | colour: Colour::default(), |
| 354 | 354 | }; |
| 355 | - let mut enc = Av1Encoder::new(cfg).expect("av1 encoder"); | |
| 355 | + // `EC_AV1_PYRAMID=<mini_gop>[:<arf_q_offset>:<leaf_q_offset>]` benches the | |
| 356 | + // encoder's coding pyramid (hidden ALTREF + show_existing_frame) instead | |
| 357 | + // of the flat one-picture-one-packet stream, the same knob shape as | |
| 358 | + // `EC_H265_RDOQ_ESTIMATE`. Unset keeps the flat default. | |
| 359 | + let pyramid = std::env::var("EC_AV1_PYRAMID").ok().and_then(|spec| { | |
| 360 | + let mut f = spec.split(':'); | |
| 361 | + let d = Pyramid::default(); | |
| 362 | + Some(Pyramid { | |
| 363 | + mini_gop: f.next()?.parse().ok()?, | |
| 364 | + arf_q_offset: f.next().and_then(|v| v.parse().ok()).unwrap_or(d.arf_q_offset), | |
| 365 | + leaf_q_offset: f.next().and_then(|v| v.parse().ok()).unwrap_or(d.leaf_q_offset), | |
| 366 | + }) | |
| 367 | + }); | |
| 368 | + let mut enc = match pyramid { | |
| 369 | + None => Av1Encoder::new(cfg).expect("av1 encoder"), | |
| 370 | + Some(p) => Av1Encoder::with_pyramid(cfg, p).expect("av1 encoder"), | |
| 371 | + }; | |
| 356 | 372 | let frame_len = (w * h + 2 * (w.div_ceil(2) * h.div_ceil(2))) as usize; |
| 357 | 373 | let mut stream = Vec::new(); |
| 374 | + let mut hidden = 0usize; | |
| 358 | 375 | let start = Instant::now(); |
| 359 | 376 | for frame in planes.chunks_exact(frame_len) { |
| 360 | 377 | let (y, rest) = frame.split_at((w * h) as usize); |
| @@ -368,7 +385,13 @@ fn bench_av1_encode(rows: &mut Vec<Row>) { | ||
| 368 | 385 | u: u.iter().map(|&s| u16::from(s)).collect(), |
| 369 | 386 | v: v.iter().map(|&s| u16::from(s)).collect(), |
| 370 | 387 | }; |
| 371 | - let packet = enc.encode(&pic).expect("av1 encode"); | |
| 388 | + for packet in enc.encode_frames(&pic).expect("av1 encode") { | |
| 389 | + hidden += usize::from(packet.level == Level::Arf); | |
| 390 | + stream.extend_from_slice(&packet.data); | |
| 391 | + } | |
| 392 | + } | |
| 393 | + for packet in enc.flush().expect("av1 flush") { | |
| 394 | + hidden += usize::from(packet.level == Level::Arf); | |
| 372 | 395 | stream.extend_from_slice(&packet.data); |
| 373 | 396 | } |
| 374 | 397 | let wall = start.elapsed().as_secs_f64(); |
| @@ -379,7 +402,7 @@ fn bench_av1_encode(rows: &mut Vec<Row>) { | ||
| 379 | 402 | component: "ec-av1", |
| 380 | 403 | direction: "encode", |
| 381 | 404 | content: format!( |
| 382 | - "{w}x{h}, {n} frames, gop=10, {fps:.1} fps, {bytes_per_frame:.0} B/frame" | |
| 405 | + "{w}x{h}, {n} frames, gop=10, {hidden} hidden, {fps:.1} fps, {bytes_per_frame:.0} B/frame" | |
| 383 | 406 | ), |
| 384 | 407 | media: format!("{media_s:.1}s"), |
| 385 | 408 | wall_ms: wall * 1000.0, |