| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | use super::cu_syntax::{Motion, ResidualBlock}; |
| 10 | use super::mc::{self, Block, McScratch, PlaneMut, PlaneRef, RefPicture}; |
| 11 | use super::pic::PicState; |
| 12 | use super::shared::SharedPic; |
| 13 | use super::weighted::ExplicitWeights; |
| 14 | use crate::decode::DecodedPicture; |
| 15 | use ec_core::error::Result; |
| 16 | use ec_h265_syntax::Sps; |
| 17 | use ec_h265_syntax::slice::SliceHeader; |
| 18 | use std::sync::{Arc, RwLockReadGuard}; |
| 19 | |
| 20 | |
| 21 | pub struct InterRefs { |
| 22 | |
| 23 | |
| 24 | pub pics: [Vec<Arc<SharedPic>>; 2], |
| 25 | |
| 26 | pub weights: Option<ExplicitWeights>, |
| 27 | |
| 28 | pub sub: (u32, u32), |
| 29 | } |
| 30 | |
| 31 | impl InterRefs { |
| 32 | |
| 33 | pub fn new( |
| 34 | pics: [Vec<Arc<SharedPic>>; 2], |
| 35 | header: &SliceHeader, |
| 36 | sps: &Sps, |
| 37 | ) -> Result<InterRefs> { |
| 38 | let weights = match header.pred_weight_table.as_ref() { |
| 39 | Some(table) => Some(ExplicitWeights::from_table( |
| 40 | table, |
| 41 | sps.bit_depth_luma_minus8 + 8, |
| 42 | sps.bit_depth_chroma_minus8 + 8, |
| 43 | sps.range.high_precision_offsets_enabled, |
| 44 | )?), |
| 45 | None => None, |
| 46 | }; |
| 47 | Ok(InterRefs { |
| 48 | pics, |
| 49 | weights, |
| 50 | sub: crate::decode::chroma_shifts(sps.chroma_format_idc), |
| 51 | }) |
| 52 | } |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | pub fn predict( |
| 59 | &self, |
| 60 | motion: &Motion, |
| 61 | pu: Block, |
| 62 | out: &mut [PlaneMut<'_>; 3], |
| 63 | scratch: &mut McScratch, |
| 64 | ) -> Result<()> { |
| 65 | for list in 0..2 { |
| 66 | if let Some(pic) = self.picture(motion, list) { |
| 67 | |
| 68 | |
| 69 | |
| 70 | let bottom = (pu.y + pu.height) as i64 + (i64::from(motion.mv[list][1]) >> 2) + 16; |
| 71 | pic.wait_rows(bottom.clamp(1, pic.height as i64) as usize)?; |
| 72 | } |
| 73 | } |
| 74 | let pics = [self.picture(motion, 0), self.picture(motion, 1)]; |
| 75 | let guards = lock_ref_planes(pics, || {}); |
| 76 | |
| 77 | let same = matches!(pics, [Some(a), Some(b)] if std::ptr::eq(a, b)); |
| 78 | let planes = [ |
| 79 | guards[0].as_deref(), |
| 80 | if same { |
| 81 | guards[0].as_deref() |
| 82 | } else { |
| 83 | guards[1].as_deref() |
| 84 | }, |
| 85 | ]; |
| 86 | |
| 87 | |
| 88 | let mut lists: [Vec<RefPicture<'_>>; 2] = [Vec::new(), Vec::new()]; |
| 89 | for list in 0..2 { |
| 90 | if let (Some(pic), Some(idx)) = (planes[list], self.index(motion, list)) { |
| 91 | lists[list] = vec![ref_picture(pic); idx + 1]; |
| 92 | } |
| 93 | } |
| 94 | mc::predict_pu_fmt( |
| 95 | [lists[0].as_slice(), lists[1].as_slice()], |
| 96 | motion, |
| 97 | self.weights.as_ref(), |
| 98 | pu, |
| 99 | self.sub, |
| 100 | out, |
| 101 | scratch, |
| 102 | ) |
| 103 | } |
| 104 | |
| 105 | |
| 106 | fn index(&self, motion: &Motion, list: usize) -> Option<usize> { |
| 107 | if !motion.pred_flags[list] { |
| 108 | return None; |
| 109 | } |
| 110 | usize::try_from(motion.ref_idx[list]) |
| 111 | .ok() |
| 112 | .filter(|&i| i < self.pics[list].len()) |
| 113 | } |
| 114 | |
| 115 | |
| 116 | fn picture(&self, motion: &Motion, list: usize) -> Option<&SharedPic> { |
| 117 | self.index(motion, list).map(|i| &*self.pics[list][i]) |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | fn lock_ref_planes<'a>( |
| 126 | pics: [Option<&'a SharedPic>; 2], |
| 127 | between: impl FnOnce(), |
| 128 | ) -> [Option<RwLockReadGuard<'a, DecodedPicture>>; 2] { |
| 129 | let same = matches!(pics, [Some(a), Some(b)] if std::ptr::eq(a, b)); |
| 130 | let first = pics[0].map(|p| p.read_planes()); |
| 131 | between(); |
| 132 | let second = match pics[1] { |
| 133 | Some(p) if !same => Some(p.read_planes()), |
| 134 | _ => None, |
| 135 | }; |
| 136 | [first, second] |
| 137 | } |
| 138 | |
| 139 | |
| 140 | fn ref_picture(pic: &DecodedPicture) -> RefPicture<'_> { |
| 141 | let (sx, sy) = pic.chroma_shifts(); |
| 142 | let (cw, ch) = (pic.width.div_ceil(1 << sx), pic.height.div_ceil(1 << sy)); |
| 143 | [ |
| 144 | PlaneRef { |
| 145 | samples: &pic.y, |
| 146 | stride: pic.width, |
| 147 | width: pic.width, |
| 148 | height: pic.height, |
| 149 | bit_depth: pic.bit_depth, |
| 150 | }, |
| 151 | PlaneRef { |
| 152 | samples: &pic.cb, |
| 153 | stride: cw, |
| 154 | width: cw, |
| 155 | height: ch, |
| 156 | bit_depth: pic.bit_depth_c, |
| 157 | }, |
| 158 | PlaneRef { |
| 159 | samples: &pic.cr, |
| 160 | stride: cw, |
| 161 | width: cw, |
| 162 | height: ch, |
| 163 | bit_depth: pic.bit_depth_c, |
| 164 | }, |
| 165 | ] |
| 166 | } |
| 167 | |
| 168 | |
| 169 | |
| 170 | pub fn add_residuals(pic: &mut PicState, residuals: &[ResidualBlock]) { |
| 171 | if !pic.has_pixels() { |
| 172 | return; |
| 173 | } |
| 174 | for block in residuals { |
| 175 | pic.add_residual( |
| 176 | block.component, |
| 177 | block.x, |
| 178 | block.y, |
| 179 | block.size, |
| 180 | &block.samples, |
| 181 | ); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | #[cfg(test)] |
| 186 | mod tests { |
| 187 | use super::*; |
| 188 | use std::sync::mpsc; |
| 189 | use std::time::Duration; |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | #[test] |
| 195 | fn shared_reference_in_both_lists_locks_once() { |
| 196 | let (tx, rx) = mpsc::channel(); |
| 197 | std::thread::spawn(move || { |
| 198 | let (w, h) = (64usize, 64usize); |
| 199 | let pic = Arc::new(SharedPic::new(0, w, h, (8, 8), true, 1)); |
| 200 | let src = DecodedPicture { |
| 201 | width: w, |
| 202 | height: h, |
| 203 | bit_depth: 8, |
| 204 | bit_depth_c: 8, |
| 205 | chroma_format_idc: 1, |
| 206 | y: vec![0; w * h], |
| 207 | cb: vec![0; (w / 2) * (h / 2)], |
| 208 | cr: vec![0; (w / 2) * (h / 2)], |
| 209 | }; |
| 210 | let writer = { |
| 211 | let pic = Arc::clone(&pic); |
| 212 | let src = src.clone(); |
| 213 | move || { |
| 214 | for to in 1..=h { |
| 215 | pic.publish_rows(&src, to); |
| 216 | } |
| 217 | } |
| 218 | }; |
| 219 | let mut writer_thread = None; |
| 220 | let guards = lock_ref_planes([Some(&*pic), Some(&*pic)], || { |
| 221 | |
| 222 | writer_thread = Some(std::thread::spawn(writer.clone())); |
| 223 | std::thread::sleep(Duration::from_millis(100)); |
| 224 | }); |
| 225 | drop(guards); |
| 226 | if let Some(t) = writer_thread { |
| 227 | t.join().expect("writer"); |
| 228 | } |
| 229 | let _ = tx.send(()); |
| 230 | }); |
| 231 | rx.recv_timeout(Duration::from_secs(20)) |
| 232 | .expect("prediction deadlocked: a reference picture was read-locked twice"); |
| 233 | } |
| 234 | } |