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