From b8d45e82893af81c4c492f365539a93b9a9fab69 Mon Sep 17 00:00:00 2001 From: kieran Date: Tue, 21 Jan 2025 11:10:46 +0000 Subject: [PATCH] fix: return stream pointer in decoder --- examples/main.rs | 2 +- src/decode.rs | 21 ++++++++++++--------- src/demux.rs | 2 +- src/filter.rs | 2 +- src/transcode.rs | 2 +- 5 files changed, 16 insertions(+), 13 deletions(-) diff --git a/examples/main.rs b/examples/main.rs index 4578c16..6dc5fdb 100644 --- a/examples/main.rs +++ b/examples/main.rs @@ -67,7 +67,7 @@ unsafe fn loop_decoder(mut demuxer: Demuxer, mut decoder: Decoder) { continue; } if let Ok(frames) = decoder.decode_pkt(pkt) { - for mut frame in frames { + for (mut frame, _stream) in frames { // do nothing but decode entire stream if media_type == AVMediaType::AVMEDIA_TYPE_VIDEO { frame = get_frame_from_hw(frame).expect("get frame failed"); diff --git a/src/decode.rs b/src/decode.rs index 4a11748..a4a108d 100644 --- a/src/decode.rs +++ b/src/decode.rs @@ -237,25 +237,25 @@ impl Decoder { } /// Flush all decoders - pub unsafe fn flush(&mut self) -> Result, Error> { + pub unsafe fn flush(&mut self) -> Result, Error> { let mut pkgs = Vec::new(); for ctx in self.codecs.values_mut() { - pkgs.extend(Self::decode_pkt_internal(ctx.context, ptr::null_mut())?); + pkgs.extend(Self::decode_pkt_internal(ctx, ptr::null_mut())?); } Ok(pkgs) } pub unsafe fn decode_pkt_internal( - ctx: *mut AVCodecContext, + ctx: &DecoderCodecContext, pkt: *mut AVPacket, - ) -> Result, Error> { - let mut ret = avcodec_send_packet(ctx, pkt); + ) -> Result, Error> { + let mut ret = avcodec_send_packet(ctx.context, pkt); bail_ffmpeg!(ret, "Failed to decode packet"); let mut pkgs = Vec::new(); while ret >= 0 { let mut frame = av_frame_alloc(); - ret = avcodec_receive_frame(ctx, frame); + ret = avcodec_receive_frame(ctx.context, frame); if ret < 0 { av_frame_free(&mut frame); if ret == AVERROR_EOF || ret == AVERROR(libc::EAGAIN) { @@ -263,17 +263,20 @@ impl Decoder { } return Err(Error::msg(format!("Failed to decode {}", ret))); } - pkgs.push(frame); + pkgs.push((frame, ctx.stream)); } Ok(pkgs) } - pub unsafe fn decode_pkt(&mut self, pkt: *mut AVPacket) -> Result, Error> { + pub unsafe fn decode_pkt( + &mut self, + pkt: *mut AVPacket, + ) -> Result, Error> { if pkt.is_null() { return self.flush(); } if let Some(ctx) = self.codecs.get_mut(&(*pkt).stream_index) { - Self::decode_pkt_internal(ctx.context, pkt) + Self::decode_pkt_internal(ctx, pkt) } else { Ok(vec![]) } diff --git a/src/demux.rs b/src/demux.rs index 372a7ae..595d349 100644 --- a/src/demux.rs +++ b/src/demux.rs @@ -31,7 +31,7 @@ unsafe extern "C" fn read_data( pub enum DemuxerInput { Url(String), - Reader(Option>, Option), + Reader(Option>, Option), } pub struct Demuxer { diff --git a/src/filter.rs b/src/filter.rs index 0e861cb..8bcb8aa 100644 --- a/src/filter.rs +++ b/src/filter.rs @@ -107,7 +107,7 @@ impl Filter { Ok(()) } - pub unsafe fn process_frame(&mut self, frame: *mut AVFrame) -> Result<*mut AVFrame, Error> { + pub unsafe fn process_frame(&mut self, _frame: *mut AVFrame) -> Result<*mut AVFrame, Error> { todo!(); } } diff --git a/src/transcode.rs b/src/transcode.rs index 99910fc..5fb7243 100644 --- a/src/transcode.rs +++ b/src/transcode.rs @@ -121,7 +121,7 @@ impl Transcoder { let src_index = (*stream).index; // check if encoded stream if let Some(enc) = self.encoders.get_mut(&src_index) { - for mut frame in self.decoder.decode_pkt(pkt)? { + for (mut frame, _stream) in self.decoder.decode_pkt(pkt)? { // scale video frame before sending to encoder let frame = if let Some(sws) = self.scalers.get_mut(&src_index) { let enc_ctx = enc.codec_context();