[FFmpeg-devel] [PATCH] avcodec/avpacket: add av_packet_remove_side_data()

James Almer jamrial at gmail.com
Wed Sep 9 01:35:10 EEST 2020


This helper removes a side data entry from the packet, maintaining the
integrity and order of the remaining entries, if any.

Signed-off-by: James Almer <jamrial at gmail.com>
---
Missing APIChanges entry and version bump.

Couldn't find a place in the tree where it could be used right now, but
it makes the API be more in line with the AVFrame one, so i decided to
write it.

 libavcodec/avpacket.c | 24 ++++++++++++++++++++++++
 libavcodec/packet.h   |  8 ++++++++
 2 files changed, 32 insertions(+)

diff --git a/libavcodec/avpacket.c b/libavcodec/avpacket.c
index 4801163227..61ea81698c 100644
--- a/libavcodec/avpacket.c
+++ b/libavcodec/avpacket.c
@@ -367,6 +367,30 @@ uint8_t *av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType
     return NULL;
 }
 
+void av_packet_remove_side_data(AVPacket *pkt, enum AVPacketSideDataType type)
+{
+    int i, elems;
+
+    for (i = 0; i < pkt->side_data_elems; i++) {
+        if (pkt->side_data[i].type != type)
+            continue;
+
+        elems = pkt->side_data_elems - 1;
+        av_freep(&pkt->side_data[i].data);
+        pkt->side_data[i].size = 0;
+
+        if (i < elems) {
+            memmove(&pkt->side_data[i], &pkt->side_data[i + 1],
+                    (elems - i) * sizeof(*pkt->side_data));
+        }
+        if (!elems)
+            av_freep(&pkt->side_data);
+        pkt->side_data_elems = elems;
+
+        break;
+    }
+}
+
 const char *av_packet_side_data_name(enum AVPacketSideDataType type)
 {
     switch(type) {
diff --git a/libavcodec/packet.h b/libavcodec/packet.h
index 0a19a0eff3..6ce3c91c07 100644
--- a/libavcodec/packet.h
+++ b/libavcodec/packet.h
@@ -595,6 +595,14 @@ int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
 uint8_t* av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type,
                                  int *size);
 
+/**
+ * Remove and free a side data entry of the given type.
+ *
+ * @param pkt packet
+ * @param type side data type to be removed
+ */
+void av_packet_remove_side_data(AVPacket *pkt, enum AVPacketSideDataType type);
+
 #if FF_API_MERGE_SD_API
 attribute_deprecated
 int av_packet_merge_side_data(AVPacket *pkt);
-- 
2.27.0



More information about the ffmpeg-devel mailing list