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