edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching·0 forks·Rust·380.6 MiB·cdccbc2·Tahinli·7 hours ago·4263 commits
| 1 | use ec_av1::encode::Picture; |
| 2 | use ec_av1::encoder::{Av1Encoder, Colour, EncoderConfig as Av1Config, Pyramid, RateTarget}; |
| 3 | use ec_core::color::ContentLight; |
| 4 | use ec_core::frame::{PixelFormat, Plane, VideoFrame}; |
| 5 | use ec_h264::{Encoder as H264Encoder, EncoderConfig as H264Config, PictureView, Preset as H264Preset}; |
| 6 | use ec_h265::encoder::{Encoder as H265Encoder, EncoderConfig as H265Config, Preset as H265Preset}; |
| 7 | use ec_h265_syntax::sei::MasteringDisplay; |
| 8 | use ec_h265_syntax::vui::{ColourDescription, VideoSignalType}; |
| 9 | use ec_prores::{AlphaPlane, EncoderConfig as ProresConfig, ProResEncoder, Profile}; |
| 10 | |
| 11 | use crate::{error, guard, hash}; |
| 12 | |
| 13 | const WIDTH: usize = 176; |
| 14 | const HEIGHT: usize = 144; |
| 15 | const FRAMES: usize = 6; |
| 16 | const AV1_FRAMES: usize = 8; |
| 17 | |
| 18 | const AV1_MODES: [Av1Mode; 4] = [Av1Mode::KeyOnly, Av1Mode::Flat, Av1Mode::Pyramid3, Av1Mode::Pyramid4]; |
| 19 | const AV1_RATES: [RateTarget; 2] = [ |
| 20 | RateTarget::Quality(40), |
| 21 | RateTarget::Bitrate { |
| 22 | bits_per_second: 300_000, |
| 23 | frames_per_second: 25.0, |
| 24 | }, |
| 25 | ]; |
| 26 | const AV1_TILES: [(u32, u32); 2] = [(0, 0), (1, 1)]; |
| 27 | const AV1_SIZES: [(usize, usize); 2] = [(176, 144), (150, 90)]; |
| 28 | |
| 29 | #[derive(Clone, Copy, Debug)] |
| 30 | enum Av1Mode { |
| 31 | KeyOnly, |
| 32 | Flat, |
| 33 | Pyramid3, |
| 34 | Pyramid4, |
| 35 | } |
| 36 | |
| 37 | #[derive(Clone, Copy, Debug)] |
| 38 | struct Av1Case { |
| 39 | mode: Av1Mode, |
| 40 | rate: RateTarget, |
| 41 | tiles: (u32, u32), |
| 42 | screen: bool, |
| 43 | w: usize, |
| 44 | h: usize, |
| 45 | speed: u8, |
| 46 | } |
| 47 | |
| 48 | impl Av1Case { |
| 49 | fn name(&self) -> String { |
| 50 | let content = if self.screen { "screen" } else { "natural" }; |
| 51 | let refused = if self.w % 2 == 1 || self.h % 2 == 1 { "-refused" } else { "" }; |
| 52 | format!( |
| 53 | "{:?}-{:?}-tiles{}x{}-{content}-{}x{}-s{}{refused}", |
| 54 | self.mode, self.rate, self.tiles.0, self.tiles.1, self.w, self.h, self.speed |
| 55 | ) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | fn case_name(codec: &str, config: &str, index: usize) -> String { |
| 60 | format!("encode_video/{codec}/{config}/{index}") |
| 61 | } |
| 62 | |
| 63 | fn outcome(case: &str, result: Result<Vec<u8>, String>) { |
| 64 | match result { |
| 65 | Ok(bytes) => hash(case, &bytes), |
| 66 | Err(err) => error(case, err), |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | fn clamp8(v: i32) -> u8 { |
| 71 | v.clamp(0, 255) as u8 |
| 72 | } |
| 73 | |
| 74 | fn natural_frame(w: usize, h: usize, t: usize) -> Vec<u8> { |
| 75 | let mut seed = ((w * 1_000_003 + h * 1009 + t * 7919) as u64) | 1; |
| 76 | let mut noise = || { |
| 77 | seed = seed |
| 78 | .wrapping_mul(6364136223846793005) |
| 79 | .wrapping_add(1442695040888963407); |
| 80 | ((seed >> 33) % 9) as i32 - 4 |
| 81 | }; |
| 82 | let sq = w / 4; |
| 83 | let x0 = (4 * t) % (w - sq); |
| 84 | let y0 = (2 * t) % (h - sq); |
| 85 | let mut out = Vec::with_capacity(w * h * 3 / 2); |
| 86 | for y in 0..h { |
| 87 | for x in 0..w { |
| 88 | let inside = (x0..x0 + sq).contains(&x) && (y0..y0 + sq).contains(&y); |
| 89 | let base: i32 = if inside { |
| 90 | 230 |
| 91 | } else { |
| 92 | (x * 200 / w + y * 40 / h) as i32 |
| 93 | }; |
| 94 | out.push(clamp8(base + noise())); |
| 95 | } |
| 96 | } |
| 97 | let (cw, ch) = (w.div_ceil(2), h.div_ceil(2)); |
| 98 | for _ in 0..ch { |
| 99 | for x in 0..cw { |
| 100 | out.push(clamp8(64 + (x * 128 / cw) as i32 + noise())); |
| 101 | } |
| 102 | } |
| 103 | for y in 0..ch { |
| 104 | for _ in 0..cw { |
| 105 | out.push(clamp8(64 + (y * 128 / ch) as i32 + noise())); |
| 106 | } |
| 107 | } |
| 108 | out |
| 109 | } |
| 110 | |
| 111 | fn screen_frame(w: usize, h: usize, t: usize) -> Vec<u8> { |
| 112 | let mut out = Vec::with_capacity(w * h * 3 / 2); |
| 113 | for y in 0..h { |
| 114 | for x in 0..w { |
| 115 | let v = if (y + t).is_multiple_of(6) { |
| 116 | 16 |
| 117 | } else if ((x + 2 * t) / 8 + y / 8).is_multiple_of(3) { |
| 118 | 235 |
| 119 | } else { |
| 120 | 81 |
| 121 | }; |
| 122 | out.push(v); |
| 123 | } |
| 124 | } |
| 125 | let chroma = w.div_ceil(2) * h.div_ceil(2) * 2; |
| 126 | out.resize(w * h + chroma, 128); |
| 127 | out |
| 128 | } |
| 129 | |
| 130 | fn split(buf: &[u8], w: usize, h: usize) -> (&[u8], &[u8], &[u8]) { |
| 131 | let (ys, cs) = (w * h, w.div_ceil(2) * h.div_ceil(2)); |
| 132 | (&buf[..ys], &buf[ys..ys + cs], &buf[ys + cs..ys + 2 * cs]) |
| 133 | } |
| 134 | |
| 135 | fn resample(src: &[u8], sw: usize, sh: usize, dw: usize, dh: usize) -> Vec<u8> { |
| 136 | let mut out = Vec::with_capacity(dw * dh); |
| 137 | for y in 0..dh { |
| 138 | for x in 0..dw { |
| 139 | out.push(src[(y * sh / dh) * sw + x * sw / dw]); |
| 140 | } |
| 141 | } |
| 142 | out |
| 143 | } |
| 144 | |
| 145 | fn to_bytes(samples: &[u8], bits: u32) -> Vec<u8> { |
| 146 | if bits == 8 { |
| 147 | return samples.to_vec(); |
| 148 | } |
| 149 | samples |
| 150 | .iter() |
| 151 | .flat_map(|&s| (u16::from(s) << (bits - 8)).to_le_bytes()) |
| 152 | .collect() |
| 153 | } |
| 154 | |
| 155 | fn frame_in(format: PixelFormat, w: usize, h: usize, buf: &[u8]) -> Result<VideoFrame, String> { |
| 156 | let (y, u, v) = split(buf, w, h); |
| 157 | let (cw, ch) = (w.div_ceil(2), h.div_ceil(2)); |
| 158 | let src = [(y, w, h), (u, cw, ch), (v, cw, ch)]; |
| 159 | let bits = format.bits_per_component(); |
| 160 | let bytes = format.bytes_per_component(); |
| 161 | let mut planes = Vec::new(); |
| 162 | for ((s, sw, sh), (row, rows)) in src.iter().zip(format.plane_geometry(w as u32, h as u32)) { |
| 163 | let data = to_bytes(&resample(s, *sw, *sh, row / bytes, rows), bits); |
| 164 | planes.push(Plane::new(data, row)); |
| 165 | } |
| 166 | VideoFrame::try_new(format, w as u32, h as u32, planes).map_err(|e| e.to_string()) |
| 167 | } |
| 168 | |
| 169 | fn alpha_plane(w: usize, h: usize, t: usize) -> AlphaPlane { |
| 170 | AlphaPlane { |
| 171 | width: w as u32, |
| 172 | height: h as u32, |
| 173 | stride: w, |
| 174 | data: (0..w * h).map(|i| ((i * 257 + t * 4099) % 65536) as u16).collect(), |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | fn h264_config( |
| 179 | preset: H264Preset, |
| 180 | cabac: bool, |
| 181 | t8: bool, |
| 182 | qp: i32, |
| 183 | bitrate: u32, |
| 184 | threads: usize, |
| 185 | ) -> H264Config { |
| 186 | let mut cfg = H264Config::new(WIDTH as u32, HEIGHT as u32); |
| 187 | cfg.preset = preset; |
| 188 | cfg.cabac = cabac; |
| 189 | cfg.transform_8x8 = t8; |
| 190 | cfg.qp = qp; |
| 191 | cfg.bitrate = bitrate; |
| 192 | cfg.threads = threads; |
| 193 | cfg.gop_size = 4; |
| 194 | cfg |
| 195 | } |
| 196 | |
| 197 | fn h264_stream(cfg: H264Config) -> Result<Vec<u8>, String> { |
| 198 | let mut enc = H264Encoder::new(cfg).map_err(|e| e.to_string())?; |
| 199 | let mut out = Vec::new(); |
| 200 | for t in 0..FRAMES { |
| 201 | let buf = natural_frame(WIDTH, HEIGHT, t); |
| 202 | let (y, u, v) = split(&buf, WIDTH, HEIGHT); |
| 203 | let view = PictureView::i420(WIDTH as u32, HEIGHT as u32, y, u, v); |
| 204 | let pic = enc.encode(&view).map_err(|e| e.to_string())?; |
| 205 | out.extend_from_slice(&pic.au); |
| 206 | } |
| 207 | Ok(out) |
| 208 | } |
| 209 | |
| 210 | fn h264() { |
| 211 | let mut index = 0; |
| 212 | for preset in [H264Preset::Fast, H264Preset::Balanced] { |
| 213 | for cabac in [true, false] { |
| 214 | for t8 in [true, false] { |
| 215 | for (qp, bitrate) in [(20, 0), (36, 0), (26, 400_000)] { |
| 216 | for threads in [1, 2, 4] { |
| 217 | index += 1; |
| 218 | let config = format!( |
| 219 | "{preset:?}-cabac{cabac}-t8{t8}-qp{qp}-br{bitrate}-th{threads}" |
| 220 | ); |
| 221 | let case = case_name("h264", &config, index); |
| 222 | guard(&case, || { |
| 223 | let cfg = h264_config(preset, cabac, t8, qp, bitrate, threads); |
| 224 | outcome(&case, h264_stream(cfg)); |
| 225 | }); |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | fn h265_config( |
| 234 | preset: H265Preset, |
| 235 | bit10: bool, |
| 236 | keyint: usize, |
| 237 | threads: usize, |
| 238 | ctb: usize, |
| 239 | hdr: bool, |
| 240 | ) -> H265Config { |
| 241 | let mut cfg = H265Config::new(WIDTH as u32, HEIGHT as u32); |
| 242 | cfg.speed = preset; |
| 243 | cfg.bit_depth = if bit10 { 10 } else { 8 }; |
| 244 | cfg.keyint = keyint; |
| 245 | cfg.threads = threads; |
| 246 | cfg.ctb_size = ctb; |
| 247 | if hdr { |
| 248 | cfg.video_signal_type = Some(VideoSignalType { |
| 249 | video_format: 5, |
| 250 | video_full_range_flag: false, |
| 251 | colour_description: Some(ColourDescription { |
| 252 | colour_primaries: 9, |
| 253 | transfer_characteristics: 16, |
| 254 | matrix_coeffs: 9, |
| 255 | }), |
| 256 | }); |
| 257 | cfg.chroma_sample_loc = Some((2, 2)); |
| 258 | cfg.mastering_display = Some(MasteringDisplay { |
| 259 | display_primaries: [(13250, 34500), (7500, 3000), (34000, 16000)], |
| 260 | white_point: (15635, 16450), |
| 261 | max_luminance: 10_000_000, |
| 262 | min_luminance: 50, |
| 263 | }); |
| 264 | cfg.content_light = ContentLight { |
| 265 | max_cll: Some(1000.0), |
| 266 | max_fall: Some(400.0), |
| 267 | ..ContentLight::default() |
| 268 | }; |
| 269 | } |
| 270 | cfg |
| 271 | } |
| 272 | |
| 273 | fn h265_stream(cfg: H265Config) -> Result<Vec<u8>, String> { |
| 274 | let format = if cfg.bit_depth == 10 { |
| 275 | PixelFormat::I010 |
| 276 | } else { |
| 277 | PixelFormat::I420 |
| 278 | }; |
| 279 | let mut frames = Vec::new(); |
| 280 | for t in 0..FRAMES { |
| 281 | frames.push(frame_in(format, WIDTH, HEIGHT, &natural_frame(WIDTH, HEIGHT, t))?); |
| 282 | } |
| 283 | let enc = H265Encoder::new(cfg).map_err(|e| e.to_string())?; |
| 284 | let pics = enc.encode_sequence(&frames).map_err(|e| e.to_string())?; |
| 285 | Ok(pics.iter().flat_map(|p| p.au.clone()).collect()) |
| 286 | } |
| 287 | |
| 288 | fn h265() { |
| 289 | let mut index = 0; |
| 290 | for preset in [H265Preset::Default, H265Preset::Fast, H265Preset::Faster] { |
| 291 | for bit10 in [false, true] { |
| 292 | for keyint in [1, 30] { |
| 293 | for threads in [1, 4] { |
| 294 | for ctb in [32, 64] { |
| 295 | for hdr in [false, true] { |
| 296 | index += 1; |
| 297 | let config = format!( |
| 298 | "{preset:?}-{}bit-keyint{keyint}-th{threads}-ctb{ctb}-hdr{hdr}", |
| 299 | if bit10 { 10 } else { 8 } |
| 300 | ); |
| 301 | let case = case_name("h265", &config, index); |
| 302 | guard(&case, || { |
| 303 | let cfg = h265_config(preset, bit10, keyint, threads, ctb, hdr); |
| 304 | outcome(&case, h265_stream(cfg)); |
| 305 | }); |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | fn pyramid(mode: Av1Mode) -> Pyramid { |
| 315 | let (mini_gop, quarter) = match mode { |
| 316 | Av1Mode::Pyramid4 => (8, Some(-4)), |
| 317 | _ => (4, None), |
| 318 | }; |
| 319 | Pyramid { |
| 320 | mini_gop, |
| 321 | mid_q_offset: Some(-8), |
| 322 | quarter_q_offset: quarter, |
| 323 | ..Pyramid::default() |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | fn av1_picture(c: &Av1Case, t: usize) -> Picture { |
| 328 | let buf = if c.screen { |
| 329 | screen_frame(c.w, c.h, t) |
| 330 | } else { |
| 331 | natural_frame(c.w, c.h, t) |
| 332 | }; |
| 333 | let (y, u, v) = split(&buf, c.w, c.h); |
| 334 | let wide = |p: &[u8]| p.iter().map(|&s| u16::from(s)).collect::<Vec<u16>>(); |
| 335 | Picture { |
| 336 | width: c.w, |
| 337 | height: c.h, |
| 338 | y: wide(y), |
| 339 | u: wide(u), |
| 340 | v: wide(v), |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | fn av1_stream(c: &Av1Case) -> Result<Vec<u8>, String> { |
| 345 | ec_av1::speed::set_speed(c.speed); |
| 346 | let gop = if matches!(c.mode, Av1Mode::KeyOnly) { 1 } else { 8 }; |
| 347 | let mut cfg = Av1Config::new(c.w, c.h, 100, gop, Colour::default()); |
| 348 | cfg.tile_cols_log2 = c.tiles.0; |
| 349 | cfg.tile_rows_log2 = c.tiles.1; |
| 350 | let built = match c.mode { |
| 351 | Av1Mode::KeyOnly | Av1Mode::Flat => Av1Encoder::with_rate_target(cfg, c.rate), |
| 352 | Av1Mode::Pyramid3 | Av1Mode::Pyramid4 => { |
| 353 | Av1Encoder::with_pyramid_and_rate_target(cfg, pyramid(c.mode), c.rate) |
| 354 | } |
| 355 | }; |
| 356 | let mut enc = built.map_err(|e| e.to_string())?; |
| 357 | let mut out = Vec::new(); |
| 358 | for t in 0..AV1_FRAMES { |
| 359 | let pic = av1_picture(c, t); |
| 360 | for packet in enc.encode_frames(&pic).map_err(|e| e.to_string())? { |
| 361 | out.extend_from_slice(&packet.data); |
| 362 | } |
| 363 | } |
| 364 | for packet in enc.flush().map_err(|e| e.to_string())? { |
| 365 | out.extend_from_slice(&packet.data); |
| 366 | } |
| 367 | Ok(out) |
| 368 | } |
| 369 | |
| 370 | fn av1_cases() -> Vec<Av1Case> { |
| 371 | let mut cases = Vec::new(); |
| 372 | for mode in AV1_MODES { |
| 373 | for rate in AV1_RATES { |
| 374 | for tiles in AV1_TILES { |
| 375 | for screen in [false, true] { |
| 376 | for (w, h) in AV1_SIZES { |
| 377 | cases.push(Av1Case { |
| 378 | mode, |
| 379 | rate, |
| 380 | tiles, |
| 381 | screen, |
| 382 | w, |
| 383 | h, |
| 384 | speed: 0, |
| 385 | }); |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | for mode in AV1_MODES { |
| 392 | for speed in 0..=10 { |
| 393 | cases.push(Av1Case { |
| 394 | mode, |
| 395 | rate: AV1_RATES[0], |
| 396 | tiles: (1, 1), |
| 397 | screen: true, |
| 398 | w: AV1_SIZES[1].0, |
| 399 | h: AV1_SIZES[1].1, |
| 400 | speed, |
| 401 | }); |
| 402 | if speed > 0 { |
| 403 | cases.push(Av1Case { |
| 404 | mode, |
| 405 | rate: AV1_RATES[0], |
| 406 | tiles: (0, 0), |
| 407 | screen: false, |
| 408 | w: WIDTH, |
| 409 | h: HEIGHT, |
| 410 | speed, |
| 411 | }); |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | cases.push(Av1Case { |
| 416 | mode: Av1Mode::KeyOnly, |
| 417 | rate: AV1_RATES[0], |
| 418 | tiles: (0, 0), |
| 419 | screen: false, |
| 420 | w: 151, |
| 421 | h: 91, |
| 422 | speed: 0, |
| 423 | }); |
| 424 | cases |
| 425 | } |
| 426 | |
| 427 | fn av1() { |
| 428 | for (i, c) in av1_cases().iter().enumerate() { |
| 429 | let case = case_name("av1", &c.name(), i + 1); |
| 430 | guard(&case, || outcome(&case, av1_stream(c))); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | fn prores_stream( |
| 435 | profile: Profile, |
| 436 | format: PixelFormat, |
| 437 | qscale: Option<u8>, |
| 438 | threads: usize, |
| 439 | alpha_bits: u8, |
| 440 | ) -> Result<Vec<u8>, String> { |
| 441 | let mut cfg = ProresConfig::new(profile, WIDTH as u32, HEIGHT as u32); |
| 442 | cfg.threads = threads; |
| 443 | cfg.qscale = qscale; |
| 444 | cfg.alpha_bits = alpha_bits; |
| 445 | let enc = ProResEncoder::new(cfg).map_err(|e| e.to_string())?; |
| 446 | let mut out = Vec::new(); |
| 447 | for t in 0..FRAMES { |
| 448 | let frame = frame_in(format, WIDTH, HEIGHT, &natural_frame(WIDTH, HEIGHT, t))?; |
| 449 | let alpha = (alpha_bits > 0).then(|| alpha_plane(WIDTH, HEIGHT, t)); |
| 450 | let bytes = enc |
| 451 | .encode_with_alpha(&frame, alpha.as_ref()) |
| 452 | .map_err(|e| e.to_string())?; |
| 453 | out.extend_from_slice(&bytes); |
| 454 | } |
| 455 | Ok(out) |
| 456 | } |
| 457 | |
| 458 | fn prores() { |
| 459 | let mut index = 0; |
| 460 | for profile in [ |
| 461 | Profile::Proxy, |
| 462 | Profile::Lt, |
| 463 | Profile::Standard, |
| 464 | Profile::Hq, |
| 465 | Profile::P4444, |
| 466 | Profile::P4444Xq, |
| 467 | ] { |
| 468 | let formats = if profile.is_444() { |
| 469 | [PixelFormat::I444, PixelFormat::I412] |
| 470 | } else { |
| 471 | [PixelFormat::I422, PixelFormat::I210] |
| 472 | }; |
| 473 | let alphas: &[u8] = if profile.is_444() { &[0, 16] } else { &[0] }; |
| 474 | for format in formats { |
| 475 | for qscale in [None, Some(8)] { |
| 476 | for threads in [1, 4] { |
| 477 | for &alpha_bits in alphas { |
| 478 | index += 1; |
| 479 | let config = format!( |
| 480 | "{profile:?}-{format:?}-q{qscale:?}-th{threads}-a{alpha_bits}" |
| 481 | ); |
| 482 | let case = case_name("prores", &config, index); |
| 483 | guard(&case, || { |
| 484 | outcome( |
| 485 | &case, |
| 486 | prores_stream(profile, format, qscale, threads, alpha_bits), |
| 487 | ); |
| 488 | }); |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | pub fn run() { |
| 497 | h264(); |
| 498 | h265(); |
| 499 | av1(); |
| 500 | prores(); |
| 501 | } |