| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | use std::sync::{Condvar, Mutex, MutexGuard, RwLock, RwLockReadGuard}; |
| 20 | |
| 21 | use super::motion_field::{ColField, MotionField}; |
| 22 | use crate::decode::DecodedPicture; |
| 23 | use ec_core::error::{Error, Result}; |
| 24 | |
| 25 | |
| 26 | #[derive(Debug, Default)] |
| 27 | struct Progress { |
| 28 | |
| 29 | rows: usize, |
| 30 | |
| 31 | col_rows: usize, |
| 32 | |
| 33 | done: bool, |
| 34 | |
| 35 | error: Option<String>, |
| 36 | } |
| 37 | |
| 38 | |
| 39 | #[derive(Debug)] |
| 40 | pub struct SharedPic { |
| 41 | |
| 42 | pub width: usize, |
| 43 | |
| 44 | pub height: usize, |
| 45 | planes: RwLock<DecodedPicture>, |
| 46 | col: RwLock<ColField>, |
| 47 | progress: Mutex<Progress>, |
| 48 | changed: Condvar, |
| 49 | } |
| 50 | |
| 51 | fn lock<T>(m: &Mutex<T>) -> MutexGuard<'_, T> { |
| 52 | |
| 53 | |
| 54 | m.lock().unwrap_or_else(|e| e.into_inner()) |
| 55 | } |
| 56 | |
| 57 | impl SharedPic { |
| 58 | |
| 59 | |
| 60 | pub fn new( |
| 61 | poc: i32, |
| 62 | width: usize, |
| 63 | height: usize, |
| 64 | bit_depth: (u32, u32), |
| 65 | pixels: bool, |
| 66 | chroma_format_idc: u32, |
| 67 | ) -> SharedPic { |
| 68 | let planes = if pixels { |
| 69 | let (sx, sy) = crate::decode::chroma_shifts(chroma_format_idc); |
| 70 | let cw = width.div_ceil(1 << sx); |
| 71 | let ch = height.div_ceil(1 << sy); |
| 72 | DecodedPicture { |
| 73 | width, |
| 74 | height, |
| 75 | bit_depth: bit_depth.0, |
| 76 | bit_depth_c: bit_depth.1, |
| 77 | chroma_format_idc, |
| 78 | y: vec![0; width * height], |
| 79 | cb: vec![0; cw * ch], |
| 80 | cr: vec![0; cw * ch], |
| 81 | } |
| 82 | } else { |
| 83 | DecodedPicture { |
| 84 | width, |
| 85 | height, |
| 86 | bit_depth: 8, |
| 87 | bit_depth_c: 8, |
| 88 | chroma_format_idc, |
| 89 | y: Vec::new(), |
| 90 | cb: Vec::new(), |
| 91 | cr: Vec::new(), |
| 92 | } |
| 93 | }; |
| 94 | SharedPic { |
| 95 | width, |
| 96 | height, |
| 97 | planes: RwLock::new(planes), |
| 98 | col: RwLock::new(ColField::new(poc, width, height)), |
| 99 | progress: Mutex::new(Progress::default()), |
| 100 | changed: Condvar::new(), |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | |
| 105 | pub fn from_planes(poc: i32, planes: DecodedPicture) -> SharedPic { |
| 106 | let (width, height) = (planes.width, planes.height); |
| 107 | SharedPic { |
| 108 | width, |
| 109 | height, |
| 110 | planes: RwLock::new(planes), |
| 111 | col: RwLock::new(ColField::new(poc, width, height)), |
| 112 | progress: Mutex::new(Progress { |
| 113 | rows: height, |
| 114 | col_rows: height, |
| 115 | done: true, |
| 116 | error: None, |
| 117 | }), |
| 118 | changed: Condvar::new(), |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | |
| 123 | |
| 124 | pub fn wait_rows(&self, rows: usize) -> Result<()> { |
| 125 | let t = super::trace::now(); |
| 126 | let r = self.wait_for(rows, |p| p.rows); |
| 127 | super::trace::since(super::trace::Phase::WaitRows, t); |
| 128 | r |
| 129 | } |
| 130 | |
| 131 | |
| 132 | pub fn wait_col_rows(&self, rows: usize) -> Result<()> { |
| 133 | self.wait_for(rows, |p| p.col_rows) |
| 134 | } |
| 135 | |
| 136 | |
| 137 | pub fn snapshot(&self) -> Result<std::sync::Arc<DecodedPicture>> { |
| 138 | self.wait_rows(usize::MAX)?; |
| 139 | let planes = self.planes.read().unwrap_or_else(|e| e.into_inner()); |
| 140 | let mut out = planes.clone(); |
| 141 | if out.chroma_format_idc == 0 { |
| 142 | |
| 143 | out.cb = Vec::new(); |
| 144 | out.cr = Vec::new(); |
| 145 | } |
| 146 | Ok(std::sync::Arc::new(out)) |
| 147 | } |
| 148 | |
| 149 | |
| 150 | |
| 151 | pub fn read_planes(&self) -> RwLockReadGuard<'_, DecodedPicture> { |
| 152 | self.planes.read().unwrap_or_else(|e| e.into_inner()) |
| 153 | } |
| 154 | |
| 155 | |
| 156 | pub fn read_col(&self) -> RwLockReadGuard<'_, ColField> { |
| 157 | self.col.read().unwrap_or_else(|e| e.into_inner()) |
| 158 | } |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | pub fn publish_rows(&self, src: &DecodedPicture, to: usize) { |
| 164 | let from = lock(&self.progress).rows; |
| 165 | if to <= from { |
| 166 | return; |
| 167 | } |
| 168 | if !src.y.is_empty() { |
| 169 | let w = self.width; |
| 170 | let mut planes = self.planes.write().unwrap_or_else(|e| e.into_inner()); |
| 171 | let (sx, sy) = planes.chroma_shifts(); |
| 172 | let cw = w.div_ceil(1 << sx); |
| 173 | let (cf, ct) = (from >> sy, to.div_ceil(1 << sy)); |
| 174 | planes.y[from * w..to * w].copy_from_slice(&src.y[from * w..to * w]); |
| 175 | planes.cb[cf * cw..ct * cw].copy_from_slice(&src.cb[cf * cw..ct * cw]); |
| 176 | planes.cr[cf * cw..ct * cw].copy_from_slice(&src.cr[cf * cw..ct * cw]); |
| 177 | } |
| 178 | self.advance(|p| p.rows = to); |
| 179 | } |
| 180 | |
| 181 | |
| 182 | |
| 183 | pub fn publish_col(&self, field: &MotionField, to: usize) { |
| 184 | let from = lock(&self.progress).col_rows; |
| 185 | if to <= from { |
| 186 | return; |
| 187 | } |
| 188 | self.col |
| 189 | .write() |
| 190 | .unwrap_or_else(|e| e.into_inner()) |
| 191 | .fill_rows(field, from, to); |
| 192 | self.advance(|p| p.col_rows = to); |
| 193 | } |
| 194 | |
| 195 | |
| 196 | pub fn complete(&self) { |
| 197 | self.advance(|p| { |
| 198 | p.rows = self.height; |
| 199 | p.col_rows = self.height; |
| 200 | p.done = true; |
| 201 | }); |
| 202 | } |
| 203 | |
| 204 | |
| 205 | pub fn fail(&self, message: &str) { |
| 206 | self.advance(|p| { |
| 207 | p.done = true; |
| 208 | if p.error.is_none() { |
| 209 | p.error = Some(message.to_string()); |
| 210 | } |
| 211 | }); |
| 212 | } |
| 213 | |
| 214 | |
| 215 | pub fn wait_done(&self) -> Result<()> { |
| 216 | self.wait_rows(usize::MAX) |
| 217 | } |
| 218 | |
| 219 | fn advance(&self, f: impl FnOnce(&mut Progress)) { |
| 220 | let mut p = lock(&self.progress); |
| 221 | f(&mut p); |
| 222 | drop(p); |
| 223 | self.changed.notify_all(); |
| 224 | } |
| 225 | |
| 226 | fn wait_for(&self, want: usize, got: impl Fn(&Progress) -> usize) -> Result<()> { |
| 227 | let mut p = lock(&self.progress); |
| 228 | loop { |
| 229 | if let Some(e) = &p.error { |
| 230 | return Err(Error::corrupt(format!("HEVC decode: {e}"))); |
| 231 | } |
| 232 | if p.done || got(&p) >= want { |
| 233 | return Ok(()); |
| 234 | } |
| 235 | p = self.changed.wait(p).unwrap_or_else(|e| e.into_inner()); |
| 236 | } |
| 237 | } |
| 238 | } |