]> git.sesse.net Git - ffmpeg/commitdiff
avcodec/avpacket: add av_packet_make_writable()
authorJames Almer <jamrial@gmail.com>
Mon, 19 Mar 2018 15:39:08 +0000 (12:39 -0300)
committerJames Almer <jamrial@gmail.com>
Thu, 22 Mar 2018 01:17:40 +0000 (22:17 -0300)
Useful as well to quickly make a packet reference counted when it
isn't already so.

Signed-off-by: James Almer <jamrial@gmail.com>
doc/APIchanges
libavcodec/avcodec.h
libavcodec/avpacket.c
libavcodec/version.h

index 4c0ee7147a15ae54e1141b59ae6a419885f6426f..d410bcdd75629f6d305fa1644e7f1c5c27892198 100644 (file)
@@ -15,6 +15,9 @@ libavutil:     2017-10-21
 
 API changes, most recent first:
 
+2018-03-21 - xxxxxxx - lavc 58.15.100 - avcodec.h
+  Add av_packet_make_writable().
+
 2018-03-18 - xxxxxxx - lavu 56.11.100 - frame.h
   Add AV_FRAME_DATA_QP_TABLE_PROPERTIES and AV_FRAME_DATA_QP_TABLE_DATA.
 
index a8322fb62a2f27400c7d0273f645ab8d362c9f7a..a78017f1fbd4b6d5c5308ca2f4313aba5ab36250 100644 (file)
@@ -4518,6 +4518,17 @@ void av_packet_move_ref(AVPacket *dst, AVPacket *src);
  */
 int av_packet_copy_props(AVPacket *dst, const AVPacket *src);
 
+/**
+ * Create a writable reference for the data described by a given packet,
+ * avoiding data copy if possible.
+ *
+ * @param pkt Packet whose data should be made writable.
+ *
+ * @return 0 on success, a negative AVERROR on failure. On failure, the
+ *         packet is unchanged.
+ */
+int av_packet_make_writable(AVPacket *pkt);
+
 /**
  * Convert valid timing fields (timestamps / durations) in a packet from one
  * timebase to another. Timestamps with unknown values (AV_NOPTS_VALUE) will be
index fe8113ab764553cdfded8dcb9cf814ff9e3652c4..0693ca6f62f586a49fa8a47d4c49b96a23232487 100644 (file)
@@ -652,6 +652,30 @@ void av_packet_move_ref(AVPacket *dst, AVPacket *src)
     src->size = 0;
 }
 
+int av_packet_make_writable(AVPacket *pkt)
+{
+    AVBufferRef *buf = NULL;
+    int ret;
+
+    if (pkt->buf && av_buffer_is_writable(pkt->buf))
+        return 0;
+
+    if (!pkt->data)
+        return AVERROR(EINVAL);
+
+    ret = packet_alloc(&buf, pkt->size);
+    if (ret < 0)
+        return ret;
+    if (pkt->size)
+        memcpy(buf->data, pkt->data, pkt->size);
+
+    av_buffer_unref(&pkt->buf);
+    pkt->buf  = buf;
+    pkt->data = buf->data;
+
+    return 0;
+}
+
 void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
 {
     if (pkt->pts != AV_NOPTS_VALUE)
index ed34095c77675e7278d4dbdc20b736d8483a896a..a5b7f752d1666cccdfbc140ce254283524a0dbb8 100644 (file)
@@ -28,7 +28,7 @@
 #include "libavutil/version.h"
 
 #define LIBAVCODEC_VERSION_MAJOR  58
-#define LIBAVCODEC_VERSION_MINOR  14
+#define LIBAVCODEC_VERSION_MINOR  15
 #define LIBAVCODEC_VERSION_MICRO 100
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \