[FFmpeg-cvslog] lavu/buffer: add a convenience function for replacing buffers

Anton Khirnov git at videolan.org
Mon Sep 28 12:48:42 EEST 2020


ffmpeg | branch: master | Anton Khirnov <anton at khirnov.net> | Fri Jun  5 11:17:49 2020 +0200| [68918d3b7fda9e75d436a27705c54b77dcb41ba9] | committer: Anton Khirnov

lavu/buffer: add a convenience function for replacing buffers

A common pattern e.g. in libavcodec is replacing/updating buffer
references: unref old one, ref new one. This function allows simplifying
such code and avoiding unnecessary refs+unrefs if the references are
already equivalent.

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=68918d3b7fda9e75d436a27705c54b77dcb41ba9
---

 doc/APIchanges      |  3 +++
 libavutil/buffer.c  | 26 ++++++++++++++++++++++++++
 libavutil/buffer.h  | 16 ++++++++++++++++
 libavutil/version.h |  2 +-
 4 files changed, 46 insertions(+), 1 deletion(-)

diff --git a/doc/APIchanges b/doc/APIchanges
index e2d7369c83..f2830968bb 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -15,6 +15,9 @@ libavutil:     2017-10-21
 
 API changes, most recent first:
 
+2020-xx-xx - xxxxxxxxxx - lavu 56.60.100 - buffer.h
+  Add a av_buffer_replace() convenience function.
+
 2020-09-xx - xxxxxxxxxx - lavu 56.59.100 - timecode.h
   Add av_timecode_make_smpte_tc_string2.
 
diff --git a/libavutil/buffer.c b/libavutil/buffer.c
index 08e6079139..d67b4bbdaf 100644
--- a/libavutil/buffer.c
+++ b/libavutil/buffer.c
@@ -216,6 +216,32 @@ int av_buffer_realloc(AVBufferRef **pbuf, int size)
     return 0;
 }
 
+int av_buffer_replace(AVBufferRef **pdst, AVBufferRef *src)
+{
+    AVBufferRef *dst = *pdst;
+    AVBufferRef *tmp;
+
+    if (!src) {
+        av_buffer_unref(pdst);
+        return 0;
+    }
+
+    if (dst && dst->buffer == src->buffer) {
+        /* make sure the data pointers match */
+        dst->data = src->data;
+        dst->size = src->size;
+        return 0;
+    }
+
+    tmp = av_buffer_ref(src);
+    if (!tmp)
+        return AVERROR(ENOMEM);
+
+    av_buffer_unref(pdst);
+    *pdst = tmp;
+    return 0;
+}
+
 AVBufferPool *av_buffer_pool_init2(int size, void *opaque,
                                    AVBufferRef* (*alloc)(void *opaque, int size),
                                    void (*pool_free)(void *opaque))
diff --git a/libavutil/buffer.h b/libavutil/buffer.h
index c0f3f6cc9a..fd4e381efa 100644
--- a/libavutil/buffer.h
+++ b/libavutil/buffer.h
@@ -197,6 +197,22 @@ int av_buffer_make_writable(AVBufferRef **buf);
  */
 int av_buffer_realloc(AVBufferRef **buf, int size);
 
+/**
+ * Ensure dst refers to the same data as src.
+ *
+ * When *dst is already equivalent to src, do nothing. Otherwise unreference dst
+ * and replace it with a new reference to src.
+ *
+ * @param dst Pointer to either a valid buffer reference or NULL. On success,
+ *            this will point to a buffer reference equivalent to src. On
+ *            failure, dst will be left untouched.
+ * @param src A buffer reference to replace dst with. May be NULL, then this
+ *            function is equivalent to av_buffer_unref(dst).
+ * @return 0 on success
+ *         AVERROR(ENOMEM) on memory allocation failure.
+ */
+int av_buffer_replace(AVBufferRef **dst, AVBufferRef *src);
+
 /**
  * @}
  */
diff --git a/libavutil/version.h b/libavutil/version.h
index 7028bd2c88..1aca823650 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -79,7 +79,7 @@
  */
 
 #define LIBAVUTIL_VERSION_MAJOR  56
-#define LIBAVUTIL_VERSION_MINOR  59
+#define LIBAVUTIL_VERSION_MINOR  60
 #define LIBAVUTIL_VERSION_MICRO 100
 
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \



More information about the ffmpeg-cvslog mailing list