Skip to content

Commit

Permalink
decoder: make format negotiators use a dynamic frame pool
Browse files Browse the repository at this point in the history
The negotiator is already dynamic so there is little point in keeping
the frame pool fully typed. This change will also allow us to create
fully dynamic decoder trait objects.
  • Loading branch information
Gnurou committed Jun 18, 2024
1 parent 7b13f3c commit e82c142
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 23 deletions.
13 changes: 8 additions & 5 deletions src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,27 @@ pub struct StreamInfo {
/// negotiate its specifics.
///
/// When the object is dropped, the decoder can accept and process new input again.
pub trait DecoderFormatNegotiator<'a> {
type FramePool: FramePool;
pub trait DecoderFormatNegotiator {
type Descriptor;

/// Returns the current decoding parameters, as extracted from the stream.
fn stream_info(&self) -> &StreamInfo;
/// Returns the frame pool in use for the decoder for `layer` set up for the
/// new format.
fn frame_pool(&mut self, layer: PoolLayer) -> Vec<&mut Self::FramePool>;
fn frame_pool(
&mut self,
layer: PoolLayer,
) -> Vec<&mut dyn FramePool<Descriptor = Self::Descriptor>>;
/// Attempt to change the pixel format of output frames to `format`.
fn try_format(&mut self, format: DecodedFormat) -> anyhow::Result<()>;
}

/// Events that can be retrieved using the `next_event` method of a decoder.
pub enum DecoderEvent<'a, H: DecodedHandle, P: FramePool<Descriptor = H::Descriptor> + ?Sized> {
pub enum DecoderEvent<'a, H: DecodedHandle> {
/// The next frame has been decoded.
FrameReady(H),
/// The format of the stream has changed and action is required.
FormatChanged(Box<dyn DecoderFormatNegotiator<'a, FramePool = P> + 'a>),
FormatChanged(Box<dyn DecoderFormatNegotiator<Descriptor = H::Descriptor> + 'a>),
}

pub trait DynHandle {
Expand Down
26 changes: 13 additions & 13 deletions src/decoder/stateless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,6 @@ where
decoder: &'a mut D,
format_hint: FH,
apply_format: F,
_mem_desc: std::marker::PhantomData<H>,
_frame_pool: std::marker::PhantomData<FP>,
}

impl<'a, H, FP, D, FH, F> StatelessDecoderFormatNegotiator<'a, H, FP, D, FH, F>
Expand All @@ -168,28 +166,33 @@ where
decoder,
format_hint,
apply_format,
_mem_desc: std::marker::PhantomData,
_frame_pool: std::marker::PhantomData,
}
}
}

impl<'a, H, FP, D, FH, F> DecoderFormatNegotiator<'a>
impl<'a, H, FP, D, FH, F> DecoderFormatNegotiator
for StatelessDecoderFormatNegotiator<'a, H, FP, D, FH, F>
where
H: DecodedHandle,
FP: FramePool<Descriptor = H::Descriptor>,
D: StatelessVideoDecoder<Handle = H, FramePool = FP> + private::StatelessVideoDecoder,
F: Fn(&mut D, &FH),
{
type FramePool = FP;
type Descriptor = H::Descriptor;

fn try_format(&mut self, format: DecodedFormat) -> anyhow::Result<()> {
self.decoder.try_format(format)
}

fn frame_pool(&mut self, layer: PoolLayer) -> Vec<&mut FP> {
self.decoder.frame_pool(layer)
fn frame_pool(
&mut self,
layer: PoolLayer,
) -> Vec<&mut dyn FramePool<Descriptor = Self::Descriptor>> {
self.decoder
.frame_pool(layer)
.into_iter()
.map(|p| p as &mut dyn FramePool<Descriptor = _>)
.collect()
}

fn stream_info(&self) -> &StreamInfo {
Expand Down Expand Up @@ -277,7 +280,7 @@ pub trait StatelessVideoDecoder {
fn stream_info(&self) -> Option<&StreamInfo>;

/// Returns the next event, if there is any pending.
fn next_event(&mut self) -> Option<DecoderEvent<Self::Handle, Self::FramePool>>;
fn next_event(&mut self) -> Option<DecoderEvent<Self::Handle>>;

/// Returns a file descriptor that signals `POLLIN` whenever an event is pending on this
/// decoder.
Expand Down Expand Up @@ -401,10 +404,7 @@ where
/// Returns the next pending event, if any, using `on_format_changed` as the format change
/// callback of the [`StatelessDecoderFormatNegotiator`] if there is a resolution change event
/// pending.
fn query_next_event<F>(
&mut self,
on_format_changed: F,
) -> Option<DecoderEvent<B::Handle, B::FramePool>>
fn query_next_event<F>(&mut self, on_format_changed: F) -> Option<DecoderEvent<B::Handle>>
where
Self: StatelessVideoDecoder<Handle = B::Handle, FramePool = B::FramePool>,
C::FormatInfo: Clone,
Expand Down
2 changes: 1 addition & 1 deletion src/decoder/stateless/av1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ where
self.backend.stream_info()
}

fn next_event(&mut self) -> Option<crate::decoder::DecoderEvent<B::Handle, B::FramePool>> {
fn next_event(&mut self) -> Option<crate::decoder::DecoderEvent<B::Handle>> {
self.query_next_event(|decoder, sequence| {
decoder.codec.sequence = Some(Rc::clone(sequence));
})
Expand Down
2 changes: 1 addition & 1 deletion src/decoder/stateless/h264.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1374,7 +1374,7 @@ where
Ok(())
}

fn next_event(&mut self) -> Option<DecoderEvent<B::Handle, B::FramePool>> {
fn next_event(&mut self) -> Option<DecoderEvent<B::Handle>> {
self.query_next_event(|decoder, sps| {
// Apply the SPS settings to the decoder so we don't enter the AwaitingFormat state
// on the next decode() call.
Expand Down
2 changes: 1 addition & 1 deletion src/decoder/stateless/h265.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1312,7 +1312,7 @@ where
Ok(())
}

fn next_event(&mut self) -> Option<DecoderEvent<B::Handle, B::FramePool>> {
fn next_event(&mut self) -> Option<DecoderEvent<B::Handle>> {
self.query_next_event(|decoder, sps| {
// Apply the SPS settings to the decoder so we don't enter the AwaitingFormat state
// on the next decode() call.
Expand Down
2 changes: 1 addition & 1 deletion src/decoder/stateless/vp8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ where
Ok(())
}

fn next_event(&mut self) -> Option<DecoderEvent<B::Handle, B::FramePool>> {
fn next_event(&mut self) -> Option<DecoderEvent<B::Handle>> {
self.query_next_event(|decoder, hdr| {
decoder.coded_resolution = Resolution {
width: hdr.width as u32,
Expand Down
2 changes: 1 addition & 1 deletion src/decoder/stateless/vp9.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ where
Ok(())
}

fn next_event(&mut self) -> Option<DecoderEvent<B::Handle, B::FramePool>> {
fn next_event(&mut self) -> Option<DecoderEvent<B::Handle>> {
self.query_next_event(|decoder, hdr| {
decoder.codec.negotiation_info = hdr.into();
})
Expand Down

0 comments on commit e82c142

Please sign in to comment.