From a985e8ed6b3444eba83763b96a27c414102c6f6e Mon Sep 17 00:00:00 2001 From: Niklas Haas Date: Thu, 26 Jul 2018 17:37:34 +0200 Subject: encode: fix AVPacket deinitialization logic Since this function is called with packets on the stack, trying to free them makes no sense. Instead, it should unref (which is what `av_interleaved_write_frame` does anyway, rather than freeing). Also, the calling code tried unreffing the packet a second time, even after it was "freed" by the callee in the failure case - and after ownership was taken over by `av_interleaved_write_frame` in the successful case. Both of these cases were wrong. (cherry picked from commit 6d61c5f68a5b1faaaf5a38080c690e35bd31858c) --- common/encode_lavc.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/common/encode_lavc.c b/common/encode_lavc.c index d8e875c58f..7cf18750cc 100644 --- a/common/encode_lavc.c +++ b/common/encode_lavc.c @@ -436,7 +436,7 @@ done: return dst; } -// Write a packet. Callee will create new pkt refs as needed. +// Write a packet. This will take over ownership of `pkt` static void encode_lavc_add_packet(struct mux_stream *dst, AVPacket *pkt) { struct encode_lavc_context *ctx = dst->ctx; @@ -476,6 +476,7 @@ static void encode_lavc_add_packet(struct mux_stream *dst, AVPacket *pkt) if (av_interleaved_write_frame(p->muxer, pkt) < 0) { MP_ERR(p, "Writing packet failed.\n"); p->failed = true; + pkt = NULL; goto done; } @@ -483,7 +484,8 @@ static void encode_lavc_add_packet(struct mux_stream *dst, AVPacket *pkt) done: pthread_mutex_unlock(&ctx->lock); - av_packet_free(&pkt); + if (pkt) + av_packet_unref(pkt); } void encode_lavc_discontinuity(struct encode_lavc_context *ctx) @@ -949,7 +951,6 @@ bool encoder_encode(struct encoder_context *p, AVFrame *frame) break; encode_lavc_add_packet(p->mux_stream, &packet); - av_packet_unref(&packet); } return true; -- cgit v1.2.3