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  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 {
199  int cdef_strengths[CDEF_MAX_STRENGTHS];
200  int cdef_uv_strengths[CDEF_MAX_STRENGTHS];
202  int cdef_bits;
203 } CdefInfo;
204 
207 typedef struct {
208  int delta_q_present_flag;
209  // Resolution of delta quant
210  int delta_q_res;
211  int delta_lf_present_flag;
212  // Resolution of delta lf level
213  int delta_lf_res;
214  // This is a flag for number of deltas of loop filter level
215  // 0: use 1 delta, for y_vertical, y_horizontal, u, and v
216  // 1: use separate deltas for each filter level
217  int delta_lf_multi;
218 } DeltaQInfo;
219 
220 typedef struct {
221  int enable_order_hint; // 0 - disable order hint, and related tools
222  int order_hint_bits_minus_1; // dist_wtd_comp, ref_frame_mvs,
223  // frame_sign_bias
224  // if 0, enable_dist_wtd_comp and
225  // enable_ref_frame_mvs must be set as 0.
226  int enable_dist_wtd_comp; // 0 - disable dist-wtd compound modes
227  // 1 - enable it
228  int enable_ref_frame_mvs; // 0 - disable ref frame mvs
229  // 1 - enable it
230 } OrderHintInfo;
231 
232 // Sequence header structure.
233 // Note: All syntax elements of sequence_header_obu that need to be
234 // bit-identical across multiple sequence headers must be part of this struct,
235 // so that consistency is checked by are_seq_headers_consistent() function.
236 // One exception is the last member 'op_params' that is ignored by
237 // are_seq_headers_consistent() function.
238 typedef struct SequenceHeader {
239  int num_bits_width;
240  int num_bits_height;
241  int max_frame_width;
242  int max_frame_height;
243  // Whether current and reference frame IDs are signaled in the bitstream.
244  // Frame id numbers are additional information that do not affect the
245  // decoding process, but provide decoders with a way of detecting missing
246  // reference frames so that appropriate action can be taken.
247  uint8_t frame_id_numbers_present_flag;
248  int frame_id_length;
249  int delta_frame_id_length;
250  BLOCK_SIZE sb_size; // Size of the superblock used for this frame
251  int mib_size; // Size of the superblock in units of MI blocks
252  int mib_size_log2; // Log 2 of above.
253 
254  OrderHintInfo order_hint_info;
255 
256  uint8_t force_screen_content_tools; // 0 - force off
257  // 1 - force on
258  // 2 - adaptive
259  uint8_t still_picture; // Video is a single frame still picture
260  uint8_t reduced_still_picture_hdr; // Use reduced header for still picture
261  uint8_t force_integer_mv; // 0 - Don't force. MV can use subpel
262  // 1 - force to integer
263  // 2 - adaptive
264  uint8_t enable_filter_intra; // enables/disables filterintra
265  uint8_t enable_intra_edge_filter; // enables/disables edge upsampling
266  uint8_t enable_interintra_compound; // enables/disables interintra_compound
267  uint8_t enable_masked_compound; // enables/disables masked compound
268  uint8_t enable_dual_filter; // 0 - disable dual interpolation filter
269  // 1 - enable vert/horz filter selection
270  uint8_t enable_warped_motion; // 0 - disable warp for the sequence
271  // 1 - enable warp for the sequence
272  uint8_t enable_superres; // 0 - Disable superres for the sequence
273  // and no frame level superres flag
274  // 1 - Enable superres for the sequence
275  // enable per-frame superres flag
276  uint8_t enable_cdef; // To turn on/off CDEF
277  uint8_t enable_restoration; // To turn on/off loop restoration
278  BITSTREAM_PROFILE profile;
279 
280  // Color config.
281  aom_bit_depth_t bit_depth; // AOM_BITS_8 in profile 0 or 1,
282  // AOM_BITS_10 or AOM_BITS_12 in profile 2 or 3.
283  uint8_t use_highbitdepth; // If true, we need to use 16bit frame buffers.
284  uint8_t monochrome; // Monochorme video
285  aom_color_primaries_t color_primaries;
286  aom_transfer_characteristics_t transfer_characteristics;
287  aom_matrix_coefficients_t matrix_coefficients;
288  int color_range;
289  int subsampling_x; // Chroma subsampling for x
290  int subsampling_y; // Chroma subsampling for y
291  aom_chroma_sample_position_t chroma_sample_position;
292  uint8_t separate_uv_delta_q;
293  uint8_t film_grain_params_present;
294 
295  // Operating point info.
296  int operating_points_cnt_minus_1;
297  int operating_point_idc[MAX_NUM_OPERATING_POINTS];
298  int timing_info_present;
299  aom_timing_info_t timing_info;
300  uint8_t decoder_model_info_present_flag;
301  aom_dec_model_info_t decoder_model_info;
302  uint8_t display_model_info_present_flag;
303  AV1_LEVEL seq_level_idx[MAX_NUM_OPERATING_POINTS];
304  uint8_t tier[MAX_NUM_OPERATING_POINTS]; // seq_tier in spec. One bit: 0 or 1.
305 
306  // IMPORTANT: the op_params member must be at the end of the struct so that
307  // are_seq_headers_consistent() can be implemented with a memcmp() call.
308  // TODO(urvang): We probably don't need the +1 here.
309  aom_dec_model_op_parameters_t op_params[MAX_NUM_OPERATING_POINTS + 1];
310 } SequenceHeader;
311 
312 typedef struct {
313  int skip_mode_allowed;
314  int skip_mode_flag;
315  int ref_frame_idx_0;
316  int ref_frame_idx_1;
317 } SkipModeInfo;
318 
319 typedef struct {
320  FRAME_TYPE frame_type;
321  REFERENCE_MODE reference_mode;
322 
323  unsigned int order_hint;
324  unsigned int display_order_hint;
325  unsigned int frame_number;
326  SkipModeInfo skip_mode_info;
327  int refresh_frame_flags; // Which ref frames are overwritten by this frame
328  int frame_refs_short_signaling;
329 } CurrentFrame;
330 
336 typedef struct {
384  TX_MODE tx_mode;
385  InterpFilter interp_filter;
399  REFRESH_FRAME_CONTEXT_MODE refresh_frame_context;
400 } FeatureFlags;
401 
405 typedef struct CommonTileParams {
406  int cols;
407  int rows;
415 
422 
427  int log2_cols;
428  int log2_rows;
429  int width;
430  int height;
452  int min_log2;
457  int col_start_sb[MAX_TILE_COLS + 1];
462  int row_start_sb[MAX_TILE_ROWS + 1];
466  unsigned int large_scale;
472  unsigned int single_tile_decoding;
474 
484  int mb_rows;
489  int mb_cols;
490 
494  int MBs;
495 
500  int mi_rows;
505  int mi_cols;
506 
528  BLOCK_SIZE mi_alloc_bsize;
529 
546 
553  TX_TYPE *tx_type_map;
554 
563  void (*free_mi)(struct CommonModeInfoParams *mi_params);
568  void (*setup_mi)(struct CommonModeInfoParams *mi_params);
575  void (*set_mb_mi)(struct CommonModeInfoParams *mi_params, int width,
576  int height);
578 };
579 
580 typedef struct CommonQuantParams CommonQuantParams;
589 
595 
604 
615 
616  /*
617  * Note: The qindex per superblock may have a delta from the qindex obtained
618  * at frame level from parameters above, based on 'cm->delta_q_info'.
619  */
620 
628  int16_t y_dequant_QTX[MAX_SEGMENTS][2];
629  int16_t u_dequant_QTX[MAX_SEGMENTS][2];
630  int16_t v_dequant_QTX[MAX_SEGMENTS][2];
640  const qm_val_t *giqmatrix[NUM_QM_LEVELS][3][TX_SIZES_ALL];
644  const qm_val_t *gqmatrix[NUM_QM_LEVELS][3][TX_SIZES_ALL];
654  const qm_val_t *y_iqmatrix[MAX_SEGMENTS][TX_SIZES_ALL];
658  const qm_val_t *u_iqmatrix[MAX_SEGMENTS][TX_SIZES_ALL];
662  const qm_val_t *v_iqmatrix[MAX_SEGMENTS][TX_SIZES_ALL];
682 };
683 
684 typedef struct CommonContexts CommonContexts;
693  PARTITION_CONTEXT **partition;
694 
703  ENTROPY_CONTEXT **entropy[MAX_MB_PLANE];
704 
711  TXFM_CONTEXT **txfm;
712 
720 };
721 
725 typedef struct AV1Common {
729  CurrentFrame current_frame;
733  struct aom_internal_error_info error;
734 
750  int width;
751  int height;
783 
794  uint32_t buffer_removal_times[MAX_NUM_OPERATING_POINTS + 1];
801 
805  RefCntBuffer *prev_frame;
806 
811  RefCntBuffer *cur_frame;
812 
833  int remapped_ref_idx[REF_FRAMES];
834 
840  struct scale_factors sf_identity;
841 
848  struct scale_factors ref_scale_factors[REF_FRAMES];
849 
857  RefCntBuffer *ref_frame_map[REF_FRAMES];
858 
865 
873 
880 
885 
890 
891 #if CONFIG_ENTROPY_STATS
895  int coef_cdf_category;
896 #endif // CONFIG_ENTROPY_STATS
897 
902 
906  struct segmentation seg;
907 
912 
917  loop_filter_info_n lf_info;
918  struct loopfilter lf;
925  RestorationInfo rst_info[MAX_MB_PLANE];
926  int32_t *rst_tmpbuf;
927  RestorationLineBuffers *rlbs;
935 
939  aom_film_grain_t film_grain_params;
940 
944  DeltaQInfo delta_q_info;
945 
949  WarpedMotionParams global_motion[REF_FRAMES];
950 
955  SequenceHeader seq_params;
956 
960  FRAME_CONTEXT *fc;
966  FRAME_CONTEXT *default_frame_context;
967 
972 
976  BufferPool *buffer_pool;
977 
985 
991  int ref_frame_id[REF_FRAMES];
1001  TPL_MV_REF *tpl_mvs;
1010  int ref_frame_sign_bias[REF_FRAMES];
1016  int8_t ref_frame_side[REF_FRAMES];
1017 
1027 
1037 
1038 #if TXCOEFF_TIMER
1039  int64_t cum_txcoeff_timer;
1040  int64_t txcoeff_timer;
1041  int txb_count;
1042 #endif // TXCOEFF_TIMER
1043 
1044 #if TXCOEFF_COST_TIMER
1045  int64_t cum_txcoeff_cost_timer;
1046  int64_t txcoeff_cost_timer;
1047  int64_t txcoeff_cost_count;
1048 #endif // TXCOEFF_COST_TIMER
1049 
1050 #if CONFIG_LPF_MASK
1051  int is_decoding;
1052 #endif // CONFIG_LPF_MASK
1053 } AV1_COMMON;
1054 
1057 // TODO(hkuang): Don't need to lock the whole pool after implementing atomic
1058 // frame reference count.
1059 static void lock_buffer_pool(BufferPool *const pool) {
1060 #if CONFIG_MULTITHREAD
1061  pthread_mutex_lock(&pool->pool_mutex);
1062 #else
1063  (void)pool;
1064 #endif
1065 }
1066 
1067 static void unlock_buffer_pool(BufferPool *const pool) {
1068 #if CONFIG_MULTITHREAD
1069  pthread_mutex_unlock(&pool->pool_mutex);
1070 #else
1071  (void)pool;
1072 #endif
1073 }
1074 
1075 static INLINE YV12_BUFFER_CONFIG *get_ref_frame(AV1_COMMON *cm, int index) {
1076  if (index < 0 || index >= REF_FRAMES) return NULL;
1077  if (cm->ref_frame_map[index] == NULL) return NULL;
1078  return &cm->ref_frame_map[index]->buf;
1079 }
1080 
1081 static INLINE int get_free_fb(AV1_COMMON *cm) {
1082  RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
1083  int i;
1084 
1085  lock_buffer_pool(cm->buffer_pool);
1086  for (i = 0; i < FRAME_BUFFERS; ++i)
1087  if (frame_bufs[i].ref_count == 0) break;
1088 
1089  if (i != FRAME_BUFFERS) {
1090  if (frame_bufs[i].buf.use_external_reference_buffers) {
1091  // If this frame buffer's y_buffer, u_buffer, and v_buffer point to the
1092  // external reference buffers. Restore the buffer pointers to point to the
1093  // internally allocated memory.
1094  YV12_BUFFER_CONFIG *ybf = &frame_bufs[i].buf;
1095  ybf->y_buffer = ybf->store_buf_adr[0];
1096  ybf->u_buffer = ybf->store_buf_adr[1];
1097  ybf->v_buffer = ybf->store_buf_adr[2];
1098  ybf->use_external_reference_buffers = 0;
1099  }
1100 
1101  frame_bufs[i].ref_count = 1;
1102  } else {
1103  // We should never run out of free buffers. If this assertion fails, there
1104  // is a reference leak.
1105  assert(0 && "Ran out of free frame buffers. Likely a reference leak.");
1106  // Reset i to be INVALID_IDX to indicate no free buffer found.
1107  i = INVALID_IDX;
1108  }
1109 
1110  unlock_buffer_pool(cm->buffer_pool);
1111  return i;
1112 }
1113 
1114 static INLINE RefCntBuffer *assign_cur_frame_new_fb(AV1_COMMON *const cm) {
1115  // Release the previously-used frame-buffer
1116  if (cm->cur_frame != NULL) {
1117  --cm->cur_frame->ref_count;
1118  cm->cur_frame = NULL;
1119  }
1120 
1121  // Assign a new framebuffer
1122  const int new_fb_idx = get_free_fb(cm);
1123  if (new_fb_idx == INVALID_IDX) return NULL;
1124 
1125  cm->cur_frame = &cm->buffer_pool->frame_bufs[new_fb_idx];
1126  cm->cur_frame->buf.buf_8bit_valid = 0;
1127  av1_zero(cm->cur_frame->interp_filter_selected);
1128  return cm->cur_frame;
1129 }
1130 
1131 // Modify 'lhs_ptr' to reference the buffer at 'rhs_ptr', and update the ref
1132 // counts accordingly.
1133 static INLINE void assign_frame_buffer_p(RefCntBuffer **lhs_ptr,
1134  RefCntBuffer *rhs_ptr) {
1135  RefCntBuffer *const old_ptr = *lhs_ptr;
1136  if (old_ptr != NULL) {
1137  assert(old_ptr->ref_count > 0);
1138  // One less reference to the buffer at 'old_ptr', so decrease ref count.
1139  --old_ptr->ref_count;
1140  }
1141 
1142  *lhs_ptr = rhs_ptr;
1143  // One more reference to the buffer at 'rhs_ptr', so increase ref count.
1144  ++rhs_ptr->ref_count;
1145 }
1146 
1147 static INLINE int frame_is_intra_only(const AV1_COMMON *const cm) {
1148  return cm->current_frame.frame_type == KEY_FRAME ||
1149  cm->current_frame.frame_type == INTRA_ONLY_FRAME;
1150 }
1151 
1152 static INLINE int frame_is_sframe(const AV1_COMMON *cm) {
1153  return cm->current_frame.frame_type == S_FRAME;
1154 }
1155 
1156 // These functions take a reference frame label between LAST_FRAME and
1157 // EXTREF_FRAME inclusive. Note that this is different to the indexing
1158 // previously used by the frame_refs[] array.
1159 static INLINE int get_ref_frame_map_idx(const AV1_COMMON *const cm,
1160  const MV_REFERENCE_FRAME ref_frame) {
1161  return (ref_frame >= LAST_FRAME && ref_frame <= EXTREF_FRAME)
1162  ? cm->remapped_ref_idx[ref_frame - LAST_FRAME]
1163  : INVALID_IDX;
1164 }
1165 
1166 static INLINE RefCntBuffer *get_ref_frame_buf(
1167  const AV1_COMMON *const cm, const MV_REFERENCE_FRAME ref_frame) {
1168  const int map_idx = get_ref_frame_map_idx(cm, ref_frame);
1169  return (map_idx != INVALID_IDX) ? cm->ref_frame_map[map_idx] : NULL;
1170 }
1171 
1172 // Both const and non-const versions of this function are provided so that it
1173 // can be used with a const AV1_COMMON if needed.
1174 static INLINE const struct scale_factors *get_ref_scale_factors_const(
1175  const AV1_COMMON *const cm, const MV_REFERENCE_FRAME ref_frame) {
1176  const int map_idx = get_ref_frame_map_idx(cm, ref_frame);
1177  return (map_idx != INVALID_IDX) ? &cm->ref_scale_factors[map_idx] : NULL;
1178 }
1179 
1180 static INLINE struct scale_factors *get_ref_scale_factors(
1181  AV1_COMMON *const cm, const MV_REFERENCE_FRAME ref_frame) {
1182  const int map_idx = get_ref_frame_map_idx(cm, ref_frame);
1183  return (map_idx != INVALID_IDX) ? &cm->ref_scale_factors[map_idx] : NULL;
1184 }
1185 
1186 static INLINE RefCntBuffer *get_primary_ref_frame_buf(
1187  const AV1_COMMON *const cm) {
1188  const int primary_ref_frame = cm->features.primary_ref_frame;
1189  if (primary_ref_frame == PRIMARY_REF_NONE) return NULL;
1190  const int map_idx = get_ref_frame_map_idx(cm, primary_ref_frame + 1);
1191  return (map_idx != INVALID_IDX) ? cm->ref_frame_map[map_idx] : NULL;
1192 }
1193 
1194 // Returns 1 if this frame might allow mvs from some reference frame.
1195 static INLINE int frame_might_allow_ref_frame_mvs(const AV1_COMMON *cm) {
1196  return !cm->features.error_resilient_mode &&
1197  cm->seq_params.order_hint_info.enable_ref_frame_mvs &&
1198  cm->seq_params.order_hint_info.enable_order_hint &&
1199  !frame_is_intra_only(cm);
1200 }
1201 
1202 // Returns 1 if this frame might use warped_motion
1203 static INLINE int frame_might_allow_warped_motion(const AV1_COMMON *cm) {
1204  return !cm->features.error_resilient_mode && !frame_is_intra_only(cm) &&
1205  cm->seq_params.enable_warped_motion;
1206 }
1207 
1208 static INLINE void ensure_mv_buffer(RefCntBuffer *buf, AV1_COMMON *cm) {
1209  const int buf_rows = buf->mi_rows;
1210  const int buf_cols = buf->mi_cols;
1211  const CommonModeInfoParams *const mi_params = &cm->mi_params;
1212 
1213  if (buf->mvs == NULL || buf_rows != mi_params->mi_rows ||
1214  buf_cols != mi_params->mi_cols) {
1215  aom_free(buf->mvs);
1216  buf->mi_rows = mi_params->mi_rows;
1217  buf->mi_cols = mi_params->mi_cols;
1218  CHECK_MEM_ERROR(cm, buf->mvs,
1219  (MV_REF *)aom_calloc(((mi_params->mi_rows + 1) >> 1) *
1220  ((mi_params->mi_cols + 1) >> 1),
1221  sizeof(*buf->mvs)));
1222  aom_free(buf->seg_map);
1223  CHECK_MEM_ERROR(
1224  cm, buf->seg_map,
1225  (uint8_t *)aom_calloc(mi_params->mi_rows * mi_params->mi_cols,
1226  sizeof(*buf->seg_map)));
1227  }
1228 
1229  const int mem_size =
1230  ((mi_params->mi_rows + MAX_MIB_SIZE) >> 1) * (mi_params->mi_stride >> 1);
1231  int realloc = cm->tpl_mvs == NULL;
1232  if (cm->tpl_mvs) realloc |= cm->tpl_mvs_mem_size < mem_size;
1233 
1234  if (realloc) {
1235  aom_free(cm->tpl_mvs);
1236  CHECK_MEM_ERROR(cm, cm->tpl_mvs,
1237  (TPL_MV_REF *)aom_calloc(mem_size, sizeof(*cm->tpl_mvs)));
1238  cm->tpl_mvs_mem_size = mem_size;
1239  }
1240 }
1241 
1242 void cfl_init(CFL_CTX *cfl, const SequenceHeader *seq_params);
1243 
1244 static INLINE int av1_num_planes(const AV1_COMMON *cm) {
1245  return cm->seq_params.monochrome ? 1 : MAX_MB_PLANE;
1246 }
1247 
1248 static INLINE void av1_init_above_context(CommonContexts *above_contexts,
1249  int num_planes, int tile_row,
1250  MACROBLOCKD *xd) {
1251  for (int i = 0; i < num_planes; ++i) {
1252  xd->above_entropy_context[i] = above_contexts->entropy[i][tile_row];
1253  }
1254  xd->above_partition_context = above_contexts->partition[tile_row];
1255  xd->above_txfm_context = above_contexts->txfm[tile_row];
1256 }
1257 
1258 static INLINE void av1_init_macroblockd(AV1_COMMON *cm, MACROBLOCKD *xd) {
1259  const int num_planes = av1_num_planes(cm);
1260  const CommonQuantParams *const quant_params = &cm->quant_params;
1261 
1262  for (int i = 0; i < num_planes; ++i) {
1263  if (xd->plane[i].plane_type == PLANE_TYPE_Y) {
1264  memcpy(xd->plane[i].seg_dequant_QTX, quant_params->y_dequant_QTX,
1265  sizeof(quant_params->y_dequant_QTX));
1266  memcpy(xd->plane[i].seg_iqmatrix, quant_params->y_iqmatrix,
1267  sizeof(quant_params->y_iqmatrix));
1268 
1269  } else {
1270  if (i == AOM_PLANE_U) {
1271  memcpy(xd->plane[i].seg_dequant_QTX, quant_params->u_dequant_QTX,
1272  sizeof(quant_params->u_dequant_QTX));
1273  memcpy(xd->plane[i].seg_iqmatrix, quant_params->u_iqmatrix,
1274  sizeof(quant_params->u_iqmatrix));
1275  } else {
1276  memcpy(xd->plane[i].seg_dequant_QTX, quant_params->v_dequant_QTX,
1277  sizeof(quant_params->v_dequant_QTX));
1278  memcpy(xd->plane[i].seg_iqmatrix, quant_params->v_iqmatrix,
1279  sizeof(quant_params->v_iqmatrix));
1280  }
1281  }
1282  }
1283  xd->mi_stride = cm->mi_params.mi_stride;
1284  xd->error_info = &cm->error;
1285  cfl_init(&xd->cfl, &cm->seq_params);
1286 }
1287 
1288 static INLINE void set_entropy_context(MACROBLOCKD *xd, int mi_row, int mi_col,
1289  const int num_planes) {
1290  int i;
1291  int row_offset = mi_row;
1292  int col_offset = mi_col;
1293  for (i = 0; i < num_planes; ++i) {
1294  struct macroblockd_plane *const pd = &xd->plane[i];
1295  // Offset the buffer pointer
1296  const BLOCK_SIZE bsize = xd->mi[0]->bsize;
1297  if (pd->subsampling_y && (mi_row & 0x01) && (mi_size_high[bsize] == 1))
1298  row_offset = mi_row - 1;
1299  if (pd->subsampling_x && (mi_col & 0x01) && (mi_size_wide[bsize] == 1))
1300  col_offset = mi_col - 1;
1301  int above_idx = col_offset;
1302  int left_idx = row_offset & MAX_MIB_MASK;
1303  pd->above_entropy_context =
1304  &xd->above_entropy_context[i][above_idx >> pd->subsampling_x];
1305  pd->left_entropy_context =
1306  &xd->left_entropy_context[i][left_idx >> pd->subsampling_y];
1307  }
1308 }
1309 
1310 static INLINE int calc_mi_size(int len) {
1311  // len is in mi units. Align to a multiple of SBs.
1312  return ALIGN_POWER_OF_TWO(len, MAX_MIB_SIZE_LOG2);
1313 }
1314 
1315 static INLINE void set_plane_n4(MACROBLOCKD *const xd, int bw, int bh,
1316  const int num_planes) {
1317  int i;
1318  for (i = 0; i < num_planes; i++) {
1319  xd->plane[i].width = (bw * MI_SIZE) >> xd->plane[i].subsampling_x;
1320  xd->plane[i].height = (bh * MI_SIZE) >> xd->plane[i].subsampling_y;
1321 
1322  xd->plane[i].width = AOMMAX(xd->plane[i].width, 4);
1323  xd->plane[i].height = AOMMAX(xd->plane[i].height, 4);
1324  }
1325 }
1326 
1327 static INLINE void set_mi_row_col(MACROBLOCKD *xd, const TileInfo *const tile,
1328  int mi_row, int bh, int mi_col, int bw,
1329  int mi_rows, int mi_cols) {
1330  xd->mb_to_top_edge = -GET_MV_SUBPEL(mi_row * MI_SIZE);
1331  xd->mb_to_bottom_edge = GET_MV_SUBPEL((mi_rows - bh - mi_row) * MI_SIZE);
1332  xd->mb_to_left_edge = -GET_MV_SUBPEL((mi_col * MI_SIZE));
1333  xd->mb_to_right_edge = GET_MV_SUBPEL((mi_cols - bw - mi_col) * MI_SIZE);
1334 
1335  xd->mi_row = mi_row;
1336  xd->mi_col = mi_col;
1337 
1338  // Are edges available for intra prediction?
1339  xd->up_available = (mi_row > tile->mi_row_start);
1340 
1341  const int ss_x = xd->plane[1].subsampling_x;
1342  const int ss_y = xd->plane[1].subsampling_y;
1343 
1344  xd->left_available = (mi_col > tile->mi_col_start);
1347  if (ss_x && bw < mi_size_wide[BLOCK_8X8])
1348  xd->chroma_left_available = (mi_col - 1) > tile->mi_col_start;
1349  if (ss_y && bh < mi_size_high[BLOCK_8X8])
1350  xd->chroma_up_available = (mi_row - 1) > tile->mi_row_start;
1351  if (xd->up_available) {
1352  xd->above_mbmi = xd->mi[-xd->mi_stride];
1353  } else {
1354  xd->above_mbmi = NULL;
1355  }
1356 
1357  if (xd->left_available) {
1358  xd->left_mbmi = xd->mi[-1];
1359  } else {
1360  xd->left_mbmi = NULL;
1361  }
1362 
1363  const int chroma_ref = ((mi_row & 0x01) || !(bh & 0x01) || !ss_y) &&
1364  ((mi_col & 0x01) || !(bw & 0x01) || !ss_x);
1365  xd->is_chroma_ref = chroma_ref;
1366  if (chroma_ref) {
1367  // To help calculate the "above" and "left" chroma blocks, note that the
1368  // current block may cover multiple luma blocks (eg, if partitioned into
1369  // 4x4 luma blocks).
1370  // First, find the top-left-most luma block covered by this chroma block
1371  MB_MODE_INFO **base_mi =
1372  &xd->mi[-(mi_row & ss_y) * xd->mi_stride - (mi_col & ss_x)];
1373 
1374  // Then, we consider the luma region covered by the left or above 4x4 chroma
1375  // prediction. We want to point to the chroma reference block in that
1376  // region, which is the bottom-right-most mi unit.
1377  // This leads to the following offsets:
1378  MB_MODE_INFO *chroma_above_mi =
1379  xd->chroma_up_available ? base_mi[-xd->mi_stride + ss_x] : NULL;
1380  xd->chroma_above_mbmi = chroma_above_mi;
1381 
1382  MB_MODE_INFO *chroma_left_mi =
1383  xd->chroma_left_available ? base_mi[ss_y * xd->mi_stride - 1] : NULL;
1384  xd->chroma_left_mbmi = chroma_left_mi;
1385  }
1386 
1387  xd->height = bh;
1388  xd->width = bw;
1389 
1390  xd->is_last_vertical_rect = 0;
1391  if (xd->width < xd->height) {
1392  if (!((mi_col + xd->width) & (xd->height - 1))) {
1393  xd->is_last_vertical_rect = 1;
1394  }
1395  }
1396 
1397  xd->is_first_horizontal_rect = 0;
1398  if (xd->width > xd->height)
1399  if (!(mi_row & (xd->width - 1))) xd->is_first_horizontal_rect = 1;
1400 }
1401 
1402 static INLINE aom_cdf_prob *get_y_mode_cdf(FRAME_CONTEXT *tile_ctx,
1403  const MB_MODE_INFO *above_mi,
1404  const MB_MODE_INFO *left_mi) {
1405  const PREDICTION_MODE above = av1_above_block_mode(above_mi);
1406  const PREDICTION_MODE left = av1_left_block_mode(left_mi);
1407  const int above_ctx = intra_mode_context[above];
1408  const int left_ctx = intra_mode_context[left];
1409  return tile_ctx->kf_y_cdf[above_ctx][left_ctx];
1410 }
1411 
1412 static INLINE void update_partition_context(MACROBLOCKD *xd, int mi_row,
1413  int mi_col, BLOCK_SIZE subsize,
1414  BLOCK_SIZE bsize) {
1415  PARTITION_CONTEXT *const above_ctx = xd->above_partition_context + mi_col;
1416  PARTITION_CONTEXT *const left_ctx =
1417  xd->left_partition_context + (mi_row & MAX_MIB_MASK);
1418 
1419  const int bw = mi_size_wide[bsize];
1420  const int bh = mi_size_high[bsize];
1421  memset(above_ctx, partition_context_lookup[subsize].above, bw);
1422  memset(left_ctx, partition_context_lookup[subsize].left, bh);
1423 }
1424 
1425 static INLINE int is_chroma_reference(int mi_row, int mi_col, BLOCK_SIZE bsize,
1426  int subsampling_x, int subsampling_y) {
1427  assert(bsize < BLOCK_SIZES_ALL);
1428  const int bw = mi_size_wide[bsize];
1429  const int bh = mi_size_high[bsize];
1430  int ref_pos = ((mi_row & 0x01) || !(bh & 0x01) || !subsampling_y) &&
1431  ((mi_col & 0x01) || !(bw & 0x01) || !subsampling_x);
1432  return ref_pos;
1433 }
1434 
1435 static INLINE aom_cdf_prob cdf_element_prob(const aom_cdf_prob *cdf,
1436  size_t element) {
1437  assert(cdf != NULL);
1438  return (element > 0 ? cdf[element - 1] : CDF_PROB_TOP) - cdf[element];
1439 }
1440 
1441 static INLINE void partition_gather_horz_alike(aom_cdf_prob *out,
1442  const aom_cdf_prob *const in,
1443  BLOCK_SIZE bsize) {
1444  (void)bsize;
1445  out[0] = CDF_PROB_TOP;
1446  out[0] -= cdf_element_prob(in, PARTITION_HORZ);
1447  out[0] -= cdf_element_prob(in, PARTITION_SPLIT);
1448  out[0] -= cdf_element_prob(in, PARTITION_HORZ_A);
1449  out[0] -= cdf_element_prob(in, PARTITION_HORZ_B);
1450  out[0] -= cdf_element_prob(in, PARTITION_VERT_A);
1451  if (bsize != BLOCK_128X128) out[0] -= cdf_element_prob(in, PARTITION_HORZ_4);
1452  out[0] = AOM_ICDF(out[0]);
1453  out[1] = AOM_ICDF(CDF_PROB_TOP);
1454 }
1455 
1456 static INLINE void partition_gather_vert_alike(aom_cdf_prob *out,
1457  const aom_cdf_prob *const in,
1458  BLOCK_SIZE bsize) {
1459  (void)bsize;
1460  out[0] = CDF_PROB_TOP;
1461  out[0] -= cdf_element_prob(in, PARTITION_VERT);
1462  out[0] -= cdf_element_prob(in, PARTITION_SPLIT);
1463  out[0] -= cdf_element_prob(in, PARTITION_HORZ_A);
1464  out[0] -= cdf_element_prob(in, PARTITION_VERT_A);
1465  out[0] -= cdf_element_prob(in, PARTITION_VERT_B);
1466  if (bsize != BLOCK_128X128) out[0] -= cdf_element_prob(in, PARTITION_VERT_4);
1467  out[0] = AOM_ICDF(out[0]);
1468  out[1] = AOM_ICDF(CDF_PROB_TOP);
1469 }
1470 
1471 static INLINE void update_ext_partition_context(MACROBLOCKD *xd, int mi_row,
1472  int mi_col, BLOCK_SIZE subsize,
1473  BLOCK_SIZE bsize,
1474  PARTITION_TYPE partition) {
1475  if (bsize >= BLOCK_8X8) {
1476  const int hbs = mi_size_wide[bsize] / 2;
1477  BLOCK_SIZE bsize2 = get_partition_subsize(bsize, PARTITION_SPLIT);
1478  switch (partition) {
1479  case PARTITION_SPLIT:
1480  if (bsize != BLOCK_8X8) break;
1481  AOM_FALLTHROUGH_INTENDED;
1482  case PARTITION_NONE:
1483  case PARTITION_HORZ:
1484  case PARTITION_VERT:
1485  case PARTITION_HORZ_4:
1486  case PARTITION_VERT_4:
1487  update_partition_context(xd, mi_row, mi_col, subsize, bsize);
1488  break;
1489  case PARTITION_HORZ_A:
1490  update_partition_context(xd, mi_row, mi_col, bsize2, subsize);
1491  update_partition_context(xd, mi_row + hbs, mi_col, subsize, subsize);
1492  break;
1493  case PARTITION_HORZ_B:
1494  update_partition_context(xd, mi_row, mi_col, subsize, subsize);
1495  update_partition_context(xd, mi_row + hbs, mi_col, bsize2, subsize);
1496  break;
1497  case PARTITION_VERT_A:
1498  update_partition_context(xd, mi_row, mi_col, bsize2, subsize);
1499  update_partition_context(xd, mi_row, mi_col + hbs, subsize, subsize);
1500  break;
1501  case PARTITION_VERT_B:
1502  update_partition_context(xd, mi_row, mi_col, subsize, subsize);
1503  update_partition_context(xd, mi_row, mi_col + hbs, bsize2, subsize);
1504  break;
1505  default: assert(0 && "Invalid partition type");
1506  }
1507  }
1508 }
1509 
1510 static INLINE int partition_plane_context(const MACROBLOCKD *xd, int mi_row,
1511  int mi_col, BLOCK_SIZE bsize) {
1512  const PARTITION_CONTEXT *above_ctx = xd->above_partition_context + mi_col;
1513  const PARTITION_CONTEXT *left_ctx =
1514  xd->left_partition_context + (mi_row & MAX_MIB_MASK);
1515  // Minimum partition point is 8x8. Offset the bsl accordingly.
1516  const int bsl = mi_size_wide_log2[bsize] - mi_size_wide_log2[BLOCK_8X8];
1517  int above = (*above_ctx >> bsl) & 1, left = (*left_ctx >> bsl) & 1;
1518 
1519  assert(mi_size_wide_log2[bsize] == mi_size_high_log2[bsize]);
1520  assert(bsl >= 0);
1521 
1522  return (left * 2 + above) + bsl * PARTITION_PLOFFSET;
1523 }
1524 
1525 // Return the number of elements in the partition CDF when
1526 // partitioning the (square) block with luma block size of bsize.
1527 static INLINE int partition_cdf_length(BLOCK_SIZE bsize) {
1528  if (bsize <= BLOCK_8X8)
1529  return PARTITION_TYPES;
1530  else if (bsize == BLOCK_128X128)
1531  return EXT_PARTITION_TYPES - 2;
1532  else
1533  return EXT_PARTITION_TYPES;
1534 }
1535 
1536 static INLINE int max_block_wide(const MACROBLOCKD *xd, BLOCK_SIZE bsize,
1537  int plane) {
1538  assert(bsize < BLOCK_SIZES_ALL);
1539  int max_blocks_wide = block_size_wide[bsize];
1540 
1541  if (xd->mb_to_right_edge < 0) {
1542  const struct macroblockd_plane *const pd = &xd->plane[plane];
1543  max_blocks_wide += xd->mb_to_right_edge >> (3 + pd->subsampling_x);
1544  }
1545 
1546  // Scale the width in the transform block unit.
1547  return max_blocks_wide >> MI_SIZE_LOG2;
1548 }
1549 
1550 static INLINE int max_block_high(const MACROBLOCKD *xd, BLOCK_SIZE bsize,
1551  int plane) {
1552  int max_blocks_high = block_size_high[bsize];
1553 
1554  if (xd->mb_to_bottom_edge < 0) {
1555  const struct macroblockd_plane *const pd = &xd->plane[plane];
1556  max_blocks_high += xd->mb_to_bottom_edge >> (3 + pd->subsampling_y);
1557  }
1558 
1559  // Scale the height in the transform block unit.
1560  return max_blocks_high >> MI_SIZE_LOG2;
1561 }
1562 
1563 static INLINE void av1_zero_above_context(AV1_COMMON *const cm,
1564  const MACROBLOCKD *xd,
1565  int mi_col_start, int mi_col_end,
1566  const int tile_row) {
1567  const SequenceHeader *const seq_params = &cm->seq_params;
1568  const int num_planes = av1_num_planes(cm);
1569  const int width = mi_col_end - mi_col_start;
1570  const int aligned_width =
1571  ALIGN_POWER_OF_TWO(width, seq_params->mib_size_log2);
1572  const int offset_y = mi_col_start;
1573  const int width_y = aligned_width;
1574  const int offset_uv = offset_y >> seq_params->subsampling_x;
1575  const int width_uv = width_y >> seq_params->subsampling_x;
1576  CommonContexts *const above_contexts = &cm->above_contexts;
1577 
1578  av1_zero_array(above_contexts->entropy[0][tile_row] + offset_y, width_y);
1579  if (num_planes > 1) {
1580  if (above_contexts->entropy[1][tile_row] &&
1581  above_contexts->entropy[2][tile_row]) {
1582  av1_zero_array(above_contexts->entropy[1][tile_row] + offset_uv,
1583  width_uv);
1584  av1_zero_array(above_contexts->entropy[2][tile_row] + offset_uv,
1585  width_uv);
1586  } else {
1587  aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
1588  "Invalid value of planes");
1589  }
1590  }
1591 
1592  av1_zero_array(above_contexts->partition[tile_row] + mi_col_start,
1593  aligned_width);
1594 
1595  memset(above_contexts->txfm[tile_row] + mi_col_start,
1596  tx_size_wide[TX_SIZES_LARGEST], aligned_width * sizeof(TXFM_CONTEXT));
1597 }
1598 
1599 static INLINE void av1_zero_left_context(MACROBLOCKD *const xd) {
1600  av1_zero(xd->left_entropy_context);
1601  av1_zero(xd->left_partition_context);
1602 
1603  memset(xd->left_txfm_context_buffer, tx_size_high[TX_SIZES_LARGEST],
1604  sizeof(xd->left_txfm_context_buffer));
1605 }
1606 
1607 // Disable array-bounds checks as the TX_SIZE enum contains values larger than
1608 // TX_SIZES_ALL (TX_INVALID) which make extending the array as a workaround
1609 // infeasible. The assert is enough for static analysis and this or other tools
1610 // asan, valgrind would catch oob access at runtime.
1611 #if defined(__GNUC__) && __GNUC__ >= 4
1612 #pragma GCC diagnostic ignored "-Warray-bounds"
1613 #endif
1614 
1615 #if defined(__GNUC__) && __GNUC__ >= 4
1616 #pragma GCC diagnostic warning "-Warray-bounds"
1617 #endif
1618 
1619 static INLINE void set_txfm_ctx(TXFM_CONTEXT *txfm_ctx, uint8_t txs, int len) {
1620  int i;
1621  for (i = 0; i < len; ++i) txfm_ctx[i] = txs;
1622 }
1623 
1624 static INLINE void set_txfm_ctxs(TX_SIZE tx_size, int n4_w, int n4_h, int skip,
1625  const MACROBLOCKD *xd) {
1626  uint8_t bw = tx_size_wide[tx_size];
1627  uint8_t bh = tx_size_high[tx_size];
1628 
1629  if (skip) {
1630  bw = n4_w * MI_SIZE;
1631  bh = n4_h * MI_SIZE;
1632  }
1633 
1634  set_txfm_ctx(xd->above_txfm_context, bw, n4_w);
1635  set_txfm_ctx(xd->left_txfm_context, bh, n4_h);
1636 }
1637 
1638 static INLINE int get_mi_grid_idx(const CommonModeInfoParams *const mi_params,
1639  int mi_row, int mi_col) {
1640  return mi_row * mi_params->mi_stride + mi_col;
1641 }
1642 
1643 static INLINE int get_alloc_mi_idx(const CommonModeInfoParams *const mi_params,
1644  int mi_row, int mi_col) {
1645  const int mi_alloc_size_1d = mi_size_wide[mi_params->mi_alloc_bsize];
1646  const int mi_alloc_row = mi_row / mi_alloc_size_1d;
1647  const int mi_alloc_col = mi_col / mi_alloc_size_1d;
1648 
1649  return mi_alloc_row * mi_params->mi_alloc_stride + mi_alloc_col;
1650 }
1651 
1652 // For this partition block, set pointers in mi_params->mi_grid_base and xd->mi.
1653 static INLINE void set_mi_offsets(const CommonModeInfoParams *const mi_params,
1654  MACROBLOCKD *const xd, int mi_row,
1655  int mi_col) {
1656  // 'mi_grid_base' should point to appropriate memory in 'mi'.
1657  const int mi_grid_idx = get_mi_grid_idx(mi_params, mi_row, mi_col);
1658  const int mi_alloc_idx = get_alloc_mi_idx(mi_params, mi_row, mi_col);
1659  mi_params->mi_grid_base[mi_grid_idx] = &mi_params->mi_alloc[mi_alloc_idx];
1660  // 'xd->mi' should point to an offset in 'mi_grid_base';
1661  xd->mi = mi_params->mi_grid_base + mi_grid_idx;
1662  // 'xd->tx_type_map' should point to an offset in 'mi_params->tx_type_map'.
1663  xd->tx_type_map = mi_params->tx_type_map + mi_grid_idx;
1664  xd->tx_type_map_stride = mi_params->mi_stride;
1665 }
1666 
1667 static INLINE void txfm_partition_update(TXFM_CONTEXT *above_ctx,
1668  TXFM_CONTEXT *left_ctx,
1669  TX_SIZE tx_size, TX_SIZE txb_size) {
1670  BLOCK_SIZE bsize = txsize_to_bsize[txb_size];
1671  int bh = mi_size_high[bsize];
1672  int bw = mi_size_wide[bsize];
1673  uint8_t txw = tx_size_wide[tx_size];
1674  uint8_t txh = tx_size_high[tx_size];
1675  int i;
1676  for (i = 0; i < bh; ++i) left_ctx[i] = txh;
1677  for (i = 0; i < bw; ++i) above_ctx[i] = txw;
1678 }
1679 
1680 static INLINE TX_SIZE get_sqr_tx_size(int tx_dim) {
1681  switch (tx_dim) {
1682  case 128:
1683  case 64: return TX_64X64; break;
1684  case 32: return TX_32X32; break;
1685  case 16: return TX_16X16; break;
1686  case 8: return TX_8X8; break;
1687  default: return TX_4X4;
1688  }
1689 }
1690 
1691 static INLINE TX_SIZE get_tx_size(int width, int height) {
1692  if (width == height) {
1693  return get_sqr_tx_size(width);
1694  }
1695  if (width < height) {
1696  if (width + width == height) {
1697  switch (width) {
1698  case 4: return TX_4X8; break;
1699  case 8: return TX_8X16; break;
1700  case 16: return TX_16X32; break;
1701  case 32: return TX_32X64; break;
1702  }
1703  } else {
1704  switch (width) {
1705  case 4: return TX_4X16; break;
1706  case 8: return TX_8X32; break;
1707  case 16: return TX_16X64; break;
1708  }
1709  }
1710  } else {
1711  if (height + height == width) {
1712  switch (height) {
1713  case 4: return TX_8X4; break;
1714  case 8: return TX_16X8; break;
1715  case 16: return TX_32X16; break;
1716  case 32: return TX_64X32; break;
1717  }
1718  } else {
1719  switch (height) {
1720  case 4: return TX_16X4; break;
1721  case 8: return TX_32X8; break;
1722  case 16: return TX_64X16; break;
1723  }
1724  }
1725  }
1726  assert(0);
1727  return TX_4X4;
1728 }
1729 
1730 static INLINE int txfm_partition_context(const TXFM_CONTEXT *const above_ctx,
1731  const TXFM_CONTEXT *const left_ctx,
1732  BLOCK_SIZE bsize, TX_SIZE tx_size) {
1733  const uint8_t txw = tx_size_wide[tx_size];
1734  const uint8_t txh = tx_size_high[tx_size];
1735  const int above = *above_ctx < txw;
1736  const int left = *left_ctx < txh;
1737  int category = TXFM_PARTITION_CONTEXTS;
1738 
1739  // dummy return, not used by others.
1740  if (tx_size <= TX_4X4) return 0;
1741 
1742  TX_SIZE max_tx_size =
1743  get_sqr_tx_size(AOMMAX(block_size_wide[bsize], block_size_high[bsize]));
1744 
1745  if (max_tx_size >= TX_8X8) {
1746  category =
1747  (txsize_sqr_up_map[tx_size] != max_tx_size && max_tx_size > TX_8X8) +
1748  (TX_SIZES - 1 - max_tx_size) * 2;
1749  }
1750  assert(category != TXFM_PARTITION_CONTEXTS);
1751  return category * 3 + above + left;
1752 }
1753 
1754 // Compute the next partition in the direction of the sb_type stored in the mi
1755 // array, starting with bsize.
1756 static INLINE PARTITION_TYPE get_partition(const AV1_COMMON *const cm,
1757  int mi_row, int mi_col,
1758  BLOCK_SIZE bsize) {
1759  const CommonModeInfoParams *const mi_params = &cm->mi_params;
1760  if (mi_row >= mi_params->mi_rows || mi_col >= mi_params->mi_cols)
1761  return PARTITION_INVALID;
1762 
1763  const int offset = mi_row * mi_params->mi_stride + mi_col;
1764  MB_MODE_INFO **mi = mi_params->mi_grid_base + offset;
1765  const BLOCK_SIZE subsize = mi[0]->bsize;
1766 
1767  assert(bsize < BLOCK_SIZES_ALL);
1768 
1769  if (subsize == bsize) return PARTITION_NONE;
1770 
1771  const int bhigh = mi_size_high[bsize];
1772  const int bwide = mi_size_wide[bsize];
1773  const int sshigh = mi_size_high[subsize];
1774  const int sswide = mi_size_wide[subsize];
1775 
1776  if (bsize > BLOCK_8X8 && mi_row + bwide / 2 < mi_params->mi_rows &&
1777  mi_col + bhigh / 2 < mi_params->mi_cols) {
1778  // In this case, the block might be using an extended partition
1779  // type.
1780  const MB_MODE_INFO *const mbmi_right = mi[bwide / 2];
1781  const MB_MODE_INFO *const mbmi_below = mi[bhigh / 2 * mi_params->mi_stride];
1782 
1783  if (sswide == bwide) {
1784  // Smaller height but same width. Is PARTITION_HORZ_4, PARTITION_HORZ or
1785  // PARTITION_HORZ_B. To distinguish the latter two, check if the lower
1786  // half was split.
1787  if (sshigh * 4 == bhigh) return PARTITION_HORZ_4;
1788  assert(sshigh * 2 == bhigh);
1789 
1790  if (mbmi_below->bsize == subsize)
1791  return PARTITION_HORZ;
1792  else
1793  return PARTITION_HORZ_B;
1794  } else if (sshigh == bhigh) {
1795  // Smaller width but same height. Is PARTITION_VERT_4, PARTITION_VERT or
1796  // PARTITION_VERT_B. To distinguish the latter two, check if the right
1797  // half was split.
1798  if (sswide * 4 == bwide) return PARTITION_VERT_4;
1799  assert(sswide * 2 == bhigh);
1800 
1801  if (mbmi_right->bsize == subsize)
1802  return PARTITION_VERT;
1803  else
1804  return PARTITION_VERT_B;
1805  } else {
1806  // Smaller width and smaller height. Might be PARTITION_SPLIT or could be
1807  // PARTITION_HORZ_A or PARTITION_VERT_A. If subsize isn't halved in both
1808  // dimensions, we immediately know this is a split (which will recurse to
1809  // get to subsize). Otherwise look down and to the right. With
1810  // PARTITION_VERT_A, the right block will have height bhigh; with
1811  // PARTITION_HORZ_A, the lower block with have width bwide. Otherwise
1812  // it's PARTITION_SPLIT.
1813  if (sswide * 2 != bwide || sshigh * 2 != bhigh) return PARTITION_SPLIT;
1814 
1815  if (mi_size_wide[mbmi_below->bsize] == bwide) return PARTITION_HORZ_A;
1816  if (mi_size_high[mbmi_right->bsize] == bhigh) return PARTITION_VERT_A;
1817 
1818  return PARTITION_SPLIT;
1819  }
1820  }
1821  const int vert_split = sswide < bwide;
1822  const int horz_split = sshigh < bhigh;
1823  const int split_idx = (vert_split << 1) | horz_split;
1824  assert(split_idx != 0);
1825 
1826  static const PARTITION_TYPE base_partitions[4] = {
1827  PARTITION_INVALID, PARTITION_HORZ, PARTITION_VERT, PARTITION_SPLIT
1828  };
1829 
1830  return base_partitions[split_idx];
1831 }
1832 
1833 static INLINE void set_sb_size(SequenceHeader *const seq_params,
1834  BLOCK_SIZE sb_size) {
1835  seq_params->sb_size = sb_size;
1836  seq_params->mib_size = mi_size_wide[seq_params->sb_size];
1837  seq_params->mib_size_log2 = mi_size_wide_log2[seq_params->sb_size];
1838 }
1839 
1840 // Returns true if the frame is fully lossless at the coded resolution.
1841 // Note: If super-resolution is used, such a frame will still NOT be lossless at
1842 // the upscaled resolution.
1843 static INLINE int is_coded_lossless(const AV1_COMMON *cm,
1844  const MACROBLOCKD *xd) {
1845  int coded_lossless = 1;
1846  if (cm->seg.enabled) {
1847  for (int i = 0; i < MAX_SEGMENTS; ++i) {
1848  if (!xd->lossless[i]) {
1849  coded_lossless = 0;
1850  break;
1851  }
1852  }
1853  } else {
1854  coded_lossless = xd->lossless[0];
1855  }
1856  return coded_lossless;
1857 }
1858 
1859 static INLINE int is_valid_seq_level_idx(AV1_LEVEL seq_level_idx) {
1860  return seq_level_idx == SEQ_LEVEL_MAX ||
1861  (seq_level_idx < SEQ_LEVELS &&
1862  // The following levels are currently undefined.
1863  seq_level_idx != SEQ_LEVEL_2_2 && seq_level_idx != SEQ_LEVEL_2_3 &&
1864  seq_level_idx != SEQ_LEVEL_3_2 && seq_level_idx != SEQ_LEVEL_3_3 &&
1865  seq_level_idx != SEQ_LEVEL_4_2 && seq_level_idx != SEQ_LEVEL_4_3 &&
1866  seq_level_idx != SEQ_LEVEL_7_0 && seq_level_idx != SEQ_LEVEL_7_1 &&
1867  seq_level_idx != SEQ_LEVEL_7_2 && seq_level_idx != SEQ_LEVEL_7_3);
1868 }
1869 
1872 #ifdef __cplusplus
1873 } // extern "C"
1874 #endif
1875 
1876 #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:200
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:725
uint8_t * last_frame_seg_map
Definition: av1_common_int.h:911
RestorationInfo rst_info[3]
Definition: av1_common_int.h:925
WarpedMotionParams global_motion[REF_FRAMES]
Definition: av1_common_int.h:949
int superres_upscaled_width
Definition: av1_common_int.h:774
bool buffer_removal_time_present
Definition: av1_common_int.h:787
int8_t ref_frame_side[REF_FRAMES]
Definition: av1_common_int.h:1016
struct scale_factors ref_scale_factors[REF_FRAMES]
Definition: av1_common_int.h:848
RefCntBuffer * prev_frame
Definition: av1_common_int.h:805
FRAME_CONTEXT * default_frame_context
Definition: av1_common_int.h:966
int ref_frame_id[REF_FRAMES]
Definition: av1_common_int.h:991
int superres_upscaled_height
Definition: av1_common_int.h:775
DeltaQInfo delta_q_info
Definition: av1_common_int.h:944
int width
Definition: av1_common_int.h:750
RefCntBuffer * cur_frame
Definition: av1_common_int.h:811
CdefInfo cdef_info
Definition: av1_common_int.h:934
loop_filter_info_n lf_info
Definition: av1_common_int.h:917
CurrentFrame current_frame
Definition: av1_common_int.h:729
unsigned int number_spatial_layers
Definition: av1_common_int.h:1031
int remapped_ref_idx[REF_FRAMES]
Definition: av1_common_int.h:833
unsigned int number_temporal_layers
Definition: av1_common_int.h:1021
RestorationLineBuffers * rlbs
Definition: av1_common_int.h:927
aom_film_grain_t film_grain_params
Definition: av1_common_int.h:939
int show_existing_frame
Definition: av1_common_int.h:879
uint32_t buffer_removal_times[(8 *4)+1]
Definition: av1_common_int.h:794
int temporal_layer_id
Definition: av1_common_int.h:1026
SequenceHeader seq_params
Definition: av1_common_int.h:955
int showable_frame
Definition: av1_common_int.h:872
int tpl_mvs_mem_size
Definition: av1_common_int.h:1005
uint32_t frame_presentation_time
Definition: av1_common_int.h:800
struct aom_internal_error_info error
Definition: av1_common_int.h:733
struct loopfilter lf
Definition: av1_common_int.h:918
int spatial_layer_id
Definition: av1_common_int.h:1036
FeatureFlags features
Definition: av1_common_int.h:884
struct scale_factors sf_identity
Definition: av1_common_int.h:840
YV12_BUFFER_CONFIG rst_frame
Definition: av1_common_int.h:928
CommonModeInfoParams mi_params
Definition: av1_common_int.h:889
uint8_t superres_scale_denominator
Definition: av1_common_int.h:782
int show_frame
Definition: av1_common_int.h:864
struct segmentation seg
Definition: av1_common_int.h:906
CommonQuantParams quant_params
Definition: av1_common_int.h:901
TPL_MV_REF * tpl_mvs
Definition: av1_common_int.h:1001
int current_frame_id
Definition: av1_common_int.h:990
int32_t * rst_tmpbuf
Definition: av1_common_int.h:926
RefCntBuffer * ref_frame_map[REF_FRAMES]
Definition: av1_common_int.h:857
CommonContexts above_contexts
Definition: av1_common_int.h:984
CommonTileParams tiles
Definition: av1_common_int.h:971
BufferPool * buffer_pool
Definition: av1_common_int.h:976
int ref_frame_sign_bias[REF_FRAMES]
Definition: av1_common_int.h:1010
FRAME_CONTEXT * fc
Definition: av1_common_int.h:960
int height
Definition: av1_common_int.h:751
int render_width
Definition: av1_common_int.h:761
int render_height
Definition: av1_common_int.h:762
Parameters related to CDEF.
Definition: av1_common_int.h:196
int cdef_bits
Definition: av1_common_int.h:202
int nb_cdef_strengths
Definition: av1_common_int.h:198
int cdef_damping
Definition: av1_common_int.h:197
Contexts used for transmitting various symbols in the bitstream.
Definition: av1_common_int.h:688
PARTITION_CONTEXT ** partition
Definition: av1_common_int.h:693
int num_planes
Definition: av1_common_int.h:717
ENTROPY_CONTEXT ** entropy[3]
Definition: av1_common_int.h:703
int num_tile_rows
Definition: av1_common_int.h:718
int num_mi_cols
Definition: av1_common_int.h:719
TXFM_CONTEXT ** txfm
Definition: av1_common_int.h:711
Params related to MB_MODE_INFO arrays and related info.
Definition: av1_common_int.h:479
int mb_cols
Definition: av1_common_int.h:489
MB_MODE_INFO * mi_alloc
Definition: av1_common_int.h:513
int mi_rows
Definition: av1_common_int.h:500
void(* setup_mi)(struct CommonModeInfoParams *mi_params)
Definition: av1_common_int.h:568
void(* free_mi)(struct CommonModeInfoParams *mi_params)
Definition: av1_common_int.h:563
int mi_cols
Definition: av1_common_int.h:505
int mi_alloc_size
Definition: av1_common_int.h:517
int MBs
Definition: av1_common_int.h:494
TX_TYPE * tx_type_map
Definition: av1_common_int.h:553
void(* set_mb_mi)(struct CommonModeInfoParams *mi_params, int width, int height)
Definition: av1_common_int.h:575
int mi_alloc_stride
Definition: av1_common_int.h:521
int mi_grid_size
Definition: av1_common_int.h:541
int mi_stride
Definition: av1_common_int.h:545
int mb_rows
Definition: av1_common_int.h:484
MB_MODE_INFO ** mi_grid_base
Definition: av1_common_int.h:537
BLOCK_SIZE mi_alloc_bsize
Definition: av1_common_int.h:528
Parameters related to quantization at the frame level.
Definition: av1_common_int.h:584
int u_ac_delta_q
Definition: av1_common_int.h:609
const qm_val_t * u_iqmatrix[8][TX_SIZES_ALL]
Definition: av1_common_int.h:658
int qmatrix_level_v
Definition: av1_common_int.h:680
const qm_val_t * giqmatrix[(1<< 4)][3][TX_SIZES_ALL]
Definition: av1_common_int.h:640
int16_t u_dequant_QTX[8][2]
Definition: av1_common_int.h:629
const qm_val_t * y_iqmatrix[8][TX_SIZES_ALL]
Definition: av1_common_int.h:654
int qmatrix_level_y
Definition: av1_common_int.h:678
int v_ac_delta_q
Definition: av1_common_int.h:614
bool using_qmatrix
Definition: av1_common_int.h:671
int u_dc_delta_q
Definition: av1_common_int.h:599
int qmatrix_level_u
Definition: av1_common_int.h:679
int base_qindex
Definition: av1_common_int.h:588
int16_t v_dequant_QTX[8][2]
Definition: av1_common_int.h:630
const qm_val_t * v_iqmatrix[8][TX_SIZES_ALL]
Definition: av1_common_int.h:662
int16_t y_dequant_QTX[8][2]
Definition: av1_common_int.h:628
int v_dc_delta_q
Definition: av1_common_int.h:603
int y_dc_delta_q
Definition: av1_common_int.h:594
const qm_val_t * gqmatrix[(1<< 4)][3][TX_SIZES_ALL]
Definition: av1_common_int.h:644
Params related to tiles.
Definition: av1_common_int.h:405
int uniform_spacing
Definition: av1_common_int.h:421
int max_width_sb
Definition: av1_common_int.h:408
int log2_rows
Definition: av1_common_int.h:428
int min_log2_rows
Definition: av1_common_int.h:440
int width
Definition: av1_common_int.h:429
int max_log2_rows
Definition: av1_common_int.h:448
int row_start_sb[MAX_TILE_ROWS+1]
Definition: av1_common_int.h:462
int cols
Definition: av1_common_int.h:406
int max_height_sb
Definition: av1_common_int.h:409
unsigned int large_scale
Definition: av1_common_int.h:466
unsigned int single_tile_decoding
Definition: av1_common_int.h:472
int max_log2_cols
Definition: av1_common_int.h:444
int log2_cols
Definition: av1_common_int.h:427
int min_log2
Definition: av1_common_int.h:452
int rows
Definition: av1_common_int.h:407
int min_inner_width
Definition: av1_common_int.h:414
int min_log2_cols
Definition: av1_common_int.h:436
int col_start_sb[MAX_TILE_COLS+1]
Definition: av1_common_int.h:457
int height
Definition: av1_common_int.h:430
Frame level features.
Definition: av1_common_int.h:336
InterpFilter interp_filter
Definition: av1_common_int.h:385
bool allow_ref_frame_mvs
Definition: av1_common_int.h:359
bool allow_warped_motion
Definition: av1_common_int.h:355
bool allow_screen_content_tools
Definition: av1_common_int.h:353
bool switchable_motion_mode
Definition: av1_common_int.h:383
TX_MODE tx_mode
Definition: av1_common_int.h:384
bool reduced_tx_set_used
Definition: av1_common_int.h:372
bool allow_intrabc
Definition: av1_common_int.h:354
int byte_alignment
Definition: av1_common_int.h:394
bool coded_lossless
Definition: av1_common_int.h:363
REFRESH_FRAME_CONTEXT_MODE refresh_frame_context
Definition: av1_common_int.h:399
bool error_resilient_mode
Definition: av1_common_int.h:378
int primary_ref_frame
Definition: av1_common_int.h:390
bool disable_cdf_update
Definition: av1_common_int.h:340
bool allow_high_precision_mv
Definition: av1_common_int.h:345
bool cur_frame_force_integer_mv
Definition: av1_common_int.h:349
bool all_lossless
Definition: av1_common_int.h:367
Stores the prediction/txfm mode of the current coding block.
Definition: blockd.h:216
BLOCK_SIZE bsize
The block size of the current coding block.
Definition: blockd.h:222
Parameters related to Restoration Info.
Definition: restoration.h:255
External frame buffer.
Definition: aom_frame_buffer.h:40
Variables related to current coding block.
Definition: blockd.h:568
bool left_available
Definition: blockd.h:624
uint8_t * tx_type_map
Definition: blockd.h:664
int mb_to_bottom_edge
Definition: blockd.h:678
TXFM_CONTEXT * left_txfm_context
Definition: blockd.h:738
struct macroblockd_plane plane[3]
Definition: blockd.h:604
int mb_to_top_edge
Definition: blockd.h:677
int mb_to_right_edge
Definition: blockd.h:676
bool up_available
Definition: blockd.h:620
MB_MODE_INFO * above_mbmi
Definition: blockd.h:643
bool chroma_up_available
Definition: blockd.h:628
TXFM_CONTEXT * above_txfm_context
Definition: blockd.h:731
bool chroma_left_available
Definition: blockd.h:632
PARTITION_CONTEXT * above_partition_context
Definition: blockd.h:716
MB_MODE_INFO * chroma_left_mbmi
Definition: blockd.h:650
TXFM_CONTEXT left_txfm_context_buffer[MAX_MIB_SIZE]
Definition: blockd.h:745
int tx_type_map_stride
Definition: blockd.h:669
MB_MODE_INFO * chroma_above_mbmi
Definition: blockd.h:657
int mi_row
Definition: blockd.h:573
int mi_stride
Definition: blockd.h:580
bool is_last_vertical_rect
Definition: blockd.h:785
bool is_first_horizontal_rect
Definition: blockd.h:790
uint8_t width
Definition: blockd.h:763
struct aom_internal_error_info * error_info
Definition: blockd.h:836
CFL_CTX cfl
Definition: blockd.h:892
int lossless[8]
Definition: blockd.h:815
ENTROPY_CONTEXT left_entropy_context[3][MAX_MIB_SIZE]
Definition: blockd.h:708
ENTROPY_CONTEXT * above_entropy_context[3]
Definition: blockd.h:701
MB_MODE_INFO ** mi
Definition: blockd.h:615
uint8_t height
Definition: blockd.h:764
MB_MODE_INFO * left_mbmi
Definition: blockd.h:638
PARTITION_CONTEXT left_partition_context[MAX_MIB_SIZE]
Definition: blockd.h:723
bool is_chroma_ref
Definition: blockd.h:599
int mi_col
Definition: blockd.h:574
int mb_to_left_edge
Definition: blockd.h:675
YV12 frame buffer data structure.
Definition: yv12config.h:38