dct-domain noise reduction (ported from lavc)
[x262.git] / encoder / macroblock.c
index c25538c..b6541fa 100644 (file)
@@ -147,108 +147,6 @@ static void quant_2x2_dc( x264_t *h, int16_t dct[2][2], int quant_mf[6][4][4], i
     h->quantf.quant_2x2_dc_core( dct, quant_mf[i_mf][0][0], i_qbits, f );
 }
 
-#if 0
-/* From a JVT doc */
-static const int f_deadzone_intra[4][4][2] = /* [num][den] */
-{
-    { {1,2}, {3,7}, {2,5}, {1,3} },
-    { {3,7}, {2,5}, {1,3}, {1,4} },
-    { {2,5}, {1,3}, {1,4}, {1,5} },
-    { {1,3}, {1,4}, {1,5}, {1,5} }
-};
-static const int f_deadzone_inter[4][4][2] = /* [num][den] */
-{
-    { {1,3}, {2,7}, {4,15},{2,9} },
-    { {2,7}, {4,15},{2,9}, {1,6} },
-    { {4,15},{2,9}, {1,6}, {1,7} },
-    { {2,9}, {1,6}, {1,7}, {2,15} }
-};
-
-
-static void quant_4x4( int16_t dct[4][4], int i_qscale, int b_intra )
-{
-    const int(*f_deadzone)[4][4][2] = b_intra ? &f_deadzone_intra : &f_deadzone_inter;
-    const int i_qbits = 15 + i_qscale / 6;
-    const int i_mf = i_qscale % 6;
-
-    int x,y;
-    for( y = 0; y < 4; y++ )
-    {
-        for( x = 0; x < 4; x++ )
-        {
-#if 0
-            const int f = b_intra ?
-                          (f_deadzone_intra[y][x][0] * ( 1 << i_qbits ) / f_deadzone_intra[y][x][1])
-                          :
-                          (f_deadzone_inter[y][x][0] * ( 1 << i_qbits ) / f_deadzone_inter[y][x][1]);
-#else
-            const int f = (*f_deadzone)[y][x][0] * ( 1 << i_qbits ) / (*f_deadzone)[y][x][1];
-#endif
-
-            if( dct[y][x] > 0 )
-            {
-                dct[y][x] =( f + dct[y][x]  * quant_mf[i_mf][y][x] ) >> i_qbits;
-            }
-            else
-            {
-                dct[y][x] = - ( ( f - dct[y][x]  * quant_mf[i_mf][y][x] ) >> i_qbits );
-            }
-        }
-    }
-}
-
-static void quant_4x4_dc( int16_t dct[4][4], int i_qscale )
-{
-    const int i_qbits = 15 + i_qscale / 6;
-    const int i_qmf = quant_mf[i_qscale%6][0][0];
-    const int f2 = f_deadzone_intra[0][0][0] * ( 2 << i_qbits ) / f_deadzone_intra[0][0][1];
-    int x,y;
-
-    for( y = 0; y < 4; y++ )
-    {
-        for( x = 0; x < 4; x++ )
-        {
-
-            if( dct[y][x] > 0 )
-            {
-                dct[y][x] =( f2 + dct[y][x]  * i_qmf) >> ( 1 + i_qbits );
-            }
-            else
-            {
-                dct[y][x] = - ( ( f2 - dct[y][x]  * i_qmf ) >> (1 + i_qbits ) );
-            }
-        }
-    }
-}
-
-static void quant_2x2_dc( int16_t dct[2][2], int i_qscale, int b_intra )
-{
-    int const i_qbits = 15 + i_qscale / 6;
-    const int i_qmf = quant_mf[i_qscale%6][0][0];
-    const int f2 = b_intra ?
-                   (f_deadzone_intra[0][0][0] * ( 2 << i_qbits ) / f_deadzone_intra[0][0][1])
-                   :
-                   (f_deadzone_inter[0][0][0] * ( 2 << i_qbits ) / f_deadzone_inter[0][0][1]);
-    int x,y;
-    for( y = 0; y < 2; y++ )
-    {
-        for( x = 0; x < 2; x++ )
-        {
-            if( dct[y][x] > 0 )
-            {
-                dct[y][x] =( f2 + dct[y][x]  * i_qmf) >> ( 1 + i_qbits );
-            }
-            else
-            {
-                dct[y][x] = - ( ( f2 - dct[y][x]  * i_qmf ) >> (1 + i_qbits ) );
-            }
-        }
-    }
-}
-
-
-#endif
-
 /* (ref: JVT-B118)
  * x264_mb_decimate_score: given dct coeffs it returns a score to see if we could empty this dct coeffs
  * to 0 (low score means set it to null)
@@ -308,9 +206,16 @@ void x264_mb_encode_i4x4( x264_t *h, int idx, int i_qscale )
     }
 
     h->dctf.sub4x4_dct( dct4x4, p_src, i_stride, p_dst, i_stride );
-    quant_4x4( h, dct4x4, h->quant4_mf[CQM_4IY], i_qscale, 1 );
+
+    if( h->mb.b_noise_reduction )
+        x264_denoise_dct( h, (int16_t*)dct4x4 );
+    if( h->mb.b_trellis )
+        x264_quant_4x4_trellis( h, dct4x4, CQM_4IY, i_qscale, DCT_LUMA_4x4, 1 );
+    else
+        quant_4x4( h, dct4x4, h->quant4_mf[CQM_4IY], i_qscale, 1 );
+
     scan_zigzag_4x4full( h->dct.block[idx].luma4x4, dct4x4 );
-    x264_mb_dequant_4x4( dct4x4, h->dequant4_mf[CQM_4IY], i_qscale );
+    h->quantf.dequant_4x4( dct4x4, h->dequant4_mf[CQM_4IY], i_qscale );
 
     /* output samples to fdec */
     h->dctf.add4x4_idct( p_dst, i_stride, dct4x4 );
