diff mbox

[FFmpeg-devel,1/2] AVFrame: add an opaque_ref field

Message ID 20170211131737.18139-2-nfxjfg@googlemail.com
State Accepted
Headers show

Commit Message

wm4 Feb. 11, 2017, 1:17 p.m. UTC
This is an extended version of the AVFrame.opaque field, which can be
used to attach arbitrary user information to an AVFrame.

The usefulness of the opaque field is rather limited, because it can
store only up to 32 bits of information (or 64 bit on 64 bit systems).
It's not possible to set this field to a memory allocation, because
there is no way to deallocate it correctly.

The opaque_ref field circumvents this by letting the user set an
AVBuffer, which makes the user data refcounted.

Signed-off-by: Anton Khirnov <anton@khirnov.net>

Merges Libav commit 04f3bd349651.
---
 doc/APIchanges      |  3 +++
 libavutil/frame.c   |  9 +++++++++
 libavutil/frame.h   | 11 +++++++++++
 libavutil/version.h |  2 +-
 4 files changed, 24 insertions(+), 1 deletion(-)

Comments

Michael Niedermayer Feb. 11, 2017, 4:30 p.m. UTC | #1
On Sat, Feb 11, 2017 at 02:17:36PM +0100, wm4 wrote:
> This is an extended version of the AVFrame.opaque field, which can be
> used to attach arbitrary user information to an AVFrame.
> 
> The usefulness of the opaque field is rather limited, because it can
> store only up to 32 bits of information (or 64 bit on 64 bit systems).
> It's not possible to set this field to a memory allocation, because
> there is no way to deallocate it correctly.
> 
> The opaque_ref field circumvents this by letting the user set an
> AVBuffer, which makes the user data refcounted.
> 
> Signed-off-by: Anton Khirnov <anton@khirnov.net>
> 
> Merges Libav commit 04f3bd349651.
> ---
>  doc/APIchanges      |  3 +++
>  libavutil/frame.c   |  9 +++++++++
>  libavutil/frame.h   | 11 +++++++++++
>  libavutil/version.h |  2 +-
>  4 files changed, 24 insertions(+), 1 deletion(-)
> 
> diff --git a/doc/APIchanges b/doc/APIchanges
> index 8bca71ef36..81e49c22b9 100644
> --- a/doc/APIchanges
> +++ b/doc/APIchanges
> @@ -15,6 +15,9 @@ libavutil:     2015-08-28
>  
>  API changes, most recent first:
>  
> +2017-02-11 - xxxxxxx - lavu 55.47.100 - frame.h
> +  Add AVFrame.opaque_ref.
> +
>  2017-01-31 - xxxxxxx - lavu 55.46.100 / 55.20.0 - cpu.h
>    Add AV_CPU_FLAG_SSSE3SLOW.
>  
> diff --git a/libavutil/frame.c b/libavutil/frame.c
> index a08e0c539d..2913982e91 100644
> --- a/libavutil/frame.c
> +++ b/libavutil/frame.c
> @@ -379,6 +379,13 @@ FF_DISABLE_DEPRECATION_WARNINGS
>  FF_ENABLE_DEPRECATION_WARNINGS
>  #endif
>  
> +    av_buffer_unref(&dst->opaque_ref);
> +    if (src->opaque_ref) {
> +        dst->opaque_ref = av_buffer_ref(src->opaque_ref);
> +        if (!dst->opaque_ref)
> +            return AVERROR(ENOMEM);
> +    }

not just here but
the error handling in the whole function is not optimal,
ideally the destination frame should be left intact on error while
this could leave a half copied, unref and left as is mix


