h264: decide intra against inter on real coded cost
Browse files at this commitdbbd3cc4d2ce1292060361c5193d46d4cca7f211Parent: a5aa64e3 changed files +260 -79
- crates/ec-h264/src/enc/mb.rs +183 -64
- crates/ec-h264/tests/encode.rs +12 -15
- lanes/h264x264-r2.report.md +65 -0
crates/ec-h264/src/enc/mb.rsmodified+183 -64
| @@ -19,7 +19,7 @@ use crate::deblock::{edge_params, filter_luma_h_edge16, filter_luma_line}; | |||
| 19 | use crate::decoder::{ | 19 | use crate::decoder::{ |
| 20 | chroma_nz_pair, gather_nbr4, gather_nbr8, luma_nz_pair, mb_neighbors, MbNeighbors, | 20 | chroma_nz_pair, gather_nbr4, gather_nbr8, luma_nz_pair, mb_neighbors, MbNeighbors, |
| 21 | }; | 21 | }; |
| 22 | -use crate::dpb::{Picture, BLK_SKIP}; | 22 | +use crate::dpb::{Picture, Plane8, BLK_SKIP}; |
| 23 | use crate::entropy::{ | 23 | use crate::entropy::{ |
| 24 | BlockCat, MbCtx, MbInfo, FLAG_CHROMA_PRED, FLAG_DECODED, FLAG_I16, FLAG_INTER, FLAG_SKIP, | 24 | BlockCat, MbCtx, MbInfo, FLAG_CHROMA_PRED, FLAG_DECODED, FLAG_I16, FLAG_INTER, FLAG_SKIP, |
| 25 | FLAG_TRANS8X8, | 25 | FLAG_TRANS8X8, |
| @@ -316,6 +316,128 @@ fn motion_rate(e: &MbEnc<'_>, bits: f64) -> i32 { | |||
| 316 | (lambda * bits).round() as i32 | 316 | (lambda * bits).round() as i32 |
| 317 | } | 317 | } |
| 318 | 318 | ||
| 319 | + | ||
| 320 | +/// Everything one macroblock's coding writes into the picture, kept so that a | ||
| 321 | +/// branch can be coded, measured and undone. | ||
| 322 | +struct MbState { | ||
| 323 | + y: [u8; 256], | ||
| 324 | + c: [[u8; 64]; 2], | ||
| 325 | + nz_y: [u8; 16], | ||
| 326 | + nz_c: [[u8; 4]; 2], | ||
| 327 | + i4_modes: [u8; 16], | ||
| 328 | + mv: [[[i16; 2]; 2]; 16], | ||
| 329 | + ref_idx: [[i8; 2]; 16], | ||
| 330 | + ref_id: [[i32; 2]; 16], | ||
| 331 | + mvd_abs: [[u8; 4]; 16], | ||
| 332 | + blk: [u8; 16], | ||
| 333 | + mb_qp: u8, | ||
| 334 | + mb_flags: u8, | ||
| 335 | + mb_cbp: u8, | ||
| 336 | + mb_dc_cbf: u8, | ||
| 337 | + mb_slice: u16, | ||
| 338 | + decoded_mbs: usize, | ||
| 339 | + qp: i32, | ||
| 340 | + qp_delta_inc: u8, | ||
| 341 | +} | ||
| 342 | + | ||
| 343 | +/// Copy `mb`'s samples out of `plane`, `n` by `n`. | ||
| 344 | +fn save_plane(plane: &Plane8, x0: usize, y0: usize, out: &mut [u8], n: usize) { | ||
| 345 | + for row in 0..n { | ||
| 346 | + let o = plane.at(x0, y0 + row); | ||
| 347 | + out[row * n..(row + 1) * n].copy_from_slice(&plane.data[o..o + n]); | ||
| 348 | + } | ||
| 349 | +} | ||
| 350 | + | ||
| 351 | +fn load_plane(plane: &mut Plane8, x0: usize, y0: usize, src: &[u8], n: usize) { | ||
| 352 | + for row in 0..n { | ||
| 353 | + let o = plane.at(x0, y0 + row); | ||
| 354 | + plane.data[o..o + n].copy_from_slice(&src[row * n..(row + 1) * n]); | ||
| 355 | + } | ||
| 356 | +} | ||
| 357 | + | ||
| 358 | +fn save_mb_state(pic: &Picture, e: &MbEnc<'_>, mb_addr: usize) -> MbState { | ||
| 359 | + let (mb_x, mb_y) = (mb_addr % pic.mb_w, mb_addr / pic.mb_w); | ||
| 360 | + let (w4, w2) = (pic.mb_w * 4, pic.mb_w * 2); | ||
| 361 | + let mut st = MbState { | ||
| 362 | + y: [0; 256], | ||
| 363 | + c: [[0; 64]; 2], | ||
| 364 | + nz_y: [0; 16], | ||
| 365 | + nz_c: [[0; 4]; 2], | ||
| 366 | + i4_modes: [0; 16], | ||
| 367 | + mv: [[[0; 2]; 2]; 16], | ||
| 368 | + ref_idx: [[0; 2]; 16], | ||
| 369 | + ref_id: [[0; 2]; 16], | ||
| 370 | + mvd_abs: [[0; 4]; 16], | ||
| 371 | + blk: [0; 16], | ||
| 372 | + mb_qp: pic.mb_qp[mb_addr], | ||
| 373 | + mb_flags: pic.mb_flags[mb_addr], | ||
| 374 | + mb_cbp: pic.mb_cbp[mb_addr], | ||
| 375 | + mb_dc_cbf: pic.mb_dc_cbf[mb_addr], | ||
| 376 | + mb_slice: pic.mb_slice[mb_addr], | ||
| 377 | + decoded_mbs: pic.decoded_mbs, | ||
| 378 | + qp: e.qp, | ||
| 379 | + qp_delta_inc: e.qp_delta_inc, | ||
| 380 | + }; | ||
| 381 | + save_plane(&pic.y, mb_x * 16, mb_y * 16, &mut st.y, 16); | ||
| 382 | + save_plane(&pic.cb, mb_x * 8, mb_y * 8, &mut st.c[0], 8); | ||
| 383 | + save_plane(&pic.cr, mb_x * 8, mb_y * 8, &mut st.c[1], 8); | ||
| 384 | + for row in 0..4 { | ||
| 385 | + let b = (mb_y * 4 + row) * w4 + mb_x * 4; | ||
| 386 | + for col in 0..4 { | ||
| 387 | + let (i, j) = (row * 4 + col, b + col); | ||
| 388 | + st.nz_y[i] = pic.nz_y[j]; | ||
| 389 | + st.i4_modes[i] = pic.i4_modes[j]; | ||
| 390 | + st.mv[i] = pic.mv[j]; | ||
| 391 | + st.ref_idx[i] = pic.ref_idx[j]; | ||
| 392 | + st.ref_id[i] = pic.ref_id[j]; | ||
| 393 | + st.mvd_abs[i] = pic.mvd_abs[j]; | ||
| 394 | + st.blk[i] = pic.blk[j]; | ||
| 395 | + } | ||
| 396 | + } | ||
| 397 | + for comp in 0..2 { | ||
| 398 | + for row in 0..2 { | ||
| 399 | + let b = (mb_y * 2 + row) * w2 + mb_x * 2; | ||
| 400 | + st.nz_c[comp][row * 2..row * 2 + 2].copy_from_slice(&pic.nz_c[comp][b..b + 2]); | ||
| 401 | + } | ||
| 402 | + } | ||
| 403 | + st | ||
| 404 | +} | ||
| 405 | + | ||
| 406 | +fn load_mb_state(pic: &mut Picture, e: &mut MbEnc<'_>, mb_addr: usize, st: &MbState) { | ||
| 407 | + let (mb_x, mb_y) = (mb_addr % pic.mb_w, mb_addr / pic.mb_w); | ||
| 408 | + let (w4, w2) = (pic.mb_w * 4, pic.mb_w * 2); | ||
| 409 | + load_plane(&mut pic.y, mb_x * 16, mb_y * 16, &st.y, 16); | ||
| 410 | + load_plane(&mut pic.cb, mb_x * 8, mb_y * 8, &st.c[0], 8); | ||
| 411 | + load_plane(&mut pic.cr, mb_x * 8, mb_y * 8, &st.c[1], 8); | ||
| 412 | + for row in 0..4 { | ||
| 413 | + let b = (mb_y * 4 + row) * w4 + mb_x * 4; | ||
| 414 | + for col in 0..4 { | ||
| 415 | + let (i, j) = (row * 4 + col, b + col); | ||
| 416 | + pic.nz_y[j] = st.nz_y[i]; | ||
| 417 | + pic.i4_modes[j] = st.i4_modes[i]; | ||
| 418 | + pic.mv[j] = st.mv[i]; | ||
| 419 | + pic.ref_idx[j] = st.ref_idx[i]; | ||
| 420 | + pic.ref_id[j] = st.ref_id[i]; | ||
| 421 | + pic.mvd_abs[j] = st.mvd_abs[i]; | ||
| 422 | + pic.blk[j] = st.blk[i]; | ||
| 423 | + } | ||
| 424 | + } | ||
| 425 | + for comp in 0..2 { | ||
| 426 | + for row in 0..2 { | ||
| 427 | + let b = (mb_y * 2 + row) * w2 + mb_x * 2; | ||
| 428 | + pic.nz_c[comp][b..b + 2].copy_from_slice(&st.nz_c[comp][row * 2..row * 2 + 2]); | ||
| 429 | + } | ||
| 430 | + } | ||
| 431 | + pic.mb_qp[mb_addr] = st.mb_qp; | ||
| 432 | + pic.mb_flags[mb_addr] = st.mb_flags; | ||
| 433 | + pic.mb_cbp[mb_addr] = st.mb_cbp; | ||
| 434 | + pic.mb_dc_cbf[mb_addr] = st.mb_dc_cbf; | ||
| 435 | + pic.mb_slice[mb_addr] = st.mb_slice; | ||
| 436 | + pic.decoded_mbs = st.decoded_mbs; | ||
| 437 | + e.qp = st.qp; | ||
| 438 | + e.qp_delta_inc = st.qp_delta_inc; | ||
| 439 | +} | ||
| 440 | + | ||
| 319 | /// Code one macroblock: decide its mode, reconstruct it into `pic` and write | 441 | /// Code one macroblock: decide its mode, reconstruct it into `pic` and write |
| 320 | /// its syntax into `w`. | 442 | /// its syntax into `w`. |
| 321 | pub(crate) fn encode_mb(pic: &mut Picture, e: &mut MbEnc<'_>, w: &mut EncEntropy, mb_addr: usize) { | 443 | pub(crate) fn encode_mb(pic: &mut Picture, e: &mut MbEnc<'_>, w: &mut EncEntropy, mb_addr: usize) { |
| @@ -355,72 +477,75 @@ pub(crate) fn encode_mb(pic: &mut Picture, e: &mut MbEnc<'_>, w: &mut EncEntropy | |||
| 355 | inter = Some(choose_inter(pic, e, mb_x, mb_y)); | 477 | inter = Some(choose_inter(pic, e, mb_x, mb_y)); |
| 356 | } | 478 | } |
| 357 | 479 | ||
| 358 | - // The intra side of the comparison carries what going intra signals: the | 480 | + // Intra against inter is decided on real coded cost: both branches are |
| 359 | - // inter side already pays for its motion vector, and an intra macroblock | 481 | + // coded, and the one with the smaller squared error plus lambda times its |
| 360 | - // in a P picture pays for a macroblock type plus sixteen prediction modes | 482 | + // actual bits is kept. The prediction-domain comparison this replaced |
| 361 | - // that the SATD of its prediction says nothing about. | 483 | + // compared an intra SATD, discounted by a constant, against a coded inter |
| 484 | + // cost, and paid for the intra side's signalling with a guessed twelve | ||
| 485 | + // bits -- three approximations where the encoder can simply measure. On | ||
| 486 | + // the library, BD-PSNR against x264 at GOP 10 over QP 22/26/30/34: | ||
| 362 | // | 487 | // |
| 363 | - // Swept on two clips of the library and the synthetic one, BD-PSNR against | 488 | + // film 3840x1608 screen 2560x1440 synthetic |
| 364 | - // x264 at GOP 10 over QP 22/26/30/34: | 489 | + // SATD and guess -1.043 -1.064 -4.449 |
| 490 | + // coded cost -0.728 -0.500 -4.523 | ||
| 365 | // | 491 | // |
| 366 | - // bits film 3840x1608 screen 2560x1440 synthetic | 492 | + // and the bit premium over x264 falls with it: on the screen capture from |
| 367 | - // 0 -1.521 -1.904 -5.718 | 493 | + // +34.4% to +17.2% at QP 22. The synthetic clip loses 0.074 dB; it is |
| 368 | - // 4 -1.305 -1.290 -5.400 | 494 | + // 352x288 of moving synthetic shapes and it prefers a heavier rate term |
| 369 | - // 8 -1.223 -0.920 -5.290 | 495 | + // (scaling lambda by 1.4 reads -4.310 there and -0.744/-0.477 on the two |
| 370 | - // 12 -1.211 -0.733 -5.286 | 496 | + // real clips), which is not a reason to put an unmeasured constant in |
| 371 | - // 16 -1.267 -0.650 -5.262 | 497 | + // front of the standard multiplier. |
| 372 | - // 32 -1.649 -0.586 -- | ||
| 373 | // | 498 | // |
| 374 | - // Screen capture keeps improving to 48 and film falls off after 16, so | 499 | + // The second encode costs about two seconds on a run that took 28.8: each |
| 375 | - // twelve is the overlap of the two flat parts rather than either optimum. | 500 | + // P macroblock is coded twice, and the loser's reconstruction and entropy |
| 376 | - // A previous sweep of this same constant landed zero, on a measurement | 501 | + // writer are thrown away. |
| 377 | - // that sampled one picture a second: at that spacing nothing is | ||
| 378 | - // predictable from the previous picture, P slices went 93% intra on film, | ||
| 379 | - // and the penalty had nothing to suppress. The sampler now takes | ||
| 380 | - // consecutive pictures. | ||
| 381 | - // | ||
| 382 | - // Skipping the intra trial when the inter cost is already low was measured | ||
| 383 | - // and dropped: it cost 0.7 dB on screen capture and bought no measurable | ||
| 384 | - // time. | ||
| 385 | let intra_cost = intra_pre_cost(pic, e, &nbr, mb_x, mb_y); | 502 | let intra_cost = intra_pre_cost(pic, e, &nbr, mb_x, mb_y); |
| 386 | - let go_intra = match &inter { | 503 | + let Some(choice) = inter else { |
| 387 | - None => true, | 504 | + encode_intra_mb(pic, e, w, mb_addr, nbr, intra_cost); |
| 388 | - Some(i) => intra_cost.best() + motion_rate(e, INTRA_MODE_BITS) < i.cost, | 505 | + return; |
| 389 | }; | 506 | }; |
| 390 | 507 | ||
| 391 | - if go_intra { | 508 | + let lambda = if e.lambda_standard { |
| 392 | - encode_intra_mb(pic, e, w, mb_addr, nbr, intra_cost); | 509 | + e.lambda |
| 393 | } else { | 510 | } else { |
| 394 | - let choice = inter.expect("a P macroblock has an inter choice"); | 511 | + lambda_ssd(e.qp) |
| 395 | - encode_inter_mb(pic, e, w, mb_addr, nbr, choice); | 512 | + }; |
| 513 | + let before = save_mb_state(pic, e, mb_addr); | ||
| 514 | + let mut w_inter = w.clone(); | ||
| 515 | + let inter_bits = { | ||
| 516 | + let start = w_inter.bit_len(); | ||
| 517 | + encode_inter_mb(pic, e, &mut w_inter, mb_addr, nbr, choice); | ||
| 518 | + (w_inter.bit_len() - start) as f64 | ||
| 519 | + }; | ||
| 520 | + let inter_rd = ssd_mb(pic, e, mb_x, mb_y) as f64 + lambda * inter_bits; | ||
| 521 | + let after_inter = save_mb_state(pic, e, mb_addr); | ||
| 522 | + load_mb_state(pic, e, mb_addr, &before); | ||
| 523 | + | ||
| 524 | + let mut w_intra = w.clone(); | ||
| 525 | + let intra_bits = { | ||
| 526 | + let start = w_intra.bit_len(); | ||
| 527 | + encode_intra_mb(pic, e, &mut w_intra, mb_addr, nbr, intra_cost); | ||
| 528 | + (w_intra.bit_len() - start) as f64 | ||
| 529 | + }; | ||
| 530 | + let intra_rd = ssd_mb(pic, e, mb_x, mb_y) as f64 + lambda * intra_bits; | ||
| 531 | + | ||
| 532 | + if intra_rd < inter_rd { | ||
| 533 | + *w = w_intra; | ||
| 534 | + } else { | ||
| 535 | + load_mb_state(pic, e, mb_addr, &after_inter); | ||
| 536 | + *w = w_inter; | ||
| 396 | } | 537 | } |
| 397 | } | 538 | } |
| 398 | 539 | ||
| 399 | -/// The Intra_16x16 mode this macroblock would use, and what it would cost: | 540 | +/// The Intra_16x16 mode this macroblock would use. Intra_16x16 against |
| 400 | -/// the *inter or intra* decision needs a number before anything is coded, and | 541 | +/// Intra_4x4 is decided later, on real rate and real distortion (see |
| 401 | -/// this is the cheap one. Intra_16x16 against Intra_4x4 is decided later, on | 542 | +/// [`encode_intra_mb`]). |
| 402 | -/// real rate and real distortion (see [`encode_intra_mb`]). | ||
| 403 | struct IntraCost { | 543 | struct IntraCost { |
| 404 | i16_mode: u8, | 544 | i16_mode: u8, |
| 405 | - i16_cost: i32, | ||
| 406 | /// True when Intra_4x4 may be used at all for this picture and preset. | 545 | /// True when Intra_4x4 may be used at all for this picture and preset. |
| 407 | allow_i4: bool, | 546 | allow_i4: bool, |
| 408 | } | 547 | } |
| 409 | 548 | ||
| 410 | -impl IntraCost { | ||
| 411 | - fn best(&self) -> i32 { | ||
| 412 | - // Intra_4x4 usually beats this when it is allowed; the margin below is | ||
| 413 | - // what keeps a P macroblock from going intra on the 16x16 cost alone. | ||
| 414 | - if self.allow_i4 { | ||
| 415 | - self.i16_cost * 3 / 4 | ||
| 416 | - } else { | ||
| 417 | - self.i16_cost | ||
| 418 | - } | ||
| 419 | - } | ||
| 420 | -} | ||
| 421 | - | ||
| 422 | - | ||
| 423 | - | ||
| 424 | /// Best Intra_16x16 mode by SATD, leaving nothing behind in the plane that a | 549 | /// Best Intra_16x16 mode by SATD, leaving nothing behind in the plane that a |
| 425 | /// later branch does not overwrite. | 550 | /// later branch does not overwrite. |
| 426 | fn intra_pre_cost( | 551 | fn intra_pre_cost( |
| @@ -457,14 +582,14 @@ fn intra_pre_cost( | |||
| 457 | } | 582 | } |
| 458 | IntraCost { | 583 | IntraCost { |
| 459 | i16_mode: best.1, | 584 | i16_mode: best.1, |
| 460 | - i16_cost: best.0, | ||
| 461 | // Intra_4x4 is tried in P pictures too, at every preset. Re-measured | 585 | // Intra_4x4 is tried in P pictures too, at every preset. Re-measured |
| 462 | - // on consecutive pictures (BD-PSNR against x264, GOP 10, QP | 586 | + // once the intra-versus-inter decision became a real one (BD-PSNR |
| 463 | - // 22/26/30/34): restricting it to I pictures reads -1.989 dB on the | 587 | + // against x264, GOP 10, QP 22/26/30/34): restricting it to I pictures |
| 464 | - // film clip where allowing it everywhere reads -1.154, and the screen | 588 | + // reads -1.613 dB on the film clip and -0.508 on the screen capture, |
| 465 | - // capture disagrees by a third of that in the other direction (-0.564 | 589 | + // where allowing it everywhere reads -0.728 and -0.500. It stays on |
| 466 | - // against -0.696). The film margin is the larger of the two, so it | 590 | + // everywhere. Under the old prediction-domain decision the screen |
| 467 | - // stays on everywhere. | 591 | + // capture preferred the restriction by a third of a dB; coding both |
| 592 | + // branches for real removed that disagreement. | ||
| 468 | allow_i4: true, | 593 | allow_i4: true, |
| 469 | } | 594 | } |
| 470 | } | 595 | } |
| @@ -477,10 +602,6 @@ fn intra_pre_cost( | |||
| 477 | /// below a half and falls away above it. | 602 | /// below a half and falls away above it. |
| 478 | const MV_BITS_WEIGHT: f64 = 0.5; | 603 | const MV_BITS_WEIGHT: f64 = 0.5; |
| 479 | 604 | ||
| 480 | -/// What going intra costs to signal in a P picture, in bits: the macroblock | ||
| 481 | -/// type and, when Intra_4x4 wins later, sixteen prediction modes. Swept at the | ||
| 482 | -/// decision site above. | ||
| 483 | -const INTRA_MODE_BITS: f64 = 12.0; | ||
| 484 | 605 | ||
| 485 | /// How a P macroblock is partitioned, in the mb_type numbering of Table 7-13. | 606 | /// How a P macroblock is partitioned, in the mb_type numbering of Table 7-13. |
| 486 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] | 607 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| @@ -534,7 +655,6 @@ struct InterChoice { | |||
| 534 | mv: [[i16; 2]; 2], | 655 | mv: [[i16; 2]; 2], |
| 535 | /// The P_Skip motion vector of 8.4.1.1. | 656 | /// The P_Skip motion vector of 8.4.1.1. |
| 536 | skip_mv: [i16; 2], | 657 | skip_mv: [i16; 2], |
| 537 | - cost: i32, | ||
| 538 | /// The skip candidate is at least as good as the searched one. | 658 | /// The skip candidate is at least as good as the searched one. |
| 539 | prefer_skip: bool, | 659 | prefer_skip: bool, |
| 540 | } | 660 | } |
| @@ -731,7 +851,6 @@ fn choose_inter(pic: &Picture, e: &MbEnc<'_>, mb_x: usize, mb_y: usize) -> Inter | |||
| 731 | shape: best.0, | 851 | shape: best.0, |
| 732 | mv: best.1, | 852 | mv: best.1, |
| 733 | skip_mv, | 853 | skip_mv, |
| 734 | - cost: best.2.min(skip_cost), | ||
| 735 | prefer_skip: best.0 == PShape::Whole && skip_cost <= best.2, | 854 | prefer_skip: best.0 == PShape::Whole && skip_cost <= best.2, |
| 736 | } | 855 | } |
| 737 | } | 856 | } |
crates/ec-h264/tests/encode.rsmodified+12 -15
| @@ -878,25 +878,22 @@ fn real_clip_t8x8_bd_psnr() { | |||
| 878 | // every reference and our encoder against nothing but itself. | 878 | // every reference and our encoder against nothing but itself. |
| 879 | // --------------------------------------------------------------------------- | 879 | // --------------------------------------------------------------------------- |
| 880 | 880 | ||
| 881 | -/// Floor for [`bd_psnr_vs_x264`] on the synthetic clip. Our encoder measured | 881 | +/// Floor for [`bd_psnr_vs_x264`] on the synthetic clip. Our encoder measures |
| 882 | -/// -5.702 dB against x264 there on 2026-08-23 (352x288, 24 pictures of | 882 | +/// -4.523 dB against x264 there (352x288, 24 pictures of |
| 883 | /// `Clip::render_smooth`, both encoders at their own default effort: our | 883 | /// `Clip::render_smooth`, both encoders at their own default effort: our |
| 884 | /// `Preset::Fast`, x264's `-preset medium`). The floor is that number with | 884 | /// `Preset::Fast`, x264's `-preset medium`). The floor is that number with |
| 885 | /// room for platform noise, so the distance can shrink but not grow | 885 | /// room for platform noise, so the distance can shrink but not grow |
| 886 | -/// unnoticed; it is not a claim that -5.7 dB is fine. On a 3840x1608 clip | 886 | +/// unnoticed; it is not a claim that -4.5 dB is fine. On a 3840x1608 clip |
| 887 | -/// from the library the same measurement reads -0.233 dB and on a 2560x1440 | 887 | +/// from the library the same measurement reads -0.728 dB and on a 2560x1440 |
| 888 | -/// screen capture +0.068 dB, so this synthetic number is the pessimistic end | 888 | +/// screen capture -0.500, so this synthetic number is the pessimistic end of |
| 889 | -/// of the range and not the typical one. | 889 | +/// the range and not the typical one. |
| 890 | /// | 890 | /// |
| 891 | -/// It moved -5.40 -> -5.75 when the intra-versus-inter penalty came off. That | 891 | +/// It moved -4.449 -> -4.523 when the intra-versus-inter decision started |
| 892 | -/// change is worth 1.17 dB on the film clip and takes fewer bits at every QP | 892 | +/// coding both branches for real. That change is worth 0.315 dB on the film |
| 893 | -/// while doing it; here it spends 5% more bits for the same quality, because | 893 | +/// clip and 0.564 on the screen capture, and takes fewer bits at every QP |
| 894 | -/// this clip's smooth ramps are what inter prediction is best at and any intra | 894 | +/// while doing it. This clip prefers a heavier rate term in that decision |
| 895 | -/// macroblock in a P picture is waste. The two contents disagreeing is the | 895 | +/// (see the sweep in `enc/mb.rs`), which the two real clips do not. |
| 896 | -/// signal that the decision belongs on real coded cost rather than on a SATD | 896 | +const BD_PSNR_VS_X264_FLOOR: f64 = -4.6; |
| 897 | -/// comparison at all -- that is the next slice, not a reason to tune the | ||
| 898 | -/// threshold back toward this clip. | ||
| 899 | -const BD_PSNR_VS_X264_FLOOR: f64 = -4.5; | ||
| 900 | 897 | ||
| 901 | fn have_x264() -> bool { | 898 | fn have_x264() -> bool { |
| 902 | std::process::Command::new("ffmpeg") | 899 | std::process::Command::new("ffmpeg") |
lanes/h264x264-r2.report.mdmodified+65 -0
| @@ -146,3 +146,68 @@ The search's motion-vector weight was swept with it and held at a half: | |||
| 146 | -0.676/-0.661/-0.795/-1.201 on screen capture. Charging a vector's whole | 146 | -0.676/-0.661/-0.795/-1.201 on screen capture. Charging a vector's whole |
| 147 | signalling cost is worse than charging half on both contents; it now says so in | 147 | signalling cost is worse than charging half on both contents; it now says so in |
| 148 | a named constant rather than a bare `/ 2.0`. | 148 | a named constant rather than a bare `/ 2.0`. |
| 149 | + | ||
| 150 | +## Addendum: the intra-versus-inter decision on real coded cost | ||
| 151 | + | ||
| 152 | +The attribution in this report put 0.83 dB (film) and 0.45 dB (screen capture) | ||
| 153 | +behind x264's sub-sample and mode refinement, and the sub-sample half landed in | ||
| 154 | +the previous slice. This is the mode half: the intra-versus-inter decision now | ||
| 155 | +codes both branches for real and keeps the one with the smaller squared error | ||
| 156 | +plus lambda times its actual bits, which is what x264 does from `subme 6` up. | ||
| 157 | + | ||
| 158 | +What it replaced compared an Intra_16x16 SATD, discounted by a fixed 3/4 for | ||
| 159 | +the Intra_4x4 that usually follows, against a coded inter cost, and paid for | ||
| 160 | +the intra side's signalling with a guessed twelve bits. Three approximations | ||
| 161 | +where the encoder can measure instead. | ||
| 162 | + | ||
| 163 | +BD-PSNR against x264 at GOP 10 over QP 22/26/30/34, both real clips sampled | ||
| 164 | +consecutively from picture 1500: | ||
| 165 | + | ||
| 166 | +| | film 3840x1608 | screen 2560x1440 | synthetic 352x288 | | ||
| 167 | +|--------------------|----------------|------------------|-------------------| | ||
| 168 | +| SATD and a guess | -1.043 | -1.064 | -4.449 | | ||
| 169 | +| real coded cost | **-0.728** | **-0.500** | -4.523 | | ||
| 170 | + | ||
| 171 | +The bit premium over x264 falls with the distance rather than being traded for | ||
| 172 | +it: the screen capture went from +34.4% to +17.2% bits at QP 22 and from +15.9% | ||
| 173 | +to +9.7% at QP 34; the film clip from +12.8% to +12.2% at QP 22. | ||
| 174 | + | ||
| 175 | +Cost: each P macroblock is coded twice and the loser's reconstruction and | ||
| 176 | +entropy writer are thrown away. The screen-capture measurement run went 28.8 s | ||
| 177 | +to 30.8 s wall, x264 and the ffmpeg decodes included. | ||
| 178 | + | ||
| 179 | +### Lambda in that decision was swept, not assumed | ||
| 180 | + | ||
| 181 | +| lambda scale | film | screen | synthetic | | ||
| 182 | +|--------------|--------|--------|-----------| | ||
| 183 | +| 0.5 | -- | -- | -5.398 | | ||
| 184 | +| 0.7 | -- | -- | -4.872 | | ||
| 185 | +| **1.0** | -0.728 | -0.500 | -4.523 | | ||
| 186 | +| 1.4 | -0.744 | -0.477 | -4.310 | | ||
| 187 | +| 2.0 | -0.778 | -0.483 | -4.300 | | ||
| 188 | +| 3.0 | -0.835 | -0.494 | -- | | ||
| 189 | + | ||
| 190 | +The two real clips are flat between 1.0 and 1.4 and fall off above it; the | ||
| 191 | +synthetic clip keeps improving to 2.0. Unscaled is the film optimum, within | ||
| 192 | +0.023 dB of the screen capture's, and it puts no unmeasured constant in front | ||
| 193 | +of the standard multiplier. The synthetic clip's 0.074 dB is the price, and the | ||
| 194 | +floor in `bd_psnr_vs_x264` moved -4.5 to -4.6 to hold the new number. | ||
| 195 | + | ||
| 196 | +### Constants re-swept under the new decision | ||
| 197 | + | ||
| 198 | +`allow_i4` in P pictures: on everywhere still. Restricting Intra_4x4 to I | ||
| 199 | +pictures reads -1.613 (film) and -0.508 (screen capture) against -0.728 and | ||
| 200 | +-0.500 with it on. Under the old decision the screen capture preferred the | ||
| 201 | +restriction by a third of a dB; coding both branches for real removed that | ||
| 202 | +disagreement, which is the same class of finding as the sampler: a constant | ||
| 203 | +tuned against a proxy measurement inverts when the proxy goes away. | ||
| 204 | + | ||
| 205 | +Two constants of the old decision are gone rather than re-swept, because | ||
| 206 | +nothing reads them any more: the twelve-bit intra signalling estimate and the | ||
| 207 | +3/4 Intra_4x4 discount. | ||
| 208 | + | ||
| 209 | +### What is left | ||
| 210 | + | ||
| 211 | +Rate-distortion quantisation (x264's trellis) is worth 0.20 dB on film and 0.10 | ||
| 212 | +on the screen capture by the ablation in this report, and we still have none. | ||
| 213 | +That is the next slice. | ||