entropymode.h 10.3 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Jingning Han's avatar
Jingning Han committed
3
 *
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
 */

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

Yaowu Xu's avatar
Yaowu Xu committed
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"
Adrian Grange's avatar
Adrian Grange 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)
Jingning Han's avatar
Jingning Han committed
30

31
32
33
34
35
36
37
38
#if CONFIG_PALETTE
#define PALETTE_COLOR_CONTEXTS 16
#define PALETTE_MAX_SIZE 8
#define PALETTE_BLOCK_SIZES (BLOCK_64X64 - BLOCK_8X8 + 1)
#define PALETTE_Y_MODE_CONTEXTS 3
#define PALETTE_MAX_BLOCK_SIZE (64 * 64)
#endif  // CONFIG_PALETTE

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

Angie Chiang's avatar
Angie Chiang committed
41
42
43
44
45
46
typedef struct {
  const int16_t *scan;
  const int16_t *iscan;
  const int16_t *neighbors;
} SCAN_ORDER;

Jingning Han's avatar
Jingning Han committed
47
struct tx_probs {
Adrian Grange's avatar
Adrian Grange committed
48
49
50
  aom_prob p32x32[TX_SIZE_CONTEXTS][TX_SIZES - 1];
  aom_prob p16x16[TX_SIZE_CONTEXTS][TX_SIZES - 2];
  aom_prob p8x8[TX_SIZE_CONTEXTS][TX_SIZES - 3];
Jingning Han's avatar
Jingning Han committed
51
52
53
54
55
56
57
58
59
};

struct tx_counts {
  unsigned int p32x32[TX_SIZE_CONTEXTS][TX_SIZES];
  unsigned int p16x16[TX_SIZE_CONTEXTS][TX_SIZES - 1];
  unsigned int p8x8[TX_SIZE_CONTEXTS][TX_SIZES - 2];
  unsigned int tx_totals[TX_SIZES];
};