@@ -325,9 +230,16 @@ void x264_mb_encode_i8x8( x264_t *h, int idx, int i_qscale )
     int16_t dct8x8[8][8];
 
     h->dctf.sub8x8_dct8( dct8x8, p_src, i_stride, p_dst, i_stride );
-    quant_8x8( h, dct8x8, h->quant8_mf[CQM_8IY], i_qscale, 1 );
+
+    if( h->mb.b_noise_reduction )
+        x264_denoise_dct( h, (int16_t*)dct8x8 );
+    if( h->mb.b_trellis )
+        x264_quant_8x8_trellis( h, dct8x8, CQM_8IY, i_qscale, 1 );
+    else 
+        quant_8x8( h, dct8x8, h->quant8_mf[CQM_8IY], i_qscale, 1 );
+
     scan_zigzag_8x8full( h->dct.luma8x8[idx], dct8x8 );
-    x264_mb_dequant_8x8( dct8x8, h->dequant8_mf[CQM_8IY], i_qscale );
+    h->quantf.dequant_8x8( dct8x8, h->dequant8_mf[CQM_8IY], i_qscale );
     h->dctf.add8x8_idct8( p_dst, i_stride, dct8x8 );
 }
 
@@ -361,9 +273,15 @@ static void x264_mb_encode_i16x16( x264_t *h, int i_qscale )
         dct4x4[0][block_idx_y[i]][block_idx_x[i]] = dct4x4[1+i][0][0];
 
         /* quant/scan/dequant */
-        quant_4x4( h, dct4x4[1+i], h->quant4_mf[CQM_4IY], i_qscale, 1 );
+        if( h->mb.b_noise_reduction )
+            x264_denoise_dct( h, (int16_t*)dct4x4[i] );
+        if( h->mb.b_trellis )
+            x264_quant_4x4_trellis( h, dct4x4[1+i], CQM_4IY, i_qscale, DCT_LUMA_AC, 1 );
+        else
+            quant_4x4( h, dct4x4[1+i], h->quant4_mf[CQM_4IY], i_qscale, 1 );
+
         scan_zigzag_4x4( h->dct.block[i].residual_ac, dct4x4[1+i] );
-        x264_mb_dequant_4x4( dct4x4[1+i], h->dequant4_mf[CQM_4IY], i_qscale );
+        h->quantf.dequant_4x4( dct4x4[1+i], h->dequant4_mf[CQM_4IY], i_qscale );
     }
 
     h->dctf.dct4x4dc( dct4x4[0] );
