Skip to content

Commit

Permalink
Add a loop to drain buffered frames in the decoder
Browse files Browse the repository at this point in the history
If we use a single decoder instance to decode all tiles in an image
grid, it is important to endure there are no buffered frames in the
decoder before decoding the next tile with the decoder.

BUG=oss-fuzz:66313
  • Loading branch information
wantehchang committed Feb 8, 2024
1 parent 8edba16 commit ed3e115
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/codec_dav1d.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,17 @@ static avifBool dav1dCodecGetNextImage(struct avifCodec * codec,
return AVIF_FALSE;
}

int res;
for (;;) {
if (dav1dData.data) {
int res = dav1d_send_data(codec->internal->dav1dContext, &dav1dData);
res = dav1d_send_data(codec->internal->dav1dContext, &dav1dData);
if ((res < 0) && (res != DAV1D_ERR(EAGAIN))) {
dav1d_data_unref(&dav1dData);
return AVIF_FALSE;
}
}

int res = dav1d_get_picture(codec->internal->dav1dContext, &nextFrame);
res = dav1d_get_picture(codec->internal->dav1dContext, &nextFrame);
if (res == DAV1D_ERR(EAGAIN)) {
if (dav1dData.data) {
// send more data
Expand Down Expand Up @@ -125,6 +126,23 @@ static avifBool dav1dCodecGetNextImage(struct avifCodec * codec,
dav1d_data_unref(&dav1dData);
}

// Drain all buffered frames in the decoder.
Dav1dPicture bufferedFrame;
memset(&bufferedFrame, 0, sizeof(Dav1dPicture));
do {
res = dav1d_get_picture(codec->internal->dav1dContext, &bufferedFrame);
if (res < 0) {
if (res != DAV1D_ERR(EAGAIN)) {
if (gotPicture) {
dav1d_picture_unref(&nextFrame);
}
return AVIF_FALSE;
}
} else {
dav1d_picture_unref(&bufferedFrame);
}
} while (res == 0);

if (gotPicture) {
dav1d_picture_unref(&codec->internal->dav1dPicture);
codec->internal->dav1dPicture = nextFrame;
Expand Down

0 comments on commit ed3e115

Please sign in to comment.