blob: 2676762be59036128e1d72133bab307faa56d9f2 [file] [log] [blame]
Deb Mukherjeeeb6ef242013-02-20 10:16:24 -08001/*
John Koleszarc2140b82010-09-09 08:16:39 -04002 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar0ea50ce2010-05-18 11:58:33 -04003 *
John Koleszar94c52e42010-06-18 12:39:21 -04004 * Use of this source code is governed by a BSD-style license
John Koleszar09202d82010-06-04 16:19:40 -04005 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
John Koleszar94c52e42010-06-18 12:39:21 -04007 * in the file PATENTS. All contributing project authors may
John Koleszar09202d82010-06-04 16:19:40 -04008 * be found in the AUTHORS file in the root of the source tree.
John Koleszar0ea50ce2010-05-18 11:58:33 -04009 */
10
Jim Bankoskic6787392012-11-28 10:41:40 -080011#include "vp9/common/vp9_entropy.h"
Jim Bankoskic6787392012-11-28 10:41:40 -080012#include "vp9/common/vp9_blockd.h"
13#include "vp9/common/vp9_onyxc_int.h"
14#include "vp9/common/vp9_entropymode.h"
Scott LaVarnway19987dc2011-08-16 16:21:21 -040015#include "vpx_mem/vpx_mem.h"
Ronald S. Bultje4cca47b2012-12-18 15:31:19 -080016#include "vpx/vpx_integer.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040017
Dmitry Kovalevca75f122013-07-15 12:26:58 -070018#define MODEL_NODES (ENTROPY_NODES - UNCONSTRAINED_NODES)
19
Ronald S. Bultje4cca47b2012-12-18 15:31:19 -080020DECLARE_ALIGNED(16, const uint8_t, vp9_norm[256]) = {
John Koleszarc6b90392012-07-13 15:21:29 -070021 0, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
22 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
23 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
24 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
25 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
26 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
27 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
28 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
29 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
30 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
31 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
32 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
33 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
34 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
35 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
36 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
Scott LaVarnwaya25f6a92011-07-19 09:17:25 -040037};
38
Paul Wilkinse5f71522013-05-09 18:47:58 +010039DECLARE_ALIGNED(16, const uint8_t,
40 vp9_coefband_trans_8x8plus[MAXBAND_INDEX + 1]) = {
41 0, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4,
42 4, 4, 4, 4, 4, 5
43};
44
Paul Wilkinse5f71522013-05-09 18:47:58 +010045DECLARE_ALIGNED(16, const uint8_t,
46 vp9_coefband_trans_4x4[MAXBAND_INDEX + 1]) = {
Paul Wilkinse41fd6e2013-05-23 15:44:45 +010047 0, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5,
48 5, 5, 5, 5, 5, 5
Paul Wilkinse5f71522013-05-09 18:47:58 +010049};
50
Paul Wilkinsc17672a2013-02-19 10:12:00 -080051DECLARE_ALIGNED(16, const uint8_t, vp9_pt_energy_class[MAX_ENTROPY_TOKENS]) = {
52 0, 1, 2, 3, 3, 4, 4, 5, 5, 5, 5, 5
Deb Mukherjee9984a152012-07-27 11:29:46 -070053};
Scott LaVarnwaya25f6a92011-07-19 09:17:25 -040054
Ronald S. Bultje513157e2013-03-25 12:14:01 -070055
John Koleszar0ea50ce2010-05-18 11:58:33 -040056
John Koleszar0ea50ce2010-05-18 11:58:33 -040057/* Array indices are identical to previously-existing CONTEXT_NODE indices */
58
Dmitry Kovaleva97fe892013-10-14 20:33:37 -070059const vp9_tree_index vp9_coef_tree[TREE_SIZE(MAX_ENTROPY_TOKENS)] = {
Deb Mukherjeeb8b3f1a2013-05-08 10:04:14 -070060 -DCT_EOB_TOKEN, 2, /* 0 = EOB */
61 -ZERO_TOKEN, 4, /* 1 = ZERO */
Deb Mukherjeeb8b3f1a2013-05-08 10:04:14 -070062 -ONE_TOKEN, 6, /* 2 = ONE */
63 8, 12, /* 3 = LOW_VAL */
64 -TWO_TOKEN, 10, /* 4 = TWO */
John Koleszarc6b90392012-07-13 15:21:29 -070065 -THREE_TOKEN, -FOUR_TOKEN, /* 5 = THREE */
Deb Mukherjee07443f12013-05-20 13:54:39 -070066 14, 16, /* 6 = HIGH_LOW */
John Koleszarc6b90392012-07-13 15:21:29 -070067 -DCT_VAL_CATEGORY1, -DCT_VAL_CATEGORY2, /* 7 = CAT_ONE */
68 18, 20, /* 8 = CAT_THREEFOUR */
Deb Mukherjee07443f12013-05-20 13:54:39 -070069 -DCT_VAL_CATEGORY3, -DCT_VAL_CATEGORY4, /* 9 = CAT_THREE */
70 -DCT_VAL_CATEGORY5, -DCT_VAL_CATEGORY6 /* 10 = CAT_FIVE */
John Koleszar0ea50ce2010-05-18 11:58:33 -040071};
72
Dmitry Kovalev24f18e12013-04-11 13:01:52 -070073struct vp9_token vp9_coef_encodings[MAX_ENTROPY_TOKENS];
John Koleszar0ea50ce2010-05-18 11:58:33 -040074
75/* Trees for extra bits. Probabilities are constant and
76 do not depend on previously encoded bits */
77
Ronald S. Bultjeebb5f2f2012-12-18 10:38:12 -080078static const vp9_prob Pcat1[] = { 159};
79static const vp9_prob Pcat2[] = { 165, 145};
80static const vp9_prob Pcat3[] = { 173, 148, 140};
81static const vp9_prob Pcat4[] = { 176, 155, 140, 135};
82static const vp9_prob Pcat5[] = { 180, 157, 141, 134, 130};
83static const vp9_prob Pcat6[] = {
Ronald S. Bultjec456b352012-12-07 14:45:05 -080084 254, 254, 254, 252, 249, 243, 230, 196, 177, 153, 140, 133, 130, 129
85};
John Koleszar0ea50ce2010-05-18 11:58:33 -040086
Deb Mukherjee39a90bc2013-05-17 06:40:25 -070087const vp9_tree_index vp9_coefmodel_tree[6] = {
88 -DCT_EOB_MODEL_TOKEN, 2, /* 0 = EOB */
89 -ZERO_TOKEN, 4, /* 1 = ZERO */
Deb Mukherjeeb8b3f1a2013-05-08 10:04:14 -070090 -ONE_TOKEN, -TWO_TOKEN,
Deb Mukherjee39a90bc2013-05-17 06:40:25 -070091};
Deb Mukherjeefd18d5d2013-03-13 11:03:17 -070092
Deb Mukherjee07443f12013-05-20 13:54:39 -070093// Model obtained from a 2-sided zero-centerd distribuition derived
94// from a Pareto distribution. The cdf of the distribution is:
95// cdf(x) = 0.5 + 0.5 * sgn(x) * [1 - {alpha/(alpha + |x|)} ^ beta]
96//
97// For a given beta and a given probablity of the 1-node, the alpha
98// is first solved, and then the {alpha, beta} pair is used to generate
99// the probabilities for the rest of the nodes.
Deb Mukherjee7a645e42013-05-21 14:39:22 -0700100
101// beta = 8
Dmitry Kovalevca75f122013-07-15 12:26:58 -0700102static const vp9_prob modelcoefprobs_pareto8[COEFPROB_MODELS][MODEL_NODES] = {
Deb Mukherjeede4d6822013-05-22 07:28:29 -0700103 { 3, 86, 128, 6, 86, 23, 88, 29},
104 { 9, 86, 129, 17, 88, 61, 94, 76},
105 { 15, 87, 129, 28, 89, 93, 100, 110},
106 { 20, 88, 130, 38, 91, 118, 106, 136},
107 { 26, 89, 131, 48, 92, 139, 111, 156},
108 { 31, 90, 131, 58, 94, 156, 117, 171},
109 { 37, 90, 132, 66, 95, 171, 122, 184},
110 { 42, 91, 132, 75, 97, 183, 127, 194},
111 { 47, 92, 133, 83, 98, 193, 132, 202},
112 { 52, 93, 133, 90, 100, 201, 137, 208},
113 { 57, 94, 134, 98, 101, 208, 142, 214},
114 { 62, 94, 135, 105, 103, 214, 146, 218},
115 { 66, 95, 135, 111, 104, 219, 151, 222},
116 { 71, 96, 136, 117, 106, 224, 155, 225},
117 { 76, 97, 136, 123, 107, 227, 159, 228},
118 { 80, 98, 137, 129, 109, 231, 162, 231},
119 { 84, 98, 138, 134, 110, 234, 166, 233},
120 { 89, 99, 138, 140, 112, 236, 170, 235},
121 { 93, 100, 139, 145, 113, 238, 173, 236},
122 { 97, 101, 140, 149, 115, 240, 176, 238},
123 {101, 102, 140, 154, 116, 242, 179, 239},
124 {105, 103, 141, 158, 118, 243, 182, 240},
125 {109, 104, 141, 162, 119, 244, 185, 241},
126 {113, 104, 142, 166, 120, 245, 187, 242},
127 {116, 105, 143, 170, 122, 246, 190, 243},
128 {120, 106, 143, 173, 123, 247, 192, 244},
129 {123, 107, 144, 177, 125, 248, 195, 244},
130 {127, 108, 145, 180, 126, 249, 197, 245},
131 {130, 109, 145, 183, 128, 249, 199, 245},
132 {134, 110, 146, 186, 129, 250, 201, 246},
133 {137, 111, 147, 189, 131, 251, 203, 246},
134 {140, 112, 147, 192, 132, 251, 205, 247},
135 {143, 113, 148, 194, 133, 251, 207, 247},
136 {146, 114, 149, 197, 135, 252, 208, 248},
137 {149, 115, 149, 199, 136, 252, 210, 248},
138 {152, 115, 150, 201, 138, 252, 211, 248},
139 {155, 116, 151, 204, 139, 253, 213, 249},
140 {158, 117, 151, 206, 140, 253, 214, 249},
141 {161, 118, 152, 208, 142, 253, 216, 249},
142 {163, 119, 153, 210, 143, 253, 217, 249},
143 {166, 120, 153, 212, 144, 254, 218, 250},
144 {168, 121, 154, 213, 146, 254, 220, 250},
145 {171, 122, 155, 215, 147, 254, 221, 250},
146 {173, 123, 155, 217, 148, 254, 222, 250},
147 {176, 124, 156, 218, 150, 254, 223, 250},
148 {178, 125, 157, 220, 151, 254, 224, 251},
149 {180, 126, 157, 221, 152, 254, 225, 251},
150 {183, 127, 158, 222, 153, 254, 226, 251},
151 {185, 128, 159, 224, 155, 255, 227, 251},
152 {187, 129, 160, 225, 156, 255, 228, 251},
153 {189, 131, 160, 226, 157, 255, 228, 251},
154 {191, 132, 161, 227, 159, 255, 229, 251},
155 {193, 133, 162, 228, 160, 255, 230, 252},
156 {195, 134, 163, 230, 161, 255, 231, 252},
157 {197, 135, 163, 231, 162, 255, 231, 252},
158 {199, 136, 164, 232, 163, 255, 232, 252},
159 {201, 137, 165, 233, 165, 255, 233, 252},
160 {202, 138, 166, 233, 166, 255, 233, 252},
161 {204, 139, 166, 234, 167, 255, 234, 252},
162 {206, 140, 167, 235, 168, 255, 235, 252},
163 {207, 141, 168, 236, 169, 255, 235, 252},
164 {209, 142, 169, 237, 171, 255, 236, 252},
165 {210, 144, 169, 237, 172, 255, 236, 252},
166 {212, 145, 170, 238, 173, 255, 237, 252},
167 {214, 146, 171, 239, 174, 255, 237, 253},
168 {215, 147, 172, 240, 175, 255, 238, 253},
169 {216, 148, 173, 240, 176, 255, 238, 253},
170 {218, 149, 173, 241, 177, 255, 239, 253},
171 {219, 150, 174, 241, 179, 255, 239, 253},
172 {220, 152, 175, 242, 180, 255, 240, 253},
173 {222, 153, 176, 242, 181, 255, 240, 253},
174 {223, 154, 177, 243, 182, 255, 240, 253},
175 {224, 155, 178, 244, 183, 255, 241, 253},
176 {225, 156, 178, 244, 184, 255, 241, 253},
177 {226, 158, 179, 244, 185, 255, 242, 253},
178 {228, 159, 180, 245, 186, 255, 242, 253},
179 {229, 160, 181, 245, 187, 255, 242, 253},
180 {230, 161, 182, 246, 188, 255, 243, 253},
181 {231, 163, 183, 246, 189, 255, 243, 253},
182 {232, 164, 184, 247, 190, 255, 243, 253},
183 {233, 165, 185, 247, 191, 255, 244, 253},
184 {234, 166, 185, 247, 192, 255, 244, 253},
185 {235, 168, 186, 248, 193, 255, 244, 253},
186 {236, 169, 187, 248, 194, 255, 244, 253},
187 {236, 170, 188, 248, 195, 255, 245, 253},
188 {237, 171, 189, 249, 196, 255, 245, 254},
189 {238, 173, 190, 249, 197, 255, 245, 254},
190 {239, 174, 191, 249, 198, 255, 245, 254},
191 {240, 175, 192, 249, 199, 255, 246, 254},
192 {240, 177, 193, 250, 200, 255, 246, 254},
193 {241, 178, 194, 250, 201, 255, 246, 254},
194 {242, 179, 195, 250, 202, 255, 246, 254},
195 {242, 181, 196, 250, 203, 255, 247, 254},
196 {243, 182, 197, 251, 204, 255, 247, 254},
197 {244, 184, 198, 251, 205, 255, 247, 254},
198 {244, 185, 199, 251, 206, 255, 247, 254},
199 {245, 186, 200, 251, 207, 255, 247, 254},
200 {246, 188, 201, 252, 207, 255, 248, 254},
201 {246, 189, 202, 252, 208, 255, 248, 254},
202 {247, 191, 203, 252, 209, 255, 248, 254},
203 {247, 192, 204, 252, 210, 255, 248, 254},
204 {248, 194, 205, 252, 211, 255, 248, 254},
205 {248, 195, 206, 252, 212, 255, 249, 254},
206 {249, 197, 207, 253, 213, 255, 249, 254},
207 {249, 198, 208, 253, 214, 255, 249, 254},
208 {250, 200, 210, 253, 215, 255, 249, 254},
209 {250, 201, 211, 253, 215, 255, 249, 254},
210 {250, 203, 212, 253, 216, 255, 249, 254},
211 {251, 204, 213, 253, 217, 255, 250, 254},
212 {251, 206, 214, 254, 218, 255, 250, 254},
213 {252, 207, 216, 254, 219, 255, 250, 254},
214 {252, 209, 217, 254, 220, 255, 250, 254},
215 {252, 211, 218, 254, 221, 255, 250, 254},
216 {253, 213, 219, 254, 222, 255, 250, 254},
217 {253, 214, 221, 254, 223, 255, 250, 254},
218 {253, 216, 222, 254, 224, 255, 251, 254},
219 {253, 218, 224, 254, 225, 255, 251, 254},
220 {254, 220, 225, 254, 225, 255, 251, 254},
221 {254, 222, 227, 255, 226, 255, 251, 254},
222 {254, 224, 228, 255, 227, 255, 251, 254},
223 {254, 226, 230, 255, 228, 255, 251, 254},
224 {255, 228, 231, 255, 230, 255, 251, 254},
225 {255, 230, 233, 255, 231, 255, 252, 254},
226 {255, 232, 235, 255, 232, 255, 252, 254},
227 {255, 235, 237, 255, 233, 255, 252, 254},
228 {255, 238, 240, 255, 235, 255, 252, 255},
229 {255, 241, 243, 255, 236, 255, 252, 254},
230 {255, 246, 247, 255, 239, 255, 253, 255}
Deb Mukherjeefd18d5d2013-03-13 11:03:17 -0700231};
232
Deb Mukherjeede4d6822013-05-22 07:28:29 -0700233static void extend_model_to_full_distribution(vp9_prob p,
234 vp9_prob *tree_probs) {
Dmitry Kovalev6a8ec3e2013-08-08 14:52:39 -0700235 const int l = (p - 1) / 2;
Dmitry Kovalevca75f122013-07-15 12:26:58 -0700236 const vp9_prob (*model)[MODEL_NODES] = modelcoefprobs_pareto8;
Deb Mukherjeede4d6822013-05-22 07:28:29 -0700237 if (p & 1) {
Deb Mukherjee66f413a2013-03-26 08:34:56 -0700238 vpx_memcpy(tree_probs + UNCONSTRAINED_NODES,
Deb Mukherjeede4d6822013-05-22 07:28:29 -0700239 model[l], MODEL_NODES * sizeof(vp9_prob));
240 } else {
Deb Mukherjee66f413a2013-03-26 08:34:56 -0700241 // interpolate
242 int i;
243 for (i = UNCONSTRAINED_NODES; i < ENTROPY_NODES; ++i)
Deb Mukherjeede4d6822013-05-22 07:28:29 -0700244 tree_probs[i] = (model[l][i - UNCONSTRAINED_NODES] +
245 model[l + 1][i - UNCONSTRAINED_NODES]) >> 1;
Deb Mukherjee66f413a2013-03-26 08:34:56 -0700246 }
Deb Mukherjeefd18d5d2013-03-13 11:03:17 -0700247}
Deb Mukherjee39a90bc2013-05-17 06:40:25 -0700248
Deb Mukherjeede4d6822013-05-22 07:28:29 -0700249void vp9_model_to_full_probs(const vp9_prob *model, vp9_prob *full) {
250 if (full != model)
251 vpx_memcpy(full, model, sizeof(vp9_prob) * UNCONSTRAINED_NODES);
252 extend_model_to_full_distribution(model[PIVOT_NODE], full);
Deb Mukherjee39a90bc2013-05-17 06:40:25 -0700253}
254
Ronald S. Bultjec456b352012-12-07 14:45:05 -0800255static vp9_tree_index cat1[2], cat2[4], cat3[6], cat4[8], cat5[10], cat6[28];
John Koleszar0ea50ce2010-05-18 11:58:33 -0400256
Ronald S. Bultje6a4b1e52012-10-31 14:40:53 -0700257static void init_bit_tree(vp9_tree_index *p, int n) {
John Koleszarc6b90392012-07-13 15:21:29 -0700258 int i = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400259
John Koleszarc6b90392012-07-13 15:21:29 -0700260 while (++i < n) {
261 p[0] = p[1] = i << 1;
262 p += 2;
263 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400264
John Koleszarc6b90392012-07-13 15:21:29 -0700265 p[0] = p[1] = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400266}
267
John Koleszarc6b90392012-07-13 15:21:29 -0700268static void init_bit_trees() {
269 init_bit_tree(cat1, 1);
270 init_bit_tree(cat2, 2);
271 init_bit_tree(cat3, 3);
272 init_bit_tree(cat4, 4);
273 init_bit_tree(cat5, 5);
Ronald S. Bultjec456b352012-12-07 14:45:05 -0800274 init_bit_tree(cat6, 14);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400275}
276
Dmitry Kovaleva97fe892013-10-14 20:33:37 -0700277const vp9_extra_bit vp9_extra_bits[MAX_ENTROPY_TOKENS] = {
John Koleszarc6b90392012-07-13 15:21:29 -0700278 { 0, 0, 0, 0},
279 { 0, 0, 0, 1},
280 { 0, 0, 0, 2},
281 { 0, 0, 0, 3},
282 { 0, 0, 0, 4},
283 { cat1, Pcat1, 1, 5},
284 { cat2, Pcat2, 2, 7},
285 { cat3, Pcat3, 3, 11},
286 { cat4, Pcat4, 4, 19},
287 { cat5, Pcat5, 5, 35},
Ronald S. Bultjec456b352012-12-07 14:45:05 -0800288 { cat6, Pcat6, 14, 67},
John Koleszarc6b90392012-07-13 15:21:29 -0700289 { 0, 0, 0, 0}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400290};
Scott LaVarnway19987dc2011-08-16 16:21:21 -0400291
Jim Bankoskic6787392012-11-28 10:41:40 -0800292#include "vp9/common/vp9_default_coef_probs.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -0400293
James Zern924d7452013-08-22 20:03:08 -0700294void vp9_default_coef_probs(VP9_COMMON *cm) {
295 vp9_copy(cm->fc.coef_probs[TX_4X4], default_coef_probs_4x4);
296 vp9_copy(cm->fc.coef_probs[TX_8X8], default_coef_probs_8x8);
297 vp9_copy(cm->fc.coef_probs[TX_16X16], default_coef_probs_16x16);
298 vp9_copy(cm->fc.coef_probs[TX_32X32], default_coef_probs_32x32);
Deb Mukherjee08f64712011-02-14 14:18:18 -0800299}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400300
Ronald S. Bultje9bc5f3e2012-10-30 16:25:53 -0700301void vp9_coef_tree_initialize() {
John Koleszarc6b90392012-07-13 15:21:29 -0700302 init_bit_trees();
Ronald S. Bultje43da8f12012-10-30 17:12:12 -0700303 vp9_tokens_from_tree(vp9_coef_encodings, vp9_coef_tree);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400304}
Deb Mukherjee1fe85a32012-06-05 15:25:07 -0700305
John Koleszarc6b90392012-07-13 15:21:29 -0700306// #define COEF_COUNT_TESTING
Deb Mukherjee1fe85a32012-06-05 15:25:07 -0700307
308#define COEF_COUNT_SAT 24
309#define COEF_MAX_UPDATE_FACTOR 112
310#define COEF_COUNT_SAT_KEY 24
311#define COEF_MAX_UPDATE_FACTOR_KEY 112
312#define COEF_COUNT_SAT_AFTER_KEY 24
313#define COEF_MAX_UPDATE_FACTOR_AFTER_KEY 128
314
Dmitry Kovalev828119d2013-07-29 14:35:55 -0700315static void adapt_coef_probs(VP9_COMMON *cm, TX_SIZE tx_size,
Dmitry Kovalev40358dc2013-07-24 17:44:04 -0700316 unsigned int count_sat,
317 unsigned int update_factor) {
Dmitry Kovalevd9d70402013-10-09 19:46:30 -0700318 const FRAME_CONTEXT *pre_fc = &cm->frame_contexts[cm->frame_context_idx];
Dmitry Kovalev7e703de2013-07-19 17:33:10 -0700319
Dmitry Kovalev828119d2013-07-29 14:35:55 -0700320 vp9_coeff_probs_model *dst_coef_probs = cm->fc.coef_probs[tx_size];
Dmitry Kovalevd9d70402013-10-09 19:46:30 -0700321 const vp9_coeff_probs_model *pre_coef_probs = pre_fc->coef_probs[tx_size];
Dmitry Kovalev828119d2013-07-29 14:35:55 -0700322 vp9_coeff_count_model *coef_counts = cm->counts.coef[tx_size];
Ronald S. Bultjee9d68a52013-05-31 09:18:59 -0700323 unsigned int (*eob_branch_count)[REF_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS] =
Dmitry Kovalev828119d2013-07-29 14:35:55 -0700324 cm->counts.eob_branch[tx_size];
Dmitry Kovalevdf19c6b2013-11-01 15:09:43 -0700325 int i, j, k, l, m;
Deb Mukherjee39a90bc2013-05-17 06:40:25 -0700326 unsigned int branch_ct[UNCONSTRAINED_NODES][2];
Ronald S. Bultje885cf812012-12-07 16:09:59 -0800327
Deb Mukherjee66f413a2013-03-26 08:34:56 -0700328 for (i = 0; i < BLOCK_TYPES; ++i)
Ronald S. Bultje0c9e2e92013-02-19 13:36:38 -0800329 for (j = 0; j < REF_TYPES; ++j)
330 for (k = 0; k < COEF_BANDS; ++k)
331 for (l = 0; l < PREV_COEF_CONTEXTS; ++l) {
332 if (l >= 3 && k == 0)
333 continue;
Dmitry Kovalevdf19c6b2013-11-01 15:09:43 -0700334 vp9_tree_probs_from_distribution(vp9_coefmodel_tree, branch_ct,
Dmitry Kovalev81518962013-11-05 11:58:57 -0800335 coef_counts[i][j][k][l]);
Ronald S. Bultje790fb132013-03-26 16:46:09 -0700336 branch_ct[0][1] = eob_branch_count[i][j][k][l] - branch_ct[0][0];
Dmitry Kovalevdf19c6b2013-11-01 15:09:43 -0700337 for (m = 0; m < UNCONSTRAINED_NODES; ++m)
338 dst_coef_probs[i][j][k][l][m] = merge_probs(
339 pre_coef_probs[i][j][k][l][m],
340 branch_ct[m],
341 count_sat, update_factor);
Ronald S. Bultje885cf812012-12-07 16:09:59 -0800342 }
Ronald S. Bultje885cf812012-12-07 16:09:59 -0800343}
344
345void vp9_adapt_coef_probs(VP9_COMMON *cm) {
Ronald S. Bultjee9d68a52013-05-31 09:18:59 -0700346 TX_SIZE t;
Dmitry Kovalev40358dc2013-07-24 17:44:04 -0700347 unsigned int count_sat, update_factor;
Deb Mukherjee1fe85a32012-06-05 15:25:07 -0700348
Alexander Voronovd6a59fb2013-10-03 20:07:24 +0400349 if (frame_is_intra_only(cm)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700350 update_factor = COEF_MAX_UPDATE_FACTOR_KEY;
351 count_sat = COEF_COUNT_SAT_KEY;
352 } else if (cm->last_frame_type == KEY_FRAME) {
353 update_factor = COEF_MAX_UPDATE_FACTOR_AFTER_KEY; /* adapt quickly */
354 count_sat = COEF_COUNT_SAT_AFTER_KEY;
355 } else {
356 update_factor = COEF_MAX_UPDATE_FACTOR;
357 count_sat = COEF_COUNT_SAT;
358 }
Ronald S. Bultjee9d68a52013-05-31 09:18:59 -0700359 for (t = TX_4X4; t <= TX_32X32; t++)
360 adapt_coef_probs(cm, t, count_sat, update_factor);
Deb Mukherjee1fe85a32012-06-05 15:25:07 -0700361}