fix: flush decoder

This commit is contained in:
kieran 2024-11-06 15:44:33 +00:00
parent d252c1cf28
commit e8ae73f3a8
No known key found for this signature in database
GPG Key ID: DE71CEB3925BE941

View File

@ -207,28 +207,31 @@ impl Decoder {
}
}
pub unsafe fn decode_pkt(
&mut self,
/// Flush all decoders
pub unsafe fn flush(&mut self) -> Result<Vec<(*mut AVFrame, *mut AVStream)>, Error> {
let mut pkgs = Vec::new();
for ctx in self.codecs.values_mut() {
pkgs.extend(Self::decode_pkt_internal(
ctx.context,
ptr::null_mut(),
ptr::null_mut(),
)?);
}
Ok(pkgs)
}
pub unsafe fn decode_pkt_internal(
ctx: *mut AVCodecContext,
pkt: *mut AVPacket,
stream: *mut AVStream,
) -> Result<Vec<(*mut AVFrame, *mut AVStream)>, Error> {
let stream_index = (*pkt).stream_index;
assert_eq!(
stream_index,
(*stream).index,
"Passed stream reference does not match stream_index of packet"
);
if let Some(ctx) = self.codecs.get_mut(&stream_index) {
let mut ret = avcodec_send_packet(ctx.context, pkt);
if ret < 0 {
return Err(Error::msg(format!("Failed to decode packet {}", ret)));
}
let mut ret = avcodec_send_packet(ctx, pkt);
return_ffmpeg_error!(ret, "Failed to decode packet");
let mut pkgs = Vec::new();
while ret >= 0 {
let frame = av_frame_alloc();
ret = avcodec_receive_frame(ctx.context, frame);
ret = avcodec_receive_frame(ctx, frame);
if ret < 0 {
if ret == AVERROR_EOF || ret == AVERROR(libc::EAGAIN) {
break;
@ -240,6 +243,25 @@ impl Decoder {
pkgs.push((frame, stream));
}
Ok(pkgs)
}
pub unsafe fn decode_pkt(
&mut self,
pkt: *mut AVPacket,
stream: *mut AVStream,
) -> Result<Vec<(*mut AVFrame, *mut AVStream)>, Error> {
if pkt.is_null() {
return self.flush();
}
let stream_index = (*pkt).stream_index;
assert_eq!(
stream_index,
(*stream).index,
"Passed stream reference does not match stream_index of packet"
);
if let Some(ctx) = self.codecs.get_mut(&stream_index) {
Self::decode_pkt_internal(ctx.context, pkt, stream)
} else {
Ok(vec![])
}