Unify binary symbol design for LAST/LAST2

Current encoding of single ref assigns bit 0 to LAST_FRAME and bit 1
to LAST2_frame, whereas the encoding of compound ref assigns bit 1 to
LAST_FRAME and bit 0 to LAST_FRAME. This patch unifies the design and
makes the binary symbol assignment for LAST/LAST2 consistent
regardless of the single/compound ref scenarios.

This patch incurs a small syntax change but should not have noticeable
coding performance impact. Once BUG 973 is fully addressed, default
cdfs for reference frame coding will be updated.

BUG=aomedia:973

Change-Id: I1f3027965c0bb2d343bc8ad873f7c0015123f151
diff --git a/av1/common/pred_common.c b/av1/common/pred_common.c
index a97fc76..334b822 100644
--- a/av1/common/pred_common.c
+++ b/av1/common/pred_common.c
@@ -602,10 +602,10 @@
       const MB_MODE_INFO *edge_mbmi = above_intra ? left_mbmi : above_mbmi;
 
       if (!has_second_ref(edge_mbmi))  // single pred (1/3)
-        pred_context = 1 + 2 * (edge_mbmi->ref_frame[0] != LAST_FRAME);
+        pred_context = 1 + 2 * (edge_mbmi->ref_frame[0] != LAST2_FRAME);
       else  // comp pred (1/3)
         pred_context =