@@ -417,9 +335,9 @@ static void x264_mb_encode_8x8_chroma( x264_t *h, int b_inter, int i_qscale )
             /* copy dc coeff */
             dct2x2[block_idx_y[i]][block_idx_x[i]] = dct4x4[i][0][0];
 
+            /* no trellis; it doesn't seem to help chroma noticeably */
             quant_4x4( h, dct4x4[i], h->quant4_mf[CQM_4IC + b_inter], i_qscale, !b_inter );
             scan_zigzag_4x4( h->dct.block[16+i+ch*4].residual_ac, dct4x4[i] );
-            x264_mb_dequant_4x4( dct4x4[i], h->dequant4_mf[CQM_4IC + b_inter], i_qscale );
 
             if( b_inter )
             {
@@ -438,28 +356,20 @@ static void x264_mb_encode_8x8_chroma( x264_t *h, int b_inter, int i_qscale )
         if( b_inter && i_decimate_score < 7 )
         {
             /* Near null chroma 8x8 block so make it null (bits saving) */
+            memset( dct4x4, 0, sizeof( dct4x4 ) );
+            memset( &h->dct.block[16+ch*4], 0, 4 * sizeof( *h->dct.block ) );
+        }
+        else
+        {
             for( i = 0; i < 4; i++ )
-            {
-                int x, y;
-                for( x = 0; x < 15; x++ )
-                {
-                    h->dct.block[16+i+ch*4].residual_ac[x] = 0;
-                }
-                for( x = 0; x < 4; x++ )
-                {
-                    for( y = 0; y < 4; y++ )
-                    {
-                        dct4x4[i][x][y] = 0;
-                    }
-                }
-            }
+                h->quantf.dequant_4x4( dct4x4[i], h->dequant4_mf[CQM_4IC + b_inter], i_qscale );
         }
 
         /* calculate dct coeffs */
         for( i = 0; i < 4; i++ )
         {
             /* copy dc coeff */
-            dct4x4[i][0][0] = dct2x2[block_idx_y[i]][block_idx_x[i]];
+            dct4x4[i][0][0] = dct2x2[0][i];
         }
         h->dctf.add8x8_idct( p_dst, i_stride, dct4x4 );
     }
