[FFmpeg-devel] [PATCH 1/2] hwaccel: allow to set a user-defined callback for releasing private av buffer.

Sebastien Zwickert dilaroga at gmail.com
Wed May 22 21:53:13 CEST 2013


         Since the new ref counted buffer API vda hwaccel module must have its
         own free callback to release correctly its private buffers.
---
 libavcodec/avcodec.h   |   12 ++++++++++++
 libavcodec/h264.c      |    9 ++++++++-
 libavcodec/mpegvideo.c |    9 ++++++++-
 3 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 7ce9df4..e24a4e6 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -3068,6 +3068,18 @@ typedef struct AVHWAccel {
      * AVCodecContext.release_buffer().
      */
     int priv_data_size;
+
+    /**
+     * Custom free callback wich releases the private picture context.
+     *
+     * opaque data is allocated with av_mallocz() of the size of HWAccel.priv_data_size.
+     * This function is optional, if not set the default free callback
+     * AVBuffer.av_buffer_default_free() is used.
+     *
+     * @param opaque the picture context
+     * @param data the av buffer data
+     */
+    void (*release_buffer)(void *opaque, uint8_t *data);
 } AVHWAccel;
 
 /**
diff --git a/libavcodec/h264.c b/libavcodec/h264.c
index 3d3746f..d693301 100644
--- a/libavcodec/h264.c
+++ b/libavcodec/h264.c
@@ -341,6 +341,7 @@ static int init_table_pools(H264Context *h)
 static int alloc_picture(H264Context *h, Picture *pic)
 {
     int i, ret = 0;
+    void *hw_pic_ctx;
 
     av_assert0(!pic->f.data[0]);
 
@@ -360,7 +361,13 @@ static int alloc_picture(H264Context *h, Picture *pic)
         const AVHWAccel *hwaccel = h->avctx->hwaccel;
         av_assert0(!pic->hwaccel_picture_private);
         if (hwaccel->priv_data_size) {
-            pic->hwaccel_priv_buf = av_buffer_allocz(hwaccel->priv_data_size);
+            if (hwaccel->release_buffer) {
+                hw_pic_ctx = av_mallocz(hwaccel->priv_data_size);
+                pic->hwaccel_priv_buf = av_buffer_create(hw_pic_ctx, hwaccel->priv_data_size,
+                                                         hwaccel->release_buffer, hw_pic_ctx, 0);
+            } else {
+                pic->hwaccel_priv_buf = av_buffer_allocz(hwaccel->priv_data_size);
+            }
             if (!pic->hwaccel_priv_buf)
                 return AVERROR(ENOMEM);
             pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data;
diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c
index 4ac2f1a..5787e9a 100644
--- a/libavcodec/mpegvideo.c
+++ b/libavcodec/mpegvideo.c
@@ -226,6 +226,7 @@ fail:
 static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
 {
     int r, ret;
+    void *hw_pic_ctx;
 
     pic->tf.f = &pic->f;
     if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
@@ -249,7 +250,13 @@ static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
     if (s->avctx->hwaccel) {
         assert(!pic->hwaccel_picture_private);
         if (s->avctx->hwaccel->priv_data_size) {
-            pic->hwaccel_priv_buf = av_buffer_allocz(s->avctx->hwaccel->priv_data_size);
+            if (s->avctx->hwaccel->release_buffer) {
+                hw_pic_ctx = av_mallocz(s->avctx->hwaccel->priv_data_size);
+                pic->hwaccel_priv_buf = av_buffer_create(hw_pic_ctx, s->avctx->hwaccel->priv_data_size,
+                                                         s->avctx->hwaccel->release_buffer, hw_pic_ctx, 0);
+            } else {
+                pic->hwaccel_priv_buf = av_buffer_allocz(s->avctx->hwaccel->priv_data_size);
+            }
             if (!pic->hwaccel_priv_buf) {
                 av_log(s->avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
                 return -1;
-- 
1.7.9.6 (Apple Git-31.1)



More information about the ffmpeg-devel mailing list