[FFmpeg-devel] [PATCH 1/3] avcodec/packet: factorize calls to packet_alloc()

James Almer jamrial at gmail.com
Thu Sep 3 07:03:09 EEST 2020


Signed-off-by: James Almer <jamrial at gmail.com>
---
 libavcodec/avpacket.c | 35 +++++++++++++++++++++++------------
 1 file changed, 23 insertions(+), 12 deletions(-)

diff --git a/libavcodec/avpacket.c b/libavcodec/avpacket.c
index 4801163227..22422e46ce 100644
--- a/libavcodec/avpacket.c
+++ b/libavcodec/avpacket.c
@@ -612,6 +612,23 @@ void av_packet_unref(AVPacket *pkt)
     pkt->size = 0;
 }
 
+static int packet_alloc_and_copy(AVBufferRef **pdst, const uint8_t *data,
+                                 int size)
+{
+    AVBufferRef *dst;
+    int ret;
+
+    ret = packet_alloc(pdst, size);
+    if (ret < 0)
+        return ret;
+    av_assert1(!size || data);
+    dst = *pdst;
+    if (size)
+        memcpy(dst->data, data, size);
+
+    return 0;
+}
+
 int av_packet_ref(AVPacket *dst, const AVPacket *src)
 {
     int ret;
@@ -623,12 +640,10 @@ int av_packet_ref(AVPacket *dst, const AVPacket *src)
         goto fail;
 
     if (!src->buf) {
-        ret = packet_alloc(&dst->buf, src->size);
+        ret = packet_alloc_and_copy(&dst->buf, src->data,
+                                    src->size);
         if (ret < 0)
             goto fail;
-        av_assert1(!src->size || src->data);
-        if (src->size)
-            memcpy(dst->buf->data, src->data, src->size);
 
         dst->data = dst->buf->data;
     } else {
@@ -676,12 +691,10 @@ int av_packet_make_refcounted(AVPacket *pkt)
     if (pkt->buf)
         return 0;
 
-    ret = packet_alloc(&pkt->buf, pkt->size);
+    ret = packet_alloc_and_copy(&pkt->buf, pkt->data,
+                                pkt->size);
     if (ret < 0)
         return ret;
-    av_assert1(!pkt->size || pkt->data);
-    if (pkt->size)
-        memcpy(pkt->buf->data, pkt->data, pkt->size);
 
     pkt->data = pkt->buf->data;
 
@@ -696,12 +709,10 @@ int av_packet_make_writable(AVPacket *pkt)
     if (pkt->buf && av_buffer_is_writable(pkt->buf))
         return 0;
 
-    ret = packet_alloc(&buf, pkt->size);
+    ret = packet_alloc_and_copy(&buf, pkt->data,
+                                pkt->size);
     if (ret < 0)
         return ret;
-    av_assert1(!pkt->size || pkt->data);
-    if (pkt->size)
-        memcpy(buf->data, pkt->data, pkt->size);
 
     av_buffer_unref(&pkt->buf);
     pkt->buf  = buf;
-- 
2.27.0



More information about the ffmpeg-devel mailing list