blob: 08415c71a2fe6af45dee623045ab32edb4d7a4f3 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
Yaowu Xuf883b422016-08-30 14:01:10 -070012#ifndef AV1_COMMON_ENTROPY_H_
13#define AV1_COMMON_ENTROPY_H_
Yaowu Xuc27fc142016-08-22 16:08:15 -070014
Alex Converseec6fb642016-10-19 11:31:48 -070015#include "./aom_config.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070016#include "aom/aom_integer.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070017#include "aom_dsp/prob.h"
18
Yaowu Xuc27fc142016-08-22 16:08:15 -070019#include "av1/common/common.h"
20#include "av1/common/enums.h"
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
26#define DIFF_UPDATE_PROB 252
27#define GROUP_DIFF_UPDATE_PROB 252
28
29#if CONFIG_ENTROPY
30#define COEF_PROBS_BUFS 16
31#define QCTX_BIN_BITS 2
32#define QCTX_BINS (1 << QCTX_BIN_BITS)
33#endif // CONFIG_ENTROPY
34
35// Coefficient token alphabet
36#define ZERO_TOKEN 0 // 0 Extra Bits 0+0
37#define ONE_TOKEN 1 // 1 Extra Bits 0+1
38#define TWO_TOKEN 2 // 2 Extra Bits 0+1
39#define THREE_TOKEN 3 // 3 Extra Bits 0+1
40#define FOUR_TOKEN 4 // 4 Extra Bits 0+1
41#define CATEGORY1_TOKEN 5 // 5-6 Extra Bits 1+1
42#define CATEGORY2_TOKEN 6 // 7-10 Extra Bits 2+1
43#define CATEGORY3_TOKEN 7 // 11-18 Extra Bits 3+1
44#define CATEGORY4_TOKEN 8 // 19-34 Extra Bits 4+1
45#define CATEGORY5_TOKEN 9 // 35-66 Extra Bits 5+1
46#define CATEGORY6_TOKEN 10 // 67+ Extra Bits 14+1
47#define EOB_TOKEN 11 // EOB Extra Bits 0+0
48
49#define ENTROPY_TOKENS 12
50
51#define ENTROPY_NODES 11
52
Yaowu Xuf883b422016-08-30 14:01:10 -070053DECLARE_ALIGNED(16, extern const uint8_t, av1_pt_energy_class[ENTROPY_TOKENS]);
Yaowu Xuc27fc142016-08-22 16:08:15 -070054
55#define CAT1_MIN_VAL 5
56#define CAT2_MIN_VAL 7
57#define CAT3_MIN_VAL 11
58#define CAT4_MIN_VAL 19
59#define CAT5_MIN_VAL 35
60#define CAT6_MIN_VAL 67
61
62// Extra bit probabilities.
Yaowu Xuf883b422016-08-30 14:01:10 -070063DECLARE_ALIGNED(16, extern const uint8_t, av1_cat1_prob[1]);
64DECLARE_ALIGNED(16, extern const uint8_t, av1_cat2_prob[2]);
65DECLARE_ALIGNED(16, extern const uint8_t, av1_cat3_prob[3]);
66DECLARE_ALIGNED(16, extern const uint8_t, av1_cat4_prob[4]);
67DECLARE_ALIGNED(16, extern const uint8_t, av1_cat5_prob[5]);
68DECLARE_ALIGNED(16, extern const uint8_t, av1_cat6_prob[14]);
Yaowu Xuc27fc142016-08-22 16:08:15 -070069
Yaowu Xuf883b422016-08-30 14:01:10 -070070#if CONFIG_AOM_HIGHBITDEPTH
71DECLARE_ALIGNED(16, extern const uint8_t, av1_cat1_prob_high10[1]);
72DECLARE_ALIGNED(16, extern const uint8_t, av1_cat2_prob_high10[2]);
73DECLARE_ALIGNED(16, extern const uint8_t, av1_cat3_prob_high10[3]);
74DECLARE_ALIGNED(16, extern const uint8_t, av1_cat4_prob_high10[4]);
75DECLARE_ALIGNED(16, extern const uint8_t, av1_cat5_prob_high10[5]);
76DECLARE_ALIGNED(16, extern const uint8_t, av1_cat6_prob_high10[16]);
77DECLARE_ALIGNED(16, extern const uint8_t, av1_cat1_prob_high12[1]);
78DECLARE_ALIGNED(16, extern const uint8_t, av1_cat2_prob_high12[2]);
79DECLARE_ALIGNED(16, extern const uint8_t, av1_cat3_prob_high12[3]);
80DECLARE_ALIGNED(16, extern const uint8_t, av1_cat4_prob_high12[4]);
81DECLARE_ALIGNED(16, extern const uint8_t, av1_cat5_prob_high12[5]);
82DECLARE_ALIGNED(16, extern const uint8_t, av1_cat6_prob_high12[18]);
83#endif // CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -070084
85#define EOB_MODEL_TOKEN 3
86
87typedef struct {
Yaowu Xuf883b422016-08-30 14:01:10 -070088 const aom_prob *prob;
Yaowu Xuc27fc142016-08-22 16:08:15 -070089 int len;
90 int base_val;
91 const int16_t *cost;
Yaowu Xuf883b422016-08-30 14:01:10 -070092} av1_extra_bit;
Yaowu Xuc27fc142016-08-22 16:08:15 -070093
94// indexed by token value
Yaowu Xuf883b422016-08-30 14:01:10 -070095extern const av1_extra_bit av1_extra_bits[ENTROPY_TOKENS];
96#if CONFIG_AOM_HIGHBITDEPTH
97extern const av1_extra_bit av1_extra_bits_high10[ENTROPY_TOKENS];
98extern const av1_extra_bit av1_extra_bits_high12[ENTROPY_TOKENS];
99#endif // CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700100
101#define DCT_MAX_VALUE 16384
Yaowu Xuf883b422016-08-30 14:01:10 -0700102#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700103#define DCT_MAX_VALUE_HIGH10 65536
104#define DCT_MAX_VALUE_HIGH12 262144
Yaowu Xuf883b422016-08-30 14:01:10 -0700105#endif // CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700106
107/* Coefficients are predicted via a 3-dimensional probability table. */
108
109#define REF_TYPES 2 // intra=0, inter=1
110
111/* Middle dimension reflects the coefficient position within the transform. */
112#define COEF_BANDS 6
113
114/* Inside dimension is measure of nearby complexity, that reflects the energy
115 of nearby coefficients are nonzero. For the first coefficient (DC, unless
116 block type is 0), we look at the (already encoded) blocks above and to the
117 left of the current block. The context index is then the number (0,1,or 2)
118 of these blocks having nonzero coefficients.
119 After decoding a coefficient, the measure is determined by the size of the
120 most recently decoded coefficient.
121 Note that the intuitive meaning of this measure changes as coefficients
122 are decoded, e.g., prior to the first token, a zero means that my neighbors
123 are empty while, after the first token, because of the use of end-of-block,
124 a zero means we just decoded a zero and hence guarantees that a non-zero
125 coefficient will appear later in this block. However, this shift
126 in meaning is perfectly OK because our context depends also on the
127 coefficient band (and since zigzag positions 0, 1, and 2 are in
128 distinct bands). */
129
130#define COEFF_CONTEXTS 6
Debargha Mukherjee3c42c092016-09-29 09:17:36 -0700131#define COEFF_CONTEXTS0 3 // for band 0
132#define BAND_COEFF_CONTEXTS(band) \
133 ((band) == 0 ? COEFF_CONTEXTS0 : COEFF_CONTEXTS)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700134
135// #define ENTROPY_STATS
136
clang-format67948d32016-09-07 22:40:40 -0700137typedef unsigned int av1_coeff_count[REF_TYPES][COEF_BANDS][COEFF_CONTEXTS]
138 [ENTROPY_TOKENS];
139typedef unsigned int av1_coeff_stats[REF_TYPES][COEF_BANDS][COEFF_CONTEXTS]
140 [ENTROPY_NODES][2];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700141
142#define SUBEXP_PARAM 4 /* Subexponential code parameter */
143#define MODULUS_PARAM 13 /* Modulus parameter */
144
Yaowu Xuf883b422016-08-30 14:01:10 -0700145struct AV1Common;
146void av1_default_coef_probs(struct AV1Common *cm);
147void av1_adapt_coef_probs(struct AV1Common *cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700148#if CONFIG_ENTROPY
Yaowu Xuf883b422016-08-30 14:01:10 -0700149void av1_partial_adapt_probs(struct AV1Common *cm, int mi_row, int mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700150#endif // CONFIG_ENTROPY
151
152// This is the index in the scan order beyond which all coefficients for
153// 8x8 transform and above are in the top band.
154// This macro is currently unused but may be used by certain implementations
155#define MAXBAND_INDEX 21
156
Debargha Mukherjee153e1f82016-11-17 09:59:14 -0800157DECLARE_ALIGNED(16, extern const uint8_t,
158 av1_coefband_trans_8x8plus[MAX_TX_SQUARE]);
Yaowu Xuf883b422016-08-30 14:01:10 -0700159DECLARE_ALIGNED(16, extern const uint8_t, av1_coefband_trans_4x8_8x4[32]);
Yaowu Xuf883b422016-08-30 14:01:10 -0700160DECLARE_ALIGNED(16, extern const uint8_t, av1_coefband_trans_4x4[16]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700161
162DECLARE_ALIGNED(16, extern const uint16_t, band_count_table[TX_SIZES_ALL][8]);
163DECLARE_ALIGNED(16, extern const uint16_t,
164 band_cum_count_table[TX_SIZES_ALL][8]);
165
166static INLINE const uint8_t *get_band_translate(TX_SIZE tx_size) {
167 switch (tx_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700168 case TX_4X4: return av1_coefband_trans_4x4;
Debargha Mukherjeee8c6f5f2016-12-16 13:24:39 -0800169 case TX_8X4:
Yaowu Xuf883b422016-08-30 14:01:10 -0700170 case TX_4X8: return av1_coefband_trans_4x8_8x4;
Yaowu Xuf883b422016-08-30 14:01:10 -0700171 default: return av1_coefband_trans_8x8plus;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700172 }
173}
174
175// 128 lists of probabilities are stored for the following ONE node probs:
176// 1, 3, 5, 7, ..., 253, 255
177// In between probabilities are interpolated linearly
178
179#define COEFF_PROB_MODELS 255
180
181#define UNCONSTRAINED_NODES 3
182
183#define PIVOT_NODE 2 // which node is pivot
184
185#define MODEL_NODES (ENTROPY_NODES - UNCONSTRAINED_NODES)
Yaowu Xuf883b422016-08-30 14:01:10 -0700186extern const aom_tree_index av1_coef_con_tree[TREE_SIZE(ENTROPY_TOKENS)];
187extern const aom_prob av1_pareto8_full[COEFF_PROB_MODELS][MODEL_NODES];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700188
Yaowu Xuf883b422016-08-30 14:01:10 -0700189typedef aom_prob av1_coeff_probs_model[REF_TYPES][COEF_BANDS][COEFF_CONTEXTS]
190 [UNCONSTRAINED_NODES];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700191
clang-format67948d32016-09-07 22:40:40 -0700192typedef unsigned int av1_coeff_count_model[REF_TYPES][COEF_BANDS]
193 [COEFF_CONTEXTS]
194 [UNCONSTRAINED_NODES + 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700195
Yaowu Xuf883b422016-08-30 14:01:10 -0700196void av1_model_to_full_probs(const aom_prob *model, aom_prob *full);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700197
Alex Converseaca9feb2016-10-10 11:08:10 -0700198#if CONFIG_EC_MULTISYMBOL
Nathan E. Egge46e84902016-08-24 14:52:42 -0400199typedef aom_cdf_prob coeff_cdf_model[REF_TYPES][COEF_BANDS][COEFF_CONTEXTS]
200 [ENTROPY_TOKENS];
201extern const aom_cdf_prob av1_pareto8_token_probs[COEFF_PROB_MODELS]
202 [ENTROPY_TOKENS - 2];
203struct frame_contexts;
204void av1_coef_pareto_cdfs(struct frame_contexts *fc);
Alex Converseaca9feb2016-10-10 11:08:10 -0700205#endif // CONFIG_EC_MULTISYMBOL
Nathan E. Egge46e84902016-08-24 14:52:42 -0400206
Yaowu Xuc27fc142016-08-22 16:08:15 -0700207typedef char ENTROPY_CONTEXT;
208
209static INLINE int combine_entropy_contexts(ENTROPY_CONTEXT a,
210 ENTROPY_CONTEXT b) {
211 return (a != 0) + (b != 0);
212}
213
214static INLINE int get_entropy_context(TX_SIZE tx_size, const ENTROPY_CONTEXT *a,
215 const ENTROPY_CONTEXT *l) {
216 ENTROPY_CONTEXT above_ec = 0, left_ec = 0;
217
Jingning Han48876392016-12-01 15:42:24 -0800218#if CONFIG_CB4X4
Jingning Hanaa8a4a52016-12-13 17:30:48 -0800219 switch (tx_size) {
Jingning Han48876392016-12-01 15:42:24 -0800220 case TX_2X2:
221 above_ec = a[0] != 0;
222 left_ec = l[0] != 0;
223 break;
Jingning Hanaa8a4a52016-12-13 17:30:48 -0800224 case TX_4X4:
225 above_ec = !!*(const uint16_t *)a;
226 left_ec = !!*(const uint16_t *)l;
227 break;
228 case TX_4X8:
229 above_ec = !!*(const uint16_t *)a;
230 left_ec = !!*(const uint32_t *)l;
231 break;
232 case TX_8X4:
233 above_ec = !!*(const uint32_t *)a;
234 left_ec = !!*(const uint16_t *)l;
235 break;
236 case TX_8X8:
237 above_ec = !!*(const uint32_t *)a;
238 left_ec = !!*(const uint32_t *)l;
239 break;
240 case TX_8X16:
241 above_ec = !!*(const uint32_t *)a;
242 left_ec = !!*(const uint64_t *)l;
243 break;
244 case TX_16X8:
245 above_ec = !!*(const uint64_t *)a;
246 left_ec = !!*(const uint32_t *)l;
247 break;
248 case TX_16X16:
249 above_ec = !!*(const uint64_t *)a;
250 left_ec = !!*(const uint64_t *)l;
251 break;
252 case TX_16X32:
253 above_ec = !!*(const uint64_t *)a;
254 left_ec = !!(*(const uint64_t *)l | *(const uint64_t *)(l + 8));
255 break;
256 case TX_32X16:
257 above_ec = !!(*(const uint64_t *)a | *(const uint64_t *)(a + 8));
258 left_ec = !!*(const uint64_t *)l;
259 break;
260 case TX_32X32:
261 above_ec = !!(*(const uint64_t *)a | *(const uint64_t *)(a + 8));
262 left_ec = !!(*(const uint64_t *)l | *(const uint64_t *)(l + 8));
263 break;
264 default: assert(0 && "Invalid transform size."); break;
265 }
266 return combine_entropy_contexts(above_ec, left_ec);
Jingning Han48876392016-12-01 15:42:24 -0800267#endif
Jingning Hanaa8a4a52016-12-13 17:30:48 -0800268
269 switch (tx_size) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700270 case TX_4X4:
271 above_ec = a[0] != 0;
272 left_ec = l[0] != 0;
273 break;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700274 case TX_4X8:
275 above_ec = a[0] != 0;
276 left_ec = !!*(const uint16_t *)l;
277 break;
278 case TX_8X4:
279 above_ec = !!*(const uint16_t *)a;
280 left_ec = l[0] != 0;
281 break;
282 case TX_8X16:
283 above_ec = !!*(const uint16_t *)a;
284 left_ec = !!*(const uint32_t *)l;
285 break;
286 case TX_16X8:
287 above_ec = !!*(const uint32_t *)a;
288 left_ec = !!*(const uint16_t *)l;
289 break;
290 case TX_16X32:
291 above_ec = !!*(const uint32_t *)a;
292 left_ec = !!*(const uint64_t *)l;
293 break;
294 case TX_32X16:
295 above_ec = !!*(const uint64_t *)a;
296 left_ec = !!*(const uint32_t *)l;
297 break;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700298 case TX_8X8:
299 above_ec = !!*(const uint16_t *)a;
300 left_ec = !!*(const uint16_t *)l;
301 break;
302 case TX_16X16:
303 above_ec = !!*(const uint32_t *)a;
304 left_ec = !!*(const uint32_t *)l;
305 break;
306 case TX_32X32:
307 above_ec = !!*(const uint64_t *)a;
308 left_ec = !!*(const uint64_t *)l;
309 break;
Debargha Mukherjee153e1f82016-11-17 09:59:14 -0800310#if CONFIG_TX64X64
311 case TX_64X64:
312 above_ec = !!(*(const uint64_t *)a | *(const uint64_t *)(a + 8));
313 left_ec = !!(*(const uint64_t *)l | *(const uint64_t *)(l + 8));
314 break;
315#endif // CONFIG_TX64X64
Yaowu Xuc27fc142016-08-22 16:08:15 -0700316 default: assert(0 && "Invalid transform size."); break;
317 }
318 return combine_entropy_contexts(above_ec, left_ec);
319}
320
Yaowu Xuc27fc142016-08-22 16:08:15 -0700321#define COEF_COUNT_SAT 24
322#define COEF_MAX_UPDATE_FACTOR 112
323#define COEF_COUNT_SAT_AFTER_KEY 24
324#define COEF_MAX_UPDATE_FACTOR_AFTER_KEY 128
325
Angie Chianged8cd9a2016-10-21 16:44:47 -0700326#if CONFIG_ADAPT_SCAN
327#define ADAPT_SCAN_UPDATE_RATE_16 (1 << 13)
328#endif
329
Yaowu Xuf883b422016-08-30 14:01:10 -0700330static INLINE aom_prob av1_merge_probs(aom_prob pre_prob,
331 const unsigned int ct[2],
332 unsigned int count_sat,
333 unsigned int max_update_factor) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700334 return merge_probs(pre_prob, ct, count_sat, max_update_factor);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700335}
336
Yaowu Xuf883b422016-08-30 14:01:10 -0700337static INLINE aom_prob av1_mode_mv_merge_probs(aom_prob pre_prob,
338 const unsigned int ct[2]) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700339 return mode_mv_merge_probs(pre_prob, ct);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700340}
341
342#ifdef __cplusplus
343} // extern "C"
344#endif
345
Yaowu Xuf883b422016-08-30 14:01:10 -0700346#endif // AV1_COMMON_ENTROPY_H_