entropymode.h 10.2 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1
/*
Yaowu Xu's avatar
Yaowu Xu committed
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Jingning Han's avatar
Jingning Han committed
3
 *
Yaowu Xu's avatar
Yaowu Xu committed
4 5 6 7 8 9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Jingning Han's avatar
Jingning Han committed
10 11
 */

Yaowu Xu's avatar
Yaowu Xu committed
12 13
#ifndef AV1_COMMON_ENTROPYMODE_H_
#define AV1_COMMON_ENTROPYMODE_H_
Jingning Han's avatar
Jingning Han committed
14

15 16 17 18
#include "av1/common/entropy.h"
#include "av1/common/entropymv.h"
#include "av1/common/filter.h"
#include "av1/common/seg_common.h"
Yaowu Xu's avatar
Yaowu Xu committed
19
#include "aom_dsp/aom_filter.h"
Jingning Han's avatar
Jingning Han committed
20 21 22 23 24 25 26 27 28

#ifdef __cplusplus
extern "C" {
#endif

#define BLOCK_SIZE_GROUPS 4

#define TX_SIZE_CONTEXTS 2

clang-format's avatar
clang-format committed
29
#define INTER_OFFSET(mode) ((mode)-NEARESTMV)
30
#if CONFIG_EXT_INTER
clang-format's avatar
clang-format committed
31
#define INTER_COMPOUND_OFFSET(mode) ((mode)-NEAREST_NEARESTMV)
32
#endif  // CONFIG_EXT_INTER
Jingning Han's avatar
Jingning Han committed
33

hui su's avatar
hui su committed
34 35
#define PALETTE_COLOR_CONTEXTS 16
#define PALETTE_MAX_SIZE 8
36
#define PALETTE_BLOCK_SIZES (BLOCK_LARGEST - BLOCK_8X8 + 1)
hui su's avatar
hui su committed
37
#define PALETTE_Y_MODE_CONTEXTS 3
38
#define PALETTE_MAX_BLOCK_SIZE (64 * 64)
hui su's avatar
hui su committed
39

Yaowu Xu's avatar
Yaowu Xu committed
40
struct AV1Common;
Jingning Han's avatar
Jingning Han committed
41

42 43 44 45 46 47
struct seg_counts {
  unsigned int tree_total[MAX_SEGMENTS];
  unsigned int tree_mispred[MAX_SEGMENTS];
  unsigned int pred[PREDICTION_PROBS][2];
};

Jingning Han's avatar
Jingning Han committed
48
typedef struct frame_contexts {
Yaowu Xu's avatar
Yaowu Xu committed
49 50
  aom_prob y_mode_prob[BLOCK_SIZE_GROUPS][INTRA_MODES - 1];
  aom_prob uv_mode_prob[INTRA_MODES][INTRA_MODES - 1];
51
#if CONFIG_EXT_PARTITION_TYPES
Yaowu Xu's avatar
Yaowu Xu committed
52
  aom_prob partition_prob[PARTITION_CONTEXTS][EXT_PARTITION_TYPES - 1];
53
#else
Yaowu Xu's avatar
Yaowu Xu committed
54
  aom_prob partition_prob[PARTITION_CONTEXTS][PARTITION_TYPES - 1];
55
#endif
Yaowu Xu's avatar
Yaowu Xu committed
56
  av1_coeff_probs_model coef_probs[TX_SIZES][PLANE_TYPES];
57 58 59
#if CONFIG_ANS
  coeff_cdf_model coef_cdfs[TX_SIZES][PLANE_TYPES];
#endif
Yaowu Xu's avatar
Yaowu Xu committed
60
  aom_prob
clang-format's avatar
clang-format committed
61 62
      switchable_interp_prob[SWITCHABLE_FILTER_CONTEXTS][SWITCHABLE_FILTERS -
                                                         1];
63 64

#if CONFIG_REF_MV
Yaowu Xu's avatar
Yaowu Xu committed
65 66 67 68
  aom_prob newmv_prob[NEWMV_MODE_CONTEXTS];
  aom_prob zeromv_prob[ZEROMV_MODE_CONTEXTS];
  aom_prob refmv_prob[REFMV_MODE_CONTEXTS];
  aom_prob drl_prob[DRL_MODE_CONTEXTS];
69

Yue Chen's avatar
Yue Chen committed
70
#if CONFIG_EXT_INTER
Yaowu Xu's avatar
Yaowu Xu committed
71
  aom_prob new2mv_prob;
Yue Chen's avatar
Yue Chen committed
72
#endif  // CONFIG_EXT_INTER
73
#endif  // CONFIG_REF_MV
74

Yaowu Xu's avatar
Yaowu Xu committed
75
  aom_prob inter_mode_probs[INTER_MODE_CONTEXTS][INTER_MODES - 1];
76
#if CONFIG_EXT_INTER
Yaowu Xu's avatar
Yaowu Xu committed
77
  aom_prob
clang-format's avatar
clang-format committed
78
      inter_compound_mode_probs[INTER_MODE_CONTEXTS][INTER_COMPOUND_MODES - 1];
Yaowu Xu's avatar
Yaowu Xu committed
79 80 81 82
  aom_prob interintra_prob[BLOCK_SIZE_GROUPS];
  aom_prob interintra_mode_prob[BLOCK_SIZE_GROUPS][INTERINTRA_MODES - 1];
  aom_prob wedge_interintra_prob[BLOCK_SIZES];
  aom_prob wedge_interinter_prob[BLOCK_SIZES];
83
#endif  // CONFIG_EXT_INTER
84
#if CONFIG_OBMC || CONFIG_WARPED_MOTION
Yaowu Xu's avatar
Yaowu Xu committed
85
  aom_prob motvar_prob[BLOCK_SIZES][MOTION_VARIATIONS - 1];
86
#endif  // CONFIG_OBMC || CONFIG_WARPED_MOTION
Yaowu Xu's avatar
Yaowu Xu committed
87 88 89
  aom_prob intra_inter_prob[INTRA_INTER_CONTEXTS];
  aom_prob comp_inter_prob[COMP_INTER_CONTEXTS];
  aom_prob single_ref_prob[REF_CONTEXTS][SINGLE_REFS - 1];
90
#if CONFIG_EXT_REFS
Yaowu Xu's avatar
Yaowu Xu committed
91 92
  aom_prob comp_ref_prob[REF_CONTEXTS][FWD_REFS - 1];
  aom_prob comp_bwdref_prob[REF_CONTEXTS][BWD_REFS - 1];
93
#else
Yaowu Xu's avatar
Yaowu Xu committed
94
  aom_prob comp_ref_prob[REF_CONTEXTS][COMP_REFS - 1];
95
#endif  // CONFIG_EXT_REFS
Yaowu Xu's avatar
Yaowu Xu committed
96
  aom_prob tx_size_probs[TX_SIZES - 1][TX_SIZE_CONTEXTS][TX_SIZES - 1];
97
#if CONFIG_VAR_TX
Yaowu Xu's avatar
Yaowu Xu committed
98
  aom_prob txfm_partition_prob[TXFM_PARTITION_CONTEXTS];
99
#endif
Yaowu Xu's avatar
Yaowu Xu committed
100
  aom_prob skip_probs[SKIP_CONTEXTS];
101 102 103
#if CONFIG_REF_MV
  nmv_context nmvc[NMV_CONTEXTS];
#else
Jingning Han's avatar
Jingning Han committed
104
  nmv_context nmvc;
105
#endif
hui su's avatar
hui su committed
106
  int initialized;
107
#if CONFIG_EXT_TX
Yaowu Xu's avatar
Yaowu Xu committed
108 109
  aom_prob inter_ext_tx_prob[EXT_TX_SETS_INTER][EXT_TX_SIZES][TX_TYPES - 1];
  aom_prob
clang-format's avatar
clang-format committed
110 111
      intra_ext_tx_prob[EXT_TX_SETS_INTRA][EXT_TX_SIZES][INTRA_MODES][TX_TYPES -
                                                                      1];
112
#else
Yaowu Xu's avatar
Yaowu Xu committed
113 114
  aom_prob intra_ext_tx_prob[EXT_TX_SIZES][TX_TYPES][TX_TYPES - 1];
  aom_prob inter_ext_tx_prob[EXT_TX_SIZES][TX_TYPES - 1];
115
#endif  // CONFIG_EXT_TX
116
#if CONFIG_SUPERTX
Yaowu Xu's avatar
Yaowu Xu committed
117
  aom_prob supertx_prob[PARTITION_SUPERTX_CONTEXTS][TX_SIZES];
118
#endif  // CONFIG_SUPERTX
119
  struct segmentation_probs seg;
hui su's avatar
hui su committed
120
#if CONFIG_EXT_INTRA
Yaowu Xu's avatar
Yaowu Xu committed
121 122
  aom_prob ext_intra_probs[PLANE_TYPES];
  aom_prob intra_filter_probs[INTRA_FILTERS + 1][INTRA_FILTERS - 1];
hui su's avatar
hui su committed
123
#endif  // CONFIG_EXT_INTRA
124
#if CONFIG_GLOBAL_MOTION
Yaowu Xu's avatar
Yaowu Xu committed
125
  aom_prob global_motion_types_prob[GLOBAL_MOTION_TYPES - 1];
126
#endif  // CONFIG_GLOBAL_MOTION
Jingning Han's avatar
Jingning Han committed
127 128 129
} FRAME_CONTEXT;

typedef struct FRAME_COUNTS {
130 131
  // Note: This structure should only contain 'unsigned int' fields, or
  // aggregates built solely from 'unsigned int' fields/elements
132
  unsigned int kf_y_mode[INTRA_MODES][INTRA_MODES][INTRA_MODES];
Jingning Han's avatar
Jingning Han committed
133 134
  unsigned int y_mode[BLOCK_SIZE_GROUPS][INTRA_MODES];
  unsigned int uv_mode[INTRA_MODES][INTRA_MODES];
135 136 137
#if CONFIG_EXT_PARTITION_TYPES
  unsigned int partition[PARTITION_CONTEXTS][EXT_PARTITION_TYPES];
#else
Jingning Han's avatar
Jingning Han committed
138
  unsigned int partition[PARTITION_CONTEXTS][PARTITION_TYPES];
139
#endif
Yaowu Xu's avatar
Yaowu Xu committed
140
  av1_coeff_count_model coef[TX_SIZES][PLANE_TYPES];
clang-format's avatar
clang-format committed
141 142 143 144
  unsigned int
      eob_branch[TX_SIZES][PLANE_TYPES][REF_TYPES][COEF_BANDS][COEFF_CONTEXTS];
  unsigned int
      switchable_interp[SWITCHABLE_FILTER_CONTEXTS][SWITCHABLE_FILTERS];
145 146 147 148
#if CONFIG_REF_MV
  unsigned int newmv_mode[NEWMV_MODE_CONTEXTS][2];
  unsigned int zeromv_mode[ZEROMV_MODE_CONTEXTS][2];
  unsigned int refmv_mode[REFMV_MODE_CONTEXTS][2];
149
  unsigned int drl_mode[DRL_MODE_CONTEXTS][2];
Yue Chen's avatar
Yue Chen committed
150 151 152
#if CONFIG_EXT_INTER
  unsigned int new2mv_mode[2];
#endif  // CONFIG_EXT_INTER
153 154
#endif

Jingning Han's avatar
Jingning Han committed
155
  unsigned int inter_mode[INTER_MODE_CONTEXTS][INTER_MODES];
156 157
#if CONFIG_EXT_INTER
  unsigned int inter_compound_mode[INTER_MODE_CONTEXTS][INTER_COMPOUND_MODES];
158
  unsigned int interintra[BLOCK_SIZE_GROUPS][2];
159
  unsigned int interintra_mode[BLOCK_SIZE_GROUPS][INTERINTRA_MODES];
160 161
  unsigned int wedge_interintra[BLOCK_SIZES][2];
  unsigned int wedge_interinter[BLOCK_SIZES][2];
162
#endif  // CONFIG_EXT_INTER
163 164 165
#if CONFIG_OBMC || CONFIG_WARPED_MOTION
  unsigned int motvar[BLOCK_SIZES][MOTION_VARIATIONS];
#endif  // CONFIG_OBMC || CONFIG_WARPED_MOTION
Jingning Han's avatar
Jingning Han committed
166 167
  unsigned int intra_inter[INTRA_INTER_CONTEXTS][2];
  unsigned int comp_inter[COMP_INTER_CONTEXTS][2];
clang-format's avatar
clang-format committed
168
  unsigned int single_ref[REF_CONTEXTS][SINGLE_REFS - 1][2];
169
#if CONFIG_EXT_REFS
clang-format's avatar
clang-format committed
170 171
  unsigned int comp_ref[REF_CONTEXTS][FWD_REFS - 1][2];
  unsigned int comp_bwdref[REF_CONTEXTS][BWD_REFS - 1][2];
172
#else
clang-format's avatar
clang-format committed
173
  unsigned int comp_ref[REF_CONTEXTS][COMP_REFS - 1][2];
174
#endif  // CONFIG_EXT_REFS
175 176 177
  // TODO(any): tx_size_totals is only used by the encoder to decide whether
  // to use forward updates for the coeff probs, and as such it does not really
  // belong into this structure.
178 179
  unsigned int tx_size_totals[TX_SIZES];
  unsigned int tx_size[TX_SIZES - 1][TX_SIZE_CONTEXTS][TX_SIZES];
180 181 182
#if CONFIG_VAR_TX
  unsigned int txfm_partition[TXFM_PARTITION_CONTEXTS][2];
#endif
Jingning Han's avatar
Jingning Han committed
183
  unsigned int skip[SKIP_CONTEXTS][2];
184 185 186
#if CONFIG_REF_MV
  nmv_context_counts mv[NMV_CONTEXTS];
#else
Jingning Han's avatar
Jingning Han committed
187
  nmv_context_counts mv;
188
#endif
189
#if CONFIG_EXT_TX
Debargha Mukherjee's avatar
Debargha Mukherjee committed
190 191 192
#if CONFIG_RECT_TX
  unsigned int tx_size_implied[TX_SIZES][TX_SIZES];
#endif  // CONFIG_RECT_TX
193
  unsigned int inter_ext_tx[EXT_TX_SETS_INTER][EXT_TX_SIZES][TX_TYPES];
clang-format's avatar
clang-format committed
194 195
  unsigned int
      intra_ext_tx[EXT_TX_SETS_INTRA][EXT_TX_SIZES][INTRA_MODES][TX_TYPES];
196
#else
197 198
  unsigned int intra_ext_tx[EXT_TX_SIZES][TX_TYPES][TX_TYPES];
  unsigned int inter_ext_tx[EXT_TX_SIZES][TX_TYPES];
199
#endif  // CONFIG_EXT_TX
200 201 202 203
#if CONFIG_SUPERTX
  unsigned int supertx[PARTITION_SUPERTX_CONTEXTS][TX_SIZES][2];
  unsigned int supertx_size[TX_SIZES];
#endif  // CONFIG_SUPERTX
204
  struct seg_counts seg;
hui su's avatar
hui su committed
205 206
#if CONFIG_EXT_INTRA
  unsigned int ext_intra[PLANE_TYPES][2];
207
  unsigned int intra_filter[INTRA_FILTERS + 1][INTRA_FILTERS];
hui su's avatar
hui su committed
208
#endif  // CONFIG_EXT_INTRA
Jingning Han's avatar
Jingning Han committed
209 210
} FRAME_COUNTS;

Yaowu Xu's avatar
Yaowu Xu committed
211 212 213 214 215 216 217 218 219 220
extern const aom_prob
    av1_kf_y_mode_prob[INTRA_MODES][INTRA_MODES][INTRA_MODES - 1];
extern const aom_prob av1_default_palette_y_mode_prob[PALETTE_BLOCK_SIZES]
                                                     [PALETTE_Y_MODE_CONTEXTS];
extern const aom_prob av1_default_palette_uv_mode_prob[2];
extern const aom_prob
    av1_default_palette_y_size_prob[PALETTE_BLOCK_SIZES][PALETTE_SIZES - 1];
extern const aom_prob
    av1_default_palette_uv_size_prob[PALETTE_BLOCK_SIZES][PALETTE_SIZES - 1];
extern const aom_prob av1_default_palette_y_color_prob
clang-format's avatar
clang-format committed
221
    [PALETTE_MAX_SIZE - 1][PALETTE_COLOR_CONTEXTS][PALETTE_COLORS - 1];
Yaowu Xu's avatar
Yaowu Xu committed
222
extern const aom_prob av1_default_palette_uv_color_prob
clang-format's avatar
clang-format committed
223
    [PALETTE_MAX_SIZE - 1][PALETTE_COLOR_CONTEXTS][PALETTE_COLORS - 1];
hui su's avatar
hui su committed
224

Yaowu Xu's avatar
Yaowu Xu committed
225 226
extern const aom_tree_index av1_intra_mode_tree[TREE_SIZE(INTRA_MODES)];
extern const aom_tree_index av1_inter_mode_tree[TREE_SIZE(INTER_MODES)];
227
#if CONFIG_EXT_INTER
Yaowu Xu's avatar
Yaowu Xu committed
228 229 230 231
extern const aom_tree_index
    av1_interintra_mode_tree[TREE_SIZE(INTERINTRA_MODES)];
extern const aom_tree_index
    av1_inter_compound_mode_tree[TREE_SIZE(INTER_COMPOUND_MODES)];
232
#endif  // CONFIG_EXT_INTER
Yaowu Xu's avatar
Yaowu Xu committed
233
extern const aom_tree_index av1_partition_tree[TREE_SIZE(PARTITION_TYPES)];
234
#if CONFIG_EXT_PARTITION_TYPES
Yaowu Xu's avatar
Yaowu Xu committed
235 236
extern const aom_tree_index
    av1_ext_partition_tree[TREE_SIZE(EXT_PARTITION_TYPES)];
237
#endif
Yaowu Xu's avatar
Yaowu Xu committed
238 239 240 241 242 243
extern const aom_tree_index
    av1_switchable_interp_tree[TREE_SIZE(SWITCHABLE_FILTERS)];
extern const aom_tree_index av1_palette_size_tree[TREE_SIZE(PALETTE_SIZES)];
extern const aom_tree_index
    av1_palette_color_tree[PALETTE_MAX_SIZE - 1][TREE_SIZE(PALETTE_COLORS)];
extern const aom_tree_index av1_tx_size_tree[TX_SIZES - 1][TREE_SIZE(TX_SIZES)];
244
#if CONFIG_EXT_INTRA
Yaowu Xu's avatar
Yaowu Xu committed
245
extern const aom_tree_index av1_intra_filter_tree[TREE_SIZE(INTRA_FILTERS)];
246 247
#endif  // CONFIG_EXT_INTRA
#if CONFIG_EXT_TX
Yaowu Xu's avatar
Yaowu Xu committed
248 249 250 251
extern const aom_tree_index
    av1_ext_tx_inter_tree[EXT_TX_SETS_INTER][TREE_SIZE(TX_TYPES)];
extern const aom_tree_index
    av1_ext_tx_intra_tree[EXT_TX_SETS_INTRA][TREE_SIZE(TX_TYPES)];
252
#else
Yaowu Xu's avatar
Yaowu Xu committed
253
extern const aom_tree_index av1_ext_tx_tree[TREE_SIZE(TX_TYPES)];
254
#endif  // CONFIG_EXT_TX
255
#if CONFIG_OBMC || CONFIG_WARPED_MOTION
Yaowu Xu's avatar
Yaowu Xu committed
256
extern const aom_tree_index av1_motvar_tree[TREE_SIZE(MOTION_VARIATIONS)];
257
#endif  // CONFIG_OBMC || CONFIG_WARPED_MOTION
Jingning Han's avatar
Jingning Han committed
258

Yaowu Xu's avatar
Yaowu Xu committed
259
void av1_setup_past_independence(struct AV1Common *cm);
Jingning Han's avatar
Jingning Han committed
260

Yaowu Xu's avatar
Yaowu Xu committed
261 262
void av1_adapt_intra_frame_probs(struct AV1Common *cm);
void av1_adapt_inter_frame_probs(struct AV1Common *cm);
Jingning Han's avatar
Jingning Han committed
263

Yaowu Xu's avatar
Yaowu Xu committed
264
static INLINE int av1_ceil_log2(int n) {
hui su's avatar
hui su committed
265 266 267 268 269 270 271 272
  int i = 1, p = 2;
  while (p < n) {
    i++;
    p = p << 1;
  }
  return i;
}

Yaowu Xu's avatar
Yaowu Xu committed
273 274
int av1_get_palette_color_context(const uint8_t *color_map, int cols, int r,
                                  int c, int n, int *color_order);
hui su's avatar
hui su committed
275

Jingning Han's avatar
Jingning Han committed
276 277 278 279
#ifdef __cplusplus
}  // extern "C"
#endif

Yaowu Xu's avatar
Yaowu Xu committed
280
#endif  // AV1_COMMON_ENTROPYMODE_H_