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; continue;
} }
if let Ok(frames) = decoder.decode_pkt(pkt) { 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 // do nothing but decode entire stream
if media_type == AVMediaType::AVMEDIA_TYPE_VIDEO { if media_type == AVMediaType::AVMEDIA_TYPE_VIDEO {
frame = get_frame_from_hw(frame).expect("get frame failed"); frame = get_frame_from_hw(frame).expect("get frame failed");

View File

@ -237,25 +237,25 @@ impl Decoder {
} }
/// Flush all decoders /// 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(); let mut pkgs = Vec::new();
for ctx in self.codecs.values_mut() { 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) Ok(pkgs)
} }
pub unsafe fn decode_pkt_internal( pub unsafe fn decode_pkt_internal(
ctx: *mut AVCodecContext, ctx: &DecoderCodecContext,
pkt: *mut AVPacket, pkt: *mut AVPacket,
) -> Result<Vec<*mut AVFrame>, Error> { ) -> Result<Vec<(*mut AVFrame, *mut AVStream)>, Error> {
let mut ret = avcodec_send_packet(ctx, pkt); let mut ret = avcodec_send_packet(ctx.context, pkt);
bail_ffmpeg!(ret, "Failed to decode packet"); bail_ffmpeg!(ret, "Failed to decode packet");
let mut pkgs = Vec::new(); let mut pkgs = Vec::new();
while ret >= 0 { while ret >= 0 {
let mut frame = av_frame_alloc(); let mut frame = av_frame_alloc();
ret = avcodec_receive_frame(ctx, frame); ret = avcodec_receive_frame(ctx.context, frame);
if ret < 0 { if ret < 0 {
av_frame_free(&mut frame); av_frame_free(&mut frame);
if ret == AVERROR_EOF || ret == AVERROR(libc::EAGAIN) { if ret == AVERROR_EOF || ret == AVERROR(libc::EAGAIN) {
@ -263,17 +263,20 @@ impl Decoder {
} }
return Err(Error::msg(format!("Failed to decode {}", ret))); return Err(Error::msg(format!("Failed to decode {}", ret)));
} }
pkgs.push(frame); pkgs.push((frame, ctx.stream));
} }
Ok(pkgs) 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() { if pkt.is_null() {
return self.flush(); return self.flush();
} }
if let Some(ctx) = self.codecs.get_mut(&(*pkt).stream_index) { 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 { } else {
Ok(vec![]) Ok(vec![])
} }

View File

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

View File

@ -107,7 +107,7 @@ impl Filter {
Ok(()) 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!(); todo!();
} }
} }

View File

@ -121,7 +121,7 @@ impl Transcoder {
let src_index = (*stream).index; let src_index = (*stream).index;
// check if encoded stream // check if encoded stream
if let Some(enc) = self.encoders.get_mut(&src_index) { 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 // scale video frame before sending to encoder
let frame = if let Some(sws) = self.scalers.get_mut(&src_index) { let frame = if let Some(sws) = self.scalers.get_mut(&src_index) {
let enc_ctx = enc.codec_context(); let enc_ctx = enc.codec_context();