@@ -590,35 +500,52 @@ void x264_macroblock_encode( x264_t *h )
         else if( h->mb.b_transform_8x8 )
         {
             int16_t dct8x8[4][8][8];
+            int nnz8x8[4] = {1,1,1,1};
             h->dctf.sub16x16_dct8( dct8x8,
                                    h->mb.pic.p_fenc[0], h->mb.pic.i_stride[0],
                                    h->mb.pic.p_fdec[0], h->mb.pic.i_stride[0] );
 
             for( idx = 0; idx < 4; idx++ )
             {
-                int i_decimate_8x8;
+                if( h->mb.b_noise_reduction )
+                    x264_denoise_dct( h, (int16_t*)dct8x8[idx] );
+                if( h->mb.b_trellis )
+                    x264_quant_8x8_trellis( h, dct8x8[idx], CQM_8PY, i_qp, 0 );
+                else
+                    quant_8x8( h, dct8x8[idx], h->quant8_mf[CQM_8PY], i_qp, 0 );
 
-                quant_8x8( h, dct8x8[idx], h->quant8_mf[CQM_8PY], i_qp, 0 );
                 scan_zigzag_8x8full( h->dct.luma8x8[idx], dct8x8[idx] );
-                x264_mb_dequant_8x8( dct8x8[idx], h->dequant8_mf[CQM_8PY], i_qp );
 
-                i_decimate_8x8 = x264_mb_decimate_score( h->dct.luma8x8[idx], 64 );
-                i_decimate_mb += i_decimate_8x8;
-                if( i_decimate_8x8 < 4 )
+                if( !h->mb.b_trellis )
                 {
-                    memset( h->dct.luma8x8[idx], 0, sizeof( h->dct.luma8x8[idx] ) );
-                    memset( dct8x8[idx], 0, sizeof( dct8x8[idx] ) );
+                    int i_decimate_8x8 = x264_mb_decimate_score( h->dct.luma8x8[idx], 64 );
+                    i_decimate_mb += i_decimate_8x8;
+                    if( i_decimate_8x8 < 4 )
+                    {
+                        memset( h->dct.luma8x8[idx], 0, sizeof( h->dct.luma8x8[idx] ) );
+                        memset( dct8x8[idx], 0, sizeof( dct8x8[idx] ) );
+                        nnz8x8[idx] = 0;
+                    }
                 }
             }
 
-            if( i_decimate_mb < 6 )
+            if( i_decimate_mb < 6 && !h->mb.b_trellis )
                 memset( h->dct.luma8x8, 0, sizeof( h->dct.luma8x8 ) );
             else
-                h->dctf.add16x16_idct8( h->mb.pic.p_fdec[0], h->mb.pic.i_stride[0], dct8x8 );
+            {
+                const int stride = h->mb.pic.i_stride[0];
+                for( idx = 0; idx < 4; idx++ )
+                    if( nnz8x8[idx] )
+                    {
+                        h->quantf.dequant_8x8( dct8x8[idx], h->dequant8_mf[CQM_8PY], i_qp );
+                        h->dctf.add8x8_idct8( &h->mb.pic.p_fdec[0][(idx&1)*8 + (idx>>1)*8*stride], stride, dct8x8[idx] );
+                    }
+            }
         }
         else
         {
             int16_t dct4x4[16][4][4];
+            int nnz8x8[4] = {1,1,1,1};
             h->dctf.sub16x16_dct( dct4x4,
                                   h->mb.pic.p_fenc[0], h->mb.pic.i_stride[0],
                                   h->mb.pic.p_fdec[0], h->mb.pic.i_stride[0] );
@@ -633,9 +560,14 @@ void x264_macroblock_encode( x264_t *h )
                 {
                     idx = i8x8 * 4 + i4x4;
 
-                    quant_4x4( h, dct4x4[idx], h->quant4_mf[CQM_4PY], i_qp, 0 );
+                    if( h->mb.b_noise_reduction )
+                        x264_denoise_dct( h, (int16_t*)dct4x4[idx] );
+                    if( h->mb.b_trellis )
+                        x264_quant_4x4_trellis( h, dct4x4[idx], CQM_4PY, i_qp, DCT_LUMA_4x4, 0 );
+                    else
+                        quant_4x4( h, dct4x4[idx], h->quant4_mf[CQM_4PY], i_qp, 0 );
+
                     scan_zigzag_4x4full( h->dct.block[idx].luma4x4, dct4x4[idx] );
-                    x264_mb_dequant_4x4( dct4x4[idx], h->dequant4_mf[CQM_4PY], i_qp );
 
                     i_decimate_8x8 += x264_mb_decimate_score( h->dct.block[idx].luma4x4, 16 );
                 }
@@ -644,25 +576,25 @@ void x264_macroblock_encode( x264_t *h )
                 i_decimate_mb += i_decimate_8x8;
                 if( i_decimate_8x8 < 4 )
                 {
-                    for( i4x4 = 0; i4x4 < 4; i4x4++ )
-                    {
-                        int x, y;
-                        idx = i8x8 * 4 + i4x4;
-                        for( i = 0; i < 16; i++ )
-                            h->dct.block[idx].luma4x4[i] = 0;
-                        for( x = 0; x < 4; x++ )
-                            for( y = 0; y < 4; y++ )
-                                dct4x4[idx][x][y] = 0;
-                    }
+                    memset( &dct4x4[i8x8*4], 0, 4 * sizeof( *dct4x4 ) );
+                    memset( &h->dct.block[i8x8*4], 0, 4 * sizeof( *h->dct.block ) );
+                    nnz8x8[i8x8] = 0;
                 }
             }
 
             if( i_decimate_mb < 6 )
-                for( idx = 0; idx < 16; idx++ )
-                    for( i = 0; i < 16; i++ )
-                        h->dct.block[idx].luma4x4[i] = 0;
+                memset( h->dct.block, 0, 16 * sizeof( *h->dct.block ) );
             else
