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