[FFmpeg-devel,v3,1/6] avutil/frame: Add av_frame_copy_side_data() and av_frame_remove_all_side_data()

Message ID c442597a35f44f3378a4c10588ed007467b0d7f0.1654106506.git.ffmpegagent@gmail.com
State New
Headers
Series Implement SEI parsing for QSV decoders |

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Aman Karmani June 1, 2022, 6:01 p.m. UTC
From: softworkz <softworkz@hotmail.com>

Signed-off-by: softworkz <softworkz@hotmail.com>
Signed-off-by: Anton Khirnov <anton@khirnov.net>
---
 doc/APIchanges      |  4 +++
 libavutil/frame.c   | 67 +++++++++++++++++++++++++++------------------
 libavutil/frame.h   | 32 ++++++++++++++++++++++
 libavutil/version.h |  2 +-
 4 files changed, 78 insertions(+), 27 deletions(-)
  

Comments

Xiang, Haihao June 24, 2022, 7:01 a.m. UTC | #1
On Wed, 2022-06-01 at 18:01 +0000, softworkz wrote:
> From: softworkz <softworkz@hotmail.com>
> 
> Signed-off-by: softworkz <softworkz@hotmail.com>
> Signed-off-by: Anton Khirnov <anton@khirnov.net>
> ---
>  doc/APIchanges      |  4 +++
>  libavutil/frame.c   | 67 +++++++++++++++++++++++++++------------------
>  libavutil/frame.h   | 32 ++++++++++++++++++++++
>  libavutil/version.h |  2 +-
>  4 files changed, 78 insertions(+), 27 deletions(-)
> 
> diff --git a/doc/APIchanges b/doc/APIchanges
> index 337f1466d8..e5dd6f1e83 100644
> --- a/doc/APIchanges
> +++ b/doc/APIchanges
> @@ -14,6 +14,10 @@ libavutil:     2021-04-27
>  
>  API changes, most recent first:
>  
> +2022-05-26 - xxxxxxxxx - lavu 57.26.100 - frame.h
> +  Add av_frame_remove_all_side_data(), av_frame_copy_side_data(),
> +  AV_FRAME_TRANSFER_SD_COPY, and AV_FRAME_TRANSFER_SD_FILTER.
> +
>  2022-05-23 - xxxxxxxxx - lavu 57.25.100 - avutil.h
>    Deprecate av_fopen_utf8() without replacement.
>  
> diff --git a/libavutil/frame.c b/libavutil/frame.c
> index fbb869fffa..bfe575612d 100644
> --- a/libavutil/frame.c
> +++ b/libavutil/frame.c
> @@ -271,9 +271,45 @@ FF_ENABLE_DEPRECATION_WARNINGS
>      return AVERROR(EINVAL);
>  }
>  
> +void av_frame_remove_all_side_data(AVFrame *frame)
> +{
> +    wipe_side_data(frame);
> +}
> +
> +int av_frame_copy_side_data(AVFrame* dst, const AVFrame* src, int flags)
> +{
> +    for (unsigned i = 0; i < src->nb_side_data; i++) {
> +        const AVFrameSideData *sd_src = src->side_data[i];
> +        AVFrameSideData *sd_dst;
> +        if ((flags & AV_FRAME_TRANSFER_SD_FILTER) &&
> +            sd_src->type == AV_FRAME_DATA_PANSCAN &&
> +            (src->width != dst->width || src->height != dst->height))
> +            continue;
> +        if (flags & AV_FRAME_TRANSFER_SD_COPY) {
> +            sd_dst = av_frame_new_side_data(dst, sd_src->type,
> +                                            sd_src->size);
> +            if (!sd_dst) {
> +                wipe_side_data(dst);
> +                return AVERROR(ENOMEM);
> +            }
> +            memcpy(sd_dst->data, sd_src->data, sd_src->size);
> +        } else {
> +            AVBufferRef *ref = av_buffer_ref(sd_src->buf);
> +            sd_dst = av_frame_new_side_data_from_buf(dst, sd_src->type, ref);
> +            if (!sd_dst) {
> +                av_buffer_unref(&ref);
> +                wipe_side_data(dst);
> +                return AVERROR(ENOMEM);
> +            }
> +        }
> +        av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
> +    }
> +    return 0;
> +}
> +
>  static int frame_copy_props(AVFrame *dst, const AVFrame *src, int force_copy)
>  {
> -    int ret, i;
> +    int ret;
>  
>      dst->key_frame              = src->key_frame;
>      dst->pict_type              = src->pict_type;
> @@ -309,31 +345,10 @@ static int frame_copy_props(AVFrame *dst, const AVFrame
> *src, int force_copy)
>  
>      av_dict_copy(&dst->metadata, src->metadata, 0);
>  
> -    for (i = 0; i < src->nb_side_data; i++) {
> -        const AVFrameSideData *sd_src = src->side_data[i];
> -        AVFrameSideData *sd_dst;
> -        if (   sd_src->type == AV_FRAME_DATA_PANSCAN
> -            && (src->width != dst->width || src->height != dst->height))
> -            continue;
> -        if (force_copy) {
> -            sd_dst = av_frame_new_side_data(dst, sd_src->type,
> -                                            sd_src->size);
> -            if (!sd_dst) {
> -                wipe_side_data(dst);
> -                return AVERROR(ENOMEM);
> -            }
> -            memcpy(sd_dst->data, sd_src->data, sd_src->size);
> -        } else {
> -            AVBufferRef *ref = av_buffer_ref(sd_src->buf);
> -            sd_dst = av_frame_new_side_data_from_buf(dst, sd_src->type, ref);
> -            if (!sd_dst) {
> -                av_buffer_unref(&ref);
> -                wipe_side_data(dst);
> -                return AVERROR(ENOMEM);
> -            }
> -        }
> -        av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
> -    }
> +    if ((ret = av_frame_copy_side_data(dst, src,
> +            (force_copy ? AV_FRAME_TRANSFER_SD_COPY : 0) |
> +            AV_FRAME_TRANSFER_SD_FILTER) < 0))
> +        return ret;
>  
>      ret = av_buffer_replace(&dst->opaque_ref, src->opaque_ref);
>      ret |= av_buffer_replace(&dst->private_ref, src->private_ref);
> diff --git a/libavutil/frame.h b/libavutil/frame.h
> index 33fac2054c..a868fa70d7 100644
> --- a/libavutil/frame.h
> +++ b/libavutil/frame.h
> @@ -850,6 +850,30 @@ int av_frame_copy(AVFrame *dst, const AVFrame *src);
>   */
>  int av_frame_copy_props(AVFrame *dst, const AVFrame *src);
>  
> +
> +/**
> + * Copy side data, rather than creating new references.
> + */
> +#define AV_FRAME_TRANSFER_SD_COPY      (1 << 0)
> +/**
> + * Filter out side data that does not match dst properties.
> + */
> +#define AV_FRAME_TRANSFER_SD_FILTER    (1 << 1)
> +
> +/**
> + * Copy all side-data from src to dst.
> + *
> + * @param dst a frame to which the side data should be copied.
> + * @param src a frame from which to copy the side data.
> + * @param flags a combination of AV_FRAME_TRANSFER_SD_*
> + *
> + * @return >= 0 on success, a negative AVERROR on error.

Can it return a positive value on success ? I only see 0 is returned on success
in av_frame_copy_side_data(). May I miss something about your patch ? 

Thanks
Haihao


> + *
> + * @note This function will create new references to side data buffers in
> src,
> + * unless the AV_FRAME_TRANSFER_SD_COPY flag is passed.
> + */
> +int av_frame_copy_side_data(AVFrame* dst, const AVFrame* src, int flags);
> +
>  /**
>   * Get the buffer reference a given data plane is stored in.
>   *
> @@ -901,6 +925,14 @@ AVFrameSideData *av_frame_get_side_data(const AVFrame
> *frame,
>   */
>  void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType
> type);
>  
> +/**
> + * Remove and free all side data instances.
> + *
> + * @param frame from which to remove all side data.
> + */
> +void av_frame_remove_all_side_data(AVFrame *frame);
> +
> +
>  
>  /**
>   * Flags for frame cropping.
> diff --git a/libavutil/version.h b/libavutil/version.h
> index 1b4b41d81f..2c7f4f6b37 100644
> --- a/libavutil/version.h
> +++ b/libavutil/version.h
> @@ -79,7 +79,7 @@
>   */
>  
>  #define LIBAVUTIL_VERSION_MAJOR  57
> -#define LIBAVUTIL_VERSION_MINOR  25
> +#define LIBAVUTIL_VERSION_MINOR  26
>  #define LIBAVUTIL_VERSION_MICRO 100
>  
>  #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
  
