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 | let pic = self.next_picture(force_keyframe); | 101 | let pic = self.next_picture(force_keyframe); |
| 102 | let out = self.session.submit(y, &self.uv, &pic)?; | 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 | /// Encodes one I420 frame as a temporal unit; `force_keyframe` makes it a KEY frame and restarts the GOP. | 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 | let pic = self.next_picture(force_keyframe); | 135 | let pic = self.next_picture(force_keyframe); |
| 136 | let out = self.session.submit_16(y, &self.uv16, &pic)?; | 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 | /// Encodes one 10-bit I420 frame of a 10-bit encoder; `force_keyframe` makes it a KEY frame and restarts the GOP. | 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 | /// Waits for every submitted frame and returns them in order. | 153 | /// Waits for every submitted frame and returns them in order. |
| 154 | pub fn flush(&mut self) -> Result<Vec<CodedFrame>, Error> { | 154 | pub fn flush(&mut self) -> Result<Vec<CodedFrame>, Error> { |
| 155 | let bytes = self.session.flush()?; | 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 | /// Changes bitrate or frame rate; takes effect on the next frame. | 159 | /// Changes bitrate or frame rate; takes effect on the next frame. |
| @@ -175,7 +175,7 @@ impl Av1Encoder { | |||
| 175 | let out = self | 175 | let out = self |
| 176 | .session | 176 | .session |
| 177 | .submit_rgb(rgb, src_width, src_height, stride_bytes, bgr, &pic)?; | 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 | /// 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. | 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 | ) -> Result<Option<CodedFrame>, Error> { | 202 | ) -> Result<Option<CodedFrame>, Error> { |
| 203 | let pic = self.next_picture(force_keyframe); | 203 | let pic = self.next_picture(force_keyframe); |
| 204 | let out = self.session.submit_d3d11(src, &pic)?; | 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 | /// Encodes one shared D3D11 texture of the source size (BGRA if `bgr`, else RGBA), downscaled to the encoder size on the GPU. | 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 | ) -> Result<Option<CodedFrame>, Error> { | 226 | ) -> Result<Option<CodedFrame>, Error> { |
| 227 | let pic = self.next_picture(force_keyframe); | 227 | let pic = self.next_picture(force_keyframe); |
| 228 | let out = self.session.submit_dmabuf(src, &pic)?; | 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 | /// Encodes one DMA-BUF frame of the source size (BGRx if `bgr`, else RGBx), box-downscaled to the encoder size on the GPU. | 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 | self.history.arm(); | 275 | self.history.arm(); |
| 276 | } | 276 | } |
| 277 | 277 | ||
| 278 | - fn coded(&mut self, bytes: Vec<u8>) -> CodedFrame { | 278 | + fn coded(&mut self, bytes: Vec<u8>) -> Result<CodedFrame, Error> { |
| 279 | - let meta = self.flight.pop(); | 279 | + let meta = self.flight.pop()?; |
| 280 | let mut data = TEMPORAL_DELIMITER.to_vec(); | 280 | let mut data = TEMPORAL_DELIMITER.to_vec(); |
| 281 | if meta.keyframe { | 281 | if meta.keyframe { |
| 282 | data.extend_from_slice(&self.sequence_header); | 282 | data.extend_from_slice(&self.sequence_header); |
| 283 | } | 283 | } |
| 284 | data.extend_from_slice(&bytes); | 284 | data.extend_from_slice(&bytes); |
| 285 | - CodedFrame { | 285 | + Ok(CodedFrame { |
| 286 | data, | 286 | data, |
| 287 | keyframe: meta.keyframe, | 287 | keyframe: meta.keyframe, |
| 288 | recovery: meta.recovery, | 288 | recovery: meta.recovery, |
| 289 | id: meta.id, | 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 | let pic = self.next_picture(force_keyframe); | 99 | let pic = self.next_picture(force_keyframe); |
| 100 | let out = self.session.submit(y, &self.uv, &pic)?; | 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 | /// Encodes one I420 frame; `force_keyframe` makes it an IDR and restarts the GOP. | 104 | /// Encodes one I420 frame; `force_keyframe` makes it an IDR and restarts the GOP. |
| @@ -132,7 +132,7 @@ impl H265Encoder { | |||
| 132 | } | 132 | } |
| 133 | let pic = self.next_picture(force_keyframe); | 133 | let pic = self.next_picture(force_keyframe); |
| 134 | let out = self.session.submit_16(y, &self.uv16, &pic)?; | 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 | /// Encodes one 10-bit I420 frame of a 10-bit encoder; `force_keyframe` makes it IDR and restarts the GOP. | 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 | /// Waits for every submitted frame and returns them in order. | 151 | /// Waits for every submitted frame and returns them in order. |
| 152 | pub fn flush(&mut self) -> Result<Vec<CodedFrame>, Error> { | 152 | pub fn flush(&mut self) -> Result<Vec<CodedFrame>, Error> { |
| 153 | let bytes = self.session.flush()?; | 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 | /// Changes bitrate or frame rate; takes effect on the next frame. | 157 | /// Changes bitrate or frame rate; takes effect on the next frame. |
| @@ -173,7 +173,7 @@ impl H265Encoder { | |||
| 173 | let out = self | 173 | let out = self |
| 174 | .session | 174 | .session |
| 175 | .submit_rgb(rgb, src_width, src_height, stride_bytes, bgr, &pic)?; | 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 | /// 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. | 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 | ) -> Result<Option<CodedFrame>, Error> { | 200 | ) -> Result<Option<CodedFrame>, Error> { |
| 201 | let pic = self.next_picture(force_keyframe); | 201 | let pic = self.next_picture(force_keyframe); |
| 202 | let out = self.session.submit_d3d11(src, &pic)?; | 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 | /// Encodes one shared D3D11 texture of the source size (BGRA if `bgr`, else RGBA), downscaled to the encoder size on the GPU. | 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 | ) -> Result<Option<CodedFrame>, Error> { | 224 | ) -> Result<Option<CodedFrame>, Error> { |
| 225 | let pic = self.next_picture(force_keyframe); | 225 | let pic = self.next_picture(force_keyframe); |
| 226 | let out = self.session.submit_dmabuf(src, &pic)?; | 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 | /// Encodes one DMA-BUF frame of the source size (BGRx if `bgr`, else RGBx), box-downscaled to the encoder size on the GPU. | 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 | self.history.arm(); | 271 | self.history.arm(); |
| 272 | } | 272 | } |
| 273 | 273 | ||
| 274 | - fn coded(&mut self, bytes: Vec<u8>) -> CodedFrame { | 274 | + fn coded(&mut self, bytes: Vec<u8>) -> Result<CodedFrame, Error> { |
| 275 | - let meta = self.flight.pop(); | 275 | + let meta = self.flight.pop()?; |
| 276 | let data = if meta.keyframe { | 276 | let data = if meta.keyframe { |
| 277 | [self.parameter_sets.as_slice(), bytes.as_slice()].concat() | 277 | [self.parameter_sets.as_slice(), bytes.as_slice()].concat() |
| 278 | } else { | 278 | } else { |
| 279 | bytes | 279 | bytes |
| 280 | }; | 280 | }; |
| 281 | - CodedFrame { | 281 | + Ok(CodedFrame { |
| 282 | data, | 282 | data, |
| 283 | keyframe: meta.keyframe, | 283 | keyframe: meta.keyframe, |
| 284 | recovery: meta.recovery, | 284 | recovery: meta.recovery, |
| 285 | id: meta.id, | 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 | self.0.push_back(meta); | 81 | self.0.push_back(meta); |
| 82 | } | 82 | } |
| 83 | 83 | ||
| 84 | - pub(crate) fn pop(&mut self) -> Meta { | 84 | + pub(crate) fn pop(&mut self) -> Result<Meta, Error> { |
| 85 | - self.0.pop_front().unwrap_or_default() | 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 | let pic = self.next_picture(force_keyframe); | 169 | let pic = self.next_picture(force_keyframe); |
| 168 | let out = self.session.submit(y, &self.uv, &pic)?; | 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 | /// Encodes one I420 frame; `force_keyframe` makes it an IDR and restarts the GOP. | 174 | /// Encodes one I420 frame; `force_keyframe` makes it an IDR and restarts the GOP. |
| @@ -185,7 +187,7 @@ impl H264Encoder { | |||
| 185 | /// Waits for every submitted frame and returns them in order. | 187 | /// Waits for every submitted frame and returns them in order. |
| 186 | pub fn flush(&mut self) -> Result<Vec<CodedFrame>, Error> { | 188 | pub fn flush(&mut self) -> Result<Vec<CodedFrame>, Error> { |
| 187 | let bytes = self.session.flush()?; | 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 | /// Changes bitrate or frame rate; takes effect on the next frame. | 193 | /// Changes bitrate or frame rate; takes effect on the next frame. |
| @@ -207,7 +209,7 @@ impl H264Encoder { | |||
| 207 | let out = self | 209 | let out = self |
| 208 | .session | 210 | .session |
| 209 | .submit_rgb(rgb, src_width, src_height, stride_bytes, bgr, &pic)?; | 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 | /// 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. | 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 | ) -> Result<Option<CodedFrame>, Error> { | 236 | ) -> Result<Option<CodedFrame>, Error> { |
| 235 | let pic = self.next_picture(force_keyframe); | 237 | let pic = self.next_picture(force_keyframe); |
| 236 | let out = self.session.submit_d3d11(src, &pic)?; | 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 | /// Encodes one shared D3D11 texture of the source size (BGRA if `bgr`, else RGBA), downscaled to the encoder size on the GPU. | 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 | ) -> Result<Option<CodedFrame>, Error> { | 260 | ) -> Result<Option<CodedFrame>, Error> { |
| 259 | let pic = self.next_picture(force_keyframe); | 261 | let pic = self.next_picture(force_keyframe); |
| 260 | let out = self.session.submit_dmabuf(src, &pic)?; | 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 | /// Encodes one DMA-BUF frame of the source size (BGRx if `bgr`, else RGBx), box-downscaled to the encoder size on the GPU. | 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 | self.history.arm(); | 317 | self.history.arm(); |
| 316 | } | 318 | } |
| 317 | 319 | ||
| 318 | - fn coded(&mut self, bytes: Vec<u8>) -> CodedFrame { | 320 | + fn coded(&mut self, bytes: Vec<u8>) -> Result<CodedFrame, Error> { |
| 319 | - let meta = self.flight.pop(); | 321 | + let meta = self.flight.pop()?; |
| 320 | let data = if meta.keyframe { | 322 | let data = if meta.keyframe { |
| 321 | [self.parameter_sets.as_slice(), bytes.as_slice()].concat() | 323 | [self.parameter_sets.as_slice(), bytes.as_slice()].concat() |
| 322 | } else { | 324 | } else { |
| 323 | bytes | 325 | bytes |
| 324 | }; | 326 | }; |
| 325 | - CodedFrame { | 327 | + Ok(CodedFrame { |
| 326 | data, | 328 | data, |
| 327 | keyframe: meta.keyframe, | 329 | keyframe: meta.keyframe, |
| 328 | recovery: meta.recovery, | 330 | recovery: meta.recovery, |
| 329 | id: meta.id, | 331 | id: meta.id, |
| 330 | - } | 332 | + }) |
| 331 | } | 333 | } |
| 332 | } | 334 | } |