> +
>      return 0;
>  }
>  
> @@ -513,6 +520,8 @@ void av_frame_unref(AVFrame *frame)
>  
>      av_buffer_unref(&frame->hw_frames_ctx);
>  
> +    av_buffer_unref(&frame->opaque_ref);
> +
>      get_frame_defaults(frame);
>  }
>  
> diff --git a/libavutil/frame.h b/libavutil/frame.h
> index b4500923af..3023559beb 100644
> --- a/libavutil/frame.h
> +++ b/libavutil/frame.h
> @@ -538,6 +538,17 @@ typedef struct AVFrame {
>       * AVHWFramesContext describing the frame.
>       */
>      AVBufferRef *hw_frames_ctx;
> +
> +    /**
> +     * AVBufferRef for free use by the API user. Libav will never check the
> +     * contents of the buffer ref. Libav calls av_buffer_unref() on it when

wrong project name

[...]
wm4 Feb. 13, 2017, 7:33 a.m. UTC | #2
On Sat, 11 Feb 2017 17:30:24 +0100
Michael Niedermayer <michael@niedermayer.cc> wrote:

> On Sat, Feb 11, 2017 at 02:17:36PM +0100, wm4 wrote:
> > This is an extended version of the AVFrame.opaque field, which can be
> > used to attach arbitrary user information to an AVFrame.
> > 
> > The usefulness of the opaque field is rather limited, because it can
> > store only up to 32 bits of information (or 64 bit on 64 bit systems).
> > It's not possible to set this field to a memory allocation, because
> > there is no way to deallocate it correctly.
> > 
> > The opaque_ref field circumvents this by letting the user set an
> > AVBuffer, which makes the user data refcounted.
> > 
> > Signed-off-by: Anton Khirnov <anton@khirnov.net>
> > 
> > Merges Libav commit 04f3bd349651.
> > ---
> >  doc/APIchanges      |  3 +++
> >  libavutil/frame.c   |  9 +++++++++
> >  libavutil/frame.h   | 11 +++++++++++
> >  libavutil/version.h |  2 +-
> >  4 files changed, 24 insertions(+), 1 deletion(-)
> > 
> > diff --git a/doc/APIchanges b/doc/APIchanges
> > index 8bca71ef36..81e49c22b9 100644
> > --- a/doc/APIchanges
> > +++ b/doc/APIchanges
> > @@ -15,6 +15,9 @@ libavutil:     2015-08-28
> >  
> >  API changes, most recent first:
> >  
> > +2017-02-11 - xxxxxxx - lavu 55.47.100 - frame.h
> > +  Add AVFrame.opaque_ref.
> > +
> >  2017-01-31 - xxxxxxx - lavu 55.46.100 / 55.20.0 - cpu.h
> >    Add AV_CPU_FLAG_SSSE3SLOW.
> >  
> > diff --git a/libavutil/frame.c b/libavutil/frame.c
> > index a08e0c539d..2913982e91 100644
> > --- a/libavutil/frame.c
> > +++ b/libavutil/frame.c
> > @@ -379,6 +379,13 @@ FF_DISABLE_DEPRECATION_WARNINGS
> >  FF_ENABLE_DEPRECATION_WARNINGS
> >  #endif
> >  
> > +    av_buffer_unref(&dst->opaque_ref);
> > +    if (src->opaque_ref) {
> > +        dst->opaque_ref = av_buffer_ref(src->opaque_ref);
> > +        if (!dst->opaque_ref)
> > +            return AVERROR(ENOMEM);
> > +    }  
> 
> not just here but
> the error handling in the whole function is not optimal,
> ideally the destination frame should be left intact on error while
> this could leave a half copied, unref and left as is mix

Well, there's not much you can do. Side data in the function has the
same problem. I think if a caller expects consistency, he will have to
unref the destination frame if this function fails. And that should
still work even with this new field.

The alternative would be either unreffing the entire frame as part of
the failure path of this function (a bit too radical?), or carefully
backing up the frame and partially copying back the result on failure
(would be too much?). I thought about working on a byte-copied
temporary AVFrame, but that doesn't help with all the references.

So I left this unchanged.

> 
> > +
> >      return 0;
> >  }
> >  
> > @@ -513,6 +520,8 @@ void av_frame_unref(AVFrame *frame)
> >  
> >      av_buffer_unref(&frame->hw_frames_ctx);
> >  
> > +    av_buffer_unref(&frame->opaque_ref);
> > +
> >      get_frame_defaults(frame);
> >  }
> >  
> > diff --git a/libavutil/frame.h b/libavutil/frame.h
> > index b4500923af..3023559beb 100644
> > --- a/libavutil/frame.h
> > +++ b/libavutil/frame.h
> > @@ -538,6 +538,17 @@ typedef struct AVFrame {
> >       * AVHWFramesContext describing the frame.
> >       */
> >      AVBufferRef *hw_frames_ctx;
> > +
> > +    /**
> > +     * AVBufferRef for free use by the API user. Libav will never check the
> > +     * contents of the buffer ref. Libav calls av_buffer_unref() on it when  
> 
> wrong project name
> 
> [...]

Changed and pushed both patches.
diff mbox

Patch

diff --git a/doc/APIchanges b/doc/APIchanges
index 8bca71ef36..81e49c22b9 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -15,6 +15,9 @@  libavutil:     2015-08-28
 
 API changes, most recent first:
 
+2017-02-11 - xxxxxxx - lavu 55.47.100 - frame.h
+  Add AVFrame.opaque_ref.
+
 2017-01-31 - xxxxxxx - lavu 55.46.100 / 55.20.0 - cpu.h
   Add AV_CPU_FLAG_SSSE3SLOW.
 
diff --git a/libavutil/frame.c b/libavutil/frame.c
index a08e0c539d..2913982e91 100644
--- a/libavutil/frame.c
+++ b/libavutil/frame.c
@@ -379,6 +379,13 @@  FF_DISABLE_DEPRECATION_WARNINGS
 FF_ENABLE_DEPRECATION_WARNINGS
 #endif
 
+    av_buffer_unref(&dst->opaque_ref);
+    if (src->opaque_ref) {
+        dst->opaque_ref = av_buffer_ref(src->opaque_ref);
+        if (!dst->opaque_ref)
+            return AVERROR(ENOMEM);
+    }
+
     return 0;
 }
 
@@ -513,6 +520,8 @@  void av_frame_unref(AVFrame *frame)
 
     av_buffer_unref(&frame->hw_frames_ctx);
 
+    av_buffer_unref(&frame->opaque_ref);
+
     get_frame_defaults(frame);
 }
 
diff --git a/libavutil/frame.h b/libavutil/frame.h
index b4500923af..3023559beb 100644
--- a/libavutil/frame.h
+++ b/libavutil/frame.h
@@ -538,6 +538,17 @@  typedef struct AVFrame {
      * AVHWFramesContext describing the frame.
      */
     AVBufferRef *hw_frames_ctx;
+
+    /**
+     * AVBufferRef for free use by the API user. Libav will never check the
+     * contents of the buffer ref. Libav calls av_buffer_unref() on it when
+     * the frame is unreferenced. av_frame_copy_props() calls create a new
+     * reference with av_buffer_ref() for the target frame's opaque_ref field.
+     *
+     * This is unrelated to the opaque field, although it serves a similar
+     * purpose.
+     */
+    AVBufferRef *opaque_ref;
 } AVFrame;
 
 /**
diff --git a/libavutil/version.h b/libavutil/version.h
index 8866064aac..a8b00bfc64 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -79,7 +79,7 @@ 
  */
 
 #define LIBAVUTIL_VERSION_MAJOR  55
-#define LIBAVUTIL_VERSION_MINOR  46
+#define LIBAVUTIL_VERSION_MINOR  47
 #define LIBAVUTIL_VERSION_MICRO 100
 
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \