From 3b17ab5d3bf235da2dd574a6002fcd3b9aca9a59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Michaj=C5=82ow?= Date: Tue, 27 Jan 2026 06:41:46 +0100 Subject: [PATCH] demux/packet: add demux_packet_unref helper --- demux/packet.c | 32 ++++++++++++++++++++++++++------ demux/packet.h | 1 + 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/demux/packet.c b/demux/packet.c index 26f8520f54..7751f36354 100644 --- a/demux/packet.c +++ b/demux/packet.c @@ -54,13 +54,8 @@ static void packet_destroy(void *ptr) demux_packet_unref_contents(dp); } -static struct demux_packet *packet_create(struct demux_packet_pool *pool) +static void set_packet_defaults(struct demux_packet *dp) { - struct demux_packet *dp = pool ? demux_packet_pool_pop(pool) : NULL; - struct AVPacket *avpkt = dp ? dp->avpacket : NULL; - if (!dp) - dp = talloc(NULL, struct demux_packet); - talloc_set_destructor(dp, packet_destroy); *dp = (struct demux_packet) { .pts = MP_NOPTS_VALUE, .dts = MP_NOPTS_VALUE, @@ -71,6 +66,31 @@ static struct demux_packet *packet_create(struct demux_packet_pool *pool) .stream = -1, .animated = -1, }; +} + +// This frees a packet backing allocations, but does not free dp itself, or +// AVPacket inside dp, which is restored to default state. +void demux_packet_unref(struct demux_packet *dp) +{ + if (!dp) + return; + struct AVPacket *avpkt = dp ? dp->avpacket : NULL; + if (avpkt) + av_packet_unref(avpkt); + ta_free_children(dp); + set_packet_defaults(dp); + dp->avpacket = avpkt; +} + +static struct demux_packet *packet_create(struct demux_packet_pool *pool) +{ + struct demux_packet *dp = pool ? demux_packet_pool_pop(pool) : NULL; + struct AVPacket *avpkt = dp ? dp->avpacket : NULL; + if (!dp) { + dp = talloc(NULL, struct demux_packet); + talloc_set_destructor(dp, packet_destroy); + } + set_packet_defaults(dp); dp->avpacket = avpkt ? avpkt : av_packet_alloc(); MP_HANDLE_OOM(dp->avpacket); return dp; diff --git a/demux/packet.h b/demux/packet.h index ee2d3e31e3..b959d513db 100644 --- a/demux/packet.h +++ b/demux/packet.h @@ -95,5 +95,6 @@ int demux_packet_add_blockadditional(struct demux_packet *dp, uint64_t id, void *data, size_t size); void demux_packet_unref_contents(struct demux_packet *dp); +void demux_packet_unref(struct demux_packet *dp); #endif /* MPLAYER_DEMUX_PACKET_H */