[2/2] avpacket: use av_packet_copy_side_data() in av_packet_copy_props()

Message ID 20170925032857.8324-2-jamrial@gmail.com
State New
Headers show

Commit Message

James Almer Sept. 25, 2017, 3:28 a.m.
The side data array in av_packet_copy_side_data() is not reallocated every
time a new element is added, unlike when calling av_packet_new_side_data()
in a loop.

Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavcodec/avpacket.c | 19 +++++--------------
 1 file changed, 5 insertions(+), 14 deletions(-)

Comments

James Almer Sept. 25, 2017, 6:19 p.m. | #1
On 9/25/2017 12:28 AM, James Almer wrote:
> The side data array in av_packet_copy_side_data() is not reallocated every
> time a new element is added, unlike when calling av_packet_new_side_data()
> in a loop.

Regarding this, I don't know if "merging" the potentially existing side
data in dst with the copied side data of src is an intended behavior of
av_packet_copy_props(), but that's in fact what it currently does.

This patch however would make side data in dst be an exact copy of that
from src.

> 
> Signed-off-by: James Almer <jamrial@gmail.com>
> ---
>  libavcodec/avpacket.c | 19 +++++--------------
>  1 file changed, 5 insertions(+), 14 deletions(-)
> 
> diff --git a/libavcodec/avpacket.c b/libavcodec/avpacket.c
> index f20c191c1..063ecedd3 100644
> --- a/libavcodec/avpacket.c
> +++ b/libavcodec/avpacket.c
> @@ -359,7 +359,7 @@ fail:
>  
>  int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
>  {
> -    int i;
> +    int ret;
>  
>      dst->pts                  = src->pts;
>      dst->dts                  = src->dts;
> @@ -373,20 +373,11 @@ FF_ENABLE_DEPRECATION_WARNINGS
>      dst->flags                = src->flags;
>      dst->stream_index         = src->stream_index;
>  
> -    for (i = 0; i < src->side_data_elems; i++) {
> -         enum AVPacketSideDataType type = src->side_data[i].type;
> -         int size          = src->side_data[i].size;
> -         uint8_t *src_data = src->side_data[i].data;
> -         uint8_t *dst_data = av_packet_new_side_data(dst, type, size);
> -
> -        if (!dst_data) {
> -            av_packet_free_side_data(dst);
> -            return AVERROR(ENOMEM);
> -        }
> -        memcpy(dst_data, src_data, size);
> -    }
> +    ret = av_packet_copy_side_data(dst, src);
> +    if (ret < 0)
> +        av_packet_free_side_data(dst);
>  
> -    return 0;
> +    return ret;
>  }
>  
>  void av_packet_unref(AVPacket *pkt)
>

Patch

diff --git a/libavcodec/avpacket.c b/libavcodec/avpacket.c
index f20c191c1..063ecedd3 100644
--- a/libavcodec/avpacket.c
+++ b/libavcodec/avpacket.c
@@ -359,7 +359,7 @@  fail:
 
 int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
 {
-    int i;
+    int ret;
 
     dst->pts                  = src->pts;
     dst->dts                  = src->dts;
@@ -373,20 +373,11 @@  FF_ENABLE_DEPRECATION_WARNINGS
     dst->flags                = src->flags;
     dst->stream_index         = src->stream_index;
 
-    for (i = 0; i < src->side_data_elems; i++) {
-         enum AVPacketSideDataType type = src->side_data[i].type;
-         int size          = src->side_data[i].size;
-         uint8_t *src_data = src->side_data[i].data;
-         uint8_t *dst_data = av_packet_new_side_data(dst, type, size);
-
-        if (!dst_data) {
-            av_packet_free_side_data(dst);
-            return AVERROR(ENOMEM);
-        }
-        memcpy(dst_data, src_data, size);
-    }
+    ret = av_packet_copy_side_data(dst, src);
+    if (ret < 0)
+        av_packet_free_side_data(dst);
 
-    return 0;
+    return ret;
 }
 
 void av_packet_unref(AVPacket *pkt)