One coded frame decodes from a bank snapshot alone (decode_frame)
Browse files at this commit1241afcf53997e963425ddcb020d3b0558bbbcf2Parent: 63370ce- refactor
- one coded frame decodes from a bank snapshot alone (decode_frame)
1 changed file +250 -119
- crates/ec-av1/src/stream.rs +250 -119
crates/ec-av1/src/stream.rsmodified+250 -119
| @@ -473,6 +473,227 @@ pub fn decode_stream_with( | |||
| 473 | // reader on the inter path, so they still refuse by name rather than | 473 | // reader on the inter path, so they still refuse by name rather than |
| 474 | // desyncing. aomenc's `--aq-mode` segmentation only ever sets | 474 | // desyncing. aomenc's `--aq-mode` segmentation only ever sets |
| 475 | // `SEG_LVL_ALT_Q`. | 475 | // `SEG_LVL_ALT_Q`. |
| 476 | + let tile_bufs: Vec<&[u8]> = tiles | ||
| 477 | + .iter() | ||
| 478 | + .map(|t| { | ||
| 479 | + data.get(tiles_base_offset + t.offset..tiles_base_offset + t.offset + t.size) | ||
| 480 | + .ok_or(Error::NeedMore) | ||
| 481 | + }) | ||
| 482 | + .collect::<Result<_>>()?; | ||
| 483 | + let seq = SeqFlags::read(&parser); | ||
| 484 | + let snap = RefSnapshot::take( | ||
| 485 | + &header, | ||
| 486 | + seq, | ||
| 487 | + std::array::from_fn(|i| ref_slots[i].as_ref()), | ||
| 488 | + &cdf_slots, | ||
| 489 | + &seg_map_slots, | ||
| 490 | + &motion_field_slots, | ||
| 491 | + &order_hint_slots, | ||
| 492 | + ); | ||
| 493 | + let bit_depth = seq.bit_depth; | ||
| 494 | + let FrameOutput { | ||
| 495 | + picture, | ||
| 496 | + stored_cdfs, | ||
| 497 | + motion_field, | ||
| 498 | + seg_map: decoded_seg_map, | ||
| 499 | + } = decode_frame( | ||
| 500 | + fctx, | ||
| 501 | + &header, | ||
| 502 | + &tile_bufs, | ||
| 503 | + seq, | ||
| 504 | + snap, | ||
| 505 | + pictures_decoded, | ||
| 506 | + pictures_shown, | ||
| 507 | + final_dump.as_deref(), | ||
| 508 | + )?; | ||
| 509 | + pictures_decoded += 1; | ||
| 510 | + for i in 0..NUM_REF_FRAMES { | ||
| 511 | + if header.refresh_frame_flags & (1 << i) != 0 { | ||
| 512 | + cdf_slots[i] = Some(stored_cdfs.clone()); | ||
| 513 | + // Spec 7.18.3.1: synthesized grain is never stored for later | ||
| 514 | + // prediction -- the reference bank always keeps the clean, | ||
| 515 | + // pre-grain decode, even though the frame pushed onto the | ||
| 516 | + // caller's output below carries the grained picture. | ||
| 517 | + ref_slots[i] = Some(picture.clone()); | ||
| 518 | + motion_field_slots[i] = Some(motion_field.clone()); | ||
| 519 | + seg_map_slots[i] = Some(decoded_seg_map.clone()); | ||
| 520 | + order_hint_slots[i] = header.order_hint; | ||
| 521 | + } | ||
| 522 | + } | ||
| 523 | + // lane-defon r1: a forward keyframe (aomenc `--enable-fwd-kf`) is a | ||
| 524 | + // KEY frame coded with `show_frame == 0`, output later by a | ||
| 525 | + // `show_existing_frame` header -- counted only once its tiles have | ||
| 526 | + // decoded, so the gate's assert covers decoded-and-compared frames. | ||
| 527 | + if header.frame_type == FrameType::Key && !header.show_frame { | ||
| 528 | + decode::note_fwd_kf(); | ||
| 529 | + } | ||
| 530 | + if header.show_frame { | ||
| 531 | + let output = if header.film_grain.apply_grain && !no_grain() { | ||
| 532 | + let mc_identity = seq.mc_identity; | ||
| 533 | + crate::film_grain::apply_grain( | ||
| 534 | + &picture, | ||
| 535 | + &header.film_grain, | ||
| 536 | + mc_identity, | ||
| 537 | + bit_depth as u32, fctx, | ||
| 538 | + ) | ||
| 539 | + } else { | ||
| 540 | + picture | ||
| 541 | + }; | ||
| 542 | + sink(&output, pictures_decoded - 1, true)?; | ||
| 543 | + pictures_shown += 1; | ||
| 544 | + } else { | ||
| 545 | + // Hidden (alt-ref/forward-key) frames never reach | ||
| 546 | + // `decode_stream`'s output, but a decode-order consumer -- the | ||
| 547 | + // `EC_AV1_FINAL_DUMP` comparison against aomdec, say -- needs | ||
| 548 | + // them, so they are reported too, with `shown == false`. | ||
| 549 | + sink(&picture, pictures_decoded - 1, false)?; | ||
| 550 | + } | ||
| 551 | + } | ||
| 552 | + Ok(()) | ||
| 553 | +} | ||
| 554 | + | ||
| 555 | + | ||
| 556 | + | ||
| 557 | +/// lane-thread2: the sequence-header bits a single frame's decode needs, | ||
| 558 | +/// read once on the parsing thread so that [`decode_frame`] never touches the | ||
| 559 | +/// [`Av1Parser`] (a worker thread has no access to it). | ||
| 560 | +#[derive(Clone, Copy)] | ||
| 561 | +pub(crate) struct SeqFlags { | ||
| 562 | + pub(crate) enable_filter_intra: bool, | ||
| 563 | + pub(crate) enable_dual_filter: bool, | ||
| 564 | + pub(crate) enable_edge_filter: bool, | ||
| 565 | + pub(crate) enable_masked_compound: bool, | ||
| 566 | + pub(crate) enable_jnt_comp: bool, | ||
| 567 | + pub(crate) enable_interintra_compound: bool, | ||
| 568 | + pub(crate) use_sb128: bool, | ||
| 569 | + pub(crate) bit_depth: u8, | ||
| 570 | + pub(crate) order_hint_bits: u32, | ||
| 571 | + pub(crate) mc_identity: bool, | ||
| 572 | +} | ||
| 573 | + | ||
| 574 | +impl SeqFlags { | ||
| 575 | + fn read(parser: &Av1Parser) -> Self { | ||
| 576 | + let seq = parser.sequence_header(); | ||
| 577 | + Self { | ||
| 578 | + enable_filter_intra: seq.is_some_and(|s| s.enable_filter_intra), | ||
| 579 | + enable_dual_filter: seq.is_some_and(|s| s.enable_dual_filter), | ||
| 580 | + enable_edge_filter: seq.is_some_and(|s| s.enable_intra_edge_filter), | ||
| 581 | + enable_masked_compound: seq.is_some_and(|s| s.enable_masked_compound), | ||
| 582 | + enable_jnt_comp: seq.is_some_and(|s| s.enable_jnt_comp), | ||
| 583 | + enable_interintra_compound: seq.is_some_and(|s| s.enable_interintra_compound), | ||
| 584 | + use_sb128: seq.is_some_and(|s| s.use_128x128_superblock), | ||
| 585 | + bit_depth: seq.map_or(8, |s| s.color_config.bit_depth), | ||
| 586 | + order_hint_bits: seq.map_or(0, |s| s.order_hint_bits), | ||
| 587 | + mc_identity: seq.is_some_and(|s| s.color_config.matrix_coefficients == 0), | ||
| 588 | + } | ||
| 589 | + } | ||
| 590 | +} | ||
| 591 | + | ||
| 592 | +/// lane-thread2: everything one frame's decode reads out of the cross-frame | ||
| 593 | +/// banks `decode_stream_with` owns -- taken on the parsing thread, before any | ||
| 594 | +/// later frame can overwrite a slot, so a frame can be decoded anywhere. | ||
| 595 | +pub(crate) struct RefSnapshot<'a> { | ||
| 596 | + /// The 8 DPB slots, indexed by slot (NOT by `ref_frame_idx`). | ||
| 597 | + pub(crate) refs: [Option<&'a Picture>; NUM_REF_FRAMES], | ||
| 598 | + /// `cdf_slots[ref_frame_idx[primary_ref_frame]]`, `None` when this frame | ||
| 599 | + /// names `PRIMARY_REF_NONE` *or* that slot holds no saved table (the | ||
| 600 | + /// second case is the refusal [`decode_frame`] raises where it always | ||
| 601 | + /// did, so the error order over a malformed stream is unchanged). | ||
| 602 | + pub(crate) primary_cdfs: Option<Cdfs>, | ||
| 603 | + /// Spec `load_previous_segment_ids`, from the same slot. | ||
| 604 | + pub(crate) prev_seg_map: Option<Vec<u8>>, | ||
| 605 | + /// Spec 7.9's projection, already run against `motion_field_slots`. | ||
| 606 | + pub(crate) tpl_field: Option<crate::motion_field::TplField>, | ||
| 607 | + /// `order_hint_slots[ref_frame_idx[i]]` for `LAST_FRAME + i`. | ||
| 608 | + pub(crate) ref_order_hints: [u32; 7], | ||
| 609 | +} | ||
| 610 | + | ||
| 611 | +impl<'a> RefSnapshot<'a> { | ||
| 612 | + /// Reads the banks for `header`. `refs` is passed in so that the | ||
| 613 | + /// single-threaded path can borrow the DPB pictures in place while the | ||
| 614 | + /// worker path hands over its own `Arc` clones. | ||
| 615 | + fn take( | ||
| 616 | + header: &FrameHeader, | ||
| 617 | + seq: SeqFlags, | ||
| 618 | + refs: [Option<&'a Picture>; NUM_REF_FRAMES], | ||
| 619 | + cdf_slots: &[Option<Cdfs>; NUM_REF_FRAMES], | ||
| 620 | + seg_map_slots: &[Option<Vec<u8>>; NUM_REF_FRAMES], | ||
| 621 | + motion_field_slots: &[Option<crate::motion_field::MotionField>; NUM_REF_FRAMES], | ||
| 622 | + order_hint_slots: &[u32; NUM_REF_FRAMES], | ||
| 623 | + ) -> Self { | ||
| 624 | + let primary = (header.primary_ref_frame != PRIMARY_REF_NONE) | ||
| 625 | + .then(|| header.ref_frame_idx[header.primary_ref_frame as usize] as usize); | ||
| 626 | + // spec 7.9's own driver, `av1_setup_motion_field`: only run when | ||
| 627 | + // this frame's header asks for it, and never for a key frame (which | ||
| 628 | + // codes no inter block at all). | ||
| 629 | + let tpl_field = (header.frame_type != FrameType::Key && header.use_ref_frame_mvs).then( | ||
| 630 | + || { | ||
| 631 | + crate::motion_field::setup_motion_field( | ||
| 632 | + motion_field_slots, | ||
| 633 | + header.ref_frame_idx, | ||
| 634 | + header.order_hint, | ||
| 635 | + seq.order_hint_bits, | ||
| 636 | + header.mi_rows as usize, | ||
| 637 | + header.mi_cols as usize, | ||
| 638 | + ) | ||
| 639 | + }, | ||
| 640 | + ); | ||
| 641 | + if std::env::var_os("EC_TPL").is_some() { | ||
| 642 | + eprintln!( | ||
| 643 | + "EC_TPL order_hint={} use_ref_frame_mvs={} cells={}", | ||
| 644 | + header.order_hint, | ||
| 645 | + header.use_ref_frame_mvs, | ||
| 646 | + tpl_field | ||
| 647 | + .as_ref() | ||
| 648 | + .map_or(0, crate::motion_field::TplField::filled_cells) | ||
| 649 | + ); | ||
| 650 | + } | ||
| 651 | + Self { | ||
| 652 | + refs, | ||
| 653 | + primary_cdfs: primary.and_then(|slot| cdf_slots[slot].clone()), | ||
| 654 | + prev_seg_map: primary.and_then(|slot| seg_map_slots[slot].clone()), | ||
| 655 | + tpl_field, | ||
| 656 | + ref_order_hints: std::array::from_fn(|i| { | ||
| 657 | + order_hint_slots[header.ref_frame_idx[i] as usize] | ||
| 658 | + }), | ||
| 659 | + } | ||
| 660 | + } | ||
| 661 | +} | ||
| 662 | + | ||
| 663 | +/// lane-thread2: what one decoded frame contributes back to the stream -- | ||
| 664 | +/// its picture (clean, pre-grain: spec 7.18.3.1) plus the three pieces a | ||
| 665 | +/// `refresh_frame_flags` bit stores into a reference slot. | ||
| 666 | +pub(crate) struct FrameOutput { | ||
| 667 | + pub(crate) picture: Picture, | ||
| 668 | + pub(crate) stored_cdfs: Cdfs, | ||
| 669 | + pub(crate) motion_field: crate::motion_field::MotionField, | ||
| 670 | + pub(crate) seg_map: Vec<u8>, | ||
| 671 | +} | ||
| 672 | + | ||
| 673 | +/// lane-thread2: one coded frame, decoded from its header, its tile payload | ||
| 674 | +/// and a snapshot of the reference banks -- and from nothing else. Lifted | ||
| 675 | +/// verbatim out of [`decode_stream_with`]'s loop so that the same code can | ||
| 676 | +/// run on the parsing thread (a frame that refreshes a reference slot) or on | ||
| 677 | +/// a worker (a leaf frame, see `decode_stream_with_threads`). | ||
| 678 | +/// | ||
| 679 | +/// `fctx` is the DECODING thread's context: a worker uses a fresh one, which | ||
| 680 | +/// is sound because every field this path reads is written by this function | ||
| 681 | +/// (or by the tile decoders it calls) before it is read -- see the | ||
| 682 | +/// `a_worker_context_starts_where_the_stream_context_would` gate. | ||
| 683 | +/// | ||
| 684 | +/// # Errors | ||
| 685 | +/// As [`decode_stream_with`], for this one frame. | ||
| 686 | +#[allow(clippy::too_many_arguments)] | ||
| 687 | +fn decode_frame( | ||
| 688 | + fctx: &crate::decode::FrameCtx, | ||
| 689 | + header: &FrameHeader, | ||
| 690 | + tile_bufs: &[&[u8]], | ||
| 691 | + seq: SeqFlags, | ||
| 692 | + snap: RefSnapshot<'_>, | ||
| 693 | + decode_idx: usize, | ||
| 694 | + shown_idx: usize, | ||
| 695 | + final_dump: Option<&str>, | ||
| 696 | +) -> Result<FrameOutput> { | ||
| 476 | if header.segmentation.enabled { | 697 | if header.segmentation.enabled { |
| 477 | for segment in 0..MAX_SEGMENTS { | 698 | for segment in 0..MAX_SEGMENTS { |
| 478 | for feature in [SEG_LVL_REF_FRAME, SEG_LVL_SKIP, SEG_LVL_GLOBALMV] { | 699 | for feature in [SEG_LVL_REF_FRAME, SEG_LVL_SKIP, SEG_LVL_GLOBALMV] { |
| @@ -487,16 +708,11 @@ pub fn decode_stream_with( | |||
| 487 | } | 708 | } |
| 488 | // spec `load_previous_segment_ids`: `PrevSegmentIds` comes from the | 709 | // spec `load_previous_segment_ids`: `PrevSegmentIds` comes from the |
| 489 | // primary reference frame's slot, and is all-zero without one. | 710 | // primary reference frame's slot, and is all-zero without one. |
| 490 | - let prev_seg_map = if header.primary_ref_frame == PRIMARY_REF_NONE { | ||
| 491 | - None | ||
| 492 | - } else { | ||
| 493 | - seg_map_slots[header.ref_frame_idx[header.primary_ref_frame as usize] as usize].clone() | ||
| 494 | - }; | ||
| 495 | decode::set_segmentation( | 711 | decode::set_segmentation( |
| 496 | header.segmentation, | 712 | header.segmentation, |
| 497 | header.mi_rows as usize, | 713 | header.mi_rows as usize, |
| 498 | header.mi_cols as usize, | 714 | header.mi_cols as usize, |
| 499 | - prev_seg_map.as_deref(), fctx, | 715 | + snap.prev_seg_map.as_deref(), fctx, |
| 500 | ); | 716 | ); |
| 501 | // lane-superres stage 2/3: `use_superres` adds/removes no per-block | 717 | // lane-superres stage 2/3: `use_superres` adds/removes no per-block |
| 502 | // symbol (spec 7.16's upscaling is a pixel-domain post-process | 718 | // symbol (spec 7.16's upscaling is a pixel-domain post-process |
| @@ -649,22 +865,9 @@ pub fn decode_stream_with( | |||
| 649 | // `Tile::offset` is relative to the buffer `parse_obu` was handed | 865 | // `Tile::offset` is relative to the buffer `parse_obu` was handed |
| 650 | // (`&data[pos..]` at the time this OBU was parsed), so it is relative | 866 | // (`&data[pos..]` at the time this OBU was parsed), so it is relative |
| 651 | // to `obu_offset`, not to `data` as a whole. | 867 | // to `obu_offset`, not to `data` as a whole. |
| 652 | - let tile_bufs: Vec<&[u8]> = tiles | 868 | + let enable_filter_intra = seq.enable_filter_intra; |
| 653 | - .iter() | 869 | + let enable_dual_filter = seq.enable_dual_filter; |
| 654 | - .map(|t| { | 870 | + let enable_edge_filter = seq.enable_edge_filter; |
| 655 | - data.get(tiles_base_offset + t.offset..tiles_base_offset + t.offset + t.size) | ||
| 656 | - .ok_or(Error::NeedMore) | ||
| 657 | - }) | ||
| 658 | - .collect::<Result<_>>()?; | ||
| 659 | - let enable_filter_intra = parser | ||
| 660 | - .sequence_header() | ||
| 661 | - .is_some_and(|seq| seq.enable_filter_intra); | ||
| 662 | - let enable_dual_filter = parser | ||
| 663 | - .sequence_header() | ||
| 664 | - .is_some_and(|seq| seq.enable_dual_filter); | ||
| 665 | - let enable_edge_filter = parser | ||
| 666 | - .sequence_header() | ||
| 667 | - .is_some_and(|seq| seq.enable_intra_edge_filter); | ||
| 668 | // Inter frames' intra blocks read this through a thread-local that only | 871 | // Inter frames' intra blocks read this through a thread-local that only |
| 669 | // the key-frame tile path used to set; without this call a stream whose | 872 | // the key-frame tile path used to set; without this call a stream whose |
| 670 | // inter frames are decoded on a thread that previously decoded a | 873 | // inter frames are decoded on a thread that previously decoded a |
| @@ -677,9 +880,7 @@ pub fn decode_stream_with( | |||
| 677 | // most 64x64 decodes identically either way (its 128 root reads no | 880 | // most 64x64 decodes identically either way (its 128 root reads no |
| 678 | // symbol -- neither half is in frame, so SPLIT is inferred), so the | 881 | // symbol -- neither half is in frame, so SPLIT is inferred), so the |
| 679 | // flag is only armed for frames bigger than one 64x64 superblock. | 882 | // flag is only armed for frames bigger than one 64x64 superblock. |
| 680 | - let use_sb128 = parser | 883 | + let use_sb128 = seq.use_sb128; |
| 681 | - .sequence_header() | ||
| 682 | - .is_some_and(|seq| seq.use_128x128_superblock); | ||
| 683 | let sb128_active = use_sb128 && (header.mi_cols > 16 || header.mi_rows > 16); | 884 | let sb128_active = use_sb128 && (header.mi_cols > 16 || header.mi_rows > 16); |
| 684 | crate::decode::set_sb128(sb128_active, fctx); | 885 | crate::decode::set_sb128(sb128_active, fctx); |
| 685 | // lane-hbd r5: `BIT_DEPTH` (decode.rs) drives `crate::decode::sample_max`, | 886 | // lane-hbd r5: `BIT_DEPTH` (decode.rs) drives `crate::decode::sample_max`, |
| @@ -687,9 +888,7 @@ pub fn decode_stream_with( | |||
| 687 | // not once per process, for the same cross-sequence-on-one-thread | 888 | // not once per process, for the same cross-sequence-on-one-thread |
| 688 | // reason as `set_enable_edge_filter` above. Defaults to 8 when no | 889 | // reason as `set_enable_edge_filter` above. Defaults to 8 when no |
| 689 | // sequence header has been seen yet, matching every existing fixture. | 890 | // sequence header has been seen yet, matching every existing fixture. |
| 690 | - let bit_depth = parser | 891 | + let bit_depth = seq.bit_depth; |
| 691 | - .sequence_header() | ||
| 692 | - .map_or(8, |seq| seq.color_config.bit_depth); | ||
| 693 | // lane-bd12: the sequence-header parser accepts `twelve_bit`, but every | 892 | // lane-bd12: the sequence-header parser accepts `twelve_bit`, but every |
| 694 | // rounding shift below is written for 8/10-bit only -- `warp.rs`'s | 893 | // rounding shift below is written for 8/10-bit only -- `warp.rs`'s |
| 695 | // `REDUCE_BITS_HORIZ` is a hard 3 where libaom uses | 894 | // `REDUCE_BITS_HORIZ` is a hard 3 where libaom uses |
| @@ -713,17 +912,11 @@ pub fn decode_stream_with( | |||
| 713 | u32::from(header.superres_denom), fctx, | 912 | u32::from(header.superres_denom), fctx, |
| 714 | ); | 913 | ); |
| 715 | // lane-av1comp: `comp_group_idx`/`compound_idx`'s own gating bits. | 914 | // lane-av1comp: `comp_group_idx`/`compound_idx`'s own gating bits. |
| 716 | - let enable_masked_compound = parser | 915 | + let enable_masked_compound = seq.enable_masked_compound; |
| 717 | - .sequence_header() | 916 | + let enable_jnt_comp = seq.enable_jnt_comp; |
| 718 | - .is_some_and(|seq| seq.enable_masked_compound); | ||
| 719 | - let enable_jnt_comp = parser | ||
| 720 | - .sequence_header() | ||
| 721 | - .is_some_and(|seq| seq.enable_jnt_comp); | ||
| 722 | // lane-sb128 r4: `interintra`'s own gating bit -- see decode.rs's | 917 | // lane-sb128 r4: `interintra`'s own gating bit -- see decode.rs's |
| 723 | // read site doc. | 918 | // read site doc. |
| 724 | - let enable_interintra_compound = parser | 919 | + let enable_interintra_compound = seq.enable_interintra_compound; |
| 725 | - .sequence_header() | ||
| 726 | - .is_some_and(|seq| seq.enable_interintra_compound); | ||
| 727 | let interp_fixed = match header.interpolation_filter { | 920 | let interp_fixed = match header.interpolation_filter { |
| 728 | ec_av1_syntax::InterpolationFilter::Switchable => None, | 921 | ec_av1_syntax::InterpolationFilter::Switchable => None, |
| 729 | fixed => Some(crate::mc::InterpFilterKind::from_header(fixed)), | 922 | fixed => Some(crate::mc::InterpFilterKind::from_header(fixed)), |
| @@ -735,8 +928,7 @@ pub fn decode_stream_with( | |||
| 735 | let initial_cdfs = if header.primary_ref_frame == PRIMARY_REF_NONE { | 928 | let initial_cdfs = if header.primary_ref_frame == PRIMARY_REF_NONE { |
| 736 | None | 929 | None |
| 737 | } else { | 930 | } else { |
| 738 | - let slot = header.ref_frame_idx[header.primary_ref_frame as usize] as usize; | 931 | + Some(snap.primary_cdfs.clone().ok_or_else(|| { |
| 739 | - Some(cdf_slots[slot].clone().ok_or_else(|| { | ||
| 740 | Error::unsupported( | 932 | Error::unsupported( |
| 741 | "AV1 decode_stream", | 933 | "AV1 decode_stream", |
| 742 | "a frame naming primary_ref_frame at a reference slot with no saved CDF state", | 934 | "a frame naming primary_ref_frame at a reference slot with no saved CDF state", |
| @@ -745,13 +937,13 @@ pub fn decode_stream_with( | |||
| 745 | }; | 937 | }; |
| 746 | let started_from = initial_cdfs.clone(); | 938 | let started_from = initial_cdfs.clone(); |
| 747 | // lane-frame80: narrow the EC_TRACE_COEFF rungs to one DECODE-order frame. | 939 | // lane-frame80: narrow the EC_TRACE_COEFF rungs to one DECODE-order frame. |
| 748 | - decode::set_coeff_trace_frame(pictures_decoded); | 940 | + decode::set_coeff_trace_frame(decode_idx); |
| 749 | // lane-t900 r9: one line per DECODE-order frame naming the header | 941 | // lane-t900 r9: one line per DECODE-order frame naming the header |
| 750 | // fields a desync cliff is usually keyed on (frame type, base q, | 942 | // fields a desync cliff is usually keyed on (frame type, base q, |
| 751 | // primary_ref, the 7 ref slots, refresh mask, order hints). | 943 | // primary_ref, the 7 ref slots, refresh mask, order hints). |
| 752 | if std::env::var_os("EC_PROBE_HDR").is_some() { | 944 | if std::env::var_os("EC_PROBE_HDR").is_some() { |
| 753 | eprintln!( | 945 | eprintln!( |
| 754 | - "EC_HDR decode_idx={pictures_decoded} type={:?} show={} show_existing={} base_q={} primary_ref={} ref_idx={:?} refresh=0x{:02x} order_hint={} tiles={}x{} txmode={:?} interp={:?} reduced_tx={} allow_warp={} switchable_mo={} ref_mvs={} skip_mode={} cdef={:?} lr={:?}", | 946 | + "EC_HDR decode_idx={decode_idx} type={:?} show={} show_existing={} base_q={} primary_ref={} ref_idx={:?} refresh=0x{:02x} order_hint={} tiles={}x{} txmode={:?} interp={:?} reduced_tx={} allow_warp={} switchable_mo={} ref_mvs={} skip_mode={} cdef={:?} lr={:?}", |
| 755 | header.frame_type, | 947 | header.frame_type, |
| 756 | header.show_frame, | 948 | header.show_frame, |
| 757 | header.show_existing_frame, | 949 | header.show_existing_frame, |
| @@ -774,16 +966,13 @@ pub fn decode_stream_with( | |||
| 774 | ); | 966 | ); |
| 775 | } | 967 | } |
| 776 | 968 | ||
| 777 | - let order_hint_bits = parser | 969 | + let order_hint_bits = seq.order_hint_bits; |
| 778 | - .sequence_header() | ||
| 779 | - .map_or(0, |seq| seq.order_hint_bits); | ||
| 780 | // spec 7.9/7.20: this frame's own `ref_order_hints` -- the OrderHint | 970 | // spec 7.9/7.20: this frame's own `ref_order_hints` -- the OrderHint |
| 781 | // of the picture sitting in each of its 7 single references' DPB | 971 | // of the picture sitting in each of its 7 single references' DPB |
| 782 | // slots, for this frame's *own* saved [`crate::motion_field::MotionField`] | 972 | // slots, for this frame's *own* saved [`crate::motion_field::MotionField`] |
| 783 | // (a later frame projects from it) whether or not this frame itself | 973 | // (a later frame projects from it) whether or not this frame itself |
| 784 | // reads temporal candidates. | 974 | // reads temporal candidates. |
| 785 | - let ref_order_hints: [u32; 7] = | 975 | + let ref_order_hints = snap.ref_order_hints; |
| 786 | - std::array::from_fn(|i| order_hint_slots[header.ref_frame_idx[i] as usize]); | ||
| 787 | 976 | ||
| 788 | let (picture, end_cdfs, motion_field) = if header.frame_type == FrameType::Key { | 977 | let (picture, end_cdfs, motion_field) = if header.frame_type == FrameType::Key { |
| 789 | let (picture, end_cdfs) = decode_key_frame_tile_with_cdfs( | 978 | let (picture, end_cdfs) = decode_key_frame_tile_with_cdfs( |
| @@ -830,7 +1019,7 @@ pub fn decode_stream_with( | |||
| 830 | (picture, end_cdfs, motion_field) | 1019 | (picture, end_cdfs, motion_field) |
| 831 | } else { | 1020 | } else { |
| 832 | let last_slot = header.ref_frame_idx[0] as usize; | 1021 | let last_slot = header.ref_frame_idx[0] as usize; |
| 833 | - let reference = ref_slots[last_slot].as_ref().ok_or_else(|| { | 1022 | + let reference = snap.refs[last_slot].ok_or_else(|| { |
| 834 | Error::unsupported( | 1023 | Error::unsupported( |
| 835 | "AV1 decode_stream", | 1024 | "AV1 decode_stream", |
| 836 | "an inter frame with no key frame before it", | 1025 | "an inter frame with no key frame before it", |
| @@ -852,33 +1041,17 @@ pub fn decode_stream_with( | |||
| 852 | if ref_frame == 0 { | 1041 | if ref_frame == 0 { |
| 853 | None | 1042 | None |
| 854 | } else { | 1043 | } else { |
| 855 | - ref_slots | 1044 | + snap.refs |
| 856 | .get(header.ref_frame_idx[ref_frame - 1] as usize) | 1045 | .get(header.ref_frame_idx[ref_frame - 1] as usize) |
| 857 | - .and_then(Option::as_ref) | 1046 | + .copied() |
| 1047 | + .flatten() | ||
| 858 | } | 1048 | } |
| 859 | }); | 1049 | }); |
| 860 | // spec 7.9's own driver, `av1_setup_motion_field`: only run when | 1050 | // spec 7.9's own driver, `av1_setup_motion_field`: only run when |
| 861 | // this frame's header actually asks for temporal candidates -- | 1051 | // this frame's header actually asks for temporal candidates -- |
| 862 | // `find_mv_stack_with_sign_bias` reproduces its old always-`None` | 1052 | // `find_mv_stack_with_sign_bias` reproduces its old always-`None` |
| 863 | // behaviour bit for bit when this is `None` (see its own doc). | 1053 | // behaviour bit for bit when this is `None` (see its own doc). |
| 864 | - let tpl_field = header.use_ref_frame_mvs.then(|| { | 1054 | + let tpl_field = &snap.tpl_field; |
| 865 | - crate::motion_field::setup_motion_field( | ||
| 866 | - &motion_field_slots, | ||
| 867 | - header.ref_frame_idx, | ||
| 868 | - header.order_hint, | ||
| 869 | - order_hint_bits, | ||
| 870 | - header.mi_rows as usize, | ||
| 871 | - header.mi_cols as usize, | ||
| 872 | - ) | ||
| 873 | - }); | ||
| 874 | - if std::env::var_os("EC_TPL").is_some() { | ||
| 875 | - eprintln!( | ||
| 876 | - "EC_TPL order_hint={} use_ref_frame_mvs={} cells={}", | ||
| 877 | - header.order_hint, | ||
| 878 | - header.use_ref_frame_mvs, | ||
| 879 | - tpl_field.as_ref().map_or(0, crate::motion_field::TplField::filled_cells) | ||
| 880 | - ); | ||
| 881 | - } | ||
| 882 | let (picture, end_cdfs, motion_field) = decode_inter_frame_tile_with_cdfs( | 1055 | let (picture, end_cdfs, motion_field) = decode_inter_frame_tile_with_cdfs( |
| 883 | &tile_bufs, | 1056 | &tile_bufs, |
| 884 | &header.tile_info, | 1057 | &header.tile_info, |
| @@ -949,7 +1122,7 @@ pub fn decode_stream_with( | |||
| 949 | .append(true) | 1122 | .append(true) |
| 950 | .open(&path) | 1123 | .open(&path) |
| 951 | { | 1124 | { |
| 952 | - let _ = writeln!(f, "=== frame idx={} ===", pictures_shown); | 1125 | + let _ = writeln!(f, "=== frame idx={} ===", shown_idx); |
| 953 | let _ = writeln!(f, "partition_w64 {:?}", stored_cdfs.partition_w64); | 1126 | let _ = writeln!(f, "partition_w64 {:?}", stored_cdfs.partition_w64); |
| 954 | let _ = writeln!(f, "partition_w32 {:?}", stored_cdfs.partition_w32); | 1127 | let _ = writeln!(f, "partition_w32 {:?}", stored_cdfs.partition_w32); |
| 955 | let _ = writeln!(f, "partition_w16 {:?}", stored_cdfs.partition_w16); | 1128 | let _ = writeln!(f, "partition_w16 {:?}", stored_cdfs.partition_w16); |
| @@ -970,7 +1143,7 @@ pub fn decode_stream_with( | |||
| 970 | // reconstruction or downstream of it. | 1143 | // reconstruction or downstream of it. |
| 971 | if let Ok(path) = std::env::var("EC_AV1_DECODE_ORDER_DUMP") { | 1144 | if let Ok(path) = std::env::var("EC_AV1_DECODE_ORDER_DUMP") { |
| 972 | use std::io::Write; | 1145 | use std::io::Write; |
| 973 | - let idx = pictures_decoded; | 1146 | + let idx = decode_idx; |
| 974 | if let Ok(mut f) = std::fs::File::create(format!("{path}.f{idx}")) { | 1147 | if let Ok(mut f) = std::fs::File::create(format!("{path}.f{idx}")) { |
| 975 | // lane-hbd r4: debug dump narrows to u8 -- this diagnostic | 1148 | // lane-hbd r4: debug dump narrows to u8 -- this diagnostic |
| 976 | // predates 10-bit support and is an 8-bit-oracle comparison | 1149 | // predates 10-bit support and is an 8-bit-oracle comparison |
| @@ -989,9 +1162,9 @@ pub fn decode_stream_with( | |||
| 989 | // aomdec's own `EC_AV1_FINAL_DUMP` rung byte for byte | 1162 | // aomdec's own `EC_AV1_FINAL_DUMP` rung byte for byte |
| 990 | // (scripts/instrument-aom-oracle.sh rung 12), which is what makes a | 1163 | // (scripts/instrument-aom-oracle.sh rung 12), which is what makes a |
| 991 | // hidden frame's pixels comparable at all. | 1164 | // hidden frame's pixels comparable at all. |
| 992 | - if let Some(prefix) = &final_dump { | 1165 | + if let Some(prefix) = final_dump { |
| 993 | use std::io::Write; | 1166 | use std::io::Write; |
| 994 | - if let Ok(mut f) = std::fs::File::create(format!("{prefix}.f{pictures_decoded}")) { | 1167 | + if let Ok(mut f) = std::fs::File::create(format!("{prefix}.f{decode_idx}")) { |
| 995 | let mut buf = Vec::new(); | 1168 | let mut buf = Vec::new(); |
| 996 | for plane in [&picture.y, &picture.u, &picture.v] { | 1169 | for plane in [&picture.y, &picture.u, &picture.v] { |
| 997 | if bit_depth == 8 { | 1170 | if bit_depth == 8 { |
| @@ -1005,56 +1178,14 @@ pub fn decode_stream_with( | |||
| 1005 | let _ = f.write_all(&buf); | 1178 | let _ = f.write_all(&buf); |
| 1006 | } | 1179 | } |
| 1007 | } | 1180 | } |
| 1008 | - pictures_decoded += 1; | 1181 | + Ok(FrameOutput { |
| 1009 | - let decoded_seg_map = decode::take_segment_ids(fctx); | 1182 | + picture, |
| 1010 | - for i in 0..NUM_REF_FRAMES { | 1183 | + stored_cdfs, |
| 1011 | - if header.refresh_frame_flags & (1 << i) != 0 { | 1184 | + motion_field, |
| 1012 | - cdf_slots[i] = Some(stored_cdfs.clone()); | 1185 | + seg_map: decode::take_segment_ids(fctx), |
| 1013 | - // Spec 7.18.3.1: synthesized grain is never stored for later | 1186 | + }) |
| 1014 | - // prediction -- the reference bank always keeps the clean, | ||
| 1015 | - // pre-grain decode, even though the frame pushed onto the | ||
| 1016 | - // caller's output below carries the grained picture. | ||
| 1017 | - ref_slots[i] = Some(picture.clone()); | ||
| 1018 | - motion_field_slots[i] = Some(motion_field.clone()); | ||
| 1019 | - seg_map_slots[i] = Some(decoded_seg_map.clone()); | ||
| 1020 | - order_hint_slots[i] = header.order_hint; | ||
| 1021 | - } | ||
| 1022 | - } | ||
| 1023 | - // lane-defon r1: a forward keyframe (aomenc `--enable-fwd-kf`) is a | ||
| 1024 | - // KEY frame coded with `show_frame == 0`, output later by a | ||
| 1025 | - // `show_existing_frame` header -- counted only once its tiles have | ||
| 1026 | - // decoded, so the gate's assert covers decoded-and-compared frames. | ||
| 1027 | - if header.frame_type == FrameType::Key && !header.show_frame { | ||
| 1028 | - decode::note_fwd_kf(); | ||
| 1029 | - } | ||
| 1030 | - if header.show_frame { | ||
| 1031 | - let output = if header.film_grain.apply_grain && !no_grain() { | ||
| 1032 | - let mc_identity = parser | ||
| 1033 | - .sequence_header() | ||
| 1034 | - .is_some_and(|seq| seq.color_config.matrix_coefficients == 0); | ||
| 1035 | - crate::film_grain::apply_grain( | ||
| 1036 | - &picture, | ||
| 1037 | - &header.film_grain, | ||
| 1038 | - mc_identity, | ||
| 1039 | - bit_depth as u32, fctx, | ||
| 1040 | - ) | ||
| 1041 | - } else { | ||
| 1042 | - picture | ||
| 1043 | - }; | ||
| 1044 | - sink(&output, pictures_decoded - 1, true)?; | ||
| 1045 | - pictures_shown += 1; | ||
| 1046 | - } else { | ||
| 1047 | - // Hidden (alt-ref/forward-key) frames never reach | ||
| 1048 | - // `decode_stream`'s output, but a decode-order consumer -- the | ||
| 1049 | - // `EC_AV1_FINAL_DUMP` comparison against aomdec, say -- needs | ||
| 1050 | - // them, so they are reported too, with `shown == false`. | ||
| 1051 | - sink(&picture, pictures_decoded - 1, false)?; | ||
| 1052 | - } | ||
| 1053 | - } | ||
| 1054 | - Ok(()) | ||
| 1055 | } | 1187 | } |
| 1056 | 1188 | ||
| 1057 | - | ||
| 1058 | /// lane-intra14 r1: INTRA-coded 1:4 strips decoded on the INTER block path | 1189 | /// lane-intra14 r1: INTRA-coded 1:4 strips decoded on the INTER block path |
| 1059 | /// (64x16, 16x64, 32x8, 8x32) -- the counters a recipe sweep needs from | 1190 | /// (64x16, 16x64, 32x8, 8x32) -- the counters a recipe sweep needs from |
| 1060 | /// outside the crate. | 1191 | /// outside the crate. |