-            1 + 2 * (edge_mbmi->ref_frame[fwd_ref_sign_idx] != LAST_FRAME);
+            1 + 2 * (edge_mbmi->ref_frame[fwd_ref_sign_idx] != LAST2_FRAME);
     } else {  // inter/inter
       const int l_sg = !has_second_ref(left_mbmi);
       const int a_sg = !has_second_ref(above_mbmi);
@@ -616,10 +616,10 @@
           l_sg ? left_mbmi->ref_frame[0]
                : left_mbmi->ref_frame[fwd_ref_sign_idx];
 
-      if (frfa == frfl && frfa == LAST_FRAME)
+      if (frfa == frfl && frfa == LAST2_FRAME)
         pred_context = 0;
       else if (l_sg && a_sg) {  // single/single
-        if (frfa == LAST_FRAME || frfl == LAST_FRAME)
+        if (frfa == LAST2_FRAME || frfl == LAST2_FRAME)
           pred_context = 1;
         else if (CHECK_GOLDEN_OR_LAST3(frfa) || CHECK_GOLDEN_OR_LAST3(frfl))
           pred_context = 2 + (frfa != frfl);
@@ -632,15 +632,14 @@
         const MV_REFERENCE_FRAME frfc = l_sg ? frfa : frfl;
         const MV_REFERENCE_FRAME rfs = a_sg ? frfa : frfl;
 
-        if (frfc == LAST_FRAME && rfs != LAST_FRAME)
+        if (frfc == LAST2_FRAME && rfs != LAST2_FRAME)
           pred_context = 1;
-        else if (rfs == LAST_FRAME && frfc != LAST_FRAME)
+        else if (rfs == LAST2_FRAME && frfc != LAST2_FRAME)
           pred_context = 2;
         else
-          pred_context =
-              3 + (frfc == LAST2_FRAME || CHECK_GOLDEN_OR_LAST3(rfs));
+          pred_context = 3 + (frfc == LAST_FRAME || CHECK_GOLDEN_OR_LAST3(rfs));
       } else {  // comp/comp
-        if (frfa == LAST_FRAME || frfl == LAST_FRAME)
+        if (frfa == LAST2_FRAME || frfl == LAST2_FRAME)
           pred_context = 2;
         else
           pred_context =
@@ -655,9 +654,9 @@
     } else {
       if (has_second_ref(edge_mbmi)) {
         pred_context =
-            4 * (edge_mbmi->ref_frame[fwd_ref_sign_idx] != LAST_FRAME);
+            4 * (edge_mbmi->ref_frame[fwd_ref_sign_idx] != LAST2_FRAME);
       } else {
-        if (edge_mbmi->ref_frame[0] == LAST_FRAME)
+        if (edge_mbmi->ref_frame[0] == LAST2_FRAME)
           pred_context = 0;
         else
           pred_context = 2 + CHECK_GOLDEN_OR_LAST3(edge_mbmi->ref_frame[0]);
diff --git a/av1/decoder/decodemv.c b/av1/decoder/decodemv.c
index 336f1aa..f0c87c5 100644
--- a/av1/decoder/decodemv.c
+++ b/av1/decoder/decodemv.c
@@ -1364,7 +1364,7 @@
         const int ctx1 = av1_get_pred_context_comp_ref_p1(cm, xd);
         const int bit1 = READ_REF_BIT(comp_ref_p1);
         if (counts) ++counts->comp_ref[ctx1][1][bit1];
-        ref_frame[!idx] = cm->comp_fwd_ref[bit1 ? 0 : 1];
+        ref_frame[!idx] = cm->comp_fwd_ref[bit1 ? 1 : 0];
       } else {
         const int ctx2 = av1_get_pred_context_comp_ref_p2(cm, xd);
         const int bit2 = READ_REF_BIT(comp_ref_p2);
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index 8e2ac59..6d9b00f 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -745,7 +745,7 @@
       WRITE_REF_BIT(bit, comp_ref_p);
 
       if (!bit) {
-        const int bit1 = mbmi->ref_frame[0] == LAST_FRAME;
+        const int bit1 = mbmi->ref_frame[0] == LAST2_FRAME;
         WRITE_REF_BIT(bit1, comp_ref_p1);
       } else {
         const int bit2 = mbmi->ref_frame[0] == GOLDEN_FRAME;
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index c53bac4..3d2b0b8 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -1161,7 +1161,7 @@
             counts->comp_ref[av1_get_pred_context_comp_ref_p(cm, xd)][0][bit]++;
             if (!bit) {
               counts->comp_ref[av1_get_pred_context_comp_ref_p1(cm, xd)][1]
-                              [ref0 == LAST_FRAME]++;
+                              [ref0 == LAST2_FRAME]++;
             } else {
               counts->comp_ref[av1_get_pred_context_comp_ref_p2(cm, xd)][2]
                               [ref0 == GOLDEN_FRAME]++;
diff --git a/av1/encoder/rdopt.c b/av1/encoder/rdopt.c
index 522761c..38818ee 100644
--- a/av1/encoder/rdopt.c
+++ b/av1/encoder/rdopt.c
@@ -6072,8 +6072,8 @@
       ref_bicomp_costs[LAST3_FRAME] += av1_cost_bit(ref_comp_p, 1);
       ref_bicomp_costs[GOLDEN_FRAME] += av1_cost_bit(ref_comp_p, 1);
 
-      ref_bicomp_costs[LAST_FRAME] += av1_cost_bit(ref_comp_p1, 1);
-      ref_bicomp_costs[LAST2_FRAME] += av1_cost_bit(ref_comp_p1, 0);
+      ref_bicomp_costs[LAST_FRAME] += av1_cost_bit(ref_comp_p1, 0);
+      ref_bicomp_costs[LAST2_FRAME] += av1_cost_bit(ref_comp_p1, 1);
 
       ref_bicomp_costs[LAST3_FRAME] += av1_cost_bit(ref_comp_p2, 0);
       ref_bicomp_costs[GOLDEN_FRAME] += av1_cost_bit(ref_comp_p2, 1);
@@ -6127,8 +6127,8 @@
       ref_costs_comp[LAST3_FRAME] += av1_cost_bit(ref_comp_p, 1);
       ref_costs_comp[GOLDEN_FRAME] += av1_cost_bit(ref_comp_p, 1);
 
-      ref_costs_comp[LAST_FRAME] += av1_cost_bit(ref_comp_p1, 1);
-      ref_costs_comp[LAST2_FRAME] += av1_cost_bit(ref_comp_p1, 0);
+      ref_costs_comp[LAST_FRAME] += av1_cost_bit(ref_comp_p1, 0);
+      ref_costs_comp[LAST2_FRAME] += av1_cost_bit(ref_comp_p1, 1);
 
       ref_costs_comp[LAST3_FRAME] += av1_cost_bit(ref_comp_p2, 0);
       ref_costs_comp[GOLDEN_FRAME] += av1_cost_bit(ref_comp_p2, 1);