diff mbox series

[FFmpeg-devel,40/42] avcodec/hevcdec: Switch to ProgressFrames

Message ID AS8P250MB074468D8CF9B736D421B2DAF8FFAA@AS8P250MB0744.EURP250.PROD.OUTLOOK.COM
State New
Headers show
Series New API for reference counting and ThreadFrames | expand

Commit Message

Andreas Rheinhardt Sept. 19, 2023, 7:57 p.m. UTC
Avoids implicit av_frame_ref() and therefore allocations
and error checks. It also avoids explicitly allocating
the AVFrames (done implicitly when getting the buffer).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/hevc_filter.c |  8 ++++----
 libavcodec/hevc_mvs.c    |  6 +++---
 libavcodec/hevc_refs.c   | 22 ++++++++++------------
 libavcodec/hevcdec.c     | 24 ++++++++++--------------
 libavcodec/hevcdec.h     |  4 ++--
 5 files changed, 29 insertions(+), 35 deletions(-)

Comments

Anton Khirnov Nov. 9, 2023, 9:50 a.m. UTC | #1
Quoting Andreas Rheinhardt (2023-09-19 21:57:32)
> Avoids implicit av_frame_ref() and therefore allocations
> and error checks. It also avoids explicitly allocating
> the AVFrames (done implicitly when getting the buffer).
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
> ---
>  libavcodec/hevc_filter.c |  8 ++++----
>  libavcodec/hevc_mvs.c    |  6 +++---
>  libavcodec/hevc_refs.c   | 22 ++++++++++------------
>  libavcodec/hevcdec.c     | 24 ++++++++++--------------
>  libavcodec/hevcdec.h     |  4 ++--
>  5 files changed, 29 insertions(+), 35 deletions(-)

Looks ok
diff mbox series

Patch

diff --git a/libavcodec/hevc_filter.c b/libavcodec/hevc_filter.c
index 0c45310ea6..07bae34782 100644
--- a/libavcodec/hevc_filter.c
+++ b/libavcodec/hevc_filter.c
@@ -26,7 +26,7 @@ 
 #include "libavutil/internal.h"
 
 #include "hevcdec.h"
-#include "threadframe.h"
+#include "progressframe.h"
 
 #define LUMA 0
 #define CB 1
@@ -874,15 +874,15 @@  void ff_hevc_hls_filter(HEVCLocalContext *lc, int x, int y, int ctb_size)
         if (y && x_end) {
             sao_filter_CTB(lc, s, x, y - ctb_size);
             if (s->threads_type & FF_THREAD_FRAME )
-                ff_thread_report_progress(&s->ref->tf, y, 0);
+                ff_thread_progress_report(&s->ref->tf, y);
         }
         if (x_end && y_end) {
             sao_filter_CTB(lc, s, x , y);
             if (s->threads_type & FF_THREAD_FRAME )
-                ff_thread_report_progress(&s->ref->tf, y + ctb_size, 0);
+                ff_thread_progress_report(&s->ref->tf, y + ctb_size);
         }
     } else if (s->threads_type & FF_THREAD_FRAME && x_end)
-        ff_thread_report_progress(&s->ref->tf, y + ctb_size - 4, 0);
+        ff_thread_progress_report(&s->ref->tf, y + ctb_size - 4);
 }
 
 void ff_hevc_hls_filters(HEVCLocalContext *lc, int x_ctb, int y_ctb, int ctb_size)
diff --git a/libavcodec/hevc_mvs.c b/libavcodec/hevc_mvs.c
index 0a8cc2c43d..be02d52b3e 100644
--- a/libavcodec/hevc_mvs.c
+++ b/libavcodec/hevc_mvs.c
@@ -23,7 +23,7 @@ 
 
 #include "hevc.h"
 #include "hevcdec.h"
