[FFmpeg-devel] [PATCH 37/42] avcodec/vp8: Convert to ProgressFrame API

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


Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
 libavcodec/vp8.c  | 90 +++++++++++++++--------------------------------
 libavcodec/vp8.h  |  5 +--
 libavcodec/webp.c |  3 +-
 3 files changed, 33 insertions(+), 65 deletions(-)

diff --git a/libavcodec/vp8.c b/libavcodec/vp8.c
index 4a51c551b8..30d22016f5 100644
--- a/libavcodec/vp8.c
+++ b/libavcodec/vp8.c
@@ -34,9 +34,9 @@
 #include "hwaccel_internal.h"
 #include "hwconfig.h"
 #include "mathops.h"
+#include "progressframe.h"
 #include "refstruct.h"
 #include "thread.h"
-#include "threadframe.h"
 #include "vp8.h"
 #include "vp89_rac.h"
 #include "vp8data.h"
@@ -102,9 +102,9 @@ static void free_buffers(VP8Context *s)
 
 static int vp8_alloc_frame(VP8Context *s, VP8Frame *f, int ref)
 {
-    int ret;
-    if ((ret = ff_thread_get_ext_buffer(s->avctx, &f->tf,
-                                        ref ? AV_GET_BUFFER_FLAG_REF : 0)) < 0)
+    int ret = ff_thread_progress_get_buffer(s->avctx, &f->tf,
+                                            ref ? AV_GET_BUFFER_FLAG_REF : 0);
+    if (ret < 0)
         return ret;
     if (!(f->seg_map = ff_refstruct_allocz(s->mb_width * s->mb_height)))
         goto fail;
@@ -116,7 +116,7 @@ static int vp8_alloc_frame(VP8Context *s, VP8Frame *f, int ref)
 
 fail:
     ff_refstruct_unref(&f->seg_map);
-    ff_thread_release_ext_buffer(s->avctx, &f->tf);
+    ff_thread_progress_unref(s->avctx, &f->tf);
     return ret;
 }
 
@@ -124,23 +124,16 @@ static void vp8_release_frame(VP8Context *s, VP8Frame *f)
 {
     ff_refstruct_unref(&f->seg_map);
     ff_refstruct_unref(&f->hwaccel_picture_private);
-    ff_thread_release_ext_buffer(s->avctx, &f->tf);
+    ff_thread_progress_unref(s->avctx, &f->tf);
 }
 
 #if CONFIG_VP8_DECODER
-static int vp8_ref_frame(VP8Context *s, VP8Frame *dst, const VP8Frame *src)
+static void vp8_replace_frame(VP8Context *s, VP8Frame *dst, const VP8Frame *src)
 {
-    int ret;
-
-    vp8_release_frame(s, dst);
-
-    if ((ret = ff_thread_ref_frame(&dst->tf, &src->tf)) < 0)
-        return ret;
+    ff_thread_progress_replace(s->avctx, &dst->tf, &src->tf);
     ff_refstruct_replace(&dst->seg_map, src->seg_map);
     ff_refstruct_replace(&dst->hwaccel_picture_private,
                           src->hwaccel_picture_private);
-
-    return 0;
 }
 #endif /* CONFIG_VP8_DECODER */
 
@@ -183,7 +176,7 @@ static VP8Frame *vp8_find_free_buffer(VP8Context *s)
         av_log(s->avctx, AV_LOG_FATAL, "Ran out of free frames!\n");
         abort();
     }
-    if (frame->tf.f->buf[0])
+    if (frame->tf.f)
         vp8_release_frame(s, frame);
 
     return frame;
