Read the mv_hp/class0_hp bits real hp streams carry
Browse files at this commite192a627341273717a8dfc5bf8fff6e3879a8421Parent: a45ccae- feat
- read the mv_hp/class0_hp bits real hp streams carry
4 changed files +76 -12
- crates/ec-av1/src/cdf_state.rs +13 -4
- crates/ec-av1/src/decode.rs +49 -7
- crates/ec-av1/src/msac.rs +10 -0
- crates/ec-av1/src/stream.rs +4 -1
crates/ec-av1/src/cdf_state.rsmodified+13 -4
| @@ -272,14 +272,19 @@ pub(crate) struct MvComponentCdfs { | |||
| 272 | pub class0_bit: [u16; 3], | 272 | pub class0_bit: [u16; 3], |
| 273 | /// The fractional part of a small-class magnitude, by its integer bit. | 273 | /// The fractional part of a small-class magnitude, by its integer bit. |
| 274 | pub class0_fr: [[u16; 5]; 2], | 274 | pub class0_fr: [[u16; 5]; 2], |
| 275 | - // `class0_hp`/`hp` (spec's half-pel-bit CDFs) are never written: this | 275 | + /// The half-pel bit of a small-class magnitude (spec `mv_class0_hp`), |
| 276 | - // encoder always sets `allow_high_precision_mv = false`, so the syntax | 276 | + /// read only when the frame header sets `allow_high_precision_mv` (a |
| 277 | - // that reads them (spec 5.9.32 `mv_class0_hp`/`mv_hp`) never fires -- | 277 | + /// real encoder's choice, not this crate's own writer's -- which always |
| 278 | - // dropped rather than kept dead. Add back if/when hp mode ships. | 278 | + /// leaves the flag off and so never needs this table, but a foreign |
| 279 | + /// stream this decoder reads can set it per frame). | ||
| 280 | + pub class0_hp: [u16; 3], | ||
| 279 | /// One bit of an above-small-class magnitude, by its position. | 281 | /// One bit of an above-small-class magnitude, by its position. |
| 280 | pub bit: [[u16; 3]; 10], | 282 | pub bit: [[u16; 3]; 10], |
| 281 | /// The fractional part of an above-small-class magnitude. | 283 | /// The fractional part of an above-small-class magnitude. |
| 282 | pub fr: [u16; 5], | 284 | pub fr: [u16; 5], |
| 285 | + /// The half-pel bit of an above-small-class magnitude (spec `mv_hp`), | ||
| 286 | + /// same `allow_high_precision_mv` gating as [`Self::class0_hp`]. | ||
| 287 | + pub hp: [u16; 3], | ||
| 283 | /// The component's sign. | 288 | /// The component's sign. |
| 284 | pub sign: [u16; 3], | 289 | pub sign: [u16; 3], |
| 285 | } | 290 | } |
| @@ -290,8 +295,10 @@ impl MvComponentCdfs { | |||
| 290 | class: cdf::MV_CLASS, | 295 | class: cdf::MV_CLASS, |
| 291 | class0_bit: cdf::MV_CLASS0_BIT, | 296 | class0_bit: cdf::MV_CLASS0_BIT, |
| 292 | class0_fr: cdf::MV_CLASS0_FR, | 297 | class0_fr: cdf::MV_CLASS0_FR, |
| 298 | + class0_hp: cdf::MV_CLASS0_HP, | ||
| 293 | bit: cdf::MV_BIT, | 299 | bit: cdf::MV_BIT, |
| 294 | fr: cdf::MV_FR, | 300 | fr: cdf::MV_FR, |
| 301 | + hp: cdf::MV_HP, | ||
| 295 | sign: cdf::MV_SIGN, | 302 | sign: cdf::MV_SIGN, |
| 296 | } | 303 | } |
| 297 | } | 304 | } |
| @@ -335,8 +342,10 @@ impl MvComponentCdfs { | |||
| 335 | reset1(&mut self.class); | 342 | reset1(&mut self.class); |
| 336 | reset1(&mut self.class0_bit); | 343 | reset1(&mut self.class0_bit); |
| 337 | reset2(&mut self.class0_fr); | 344 | reset2(&mut self.class0_fr); |
| 345 | + reset1(&mut self.class0_hp); | ||
| 338 | reset2(&mut self.bit); | 346 | reset2(&mut self.bit); |
| 339 | reset1(&mut self.fr); | 347 | reset1(&mut self.fr); |
| 348 | + reset1(&mut self.hp); | ||
| 340 | reset1(&mut self.sign); | 349 | reset1(&mut self.sign); |
| 341 | } | 350 | } |
| 342 | } | 351 | } |
crates/ec-av1/src/decode.rsmodified+49 -7
| @@ -2509,20 +2509,38 @@ fn mv_class_base(class: usize) -> i32 { | |||
| 2509 | /// One motion vector component's non-zero diff (spec 5.11.32 | 2509 | /// One motion vector component's non-zero diff (spec 5.11.32 |
| 2510 | /// `read_mv_component`), the inverse of [`crate::tile`]'s private | 2510 | /// `read_mv_component`), the inverse of [`crate::tile`]'s private |
| 2511 | /// `write_mv_component`. | 2511 | /// `write_mv_component`. |
| 2512 | -fn read_mv_component(dec: &mut SymbolDecoder, c: &mut MvComponentCdfs) -> i32 { | 2512 | +fn read_mv_component( |
| 2513 | + dec: &mut SymbolDecoder, | ||
| 2514 | + c: &mut MvComponentCdfs, | ||
| 2515 | + allow_high_precision_mv: bool, | ||
| 2516 | +) -> i32 { | ||
| 2513 | let sign = dec.symbol(&mut c.sign); | 2517 | let sign = dec.symbol(&mut c.sign); |
| 2514 | let class = dec.symbol(&mut c.class); | 2518 | let class = dec.symbol(&mut c.class); |
| 2515 | let local = if class == 0 { | 2519 | let local = if class == 0 { |
| 2516 | let bit = dec.symbol(&mut c.class0_bit); | 2520 | let bit = dec.symbol(&mut c.class0_bit); |
| 2517 | let fr = dec.symbol(&mut c.class0_fr[bit]); | 2521 | let fr = dec.symbol(&mut c.class0_fr[bit]); |
| 2518 | - (bit << 3) | (fr << 1) | 1 | 2522 | + // spec 5.11.32 `mv_class0_hp`: read only when the frame allows |
| 2523 | + // high-precision motion vectors, else implicitly 1 (the low bit | ||
| 2524 | + // stays at half-pel precision) -- this crate's own writer always | ||
| 2525 | + // leaves the flag off, but a foreign stream can set it per frame. | ||
| 2526 | + let hp = if allow_high_precision_mv { | ||
| 2527 | + dec.symbol(&mut c.class0_hp) | ||
| 2528 | + } else { | ||
| 2529 | + 1 | ||
| 2530 | + }; | ||
| 2531 | + (bit << 3) | (fr << 1) | hp | ||
| 2519 | } else { | 2532 | } else { |
| 2520 | let mut d = 0; | 2533 | let mut d = 0; |
| 2521 | for i in 0..class { | 2534 | for i in 0..class { |
| 2522 | d |= dec.symbol(&mut c.bit[i]) << i; | 2535 | d |= dec.symbol(&mut c.bit[i]) << i; |
| 2523 | } | 2536 | } |
| 2524 | let fr = dec.symbol(&mut c.fr); | 2537 | let fr = dec.symbol(&mut c.fr); |
| 2525 | - (d << 3) | (fr << 1) | 1 | 2538 | + let hp = if allow_high_precision_mv { |
| 2539 | + dec.symbol(&mut c.hp) | ||
| 2540 | + } else { | ||
| 2541 | + 1 | ||
| 2542 | + }; | ||
| 2543 | + (d << 3) | (fr << 1) | hp | ||
| 2526 | }; | 2544 | }; |
| 2527 | let mag = mv_class_base(class) + local as i32 + 1; | 2545 | let mag = mv_class_base(class) + local as i32 + 1; |
| 2528 | if sign == 1 { -mag } else { mag } | 2546 | if sign == 1 { -mag } else { mag } |
| @@ -2535,14 +2553,15 @@ fn read_mv( | |||
| 2535 | mv_comp: &mut [MvComponentCdfs; 2], | 2553 | mv_comp: &mut [MvComponentCdfs; 2], |
| 2536 | mv_joint: &mut [u16; 5], | 2554 | mv_joint: &mut [u16; 5], |
| 2537 | pred: (i32, i32), | 2555 | pred: (i32, i32), |
| 2556 | + allow_high_precision_mv: bool, | ||
| 2538 | ) -> (i32, i32) { | 2557 | ) -> (i32, i32) { |
| 2539 | let joint = dec.symbol(mv_joint); | 2558 | let joint = dec.symbol(mv_joint); |
| 2540 | let mut diff = (0, 0); | 2559 | let mut diff = (0, 0); |
| 2541 | if joint == 2 || joint == 3 { | 2560 | if joint == 2 || joint == 3 { |
| 2542 | - diff.0 = read_mv_component(dec, &mut mv_comp[0]); | 2561 | + diff.0 = read_mv_component(dec, &mut mv_comp[0], allow_high_precision_mv); |
| 2543 | } | 2562 | } |
| 2544 | if joint == 1 || joint == 3 { | 2563 | if joint == 1 || joint == 3 { |
| 2545 | - diff.1 = read_mv_component(dec, &mut mv_comp[1]); | 2564 | + diff.1 = read_mv_component(dec, &mut mv_comp[1], allow_high_precision_mv); |
| 2546 | } | 2565 | } |
| 2547 | (pred.0 + diff.0, pred.1 + diff.1) | 2566 | (pred.0 + diff.0, pred.1 + diff.1) |
| 2548 | } | 2567 | } |
| @@ -2650,6 +2669,7 @@ fn decode_inter_block( | |||
| 2650 | scan_luma: &[u16], | 2669 | scan_luma: &[u16], |
| 2651 | scan_chroma: &[u16], | 2670 | scan_chroma: &[u16], |
| 2652 | size_group: usize, | 2671 | size_group: usize, |
| 2672 | + allow_high_precision_mv: bool, | ||
| 2653 | ) -> Result<()> { | 2673 | ) -> Result<()> { |
| 2654 | const LAST_FRAME: i8 = 1; | 2674 | const LAST_FRAME: i8 = 1; |
| 2655 | 2675 | ||
| @@ -2698,7 +2718,13 @@ fn decode_inter_block( | |||
| 2698 | dec.symbol(&mut cdfs.drl_mode[stack.drl_ctx[0]]); | 2718 | dec.symbol(&mut cdfs.drl_mode[stack.drl_ctx[0]]); |
| 2699 | } | 2719 | } |
| 2700 | ( | 2720 | ( |
| 2701 | - read_mv(dec, &mut cdfs.mv_comp, &mut cdfs.mv_joint, stack.pred_mv), | 2721 | + read_mv( |
| 2722 | + dec, | ||
| 2723 | + &mut cdfs.mv_comp, | ||
| 2724 | + &mut cdfs.mv_joint, | ||
| 2725 | + stack.pred_mv, | ||
| 2726 | + allow_high_precision_mv, | ||
| 2727 | + ), | ||
| 2702 | true, | 2728 | true, |
| 2703 | ) | 2729 | ) |
| 2704 | } else { | 2730 | } else { |
| @@ -3044,6 +3070,7 @@ fn decode_inter_block8( | |||
| 3044 | scan8: &[u16], | 3070 | scan8: &[u16], |
| 3045 | scan4: &[u16], | 3071 | scan4: &[u16], |
| 3046 | prev_leaf: Option<((usize, usize), bool, bool)>, | 3072 | prev_leaf: Option<((usize, usize), bool, bool)>, |
| 3073 | + allow_high_precision_mv: bool, | ||
| 3047 | ) -> Result<(bool, bool)> { | 3074 | ) -> Result<(bool, bool)> { |
| 3048 | const LAST_FRAME: i8 = 1; | 3075 | const LAST_FRAME: i8 = 1; |
| 3049 | /// `Y_MODE`'s size group (`common_data.h`'s `size_group_lookup[BLOCK_8X8]`). | 3076 | /// `Y_MODE`'s size group (`common_data.h`'s `size_group_lookup[BLOCK_8X8]`). |
| @@ -3106,7 +3133,13 @@ fn decode_inter_block8( | |||
| 3106 | dec.symbol(&mut cdfs.drl_mode[stack.drl_ctx[0]]); | 3133 | dec.symbol(&mut cdfs.drl_mode[stack.drl_ctx[0]]); |
| 3107 | } | 3134 | } |
| 3108 | ( | 3135 | ( |
| 3109 | - read_mv(dec, &mut cdfs.mv_comp, &mut cdfs.mv_joint, stack.pred_mv), | 3136 | + read_mv( |
| 3137 | + dec, | ||
| 3138 | + &mut cdfs.mv_comp, | ||
| 3139 | + &mut cdfs.mv_joint, | ||
| 3140 | + stack.pred_mv, | ||
| 3141 | + allow_high_precision_mv, | ||
| 3142 | + ), | ||
| 3110 | true, | 3143 | true, |
| 3111 | ) | 3144 | ) |
| 3112 | } else { | 3145 | } else { |
| @@ -3413,6 +3446,7 @@ pub fn decode_inter_frame_tile( | |||
| 3413 | reference: &Picture, | 3446 | reference: &Picture, |
| 3414 | cdef: &CdefParams, | 3447 | cdef: &CdefParams, |
| 3415 | loop_filter: &LoopFilterParams, | 3448 | loop_filter: &LoopFilterParams, |
| 3449 | + allow_high_precision_mv: bool, | ||
| 3416 | ) -> Result<Picture> { | 3450 | ) -> Result<Picture> { |
| 3417 | decode_inter_frame_tile_with_cdfs( | 3451 | decode_inter_frame_tile_with_cdfs( |
| 3418 | data, | 3452 | data, |
| @@ -3425,6 +3459,7 @@ pub fn decode_inter_frame_tile( | |||
| 3425 | cdef, | 3459 | cdef, |
| 3426 | loop_filter, | 3460 | loop_filter, |
| 3427 | None, | 3461 | None, |
| 3462 | + allow_high_precision_mv, | ||
| 3428 | ) | 3463 | ) |
| 3429 | .map(|(picture, _)| picture) | 3464 | .map(|(picture, _)| picture) |
| 3430 | } | 3465 | } |
| @@ -3445,6 +3480,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | |||
| 3445 | cdef: &CdefParams, | 3480 | cdef: &CdefParams, |
| 3446 | loop_filter: &LoopFilterParams, | 3481 | loop_filter: &LoopFilterParams, |
| 3447 | initial_cdfs: Option<Cdfs>, | 3482 | initial_cdfs: Option<Cdfs>, |
| 3483 | + allow_high_precision_mv: bool, | ||
| 3448 | ) -> Result<(Picture, Cdfs)> { | 3484 | ) -> Result<(Picture, Cdfs)> { |
| 3449 | if mi_cols == 0 || mi_rows == 0 { | 3485 | if mi_cols == 0 || mi_rows == 0 { |
| 3450 | return Err(unsupported("a frame with no mode-info grid")); | 3486 | return Err(unsupported("a frame with no mode-info grid")); |
| @@ -3591,6 +3627,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | |||
| 3591 | &scan32, | 3627 | &scan32, |
| 3592 | &scan16, | 3628 | &scan16, |
| 3593 | 3, | 3629 | 3, |
| 3630 | + allow_high_precision_mv, | ||
| 3594 | )?; | 3631 | )?; |
| 3595 | } | 3632 | } |
| 3596 | PARTITION_SPLIT => { | 3633 | PARTITION_SPLIT => { |
| @@ -3643,6 +3680,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | |||
| 3643 | &scan16, | 3680 | &scan16, |
| 3644 | &scan8, | 3681 | &scan8, |
| 3645 | 2, | 3682 | 2, |
| 3683 | + allow_high_precision_mv, | ||
| 3646 | )?; | 3684 | )?; |
| 3647 | } else { | 3685 | } else { |
| 3648 | let ctx16 = neighbours.partition_ctx(at16, SUB); | 3686 | let ctx16 = neighbours.partition_ctx(at16, SUB); |
| @@ -3691,6 +3729,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | |||
| 3691 | &scan8, | 3729 | &scan8, |
| 3692 | &scan4, | 3730 | &scan4, |
| 3693 | prev_leaf, | 3731 | prev_leaf, |
| 3732 | + allow_high_precision_mv, | ||
| 3694 | )?; | 3733 | )?; |
| 3695 | prev_leaf = Some((leaf_mi, skip, is_inter)); | 3734 | prev_leaf = Some((leaf_mi, skip, is_inter)); |
| 3696 | } | 3735 | } |
| @@ -4170,6 +4209,7 @@ mod tests { | |||
| 4170 | &reference, | 4209 | &reference, |
| 4171 | &CdefParams::default(), | 4210 | &CdefParams::default(), |
| 4172 | &LoopFilterParams::default(), | 4211 | &LoopFilterParams::default(), |
| 4212 | + false, | ||
| 4173 | ) | 4213 | ) |
| 4174 | .unwrap(); | 4214 | .unwrap(); |
| 4175 | assert_eq!( | 4215 | assert_eq!( |
| @@ -4343,6 +4383,7 @@ mod tests { | |||
| 4343 | &[], | 4383 | &[], |
| 4344 | &[], | 4384 | &[], |
| 4345 | 0, | 4385 | 0, |
| 4386 | + false, | ||
| 4346 | ) | 4387 | ) |
| 4347 | .unwrap(); | 4388 | .unwrap(); |
| 4348 | 4389 | ||
| @@ -4430,6 +4471,7 @@ mod tests { | |||
| 4430 | &reference, | 4471 | &reference, |
| 4431 | &CdefParams::default(), | 4472 | &CdefParams::default(), |
| 4432 | &LoopFilterParams::default(), | 4473 | &LoopFilterParams::default(), |
| 4474 | + false, | ||
| 4433 | ) | 4475 | ) |
| 4434 | .unwrap(); | 4476 | .unwrap(); |
| 4435 | assert_eq!(decoded.y, ffmpeg_frames[i].y, "frame {i} luma vs ffmpeg"); | 4477 | assert_eq!(decoded.y, ffmpeg_frames[i].y, "frame {i} luma vs ffmpeg"); |
crates/ec-av1/src/msac.rsmodified+10 -0
| @@ -299,6 +299,7 @@ impl<'a> SymbolDecoder<'a> { | |||
| 299 | /// [`SymbolEncoder::symbol_fixed`]. | 299 | /// [`SymbolEncoder::symbol_fixed`]. |
| 300 | pub fn symbol_fixed(&mut self, cdf: &[u16]) -> usize { | 300 | pub fn symbol_fixed(&mut self, cdf: &[u16]) -> usize { |
| 301 | let nsyms = cdf.len() - 1; | 301 | let nsyms = cdf.len() - 1; |
| 302 | + let rng_before = self.range; | ||
| 302 | let mut cur = self.range; | 303 | let mut cur = self.range; |
| 303 | let mut prev; | 304 | let mut prev; |
| 304 | let mut symbol = 0; | 305 | let mut symbol = 0; |
| @@ -314,6 +315,15 @@ impl<'a> SymbolDecoder<'a> { | |||
| 314 | } | 315 | } |
| 315 | self.range = prev - cur; | 316 | self.range = prev - cur; |
| 316 | self.value -= cur; | 317 | self.value -= cur; |
| 318 | + if std::env::var_os("EC_RNG").is_some() { | ||
| 319 | + use std::sync::atomic::{AtomicU64, Ordering}; | ||
| 320 | + static EC_RNG_N: AtomicU64 = AtomicU64::new(0); | ||
| 321 | + let n = EC_RNG_N.fetch_add(1, Ordering::Relaxed); | ||
| 322 | + eprintln!( | ||
| 323 | + "EC_RNG n={n} rng_before={rng_before} rng_after={} nsyms={nsyms} symbol={symbol}", | ||
| 324 | + self.range | ||
| 325 | + ); | ||
| 326 | + } | ||
| 317 | 327 | ||
| 318 | // Renormalise back into `[2^15, 2^16)`, the same shift the encoder | 328 | // Renormalise back into `[2^15, 2^16)`, the same shift the encoder |
| 319 | // applied when it narrowed to this symbol. | 329 | // applied when it narrowed to this symbol. |
crates/ec-av1/src/stream.rsmodified+4 -1
| @@ -221,6 +221,7 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | |||
| 221 | &header.cdef, | 221 | &header.cdef, |
| 222 | &header.loop_filter, | 222 | &header.loop_filter, |
| 223 | initial_cdfs, | 223 | initial_cdfs, |
| 224 | + header.allow_high_precision_mv, | ||
| 224 | )? | 225 | )? |
| 225 | }; | 226 | }; |
| 226 | // Spec 7.20: `disable_frame_end_update_cdf` stores the frame's | 227 | // Spec 7.20: `disable_frame_end_update_cdf` stores the frame's |
| @@ -1313,11 +1314,13 @@ mod tests { | |||
| 1313 | eprintln!("segmentation: {:?}", header.segmentation); | 1314 | eprintln!("segmentation: {:?}", header.segmentation); |
| 1314 | eprintln!("loop_restoration: {:?}", header.loop_restoration); | 1315 | eprintln!("loop_restoration: {:?}", header.loop_restoration); |
| 1315 | eprintln!( | 1316 | eprintln!( |
| 1316 | - "use_128x128_superblock: {:?} disable_cdf_update: {:?} disable_frame_end_update_cdf: {:?} primary_ref_frame: {:?}", | 1317 | + "use_128x128_superblock: {:?} disable_cdf_update: {:?} disable_frame_end_update_cdf: {:?} primary_ref_frame: {:?} allow_high_precision_mv: {:?} force_integer_mv: {:?}", |
| 1317 | p.sequence_header().map(|s| s.use_128x128_superblock), | 1318 | p.sequence_header().map(|s| s.use_128x128_superblock), |
| 1318 | header.disable_cdf_update, | 1319 | header.disable_cdf_update, |
| 1319 | header.disable_frame_end_update_cdf, | 1320 | header.disable_frame_end_update_cdf, |
| 1320 | header.primary_ref_frame, | 1321 | header.primary_ref_frame, |
| 1322 | + header.allow_high_precision_mv, | ||
| 1323 | + header.force_integer_mv, | ||
| 1321 | ); | 1324 | ); |
| 1322 | } | 1325 | } |
| 1323 | } | 1326 | } |