B4: ec-vk Flight::pop returns Err on empty queue, not a default Meta
Browse files at this commitdf1946ca404f2a0d9ccf38ccd93dd1738a8b78ccParent: 22f0f3f3 changed files +33 -31
- 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-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 | } |