From 4d33d316a131c201c3737f6be621013f66f6bd8a Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Sun, 18 Oct 2015 17:02:08 +0200 Subject: avpacket: Provide an alloc and a free function for the struct Pave the way for having the size of the AVPacket struct not part of the ABI. --- libavcodec/avcodec.h | 22 ++++++++++++++++++++++ libavcodec/avpacket.c | 20 ++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index a370e3ebb8..9e6b0defae 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -3412,6 +3412,28 @@ void avsubtitle_free(AVSubtitle *sub); * @{ */ +/** + * Allocate an AVPacket and set its fields to default values. The resulting + * struct must be freed using av_packet_free(). + * + * @return An AVPacket filled with default values or NULL on failure. + * + * @note this only allocates the AVPacket itself, not the data buffers. Those + * must be allocated through other means such as av_new_packet. + * + * @see av_new_packet + */ +AVPacket *av_packet_alloc(void); + +/** + * Free the packet, if the packet is reference counted, it will be + * unreferenced first. + * + * @param packet packet to be freed. The pointer will be set to NULL. + * @note passing NULL is a no-op. + */ +void av_packet_free(AVPacket **pkt); + /** * Initialize optional fields of a packet with default values. * diff --git a/libavcodec/avpacket.c b/libavcodec/avpacket.c index cec5bf89c3..01a250a872 100644 --- a/libavcodec/avpacket.c +++ b/libavcodec/avpacket.c @@ -46,6 +46,26 @@ FF_ENABLE_DEPRECATION_WARNINGS pkt->side_data_elems = 0; } +AVPacket *av_packet_alloc(void) +{ + AVPacket *pkt = av_mallocz(sizeof(AVPacket)); + if (!pkt) + return pkt; + + av_packet_unref(pkt); + + return pkt; +} + +void av_packet_free(AVPacket **pkt) +{ + if (!pkt || !*pkt) + return; + + av_packet_unref(*pkt); + av_freep(pkt); +} + static int packet_alloc(AVBufferRef **buf, int size) { int ret; -- cgit v1.2.3