wip(av1): finish the txb_skip 7-context rewire; find TX16 tx_type mismatch
Browse files at this commitdcf114768dae66863cbf00ded222ea144e1c8afaParent: ab946374 changed files +333 -40
- crates/ec-av1/examples/probe_txsel.rs +63 -0
- crates/ec-av1/src/cdf.rs +150 -0
- crates/ec-av1/src/cdf_state.rs +39 -35
- crates/ec-av1/src/decode.rs +81 -5
crates/ec-av1/examples/probe_txsel.rsadded+63 -0
| @@ -0,0 +1,63 @@ | |||
| 1 | +use std::io::Write; | ||
| 2 | +use std::process::{Command, Stdio}; | ||
| 3 | + | ||
| 4 | +fn main() { | ||
| 5 | + let path = std::env::args() | ||
| 6 | + .nth(1) | ||
| 7 | + .unwrap_or_else(|| "/tmp/mb.obu".into()); | ||
| 8 | + let stream = std::fs::read(&path).expect("read stream"); | ||
| 9 | + let frames = match ec_av1::stream::decode_stream(&stream) { | ||
| 10 | + Ok(f) => f, | ||
| 11 | + Err(e) => { | ||
| 12 | + eprintln!("decode error: {e}"); | ||
| 13 | + std::process::exit(1); | ||
| 14 | + } | ||
| 15 | + }; | ||
| 16 | + let (width, height) = (64usize, 64usize); | ||
| 17 | + let mut child = Command::new("ffmpeg") | ||
| 18 | + .args([ | ||
| 19 | + "-v", "error", "-f", "obu", "-i", "-", "-f", "rawvideo", "-pix_fmt", "yuv420p", "-", | ||
| 20 | + ]) | ||
| 21 | + .stdin(Stdio::piped()) | ||
| 22 | + .stdout(Stdio::piped()) | ||
| 23 | + .stderr(Stdio::piped()) | ||
| 24 | + .spawn() | ||
| 25 | + .expect("ffmpeg spawn"); | ||
| 26 | + child | ||
| 27 | + .stdin | ||
| 28 | + .take() | ||
| 29 | + .unwrap() | ||
| 30 | + .write_all(&stream) | ||
| 31 | + .expect("write stream"); | ||
| 32 | + let out = child.wait_with_output().expect("ffmpeg run"); | ||
| 33 | + if !out.status.success() { | ||
| 34 | + eprintln!("ffmpeg refused: {}", String::from_utf8_lossy(&out.stderr)); | ||
| 35 | + std::process::exit(1); | ||
| 36 | + } | ||
| 37 | + let luma = width * height; | ||
| 38 | + let chroma = luma / 4; | ||
| 39 | + let y_ref = &out.stdout[..luma]; | ||
| 40 | + let u_ref = &out.stdout[luma..luma + chroma]; | ||
| 41 | + let v_ref = &out.stdout[luma + chroma..luma + 2 * chroma]; | ||
| 42 | + | ||
| 43 | + let pic = &frames[0]; | ||
| 44 | + let y_match = pic.y == y_ref; | ||
| 45 | + let u_match = pic.u == u_ref; | ||
| 46 | + let v_match = pic.v == v_ref; | ||
| 47 | + println!("decoded ok, y_match={y_match} u_match={u_match} v_match={v_match}"); | ||
| 48 | + if !y_match { | ||
| 49 | + for i in 0..luma { | ||
| 50 | + if pic.y[i] != y_ref[i] { | ||
| 51 | + println!( | ||
| 52 | + "first luma mismatch at idx {i} (row {} col {}): ours={} ref={}", | ||
| 53 | + i / width, | ||
| 54 | + i % width, | ||
| 55 | + pic.y[i], | ||
| 56 | + y_ref[i] | ||
| 57 | + ); | ||
| 58 | + break; | ||
| 59 | + } | ||
| 60 | + } | ||
| 61 | + } | ||
| 62 | + std::process::exit(if y_match && u_match && v_match { 0 } else { 2 }); | ||
| 63 | +} | ||
crates/ec-av1/src/cdf.rsmodified+150 -0
| @@ -3758,3 +3758,153 @@ pub const INTRA_TX_TYPE_SET1_8: [[u16; 8]; 13] = [ | |||
| 3758 | /// at the flat 5-way default (unlike its trained `TX_16X16` neighbour, | 3758 | /// at the flat 5-way default (unlike its trained `TX_16X16` neighbour, |
| 3759 | /// [`INTRA_TX_TYPE_SET2_16`]) -- every mode reads the same uniform CDF. | 3759 | /// [`INTRA_TX_TYPE_SET2_16`]) -- every mode reads the same uniform CDF. |
| 3760 | pub const INTRA_TX_TYPE_SET2_8: [[u16; 6]; 13] = [[6554, 13107, 19661, 26214, 32768, 0]; 13]; | 3760 | pub const INTRA_TX_TYPE_SET2_8: [[u16; 6]; 13] = [[6554, 13107, 19661, 26214, 32768, 0]; 13]; |
| 3761 | +/// The full luma sub-range of spec `TXB_SKIP_CONTEXTS` (contexts 0..6, the | ||
| 3762 | +/// `plane_bsize != tx_size` `skip_contexts[top][left]` table plus its own | ||
| 3763 | +/// single `plane_bsize == tx_size` context 0), for a `TxMode::Select` block | ||
| 3764 | +/// whose luma transform is split into several transform units smaller than | ||
| 3765 | +/// the block itself -- the single-context `TXB_SKIP_LUMA_*` constants above | ||
| 3766 | +/// stay for every other caller, which only ever reads context 0. | ||
| 3767 | +pub const TXB_SKIP_LUMA_8_Q0_CTX: [[u16; 3]; 7] = [ | ||
| 3768 | + [31548, 32768, 0], | ||
| 3769 | + [1549, 32768, 0], | ||
| 3770 | + [10130, 32768, 0], | ||
| 3771 | + [16656, 32768, 0], | ||
| 3772 | + [18591, 32768, 0], | ||
| 3773 | + [26308, 32768, 0], | ||
| 3774 | + [32537, 32768, 0], | ||
| 3775 | +]; | ||
| 3776 | +pub const TXB_SKIP_LUMA_8_Q1_CTX: [[u16; 3]; 7] = [ | ||
| 3777 | + [31782, 32768, 0], | ||
| 3778 | + [1836, 32768, 0], | ||
| 3779 | + [10689, 32768, 0], | ||
| 3780 | + [17604, 32768, 0], | ||
| 3781 | + [21622, 32768, 0], | ||
| 3782 | + [27518, 32768, 0], | ||
| 3783 | + [32399, 32768, 0], | ||
| 3784 | +]; | ||
| 3785 | +pub const TXB_SKIP_LUMA_8_CTX: [[u16; 3]; 7] = [ | ||
| 3786 | + [31957, 32768, 0], | ||
| 3787 | + [3230, 32768, 0], | ||
| 3788 | + [11153, 32768, 0], | ||
| 3789 | + [18123, 32768, 0], | ||
| 3790 | + [20143, 32768, 0], | ||
| 3791 | + [26536, 32768, 0], | ||
| 3792 | + [31986, 32768, 0], | ||
| 3793 | +]; | ||
| 3794 | +pub const TXB_SKIP_LUMA_8_Q3_CTX: [[u16; 3]; 7] = [ | ||
| 3795 | + [31903, 32768, 0], | ||
| 3796 | + [2044, 32768, 0], | ||
| 3797 | + [7528, 32768, 0], | ||
| 3798 | + [14618, 32768, 0], | ||
| 3799 | + [16182, 32768, 0], | ||
| 3800 | + [24168, 32768, 0], | ||
| 3801 | + [31037, 32768, 0], | ||
| 3802 | +]; | ||
| 3803 | +pub const TXB_SKIP_LUMA_16_Q0_CTX: [[u16; 3]; 7] = [ | ||
| 3804 | + [29957, 32768, 0], | ||
| 3805 | + [5391, 32768, 0], | ||
| 3806 | + [18039, 32768, 0], | ||
| 3807 | + [23566, 32768, 0], | ||
| 3808 | + [22431, 32768, 0], | ||
| 3809 | + [25822, 32768, 0], | ||
| 3810 | + [32197, 32768, 0], | ||
| 3811 | +]; | ||
| 3812 | +pub const TXB_SKIP_LUMA_16_Q1_CTX: [[u16; 3]; 7] = [ | ||
| 3813 | + [31901, 32768, 0], | ||
| 3814 | + [10311, 32768, 0], | ||
| 3815 | + [18047, 32768, 0], | ||
| 3816 | + [24806, 32768, 0], | ||
| 3817 | + [23288, 32768, 0], | ||
| 3818 | + [27914, 32768, 0], | ||
| 3819 | + [32296, 32768, 0], | ||
| 3820 | +]; | ||
| 3821 | +pub const TXB_SKIP_LUMA_16_CTX: [[u16; 3]; 7] = [ | ||
| 3822 | + [32363, 32768, 0], | ||
| 3823 | + [10692, 32768, 0], | ||
| 3824 | + [19090, 32768, 0], | ||
| 3825 | + [24357, 32768, 0], | ||
| 3826 | + [24442, 32768, 0], | ||
| 3827 | + [28312, 32768, 0], | ||
| 3828 | + [32169, 32768, 0], | ||
| 3829 | +]; | ||
| 3830 | +pub const TXB_SKIP_LUMA_16_Q3_CTX: [[u16; 3]; 7] = [ | ||
| 3831 | + [32510, 32768, 0], | ||
| 3832 | + [8430, 32768, 0], | ||
| 3833 | + [17318, 32768, 0], | ||
| 3834 | + [24154, 32768, 0], | ||
| 3835 | + [23674, 32768, 0], | ||
| 3836 | + [28789, 32768, 0], | ||
| 3837 | + [32139, 32768, 0], | ||
| 3838 | +]; | ||
| 3839 | +pub const TXB_SKIP_LUMA_32_Q0_CTX: [[u16; 3]; 7] = [ | ||
| 3840 | + [17920, 32768, 0], | ||
| 3841 | + [1818, 32768, 0], | ||
| 3842 | + [7282, 32768, 0], | ||
| 3843 | + [25273, 32768, 0], | ||
| 3844 | + [10923, 32768, 0], | ||
| 3845 | + [31554, 32768, 0], | ||
| 3846 | + [32624, 32768, 0], | ||
| 3847 | +]; | ||
| 3848 | +pub const TXB_SKIP_LUMA_32_Q1_CTX: [[u16; 3]; 7] = [ | ||
| 3849 | + [26726, 32768, 0], | ||
| 3850 | + [1045, 32768, 0], | ||
| 3851 | + [11703, 32768, 0], | ||
| 3852 | + [20590, 32768, 0], | ||
| 3853 | + [18554, 32768, 0], | ||
| 3854 | + [25970, 32768, 0], | ||
| 3855 | + [31938, 32768, 0], | ||
| 3856 | +]; | ||
| 3857 | +pub const TXB_SKIP_LUMA_32_CTX: [[u16; 3]; 7] = [ | ||
| 3858 | + [30669, 32768, 0], | ||
| 3859 | + [3832, 32768, 0], | ||
| 3860 | + [11663, 32768, 0], | ||
| 3861 | + [18889, 32768, 0], | ||
| 3862 | + [19782, 32768, 0], | ||
| 3863 | + [23313, 32768, 0], | ||
| 3864 | + [31330, 32768, 0], | ||
| 3865 | +]; | ||
| 3866 | +pub const TXB_SKIP_LUMA_32_Q3_CTX: [[u16; 3]; 7] = [ | ||
| 3867 | + [31671, 32768, 0], | ||
| 3868 | + [2056, 32768, 0], | ||
| 3869 | + [11746, 32768, 0], | ||
| 3870 | + [16852, 32768, 0], | ||
| 3871 | + [18635, 32768, 0], | ||
| 3872 | + [24715, 32768, 0], | ||
| 3873 | + [31484, 32768, 0], | ||
| 3874 | +]; | ||
| 3875 | +pub const TXB_SKIP_LUMA_64_Q0_CTX: [[u16; 3]; 7] = [ | ||
| 3876 | + [6308, 32768, 0], | ||
| 3877 | + [117, 32768, 0], | ||
| 3878 | + [1638, 32768, 0], | ||
| 3879 | + [2161, 32768, 0], | ||
| 3880 | + [16384, 32768, 0], | ||
| 3881 | + [10923, 32768, 0], | ||
| 3882 | + [30247, 32768, 0], | ||
| 3883 | +]; | ||
| 3884 | +pub const TXB_SKIP_LUMA_64_Q1_CTX: [[u16; 3]; 7] = [ | ||
| 3885 | + [26584, 32768, 0], | ||
| 3886 | + [188, 32768, 0], | ||
| 3887 | + [8847, 32768, 0], | ||
| 3888 | + [24519, 32768, 0], | ||
| 3889 | + [22938, 32768, 0], | ||
| 3890 | + [30583, 32768, 0], | ||
| 3891 | + [32608, 32768, 0], | ||
| 3892 | +]; | ||
| 3893 | +pub const TXB_SKIP_LUMA_64_CTX: [[u16; 3]; 7] = [ | ||
| 3894 | + [28573, 32768, 0], | ||
| 3895 | + [3183, 32768, 0], | ||
| 3896 | + [17802, 32768, 0], | ||
| 3897 | + [25977, 32768, 0], | ||
| 3898 | + [26677, 32768, 0], | ||
| 3899 | + [27832, 32768, 0], | ||
| 3900 | + [32387, 32768, 0], | ||
| 3901 | +]; | ||
| 3902 | +pub const TXB_SKIP_LUMA_64_Q3_CTX: [[u16; 3]; 7] = [ | ||
| 3903 | + [31539, 32768, 0], | ||
| 3904 | + [8433, 32768, 0], | ||
| 3905 | + [20576, 32768, 0], | ||
| 3906 | + [27904, 32768, 0], | ||
| 3907 | + [27852, 32768, 0], | ||
| 3908 | + [30026, 32768, 0], | ||
| 3909 | + [32441, 32768, 0], | ||
| 3910 | +]; | ||
crates/ec-av1/src/cdf_state.rsmodified+39 -35
| @@ -116,10 +116,14 @@ pub(crate) struct Cdfs { | |||
| 116 | pub cfl_alpha: [[u16; 17]; 6], | 116 | pub cfl_alpha: [[u16; 17]; 6], |
| 117 | /// The angle a directional mode is nudged by. | 117 | /// The angle a directional mode is nudged by. |
| 118 | pub angle_delta: [[u16; 8]; 8], | 118 | pub angle_delta: [[u16; 8]; 8], |
| 119 | - /// The one-context all-zero flag of a 16x16 luma transform. | 119 | + /// The all-zero flag of a 16x16 luma transform, 7 contexts (index 0 for |
| 120 | - pub txb_skip_luma_16: [[u16; 3]; 1], | 120 | + /// a lone TU whose own bsize equals the block's; 1..6 the |
| 121 | - /// The one-context all-zero flag of an 8x8 luma transform. | 121 | + /// `skip_contexts[top][left]` table when `TxMode::Select` splits the |
| 122 | - pub txb_skip_luma_8: [[u16; 3]; 1], | 122 | + /// block's luma into several smaller transform units, spec |
| 123 | + /// `get_txb_ctx_general`). | ||
| 124 | + pub txb_skip_luma_16: [[u16; 3]; 7], | ||
| 125 | + /// The same, for an 8x8 luma transform. | ||
| 126 | + pub txb_skip_luma_8: [[u16; 3]; 7], | ||
| 123 | /// The all-zero flag of an 8x8 chroma transform. | 127 | /// The all-zero flag of an 8x8 chroma transform. |
| 124 | pub txb_skip_chroma_8: [[u16; 3]; 3], | 128 | pub txb_skip_chroma_8: [[u16; 3]; 3], |
| 125 | /// The all-zero flag of a 4x4 chroma transform. | 129 | /// The all-zero flag of a 4x4 chroma transform. |
| @@ -170,10 +174,10 @@ pub(crate) struct Cdfs { | |||
| 170 | /// `PARTITION_NONE` is ever coded against it, but the alphabet still | 174 | /// `PARTITION_NONE` is ever coded against it, but the alphabet still |
| 171 | /// adapts on every read, same as every other partition table. | 175 | /// adapts on every read, same as every other partition table. |
| 172 | pub partition_w8: [[u16; 5]; 4], | 176 | pub partition_w8: [[u16; 5]; 4], |
| 173 | - /// The one-context all-zero flag of a 32x32 luma transform. | 177 | + /// The same as `txb_skip_luma_16`'s 7 contexts, for a 32x32 luma transform. |
| 174 | - pub txb_skip_luma_32: [[u16; 3]; 1], | 178 | + pub txb_skip_luma_32: [[u16; 3]; 7], |
| 175 | /// The same, for a 64x64 luma transform. | 179 | /// The same, for a 64x64 luma transform. |
| 176 | - pub txb_skip_luma_64: [[u16; 3]; 1], | 180 | + pub txb_skip_luma_64: [[u16; 3]; 7], |
| 177 | /// The all-zero flag of a 16x16 chroma transform. | 181 | /// The all-zero flag of a 16x16 chroma transform. |
| 178 | pub txb_skip_chroma_16: [[u16; 3]; 3], | 182 | pub txb_skip_chroma_16: [[u16; 3]; 3], |
| 179 | /// The all-zero flag of a 32x32 chroma transform. | 183 | /// The all-zero flag of a 32x32 chroma transform. |
| @@ -464,20 +468,20 @@ impl Cdfs { | |||
| 464 | cfl_sign: cdf::CFL_SIGN, | 468 | cfl_sign: cdf::CFL_SIGN, |
| 465 | cfl_alpha: cdf::CFL_ALPHA, | 469 | cfl_alpha: cdf::CFL_ALPHA, |
| 466 | angle_delta: cdf::ANGLE_DELTA, | 470 | angle_delta: cdf::ANGLE_DELTA, |
| 467 | - txb_skip_luma_16: [pick( | 471 | + txb_skip_luma_16: pick( |
| 468 | - q_ctx, | 472 | + q_ctx, |
| 469 | - cdf::TXB_SKIP_LUMA_16_Q0, | 473 | + cdf::TXB_SKIP_LUMA_16_Q0_CTX, |
| 470 | - cdf::TXB_SKIP_LUMA_16_Q1, | 474 | + cdf::TXB_SKIP_LUMA_16_Q1_CTX, |
| 471 | - cdf::TXB_SKIP_LUMA_16, | 475 | + cdf::TXB_SKIP_LUMA_16_CTX, |
| 472 | - cdf::TXB_SKIP_LUMA_16_Q3, | 476 | + cdf::TXB_SKIP_LUMA_16_Q3_CTX, |
| 473 | - )], | 477 | + ), |
| 474 | - txb_skip_luma_8: [pick( | 478 | + txb_skip_luma_8: pick( |
| 475 | - q_ctx, | 479 | + q_ctx, |
| 476 | - cdf::TXB_SKIP_LUMA_8_Q0, | 480 | + cdf::TXB_SKIP_LUMA_8_Q0_CTX, |
| 477 | - cdf::TXB_SKIP_LUMA_8_Q1, | 481 | + cdf::TXB_SKIP_LUMA_8_Q1_CTX, |
| 478 | - cdf::TXB_SKIP_LUMA_8, | 482 | + cdf::TXB_SKIP_LUMA_8_CTX, |
| 479 | - cdf::TXB_SKIP_LUMA_8_Q3, | 483 | + cdf::TXB_SKIP_LUMA_8_Q3_CTX, |
| 480 | - )], | 484 | + ), |
| 481 | txb_skip_chroma_8: pick( | 485 | txb_skip_chroma_8: pick( |
| 482 | q_ctx, | 486 | q_ctx, |
| 483 | cdf::TXB_SKIP_CHROMA_8_Q0, | 487 | cdf::TXB_SKIP_CHROMA_8_Q0, |
| @@ -634,20 +638,20 @@ impl Cdfs { | |||
| 634 | ), | 638 | ), |
| 635 | partition_w16: cdf::PARTITION_W16, | 639 | partition_w16: cdf::PARTITION_W16, |
| 636 | partition_w8: cdf::PARTITION_W8, | 640 | partition_w8: cdf::PARTITION_W8, |
| 637 | - txb_skip_luma_32: [pick( | 641 | + txb_skip_luma_32: pick( |
| 638 | - q_ctx, | 642 | + q_ctx, |
| 639 | - cdf::TXB_SKIP_LUMA_32_Q0, | 643 | + cdf::TXB_SKIP_LUMA_32_Q0_CTX, |
| 640 | - cdf::TXB_SKIP_LUMA_32_Q1, | 644 | + cdf::TXB_SKIP_LUMA_32_Q1_CTX, |
| 641 | - cdf::TXB_SKIP_LUMA_32, | 645 | + cdf::TXB_SKIP_LUMA_32_CTX, |
| 642 | - cdf::TXB_SKIP_LUMA_32_Q3, | 646 | + cdf::TXB_SKIP_LUMA_32_Q3_CTX, |
| 643 | - )], | 647 | + ), |
| 644 | - txb_skip_luma_64: [pick( | 648 | + txb_skip_luma_64: pick( |
| 645 | - q_ctx, | 649 | + q_ctx, |
| 646 | - cdf::TXB_SKIP_LUMA_64_Q0, | 650 | + cdf::TXB_SKIP_LUMA_64_Q0_CTX, |
| 647 | - cdf::TXB_SKIP_LUMA_64_Q1, | 651 | + cdf::TXB_SKIP_LUMA_64_Q1_CTX, |
| 648 | - cdf::TXB_SKIP_LUMA_64, | 652 | + cdf::TXB_SKIP_LUMA_64_CTX, |
| 649 | - cdf::TXB_SKIP_LUMA_64_Q3, | 653 | + cdf::TXB_SKIP_LUMA_64_Q3_CTX, |
| 650 | - )], | 654 | + ), |
| 651 | txb_skip_chroma_16: pick( | 655 | txb_skip_chroma_16: pick( |
| 652 | q_ctx, | 656 | q_ctx, |
| 653 | cdf::TXB_SKIP_CHROMA_16_Q0, | 657 | cdf::TXB_SKIP_CHROMA_16_Q0, |
crates/ec-av1/src/decode.rsmodified+81 -5
| @@ -381,17 +381,37 @@ fn read_coeffs( | |||
| 381 | /// [`crate::tile`]'s own private `Neighbour`. | 381 | /// [`crate::tile`]'s own private `Neighbour`. |
| 382 | #[derive(Clone, Copy, Default)] | 382 | #[derive(Clone, Copy, Default)] |
| 383 | struct Neighbour { | 383 | struct Neighbour { |
| 384 | - coded: bool, | 384 | + /// The transform unit's cumulative coefficient level (spec `cul_level`, |
| 385 | + /// `decodetxb.c`'s `read_coeffs_txb`): the sum of every coded level's | ||
| 386 | + /// magnitude, clamped to `COEFF_CONTEXT_MASK` (7). A plain "coded or not" | ||
| 387 | + /// boolean (`level != 0`) is enough for every context this decoder read | ||
| 388 | + /// until now, but the *luma* `txb_skip_ctx` of a transform unit smaller | ||
| 389 | + /// than its own block (`TxMode::Select`, spec `get_txb_ctx_general`) | ||
| 390 | + /// needs the actual magnitude tier of its above/left neighbours, not | ||
| 391 | + /// just whether they coded anything. | ||
| 392 | + level: u8, | ||
| 385 | dc: Option<bool>, | 393 | dc: Option<bool>, |
| 386 | } | 394 | } |
| 387 | 395 | ||
| 388 | fn neighbour_state(grid: &[i32]) -> Neighbour { | 396 | fn neighbour_state(grid: &[i32]) -> Neighbour { |
| 397 | + let cul: u32 = grid.iter().map(|&l| l.unsigned_abs()).sum(); | ||
| 389 | Neighbour { | 398 | Neighbour { |
| 390 | - coded: grid.iter().any(|&l| l != 0), | 399 | + level: cul.min(7) as u8, |
| 391 | dc: (grid[0] != 0).then_some(grid[0] < 0), | 400 | dc: (grid[0] != 0).then_some(grid[0] < 0), |
| 392 | } | 401 | } |
| 393 | } | 402 | } |
| 394 | 403 | ||
| 404 | +/// spec `get_txb_ctx_general`'s `skip_contexts[top][left]` table (plane 0, | ||
| 405 | +/// transform unit smaller than the block it sits in): `top`/`left` are the | ||
| 406 | +/// above/left neighbours' magnitude tiers, each already clamped to 4. | ||
| 407 | +const SKIP_CONTEXTS: [[usize; 5]; 5] = [ | ||
| 408 | + [1, 2, 2, 2, 3], | ||
| 409 | + [2, 4, 4, 4, 5], | ||
| 410 | + [2, 4, 4, 4, 5], | ||
| 411 | + [2, 4, 4, 4, 5], | ||
| 412 | + [3, 5, 5, 5, 6], | ||
| 413 | +]; | ||
| 414 | + | ||
| 395 | /// The neighbour state a block's contexts are read from — a duplicate of | 415 | /// The neighbour state a block's contexts are read from — a duplicate of |
| 396 | /// [`crate::tile`]'s own private `Neighbours` (that module is another lane's | 416 | /// [`crate::tile`]'s own private `Neighbours` (that module is another lane's |
| 397 | /// territory this round), kept at the same two granularities the real writer | 417 | /// territory this round), kept at the same two granularities the real writer |
| @@ -601,14 +621,37 @@ impl Neighbours { | |||
| 601 | for cell in 0..side_mi { | 621 | for cell in 0..side_mi { |
| 602 | let above = &self.above[mi_c + cell][plane]; | 622 | let above = &self.above[mi_c + cell][plane]; |
| 603 | let left = &self.left[mi_r + cell][plane]; | 623 | let left = &self.left[mi_r + cell][plane]; |
| 604 | - above_coded |= above.coded; | 624 | + above_coded |= above.level != 0; |
| 605 | - left_coded |= left.coded; | 625 | + left_coded |= left.level != 0; |
| 606 | vote += dc_vote(above.dc) + dc_vote(left.dc); | 626 | vote += dc_vote(above.dc) + dc_vote(left.dc); |
| 607 | } | 627 | } |
| 608 | (above_coded, left_coded, vote) | 628 | (above_coded, left_coded, vote) |
| 609 | }) | 629 | }) |
| 610 | } | 630 | } |
| 611 | 631 | ||
| 632 | + /// The luma `txb_skip_ctx` of a transform unit smaller than its own block | ||
| 633 | + /// (spec `get_txb_ctx_general`, plane 0, `plane_bsize != tx_size`): the | ||
| 634 | + /// above/left neighbours' magnitude tiers, OR-reduced over the unit's own | ||
| 635 | + /// span and each clamped to 4, indexed into [`SKIP_CONTEXTS`]. Unlike | ||
| 636 | + /// [`Self::around_mi`]'s plain coded/uncoded bit, this needs the real | ||
| 637 | + /// cumulative level -- a lone-TU block never calls this (its `txb_skip_ctx` | ||
| 638 | + /// is always 0, spec's `plane_bsize == tx_size` branch). | ||
| 639 | + fn luma_skip_ctx(&self, (mi_r, mi_c): (usize, usize), side_mi: usize) -> usize { | ||
| 640 | + let mut top = 0u8; | ||
| 641 | + let mut left = 0u8; | ||
| 642 | + for cell in 0..side_mi { | ||
| 643 | + top |= self.above[mi_c + cell][0].level; | ||
| 644 | + left |= self.left[mi_r + cell][0].level; | ||
| 645 | + } | ||
| 646 | + let ctx = SKIP_CONTEXTS[(top as usize).min(4)][(left as usize).min(4)]; | ||
| 647 | + if std::env::var_os("EC_AV1_TRACE").is_some() { | ||
| 648 | + eprintln!( | ||
| 649 | + "TRACE luma_skip_ctx mi=({mi_r},{mi_c}) side_mi={side_mi} top={top} left={left} ctx={ctx}" | ||
| 650 | + ); | ||
| 651 | + } | ||
| 652 | + ctx | ||
| 653 | + } | ||
| 654 | + | ||
| 612 | /// Writes one coded block into every cell it covers, on both grids: the | 655 | /// Writes one coded block into every cell it covers, on both grids: the |
| 613 | /// mode/side arrays up to `side`'s own width, and the coefficient-context | 656 | /// mode/side arrays up to `side`'s own width, and the coefficient-context |
| 614 | /// arrays up to the true frame edge — the units past it are left at their | 657 | /// arrays up to the true frame edge — the units past it are left at their |
| @@ -1032,9 +1075,16 @@ fn read_plane( | |||
| 1032 | base_q_idx: u8, | 1075 | base_q_idx: u8, |
| 1033 | cfl: Option<(i32, &[i32])>, | 1076 | cfl: Option<(i32, &[i32])>, |
| 1034 | filter_intra: Option<usize>, | 1077 | filter_intra: Option<usize>, |
| 1078 | + // Only meaningful for `plane_idx == 0`: `Some` when this transform unit is | ||
| 1079 | + // smaller than its own block (spec `get_txb_ctx_general`'s | ||
| 1080 | + // `plane_bsize != tx_size` branch), carrying the already-looked-up | ||
| 1081 | + // `SKIP_CONTEXTS` value; `None` everywhere else (a lone luma TU, or any | ||
| 1082 | + // chroma plane), where `txb_skip_ctx` is 0 or the usual OR-of-neighbours | ||
| 1083 | + // formula below. | ||
| 1084 | + luma_skip_ctx: Option<usize>, | ||
| 1035 | ) -> Result<Vec<i32>> { | 1085 | ) -> Result<Vec<i32>> { |
| 1036 | let skip_ctx = if plane_idx == 0 { | 1086 | let skip_ctx = if plane_idx == 0 { |
| 1037 | - 0 | 1087 | + luma_skip_ctx.unwrap_or(0) |
| 1038 | } else { | 1088 | } else { |
| 1039 | usize::from(around.0) + usize::from(around.1) | 1089 | usize::from(around.0) + usize::from(around.1) |
| 1040 | }; | 1090 | }; |
| @@ -1215,6 +1265,7 @@ fn decode_block( | |||
| 1215 | base_q_idx, | 1265 | base_q_idx, |
| 1216 | None, | 1266 | None, |
| 1217 | filter_intra, | 1267 | filter_intra, |
| 1268 | + None, | ||
| 1218 | )?; | 1269 | )?; |
| 1219 | let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); | 1270 | let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); |
| 1220 | let u_grid = read_plane( | 1271 | let u_grid = read_plane( |
| @@ -1235,6 +1286,7 @@ fn decode_block( | |||
| 1235 | base_q_idx, | 1286 | base_q_idx, |
| 1236 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), | 1287 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), |
| 1237 | None, | 1288 | None, |
| 1289 | + None, | ||
| 1238 | )?; | 1290 | )?; |
| 1239 | let v_grid = read_plane( | 1291 | let v_grid = read_plane( |
| 1240 | dec, | 1292 | dec, |
| @@ -1254,6 +1306,7 @@ fn decode_block( | |||
| 1254 | base_q_idx, | 1306 | base_q_idx, |
| 1255 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), | 1307 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), |
| 1256 | None, | 1308 | None, |
| 1309 | + None, | ||
| 1257 | )?; | 1310 | )?; |
| 1258 | neighbours.record(at, side, mode, &[luma_grid, u_grid, v_grid]); | 1311 | neighbours.record(at, side, mode, &[luma_grid, u_grid, v_grid]); |
| 1259 | } else { | 1312 | } else { |
| @@ -1274,6 +1327,11 @@ fn decode_block( | |||
| 1274 | let tu_py = py + tu_row * logical_tx; | 1327 | let tu_py = py + tu_row * logical_tx; |
| 1275 | let tu_around = neighbours.around_mi(tu_mi, logical_tx)[0]; | 1328 | let tu_around = neighbours.around_mi(tu_mi, logical_tx)[0]; |
| 1276 | let tu_reach = Reach::of(logical_tx, tu_px, tu_py, y.width, y.height); | 1329 | let tu_reach = Reach::of(logical_tx, tu_px, tu_py, y.width, y.height); |
| 1330 | + // This transform unit's own bsize (`logical_tx`) is smaller | ||
| 1331 | + // than the block it sits in (`side`), so `txb_skip_ctx` is | ||
| 1332 | + // the neighbour-magnitude table, not the lone-TU 0 (spec | ||
| 1333 | + // `get_txb_ctx_general`'s `plane_bsize != tx_size` branch). | ||
| 1334 | + let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, logical_tx / MI); | ||
| 1277 | let tu_grid = read_plane( | 1335 | let tu_grid = read_plane( |
| 1278 | dec, | 1336 | dec, |
| 1279 | cdfs, | 1337 | cdfs, |
| @@ -1292,6 +1350,7 @@ fn decode_block( | |||
| 1292 | base_q_idx, | 1350 | base_q_idx, |
| 1293 | None, | 1351 | None, |
| 1294 | filter_intra, | 1352 | filter_intra, |
| 1353 | + Some(tu_skip_ctx), | ||
| 1295 | )?; | 1354 | )?; |
| 1296 | neighbours.record_mi_luma(tu_mi, logical_tx, &tu_grid); | 1355 | neighbours.record_mi_luma(tu_mi, logical_tx, &tu_grid); |
| 1297 | } | 1356 | } |
| @@ -1316,6 +1375,7 @@ fn decode_block( | |||
| 1316 | base_q_idx, | 1375 | base_q_idx, |
| 1317 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), | 1376 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), |
| 1318 | None, | 1377 | None, |
| 1378 | + None, | ||
| 1319 | )?; | 1379 | )?; |
| 1320 | let v_grid = read_plane( | 1380 | let v_grid = read_plane( |
| 1321 | dec, | 1381 | dec, |
| @@ -1335,6 +1395,7 @@ fn decode_block( | |||
| 1335 | base_q_idx, | 1395 | base_q_idx, |
| 1336 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), | 1396 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), |
| 1337 | None, | 1397 | None, |
| 1398 | + None, | ||
| 1338 | )?; | 1399 | )?; |
| 1339 | neighbours.record_split_luma(at, side, mode, [&u_grid, &v_grid]); | 1400 | neighbours.record_split_luma(at, side, mode, [&u_grid, &v_grid]); |
| 1340 | } | 1401 | } |
| @@ -1458,6 +1519,7 @@ fn decode_leaf8( | |||
| 1458 | base_q_idx, | 1519 | base_q_idx, |
| 1459 | None, | 1520 | None, |
| 1460 | filter_intra, | 1521 | filter_intra, |
| 1522 | + None, | ||
| 1461 | )?; | 1523 | )?; |
| 1462 | let ac = alpha.map(|_| cfl_ac_q3(y, px, py, 8)); | 1524 | let ac = alpha.map(|_| cfl_ac_q3(y, px, py, 8)); |
| 1463 | u_grid = read_plane( | 1525 | u_grid = read_plane( |
| @@ -1478,6 +1540,7 @@ fn decode_leaf8( | |||
| 1478 | base_q_idx, | 1540 | base_q_idx, |
| 1479 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), | 1541 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), |
| 1480 | None, | 1542 | None, |
| 1543 | + None, | ||
| 1481 | )?; | 1544 | )?; |
| 1482 | v_grid = read_plane( | 1545 | v_grid = read_plane( |
| 1483 | dec, | 1546 | dec, |
| @@ -1497,6 +1560,7 @@ fn decode_leaf8( | |||
| 1497 | base_q_idx, | 1560 | base_q_idx, |
| 1498 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), | 1561 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), |
| 1499 | None, | 1562 | None, |
| 1563 | + None, | ||
| 1500 | )?; | 1564 | )?; |
| 1501 | } | 1565 | } |
| 1502 | neighbours.record_mi(leaf_mi, 8, &[luma_grid, u_grid, v_grid]); | 1566 | neighbours.record_mi(leaf_mi, 8, &[luma_grid, u_grid, v_grid]); |
| @@ -2570,6 +2634,12 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( | |||
| 2570 | let ctx16 = neighbours.partition_ctx(at16, SUB); | 2634 | let ctx16 = neighbours.partition_ctx(at16, SUB); |
| 2571 | if has_cols16 && has_rows16 { | 2635 | if has_cols16 && has_rows16 { |
| 2572 | let part16 = dec.symbol(&mut cdfs.partition_w16[ctx16]); | 2636 | let part16 = dec.symbol(&mut cdfs.partition_w16[ctx16]); |
| 2637 | + if std::env::var_os("EC_AV1_TRACE").is_some() { | ||
| 2638 | + eprintln!( | ||
| 2639 | + "TRACE partition_w16 mi=({},{}) ctx={ctx16} value={part16}", | ||
| 2640 | + at16.0, at16.1 | ||
| 2641 | + ); | ||
| 2642 | + } | ||
| 2573 | if part16 == PARTITION_NONE { | 2643 | if part16 == PARTITION_NONE { |
| 2574 | decode_block( | 2644 | decode_block( |
| 2575 | &mut dec, | 2645 | &mut dec, |
| @@ -3265,6 +3335,7 @@ fn decode_inter_block( | |||
| 3265 | base_q_idx, | 3335 | base_q_idx, |
| 3266 | None, | 3336 | None, |
| 3267 | None, | 3337 | None, |
| 3338 | + None, | ||
| 3268 | )?; | 3339 | )?; |
| 3269 | let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); | 3340 | let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); |
| 3270 | u_grid = read_plane( | 3341 | u_grid = read_plane( |
| @@ -3285,6 +3356,7 @@ fn decode_inter_block( | |||
| 3285 | base_q_idx, | 3356 | base_q_idx, |
| 3286 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), | 3357 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), |
| 3287 | None, | 3358 | None, |
| 3359 | + None, | ||
| 3288 | )?; | 3360 | )?; |
| 3289 | v_grid = read_plane( | 3361 | v_grid = read_plane( |
| 3290 | dec, | 3362 | dec, |
| @@ -3304,6 +3376,7 @@ fn decode_inter_block( | |||
| 3304 | base_q_idx, | 3376 | base_q_idx, |
| 3305 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), | 3377 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), |
| 3306 | None, | 3378 | None, |
| 3379 | + None, | ||
| 3307 | )?; | 3380 | )?; |
| 3308 | } | 3381 | } |
| 3309 | } | 3382 | } |
| @@ -3660,6 +3733,7 @@ fn decode_inter_block8( | |||
| 3660 | base_q_idx, | 3733 | base_q_idx, |
| 3661 | None, | 3734 | None, |
| 3662 | None, | 3735 | None, |
| 3736 | + None, | ||
| 3663 | )?; | 3737 | )?; |
| 3664 | u_grid = read_plane( | 3738 | u_grid = read_plane( |
| 3665 | dec, | 3739 | dec, |
| @@ -3679,6 +3753,7 @@ fn decode_inter_block8( | |||
| 3679 | base_q_idx, | 3753 | base_q_idx, |
| 3680 | None, | 3754 | None, |
| 3681 | None, | 3755 | None, |
| 3756 | + None, | ||
| 3682 | )?; | 3757 | )?; |
| 3683 | v_grid = read_plane( | 3758 | v_grid = read_plane( |
| 3684 | dec, | 3759 | dec, |
| @@ -3698,6 +3773,7 @@ fn decode_inter_block8( | |||
| 3698 | base_q_idx, | 3773 | base_q_idx, |
| 3699 | None, | 3774 | None, |
| 3700 | None, | 3775 | None, |
| 3776 | + None, | ||
| 3701 | )?; | 3777 | )?; |
| 3702 | } | 3778 | } |
| 3703 | } | 3779 | } |