ec-h265: Decoder::end_picture finishes the access unit at once (no one-picture output delay on no-reorder streams); Frame::cropped applies the SPS conformance window
Browse files at this commit9953355c2c468d9c357fdebbec67345ea006d173Parent: d3055643 changed files +57 -0
- crates/ec-h265/src/dec/dpb.rs +29 -0
- crates/ec-h265/src/dec/mod.rs +6 -0
- crates/ec-h265/tests/it/encode_threads.rs +22 -0
crates/ec-h265/src/dec/dpb.rsmodified+29 -0
| @@ -93,6 +93,35 @@ pub struct Frame { | |||
| 93 | } | 93 | } |
| 94 | 94 | ||
| 95 | impl Frame { | 95 | impl Frame { |
| 96 | + /// The picture cropped to the conformance window, at the display size. | ||
| 97 | + pub fn cropped(&self) -> DecodedPicture { | ||
| 98 | + let p = &self.picture; | ||
| 99 | + let (sx, sy) = crate::decode::chroma_shifts(p.chroma_format_idc); | ||
| 100 | + let (left, top) = ((self.window.left << sx) as usize, (self.window.top << sy) as usize); | ||
| 101 | + let width = p.width - left - ((self.window.right << sx) as usize); | ||
| 102 | + let height = p.height - top - ((self.window.bottom << sy) as usize); | ||
| 103 | + let crop = |plane: &[u16], stride: usize, x: usize, y: usize, w: usize, h: usize| { | ||
| 104 | + (y..y + h) | ||
| 105 | + .flat_map(|r| plane[r * stride + x..r * stride + x + w].iter().copied()) | ||
| 106 | + .collect::<Vec<u16>>() | ||
| 107 | + }; | ||
| 108 | + let cw = p.width >> sx; | ||
| 109 | + let chroma = |plane: &[u16]| match plane.is_empty() { | ||
| 110 | + true => Vec::new(), | ||
| 111 | + false => crop(plane, cw, left >> sx, top >> sy, width >> sx, height >> sy), | ||
| 112 | + }; | ||
| 113 | + DecodedPicture { | ||
| 114 | + width, | ||
| 115 | + height, | ||
| 116 | + bit_depth: p.bit_depth, | ||
| 117 | + bit_depth_c: p.bit_depth_c, | ||
| 118 | + chroma_format_idc: p.chroma_format_idc, | ||
| 119 | + y: crop(&p.y, p.width, left, top, width, height), | ||
| 120 | + cb: chroma(&p.cb), | ||
| 121 | + cr: chroma(&p.cr), | ||
| 122 | + } | ||
| 123 | + } | ||
| 124 | + | ||
| 96 | /// The mastering display colour volume in force for this frame (SEI 137). | 125 | /// The mastering display colour volume in force for this frame (SEI 137). |
| 97 | pub fn mastering_display(&self) -> Option<&MasteringDisplay> { | 126 | pub fn mastering_display(&self) -> Option<&MasteringDisplay> { |
| 98 | self.meta.mastering_display.as_ref() | 127 | self.meta.mastering_display.as_ref() |
crates/ec-h265/src/dec/mod.rsmodified+6 -0
| @@ -310,6 +310,12 @@ impl Decoder { | |||
| 310 | Ok(()) | 310 | Ok(()) |
| 311 | } | 311 | } |
| 312 | 312 | ||
| 313 | + /// End of the current access unit: finish the open picture now instead of | ||
| 314 | + /// at the next picture's first slice, so a no-reorder stream outputs it at once. | ||
| 315 | + pub fn end_picture(&mut self) { | ||
| 316 | + self.finish_picture(); | ||
| 317 | + } | ||
| 318 | + | ||
| 313 | /// End of stream: finish the open picture and output everything pending. | 319 | /// End of stream: finish the open picture and output everything pending. |
| 314 | pub fn flush(&mut self) -> Result<()> { | 320 | pub fn flush(&mut self) -> Result<()> { |
| 315 | self.finish_picture(); | 321 | self.finish_picture(); |
crates/ec-h265/tests/it/encode_threads.rsmodified+22 -0
| @@ -174,3 +174,25 @@ fn sequence_encoder_matches_encode_sequence_and_forces_idrs() { | |||
| 174 | "forced-IDR stream does not decode to the reconstruction" | 174 | "forced-IDR stream does not decode to the reconstruction" |
| 175 | ); | 175 | ); |
| 176 | } | 176 | } |
| 177 | + | ||
| 178 | +#[test] | ||
| 179 | +fn end_picture_outputs_each_access_unit_cropped() { | ||
| 180 | + let frames = moving_clip(4, 200, 120); | ||
| 181 | + let mut cfg = EncoderConfig::new(200, 120); | ||
| 182 | + cfg.rate_control = RateControl::ConstantQp(30); | ||
| 183 | + cfg.keyint = 4; | ||
| 184 | + cfg.keep_recon = true; | ||
| 185 | + cfg.threads = 1; | ||
| 186 | + let mut seq = ec_h265::SequenceEncoder::new(Encoder::new(cfg).expect("encoder")).expect("sequence"); | ||
| 187 | + let mut dec = Decoder::new(); | ||
| 188 | + for f in &frames { | ||
| 189 | + let pic = seq.encode(f, false).expect("encode"); | ||
| 190 | + dec.decode_annex_b(&pic.au).expect("decode"); | ||
| 191 | + dec.end_picture(); | ||
| 192 | + let out = dec.next_frame().expect("a frame per access unit").cropped(); | ||
| 193 | + assert_eq!((out.width, out.height), (200, 120)); | ||
| 194 | + let recon = pic.recon.expect("recon"); | ||
| 195 | + let y: Vec<u8> = out.y.iter().map(|&v| v as u8).collect(); | ||
| 196 | + assert!(*y == *recon.planes[0].data, "cropped luma differs from the reconstruction"); | ||
| 197 | + } | ||
| 198 | +} | ||