From 63fc44dfa5c459c3736498f1f6e618cff3c56eeb Mon Sep 17 00:00:00 2001
From: John Koleszar <jkoleszar@google.com>
Date: Fri, 4 Feb 2011 11:36:04 -0500
Subject: [PATCH] correct quantizer initialization

The encoder was not correctly catching transitions in the quantizer
deltas. If a delta_q was set, then the quantizer would be reinitialized
on every frame, but if they transitioned to 0, the quantizer would
not be reinitialized, leading to a encode-decode mismatch.

This bug was triggered by commit 999e155, which sets a Y2 delta Q
for very low base Q levels.

Change-Id: Ia6733464a55ee4ff2edbb82c0873980d345446f5
---
 vp8/encoder/encodeframe.c | 7 -------
 vp8/encoder/onyx_if.c     | 9 +++++++++
 2 files changed, 9 insertions(+), 7 deletions(-)

diff --git a/vp8/encoder/encodeframe.c b/vp8/encoder/encodeframe.c
index d535894734..f06a4ca883 100644
--- a/vp8/encoder/encodeframe.c
+++ b/vp8/encoder/encodeframe.c
@@ -399,13 +399,6 @@ void vp8cx_frame_init_quantizer(VP8_COMP *cpi)
     // Clear Zbin mode boost for default case
     cpi->zbin_mode_boost = 0;
 
-    // vp8cx_init_quantizer() is first called in vp8_create_compressor(). A check is added here so that vp8cx_init_quantizer() is only called
-    // when these values are not all zero.
-    if (cpi->common.y1dc_delta_q | cpi->common.y2dc_delta_q | cpi->common.uvdc_delta_q | cpi->common.y2ac_delta_q | cpi->common.uvac_delta_q)
-    {
-        vp8cx_init_quantizer(cpi);
-    }
-
     // MB level quantizer setup
     vp8cx_mb_init_quantizer(cpi, &cpi->mb);
 }
diff --git a/vp8/encoder/onyx_if.c b/vp8/encoder/onyx_if.c
index df1a6f57ef..912d64179c 100644
--- a/vp8/encoder/onyx_if.c
+++ b/vp8/encoder/onyx_if.c
@@ -3123,11 +3123,14 @@ static int pick_frame_size(VP8_COMP *cpi)
 
     return 1;
 }
+
 static void set_quantizer(VP8_COMP *cpi, int Q)
 {
     VP8_COMMON *cm = &cpi->common;
     MACROBLOCKD *mbd = &cpi->mb.e_mbd;
+    int update = 0;
 
+    update |= cm->base_qindex != Q;
     cm->base_qindex = Q;
 
     cm->y1dc_delta_q = 0;
@@ -3138,13 +3141,19 @@ static void set_quantizer(VP8_COMP *cpi, int Q)
 
     if(Q<4)
     {
+        update |= cm->y2dc_delta_q != 4-Q;
         cm->y2dc_delta_q = 4-Q;
     }
+
     // Set Segment specific quatizers
     mbd->segment_feature_data[MB_LVL_ALT_Q][0] = cpi->segment_feature_data[MB_LVL_ALT_Q][0];
     mbd->segment_feature_data[MB_LVL_ALT_Q][1] = cpi->segment_feature_data[MB_LVL_ALT_Q][1];
     mbd->segment_feature_data[MB_LVL_ALT_Q][2] = cpi->segment_feature_data[MB_LVL_ALT_Q][2];
     mbd->segment_feature_data[MB_LVL_ALT_Q][3] = cpi->segment_feature_data[MB_LVL_ALT_Q][3];
+
+    if(update)
+        vp8cx_init_quantizer(cpi);
+
 }
 
 static void update_alt_ref_frame_and_stats(VP8_COMP *cpi)
-- 
GitLab