forge
Explore
Log in

Don't you dare go Hollow

U7: replace the MotionSource trait, ZeroMotion placeholder and CuGeometry with the concrete SliceMotion (derive_motion and derive_merge take the CU position, size and part mode); no output change

Browse files at this commit

703658386036d85b4958ab9aff68909d0d9df956Ahmet Kaan GümüşParent: 0214c6d
crates/ec-h265/src/dec/cu_syntax.rsmodified+19 -104
@@ -1,14 +1,11 @@
1-//! CABAC syntax of slice data for P and B slices (and for I slices when the1+//! CABAC syntax of slice data for I, P and B slices: coding quadtree, coding units, prediction units, the
2-//! syntax check runs): coding quadtree, coding units, prediction units, the
3 //! transform tree, SAO parameters and the slice-segment loop (H.265 7.3.8.1 to2 //! transform tree, SAO parameters and the slice-segment loop (H.265 7.3.8.1 to
4 //! 7.3.8.12, bin binarisations of 9.3.3 and context selection of 9.3.4.2).3 //! 7.3.8.12, bin binarisations of 9.3.3 and context selection of 9.3.4.2).
5 //!4 //!
6 //! The parser reads every bin, keeps the syntax of each coding unit, and turns5 //! The parser reads every bin, keeps the syntax of each coding unit, and turns
7 //! each transform block's coefficients into residual samples through the same6 //! each transform block's coefficients into residual samples through the same
8 //! dequantisation and inverse transform the intra path uses. It does not7 //! dequantisation and inverse transform the intra path uses. It does not
9-//! predict. Motion for each prediction unit comes from a [`MotionSource`]; the8+//! predict. Motion for each prediction unit comes from a [`SliceMotion`].
10-//! merge and AMVP lane implements that trait, and [`ZeroMotion`] stands in until
11-//! then.
12 //!9 //!
13 //! Slices, slice segments, tiles and wavefront substreams are followed: CTBs10 //! Slices, slice segments, tiles and wavefront substreams are followed: CTBs
14 //! are visited in tile scan order, contexts are initialised, synced or restored11 //! are visited in tile scan order, contexts are initialised, synced or restored
@@ -17,7 +14,7 @@
17 14
18 use super::inter::{self, InterRefs};15 use super::inter::{self, InterRefs};
19 use super::mc::{Block, McScratch};16 use super::mc::{Block, McScratch};
20-use super::motion_field::MotionField;17+use super::motion_field::SliceMotion;
21 use super::pic::{BlockInfo, CtbInfo, EDGE_PU, EDGE_TU, PicState, SaoParams, SliceFilter};18 use super::pic::{BlockInfo, CtbInfo, EDGE_PU, EDGE_TU, PicState, SaoParams, SliceFilter};
22 use super::rext;19 use super::rext;
23 use super::tiles::TileScan;20 use super::tiles::TileScan;
@@ -93,7 +90,7 @@ pub struct PredictionUnit {
93 pub mvd: [[i32; 2]; 2],90 pub mvd: [[i32; 2]; 2],
94 }91 }
95 92
96-/// Motion of one prediction unit, as a [`MotionSource`] returns it.93+/// Motion of one prediction unit, as [`SliceMotion::derive_motion`] returns it.
97 #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]94 #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
98 pub struct Motion {95 pub struct Motion {
99 /// `predFlagLX` for list 0 and list 1.96 /// `predFlagLX` for list 0 and list 1.
@@ -104,75 +101,6 @@ pub struct Motion {
104 pub mv: [[i32; 2]; 2],101 pub mv: [[i32; 2]; 2],
105 }102 }
106 103
107-/// The geometry of a coding unit, as a [`MotionSource`] needs it.
108-#[derive(Clone, Copy, Debug, PartialEq, Eq)]
109-pub struct CuGeometry {
110- /// Luma position of the coding unit.
111- pub x: usize,
112- /// Luma position of the coding unit.
113- pub y: usize,
114- /// Luma size of the coding unit.
115- pub size: usize,
116- /// The partitioning.
117- pub part_mode: PartMode,
118- /// Whether the coding unit is skipped.
119- pub skip: bool,
120-}
121-
122-/// Derives the motion of each prediction unit. Called once per unit, in decoding
123-/// order, after the unit's syntax has been parsed (so merge candidates from
124-/// earlier units are available to the implementation). Merge and AMVP derivation
125-/// (8.5.3) is the job of the implementation; the parser does not look inside.
126-pub trait MotionSource {
127- /// The motion of unit `pu_index` of the coding unit `cu`.
128- fn derive_motion(&mut self, cu: &CuGeometry, pu_index: usize, pu: &PredictionUnit) -> Motion;
129-
130- /// Called once the CTB row that ends at luma row `row_end` is fully parsed. Only
131- /// pictures with one tile get the call (their CTBs come in raster order).
132- fn row_done(&mut self, _pic: &mut PicState, _row_end: usize) {}
133-
134- /// The motion field this source writes, when it is a wavefront band (see [`wpp`]).
135- fn band_cells(&mut self) -> Option<&mut MotionField> {
136- None
137- }
138-}
139-
140-/// The placeholder [`MotionSource`]: zero motion vectors and reference index 0 for
141-/// every list the unit uses. Replace it with the merge/AMVP derivation.
142-#[derive(Clone, Copy, Debug, Default)]
143-pub struct ZeroMotion;
144-
145-impl MotionSource for ZeroMotion {
146- fn derive_motion(&mut self, _cu: &CuGeometry, _pu_index: usize, pu: &PredictionUnit) -> Motion {
147- let pred_flags = if pu.merge_flag {
148- [true, false]
149- } else {
150- match pu.inter_pred_idc {
151- InterPredIdc::PredL0 => [true, false],
152- InterPredIdc::PredL1 => [false, true],
153- InterPredIdc::PredBi => [true, true],
154- }
155- };
156- let ref_idx = [
157- if pred_flags[0] {
158- i8::try_from(pu.ref_idx[0]).unwrap_or(0)
159- } else {
160- -1
161- },
162- if pred_flags[1] {
163- i8::try_from(pu.ref_idx[1]).unwrap_or(0)
164- } else {
165- -1
166- },
167- ];
168- Motion {
169- pred_flags,
170- ref_idx,
171- mv: [[0, 0], [0, 0]],
172- }
173- }
174-}
175-
176 /// The residual samples of one transform block, after dequantisation and the104 /// The residual samples of one transform block, after dequantisation and the
177 /// inverse transform. Samples are row-major, `size * size`.105 /// inverse transform. Samples are row-major, `size * size`.
178 #[derive(Clone, Debug, PartialEq, Eq)]106 #[derive(Clone, Debug, PartialEq, Eq)]
@@ -212,7 +140,7 @@ pub struct CodingUnit {
212 pub chroma_mode: u8,140 pub chroma_mode: u8,
213 /// The prediction units, in order.141 /// The prediction units, in order.
214 pub pus: Vec<PredictionUnit>,142 pub pus: Vec<PredictionUnit>,
215- /// The motion of each prediction unit, from the [`MotionSource`].143+ /// The motion of each prediction unit, from [`SliceMotion`].
216 pub motion: Vec<Motion>,144 pub motion: Vec<Motion>,
217 /// `rqt_root_cbf` (inferred 1 for intra).145 /// `rqt_root_cbf` (inferred 1 for intra).
218 pub rqt_root_cbf: bool,146 pub rqt_root_cbf: bool,
@@ -245,8 +173,7 @@ pub struct SliceStats {
245 }173 }
246 174
247 /// Parse one slice segment's data into `pic`. The block maps are always175 /// Parse one slice segment's data into `pic`. The block maps are always
248-/// filled. When `pic` has sample planes (pixel decode), intra coding units are176+/// filled. Intra coding units are reconstructed in decoding order, and inter coding units are predicted from
249-/// reconstructed in decoding order, and inter coding units are predicted from
250 /// `refs` (8.5.3.3) per prediction unit, then get their residual added.177 /// `refs` (8.5.3.3) per prediction unit, then get their residual added.
251 #[allow(clippy::too_many_arguments)] // the slice, the picture it decodes into, and its references178 #[allow(clippy::too_many_arguments)] // the slice, the picture it decodes into, and its references
252 pub fn parse_slice_into(179 pub fn parse_slice_into(
@@ -254,7 +181,7 @@ pub fn parse_slice_into(
254 pps: &Pps,181 pps: &Pps,
255 header: &SliceHeader,182 header: &SliceHeader,
256 data: &[u8],183 data: &[u8],
257- motion: &mut dyn MotionSource,184+ motion: &mut SliceMotion<'_>,
258 pic: &mut PicState,185 pic: &mut PicState,
259 refs: Option<&InterRefs>,186 refs: Option<&InterRefs>,
260 sink: &mut dyn FnMut(&CodingUnit),187 sink: &mut dyn FnMut(&CodingUnit),
@@ -456,15 +383,15 @@ pub(super) fn trailing_bits_ok(data: &[u8], read: usize) -> bool {
456 }383 }
457 384
458 /// The coding-tree walk of one slice segment, starting with `last_qp` as qPY_PREV.385 /// The coding-tree walk of one slice segment, starting with `last_qp` as qPY_PREV.
459-fn new_parser<'a>(386+fn new_parser<'a, 'm>(
460 sps: &'a Sps,387 sps: &'a Sps,
461 pps: &'a Pps,388 pps: &'a Pps,
462 header: &'a SliceHeader,389 header: &'a SliceHeader,
463 pic: &'a mut PicState,390 pic: &'a mut PicState,
464 refs: Option<&'a InterRefs>,391 refs: Option<&'a InterRefs>,
465- motion: &'a mut dyn MotionSource,392+ motion: &'a mut SliceMotion<'m>,
466 last_qp: i32,393 last_qp: i32,
467-) -> Parser<'a> {394+) -> Parser<'a, 'm> {
468 let min_cb_log2 = sps.log2_min_cb_size_minus3 + 3;395 let min_cb_log2 = sps.log2_min_cb_size_minus3 + 3;
469 let ctb_log2 = min_cb_log2 + sps.log2_diff_max_min_cb_size;396 let ctb_log2 = min_cb_log2 + sps.log2_diff_max_min_cb_size;
470 let min_tb_log2 = sps.log2_min_tb_size_minus2 + 2;397 let min_tb_log2 = sps.log2_min_tb_size_minus2 + 2;
@@ -555,7 +482,7 @@ struct Cbf {
555 }482 }
556 483
557 /// The coding-tree walk state for one slice segment.484 /// The coding-tree walk state for one slice segment.
558-struct Parser<'a> {485+struct Parser<'a, 'm> {
559 sps: &'a Sps,486 sps: &'a Sps,
560 pps: &'a Pps,487 pps: &'a Pps,
561 header: &'a SliceHeader,488 header: &'a SliceHeader,
@@ -599,11 +526,11 @@ struct Parser<'a> {
599 cu_bypass: bool,526 cu_bypass: bool,
600 /// The chroma modes of the intra coding unit being parsed, with their DM flags.527 /// The chroma modes of the intra coding unit being parsed, with their DM flags.
601 chroma_modes: ([u8; 4], [bool; 4]),528 chroma_modes: ([u8; 4], [bool; 4]),
602- motion: &'a mut dyn MotionSource,529+ motion: &'a mut SliceMotion<'m>,
603 stats: SliceStats,530 stats: SliceStats,
604 }531 }
605 532
606-impl Parser<'_> {533+impl Parser<'_, '_> {
607 fn info_at(&self, x: usize, y: usize) -> BlockInfo {534 fn info_at(&self, x: usize, y: usize) -> BlockInfo {
608 self.pic.info[self.pic.unit(x, y)]535 self.pic.info[self.pic.unit(x, y)]
609 }536 }
@@ -765,15 +692,10 @@ impl Parser<'_> {
765 692
766 if skip {693 if skip {
767 self.stats.skip_cus += 1;694 self.stats.skip_cus += 1;
768- let geom = CuGeometry {
769- x: x0,
770- y: y0,
771- size: n,
772- part_mode: PartMode::Part2Nx2N,
773- skip: true,
774- };
775 let pu = self.prediction_unit(dec, x0, y0, n, n, depth, true)?;695 let pu = self.prediction_unit(dec, x0, y0, n, n, depth, true)?;
776- let m = self.motion.derive_motion(&geom, 0, &pu);696+ let m = self
697+ .motion
698+ .derive_motion(x0, y0, n, PartMode::Part2Nx2N, 0, &pu);
777 self.predict_pu(x0, y0, n, n, &m)?;699 self.predict_pu(x0, y0, n, n, &m)?;
778 cu.motion.push(m);700 cu.motion.push(m);
779 cu.pus.push(pu);701 cu.pus.push(pu);
@@ -823,16 +745,9 @@ impl Parser<'_> {
823 } else {745 } else {
824 PartMode::Part2Nx2N746 PartMode::Part2Nx2N
825 };747 };
826- let geom = CuGeometry {
827- x: x0,
828- y: y0,
829- size: n,
830- part_mode: cu.part_mode,
831- skip: false,
832- };
833 for (i, (px, py, pw, ph)) in pu_rects(cu.part_mode, x0, y0, n).into_iter().enumerate() {748 for (i, (px, py, pw, ph)) in pu_rects(cu.part_mode, x0, y0, n).into_iter().enumerate() {
834 let pu = self.prediction_unit(dec, px, py, pw, ph, depth, false)?;749 let pu = self.prediction_unit(dec, px, py, pw, ph, depth, false)?;
835- let m = self.motion.derive_motion(&geom, i, &pu);750+ let m = self.motion.derive_motion(x0, y0, n, cu.part_mode, i, &pu);
836 self.predict_pu(px, py, pw, ph, &m)?;751 self.predict_pu(px, py, pw, ph, &m)?;
837 self.pic.mark_edges(px, py, pw, ph, EDGE_PU);752 self.pic.mark_edges(px, py, pw, ph, EDGE_PU);
838 cu.motion.push(m);753 cu.motion.push(m);
@@ -861,7 +776,7 @@ impl Parser<'_> {
861 }776 }
862 777
863 /// Motion-compensated prediction of one prediction unit (8.5.3.3) into the778 /// Motion-compensated prediction of one prediction unit (8.5.3.3) into the
864- /// picture's samples. Nothing happens without pixel decode.779+ /// picture's samples.
865 fn predict_pu(&mut self, x: usize, y: usize, w: usize, h: usize, m: &Motion) -> Result<()> {780 fn predict_pu(&mut self, x: usize, y: usize, w: usize, h: usize, m: &Motion) -> Result<()> {
866 let Some(refs) = self.refs else {781 let Some(refs) = self.refs else {
867 return Err(Error::corrupt(782 return Err(Error::corrupt(
@@ -1217,7 +1132,7 @@ impl Parser<'_> {
1217 }1132 }
1218 1133
1219 /// The transform unit (7.3.8.10): the QP delta, then each coded block's1134 /// The transform unit (7.3.8.10): the QP delta, then each coded block's
1220- /// residual. With pixel decode on, an intra block is predicted and1135+ /// residual. An intra block is predicted and
1221 /// reconstructed here, in the order luma, Cb, Cr that its neighbours need.1136 /// reconstructed here, in the order luma, Cb, Cr that its neighbours need.
1222 fn transform_unit(1137 fn transform_unit(
1223 &mut self,1138 &mut self,
crates/ec-h265/src/dec/cu_syntax/wpp.rsmodified+11 -21
@@ -18,7 +18,7 @@ use std::panic::{AssertUnwindSafe, catch_unwind};
18 use std::sync::atomic::{AtomicUsize, Ordering};18 use std::sync::atomic::{AtomicUsize, Ordering};
19 use std::sync::{Condvar, Mutex, MutexGuard};19 use std::sync::{Condvar, Mutex, MutexGuard};
20 20
21-use super::{MotionSource, SliceStats, new_parser, trailing_bits_ok};21+use super::{SliceStats, new_parser, trailing_bits_ok};
22 use crate::cabac::{CabacDecoder, Contexts};22 use crate::cabac::{CabacDecoder, Contexts};
23 use crate::dec::inter::InterRefs;23 use crate::dec::inter::InterRefs;
24 use crate::dec::motion_field::{FieldCell, MotionField, SliceMotion};24 use crate::dec::motion_field::{FieldCell, MotionField, SliceMotion};
@@ -127,7 +127,7 @@ impl Board {
127 ry: usize,127 ry: usize,
128 x: usize,128 x: usize,
129 pic: &mut PicState,129 pic: &mut PicState,
130- motion: &mut dyn MotionSource,130+ motion: &mut SliceMotion<'_>,
131 ) -> Result<Option<Contexts>> {131 ) -> Result<Option<Contexts>> {
132 if ry == 0 {132 if ry == 0 {
133 return Ok(None);133 return Ok(None);
@@ -163,9 +163,7 @@ impl Board {
163 pic.ctb[(ry - 1) * cols + k] = slot.ctb[k];163 pic.ctb[(ry - 1) * cols + k] = slot.ctb[k];
164 }164 }
165 let unit_row = (ry << self.ctb_log2) / 4 - 1;165 let unit_row = (ry << self.ctb_log2) / 4 - 1;
166- let field = motion166+ let field = &mut *motion.field;
167- .band_cells()
168- .ok_or_else(|| Error::corrupt("HEVC slice (wavefront): a row without a motion band"))?;
169 for k in k0..=k1 {167 for k in k0..=k1 {
170 let (ua, ub) = (k * ctb / 4, ((k + 1) * ctb / 4).min(self.w4));168 let (ua, ub) = (k * ctb / 4, ((k + 1) * ctb / 4).min(self.w4));
171 field.cell_row_mut(unit_row)[ua..ub].copy_from_slice(&slot.cells[ua..ub]);169 field.cell_row_mut(unit_row)[ua..ub].copy_from_slice(&slot.cells[ua..ub]);
@@ -180,7 +178,7 @@ impl Board {
180 ry: usize,178 ry: usize,
181 x: usize,179 x: usize,
182 pic: &PicState,180 pic: &PicState,
183- motion: &mut dyn MotionSource,181+ motion: &mut SliceMotion<'_>,
184 wpp: Option<&Contexts>,182 wpp: Option<&Contexts>,
185 ) -> Result<()> {183 ) -> Result<()> {
186 let ctb = 1usize << self.ctb_log2;184 let ctb = 1usize << self.ctb_log2;
@@ -193,13 +191,7 @@ impl Board {
193 (((x + 1) * ctb) >> self.sub_x).min(cw),191 (((x + 1) * ctb) >> self.sub_x).min(cw),
194 );192 );
195 let (ua, ub) = (x * ctb / 4, ((x + 1) * ctb / 4).min(self.w4));193 let (ua, ub) = (x * ctb / 4, ((x + 1) * ctb / 4).min(self.w4));
196- let field = if full {194+ let field = if full { Some(&mut *motion.field) } else { None };
197- Some(motion.band_cells().ok_or_else(|| {
198- Error::corrupt("HEVC slice (wavefront): a row without a motion band")
199- })?)
200- } else {
201- None
202- };
203 let mut st = lock(&self.state);195 let mut st = lock(&self.state);
204 let slot = &mut st.slots[ry];196 let slot = &mut st.slots[ry];
205 if slot.ctb.is_empty() {197 if slot.ctb.is_empty() {
@@ -254,13 +246,13 @@ trait RowSync {
254 fn enter(246 fn enter(
255 &mut self,247 &mut self,
256 pic: &mut PicState,248 pic: &mut PicState,
257- motion: &mut dyn MotionSource,249+ motion: &mut SliceMotion<'_>,
258 x: usize,250 x: usize,
259 ) -> Result<Option<Contexts>>;251 ) -> Result<Option<Contexts>>;
260 fn leave(252 fn leave(
261 &mut self,253 &mut self,
262 pic: &PicState,254 pic: &PicState,
263- motion: &mut dyn MotionSource,255+ motion: &mut SliceMotion<'_>,
264 x: usize,256 x: usize,
265 wpp: Option<&Contexts>,257 wpp: Option<&Contexts>,
266 ) -> Result<()>;258 ) -> Result<()>;
@@ -270,7 +262,7 @@ impl RowSync for Link<'_> {
270 fn enter(262 fn enter(
271 &mut self,263 &mut self,
272 pic: &mut PicState,264 pic: &mut PicState,
273- motion: &mut dyn MotionSource,265+ motion: &mut SliceMotion<'_>,
274 x: usize,266 x: usize,
275 ) -> Result<Option<Contexts>> {267 ) -> Result<Option<Contexts>> {
276 self.board.enter(self.ry, x, pic, motion)268 self.board.enter(self.ry, x, pic, motion)
@@ -279,7 +271,7 @@ impl RowSync for Link<'_> {
279 fn leave(271 fn leave(
280 &mut self,272 &mut self,
281 pic: &PicState,273 pic: &PicState,
282- motion: &mut dyn MotionSource,274+ motion: &mut SliceMotion<'_>,
283 x: usize,275 x: usize,
284 wpp: Option<&Contexts>,276 wpp: Option<&Contexts>,
285 ) -> Result<()> {277 ) -> Result<()> {
@@ -319,7 +311,7 @@ fn parse_row(
319 pps: &Pps,311 pps: &Pps,
320 header: &SliceHeader,312 header: &SliceHeader,
321 pic: &mut PicState,313 pic: &mut PicState,
322- motion: &mut dyn MotionSource,314+ motion: &mut SliceMotion<'_>,
323 refs: Option<&InterRefs>,315 refs: Option<&InterRefs>,
324 seg: &SegConsts,316 seg: &SegConsts,
325 data: &[u8],317 data: &[u8],
@@ -576,9 +568,7 @@ pub(in crate::dec) fn parse_segment(
576 };568 };
577 let results: Vec<Result<Vec<RowDone>>> = std::thread::scope(|s| {569 let results: Vec<Result<Vec<RowDone>>> = std::thread::scope(|s| {
578 let helpers: Vec<_> = (1..workers.clamp(1, nrows))570 let helpers: Vec<_> = (1..workers.clamp(1, nrows))
579- .map(|_| {571+ .map(|_| s.spawn(|| run()))
580- s.spawn(|| run())
581- })
582 .collect();572 .collect();
583 let mut all = vec![run()];573 let mut all = vec![run()];
584 for h in helpers {574 for h in helpers {
crates/ec-h265/src/dec/dpb.rsmodified+5 -3
@@ -40,8 +40,7 @@ pub struct DpbPicture {
40 pub au_index: u32,40 pub au_index: u32,
41 /// Cropping of the SPS the picture was decoded under.41 /// Cropping of the SPS the picture was decoded under.
42 pub window: ConformanceWindow,42 pub window: ConformanceWindow,
43- /// The samples and the collocated motion (8.5.3.2.8). Empty samples when the decoder43+ /// The samples and the collocated motion (8.5.3.2.8). The samples may still be being decoded.
44- /// runs without pixel decode. The samples may still be being decoded.
45 pub picture: Arc<SharedPic>,44 pub picture: Arc<SharedPic>,
46 /// HDR, colour and hash metadata of the picture.45 /// HDR, colour and hash metadata of the picture.
47 pub meta: Arc<PictureMeta>,46 pub meta: Arc<PictureMeta>,
@@ -97,7 +96,10 @@ impl Frame {
97 pub fn cropped(&self) -> DecodedPicture {96 pub fn cropped(&self) -> DecodedPicture {
98 let p = &self.picture;97 let p = &self.picture;
99 let (sx, sy) = crate::decode::chroma_shifts(p.chroma_format_idc);98 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);99+ let (left, top) = (
100+ (self.window.left << sx) as usize,
101+ (self.window.top << sy) as usize,
102+ );
101 let width = p.width - left - ((self.window.right << sx) as usize);103 let width = p.width - left - ((self.window.right << sx) as usize);
102 let height = p.height - top - ((self.window.bottom << sy) as usize);104 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| {105 let crop = |plane: &[u16], stride: usize, x: usize, y: usize, w: usize, h: usize| {
crates/ec-h265/src/dec/merge.rsmodified+8 -5
@@ -3,7 +3,7 @@
3 //! temporal candidate, combined bi-predictive candidates for B slices, zero3 //! temporal candidate, combined bi-predictive candidates for B slices, zero
4 //! candidates, and the 8x4 / 4x8 restriction on bi-prediction.4 //! candidates, and the 8x4 / 4x8 restriction on bi-prediction.
5 5
6-use super::cu_syntax::{CuGeometry, PartMode, PredictionUnit};6+use super::cu_syntax::{PartMode, PredictionUnit};
7 use super::motion_field::{PbMotion, SliceMotion, to16};7 use super::motion_field::{PbMotion, SliceMotion, to16};
8 8
9 /// Table 8-7: `l0CandIdx` for `combIdx` 0 to 11.9 /// Table 8-7: `l0CandIdx` for `combIdx` 0 to 11.
@@ -15,21 +15,24 @@ const COMB_L1: [usize; 12] = [1, 0, 2, 0, 2, 1, 3, 0, 3, 1, 3, 2];
15 /// the merge candidate list (8.5.3.2.2).15 /// the merge candidate list (8.5.3.2.2).
16 pub fn derive_merge(16 pub fn derive_merge(
17 src: &SliceMotion<'_>,17 src: &SliceMotion<'_>,
18- cu: &CuGeometry,18+ cu_x: usize,
19+ cu_y: usize,
20+ cu_size: usize,
21+ part_mode: PartMode,
19 pu_index: usize,22 pu_index: usize,
20 pu: &PredictionUnit,23 pu: &PredictionUnit,
21 ) -> PbMotion {24 ) -> PbMotion {
22 let params = src.params;25 let params = src.params;
23 // 8.5.3.2.2: with a parallel merge level above 4x4, every 8x8 coding unit shares26 // 8.5.3.2.2: with a parallel merge level above 4x4, every 8x8 coding unit shares
24 // one merge list, derived as if it were a single prediction unit.27 // one merge list, derived as if it were a single prediction unit.
25- let single_list = params.log2_par_mrg_level > 2 && cu.size == 8;28+ let single_list = params.log2_par_mrg_level > 2 && cu_size == 8;
26 let (x, y, w, h, part_idx) = if single_list {29 let (x, y, w, h, part_idx) = if single_list {
27- (cu.x, cu.y, cu.size, cu.size, 0)30+ (cu_x, cu_y, cu_size, cu_size, 0)
28 } else {31 } else {
29 (pu.x, pu.y, pu.width, pu.height, pu_index)32 (pu.x, pu.y, pu.width, pu.height, pu_index)
30 };33 };
31 34
32- let mut cands = spatial_candidates(src, cu.part_mode, x, y, w, h, part_idx);35+ let mut cands = spatial_candidates(src, part_mode, x, y, w, h, part_idx);
33 if let Some(col) = temporal_candidate(src, x, y, w, h) {36 if let Some(col) = temporal_candidate(src, x, y, w, h) {
34 cands.push(col);37 cands.push(col);
35 }38 }
crates/ec-h265/src/dec/motion_field.rsmodified+20 -14
@@ -15,7 +15,7 @@
15 15
16 use std::sync::Arc;16 use std::sync::Arc;
17 17
18-use super::cu_syntax::{CuGeometry, Motion, MotionSource, PredictionUnit};18+use super::cu_syntax::{Motion, PartMode, PredictionUnit};
19 use super::dpb::Dpb;19 use super::dpb::Dpb;
20 use super::pic::{PicState, PuMotion};20 use super::pic::{PicState, PuMotion};
21 use super::rps::{RefEntry, RefPicLists};21 use super::rps::{RefEntry, RefPicLists};
@@ -55,7 +55,7 @@ impl PbMotion {
55 PbMotion::new(m.pred_flags, m.ref_idx, [to16(m.mv[0]), to16(m.mv[1])])55 PbMotion::new(m.pred_flags, m.ref_idx, [to16(m.mv[0]), to16(m.mv[1])])
56 }56 }
57 57
58- /// The [`Motion`] a [`MotionSource`] returns for this block.58+ /// The [`Motion`] [`SliceMotion::derive_motion`] returns for this block.
59 pub fn to_motion(self) -> Motion {59 pub fn to_motion(self) -> Motion {
60 Motion {60 Motion {
61 pred_flags: self.pred,61 pred_flags: self.pred,
@@ -427,7 +427,7 @@ pub struct MotionParams {
427 pub no_backward_pred: bool,427 pub no_backward_pred: bool,
428 }428 }
429 429
430-/// The [`MotionSource`] of the decoder: derives each prediction unit's motion with430+/// The motion source of the decoder: derives each prediction unit's motion with
431 /// the merge or AMVP process and stores it in the picture's motion field.431 /// the merge or AMVP process and stores it in the picture's motion field.
432 pub struct SliceMotion<'a> {432 pub struct SliceMotion<'a> {
433 /// Slice parameters.433 /// Slice parameters.
@@ -441,7 +441,7 @@ pub struct SliceMotion<'a> {
441 /// The collocated picture, if temporal prediction applies.441 /// The collocated picture, if temporal prediction applies.
442 pub col: Option<Arc<SharedPic>>,442 pub col: Option<Arc<SharedPic>>,
443 /// The picture this slice belongs to, when its rows are published as they are443 /// The picture this slice belongs to, when its rows are published as they are
444- /// decoded (see [`MotionSource::row_done`]).444+ /// decoded (see [`SliceMotion::row_done`]).
445 pub publish: Option<Arc<SharedPic>>,445 pub publish: Option<Arc<SharedPic>>,
446 /// Index of this slice in the picture's slice list.446 /// Index of this slice in the picture's slice list.
447 pub slice: u16,447 pub slice: u16,
@@ -589,8 +589,9 @@ impl<'a> SliceMotion<'a> {
589 }589 }
590 }590 }
591 591
592-impl MotionSource for SliceMotion<'_> {592+impl SliceMotion<'_> {
593- fn row_done(&mut self, pic: &mut PicState, row_end: usize) {593+ /// Called once the CTB row that ends at luma row `row_end` is fully parsed.
594+ pub fn row_done(&mut self, pic: &mut PicState, row_end: usize) {
594 // The CTB row that ended is `row_end - ctb`; its motion is final.595 // The CTB row that ended is `row_end - ctb`; its motion is final.
595 let ctb = 1usize << self.params.ctb_log2;596 let ctb = 1usize << self.params.ctb_log2;
596 let y0 = row_end.saturating_sub(ctb);597 let y0 = row_end.saturating_sub(ctb);
@@ -601,14 +602,20 @@ impl MotionSource for SliceMotion<'_> {
601 }602 }
602 }603 }
603 604
604- fn band_cells(&mut self) -> Option<&mut MotionField> {605+ /// The motion of unit `pu_index` of the coding unit at `(x, y)`, `size` luma samples
605- Some(&mut *self.field)606+ /// square, derived and stored.
606- }607+ pub fn derive_motion(
607-608+ &mut self,
608- fn derive_motion(&mut self, cu: &CuGeometry, pu_index: usize, pu: &PredictionUnit) -> Motion {609+ x: usize,
609- self.field.cur_tile = self.field.tile_at(cu.x, cu.y);610+ y: usize,
611+ size: usize,
612+ part_mode: PartMode,
613+ pu_index: usize,
614+ pu: &PredictionUnit,
615+ ) -> Motion {
616+ self.field.cur_tile = self.field.tile_at(x, y);
610 let derived = if pu.merge_flag {617 let derived = if pu.merge_flag {
611- merge::derive_merge(self, cu, pu_index, pu)618+ merge::derive_merge(self, x, y, size, part_mode, pu_index, pu)
612 } else {619 } else {
613 amvp::derive_amvp(self, pu)620 amvp::derive_amvp(self, pu)
614 };621 };
@@ -636,7 +643,6 @@ pub fn scale_mv(mv: [i32; 2], td: i32, tb: i32) -> [i32; 2] {
636 })643 })
637 }644 }
638 645
639-
640 /// A vector widened to `i32`.646 /// A vector widened to `i32`.
641 pub fn to32(mv: [i16; 2]) -> [i32; 2] {647 pub fn to32(mv: [i16; 2]) -> [i32; 2] {
642 [i32::from(mv[0]), i32::from(mv[1])]648 [i32::from(mv[0]), i32::from(mv[1])]
crates/ec-h265/src/enc_p.rsmodified+2 -9
@@ -22,7 +22,7 @@
22 use crate::cabac::{CabacEncoder, Contexts, ctx};22 use crate::cabac::{CabacEncoder, Contexts, ctx};
23 use crate::ctu::{Pel, lambda_for};23 use crate::ctu::{Pel, lambda_for};
24 use crate::dec::amvp::derive_amvp;24 use crate::dec::amvp::derive_amvp;
25-use crate::dec::cu_syntax::{CuGeometry, PartMode, PredictionUnit};25+use crate::dec::cu_syntax::{PartMode, PredictionUnit};
26 use crate::dec::deblock::{deblock_picture, deblock_rows};26 use crate::dec::deblock::{deblock_picture, deblock_rows};
27 use crate::dec::mc::{Block, McScratch, PlaneMut, PlaneRef, RefPicture, predict_pu};27 use crate::dec::mc::{Block, McScratch, PlaneMut, PlaneRef, RefPicture, predict_pu};
28 use crate::dec::merge::derive_merge;28 use crate::dec::merge::derive_merge;
@@ -905,13 +905,6 @@ impl<'a> PCoder<'a> {
905 ) -> Result<()> {905 ) -> Result<()> {
906 let n = 1usize << log2;906 let n = 1usize << log2;
907 let analysis_mv = analysis.mv;907 let analysis_mv = analysis.mv;
908- let geom = CuGeometry {
909- x,
910- y,
911- size: n,
912- part_mode: PartMode::Part2Nx2N,
913- skip: false,
914- };
915 // Candidate motions: each distinct merge candidate, then AMVP with the908 // Candidate motions: each distinct merge candidate, then AMVP with the
916 // analysed vector under either predictor.909 // analysed vector under either predictor.
917 let mut merge_motions: Vec<(u8, PbMotion)> = Vec::new();910 let mut merge_motions: Vec<(u8, PbMotion)> = Vec::new();
@@ -927,7 +920,7 @@ impl<'a> PCoder<'a> {
927 };920 };
928 let m = {921 let m = {
929 let sm = self.sm();922 let sm = self.sm();
930- derive_merge(&sm, &geom, 0, &pu)923+ derive_merge(&sm, x, y, n, PartMode::Part2Nx2N, 0, &pu)
931 };924 };
932 if merge_motions.iter().all(|(_, prev)| *prev != m) {925 if merge_motions.iter().all(|(_, prev)| *prev != m) {
933 merge_motions.push((idx, m));926 merge_motions.push((idx, m));