diff mbox series

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

Message ID 20200903040311.53886-1-jamrial@gmail.com
State New
Headers show
Series [FFmpeg-devel,1/3] avcodec/packet: factorize calls to packet_alloc()
Related show

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

James Almer Sept. 3, 2020, 4:03 a.m. UTC
Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavcodec/avpacket.c | 35 +++++++++++++++++++++++------------
 1 file changed, 23 insertions(+), 12 deletions(-)
diff mbox series

Patch

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;