Merge branch 'simplify/b4' into simplify/w1a
Browse files at this commit6 changed files +55 -44
- crates/ec-hw/src/dec/mod.rs +3 -1
- crates/ec-hw/src/error.rs +1 -1
- crates/ec-hw/src/hw.rs +18 -11
- crates/ec-vk/src/av1.rs +10 -10
- crates/ec-vk/src/h265.rs +10 -10
- crates/ec-vk/src/lib.rs +13 -11
crates/ec-hw/src/dec/mod.rsmodified+3 -1
| @@ -473,7 +473,9 @@ impl Session { | ||
| 473 | 473 | } |
| 474 | 474 | |
| 475 | 475 | fn poison(&self) { |
| 476 | - self.poisoned.store(true, Ordering::Release); | |
| 476 | + if !self.poisoned.swap(true, Ordering::AcqRel) { | |
| 477 | + crate::reset::report_gpu_reset(); | |
| 478 | + } | |
| 477 | 479 | } |
| 478 | 480 | |
| 479 | 481 | /// Wait for a submitted picture, condemning every session on a reset. |
crates/ec-hw/src/error.rsmodified+1 -1
| @@ -55,7 +55,7 @@ impl Error { | ||
| 55 | 55 | |
| 56 | 56 | /// The decoder was poisoned by a hung or lost GPU context. |
| 57 | 57 | pub fn poisoned(what: impl Into<String>) -> Error { |
| 58 | - Error::Config(format!("hardware decoder poisoned: {}", what.into())) | |
| 58 | + Error::hw_reset(format!("hardware decoder poisoned: {}", what.into())) | |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | /// Build an [`Error::HwReset`]. |
crates/ec-hw/src/hw.rsmodified+18 -11
| @@ -737,12 +737,13 @@ impl HwEncoder { | ||
| 737 | 737 | match &mut self.inner { |
| 738 | 738 | EncInner::Vk(enc) => { |
| 739 | 739 | vk_session_alive(self.born_epoch)?; |
| 740 | + let coded = enc.submit(input, meta.force_keyframe)?; | |
| 740 | 741 | self.pending.push_back(meta.timestamp); |
| 741 | - let Some(coded) = enc.submit(input, meta.force_keyframe)? else { | |
| 742 | + let Some(coded) = coded else { | |
| 742 | 743 | return Ok(None); |
| 743 | 744 | }; |
| 744 | - self.steer(coded.data.len())?; | |
| 745 | 745 | let timestamp = self.pending.pop_front().unwrap_or_default(); |
| 746 | + self.steer(coded.data.len())?; | |
| 746 | 747 | Ok(Some(self.vk_frame(coded, timestamp))) |
| 747 | 748 | } |
| 748 | 749 | #[cfg(target_os = "linux")] |
| @@ -764,12 +765,18 @@ impl HwEncoder { | ||
| 764 | 765 | EncInner::Vk(enc) => { |
| 765 | 766 | vk_session_alive(self.born_epoch)?; |
| 766 | 767 | let frames = enc.flush()?; |
| 767 | - let mut out = Vec::with_capacity(frames.len()); | |
| 768 | - for coded in frames { | |
| 769 | - self.steer(coded.data.len())?; | |
| 770 | - let timestamp = self.pending.pop_front().unwrap_or_default(); | |
| 771 | - out.push(self.vk_frame(coded, timestamp)); | |
| 768 | + let out: Vec<CodedFrame> = frames | |
| 769 | + .into_iter() | |
| 770 | + .map(|coded| { | |
| 771 | + let timestamp = self.pending.pop_front().unwrap_or_default(); | |
| 772 | + self.vk_frame(coded, timestamp) | |
| 773 | + }) | |
| 774 | + .collect(); | |
| 775 | + let mut steered = Ok(()); | |
| 776 | + for frame in &out { | |
| 777 | + steered = steered.and(self.steer(frame.data.len())); | |
| 772 | 778 | } |
| 779 | + steered?; | |
| 773 | 780 | Ok(out) |
| 774 | 781 | } |
| 775 | 782 | #[cfg(target_os = "linux")] |
| @@ -865,6 +872,7 @@ impl HwEncoder { | ||
| 865 | 872 | self.idle()?; |
| 866 | 873 | match &mut self.inner { |
| 867 | 874 | EncInner::Vk(enc) => { |
| 875 | + vk_session_alive(self.born_epoch)?; | |
| 868 | 876 | let coded = enc.encode_16(frame, meta.force_keyframe)?; |
| 869 | 877 | self.steer(coded.data.len())?; |
| 870 | 878 | Ok(self.vk_frame(coded, meta.timestamp)) |
| @@ -1278,12 +1286,11 @@ impl HwDecoder { | ||
| 1278 | 1286 | } |
| 1279 | 1287 | #[cfg(target_os = "linux")] |
| 1280 | 1288 | if let Err(Error::Unsupported { .. }) = result |
| 1281 | - && let Some(fed) = self.replay.take() | |
| 1289 | + && let Some(fed) = &self.replay | |
| 1282 | 1290 | && let Ok(mut va) = open_va_decoder(self.codec) |
| 1291 | + && fed.iter().all(|(chunk, ts)| va.decode(chunk, *ts).is_ok()) | |
| 1283 | 1292 | { |
| 1284 | - for (chunk, ts) in &fed { | |
| 1285 | - va.decode(chunk, *ts)?; | |
| 1286 | - } | |
| 1293 | + self.replay = None; | |
| 1287 | 1294 | self.inner = DecInner::Va(va); |
| 1288 | 1295 | return Ok(()); |
| 1289 | 1296 | } |
crates/ec-vk/src/av1.rsmodified+10 -10
| @@ -100,7 +100,7 @@ impl Av1Encoder { | ||
| 100 | 100 | } |
| 101 | 101 | let pic = self.next_picture(force_keyframe); |
| 102 | 102 | let out = self.session.submit(y, &self.uv, &pic)?; |
| 103 | - Ok(out.map(|bytes| self.coded(bytes))) | |
| 103 | + out.map(|bytes| self.coded(bytes)).transpose() | |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | /// Encodes one I420 frame as a temporal unit; `force_keyframe` makes it a KEY frame and restarts the GOP. |
| @@ -134,7 +134,7 @@ impl Av1Encoder { | ||
| 134 | 134 | } |
| 135 | 135 | let pic = self.next_picture(force_keyframe); |
| 136 | 136 | let out = self.session.submit_16(y, &self.uv16, &pic)?; |
| 137 | - Ok(out.map(|bytes| self.coded(bytes))) | |
| 137 | + out.map(|bytes| self.coded(bytes)).transpose() | |
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | /// Encodes one 10-bit I420 frame of a 10-bit encoder; `force_keyframe` makes it a KEY frame and restarts the GOP. |
| @@ -153,7 +153,7 @@ impl Av1Encoder { | ||
| 153 | 153 | /// Waits for every submitted frame and returns them in order. |
| 154 | 154 | pub fn flush(&mut self) -> Result<Vec<CodedFrame>, Error> { |
| 155 | 155 | let bytes = self.session.flush()?; |
| 156 | - Ok(bytes.into_iter().map(|b| self.coded(b)).collect()) | |
| 156 | + bytes.into_iter().map(|b| self.coded(b)).collect() | |
| 157 | 157 | } |
| 158 | 158 | |
| 159 | 159 | /// Changes bitrate or frame rate; takes effect on the next frame. |
| @@ -175,7 +175,7 @@ impl Av1Encoder { | ||
| 175 | 175 | let out = self |
| 176 | 176 | .session |
| 177 | 177 | .submit_rgb(rgb, src_width, src_height, stride_bytes, bgr, &pic)?; |
| 178 | - Ok(out.map(|bytes| self.coded(bytes))) | |
| 178 | + out.map(|bytes| self.coded(bytes)).transpose() | |
| 179 | 179 | } |
| 180 | 180 | |
| 181 | 181 | /// Encodes one packed RGB frame (src_width x src_height, BGRx if `bgr`, else RGBx; row stride in bytes), box-downscaled to the encoder size on the GPU. |
| @@ -202,7 +202,7 @@ impl Av1Encoder { | ||
| 202 | 202 | ) -> Result<Option<CodedFrame>, Error> { |
| 203 | 203 | let pic = self.next_picture(force_keyframe); |
| 204 | 204 | let out = self.session.submit_d3d11(src, &pic)?; |
| 205 | - Ok(out.map(|bytes| self.coded(bytes))) | |
| 205 | + out.map(|bytes| self.coded(bytes)).transpose() | |
| 206 | 206 | } |
| 207 | 207 | |
| 208 | 208 | /// Encodes one shared D3D11 texture of the source size (BGRA if `bgr`, else RGBA), downscaled to the encoder size on the GPU. |
| @@ -226,7 +226,7 @@ impl Av1Encoder { | ||
| 226 | 226 | ) -> Result<Option<CodedFrame>, Error> { |
| 227 | 227 | let pic = self.next_picture(force_keyframe); |
| 228 | 228 | let out = self.session.submit_dmabuf(src, &pic)?; |
| 229 | - Ok(out.map(|bytes| self.coded(bytes))) | |
| 229 | + out.map(|bytes| self.coded(bytes)).transpose() | |
| 230 | 230 | } |
| 231 | 231 | |
| 232 | 232 | /// Encodes one DMA-BUF frame of the source size (BGRx if `bgr`, else RGBx), box-downscaled to the encoder size on the GPU. |
| @@ -275,18 +275,18 @@ impl Av1Encoder { | ||
| 275 | 275 | self.history.arm(); |
| 276 | 276 | } |
| 277 | 277 | |
| 278 | - fn coded(&mut self, bytes: Vec<u8>) -> CodedFrame { | |
| 279 | - let meta = self.flight.pop(); | |
| 278 | + fn coded(&mut self, bytes: Vec<u8>) -> Result<CodedFrame, Error> { | |
| 279 | + let meta = self.flight.pop()?; | |
| 280 | 280 | let mut data = TEMPORAL_DELIMITER.to_vec(); |
| 281 | 281 | if meta.keyframe { |
| 282 | 282 | data.extend_from_slice(&self.sequence_header); |
| 283 | 283 | } |
| 284 | 284 | data.extend_from_slice(&bytes); |
| 285 | - CodedFrame { | |
| 285 | + Ok(CodedFrame { | |
| 286 | 286 | data, |
| 287 | 287 | keyframe: meta.keyframe, |
| 288 | 288 | recovery: meta.recovery, |
| 289 | 289 | id: meta.id, |
| 290 | - } | |
| 290 | + }) | |
| 291 | 291 | } |
| 292 | 292 | } |
crates/ec-vk/src/h265.rsmodified+10 -10
| @@ -98,7 +98,7 @@ impl H265Encoder { | ||
| 98 | 98 | } |
| 99 | 99 | let pic = self.next_picture(force_keyframe); |
| 100 | 100 | let out = self.session.submit(y, &self.uv, &pic)?; |
| 101 | - Ok(out.map(|bytes| self.coded(bytes))) | |
| 101 | + out.map(|bytes| self.coded(bytes)).transpose() | |
| 102 | 102 | } |
| 103 | 103 | |
| 104 | 104 | /// Encodes one I420 frame; `force_keyframe` makes it an IDR and restarts the GOP. |
| @@ -132,7 +132,7 @@ impl H265Encoder { | ||
| 132 | 132 | } |
| 133 | 133 | let pic = self.next_picture(force_keyframe); |
| 134 | 134 | let out = self.session.submit_16(y, &self.uv16, &pic)?; |
| 135 | - Ok(out.map(|bytes| self.coded(bytes))) | |
| 135 | + out.map(|bytes| self.coded(bytes)).transpose() | |
| 136 | 136 | } |
| 137 | 137 | |
| 138 | 138 | /// Encodes one 10-bit I420 frame of a 10-bit encoder; `force_keyframe` makes it IDR and restarts the GOP. |
| @@ -151,7 +151,7 @@ impl H265Encoder { | ||
| 151 | 151 | /// Waits for every submitted frame and returns them in order. |
| 152 | 152 | pub fn flush(&mut self) -> Result<Vec<CodedFrame>, Error> { |
| 153 | 153 | let bytes = self.session.flush()?; |
| 154 | - Ok(bytes.into_iter().map(|b| self.coded(b)).collect()) | |
| 154 | + bytes.into_iter().map(|b| self.coded(b)).collect() | |
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | /// Changes bitrate or frame rate; takes effect on the next frame. |
| @@ -173,7 +173,7 @@ impl H265Encoder { | ||
| 173 | 173 | let out = self |
| 174 | 174 | .session |
| 175 | 175 | .submit_rgb(rgb, src_width, src_height, stride_bytes, bgr, &pic)?; |
| 176 | - Ok(out.map(|bytes| self.coded(bytes))) | |
| 176 | + out.map(|bytes| self.coded(bytes)).transpose() | |
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | /// Encodes one packed RGB frame (src_width x src_height, BGRx if `bgr`, else RGBx; row stride in bytes), box-downscaled to the encoder size on the GPU. |
| @@ -200,7 +200,7 @@ impl H265Encoder { | ||
| 200 | 200 | ) -> Result<Option<CodedFrame>, Error> { |
| 201 | 201 | let pic = self.next_picture(force_keyframe); |
| 202 | 202 | let out = self.session.submit_d3d11(src, &pic)?; |
| 203 | - Ok(out.map(|bytes| self.coded(bytes))) | |
| 203 | + out.map(|bytes| self.coded(bytes)).transpose() | |
| 204 | 204 | } |
| 205 | 205 | |
| 206 | 206 | /// Encodes one shared D3D11 texture of the source size (BGRA if `bgr`, else RGBA), downscaled to the encoder size on the GPU. |
| @@ -224,7 +224,7 @@ impl H265Encoder { | ||
| 224 | 224 | ) -> Result<Option<CodedFrame>, Error> { |
| 225 | 225 | let pic = self.next_picture(force_keyframe); |
| 226 | 226 | let out = self.session.submit_dmabuf(src, &pic)?; |
| 227 | - Ok(out.map(|bytes| self.coded(bytes))) | |
| 227 | + out.map(|bytes| self.coded(bytes)).transpose() | |
| 228 | 228 | } |
| 229 | 229 | |
| 230 | 230 | /// Encodes one DMA-BUF frame of the source size (BGRx if `bgr`, else RGBx), box-downscaled to the encoder size on the GPU. |
| @@ -271,18 +271,18 @@ impl H265Encoder { | ||
| 271 | 271 | self.history.arm(); |
| 272 | 272 | } |
| 273 | 273 | |
| 274 | - fn coded(&mut self, bytes: Vec<u8>) -> CodedFrame { | |
| 275 | - let meta = self.flight.pop(); | |
| 274 | + fn coded(&mut self, bytes: Vec<u8>) -> Result<CodedFrame, Error> { | |
| 275 | + let meta = self.flight.pop()?; | |
| 276 | 276 | let data = if meta.keyframe { |
| 277 | 277 | [self.parameter_sets.as_slice(), bytes.as_slice()].concat() |
| 278 | 278 | } else { |
| 279 | 279 | bytes |
| 280 | 280 | }; |
| 281 | - CodedFrame { | |
| 281 | + Ok(CodedFrame { | |
| 282 | 282 | data, |
| 283 | 283 | keyframe: meta.keyframe, |
| 284 | 284 | recovery: meta.recovery, |
| 285 | 285 | id: meta.id, |
| 286 | - } | |
| 286 | + }) | |
| 287 | 287 | } |
| 288 | 288 | } |
crates/ec-vk/src/lib.rsmodified+13 -11
| @@ -81,8 +81,10 @@ impl Flight { | ||
| 81 | 81 | self.0.push_back(meta); |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | - pub(crate) fn pop(&mut self) -> Meta { | |
| 85 | - self.0.pop_front().unwrap_or_default() | |
| 84 | + pub(crate) fn pop(&mut self) -> Result<Meta, Error> { | |
| 85 | + self.0 | |
| 86 | + .pop_front() | |
| 87 | + .ok_or_else(|| Error::Invalid("frame queue out of step".into())) | |
| 86 | 88 | } |
| 87 | 89 | } |
| 88 | 90 | |
| @@ -166,7 +168,7 @@ impl H264Encoder { | ||
| 166 | 168 | } |
| 167 | 169 | let pic = self.next_picture(force_keyframe); |
| 168 | 170 | let out = self.session.submit(y, &self.uv, &pic)?; |
| 169 | - Ok(out.map(|bytes| self.coded(bytes))) | |
| 171 | + out.map(|bytes| self.coded(bytes)).transpose() | |
| 170 | 172 | } |
| 171 | 173 | |
| 172 | 174 | /// Encodes one I420 frame; `force_keyframe` makes it an IDR and restarts the GOP. |
| @@ -185,7 +187,7 @@ impl H264Encoder { | ||
| 185 | 187 | /// Waits for every submitted frame and returns them in order. |
| 186 | 188 | pub fn flush(&mut self) -> Result<Vec<CodedFrame>, Error> { |
| 187 | 189 | let bytes = self.session.flush()?; |
| 188 | - Ok(bytes.into_iter().map(|b| self.coded(b)).collect()) | |
| 190 | + bytes.into_iter().map(|b| self.coded(b)).collect() | |
| 189 | 191 | } |
| 190 | 192 | |
| 191 | 193 | /// Changes bitrate or frame rate; takes effect on the next frame. |
| @@ -207,7 +209,7 @@ impl H264Encoder { | ||
| 207 | 209 | let out = self |
| 208 | 210 | .session |
| 209 | 211 | .submit_rgb(rgb, src_width, src_height, stride_bytes, bgr, &pic)?; |
| 210 | - Ok(out.map(|bytes| self.coded(bytes))) | |
| 212 | + out.map(|bytes| self.coded(bytes)).transpose() | |
| 211 | 213 | } |
| 212 | 214 | |
| 213 | 215 | /// Encodes one packed RGB frame (src_width x src_height, BGRx if `bgr`, else RGBx; row stride in bytes), box-downscaled to the encoder size on the GPU. |
| @@ -234,7 +236,7 @@ impl H264Encoder { | ||
| 234 | 236 | ) -> Result<Option<CodedFrame>, Error> { |
| 235 | 237 | let pic = self.next_picture(force_keyframe); |
| 236 | 238 | let out = self.session.submit_d3d11(src, &pic)?; |
| 237 | - Ok(out.map(|bytes| self.coded(bytes))) | |
| 239 | + out.map(|bytes| self.coded(bytes)).transpose() | |
| 238 | 240 | } |
| 239 | 241 | |
| 240 | 242 | /// Encodes one shared D3D11 texture of the source size (BGRA if `bgr`, else RGBA), downscaled to the encoder size on the GPU. |
| @@ -258,7 +260,7 @@ impl H264Encoder { | ||
| 258 | 260 | ) -> Result<Option<CodedFrame>, Error> { |
| 259 | 261 | let pic = self.next_picture(force_keyframe); |
| 260 | 262 | let out = self.session.submit_dmabuf(src, &pic)?; |
| 261 | - Ok(out.map(|bytes| self.coded(bytes))) | |
| 263 | + out.map(|bytes| self.coded(bytes)).transpose() | |
| 262 | 264 | } |
| 263 | 265 | |
| 264 | 266 | /// Encodes one DMA-BUF frame of the source size (BGRx if `bgr`, else RGBx), box-downscaled to the encoder size on the GPU. |
| @@ -315,18 +317,18 @@ impl H264Encoder { | ||
| 315 | 317 | self.history.arm(); |
| 316 | 318 | } |
| 317 | 319 | |
| 318 | - fn coded(&mut self, bytes: Vec<u8>) -> CodedFrame { | |
| 319 | - let meta = self.flight.pop(); | |
| 320 | + fn coded(&mut self, bytes: Vec<u8>) -> Result<CodedFrame, Error> { | |
| 321 | + let meta = self.flight.pop()?; | |
| 320 | 322 | let data = if meta.keyframe { |
| 321 | 323 | [self.parameter_sets.as_slice(), bytes.as_slice()].concat() |
| 322 | 324 | } else { |
| 323 | 325 | bytes |
| 324 | 326 | }; |
| 325 | - CodedFrame { | |
| 327 | + Ok(CodedFrame { | |
| 326 | 328 | data, |
| 327 | 329 | keyframe: meta.keyframe, |
| 328 | 330 | recovery: meta.recovery, |
| 329 | 331 | id: meta.id, |
| 330 | - } | |
| 332 | + }) | |
| 331 | 333 | } |
| 332 | 334 | } |