@@ -1829,7 +1822,7 @@ static const uint8_t subpel_idx[3][8] = {
  */
 static av_always_inline
 void vp8_mc_luma(VP8Context *s, VP8ThreadData *td, uint8_t *dst,
-                 const ThreadFrame *ref, const VP8mv *mv,
+                 const ProgressFrame *ref, const VP8mv *mv,
                  int x_off, int y_off, int block_w, int block_h,
                  int width, int height, ptrdiff_t linesize,
                  vp8_mc_func mc_func[3][3])
@@ -1846,7 +1839,7 @@ void vp8_mc_luma(VP8Context *s, VP8ThreadData *td, uint8_t *dst,
         y_off += mv->y >> 2;
 
         // edge emulation
-        ff_thread_await_progress(ref, (3 + y_off + block_h + subpel_idx[2][my]) >> 4, 0);
+        ff_thread_progress_await(ref, (3 + y_off + block_h + subpel_idx[2][my]) >> 4);
         src += y_off * linesize + x_off;
         if (x_off < mx_idx || x_off >= width  - block_w - subpel_idx[2][mx] ||
             y_off < my_idx || y_off >= height - block_h - subpel_idx[2][my]) {
@@ -1862,7 +1855,7 @@ void vp8_mc_luma(VP8Context *s, VP8ThreadData *td, uint8_t *dst,
         }
         mc_func[my_idx][mx_idx](dst, linesize, src, src_linesize, block_h, mx, my);
     } else {
-        ff_thread_await_progress(ref, (3 + y_off + block_h) >> 4, 0);
+        ff_thread_progress_await(ref, (3 + y_off + block_h) >> 4);
         mc_func[0][0](dst, linesize, src + y_off * linesize + x_off,
                       linesize, block_h, 0, 0);
     }
@@ -1887,7 +1880,7 @@ void vp8_mc_luma(VP8Context *s, VP8ThreadData *td, uint8_t *dst,
  */
 static av_always_inline
 void vp8_mc_chroma(VP8Context *s, VP8ThreadData *td, uint8_t *dst1,
-                   uint8_t *dst2, const ThreadFrame *ref, const VP8mv *mv,
+                   uint8_t *dst2, const ProgressFrame *ref, const VP8mv *mv,
                    int x_off, int y_off, int block_w, int block_h,
                    int width, int height, ptrdiff_t linesize,
                    vp8_mc_func mc_func[3][3])
@@ -1904,7 +1897,7 @@ void vp8_mc_chroma(VP8Context *s, VP8ThreadData *td, uint8_t *dst1,
         // edge emulation
         src1 += y_off * linesize + x_off;
         src2 += y_off * linesize + x_off;
-        ff_thread_await_progress(ref, (3 + y_off + block_h + subpel_idx[2][my]) >> 3, 0);
+        ff_thread_progress_await(ref, (3 + y_off + block_h + subpel_idx[2][my]) >> 3);
         if (x_off < mx_idx || x_off >= width  - block_w - subpel_idx[2][mx] ||
             y_off < my_idx || y_off >= height - block_h - subpel_idx[2][my]) {
             s->vdsp.emulated_edge_mc(td->edge_emu_buffer,
@@ -1929,7 +1922,7 @@ void vp8_mc_chroma(VP8Context *s, VP8ThreadData *td, uint8_t *dst1,
             mc_func[my_idx][mx_idx](dst2, linesize, src2, linesize, block_h, mx, my);
         }
     } else {
-        ff_thread_await_progress(ref, (3 + y_off + block_h) >> 3, 0);
+        ff_thread_progress_await(ref, (3 + y_off + block_h) >> 3);
         mc_func[0][0](dst1, linesize, src1 + y_off * linesize + x_off, linesize, block_h, 0, 0);
         mc_func[0][0](dst2, linesize, src2 + y_off * linesize + x_off, linesize, block_h, 0, 0);
     }
@@ -1937,7 +1930,7 @@ void vp8_mc_chroma(VP8Context *s, VP8ThreadData *td, uint8_t *dst1,
 
 static av_always_inline
 void vp8_mc_part(VP8Context *s, VP8ThreadData *td, uint8_t *const dst[3],
-                 const ThreadFrame *ref_frame, int x_off, int y_off,
+                 const ProgressFrame *ref_frame, int x_off, int y_off,
                  int bx_off, int by_off, int block_w, int block_h,
                  int width, int height, const VP8mv *mv)
 {
@@ -2002,7 +1995,7 @@ void inter_predict(VP8Context *s, VP8ThreadData *td, uint8_t *const dst[3],
 {
     int x_off = mb_x << 4, y_off = mb_y << 4;
     int width = 16 * s->mb_width, height = 16 * s->mb_height;
-    const ThreadFrame *ref = &s->framep[mb->ref_frame]->tf;
+    const ProgressFrame *ref = &s->framep[mb->ref_frame]->tf;
     const VP8mv *bmv = mb->bmv;
 
     switch (mb->partitioning) {
@@ -2422,7 +2415,7 @@ static av_always_inline int decode_mb_row_no_filter(AVCodecContext *avctx, void
         // if we re-use the same map.
         if (prev_frame && s->segmentation.enabled &&
             !s->segmentation.update_map)
-            ff_thread_await_progress(&prev_frame->tf, mb_y, 0);
+            ff_thread_progress_await(&prev_frame->tf, mb_y);
         mb = s->macroblocks + (s->mb_height - mb_y - 1) * 2;
         memset(mb - 1, 0, sizeof(*mb)); // zero left macroblock
         AV_WN32A(s->intra4x4_pred_mode_left, DC_PRED * 0x01010101);
@@ -2630,7 +2623,7 @@ int vp78_decode_mb_row_sliced(AVCodecContext *avctx, void *tdata, int jobnr,
         td->mv_bounds.mv_max.y -= 64 * num_jobs;
 
         if (avctx->active_thread_type == FF_THREAD_FRAME)
-            ff_thread_report_progress(&curframe->tf, mb_y, 0);
+            ff_thread_progress_report(&curframe->tf, mb_y);
     }
 
     return 0;
@@ -2694,7 +2687,7 @@ int vp78_decode_frame(AVCodecContext *avctx, AVFrame *rframe, int *got_frame,
 
     // release no longer referenced frames
     for (i = 0; i < 5; i++)
-        if (s->frames[i].tf.f->buf[0] &&
+        if (s->frames[i].tf.f &&
             &s->frames[i] != prev_frame &&
             &s->frames[i] != s->framep[VP8_FRAME_PREVIOUS] &&
             &s->frames[i] != s->framep[VP8_FRAME_GOLDEN]   &&
@@ -2723,14 +2716,14 @@ int vp78_decode_frame(AVCodecContext *avctx, AVFrame *rframe, int *got_frame,
         goto err;
     }
 
+    if ((ret = vp8_alloc_frame(s, curframe, referenced)) < 0)
+        goto err;
     if (s->keyframe)
         curframe->tf.f->flags |= AV_FRAME_FLAG_KEY;
     else
         curframe->tf.f->flags &= ~AV_FRAME_FLAG_KEY;
     curframe->tf.f->pict_type = s->keyframe ? AV_PICTURE_TYPE_I
                                             : AV_PICTURE_TYPE_P;
-    if ((ret = vp8_alloc_frame(s, curframe, referenced)) < 0)
-        goto err;
 
     // check if golden and altref are swapped
     if (s->update_altref != VP8_FRAME_NONE)
@@ -2787,7 +2780,7 @@ int vp78_decode_frame(AVCodecContext *avctx, AVFrame *rframe, int *got_frame,
             // if we re-use the same map.
             if (prev_frame && s->segmentation.enabled &&
                 !s->segmentation.update_map)
-                ff_thread_await_progress(&prev_frame->tf, 1, 0);
+                ff_thread_progress_await(&prev_frame->tf, 1);
             if (is_vp7)
                 ret = vp7_decode_mv_mb_modes(avctx, curframe, prev_frame);
             else
@@ -2818,7 +2811,7 @@ int vp78_decode_frame(AVCodecContext *avctx, AVFrame *rframe, int *got_frame,
                             num_jobs);
     }
 
-    ff_thread_report_progress(&curframe->tf, INT_MAX, 0);
+    ff_thread_progress_report(&curframe->tf, INT_MAX);
     memcpy(&s->framep[0], &s->next_framep[0], sizeof(s->framep[0]) * 4);
 
 skip_decode:
@@ -2855,32 +2848,15 @@ static int vp7_decode_frame(AVCodecContext *avctx, AVFrame *frame,
 
 av_cold int ff_vp8_decode_free(AVCodecContext *avctx)
 {
-    VP8Context *s = avctx->priv_data;
-    int i;
-
     vp8_decode_flush_impl(avctx, 1);
-    for (i = 0; i < FF_ARRAY_ELEMS(s->frames); i++)
-        av_frame_free(&s->frames[i].tf.f);
 
     return 0;
 }
 
-static av_cold int vp8_init_frames(VP8Context *s)
-{
-    int i;
-    for (i = 0; i < FF_ARRAY_ELEMS(s->frames); i++) {
-        s->frames[i].tf.f = av_frame_alloc();
-        if (!s->frames[i].tf.f)
-            return AVERROR(ENOMEM);
-    }
-    return 0;
-}
-
 static av_always_inline
 int vp78_decode_init(AVCodecContext *avctx, int is_vp7)
 {
     VP8Context *s = avctx->priv_data;
-    int ret;
 
     s->avctx = avctx;
     s->vp7   = avctx->codec->id == AV_CODEC_ID_VP7;
@@ -2905,11 +2881,6 @@ int vp78_decode_init(AVCodecContext *avctx, int is_vp7)
     /* does not change for VP8 */
     memcpy(s->prob[0].scan, ff_zigzag_scan, sizeof(s->prob[0].scan));
 
-    if ((ret = vp8_init_frames(s)) < 0) {
-        ff_vp8_decode_free(avctx);
-        return ret;
-    }
-
     return 0;
 }
 
@@ -2933,7 +2904,6 @@ static int vp8_decode_update_thread_context(AVCodecContext *dst,
                                             const AVCodecContext *src)
 {
     VP8Context *s = dst->priv_data, *s_src = src->priv_data;
-    int i;
 
     if (s->macroblocks_base &&
         (s_src->mb_width != s->mb_width || s_src->mb_height != s->mb_height)) {
@@ -2948,13 +2918,8 @@ static int vp8_decode_update_thread_context(AVCodecContext *dst,
     s->lf_delta     = s_src->lf_delta;
     memcpy(s->sign_bias, s_src->sign_bias, sizeof(s->sign_bias));
 
-    for (i = 0; i < FF_ARRAY_ELEMS(s_src->frames); i++) {
-        if (s_src->frames[i].tf.f->buf[0]) {
-            int ret = vp8_ref_frame(s, &s->frames[i], &s_src->frames[i]);
-            if (ret < 0)
-                return ret;
-        }
-    }
+    for (int i = 0; i < FF_ARRAY_ELEMS(s_src->frames); i++)
+        vp8_replace_frame(s, &s->frames[i], &s_src->frames[i]);
 
     s->framep[0] = REBASE(s_src->next_framep[0]);
     s->framep[1] = REBASE(s_src->next_framep[1]);
@@ -2978,6 +2943,7 @@ const FFCodec ff_vp7_decoder = {
     FF_CODEC_DECODE_CB(vp7_decode_frame),
     .p.capabilities        = AV_CODEC_CAP_DR1,
     .flush                 = vp8_decode_flush,
+    .caps_internal         = FF_CODEC_CAP_USES_PROGRESSFRAMES,
 };
 #endif /* CONFIG_VP7_DECODER */
 
@@ -2993,7 +2959,7 @@ const FFCodec ff_vp8_decoder = {
     FF_CODEC_DECODE_CB(ff_vp8_decode_frame),
     .p.capabilities        = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
                              AV_CODEC_CAP_SLICE_THREADS,
-    .caps_internal         = FF_CODEC_CAP_ALLOCATE_PROGRESS,
+    .caps_internal         = FF_CODEC_CAP_USES_PROGRESSFRAMES,
     .flush                 = vp8_decode_flush,
     UPDATE_THREAD_CONTEXT(vp8_decode_update_thread_context),
     .hw_configs            = (const AVCodecHWConfigInternal *const []) {
diff --git a/libavcodec/vp8.h b/libavcodec/vp8.h
index eb9fa2f166..e38b92f0fe 100644
--- a/libavcodec/vp8.h
+++ b/libavcodec/vp8.h
@@ -31,8 +31,9 @@
 #include "libavutil/mem_internal.h"
 #include "libavutil/thread.h"
 
+#include "avcodec.h"
 #include "h264pred.h"
-#include "threadframe.h"
+#include "progressframe.h"
 #include "videodsp.h"
 #include "vp8dsp.h"
 #include "vpx_rac.h"
@@ -150,7 +151,7 @@ typedef struct VP8ThreadData {
 } VP8ThreadData;
 
 typedef struct VP8Frame {
-    ThreadFrame tf;
+    ProgressFrame tf;
     uint8_t *seg_map; ///< RefStruct reference
 
     void *hwaccel_picture_private; ///< RefStruct reference
diff --git a/libavcodec/webp.c b/libavcodec/webp.c
index 54b3fde6dc..85cb884153 100644
--- a/libavcodec/webp.c
+++ b/libavcodec/webp.c
@@ -1565,5 +1565,6 @@ const FFCodec ff_webp_decoder = {
     FF_CODEC_DECODE_CB(webp_decode_frame),
     .close          = webp_decode_close,
     .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
-    .caps_internal  = FF_CODEC_CAP_ICC_PROFILES,
+    .caps_internal  = FF_CODEC_CAP_ICC_PROFILES |
+                      FF_CODEC_CAP_USES_PROGRESSFRAMES,
 };
-- 
2.34.1



More information about the ffmpeg-devel mailing list