forge
Explore
Log in

Don't you dare go Hollow

B4: ec-vk Flight::pop returns Err on empty queue, not a default Meta

Browse files at this commit

df1946ca404f2a0d9ccf38ccd93dd1738a8b78ccAhmet Kaan GümüşParent: 22f0f3f
Flight::pop fell back to Meta::default() (keyframe false, id 0) when the
queue was out of step, silently emitting a wrong frame. Now it returns
Invalid("frame queue out of step"); coded() and its callers propagate it.
Bitstream output is unchanged; only the impossible empty-queue case differs.
crates/ec-vk/src/av1.rsmodified+10 -10
@@ -100,7 +100,7 @@ impl Av1Encoder {
100100 }
101101 let pic = self.next_picture(force_keyframe);
102102 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()
104104 }
105105
106106 /// 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 {
134134 }
135135 let pic = self.next_picture(force_keyframe);
136136 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()
138138 }
139139
140140 /// 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 {
153153 /// Waits for every submitted frame and returns them in order.
154154 pub fn flush(&mut self) -> Result<Vec<CodedFrame>, Error> {
155155 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()
157157 }
158158
159159 /// Changes bitrate or frame rate; takes effect on the next frame.
@@ -175,7 +175,7 @@ impl Av1Encoder {
175175 let out = self
176176 .session
177177 .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()
179179 }
180180
181181 /// 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 {
202202 ) -> Result<Option<CodedFrame>, Error> {
203203 let pic = self.next_picture(force_keyframe);
204204 let out = self.session.submit_d3d11(src, &pic)?;
205- Ok(out.map(|bytes| self.coded(bytes)))
205+ out.map(|bytes| self.coded(bytes)).transpose()
206206 }
207207
208208 /// 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 {
226226 ) -> Result<Option<CodedFrame>, Error> {
227227 let pic = self.next_picture(force_keyframe);
228228 let out = self.session.submit_dmabuf(src, &pic)?;
229- Ok(out.map(|bytes| self.coded(bytes)))
229+ out.map(|bytes| self.coded(bytes)).transpose()
230230 }
231231
232232 /// 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 {
275275 self.history.arm();
276276 }
277277
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()?;
280280 let mut data = TEMPORAL_DELIMITER.to_vec();
281281 if meta.keyframe {
282282 data.extend_from_slice(&self.sequence_header);
283283 }
284284 data.extend_from_slice(&bytes);
285- CodedFrame {
285+ Ok(CodedFrame {
286286 data,
287287 keyframe: meta.keyframe,
288288 recovery: meta.recovery,
289289 id: meta.id,
290- }
290+ })
291291 }
292292 }
crates/ec-vk/src/h265.rsmodified+10 -10
@@ -98,7 +98,7 @@ impl H265Encoder {
9898 }
9999 let pic = self.next_picture(force_keyframe);
100100 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()
102102 }
103103
104104 /// Encodes one I420 frame; `force_keyframe` makes it an IDR and restarts the GOP.
@@ -132,7 +132,7 @@ impl H265Encoder {
132132 }
133133 let pic = self.next_picture(force_keyframe);
134134 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()
136136 }
137137
138138 /// 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 {
151151 /// Waits for every submitted frame and returns them in order.
152152 pub fn flush(&mut self) -> Result<Vec<CodedFrame>, Error> {
153153 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()
155155 }
156156
157157 /// Changes bitrate or frame rate; takes effect on the next frame.
@@ -173,7 +173,7 @@ impl H265Encoder {
173173 let out = self
174174 .session
175175 .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()
177177 }
178178
179179 /// 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 {
200200 ) -> Result<Option<CodedFrame>, Error> {
201201 let pic = self.next_picture(force_keyframe);
202202 let out = self.session.submit_d3d11(src, &pic)?;
203- Ok(out.map(|bytes| self.coded(bytes)))
203+ out.map(|bytes| self.coded(bytes)).transpose()
204204 }
205205
206206 /// 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 {
224224 ) -> Result<Option<CodedFrame>, Error> {
225225 let pic = self.next_picture(force_keyframe);
226226 let out = self.session.submit_dmabuf(src, &pic)?;
227- Ok(out.map(|bytes| self.coded(bytes)))
227+ out.map(|bytes| self.coded(bytes)).transpose()
228228 }
229229
230230 /// 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 {
271271 self.history.arm();
272272 }
273273
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()?;
276276 let data = if meta.keyframe {
277277 [self.parameter_sets.as_slice(), bytes.as_slice()].concat()
278278 } else {
279279 bytes
280280 };
281- CodedFrame {
281+ Ok(CodedFrame {
282282 data,
283283 keyframe: meta.keyframe,
284284 recovery: meta.recovery,
285285 id: meta.id,
286- }
286+ })
287287 }
288288 }
crates/ec-vk/src/lib.rsmodified+13 -11
@@ -81,8 +81,10 @@ impl Flight {
8181 self.0.push_back(meta);
8282 }
8383
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()))
8688 }
8789 }
8890
@@ -166,7 +168,7 @@ impl H264Encoder {
166168 }
167169 let pic = self.next_picture(force_keyframe);
168170 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()
170172 }
171173
172174 /// Encodes one I420 frame; `force_keyframe` makes it an IDR and restarts the GOP.
@@ -185,7 +187,7 @@ impl H264Encoder {
185187 /// Waits for every submitted frame and returns them in order.
186188 pub fn flush(&mut self) -> Result<Vec<CodedFrame>, Error> {
187189 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()
189191 }
190192
191193 /// Changes bitrate or frame rate; takes effect on the next frame.
@@ -207,7 +209,7 @@ impl H264Encoder {
207209 let out = self
208210 .session
209211 .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()
211213 }
212214
213215 /// 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 {
234236 ) -> Result<Option<CodedFrame>, Error> {
235237 let pic = self.next_picture(force_keyframe);
236238 let out = self.session.submit_d3d11(src, &pic)?;
237- Ok(out.map(|bytes| self.coded(bytes)))
239+ out.map(|bytes| self.coded(bytes)).transpose()
238240 }
239241
240242 /// 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 {
258260 ) -> Result<Option<CodedFrame>, Error> {
259261 let pic = self.next_picture(force_keyframe);
260262 let out = self.session.submit_dmabuf(src, &pic)?;
261- Ok(out.map(|bytes| self.coded(bytes)))
263+ out.map(|bytes| self.coded(bytes)).transpose()
262264 }
263265
264266 /// 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 {
315317 self.history.arm();
316318 }
317319
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()?;
320322 let data = if meta.keyframe {
321323 [self.parameter_sets.as_slice(), bytes.as_slice()].concat()
322324 } else {
323325 bytes
324326 };
325- CodedFrame {
327+ Ok(CodedFrame {
326328 data,
327329 keyframe: meta.keyframe,
328330 recovery: meta.recovery,
329331 id: meta.id,
330- }
332+ })
331333 }
332334 }