Skip to content

Commit

Permalink
Handle non-fatal error dav1d::Error::Again (#2043)
Browse files Browse the repository at this point in the history
  • Loading branch information
FlareFlo authored Nov 4, 2023
1 parent f1cdb3b commit eb05a39
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/codecs/avif/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ impl<R: Read> AvifDecoder<R> {
primary_decoder
.send_data(coded.to_vec(), None, None, None)
.map_err(error_map)?;
let picture = primary_decoder.get_picture().map_err(error_map)?;
let picture = read_until_ready(&mut primary_decoder)?;
let alpha_item = ctx.alpha_item_coded_data().unwrap_or_default();
let alpha_picture = if !alpha_item.is_empty() {
let mut alpha_decoder = dav1d::Decoder::new().map_err(error_map)?;
alpha_decoder
.send_data(alpha_item.to_vec(), None, None, None)
.map_err(error_map)?;
Some(alpha_decoder.get_picture().map_err(error_map)?)
Some(read_until_ready(&mut alpha_decoder)?)
} else {
None
};
Expand All @@ -68,6 +68,7 @@ impl<R: Read> AvifDecoder<R> {

/// Wrapper struct around a `Cursor<Vec<u8>>`
pub struct AvifReader<R>(Cursor<Vec<u8>>, PhantomData<R>);

impl<R> Read for AvifReader<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.0.read(buf)
Expand Down Expand Up @@ -172,3 +173,19 @@ impl<'a, R: 'a + Read> ImageDecoder<'a> for AvifDecoder<R> {
Ok(())
}
}

/// `get_picture` and `send_pending_data` yield `Again` as a non-fatal error requesting more data is sent to the decoder
/// This ensures that in the case of `Again` all pending data is submitted
/// This should be called after `send_data` (which does not yield `Again` when called the first time)
fn read_until_ready(decoder: &mut dav1d::Decoder) -> ImageResult<dav1d::Picture> {
loop {
match decoder.get_picture() {
Err(dav1d::Error::Again) => match decoder.send_pending_data() {
Ok(_) => {}
Err(dav1d::Error::Again) => {}
Err(e) => return Err(error_map(e)),
},
r => return r.map_err(error_map),
}
}
}

0 comments on commit eb05a39

Please sign in to comment.