Merge branch 'lane-av1facade' (02529d9e): the streaming facade holds one picture so its lookahead matches encode_sequence; facade bytes proven identical to the gate path
Browse files at this commit2 changed files +192 -80
- crates/ec-av1/src/encode.rs +26 -15
- crates/ec-av1/src/encoder.rs +166 -65
crates/ec-av1/src/encode.rsmodified+26 -15
| @@ -11758,18 +11758,21 @@ mod tests { | |||
| 11758 | }) | 11758 | }) |
| 11759 | } | 11759 | } |
| 11760 | 11760 | ||
| 11761 | - /// [`our_ladder`] through the streaming facade with a coding pyramid: the | 11761 | + /// [`our_ladder`] through the streaming facade — the surface the editor's |
| 11762 | - /// four points are the same `base_q_idx` ladder, but each stream is | 11762 | + /// export drives (`EC_AV1_GATE_FACADE=1`). With a [`crate::encoder::Pyramid`] |
| 11763 | - /// reordered (hidden `ALTREF` first, `show_existing_frame` last per | 11763 | + /// each stream is also reordered (hidden `ALTREF` first, |
| 11764 | - /// group) and each level is coded at its own quantizer offset. Returns | 11764 | + /// `show_existing_frame` last per group) and each level is coded at its |
| 11765 | - /// the ladder, the wall, and — per pyramid level over the whole ladder — | 11765 | + /// own quantizer offset; without one it is the flat ladder, which |
| 11766 | - /// how many frames and how many bytes it spent. | 11766 | + /// `encoder::tests::the_facade_codes_the_same_bytes_as_encode_sequence` |
| 11767 | - fn our_ladder_pyramid( | 11767 | + /// pins byte-identical to [`our_ladder`]'s, so this arm must print the |
| 11768 | + /// same BD numbers. Returns the ladder, the wall, and — per pyramid level | ||
| 11769 | + /// over the whole ladder — how many frames and how many bytes it spent. | ||
| 11770 | + fn our_ladder_facade( | ||
| 11768 | name: &str, | 11771 | name: &str, |
| 11769 | source: &[Picture], | 11772 | source: &[Picture], |
| 11770 | width: usize, | 11773 | width: usize, |
| 11771 | height: usize, | 11774 | height: usize, |
| 11772 | - pyramid: crate::encoder::Pyramid, | 11775 | + pyramid: Option<crate::encoder::Pyramid>, |
| 11773 | ) -> (Vec<(f64, f64)>, f64, [usize; 4], [usize; 4]) { | 11776 | ) -> (Vec<(f64, f64)>, f64, [usize; 4], [usize; 4]) { |
| 11774 | use crate::encoder::{Av1Encoder, Colour, EncoderConfig, Level}; | 11777 | use crate::encoder::{Av1Encoder, Colour, EncoderConfig, Level}; |
| 11775 | let slot = |l: Level| match l { | 11778 | let slot = |l: Level| match l { |
| @@ -11792,7 +11795,10 @@ mod tests { | |||
| 11792 | tile_rows_log2: 0, | 11795 | tile_rows_log2: 0, |
| 11793 | }; | 11796 | }; |
| 11794 | let start = std::time::Instant::now(); | 11797 | let start = std::time::Instant::now(); |
| 11795 | - let mut enc = Av1Encoder::with_pyramid(config, pyramid).unwrap(); | 11798 | + let mut enc = match pyramid { |
| 11799 | + None => Av1Encoder::new(config).unwrap(), | ||
| 11800 | + Some(p) => Av1Encoder::with_pyramid(config, p).unwrap(), | ||
| 11801 | + }; | ||
| 11796 | let mut packets = Vec::new(); | 11802 | let mut packets = Vec::new(); |
| 11797 | for picture in source { | 11803 | for picture in source { |
| 11798 | packets.extend(enc.encode_frames(picture).unwrap()); | 11804 | packets.extend(enc.encode_frames(picture).unwrap()); |
| @@ -12121,13 +12127,18 @@ mod tests { | |||
| 12121 | let _ = crate::tile::take_compound_size_hits(); | 12127 | let _ = crate::tile::take_compound_size_hits(); |
| 12122 | let _ = crate::tile::take_compound_leaf_mode_hits(); | 12128 | let _ = crate::tile::take_compound_leaf_mode_hits(); |
| 12123 | let _ = crate::motion::take_census(); | 12129 | let _ = crate::motion::take_census(); |
| 12124 | - let (ours, ours_wall) = match pyramid_from_env() { | 12130 | + let pyramid_env = pyramid_from_env(); |
| 12125 | - None => our_ladder(name, &source, width, height, fctx), | 12131 | + // The entry-surface arm: run the SAME ladder through the |
| 12126 | - Some(pyramid) => { | 12132 | + // streaming facade the editor's export calls, which must print |
| 12133 | + // the same BD numbers as the sequence path. | ||
| 12134 | + let facade = std::env::var_os("EC_AV1_GATE_FACADE").is_some(); | ||
| 12135 | + let (ours, ours_wall) = match (facade, pyramid_env) { | ||
| 12136 | + (false, None) => our_ladder(name, &source, width, height, fctx), | ||
| 12137 | + (_, pyramid) => { | ||
| 12127 | let (ladder, wall, counts, bytes) = | 12138 | let (ladder, wall, counts, bytes) = |
| 12128 | - our_ladder_pyramid(name, &source, width, height, pyramid); | 12139 | + our_ladder_facade(name, &source, width, height, pyramid); |
| 12129 | eprintln!( | 12140 | eprintln!( |
| 12130 | - "{name}: pyramid {pyramid:?} -- frames key {} arf {} leaf {} \ | 12141 | + "{name}: facade, pyramid {pyramid:?} -- frames key {} arf {} leaf {} \ |
| 12131 | show_existing {}; bytes key {} arf {} leaf {} show_existing {}", | 12142 | show_existing {}; bytes key {} arf {} leaf {} show_existing {}", |
| 12132 | counts[0], counts[1], counts[2], counts[3], | 12143 | counts[0], counts[1], counts[2], counts[3], |
| 12133 | bytes[0], bytes[1], bytes[2], bytes[3], | 12144 | bytes[0], bytes[1], bytes[2], bytes[3], |
| @@ -12555,7 +12566,7 @@ mod tests { | |||
| 12555 | None => our_ladder(name, &source, cw, ch, fctx), | 12566 | None => our_ladder(name, &source, cw, ch, fctx), |
| 12556 | Some(pyramid) => { | 12567 | Some(pyramid) => { |
| 12557 | let (ladder, wall, counts, bytes) = | 12568 | let (ladder, wall, counts, bytes) = |
| 12558 | - our_ladder_pyramid(name, &source, cw, ch, pyramid); | 12569 | + our_ladder_facade(name, &source, cw, ch, Some(pyramid)); |
| 12559 | eprintln!( | 12570 | eprintln!( |
| 12560 | "{name}: pyramid {pyramid:?} -- frames key {} arf {} leaf {} \ | 12571 | "{name}: pyramid {pyramid:?} -- frames key {} arf {} leaf {} \ |
| 12561 | show_existing {}; bytes key {} arf {} leaf {} show_existing {}", | 12572 | show_existing {}; bytes key {} arf {} leaf {} show_existing {}", |
crates/ec-av1/src/encoder.rsmodified+166 -65
| @@ -1,14 +1,16 @@ | |||
| 1 | //! The streaming encoder facade — I11 of `lanes/av1-inter-plan.md`, the | 1 | //! The streaming encoder facade — I11 of `lanes/av1-inter-plan.md`, the |
| 2 | //! surface `edith_replica`'s engine calls in place of rav1e. | 2 | //! surface `edith_replica`'s engine calls in place of rav1e. |
| 3 | //! | 3 | //! |
| 4 | -//! One picture in, one [`Packet`] out, same contract as the repo's other | 4 | +//! ONE FRAME OF LATENCY, everywhere: a picture handed to |
| 5 | -//! software encoder shims (`shims/rusty_h264`'s `Encoder::try_encode`, | 5 | +//! [`Av1Encoder::encode`]/[`Av1Encoder::encode_frames`] is held until the |
| 6 | -//! "nothing is ever held back"): [`Av1Encoder::encode`] never buffers a | 6 | +//! NEXT picture arrives, because an inter frame's per-superblock temporal |
| 7 | -//! picture past its own call, so there is no flush to drain. That contract | 7 | +//! lambda map reads the next source picture as its lookahead (lane-av1tpl) |
| 8 | -//! still holds for every caller that does not opt into a coding pyramid; | 8 | +//! — exactly as [`crate::encode::encode_sequence`] does, so the facade and |
| 9 | -//! [`Av1Encoder::with_pyramid`] does reorder pictures, and such a stream is | 9 | +//! the sequence path now code the same bytes for the same pictures. A |
| 10 | -//! driven through [`Av1Encoder::encode_frames`] (zero or more packets per | 10 | +//! stream therefore ENDS with [`Av1Encoder::flush`], which codes whatever is |
| 11 | -//! picture) and [`Av1Encoder::flush`] instead. Internally it | 11 | +//! still held. [`Av1Encoder::with_pyramid`] holds a whole mini-GOP for the |
| 12 | +//! same kind of reason (it reorders pictures) and is driven through | ||
| 13 | +//! [`Av1Encoder::encode_frames`]/[`Av1Encoder::flush`] too. Internally it | ||
| 12 | //! is [`crate::encode::encode_key_frame_inner`] and | 14 | //! is [`crate::encode::encode_key_frame_inner`] and |
| 13 | //! [`crate::encode::encode_inter_frame`] driven by a small state machine — | 15 | //! [`crate::encode::encode_inter_frame`] driven by a small state machine — |
| 14 | //! a key frame every `gop` pictures, an inter frame predicting from the | 16 | //! a key frame every `gop` pictures, an inter frame predicting from the |
| @@ -534,6 +536,10 @@ pub struct Av1Encoder { | |||
| 534 | dpb: [Option<DpbSlot>; 8], | 536 | dpb: [Option<DpbSlot>; 8], |
| 535 | /// Pictures held back waiting for their mini-GOP's hidden frame. | 537 | /// Pictures held back waiting for their mini-GOP's hidden frame. |
| 536 | pending: Vec<(u64, Picture)>, | 538 | pending: Vec<(u64, Picture)>, |
| 539 | + /// The flat path's one-picture delay queue: the picture whose packet the | ||
| 540 | + /// NEXT call returns, coded with that call's picture as its lookahead | ||
| 541 | + /// (lane-av1facade). Unused in pyramid mode, which buffers in `pending`. | ||
| 542 | + held: Option<Picture>, | ||
| 537 | /// Which of [`ANCHOR_SLOTS`] the group about to be coded reads as its | 543 | /// Which of [`ANCHOR_SLOTS`] the group about to be coded reads as its |
| 538 | /// anchor. | 544 | /// anchor. |
| 539 | anchor: usize, | 545 | anchor: usize, |
| @@ -601,6 +607,7 @@ impl Av1Encoder { | |||
| 601 | pyramid: None, | 607 | pyramid: None, |
| 602 | dpb: [const { None }; 8], | 608 | dpb: [const { None }; 8], |
| 603 | pending: Vec::new(), | 609 | pending: Vec::new(), |
| 610 | + held: None, | ||
| 604 | anchor: 0, | 611 | anchor: 0, |
| 605 | next_dts: 0, | 612 | next_dts: 0, |
| 606 | }) | 613 | }) |
| @@ -678,9 +685,15 @@ impl Av1Encoder { | |||
| 678 | (self.config.width, self.config.height) | 685 | (self.config.width, self.config.height) |
| 679 | } | 686 | } |
| 680 | 687 | ||
| 681 | - /// Encodes one picture, key or inter by this stream's `gop` cadence, and | 688 | + /// Takes one picture and returns the PREVIOUS picture's packet, key or |
| 682 | - /// returns its packet — always exactly one, never held back for a later | 689 | + /// inter by this stream's `gop` cadence — the facade's one frame of |
| 683 | - /// call. | 690 | + /// latency (the picture just handed in is what the previous one is coded |
| 691 | + /// against as its lookahead). The FIRST call has no previous picture and | ||
| 692 | + /// returns a packet with an EMPTY `data` (its other fields are | ||
| 693 | + /// placeholders; test `data.is_empty()`), and the last picture's packet | ||
| 694 | + /// comes out of [`Av1Encoder::flush`] — so a caller that concatenates | ||
| 695 | + /// `data` in call order still gets the whole stream, provided it flushes | ||
| 696 | + /// at the end. | ||
| 684 | /// | 697 | /// |
| 685 | /// # Errors | 698 | /// # Errors |
| 686 | /// Returns an error when `picture`'s size does not match | 699 | /// Returns an error when `picture`'s size does not match |
| @@ -697,12 +710,19 @@ impl Av1Encoder { | |||
| 697 | )); | 710 | )); |
| 698 | } | 711 | } |
| 699 | let mut packets = self.encode_frames(picture)?; | 712 | let mut packets = self.encode_frames(picture)?; |
| 700 | - Ok(packets.pop().expect("the flat path emits exactly one packet")) | 713 | + Ok(packets.pop().unwrap_or(Packet { |
| 701 | - } | 714 | + data: Vec::new(), |
| 702 | - | 715 | + key: false, |
| 703 | - /// Encodes one picture and returns every packet it completed, in coding | 716 | + order: 0, |
| 704 | - /// order. Without a [`Pyramid`] that is always exactly one packet and | 717 | + dts: 0, |
| 705 | - /// this is [`Av1Encoder::encode`] with a `Vec` around it; with one it is | 718 | + level: Level::Leaf, |
| 719 | + })) | ||
| 720 | + } | ||
| 721 | + | ||
| 722 | + /// Takes one picture and returns every packet it completed, in coding | ||
| 723 | + /// order. Without a [`Pyramid`] that is one packet — the PREVIOUS | ||
| 724 | + /// picture's, since this one is its lookahead — and none at all on the | ||
| 725 | + /// first call; with one it is | ||
| 706 | /// empty for most pictures and a whole mini-GOP's worth (the hidden | 726 | /// empty for most pictures and a whole mini-GOP's worth (the hidden |
| 707 | /// frame, its leaves and the `show_existing_frame` that re-outputs it) | 727 | /// frame, its leaves and the `show_existing_frame` that re-outputs it) |
| 708 | /// for the picture that closes a group. | 728 | /// for the picture that closes a group. |
| @@ -721,7 +741,13 @@ impl Av1Encoder { | |||
| 721 | )); | 741 | )); |
| 722 | } | 742 | } |
| 723 | let Some(pyramid) = self.pyramid else { | 743 | let Some(pyramid) = self.pyramid else { |
| 724 | - return Ok(vec![self.encode_flat(picture)?]); | 744 | + // The one-picture delay queue: this picture is the lookahead the |
| 745 | + // previous one is coded with, which is what makes the facade's | ||
| 746 | + // bytes identical to `encode_sequence`'s (lane-av1facade). | ||
| 747 | + return Ok(match self.held.replace(picture.clone()) { | ||
| 748 | + None => Vec::new(), | ||
| 749 | + Some(previous) => vec![self.encode_flat(&previous, Some(picture))?], | ||
| 750 | + }); | ||
| 725 | }; | 751 | }; |
| 726 | let order = self.next_index; | 752 | let order = self.next_index; |
| 727 | self.next_index += 1; | 753 | self.next_index += 1; |
| @@ -743,19 +769,26 @@ impl Av1Encoder { | |||
| 743 | Ok(Vec::new()) | 769 | Ok(Vec::new()) |
| 744 | } | 770 | } |
| 745 | 771 | ||
| 746 | - /// Codes and returns every picture still held back — the tail of a stream | 772 | + /// Codes and returns every picture still held back — the last picture of |
| 747 | - /// whose last mini-GOP never filled up. Empty (and cheap) for a stream | 773 | + /// a flat stream (which has no lookahead and is coded without one), or |
| 748 | - /// with no [`Pyramid`], which never holds a picture at all. | 774 | + /// the tail of a pyramid stream whose last mini-GOP never filled up. |
| 775 | + /// Empty once there is nothing held. | ||
| 749 | /// | 776 | /// |
| 750 | /// # Errors | 777 | /// # Errors |
| 751 | /// As [`Av1Encoder::encode_frames`]. | 778 | /// As [`Av1Encoder::encode_frames`]. |
| 752 | pub fn flush(&mut self) -> Result<Vec<Packet>> { | 779 | pub fn flush(&mut self) -> Result<Vec<Packet>> { |
| 753 | crate::encode::arm_tiles(self.config.tile_cols_log2, self.config.tile_rows_log2); | 780 | crate::encode::arm_tiles(self.config.tile_cols_log2, self.config.tile_rows_log2); |
| 781 | + if let Some(previous) = self.held.take() { | ||
| 782 | + return Ok(vec![self.encode_flat(&previous, None)?]); | ||
| 783 | + } | ||
| 754 | self.drain_pending() | 784 | self.drain_pending() |
| 755 | } | 785 | } |
| 756 | 786 | ||
| 757 | - /// The pre-pyramid path, byte for byte: one picture in, one packet out. | 787 | + /// The flat path: one picture and its lookahead (the next source picture, |
| 758 | - fn encode_flat(&mut self, picture: &Picture) -> Result<Packet> { | 788 | + /// `None` at the end of the stream) in, one packet out — the same inputs |
| 789 | + /// [`crate::encode::encode_sequence`] gives each of its inter frames, so | ||
| 790 | + /// the bytes are identical. | ||
| 791 | + fn encode_flat(&mut self, picture: &Picture, lookahead: Option<&Picture>) -> Result<Packet> { | ||
| 759 | let render = (self.config.width, self.config.height); | 792 | let render = (self.config.width, self.config.height); |
| 760 | let padded = picture.padded_to(SUPERBLOCK); | 793 | let padded = picture.padded_to(SUPERBLOCK); |
| 761 | let is_key = self.next_index.is_multiple_of(self.config.gop as u64); | 794 | let is_key = self.next_index.is_multiple_of(self.config.gop as u64); |
| @@ -803,9 +836,7 @@ impl Av1Encoder { | |||
| 803 | self.prev2.as_ref(), | 836 | self.prev2.as_ref(), |
| 804 | &self.fctx, | 837 | &self.fctx, |
| 805 | None, | 838 | None, |
| 806 | - // The streaming facade holds no lookahead buffer | 839 | + lookahead.map(|n| n.padded_to(SUPERBLOCK)).as_ref(), |
| 807 | - // (lane-av1tpl). | ||
| 808 | - None, | ||
| 809 | )? | 840 | )? |
| 810 | }; | 841 | }; |
| 811 | 842 | ||
| @@ -1098,17 +1129,34 @@ mod tests { | |||
| 1098 | picture | 1129 | picture |
| 1099 | } | 1130 | } |
| 1100 | 1131 | ||
| 1132 | + /// Drives a whole stream through the facade the way a caller must since | ||
| 1133 | + /// it holds one picture of lookahead: every picture through | ||
| 1134 | + /// [`Av1Encoder::encode_frames`], then [`Av1Encoder::flush`]. | ||
| 1135 | + fn encode_all(enc: &mut Av1Encoder, pictures: &[Picture]) -> Vec<Packet> { | ||
| 1136 | + let mut packets = Vec::with_capacity(pictures.len()); | ||
| 1137 | + for (i, picture) in pictures.iter().enumerate() { | ||
| 1138 | + packets.extend( | ||
| 1139 | + enc.encode_frames(picture) | ||
| 1140 | + .unwrap_or_else(|e| panic!("picture {i}: {e}")), | ||
| 1141 | + ); | ||
| 1142 | + } | ||
| 1143 | + packets.extend(enc.flush().unwrap_or_else(|e| panic!("flush: {e}"))); | ||
| 1144 | + packets | ||
| 1145 | + } | ||
| 1146 | + | ||
| 1101 | /// Every OBU stream this test writes to `ffmpeg`/`ffprobe`, concatenated | 1147 | /// Every OBU stream this test writes to `ffmpeg`/`ffprobe`, concatenated |
| 1102 | /// in order. | 1148 | /// in order. |
| 1103 | fn concat(packets: &[Packet]) -> Vec<u8> { | 1149 | fn concat(packets: &[Packet]) -> Vec<u8> { |
| 1104 | packets.iter().flat_map(|p| p.data.clone()).collect() | 1150 | packets.iter().flat_map(|p| p.data.clone()).collect() |
| 1105 | } | 1151 | } |
| 1106 | 1152 | ||
| 1107 | - /// One call to [`Av1Encoder::encode`] returns exactly one packet, always | 1153 | + /// The facade's latency contract (lane-av1facade): the first |
| 1108 | - /// — the facade's whole "nothing held back" contract, checked without | 1154 | + /// [`Av1Encoder::encode`] call returns an empty packet, every later call |
| 1109 | - /// ffmpeg since it is a property of the return type, not the bytes. | 1155 | + /// returns the PREVIOUS picture's packet, and [`Av1Encoder::flush`] |
| 1156 | + /// yields the last one — so a caller still gets exactly one non-empty | ||
| 1157 | + /// packet per picture, one call later. | ||
| 1110 | #[test] | 1158 | #[test] |
| 1111 | - fn one_in_one_out() { | 1159 | + fn one_in_one_out_delayed_by_a_frame() { |
| 1112 | let config = EncoderConfig { | 1160 | let config = EncoderConfig { |
| 1113 | width: 64, | 1161 | width: 64, |
| 1114 | height: 64, | 1162 | height: 64, |
| @@ -1121,8 +1169,67 @@ mod tests { | |||
| 1121 | let mut enc = Av1Encoder::new(config).unwrap(); | 1169 | let mut enc = Av1Encoder::new(config).unwrap(); |
| 1122 | for t in 0..5u64 { | 1170 | for t in 0..5u64 { |
| 1123 | let packet = enc.encode(&test_card(64, 64, t as usize)).unwrap(); | 1171 | let packet = enc.encode(&test_card(64, 64, t as usize)).unwrap(); |
| 1172 | + if t == 0 { | ||
| 1173 | + assert!(packet.data.is_empty(), "the first call holds its picture back"); | ||
| 1174 | + continue; | ||
| 1175 | + } | ||
| 1124 | assert!(!packet.data.is_empty(), "picture {t}: empty packet"); | 1176 | assert!(!packet.data.is_empty(), "picture {t}: empty packet"); |
| 1125 | - assert_eq!(packet.order, t, "picture {t}: order"); | 1177 | + assert_eq!(packet.order, t - 1, "picture {t}: order"); |
| 1178 | + } | ||
| 1179 | + let tail = enc.flush().unwrap(); | ||
| 1180 | + assert_eq!(tail.len(), 1, "flush yields the last picture"); | ||
| 1181 | + assert_eq!(tail[0].order, 4); | ||
| 1182 | + assert!(enc.flush().unwrap().is_empty(), "nothing is held twice"); | ||
| 1183 | + } | ||
| 1184 | + | ||
| 1185 | + /// THE ENTRY-SURFACE GATE (lane-av1facade): the streaming facade — what | ||
| 1186 | + /// the editor's export drives — codes byte for byte what | ||
| 1187 | + /// [`crate::encode::encode_sequence`] codes, which is the path the BD | ||
| 1188 | + /// gate measures. It did not before: the facade passed no lookahead, so | ||
| 1189 | + /// every inter frame missed the per-superblock temporal lambda map | ||
| 1190 | + /// (lane-av1tpl) the gate's numbers were measured with. | ||
| 1191 | + /// | ||
| 1192 | + /// Runs on the gate's own clip at the gate's own size when the fixture is | ||
| 1193 | + /// there, and on a synthetic card otherwise — never skipped, since a | ||
| 1194 | + /// skipped identity gate is the defect it is meant to catch. | ||
| 1195 | + #[test] | ||
| 1196 | + fn the_facade_codes_the_same_bytes_as_encode_sequence() { | ||
| 1197 | + let frames = 12usize; | ||
| 1198 | + let pictures = h264_clip_frames(640, 384, frames).unwrap_or_else(|| { | ||
| 1199 | + eprintln!("no h264 clip: the facade identity gate runs on a synthetic card"); | ||
| 1200 | + (0..frames).map(|t| test_card(64, 64, t)).collect() | ||
| 1201 | + }); | ||
| 1202 | + let (width, height) = (pictures[0].width, pictures[0].height); | ||
| 1203 | + for q in [150u8, 60] { | ||
| 1204 | + let config = EncoderConfig { | ||
| 1205 | + width, | ||
| 1206 | + height, | ||
| 1207 | + base_q_idx: q, | ||
| 1208 | + // One key frame then all inter, which is `encode_sequence`'s | ||
| 1209 | + // only shape, and the gate's recipe. | ||
| 1210 | + gop: frames, | ||
| 1211 | + // `encode_sequence` writes an unspecified colour config. | ||
| 1212 | + colour: Colour::Unspecified, | ||
| 1213 | + tile_cols_log2: 0, | ||
| 1214 | + tile_rows_log2: 0, | ||
| 1215 | + }; | ||
| 1216 | + let mut enc = Av1Encoder::new(config).unwrap(); | ||
| 1217 | + let packets = encode_all(&mut enc, &pictures); | ||
| 1218 | + let sequence = crate::encode::encode_sequence(&pictures, q, DEADZONE).unwrap(); | ||
| 1219 | + assert_eq!(packets.len(), frames, "q={q}: one packet per picture"); | ||
| 1220 | + for (i, (packet, coded)) in packets.iter().zip(&sequence.frames).enumerate() { | ||
| 1221 | + let cropped = crate::encode::crop_encoded(coded, width, height); | ||
| 1222 | + assert_eq!( | ||
| 1223 | + (packet.data.len(), packet.data.clone()), | ||
| 1224 | + (cropped.stream.len(), cropped.stream.clone()), | ||
| 1225 | + "q={q} frame {i}: the facade's bytes are not the sequence path's" | ||
| 1226 | + ); | ||
| 1227 | + } | ||
| 1228 | + assert_eq!( | ||
| 1229 | + concat(&packets), | ||
| 1230 | + sequence.stream, | ||
| 1231 | + "q={q}: whole stream" | ||
| 1232 | + ); | ||
| 1126 | } | 1233 | } |
| 1127 | } | 1234 | } |
| 1128 | 1235 | ||
| @@ -1141,8 +1248,10 @@ mod tests { | |||
| 1141 | tile_rows_log2: 0, | 1248 | tile_rows_log2: 0, |
| 1142 | }; | 1249 | }; |
| 1143 | let mut enc = Av1Encoder::new(config).unwrap(); | 1250 | let mut enc = Av1Encoder::new(config).unwrap(); |
| 1144 | - let keys: Vec<bool> = (0..7) | 1251 | + let pictures: Vec<Picture> = (0..7).map(|t| test_card(64, 64, t)).collect(); |
| 1145 | - .map(|t| enc.encode(&test_card(64, 64, t)).unwrap().key) | 1252 | + let keys: Vec<bool> = encode_all(&mut enc, &pictures) |
| 1253 | + .iter() | ||
| 1254 | + .map(|p| p.key) | ||
| 1146 | .collect(); | 1255 | .collect(); |
| 1147 | assert_eq!(keys, vec![true, false, false, true, false, false, true]); | 1256 | assert_eq!(keys, vec![true, false, false, true, false, false, true]); |
| 1148 | } | 1257 | } |
| @@ -1251,9 +1360,8 @@ mod tests { | |||
| 1251 | ) | 1360 | ) |
| 1252 | ); | 1361 | ); |
| 1253 | 1362 | ||
| 1254 | - let packets: Vec<Packet> = (0..30) | 1363 | + let pictures: Vec<Picture> = (0..30).map(|t| test_card(width, height, t)).collect(); |
| 1255 | - .map(|t| enc.encode(&test_card(width, height, t)).unwrap()) | 1364 | + let packets: Vec<Packet> = encode_all(&mut enc, &pictures); |
| 1256 | - .collect(); | ||
| 1257 | let expect_key: Vec<bool> = (0..30).map(|t| t % 15 == 0).collect(); | 1365 | let expect_key: Vec<bool> = (0..30).map(|t| t % 15 == 0).collect(); |
| 1258 | assert_eq!( | 1366 | assert_eq!( |
| 1259 | packets.iter().map(|p| p.key).collect::<Vec<_>>(), | 1367 | packets.iter().map(|p| p.key).collect::<Vec<_>>(), |
| @@ -1296,8 +1404,8 @@ mod tests { | |||
| 1296 | tile_rows_log2: 0, | 1404 | tile_rows_log2: 0, |
| 1297 | }; | 1405 | }; |
| 1298 | let mut enc = Av1Encoder::new(config).unwrap(); | 1406 | let mut enc = Av1Encoder::new(config).unwrap(); |
| 1299 | - let packet = enc.encode(&test_card(64, 64, 0)).unwrap(); | 1407 | + let packets = encode_all(&mut enc, &[test_card(64, 64, 0)]); |
| 1300 | - let colour = ffprobe_colour(&packet.data); | 1408 | + let colour = ffprobe_colour(&packets[0].data); |
| 1301 | assert_eq!(colour, "tv,bt709,bt709,bt709", "ffprobe colour fields"); | 1409 | assert_eq!(colour, "tv,bt709,bt709,bt709", "ffprobe colour fields"); |
| 1302 | } | 1410 | } |
| 1303 | 1411 | ||
| @@ -1320,8 +1428,8 @@ mod tests { | |||
| 1320 | tile_rows_log2: 0, | 1428 | tile_rows_log2: 0, |
| 1321 | }; | 1429 | }; |
| 1322 | let mut enc = Av1Encoder::new(config).unwrap(); | 1430 | let mut enc = Av1Encoder::new(config).unwrap(); |
| 1323 | - let packet = enc.encode(&test_card(64, 64, 0)).unwrap(); | 1431 | + let packets = encode_all(&mut enc, &[test_card(64, 64, 0)]); |
| 1324 | - let colour = ffprobe_colour(&packet.data); | 1432 | + let colour = ffprobe_colour(&packets[0].data); |
| 1325 | assert_eq!( | 1433 | assert_eq!( |
| 1326 | colour, "tv,smpte170m,smpte170m,smpte170m", | 1434 | colour, "tv,smpte170m,smpte170m,smpte170m", |
| 1327 | "ffprobe colour fields" | 1435 | "ffprobe colour fields" |
| @@ -1433,9 +1541,9 @@ mod tests { | |||
| 1433 | }; | 1541 | }; |
| 1434 | let mut enc = | 1542 | let mut enc = |
| 1435 | Av1Encoder::with_rate_target(config, RateTarget::BytesPerFrame(target_bytes)).unwrap(); | 1543 | Av1Encoder::with_rate_target(config, RateTarget::BytesPerFrame(target_bytes)).unwrap(); |
| 1436 | - let sizes: Vec<usize> = pictures | 1544 | + let sizes: Vec<usize> = encode_all(&mut enc, &pictures) |
| 1437 | .iter() | 1545 | .iter() |
| 1438 | - .map(|p| enc.encode(p).unwrap().data.len()) | 1546 | + .map(|p| p.data.len()) |
| 1439 | .collect(); | 1547 | .collect(); |
| 1440 | // Discard the key frame (always far larger than an inter target) and | 1548 | // Discard the key frame (always far larger than an inter target) and |
| 1441 | // the next 4 inter frames the loop needs to step toward it. | 1549 | // the next 4 inter frames the loop needs to step toward it. |
| @@ -1535,7 +1643,7 @@ mod tests { | |||
| 1535 | Av1Encoder::with_rate_target(config, RateTarget::BytesPerFrame(4_000)).unwrap(); | 1643 | Av1Encoder::with_rate_target(config, RateTarget::BytesPerFrame(4_000)).unwrap(); |
| 1536 | let mut prev_q = enc.rate_loop.as_ref().unwrap().q_idx(Level::Leaf); | 1644 | let mut prev_q = enc.rate_loop.as_ref().unwrap().q_idx(Level::Leaf); |
| 1537 | for picture in &pictures { | 1645 | for picture in &pictures { |
| 1538 | - enc.encode(picture).unwrap(); | 1646 | + enc.encode_frames(picture).unwrap(); |
| 1539 | let q = enc.rate_loop.as_ref().unwrap().q_idx(Level::Leaf); | 1647 | let q = enc.rate_loop.as_ref().unwrap().q_idx(Level::Leaf); |
| 1540 | let step = (i32::from(q) - i32::from(prev_q)).abs(); | 1648 | let step = (i32::from(q) - i32::from(prev_q)).abs(); |
| 1541 | assert!( | 1649 | assert!( |
| @@ -1723,10 +1831,7 @@ mod tests { | |||
| 1723 | let layout = format!("{}x{} tiles", 1 << cols_log2, 1 << rows_log2); | 1831 | let layout = format!("{}x{} tiles", 1 << cols_log2, 1 << rows_log2); |
| 1724 | let mut enc = Av1Encoder::new(config).unwrap(); | 1832 | let mut enc = Av1Encoder::new(config).unwrap(); |
| 1725 | let mut stream = Vec::new(); | 1833 | let mut stream = Vec::new(); |
| 1726 | - for (i, picture) in sources.iter().enumerate() { | 1834 | + for packet in encode_all(&mut enc, &sources) { |
| 1727 | - let packet = enc | ||
| 1728 | - .encode(picture) | ||
| 1729 | - .unwrap_or_else(|e| panic!("{layout} frame {i}: {e}")); | ||
| 1730 | stream.extend_from_slice(&packet.data); | 1835 | stream.extend_from_slice(&packet.data); |
| 1731 | } | 1836 | } |
| 1732 | sizes.push((layout.clone(), stream.len())); | 1837 | sizes.push((layout.clone(), stream.len())); |
| @@ -1819,8 +1924,8 @@ mod tests { | |||
| 1819 | let mut enc = Av1Encoder::new(config).unwrap(); | 1924 | let mut enc = Av1Encoder::new(config).unwrap(); |
| 1820 | let start = std::time::Instant::now(); | 1925 | let start = std::time::Instant::now(); |
| 1821 | let mut bytes = 0usize; | 1926 | let mut bytes = 0usize; |
| 1822 | - for picture in &sources { | 1927 | + for packet in encode_all(&mut enc, &sources) { |
| 1823 | - bytes += enc.encode(picture).unwrap().data.len(); | 1928 | + bytes += packet.data.len(); |
| 1824 | } | 1929 | } |
| 1825 | let wall = start.elapsed().as_secs_f64(); | 1930 | let wall = start.elapsed().as_secs_f64(); |
| 1826 | let name = format!("{}x{}", 1 << cols_log2, 1 << rows_log2); | 1931 | let name = format!("{}x{}", 1 << cols_log2, 1 << rows_log2); |
| @@ -1860,8 +1965,8 @@ mod tests { | |||
| 1860 | }; | 1965 | }; |
| 1861 | let mut enc = Av1Encoder::new(config).unwrap(); | 1966 | let mut enc = Av1Encoder::new(config).unwrap(); |
| 1862 | let mut stream = Vec::new(); | 1967 | let mut stream = Vec::new(); |
| 1863 | - for picture in &sources { | 1968 | + for packet in encode_all(&mut enc, &sources) { |
| 1864 | - stream.extend_from_slice(&enc.encode(picture).unwrap().data); | 1969 | + stream.extend_from_slice(&packet.data); |
| 1865 | } | 1970 | } |
| 1866 | stream | 1971 | stream |
| 1867 | }; | 1972 | }; |
| @@ -1928,8 +2033,8 @@ mod tests { | |||
| 1928 | }; | 2033 | }; |
| 1929 | let mut enc = Av1Encoder::new(config).unwrap(); | 2034 | let mut enc = Av1Encoder::new(config).unwrap(); |
| 1930 | let mut stream = Vec::new(); | 2035 | let mut stream = Vec::new(); |
| 1931 | - for picture in &sources { | 2036 | + for packet in encode_all(&mut enc, &sources) { |
| 1932 | - stream.extend_from_slice(&enc.encode(picture).unwrap().data); | 2037 | + stream.extend_from_slice(&packet.data); |
| 1933 | } | 2038 | } |
| 1934 | stream | 2039 | stream |
| 1935 | }; | 2040 | }; |
| @@ -1964,8 +2069,8 @@ mod tests { | |||
| 1964 | }; | 2069 | }; |
| 1965 | let mut enc = Av1Encoder::new(config).unwrap(); | 2070 | let mut enc = Av1Encoder::new(config).unwrap(); |
| 1966 | let mut stream = Vec::new(); | 2071 | let mut stream = Vec::new(); |
| 1967 | - for picture in &sources { | 2072 | + for packet in encode_all(&mut enc, &sources) { |
| 1968 | - stream.extend_from_slice(&enc.encode(picture).unwrap().data); | 2073 | + stream.extend_from_slice(&packet.data); |
| 1969 | } | 2074 | } |
| 1970 | crate::decode::set_filter_replay_disabled(false); | 2075 | crate::decode::set_filter_replay_disabled(false); |
| 1971 | stream | 2076 | stream |
| @@ -2031,8 +2136,8 @@ mod tests { | |||
| 2031 | let mut enc = Av1Encoder::new(config).unwrap(); | 2136 | let mut enc = Av1Encoder::new(config).unwrap(); |
| 2032 | let _ = crate::par::take_stage_ns(); | 2137 | let _ = crate::par::take_stage_ns(); |
| 2033 | let start = std::time::Instant::now(); | 2138 | let start = std::time::Instant::now(); |
| 2034 | - for picture in &sources { | 2139 | + for packet in encode_all(&mut enc, &sources) { |
| 2035 | - assert!(!enc.encode(picture).unwrap().data.is_empty()); | 2140 | + assert!(!packet.data.is_empty()); |
| 2036 | } | 2141 | } |
| 2037 | let wall = start.elapsed().as_secs_f64(); | 2142 | let wall = start.elapsed().as_secs_f64(); |
| 2038 | let ns = crate::par::take_stage_ns(); | 2143 | let ns = crate::par::take_stage_ns(); |
| @@ -2102,8 +2207,8 @@ mod tests { | |||
| 2102 | let mut enc = Av1Encoder::new(config).unwrap(); | 2207 | let mut enc = Av1Encoder::new(config).unwrap(); |
| 2103 | let start = std::time::Instant::now(); | 2208 | let start = std::time::Instant::now(); |
| 2104 | let mut bytes = 0usize; | 2209 | let mut bytes = 0usize; |
| 2105 | - for picture in &sources { | 2210 | + for packet in encode_all(&mut enc, &sources) { |
| 2106 | - bytes += enc.encode(picture).unwrap().data.len(); | 2211 | + bytes += packet.data.len(); |
| 2107 | } | 2212 | } |
| 2108 | (start.elapsed(), bytes) | 2213 | (start.elapsed(), bytes) |
| 2109 | }; | 2214 | }; |
| @@ -2243,10 +2348,7 @@ mod tests { | |||
| 2243 | let layout = format!("{}x{} tiles", 1 << cols_log2, 1 << rows_log2); | 2348 | let layout = format!("{}x{} tiles", 1 << cols_log2, 1 << rows_log2); |
| 2244 | let mut enc = Av1Encoder::new(config).unwrap(); | 2349 | let mut enc = Av1Encoder::new(config).unwrap(); |
| 2245 | let mut stream = Vec::new(); | 2350 | let mut stream = Vec::new(); |
| 2246 | - for (i, picture) in sources.iter().enumerate() { | 2351 | + for packet in encode_all(&mut enc, &sources) { |
| 2247 | - let packet = enc | ||
| 2248 | - .encode(picture) | ||
| 2249 | - .unwrap_or_else(|e| panic!("{layout} frame {i}: {e}")); | ||
| 2250 | stream.extend_from_slice(&packet.data); | 2352 | stream.extend_from_slice(&packet.data); |
| 2251 | } | 2353 | } |
| 2252 | let ours = crate::stream::decode_stream(&stream).expect("our decoder"); | 2354 | let ours = crate::stream::decode_stream(&stream).expect("our decoder"); |
| @@ -2301,8 +2403,7 @@ mod tests { | |||
| 2301 | Av1Encoder::with_rate_target(config, RateTarget::Quality(quality)).unwrap(); | 2403 | Av1Encoder::with_rate_target(config, RateTarget::Quality(quality)).unwrap(); |
| 2302 | let mut stream = Vec::new(); | 2404 | let mut stream = Vec::new(); |
| 2303 | let mut total_bytes = 0usize; | 2405 | let mut total_bytes = 0usize; |
| 2304 | - for picture in &pictures { | 2406 | + for packet in encode_all(&mut enc, &pictures) { |
| 2305 | - let packet = enc.encode(picture).unwrap(); | ||
| 2306 | total_bytes += packet.data.len(); | 2407 | total_bytes += packet.data.len(); |
| 2307 | stream.extend_from_slice(&packet.data); | 2408 | stream.extend_from_slice(&packet.data); |
| 2308 | } | 2409 | } |