[FFmpeg-devel] [PATCH 14/42] avcodec/hevcdec: Use RefStruct API for RefPicListTap buffer

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Tue Sep 19 22:57:06 EEST 2023


Given that the RefStruct API relies on the user to know
the size of the objects and does not provide a way to get it,
we need to store the number of elements allocated ourselves;
but this is actually better than deriving it from the size
in bytes.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
 libavcodec/hevc_refs.c | 14 ++++++++------
 libavcodec/hevcdec.c   |  5 ++---
 libavcodec/hevcdec.h   |  3 ++-
 3 files changed, 12 insertions(+), 10 deletions(-)

diff --git a/libavcodec/hevc_refs.c b/libavcodec/hevc_refs.c
index 8f49704b54..ae464e8e6d 100644
--- a/libavcodec/hevc_refs.c
+++ b/libavcodec/hevc_refs.c
@@ -45,7 +45,8 @@ void ff_hevc_unref_frame(HEVCContext *s, HEVCFrame *frame, int flags)
         av_buffer_unref(&frame->tab_mvf_buf);
         frame->tab_mvf = NULL;
 
-        av_buffer_unref(&frame->rpl_buf);
+        ff_refstruct_unref(&frame->rpl);
+        frame->nb_rpl_elems = 0;
         av_buffer_unref(&frame->rpl_tab_buf);
         frame->rpl_tab    = NULL;
         frame->refPicList = NULL;
@@ -95,9 +96,10 @@ static HEVCFrame *alloc_frame(HEVCContext *s)
         if (ret < 0)
             return NULL;
 
-        frame->rpl_buf = av_buffer_allocz(s->pkt.nb_nals * sizeof(RefPicListTab));
-        if (!frame->rpl_buf)
+        frame->rpl = ff_refstruct_allocz(s->pkt.nb_nals * sizeof(*frame->rpl));
+        if (!frame->rpl)
             goto fail;
+        frame->nb_rpl_elems = s->pkt.nb_nals;
 
         frame->tab_mvf_buf = av_buffer_pool_get(s->tab_mvf_pool);
         if (!frame->tab_mvf_buf)
@@ -110,7 +112,7 @@ static HEVCFrame *alloc_frame(HEVCContext *s)
         frame->rpl_tab   = (RefPicListTab **)frame->rpl_tab_buf->data;
         frame->ctb_count = s->ps.sps->ctb_width * s->ps.sps->ctb_height;
         for (j = 0; j < frame->ctb_count; j++)
-            frame->rpl_tab[j] = (RefPicListTab *)frame->rpl_buf->data;
+            frame->rpl_tab[j] = frame->rpl;
 
         if (s->sei.picture_timing.picture_struct == AV_PICTURE_STRUCTURE_TOP_FIELD)
             frame->frame->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
@@ -295,11 +297,11 @@ static int init_slice_rpl(HEVCContext *s)
     int ctb_addr_ts  = s->ps.pps->ctb_addr_rs_to_ts[s->sh.slice_segment_addr];
     int i;
 
-    if (s->slice_idx >= frame->rpl_buf->size / sizeof(RefPicListTab))
+    if (s->slice_idx >= frame->nb_rpl_elems)
         return AVERROR_INVALIDDATA;
 
     for (i = ctb_addr_ts; i < ctb_count; i++)
-        frame->rpl_tab[i] = (RefPicListTab *)frame->rpl_buf->data + s->slice_idx;
+        frame->rpl_tab[i] = frame->rpl + s->slice_idx;
 
     frame->refPicList = (RefPicList *)frame->rpl_tab[ctb_addr_ts];
 
diff --git a/libavcodec/hevcdec.c b/libavcodec/hevcdec.c
index 8316a815e7..44561de821 100644
--- a/libavcodec/hevcdec.c
+++ b/libavcodec/hevcdec.c
@@ -3404,9 +3404,8 @@ static int hevc_ref_frame(HEVCContext *s, HEVCFrame *dst, HEVCFrame *src)
         goto fail;
     dst->rpl_tab = src->rpl_tab;
 
-    dst->rpl_buf = av_buffer_ref(src->rpl_buf);
-    if (!dst->rpl_buf)
-        goto fail;
+    dst->rpl = ff_refstruct_ref(src->rpl);
+    dst->nb_rpl_elems = src->nb_rpl_elems;
 
     dst->poc        = src->poc;
     dst->ctb_count  = src->ctb_count;
diff --git a/libavcodec/hevcdec.h b/libavcodec/hevcdec.h
index 1d29fc24d7..c13406f0a8 100644
--- a/libavcodec/hevcdec.h
+++ b/libavcodec/hevcdec.h
@@ -417,7 +417,8 @@ typedef struct HEVCFrame {
 
     AVBufferRef *tab_mvf_buf;
     AVBufferRef *rpl_tab_buf;
-    AVBufferRef *rpl_buf;
+    RefPicListTab *rpl;            ///< RefStruct reference
+    int nb_rpl_elems;
 
     void *hwaccel_picture_private; ///< RefStruct reference
 
-- 
2.34.1



More information about the ffmpeg-devel mailing list