entropymode.h 10.1 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1 2 3 4 5 6 7 8 9 10
/*
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

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

14 15 16 17
#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
18
#include "aom_dsp/aom_filter.h"
Jingning Han's avatar
Jingning Han committed
19 20 21 22 23 24 25 26 27

#ifdef __cplusplus
extern "C" {
#endif

#define BLOCK_SIZE_GROUPS 4

#define TX_SIZE_CONTEXTS 2

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

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

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

41 42 43 44 45 46
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
47
typedef struct frame_contexts {
Yaowu Xu's avatar
Yaowu Xu committed
48 49
  aom_prob y_mode_prob[BLOCK_SIZE_GROUPS][INTRA_MODES - 1];
  aom_prob uv_mode_prob[INTRA_MODES][INTRA_MODES - 1];
50
#if CONFIG_EXT_PARTITION_TYPES
Yaowu Xu's avatar
Yaowu Xu committed
51
  aom_prob partition_prob[PARTITION_CONTEXTS][EXT_PARTITION_TYPES - 1];
52
#else
Yaowu Xu's avatar
Yaowu Xu committed
53
  aom_prob partition_prob[PARTITION_CONTEXTS][PARTITION_TYPES - 1];
54
#endif
Yaowu Xu's avatar
Yaowu Xu committed
55
  av1_coeff_probs_model coef_probs[TX_SIZES][PLANE_TYPES];
56 57 58
#if CONFIG_ANS
  coeff_cdf_model coef_cdfs[TX_SIZES][PLANE_TYPES];
#endif
Yaowu Xu's avatar
Yaowu Xu committed
59
  aom_prob
clang-format's avatar
clang-format committed
60 61
      switchable_interp_prob[SWITCHABLE_FILTER_CONTEXTS][SWITCHABLE_FILTERS -
                                                         1];
62 63

#if CONFIG_REF_MV
Yaowu Xu's avatar
Yaowu Xu committed
64 65 66 67
  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];
68

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

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

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

Jingning Han's avatar
Jingning Han committed
154
  unsigned int inter_mode[INTER_MODE_CONTEXTS][INTER_MODES];
155 156
#if CONFIG_EXT_INTER
  unsigned int inter_compound_mode[INTER_MODE_CONTEXTS][INTER_COMPOUND_MODES];
157
  unsigned int interintra[BLOCK_SIZE_GROUPS][2];
158
  unsigned int interintra_mode[BLOCK_SIZE_GROUPS][INTERINTRA_MODES];
159 160
  unsigned int wedge_interintra[BLOCK_SIZES][2];
  unsigned int wedge_interinter[BLOCK_SIZES][2];
161
#endif  // CONFIG_EXT_INTER
162 163 164
#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
165 166
  unsigned int intra_inter[INTRA_INTER_CONTEXTS][2];
  unsigned int comp_inter[COMP_INTER_CONTEXTS][2];
clang-format's avatar
clang-format committed
167
  unsigned int single_ref[REF_CONTEXTS][SINGLE_REFS - 1][2];
168
#if CONFIG_EXT_REFS
clang-format's avatar
clang-format committed
169 170
  unsigned int comp_ref[REF_CONTEXTS][FWD_REFS - 1][2];
  unsigned int comp_bwdref[REF_CONTEXTS][BWD_REFS - 1][2];
171
#else
clang-format's avatar
clang-format committed
172
  unsigned int comp_ref[REF_CONTEXTS][COMP_REFS - 1][2];
173
#endif  // CONFIG_EXT_REFS
174 175 176
  // 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.
177 178
  unsigned int tx_size_totals[TX_SIZES];
  unsigned int tx_size[TX_SIZES - 1][TX_SIZE_CONTEXTS][TX_SIZES];
179 180 181
#if CONFIG_VAR_TX
  unsigned int txfm_partition[TXFM_PARTITION_CONTEXTS][2];
#endif
Jingning Han's avatar
Jingning Han committed
182
  unsigned int skip[SKIP_CONTEXTS][2];
183 184 185
#if CONFIG_REF_MV
  nmv_context_counts mv[NMV_CONTEXTS];
#else
Jingning Han's avatar
Jingning Han committed
186
  nmv_context_counts mv;
187
#endif
188
#if CONFIG_EXT_TX
Debargha Mukherjee's avatar
Debargha Mukherjee committed
189 190 191
#if CONFIG_RECT_TX
  unsigned int tx_size_implied[TX_SIZES][TX_SIZES];
#endif  // CONFIG_RECT_TX
192
  unsigned int inter_ext_tx[EXT_TX_SETS_INTER][EXT_TX_SIZES][TX_TYPES];
clang-format's avatar
clang-format committed
193 194
  unsigned int
      intra_ext_tx[EXT_TX_SETS_INTRA][EXT_TX_SIZES][INTRA_MODES][TX_TYPES];
195
#else
196 197
  unsigned int intra_ext_tx[EXT_TX_SIZES][TX_TYPES][TX_TYPES];
  unsigned int inter_ext_tx[EXT_TX_SIZES][TX_TYPES];
198
#endif  // CONFIG_EXT_TX
199 200 201 202
#if CONFIG_SUPERTX
  unsigned int supertx[PARTITION_SUPERTX_CONTEXTS][TX_SIZES][2];
  unsigned int supertx_size[TX_SIZES];
#endif  // CONFIG_SUPERTX
203
  struct seg_counts seg;
hui su's avatar
hui su committed
204 205
#if CONFIG_EXT_INTRA
  unsigned int ext_intra[PLANE_TYPES][2];
206
  unsigned int intra_filter[INTRA_FILTERS + 1][INTRA_FILTERS];
hui su's avatar
hui su committed
207
#endif  // CONFIG_EXT_INTRA
Jingning Han's avatar
Jingning Han committed
208 209
} FRAME_COUNTS;

Yaowu Xu's avatar
Yaowu Xu committed
210 211 212 213 214 215 216 217 218 219
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
220
    [PALETTE_MAX_SIZE - 1][PALETTE_COLOR_CONTEXTS][PALETTE_COLORS - 1];
Yaowu Xu's avatar
Yaowu Xu committed
221
extern const aom_prob av1_default_palette_uv_color_prob
clang-format's avatar
clang-format committed
222
    [PALETTE_MAX_SIZE - 1][PALETTE_COLOR_CONTEXTS][PALETTE_COLORS - 1];
hui su's avatar
hui su committed
223

Yaowu Xu's avatar
Yaowu Xu committed
224 225
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)];
226
#if CONFIG_EXT_INTER
Yaowu Xu's avatar
Yaowu Xu committed
227 228 229 230
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)];
231
#endif  // CONFIG_EXT_INTER
Yaowu Xu's avatar
Yaowu Xu committed
232
extern const aom_tree_index av1_partition_tree[TREE_SIZE(PARTITION_TYPES)];
233
#if CONFIG_EXT_PARTITION_TYPES
Yaowu Xu's avatar
Yaowu Xu committed
234 235
extern const aom_tree_index
    av1_ext_partition_tree[TREE_SIZE(EXT_PARTITION_TYPES)];
236
#endif
Yaowu Xu's avatar
Yaowu Xu committed
237 238 239 240 241 242
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)];
243
#if CONFIG_EXT_INTRA
Yaowu Xu's avatar
Yaowu Xu committed
244
extern const aom_tree_index av1_intra_filter_tree[TREE_SIZE(INTRA_FILTERS)];
245 246
#endif  // CONFIG_EXT_INTRA
#if CONFIG_EXT_TX
Yaowu Xu's avatar
Yaowu Xu committed
247 248 249 250
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)];
251
#else
Yaowu Xu's avatar
Yaowu Xu committed
252
extern const aom_tree_index av1_ext_tx_tree[TREE_SIZE(TX_TYPES)];
253
#endif  // CONFIG_EXT_TX
254
#if CONFIG_OBMC || CONFIG_WARPED_MOTION
Yaowu Xu's avatar
Yaowu Xu committed
255
extern const aom_tree_index av1_motvar_tree[TREE_SIZE(MOTION_VARIATIONS)];
256
#endif  // CONFIG_OBMC || CONFIG_WARPED_MOTION
Jingning Han's avatar
Jingning Han committed
257

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

Yaowu Xu's avatar
Yaowu Xu committed
260 261
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
262

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

Yaowu Xu's avatar
Yaowu Xu committed
272 273
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
274

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

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