Skip to content

Commit

Permalink
Open and close the codec only if extradata is available (#59)
Browse files Browse the repository at this point in the history
Opening and closing the codec in order to flush the buffer could lead
to corrupted decoded frames (x264 version < 151).
On the other side just flushing the buffer could lead to garbage
decoded frames when SPS/PPS is available only in the header.

This is based on AkarinVS/L-SMASH-Works@3d2f02e.
  • Loading branch information
Asd-g committed Mar 17, 2024
1 parent 266fe29 commit fcd1ed5
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions common/lwlibav_dec.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,31 @@ void lwlibav_flush_buffers
lwlibav_decode_handler_t *dhp
)
{
const AVCodecParameters *codecpar = dhp->format->streams[ dhp->stream_index ]->codecpar;
const AVCodec *codec = dhp->ctx->codec;
void *app_specific = dhp->ctx->opaque;
AVCodecContext *ctx = NULL;
if( open_decoder( &ctx, codecpar, codec, dhp->ctx->thread_count, dhp->drc, dhp->ff_options ) < 0 )
if (dhp->index_entries_count <= 1)
{
avcodec_flush_buffers( dhp->ctx );
dhp->error = 1;
lw_log_show( &dhp->lh, LW_LOG_FATAL,
"Failed to flush buffers by a reliable way.\n"
"It is recommended you reopen the file." );
const AVCodecParameters* codecpar = dhp->format->streams[dhp->stream_index]->codecpar;
const AVCodec* codec = dhp->ctx->codec;
void* app_specific = dhp->ctx->opaque;
AVCodecContext* ctx = NULL;
if (open_decoder(&ctx, codecpar, codec, dhp->ctx->thread_count, dhp->drc, dhp->ff_options) < 0)
{
avcodec_flush_buffers(dhp->ctx);
dhp->error = 1;
lw_log_show(&dhp->lh, LW_LOG_FATAL,
"Failed to flush buffers by a reliable way.\n"
"It is recommended you reopen the file.");
}
else
{
dhp->ctx->opaque = NULL;
avcodec_free_context(&dhp->ctx);
dhp->ctx = ctx;
dhp->ctx->opaque = app_specific;
}
}
else
{
dhp->ctx->opaque = NULL;
avcodec_free_context( &dhp->ctx );
dhp->ctx = ctx;
dhp->ctx->opaque = app_specific;
}
avcodec_flush_buffers(dhp->ctx);

dhp->exh.delay_count = 0;
}

Expand Down

0 comments on commit fcd1ed5

Please sign in to comment.