The reference bank and the output queue share the picture
Browse files at this commit900cf2dae324a6041f8bee0c03d6a2c38a47f44bParent: a02c7bd- perf
- the reference bank and the output queue share the picture
1 changed file +27 -17
- crates/ec-av1/src/stream.rs +27 -17
crates/ec-av1/src/stream.rsmodified+27 -17
| @@ -296,11 +296,15 @@ fn decode_threads() -> usize { | ||
| 296 | 296 | #[derive(Default)] |
| 297 | 297 | struct Reorder { |
| 298 | 298 | next: usize, |
| 299 | - pending: std::collections::BTreeMap<usize, (Picture, usize, bool)>, | |
| 299 | + // lane-pool1: the picture is shared, not owned -- a decoded frame that | |
| 300 | + // refreshes a reference slot is already behind an `Arc` for the slot, and | |
| 301 | + // handing the output an owned `Picture` meant copying every plane of it | |
| 302 | + // (24 MB per 4K 10-bit frame) purely to put it in this queue. | |
| 303 | + pending: std::collections::BTreeMap<usize, (std::sync::Arc<Picture>, usize, bool)>, | |
| 300 | 304 | } |
| 301 | 305 | |
| 302 | 306 | impl Reorder { |
| 303 | - fn push(&mut self, seq: usize, picture: Picture, decode_idx: usize, shown: bool) { | |
| 307 | + fn push(&mut self, seq: usize, picture: std::sync::Arc<Picture>, decode_idx: usize, shown: bool) { | |
| 304 | 308 | self.pending.insert(seq, (picture, decode_idx, shown)); |
| 305 | 309 | } |
| 306 | 310 | |
| @@ -431,7 +435,7 @@ impl SlotPromise { | ||
| 431 | 435 | /// against separate in-flight bounds, since a waiting output job costs no |
| 432 | 436 | /// CPU), and either the (grain-synthesised) picture with the decode index to |
| 433 | 437 | /// report or the error that aborts the stream. |
| 434 | -type LeafResult = (usize, bool, Result<(Picture, usize, bool)>); | |
| 438 | +type LeafResult = (usize, bool, Result<(std::sync::Arc<Picture>, usize, bool)>); | |
| 435 | 439 | |
| 436 | 440 | /// Waits for one in-flight job and emits whatever that unblocks. |
| 437 | 441 | fn collect_one( |
| @@ -503,7 +507,10 @@ pub fn decode_stream_with_threads( | ||
| 503 | 507 | // luma-only (chroma coincidentally shares this fixture's flat regions) |
| 504 | 508 | // and small-magnitude (the two pictures mostly agree), which is exactly |
| 505 | 509 | // what made it look like a rounding bug rather than a wrong reference. |
| 506 | - let mut ref_slots: [Option<Picture>; NUM_REF_FRAMES] = std::array::from_fn(|_| None); | |
| 510 | + // lane-pool1: shared, not owned -- a frame refreshing k slots used to copy | |
| 511 | + // its whole picture k times, and its output a (k+1)-th time. | |
| 512 | + let mut ref_slots: [Option<std::sync::Arc<Picture>>; NUM_REF_FRAMES] = | |
| 513 | + std::array::from_fn(|_| None); | |
| 507 | 514 | // lane-av1tmvp: each of the 8 reference slots' own saved temporal motion |
| 508 | 515 | // field (spec 7.9's per-frame `MotionFieldMvs` storage, libaom |
| 509 | 516 | // `cur_frame->mvs`) plus the `OrderHint` the picture in that slot was |
| @@ -626,15 +633,15 @@ pub fn decode_stream_with_threads( | ||
| 626 | 633 | let reply = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { |
| 627 | 634 | let value = promise.wait()?; |
| 628 | 635 | let output = if grain.apply_grain && !no_grain() { |
| 629 | - crate::film_grain::apply_grain( | |
| 636 | + std::sync::Arc::new(crate::film_grain::apply_grain( | |
| 630 | 637 | &value.picture, |
| 631 | 638 | &grain, |
| 632 | 639 | seq.mc_identity, |
| 633 | 640 | u32::from(seq.bit_depth), |
| 634 | 641 | &crate::decode::FrameCtx::new(), |
| 635 | - ) | |
| 642 | + )) | |
| 636 | 643 | } else { |
| 637 | - value.picture.as_ref().clone() | |
| 644 | + std::sync::Arc::clone(&value.picture) | |
| 638 | 645 | }; |
| 639 | 646 | Ok((output, decode_idx, true)) |
| 640 | 647 | })) |
| @@ -680,12 +687,12 @@ pub fn decode_stream_with_threads( | ||
| 680 | 687 | let mc_identity = parser |
| 681 | 688 | .sequence_header() |
| 682 | 689 | .is_some_and(|seq| seq.color_config.matrix_coefficients == 0); |
| 683 | - crate::film_grain::apply_grain( | |
| 690 | + std::sync::Arc::new(crate::film_grain::apply_grain( | |
| 684 | 691 | &picture, |
| 685 | 692 | &header.film_grain, |
| 686 | 693 | mc_identity, |
| 687 | 694 | bit_depth as u32, fctx, |
| 688 | - ) | |
| 695 | + )) | |
| 689 | 696 | } else { |
| 690 | 697 | picture |
| 691 | 698 | }; |
| @@ -901,15 +908,15 @@ pub fn decode_stream_with_threads( | ||
| 901 | 908 | // reported to the sink clean, exactly as the inline path |
| 902 | 909 | // below reports it (spec 7.18.3.1). |
| 903 | 910 | let output = if shown && header.film_grain.apply_grain && !no_grain() { |
| 904 | - crate::film_grain::apply_grain( | |
| 911 | + std::sync::Arc::new(crate::film_grain::apply_grain( | |
| 905 | 912 | &picture, |
| 906 | 913 | &header.film_grain, |
| 907 | 914 | seq.mc_identity, |
| 908 | 915 | u32::from(seq.bit_depth), |
| 909 | 916 | &fctx, |
| 910 | - ) | |
| 917 | + )) | |
| 911 | 918 | } else { |
| 912 | - std::sync::Arc::try_unwrap(picture).unwrap_or_else(|p| (*p).clone()) | |
| 919 | + picture | |
| 913 | 920 | }; |
| 914 | 921 | Ok((output, decode_idx, shown)) |
| 915 | 922 | })) |
| @@ -945,7 +952,7 @@ pub fn decode_stream_with_threads( | ||
| 945 | 952 | let snap = RefSnapshot::take( |
| 946 | 953 | &header, |
| 947 | 954 | seq, |
| 948 | - std::array::from_fn(|i| ref_slots[i].as_ref()), | |
| 955 | + std::array::from_fn(|i| ref_slots[i].as_deref()), | |
| 949 | 956 | std::array::from_fn(|i| cdf_slots[i].as_ref()), |
| 950 | 957 | std::array::from_fn(|i| seg_map_slots[i].as_deref()), |
| 951 | 958 | &std::array::from_fn(|i| motion_field_slots[i].as_ref()), |
| @@ -968,13 +975,16 @@ pub fn decode_stream_with_threads( | ||
| 968 | 975 | final_dump.as_deref(), |
| 969 | 976 | )?; |
| 970 | 977 | pictures_decoded += 1; |
| 978 | + // lane-pool1: one allocation, then every slot store and the output | |
| 979 | + // itself are refcount bumps instead of plane copies. | |
| 980 | + let picture = std::sync::Arc::new(picture); | |
| 971 | 981 | if threads > 1 { |
| 972 | 982 | // lane-thread3: only a key frame reaches this inline path at |
| 973 | 983 | // `threads > 1`, and the bank it fills is the promise array -- |
| 974 | 984 | // already resolved, since the picture exists right here. |
| 975 | 985 | if header.refresh_frame_flags != 0 { |
| 976 | 986 | let promise = SlotPromise::ready(std::sync::Arc::new(SlotValue { |
| 977 | - picture: std::sync::Arc::new(picture.clone()), | |
| 987 | + picture: std::sync::Arc::clone(&picture), | |
| 978 | 988 | cdfs: stored_cdfs, |
| 979 | 989 | motion_field, |
| 980 | 990 | seg_map: decoded_seg_map, |
| @@ -995,7 +1005,7 @@ pub fn decode_stream_with_threads( | ||
| 995 | 1005 | // clean, pre-grain decode, even though the frame pushed |
| 996 | 1006 | // onto the caller's output below carries the grained |
| 997 | 1007 | // picture. |
| 998 | - ref_slots[i] = Some(picture.clone()); | |
| 1008 | + ref_slots[i] = Some(std::sync::Arc::clone(&picture)); | |
| 999 | 1009 | motion_field_slots[i] = Some(motion_field.clone()); |
| 1000 | 1010 | seg_map_slots[i] = Some(decoded_seg_map.clone()); |
| 1001 | 1011 | order_hint_slots[i] = header.order_hint; |
| @@ -1012,12 +1022,12 @@ pub fn decode_stream_with_threads( | ||
| 1012 | 1022 | if header.show_frame { |
| 1013 | 1023 | let output = if header.film_grain.apply_grain && !no_grain() { |
| 1014 | 1024 | let mc_identity = seq.mc_identity; |
| 1015 | - crate::film_grain::apply_grain( | |
| 1025 | + std::sync::Arc::new(crate::film_grain::apply_grain( | |
| 1016 | 1026 | &picture, |
| 1017 | 1027 | &header.film_grain, |
| 1018 | 1028 | mc_identity, |
| 1019 | 1029 | bit_depth as u32, fctx, |
| 1020 | - ) | |
| 1030 | + )) | |
| 1021 | 1031 | } else { |
| 1022 | 1032 | picture |
| 1023 | 1033 | }; |