From 392a958274f6456add66363ae2dfdfc060b94fe9 Mon Sep 17 00:00:00 2001
From: John Koleszar <jkoleszar@google.com>
Date: Mon, 9 Aug 2010 13:27:26 -0400
Subject: [PATCH] avoid negative array subscript warnings

The mv_ref and sub_mv_ref token encodings are indexed from NEARESTMV
and LEFT4X4, respectively, rather than being zero-based like the
other token encodings.

Change-Id: I3699c3f84111209ecfb91097c4b900773e9a3ad5
---
 vp8/common/entropymode.c | 6 ++++--
 vp8/common/entropymode.h | 4 ----
 vp8/common/treecoder.c   | 6 ++++++
 vp8/common/treecoder.h   | 2 ++
 vp8/encoder/bitstream.c  | 6 ++++--
 vp8/encoder/rdopt.c      | 3 ++-
 6 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/vp8/common/entropymode.c b/vp8/common/entropymode.c
index 493728d5d5..41922834fd 100644
--- a/vp8/common/entropymode.c
+++ b/vp8/common/entropymode.c
@@ -264,8 +264,10 @@ void vp8_entropy_mode_init()
     vp8_tokens_from_tree(vp8_uv_mode_encodings,  vp8_uv_mode_tree);
     vp8_tokens_from_tree(vp8_mbsplit_encodings, vp8_mbsplit_tree);
 
-    vp8_tokens_from_tree(VP8_MVREFENCODINGS,   vp8_mv_ref_tree);
-    vp8_tokens_from_tree(VP8_SUBMVREFENCODINGS, vp8_sub_mv_ref_tree);
+    vp8_tokens_from_tree_offset(vp8_mv_ref_encoding_array,
+                                vp8_mv_ref_tree, NEARESTMV);
+    vp8_tokens_from_tree_offset(vp8_sub_mv_ref_encoding_array,
+                                vp8_sub_mv_ref_tree, LEFT4X4);
 
     vp8_tokens_from_tree(vp8_small_mvencodings, vp8_small_mvtree);
 }
diff --git a/vp8/common/entropymode.h b/vp8/common/entropymode.h
index afa513d3c7..3d5af7cf7d 100644
--- a/vp8/common/entropymode.h
+++ b/vp8/common/entropymode.h
@@ -54,10 +54,6 @@ extern struct vp8_token_struct vp8_mbsplit_encodings  [VP8_NUMMBSPLITS];
 extern struct vp8_token_struct vp8_mv_ref_encoding_array    [VP8_MVREFS];
 extern struct vp8_token_struct vp8_sub_mv_ref_encoding_array [VP8_SUBMVREFS];
 
-#define VP8_MVREFENCODINGS      (vp8_mv_ref_encoding_array - NEARESTMV)
-#define VP8_SUBMVREFENCODINGS   (vp8_sub_mv_ref_encoding_array - LEFT4X4)
-
-
 extern const vp8_tree_index vp8_small_mvtree[];
 
 extern struct vp8_token_struct vp8_small_mvencodings [8];
diff --git a/vp8/common/treecoder.c b/vp8/common/treecoder.c
index 5829cb7016..495abd7162 100644
--- a/vp8/common/treecoder.c
+++ b/vp8/common/treecoder.c
@@ -47,6 +47,12 @@ void vp8_tokens_from_tree(struct vp8_token_struct *p, vp8_tree t)
     tree2tok(p, t, 0, 0, 0);
 }
 
+void vp8_tokens_from_tree_offset(struct vp8_token_struct *p, vp8_tree t,
+                                 int offset)
+{
+    tree2tok(p - offset, t, 0, 0, 0);
+}
+
 static void branch_counts(
     int n,                      /* n = size of alphabet */
     vp8_token tok               [ /* n */ ],
diff --git a/vp8/common/treecoder.h b/vp8/common/treecoder.h
index c8f5af96d9..9903275362 100644
--- a/vp8/common/treecoder.h
+++ b/vp8/common/treecoder.h
@@ -54,6 +54,8 @@ typedef const struct vp8_token_struct
 /* Construct encoding array from tree. */
 
 void vp8_tokens_from_tree(struct vp8_token_struct *, vp8_tree);
+void vp8_tokens_from_tree_offset(struct vp8_token_struct *, vp8_tree,
+                                 int offset);
 
 
 /* Convert array of token occurrence counts into a table of probabilities
diff --git a/vp8/encoder/bitstream.c b/vp8/encoder/bitstream.c
index 3d7c7612de..21629841b0 100644
--- a/vp8/encoder/bitstream.c
+++ b/vp8/encoder/bitstream.c
@@ -792,7 +792,8 @@ static void write_mv_ref
 
     assert(NEARESTMV <= m  &&  m <= SPLITMV);
 
-    vp8_write_token(w, vp8_mv_ref_tree, p, VP8_MVREFENCODINGS + m);
+    vp8_write_token(w, vp8_mv_ref_tree, p,
+                    vp8_mv_ref_encoding_array - NEARESTMV + m);
 }
 
 static void write_sub_mv_ref
@@ -802,7 +803,8 @@ static void write_sub_mv_ref
 {
     assert(LEFT4X4 <= m  &&  m <= NEW4X4);
 
-    vp8_write_token(w, vp8_sub_mv_ref_tree, p, VP8_SUBMVREFENCODINGS + m);
+    vp8_write_token(w, vp8_sub_mv_ref_tree, p,
+                    vp8_sub_mv_ref_encoding_array - LEFT4X4 + m);
 }
 
 static void write_mv
diff --git a/vp8/encoder/rdopt.c b/vp8/encoder/rdopt.c
index 8fe2d206a6..3c6f0b3245 100644
--- a/vp8/encoder/rdopt.c
+++ b/vp8/encoder/rdopt.c
@@ -880,7 +880,8 @@ int vp8_cost_mv_ref(MB_PREDICTION_MODE m, const int near_mv_ref_ct[4])
     vp8_prob p [VP8_MVREFS-1];
     assert(NEARESTMV <= m  &&  m <= SPLITMV);
     vp8_mv_ref_probs(p, near_mv_ref_ct);
-    return vp8_cost_token(vp8_mv_ref_tree, p, VP8_MVREFENCODINGS + m);
+    return vp8_cost_token(vp8_mv_ref_tree, p,
+                          vp8_mv_ref_encoding_array - NEARESTMV + m);
 }
 
 void vp8_set_mbmode_and_mvs(MACROBLOCK *x, MB_PREDICTION_MODE mb, MV *mv)
-- 
GitLab