]> granicus.if.org Git - libvpx/commitdiff
Revised coding using adaptive mode context to depend on frame type
authorYaowu Xu <yaowu@google.com>
Thu, 8 Dec 2011 19:43:09 +0000 (11:43 -0800)
committerYaowu Xu <yaowu@google.com>
Fri, 9 Dec 2011 20:13:42 +0000 (12:13 -0800)
A previous commit 76feb965 made the vp8_mode_context adaptive on a frame
frame basis, this commit further made the coding context adaptive to two
frame types separately. Tests on derf set showed a further small gain on
all metrics: avg psnr 0.10%, glb psnr: 0.11%, ssim: 0.08%

http://www.corp.google.com/~yaowu/no_crawl/newNearMode_1209.html

Change-Id: I7b3e32ec8729de1903d14a3f1213f1624b78cdee

vp8/common/alloccommon.c
vp8/common/entropymode.c
vp8/common/entropymode.h
vp8/common/modecont.c
vp8/common/modecont.h
vp8/common/onyxc_int.h
vp8/decoder/decodframe.c
vp8/encoder/bitstream.c
vp8/encoder/mcomp.h
vp8/encoder/onyx_if.c
vp8/encoder/ratectrl.c

index 0c7a9ef8d719e7e6884c1e2883799602a1e27020..4457c8821c7433da4732cbf3333f2c2d37bbe192 100644 (file)
@@ -190,12 +190,6 @@ void vp8_create_common(VP8_COMMON *oci)
     vp8_machine_specific_config(oci);
 
     vp8_init_mbmode_probs(oci);
-#if CONFIG_NEWNEAR
-    vp8_init_mv_ref_counts(oci);
-#endif
-    vpx_memcpy( oci->vp8_mode_contexts,
-                default_vp8_mode_contexts,
-                sizeof(default_vp8_mode_contexts));
 
     vp8_default_bmode_probs(oci->fc.bmode_prob);
 
index 5b1a6d35cfa947e6666bd574b8de734abb1121e7..a83c1d4bb488c68211f8dff0720f57bbe7f85c64 100644 (file)
@@ -9,10 +9,12 @@
  */
 
 
+#include "modecont.h"
 #include "entropymode.h"
 #include "entropy.h"
 #include "vpx_mem/vpx_mem.h"
 
+
 #if CONFIG_QIMODE
 const unsigned int kf_y_mode_cts[8][VP8_YMODES] =
 {
@@ -356,43 +358,59 @@ void vp8_entropy_mode_init()
 }
 
 #if CONFIG_NEWNEAR