60
61
62
63
64
65
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
66
typedef struct frame_contexts {
Adrian Grange's avatar
Adrian Grange committed
67
68
69
  aom_prob y_mode_prob[BLOCK_SIZE_GROUPS][INTRA_MODES - 1];
  aom_prob uv_mode_prob[INTRA_MODES][INTRA_MODES - 1];
  aom_prob partition_prob[PARTITION_CONTEXTS][PARTITION_TYPES - 1];
70
  av1_coeff_probs_model coef_probs[TX_SIZES][PLANE_TYPES];
71
#if CONFIG_RANS || CONFIG_DAALA_EC
72
73
  coeff_cdf_model coef_cdfs[TX_SIZES][PLANE_TYPES];
#endif  // CONFIG_RANS
clang-format's avatar
clang-format committed
74
75
  aom_prob switchable_interp_prob[SWITCHABLE_FILTER_CONTEXTS]
                                 [SWITCHABLE_FILTERS - 1];
76

77
78
79
80
81
82
#if CONFIG_ADAPT_SCAN
  // TODO(angiebird): try aom_prob
  uint32_t non_zero_prob_4X4[TX_TYPES][16];
  uint32_t non_zero_prob_8X8[TX_TYPES][64];
  uint32_t non_zero_prob_16X16[TX_TYPES][256];
  uint32_t non_zero_prob_32X32[TX_TYPES][1024];
Angie Chiang's avatar
Angie Chiang committed
83

84
85
86
87
88
89
90
91
92
  DECLARE_ALIGNED(16, int16_t, scan_4X4[TX_TYPES][16]);
  DECLARE_ALIGNED(16, int16_t, scan_8X8[TX_TYPES][64]);
  DECLARE_ALIGNED(16, int16_t, scan_16X16[TX_TYPES][256]);
  DECLARE_ALIGNED(16, int16_t, scan_32X32[TX_TYPES][1024]);

  DECLARE_ALIGNED(16, int16_t, iscan_4X4[TX_TYPES][16]);
  DECLARE_ALIGNED(16, int16_t, iscan_8X8[TX_TYPES][64]);
  DECLARE_ALIGNED(16, int16_t, iscan_16X16[TX_TYPES][256]);
  DECLARE_ALIGNED(16, int16_t, iscan_32X32[TX_TYPES][1024]);
93
94
95
96
97

  int16_t nb_4X4[TX_TYPES][(16 + 1) * 2];
  int16_t nb_8X8[TX_TYPES][(64 + 1) * 2];
  int16_t nb_16X16[TX_TYPES][(256 + 1) * 2];
  int16_t nb_32X32[TX_TYPES][(1024 + 1) * 2];
Angie Chiang's avatar
Angie Chiang committed
98
99

  SCAN_ORDER sc[TX_SIZES][TX_TYPES];
100
101
#endif

102
103
104
105
#if CONFIG_REF_MV
  aom_prob newmv_prob[NEWMV_MODE_CONTEXTS];
  aom_prob zeromv_prob[ZEROMV_MODE_CONTEXTS];
  aom_prob refmv_prob[REFMV_MODE_CONTEXTS];
106
  aom_prob drl_prob[DRL_MODE_CONTEXTS];
107
108
#endif

Adrian Grange's avatar
Adrian Grange committed
109
  aom_prob inter_mode_probs[INTER_MODE_CONTEXTS][INTER_MODES - 1];
110
111
112
#if CONFIG_MOTION_VAR
  aom_prob motion_mode_prob[BLOCK_SIZES][MOTION_MODES - 1];
#endif  // CONFIG_MOTION_VAR
Adrian Grange's avatar
Adrian Grange committed
113
114
  aom_prob intra_inter_prob[INTRA_INTER_CONTEXTS];
  aom_prob comp_inter_prob[COMP_INTER_CONTEXTS];
115
116
117
118
119
  aom_prob single_ref_prob[REF_CONTEXTS][SINGLE_REFS - 1];
#if CONFIG_EXT_REFS
  aom_prob comp_fwdref_prob[REF_CONTEXTS][FWD_REFS - 1];
  aom_prob comp_bwdref_prob[REF_CONTEXTS][BWD_REFS - 1];
#else
Adrian Grange's avatar
Adrian Grange committed
120
  aom_prob comp_ref_prob[REF_CONTEXTS];
121
#endif  // CONFIG_EXT_REFS
Jingning Han's avatar
Jingning Han committed
122
  struct tx_probs tx_probs;
Adrian Grange's avatar
Adrian Grange committed
123
  aom_prob skip_probs[SKIP_CONTEXTS];
124
125
126
#if CONFIG_REF_MV
  nmv_context nmvc[NMV_CONTEXTS];
#else
Jingning Han's avatar
Jingning Han committed
127
  nmv_context nmvc;
128
#endif
129
130
131
#if CONFIG_MISC_FIXES
  struct segmentation_probs seg;
#endif
Adrian Grange's avatar
Adrian Grange committed
132
133
  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];
Jingning Han's avatar
Jingning Han committed
134
  int initialized;
135
#if CONFIG_DAALA_EC
136
  aom_cdf_prob y_mode_cdf[BLOCK_SIZE_GROUPS][INTRA_MODES];
137
  aom_cdf_prob uv_mode_cdf[INTRA_MODES][INTRA_MODES];
138
  aom_cdf_prob partition_cdf[PARTITION_CONTEXTS][PARTITION_TYPES];
clang-format's avatar
clang-format committed
139
140
  aom_cdf_prob switchable_interp_cdf[SWITCHABLE_FILTER_CONTEXTS]
                                    [SWITCHABLE_FILTERS];
141
  aom_cdf_prob inter_mode_cdf[INTER_MODE_CONTEXTS][INTER_MODES];
142
143
  aom_cdf_prob intra_ext_tx_cdf[EXT_TX_SIZES][TX_TYPES][TX_TYPES];
  aom_cdf_prob inter_ext_tx_cdf[EXT_TX_SIZES][TX_TYPES];
144
#endif
145
146
147
#if CONFIG_DELTA_Q
  aom_prob delta_q_prob[DELTA_Q_CONTEXTS];
