1
0
mirror of https://github.com/mpv-player/mpv synced 2025-04-29 23:01:12 +00:00

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.
This commit is contained in:
Niklas Haas 2018-07-26 17:37:34 +02:00 committed by Jan Ekström
parent 9f6147a56f
commit 6d61c5f68a

View File

@ -436,7 +436,7 @@ done:
return dst; 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) static void encode_lavc_add_packet(struct mux_stream *dst, AVPacket *pkt)
{ {
struct encode_lavc_context *ctx = dst->ctx; 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) { if (av_interleaved_write_frame(p->muxer, pkt) < 0) {
MP_ERR(p, "Writing packet failed.\n"); MP_ERR(p, "Writing packet failed.\n");
p->failed = true; p->failed = true;
pkt = NULL;
goto done; goto done;
} }
@ -483,7 +484,8 @@ static void encode_lavc_add_packet(struct mux_stream *dst, AVPacket *pkt)
done: done:
pthread_mutex_unlock(&ctx->lock); pthread_mutex_unlock(&ctx->lock);
av_packet_free(&pkt); if (pkt)
av_packet_unref(pkt);
} }
void encode_lavc_discontinuity(struct encode_lavc_context *ctx) void encode_lavc_discontinuity(struct encode_lavc_context *ctx)
@ -949,7 +951,6 @@ bool encoder_encode(struct encoder_context *p, AVFrame *frame)
break; break;
encode_lavc_add_packet(p->mux_stream, &packet); encode_lavc_add_packet(p->mux_stream, &packet);
av_packet_unref(&packet);
} }
return true; return true;