-#include "threadframe.h"
+#include "progressframe.h"
 
 static const uint8_t l0_l1_cand_idx[12][2] = {
     { 0, 1, },
@@ -248,7 +248,7 @@  static int temporal_luma_motion_vector(const HEVCContext *s, int x0, int y0,
         x                 &= ~15;
         y                 &= ~15;
         if (s->threads_type == FF_THREAD_FRAME)
-            ff_thread_await_progress(&ref->tf, y, 0);
+            ff_thread_progress_await(&ref->tf, y);
         x_pu               = x >> s->ps.sps->log2_min_pu_size;
         y_pu               = y >> s->ps.sps->log2_min_pu_size;
         temp_col           = TAB_MVF(x_pu, y_pu);
@@ -262,7 +262,7 @@  static int temporal_luma_motion_vector(const HEVCContext *s, int x0, int y0,
         x                 &= ~15;
         y                 &= ~15;
         if (s->threads_type == FF_THREAD_FRAME)
-            ff_thread_await_progress(&ref->tf, y, 0);
+            ff_thread_progress_await(&ref->tf, y);
         x_pu               = x >> s->ps.sps->log2_min_pu_size;
         y_pu               = y >> s->ps.sps->log2_min_pu_size;
         temp_col           = TAB_MVF(x_pu, y_pu);
diff --git a/libavcodec/hevc_refs.c b/libavcodec/hevc_refs.c
index 9cdc4233df..afb9ff0cc0 100644
--- a/libavcodec/hevc_refs.c
+++ b/libavcodec/hevc_refs.c
@@ -27,18 +27,15 @@ 
 #include "thread.h"
 #include "hevc.h"
 #include "hevcdec.h"
+#include "progressframe.h"
 #include "refstruct.h"
-#include "threadframe.h"
 
 void ff_hevc_unref_frame(HEVCContext *s, HEVCFrame *frame, int flags)
 {
-    /* frame->frame can be NULL if context init failed */
-    if (!frame->frame || !frame->frame->buf[0])
-        return;
-
     frame->flags &= ~flags;
     if (!frame->flags) {
-        ff_thread_release_ext_buffer(s->avctx, &frame->tf);
+        ff_thread_progress_unref(s->avctx, &frame->tf);
+        frame->frame = NULL;
         ff_thread_release_buffer(s->avctx, frame->frame_grain);
         frame->needs_fg = 0;
 
@@ -84,13 +81,14 @@  static HEVCFrame *alloc_frame(HEVCContext *s)
     int i, j, ret;
     for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
         HEVCFrame *frame = &s->DPB[i];
-        if (frame->frame->buf[0])
+        if (frame->frame)
             continue;
 
-        ret = ff_thread_get_ext_buffer(s->avctx, &frame->tf,
-                                       AV_GET_BUFFER_FLAG_REF);
+        ret = ff_thread_progress_get_buffer(s->avctx, &frame->tf,
+                                            AV_GET_BUFFER_FLAG_REF);
         if (ret < 0)
             return NULL;
+        frame->frame = frame->tf.f;
 
         frame->rpl = ff_refstruct_allocz(s->pkt.nb_nals * sizeof(*frame->rpl));
         if (!frame->rpl)
@@ -136,7 +134,7 @@  int ff_hevc_set_new_ref(HEVCContext *s, AVFrame **frame, int poc)
     for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
         HEVCFrame *frame = &s->DPB[i];
 
-        if (frame->frame->buf[0] && frame->sequence == s->seq_decode &&
+        if (frame->frame && frame->sequence == s->seq_decode &&
             frame->poc == poc) {
             av_log(s->avctx, AV_LOG_ERROR, "Duplicate POC in a sequence: %d.\n",
                    poc);
@@ -395,7 +393,7 @@  static HEVCFrame *find_ref_idx(HEVCContext *s, int poc, uint8_t use_msb)
 
     for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
         HEVCFrame *ref = &s->DPB[i];
-        if (ref->frame->buf[0] && ref->sequence == s->seq_decode) {
+        if (ref->frame && ref->sequence == s->seq_decode) {
             if ((ref->poc & mask) == poc && (use_msb || ref->poc != s->poc))
                 return ref;
         }
@@ -442,7 +440,7 @@  static HEVCFrame *generate_missing_ref(HEVCContext *s, int poc)
     frame->flags    = 0;
 
     if (s->threads_type == FF_THREAD_FRAME)
-        ff_thread_report_progress(&frame->tf, INT_MAX, 0);
+        ff_thread_progress_report(&frame->tf, INT_MAX);
 
     return frame;
 }
diff --git a/libavcodec/hevcdec.c b/libavcodec/hevcdec.c
index a476a7c7ce..355604a46f 100644
--- a/libavcodec/hevcdec.c
+++ b/libavcodec/hevcdec.c
@@ -47,9 +47,9 @@ 
 #include "hwconfig.h"
 #include "internal.h"
 #include "profiles.h"
+#include "progressframe.h"
 #include "refstruct.h"
 #include "thread.h"
-#include "threadframe.h"
 
 static const uint8_t hevc_pel_weight[65] = { [2] = 0, [4] = 1, [6] = 2, [8] = 3, [12] = 4, [16] = 5, [24] = 6, [32] = 7, [48] = 8, [64] = 9 };
 
@@ -1857,7 +1857,7 @@  static void hevc_await_progress(const HEVCContext *s, const HEVCFrame *ref,
     if (s->threads_type == FF_THREAD_FRAME ) {
         int y = FFMAX(0, (mv->y >> 2) + y0 + height + 9);
 
-        ff_thread_await_progress(&ref->tf, y, 0);
+        ff_thread_progress_await(&ref->tf, y);
     }
 }
 
@@ -3210,7 +3210,7 @@  static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
 
 fail:
     if (s->ref && s->threads_type == FF_THREAD_FRAME)
-        ff_thread_report_progress(&s->ref->tf, INT_MAX, 0);
+        ff_thread_progress_report(&s->ref->tf, INT_MAX);
 
     return ret;
 }
@@ -3381,14 +3381,15 @@  static int hevc_ref_frame(HEVCContext *s, HEVCFrame *dst, HEVCFrame *src)
 {
     int ret;
 
-    ret = ff_thread_ref_frame(&dst->tf, &src->tf);
-    if (ret < 0)
-        return ret;
+    ff_thread_progress_ref(&dst->tf, &src->tf);
+    dst->frame = dst->tf.f;
 
     if (src->needs_fg) {
         ret = av_frame_ref(dst->frame_grain, src->frame_grain);
-        if (ret < 0)
+        if (ret < 0) {
+            ff_hevc_unref_frame(s, dst, ~0);
             return ret;
+        }
         dst->needs_fg = 1;
     }
 
@@ -3428,7 +3429,6 @@  static av_cold int hevc_decode_free(AVCodecContext *avctx)
 
     for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
         ff_hevc_unref_frame(s, &s->DPB[i], ~0);
-        av_frame_free(&s->DPB[i].frame);
         av_frame_free(&s->DPB[i].frame_grain);
     }
 
@@ -3474,11 +3474,6 @@  static av_cold int hevc_init_context(AVCodecContext *avctx)
         return AVERROR(ENOMEM);
 
     for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
-        s->DPB[i].frame = av_frame_alloc();
-        if (!s->DPB[i].frame)
-            return AVERROR(ENOMEM);
-        s->DPB[i].tf.f = s->DPB[i].frame;
-
         s->DPB[i].frame_grain = av_frame_alloc();
         if (!s->DPB[i].frame_grain)
             return AVERROR(ENOMEM);
@@ -3510,7 +3505,7 @@  static int hevc_update_thread_context(AVCodecContext *dst,
 
     for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
         ff_hevc_unref_frame(s, &s->DPB[i], ~0);
-        if (s0->DPB[i].frame->buf[0]) {
+        if (s0->DPB[i].frame) {
             ret = hevc_ref_frame(s, &s->DPB[i], &s0->DPB[i]);
             if (ret < 0)
                 return ret;
@@ -3671,6 +3666,7 @@  const FFCodec ff_hevc_decoder = {
     .p.capabilities        = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
                              AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS,
     .caps_internal         = FF_CODEC_CAP_EXPORTS_CROPPING |
+                             FF_CODEC_CAP_USES_PROGRESSFRAMES |
                              FF_CODEC_CAP_INIT_CLEANUP,
     .p.profiles            = NULL_IF_CONFIG_SMALL(ff_hevc_profiles),
     .hw_configs            = (const AVCodecHWConfigInternal *const []) {
diff --git a/libavcodec/hevcdec.h b/libavcodec/hevcdec.h
index bed8716f99..827a26c536 100644
--- a/libavcodec/hevcdec.h
+++ b/libavcodec/hevcdec.h
@@ -40,7 +40,7 @@ 
 #include "hevc_sei.h"
 #include "hevcdsp.h"
 #include "h274.h"
-#include "threadframe.h"
+#include "progressframe.h"
 #include "videodsp.h"
 
 #define SHIFT_CTB_WPP 2
@@ -406,7 +406,7 @@  typedef struct DBParams {
 typedef struct HEVCFrame {
     AVFrame *frame;
     AVFrame *frame_grain;
-    ThreadFrame tf;
+    ProgressFrame tf;
     int needs_fg; /* 1 if grain needs to be applied by the decoder */
     MvField *tab_mvf;              ///< RefStruct reference
     RefPicList *refPicList;