#endif
Jingning Han's avatar
Jingning Han committed
148
149
150
} FRAME_CONTEXT;

typedef struct FRAME_COUNTS {
151
  unsigned int kf_y_mode[INTRA_MODES][INTRA_MODES][INTRA_MODES];
Jingning Han's avatar
Jingning Han committed
152
153
154
  unsigned int y_mode[BLOCK_SIZE_GROUPS][INTRA_MODES];
  unsigned int uv_mode[INTRA_MODES][INTRA_MODES];
  unsigned int partition[PARTITION_CONTEXTS][PARTITION_TYPES];
155
  av1_coeff_count_model coef[TX_SIZES][PLANE_TYPES];
clang-format's avatar
clang-format committed
156
157
158
159
  unsigned int eob_branch[TX_SIZES][PLANE_TYPES][REF_TYPES][COEF_BANDS]
                         [COEFF_CONTEXTS];
  unsigned int switchable_interp[SWITCHABLE_FILTER_CONTEXTS]
                                [SWITCHABLE_FILTERS];
160

161
162
163
164
165
166
167
168
#if CONFIG_ADAPT_SCAN
  unsigned int non_zero_count_4X4[TX_TYPES][16];
  unsigned int non_zero_count_8X8[TX_TYPES][64];
  unsigned int non_zero_count_16X16[TX_TYPES][256];
  unsigned int non_zero_count_32X32[TX_TYPES][1024];
  unsigned int txb_count[TX_SIZES][TX_TYPES];
#endif

169
170
171
172
#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];
173
  unsigned int drl_mode[DRL_MODE_CONTEXTS][2];
174
175
#endif

Jingning Han's avatar
Jingning Han committed
176
  unsigned int inter_mode[INTER_MODE_CONTEXTS][INTER_MODES];
177
178
179
#if CONFIG_MOTION_VAR
  unsigned int motion_mode[BLOCK_SIZES][MOTION_MODES];
#endif  // CONFIG_MOTION_VAR
Jingning Han's avatar
Jingning Han committed
180
181
  unsigned int intra_inter[INTRA_INTER_CONTEXTS][2];
  unsigned int comp_inter[COMP_INTER_CONTEXTS][2];
182
183
184
185
186
  unsigned int single_ref[REF_CONTEXTS][SINGLE_REFS - 1][2];
#if CONFIG_EXT_REFS
  unsigned int comp_fwdref[REF_CONTEXTS][FWD_REFS - 1][2];
  unsigned int comp_bwdref[REF_CONTEXTS][BWD_REFS - 1][2];
#else
Jingning Han's avatar
Jingning Han committed
187
  unsigned int comp_ref[REF_CONTEXTS][2];
188
#endif  // CONFIG_EXT_REFS
Jingning Han's avatar
Jingning Han committed
189
190
  struct tx_counts tx;
  unsigned int skip[SKIP_CONTEXTS][2];
191
192
193
#if CONFIG_REF_MV
  nmv_context_counts mv[NMV_CONTEXTS];
#else
Jingning Han's avatar
Jingning Han committed
194
  nmv_context_counts mv;
195
#endif
196
197
#if CONFIG_MISC_FIXES
  struct seg_counts seg;
198
199
200
#endif
#if CONFIG_DELTA_Q
  unsigned int delta_q[DELTA_Q_CONTEXTS][2];
201
#endif
202
203
  unsigned int intra_ext_tx[EXT_TX_SIZES][TX_TYPES][TX_TYPES];
  unsigned int inter_ext_tx[EXT_TX_SIZES][TX_TYPES];
Jingning Han's avatar
Jingning Han committed
204
205
} FRAME_COUNTS;

clang-format's avatar
clang-format committed
206
207
extern const aom_prob av1_kf_y_mode_prob[INTRA_MODES][INTRA_MODES]
                                        [INTRA_MODES - 1];
208
209
210
#if CONFIG_DAALA_EC
extern aom_cdf_prob av1_kf_y_mode_cdf[INTRA_MODES][INTRA_MODES][INTRA_MODES];
#endif
hui su's avatar
hui su committed
211

