A split-transform rect strip must still stamp its palette neighbour state
Browse files at this commit1b23037c1c3749ce7e227c01ed3cbd612590e12cParent: 2b5b4fa- fix
- a split-transform rect strip must still stamp its palette neighbour state
1 changed file +67 -23
- crates/ec-av1/src/decode.rs +67 -23
crates/ec-av1/src/decode.rsmodified+67 -23
| @@ -1049,6 +1049,38 @@ thread_local! { | |||
| 1049 | const { std::cell::RefCell::new(None) }; | 1049 | const { std::cell::RefCell::new(None) }; |
| 1050 | } | 1050 | } |
| 1051 | 1051 | ||
| 1052 | + | ||
| 1053 | +/// Arms the pending [`PALETTE_PRED`] override for the next `reconstruct` | ||
| 1054 | +/// (`set`) / consumes it (`take`). Set and take MUST pair: a buffer armed at a | ||
| 1055 | +/// site whose reconstruct never runs would be picked up by the NEXT block's | ||
| 1056 | +/// reconstruct (class [[refusal-hides-a-defect]] / stale thread-local), so | ||
| 1057 | +/// `EC_DEBUG_PAL=1` traces every arm and every consumption with its call site. | ||
| 1058 | +#[track_caller] | ||
| 1059 | +fn set_palette_pred(buf: Vec<u16>) { | ||
| 1060 | + if std::env::var_os("EC_DEBUG_PAL").is_some() { | ||
| 1061 | + let stale = PALETTE_PRED.with(|c| c.borrow().is_some()); | ||
| 1062 | + eprintln!( | ||
| 1063 | + "PALSET len={} stale={stale} at {}", | ||
| 1064 | + buf.len(), | ||
| 1065 | + std::panic::Location::caller() | ||
| 1066 | + ); | ||
| 1067 | + } | ||
| 1068 | + PALETTE_PRED.with(|c| *c.borrow_mut() = Some(buf)); | ||
| 1069 | +} | ||
| 1070 | + | ||
| 1071 | +#[track_caller] | ||
| 1072 | +fn take_palette_pred() -> Option<Vec<u16>> { | ||
| 1073 | + let got = PALETTE_PRED.with(|c| c.borrow_mut().take()); | ||
| 1074 | + if std::env::var_os("EC_DEBUG_PAL").is_some() { | ||
| 1075 | + eprintln!( | ||
| 1076 | + "PALTAKE len={:?} at {}", | ||
| 1077 | + got.as_ref().map(Vec::len), | ||
| 1078 | + std::panic::Location::caller() | ||
| 1079 | + ); | ||
| 1080 | + } | ||
| 1081 | + got | ||
| 1082 | +} | ||
| 1083 | + | ||
| 1052 | thread_local! { | 1084 | thread_local! { |
| 1053 | /// The current frame's `mi` grid, kept only while a frame header set | 1085 | /// The current frame's `mi` grid, kept only while a frame header set |
| 1054 | /// `allow_intrabc` -- an intrabc block's DV predictor is the ordinary | 1086 | /// `allow_intrabc` -- an intrabc block's DV predictor is the ordinary |
| @@ -4178,7 +4210,7 @@ fn decode_rect_split( | |||
| 4178 | buf[base..base + tx].iter().copied() | 4210 | buf[base..base + tx].iter().copied() |
| 4179 | }) | 4211 | }) |
| 4180 | .collect(); | 4212 | .collect(); |
| 4181 | - PALETTE_PRED.with(|c| *c.borrow_mut() = Some(tu_buf)); | 4213 | + set_palette_pred(tu_buf); |
| 4182 | } | 4214 | } |
| 4183 | if m.skip { | 4215 | if m.skip { |
| 4184 | y.reconstruct( | 4216 | y.reconstruct( |
| @@ -4232,14 +4264,14 @@ fn decode_rect_split( | |||
| 4232 | let (u_grid, v_grid) = if m.skip { | 4264 | let (u_grid, v_grid) = if m.skip { |
| 4233 | let zero = vec![0i32; chroma_w * chroma_h]; | 4265 | let zero = vec![0i32; chroma_w * chroma_h]; |
| 4234 | if let Some((ub, _)) = &m.palette_uv { | 4266 | if let Some((ub, _)) = &m.palette_uv { |
| 4235 | - PALETTE_PRED.with(|c| *c.borrow_mut() = Some(ub.clone())); | 4267 | + set_palette_pred(ub.clone()); |
| 4236 | } | 4268 | } |
| 4237 | u.reconstruct_rect( | 4269 | u.reconstruct_rect( |
| 4238 | cpx, cpy, chroma_w, chroma_h, m.uv_predict_mode, m.angle_delta_uv, reach, &zero, | 4270 | cpx, cpy, chroma_w, chroma_h, m.uv_predict_mode, m.angle_delta_uv, reach, &zero, |
| 4239 | m.alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), None, m.smooth_neighbor_uv, | 4271 | m.alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), None, m.smooth_neighbor_uv, |
| 4240 | ); | 4272 | ); |
| 4241 | if let Some((_, vb)) = &m.palette_uv { | 4273 | if let Some((_, vb)) = &m.palette_uv { |
| 4242 | - PALETTE_PRED.with(|c| *c.borrow_mut() = Some(vb.clone())); | 4274 | + set_palette_pred(vb.clone()); |
| 4243 | } | 4275 | } |
| 4244 | v.reconstruct_rect( | 4276 | v.reconstruct_rect( |
| 4245 | cpx, cpy, chroma_w, chroma_h, m.uv_predict_mode, m.angle_delta_uv, reach, &zero, | 4277 | cpx, cpy, chroma_w, chroma_h, m.uv_predict_mode, m.angle_delta_uv, reach, &zero, |
| @@ -4289,7 +4321,7 @@ fn decode_rect_split( | |||
| 4289 | let plane = if plane_idx == 1 { &mut *u } else { &mut *v }; | 4321 | let plane = if plane_idx == 1 { &mut *u } else { &mut *v }; |
| 4290 | if let Some((ub, vb)) = &m.palette_uv { | 4322 | if let Some((ub, vb)) = &m.palette_uv { |
| 4291 | let buf = if plane_idx == 1 { ub } else { vb }; | 4323 | let buf = if plane_idx == 1 { ub } else { vb }; |
| 4292 | - PALETTE_PRED.with(|c| *c.borrow_mut() = Some(buf.clone())); | 4324 | + set_palette_pred(buf.clone()); |
| 4293 | } | 4325 | } |
| 4294 | plane.reconstruct_rect( | 4326 | plane.reconstruct_rect( |
| 4295 | cpx, | 4327 | cpx, |
| @@ -4447,6 +4479,18 @@ fn decode_block_rect( | |||
| 4447 | decode_rect_split( | 4479 | decode_rect_split( |
| 4448 | dec, cdfs, neighbours, at, bw, bh, tx, &modes, y, u, v, base_q_idx, reduced_tx_set, | 4480 | dec, cdfs, neighbours, at, bw, bh, tx, &modes, y, u, v, base_q_idx, reduced_tx_set, |
| 4449 | )?; | 4481 | )?; |
| 4482 | + // This early return skips this fn's own tail, so the palette | ||
| 4483 | + // neighbour state has to be stamped HERE too: without it a split-tx | ||
| 4484 | + // strip left the above/left palette arrays holding a DEAD block's | ||
| 4485 | + // colours, and the next block's colour cache | ||
| 4486 | + // ([`Neighbours::palette_uv_cache`]) then hit a cached entry with the | ||
| 4487 | + // wrong VALUE -- same bit count, so the entropy stream stayed in sync | ||
| 4488 | + // and only those pixels were wrong (lane-palette2 r10). | ||
| 4489 | + let (py_size, py_colors) = palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors)); | ||
| 4490 | + neighbours.record_palette_y_rect(at, bw, bh, py_size, py_colors); | ||
| 4491 | + let (puv_size, puv_colors) = | ||
| 4492 | + palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors)); | ||
| 4493 | + neighbours.record_palette_uv_rect(at, bw, bh, puv_size, puv_colors); | ||
| 4450 | RECT_PARTITION_HITS.with(|c| c.set(c.get() + 1)); | 4494 | RECT_PARTITION_HITS.with(|c| c.set(c.get() + 1)); |
| 4451 | return Ok(()); | 4495 | return Ok(()); |
| 4452 | } | 4496 | } |
| @@ -4467,7 +4511,7 @@ fn decode_block_rect( | |||
| 4467 | // right before each `reconstruct_rect` call below (lane-palette2 r4). | 4511 | // right before each `reconstruct_rect` call below (lane-palette2 r4). |
| 4468 | if let Some(ref pyv) = palette_y { | 4512 | if let Some(ref pyv) = palette_y { |
| 4469 | let buf: Vec<u16> = pyv.map.iter().map(|&idx| pyv.colors[idx as usize]).collect(); | 4513 | let buf: Vec<u16> = pyv.map.iter().map(|&idx| pyv.colors[idx as usize]).collect(); |
| 4470 | - PALETTE_PRED.with(|c| *c.borrow_mut() = Some(buf)); | 4514 | + set_palette_pred(buf); |
| 4471 | } | 4515 | } |
| 4472 | let palette_uv_bufs: Option<(Vec<u16>, Vec<u16>)> = palette_uv.as_ref().map(|puv| { | 4516 | let palette_uv_bufs: Option<(Vec<u16>, Vec<u16>)> = palette_uv.as_ref().map(|puv| { |
| 4473 | ( | 4517 | ( |
| @@ -4491,7 +4535,7 @@ fn decode_block_rect( | |||
| 4491 | ); | 4535 | ); |
| 4492 | let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); | 4536 | let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); |
| 4493 | if let Some((ub, _)) = &palette_uv_bufs { | 4537 | if let Some((ub, _)) = &palette_uv_bufs { |
| 4494 | - PALETTE_PRED.with(|c| *c.borrow_mut() = Some(ub.clone())); | 4538 | + set_palette_pred(ub.clone()); |
| 4495 | } | 4539 | } |
| 4496 | u.reconstruct_rect( | 4540 | u.reconstruct_rect( |
| 4497 | cpx, | 4541 | cpx, |
| @@ -4507,7 +4551,7 @@ fn decode_block_rect( | |||
| 4507 | smooth_neighbor_uv, | 4551 | smooth_neighbor_uv, |
| 4508 | ); | 4552 | ); |
| 4509 | if let Some((_, vb)) = &palette_uv_bufs { | 4553 | if let Some((_, vb)) = &palette_uv_bufs { |
| 4510 | - PALETTE_PRED.with(|c| *c.borrow_mut() = Some(vb.clone())); | 4554 | + set_palette_pred(vb.clone()); |
| 4511 | } | 4555 | } |
| 4512 | v.reconstruct_rect( | 4556 | v.reconstruct_rect( |
| 4513 | cpx, | 4557 | cpx, |
| @@ -4636,7 +4680,7 @@ fn decode_block_rect( | |||
| 4636 | ); | 4680 | ); |
| 4637 | let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); | 4681 | let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); |
| 4638 | if let Some((ub, _)) = &palette_uv_bufs { | 4682 | if let Some((ub, _)) = &palette_uv_bufs { |
| 4639 | - PALETTE_PRED.with(|c| *c.borrow_mut() = Some(ub.clone())); | 4683 | + set_palette_pred(ub.clone()); |
| 4640 | } | 4684 | } |
| 4641 | let u_default_tx = default_intra_tx_type(uv_predict_mode as u8); | 4685 | let u_default_tx = default_intra_tx_type(uv_predict_mode as u8); |
| 4642 | let u_skip_ctx = usize::from(around[1].0) + usize::from(around[1].1); | 4686 | let u_skip_ctx = usize::from(around[1].0) + usize::from(around[1].1); |
| @@ -4679,7 +4723,7 @@ fn decode_block_rect( | |||
| 4679 | smooth_neighbor_uv, | 4723 | smooth_neighbor_uv, |
| 4680 | ); | 4724 | ); |
| 4681 | if let Some((_, vb)) = &palette_uv_bufs { | 4725 | if let Some((_, vb)) = &palette_uv_bufs { |
| 4682 | - PALETTE_PRED.with(|c| *c.borrow_mut() = Some(vb.clone())); | 4726 | + set_palette_pred(vb.clone()); |
| 4683 | } | 4727 | } |
| 4684 | let v_default_tx = default_intra_tx_type(uv_predict_mode as u8); | 4728 | let v_default_tx = default_intra_tx_type(uv_predict_mode as u8); |
| 4685 | let v_skip_ctx = usize::from(around[2].0) + usize::from(around[2].1); | 4729 | let v_skip_ctx = usize::from(around[2].0) + usize::from(around[2].1); |
| @@ -5047,7 +5091,7 @@ fn decode_block_rect64( | |||
| 5047 | let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height); | 5091 | let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height); |
| 5048 | if let Some(ref pyv) = palette_y { | 5092 | if let Some(ref pyv) = palette_y { |
| 5049 | let buf: Vec<u16> = pyv.map.iter().map(|&idx| pyv.colors[idx as usize]).collect(); | 5093 | let buf: Vec<u16> = pyv.map.iter().map(|&idx| pyv.colors[idx as usize]).collect(); |
| 5050 | - PALETTE_PRED.with(|c| *c.borrow_mut() = Some(buf)); | 5094 | + set_palette_pred(buf); |
| 5051 | } | 5095 | } |
| 5052 | let palette_uv_bufs: Option<(Vec<u16>, Vec<u16>)> = palette_uv.as_ref().map(|puv| { | 5096 | let palette_uv_bufs: Option<(Vec<u16>, Vec<u16>)> = palette_uv.as_ref().map(|puv| { |
| 5053 | ( | 5097 | ( |
| @@ -5071,7 +5115,7 @@ fn decode_block_rect64( | |||
| 5071 | ); | 5115 | ); |
| 5072 | let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); | 5116 | let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); |
| 5073 | if let Some((ub, _)) = &palette_uv_bufs { | 5117 | if let Some((ub, _)) = &palette_uv_bufs { |
| 5074 | - PALETTE_PRED.with(|c| *c.borrow_mut() = Some(ub.clone())); | 5118 | + set_palette_pred(ub.clone()); |
| 5075 | } | 5119 | } |
| 5076 | u.reconstruct_rect( | 5120 | u.reconstruct_rect( |
| 5077 | cpx, | 5121 | cpx, |
| @@ -5087,7 +5131,7 @@ fn decode_block_rect64( | |||
| 5087 | smooth_neighbor_uv, | 5131 | smooth_neighbor_uv, |
| 5088 | ); | 5132 | ); |
| 5089 | if let Some((_, vb)) = &palette_uv_bufs { | 5133 | if let Some((_, vb)) = &palette_uv_bufs { |
| 5090 | - PALETTE_PRED.with(|c| *c.borrow_mut() = Some(vb.clone())); | 5134 | + set_palette_pred(vb.clone()); |
| 5091 | } | 5135 | } |
| 5092 | v.reconstruct_rect( | 5136 | v.reconstruct_rect( |
| 5093 | cpx, | 5137 | cpx, |
| @@ -5871,7 +5915,7 @@ impl PlaneBuf { | |||
| 5871 | // As [`Self::reconstruct`]'s own [`PALETTE_PRED`] override | 5915 | // As [`Self::reconstruct`]'s own [`PALETTE_PRED`] override |
| 5872 | // (lane-palette2 r4): a palette block's prediction is the | 5916 | // (lane-palette2 r4): a palette block's prediction is the |
| 5873 | // reconstructed colour-index map, needing no edge pixels at all. | 5917 | // reconstructed colour-index map, needing no edge pixels at all. |
| 5874 | - let prediction = if let Some(buf) = PALETTE_PRED.with(|c| c.borrow_mut().take()) { | 5918 | + let prediction = if let Some(buf) = take_palette_pred() { |
| 5875 | buf | 5919 | buf |
| 5876 | } else { | 5920 | } else { |
| 5877 | let (above, left, corner) = self.edges_rect(x, y, bw, bh, reach); | 5921 | let (above, left, corner) = self.edges_rect(x, y, bw, bh, reach); |
| @@ -5955,7 +5999,7 @@ impl PlaneBuf { | |||
| 5955 | // A palette block's own [`PALETTE_PRED`] override (lane-palette r2) | 5999 | // A palette block's own [`PALETTE_PRED`] override (lane-palette r2) |
| 5956 | // takes priority over `predict()`/`predict_filter_intra` entirely -- | 6000 | // takes priority over `predict()`/`predict_filter_intra` entirely -- |
| 5957 | // palette prediction needs no edge pixels at all. | 6001 | // palette prediction needs no edge pixels at all. |
| 5958 | - let prediction = if let Some(buf) = PALETTE_PRED.with(|c| c.borrow_mut().take()) { | 6002 | + let prediction = if let Some(buf) = take_palette_pred() { |
| 5959 | buf | 6003 | buf |
| 5960 | } else { | 6004 | } else { |
| 5961 | let (above, left, corner) = self.edges(x, y, side, reach); | 6005 | let (above, left, corner) = self.edges(x, y, side, reach); |
| @@ -6278,7 +6322,7 @@ fn decode_block( | |||
| 6278 | (yb, ub, vb) | 6322 | (yb, ub, vb) |
| 6279 | }); | 6323 | }); |
| 6280 | if let Some((yb, _, _)) = &intrabc_bufs { | 6324 | if let Some((yb, _, _)) = &intrabc_bufs { |
| 6281 | - PALETTE_PRED.with(|c| *c.borrow_mut() = Some(yb.clone())); | 6325 | + set_palette_pred(yb.clone()); |
| 6282 | } | 6326 | } |
| 6283 | // A palette-Y block's own prediction is the reconstructed colour-index | 6327 | // A palette-Y block's own prediction is the reconstructed colour-index |
| 6284 | // map, not `predict()`'s edge-based one -- [`PALETTE_PRED`] carries it | 6328 | // map, not `predict()`'s edge-based one -- [`PALETTE_PRED`] carries it |
| @@ -6302,7 +6346,7 @@ fn decode_block( | |||
| 6302 | if let Some(buf) = &palette_y_buf | 6346 | if let Some(buf) = &palette_y_buf |
| 6303 | && logical_tx == side | 6347 | && logical_tx == side |
| 6304 | { | 6348 | { |
| 6305 | - PALETTE_PRED.with(|c| *c.borrow_mut() = Some(buf.clone())); | 6349 | + set_palette_pred(buf.clone()); |
| 6306 | } | 6350 | } |
| 6307 | // A UV-palette block's own chroma prediction override (lane-palette2 r1), | 6351 | // A UV-palette block's own chroma prediction override (lane-palette2 r1), |
| 6308 | // same [`PALETTE_PRED`] slot as Y above -- set fresh right before each | 6352 | // same [`PALETTE_PRED`] slot as Y above -- set fresh right before each |
| @@ -6339,7 +6383,7 @@ fn decode_block( | |||
| 6339 | ); | 6383 | ); |
| 6340 | let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); | 6384 | let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); |
| 6341 | if let Some((ub, _)) = &palette_uv_bufs { | 6385 | if let Some((ub, _)) = &palette_uv_bufs { |
| 6342 | - PALETTE_PRED.with(|c| *c.borrow_mut() = Some(ub.clone())); | 6386 | + set_palette_pred(ub.clone()); |
| 6343 | } | 6387 | } |
| 6344 | u.reconstruct( | 6388 | u.reconstruct( |
| 6345 | cpx, | 6389 | cpx, |
| @@ -6354,7 +6398,7 @@ fn decode_block( | |||
| 6354 | smooth_neighbor_uv, | 6398 | smooth_neighbor_uv, |
| 6355 | ); | 6399 | ); |
| 6356 | if let Some((_, vb)) = &palette_uv_bufs { | 6400 | if let Some((_, vb)) = &palette_uv_bufs { |
| 6357 | - PALETTE_PRED.with(|c| *c.borrow_mut() = Some(vb.clone())); | 6401 | + set_palette_pred(vb.clone()); |
| 6358 | } | 6402 | } |
| 6359 | v.reconstruct( | 6403 | v.reconstruct( |
| 6360 | cpx, | 6404 | cpx, |
| @@ -6401,7 +6445,7 @@ fn decode_block( | |||
| 6401 | )?; | 6445 | )?; |
| 6402 | let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); | 6446 | let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); |
| 6403 | if let Some((ub, _)) = &palette_uv_bufs { | 6447 | if let Some((ub, _)) = &palette_uv_bufs { |
| 6404 | - PALETTE_PRED.with(|c| *c.borrow_mut() = Some(ub.clone())); | 6448 | + set_palette_pred(ub.clone()); |
| 6405 | } | 6449 | } |
| 6406 | let u_grid = read_plane( | 6450 | let u_grid = read_plane( |
| 6407 | dec, | 6451 | dec, |
| @@ -6432,7 +6476,7 @@ fn decode_block( | |||
| 6432 | ); | 6476 | ); |
| 6433 | } | 6477 | } |
| 6434 | if let Some((_, vb)) = &palette_uv_bufs { | 6478 | if let Some((_, vb)) = &palette_uv_bufs { |
| 6435 | - PALETTE_PRED.with(|c| *c.borrow_mut() = Some(vb.clone())); | 6479 | + set_palette_pred(vb.clone()); |
| 6436 | } | 6480 | } |
| 6437 | let v_grid = read_plane( | 6481 | let v_grid = read_plane( |
| 6438 | dec, | 6482 | dec, |
| @@ -6488,7 +6532,7 @@ fn decode_block( | |||
| 6488 | window[row * logical_tx..][..logical_tx] | 6532 | window[row * logical_tx..][..logical_tx] |
| 6489 | .copy_from_slice(&buf[src..][..logical_tx]); | 6533 | .copy_from_slice(&buf[src..][..logical_tx]); |
| 6490 | } | 6534 | } |
| 6491 | - PALETTE_PRED.with(|c| *c.borrow_mut() = Some(window)); | 6535 | + set_palette_pred(window); |
| 6492 | } | 6536 | } |
| 6493 | let tu_grid = read_plane( | 6537 | let tu_grid = read_plane( |
| 6494 | dec, | 6538 | dec, |
| @@ -6526,7 +6570,7 @@ fn decode_block( | |||
| 6526 | let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); | 6570 | let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); |
| 6527 | let chroma_around = neighbours.around(at, side); | 6571 | let chroma_around = neighbours.around(at, side); |
| 6528 | if let Some((ub, _)) = &palette_uv_bufs { | 6572 | if let Some((ub, _)) = &palette_uv_bufs { |
| 6529 | - PALETTE_PRED.with(|c| *c.borrow_mut() = Some(ub.clone())); | 6573 | + set_palette_pred(ub.clone()); |
| 6530 | } | 6574 | } |
| 6531 | let u_grid = read_plane( | 6575 | let u_grid = read_plane( |
| 6532 | dec, | 6576 | dec, |
| @@ -6551,7 +6595,7 @@ fn decode_block( | |||
| 6551 | smooth_neighbor_uv, | 6595 | smooth_neighbor_uv, |
| 6552 | )?; | 6596 | )?; |
| 6553 | if let Some((_, vb)) = &palette_uv_bufs { | 6597 | if let Some((_, vb)) = &palette_uv_bufs { |
| 6554 | - PALETTE_PRED.with(|c| *c.borrow_mut() = Some(vb.clone())); | 6598 | + set_palette_pred(vb.clone()); |
| 6555 | } | 6599 | } |
| 6556 | let v_grid = read_plane( | 6600 | let v_grid = read_plane( |
| 6557 | dec, | 6601 | dec, |