ec-vp8: two partition-0 desync bugs fixed (M2 debugging state)
Browse files at this commitd9a9866f50ac29ead3ce1b26025d7fa0e0e23d39Parent: a5b02e05 changed files +56 -50
- crates/ec-vp8/examples/dbg_first.rs +5 -33
- crates/ec-vp8/src/decode.rs +16 -6
- crates/ec-vp8/src/header.rs +19 -9
- crates/ec-vp8/src/modes.rs +14 -0
- crates/ec-vp8/tests/header_parse.rs +2 -2
crates/ec-vp8/examples/dbg_first.rsmodified+5 -33
| @@ -1,46 +1,18 @@ | |||
| 1 | -// Decode one frame: print header/dequant summary + pixel stats. | 1 | +// Decode frame 0 of an IVF with decision tracing (EC_VP8_TRACE=1). |
| 2 | use ec_vp8::decode::Decoder; | 2 | use ec_vp8::decode::Decoder; |
| 3 | -use ec_vp8::transform; | ||
| 4 | -use ec_vp8::{PersistedState, frame, header::FrameHeader}; | ||
| 5 | 3 | ||
| 6 | fn main() { | 4 | fn main() { |
| 7 | let path = std::env::args().nth(1).expect("ivf path"); | 5 | let path = std::env::args().nth(1).expect("ivf path"); |
| 8 | let bytes = std::fs::read(path).unwrap(); | 6 | let bytes = std::fs::read(path).unwrap(); |
| 9 | let sz = u32::from_le_bytes([bytes[32], bytes[33], bytes[34], bytes[35]]) as usize; | 7 | let sz = u32::from_le_bytes([bytes[32], bytes[33], bytes[34], bytes[35]]) as usize; |
| 10 | let frame = &bytes[44..44 + sz]; | 8 | let frame = &bytes[44..44 + sz]; |
| 11 | - let mut st = PersistedState::default(); | ||
| 12 | - let (h, _) = FrameHeader::parse(frame, &mut st).unwrap(); | ||
| 13 | - println!( | ||
| 14 | - "q={} y2dc_d={} y2ac_d={} skip={} lvl={}", | ||
| 15 | - h.quant.yac_qi, | ||
| 16 | - h.quant.y2dc_delta, | ||
| 17 | - h.quant.y2ac_delta, | ||
| 18 | - h.coeff_skip_enabled, | ||
| 19 | - h.filter_level | ||
| 20 | - ); | ||
| 21 | - let dq = transform::dequant( | ||
| 22 | - i32::from(h.quant.yac_qi), | ||
| 23 | - h.quant.ydc_delta, | ||
| 24 | - h.quant.y2dc_delta, | ||
| 25 | - h.quant.y2ac_delta, | ||
| 26 | - h.quant.uvdc_delta, | ||
| 27 | - h.quant.uvac_delta, | ||
| 28 | - ); | ||
| 29 | - println!( | ||
| 30 | - "y1dc={} y1ac={} y2dc={} y2ac={}", | ||
| 31 | - dq.y1_dc, dq.y1_ac, dq.y2_dc, dq.y2_ac | ||
| 32 | - ); | ||
| 33 | let mut dec = Decoder::new(); | 9 | let mut dec = Decoder::new(); |
| 34 | match dec.decode(frame) { | 10 | match dec.decode(frame) { |
| 35 | Ok(Some(pic)) => println!( | 11 | Ok(Some(pic)) => println!( |
| 36 | - "{}x{} y[0..8]={:?} min={} max={}", | 12 | + "OUT {}x{} y[0..8]={:?}", |
| 37 | - pic.width, | 13 | + pic.width, pic.height, &pic.y[..8] |
| 38 | - pic.height, | ||
| 39 | - &pic.y[..8], | ||
| 40 | - pic.y.iter().min().unwrap(), | ||
| 41 | - pic.y.iter().max().unwrap() | ||
| 42 | ), | 14 | ), |
| 43 | - Ok(None) => println!("hidden"), | 15 | + Ok(None) => println!("OUT hidden"), |
| 44 | - Err(e) => println!("ERR {e}"), | 16 | + Err(e) => println!("OUT ERR {e}"), |
| 45 | } | 17 | } |
| 46 | } | 18 | } |
crates/ec-vp8/src/decode.rsmodified+16 -6
| @@ -119,7 +119,9 @@ pub struct Picture { | |||
| 119 | pub u: Vec<u8>, | 119 | pub u: Vec<u8>, |
| 120 | /// V (Cr) plane, half resolution. | 120 | /// V (Cr) plane, half resolution. |
| 121 | pub v: Vec<u8>, | 121 | pub v: Vec<u8>, |
| 122 | + /// Frame width in pixels. | ||
| 122 | pub width: u16, | 123 | pub width: u16, |
| 124 | + /// Frame height in pixels. | ||
| 123 | pub height: u16, | 125 | pub height: u16, |
| 124 | /// Luma row pitch of the returned planes. | 126 | /// Luma row pitch of the returned planes. |
| 125 | pub stride: usize, | 127 | pub stride: usize, |
| @@ -167,9 +169,9 @@ impl Decoder { | |||
| 167 | /// Decode one frame; returns the picture when it is for display | 169 | /// Decode one frame; returns the picture when it is for display |
| 168 | /// (`show_frame`). Hidden frames still update the decode state. | 170 | /// (`show_frame`). Hidden frames still update the decode state. |
| 169 | pub fn decode(&mut self, frame: &[u8]) -> Result<Option<Picture>> { | 171 | pub fn decode(&mut self, frame: &[u8]) -> Result<Option<Picture>> { |
| 170 | - let (header, _part0) = FrameHeader::parse(frame, &mut self.state)?; | 172 | + let (header, hdr_dec) = FrameHeader::parse(frame, &mut self.state)?; |
| 171 | match header.tag.frame_type { | 173 | match header.tag.frame_type { |
| 172 | - FrameType::Key => self.decode_keyframe(frame, header), | 174 | + FrameType::Key => self.decode_keyframe(frame, header, hdr_dec), |
| 173 | FrameType::Inter => Err(Error::unsupported( | 175 | FrameType::Inter => Err(Error::unsupported( |
| 174 | "VP8 inter frame", | 176 | "VP8 inter frame", |
| 175 | "inter-frame decoding is the next milestone of this crate", | 177 | "inter-frame decoding is the next milestone of this crate", |
| @@ -209,7 +211,12 @@ impl Decoder { | |||
| 209 | &mut self.mb_info[row * w + col] | 211 | &mut self.mb_info[row * w + col] |
| 210 | } | 212 | } |
| 211 | 213 | ||
| 212 | - fn decode_keyframe(&mut self, frame: &[u8], header: FrameHeader) -> Result<Option<Picture>> { | 214 | + fn decode_keyframe( |
| 215 | + &mut self, | ||
| 216 | + frame: &[u8], | ||
| 217 | + header: FrameHeader, | ||
| 218 | + mut hdr_dec: BoolDecoder<'_>, | ||
| 219 | + ) -> Result<Option<Picture>> { | ||
| 213 | let kf = header.dims.expect("key frame header carries dimensions"); | 220 | let kf = header.dims.expect("key frame header carries dimensions"); |
| 214 | self.ensure_buffers(kf); | 221 | self.ensure_buffers(kf); |
| 215 | 222 | ||
| @@ -255,9 +262,6 @@ impl Decoder { | |||
| 255 | ); | 262 | ); |
| 256 | } | 263 | } |
| 257 | 264 | ||
| 258 | - let hdr_start = crate::frame::FRAME_TAG_SZ + crate::frame::KEYFRAME_HEADER_SZ; | ||
| 259 | - let mut hdr_dec = BoolDecoder::new(&frame[hdr_start..header.token_data_offset])?; | ||
| 260 | - | ||
| 261 | self.token_ctxs.reset_above_row(); | 265 | self.token_ctxs.reset_above_row(); |
| 262 | 266 | ||
| 263 | for row in 1..=self.mb_rows { | 267 | for row in 1..=self.mb_rows { |
| @@ -280,6 +284,9 @@ impl Decoder { | |||
| 280 | // `r` (0-based) reads partition `r % n` like the reference. | 284 | // `r` (0-based) reads partition `r % n` like the reference. |
| 281 | let part = (row - 1) % partitions.len(); | 285 | let part = (row - 1) % partitions.len(); |
| 282 | for col in 1..=self.mb_cols { | 286 | for col in 1..=self.mb_cols { |
| 287 | + if std::env::var_os("EC_VP8_TRACE").is_some() { | ||
| 288 | + eprintln!("M {} {}", row - 1, col - 1); | ||
| 289 | + } | ||
| 283 | let mb = self.mbi(row, col).clone(); | 290 | let mb = self.mbi(row, col).clone(); |
| 284 | let has_y2 = mb.has_y2(); | 291 | let has_y2 = mb.has_y2(); |
| 285 | let seg_i = usize::from(header.segmentation.enabled) * usize::from(mb.segment_id); | 292 | let seg_i = usize::from(header.segmentation.enabled) * usize::from(mb.segment_id); |
| @@ -292,6 +299,9 @@ impl Decoder { | |||
| 292 | y2_dc: dq.y2_dc, | 299 | y2_dc: dq.y2_dc, |
| 293 | y2_ac: dq.y2_ac, | 300 | y2_ac: dq.y2_ac, |
| 294 | }; | 301 | }; |
| 302 | + if std::env::var_os("EC_VP8_TRACE").is_some() { | ||
| 303 | + eprintln!("T {} {} hy2={}", row - 1, col - 1, has_y2); | ||
| 304 | + } | ||
| 295 | let probs = self.state.coeff_probs; | 305 | let probs = self.state.coeff_probs; |
| 296 | let coeffs = if mb.skip { | 306 | let coeffs = if mb.skip { |
| 297 | tokens::skip_mb_tokens(&mut self.token_ctxs, col - 1, has_y2) | 307 | tokens::skip_mb_tokens(&mut self.token_ctxs, col - 1, has_y2) |
crates/ec-vp8/src/header.rsmodified+19 -9
| @@ -192,7 +192,7 @@ impl FrameHeader { | |||
| 192 | pub fn parse<'a>( | 192 | pub fn parse<'a>( |
| 193 | data: &'a [u8], | 193 | data: &'a [u8], |
| 194 | state: &mut PersistedState, | 194 | state: &mut PersistedState, |
| 195 | - ) -> Result<(FrameHeader, &'a [u8])> { | 195 | + ) -> Result<(FrameHeader, BoolDecoder<'a>)> { |
| 196 | let tag = FrameTag::parse(data)?; | 196 | let tag = FrameTag::parse(data)?; |
| 197 | let key = tag.frame_type == FrameType::Key; | 197 | let key = tag.frame_type == FrameType::Key; |
| 198 | let mut off = crate::frame::FRAME_TAG_SZ; | 198 | let mut off = crate::frame::FRAME_TAG_SZ; |
| @@ -372,10 +372,16 @@ impl FrameHeader { | |||
| 372 | // --- entropy updates (§9.9; dixie decode_entropy_header) --- | 372 | // --- entropy updates (§9.9; dixie decode_entropy_header) --- |
| 373 | // Coefficient probability updates are read for every frame; on | 373 | // Coefficient probability updates are read for every frame; on |
| 374 | // key frames the tables were reset to defaults above. | 374 | // key frames the tables were reset to defaults above. |
| 375 | + let trace = std::env::var_os("EC_VP8_TRACE").is_some(); | ||
| 376 | + let mut idx = 0; | ||
| 375 | for i in 0..4 { | 377 | for i in 0..4 { |
| 376 | for j in 0..8 { | 378 | for j in 0..8 { |
| 377 | for k in 0..3 { | 379 | for k in 0..3 { |
| 378 | for l in 0..11 { | 380 | for l in 0..11 { |
| 381 | + if trace { | ||
| 382 | + eprintln!("IDX {idx} flag={}", crate::tables::COEFF_UPDATE_PROBS[i][j][k][l]); | ||
| 383 | + } | ||
| 384 | + idx += 1; | ||
| 379 | if d.read_bool(crate::tables::COEFF_UPDATE_PROBS[i][j][k][l]) { | 385 | if d.read_bool(crate::tables::COEFF_UPDATE_PROBS[i][j][k][l]) { |
| 380 | state.coeff_probs[i][j][k][l] = d.read_literal(8) as u8; | 386 | state.coeff_probs[i][j][k][l] = d.read_literal(8) as u8; |
| 381 | } | 387 | } |
| @@ -412,17 +418,18 @@ impl FrameHeader { | |||
| 412 | state.uv_mode_probs[i] = d.read_literal(8) as u8; | 418 | state.uv_mode_probs[i] = d.read_literal(8) as u8; |
| 413 | } | 419 | } |
| 414 | } | 420 | } |
| 421 | + // MV probability updates: P(7) form, x ? x << 1 : 1 (§17.2). | ||
| 422 | + for c in 0..2 { | ||
| 423 | + for j in 0..MV_PROB_CNT { | ||
| 424 | + if d.read_bool(MV_UPDATE_PROBS[c][j]) { | ||
| 425 | + state.mv_probs[c][j] = d.read_prob7(); | ||
| 426 | + } | ||
| 427 | + } | ||
| 428 | + } | ||
| 415 | probs | 429 | probs |
| 416 | } else { | 430 | } else { |
| 417 | (112, 86, 140) // placeholders; only read on inter frames | 431 | (112, 86, 140) // placeholders; only read on inter frames |
| 418 | }; | 432 | }; |
| 419 | - for c in 0..2 { | ||
| 420 | - for j in 0..MV_PROB_CNT { | ||
| 421 | - if d.read_bool(MV_UPDATE_PROBS[c][j]) { | ||
| 422 | - state.mv_probs[c][j] = d.read_prob7(); | ||
| 423 | - } | ||
| 424 | - } | ||
| 425 | - } | ||
| 426 | 433 | ||
| 427 | // The reference decoder zero-fills past the end of a partition; | 434 | // The reference decoder zero-fills past the end of a partition; |
| 428 | // tiny partitions (e.g. one-MB key frames) can legitimately | 435 | // tiny partitions (e.g. one-MB key frames) can legitimately |
| @@ -458,6 +465,9 @@ impl FrameHeader { | |||
| 458 | token_data_offset: part0_end, | 465 | token_data_offset: part0_end, |
| 459 | partition_sizes, | 466 | partition_sizes, |
| 460 | }; | 467 | }; |
| 461 | - Ok((header, &data[off..part0_end])) | 468 | + let _ = off; |
| 469 | + // The partition-0 decoder is returned mid-stream: the per-MB | ||
| 470 | + // mode records continue from exactly where the header stopped. | ||
| 471 | + Ok((header, d)) | ||
| 462 | } | 472 | } |
| 463 | } | 473 | } |
crates/ec-vp8/src/modes.rsmodified+14 -0
| @@ -181,17 +181,27 @@ pub enum MvRef { | |||
| 181 | } | 181 | } |
| 182 | 182 | ||
| 183 | /// mv_ref coding tree (RFC 6386 §16.2): leaves are [`MvRef`] integers. | 183 | /// mv_ref coding tree (RFC 6386 §16.2): leaves are [`MvRef`] integers. |
| 184 | +/// mv_ref coding tree (RFC 6386 §16.2); leaves are [`MvRef`] integers. | ||
| 184 | pub const MV_REF_TREE: [i8; 8] = [0, 2, -1, 4, -2, 6, -3, -4]; | 185 | pub const MV_REF_TREE: [i8; 8] = [0, 2, -1, 4, -2, 6, -3, -4]; |
| 185 | 186 | ||
| 186 | /// Motion-vector component: row then column, each with the 19-probability | 187 | /// Motion-vector component: row then column, each with the 19-probability |
| 187 | /// table (§17.1). | 188 | /// table (§17.1). |
| 189 | +/// Probability-table offsets of one MV component (RFC 6386 §17.1). | ||
| 188 | pub const MVP_IS_SHORT: usize = 0; | 190 | pub const MVP_IS_SHORT: usize = 0; |
| 191 | +/// Sign probability offset (§17.1). | ||
| 189 | pub const MVP_SIGN: usize = 1; | 192 | pub const MVP_SIGN: usize = 1; |
| 193 | +/// Short-value tree offset (§17.1). | ||
| 194 | +/// Short-value tree offset (§17.1). | ||
| 190 | pub const MVP_SHORT: usize = 2; | 195 | pub const MVP_SHORT: usize = 2; |
| 196 | +/// Long-value bit probabilities offset (§17.1). | ||
| 197 | +/// Long-value bit probabilities offset (§17.1). | ||
| 191 | pub const MVP_BITS: usize = MVP_SHORT + 7; | 198 | pub const MVP_BITS: usize = MVP_SHORT + 7; |
| 199 | +/// Total probabilities per MV component (§17.1). | ||
| 200 | +/// Total probabilities per MV component (§17.1). | ||
| 192 | pub const MVP_COUNT: usize = MVP_BITS + 10; | 201 | pub const MVP_COUNT: usize = MVP_BITS + 10; |
| 193 | 202 | ||
| 194 | /// Small-value coding tree for MV components (§17.1 `small_mvtree`). | 203 | /// Small-value coding tree for MV components (§17.1 `small_mvtree`). |
| 204 | +/// Small-value coding tree for MV components (§17.1). | ||
| 195 | pub const SMALL_MV_TREE: [i8; 14] = [ | 205 | pub const SMALL_MV_TREE: [i8; 14] = [ |
| 196 | 2, 8, // "0" / "1" subtrees | 206 | 2, 8, // "0" / "1" subtrees |
| 197 | 4, 6, // "00" / "01" | 207 | 4, 6, // "00" / "01" |
| @@ -206,6 +216,7 @@ pub const SMALL_MV_TREE: [i8; 14] = [ | |||
| 206 | /// RFC's `MVpartition` integers: 0 = top/bottom, 1 = left/right, | 216 | /// RFC's `MVpartition` integers: 0 = top/bottom, 1 = left/right, |
| 207 | /// 2 = quarters, 3 = MV_16. | 217 | /// 2 = quarters, 3 = MV_16. |
| 208 | pub const MV_PARTITION_TREE: [i8; 6] = [-3, 2, -2, 4, 0, -1]; | 218 | pub const MV_PARTITION_TREE: [i8; 6] = [-3, 2, -2, 4, 0, -1]; |
| 219 | +/// SPLITMV partition tree fixed probabilities (§16.4). | ||
| 209 | pub const MV_PARTITION_PROBS: [u8; 3] = [110, 111, 150]; | 220 | pub const MV_PARTITION_PROBS: [u8; 3] = [110, 111, 150]; |
| 210 | 221 | ||
| 211 | /// The four SPLITMV partitionings as subblock groups (§16.4), indexed | 222 | /// The four SPLITMV partitionings as subblock groups (§16.4), indexed |
| @@ -240,6 +251,8 @@ pub enum SubMvRef { | |||
| 240 | New4x4 = 3, | 251 | New4x4 = 3, |
| 241 | } | 252 | } |
| 242 | 253 | ||
| 254 | +/// Sub-MV coding tree (RFC 6386 §16.4); leaves are [`SubMvRef`] integers. | ||
| 255 | +/// Sub-MV coding tree (RFC 6386 §16.4); leaves are [`SubMvRef`] integers. | ||
| 243 | pub const SUB_MV_REF_TREE: [i8; 6] = [0, 2, -1, 4, -2, -3]; | 256 | pub const SUB_MV_REF_TREE: [i8; 6] = [0, 2, -1, 4, -2, -3]; |
| 244 | 257 | ||
| 245 | /// Context-conditioned sub-MV probabilities (§16.4 `sub_mv_ref_prob`). | 258 | /// Context-conditioned sub-MV probabilities (§16.4 `sub_mv_ref_prob`). |
| @@ -253,6 +266,7 @@ pub const SUB_MV_REF_PROBS: [[u8; 3]; 5] = [ | |||
| 253 | 266 | ||
| 254 | /// Sub-MV tree context (§16.4 `vp8_mvCont`): 4 = left==above==zero, | 267 | /// Sub-MV tree context (§16.4 `vp8_mvCont`): 4 = left==above==zero, |
| 255 | /// 3 = left==above, 2 = above zero, 1 = left zero, 0 = normal. | 268 | /// 3 = left==above, 2 = above zero, 1 = left zero, 0 = normal. |
| 269 | +/// Sub-MV tree context (§16.4 `vp8_mvCont`): 4 = left==above==zero, | ||
| 256 | pub fn mv_cont(l: (i16, i16), a: (i16, i16)) -> usize { | 270 | pub fn mv_cont(l: (i16, i16), a: (i16, i16)) -> usize { |
| 257 | let lez = l == (0, 0); | 271 | let lez = l == (0, 0); |
| 258 | let aez = a == (0, 0); | 272 | let aez = a == (0, 0); |
crates/ec-vp8/tests/header_parse.rsmodified+2 -2
| @@ -39,8 +39,8 @@ fn parses_real_key_frame_headers() { | |||
| 39 | assert_eq!((kf.h_scale, kf.v_scale), (0, 0), "{name} unscaled"); | 39 | assert_eq!((kf.h_scale, kf.v_scale), (0, 0), "{name} unscaled"); |
| 40 | 40 | ||
| 41 | let mut state = PersistedState::default(); | 41 | let mut state = PersistedState::default(); |
| 42 | - let (header, part0) = FrameHeader::parse(first, &mut state).unwrap(); | 42 | + let (header, _) = FrameHeader::parse(first, &mut state).unwrap(); |
| 43 | - assert_eq!(part0.len(), tag.first_part_size as usize); | 43 | + assert_eq!(header.token_data_offset - 10, tag.first_part_size as usize); |
| 44 | assert!(header.dims.is_some()); | 44 | assert!(header.dims.is_some()); |
| 45 | assert_eq!(header.color_space, 0, "{name}: YUV colour space"); | 45 | assert_eq!(header.color_space, 0, "{name}: YUV colour space"); |
| 46 | assert_eq!( | 46 | assert_eq!( |