[FFmpeg-devel] [PATCH] avcodec/hevcdec: fix the order of in-loop filters

Zhexiong Huang huangzhexiong at bytedance.com
Fri Dec 17 10:46:37 EET 2021


As we know, in-loop filters in HEVC are applied by the following
ordered steps: firstly deblocking filter, then sample adaptive offset
filter if enabled. However, in the current version of FFmpeg, pixels
without being deblocking-filtered could be used by SAO filter when CTU
size is 16 and chroma format is 4:2:0 or 4:2:2, which could lead to the
wrong result in chroma components.

This patch changes the algorithm of deblocking filter, which ensures
that SAO filter is applied after deblocking filter for all the related
pixels. The new algorithm fixes this decoding problem when CTU size is
16, and shall not affect performance and correctness when CTU size is
32 or 64.

Signed-off-by: Zhexiong Huang <huangzhexiong at bytedance.com>
---
 libavcodec/hevc_filter.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/libavcodec/hevc_filter.c b/libavcodec/hevc_filter.c
index 3c45b5a39e..c53875d85f 100644
--- a/libavcodec/hevc_filter.c
+++ b/libavcodec/hevc_filter.c
@@ -512,10 +512,10 @@ static void deblocking_filter_CTB(HEVCContext *s, int x0, int y0)
 
     x_end2 = x_end;
     if (x_end2 != s->ps.sps->width)
-        x_end2 -= 8;
+        x_end2 += 8;
     for (y = y0; y < y_end; y += 8) {
         // vertical filtering luma
-        for (x = x0 ? x0 : 8; x < x_end; x += 8) {
+        for (x = x0 + 8; x < x_end2; x += 8) {
             const int bs0 = s->vertical_bs[(x +  y      * s->bs_width) >> 2];
             const int bs1 = s->vertical_bs[(x + (y + 4) * s->bs_width) >> 2];
             if (bs0 || bs1) {
@@ -545,7 +545,7 @@ static void deblocking_filter_CTB(HEVCContext *s, int x0, int y0)
              continue;
 
         // horizontal filtering luma
-        for (x = x0 ? x0 - 8 : 0; x < x_end2; x += 8) {
+        for (x = x0; x < x_end; x += 8) {
             const int bs0 = s->horizontal_bs[( x      + y * s->bs_width) >> 2];
             const int bs1 = s->horizontal_bs[((x + 4) + y * s->bs_width) >> 2];
             if (bs0 || bs1) {
@@ -579,9 +579,13 @@ static void deblocking_filter_CTB(HEVCContext *s, int x0, int y0)
             int h = 1 << s->ps.sps->hshift[chroma];
             int v = 1 << s->ps.sps->vshift[chroma];
 
+            x_end2 = x_end;
+            if (x_end2 != s->ps.sps->width)
+                x_end2 += 8 * h;
+
             // vertical filtering chroma
             for (y = y0; y < y_end; y += (8 * v)) {
-                for (x = x0 ? x0 : 8 * h; x < x_end; x += (8 * h)) {
+                for (x = x0 + 8 * h; x < x_end2; x += (8 * h)) {
                     const int bs0 = s->vertical_bs[(x +  y            * s->bs_width) >> 2];
                     const int bs1 = s->vertical_bs[(x + (y + (4 * v)) * s->bs_width) >> 2];
 
@@ -612,10 +616,7 @@ static void deblocking_filter_CTB(HEVCContext *s, int x0, int y0)
 
                 // horizontal filtering chroma
                 tc_offset = x0 ? left_tc_offset : cur_tc_offset;
-                x_end2 = x_end;
-                if (x_end != s->ps.sps->width)
-                    x_end2 = x_end - 8 * h;
-                for (x = x0 ? x0 - 8 * h : 0; x < x_end2; x += (8 * h)) {
+                for (x = x0; x < x_end; x += (8 * h)) {
                     const int bs0 = s->horizontal_bs[( x          + y * s->bs_width) >> 2];
                     const int bs1 = s->horizontal_bs[((x + 4 * h) + y * s->bs_width) >> 2];
                     if ((bs0 == 2) || (bs1 == 2)) {
-- 
2.25.1


More information about the ffmpeg-devel mailing list