vp9_quant_common.c 2.4 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5 6
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
John Koleszar's avatar
John Koleszar committed
9 10 11
 */


12
#include "vp9/common/vp9_quant_common.h"
John Koleszar's avatar
John Koleszar committed
13

14 15
static int dc_qlookup[QINDEX_RANGE];
static int ac_qlookup[QINDEX_RANGE];
16

17 18
#define ACDC_MIN 4

19
void vp9_init_quant_tables() {
John Koleszar's avatar
John Koleszar committed
20 21 22 23 24 25 26 27 28 29 30 31 32
  int i;
  int current_val = 4;
  int last_val = 4;
  int ac_val;

  for (i = 0; i < QINDEX_RANGE; i++) {
    ac_qlookup[i] = current_val;
    current_val = (int)((double)current_val * 1.02);
    if (current_val == last_val)
      current_val++;
    last_val = current_val;

    ac_val = ac_qlookup[i];
33 34 35
    dc_qlookup[i] = (int)((0.000000305 * ac_val * ac_val * ac_val) +
                          (-0.00065 * ac_val * ac_val) +
                          (0.9 * ac_val) + 0.5);
John Koleszar's avatar
John Koleszar committed
36 37 38
    if (dc_qlookup[i] < ACDC_MIN)
      dc_qlookup[i] = ACDC_MIN;
  }
39
}
John Koleszar's avatar
John Koleszar committed
40

41
int vp9_dc_quant(int QIndex, int Delta) {
John Koleszar's avatar
John Koleszar committed
42
  int retval;
John Koleszar's avatar
John Koleszar committed
43

John Koleszar's avatar
John Koleszar committed
44
  QIndex = QIndex + Delta;
John Koleszar's avatar
John Koleszar committed
45

John Koleszar's avatar
John Koleszar committed
46 47 48 49
  if (QIndex > MAXQ)
    QIndex = MAXQ;
  else if (QIndex < 0)
    QIndex = 0;
John Koleszar's avatar
John Koleszar committed
50

John Koleszar's avatar
John Koleszar committed
51 52
  retval = dc_qlookup[ QIndex ];
  return retval;
John Koleszar's avatar
John Koleszar committed
53 54
}

55
int vp9_dc2quant(int QIndex, int Delta) {
John Koleszar's avatar
John Koleszar committed
56
  int retval;
John Koleszar's avatar
John Koleszar committed
57

John Koleszar's avatar
John Koleszar committed
58
  QIndex = QIndex + Delta;
John Koleszar's avatar
John Koleszar committed
59

John Koleszar's avatar
John Koleszar committed
60 61 62 63
  if (QIndex > MAXQ)
    QIndex = MAXQ;
  else if (QIndex < 0)
    QIndex = 0;
John Koleszar's avatar
John Koleszar committed
64

John Koleszar's avatar
John Koleszar committed
65
  retval = dc_qlookup[ QIndex ];
66

John Koleszar's avatar
John Koleszar committed
67
  return retval;
John Koleszar's avatar
John Koleszar committed
68 69

}
70
int vp9_dc_uv_quant(int QIndex, int Delta) {
John Koleszar's avatar
John Koleszar committed
71
  int retval;
John Koleszar's avatar
John Koleszar committed
72

John Koleszar's avatar
John Koleszar committed
73
  QIndex = QIndex + Delta;
John Koleszar's avatar
John Koleszar committed
74

John Koleszar's avatar
John Koleszar committed
75 76 77 78
  if (QIndex > MAXQ)
    QIndex = MAXQ;
  else if (QIndex < 0)
    QIndex = 0;
John Koleszar's avatar
John Koleszar committed
79

John Koleszar's avatar
John Koleszar committed
80
  retval = dc_qlookup[ QIndex ];
John Koleszar's avatar
John Koleszar committed
81

John Koleszar's avatar
John Koleszar committed
82
  return retval;
John Koleszar's avatar
John Koleszar committed
83 84
}

85
int vp9_ac_yquant(int QIndex) {
John Koleszar's avatar
John Koleszar committed
86
  int retval;
John Koleszar's avatar
John Koleszar committed
87

John Koleszar's avatar
John Koleszar committed
88 89 90 91
  if (QIndex > MAXQ)
    QIndex = MAXQ;
  else if (QIndex < 0)
    QIndex = 0;
John Koleszar's avatar
John Koleszar committed
92

John Koleszar's avatar
John Koleszar committed
93 94
  retval = ac_qlookup[ QIndex ];
  return retval;
John Koleszar's avatar
John Koleszar committed
95 96
}

97
int vp9_ac2quant(int QIndex, int Delta) {
John Koleszar's avatar
John Koleszar committed
98
  int retval;
John Koleszar's avatar
John Koleszar committed
99

John Koleszar's avatar
John Koleszar committed
100
  QIndex = QIndex + Delta;
John Koleszar's avatar
John Koleszar committed
101

John Koleszar's avatar
John Koleszar committed
102 103 104 105
  if (QIndex > MAXQ)
    QIndex = MAXQ;
  else if (QIndex < 0)
    QIndex = 0;
106

John Koleszar's avatar
John Koleszar committed
107 108 109
  retval = (ac_qlookup[ QIndex ] * 775) / 1000;
  if (retval < 4)
    retval = 4;
110

John Koleszar's avatar
John Koleszar committed
111
  return retval;
John Koleszar's avatar
John Koleszar committed
112
}
113
int vp9_ac_uv_quant(int QIndex, int Delta) {
John Koleszar's avatar
John Koleszar committed
114
  int retval;
John Koleszar's avatar
John Koleszar committed
115

John Koleszar's avatar
John Koleszar committed
116
  QIndex = QIndex + Delta;
John Koleszar's avatar
John Koleszar committed
117

John Koleszar's avatar
John Koleszar committed
118 119 120 121
  if (QIndex > MAXQ)
    QIndex = MAXQ;
  else if (QIndex < 0)
    QIndex = 0;
John Koleszar's avatar
John Koleszar committed
122

John Koleszar's avatar
John Koleszar committed
123 124
  retval = ac_qlookup[ QIndex ];
  return retval;
John Koleszar's avatar
John Koleszar committed
125
}