fix: return stream pointer in decoder

This commit is contained in:
kieran 2025-01-21 11:10:46 +00:00
parent de2050cec0
commit b8d45e8289
No known key found for this signature in database
GPG Key ID: DE71CEB3925BE941
5 changed files with 16 additions and 13 deletions

View File

@ -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");

View File

@ -237,25 +237,25 @@ impl Decoder {
}
/// Flush all decoders
pub unsafe fn flush(&mut self) -> Result<Vec<*mut AVFrame>, Error> {
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())?);
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<Vec<*mut AVFrame>, Error> {
let mut ret = avcodec_send_packet(ctx, pkt);
) -> Result<Vec<(*mut AVFrame, *mut AVStream)>, 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<Vec<*mut AVFrame>, Error> {
pub unsafe fn decode_pkt(
&mut self,
pkt: *mut AVPacket,
) -> Result<Vec<(*mut AVFrame, *mut AVStream)>, 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![])
}

View File

@ -31,7 +31,7 @@ unsafe extern "C" fn read_data(
pub enum DemuxerInput {
Url(String),
Reader(Option<SlimBox<dyn Read + 'static>>, Option<String>),
Reader(Option<SlimBox<dyn Read>>, Option<String>),
}
pub struct Demuxer {

View File

@ -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!();
}
}

View File

@ -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();