diff mbox series

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

Message ID 20211217084611.3234-1-huangzhexiong@bytedance.com
State New
Headers show
Series [FFmpeg-devel] avcodec/hevcdec: fix the order of in-loop filters | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished
andriy/make_ppc success Make finished
andriy/make_fate_ppc success Make fate finished

Commit Message

黄哲雄 Dec. 17, 2021, 8:46 a.m. UTC
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@bytedance.com>
---
 libavcodec/hevc_filter.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

Comments

黄哲雄 Dec. 17, 2021, 8:55 a.m. UTC | #1
To verify if this problem has been fixed, the following bitstream can
be used:
https://streams.videolan.org/ffmpeg/incoming/BlowingBubbles_ctu16.265
This bitstream is encoded by the following command line:
x265 --input BlowingBubbles_416x240_50.yuv --input-res 416x240 --fps 50 -s
16 -o bs.265
The MD5 of decoded yuv shall be 947723f7d8eeca7be6fcf5c91a071ada, not
18ca1e6fd9d8c47709a5bb156ee275a6 in the current version of FFmpeg.
It is verified by HM and libde265 decoder.
On Fri, Dec 17, 2021, 16:46 <huangzhexiong@bytedance.com> wrote:
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 --- 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
diff mbox series

Patch

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)) {