212
213
214
215
#if CONFIG_PALETTE
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];
clang-format's avatar
clang-format committed
216
217
218
219
220
221
222
223
224
225
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[PALETTE_MAX_SIZE - 1]
                                                      [PALETTE_COLOR_CONTEXTS]
                                                      [PALETTE_COLORS - 1];
extern const aom_prob av1_default_palette_uv_color_prob[PALETTE_MAX_SIZE - 1]
                                                       [PALETTE_COLOR_CONTEXTS]
                                                       [PALETTE_COLORS - 1];
226
227
#endif  // CONFIG_PALETTE

228
229
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)];
230
231
232
#if CONFIG_DAALA_EC
extern int av1_intra_mode_ind[INTRA_MODES];
extern int av1_intra_mode_inv[INTRA_MODES];
233
234
extern int av1_inter_mode_ind[INTER_MODES];
extern int av1_inter_mode_inv[INTER_MODES];
235
#endif
236
237
238
#if CONFIG_MOTION_VAR
extern const aom_tree_index av1_motion_mode_tree[TREE_SIZE(MOTION_MODES)];
#endif  // CONFIG_MOTION_VAR
239
extern const aom_tree_index av1_partition_tree[TREE_SIZE(PARTITION_TYPES)];
Adrian Grange's avatar
Adrian Grange committed
240
extern const aom_tree_index
241
    av1_switchable_interp_tree[TREE_SIZE(SWITCHABLE_FILTERS)];
Jingning Han's avatar
Jingning Han committed
242

243
244
#if CONFIG_PALETTE
extern const aom_tree_index av1_palette_size_tree[TREE_SIZE(PALETTE_SIZES)];
clang-format's avatar
clang-format committed
245
246
extern const aom_tree_index av1_palette_color_tree[PALETTE_MAX_SIZE - 1]
                                                  [TREE_SIZE(PALETTE_COLORS)];
247
248
#endif  // CONFIG_PALETTE

249
250
251
252
253
#if CONFIG_DAALA_EC
extern int av1_switchable_interp_ind[SWITCHABLE_FILTERS];
extern int av1_switchable_interp_inv[SWITCHABLE_FILTERS];
#endif

254
void av1_setup_past_independence(struct AV1Common *cm);
Jingning Han's avatar
Jingning Han committed
255

256
257
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
258

259
void av1_tx_counts_to_branch_counts_32x32(const unsigned int *tx_count_32x32p,
clang-format's avatar
clang-format committed
260
                                          unsigned int (*ct_32x32p)[2]);
261
void av1_tx_counts_to_branch_counts_16x16(const unsigned int *tx_count_16x16p,
clang-format's avatar
clang-format committed
262
                                          unsigned int (*ct_16x16p)[2]);
263
void av1_tx_counts_to_branch_counts_8x8(const unsigned int *tx_count_8x8p,
clang-format's avatar
clang-format committed
264
                                        unsigned int (*ct_8x8p)[2]);
Jingning Han's avatar
Jingning Han committed
265

266
extern const aom_tree_index av1_ext_tx_tree[TREE_SIZE(TX_TYPES)];
267
268
269
270
#if CONFIG_DAALA_EC
extern int av1_ext_tx_ind[TX_TYPES];
extern int av1_ext_tx_inv[TX_TYPES];
#endif
271

272
static INLINE int av1_ceil_log2(int n) {
hui su's avatar
hui su committed
273
274
275
276
277
278
279
280
  int i = 1, p = 2;
  while (p < n) {
    i++;
    p = p << 1;
  }
  return i;
}

281
282
#if CONFIG_PALETTE
int av1_get_palette_color_context(const uint8_t *color_map, int cols, int r,
283
284
                                  int c, int n, uint8_t *color_order,
                                  int *color_idx);
285
286
#endif  // CONFIG_PALETTE

Jingning Han's avatar
Jingning Han committed
287
288
289
290
#ifdef __cplusplus
}  // extern "C"
#endif

291
#endif  // AV1_COMMON_ENTROPYMODE_H_