-                h->dctf.add16x16_idct( h->mb.pic.p_fdec[0], h->mb.pic.i_stride[0], dct4x4 );
+            {
+                const int stride = h->mb.pic.i_stride[0];
+                for( i8x8 = 0; i8x8 < 4; i8x8++ )
+                    if( nnz8x8[i8x8] )
+                    {
+                        for( i = 0; i < 4; i++ )
+                            h->quantf.dequant_4x4( dct4x4[i8x8*4+i], h->dequant4_mf[CQM_4PY], i_qp );
+                        h->dctf.add8x8_idct( &h->mb.pic.p_fdec[0][(i8x8&1)*8 + (i8x8>>1)*8*stride], stride, &dct4x4[i8x8*4] );
+                    }
+            }
         }
     }
 
@@ -726,23 +658,16 @@ void x264_macroblock_encode( x264_t *h )
             h->mb.i_cbp_chroma = 0x02;    /* dc+ac (we can't do only ac) */
         }
     }
-    if( h->mb.i_cbp_chroma == 0x00 &&
-        ( array_non_zero_count( h->dct.chroma_dc[0], 4 ) > 0 || array_non_zero_count( h->dct.chroma_dc[1], 4 ) ) > 0 )
+    if( h->mb.i_cbp_chroma == 0x00 && array_non_zero( h->dct.chroma_dc[0], 8 ) )
     {
         h->mb.i_cbp_chroma = 0x01;    /* dc only */
     }
 
     if( h->param.b_cabac )
     {
-        if( h->mb.i_type == I_16x16 && array_non_zero_count( h->dct.luma16x16_dc, 16 ) > 0 )
-            i_cbp_dc = 0x01;
-        else
-            i_cbp_dc = 0x00;
-
-        if( array_non_zero_count( h->dct.chroma_dc[0], 4 ) > 0 )
-            i_cbp_dc |= 0x02;
-        if( array_non_zero_count( h->dct.chroma_dc[1], 4 ) > 0 )
-            i_cbp_dc |= 0x04;
+        i_cbp_dc = ( h->mb.i_type == I_16x16 && array_non_zero( h->dct.luma16x16_dc, 16 ) )
+                 | array_non_zero( h->dct.chroma_dc[0], 4 ) << 1
+                 | array_non_zero( h->dct.chroma_dc[1], 4 ) << 2;
     }
 
     /* store cbp */
@@ -875,3 +800,63 @@ int x264_macroblock_probe_skip( x264_t *h, int b_bidir )
 
     return 1;
 }
+
+/****************************************************************************
+ * DCT-domain noise reduction / adaptive deadzone
+ * from libavcodec
+ ****************************************************************************/
+
+void x264_noise_reduction_update( x264_t *h )
+{
+    int cat, i;
+    for( cat = 0; cat < 4; cat++ )
+    {
+        int b_8x8 = cat >= 2;
+        int size = b_8x8 ? 64 : 16;
+        const int *weight = b_8x8 ? x264_dct8_weight2_tab : x264_dct4_weight2_tab;
+
+        if( h->nr_count[cat] > (b_8x8 ? (1<<16) : (1<<18)) )
+        {
+            for( i = 0; i < size; i++ )
+                h->nr_residual_sum[cat][i] >>= 1;
+            h->nr_count[cat] >>= 1;
+        }
+
+        for( i = 0; i < size; i++ )
+            h->nr_offset[cat][i] =
+                ((uint64_t)h->param.analyse.i_noise_reduction * h->nr_count[cat]
+                 + h->nr_residual_sum[cat][i]/2)
+              / ((uint64_t)h->nr_residual_sum[cat][i] * weight[i]/256 + 1);
+    }
+}
+
+void x264_denoise_dct( x264_t *h, int16_t *dct )
+{
+    const int cat = !IS_INTRA(h->mb.i_type) + 2*h->mb.b_transform_8x8;
+    int i;
+
+    h->nr_count[cat]++;
+
+    for( i = (cat >= 2 ? 63 : 15); i >= 1; i-- )
+    {
+        int level = dct[i];
+        if( level )
+        {
+            if( level > 0 )
+            {
+                h->nr_residual_sum[cat][i] += level;
+                level -= h->nr_offset[cat][i];
+                if( level < 0 )
+                    level = 0;
+            }
+            else
+            {
+                h->nr_residual_sum[cat][i] -= level;
+                level += h->nr_offset[cat][i];
+                if( level > 0 )
+                    level = 0;
+            }
+            dct[i] = level;
+        }
+    }
+}