-void vp8_init_mv_ref_counts(VP8_COMMON *pc)
+void vp8_init_mode_contexts(VP8_COMMON *pc)
 {
     vpx_memset(pc->mv_ref_ct, 0, sizeof(pc->mv_ref_ct));
+    vpx_memset(pc->mv_ref_ct_a, 0, sizeof(pc->mv_ref_ct_a));
+
+    vpx_memcpy( pc->mode_context,
+                default_vp8_mode_contexts,
+                sizeof (pc->mode_context));
+    vpx_memcpy( pc->mode_context_a,
+                default_vp8_mode_contexts,
+                sizeof (pc->mode_context_a));
+
 }
 
 void vp8_accum_mv_refs(VP8_COMMON *pc,
                        MB_PREDICTION_MODE m,
                        const int ct[4])
 {
+    int (*mv_ref_ct)[4][2];
+
+    if(pc->refresh_alt_ref_frame)
+        mv_ref_ct = pc->mv_ref_ct_a;
+    else
+        mv_ref_ct = pc->mv_ref_ct;
+
     if (m == ZEROMV)
     {
-        ++pc->mv_ref_ct [ct[0]] [0] [0];
+        ++mv_ref_ct [ct[0]] [0] [0];
     }
     else
     {
-        ++pc->mv_ref_ct [ct[0]] [0] [1];
+        ++mv_ref_ct [ct[0]] [0] [1];
         if (m == NEARESTMV)
         {
-            ++pc->mv_ref_ct [ct[1]] [1] [0];
+            ++mv_ref_ct [ct[1]] [1] [0];
         }
         else
         {
-            ++pc->mv_ref_ct [ct[1]] [1] [1];
+            ++mv_ref_ct [ct[1]] [1] [1];
             if (m == NEARMV)
             {
-                ++pc->mv_ref_ct [ct[2]] [2] [0];
+                ++mv_ref_ct [ct[2]] [2] [0];
             }
             else
             {
-                ++pc->mv_ref_ct [ct[2]] [2] [1];
+                ++mv_ref_ct [ct[2]] [2] [1];
                 if (m == NEWMV)
                 {
-                    ++pc->mv_ref_ct [ct[3]] [3] [0];
+                    ++mv_ref_ct [ct[3]] [3] [0];
                 }
                 else
                 {
-                    ++pc->mv_ref_ct [ct[3]] [3] [1];
+                    ++mv_ref_ct [ct[3]] [3] [1];
                 }
             }
         }
@@ -402,24 +420,37 @@ void vp8_accum_mv_refs(VP8_COMMON *pc,
 void vp8_update_mode_context(VP8_COMMON *pc)
 {
     int i, j;
+    int (*mv_ref_ct)[4][2];
+    int (*mode_context)[4];
+
+    if(pc->refresh_alt_ref_frame)
+    {
+        mv_ref_ct = pc->mv_ref_ct_a;
+        mode_context = pc->mode_context_a;
+    }
+    else
+    {
+        mv_ref_ct = pc->mv_ref_ct;
+        mode_context = pc->mode_context;
+    }
+
     for (j = 0; j < 6; j++)
     {
         for (i = 0; i < 4; i++)
         {
             int this_prob;
-            int count;
-            // context probs
-            count = pc->mv_ref_ct[j][i][0] + pc->mv_ref_ct[j][i][1];
+            int count = mv_ref_ct[j][i][0] + mv_ref_ct[j][i][1];
+
             if (count)
-                this_prob = 256 * pc->mv_ref_ct[j][i][0] / count;
+                this_prob = 256 * mv_ref_ct[j][i][0] / count;
             else
                 this_prob = 128;
+            this_prob = this_prob? (this_prob<255?this_prob:255):1;
             if (this_prob == 0)
                 this_prob = 1;
             if (this_prob == 256)
                 this_prob = 255;
-
-            pc->mode_context[j][i] = this_prob;
+            mode_context[j][i] = this_prob;
         }
     }
 }
@@ -427,14 +458,25 @@ void vp8_update_mode_context(VP8_COMMON *pc)
 void print_mode_contexts(VP8_COMMON *pc)
 {
     int j, i;
+    printf("====================\n");
     for(j=0; j<6; j++)
     {
         for (i = 0; i < 4; i++)
         {
-            printf( "%4d ", pc->vp8_mode_contexts[j][i]);
+            printf( "%4d ", pc->mode_context[j][i]);
         }
         printf("\n");
     }
+    printf("====================\n");
+    for(j=0; j<6; j++)
+    {
+        for (i = 0; i < 4; i++)
+        {
+            printf( "%4d ", pc->mode_context_a[j][i]);
+        }
+        printf("\n");
+    }
+
 }
 void print_mv_ref_cts(VP8_COMMON *pc)
 {
index 7f5bdf901d7084dc1c94283bc7a0c2aa80b1f7fd..1c6d235604191dfc427fb246ff9ae1225f282af6 100644 (file)
@@ -63,6 +63,8 @@ void vp8_entropy_mode_init(void);
 
 void vp8_init_mbmode_probs(VP8_COMMON *x);
 
+extern void vp8_update_mode_context(VP8_COMMON *pc);;
+
 void   vp8_default_bmode_probs(vp8_prob dest [VP8_BINTRAMODES-1]);
 void vp8_kf_default_bmode_probs(vp8_prob dest [VP8_BINTRAMODES] [VP8_BINTRAMODES] [VP8_BINTRAMODES-1]);
 
index d7491f241e0180c4c53f7f19953a2a754a3f918a..c7f903b16f6add0bdecb16b621865ae117ff201f 100644 (file)
 
 
 #include "entropy.h"
-
+#if CONFIG_NEWNEAR
+const int default_vp8_mode_contexts[6][4] =
+{
+    {   /* 0 */
+         7,     1,     1,   183},
+    {   /* 1 */
+        14,    18,    14,   147},
+    {/* 2 */
+       135,    64,    57,    68},
+    {   /* 3 */
+         60,    56,   128,   65},
+    {/* 4 */
+        159,   134,   128,   34},
+    {   /* 5 */
+        234,   188,   128,   28},
+};
+const int default_vp8_mode_contexts_a[6][4] =
+{
+    {   /* 0 */
+         4,     1,    1,   143},
+    {   /* 1 */
+         7,     9,    7,   107},
+    {/* 2 */
+        95,    34,   57,    68},
+    {   /* 3 */
+        95,    56,   128,   65},
+    {/* 4 */
+        159,   67,   128,   34},
+    {   /* 5 */
+        234,   94,   128,   28},
+};
+#else
 const int default_vp8_mode_contexts[6][4] =
 {
     {
@@ -38,3 +69,4 @@ const int default_vp8_mode_contexts[6][4] =
         234,   188,   128,    28,
     },
 };
+#endif
\ No newline at end of file
index 476ae3fa9dd673394bc537d8b07bcef2d8a150e9..8117e63919646a5b84b559fe2690e8e781b95bbf 100644 (file)
@@ -13,5 +13,7 @@
 #define __INC_MODECONT_H
 
 extern const int default_vp8_mode_contexts[6][4];
-
+#if CONFIG_NEWNEAR
+extern const int default_vp8_mode_contexts_a[6][4];
+#endif
 #endif
index 4bca5af87ef81edbe4b07d2162690d234f971e0e..3cb63d5af421e65444db7fc329fe7d452db0a994 100644 (file)
@@ -218,12 +218,13 @@ typedef struct VP8Common
 #if CONFIG_NEWNEAR
     int mv_ref_ct[6][4][2];
     int mode_context[6][4];
+    int mv_ref_ct_a[6][4][2];
+    int mode_context_a[6][4];
 #endif
 
     int vp8_mode_contexts[6][4];
 
     unsigned int current_video_frame;
-
     int near_boffset[3];
     int version;
 
index 661c28d2db0a2b7910972966e2afb92a7ce2f26e..0659f930880479090da6ab8d48f0f24d85d2af05 100644 (file)
@@ -848,8 +848,16 @@ static void init_frame(VP8D_COMP *pbi)
         vpx_memcpy(&pc->lfc_a, &pc->fc, sizeof(pc->fc));
 
 #if CONFIG_NEWNEAR
-        vp8_init_mv_ref_counts(&pbi->common);
-#endif
+        vp8_init_mode_contexts(&pbi->common);
+        vpx_memcpy( pbi->common.vp8_mode_contexts,
+                    pbi->common.mode_context,
+                    sizeof(pbi->common.mode_context));
+
+#else
+        vpx_memcpy( pbi->common.vp8_mode_contexts,
+                    default_vp8_mode_contexts,
+                    sizeof(default_vp8_mode_contexts));
+#endif /* CONFIG_NEWNEAR */
     }
     else
     {
@@ -1236,9 +1244,23 @@ int vp8_decode_frame(VP8D_COMP *pbi)
 #endif
 
         if(pc->refresh_alt_ref_frame)
+        {
             vpx_memcpy(&pc->fc, &pc->lfc_a, sizeof(pc->fc));
+#if CONFIG_NEWNEAR
+            vpx_memcpy( pc->vp8_mode_contexts,
+                        pc->mode_context_a,
+                        sizeof(pc->vp8_mode_contexts));
+#endif
+        }
         else
+        {
             vpx_memcpy(&pc->fc, &pc->lfc, sizeof(pc->fc));
+#if CONFIG_NEWNEAR
+            vpx_memcpy( pc->vp8_mode_contexts,
+                        pc->mode_context,
+                        sizeof(pc->vp8_mode_contexts));
+#endif
+        }
 
         /* Buffer to buffer copy flags. */
         pc->copy_buffer_to_gf = 0;
@@ -1351,25 +1373,9 @@ int vp8_decode_frame(VP8D_COMP *pbi)
 
     vp8_decode_mode_mvs(pbi);
 #if CONFIG_NEWNEAR
-    if(!pbi->common.refresh_alt_ref_frame)
+    if(pbi->common.frame_type != KEY_FRAME)
     {
         vp8_update_mode_context(&pbi->common);
-        vpx_memcpy( pc->vp8_mode_contexts,
-                    pbi->common.mode_context,
-                    sizeof(pbi->common.mode_context));
-
-            if(0) //pbi->common.current_video_frame<2)
-            {
-                printf("mv_ref_ct on frame %d:\n",
-                        pbi->common.current_video_frame);
-                print_mv_ref_cts(&pbi->common);
-
-                printf("mode_contexts on frame %d:\n",
-                        pbi->common.current_video_frame);
-                print_mode_contexts();
-            }
-
-
     }
 #endif
 
index 3b9637dc1cf601dd7e09b020225ed7077197b82a..46498c75860f3d6202f76193e4606770b128655b 100644 (file)
@@ -2319,25 +2319,7 @@ void vp8_pack_bitstream(VP8_COMP *cpi, unsigned char *dest, unsigned long *size)
         pack_inter_mode_mvs(cpi);
 
 #if CONFIG_NEWNEAR
-        if(!cpi->common.refresh_alt_ref_frame)
-        {
-            vp8_update_mode_context(&cpi->common);
-            vpx_memcpy( pc->vp8_mode_contexts,
-                        cpi->common.mode_context,
-                        sizeof(cpi->common.mode_context));
-
-            if(0) //(cpi->common.current_video_frame<2)
-            {
-
-                printf("mv_ref_ct on frame %d:\n",
-                        cpi->common.current_video_frame);
-                print_mv_ref_cts(&cpi->common);
-
-                printf("mode_contexts on frame %d:\n",
-                        cpi->common.current_video_frame);
-                print_mode_contexts();
-            }
-        }
+        vp8_update_mode_context(&cpi->common);
 #endif
 
 #ifdef ENTROPY_STATS
index 416c4d5eb6a19b78c5424730d508728e75b40308..f9b450e3cd01ca86da89b2b5cc8fce9dbae2571d 100644 (file)
@@ -25,7 +25,6 @@ extern void accum_mv_refs(MB_PREDICTION_MODE, const int near_mv_ref_cts[4]);
 #define MAX_FULL_PEL_VAL ((1 << (MAX_MVSEARCH_STEPS)) - 1)      // Max full pel mv specified in 1 pel units
 #define MAX_FIRST_STEP (1 << (MAX_MVSEARCH_STEPS-1))            // Maximum size of the first step in full pel units
 
-extern void print_mode_context(void);
 extern int vp8_mv_bit_cost(int_mv *mv, int_mv *ref, int *mvcost[2], int Weight);
 extern void vp8_init_dsmotion_compensation(MACROBLOCK *x, int stride);
 extern void vp8_init3smotion_compensation(MACROBLOCK *x,  int stride);
index 6d929067586967c111dfb6beb4d6de2d4aceff95..f9a8f274d6f064f3b681f7a91d76938d01eba0c4 100644 (file)
@@ -2630,7 +2630,9 @@ void vp8_remove_compressor(VP8_PTR *ptr)
                                    - cpi->first_time_stamp_ever) / 10000000.000;
             double total_encode_time = (cpi->time_receive_data + cpi->time_compress_data)   / 1000.000;
             double dr = (double)cpi->bytes * (double) 8 / (double)1000  / time_encoded;
-
+#if CONFIG_NEWNEAR&&defined(MODE_STATS)
+            print_mode_contexts(&cpi->common);
+#endif
             if (cpi->b_calculate_psnr)
             {
                 YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
@@ -4361,6 +4363,8 @@ static void encode_frame_to_data_rate
             /* setup entropy for nonkey frame */
             vp8_setup_inter_frame(cpi);
         }
+
+
         // transform / motion compensation build reconstruction frame
         vp8_encode_frame(cpi);
 
index 62735d5d3366ee55b0ad4ff716af8b577c7d039e..bc11457483233a97e35f0a96530aa0a8617c8707 100644 (file)
@@ -266,15 +266,40 @@ void vp8_setup_key_frame(VP8_COMP *cpi)
     vpx_memcpy(&cpi->common.lfc_a, &cpi->common.fc, sizeof(cpi->common.fc));
 
 #if CONFIG_NEWNEAR
-    vp8_init_mv_ref_counts(&cpi->common);
-#endif
+    vp8_init_mode_contexts(&cpi->common);
+    vpx_memcpy( cpi->common.vp8_mode_contexts,
+                cpi->common.mode_context,
+                sizeof(cpi->common.mode_context));
+#else
+    vpx_memcpy( cpi->common.vp8_mode_contexts,
+                default_vp8_mode_contexts,
+                sizeof(default_vp8_mode_contexts));
+#endif /* CONFIG_NEWNEAR */
 }
 void vp8_setup_inter_frame(VP8_COMP *cpi)
 {
     if(cpi->common.refresh_alt_ref_frame)
-        vpx_memcpy(&cpi->common.fc, &cpi->common.lfc_a, sizeof(cpi->common.fc));
+    {
+        vpx_memcpy( &cpi->common.fc,
+                    &cpi->common.lfc_a,
+                    sizeof(cpi->common.fc));
+#if CONFIG_NEWNEAR
+        vpx_memcpy( cpi->common.vp8_mode_contexts,
+                    cpi->common.mode_context_a,
+                    sizeof(cpi->common.vp8_mode_contexts));
+#endif /* CONFIG_NEWNEAR */
+    }
     else
-        vpx_memcpy(&cpi->common.fc, &cpi->common.lfc, sizeof(cpi->common.fc));
+    {
+        vpx_memcpy( &cpi->common.fc,
+                    &cpi->common.lfc,
+                    sizeof(cpi->common.fc));
+#if CONFIG_NEWNEAR
+        vpx_memcpy( cpi->common.vp8_mode_contexts,
+                    cpi->common.mode_context,
+                    sizeof(cpi->common.vp8_mode_contexts));
+#endif /* CONFIG_NEWNEAR */
+    }
 }