A superblock can stay whole
Browse files at this commitda2c24a5170ccabd6ff5811df82f595eb776b311Parent: 0eba770- feat
- a superblock can stay whole
2 changed files +556 -79
- crates/ec-av1/src/cdf.rs +180 -2
- crates/ec-av1/src/tile.rs +376 -77
crates/ec-av1/src/cdf.rsmodified+180 -2
| @@ -210,7 +210,7 @@ pub const TXB_SKIP_LUMA_64: [u16; 3] = [28573, 32768, 0]; | |||
| 210 | /// `Default_Txb_Skip_Cdf[2][3][7]` (spec 9.4): the all-zero flag of a 32x32 | 210 | /// `Default_Txb_Skip_Cdf[2][3][7]` (spec 9.4): the all-zero flag of a 32x32 |
| 211 | /// chroma transform block that covers its whole plane block with no coded | 211 | /// chroma transform block that covers its whole plane block with no coded |
| 212 | /// neighbour, which is context 7. | 212 | /// neighbour, which is context 7. |
| 213 | -pub const TXB_SKIP_CHROMA_32: [u16; 3] = [5124, 32768, 0]; | 213 | +pub const TXB_SKIP_CHROMA_32_NONE: [u16; 3] = [5124, 32768, 0]; |
| 214 | 214 | ||
| 215 | /// `Default_Eob_Pt_1024_Cdf[2][0][0]` (spec 9.4): the end-of-block position | 215 | /// `Default_Eob_Pt_1024_Cdf[2][0][0]` (spec 9.4): the end-of-block position |
| 216 | /// token of a luma transform block with 1024 coded coefficient positions — | 216 | /// token of a luma transform block with 1024 coded coefficient positions — |
| @@ -222,7 +222,7 @@ pub const EOB_PT_1024_LUMA: [u16; 12] = [ | |||
| 222 | /// `Default_Coeff_Base_Eob_Cdf[2][4][0][0]` (spec 9.4): the level of the last | 222 | /// `Default_Coeff_Base_Eob_Cdf[2][4][0][0]` (spec 9.4): the level of the last |
| 223 | /// coefficient of a 64x64 luma transform block, less one, when that | 223 | /// coefficient of a 64x64 luma transform block, less one, when that |
| 224 | /// coefficient is the DC. | 224 | /// coefficient is the DC. |
| 225 | -pub const COEFF_BASE_EOB_LUMA_64: [u16; 4] = [935, 3382, 32768, 0]; | 225 | +pub const COEFF_BASE_EOB_LUMA_64_DC: [u16; 4] = [935, 3382, 32768, 0]; |
| 226 | 226 | ||
| 227 | /// `Default_Dc_Sign_Cdf[2][0]` (spec 9.4): the sign of a luma DC coefficient, | 227 | /// `Default_Dc_Sign_Cdf[2][0]` (spec 9.4): the sign of a luma DC coefficient, |
| 228 | /// indexed by whether the neighbouring blocks' DC signs lean negative (1), | 228 | /// indexed by whether the neighbouring blocks' DC signs lean negative (1), |
| @@ -544,3 +544,181 @@ pub const ANGLE_DELTA: [[u16; 8]; 8] = [ | |||
| 544 | [2240, 11096, 15453, 20341, 22561, 28917, 32768, 0], | 544 | [2240, 11096, 15453, 20341, 22561, 28917, 32768, 0], |
| 545 | [3605, 10428, 12459, 17676, 21244, 30655, 32768, 0], | 545 | [3605, 10428, 12459, 17676, 21244, 30655, 32768, 0], |
| 546 | ]; | 546 | ]; |
| 547 | +/// `Default_Coeff_Base_Cdf[2][4][0]` (spec 9.4): the level of a coefficient of a 64x64 luma | ||
| 548 | +/// transform block, capped at three, for each of the forty-two contexts its neighbours and its | ||
| 549 | +/// position give. | ||
| 550 | +pub const COEFF_BASE_LUMA_64: [[u16; 5]; 42] = [ | ||
| 551 | + [2575, 7281, 11077, 32768, 0], | ||
| 552 | + [14002, 20866, 25402, 32768, 0], | ||
| 553 | + [6343, 15056, 19658, 32768, 0], | ||
| 554 | + [4474, 11858, 17041, 32768, 0], | ||
| 555 | + [2865, 8299, 12534, 32768, 0], | ||
| 556 | + [1344, 3949, 6391, 32768, 0], | ||
| 557 | + [24720, 31239, 32459, 32768, 0], | ||
| 558 | + [12585, 25356, 29968, 32768, 0], | ||
| 559 | + [7181, 18246, 24444, 32768, 0], | ||
| 560 | + [5025, 13667, 19885, 32768, 0], | ||
| 561 | + [2521, 7304, 11605, 32768, 0], | ||
| 562 | + [29908, 32252, 32584, 32768, 0], | ||
| 563 | + [17421, 29156, 31575, 32768, 0], | ||
| 564 | + [9889, 22188, 27782, 32768, 0], | ||
| 565 | + [5878, 15647, 22123, 32768, 0], | ||
| 566 | + [2814, 8665, 13323, 32768, 0], | ||
| 567 | + [30183, 32568, 32713, 32768, 0], | ||
| 568 | + [18528, 30195, 32049, 32768, 0], | ||
| 569 | + [10982, 24606, 29657, 32768, 0], | ||
| 570 | + [6957, 18165, 25231, 32768, 0], | ||
| 571 | + [3508, 10118, 15468, 32768, 0], | ||
| 572 | + [31761, 32736, 32748, 32768, 0], | ||
| 573 | + [21041, 31328, 32546, 32768, 0], | ||
| 574 | + [12568, 26732, 31166, 32768, 0], | ||
| 575 | + [8052, 20720, 27733, 32768, 0], | ||
| 576 | + [4336, 12192, 18396, 32768, 0], | ||
| 577 | + [8192, 16384, 24576, 32768, 0], | ||
| 578 | + [8192, 16384, 24576, 32768, 0], | ||
| 579 | + [8192, 16384, 24576, 32768, 0], | ||
| 580 | + [8192, 16384, 24576, 32768, 0], | ||
| 581 | + [8192, 16384, 24576, 32768, 0], | ||
| 582 | + [8192, 16384, 24576, 32768, 0], | ||
| 583 | + [8192, 16384, 24576, 32768, 0], | ||
| 584 | + [8192, 16384, 24576, 32768, 0], | ||
| 585 | + [8192, 16384, 24576, 32768, 0], | ||
| 586 | + [8192, 16384, 24576, 32768, 0], | ||
| 587 | + [8192, 16384, 24576, 32768, 0], | ||
| 588 | + [8192, 16384, 24576, 32768, 0], | ||
| 589 | + [8192, 16384, 24576, 32768, 0], | ||
| 590 | + [8192, 16384, 24576, 32768, 0], | ||
| 591 | + [8192, 16384, 24576, 32768, 0], | ||
| 592 | + [8192, 16384, 24576, 32768, 0], | ||
| 593 | +]; | ||
| 594 | + | ||
| 595 | +/// `Default_Coeff_Base_Eob_Cdf[2][4][0]` (spec 9.4): the level of the last coefficient of a | ||
| 596 | +/// 64x64 luma transform block, less one, for each of the four contexts its position gives. | ||
| 597 | +pub const COEFF_BASE_EOB_LUMA_64: [[u16; 4]; 4] = [ | ||
| 598 | + [935, 3382, 32768, 0], | ||
| 599 | + [30789, 31909, 32768, 0], | ||
| 600 | + [32466, 32756, 32768, 0], | ||
| 601 | + [30860, 32513, 32768, 0], | ||
| 602 | +]; | ||
| 603 | + | ||
| 604 | +/// `Default_Eob_Extra_Cdf[2][4][0]` (spec 9.4): the top bit of the end-of-block position of a | ||
| 605 | +/// 64x64 luma transform block, one context per end-of-block group. | ||
| 606 | +pub const EOB_EXTRA_LUMA_64: [[u16; 3]; 9] = [ | ||
| 607 | + [26998, 32768, 0], | ||
| 608 | + [16737, 32768, 0], | ||
| 609 | + [17838, 32768, 0], | ||
| 610 | + [18922, 32768, 0], | ||
| 611 | + [19515, 32768, 0], | ||
| 612 | + [18636, 32768, 0], | ||
| 613 | + [17333, 32768, 0], | ||
| 614 | + [15776, 32768, 0], | ||
| 615 | + [22658, 32768, 0], | ||
| 616 | +]; | ||
| 617 | + | ||
| 618 | +/// `Default_Coeff_Base_Cdf[2][3][1]` (spec 9.4): the level of a coefficient of a 32x32 chroma | ||
| 619 | +/// transform block, capped at three, for each of the forty-two contexts its neighbours and its | ||
| 620 | +/// position give. | ||
| 621 | +pub const COEFF_BASE_CHROMA_32: [[u16; 5]; 42] = [ | ||
| 622 | + [10202, 20633, 25484, 32768, 0], | ||
| 623 | + [27336, 31445, 32352, 32768, 0], | ||
| 624 | + [12420, 24384, 28552, 32768, 0], | ||
| 625 | + [7648, 18115, 23856, 32768, 0], | ||
| 626 | + [5662, 14341, 19902, 32768, 0], | ||
| 627 | + [3611, 10328, 15390, 32768, 0], | ||
| 628 | + [30945, 32616, 32736, 32768, 0], | ||
| 629 | + [18682, 30505, 32253, 32768, 0], | ||
| 630 | + [11513, 25336, 30203, 32768, 0], | ||
| 631 | + [7449, 19452, 26148, 32768, 0], | ||
| 632 | + [4482, 13051, 18886, 32768, 0], | ||
| 633 | + [32022, 32690, 32747, 32768, 0], | ||
| 634 | + [18578, 30501, 32146, 32768, 0], | ||
| 635 | + [11249, 23368, 28631, 32768, 0], | ||
| 636 | + [5645, 16958, 22158, 32768, 0], | ||
| 637 | + [5009, 11444, 16637, 32768, 0], | ||
| 638 | + [31357, 32710, 32748, 32768, 0], | ||
| 639 | + [21552, 31494, 32504, 32768, 0], | ||
| 640 | + [13891, 27677, 31340, 32768, 0], | ||
| 641 | + [9051, 22098, 28172, 32768, 0], | ||
| 642 | + [5190, 13377, 19486, 32768, 0], | ||
| 643 | + [32364, 32740, 32748, 32768, 0], | ||
| 644 | + [24839, 31907, 32551, 32768, 0], | ||
| 645 | + [17160, 28779, 31696, 32768, 0], | ||
| 646 | + [12452, 24137, 29602, 32768, 0], | ||
| 647 | + [6165, 15389, 22477, 32768, 0], | ||
| 648 | + [8192, 16384, 24576, 32768, 0], | ||
| 649 | + [8192, 16384, 24576, 32768, 0], | ||
| 650 | + [8192, 16384, 24576, 32768, 0], | ||
| 651 | + [8192, 16384, 24576, 32768, 0], | ||
| 652 | + [8192, 16384, 24576, 32768, 0], | ||
| 653 | + [8192, 16384, 24576, 32768, 0], | ||
| 654 | + [8192, 16384, 24576, 32768, 0], | ||
| 655 | + [8192, 16384, 24576, 32768, 0], | ||
| 656 | + [8192, 16384, 24576, 32768, 0], | ||
| 657 | + [8192, 16384, 24576, 32768, 0], | ||
| 658 | + [8192, 16384, 24576, 32768, 0], | ||
| 659 | + [8192, 16384, 24576, 32768, 0], | ||
| 660 | + [8192, 16384, 24576, 32768, 0], | ||
| 661 | + [8192, 16384, 24576, 32768, 0], | ||
| 662 | + [8192, 16384, 24576, 32768, 0], | ||
| 663 | + [8192, 16384, 24576, 32768, 0], | ||
| 664 | +]; | ||
| 665 | + | ||
| 666 | +/// `Default_Coeff_Br_Cdf[2][3][1]` (spec 9.4): the base-range tail of a 32x32 chroma transform | ||
| 667 | +/// block, for each of the twenty-one contexts its neighbours and its position give. | ||
| 668 | +pub const COEFF_BR_CHROMA_32: [[u16; 5]; 21] = [ | ||
| 669 | + [12339, 17329, 20140, 32768, 0], | ||
| 670 | + [13505, 19895, 23225, 32768, 0], | ||
| 671 | + [9847, 16944, 21564, 32768, 0], | ||
| 672 | + [7280, 13256, 18348, 32768, 0], | ||
| 673 | + [4712, 10009, 14454, 32768, 0], | ||
| 674 | + [4361, 7914, 12477, 32768, 0], | ||
| 675 | + [2870, 5628, 7995, 32768, 0], | ||
| 676 | + [20061, 25504, 28526, 32768, 0], | ||
| 677 | + [15235, 22878, 26145, 32768, 0], | ||
| 678 | + [12985, 19958, 24155, 32768, 0], | ||
| 679 | + [9782, 16641, 21403, 32768, 0], | ||
| 680 | + [9456, 16360, 20760, 32768, 0], | ||
| 681 | + [6855, 12940, 18557, 32768, 0], | ||
| 682 | + [5661, 10564, 15002, 32768, 0], | ||
| 683 | + [25656, 30602, 31894, 32768, 0], | ||
| 684 | + [22570, 29107, 31092, 32768, 0], | ||
| 685 | + [18917, 26423, 29541, 32768, 0], | ||
| 686 | + [15940, 23649, 27754, 32768, 0], | ||
| 687 | + [12803, 20581, 25219, 32768, 0], | ||
| 688 | + [11082, 18695, 23376, 32768, 0], | ||
| 689 | + [7939, 14373, 19005, 32768, 0], | ||
| 690 | +]; | ||
| 691 | + | ||
| 692 | +/// `Default_Coeff_Base_Eob_Cdf[2][3][1]` (spec 9.4): the level of the last coefficient of a | ||
| 693 | +/// 32x32 chroma transform block, less one, for each of the four contexts its position gives. | ||
| 694 | +pub const COEFF_BASE_EOB_CHROMA_32: [[u16; 4]; 4] = [ | ||
| 695 | + [22737, 29105, 32768, 0], | ||
| 696 | + [30810, 32362, 32768, 0], | ||
| 697 | + [30014, 32627, 32768, 0], | ||
| 698 | + [30528, 32574, 32768, 0], | ||
| 699 | +]; | ||
| 700 | + | ||
| 701 | +/// `Default_Eob_Extra_Cdf[2][3][1]` (spec 9.4): the top bit of the end-of-block position of a | ||
| 702 | +/// 32x32 chroma transform block, one context per end-of-block group. | ||
| 703 | +pub const EOB_EXTRA_CHROMA_32: [[u16; 3]; 9] = [ | ||
| 704 | + [26579, 32768, 0], | ||
| 705 | + [24910, 32768, 0], | ||
| 706 | + [18637, 32768, 0], | ||
| 707 | + [19800, 32768, 0], | ||
| 708 | + [20388, 32768, 0], | ||
| 709 | + [9887, 32768, 0], | ||
| 710 | + [15642, 32768, 0], | ||
| 711 | + [30198, 32768, 0], | ||
| 712 | + [24721, 32768, 0], | ||
| 713 | +]; | ||
| 714 | + | ||
| 715 | +/// `Default_Txb_Skip_Cdf[2][3][7..10]` (spec 9.4): the all-zero flag of a 32x32 chroma transform | ||
| 716 | +/// block that covers its whole plane block, for the three contexts its coded neighbours give. | ||
| 717 | +pub const TXB_SKIP_CHROMA_32: [[u16; 3]; 3] = | ||
| 718 | + [[5124, 32768, 0], [18719, 32768, 0], [28468, 32768, 0]]; | ||
| 719 | + | ||
| 720 | +/// `Default_Eob_Pt_1024_Cdf[2][1][0]` (spec 9.4): the end-of-block group of a | ||
| 721 | +/// 1024-coefficient chroma transform block with a two-dimensional transform. | ||
| 722 | +pub const EOB_PT_1024_CHROMA: [u16; 12] = [ | ||
| 723 | + 9577, 12466, 17739, 20750, 22061, 23215, 24601, 25483, 25843, 32056, 32768, 0, | ||
| 724 | +]; | ||
crates/ec-av1/src/tile.rsmodified+376 -77
| @@ -224,15 +224,15 @@ pub fn dc_key_frame_tile_levels( | |||
| 224 | dc_level, | 224 | dc_level, |
| 225 | dc_sign_ctx(above[c as usize], left), | 225 | dc_sign_ctx(above[c as usize], left), |
| 226 | &cdf::TXB_SKIP_LUMA_64, | 226 | &cdf::TXB_SKIP_LUMA_64, |
| 227 | - &cdf::COEFF_BASE_EOB_LUMA_64, | 227 | + &cdf::COEFF_BASE_EOB_LUMA_64_DC, |
| 228 | ); | 228 | ); |
| 229 | 229 | ||
| 230 | // Both chroma transform blocks are all-zero. Their planes carry no | 230 | // Both chroma transform blocks are all-zero. Their planes carry no |
| 231 | // coded coefficient anywhere in the frame, so the neighbour halves | 231 | // coded coefficient anywhere in the frame, so the neighbour halves |
| 232 | // of their context stay 0 and only the offset for a transform block | 232 | // of their context stay 0 and only the offset for a transform block |
| 233 | // that covers its whole plane block is left: context 7. | 233 | // that covers its whole plane block is left: context 7. |
| 234 | - enc.symbol_fixed(1, &cdf::TXB_SKIP_CHROMA_32); | 234 | + enc.symbol_fixed(1, &cdf::TXB_SKIP_CHROMA_32_NONE); |
| 235 | - enc.symbol_fixed(1, &cdf::TXB_SKIP_CHROMA_32); | 235 | + enc.symbol_fixed(1, &cdf::TXB_SKIP_CHROMA_32_NONE); |
| 236 | 236 | ||
| 237 | above[c as usize] = Some(negative); | 237 | above[c as usize] = Some(negative); |
| 238 | left = Some(negative); | 238 | left = Some(negative); |
| @@ -278,6 +278,8 @@ pub fn split_dc_key_frame_tile( | |||
| 278 | // it, and an uncoded one leaves it clear, so the context is just | 278 | // it, and an uncoded one leaves it clear, so the context is just |
| 279 | // which neighbours exist. The 32x32 blocks below read a bit their | 279 | // which neighbours exist. The 32x32 blocks below read a bit their |
| 280 | // own size leaves clear, so their context is 0 throughout. | 280 | // own size leaves clear, so their context is 0 throughout. |
| 281 | + // Every superblock here is split, so an existing neighbour always | ||
| 282 | + // sets that bit. | ||
| 281 | let ctx = 2 * usize::from(sb_c > 0) + usize::from(sb_r > 0); | 283 | let ctx = 2 * usize::from(sb_c > 0) + usize::from(sb_r > 0); |
| 282 | enc.symbol_fixed(PARTITION_SPLIT, &cdf::PARTITION_W64[ctx]); | 284 | enc.symbol_fixed(PARTITION_SPLIT, &cdf::PARTITION_W64[ctx]); |
| 283 | 285 | ||
| @@ -412,6 +414,39 @@ const CHROMA_16: TxbCoding = TxbCoding { | |||
| 412 | dc_sign: &cdf::DC_SIGN_CHROMA, | 414 | dc_sign: &cdf::DC_SIGN_CHROMA, |
| 413 | }; | 415 | }; |
| 414 | 416 | ||
| 417 | +/// The luma all-zero flag of a 64x64 transform, whose one context is fixed the | ||
| 418 | +/// way [`TXB_SKIP_LUMA_32_CTX`] is. | ||
| 419 | +const TXB_SKIP_LUMA_64_CTX: [[u16; 3]; 1] = [cdf::TXB_SKIP_LUMA_64]; | ||
| 420 | + | ||
| 421 | +/// The 64x64 luma transform of a whole superblock. Only its top-left 32x32 | ||
| 422 | +/// carries coefficients, so it is scanned and its end-of-block position read | ||
| 423 | +/// as a 32x32 transform is; what it does not share with [`LUMA_32`] is every | ||
| 424 | +/// coefficient CDF, which is indexed by the transform size. | ||
| 425 | +const LUMA_64: TxbCoding = TxbCoding { | ||
| 426 | + side: TX32, | ||
| 427 | + txb_skip: &TXB_SKIP_LUMA_64_CTX, | ||
| 428 | + eob_pt: &cdf::EOB_PT_1024_LUMA, | ||
| 429 | + eob_extra: &cdf::EOB_EXTRA_LUMA_64, | ||
| 430 | + base: &cdf::COEFF_BASE_LUMA_64, | ||
| 431 | + base_eob: &cdf::COEFF_BASE_EOB_LUMA_64, | ||
| 432 | + // The base-range tail is the one table a 64x64 transform does not have of | ||
| 433 | + // its own: the index is clamped at the 32x32 size, so it reads that row. | ||
| 434 | + br: &cdf::COEFF_BR_LUMA_32, | ||
| 435 | + dc_sign: &cdf::DC_SIGN_LUMA, | ||
| 436 | +}; | ||
| 437 | + | ||
| 438 | +/// The 32x32 transform of either chroma plane of that superblock at 4:2:0. | ||
| 439 | +const CHROMA_32: TxbCoding = TxbCoding { | ||
| 440 | + side: TX32, | ||
| 441 | + txb_skip: &cdf::TXB_SKIP_CHROMA_32, | ||
| 442 | + eob_pt: &cdf::EOB_PT_1024_CHROMA, | ||
| 443 | + eob_extra: &cdf::EOB_EXTRA_CHROMA_32, | ||
| 444 | + base: &cdf::COEFF_BASE_CHROMA_32, | ||
| 445 | + base_eob: &cdf::COEFF_BASE_EOB_CHROMA_32, | ||
| 446 | + br: &cdf::COEFF_BR_CHROMA_32, | ||
| 447 | + dc_sign: &cdf::DC_SIGN_CHROMA, | ||
| 448 | +}; | ||
| 449 | + | ||
| 415 | /// What one coded block leaves behind for the blocks that read it as a | 450 | /// What one coded block leaves behind for the blocks that read it as a |
| 416 | /// neighbour: whether it coded anything at all, and the sign of its DC. | 451 | /// neighbour: whether it coded anything at all, and the sign of its DC. |
| 417 | #[derive(Clone, Copy, Default)] | 452 | #[derive(Clone, Copy, Default)] |
| @@ -443,42 +478,66 @@ fn check_blocks(mi_cols: u32, mi_rows: u32) -> Result<()> { | |||
| 443 | Ok(()) | 478 | Ok(()) |
| 444 | } | 479 | } |
| 445 | 480 | ||
| 446 | -/// Writes the payload of a one-tile key frame in which every superblock is | 481 | +/// The coefficients of one superblock: either one 64x64 block covering it, or |
| 447 | -/// split into 32x32 DC-predicted blocks, each carrying the coefficients | 482 | +/// the four 32x32 blocks it is split into. |
| 448 | -/// `blocks` gives it. | ||
| 449 | /// | 483 | /// |
| 450 | -/// `blocks` gives one coefficient set per 32x32 block in raster order across | 484 | +/// A superblock at the right-hand or bottom edge of the frame may be half |
| 451 | -/// the frame. The frame need not be a whole number of superblocks: a | 485 | +/// outside it, and such a superblock cannot be left whole — the spec has no |
| 452 | -/// superblock at the right-hand or bottom edge may be half outside it, and the | 486 | +/// partition that keeps a block outside the frame — so it must be |
| 453 | -/// blocks that would sit in the half outside are not coded. Each set carries a 32x32 luma transform and the 16x16 transform of | 487 | +/// [`Split`](Superblock::Split), and carries only the quadrants that are |
| 454 | -/// each chroma plane; an empty list writes an all-zero transform block. Unlike | 488 | +/// inside, in raster order among themselves. |
| 455 | -/// the DC writers, the coefficients may sit anywhere in their transform, so a | 489 | +#[derive(Clone, Debug, PartialEq, Eq)] |
| 456 | -/// block here is a picture rather than a flat grey: a single coefficient off | 490 | +pub enum Superblock { |
| 457 | -/// the origin decodes to the basis function it selects. | 491 | + /// One 64x64 block, whose luma transform is 64x64 — of which only the |
| 492 | + /// top-left 32x32 carries coefficients — and whose chroma transforms are | ||
| 493 | + /// 32x32 each at 4:2:0. | ||
| 494 | + Whole(BlockCoeffs), | ||
| 495 | + /// The 32x32 blocks the superblock is split into, each with a 32x32 luma | ||
| 496 | + /// transform and a 16x16 transform per chroma plane. | ||
| 497 | + Split(Vec<BlockCoeffs>), | ||
| 498 | +} | ||
| 499 | + | ||
| 500 | +/// Writes the payload of a one-tile key frame built from `superblocks`, each | ||
| 501 | +/// either one 64x64 block or the 32x32 blocks it splits into. | ||
| 502 | +/// | ||
| 503 | +/// `superblocks` gives one entry per superblock in raster order across the | ||
| 504 | +/// frame. Every block is DC-predicted unless its [`BlockCoeffs::mode`] says | ||
| 505 | +/// otherwise, and carries the coefficients its lists give; the coefficients | ||
| 506 | +/// may sit anywhere in their transform, so a block is a picture rather than a | ||
| 507 | +/// flat grey. | ||
| 458 | /// | 508 | /// |
| 459 | /// # Errors | 509 | /// # Errors |
| 460 | -/// Returns an error when the frame is not a whole number of 32x32 blocks, | 510 | +/// Returns an error when the frame is not a whole number of 32x32 blocks, when |
| 461 | -/// when `blocks` does not carry exactly one set per 32x32 block, | 511 | +/// `superblocks` does not carry exactly one entry per superblock or an entry |
| 462 | -/// when a coefficient sits outside its transform, repeats a position, carries | 512 | +/// does not carry one block per quadrant inside the frame, when a superblock |
| 463 | -/// a zero level or one wider than the base and base-range syntax reach, or | 513 | +/// that is half outside the frame is left whole, when a block names an intra |
| 464 | -/// when `base_q_idx` is outside the q-context band whose default CDFs this | 514 | +/// mode a key frame does not code, when a coefficient sits outside its |
| 465 | -/// crate carries. | 515 | +/// transform, repeats a position, carries a zero level or one wider than the |
| 466 | -pub fn split_coeff_key_frame_tile( | 516 | +/// Golomb tail reaches, or when `base_q_idx` is outside the q-context band |
| 517 | +/// whose default CDFs this crate carries. | ||
| 518 | +pub fn sb_coeff_key_frame_tile( | ||
| 467 | mi_cols: u32, | 519 | mi_cols: u32, |
| 468 | mi_rows: u32, | 520 | mi_rows: u32, |
| 469 | base_q_idx: u8, | 521 | base_q_idx: u8, |
| 470 | - blocks: &[BlockCoeffs], | 522 | + superblocks: &[Superblock], |
| 471 | ) -> Result<Vec<u8>> { | 523 | ) -> Result<Vec<u8>> { |
| 472 | check_blocks(mi_cols, mi_rows)?; | 524 | check_blocks(mi_cols, mi_rows)?; |
| 473 | let (cols, rows) = (mi_cols / BLOCK_MI, mi_rows / BLOCK_MI); | 525 | let (cols, rows) = (mi_cols / BLOCK_MI, mi_rows / BLOCK_MI); |
| 474 | let (sb_cols, sb_rows) = (cols.div_ceil(2), rows.div_ceil(2)); | 526 | let (sb_cols, sb_rows) = (cols.div_ceil(2), rows.div_ceil(2)); |
| 475 | - if blocks.len() != (cols * rows) as usize { | 527 | + if superblocks.len() != (sb_cols * sb_rows) as usize { |
| 476 | return Err(Error::unsupported( | 528 | return Err(Error::unsupported( |
| 477 | "AV1 tile", | 529 | "AV1 tile", |
| 478 | - "a coefficient key frame needs one coefficient set per 32x32 block", | 530 | + "a coefficient key frame needs one entry per superblock", |
| 479 | )); | 531 | )); |
| 480 | } | 532 | } |
| 481 | - if let Some(bad) = blocks.iter().find(|b| usize::from(b.mode) >= INTRA_MODES) { | 533 | + let bad_mode = superblocks |
| 534 | + .iter() | ||
| 535 | + .flat_map(|sb| match sb { | ||
| 536 | + Superblock::Whole(b) => std::slice::from_ref(b), | ||
| 537 | + Superblock::Split(b) => b.as_slice(), | ||
| 538 | + }) | ||
| 539 | + .find(|b| usize::from(b.mode) >= INTRA_MODES); | ||
| 540 | + if let Some(bad) = bad_mode { | ||
| 482 | return Err(Error::unsupported( | 541 | return Err(Error::unsupported( |
| 483 | "AV1 tile", | 542 | "AV1 tile", |
| 484 | format!( | 543 | format!( |
| @@ -494,24 +553,14 @@ pub fn split_coeff_key_frame_tile( | |||
| 494 | base_q_idx must be 61..=120", | 553 | base_q_idx must be 61..=120", |
| 495 | )); | 554 | )); |
| 496 | } | 555 | } |
| 497 | - // One level grid per plane of every block, in the plane order they are | ||
| 498 | - // coded in: luma, then U, then V. | ||
| 499 | - let grids = blocks | ||
| 500 | - .iter() | ||
| 501 | - .map(|b| { | ||
| 502 | - Ok([ | ||
| 503 | - level_grid(&b.luma, TX32)?, | ||
| 504 | - level_grid(&b.u, TX16)?, | ||
| 505 | - level_grid(&b.v, TX16)?, | ||
| 506 | - ]) | ||
| 507 | - }) | ||
| 508 | - .collect::<Result<Vec<_>>>()?; | ||
| 509 | 556 | ||
| 510 | - let planes = [&LUMA_32, &CHROMA_16, &CHROMA_16]; | 557 | + let split_planes = [&LUMA_32, &CHROMA_16, &CHROMA_16]; |
| 558 | + let whole_planes = [&LUMA_64, &CHROMA_32, &CHROMA_32]; | ||
| 511 | let scans = [default_scan(TX32), default_scan(TX16)]; | 559 | let scans = [default_scan(TX32), default_scan(TX16)]; |
| 512 | // What the blocks above and to the left of the one being coded left | 560 | // What the blocks above and to the left of the one being coded left |
| 513 | - // behind, per plane. The left column is reset at every superblock row | 561 | + // behind, per plane, one entry per 32x32 column and row. The left column |
| 514 | - // because a tile starts each row with no left neighbour. | 562 | + // is reset at every superblock row because a tile starts each row with no |
| 563 | + // left neighbour. | ||
| 515 | let mut above = vec![[Neighbour::default(); 3]; cols as usize]; | 564 | let mut above = vec![[Neighbour::default(); 3]; cols as usize]; |
| 516 | let mut left = vec![[Neighbour::default(); 3]; rows as usize]; | 565 | let mut left = vec![[Neighbour::default(); 3]; rows as usize]; |
| 517 | // The luma intra mode of those same neighbours, which picks the CDF the | 566 | // The luma intra mode of those same neighbours, which picks the CDF the |
| @@ -519,13 +568,23 @@ pub fn split_coeff_key_frame_tile( | |||
| 519 | // `DC_PRED`, which is what these start and are reset to. | 568 | // `DC_PRED`, which is what these start and are reset to. |
| 520 | let mut above_mode = vec![DC_PRED; cols as usize]; | 569 | let mut above_mode = vec![DC_PRED; cols as usize]; |
| 521 | let mut left_mode = vec![DC_PRED; rows as usize]; | 570 | let mut left_mode = vec![DC_PRED; rows as usize]; |
| 571 | + // Whether those neighbours were split, which is what the superblock's | ||
| 572 | + // partition symbol reads: the context is bit three of the neighbour's | ||
| 573 | + // entry in the spec's partition-context table, and of the sizes a | ||
| 574 | + // superblock can leave behind only a 32x32 block sets it. A neighbour | ||
| 575 | + // outside the tile reads as an unsplit one, which is what these start and | ||
| 576 | + // are reset to. | ||
| 577 | + let mut above_split = vec![false; cols as usize]; | ||
| 578 | + let mut left_split = vec![false; rows as usize]; | ||
| 522 | 579 | ||
| 523 | let mut enc = SymbolEncoder::new(); | 580 | let mut enc = SymbolEncoder::new(); |
| 524 | for sb_r in 0..sb_rows { | 581 | for sb_r in 0..sb_rows { |
| 525 | left.iter_mut().for_each(|l| *l = Default::default()); | 582 | left.iter_mut().for_each(|l| *l = Default::default()); |
| 526 | left_mode.iter_mut().for_each(|m| *m = DC_PRED); | 583 | left_mode.iter_mut().for_each(|m| *m = DC_PRED); |
| 584 | + left_split.iter_mut().for_each(|l| *l = false); | ||
| 527 | for sb_c in 0..sb_cols { | 585 | for sb_c in 0..sb_cols { |
| 528 | - let ctx = 2 * usize::from(sb_c > 0) + usize::from(sb_r > 0); | 586 | + let (r0, c0) = (sb_r as usize * 2, sb_c as usize * 2); |
| 587 | + let ctx = 2 * usize::from(left_split[r0]) + usize::from(above_split[c0]); | ||
| 529 | // A superblock whose bottom or right half is outside the frame | 588 | // A superblock whose bottom or right half is outside the frame |
| 530 | // cannot be left unsplit, so the decoder reads a flag instead of | 589 | // cannot be left unsplit, so the decoder reads a flag instead of |
| 531 | // the partition symbol — and reads nothing at all when both halves | 590 | // the partition symbol — and reads nothing at all when both halves |
| @@ -534,71 +593,215 @@ pub fn split_coeff_key_frame_tile( | |||
| 534 | sb_c * SB_MI + SB_MI / 2 < mi_cols, | 593 | sb_c * SB_MI + SB_MI / 2 < mi_cols, |
| 535 | sb_r * SB_MI + SB_MI / 2 < mi_rows, | 594 | sb_r * SB_MI + SB_MI / 2 < mi_rows, |
| 536 | ); | 595 | ); |
| 596 | + // The quadrants of this superblock that are inside the frame, as | ||
| 597 | + // positions in the 32x32 block grid. | ||
| 598 | + let quadrants: Vec<(usize, usize)> = (0..4) | ||
| 599 | + .map(|q| (sb_r * 2 + q / 2, sb_c * 2 + q % 2)) | ||
| 600 | + .filter(|&(r, c)| r < rows && c < cols) | ||
| 601 | + .map(|(r, c)| (r as usize, c as usize)) | ||
| 602 | + .collect(); | ||
| 603 | + | ||
| 604 | + match &superblocks[(sb_r * sb_cols + sb_c) as usize] { | ||
| 605 | + Superblock::Whole(block) => { | ||
| 606 | + if !has_cols || !has_rows { | ||
| 607 | + return Err(Error::unsupported( | ||
| 608 | + "AV1 tile", | ||
| 609 | + "a superblock that is half outside the frame cannot be left whole", | ||
| 610 | + )); | ||
| 611 | + } | ||
| 612 | + enc.symbol_fixed(PARTITION_NONE, &cdf::PARTITION_W64[ctx]); | ||
| 613 | + let mode = write_intra_mode( | ||
| 614 | + &mut enc, | ||
| 615 | + block, | ||
| 616 | + above_mode[c0], | ||
| 617 | + left_mode[r0], | ||
| 618 | + // A 64x64 block is too big to be offered chroma from | ||
| 619 | + // luma, so its chroma mode reads the table without it. | ||
| 620 | + &cdf::UV_MODE_NO_CFL[usize::from(block.mode)], | ||
| 621 | + ); | ||
| 622 | + let grids = [ | ||
| 623 | + level_grid(&block.luma, TX32)?, | ||
| 624 | + level_grid(&block.u, TX32)?, | ||
| 625 | + level_grid(&block.v, TX32)?, | ||
| 626 | + ]; | ||
| 627 | + write_block_planes( | ||
| 628 | + &mut enc, | ||
| 629 | + &whole_planes, | ||
| 630 | + &grids, | ||
| 631 | + &[&scans[0], &scans[0], &scans[0]], | ||
| 632 | + &above[c0], | ||
| 633 | + &left[r0], | ||
| 634 | + ); | ||
| 635 | + // The block covers two columns and two rows of the 32x32 | ||
| 636 | + // grid, so both of each read it as their neighbour. | ||
| 637 | + for (r, c) in quadrants { | ||
| 638 | + for plane in 0..3 { | ||
| 639 | + let state = neighbour_state(&grids[plane]); | ||
| 640 | + above[c][plane] = state; | ||
| 641 | + left[r][plane] = state; | ||
| 642 | + } | ||
| 643 | + above_mode[c] = mode; | ||
| 644 | + left_mode[r] = mode; | ||
| 645 | + above_split[c] = false; | ||
| 646 | + left_split[r] = false; | ||
| 647 | + } | ||
| 648 | + } | ||
| 649 | + Superblock::Split(blocks) => { | ||
| 537 | match (has_cols, has_rows) { | 650 | match (has_cols, has_rows) { |
| 538 | (true, true) => enc.symbol_fixed(PARTITION_SPLIT, &cdf::PARTITION_W64[ctx]), | 651 | (true, true) => enc.symbol_fixed(PARTITION_SPLIT, &cdf::PARTITION_W64[ctx]), |
| 539 | - (true, false) => enc.symbol_fixed(1, &gather(&cdf::PARTITION_W64[ctx], VERT_ALIKE)), | 652 | + (true, false) => { |
| 540 | - (false, true) => enc.symbol_fixed(1, &gather(&cdf::PARTITION_W64[ctx], HORZ_ALIKE)), | 653 | + enc.symbol_fixed(1, &gather(&cdf::PARTITION_W64[ctx], VERT_ALIKE)); |
| 654 | + } | ||
| 655 | + (false, true) => { | ||
| 656 | + enc.symbol_fixed(1, &gather(&cdf::PARTITION_W64[ctx], HORZ_ALIKE)); | ||
| 657 | + } | ||
| 541 | (false, false) => {} | 658 | (false, false) => {} |
| 542 | } | 659 | } |
| 543 | - | 660 | + if blocks.len() != quadrants.len() { |
| 544 | - for quadrant in 0..4 { | 661 | + return Err(Error::unsupported( |
| 545 | - let (r, c) = (sb_r * 2 + quadrant / 2, sb_c * 2 + quadrant % 2); | 662 | + "AV1 tile", |
| 546 | - if r >= rows || c >= cols { | 663 | + "a split superblock needs one block per quadrant inside the frame", |
| 547 | - // The quadrant is outside the frame: the decoder never | 664 | + )); |
| 548 | - // walks into it. | ||
| 549 | - continue; | ||
| 550 | } | 665 | } |
| 551 | - let (r, c) = (r as usize, c as usize); | 666 | + for (block, (r, c)) in blocks.iter().zip(quadrants) { |
| 552 | - let block = &grids[r * cols as usize + c]; | ||
| 553 | - let mode = usize::from(blocks[r * cols as usize + c].mode); | ||
| 554 | - | ||
| 555 | enc.symbol_fixed(PARTITION_NONE, &cdf::PARTITION_W32[0]); | 667 | enc.symbol_fixed(PARTITION_NONE, &cdf::PARTITION_W32[0]); |
| 668 | + let mode = write_intra_mode( | ||
| 669 | + &mut enc, | ||
| 670 | + block, | ||
| 671 | + above_mode[c], | ||
| 672 | + left_mode[r], | ||
| 673 | + &cdf::UV_MODE_CFL[usize::from(block.mode)], | ||
| 674 | + ); | ||
| 675 | + let grids = [ | ||
| 676 | + level_grid(&block.luma, TX32)?, | ||
| 677 | + level_grid(&block.u, TX16)?, | ||
| 678 | + level_grid(&block.v, TX16)?, | ||
| 679 | + ]; | ||
| 680 | + write_block_planes( | ||
| 681 | + &mut enc, | ||
| 682 | + &split_planes, | ||
| 683 | + &grids, | ||
| 684 | + &[&scans[0], &scans[1], &scans[1]], | ||
| 685 | + &above[c], | ||
| 686 | + &left[r], | ||
| 687 | + ); | ||
| 688 | + for plane in 0..3 { | ||
| 689 | + let state = neighbour_state(&grids[plane]); | ||
| 690 | + above[c][plane] = state; | ||
| 691 | + left[r][plane] = state; | ||
| 692 | + } | ||
| 693 | + above_mode[c] = mode; | ||
| 694 | + left_mode[r] = mode; | ||
| 695 | + above_split[c] = true; | ||
| 696 | + left_split[r] = true; | ||
| 697 | + } | ||
| 698 | + } | ||
| 699 | + } | ||
| 700 | + } | ||
| 701 | + } | ||
| 702 | + Ok(enc.finish()) | ||
| 703 | +} | ||
| 704 | + | ||
| 705 | +/// Writes everything a key frame's block carries before its coefficients: the | ||
| 706 | +/// skip flag, its luma intra mode against the CDF its neighbours' modes pick, | ||
| 707 | +/// the angle a directional mode is steered by, and its chroma mode. Hands back | ||
| 708 | +/// the luma mode, which is what the blocks beside it read. | ||
| 709 | +fn write_intra_mode( | ||
| 710 | + enc: &mut SymbolEncoder, | ||
| 711 | + block: &BlockCoeffs, | ||
| 712 | + above_mode: usize, | ||
| 713 | + left_mode: usize, | ||
| 714 | + uv_cdf: &[u16], | ||
| 715 | +) -> usize { | ||
| 716 | + let mode = usize::from(block.mode); | ||
| 556 | enc.symbol_fixed(0, &cdf::SKIP[0]); | 717 | enc.symbol_fixed(0, &cdf::SKIP[0]); |
| 557 | enc.symbol_fixed( | 718 | enc.symbol_fixed( |
| 558 | mode, | 719 | mode, |
| 559 | - &cdf::KF_Y_MODE[INTRA_MODE_CTX[above_mode[c]]][INTRA_MODE_CTX[left_mode[r]]], | 720 | + &cdf::KF_Y_MODE[INTRA_MODE_CTX[above_mode]][INTRA_MODE_CTX[left_mode]], |
| 560 | ); | 721 | ); |
| 561 | if (V_PRED..=D67_PRED).contains(&mode) { | 722 | if (V_PRED..=D67_PRED).contains(&mode) { |
| 562 | enc.symbol_fixed(ANGLE_DELTA_ZERO, &cdf::ANGLE_DELTA[mode - V_PRED]); | 723 | enc.symbol_fixed(ANGLE_DELTA_ZERO, &cdf::ANGLE_DELTA[mode - V_PRED]); |
| 563 | } | 724 | } |
| 564 | - enc.symbol_fixed(DC_PRED, &cdf::UV_MODE_CFL[mode]); | 725 | + enc.symbol_fixed(DC_PRED, uv_cdf); |
| 565 | - above_mode[c] = mode; | 726 | + mode |
| 566 | - left_mode[r] = mode; | 727 | +} |
| 567 | 728 | ||
| 568 | - for (plane, grid) in block.iter().enumerate() { | 729 | +/// Writes the three transform blocks of one coded block, in the order a |
| 569 | - let coding = planes[plane]; | 730 | +/// decoder reads them. |
| 570 | - // Luma's transform covers its whole block, which fixes the | 731 | +fn write_block_planes( |
| 571 | - // all-zero flag's context at zero; a chroma transform reads | 732 | + enc: &mut SymbolEncoder, |
| 572 | - // whether its neighbours coded anything, on top of the | 733 | + planes: &[&TxbCoding; 3], |
| 573 | - // offset the chroma tables start at. | 734 | + grids: &[Vec<i32>; 3], |
| 735 | + scans: &[&Vec<u16>; 3], | ||
| 736 | + above: &[Neighbour; 3], | ||
| 737 | + left: &[Neighbour; 3], | ||
| 738 | +) { | ||
| 739 | + for (plane, grid) in grids.iter().enumerate() { | ||
| 740 | + // Luma's transform covers its whole block, which fixes the all-zero | ||
| 741 | + // flag's context at zero; a chroma transform reads whether its | ||
| 742 | + // neighbours coded anything, on top of the offset the chroma tables | ||
| 743 | + // start at. | ||
| 574 | let skip_ctx = if plane == 0 { | 744 | let skip_ctx = if plane == 0 { |
| 575 | 0 | 745 | 0 |
| 576 | } else { | 746 | } else { |
| 577 | - usize::from(above[c][plane].coded) + usize::from(left[r][plane].coded) | 747 | + usize::from(above[plane].coded) + usize::from(left[plane].coded) |
| 578 | }; | 748 | }; |
| 579 | write_coeffs( | 749 | write_coeffs( |
| 580 | - &mut enc, | 750 | + enc, |
| 581 | - coding, | 751 | + planes[plane], |
| 582 | grid, | 752 | grid, |
| 583 | - &scans[usize::from(plane > 0)], | 753 | + scans[plane], |
| 584 | skip_ctx, | 754 | skip_ctx, |
| 585 | - dc_sign_ctx(above[c][plane].dc, left[r][plane].dc), | 755 | + dc_sign_ctx(above[plane].dc, left[plane].dc), |
| 586 | ); | 756 | ); |
| 587 | } | 757 | } |
| 758 | +} | ||
| 588 | 759 | ||
| 589 | - for plane in 0..3 { | 760 | +/// What a coded transform block leaves behind for the blocks that read it as a |
| 590 | - let grid = &block[plane]; | 761 | +/// neighbour. |
| 591 | - let state = Neighbour { | 762 | +fn neighbour_state(grid: &[i32]) -> Neighbour { |
| 763 | + Neighbour { | ||
| 592 | coded: grid.iter().any(|&l| l != 0), | 764 | coded: grid.iter().any(|&l| l != 0), |
| 593 | dc: (grid[0] != 0).then_some(grid[0] < 0), | 765 | dc: (grid[0] != 0).then_some(grid[0] < 0), |
| 594 | - }; | ||
| 595 | - above[c][plane] = state; | ||
| 596 | - left[r][plane] = state; | ||
| 597 | - } | ||
| 598 | } | 766 | } |
| 599 | } | 767 | } |
| 768 | + | ||
| 769 | +/// Writes the payload of a one-tile key frame in which every superblock is | ||
| 770 | +/// split into 32x32 blocks, each carrying the coefficients `blocks` gives it. | ||
| 771 | +/// | ||
| 772 | +/// `blocks` gives one coefficient set per 32x32 block in raster order across | ||
| 773 | +/// the frame. This is [`sb_coeff_key_frame_tile`] with every superblock split. | ||
| 774 | +/// | ||
| 775 | +/// # Errors | ||
| 776 | +/// As [`sb_coeff_key_frame_tile`], with `blocks` sized for the 32x32 grid. | ||
| 777 | +pub fn split_coeff_key_frame_tile( | ||
| 778 | + mi_cols: u32, | ||
| 779 | + mi_rows: u32, | ||
| 780 | + base_q_idx: u8, | ||
| 781 | + blocks: &[BlockCoeffs], | ||
| 782 | +) -> Result<Vec<u8>> { | ||
| 783 | + check_blocks(mi_cols, mi_rows)?; | ||
| 784 | + let (cols, rows) = (mi_cols / BLOCK_MI, mi_rows / BLOCK_MI); | ||
| 785 | + if blocks.len() != (cols * rows) as usize { | ||
| 786 | + return Err(Error::unsupported( | ||
| 787 | + "AV1 tile", | ||
| 788 | + "a coefficient key frame needs one coefficient set per 32x32 block", | ||
| 789 | + )); | ||
| 600 | } | 790 | } |
| 601 | - Ok(enc.finish()) | 791 | + let (sb_cols, sb_rows) = (cols.div_ceil(2), rows.div_ceil(2)); |
| 792 | + let superblocks: Vec<Superblock> = (0..sb_rows) | ||
| 793 | + .flat_map(|sb_r| (0..sb_cols).map(move |sb_c| (sb_r, sb_c))) | ||
| 794 | + .map(|(sb_r, sb_c)| { | ||
| 795 | + Superblock::Split( | ||
| 796 | + (0..4) | ||
| 797 | + .map(|q| (sb_r * 2 + q / 2, sb_c * 2 + q % 2)) | ||
| 798 | + .filter(|&(r, c)| r < rows && c < cols) | ||
| 799 | + .map(|(r, c)| blocks[(r * cols + c) as usize].clone()) | ||
| 800 | + .collect(), | ||
| 801 | + ) | ||
| 802 | + }) | ||
| 803 | + .collect(); | ||
| 804 | + sb_coeff_key_frame_tile(mi_cols, mi_rows, base_q_idx, &superblocks) | ||
| 602 | } | 805 | } |
| 603 | 806 | ||
| 604 | /// Lays one plane's coefficient list out over its transform, rejecting the | 807 | /// Lays one plane's coefficient list out over its transform, rejecting the |
| @@ -1994,6 +2197,102 @@ mod tests { | |||
| 1994 | /// alternating, so a block written into the wrong place or a flag the | 2197 | /// alternating, so a block written into the wrong place or a flag the |
| 1995 | /// decoder reads as a different number of bits shows up as a block with the | 2198 | /// decoder reads as a different number of bits shows up as a block with the |
| 1996 | /// other block's shape — or as a decode failure. | 2199 | /// other block's shape — or as a decode failure. |
| 2200 | + fn decode_luma_sb(width: usize, height: usize, sbs: &[Superblock]) -> Vec<Vec<u8>> { | ||
| 2201 | + let (seq, header) = frame_of(width as u32, height as u32); | ||
| 2202 | + let tile = sb_coeff_key_frame_tile(header.mi_cols, header.mi_rows, Q_IDX, sbs).unwrap(); | ||
| 2203 | + let mut stream = temporal_delimiter(); | ||
| 2204 | + stream.extend_from_slice(&sequence_header_obu(&seq).unwrap()); | ||
| 2205 | + stream.extend_from_slice(&frame_obu(&seq, &header, &tile).unwrap()); | ||
| 2206 | + let planes = ffmpeg_decode(&stream, width, height); | ||
| 2207 | + planes[..width * height] | ||
| 2208 | + .chunks_exact(width) | ||
| 2209 | + .map(<[u8]>::to_vec) | ||
| 2210 | + .collect() | ||
| 2211 | + } | ||
| 2212 | + | ||
| 2213 | + /// A superblock left whole carries one 64x64 transform, whose lowest | ||
| 2214 | + /// horizontal basis function falls across the whole sixty-four samples in | ||
| 2215 | + /// one stroke. Were the same superblock split, each half would restart the | ||
| 2216 | + /// gradient at its own left edge and the picture would rise again in the | ||
| 2217 | + /// middle, so the walk across the row is what tells the two apart — and | ||
| 2218 | + /// the split superblock beside it proves the two partitions still agree on | ||
| 2219 | + /// where the next one begins. | ||
| 2220 | + #[test] | ||
| 2221 | + fn a_whole_superblock_covers_all_four_of_its_quadrants() { | ||
| 2222 | + if !have_ffmpeg() { | ||
| 2223 | + eprintln!("SKIP a_whole_superblock_covers_all_four_of_its_quadrants: no ffmpeg"); | ||
| 2224 | + return; | ||
| 2225 | + } | ||
| 2226 | + let across = BlockCoeffs::from(vec![Coeff { | ||
| 2227 | + row: 0, | ||
| 2228 | + col: 1, | ||
| 2229 | + level: 20, | ||
| 2230 | + }]); | ||
| 2231 | + let down = BlockCoeffs::from(vec![Coeff { | ||
| 2232 | + row: 1, | ||
| 2233 | + col: 0, | ||
| 2234 | + level: 20, | ||
| 2235 | + }]); | ||
| 2236 | + let sbs = [ | ||
| 2237 | + Superblock::Whole(across), | ||
| 2238 | + Superblock::Split(vec![down.clone(), down.clone(), down.clone(), down]), | ||
| 2239 | + ]; | ||
| 2240 | + let rows = decode_luma_sb(128, 64, &sbs); | ||
| 2241 | + | ||
| 2242 | + for (y, row) in rows.iter().enumerate() { | ||
| 2243 | + assert_eq!( | ||
| 2244 | + &row[..64], | ||
| 2245 | + &rows[0][..64], | ||
| 2246 | + "row {y} of the whole superblock repeats its first row" | ||
| 2247 | + ); | ||
| 2248 | + } | ||
| 2249 | + let first = &rows[0][..64]; | ||
| 2250 | + assert!( | ||
| 2251 | + first.windows(2).all(|w| w[0] >= w[1]), | ||
| 2252 | + "the whole superblock falls across its sixty-four samples without \ | ||
| 2253 | + restarting: {first:?}" | ||
| 2254 | + ); | ||
| 2255 | + assert!( | ||
| 2256 | + first[0] > first[63], | ||
| 2257 | + "the whole superblock falls from left to right, got {} then {}", | ||
| 2258 | + first[0], | ||
| 2259 | + first[63] | ||
| 2260 | + ); | ||
| 2261 | + for block_row in 0..2 { | ||
| 2262 | + for block_col in 2..4 { | ||
| 2263 | + let block = block_at(&rows, block_row, block_col); | ||
| 2264 | + assert_falls_down(&block, &format!("quadrant ({block_row},{block_col})")); | ||
| 2265 | + } | ||
| 2266 | + } | ||
| 2267 | + } | ||
| 2268 | + | ||
| 2269 | + /// A superblock half outside the frame has no partition that keeps a block | ||
| 2270 | + /// outside it, so it cannot be left whole. | ||
| 2271 | + #[test] | ||
| 2272 | + fn a_whole_superblock_at_the_frame_edge_is_refused() { | ||
| 2273 | + let block = BlockCoeffs::from(vec![Coeff { | ||
| 2274 | + row: 0, | ||
| 2275 | + col: 0, | ||
| 2276 | + level: 4, | ||
| 2277 | + }]); | ||
| 2278 | + let sbs = [ | ||
| 2279 | + Superblock::Split(vec![ | ||
| 2280 | + block.clone(), | ||
| 2281 | + block.clone(), | ||
| 2282 | + block.clone(), | ||
| 2283 | + block.clone(), | ||
| 2284 | + ]), | ||
| 2285 | + Superblock::Whole(block.clone()), | ||
| 2286 | + ]; | ||
| 2287 | + // Ninety-six samples across is three 32x32 blocks, so the second | ||
| 2288 | + // superblock hangs half outside the frame. | ||
| 2289 | + let err = sb_coeff_key_frame_tile(24, 16, Q_IDX, &sbs).unwrap_err(); | ||
| 2290 | + assert!( | ||
| 2291 | + format!("{err}").contains("half outside the frame"), | ||
| 2292 | + "got {err}" | ||
| 2293 | + ); | ||
| 2294 | + } | ||
| 2295 | + | ||
| 1997 | #[test] | 2296 | #[test] |
| 1998 | fn a_frame_that_is_not_a_whole_number_of_superblocks_codes_every_block() { | 2297 | fn a_frame_that_is_not_a_whole_number_of_superblocks_codes_every_block() { |
| 1999 | if !have_ffmpeg() { | 2298 | if !have_ffmpeg() { |