Skip to content

Commit fcd1ed5

Browse files
committed
Open and close the codec only if extradata is available (#59)
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.
1 parent 266fe29 commit fcd1ed5

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

common/lwlibav_dec.c

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,31 @@ void lwlibav_flush_buffers
4545
lwlibav_decode_handler_t *dhp
4646
)
4747
{
48-
const AVCodecParameters *codecpar = dhp->format->streams[ dhp->stream_index ]->codecpar;
49-
const AVCodec *codec = dhp->ctx->codec;
50-
void *app_specific = dhp->ctx->opaque;
51-
AVCodecContext *ctx = NULL;
52-
if( open_decoder( &ctx, codecpar, codec, dhp->ctx->thread_count, dhp->drc, dhp->ff_options ) < 0 )
48+
if (dhp->index_entries_count <= 1)
5349
{
54-
avcodec_flush_buffers( dhp->ctx );
55-
dhp->error = 1;
56-
lw_log_show( &dhp->lh, LW_LOG_FATAL,
57-
"Failed to flush buffers by a reliable way.\n"
58-
"It is recommended you reopen the file." );
50+
const AVCodecParameters* codecpar = dhp->format->streams[dhp->stream_index]->codecpar;
51+
const AVCodec* codec = dhp->ctx->codec;
52+
void* app_specific = dhp->ctx->opaque;
53+
AVCodecContext* ctx = NULL;
54+
if (open_decoder(&ctx, codecpar, codec, dhp->ctx->thread_count, dhp->drc, dhp->ff_options) < 0)
55+
{
56+
avcodec_flush_buffers(dhp->ctx);
57+
dhp->error = 1;
58+
lw_log_show(&dhp->lh, LW_LOG_FATAL,
59+
"Failed to flush buffers by a reliable way.\n"
60+
"It is recommended you reopen the file.");
61+
}
62+
else
63+
{
64+
dhp->ctx->opaque = NULL;
65+
avcodec_free_context(&dhp->ctx);
66+
dhp->ctx = ctx;
67+
dhp->ctx->opaque = app_specific;
68+
}
5969
}
6070
else
61-
{
62-
dhp->ctx->opaque = NULL;
63-
avcodec_free_context( &dhp->ctx );
64-
dhp->ctx = ctx;
65-
dhp->ctx->opaque = app_specific;
66-
}
71+
avcodec_flush_buffers(dhp->ctx);
72+
6773
dhp->exh.delay_count = 0;
6874
}
6975

0 commit comments

Comments
 (0)