Line data Source code
1 : /*
2 : * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3 : *
4 : * 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.
10 : */
11 :
12 : #ifndef AV1_COMMON_ENTROPY_H_
13 : #define AV1_COMMON_ENTROPY_H_
14 :
15 : #include "./aom_config.h"
16 : #include "aom/aom_integer.h"
17 : #include "aom_dsp/prob.h"
18 :
19 : #include "av1/common/common.h"
20 : #include "av1/common/common_data.h"
21 : #include "av1/common/enums.h"
22 :
23 : #ifdef __cplusplus
24 : extern "C" {
25 : #endif
26 :
27 : #define DIFF_UPDATE_PROB 252
28 : #define GROUP_DIFF_UPDATE_PROB 252
29 :
30 : #if CONFIG_Q_ADAPT_PROBS
31 : #define QCTX_BIN_BITS 2
32 : #define QCTX_BINS (1 << QCTX_BIN_BITS)
33 : #endif // CONFIG_Q_ADAPT_PROBS
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 : #define NO_EOB 0 // Not an end-of-block
49 : #define EARLY_EOB 1 // End of block before the last position
50 : #define LAST_EOB 2 // End of block in the last position (implicit)
51 : #define BLOCK_Z_TOKEN 255 // block zero
52 : #define HEAD_TOKENS 5
53 : #define TAIL_TOKENS 9
54 : #define ONE_TOKEN_EOB 1
55 : #define ONE_TOKEN_NEOB 2
56 : #define TWO_TOKEN_PLUS_EOB 3
57 : #define TWO_TOKEN_PLUS_NEOB 4
58 : #define ENTROPY_TOKENS 12
59 :
60 : #define ENTROPY_NODES 11
61 :
62 : #if CONFIG_LV_MAP
63 : #define TXB_SKIP_CONTEXTS 13
64 : #define SIG_COEF_CONTEXTS 20
65 : #define EOB_COEF_CONTEXTS 25
66 : #define COEFF_BASE_CONTEXTS 42
67 : #define DC_SIGN_CONTEXTS 3
68 :
69 : #define BR_TMP_OFFSET 12
70 : #define BR_REF_CAT 4
71 : #define LEVEL_CONTEXTS (BR_TMP_OFFSET * BR_REF_CAT)
72 :
73 : #define NUM_BASE_LEVELS 2
74 : #define COEFF_BASE_RANGE (15 - NUM_BASE_LEVELS)
75 :
76 : #define COEFF_CONTEXT_BITS 6
77 : #define COEFF_CONTEXT_MASK ((1 << COEFF_CONTEXT_BITS) - 1)
78 : #endif
79 :
80 : DECLARE_ALIGNED(16, extern const uint8_t, av1_pt_energy_class[ENTROPY_TOKENS]);
81 :
82 : #define CAT1_MIN_VAL 5
83 : #define CAT2_MIN_VAL 7
84 : #define CAT3_MIN_VAL 11
85 : #define CAT4_MIN_VAL 19
86 : #define CAT5_MIN_VAL 35
87 : #define CAT6_MIN_VAL 67
88 :
89 : // Extra bit probabilities.
90 : DECLARE_ALIGNED(16, extern const uint8_t, av1_cat1_prob[1]);
91 : DECLARE_ALIGNED(16, extern const uint8_t, av1_cat2_prob[2]);
92 : DECLARE_ALIGNED(16, extern const uint8_t, av1_cat3_prob[3]);
93 : DECLARE_ALIGNED(16, extern const uint8_t, av1_cat4_prob[4]);
94 : DECLARE_ALIGNED(16, extern const uint8_t, av1_cat5_prob[5]);
95 : DECLARE_ALIGNED(16, extern const uint8_t, av1_cat6_prob[18]);
96 : #if CONFIG_NEW_MULTISYMBOL
97 : extern const aom_cdf_prob *av1_cat1_cdf[];
98 : extern const aom_cdf_prob *av1_cat2_cdf[];
99 : extern const aom_cdf_prob *av1_cat3_cdf[];
100 : extern const aom_cdf_prob *av1_cat4_cdf[];
101 : extern const aom_cdf_prob *av1_cat5_cdf[];
102 : extern const aom_cdf_prob *av1_cat6_cdf[];
103 : #endif
104 :
105 : #define EOB_MODEL_TOKEN 3
106 :
107 : typedef struct {
108 : #if CONFIG_NEW_MULTISYMBOL
109 : const aom_cdf_prob **cdf;
110 : #else
111 : const aom_prob *prob;
112 : #endif
113 : int len;
114 : int base_val;
115 : const int16_t *cost;
116 : } av1_extra_bit;
117 :
118 : // indexed by token value
119 : extern const av1_extra_bit av1_extra_bits[ENTROPY_TOKENS];
120 :
121 0 : static INLINE int av1_get_cat6_extrabits_size(TX_SIZE tx_size,
122 : aom_bit_depth_t bit_depth) {
123 0 : tx_size = txsize_sqr_up_map[tx_size];
124 : #if CONFIG_TX64X64
125 : // TODO(debargha): Does TX_64X64 require an additional extrabit?
126 : if (tx_size > TX_32X32) tx_size = TX_32X32;
127 : #endif
128 : #if CONFIG_CHROMA_2X2
129 : int tx_offset = (tx_size < TX_4X4) ? 0 : (int)(tx_size - TX_4X4);
130 : #else
131 0 : int tx_offset = (int)(tx_size - TX_4X4);
132 : #endif
133 0 : int bits = (int)bit_depth + 3 + tx_offset;
134 : #if CONFIG_NEW_MULTISYMBOL
135 : // Round up
136 : bits = AOMMIN((int)sizeof(av1_cat6_prob), ((bits + 3) & ~3));
137 : #endif
138 0 : assert(bits <= (int)sizeof(av1_cat6_prob));
139 0 : return bits;
140 : }
141 :
142 : #define DCT_MAX_VALUE 16384
143 : #if CONFIG_HIGHBITDEPTH
144 : #define DCT_MAX_VALUE_HIGH10 65536
145 : #define DCT_MAX_VALUE_HIGH12 262144
146 : #endif // CONFIG_HIGHBITDEPTH
147 :
148 : /* Coefficients are predicted via a 3-dimensional probability table. */
149 :
150 : #define REF_TYPES 2 // intra=0, inter=1
151 :
152 : /* Middle dimension reflects the coefficient position within the transform. */
153 : #define COEF_BANDS 6
154 :
155 : /* Inside dimension is measure of nearby complexity, that reflects the energy
156 : of nearby coefficients are nonzero. For the first coefficient (DC, unless
157 : block type is 0), we look at the (already encoded) blocks above and to the
158 : left of the current block. The context index is then the number (0,1,or 2)
159 : of these blocks having nonzero coefficients.
160 : After decoding a coefficient, the measure is determined by the size of the
161 : most recently decoded coefficient.
162 : Note that the intuitive meaning of this measure changes as coefficients
163 : are decoded, e.g., prior to the first token, a zero means that my neighbors
164 : are empty while, after the first token, because of the use of end-of-block,
165 : a zero means we just decoded a zero and hence guarantees that a non-zero
166 : coefficient will appear later in this block. However, this shift
167 : in meaning is perfectly OK because our context depends also on the
168 : coefficient band (and since zigzag positions 0, 1, and 2 are in
169 : distinct bands). */
170 :
171 : #define COEFF_CONTEXTS 6
172 : #define BLOCKZ_CONTEXTS 3
173 : #define COEFF_CONTEXTS0 3 // for band 0
174 : #define BAND_COEFF_CONTEXTS(band) \
175 : ((band) == 0 ? COEFF_CONTEXTS0 : COEFF_CONTEXTS)
176 :
177 : // #define ENTROPY_STATS
178 :
179 : typedef unsigned int av1_coeff_count[REF_TYPES][COEF_BANDS][COEFF_CONTEXTS]
180 : [ENTROPY_TOKENS];
181 : typedef unsigned int av1_coeff_stats[REF_TYPES][COEF_BANDS][COEFF_CONTEXTS]
182 : [ENTROPY_NODES][2];
183 :
184 : #define SUBEXP_PARAM 4 /* Subexponential code parameter */
185 : #define MODULUS_PARAM 13 /* Modulus parameter */
186 :
187 : struct AV1Common;
188 : struct frame_contexts;
189 : void av1_default_coef_probs(struct AV1Common *cm);
190 : void av1_adapt_coef_probs(struct AV1Common *cm);
191 : #if CONFIG_EC_ADAPT
192 : void av1_adapt_coef_cdfs(struct AV1Common *cm, struct frame_contexts *pre_fc);
193 : #endif
194 :
195 : // This is the index in the scan order beyond which all coefficients for
196 : // 8x8 transform and above are in the top band.
197 : // This macro is currently unused but may be used by certain implementations
198 : #define MAXBAND_INDEX 21
199 :
200 : DECLARE_ALIGNED(16, extern const uint8_t,
201 : av1_coefband_trans_8x8plus[MAX_TX_SQUARE]);
202 : DECLARE_ALIGNED(16, extern const uint8_t, av1_coefband_trans_4x8_8x4[32]);
203 : DECLARE_ALIGNED(16, extern const uint8_t, av1_coefband_trans_4x4[16]);
204 :
205 : DECLARE_ALIGNED(16, extern const uint16_t, band_count_table[TX_SIZES_ALL][8]);
206 : DECLARE_ALIGNED(16, extern const uint16_t,
207 : band_cum_count_table[TX_SIZES_ALL][8]);
208 :
209 0 : static INLINE const uint8_t *get_band_translate(TX_SIZE tx_size) {
210 0 : switch (tx_size) {
211 0 : case TX_4X4: return av1_coefband_trans_4x4;
212 : case TX_8X4:
213 0 : case TX_4X8: return av1_coefband_trans_4x8_8x4;
214 0 : default: return av1_coefband_trans_8x8plus;
215 : }
216 : }
217 :
218 : // 128 lists of probabilities are stored for the following ONE node probs:
219 : // 1, 3, 5, 7, ..., 253, 255
220 : // In between probabilities are interpolated linearly
221 :
222 : #define COEFF_PROB_MODELS 255
223 :
224 : #define UNCONSTRAINED_NODES 3
225 :
226 : #define PIVOT_NODE 2 // which node is pivot
227 :
228 : #define MODEL_NODES (ENTROPY_NODES - UNCONSTRAINED_NODES)
229 : #define TAIL_NODES (MODEL_NODES + 1)
230 : extern const aom_tree_index av1_coef_con_tree[TREE_SIZE(ENTROPY_TOKENS)];
231 : extern const aom_prob av1_pareto8_full[COEFF_PROB_MODELS][MODEL_NODES];
232 :
233 : typedef aom_prob av1_coeff_probs_model[REF_TYPES][COEF_BANDS][COEFF_CONTEXTS]
234 : [UNCONSTRAINED_NODES];
235 :
236 : typedef unsigned int av1_coeff_count_model[REF_TYPES][COEF_BANDS]
237 : [COEFF_CONTEXTS]
238 : [UNCONSTRAINED_NODES + 1];
239 :
240 : void av1_model_to_full_probs(const aom_prob *model, aom_prob *full);
241 :
242 : typedef aom_cdf_prob coeff_cdf_model[REF_TYPES][COEF_BANDS][COEFF_CONTEXTS]
243 : [CDF_SIZE(ENTROPY_TOKENS)];
244 : typedef aom_prob av1_blockz_probs_model[REF_TYPES][BLOCKZ_CONTEXTS];
245 : typedef unsigned int av1_blockz_count_model[REF_TYPES][BLOCKZ_CONTEXTS][2];
246 : extern const aom_cdf_prob av1_pareto8_token_probs[COEFF_PROB_MODELS]
247 : [ENTROPY_TOKENS - 2];
248 : extern const aom_cdf_prob av1_pareto8_tail_probs[COEFF_PROB_MODELS]
249 : [ENTROPY_TOKENS - 3];
250 : struct frame_contexts;
251 :
252 : void av1_coef_head_cdfs(struct frame_contexts *fc);
253 : void av1_coef_pareto_cdfs(struct frame_contexts *fc);
254 :
255 : typedef char ENTROPY_CONTEXT;
256 :
257 0 : static INLINE int combine_entropy_contexts(ENTROPY_CONTEXT a,
258 : ENTROPY_CONTEXT b) {
259 0 : return (a != 0) + (b != 0);
260 : }
261 :
262 0 : static INLINE int get_entropy_context(TX_SIZE tx_size, const ENTROPY_CONTEXT *a,
263 : const ENTROPY_CONTEXT *l) {
264 0 : ENTROPY_CONTEXT above_ec = 0, left_ec = 0;
265 :
266 : #if CONFIG_CHROMA_2X2
267 : switch (tx_size) {
268 : case TX_2X2:
269 : above_ec = a[0] != 0;
270 : left_ec = l[0] != 0;
271 : break;
272 : case TX_4X4:
273 : above_ec = !!*(const uint16_t *)a;
274 : left_ec = !!*(const uint16_t *)l;
275 : break;
276 : case TX_4X8:
277 : above_ec = !!*(const uint16_t *)a;
278 : left_ec = !!*(const uint32_t *)l;
279 : break;
280 : case TX_8X4:
281 : above_ec = !!*(const uint32_t *)a;
282 : left_ec = !!*(const uint16_t *)l;
283 : break;
284 : case TX_8X8:
285 : above_ec = !!*(const uint32_t *)a;
286 : left_ec = !!*(const uint32_t *)l;
287 : break;
288 : case TX_8X16:
289 : above_ec = !!*(const uint32_t *)a;
290 : left_ec = !!*(const uint64_t *)l;
291 : break;
292 : case TX_16X8:
293 : above_ec = !!*(const uint64_t *)a;
294 : left_ec = !!*(const uint32_t *)l;
295 : break;
296 : case TX_16X16:
297 : above_ec = !!*(const uint64_t *)a;
298 : left_ec = !!*(const uint64_t *)l;
299 : break;
300 : case TX_16X32:
301 : above_ec = !!*(const uint64_t *)a;
302 : left_ec = !!(*(const uint64_t *)l | *(const uint64_t *)(l + 8));
303 : break;
304 : case TX_32X16:
305 : above_ec = !!(*(const uint64_t *)a | *(const uint64_t *)(a + 8));
306 : left_ec = !!*(const uint64_t *)l;
307 : break;
308 : case TX_32X32:
309 : above_ec = !!(*(const uint64_t *)a | *(const uint64_t *)(a + 8));
310 : left_ec = !!(*(const uint64_t *)l | *(const uint64_t *)(l + 8));
311 : break;
312 : #if CONFIG_TX64X64
313 : case TX_64X64:
314 : above_ec = !!(*(const uint64_t *)a | *(const uint64_t *)(a + 8) |
315 : *(const uint64_t *)(a + 16) | *(const uint64_t *)(a + 24));
316 : left_ec = !!(*(const uint64_t *)l | *(const uint64_t *)(l + 8) |
317 : *(const uint64_t *)(l + 16) | *(const uint64_t *)(l + 24));
318 : break;
319 : #endif // CONFIG_TX64X64
320 : #if CONFIG_EXT_TX && CONFIG_RECT_TX && CONFIG_RECT_TX_EXT
321 : case TX_4X16:
322 : above_ec = !!*(const uint16_t *)a;
323 : left_ec = !!*(const uint64_t *)l;
324 : break;
325 : case TX_16X4:
326 : above_ec = !!*(const uint64_t *)a;
327 : left_ec = !!*(const uint16_t *)l;
328 : break;
329 : case TX_8X32:
330 : above_ec = !!*(const uint32_t *)a;
331 : left_ec = !!(*(const uint64_t *)l | *(const uint64_t *)(l + 8));
332 : break;
333 : case TX_32X8:
334 : above_ec = !!(*(const uint64_t *)a | *(const uint64_t *)(a + 8));
335 : left_ec = !!*(const uint32_t *)l;
336 : break;
337 : #endif // CONFIG_EXT_TX && CONFIG_RECT_TX && CONFIG_RECT_TX_EXT
338 : default: assert(0 && "Invalid transform size."); break;
339 : }
340 : return combine_entropy_contexts(above_ec, left_ec);
341 : #endif // CONFIG_CHROMA_2X2
342 :
343 0 : switch (tx_size) {
344 : case TX_4X4:
345 0 : above_ec = a[0] != 0;
346 0 : left_ec = l[0] != 0;
347 0 : break;
348 : case TX_4X8:
349 0 : above_ec = a[0] != 0;
350 0 : left_ec = !!*(const uint16_t *)l;
351 0 : break;
352 : case TX_8X4:
353 0 : above_ec = !!*(const uint16_t *)a;
354 0 : left_ec = l[0] != 0;
355 0 : break;
356 : case TX_8X16:
357 0 : above_ec = !!*(const uint16_t *)a;
358 0 : left_ec = !!*(const uint32_t *)l;
359 0 : break;
360 : case TX_16X8:
361 0 : above_ec = !!*(const uint32_t *)a;
362 0 : left_ec = !!*(const uint16_t *)l;
363 0 : break;
364 : case TX_16X32:
365 0 : above_ec = !!*(const uint32_t *)a;
366 0 : left_ec = !!*(const uint64_t *)l;
367 0 : break;
368 : case TX_32X16:
369 0 : above_ec = !!*(const uint64_t *)a;
370 0 : left_ec = !!*(const uint32_t *)l;
371 0 : break;
372 : case TX_8X8:
373 0 : above_ec = !!*(const uint16_t *)a;
374 0 : left_ec = !!*(const uint16_t *)l;
375 0 : break;
376 : case TX_16X16:
377 0 : above_ec = !!*(const uint32_t *)a;
378 0 : left_ec = !!*(const uint32_t *)l;
379 0 : break;
380 : case TX_32X32:
381 0 : above_ec = !!*(const uint64_t *)a;
382 0 : left_ec = !!*(const uint64_t *)l;
383 0 : break;
384 : #if CONFIG_TX64X64
385 : case TX_64X64:
386 : above_ec = !!(*(const uint64_t *)a | *(const uint64_t *)(a + 8));
387 : left_ec = !!(*(const uint64_t *)l | *(const uint64_t *)(l + 8));
388 : break;
389 : #endif // CONFIG_TX64X64
390 : #if CONFIG_EXT_TX && CONFIG_RECT_TX && CONFIG_RECT_TX_EXT
391 : case TX_4X16:
392 : above_ec = a[0] != 0;
393 : left_ec = !!*(const uint32_t *)l;
394 : break;
395 : case TX_16X4:
396 : above_ec = !!*(const uint32_t *)a;
397 : left_ec = l[0] != 0;
398 : break;
399 : case TX_8X32:
400 : above_ec = !!*(const uint16_t *)a;
401 : left_ec = !!*(const uint64_t *)l;
402 : break;
403 : case TX_32X8:
404 : above_ec = !!*(const uint64_t *)a;
405 : left_ec = !!*(const uint16_t *)l;
406 : break;
407 : #endif // CONFIG_EXT_TX && CONFIG_RECT_TX && CONFIG_RECT_TX_EXT
408 0 : default: assert(0 && "Invalid transform size."); break;
409 : }
410 0 : return combine_entropy_contexts(above_ec, left_ec);
411 : }
412 :
413 : #define COEF_COUNT_SAT 24
414 : #define COEF_MAX_UPDATE_FACTOR 112
415 : #define COEF_COUNT_SAT_AFTER_KEY 24
416 : #define COEF_MAX_UPDATE_FACTOR_AFTER_KEY 128
417 :
418 : #if CONFIG_ADAPT_SCAN
419 : #define ADAPT_SCAN_UPDATE_RATE_16 (1 << 13)
420 : #endif
421 :
422 0 : static INLINE aom_prob av1_merge_probs(aom_prob pre_prob,
423 : const unsigned int ct[2],
424 : unsigned int count_sat,
425 : unsigned int max_update_factor) {
426 0 : return merge_probs(pre_prob, ct, count_sat, max_update_factor);
427 : }
428 :
429 0 : static INLINE aom_prob av1_mode_mv_merge_probs(aom_prob pre_prob,
430 : const unsigned int ct[2]) {
431 0 : return mode_mv_merge_probs(pre_prob, ct);
432 : }
433 :
434 : #if CONFIG_EC_ADAPT
435 : void av1_average_tile_coef_cdfs(struct frame_contexts *fc,
436 : struct frame_contexts *ec_ctxs[],
437 : aom_cdf_prob *cdf_ptrs[], int num_tiles);
438 : void av1_average_tile_mv_cdfs(struct frame_contexts *fc,
439 : struct frame_contexts *ec_ctxs[],
440 : aom_cdf_prob *cdf_ptrs[], int num_tiles);
441 : void av1_average_tile_intra_cdfs(struct frame_contexts *fc,
442 : struct frame_contexts *ec_ctxs[],
443 : aom_cdf_prob *cdf_ptrs[], int num_tiles);
444 : void av1_average_tile_inter_cdfs(struct AV1Common *cm,
445 : struct frame_contexts *fc,
446 : struct frame_contexts *ec_ctxs[],
447 : aom_cdf_prob *cdf_ptrs[], int num_tiles);
448 : #if CONFIG_PVQ
449 : void av1_default_pvq_probs(struct AV1Common *cm);
450 : void av1_average_tile_pvq_cdfs(struct frame_contexts *fc,
451 : struct frame_contexts *ec_ctxs[], int num_tiles);
452 : #endif // CONFIG_PVQ
453 : #endif // CONFIG_EC_ADAPT
454 : #ifdef __cplusplus
455 : } // extern "C"
456 : #endif
457 :
458 : #endif // AV1_COMMON_ENTROPY_H_
|