AOMedia AV1 Codec
av1_common_int.h
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 AOM_AV1_COMMON_AV1_COMMON_INT_H_
13 #define AOM_AV1_COMMON_AV1_COMMON_INT_H_
14 
15 #include "config/aom_config.h"
16 #include "config/av1_rtcd.h"
17 
18 #include "aom/internal/aom_codec_internal.h"
19 #include "aom_util/aom_thread.h"
20 #include "av1/common/alloccommon.h"
21 #include "av1/common/av1_loopfilter.h"
22 #include "av1/common/entropy.h"
23 #include "av1/common/entropymode.h"
24 #include "av1/common/entropymv.h"
25 #include "av1/common/enums.h"
26 #include "av1/common/frame_buffers.h"
27 #include "av1/common/mv.h"
28 #include "av1/common/quant_common.h"
29 #include "av1/common/restoration.h"
30 #include "av1/common/tile_common.h"
31 #include "av1/common/timing.h"
32 #include "av1/common/odintrin.h"
33 #include "av1/encoder/hash_motion.h"
34 #include "aom_dsp/grain_synthesis.h"
35 #include "aom_dsp/grain_table.h"
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 #if defined(__clang__) && defined(__has_warning)
41 #if __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough")
42 #define AOM_FALLTHROUGH_INTENDED [[clang::fallthrough]] // NOLINT
43 #endif
44 #elif defined(__GNUC__) && __GNUC__ >= 7
45 #define AOM_FALLTHROUGH_INTENDED __attribute__((fallthrough)) // NOLINT
46 #endif
47 
48 #ifndef AOM_FALLTHROUGH_INTENDED
49 #define AOM_FALLTHROUGH_INTENDED \
50  do { \
51  } while (0)
52 #endif
53 
54 #define CDEF_MAX_STRENGTHS 16
55 
56 /* Constant values while waiting for the sequence header */
57 #define FRAME_ID_LENGTH 15
58 #define DELTA_FRAME_ID_LENGTH 14
59 
60 #define FRAME_CONTEXTS (FRAME_BUFFERS + 1)
61 // Extra frame context which is always kept at default values
62 #define FRAME_CONTEXT_DEFAULTS (FRAME_CONTEXTS - 1)
63 #define PRIMARY_REF_BITS 3
64 #define PRIMARY_REF_NONE 7
65 
66 #define NUM_PING_PONG_BUFFERS 2
67 
68 #define MAX_NUM_TEMPORAL_LAYERS 8
69 #define MAX_NUM_SPATIAL_LAYERS 4
70 /* clang-format off */
71 // clang-format seems to think this is a pointer dereference and not a
72 // multiplication.
73 #define MAX_NUM_OPERATING_POINTS \
74  (MAX_NUM_TEMPORAL_LAYERS * MAX_NUM_SPATIAL_LAYERS)
75 /* clang-format on */
76 
77 // TODO(jingning): Turning this on to set up transform coefficient
78 // processing timer.
79 #define TXCOEFF_TIMER 0
80 #define TXCOEFF_COST_TIMER 0
81 
84 enum {
85  SINGLE_REFERENCE = 0,
86  COMPOUND_REFERENCE = 1,
87  REFERENCE_MODE_SELECT = 2,
88  REFERENCE_MODES = 3,
89 } UENUM1BYTE(REFERENCE_MODE);
90 
91 enum {
95  REFRESH_FRAME_CONTEXT_DISABLED,
100  REFRESH_FRAME_CONTEXT_BACKWARD,
101 } UENUM1BYTE(REFRESH_FRAME_CONTEXT_MODE);
102 
103 #define MFMV_STACK_SIZE 3
104 typedef struct {
105  int_mv mfmv0;
106  uint8_t ref_frame_offset;
107 } TPL_MV_REF;
108 
109 typedef struct {
110  int_mv mv;
111  MV_REFERENCE_FRAME ref_frame;
112 } MV_REF;
113 
114 typedef struct RefCntBuffer {
115  // For a RefCntBuffer, the following are reference-holding variables:
116  // - cm->ref_frame_map[]
117  // - cm->cur_frame
118  // - cm->scaled_ref_buf[] (encoder only)
119  // - pbi->output_frame_index[] (decoder only)
120  // With that definition, 'ref_count' is the number of reference-holding
121  // variables that are currently referencing this buffer.
122  // For example:
123  // - suppose this buffer is at index 'k' in the buffer pool, and
124  // - Total 'n' of the variables / array elements above have value 'k' (that
125  // is, they are pointing to buffer at index 'k').
126  // Then, pool->frame_bufs[k].ref_count = n.
127  int ref_count;
128 
129  unsigned int order_hint;
130  unsigned int ref_order_hints[INTER_REFS_PER_FRAME];
131 
132  // These variables are used only in encoder and compare the absolute
133  // display order hint to compute the relative distance and overcome
134  // the limitation of get_relative_dist() which returns incorrect
135  // distance when a very old frame is used as a reference.
136  unsigned int display_order_hint;
137  unsigned int ref_display_order_hint[INTER_REFS_PER_FRAME];
138 
139  MV_REF *mvs;
140  uint8_t *seg_map;
141  struct segmentation seg;
142  int mi_rows;
143  int mi_cols;
144  // Width and height give the size of the buffer (before any upscaling, unlike
145  // the sizes that can be derived from the buf structure)
146  int width;
147  int height;
148  WarpedMotionParams global_motion[REF_FRAMES];
149  int showable_frame; // frame can be used as show existing frame in future
150  uint8_t film_grain_params_present;
151  aom_film_grain_t film_grain_params;
152  aom_codec_frame_buffer_t raw_frame_buffer;
153  YV12_BUFFER_CONFIG buf;
154  FRAME_TYPE frame_type;
155 
156  // This is only used in the encoder but needs to be indexed per ref frame
157  // so it's extremely convenient to keep it here.
158  int interp_filter_selected[SWITCHABLE];
159 
160  // Inter frame reference frame delta for loop filter
161  int8_t ref_deltas[REF_FRAMES];
162 
163  // 0 = ZERO_MV, MV
164  int8_t mode_deltas[MAX_MODE_LF_DELTAS];
165 
166  FRAME_CONTEXT frame_context;
167 } RefCntBuffer;
168 
169 typedef struct BufferPool {
170 // Protect BufferPool from being accessed by several FrameWorkers at
171 // the same time during frame parallel decode.
172 // TODO(hkuang): Try to use atomic variable instead of locking the whole pool.
173 // TODO(wtc): Remove this. See
174 // https://chromium-review.googlesource.com/c/webm/libvpx/+/560630.
175 #if CONFIG_MULTITHREAD
176  pthread_mutex_t pool_mutex;
177 #endif
178 
179  // Private data associated with the frame buffer callbacks.
180  void *cb_priv;
181 
183  aom_release_frame_buffer_cb_fn_t release_fb_cb;
184 
185  RefCntBuffer frame_bufs[FRAME_BUFFERS];
186 
187  // Frame buffers allocated internally by the codec.
188  InternalFrameBufferList int_frame_buffers;
189 } BufferPool;
190 
194 typedef struct {
197  int cdef_strengths[CDEF_MAX_STRENGTHS];
198  int cdef_uv_strengths[CDEF_MAX_STRENGTHS];
200  int cdef_bits;
201 } CdefInfo;
202 
205 typedef struct {
206  int delta_q_present_flag;
207  // Resolution of delta quant
208  int delta_q_res;
209  int delta_lf_present_flag;
210  // Resolution of delta lf level
211  int delta_lf_res;
212  // This is a flag for number of deltas of loop filter level
213  // 0: use 1 delta, for y_vertical, y_horizontal, u, and v
214  // 1: use separate deltas for each filter level
215  int delta_lf_multi;
216 } DeltaQInfo;
217 
218 typedef struct {
219  int enable_order_hint; // 0 - disable order hint, and related tools
220  int order_hint_bits_minus_1; // dist_wtd_comp, ref_frame_mvs,
221  // frame_sign_bias
222  // if 0, enable_dist_wtd_comp and
223  // enable_ref_frame_mvs must be set as 0.
224  int enable_dist_wtd_comp; // 0 - disable dist-wtd compound modes
225  // 1 - enable it
226  int enable_ref_frame_mvs; // 0 - disable ref frame mvs
227  // 1 - enable it
228 } OrderHintInfo;
229 
230 // Sequence header structure.
231 // Note: All syntax elements of sequence_header_obu that need to be
232 // bit-identical across multiple sequence headers must be part of this struct,
233 // so that consistency is checked by are_seq_headers_consistent() function.
234 // One exception is the last member 'op_params' that is ignored by
235 // are_seq_headers_consistent() function.
236 typedef struct SequenceHeader {
237  int num_bits_width;
238  int num_bits_height;
239  int max_frame_width;
240  int max_frame_height;
241  // Whether current and reference frame IDs are signaled in the bitstream.
242  // Frame id numbers are additional information that do not affect the
243  // decoding process, but provide decoders with a way of detecting missing
244  // reference frames so that appropriate action can be taken.
245  uint8_t frame_id_numbers_present_flag;
246  int frame_id_length;
247  int delta_frame_id_length;
248  BLOCK_SIZE sb_size; // Size of the superblock used for this frame
249  int mib_size; // Size of the superblock in units of MI blocks
250  int mib_size_log2; // Log 2 of above.
251 
252  OrderHintInfo order_hint_info;
253 
254  uint8_t force_screen_content_tools; // 0 - force off
255  // 1 - force on
256  // 2 - adaptive
257  uint8_t still_picture; // Video is a single frame still picture
258  uint8_t reduced_still_picture_hdr; // Use reduced header for still picture
259  uint8_t force_integer_mv; // 0 - Don't force. MV can use subpel
260  // 1 - force to integer
261  // 2 - adaptive
262  uint8_t enable_filter_intra; // enables/disables filterintra
263  uint8_t enable_intra_edge_filter; // enables/disables edge upsampling
264  uint8_t enable_interintra_compound; // enables/disables interintra_compound
265  uint8_t enable_masked_compound; // enables/disables masked compound
266  uint8_t enable_dual_filter; // 0 - disable dual interpolation filter
267  // 1 - enable vert/horz filter selection
268  uint8_t enable_warped_motion; // 0 - disable warp for the sequence
269  // 1 - enable warp for the sequence
270  uint8_t enable_superres; // 0 - Disable superres for the sequence
271  // and no frame level superres flag
272  // 1 - Enable superres for the sequence
273  // enable per-frame superres flag
274  uint8_t enable_cdef; // To turn on/off CDEF
275  uint8_t enable_restoration; // To turn on/off loop restoration
276  BITSTREAM_PROFILE profile;
277 
278  // Color config.
279  aom_bit_depth_t bit_depth; // AOM_BITS_8 in profile 0 or 1,
280  // AOM_BITS_10 or AOM_BITS_12 in profile 2 or 3.
281  uint8_t use_highbitdepth; // If true, we need to use 16bit frame buffers.
282  uint8_t monochrome; // Monochorme video
283  aom_color_primaries_t color_primaries;
284  aom_transfer_characteristics_t transfer_characteristics;
285  aom_matrix_coefficients_t matrix_coefficients;
286  int color_range;
287  int subsampling_x; // Chroma subsampling for x
288  int subsampling_y; // Chroma subsampling for y
289  aom_chroma_sample_position_t chroma_sample_position;
290  uint8_t separate_uv_delta_q;
291  uint8_t film_grain_params_present;
292 
293  // Operating point info.
294  int operating_points_cnt_minus_1;
295  int operating_point_idc[MAX_NUM_OPERATING_POINTS];
296  int timing_info_present;
297  aom_timing_info_t timing_info;
298  uint8_t decoder_model_info_present_flag;
299  aom_dec_model_info_t decoder_model_info;
300  uint8_t display_model_info_present_flag;
301  AV1_LEVEL seq_level_idx[MAX_NUM_OPERATING_POINTS];
302  uint8_t tier[MAX_NUM_OPERATING_POINTS]; // seq_tier in spec. One bit: 0 or 1.
303 
304  // IMPORTANT: the op_params member must be at the end of the struct so that
305  // are_seq_headers_consistent() can be implemented with a memcmp() call.
306  // TODO(urvang): We probably don't need the +1 here.
307  aom_dec_model_op_parameters_t op_params[MAX_NUM_OPERATING_POINTS + 1];
308 } SequenceHeader;
309 
310 typedef struct {
311  int skip_mode_allowed;
312  int skip_mode_flag;
313  int ref_frame_idx_0;
314  int ref_frame_idx_1;
315 } SkipModeInfo;
316 
317 typedef struct {
318  FRAME_TYPE frame_type;
319  REFERENCE_MODE reference_mode;
320 
321  unsigned int order_hint;
322  unsigned int display_order_hint;
323  unsigned int frame_number;
324  SkipModeInfo skip_mode_info;
325  int refresh_frame_flags; // Which ref frames are overwritten by this frame
326  int frame_refs_short_signaling;
327 } CurrentFrame;
328 
334 typedef struct {
382  TX_MODE tx_mode;
383  InterpFilter interp_filter;
397  REFRESH_FRAME_CONTEXT_MODE refresh_frame_context;
398 } FeatureFlags;
399 
403 typedef struct CommonTileParams {
404  int cols;
405  int rows;
413 
420 
425  int log2_cols;
426  int log2_rows;
427  int width;
428  int height;
450  int min_log2;
455  int col_start_sb[MAX_TILE_COLS + 1];
460  int row_start_sb[MAX_TILE_ROWS + 1];
464  unsigned int large_scale;
470  unsigned int single_tile_decoding;
472 
482  int mb_rows;
487  int mb_cols;
488 
492  int MBs;
493 
498  int mi_rows;
503  int mi_cols;
504 
526  BLOCK_SIZE mi_alloc_bsize;
527 
544 
551  TX_TYPE *tx_type_map;
552 
561  void (*free_mi)(struct CommonModeInfoParams *mi_params);
566  void (*setup_mi)(struct CommonModeInfoParams *mi_params);
573  void (*set_mb_mi)(struct CommonModeInfoParams *mi_params, int width,
574  int height);
576 };
577 
578 typedef struct CommonQuantParams CommonQuantParams;
587 
593 
602 
613 
614  /*
615  * Note: The qindex per superblock may have a delta from the qindex obtained
616  * at frame level from parameters above, based on 'cm->delta_q_info'.
617  */
618 
626  int16_t y_dequant_QTX[MAX_SEGMENTS][2];
627  int16_t u_dequant_QTX[MAX_SEGMENTS][2];
628  int16_t v_dequant_QTX[MAX_SEGMENTS][2];
638  const qm_val_t *giqmatrix[NUM_QM_LEVELS][3][TX_SIZES_ALL];
642  const qm_val_t *gqmatrix[NUM_QM_LEVELS][3][TX_SIZES_ALL];
652  const qm_val_t *y_iqmatrix[MAX_SEGMENTS][TX_SIZES_ALL];
656  const qm_val_t *u_iqmatrix[MAX_SEGMENTS][TX_SIZES_ALL];
660  const qm_val_t *v_iqmatrix[MAX_SEGMENTS][TX_SIZES_ALL];
680 };
681 
682 typedef struct CommonContexts CommonContexts;
691  PARTITION_CONTEXT **partition;
692 
701  ENTROPY_CONTEXT **entropy[MAX_MB_PLANE];
702 
709  TXFM_CONTEXT **txfm;
710 
718 };
719 
723 typedef struct AV1Common {
727  CurrentFrame current_frame;
731  struct aom_internal_error_info error;
732 
748  int width;
749  int height;
781 
792  uint32_t buffer_removal_times[MAX_NUM_OPERATING_POINTS + 1];
799 
803  RefCntBuffer *prev_frame;
804 
809  RefCntBuffer *cur_frame;
810 
831  int remapped_ref_idx[REF_FRAMES];
832 
838  struct scale_factors sf_identity;
839 
846  struct scale_factors ref_scale_factors[REF_FRAMES];
847 
855  RefCntBuffer *ref_frame_map[REF_FRAMES];
856 
863 
871 
878 
883 
888 
889 #if CONFIG_ENTROPY_STATS
890 
893  int coef_cdf_category;
894 #endif // CONFIG_ENTROPY_STATS
895 
900 
904  struct segmentation seg;
905 
910 
915  loop_filter_info_n lf_info;
916  struct loopfilter lf;
923  RestorationInfo rst_info[MAX_MB_PLANE];
924  int32_t *rst_tmpbuf;
925  RestorationLineBuffers *rlbs;
933 
937  aom_film_grain_t film_grain_params;
938 
942  DeltaQInfo delta_q_info;
943 
947  WarpedMotionParams global_motion[REF_FRAMES];
948 
953  SequenceHeader seq_params;
954 
958  FRAME_CONTEXT *fc;
964  FRAME_CONTEXT *default_frame_context;
965 
970 
974  BufferPool *buffer_pool;
975 
983 
989  int ref_frame_id[REF_FRAMES];
999  TPL_MV_REF *tpl_mvs;
1008  int ref_frame_sign_bias[REF_FRAMES];
1014  int8_t ref_frame_side[REF_FRAMES];
1015 
1025 
1035 
1036 #if TXCOEFF_TIMER
1037  int64_t cum_txcoeff_timer;
1038  int64_t txcoeff_timer;
1039  int txb_count;
1040 #endif // TXCOEFF_TIMER
1041 
1042 #if TXCOEFF_COST_TIMER
1043  int64_t cum_txcoeff_cost_timer;
1044  int64_t txcoeff_cost_timer;
1045  int64_t txcoeff_cost_count;
1046 #endif // TXCOEFF_COST_TIMER
1047 
1048 #if CONFIG_LPF_MASK
1049  int is_decoding;
1050 #endif // CONFIG_LPF_MASK
1051 } AV1_COMMON;
1052 
1055 // TODO(hkuang): Don't need to lock the whole pool after implementing atomic
1056 // frame reference count.
1057 static void lock_buffer_pool(BufferPool *const pool) {
1058 #if CONFIG_MULTITHREAD
1059  pthread_mutex_lock(&pool->pool_mutex);
1060 #else
1061  (void)pool;
1062 #endif
1063 }
1064 
1065 static void unlock_buffer_pool(BufferPool *const pool) {
1066 #if CONFIG_MULTITHREAD
1067  pthread_mutex_unlock(&pool->pool_mutex);
1068 #else
1069  (void)pool;
1070 #endif
1071 }
1072 
1073 static INLINE YV12_BUFFER_CONFIG *get_ref_frame(AV1_COMMON *cm, int index) {
1074  if (index < 0 || index >= REF_FRAMES) return NULL;
1075  if (cm->ref_frame_map[index] == NULL) return NULL;
1076  return &cm->ref_frame_map[index]->buf;
1077 }
1078 
1079 static INLINE int get_free_fb(AV1_COMMON *cm) {
1080  RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
1081  int i;
1082 
1083  lock_buffer_pool(cm->buffer_pool);
1084  for (i = 0; i < FRAME_BUFFERS; ++i)
1085  if (frame_bufs[i].ref_count == 0) break;
1086 
1087  if (i != FRAME_BUFFERS) {
1088  if (frame_bufs[i].buf.use_external_reference_buffers) {
1089  // If this frame buffer's y_buffer, u_buffer, and v_buffer point to the
1090  // external reference buffers. Restore the buffer pointers to point to the
1091  // internally allocated memory.
1092  YV12_BUFFER_CONFIG *ybf = &frame_bufs[i].buf;
1093  ybf->y_buffer = ybf->store_buf_adr[0];
1094  ybf->u_buffer = ybf->store_buf_adr[1];
1095  ybf->v_buffer = ybf->store_buf_adr[2];
1096  ybf->use_external_reference_buffers = 0;
1097  }
1098 
1099  frame_bufs[i].ref_count = 1;
1100  } else {
1101  // We should never run out of free buffers. If this assertion fails, there
1102  // is a reference leak.
1103  assert(0 && "Ran out of free frame buffers. Likely a reference leak.");
1104  // Reset i to be INVALID_IDX to indicate no free buffer found.
1105  i = INVALID_IDX;
1106  }
1107 
1108  unlock_buffer_pool(cm->buffer_pool);
1109  return i;
1110 }
1111 
1112 static INLINE RefCntBuffer *assign_cur_frame_new_fb(AV1_COMMON *const cm) {
1113  // Release the previously-used frame-buffer
1114  if (cm->cur_frame != NULL) {
1115  --cm->cur_frame->ref_count;
1116  cm->cur_frame = NULL;
1117  }
1118 
1119  // Assign a new framebuffer
1120  const int new_fb_idx = get_free_fb(cm);
1121  if (new_fb_idx == INVALID_IDX) return NULL;
1122 
1123  cm->cur_frame = &cm->buffer_pool->frame_bufs[new_fb_idx];
1124  cm->cur_frame->buf.buf_8bit_valid = 0;
1125  av1_zero(cm->cur_frame->interp_filter_selected);
1126  return cm->cur_frame;
1127 }
1128 
1129 // Modify 'lhs_ptr' to reference the buffer at 'rhs_ptr', and update the ref
1130 // counts accordingly.
1131 static INLINE void assign_frame_buffer_p(RefCntBuffer **lhs_ptr,
1132  RefCntBuffer *rhs_ptr) {
1133  RefCntBuffer *const old_ptr = *lhs_ptr;
1134  if (old_ptr != NULL) {
1135  assert(old_ptr->ref_count > 0);
1136  // One less reference to the buffer at 'old_ptr', so decrease ref count.
1137  --old_ptr->ref_count;
1138  }
1139 
1140  *lhs_ptr = rhs_ptr;
1141  // One more reference to the buffer at 'rhs_ptr', so increase ref count.
1142  ++rhs_ptr->ref_count;
1143 }
1144 
1145 static INLINE int frame_is_intra_only(const AV1_COMMON *const cm) {
1146  return cm->current_frame.frame_type == KEY_FRAME ||
1147  cm->current_frame.frame_type == INTRA_ONLY_FRAME;
1148 }
1149 
1150 static INLINE int frame_is_sframe(const AV1_COMMON *cm) {
1151  return cm->current_frame.frame_type == S_FRAME;
1152 }
1153 
1154 // These functions take a reference frame label between LAST_FRAME and
1155 // EXTREF_FRAME inclusive. Note that this is different to the indexing
1156 // previously used by the frame_refs[] array.
1157 static INLINE int get_ref_frame_map_idx(const AV1_COMMON *const cm,
1158  const MV_REFERENCE_FRAME ref_frame) {
1159  return (ref_frame >= LAST_FRAME && ref_frame <= EXTREF_FRAME)
1160  ? cm->remapped_ref_idx[ref_frame - LAST_FRAME]
1161  : INVALID_IDX;
1162 }
1163 
1164 static INLINE RefCntBuffer *get_ref_frame_buf(
1165  const AV1_COMMON *const cm, const MV_REFERENCE_FRAME ref_frame) {
1166  const int map_idx = get_ref_frame_map_idx(cm, ref_frame);
1167  return (map_idx != INVALID_IDX) ? cm->ref_frame_map[map_idx] : NULL;
1168 }
1169 
1170 // Both const and non-const versions of this function are provided so that it
1171 // can be used with a const AV1_COMMON if needed.
1172 static INLINE const struct scale_factors *get_ref_scale_factors_const(
1173  const AV1_COMMON *const cm, const MV_REFERENCE_FRAME ref_frame) {
1174  const int map_idx = get_ref_frame_map_idx(cm, ref_frame);
1175  return (map_idx != INVALID_IDX) ? &cm->ref_scale_factors[map_idx] : NULL;
1176 }
1177 
1178 static INLINE struct scale_factors *get_ref_scale_factors(
1179  AV1_COMMON *const cm, const MV_REFERENCE_FRAME ref_frame) {
1180  const int map_idx = get_ref_frame_map_idx(cm, ref_frame);
1181  return (map_idx != INVALID_IDX) ? &cm->ref_scale_factors[map_idx] : NULL;
1182 }
1183 
1184 static INLINE RefCntBuffer *get_primary_ref_frame_buf(
1185  const AV1_COMMON *const cm) {
1186  const int primary_ref_frame = cm->features.primary_ref_frame;
1187  if (primary_ref_frame == PRIMARY_REF_NONE) return NULL;
1188  const int map_idx = get_ref_frame_map_idx(cm, primary_ref_frame + 1);
1189  return (map_idx != INVALID_IDX) ? cm->ref_frame_map[map_idx] : NULL;
1190 }
1191 
1192 // Returns 1 if this frame might allow mvs from some reference frame.
1193 static INLINE int frame_might_allow_ref_frame_mvs(const AV1_COMMON *cm) {
1194  return !cm->features.error_resilient_mode &&
1195  cm->seq_params.order_hint_info.enable_ref_frame_mvs &&
1196  cm->seq_params.order_hint_info.enable_order_hint &&
1197  !frame_is_intra_only(cm);
1198 }
1199 
1200 // Returns 1 if this frame might use warped_motion
1201 static INLINE int frame_might_allow_warped_motion(const AV1_COMMON *cm) {
1202  return !cm->features.error_resilient_mode && !frame_is_intra_only(cm) &&
1203  cm->seq_params.enable_warped_motion;
1204 }
1205 
1206 static INLINE void ensure_mv_buffer(RefCntBuffer *buf, AV1_COMMON *cm) {
1207  const int buf_rows = buf->mi_rows;
1208  const int buf_cols = buf->mi_cols;
1209  const CommonModeInfoParams *const mi_params = &cm->mi_params;
1210 
1211  if (buf->mvs == NULL || buf_rows != mi_params->mi_rows ||
1212  buf_cols != mi_params->mi_cols) {
1213  aom_free(buf->mvs);
1214  buf->mi_rows = mi_params->mi_rows;
1215  buf->mi_cols = mi_params->mi_cols;
1216  CHECK_MEM_ERROR(cm, buf->mvs,
1217  (MV_REF *)aom_calloc(((mi_params->mi_rows + 1) >> 1) *
1218  ((mi_params->mi_cols + 1) >> 1),
1219  sizeof(*buf->mvs)));
1220  aom_free(buf->seg_map);
1221  CHECK_MEM_ERROR(
1222  cm, buf->seg_map,
1223  (uint8_t *)aom_calloc(mi_params->mi_rows * mi_params->mi_cols,
1224  sizeof(*buf->seg_map)));
1225  }
1226 
1227  const int mem_size =
1228  ((mi_params->mi_rows + MAX_MIB_SIZE) >> 1) * (mi_params->mi_stride >> 1);
1229  int realloc = cm->tpl_mvs == NULL;
1230  if (cm->tpl_mvs) realloc |= cm->tpl_mvs_mem_size < mem_size;
1231 
1232  if (realloc) {
1233  aom_free(cm->tpl_mvs);
1234  CHECK_MEM_ERROR(cm, cm->tpl_mvs,
1235  (TPL_MV_REF *)aom_calloc(mem_size, sizeof(*cm->tpl_mvs)));
1236  cm->tpl_mvs_mem_size = mem_size;
1237  }
1238 }
1239 
1240 void cfl_init(CFL_CTX *cfl, const SequenceHeader *seq_params);
1241 
1242 static INLINE int av1_num_planes(const AV1_COMMON *cm) {
1243  return cm->seq_params.monochrome ? 1 : MAX_MB_PLANE;
1244 }
1245 
1246 static INLINE void av1_init_above_context(CommonContexts *above_contexts,
1247  int num_planes, int tile_row,
1248  MACROBLOCKD *xd) {
1249  for (int i = 0; i < num_planes; ++i) {
1250  xd->above_entropy_context[i] = above_contexts->entropy[i][tile_row];
1251  }
1252  xd->above_partition_context = above_contexts->partition[tile_row];
1253  xd->above_txfm_context = above_contexts->txfm[tile_row];
1254 }
1255 
1256 static INLINE void av1_init_macroblockd(AV1_COMMON *cm, MACROBLOCKD *xd) {
1257  const int num_planes = av1_num_planes(cm);
1258  const CommonQuantParams *const quant_params = &cm->quant_params;
1259 
1260  for (int i = 0; i < num_planes; ++i) {
1261  if (xd->plane[i].plane_type == PLANE_TYPE_Y) {
1262  memcpy(xd->plane[i].seg_dequant_QTX, quant_params->y_dequant_QTX,
1263  sizeof(quant_params->y_dequant_QTX));
1264  memcpy(xd->plane[i].seg_iqmatrix, quant_params->y_iqmatrix,
1265  sizeof(quant_params->y_iqmatrix));
1266 
1267  } else {
1268  if (i == AOM_PLANE_U) {
1269  memcpy(xd->plane[i].seg_dequant_QTX, quant_params->u_dequant_QTX,
1270  sizeof(quant_params->u_dequant_QTX));
1271  memcpy(xd->plane[i].seg_iqmatrix, quant_params->u_iqmatrix,
1272  sizeof(quant_params->u_iqmatrix));
1273  } else {
1274  memcpy(xd->plane[i].seg_dequant_QTX, quant_params->v_dequant_QTX,
1275  sizeof(quant_params->v_dequant_QTX));
1276  memcpy(xd->plane[i].seg_iqmatrix, quant_params->v_iqmatrix,
1277  sizeof(quant_params->v_iqmatrix));
1278  }
1279  }
1280  }
1281  xd->mi_stride = cm->mi_params.mi_stride;
1282  xd->error_info = &cm->error;
1283  cfl_init(&xd->cfl, &cm->seq_params);
1284 }
1285 
1286 static INLINE void set_entropy_context(MACROBLOCKD *xd, int mi_row, int mi_col,
1287  const int num_planes) {
1288  int i;
1289  int row_offset = mi_row;
1290  int col_offset = mi_col;
1291  for (i = 0; i < num_planes; ++i) {
1292  struct macroblockd_plane *const pd = &xd->plane[i];
1293  // Offset the buffer pointer
1294  const BLOCK_SIZE bsize = xd->mi[0]->bsize;
1295  if (pd->subsampling_y && (mi_row & 0x01) && (mi_size_high[bsize] == 1))
1296  row_offset = mi_row - 1;
1297  if (pd->subsampling_x && (mi_col & 0x01) && (mi_size_wide[bsize] == 1))
1298  col_offset = mi_col - 1;
1299  int above_idx = col_offset;
1300  int left_idx = row_offset & MAX_MIB_MASK;
1301  pd->above_entropy_context =
1302  &xd->above_entropy_context[i][above_idx >> pd->subsampling_x];
1303  pd->left_entropy_context =
1304  &xd->left_entropy_context[i][left_idx >> pd->subsampling_y];
1305  }
1306 }
1307 
1308 static INLINE int calc_mi_size(int len) {
1309  // len is in mi units. Align to a multiple of SBs.
1310  return ALIGN_POWER_OF_TWO(len, MAX_MIB_SIZE_LOG2);
1311 }
1312 
1313 static INLINE void set_plane_n4(MACROBLOCKD *const xd, int bw, int bh,
1314  const int num_planes) {
1315  int i;
1316  for (i = 0; i < num_planes; i++) {
1317  xd->plane[i].width = (bw * MI_SIZE) >> xd->plane[i].subsampling_x;
1318  xd->plane[i].height = (bh * MI_SIZE) >> xd->plane[i].subsampling_y;
1319 
1320  xd->plane[i].width = AOMMAX(xd->plane[i].width, 4);
1321  xd->plane[i].height = AOMMAX(xd->plane[i].height, 4);
1322  }
1323 }
1324 
1325 static INLINE void set_mi_row_col(MACROBLOCKD *xd, const TileInfo *const tile,
1326  int mi_row, int bh, int mi_col, int bw,
1327  int mi_rows, int mi_cols) {
1328  xd->mb_to_top_edge = -GET_MV_SUBPEL(mi_row * MI_SIZE);
1329  xd->mb_to_bottom_edge = GET_MV_SUBPEL((mi_rows - bh - mi_row) * MI_SIZE);
1330  xd->mb_to_left_edge = -GET_MV_SUBPEL((mi_col * MI_SIZE));
1331  xd->mb_to_right_edge = GET_MV_SUBPEL((mi_cols - bw - mi_col) * MI_SIZE);
1332 
1333  xd->mi_row = mi_row;
1334  xd->mi_col = mi_col;
1335 
1336  // Are edges available for intra prediction?
1337  xd->up_available = (mi_row > tile->mi_row_start);
1338 
1339  const int ss_x = xd->plane[1].subsampling_x;
1340  const int ss_y = xd->plane[1].subsampling_y;
1341 
1342  xd->left_available = (mi_col > tile->mi_col_start);
1345  if (ss_x && bw < mi_size_wide[BLOCK_8X8])
1346  xd->chroma_left_available = (mi_col - 1) > tile->mi_col_start;
1347  if (ss_y && bh < mi_size_high[BLOCK_8X8])
1348  xd->chroma_up_available = (mi_row - 1) > tile->mi_row_start;
1349  if (xd->up_available) {
1350  xd->above_mbmi = xd->mi[-xd->mi_stride];
1351  } else {
1352  xd->above_mbmi = NULL;
1353  }
1354 
1355  if (xd->left_available) {
1356  xd->left_mbmi = xd->mi[-1];
1357  } else {
1358  xd->left_mbmi = NULL;
1359  }
1360 
1361  const int chroma_ref = ((mi_row & 0x01) || !(bh & 0x01) || !ss_y) &&
1362  ((mi_col & 0x01) || !(bw & 0x01) || !ss_x);
1363  xd->is_chroma_ref = chroma_ref;
1364  if (chroma_ref) {
1365  // To help calculate the "above" and "left" chroma blocks, note that the
1366  // current block may cover multiple luma blocks (eg, if partitioned into
1367  // 4x4 luma blocks).
1368  // First, find the top-left-most luma block covered by this chroma block
1369  MB_MODE_INFO **base_mi =
1370  &xd->mi[-(mi_row & ss_y) * xd->mi_stride - (mi_col & ss_x)];
1371 
1372  // Then, we consider the luma region covered by the left or above 4x4 chroma
1373  // prediction. We want to point to the chroma reference block in that
1374  // region, which is the bottom-right-most mi unit.
1375  // This leads to the following offsets:
1376  MB_MODE_INFO *chroma_above_mi =
1377  xd->chroma_up_available ? base_mi[-xd->mi_stride + ss_x] : NULL;
1378  xd->chroma_above_mbmi = chroma_above_mi;
1379 
1380  MB_MODE_INFO *chroma_left_mi =
1381  xd->chroma_left_available ? base_mi[ss_y * xd->mi_stride - 1] : NULL;
1382  xd->chroma_left_mbmi = chroma_left_mi;
1383  }
1384 
1385  xd->height = bh;
1386  xd->width = bw;
1387 
1388  xd->is_last_vertical_rect = 0;
1389  if (xd->width < xd->height) {
1390  if (!((mi_col + xd->width) & (xd->height - 1))) {
1391  xd->is_last_vertical_rect = 1;
1392  }
1393  }
1394 
1395  xd->is_first_horizontal_rect = 0;
1396  if (xd->width > xd->height)
1397  if (!(mi_row & (xd->width - 1))) xd->is_first_horizontal_rect = 1;
1398 }
1399 
1400 static INLINE aom_cdf_prob *get_y_mode_cdf(FRAME_CONTEXT *tile_ctx,
1401  const MB_MODE_INFO *above_mi,
1402  const MB_MODE_INFO *left_mi) {
1403  const PREDICTION_MODE above = av1_above_block_mode(above_mi);
1404  const PREDICTION_MODE left = av1_left_block_mode(left_mi);
1405  const int above_ctx = intra_mode_context[above];
1406  const int left_ctx = intra_mode_context[left];
1407  return tile_ctx->kf_y_cdf[above_ctx][left_ctx];
1408 }
1409 
1410 static INLINE void update_partition_context(MACROBLOCKD *xd, int mi_row,
1411  int mi_col, BLOCK_SIZE subsize,
1412  BLOCK_SIZE bsize) {
1413  PARTITION_CONTEXT *const above_ctx = xd->above_partition_context + mi_col;
1414  PARTITION_CONTEXT *const left_ctx =
1415  xd->left_partition_context + (mi_row & MAX_MIB_MASK);
1416 
1417  const int bw = mi_size_wide[bsize];
1418  const int bh = mi_size_high[bsize];
1419  memset(above_ctx, partition_context_lookup[subsize].above, bw);
1420  memset(left_ctx, partition_context_lookup[subsize].left, bh);
1421 }
1422 
1423 static INLINE int is_chroma_reference(int mi_row, int mi_col, BLOCK_SIZE bsize,
1424  int subsampling_x, int subsampling_y) {
1425  assert(bsize < BLOCK_SIZES_ALL);
1426  const int bw = mi_size_wide[bsize];
1427  const int bh = mi_size_high[bsize];
1428  int ref_pos = ((mi_row & 0x01) || !(bh & 0x01) || !subsampling_y) &&
1429  ((mi_col & 0x01) || !(bw & 0x01) || !subsampling_x);
1430  return ref_pos;
1431 }
1432 
1433 static INLINE aom_cdf_prob cdf_element_prob(const aom_cdf_prob *cdf,
1434  size_t element) {
1435  assert(cdf != NULL);
1436  return (element > 0 ? cdf[element - 1] : CDF_PROB_TOP) - cdf[element];
1437 }
1438 
1439 static INLINE void partition_gather_horz_alike(aom_cdf_prob *out,
1440  const aom_cdf_prob *const in,
1441  BLOCK_SIZE bsize) {
1442  (void)bsize;
1443  out[0] = CDF_PROB_TOP;
1444  out[0] -= cdf_element_prob(in, PARTITION_HORZ);
1445  out[0] -= cdf_element_prob(in, PARTITION_SPLIT);
1446  out[0] -= cdf_element_prob(in, PARTITION_HORZ_A);
1447  out[0] -= cdf_element_prob(in, PARTITION_HORZ_B);
1448  out[0] -= cdf_element_prob(in, PARTITION_VERT_A);
1449  if (bsize != BLOCK_128X128) out[0] -= cdf_element_prob(in, PARTITION_HORZ_4);
1450  out[0] = AOM_ICDF(out[0]);
1451  out[1] = AOM_ICDF(CDF_PROB_TOP);
1452 }
1453 
1454 static INLINE void partition_gather_vert_alike(aom_cdf_prob *out,
1455  const aom_cdf_prob *const in,
1456  BLOCK_SIZE bsize) {
1457  (void)bsize;
1458  out[0] = CDF_PROB_TOP;
1459  out[0] -= cdf_element_prob(in, PARTITION_VERT);
1460  out[0] -= cdf_element_prob(in, PARTITION_SPLIT);
1461  out[0] -= cdf_element_prob(in, PARTITION_HORZ_A);
1462  out[0] -= cdf_element_prob(in, PARTITION_VERT_A);
1463  out[0] -= cdf_element_prob(in, PARTITION_VERT_B);
1464  if (bsize != BLOCK_128X128) out[0] -= cdf_element_prob(in, PARTITION_VERT_4);
1465  out[0] = AOM_ICDF(out[0]);
1466  out[1] = AOM_ICDF(CDF_PROB_TOP);
1467 }
1468 
1469 static INLINE void update_ext_partition_context(MACROBLOCKD *xd, int mi_row,
1470  int mi_col, BLOCK_SIZE subsize,
1471  BLOCK_SIZE bsize,
1472  PARTITION_TYPE partition) {
1473  if (bsize >= BLOCK_8X8) {
1474  const int hbs = mi_size_wide[bsize] / 2;
1475  BLOCK_SIZE bsize2 = get_partition_subsize(bsize, PARTITION_SPLIT);
1476  switch (partition) {
1477  case PARTITION_SPLIT:
1478  if (bsize != BLOCK_8X8) break;
1479  AOM_FALLTHROUGH_INTENDED;
1480  case PARTITION_NONE:
1481  case PARTITION_HORZ:
1482  case PARTITION_VERT:
1483  case PARTITION_HORZ_4:
1484  case PARTITION_VERT_4:
1485  update_partition_context(xd, mi_row, mi_col, subsize, bsize);
1486  break;
1487  case PARTITION_HORZ_A:
1488  update_partition_context(xd, mi_row, mi_col, bsize2, subsize);
1489  update_partition_context(xd, mi_row + hbs, mi_col, subsize, subsize);
1490  break;
1491  case PARTITION_HORZ_B:
1492  update_partition_context(xd, mi_row, mi_col, subsize, subsize);
1493  update_partition_context(xd, mi_row + hbs, mi_col, bsize2, subsize);
1494  break;
1495  case PARTITION_VERT_A:
1496  update_partition_context(xd, mi_row, mi_col, bsize2, subsize);
1497  update_partition_context(xd, mi_row, mi_col + hbs, subsize, subsize);
1498  break;
1499  case PARTITION_VERT_B:
1500  update_partition_context(xd, mi_row, mi_col, subsize, subsize);
1501  update_partition_context(xd, mi_row, mi_col + hbs, bsize2, subsize);
1502  break;
1503  default: assert(0 && "Invalid partition type");
1504  }
1505  }
1506 }
1507 
1508 static INLINE int partition_plane_context(const MACROBLOCKD *xd, int mi_row,
1509  int mi_col, BLOCK_SIZE bsize) {
1510  const PARTITION_CONTEXT *above_ctx = xd->above_partition_context + mi_col;
1511  const PARTITION_CONTEXT *left_ctx =
1512  xd->left_partition_context + (mi_row & MAX_MIB_MASK);
1513  // Minimum partition point is 8x8. Offset the bsl accordingly.
1514  const int bsl = mi_size_wide_log2[bsize] - mi_size_wide_log2[BLOCK_8X8];
1515  int above = (*above_ctx >> bsl) & 1, left = (*left_ctx >> bsl) & 1;
1516 
1517  assert(mi_size_wide_log2[bsize] == mi_size_high_log2[bsize]);
1518  assert(bsl >= 0);
1519 
1520  return (left * 2 + above) + bsl * PARTITION_PLOFFSET;
1521 }
1522 
1523 // Return the number of elements in the partition CDF when
1524 // partitioning the (square) block with luma block size of bsize.
1525 static INLINE int partition_cdf_length(BLOCK_SIZE bsize) {
1526  if (bsize <= BLOCK_8X8)
1527  return PARTITION_TYPES;
1528  else if (bsize == BLOCK_128X128)
1529  return EXT_PARTITION_TYPES - 2;
1530  else
1531  return EXT_PARTITION_TYPES;
1532 }
1533 
1534 static INLINE int max_block_wide(const MACROBLOCKD *xd, BLOCK_SIZE bsize,
1535  int plane) {
1536  assert(bsize < BLOCK_SIZES_ALL);
1537  int max_blocks_wide = block_size_wide[bsize];
1538 
1539  if (xd->mb_to_right_edge < 0) {
1540  const struct macroblockd_plane *const pd = &xd->plane[plane];
1541  max_blocks_wide += xd->mb_to_right_edge >> (3 + pd->subsampling_x);
1542  }
1543 
1544  // Scale the width in the transform block unit.
1545  return max_blocks_wide >> MI_SIZE_LOG2;
1546 }
1547 
1548 static INLINE int max_block_high(const MACROBLOCKD *xd, BLOCK_SIZE bsize,
1549  int plane) {
1550  int max_blocks_high = block_size_high[bsize];
1551 
1552  if (xd->mb_to_bottom_edge < 0) {
1553  const struct macroblockd_plane *const pd = &xd->plane[plane];
1554  max_blocks_high += xd->mb_to_bottom_edge >> (3 + pd->subsampling_y);
1555  }
1556 
1557  // Scale the height in the transform block unit.
1558  return max_blocks_high >> MI_SIZE_LOG2;
1559 }
1560 
1561 static INLINE void av1_zero_above_context(AV1_COMMON *const cm,
1562  const MACROBLOCKD *xd,
1563  int mi_col_start, int mi_col_end,
1564  const int tile_row) {
1565  const SequenceHeader *const seq_params = &cm->seq_params;
1566  const int num_planes = av1_num_planes(cm);
1567  const int width = mi_col_end - mi_col_start;
1568  const int aligned_width =
1569  ALIGN_POWER_OF_TWO(width, seq_params->mib_size_log2);
1570  const int offset_y = mi_col_start;
1571  const int width_y = aligned_width;
1572  const int offset_uv = offset_y >> seq_params->subsampling_x;
1573  const int width_uv = width_y >> seq_params->subsampling_x;
1574  CommonContexts *const above_contexts = &cm->above_contexts;
1575 
1576  av1_zero_array(above_contexts->entropy[0][tile_row] + offset_y, width_y);
1577  if (num_planes > 1) {
1578  if (above_contexts->entropy[1][tile_row] &&
1579  above_contexts->entropy[2][tile_row]) {
1580  av1_zero_array(above_contexts->entropy[1][tile_row] + offset_uv,
1581  width_uv);
1582  av1_zero_array(above_contexts->entropy[2][tile_row] + offset_uv,
1583  width_uv);
1584  } else {
1585  aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
1586  "Invalid value of planes");
1587  }
1588  }
1589 
1590  av1_zero_array(above_contexts->partition[tile_row] + mi_col_start,
1591  aligned_width);
1592 
1593  memset(above_contexts->txfm[tile_row] + mi_col_start,
1594  tx_size_wide[TX_SIZES_LARGEST], aligned_width * sizeof(TXFM_CONTEXT));
1595 }
1596 
1597 static INLINE void av1_zero_left_context(MACROBLOCKD *const xd) {
1598  av1_zero(xd->left_entropy_context);
1599  av1_zero(xd->left_partition_context);
1600 
1601  memset(xd->left_txfm_context_buffer, tx_size_high[TX_SIZES_LARGEST],
1602  sizeof(xd->left_txfm_context_buffer));
1603 }
1604 
1605 // Disable array-bounds checks as the TX_SIZE enum contains values larger than
1606 // TX_SIZES_ALL (TX_INVALID) which make extending the array as a workaround
1607 // infeasible. The assert is enough for static analysis and this or other tools
1608 // asan, valgrind would catch oob access at runtime.
1609 #if defined(__GNUC__) && __GNUC__ >= 4
1610 #pragma GCC diagnostic ignored "-Warray-bounds"
1611 #endif
1612 
1613 #if defined(__GNUC__) && __GNUC__ >= 4
1614 #pragma GCC diagnostic warning "-Warray-bounds"
1615 #endif
1616 
1617 static INLINE void set_txfm_ctx(TXFM_CONTEXT *txfm_ctx, uint8_t txs, int len) {
1618  int i;
1619  for (i = 0; i < len; ++i) txfm_ctx[i] = txs;
1620 }
1621 
1622 static INLINE void set_txfm_ctxs(TX_SIZE tx_size, int n4_w, int n4_h, int skip,
1623  const MACROBLOCKD *xd) {
1624  uint8_t bw = tx_size_wide[tx_size];
1625  uint8_t bh = tx_size_high[tx_size];
1626 
1627  if (skip) {
1628  bw = n4_w * MI_SIZE;
1629  bh = n4_h * MI_SIZE;
1630  }
1631 
1632  set_txfm_ctx(xd->above_txfm_context, bw, n4_w);
1633  set_txfm_ctx(xd->left_txfm_context, bh, n4_h);
1634 }
1635 
1636 static INLINE int get_mi_grid_idx(const CommonModeInfoParams *const mi_params,
1637  int mi_row, int mi_col) {
1638  return mi_row * mi_params->mi_stride + mi_col;
1639 }
1640 
1641 static INLINE int get_alloc_mi_idx(const CommonModeInfoParams *const mi_params,
1642  int mi_row, int mi_col) {
1643  const int mi_alloc_size_1d = mi_size_wide[mi_params->mi_alloc_bsize];
1644  const int mi_alloc_row = mi_row / mi_alloc_size_1d;
1645  const int mi_alloc_col = mi_col / mi_alloc_size_1d;
1646 
1647  return mi_alloc_row * mi_params->mi_alloc_stride + mi_alloc_col;
1648 }
1649 
1650 // For this partition block, set pointers in mi_params->mi_grid_base and xd->mi.
1651 static INLINE void set_mi_offsets(const CommonModeInfoParams *const mi_params,
1652  MACROBLOCKD *const xd, int mi_row,
1653  int mi_col) {
1654  // 'mi_grid_base' should point to appropriate memory in 'mi'.
1655  const int mi_grid_idx = get_mi_grid_idx(mi_params, mi_row, mi_col);
1656  const int mi_alloc_idx = get_alloc_mi_idx(mi_params, mi_row, mi_col);
1657  mi_params->mi_grid_base[mi_grid_idx] = &mi_params->mi_alloc[mi_alloc_idx];
1658  // 'xd->mi' should point to an offset in 'mi_grid_base';
1659  xd->mi = mi_params->mi_grid_base + mi_grid_idx;
1660  // 'xd->tx_type_map' should point to an offset in 'mi_params->tx_type_map'.
1661  xd->tx_type_map = mi_params->tx_type_map + mi_grid_idx;
1662  xd->tx_type_map_stride = mi_params->mi_stride;
1663 }
1664 
1665 static INLINE void txfm_partition_update(TXFM_CONTEXT *above_ctx,
1666  TXFM_CONTEXT *left_ctx,
1667  TX_SIZE tx_size, TX_SIZE txb_size) {
1668  BLOCK_SIZE bsize = txsize_to_bsize[txb_size];
1669  int bh = mi_size_high[bsize];
1670  int bw = mi_size_wide[bsize];
1671  uint8_t txw = tx_size_wide[tx_size];
1672  uint8_t txh = tx_size_high[tx_size];
1673  int i;
1674  for (i = 0; i < bh; ++i) left_ctx[i] = txh;
1675  for (i = 0; i < bw; ++i) above_ctx[i] = txw;
1676 }
1677 
1678 static INLINE TX_SIZE get_sqr_tx_size(int tx_dim) {
1679  switch (tx_dim) {
1680  case 128:
1681  case 64: return TX_64X64; break;
1682  case 32: return TX_32X32; break;
1683  case 16: return TX_16X16; break;
1684  case 8: return TX_8X8; break;
1685  default: return TX_4X4;
1686  }
1687 }
1688 
1689 static INLINE TX_SIZE get_tx_size(int width, int height) {
1690  if (width == height) {
1691  return get_sqr_tx_size(width);
1692  }
1693  if (width < height) {
1694  if (width + width == height) {
1695  switch (width) {
1696  case 4: return TX_4X8; break;
1697  case 8: return TX_8X16; break;
1698  case 16: return TX_16X32; break;
1699  case 32: return TX_32X64; break;
1700  }
1701  } else {
1702  switch (width) {
1703  case 4: return TX_4X16; break;
1704  case 8: return TX_8X32; break;
1705  case 16: return TX_16X64; break;
1706  }
1707  }
1708  } else {
1709  if (height + height == width) {
1710  switch (height) {
1711  case 4: return TX_8X4; break;
1712  case 8: return TX_16X8; break;
1713  case 16: return TX_32X16; break;
1714  case 32: return TX_64X32; break;
1715  }
1716  } else {
1717  switch (height) {
1718  case 4: return TX_16X4; break;
1719  case 8: return TX_32X8; break;
1720  case 16: return TX_64X16; break;
1721  }
1722  }
1723  }
1724  assert(0);
1725  return TX_4X4;
1726 }
1727 
1728 static INLINE int txfm_partition_context(const TXFM_CONTEXT *const above_ctx,
1729  const TXFM_CONTEXT *const left_ctx,
1730  BLOCK_SIZE bsize, TX_SIZE tx_size) {
1731  const uint8_t txw = tx_size_wide[tx_size];
1732  const uint8_t txh = tx_size_high[tx_size];
1733  const int above = *above_ctx < txw;
1734  const int left = *left_ctx < txh;
1735  int category = TXFM_PARTITION_CONTEXTS;
1736 
1737  // dummy return, not used by others.
1738  if (tx_size <= TX_4X4) return 0;
1739 
1740  TX_SIZE max_tx_size =
1741  get_sqr_tx_size(AOMMAX(block_size_wide[bsize], block_size_high[bsize]));
1742 
1743  if (max_tx_size >= TX_8X8) {
1744  category =
1745  (txsize_sqr_up_map[tx_size] != max_tx_size && max_tx_size > TX_8X8) +
1746  (TX_SIZES - 1 - max_tx_size) * 2;
1747  }
1748  assert(category != TXFM_PARTITION_CONTEXTS);
1749  return category * 3 + above + left;
1750 }
1751 
1752 // Compute the next partition in the direction of the sb_type stored in the mi
1753 // array, starting with bsize.
1754 static INLINE PARTITION_TYPE get_partition(const AV1_COMMON *const cm,
1755  int mi_row, int mi_col,
1756  BLOCK_SIZE bsize) {
1757  const CommonModeInfoParams *const mi_params = &cm->mi_params;
1758  if (mi_row >= mi_params->mi_rows || mi_col >= mi_params->mi_cols)
1759  return PARTITION_INVALID;
1760 
1761  const int offset = mi_row * mi_params->mi_stride + mi_col;
1762  MB_MODE_INFO **mi = mi_params->mi_grid_base + offset;
1763  const BLOCK_SIZE subsize = mi[0]->bsize;
1764 
1765  assert(bsize < BLOCK_SIZES_ALL);
1766 
1767  if (subsize == bsize) return PARTITION_NONE;
1768 
1769  const int bhigh = mi_size_high[bsize];
1770  const int bwide = mi_size_wide[bsize];
1771  const int sshigh = mi_size_high[subsize];
1772  const int sswide = mi_size_wide[subsize];
1773 
1774  if (bsize > BLOCK_8X8 && mi_row + bwide / 2 < mi_params->mi_rows &&
1775  mi_col + bhigh / 2 < mi_params->mi_cols) {
1776  // In this case, the block might be using an extended partition
1777  // type.
1778  const MB_MODE_INFO *const mbmi_right = mi[bwide / 2];
1779  const MB_MODE_INFO *const mbmi_below = mi[bhigh / 2 * mi_params->mi_stride];
1780 
1781  if (sswide == bwide) {
1782  // Smaller height but same width. Is PARTITION_HORZ_4, PARTITION_HORZ or
1783  // PARTITION_HORZ_B. To distinguish the latter two, check if the lower
1784  // half was split.
1785  if (sshigh * 4 == bhigh) return PARTITION_HORZ_4;
1786  assert(sshigh * 2 == bhigh);
1787 
1788  if (mbmi_below->bsize == subsize)
1789  return PARTITION_HORZ;
1790  else
1791  return PARTITION_HORZ_B;
1792  } else if (sshigh == bhigh) {
1793  // Smaller width but same height. Is PARTITION_VERT_4, PARTITION_VERT or
1794  // PARTITION_VERT_B. To distinguish the latter two, check if the right
1795  // half was split.
1796  if (sswide * 4 == bwide) return PARTITION_VERT_4;
1797  assert(sswide * 2 == bhigh);
1798 
1799  if (mbmi_right->bsize == subsize)
1800  return PARTITION_VERT;
1801  else
1802  return PARTITION_VERT_B;
1803  } else {
1804  // Smaller width and smaller height. Might be PARTITION_SPLIT or could be
1805  // PARTITION_HORZ_A or PARTITION_VERT_A. If subsize isn't halved in both
1806  // dimensions, we immediately know this is a split (which will recurse to
1807  // get to subsize). Otherwise look down and to the right. With
1808  // PARTITION_VERT_A, the right block will have height bhigh; with
1809  // PARTITION_HORZ_A, the lower block with have width bwide. Otherwise
1810  // it's PARTITION_SPLIT.
1811  if (sswide * 2 != bwide || sshigh * 2 != bhigh) return PARTITION_SPLIT;
1812 
1813  if (mi_size_wide[mbmi_below->bsize] == bwide) return PARTITION_HORZ_A;
1814  if (mi_size_high[mbmi_right->bsize] == bhigh) return PARTITION_VERT_A;
1815 
1816  return PARTITION_SPLIT;
1817  }
1818  }
1819  const int vert_split = sswide < bwide;
1820  const int horz_split = sshigh < bhigh;
1821  const int split_idx = (vert_split << 1) | horz_split;
1822  assert(split_idx != 0);
1823 
1824  static const PARTITION_TYPE base_partitions[4] = {
1825  PARTITION_INVALID, PARTITION_HORZ, PARTITION_VERT, PARTITION_SPLIT
1826  };
1827 
1828  return base_partitions[split_idx];
1829 }
1830 
1831 static INLINE void set_sb_size(SequenceHeader *const seq_params,
1832  BLOCK_SIZE sb_size) {
1833  seq_params->sb_size = sb_size;
1834  seq_params->mib_size = mi_size_wide[seq_params->sb_size];
1835  seq_params->mib_size_log2 = mi_size_wide_log2[seq_params->sb_size];
1836 }
1837 
1838 // Returns true if the frame is fully lossless at the coded resolution.
1839 // Note: If super-resolution is used, such a frame will still NOT be lossless at
1840 // the upscaled resolution.
1841 static INLINE int is_coded_lossless(const AV1_COMMON *cm,
1842  const MACROBLOCKD *xd) {
1843  int coded_lossless = 1;
1844  if (cm->seg.enabled) {
1845  for (int i = 0; i < MAX_SEGMENTS; ++i) {
1846  if (!xd->lossless[i]) {
1847  coded_lossless = 0;
1848  break;
1849  }
1850  }
1851  } else {
1852  coded_lossless = xd->lossless[0];
1853  }
1854  return coded_lossless;
1855 }
1856 
1857 static INLINE int is_valid_seq_level_idx(AV1_LEVEL seq_level_idx) {
1858  return seq_level_idx == SEQ_LEVEL_MAX ||
1859  (seq_level_idx < SEQ_LEVELS &&
1860  // The following levels are currently undefined.
1861  seq_level_idx != SEQ_LEVEL_2_2 && seq_level_idx != SEQ_LEVEL_2_3 &&
1862  seq_level_idx != SEQ_LEVEL_3_2 && seq_level_idx != SEQ_LEVEL_3_3 &&
1863  seq_level_idx != SEQ_LEVEL_4_2 && seq_level_idx != SEQ_LEVEL_4_3 &&
1864  seq_level_idx != SEQ_LEVEL_7_0 && seq_level_idx != SEQ_LEVEL_7_1 &&
1865  seq_level_idx != SEQ_LEVEL_7_2 && seq_level_idx != SEQ_LEVEL_7_3);
1866 }
1867 
1870 #ifdef __cplusplus
1871 } // extern "C"
1872 #endif
1873 
1874 #endif // AOM_AV1_COMMON_AV1_COMMON_INT_H_
bool reduced_tx_set_used
Definition: av1_common_int.h:370
DeltaQInfo delta_q_info
Definition: av1_common_int.h:942
struct scale_factors sf_identity
Definition: av1_common_int.h:838
#define AOM_PLANE_U
Definition: aom_image.h:200
bool allow_warped_motion
Definition: av1_common_int.h:353
CdefInfo cdef_info
Definition: av1_common_int.h:932
YV12_BUFFER_CONFIG rst_frame
Definition: av1_common_int.h:926
int qmatrix_level_u
Definition: av1_common_int.h:677
int log2_cols
Definition: av1_common_int.h:425
int u_ac_delta_q
Definition: av1_common_int.h:607
TPL_MV_REF * tpl_mvs
Definition: av1_common_int.h:999
int show_frame
Definition: av1_common_int.h:862
int max_width_sb
Definition: av1_common_int.h:406
int spatial_layer_id
Definition: av1_common_int.h:1034
int mb_to_left_edge
Definition: blockd.h:675
bool allow_ref_frame_mvs
Definition: av1_common_int.h:357
const qm_val_t * u_iqmatrix[8][TX_SIZES_ALL]
Definition: av1_common_int.h:656
RefCntBuffer * prev_frame
Definition: av1_common_int.h:803
uint8_t superres_scale_denominator
Definition: av1_common_int.h:780
The coded data for this stream is corrupt or incomplete.
Definition: aom_codec.h:195
struct loopfilter lf
Definition: av1_common_int.h:916
int mi_row
Definition: blockd.h:573
FeatureFlags features
Definition: av1_common_int.h:882
int min_log2_cols
Definition: av1_common_int.h:434
int cdef_damping
Definition: av1_common_int.h:195
const qm_val_t * gqmatrix[(1<< 4)][3][TX_SIZES_ALL]
Definition: av1_common_int.h:642
int min_inner_width
Definition: av1_common_int.h:412
TXFM_CONTEXT left_txfm_context_buffer[MAX_MIB_SIZE]
Definition: blockd.h:745
bool is_chroma_ref
Definition: blockd.h:599
TXFM_CONTEXT ** txfm
Definition: av1_common_int.h:709
struct macroblockd_plane plane[3]
Definition: blockd.h:604
TX_TYPE * tx_type_map
Definition: av1_common_int.h:551
void(* free_mi)(struct CommonModeInfoParams *mi_params)
Definition: av1_common_int.h:561
TX_MODE tx_mode
Definition: av1_common_int.h:382
FRAME_CONTEXT * fc
Definition: av1_common_int.h:958
int cdef_bits
Definition: av1_common_int.h:200
enum aom_color_primaries aom_color_primaries_t
List of supported color primaries.
uint8_t * tx_type_map
Definition: blockd.h:664
int primary_ref_frame
Definition: av1_common_int.h:388
enum aom_matrix_coefficients aom_matrix_coefficients_t
List of supported matrix coefficients.
int mi_grid_size
Definition: av1_common_int.h:539
aom_film_grain_t film_grain_params
Definition: av1_common_int.h:937
External frame buffer.
Definition: aom_frame_buffer.h:40
bool using_qmatrix
Definition: av1_common_int.h:669
ENTROPY_CONTEXT ** entropy[3]
Definition: av1_common_int.h:701
MB_MODE_INFO ** mi_grid_base
Definition: av1_common_int.h:535
int remapped_ref_idx[REF_FRAMES]
Definition: av1_common_int.h:831
bool left_available
Definition: blockd.h:624
TXFM_CONTEXT * left_txfm_context
Definition: blockd.h:738
Contexts used for transmitting various symbols in the bitstream.
Definition: av1_common_int.h:686
int superres_upscaled_height
Definition: av1_common_int.h:773
bool is_first_horizontal_rect
Definition: blockd.h:790
int num_tile_rows
Definition: av1_common_int.h:716
int(* aom_get_frame_buffer_cb_fn_t)(void *priv, size_t min_size, aom_codec_frame_buffer_t *fb)
get frame buffer callback prototype
Definition: aom_frame_buffer.h:64
int mi_cols
Definition: av1_common_int.h:503
CommonQuantParams quant_params
Definition: av1_common_int.h:899
FRAME_CONTEXT * default_frame_context
Definition: av1_common_int.h:964
bool all_lossless
Definition: av1_common_int.h:365
bool switchable_motion_mode
Definition: av1_common_int.h:381
int mb_to_right_edge
Definition: blockd.h:676
int lossless[8]
Definition: blockd.h:815
bool up_available
Definition: blockd.h:620
int current_frame_id
Definition: av1_common_int.h:988
int16_t y_dequant_QTX[8][2]
Definition: av1_common_int.h:626
int(* aom_release_frame_buffer_cb_fn_t)(void *priv, aom_codec_frame_buffer_t *fb)
release frame buffer callback prototype
Definition: aom_frame_buffer.h:77
int u_dc_delta_q
Definition: av1_common_int.h:597
int nb_cdef_strengths
Definition: av1_common_int.h:196
struct aom_internal_error_info * error_info
Definition: blockd.h:836
PARTITION_CONTEXT * above_partition_context
Definition: blockd.h:716
bool allow_intrabc
Definition: av1_common_int.h:352
int y_dc_delta_q
Definition: av1_common_int.h:592
CommonModeInfoParams mi_params
Definition: av1_common_int.h:887
int mi_col
Definition: blockd.h:574
RestorationInfo rst_info[3]
Definition: av1_common_int.h:923
const qm_val_t * v_iqmatrix[8][TX_SIZES_ALL]
Definition: av1_common_int.h:660
MB_MODE_INFO * mi_alloc
Definition: av1_common_int.h:511
MB_MODE_INFO * chroma_left_mbmi
Definition: blockd.h:650
int min_log2
Definition: av1_common_int.h:450
bool allow_screen_content_tools
Definition: av1_common_int.h:351
int mi_alloc_size
Definition: av1_common_int.h:515
int v_ac_delta_q
Definition: av1_common_int.h:612
int height
Definition: av1_common_int.h:428
int showable_frame
Definition: av1_common_int.h:870
int mb_cols
Definition: av1_common_int.h:487
int width
Definition: av1_common_int.h:748
int16_t u_dequant_QTX[8][2]
Definition: av1_common_int.h:627
int tpl_mvs_mem_size
Definition: av1_common_int.h:1003
RefCntBuffer * ref_frame_map[REF_FRAMES]
Definition: av1_common_int.h:855
const qm_val_t * giqmatrix[(1<< 4)][3][TX_SIZES_ALL]
Definition: av1_common_int.h:638
REFRESH_FRAME_CONTEXT_MODE refresh_frame_context
Definition: av1_common_int.h:397
ENTROPY_CONTEXT * above_entropy_context[3]
Definition: blockd.h:701
Params related to MB_MODE_INFO arrays and related info.
Definition: av1_common_int.h:477
int render_height
Definition: av1_common_int.h:760
bool coded_lossless
Definition: av1_common_int.h:361
int log2_rows
Definition: av1_common_int.h:426
Frame level features.
Definition: av1_common_int.h:334
int ref_frame_id[REF_FRAMES]
Definition: av1_common_int.h:989
int v_dc_delta_q
Definition: av1_common_int.h:601
Parameters related to quantization at the frame level.
Definition: av1_common_int.h:582
Parameters related to Restoration Info.
Definition: restoration.h:255
int mb_rows
Definition: av1_common_int.h:482
int col_start_sb[MAX_TILE_COLS+1]
Definition: av1_common_int.h:455
int mb_to_bottom_edge
Definition: blockd.h:678
CommonContexts above_contexts
Definition: av1_common_int.h:982
bool allow_high_precision_mv
Definition: av1_common_int.h:343
WarpedMotionParams global_motion[REF_FRAMES]
Definition: av1_common_int.h:947
MB_MODE_INFO * above_mbmi
Definition: blockd.h:643
loop_filter_info_n lf_info
Definition: av1_common_int.h:915
BufferPool * buffer_pool
Definition: av1_common_int.h:974
bool cur_frame_force_integer_mv
Definition: av1_common_int.h:347
int mi_rows
Definition: av1_common_int.h:498
bool chroma_left_available
Definition: blockd.h:632
void(* set_mb_mi)(struct CommonModeInfoParams *mi_params, int width, int height)
Definition: av1_common_int.h:573
uint8_t width
Definition: blockd.h:763
CommonTileParams tiles
Definition: av1_common_int.h:969
CurrentFrame current_frame
Definition: av1_common_int.h:727
int render_width
Definition: av1_common_int.h:759
int uniform_spacing
Definition: av1_common_int.h:419
YV12 frame buffer data structure.
Definition: yv12config.h:38
CFL_CTX cfl
Definition: blockd.h:892
RestorationLineBuffers * rlbs
Definition: av1_common_int.h:925
bool is_last_vertical_rect
Definition: blockd.h:785
unsigned int number_spatial_layers
Definition: av1_common_int.h:1029
int32_t * rst_tmpbuf
Definition: av1_common_int.h:924
SequenceHeader seq_params
Definition: av1_common_int.h:953
Parameters related to CDEF.
Definition: av1_common_int.h:194
int min_log2_rows
Definition: av1_common_int.h:438
struct scale_factors ref_scale_factors[REF_FRAMES]
Definition: av1_common_int.h:846
MB_MODE_INFO ** mi
Definition: blockd.h:615
ENTROPY_CONTEXT left_entropy_context[3][MAX_MIB_SIZE]
Definition: blockd.h:708
RefCntBuffer * cur_frame
Definition: av1_common_int.h:809
Variables related to current coding block.
Definition: blockd.h:568
Params related to tiles.
Definition: av1_common_int.h:403
int num_mi_cols
Definition: av1_common_int.h:717
int rows
Definition: av1_common_int.h:405
int mi_alloc_stride
Definition: av1_common_int.h:519
int max_log2_cols
Definition: av1_common_int.h:442
int temporal_layer_id
Definition: av1_common_int.h:1024
bool chroma_up_available
Definition: blockd.h:628
uint32_t buffer_removal_times[(8 *4)+1]
Definition: av1_common_int.h:792
int cols
Definition: av1_common_int.h:404
int mi_stride
Definition: av1_common_int.h:543
uint8_t height
Definition: blockd.h:764
enum aom_bit_depth aom_bit_depth_t
Bit depth for codecThis enumeration determines the bit depth of the codec.
int byte_alignment
Definition: av1_common_int.h:392
int16_t v_dequant_QTX[8][2]
Definition: av1_common_int.h:628
struct aom_internal_error_info error
Definition: av1_common_int.h:731
bool buffer_removal_time_present
Definition: av1_common_int.h:785
uint8_t * last_frame_seg_map
Definition: av1_common_int.h:909
Top level common structure used by both encoder and decoder.
Definition: av1_common_int.h:723
enum aom_chroma_sample_position aom_chroma_sample_position_t
List of chroma sample positions.
BLOCK_SIZE bsize
The block size of the current coding block.
Definition: blockd.h:222
InterpFilter interp_filter
Definition: av1_common_int.h:383
Stores the prediction/txfm mode of the current coding block.
Definition: blockd.h:216
int row_start_sb[MAX_TILE_ROWS+1]
Definition: av1_common_int.h:460
struct segmentation seg
Definition: av1_common_int.h:904
unsigned int large_scale
Definition: av1_common_int.h:464
bool disable_cdf_update
Definition: av1_common_int.h:338
int superres_upscaled_width
Definition: av1_common_int.h:772
int tx_type_map_stride
Definition: blockd.h:669
int qmatrix_level_y
Definition: av1_common_int.h:676
BLOCK_SIZE mi_alloc_bsize
Definition: av1_common_int.h:526
int max_log2_rows
Definition: av1_common_int.h:446
int base_qindex
Definition: av1_common_int.h:586
TXFM_CONTEXT * above_txfm_context
Definition: blockd.h:731
const qm_val_t * y_iqmatrix[8][TX_SIZES_ALL]
Definition: av1_common_int.h:652
MB_MODE_INFO * left_mbmi
Definition: blockd.h:638
int max_height_sb
Definition: av1_common_int.h:407
MB_MODE_INFO * chroma_above_mbmi
Definition: blockd.h:657
int mb_to_top_edge
Definition: blockd.h:677
enum aom_transfer_characteristics aom_transfer_characteristics_t
List of supported transfer functions.
int width
Definition: av1_common_int.h:427
int8_t ref_frame_side[REF_FRAMES]
Definition: av1_common_int.h:1014
int qmatrix_level_v
Definition: av1_common_int.h:678
unsigned int number_temporal_layers
Definition: av1_common_int.h:1019
int mi_stride
Definition: blockd.h:580
int height
Definition: av1_common_int.h:749
bool error_resilient_mode
Definition: av1_common_int.h:376
int ref_frame_sign_bias[REF_FRAMES]
Definition: av1_common_int.h:1008
int MBs
Definition: av1_common_int.h:492
int num_planes
Definition: av1_common_int.h:715
void(* setup_mi)(struct CommonModeInfoParams *mi_params)
Definition: av1_common_int.h:566
PARTITION_CONTEXT left_partition_context[MAX_MIB_SIZE]
Definition: blockd.h:723
uint32_t frame_presentation_time
Definition: av1_common_int.h:798
PARTITION_CONTEXT ** partition
Definition: av1_common_int.h:691
unsigned int single_tile_decoding
Definition: av1_common_int.h:470
int show_existing_frame
Definition: av1_common_int.h:877