mirror of
https://github.com/shinyobjectz/raea.git
synced 2026-12-01 06:53:19 -10:00
#28 (3): --quality reaches openh264 (Windows' and Linux's encoder). It ignored EncodeOpts.quality, so draft and master wrote byte-identical files. openh264 has no constant-quality mode the crate reaches (a fixed QP with rate control off is dropped, since the crate never sets the layer's QP; Quality mode and a pinned QP range under Bitrate mode fail with Native:3), so the quality now scales the bitrate: io/soft quality_kbps, 2x per 0.1 from high (0.85, the given bitrate unchanged), so master is twice it and draft under a fifth. No --quality keeps the bitrate as before. io/soft/tests/quality.rs encodes three seconds of 640x360 at draft, high and master and needs each a quarter larger than the last (before: one file, three times). Encode speed is openh264's at a different bitrate, nothing added per frame (part of #28)
This commit is contained in:
1 parent
ec33f6b871
commit
298d32431b
2 files changed
+51
-3
No files matched your search
+12
-3
@@ -249,12 +249,21 @@ fn open_h264(path: &Path) -> R<Decoder> {
|
||||
})
|
||||
}
|
||||
|
||||
/// openh264's encoder at the options' bitrate, or all-intra at a fixed quantiser when `intra_qp` is set (every frame
|
||||
/// an IDR: an optimized copy, raea_io_media::mezz).
|
||||
/// The bitrate openh264 is given: the options', or with --quality (o.quality, 0..1) that bitrate scaled by it, 2x a
|
||||
/// step of 0.1 from high (0.85, the default bitrate): master (0.95) twice it, draft (0.6) under a fifth. openh264 has
|
||||
/// no constant-quality mode the crate reaches, and it ignored the quality, so draft and master wrote the same file (#28).
|
||||
pub fn quality_kbps(o: &EncodeOpts) -> u32 {
|
||||
match o.quality {
|
||||
Some(q) => ((o.kbps as f64 * 2f64.powf((q.clamp(0.0, 1.0) as f64 - 0.85) * 10.0)).round() as u32).clamp(300, 240_000),
|
||||
None => o.kbps,
|
||||
}
|
||||
}
|
||||
|
||||
/// openh264's encoder at the options' bitrate (quality_kbps), or all-intra at a fixed quantiser when `intra_qp` is set (every frame an IDR: an optimized copy, raea_io_media::mezz).
|
||||
#[cfg(feature = "h264")]
|
||||
pub fn h264_encoder(out: &Path, o: &EncodeOpts, intra_qp: Option<u8>) -> R<Box<dyn VideoEncoder>> {
|
||||
let config = EncoderConfig::new()
|
||||
.bitrate(BitRate::from_bps(o.kbps * 1000))
|
||||
.bitrate(BitRate::from_bps(quality_kbps(o) * 1000))
|
||||
.max_frame_rate(FrameRate::from_hz(o.fps as f32))
|
||||
.rate_control_mode(RateControlMode::Bitrate)
|
||||
// every frame is a sample: rate control may not drop one (the edit has a frame for every tick)
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
//! #28 (3): `--quality` reaches openh264. It ignored EncodeOpts.quality, so draft and master gave byte-identical files
|
||||
//! (Windows' and Linux's encoder); now quality scales its bitrate (quality_kbps).
|
||||
#![cfg(feature = "h264")]
|
||||
|
||||
use raea_io_codec::EncodeOpts;
|
||||
use raea_io_soft::h264_encoder;
|
||||
|
||||
const W: u32 = 640;
|
||||
const H: u32 = 360;
|
||||
|
||||
/// 90 frames (three seconds, so the rate outweighs the first frame) of a moving pattern of soft rings: detailed enough that the bitrate decides the size, not noise that
|
||||
/// every rate leaves at openh264's coarsest quantiser
|
||||
fn encode(quality: Option<f32>) -> u64 {
|
||||
let dir = std::env::temp_dir().join(format!("raea-soft-quality-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let out = dir.join(format!("{quality:?}.mp4"));
|
||||
let opts = EncodeOpts { width: W, height: H, fps: 30.0, kbps: 2000, quality, nv12: false, realtime: false };
|
||||
let mut enc = h264_encoder(&out, &opts, None).unwrap();
|
||||
for k in 0..90u32 {
|
||||
let px: Vec<u8> = (0..W * H).flat_map(|i| {
|
||||
let (x, y) = (i % W, i / W);
|
||||
let (dx, dy) = (x as i64 - 320 + k as i64 * 4, y as i64 - 180);
|
||||
let v = ((dx * dx + dy * dy) / 23 % 256) as u8;
|
||||
[v, v.wrapping_mul(3), 255 - v, 255]
|
||||
}).collect();
|
||||
enc.push(&px).unwrap();
|
||||
}
|
||||
enc.finish(None).unwrap();
|
||||
let n = std::fs::metadata(&out).unwrap().len();
|
||||
let _ = std::fs::remove_file(&out);
|
||||
n
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn quality_changes_what_openh264_writes() {
|
||||
let (draft, master, rate) = (encode(Some(0.6)), encode(Some(0.95)), encode(Some(0.85)));
|
||||
// before, all three were the same file
|
||||
assert!(draft * 5 / 4 < rate && rate * 5 / 4 < master, "draft {draft}, high {rate} and master {master} bytes: a higher quality spends more");
|
||||
}
|
||||
Reference in new issue
Block a user