aom: rtc: Avoid reset of segment for inactive blocks

From d4043476019a88e108d125f4168fd0533887af56 Mon Sep 17 00:00:00 2001
From: Marco Paniconi <[EMAIL REDACTED]>
Date: Thu, 28 Mar 2024 15:18:58 -0700
Subject: [PATCH] rtc: Avoid reset of segment for inactive blocks

For active_map in rtc mode.
For skipped blocks that are labeled as inactive:
remove the reset to 0 for the segment_id. This allows
them to be skippped in loopfilter at the block level
(since SEG_LVL_ALT_LF_Y_H/V is set in av1_apply_active_map).

The fix needed to support this is to make sure the
segment_skip block uses GLOBALMV and LAST_FRAME,
as required by the bitstream. In the current code we
were allowing for NEAREST if motion vector is (0, 0)),
which is incorrect. The current patch explicitly puts
this constraint (globalmv-last) early in nonrd_pickmode.

This fix has small speedup, ~1%, with negligible quality
change.

Change-Id: Ic5e9dbd7b9bec4d57edf90cf73f44719689e1dab
---
 av1/encoder/aq_cyclicrefresh.c | 4 ----
 av1/encoder/nonrd_pickmode.c   | 4 ++++
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/av1/encoder/aq_cyclicrefresh.c b/av1/encoder/aq_cyclicrefresh.c
index 73357eb07..1aa8dde32 100644
--- a/av1/encoder/aq_cyclicrefresh.c
+++ b/av1/encoder/aq_cyclicrefresh.c
@@ -179,10 +179,6 @@ void av1_cyclic_reset_segment_skip(const AV1_COMP *cpi, MACROBLOCK *const x,
         memset(&cm->cur_frame->seg_map[map_offset], segment_id, xmis);
       }
     }
-  } else if (prev_segment_id == AM_SEGMENT_ID_INACTIVE) {
-    // TODO(marpan): Look into why this condition is needed
-    // (when skip_over4x4 = 1) to prevent decoder failure.
-    mbmi->segment_id = 0;
   }
   if (!dry_run) {
     if (cyclic_refresh_segment_id(prev_segment_id) == CR_SEGMENT_ID_BOOST1)
diff --git a/av1/encoder/nonrd_pickmode.c b/av1/encoder/nonrd_pickmode.c
index 41e2e212a..57c74f66d 100644
--- a/av1/encoder/nonrd_pickmode.c
+++ b/av1/encoder/nonrd_pickmode.c
@@ -2357,6 +2357,10 @@ static AOM_FORCE_INLINE bool skip_inter_mode_nonrd(
     *ref_frame2 = NONE_FRAME;
   }
 
+  if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP) &&
+      (*this_mode != GLOBALMV || *ref_frame != LAST_FRAME))
+    return true;
+
   if (x->sb_me_block && *ref_frame == LAST_FRAME) {
     // We want to make sure to test the superblock MV:
     // so don't skip (return false) for NEAREST_LAST or NEAR_LAST if they