Code coefficients anywhere in a 32x32 transform
Browse files at this commit0dac98480fdde4a1421141319d4df2369e38c104Parent: a25e4a3- feat
- code coefficients anywhere in a 32x32 transform
2 changed files +781 -11
- crates/ec-av1/src/cdf.rs +107 -9
- crates/ec-av1/src/tile.rs +674 -2
crates/ec-av1/src/cdf.rsmodified+107 -9
| @@ -229,11 +229,6 @@ pub const COEFF_BASE_EOB_LUMA_64: [u16; 4] = [935, 3382, 32768, 0]; | |||
| 229 | /// positive (2) or neither (0). | 229 | /// positive (2) or neither (0). |
| 230 | pub const DC_SIGN_LUMA: [[u16; 3]; 3] = [[16000, 32768, 0], [13056, 32768, 0], [18816, 32768, 0]]; | 230 | pub const DC_SIGN_LUMA: [[u16; 3]; 3] = [[16000, 32768, 0], [13056, 32768, 0], [18816, 32768, 0]]; |
| 231 | 231 | ||
| 232 | -/// `Default_Coeff_Br_Cdf[2][3][0][0]` (spec 9.4): a base-range increment of a | ||
| 233 | -/// coefficient whose neighbours are all zero, in a transform of 32x32 or | ||
| 234 | -/// larger (the table is shared from 32x32 up). | ||
| 235 | -pub const COEFF_BR_LUMA: [u16; 5] = [9040, 14786, 18360, 32768, 0]; | ||
| 236 | - | ||
| 237 | /// `Default_Partition_W32_Cdf` (spec 9.4): the ten partition types of a | 232 | /// `Default_Partition_W32_Cdf` (spec 9.4): the ten partition types of a |
| 238 | /// 32x32 block, indexed by the context the blocks above and left give. | 233 | /// 32x32 block, indexed by the context the blocks above and left give. |
| 239 | pub const PARTITION_W32: [[u16; 11]; 4] = [ | 234 | pub const PARTITION_W32: [[u16; 11]; 4] = [ |
| @@ -269,7 +264,110 @@ pub const TXB_SKIP_LUMA_32: [u16; 3] = [30669, 32768, 0]; | |||
| 269 | /// neighbour, which is context 7. | 264 | /// neighbour, which is context 7. |
| 270 | pub const TXB_SKIP_CHROMA_16: [u16; 3] = [3648, 32768, 0]; | 265 | pub const TXB_SKIP_CHROMA_16: [u16; 3] = [3648, 32768, 0]; |
| 271 | 266 | ||
| 272 | -/// `Default_Coeff_Base_Eob_Cdf[2][3][0][0]` (spec 9.4): the level of the last | 267 | +/// `Default_Coeff_Base_Cdf[2][3][0]` (spec 9.4): the level of a coefficient |
| 273 | -/// coefficient of a 32x32 luma transform block, less one, when that | 268 | +/// of a 32x32 luma transform block, capped at three, for each of the |
| 274 | -/// coefficient is the DC. | 269 | +/// forty-two contexts its neighbours' magnitudes and its own position give. |
| 275 | -pub const COEFF_BASE_EOB_LUMA_32: [u16; 4] = [2809, 19301, 32768, 0]; | 270 | +pub const COEFF_BASE_LUMA_32: [[u16; 5]; 42] = [ |
| 271 | + [4456, 11274, 15533, 32768, 0], | ||
| 272 | + [21219, 29079, 31616, 32768, 0], | ||
| 273 | + [11173, 23774, 28567, 32768, 0], | ||
| 274 | + [7282, 18293, 24263, 32768, 0], | ||
| 275 | + [4890, 13286, 19115, 32768, 0], | ||
| 276 | + [1890, 5508, 8659, 32768, 0], | ||
| 277 | + [26651, 32136, 32647, 32768, 0], | ||
| 278 | + [14630, 28254, 31455, 32768, 0], | ||
| 279 | + [8716, 21287, 27395, 32768, 0], | ||
| 280 | + [5615, 15331, 22008, 32768, 0], | ||
| 281 | + [2675, 7700, 12150, 32768, 0], | ||
| 282 | + [29954, 32526, 32690, 32768, 0], | ||
| 283 | + [16126, 28982, 31633, 32768, 0], | ||
| 284 | + [9030, 21361, 27352, 32768, 0], | ||
| 285 | + [5411, 14793, 21271, 32768, 0], | ||
| 286 | + [2943, 8422, 13163, 32768, 0], | ||
| 287 | + [29539, 32601, 32730, 32768, 0], | ||
| 288 | + [18125, 30385, 32201, 32768, 0], | ||
| 289 | + [10422, 24090, 29468, 32768, 0], | ||
| 290 | + [6468, 17487, 24438, 32768, 0], | ||
| 291 | + [2970, 8653, 13531, 32768, 0], | ||
| 292 | + [30912, 32715, 32748, 32768, 0], | ||
| 293 | + [20666, 31373, 32497, 32768, 0], | ||
| 294 | + [12509, 26640, 30917, 32768, 0], | ||
| 295 | + [8058, 20629, 27290, 32768, 0], | ||
| 296 | + [4231, 12006, 18052, 32768, 0], | ||
| 297 | + [8192, 16384, 24576, 32768, 0], | ||
| 298 | + [8192, 16384, 24576, 32768, 0], | ||
| 299 | + [8192, 16384, 24576, 32768, 0], | ||
| 300 | + [8192, 16384, 24576, 32768, 0], | ||
| 301 | + [8192, 16384, 24576, 32768, 0], | ||
| 302 | + [8192, 16384, 24576, 32768, 0], | ||
| 303 | + [8192, 16384, 24576, 32768, 0], | ||
| 304 | + [8192, 16384, 24576, 32768, 0], | ||
| 305 | + [8192, 16384, 24576, 32768, 0], | ||
| 306 | + [8192, 16384, 24576, 32768, 0], | ||
| 307 | + [8192, 16384, 24576, 32768, 0], | ||
| 308 | + [8192, 16384, 24576, 32768, 0], | ||
| 309 | + [8192, 16384, 24576, 32768, 0], | ||
| 310 | + [8192, 16384, 24576, 32768, 0], | ||
| 311 | + [8192, 16384, 24576, 32768, 0], | ||
| 312 | + [8192, 16384, 24576, 32768, 0], | ||
| 313 | +]; | ||
| 314 | + | ||
| 315 | +/// `Default_Coeff_Br_Cdf[2][3][0]` (spec 9.4): a base-range increment in a | ||
| 316 | +/// 32x32 luma transform block, for each of the twenty-one contexts. | ||
| 317 | +pub const COEFF_BR_LUMA_32: [[u16; 5]; 21] = [ | ||
| 318 | + [9040, 14786, 18360, 32768, 0], | ||
| 319 | + [9979, 15718, 19415, 32768, 0], | ||
| 320 | + [7913, 13918, 18311, 32768, 0], | ||
| 321 | + [5859, 10889, 15184, 32768, 0], | ||
| 322 | + [4593, 8677, 12510, 32768, 0], | ||
| 323 | + [3820, 7396, 10791, 32768, 0], | ||
| 324 | + [1730, 3471, 5192, 32768, 0], | ||
| 325 | + [11803, 18365, 22709, 32768, 0], | ||
| 326 | + [11419, 18058, 22225, 32768, 0], | ||
| 327 | + [9418, 15774, 20243, 32768, 0], | ||
| 328 | + [7539, 13325, 17657, 32768, 0], | ||
| 329 | + [6233, 11317, 15384, 32768, 0], | ||
| 330 | + [5137, 9656, 13545, 32768, 0], | ||
| 331 | + [2977, 5774, 8349, 32768, 0], | ||
| 332 | + [21207, 27246, 29640, 32768, 0], | ||
| 333 | + [19547, 26578, 29497, 32768, 0], | ||
| 334 | + [16169, 23871, 27690, 32768, 0], | ||
| 335 | + [12820, 20458, 25018, 32768, 0], | ||
| 336 | + [10224, 17332, 22214, 32768, 0], | ||
| 337 | + [8526, 15048, 19884, 32768, 0], | ||
| 338 | + [5037, 9410, 13118, 32768, 0], | ||
| 339 | +]; | ||
| 340 | + | ||
| 341 | +/// `Default_Coeff_Base_Eob_Cdf[2][3][0]` (spec 9.4): the level of the last | ||
| 342 | +/// coefficient of a 32x32 luma transform block, less one, for each of the | ||
| 343 | +/// four contexts its position in the scan gives. | ||
| 344 | +pub const COEFF_BASE_EOB_LUMA_32: [[u16; 4]; 4] = [ | ||
| 345 | + [2809, 19301, 32768, 0], | ||
| 346 | + [32205, 32622, 32768, 0], | ||
| 347 | + [32338, 32730, 32768, 0], | ||
| 348 | + [31786, 32616, 32768, 0], | ||
| 349 | +]; | ||
| 350 | + | ||
| 351 | +/// `Default_Eob_Extra_Cdf[2][3][0]` (spec 9.4): the top bit of the offset | ||
| 352 | +/// into an end-of-block position group, for each group. | ||
| 353 | +pub const EOB_EXTRA_LUMA_32: [[u16; 3]; 9] = [ | ||
| 354 | + [23312, 32768, 0], | ||
| 355 | + [21607, 32768, 0], | ||
| 356 | + [16526, 32768, 0], | ||
| 357 | + [18957, 32768, 0], | ||
| 358 | + [18034, 32768, 0], | ||
| 359 | + [18934, 32768, 0], | ||
| 360 | + [24247, 32768, 0], | ||
| 361 | + [16921, 32768, 0], | ||
| 362 | + [17080, 32768, 0], | ||
| 363 | +]; | ||
| 364 | + | ||
| 365 | +/// `Av1_Nz_Map_Ctx_Offset` for a 32x32 transform (the position term of the | ||
| 366 | +/// coefficient-level context, spec 8.3.2): row and column clamped to four. | ||
| 367 | +pub const NZ_MAP_CTX_OFFSET_32: [[u8; 5]; 5] = [ | ||
| 368 | + [0, 1, 6, 6, 21], | ||
| 369 | + [1, 6, 6, 21, 21], | ||
| 370 | + [6, 6, 21, 21, 21], | ||
| 371 | + [6, 21, 21, 21, 21], | ||
| 372 | + [21, 21, 21, 21, 21], | ||
| 373 | +]; | ||
crates/ec-av1/src/tile.rsmodified+674 -2
| @@ -244,7 +244,7 @@ pub fn split_dc_key_frame_tile( | |||
| 244 | dc_level, | 244 | dc_level, |
| 245 | dc_sign_ctx(above[c as usize], left[r as usize]), | 245 | dc_sign_ctx(above[c as usize], left[r as usize]), |
| 246 | &cdf::TXB_SKIP_LUMA_32, | 246 | &cdf::TXB_SKIP_LUMA_32, |
| 247 | - &cdf::COEFF_BASE_EOB_LUMA_32, | 247 | + &cdf::COEFF_BASE_EOB_LUMA_32[0], |
| 248 | ); | 248 | ); |
| 249 | enc.symbol_fixed(1, &cdf::TXB_SKIP_CHROMA_16); | 249 | enc.symbol_fixed(1, &cdf::TXB_SKIP_CHROMA_16); |
| 250 | enc.symbol_fixed(1, &cdf::TXB_SKIP_CHROMA_16); | 250 | enc.symbol_fixed(1, &cdf::TXB_SKIP_CHROMA_16); |
| @@ -257,6 +257,318 @@ pub fn split_dc_key_frame_tile( | |||
| 257 | Ok(enc.finish()) | 257 | Ok(enc.finish()) |
| 258 | } | 258 | } |
| 259 | 259 | ||
| 260 | +/// One quantised coefficient of a 32x32 luma transform block. | ||
| 261 | +/// | ||
| 262 | +/// `row` and `col` are its position in the transform, not in the picture: a | ||
| 263 | +/// coefficient at row 0 varies along the picture's width and one at column 0 | ||
| 264 | +/// along its height, and the coefficient at the origin is the DC the block's | ||
| 265 | +/// average sample value rides on. | ||
| 266 | +#[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
| 267 | +pub struct Coeff { | ||
| 268 | + /// The coefficient's row in the transform, 0..32. | ||
| 269 | + pub row: u8, | ||
| 270 | + /// The coefficient's column in the transform, 0..32. | ||
| 271 | + pub col: u8, | ||
| 272 | + /// Its quantised level, which the base and base-range syntax carry for | ||
| 273 | + /// magnitudes up to [`MAX_LEVEL`]. | ||
| 274 | + pub level: i32, | ||
| 275 | +} | ||
| 276 | + | ||
| 277 | +/// Side of the luma transform the coefficient writer codes. | ||
| 278 | +const TX32: usize = 32; | ||
| 279 | +/// How many coefficients that transform holds. | ||
| 280 | +const TX32_AREA: usize = TX32 * TX32; | ||
| 281 | + | ||
| 282 | +/// Writes the payload of a one-tile key frame in which every superblock is | ||
| 283 | +/// split into four 32x32 DC-predicted blocks, each carrying the luma | ||
| 284 | +/// coefficients `blocks` gives it and no chroma residual. | ||
| 285 | +/// | ||
| 286 | +/// `blocks` gives one coefficient list per 32x32 block in raster order across | ||
| 287 | +/// the frame, so it is twice as wide and twice as tall as the superblock grid. | ||
| 288 | +/// An empty list writes an all-zero transform block. Unlike the DC writers, | ||
| 289 | +/// the coefficients may sit anywhere in the transform, so a block here is a | ||
| 290 | +/// picture rather than a flat grey: a single coefficient off the origin | ||
| 291 | +/// decodes to the basis function it selects. | ||
| 292 | +/// | ||
| 293 | +/// # Errors | ||
| 294 | +/// Returns an error when the frame is not a whole number of 64x64 | ||
| 295 | +/// superblocks, when `blocks` does not carry exactly one list per 32x32 block, | ||
| 296 | +/// when a coefficient sits outside the transform, repeats a position, carries | ||
| 297 | +/// a zero level or one wider than the base and base-range syntax reach, or | ||
| 298 | +/// when `base_q_idx` is outside the q-context band whose default CDFs this | ||
| 299 | +/// crate carries. | ||
| 300 | +pub fn split_coeff_key_frame_tile( | ||
| 301 | + mi_cols: u32, | ||
| 302 | + mi_rows: u32, | ||
| 303 | + base_q_idx: u8, | ||
| 304 | + blocks: &[Vec<Coeff>], | ||
| 305 | +) -> Result<Vec<u8>> { | ||
| 306 | + check_superblocks(mi_cols, mi_rows)?; | ||
| 307 | + let (sb_cols, sb_rows) = (mi_cols / SB_MI, mi_rows / SB_MI); | ||
| 308 | + let (cols, rows) = (sb_cols * 2, sb_rows * 2); | ||
| 309 | + if blocks.len() != (cols * rows) as usize { | ||
| 310 | + return Err(Error::unsupported( | ||
| 311 | + "AV1 tile", | ||
| 312 | + "a coefficient key frame needs one coefficient list per 32x32 block", | ||
| 313 | + )); | ||
| 314 | + } | ||
| 315 | + if !Q_CTX_2.contains(&base_q_idx) { | ||
| 316 | + return Err(Error::unsupported( | ||
| 317 | + "AV1 tile", | ||
| 318 | + "the coefficient CDFs of only one q context are known, so \ | ||
| 319 | + base_q_idx must be 61..=120", | ||
| 320 | + )); | ||
| 321 | + } | ||
| 322 | + let grids = blocks | ||
| 323 | + .iter() | ||
| 324 | + .map(|b| level_grid(b)) | ||
| 325 | + .collect::<Result<Vec<_>>>()?; | ||
| 326 | + | ||
| 327 | + let scan = default_scan_32(); | ||
| 328 | + // The sign of each coded block's DC, for the neighbours the DC sign | ||
| 329 | + // context is read from. A block whose DC is zero casts no vote, however | ||
| 330 | + // much its other coefficients carry. | ||
| 331 | + let mut above: Vec<Option<bool>> = vec![None; cols as usize]; | ||
| 332 | + let mut left: Vec<Option<bool>> = vec![None; rows as usize]; | ||
| 333 | + | ||
| 334 | + let mut enc = SymbolEncoder::new(); | ||
| 335 | + for sb_r in 0..sb_rows { | ||
| 336 | + left.iter_mut().for_each(|l| *l = None); | ||
| 337 | + for sb_c in 0..sb_cols { | ||
| 338 | + let ctx = 2 * usize::from(sb_c > 0) + usize::from(sb_r > 0); | ||
| 339 | + enc.symbol_fixed(PARTITION_SPLIT, &cdf::PARTITION_W64[ctx]); | ||
| 340 | + | ||
| 341 | + for quadrant in 0..4 { | ||
| 342 | + let (r, c) = (sb_r * 2 + quadrant / 2, sb_c * 2 + quadrant % 2); | ||
| 343 | + let grid = &grids[(r * cols + c) as usize]; | ||
| 344 | + | ||
| 345 | + enc.symbol_fixed(PARTITION_NONE, &cdf::PARTITION_W32[0]); | ||
| 346 | + enc.symbol_fixed(0, &cdf::SKIP[0]); | ||
| 347 | + enc.symbol_fixed(DC_PRED, &cdf::KF_Y_MODE[0][0]); | ||
| 348 | + enc.symbol_fixed(DC_PRED, &cdf::UV_MODE_CFL_DC); | ||
| 349 | + | ||
| 350 | + write_coeffs( | ||
| 351 | + &mut enc, | ||
| 352 | + grid, | ||
| 353 | + &scan, | ||
| 354 | + dc_sign_ctx(above[c as usize], left[r as usize]), | ||
| 355 | + ); | ||
| 356 | + enc.symbol_fixed(1, &cdf::TXB_SKIP_CHROMA_16); | ||
| 357 | + enc.symbol_fixed(1, &cdf::TXB_SKIP_CHROMA_16); | ||
| 358 | + | ||
| 359 | + let dc = grid[0]; | ||
| 360 | + let vote = (dc != 0).then_some(dc < 0); | ||
| 361 | + above[c as usize] = vote; | ||
| 362 | + left[r as usize] = vote; | ||
| 363 | + } | ||
| 364 | + } | ||
| 365 | + } | ||
| 366 | + Ok(enc.finish()) | ||
| 367 | +} | ||
| 368 | + | ||
| 369 | +/// Lays one block's coefficient list out over the transform, rejecting the | ||
| 370 | +/// positions and levels the writer does not code. | ||
| 371 | +fn level_grid(coeffs: &[Coeff]) -> Result<Vec<i32>> { | ||
| 372 | + let mut grid = vec![0i32; TX32_AREA]; | ||
| 373 | + for coeff in coeffs { | ||
| 374 | + if usize::from(coeff.row) >= TX32 || usize::from(coeff.col) >= TX32 { | ||
| 375 | + return Err(Error::unsupported( | ||
| 376 | + "AV1 tile", | ||
| 377 | + "a coefficient sits outside the 32x32 transform the writer codes", | ||
| 378 | + )); | ||
| 379 | + } | ||
| 380 | + if coeff.level == 0 || coeff.level.abs() > MAX_LEVEL { | ||
| 381 | + return Err(Error::unsupported( | ||
| 382 | + "AV1 tile", | ||
| 383 | + "coefficients are written for levels -14..=14 without zero; \ | ||
| 384 | + wider levels need the Golomb tail", | ||
| 385 | + )); | ||
| 386 | + } | ||
| 387 | + let pos = usize::from(coeff.row) * TX32 + usize::from(coeff.col); | ||
| 388 | + if grid[pos] != 0 { | ||
| 389 | + return Err(Error::unsupported( | ||
| 390 | + "AV1 tile", | ||
| 391 | + "two coefficients of one block share a position", | ||
| 392 | + )); | ||
| 393 | + } | ||
| 394 | + grid[pos] = coeff.level; | ||
| 395 | + } | ||
| 396 | + Ok(grid) | ||
| 397 | +} | ||
| 398 | + | ||
| 399 | +/// The default scan of a 32x32 transform (spec 8.4.2's `Default_Scan_32x32`), | ||
| 400 | +/// as raster positions in the order they are coded. | ||
| 401 | +/// | ||
| 402 | +/// The table is a rule rather than a thousand pinned numbers: the scan walks | ||
| 403 | +/// the anti-diagonals of the transform outwards from the origin, each diagonal | ||
| 404 | +/// from its top-right end on odd diagonals and from its bottom-left end on | ||
| 405 | +/// even ones. | ||
| 406 | +fn default_scan_32() -> Vec<u16> { | ||
| 407 | + let mut scan = Vec::with_capacity(TX32_AREA); | ||
| 408 | + for d in 0..(2 * TX32 - 1) { | ||
| 409 | + let lo = d.saturating_sub(TX32 - 1); | ||
| 410 | + let hi = d.min(TX32 - 1); | ||
| 411 | + let diagonal = (lo..=hi).map(|row| (row * TX32 + (d - row)) as u16); | ||
| 412 | + if d % 2 == 0 { | ||
| 413 | + scan.extend(diagonal.rev()); | ||
| 414 | + } else { | ||
| 415 | + scan.extend(diagonal); | ||
| 416 | + } | ||
| 417 | + } | ||
| 418 | + scan | ||
| 419 | +} | ||
| 420 | + | ||
| 421 | +/// coeffs() for a luma transform block of any coefficients the base and | ||
| 422 | +/// base-range syntax reach (spec 5.11.39). | ||
| 423 | +/// | ||
| 424 | +/// The block is its own transform size, so the all-zero flag's context is 0; | ||
| 425 | +/// a 32x32 transform is DCT-only, so no transform type is coded; and the | ||
| 426 | +/// levels the contexts below are read from are the levels of coefficients | ||
| 427 | +/// later in the scan, which a decoder walking the scan backwards already has. | ||
| 428 | +fn write_coeffs(enc: &mut SymbolEncoder, grid: &[i32], scan: &[u16], sign_ctx: usize) { | ||
| 429 | + let eob = scan | ||
| 430 | + .iter() | ||
| 431 | + .rposition(|&pos| grid[pos as usize] != 0) | ||
| 432 | + .map_or(0, |i| i + 1); | ||
| 433 | + enc.symbol_fixed(usize::from(eob == 0), &cdf::TXB_SKIP_LUMA_32); | ||
| 434 | + if eob == 0 { | ||
| 435 | + return; | ||
| 436 | + } | ||
| 437 | + | ||
| 438 | + write_eob(enc, eob); | ||
| 439 | + | ||
| 440 | + for scan_idx in (0..eob).rev() { | ||
| 441 | + let pos = scan[scan_idx] as usize; | ||
| 442 | + let (row, col) = (pos / TX32, pos % TX32); | ||
| 443 | + let level = grid[pos].abs(); | ||
| 444 | + if scan_idx == eob - 1 { | ||
| 445 | + let ctx = eob_coeff_ctx(scan_idx); | ||
| 446 | + enc.symbol_fixed( | ||
| 447 | + (level.min(NUM_BASE_LEVELS + 1) - 1) as usize, | ||
| 448 | + &cdf::COEFF_BASE_EOB_LUMA_32[ctx], | ||
| 449 | + ); | ||
| 450 | + } else { | ||
| 451 | + let ctx = base_ctx(grid, row, col); | ||
| 452 | + enc.symbol_fixed( | ||
| 453 | + level.min(NUM_BASE_LEVELS + 1) as usize, | ||
| 454 | + &cdf::COEFF_BASE_LUMA_32[ctx], | ||
| 455 | + ); | ||
| 456 | + } | ||
| 457 | + if level > NUM_BASE_LEVELS { | ||
| 458 | + let ctx = br_ctx(grid, row, col); | ||
| 459 | + let mut remaining = level - (NUM_BASE_LEVELS + 1); | ||
| 460 | + let mut sent = 0; | ||
| 461 | + while sent < COEFF_BASE_RANGE { | ||
| 462 | + let k = remaining.min(BR_STEP); | ||
| 463 | + enc.symbol_fixed(k as usize, &cdf::COEFF_BR_LUMA_32[ctx]); | ||
| 464 | + if k < BR_STEP { | ||
| 465 | + break; | ||
| 466 | + } | ||
| 467 | + remaining -= k; | ||
| 468 | + sent += BR_STEP; | ||
| 469 | + } | ||
| 470 | + } | ||
| 471 | + } | ||
| 472 | + | ||
| 473 | + // The signs come after the levels, in scan order, the DC's from a CDF and | ||
| 474 | + // the rest as raw bits (spec 5.11.39). | ||
| 475 | + for &pos in &scan[..eob] { | ||
| 476 | + let level = grid[pos as usize]; | ||
| 477 | + if level == 0 { | ||
| 478 | + continue; | ||
| 479 | + } | ||
| 480 | + if pos == 0 { | ||
| 481 | + enc.symbol_fixed(usize::from(level < 0), &cdf::DC_SIGN_LUMA[sign_ctx]); | ||
| 482 | + } else { | ||
| 483 | + enc.literal(u32::from(level < 0), 1); | ||
| 484 | + } | ||
| 485 | + } | ||
| 486 | +} | ||
| 487 | + | ||
| 488 | +/// The end-of-block position (spec 5.11.39): which group of scan positions the | ||
| 489 | +/// last coded coefficient falls in, then its offset inside that group — the | ||
| 490 | +/// offset's top bit from a CDF and the rest as raw bits. | ||
| 491 | +fn write_eob(enc: &mut SymbolEncoder, eob: usize) { | ||
| 492 | + /// `Eob_Group_Start` (spec 5.11.39): the first scan position each group of | ||
| 493 | + /// end-of-block positions covers, indexed by the group's own number. | ||
| 494 | + const GROUP_START: [usize; 12] = [0, 1, 2, 3, 5, 9, 17, 33, 65, 129, 257, 513]; | ||
| 495 | + /// `Eob_Offset_Bits` (spec 5.11.39): how wide each group's offset is. | ||
| 496 | + const OFFSET_BITS: [u32; 12] = [0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | ||
| 497 | + | ||
| 498 | + let group = GROUP_START | ||
| 499 | + .iter() | ||
| 500 | + .rposition(|&start| start <= eob) | ||
| 501 | + .expect("the first group starts at zero"); | ||
| 502 | + // A 32x32 transform codes its end-of-block group out of the eleven the | ||
| 503 | + // 1024-coefficient alphabet carries, numbered from one. | ||
| 504 | + enc.symbol_fixed(group - 1, &cdf::EOB_PT_1024_LUMA); | ||
| 505 | + | ||
| 506 | + let bits = OFFSET_BITS[group]; | ||
| 507 | + if bits > 0 { | ||
| 508 | + let offset = (eob - GROUP_START[group]) as u32; | ||
| 509 | + let top = (offset >> (bits - 1)) & 1; | ||
| 510 | + enc.symbol_fixed(top as usize, &cdf::EOB_EXTRA_LUMA_32[group - 3]); | ||
| 511 | + if bits > 1 { | ||
| 512 | + enc.literal(offset & ((1 << (bits - 1)) - 1), bits - 1); | ||
| 513 | + } | ||
| 514 | + } | ||
| 515 | +} | ||
| 516 | + | ||
| 517 | +/// The context of the level of the last coded coefficient (spec 8.3.2): how | ||
| 518 | +/// far into the scan it sits, in quarters and eighths of the transform. | ||
| 519 | +fn eob_coeff_ctx(scan_idx: usize) -> usize { | ||
| 520 | + match scan_idx { | ||
| 521 | + 0 => 0, | ||
| 522 | + i if i <= TX32_AREA / 8 => 1, | ||
| 523 | + i if i <= TX32_AREA / 4 => 2, | ||
| 524 | + _ => 3, | ||
| 525 | + } | ||
| 526 | +} | ||
| 527 | + | ||
| 528 | +/// The context of a coefficient's level (spec 8.3.2): the magnitudes of the | ||
| 529 | +/// five neighbours below and to the right of it — the ones a decoder walking | ||
| 530 | +/// the scan backwards already has — plus a term for where in the transform it | ||
| 531 | +/// sits. The DC reads context 0 whatever its neighbours carry. | ||
| 532 | +fn base_ctx(grid: &[i32], row: usize, col: usize) -> usize { | ||
| 533 | + if row == 0 && col == 0 { | ||
| 534 | + return 0; | ||
| 535 | + } | ||
| 536 | + let mag: i32 = [(1, 0), (0, 1), (1, 1), (2, 0), (0, 2)] | ||
| 537 | + .iter() | ||
| 538 | + .map(|&(dr, dc)| neighbour(grid, row + dr, col + dc).abs().min(3)) | ||
| 539 | + .sum(); | ||
| 540 | + let offset = cdf::NZ_MAP_CTX_OFFSET_32[row.min(4)][col.min(4)] as usize; | ||
| 541 | + (((mag + 1) >> 1).min(4) as usize) + offset | ||
| 542 | +} | ||
| 543 | + | ||
| 544 | +/// The context of a coefficient's base-range tail (spec 8.3.2): the magnitudes | ||
| 545 | +/// of its three closest neighbours below and to the right, uncapped, and a | ||
| 546 | +/// term separating the DC, the corner of the transform, and the rest. | ||
| 547 | +fn br_ctx(grid: &[i32], row: usize, col: usize) -> usize { | ||
| 548 | + let mag: i32 = [(1, 0), (0, 1), (1, 1)] | ||
| 549 | + .iter() | ||
| 550 | + .map(|&(dr, dc)| neighbour(grid, row + dr, col + dc).abs()) | ||
| 551 | + .sum(); | ||
| 552 | + let mag = (((mag + 1) >> 1).min(6)) as usize; | ||
| 553 | + if row == 0 && col == 0 { | ||
| 554 | + mag | ||
| 555 | + } else if row < 2 && col < 2 { | ||
| 556 | + mag + 7 | ||
| 557 | + } else { | ||
| 558 | + mag + 14 | ||
| 559 | + } | ||
| 560 | +} | ||
| 561 | + | ||
| 562 | +/// A level at a position that may fall off the transform, where the levels a | ||
| 563 | +/// context reads are zero. | ||
| 564 | +fn neighbour(grid: &[i32], row: usize, col: usize) -> i32 { | ||
| 565 | + if row >= TX32 || col >= TX32 { | ||
| 566 | + 0 | ||
| 567 | + } else { | ||
| 568 | + grid[row * TX32 + col] | ||
| 569 | + } | ||
| 570 | +} | ||
| 571 | + | ||
| 260 | /// Shared by both DC writers: one level per block, none of them zero, and a | 572 | /// Shared by both DC writers: one level per block, none of them zero, and a |
| 261 | /// q index the coefficient CDFs are known for. | 573 | /// q index the coefficient CDFs are known for. |
| 262 | fn check_levels(levels: &[i32], blocks: usize, base_q_idx: u8) -> Result<()> { | 574 | fn check_levels(levels: &[i32], blocks: usize, base_q_idx: u8) -> Result<()> { |
| @@ -305,7 +617,7 @@ fn write_dc_coeffs( | |||
| 305 | let mut sent = 0; | 617 | let mut sent = 0; |
| 306 | while sent < COEFF_BASE_RANGE { | 618 | while sent < COEFF_BASE_RANGE { |
| 307 | let k = remaining.min(BR_STEP); | 619 | let k = remaining.min(BR_STEP); |
| 308 | - enc.symbol_fixed(k as usize, &cdf::COEFF_BR_LUMA); | 620 | + enc.symbol_fixed(k as usize, &cdf::COEFF_BR_LUMA_32[0]); |
| 309 | if k < BR_STEP { | 621 | if k < BR_STEP { |
| 310 | break; | 622 | break; |
| 311 | } | 623 | } |
| @@ -810,4 +1122,364 @@ mod tests { | |||
| 810 | assert!(flat_key_frame_tile(16, 20).is_err()); | 1122 | assert!(flat_key_frame_tile(16, 20).is_err()); |
| 811 | assert!(flat_key_frame_tile(0, 16).is_err()); | 1123 | assert!(flat_key_frame_tile(0, 16).is_err()); |
| 812 | } | 1124 | } |
| 1125 | + | ||
| 1126 | + /// Encodes a 64x64 key frame whose four 32x32 blocks carry `blocks` and | ||
| 1127 | + /// hands back the luma plane as rows of samples. | ||
| 1128 | + fn decode_coeff_quadrants(blocks: &[Vec<Coeff>]) -> Vec<Vec<u8>> { | ||
| 1129 | + let (seq, header) = frame_of(64, 64); | ||
| 1130 | + let tile = | ||
| 1131 | + split_coeff_key_frame_tile(header.mi_cols, header.mi_rows, Q_IDX, blocks).unwrap(); | ||
| 1132 | + let mut stream = temporal_delimiter(); | ||
| 1133 | + stream.extend_from_slice(&sequence_header_obu(&seq).unwrap()); | ||
| 1134 | + stream.extend_from_slice(&frame_obu(&seq, &header, &tile).unwrap()); | ||
| 1135 | + let planes = ffmpeg_decode(&stream, 64, 64); | ||
| 1136 | + let (luma, chroma) = planes.split_at(64 * 64); | ||
| 1137 | + for (i, &s) in chroma.iter().enumerate() { | ||
| 1138 | + assert_eq!( | ||
| 1139 | + s, 128, | ||
| 1140 | + "chroma sample {i}: no block codes a chroma coefficient, and a \ | ||
| 1141 | + DC prediction with no coded neighbour is mid-grey" | ||
| 1142 | + ); | ||
| 1143 | + } | ||
| 1144 | + luma.chunks_exact(64).map(<[u8]>::to_vec).collect() | ||
| 1145 | + } | ||
| 1146 | + | ||
| 1147 | + /// The top-left 32x32 block of a decoded frame, which is the only one whose | ||
| 1148 | + /// prediction has no neighbour to lean on. | ||
| 1149 | + fn top_left_block(rows: &[Vec<u8>]) -> Vec<Vec<u8>> { | ||
| 1150 | + rows[..32].iter().map(|r| r[..32].to_vec()).collect() | ||
| 1151 | + } | ||
| 1152 | + | ||
| 1153 | + /// A transform's basis functions all average to zero except the DC's, so a | ||
| 1154 | + /// block carrying no DC decodes to samples that average to its prediction — | ||
| 1155 | + /// mid-grey for the block with no neighbours. Rounding of the inverse | ||
| 1156 | + /// transform moves the average by less than a sample. | ||
| 1157 | + fn assert_mean_is_mid_grey(block: &[Vec<u8>]) { | ||
| 1158 | + let sum: i32 = block.iter().flatten().map(|&s| i32::from(s)).sum(); | ||
| 1159 | + let mean = f64::from(sum) / (32.0 * 32.0); | ||
| 1160 | + assert!( | ||
| 1161 | + (mean - 128.0).abs() <= 1.0, | ||
| 1162 | + "a block with no DC coefficient averages to its mid-grey prediction, got {mean}" | ||
| 1163 | + ); | ||
| 1164 | + } | ||
| 1165 | + | ||
| 1166 | + /// A coefficient at row 0, column 1 selects the basis function that is flat | ||
| 1167 | + /// down the block and a half-cycle of a cosine across it. Nothing about | ||
| 1168 | + /// that shape is pinned here: every row of the block must be the same row, | ||
| 1169 | + /// the samples must fall from left to right and actually move, and the | ||
| 1170 | + /// block must still average to its prediction. A desync in the scan, the | ||
| 1171 | + /// end-of-block position or the level contexts breaks one of those. | ||
| 1172 | + #[test] | ||
| 1173 | + fn a_coefficient_off_the_origin_selects_its_own_basis_function() { | ||
| 1174 | + if !have_ffmpeg() { | ||
| 1175 | + eprintln!( | ||
| 1176 | + "SKIP a_coefficient_off_the_origin_selects_its_own_basis_function: no ffmpeg" | ||
| 1177 | + ); | ||
| 1178 | + return; | ||
| 1179 | + } | ||
| 1180 | + let blocks = vec![ | ||
| 1181 | + vec![Coeff { | ||
| 1182 | + row: 0, | ||
| 1183 | + col: 1, | ||
| 1184 | + level: 12, | ||
| 1185 | + }], | ||
| 1186 | + Vec::new(), | ||
| 1187 | + Vec::new(), | ||
| 1188 | + Vec::new(), | ||
| 1189 | + ]; | ||
| 1190 | + let rows = decode_coeff_quadrants(&blocks); | ||
| 1191 | + let block = top_left_block(&rows); | ||
| 1192 | + | ||
| 1193 | + for (y, row) in block.iter().enumerate() { | ||
| 1194 | + assert_eq!(row, &block[0], "row {y} of a horizontal basis function"); | ||
| 1195 | + } | ||
| 1196 | + assert!( | ||
| 1197 | + block[0][0] > block[0][31], | ||
| 1198 | + "a positive coefficient leans the block towards its left edge: {:?}", | ||
| 1199 | + block[0] | ||
| 1200 | + ); | ||
| 1201 | + for x in 1..32 { | ||
| 1202 | + assert!( | ||
| 1203 | + block[0][x] <= block[0][x - 1], | ||
| 1204 | + "the half-cycle falls from left to right, broke at column {x}: {:?}", | ||
| 1205 | + block[0] | ||
| 1206 | + ); | ||
| 1207 | + } | ||
| 1208 | + assert_mean_is_mid_grey(&block); | ||
| 1209 | + | ||
| 1210 | + // The same coefficient transposed selects the basis function that is | ||
| 1211 | + // flat across the block and a half-cycle down it. | ||
| 1212 | + let blocks = vec![ | ||
| 1213 | + vec![Coeff { | ||
| 1214 | + row: 1, | ||
| 1215 | + col: 0, | ||
| 1216 | + level: 12, | ||
| 1217 | + }], | ||
| 1218 | + Vec::new(), | ||
| 1219 | + Vec::new(), | ||
| 1220 | + Vec::new(), | ||
| 1221 | + ]; | ||
| 1222 | + let rows = decode_coeff_quadrants(&blocks); | ||
| 1223 | + let block = top_left_block(&rows); | ||
| 1224 | + for (y, row) in block.iter().enumerate() { | ||
| 1225 | + for (x, &s) in row.iter().enumerate() { | ||
| 1226 | + assert_eq!(s, row[0], "sample ({y},{x}) of a vertical basis function"); | ||
| 1227 | + } | ||
| 1228 | + } | ||
| 1229 | + assert!( | ||
| 1230 | + block[0][0] > block[31][0], | ||
| 1231 | + "a positive coefficient leans the block up" | ||
| 1232 | + ); | ||
| 1233 | + for y in 1..32 { | ||
| 1234 | + assert!( | ||
| 1235 | + block[y][0] <= block[y - 1][0], | ||
| 1236 | + "the half-cycle falls from top to bottom, broke at row {y}" | ||
| 1237 | + ); | ||
| 1238 | + } | ||
| 1239 | + assert_mean_is_mid_grey(&block); | ||
| 1240 | + } | ||
| 1241 | + | ||
| 1242 | + /// A coefficient late in the scan pushes the end-of-block position into a | ||
| 1243 | + /// group that carries an offset — the far corner of a 32x32 transform is | ||
| 1244 | + /// the last of the eleven groups, whose offset is nine bits wide, one of | ||
| 1245 | + /// them from a CDF and the rest raw. Every coefficient before it is coded | ||
| 1246 | + /// too, so this also walks the base and base-range contexts over a block | ||
| 1247 | + /// with neighbours that carry levels rather than zeros. | ||
| 1248 | + #[test] | ||
| 1249 | + fn a_coefficient_in_the_far_corner_reaches_the_last_end_of_block_group() { | ||
| 1250 | + if !have_ffmpeg() { | ||
| 1251 | + eprintln!("SKIP a_coefficient_in_the_far_corner_reaches_the_last_end_of_block_group"); | ||
| 1252 | + return; | ||
| 1253 | + } | ||
| 1254 | + let block = vec![ | ||
| 1255 | + Coeff { | ||
| 1256 | + row: 31, | ||
| 1257 | + col: 31, | ||
| 1258 | + level: 5, | ||
| 1259 | + }, | ||
| 1260 | + Coeff { | ||
| 1261 | + row: 5, | ||
| 1262 | + col: 7, | ||
| 1263 | + level: -9, | ||
| 1264 | + }, | ||
| 1265 | + Coeff { | ||
| 1266 | + row: 1, | ||
| 1267 | + col: 1, | ||
| 1268 | + level: 14, | ||
| 1269 | + }, | ||
| 1270 | + Coeff { | ||
| 1271 | + row: 0, | ||
| 1272 | + col: 2, | ||
| 1273 | + level: -3, | ||
| 1274 | + }, | ||
| 1275 | + Coeff { | ||
| 1276 | + row: 2, | ||
| 1277 | + col: 0, | ||
| 1278 | + level: 2, | ||
| 1279 | + }, | ||
| 1280 | + Coeff { | ||
| 1281 | + row: 1, | ||
| 1282 | + col: 2, | ||
| 1283 | + level: -1, | ||
| 1284 | + }, | ||
| 1285 | + ]; | ||
| 1286 | + let rows = decode_coeff_quadrants(&[block, Vec::new(), Vec::new(), Vec::new()]); | ||
| 1287 | + let block = top_left_block(&rows); | ||
| 1288 | + assert_mean_is_mid_grey(&block); | ||
| 1289 | + assert!( | ||
| 1290 | + block.iter().flatten().any(|&s| s != block[0][0]), | ||
| 1291 | + "six coefficients do not decode to a flat block" | ||
| 1292 | + ); | ||
| 1293 | + // The blocks that carry nothing are flat: their prediction is whatever | ||
| 1294 | + // their neighbours left, and they add no residual to it. | ||
| 1295 | + for (name, ys, xs) in [("top right", 0..32, 32..64), ("bottom left", 32..64, 0..32)] { | ||
| 1296 | + let first = rows[ys.start][xs.start]; | ||
| 1297 | + for y in ys { | ||
| 1298 | + for x in xs.clone() { | ||
| 1299 | + assert_eq!(rows[y][x], first, "the {name} block carries no coefficient"); | ||
| 1300 | + } | ||
| 1301 | + } | ||
| 1302 | + } | ||
| 1303 | + } | ||
| 1304 | + | ||
| 1305 | + /// Neighbouring blocks that each carry coefficients read their DC sign | ||
| 1306 | + /// context, their prediction and their own contexts off each other, so a | ||
| 1307 | + /// frame whose four blocks all carry mixed-sign coefficients exercises what | ||
| 1308 | + /// a single coded block cannot. The check is that it decodes at all — a | ||
| 1309 | + /// desync anywhere fails the decode or the frame size — and that the block | ||
| 1310 | + /// with no neighbours still averages to mid-grey. | ||
| 1311 | + #[test] | ||
| 1312 | + fn every_quadrant_carries_its_own_coefficients() { | ||
| 1313 | + if !have_ffmpeg() { | ||
| 1314 | + eprintln!("SKIP every_quadrant_carries_its_own_coefficients: no ffmpeg"); | ||
| 1315 | + return; | ||
| 1316 | + } | ||
| 1317 | + let blocks = vec![ | ||
| 1318 | + vec![ | ||
| 1319 | + Coeff { | ||
| 1320 | + row: 0, | ||
| 1321 | + col: 1, | ||
| 1322 | + level: 7, | ||
| 1323 | + }, | ||
| 1324 | + Coeff { | ||
| 1325 | + row: 3, | ||
| 1326 | + col: 4, | ||
| 1327 | + level: -14, | ||
| 1328 | + }, | ||
| 1329 | + ], | ||
| 1330 | + vec![ | ||
| 1331 | + Coeff { | ||
| 1332 | + row: 0, | ||
| 1333 | + col: 0, | ||
| 1334 | + level: -6, | ||
| 1335 | + }, | ||
| 1336 | + Coeff { | ||
| 1337 | + row: 2, | ||
| 1338 | + col: 2, | ||
| 1339 | + level: 9, | ||
| 1340 | + }, | ||
| 1341 | + ], | ||
| 1342 | + vec![ | ||
| 1343 | + Coeff { | ||
| 1344 | + row: 0, | ||
| 1345 | + col: 0, | ||
| 1346 | + level: 6, | ||
| 1347 | + }, | ||
| 1348 | + Coeff { | ||
| 1349 | + row: 1, | ||
| 1350 | + col: 0, | ||
| 1351 | + level: -2, | ||
| 1352 | + }, | ||
| 1353 | + Coeff { | ||
| 1354 | + row: 8, | ||
| 1355 | + col: 9, | ||
| 1356 | + level: 3, | ||
| 1357 | + }, | ||
| 1358 | + ], | ||
| 1359 | + vec![ | ||
| 1360 | + Coeff { | ||
| 1361 | + row: 0, | ||
| 1362 | + col: 0, | ||
| 1363 | + level: 11, | ||
| 1364 | + }, | ||
| 1365 | + Coeff { | ||
| 1366 | + row: 16, | ||
| 1367 | + col: 1, | ||
| 1368 | + level: -4, | ||
| 1369 | + }, | ||
| 1370 | + ], | ||
| 1371 | + ]; | ||
| 1372 | + let rows = decode_coeff_quadrants(&blocks); | ||
| 1373 | + assert_mean_is_mid_grey(&top_left_block(&rows)); | ||
| 1374 | + for (name, ys, xs) in [ | ||
| 1375 | + ("top right", 0..32, 32..64), | ||
| 1376 | + ("bottom left", 32..64, 0..32), | ||
| 1377 | + ("bottom right", 32..64, 32..64), | ||
| 1378 | + ] { | ||
| 1379 | + let block: Vec<Vec<u8>> = rows[ys].iter().map(|r| r[xs.clone()].to_vec()).collect(); | ||
| 1380 | + assert!( | ||
| 1381 | + block.iter().flatten().any(|&s| s != block[0][0]), | ||
| 1382 | + "the {name} block carries coefficients, so it is not flat" | ||
| 1383 | + ); | ||
| 1384 | + } | ||
| 1385 | + } | ||
| 1386 | + | ||
| 1387 | + /// The coefficient writer refuses what it cannot code rather than writing a | ||
| 1388 | + /// stream a decoder walks off the end of. | ||
| 1389 | + #[test] | ||
| 1390 | + fn coefficients_the_writer_cannot_code_are_refused() { | ||
| 1391 | + let empty = || vec![Vec::new(), Vec::new(), Vec::new(), Vec::new()]; | ||
| 1392 | + let cases: [(&str, Vec<Vec<Coeff>>); 5] = [ | ||
| 1393 | + ("one list short", vec![Vec::new(), Vec::new(), Vec::new()]), | ||
| 1394 | + ("off the transform", { | ||
| 1395 | + let mut b = empty(); | ||
| 1396 | + b[0] = vec![Coeff { | ||
| 1397 | + row: 32, | ||
| 1398 | + col: 0, | ||
| 1399 | + level: 1, | ||
| 1400 | + }]; | ||
| 1401 | + b | ||
| 1402 | + }), | ||
| 1403 | + ("a zero level", { | ||
| 1404 | + let mut b = empty(); | ||
| 1405 | + b[1] = vec![Coeff { | ||
| 1406 | + row: 0, | ||
| 1407 | + col: 0, | ||
| 1408 | + level: 0, | ||
| 1409 | + }]; | ||
| 1410 | + b | ||
| 1411 | + }), | ||
| 1412 | + ("a level past the base-range tail", { | ||
| 1413 | + let mut b = empty(); | ||
| 1414 | + b[2] = vec![Coeff { | ||
| 1415 | + row: 0, | ||
| 1416 | + col: 0, | ||
| 1417 | + level: 15, | ||
| 1418 | + }]; | ||
| 1419 | + b | ||
| 1420 | + }), | ||
| 1421 | + ("two coefficients at one position", { | ||
| 1422 | + let mut b = empty(); | ||
| 1423 | + b[3] = vec![ | ||
| 1424 | + Coeff { | ||
| 1425 | + row: 4, | ||
| 1426 | + col: 4, | ||
| 1427 | + level: 1, | ||
| 1428 | + }, | ||
| 1429 | + Coeff { | ||
| 1430 | + row: 4, | ||
| 1431 | + col: 4, | ||
| 1432 | + level: 2, | ||
| 1433 | + }, | ||
| 1434 | + ]; | ||
| 1435 | + b | ||
| 1436 | + }), | ||
| 1437 | + ]; | ||
| 1438 | + for (name, blocks) in cases { | ||
| 1439 | + assert!( | ||
| 1440 | + split_coeff_key_frame_tile(16, 16, Q_IDX, &blocks).is_err(), | ||
| 1441 | + "{name} must be refused" | ||
| 1442 | + ); | ||
| 1443 | + } | ||
| 1444 | + assert!( | ||
| 1445 | + split_coeff_key_frame_tile(16, 16, 200, &empty()).is_err(), | ||
| 1446 | + "a q index outside the band whose CDFs are known must be refused" | ||
| 1447 | + ); | ||
| 1448 | + } | ||
| 1449 | + | ||
| 1450 | + /// The default scan is written as the rule that generates it rather than a | ||
| 1451 | + /// thousand pinned numbers, so the rule is checked against what a scan must | ||
| 1452 | + /// be: every position exactly once, the origin first, and never a position | ||
| 1453 | + /// before one it sits diagonally behind — which is what lets a decoder read | ||
| 1454 | + /// a coefficient's context off the coefficients it has already decoded. | ||
| 1455 | + #[test] | ||
| 1456 | + fn the_default_scan_walks_every_position_outwards() { | ||
| 1457 | + let scan = default_scan_32(); | ||
| 1458 | + assert_eq!(scan.len(), TX32_AREA); | ||
| 1459 | + let mut seen = vec![false; TX32_AREA]; | ||
| 1460 | + for &pos in &scan { | ||
| 1461 | + assert!(!seen[pos as usize], "position {pos} is scanned twice"); | ||
| 1462 | + seen[pos as usize] = true; | ||
| 1463 | + } | ||
| 1464 | + assert_eq!(scan[0], 0, "the scan starts at the DC"); | ||
| 1465 | + | ||
| 1466 | + let mut order = vec![0usize; TX32_AREA]; | ||
| 1467 | + for (i, &pos) in scan.iter().enumerate() { | ||
| 1468 | + order[pos as usize] = i; | ||
| 1469 | + } | ||
| 1470 | + for row in 0..TX32 { | ||
| 1471 | + for col in 0..TX32 { | ||
| 1472 | + for (dr, dc) in [(1, 0), (0, 1), (1, 1), (2, 0), (0, 2)] { | ||
| 1473 | + let (nr, nc) = (row + dr, col + dc); | ||
| 1474 | + if nr < TX32 && nc < TX32 { | ||
| 1475 | + assert!( | ||
| 1476 | + order[nr * TX32 + nc] > order[row * TX32 + col], | ||
| 1477 | + "({nr},{nc}) is a context neighbour of ({row},{col}), so it must be \ | ||
| 1478 | + scanned after it" | ||
| 1479 | + ); | ||
| 1480 | + } | ||
| 1481 | + } | ||
| 1482 | + } | ||
| 1483 | + } | ||
| 1484 | + } | ||
| 813 | } | 1485 | } |