Soft Works June 26, 2022, 11:35 p.m. UTC | #2
> -----Original Message-----
> From: Xiang, Haihao <haihao.xiang@intel.com>
> Sent: Friday, June 24, 2022 9:02 AM
> To: ffmpeg-devel@ffmpeg.org
> Cc: haihao.xiang-at-intel.com@ffmpeg.org; softworkz@hotmail.com
> Subject: Re: [FFmpeg-devel] [PATCH v3 1/6] avutil/frame: Add
> av_frame_copy_side_data() and av_frame_remove_all_side_data()
> 
> On Wed, 2022-06-01 at 18:01 +0000, softworkz wrote:
> > From: softworkz <softworkz@hotmail.com>
> >
> > Signed-off-by: softworkz <softworkz@hotmail.com>
> > Signed-off-by: Anton Khirnov <anton@khirnov.net>
> > ---
> >  doc/APIchanges      |  4 +++
> >  libavutil/frame.c   | 67 +++++++++++++++++++++++++++--------------
> ----
> >  libavutil/frame.h   | 32 ++++++++++++++++++++++
> >  libavutil/version.h |  2 +-
> >  4 files changed, 78 insertions(+), 27 deletions(-)
> >
> > diff --git a/doc/APIchanges b/doc/APIchanges
> > index 337f1466d8..e5dd6f1e83 100644
> > --- a/doc/APIchanges
> > +++ b/doc/APIchanges
> > @@ -14,6 +14,10 @@ libavutil:     2021-04-27
> >
> >  API changes, most recent first:
> >
> > +2022-05-26 - xxxxxxxxx - lavu 57.26.100 - frame.h
> > +  Add av_frame_remove_all_side_data(), av_frame_copy_side_data(),
> > +  AV_FRAME_TRANSFER_SD_COPY, and AV_FRAME_TRANSFER_SD_FILTER.
> > +
> >  2022-05-23 - xxxxxxxxx - lavu 57.25.100 - avutil.h
> >    Deprecate av_fopen_utf8() without replacement.
> >
> > diff --git a/libavutil/frame.c b/libavutil/frame.c
> > index fbb869fffa..bfe575612d 100644
> > --- a/libavutil/frame.c
> > +++ b/libavutil/frame.c
> > @@ -271,9 +271,45 @@ FF_ENABLE_DEPRECATION_WARNINGS
> >      return AVERROR(EINVAL);
> >  }
> >
> > +void av_frame_remove_all_side_data(AVFrame *frame)
> > +{
> > +    wipe_side_data(frame);
> > +}
> > +
> > +int av_frame_copy_side_data(AVFrame* dst, const AVFrame* src, int
> flags)
> > +{
> > +    for (unsigned i = 0; i < src->nb_side_data; i++) {
> > +        const AVFrameSideData *sd_src = src->side_data[i];
> > +        AVFrameSideData *sd_dst;
> > +        if ((flags & AV_FRAME_TRANSFER_SD_FILTER) &&
> > +            sd_src->type == AV_FRAME_DATA_PANSCAN &&
> > +            (src->width != dst->width || src->height != dst-
> >height))
> > +            continue;
> > +        if (flags & AV_FRAME_TRANSFER_SD_COPY) {
> > +            sd_dst = av_frame_new_side_data(dst, sd_src->type,
> > +                                            sd_src->size);
> > +            if (!sd_dst) {
> > +                wipe_side_data(dst);
> > +                return AVERROR(ENOMEM);
> > +            }
> > +            memcpy(sd_dst->data, sd_src->data, sd_src->size);
> > +        } else {
> > +            AVBufferRef *ref = av_buffer_ref(sd_src->buf);
> > +            sd_dst = av_frame_new_side_data_from_buf(dst, sd_src-
> >type, ref);
> > +            if (!sd_dst) {
> > +                av_buffer_unref(&ref);
> > +                wipe_side_data(dst);
> > +                return AVERROR(ENOMEM);
> > +            }
> > +        }
> > +        av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
> > +    }
> > +    return 0;
> > +}
> > +
> >  static int frame_copy_props(AVFrame *dst, const AVFrame *src, int
> force_copy)
> >  {
> > -    int ret, i;
> > +    int ret;
> >
> >      dst->key_frame              = src->key_frame;
> >      dst->pict_type              = src->pict_type;
> > @@ -309,31 +345,10 @@ static int frame_copy_props(AVFrame *dst,
> const AVFrame
> > *src, int force_copy)
> >
> >      av_dict_copy(&dst->metadata, src->metadata, 0);
> >
> > -    for (i = 0; i < src->nb_side_data; i++) {
> > -        const AVFrameSideData *sd_src = src->side_data[i];
> > -        AVFrameSideData *sd_dst;
> > -        if (   sd_src->type == AV_FRAME_DATA_PANSCAN
> > -            && (src->width != dst->width || src->height != dst-
> >height))
> > -            continue;
> > -        if (force_copy) {
> > -            sd_dst = av_frame_new_side_data(dst, sd_src->type,
> > -                                            sd_src->size);
> > -            if (!sd_dst) {
> > -                wipe_side_data(dst);
> > -                return AVERROR(ENOMEM);
> > -            }
> > -            memcpy(sd_dst->data, sd_src->data, sd_src->size);
> > -        } else {
> > -            AVBufferRef *ref = av_buffer_ref(sd_src->buf);
> > -            sd_dst = av_frame_new_side_data_from_buf(dst, sd_src-
> >type, ref);
> > -            if (!sd_dst) {
> > -                av_buffer_unref(&ref);
> > -                wipe_side_data(dst);
> > -                return AVERROR(ENOMEM);
> > -            }
> > -        }
> > -        av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
> > -    }
> > +    if ((ret = av_frame_copy_side_data(dst, src,
> > +            (force_copy ? AV_FRAME_TRANSFER_SD_COPY : 0) |
> > +            AV_FRAME_TRANSFER_SD_FILTER) < 0))
> > +        return ret;
> >
> >      ret = av_buffer_replace(&dst->opaque_ref, src->opaque_ref);
> >      ret |= av_buffer_replace(&dst->private_ref, src->private_ref);
> > diff --git a/libavutil/frame.h b/libavutil/frame.h
> > index 33fac2054c..a868fa70d7 100644
> > --- a/libavutil/frame.h
> > +++ b/libavutil/frame.h
> > @@ -850,6 +850,30 @@ int av_frame_copy(AVFrame *dst, const AVFrame
> *src);
> >   */
> >  int av_frame_copy_props(AVFrame *dst, const AVFrame *src);
> >
> > +
> > +/**
> > + * Copy side data, rather than creating new references.
> > + */
> > +#define AV_FRAME_TRANSFER_SD_COPY      (1 << 0)
> > +/**
> > + * Filter out side data that does not match dst properties.
> > + */
> > +#define AV_FRAME_TRANSFER_SD_FILTER    (1 << 1)
> > +
> > +/**
> > + * Copy all side-data from src to dst.
> > + *
> > + * @param dst a frame to which the side data should be copied.
> > + * @param src a frame from which to copy the side data.
> > + * @param flags a combination of AV_FRAME_TRANSFER_SD_*
> > + *
> > + * @return >= 0 on success, a negative AVERROR on error.
> 
> Can it return a positive value on success ? I only see 0 is returned
> on success
> in av_frame_copy_side_data(). May I miss something about your patch ?

I guess I had just copied that message from av_frame_copy() or 
av_frame_apply_cropping(). Both don't seem to return positive 
values either. 

But I don't want to talk it away - I'll submit an update with the
typical doc message.

Thanks,
softworkz
  

Patch

diff --git a/doc/APIchanges b/doc/APIchanges
index 337f1466d8..e5dd6f1e83 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -14,6 +14,10 @@  libavutil:     2021-04-27
 
 API changes, most recent first:
 
+2022-05-26 - xxxxxxxxx - lavu 57.26.100 - frame.h
+  Add av_frame_remove_all_side_data(), av_frame_copy_side_data(),
+  AV_FRAME_TRANSFER_SD_COPY, and AV_FRAME_TRANSFER_SD_FILTER.
+
 2022-05-23 - xxxxxxxxx - lavu 57.25.100 - avutil.h
   Deprecate av_fopen_utf8() without replacement.
 
diff --git a/libavutil/frame.c b/libavutil/frame.c
index fbb869fffa..bfe575612d 100644
--- a/libavutil/frame.c
+++ b/libavutil/frame.c
@@ -271,9 +271,45 @@  FF_ENABLE_DEPRECATION_WARNINGS
     return AVERROR(EINVAL);
 }
 
+void av_frame_remove_all_side_data(AVFrame *frame)
+{
+    wipe_side_data(frame);
+}
+
+int av_frame_copy_side_data(AVFrame* dst, const AVFrame* src, int flags)
+{
+    for (unsigned i = 0; i < src->nb_side_data; i++) {
+        const AVFrameSideData *sd_src = src->side_data[i];
+        AVFrameSideData *sd_dst;
+        if ((flags & AV_FRAME_TRANSFER_SD_FILTER) &&
+            sd_src->type == AV_FRAME_DATA_PANSCAN &&
+            (src->width != dst->width || src->height != dst->height))
+            continue;
+        if (flags & AV_FRAME_TRANSFER_SD_COPY) {
+            sd_dst = av_frame_new_side_data(dst, sd_src->type,
+                                            sd_src->size);
+            if (!sd_dst) {
+                wipe_side_data(dst);
+                return AVERROR(ENOMEM);
+            }
+            memcpy(sd_dst->data, sd_src->data, sd_src->size);
+        } else {
+            AVBufferRef *ref = av_buffer_ref(sd_src->buf);
+            sd_dst = av_frame_new_side_data_from_buf(dst, sd_src->type, ref);
+            if (!sd_dst) {
+                av_buffer_unref(&ref);
+                wipe_side_data(dst);
+                return AVERROR(ENOMEM);
+            }
+        }
+        av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
+    }
+    return 0;
+}
+
 static int frame_copy_props(AVFrame *dst, const AVFrame *src, int force_copy)
 {
-    int ret, i;
+    int ret;
 
     dst->key_frame              = src->key_frame;
     dst->pict_type              = src->pict_type;
@@ -309,31 +345,10 @@  static int frame_copy_props(AVFrame *dst, const AVFrame *src, int force_copy)
 
     av_dict_copy(&dst->metadata, src->metadata, 0);
 
-    for (i = 0; i < src->nb_side_data; i++) {
-        const AVFrameSideData *sd_src = src->side_data[i];
-        AVFrameSideData *sd_dst;
-        if (   sd_src->type == AV_FRAME_DATA_PANSCAN
-            && (src->width != dst->width || src->height != dst->height))
-            continue;
-        if (force_copy) {
-            sd_dst = av_frame_new_side_data(dst, sd_src->type,
-                                            sd_src->size);
-            if (!sd_dst) {
-                wipe_side_data(dst);
-                return AVERROR(ENOMEM);
-            }
-            memcpy(sd_dst->data, sd_src->data, sd_src->size);
-        } else {
-            AVBufferRef *ref = av_buffer_ref(sd_src->buf);
-            sd_dst = av_frame_new_side_data_from_buf(dst, sd_src->type, ref);
-            if (!sd_dst) {
-                av_buffer_unref(&ref);
-                wipe_side_data(dst);
-                return AVERROR(ENOMEM);
-            }
-        }
-        av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
-    }
+    if ((ret = av_frame_copy_side_data(dst, src,
+            (force_copy ? AV_FRAME_TRANSFER_SD_COPY : 0) |
+            AV_FRAME_TRANSFER_SD_FILTER) < 0))
+        return ret;
 
     ret = av_buffer_replace(&dst->opaque_ref, src->opaque_ref);
     ret |= av_buffer_replace(&dst->private_ref, src->private_ref);
diff --git a/libavutil/frame.h b/libavutil/frame.h
index 33fac2054c..a868fa70d7 100644
--- a/libavutil/frame.h
+++ b/libavutil/frame.h
@@ -850,6 +850,30 @@  int av_frame_copy(AVFrame *dst, const AVFrame *src);
  */
 int av_frame_copy_props(AVFrame *dst, const AVFrame *src);
 
+
+/**
+ * Copy side data, rather than creating new references.
+ */
+#define AV_FRAME_TRANSFER_SD_COPY      (1 << 0)
+/**
+ * Filter out side data that does not match dst properties.
+ */
+#define AV_FRAME_TRANSFER_SD_FILTER    (1 << 1)
+
+/**
+ * Copy all side-data from src to dst.
+ *
+ * @param dst a frame to which the side data should be copied.
+ * @param src a frame from which to copy the side data.
+ * @param flags a combination of AV_FRAME_TRANSFER_SD_*
+ *
+ * @return >= 0 on success, a negative AVERROR on error.
+ *
+ * @note This function will create new references to side data buffers in src,
+ * unless the AV_FRAME_TRANSFER_SD_COPY flag is passed.
+ */
+int av_frame_copy_side_data(AVFrame* dst, const AVFrame* src, int flags);
+
 /**
  * Get the buffer reference a given data plane is stored in.
  *
@@ -901,6 +925,14 @@  AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
  */
 void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type);
 
+/**
+ * Remove and free all side data instances.
+ *
+ * @param frame from which to remove all side data.
+ */
+void av_frame_remove_all_side_data(AVFrame *frame);
+
+
 
 /**
  * Flags for frame cropping.
diff --git a/libavutil/version.h b/libavutil/version.h
index 1b4b41d81f..2c7f4f6b37 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -79,7 +79,7 @@ 
  */
 
 #define LIBAVUTIL_VERSION_MAJOR  57
-#define LIBAVUTIL_VERSION_MINOR  25
+#define LIBAVUTIL_VERSION_MINOR  26
 #define LIBAVUTIL_VERSION_MICRO 100
 
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \