diff mbox series

[FFmpeg-devel,3/7] lavc/error_resilience: fill ecinfo

Message ID 20230721133746.33335-3-jdek@itanimul.li
State New
Headers show
Series [FFmpeg-devel,1/7] lavu: add ecinfo sidedata | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

J. Dekker July 21, 2023, 1:37 p.m. UTC
Fill ECInfo inside error resilience using references set by the decoder.

Co-Authored-By: Thomas Guillem <thomas@gllm.fr>
Signed-off-by: J. Dekker <jdek@itanimul.li>
---
 libavcodec/error_resilience.c | 91 +++++++++++++++++++++++++++++------
 libavcodec/error_resilience.h |  4 +-
 2 files changed, 79 insertions(+), 16 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/error_resilience.c b/libavcodec/error_resilience.c
index 2aa6f1d864..c1417ecf07 100644
--- a/libavcodec/error_resilience.c
+++ b/libavcodec/error_resilience.c
@@ -28,6 +28,7 @@ 
 #include <limits.h>
 
 #include "libavutil/internal.h"
+#include "libavutil/ec.h"
 #include "avcodec.h"
 #include "error_resilience.h"
 #include "me_cmp.h"
@@ -411,7 +412,7 @@  static void guess_mv(ERContext *s)
 
     num_avail = 0;
     if (s->last_pic.motion_val[0])
-        ff_thread_await_progress(s->last_pic.tf, mb_height-1, 0);
+        ff_thread_await_progress(s->last_pic.tf, INT_MAX, 0);
     for (i = 0; i < mb_width * mb_height; i++) {
         const int mb_xy = s->mb_index2xy[i];
         int f = 0;
@@ -889,25 +890,61 @@  void ff_er_add_slice(ERContext *s, int startx, int starty,
     }
 }
 
-void ff_er_frame_end(ERContext *s)
+static void
+er_fill_info_ref(ERContext *s)
+{
+    uint64_t acc_ok = 0, acc_err = 0;
+    av_assert0(s->cur_pic.info);
+
+    if (s->cur_pic.f->pict_type != AV_PICTURE_TYPE_I) {
+        ERPicture *reffs[2] = {&s->last_pic, &s->next_pic};
+        int i, nb_ref_pics = s->cur_pic.f->pict_type == AV_PICTURE_TYPE_B ? 2 : 1;
+
+        for (i = 0; i < nb_ref_pics; i++) {
+            ERPicture *reff = reffs[i];
+
+            if (reff->info == NULL)
+                continue;
+
+            ff_thread_await_progress(reff->tf, INT_MAX, 0);
+
+            /* should check more accurately how refs are used */
+            if (reff->info->error == 0 && reff->info->ref_error == 0)
+                continue;
+
+            if (acc_err < (reff->info->error + reff->info->ref_error)){
+                acc_err = reff->info->error + reff->info->ref_error;
+                acc_ok  = reff->info->ok + reff->info->ref_ok;
+            }
+        }
+    }
+
+    s->cur_pic.info->ref_error = acc_err;
+    s->cur_pic.info->ref_ok = acc_ok;
+}
+
+int ff_er_frame_end(ERContext *s)
 {
     int *linesize = NULL;
-    int i, mb_x, mb_y, error, error_type, dc_error, mv_error, ac_error;
+    int i, mb_x, mb_y, error, error_type, dc_error, mv_error, ac_error, terror;
     int distance;
     int threshold_part[4] = { 100, 100, 100 };
     int threshold = 50;
     int is_intra_likely;
     int size = s->b8_stride * 2 * s->mb_height;
 
+
     /* We do not support ER of field pictures yet,
      * though it should not crash if enabled. */
-    if (!s->avctx->error_concealment || !atomic_load(&s->error_count)  ||
-        s->avctx->lowres                                               ||
-        !er_supported(s)                                               ||
-        atomic_load(&s->error_count) == 3 * s->mb_width *
-                          (s->avctx->skip_top + s->avctx->skip_bottom)) {
-        return;
+    if (!s->avctx->error_concealment || s->avctx->lowres || !er_supported(s))
+        return 0;
+
+    if (!atomic_load(&s->error_count) ||
+         atomic_load(&s->error_count) == 3 * s->mb_width * (s->avctx->skip_top + s->avctx->skip_bottom))
+    {
+        goto end_find_ref_errs;
     }
+
     linesize = s->cur_pic.f->linesize;
 
     if (   s->avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO
@@ -921,7 +958,7 @@  void ff_er_frame_end(ERContext *s)
 
         if (mb_x == s->mb_width) {
             av_log(s->avctx, AV_LOG_DEBUG, "ignoring last missing slice\n");
-            return;
+            goto end_find_ref_errs;
         }
     }
 
@@ -960,7 +997,7 @@  void ff_er_frame_end(ERContext *s)
                 s->cur_pic.ref_index[i]  = NULL;
                 s->cur_pic.motion_val[i] = NULL;
             }
-            return;
+            goto end_find_ref_errs;
         }
     }
 
@@ -1100,10 +1137,12 @@  void ff_er_frame_end(ERContext *s)
     }
 #endif
 
-    dc_error = ac_error = mv_error = 0;
+    terror = dc_error = ac_error = mv_error = 0;
     for (i = 0; i < s->mb_num; i++) {
         const int mb_xy = s->mb_index2xy[i];
         int error = s->error_status_table[mb_xy];
+        if (error)
+            terror++;
         if (error & ER_DC_ERROR)
             dc_error++;
         if (error & ER_AC_ERROR)
@@ -1111,10 +1150,9 @@  void ff_er_frame_end(ERContext *s)
         if (error & ER_MV_ERROR)
             mv_error++;
     }
-    av_log(s->avctx, AV_LOG_INFO, "concealing %d DC, %d AC, %d MV errors in %c frame\n",
-           dc_error, ac_error, mv_error, av_get_picture_type_char(s->cur_pic.f->pict_type));
 
-    s->cur_pic.f->decode_error_flags |= FF_DECODE_ERROR_CONCEALMENT_ACTIVE;
+    fprintf(stderr, "concealing %d DC, %d AC, %d MV errors in %c frame: %d\n",
+           dc_error, ac_error, mv_error, av_get_picture_type_char(s->cur_pic.f->pict_type), terror * 256);
 
     is_intra_likely = is_intra_more_likely(s);
 
@@ -1349,7 +1387,30 @@  void ff_er_frame_end(ERContext *s)
         s->cur_pic.motion_val[i] = NULL;
     }
 
+    av_assert0(terror);
+    if (s->cur_pic.info) {
+        /* assumes 16x16 mbs */
+        s->cur_pic.info->error = terror * 256;
+        s->cur_pic.info->ok    = (s->mb_num - terror) * 256;
+        er_fill_info_ref(s);
+    }
+
+    memset(&s->cur_pic, 0, sizeof(ERPicture));
+    memset(&s->last_pic, 0, sizeof(ERPicture));
+    memset(&s->next_pic, 0, sizeof(ERPicture));
+
+    return 1;
+
+end_find_ref_errs:
+    if (s->cur_pic.info)
+    {
+        er_fill_info_ref(s);
+        s->cur_pic.info->error = s->cur_pic.info->ok = 0;
+    }
+
     memset(&s->cur_pic, 0, sizeof(ERPicture));
     memset(&s->last_pic, 0, sizeof(ERPicture));
     memset(&s->next_pic, 0, sizeof(ERPicture));
+
+    return 0;
 }
diff --git a/libavcodec/error_resilience.h b/libavcodec/error_resilience.h
index 47cc8a4fc6..97479d2c0c 100644
--- a/libavcodec/error_resilience.h
+++ b/libavcodec/error_resilience.h
@@ -25,6 +25,7 @@ 
 #include "avcodec.h"
 #include "me_cmp.h"
 #include "threadframe.h"
+#include "libavutil/ec.h"
 
 ///< current MB is the first after a resync marker
 #define VP_START               1
@@ -48,6 +49,7 @@  typedef struct ERPicture {
 
     uint32_t *mb_type;
     int field_picture;
+    AVECInfo *info;
 } ERPicture;
 
 typedef struct ERContext {
@@ -90,7 +92,7 @@  typedef struct ERContext {
 } ERContext;
 
 void ff_er_frame_start(ERContext *s);
-void ff_er_frame_end(ERContext *s);
+int ff_er_frame_end(ERContext *s);
 void ff_er_add_slice(ERContext *s, int startx, int starty, int endx, int endy,
                      int status);