Line data Source code
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 AV1_COMMON_BLOCKD_H_
13 : #define AV1_COMMON_BLOCKD_H_
14 :
15 : #include "./aom_config.h"
16 :
17 : #include "aom_dsp/aom_dsp_common.h"
18 : #include "aom_ports/mem.h"
19 : #include "aom_scale/yv12config.h"
20 :
21 : #include "av1/common/common_data.h"
22 : #include "av1/common/quant_common.h"
23 : #include "av1/common/entropy.h"
24 : #include "av1/common/entropymode.h"
25 : #include "av1/common/mv.h"
26 : #include "av1/common/scale.h"
27 : #include "av1/common/seg_common.h"
28 : #include "av1/common/tile_common.h"
29 : #if CONFIG_PVQ
30 : #include "av1/common/pvq.h"
31 : #include "av1/common/pvq_state.h"
32 : #include "av1/decoder/decint.h"
33 : #endif
34 : #if CONFIG_CFL
35 : #include "av1/common/cfl.h"
36 : #endif
37 : #ifdef __cplusplus
38 : extern "C" {
39 : #endif
40 :
41 : #if (CONFIG_CHROMA_SUB8X8 || CONFIG_CHROMA_2X2)
42 : #define SUB8X8_COMP_REF 0
43 : #else
44 : #define SUB8X8_COMP_REF 1
45 : #endif
46 :
47 : #define MAX_MB_PLANE 3
48 :
49 : #if CONFIG_EXT_INTER
50 :
51 : #if CONFIG_COMPOUND_SEGMENT
52 : // Set COMPOUND_SEGMENT_TYPE to one of the three
53 : // 0: Uniform
54 : // 1: Difference weighted
55 : #define COMPOUND_SEGMENT_TYPE 1
56 : #define MAX_SEG_MASK_BITS 1
57 :
58 : // SEG_MASK_TYPES should not surpass 1 << MAX_SEG_MASK_BITS
59 : typedef enum {
60 : #if COMPOUND_SEGMENT_TYPE == 0
61 : UNIFORM_45 = 0,
62 : UNIFORM_45_INV,
63 : #elif COMPOUND_SEGMENT_TYPE == 1
64 : DIFFWTD_38 = 0,
65 : DIFFWTD_38_INV,
66 : #endif // COMPOUND_SEGMENT_TYPE
67 : SEG_MASK_TYPES,
68 : } SEG_MASK_TYPE;
69 :
70 : #endif // CONFIG_COMPOUND_SEGMENT
71 : #endif // CONFIG_EXT_INTER
72 :
73 : typedef enum {
74 : KEY_FRAME = 0,
75 : INTER_FRAME = 1,
76 : FRAME_TYPES,
77 : } FRAME_TYPE;
78 :
79 0 : static INLINE int is_inter_mode(PREDICTION_MODE mode) {
80 : #if CONFIG_EXT_INTER
81 0 : return mode >= NEARESTMV && mode <= NEW_NEWMV;
82 : #else
83 : return mode >= NEARESTMV && mode <= NEWMV;
84 : #endif // CONFIG_EXT_INTER
85 : }
86 :
87 : #if CONFIG_PVQ
88 : typedef struct PVQ_INFO {
89 : int theta[PVQ_MAX_PARTITIONS];
90 : int qg[PVQ_MAX_PARTITIONS];
91 : int k[PVQ_MAX_PARTITIONS];
92 : od_coeff y[OD_TXSIZE_MAX * OD_TXSIZE_MAX];
93 : int nb_bands;
94 : int off[PVQ_MAX_PARTITIONS];
95 : int size[PVQ_MAX_PARTITIONS];
96 : int skip_rest;
97 : int skip_dir;
98 : int bs; // log of the block size minus two,
99 : // i.e. equivalent to aom's TX_SIZE
100 : // Block skip info, indicating whether DC/AC, is coded.
101 : PVQ_SKIP_TYPE ac_dc_coded; // bit0: DC coded, bit1 : AC coded (1 means coded)
102 : tran_low_t dq_dc_residue;
103 : } PVQ_INFO;
104 :
105 : typedef struct PVQ_QUEUE {
106 : PVQ_INFO *buf; // buffer for pvq info, stored in encoding order
107 : int curr_pos; // curr position to write PVQ_INFO
108 : int buf_len; // allocated buffer length
109 : int last_pos; // last written position of PVQ_INFO in a tile
110 : } PVQ_QUEUE;
111 : #endif
112 :
113 : typedef struct {
114 : uint8_t *plane[MAX_MB_PLANE];
115 : int stride[MAX_MB_PLANE];
116 : } BUFFER_SET;
117 :
118 : #if CONFIG_EXT_INTER
119 0 : static INLINE int is_inter_singleref_mode(PREDICTION_MODE mode) {
120 0 : return mode >= NEARESTMV && mode <= NEWMV;
121 : }
122 : #if CONFIG_COMPOUND_SINGLEREF
123 : static INLINE int is_inter_singleref_comp_mode(PREDICTION_MODE mode) {
124 : return mode >= SR_NEAREST_NEARMV && mode <= SR_NEW_NEWMV;
125 : }
126 : #endif // CONFIG_COMPOUND_SINGLEREF
127 0 : static INLINE int is_inter_compound_mode(PREDICTION_MODE mode) {
128 0 : return mode >= NEAREST_NEARESTMV && mode <= NEW_NEWMV;
129 : }
130 :
131 0 : static INLINE PREDICTION_MODE compound_ref0_mode(PREDICTION_MODE mode) {
132 : static PREDICTION_MODE lut[] = {
133 : MB_MODE_COUNT, // DC_PRED
134 : MB_MODE_COUNT, // V_PRED
135 : MB_MODE_COUNT, // H_PRED
136 : MB_MODE_COUNT, // D45_PRED
137 : MB_MODE_COUNT, // D135_PRED
138 : MB_MODE_COUNT, // D117_PRED
139 : MB_MODE_COUNT, // D153_PRED
140 : MB_MODE_COUNT, // D207_PRED
141 : MB_MODE_COUNT, // D63_PRED
142 : #if CONFIG_ALT_INTRA
143 : MB_MODE_COUNT, // SMOOTH_PRED
144 : #if CONFIG_SMOOTH_HV
145 : MB_MODE_COUNT, // SMOOTH_V_PRED
146 : MB_MODE_COUNT, // SMOOTH_H_PRED
147 : #endif // CONFIG_SMOOTH_HV
148 : #endif // CONFIG_ALT_INTRA
149 : MB_MODE_COUNT, // TM_PRED
150 : MB_MODE_COUNT, // NEARESTMV
151 : MB_MODE_COUNT, // NEARMV
152 : MB_MODE_COUNT, // ZEROMV
153 : MB_MODE_COUNT, // NEWMV
154 : #if CONFIG_COMPOUND_SINGLEREF
155 : NEARESTMV, // SR_NEAREST_NEARMV
156 : NEARESTMV, // SR_NEAREST_NEWMV
157 : NEARMV, // SR_NEAR_NEWMV
158 : ZEROMV, // SR_ZERO_NEWMV
159 : NEWMV, // SR_NEW_NEWMV
160 : #endif // CONFIG_COMPOUND_SINGLEREF
161 : NEARESTMV, // NEAREST_NEARESTMV
162 : NEARMV, // NEAR_NEARMV
163 : NEARESTMV, // NEAREST_NEWMV
164 : NEWMV, // NEW_NEARESTMV
165 : NEARMV, // NEAR_NEWMV
166 : NEWMV, // NEW_NEARMV
167 : ZEROMV, // ZERO_ZEROMV
168 : NEWMV, // NEW_NEWMV
169 : };
170 : assert(NELEMENTS(lut) == MB_MODE_COUNT);
171 0 : assert(is_inter_compound_mode(mode));
172 0 : return lut[mode];
173 : }
174 :
175 0 : static INLINE PREDICTION_MODE compound_ref1_mode(PREDICTION_MODE mode) {
176 : static PREDICTION_MODE lut[] = {
177 : MB_MODE_COUNT, // DC_PRED
178 : MB_MODE_COUNT, // V_PRED
179 : MB_MODE_COUNT, // H_PRED
180 : MB_MODE_COUNT, // D45_PRED
181 : MB_MODE_COUNT, // D135_PRED
182 : MB_MODE_COUNT, // D117_PRED
183 : MB_MODE_COUNT, // D153_PRED
184 : MB_MODE_COUNT, // D207_PRED
185 : MB_MODE_COUNT, // D63_PRED
186 : #if CONFIG_ALT_INTRA
187 : MB_MODE_COUNT, // SMOOTH_PRED
188 : #if CONFIG_SMOOTH_HV
189 : MB_MODE_COUNT, // SMOOTH_V_PRED
190 : MB_MODE_COUNT, // SMOOTH_H_PRED
191 : #endif // CONFIG_SMOOTH_HV
192 : #endif // CONFIG_ALT_INTRA
193 : MB_MODE_COUNT, // TM_PRED
194 : MB_MODE_COUNT, // NEARESTMV
195 : MB_MODE_COUNT, // NEARMV
196 : MB_MODE_COUNT, // ZEROMV
197 : MB_MODE_COUNT, // NEWMV
198 : #if CONFIG_COMPOUND_SINGLEREF
199 : NEARMV, // SR_NEAREST_NEARMV
200 : NEWMV, // SR_NEAREST_NEWMV
201 : NEWMV, // SR_NEAR_NEWMV
202 : NEWMV, // SR_ZERO_NEWMV
203 : NEWMV, // SR_NEW_NEWMV
204 : #endif // CONFIG_COMPOUND_SINGLEREF
205 : NEARESTMV, // NEAREST_NEARESTMV
206 : NEARMV, // NEAR_NEARMV
207 : NEWMV, // NEAREST_NEWMV
208 : NEARESTMV, // NEW_NEARESTMV
209 : NEWMV, // NEAR_NEWMV
210 : NEARMV, // NEW_NEARMV
211 : ZEROMV, // ZERO_ZEROMV
212 : NEWMV, // NEW_NEWMV
213 : };
214 : assert(NELEMENTS(lut) == MB_MODE_COUNT);
215 0 : assert(is_inter_compound_mode(mode));
216 0 : return lut[mode];
217 : }
218 :
219 0 : static INLINE int have_nearmv_in_inter_mode(PREDICTION_MODE mode) {
220 0 : return (mode == NEARMV || mode == NEAR_NEARMV || mode == NEAR_NEWMV ||
221 : mode == NEW_NEARMV);
222 : }
223 :
224 0 : static INLINE int have_newmv_in_inter_mode(PREDICTION_MODE mode) {
225 0 : return (mode == NEWMV || mode == NEW_NEWMV || mode == NEAREST_NEWMV ||
226 0 : mode == NEW_NEARESTMV || mode == NEAR_NEWMV || mode == NEW_NEARMV);
227 : }
228 :
229 0 : static INLINE int use_masked_motion_search(COMPOUND_TYPE type) {
230 : #if CONFIG_WEDGE
231 0 : return (type == COMPOUND_WEDGE);
232 : #else
233 : (void)type;
234 : return 0;
235 : #endif
236 : }
237 :
238 0 : static INLINE int is_masked_compound_type(COMPOUND_TYPE type) {
239 : #if CONFIG_COMPOUND_SEGMENT && CONFIG_WEDGE
240 0 : return (type == COMPOUND_WEDGE || type == COMPOUND_SEG);
241 : #elif !CONFIG_COMPOUND_SEGMENT && CONFIG_WEDGE
242 : return (type == COMPOUND_WEDGE);
243 : #elif CONFIG_COMPOUND_SEGMENT && !CONFIG_WEDGE
244 : return (type == COMPOUND_SEG);
245 : #endif // CONFIG_COMPOUND_SEGMENT
246 : (void)type;
247 : return 0;
248 : }
249 : #else
250 :
251 : static INLINE int have_nearmv_in_inter_mode(PREDICTION_MODE mode) {
252 : return (mode == NEARMV);
253 : }
254 :
255 : static INLINE int have_newmv_in_inter_mode(PREDICTION_MODE mode) {
256 : return (mode == NEWMV);
257 : }
258 : #endif // CONFIG_EXT_INTER
259 :
260 : /* For keyframes, intra block modes are predicted by the (already decoded)
261 : modes for the Y blocks to the left and above us; for interframes, there
262 : is a single probability table. */
263 :
264 : typedef struct {
265 : PREDICTION_MODE as_mode;
266 : int_mv as_mv[2]; // first, second inter predictor motion vectors
267 : int_mv pred_mv[2];
268 : #if CONFIG_EXT_INTER
269 : int_mv ref_mv[2];
270 : #endif // CONFIG_EXT_INTER
271 : } b_mode_info;
272 :
273 : typedef int8_t MV_REFERENCE_FRAME;
274 :
275 : #if CONFIG_PALETTE
276 : typedef struct {
277 : // Number of base colors for Y (0) and UV (1)
278 : uint8_t palette_size[2];
279 : // Value of base colors for Y, U, and V
280 : uint16_t palette_colors[3 * PALETTE_MAX_SIZE];
281 : // Only used by encoder to store the color index of the top left pixel.
282 : // TODO(huisu): move this to encoder
283 : uint8_t palette_first_color_idx[2];
284 : } PALETTE_MODE_INFO;
285 : #endif // CONFIG_PALETTE
286 :
287 : #if CONFIG_FILTER_INTRA
288 : #define USE_3TAP_INTRA_FILTER 1 // 0: 4-tap; 1: 3-tap
289 : typedef struct {
290 : // 1: an ext intra mode is used; 0: otherwise.
291 : uint8_t use_filter_intra_mode[PLANE_TYPES];
292 : FILTER_INTRA_MODE filter_intra_mode[PLANE_TYPES];
293 : } FILTER_INTRA_MODE_INFO;
294 : #endif // CONFIG_FILTER_INTRA
295 :
296 : #if CONFIG_VAR_TX
297 : #if CONFIG_RD_DEBUG
298 : #define TXB_COEFF_COST_MAP_SIZE (2 * MAX_MIB_SIZE)
299 : #endif
300 : #endif
301 :
302 : typedef struct RD_STATS {
303 : int rate;
304 : int64_t dist;
305 : // Please be careful of using rdcost, it's not guaranteed to be set all the
306 : // time.
307 : // TODO(angiebird): Create a set of functions to manipulate the RD_STATS. In
308 : // these functions, make sure rdcost is always up-to-date according to
309 : // rate/dist.
310 : int64_t rdcost;
311 : int64_t sse;
312 : int skip; // sse should equal to dist when skip == 1
313 : #if CONFIG_DAALA_DIST && CONFIG_CB4X4
314 : int64_t dist_y;
315 : #endif
316 : #if CONFIG_RD_DEBUG
317 : int txb_coeff_cost[MAX_MB_PLANE];
318 : #if CONFIG_VAR_TX
319 : int txb_coeff_cost_map[MAX_MB_PLANE][TXB_COEFF_COST_MAP_SIZE]
320 : [TXB_COEFF_COST_MAP_SIZE];
321 : #endif // CONFIG_VAR_TX
322 : #endif // CONFIG_RD_DEBUG
323 : } RD_STATS;
324 :
325 : #if CONFIG_EXT_INTER
326 : // This struct is used to group function args that are commonly
327 : // sent together in functions related to interinter compound modes
328 : typedef struct {
329 : #if CONFIG_WEDGE
330 : int wedge_index;
331 : int wedge_sign;
332 : #endif // CONFIG_WEDGE
333 : #if CONFIG_COMPOUND_SEGMENT
334 : SEG_MASK_TYPE mask_type;
335 : uint8_t *seg_mask;
336 : #endif // CONFIG_COMPOUND_SEGMENT
337 : COMPOUND_TYPE interinter_compound_type;
338 : } INTERINTER_COMPOUND_DATA;
339 : #endif // CONFIG_EXT_INTER
340 :
341 : // This structure now relates to 8x8 block regions.
342 : typedef struct MB_MODE_INFO {
343 : // Common for both INTER and INTRA blocks
344 : BLOCK_SIZE sb_type;
345 : PREDICTION_MODE mode;
346 : TX_SIZE tx_size;
347 : #if CONFIG_VAR_TX
348 : // TODO(jingning): This effectively assigned a separate entry for each
349 : // 8x8 block. Apparently it takes much more space than needed.
350 : TX_SIZE inter_tx_size[MAX_MIB_SIZE][MAX_MIB_SIZE];
351 : TX_SIZE min_tx_size;
352 : #endif
353 : int8_t skip;
354 : int8_t segment_id;
355 : #if CONFIG_SUPERTX
356 : // Minimum of all segment IDs under the current supertx block.
357 : int8_t segment_id_supertx;
358 : #endif // CONFIG_SUPERTX
359 : int8_t seg_id_predicted; // valid only when temporal_update is enabled
360 :
361 : // Only for INTRA blocks
362 : PREDICTION_MODE uv_mode;
363 : #if CONFIG_PALETTE
364 : PALETTE_MODE_INFO palette_mode_info;
365 : #endif // CONFIG_PALETTE
366 : #if CONFIG_INTRABC
367 : uint8_t use_intrabc;
368 : #endif // CONFIG_INTRABC
369 :
370 : // Only for INTER blocks
371 : #if CONFIG_DUAL_FILTER
372 : InterpFilter interp_filter[4];
373 : #else
374 : InterpFilter interp_filter;
375 : #endif
376 : MV_REFERENCE_FRAME ref_frame[2];
377 : TX_TYPE tx_type;
378 : #if CONFIG_TXK_SEL
379 : TX_TYPE txk_type[MAX_SB_SQUARE / (TX_SIZE_W_MIN * TX_SIZE_H_MIN)];
380 : #endif
381 :
382 : #if CONFIG_FILTER_INTRA
383 : FILTER_INTRA_MODE_INFO filter_intra_mode_info;
384 : #endif // CONFIG_FILTER_INTRA
385 : #if CONFIG_EXT_INTRA
386 : // The actual prediction angle is the base angle + (angle_delta * step).
387 : int8_t angle_delta[2];
388 : #if CONFIG_INTRA_INTERP
389 : // To-Do (huisu): this may be replaced by interp_filter
390 : INTRA_FILTER intra_filter;
391 : #endif // CONFIG_INTRA_INTERP
392 : #endif // CONFIG_EXT_INTRA
393 :
394 : #if CONFIG_EXT_INTER
395 : #if CONFIG_INTERINTRA
396 : // interintra members
397 : INTERINTRA_MODE interintra_mode;
398 : #endif
399 : // TODO(debargha): Consolidate these flags
400 : int use_wedge_interintra;
401 : int interintra_wedge_index;
402 : int interintra_wedge_sign;
403 : // interinter members
404 : COMPOUND_TYPE interinter_compound_type;
405 : #if CONFIG_WEDGE
406 : int wedge_index;
407 : int wedge_sign;
408 : #endif // CONFIG_WEDGE
409 : #if CONFIG_COMPOUND_SEGMENT
410 : SEG_MASK_TYPE mask_type;
411 : #endif // CONFIG_COMPOUND_SEGMENT
412 : #endif // CONFIG_EXT_INTER
413 : MOTION_MODE motion_mode;
414 : #if CONFIG_MOTION_VAR
415 : int overlappable_neighbors[2];
416 : #endif // CONFIG_MOTION_VAR
417 : int_mv mv[2];
418 : int_mv pred_mv[2];
419 : uint8_t ref_mv_idx;
420 : #if CONFIG_EXT_PARTITION_TYPES
421 : PARTITION_TYPE partition;
422 : #endif
423 : #if CONFIG_NEW_QUANT
424 : int dq_off_index;
425 : int send_dq_bit;
426 : #endif // CONFIG_NEW_QUANT
427 : /* deringing gain *per-superblock* */
428 : int8_t cdef_strength;
429 : #if CONFIG_DELTA_Q
430 : int current_q_index;
431 : #if CONFIG_EXT_DELTA_Q
432 : int current_delta_lf_from_base;
433 : #endif
434 : #endif
435 : #if CONFIG_RD_DEBUG
436 : RD_STATS rd_stats;
437 : int mi_row;
438 : int mi_col;
439 : #endif
440 : #if CONFIG_WARPED_MOTION
441 : int num_proj_ref[2];
442 : WarpedMotionParams wm_params[2];
443 : #endif // CONFIG_WARPED_MOTION
444 :
445 : #if CONFIG_CFL
446 : // Index of the alpha Cb and alpha Cr combination
447 : int cfl_alpha_idx;
448 : // Signs of alpha Cb and alpha Cr
449 : CFL_SIGN_TYPE cfl_alpha_signs[CFL_PRED_PLANES];
450 : #endif
451 :
452 : BOUNDARY_TYPE boundary_info;
453 : } MB_MODE_INFO;
454 :
455 : typedef struct MODE_INFO {
456 : MB_MODE_INFO mbmi;
457 : b_mode_info bmi[4];
458 : } MODE_INFO;
459 :
460 : #if CONFIG_INTRABC
461 : static INLINE int is_intrabc_block(const MB_MODE_INFO *mbmi) {
462 : return mbmi->use_intrabc;
463 : }
464 : #endif
465 :
466 0 : static INLINE PREDICTION_MODE get_y_mode(const MODE_INFO *mi, int block) {
467 : #if CONFIG_CB4X4
468 : (void)block;
469 0 : return mi->mbmi.mode;
470 : #else
471 : return mi->mbmi.sb_type < BLOCK_8X8 ? mi->bmi[block].as_mode : mi->mbmi.mode;
472 : #endif
473 : }
474 :
475 0 : static INLINE int is_inter_block(const MB_MODE_INFO *mbmi) {
476 : #if CONFIG_INTRABC
477 : if (is_intrabc_block(mbmi)) return 1;
478 : #endif
479 0 : return mbmi->ref_frame[0] > INTRA_FRAME;
480 : }
481 :
482 0 : static INLINE int has_second_ref(const MB_MODE_INFO *mbmi) {
483 0 : return mbmi->ref_frame[1] > INTRA_FRAME;
484 : }
485 :
486 : PREDICTION_MODE av1_left_block_mode(const MODE_INFO *cur_mi,
487 : const MODE_INFO *left_mi, int b);
488 :
489 : PREDICTION_MODE av1_above_block_mode(const MODE_INFO *cur_mi,
490 : const MODE_INFO *above_mi, int b);
491 :
492 : #if CONFIG_GLOBAL_MOTION
493 0 : static INLINE int is_global_mv_block(const MODE_INFO *mi, int block,
494 : TransformationType type) {
495 0 : PREDICTION_MODE mode = get_y_mode(mi, block);
496 : #if GLOBAL_SUB8X8_USED
497 : const int block_size_allowed = 1;
498 : #else
499 0 : const BLOCK_SIZE bsize = mi->mbmi.sb_type;
500 0 : const int block_size_allowed = (bsize >= BLOCK_8X8);
501 : #endif // GLOBAL_SUB8X8_USED
502 : #if CONFIG_EXT_INTER
503 0 : return (mode == ZEROMV || mode == ZERO_ZEROMV) && type > TRANSLATION &&
504 : block_size_allowed;
505 : #else
506 : return mode == ZEROMV && type > TRANSLATION && block_size_allowed;
507 : #endif // CONFIG_EXT_INTER
508 : }
509 : #endif // CONFIG_GLOBAL_MOTION
510 :
511 : enum mv_precision { MV_PRECISION_Q3, MV_PRECISION_Q4 };
512 :
513 : struct buf_2d {
514 : uint8_t *buf;
515 : uint8_t *buf0;
516 : int width;
517 : int height;
518 : int stride;
519 : };
520 :
521 : typedef struct macroblockd_plane {
522 : tran_low_t *dqcoeff;
523 : PLANE_TYPE plane_type;
524 : int subsampling_x;
525 : int subsampling_y;
526 : struct buf_2d dst;
527 : struct buf_2d pre[2];
528 : ENTROPY_CONTEXT *above_context;
529 : ENTROPY_CONTEXT *left_context;
530 : int16_t seg_dequant[MAX_SEGMENTS][2];
531 : #if CONFIG_NEW_QUANT
532 : dequant_val_type_nuq seg_dequant_nuq[MAX_SEGMENTS][QUANT_PROFILES]
533 : [COEF_BANDS];
534 : #endif
535 : #if CONFIG_PALETTE
536 : uint8_t *color_index_map;
537 : #endif // CONFIG_PALETTE
538 :
539 : // number of 4x4s in current block
540 : uint16_t n4_w, n4_h;
541 : // log2 of n4_w, n4_h
542 : uint8_t n4_wl, n4_hl;
543 : // block size in pixels
544 : uint8_t width, height;
545 :
546 : #if CONFIG_AOM_QM
547 : const qm_val_t *seg_iqmatrix[MAX_SEGMENTS][2][TX_SIZES_ALL];
548 : const qm_val_t *seg_qmatrix[MAX_SEGMENTS][2][TX_SIZES_ALL];
549 : #endif
550 : // encoder
551 : const int16_t *dequant;
552 : #if CONFIG_NEW_QUANT
553 : const dequant_val_type_nuq *dequant_val_nuq[QUANT_PROFILES];
554 : #endif // CONFIG_NEW_QUANT
555 :
556 : #if CONFIG_PVQ || CONFIG_DAALA_DIST
557 : DECLARE_ALIGNED(16, int16_t, pred[MAX_SB_SQUARE]);
558 : // PVQ: forward transformed predicted image, a reference for PVQ.
559 : tran_low_t *pvq_ref_coeff;
560 : #endif
561 : } MACROBLOCKD_PLANE;
562 :
563 : #define BLOCK_OFFSET(x, i) \
564 : ((x) + (i) * (1 << (tx_size_wide_log2[0] + tx_size_high_log2[0])))
565 :
566 : typedef struct RefBuffer {
567 : int idx;
568 : YV12_BUFFER_CONFIG *buf;
569 : struct scale_factors sf;
570 : } RefBuffer;
571 :
572 : #if CONFIG_ADAPT_SCAN
573 : typedef int16_t EobThresholdMD[TX_TYPES][EOB_THRESHOLD_NUM];
574 : #endif
575 :
576 : typedef struct macroblockd {
577 : struct macroblockd_plane plane[MAX_MB_PLANE];
578 : uint8_t bmode_blocks_wl;
579 : uint8_t bmode_blocks_hl;
580 :
581 : FRAME_COUNTS *counts;
582 : TileInfo tile;
583 :
584 : int mi_stride;
585 :
586 : MODE_INFO **mi;
587 : MODE_INFO *left_mi;
588 : MODE_INFO *above_mi;
589 : MB_MODE_INFO *left_mbmi;
590 : MB_MODE_INFO *above_mbmi;
591 :
592 : int up_available;
593 : int left_available;
594 : #if CONFIG_CHROMA_SUB8X8
595 : int chroma_up_available;
596 : int chroma_left_available;
597 : #endif
598 :
599 : const aom_prob (*partition_probs)[PARTITION_TYPES - 1];
600 :
601 : /* Distance of MB away from frame edges */
602 : int mb_to_left_edge;
603 : int mb_to_right_edge;
604 : int mb_to_top_edge;
605 : int mb_to_bottom_edge;
606 :
607 : FRAME_CONTEXT *fc;
608 :
609 : /* pointers to reference frames */
610 : const RefBuffer *block_refs[2];
611 :
612 : /* pointer to current frame */
613 : const YV12_BUFFER_CONFIG *cur_buf;
614 :
615 : #if CONFIG_INTRABC
616 : /* Scale of the current frame with respect to itself */
617 : struct scale_factors sf_identity;
618 : #endif
619 :
620 : ENTROPY_CONTEXT *above_context[MAX_MB_PLANE];
621 : ENTROPY_CONTEXT left_context[MAX_MB_PLANE][2 * MAX_MIB_SIZE];
622 :
623 : PARTITION_CONTEXT *above_seg_context;
624 : PARTITION_CONTEXT left_seg_context[MAX_MIB_SIZE];
625 :
626 : #if CONFIG_VAR_TX
627 : TXFM_CONTEXT *above_txfm_context;
628 : TXFM_CONTEXT *left_txfm_context;
629 : TXFM_CONTEXT left_txfm_context_buffer[2 * MAX_MIB_SIZE];
630 :
631 : TX_SIZE max_tx_size;
632 : #if CONFIG_SUPERTX
633 : TX_SIZE supertx_size;
634 : #endif
635 : #endif
636 :
637 : // block dimension in the unit of mode_info.
638 : uint8_t n8_w, n8_h;
639 :
640 : uint8_t ref_mv_count[MODE_CTX_REF_FRAMES];
641 : CANDIDATE_MV ref_mv_stack[MODE_CTX_REF_FRAMES][MAX_REF_MV_STACK_SIZE];
642 : uint8_t is_sec_rect;
643 :
644 : #if CONFIG_PVQ
645 : daala_dec_ctx daala_dec;
646 : #endif
647 : #if CONFIG_EC_ADAPT
648 : FRAME_CONTEXT *tile_ctx;
649 : #endif
650 : /* Bit depth: 8, 10, 12 */
651 : int bd;
652 :
653 : int qindex[MAX_SEGMENTS];
654 : int lossless[MAX_SEGMENTS];
655 : int corrupted;
656 :
657 : struct aom_internal_error_info *error_info;
658 : #if CONFIG_GLOBAL_MOTION
659 : WarpedMotionParams *global_motion;
660 : #endif // CONFIG_GLOBAL_MOTION
661 : #if CONFIG_DELTA_Q
662 : int prev_qindex;
663 : int delta_qindex;
664 : int current_qindex;
665 : #if CONFIG_EXT_DELTA_Q
666 : // Since actual frame level loop filtering level value is not available
667 : // at the beginning of the tile (only available during actual filtering)
668 : // at encoder side.we record the delta_lf (against the frame level loop
669 : // filtering level) and code the delta between previous superblock's delta
670 : // lf and current delta lf. It is equivalent to the delta between previous
671 : // superblock's actual lf and current lf.
672 : int prev_delta_lf_from_base;
673 : int current_delta_lf_from_base;
674 : #endif
675 : #endif
676 : #if CONFIG_ADAPT_SCAN
677 : const EobThresholdMD *eob_threshold_md;
678 : #endif
679 :
680 : #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SEGMENT
681 : DECLARE_ALIGNED(16, uint8_t, seg_mask[2 * MAX_SB_SQUARE]);
682 : #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SEGMENT
683 :
684 : #if CONFIG_CFL
685 : CFL_CTX *cfl;
686 : #endif
687 : } MACROBLOCKD;
688 :
689 0 : static INLINE BLOCK_SIZE get_subsize(BLOCK_SIZE bsize,
690 : PARTITION_TYPE partition) {
691 0 : if (partition == PARTITION_INVALID)
692 0 : return BLOCK_INVALID;
693 : else
694 0 : return subsize_lookup[partition][bsize];
695 : }
696 :
697 : static const TX_TYPE intra_mode_to_tx_type_context[INTRA_MODES] = {
698 : DCT_DCT, // DC
699 : ADST_DCT, // V
700 : DCT_ADST, // H
701 : DCT_DCT, // D45
702 : ADST_ADST, // D135
703 : ADST_DCT, // D117
704 : DCT_ADST, // D153
705 : DCT_ADST, // D207
706 : ADST_DCT, // D63
707 : #if CONFIG_ALT_INTRA
708 : ADST_ADST, // SMOOTH
709 : #if CONFIG_SMOOTH_HV
710 : ADST_DCT, // SMOOTH_V
711 : DCT_ADST, // SMOOTH_H
712 : #endif // CONFIG_SMOOTH_HV
713 : #endif // CONFIG_ALT_INTRA
714 : ADST_ADST, // TM
715 : };
716 :
717 : #if CONFIG_SUPERTX
718 : static INLINE int supertx_enabled(const MB_MODE_INFO *mbmi) {
719 : TX_SIZE max_tx_size = txsize_sqr_map[mbmi->tx_size];
720 : return tx_size_wide[max_tx_size] >
721 : AOMMIN(block_size_wide[mbmi->sb_type], block_size_high[mbmi->sb_type]);
722 : }
723 : #endif // CONFIG_SUPERTX
724 :
725 : #define USE_TXTYPE_SEARCH_FOR_SUB8X8_IN_CB4X4 1
726 :
727 : #if CONFIG_RECT_TX
728 0 : static INLINE int is_rect_tx(TX_SIZE tx_size) { return tx_size >= TX_SIZES; }
729 : #endif // CONFIG_RECT_TX
730 :
731 : #if CONFIG_EXT_TX
732 : #define ALLOW_INTRA_EXT_TX 1
733 :
734 : typedef enum {
735 : // DCT only
736 : EXT_TX_SET_DCTONLY = 0,
737 : // DCT + Identity only
738 : EXT_TX_SET_DCT_IDTX = 1,
739 : // Discrete Trig transforms w/o flip (4) + Identity (1)
740 : EXT_TX_SET_DTT4_IDTX = 2,
741 : // Discrete Trig transforms w/o flip (4) + Identity (1) + 1D Hor/vert DCT (2)
742 : EXT_TX_SET_DTT4_IDTX_1DDCT = 3,
743 : // Discrete Trig transforms w/ flip (9) + Identity (1) + 1D Hor/Ver DCT (2)
744 : EXT_TX_SET_DTT9_IDTX_1DDCT = 4,
745 : // Discrete Trig transforms w/ flip (9) + Identity (1) + 1D Hor/Ver (6)
746 : EXT_TX_SET_ALL16 = 5,
747 : EXT_TX_SET_TYPES
748 : } TxSetType;
749 :
750 : // Number of transform types in each set type
751 : static const int num_ext_tx_set[EXT_TX_SET_TYPES] = { 1, 2, 5, 7, 12, 16 };
752 :
753 : // Maps intra set index to the set type
754 : static const int ext_tx_set_type_intra[EXT_TX_SETS_INTRA] = {
755 : EXT_TX_SET_DCTONLY, EXT_TX_SET_DTT4_IDTX_1DDCT, EXT_TX_SET_DTT4_IDTX
756 : };
757 :
758 : // Maps inter set index to the set type
759 : static const int ext_tx_set_type_inter[EXT_TX_SETS_INTER] = {
760 : EXT_TX_SET_DCTONLY, EXT_TX_SET_ALL16, EXT_TX_SET_DTT9_IDTX_1DDCT,
761 : EXT_TX_SET_DCT_IDTX
762 : };
763 :
764 : // Maps set types above to the indices used for intra
765 : static const int ext_tx_set_index_intra[EXT_TX_SET_TYPES] = { 0, -1, 2,
766 : 1, -1, -1 };
767 :
768 : // Maps set types above to the indices used for inter
769 : static const int ext_tx_set_index_inter[EXT_TX_SET_TYPES] = {
770 : 0, 3, -1, -1, 2, 1
771 : };
772 :
773 0 : static INLINE TxSetType get_ext_tx_set_type(TX_SIZE tx_size, BLOCK_SIZE bs,
774 : int is_inter, int use_reduced_set) {
775 0 : const TX_SIZE tx_size_sqr_up = txsize_sqr_up_map[tx_size];
776 0 : const TX_SIZE tx_size_sqr = txsize_sqr_map[tx_size];
777 : #if CONFIG_CB4X4 && USE_TXTYPE_SEARCH_FOR_SUB8X8_IN_CB4X4
778 : (void)bs;
779 0 : if (tx_size_sqr > TX_32X32) return EXT_TX_SET_DCTONLY;
780 : #else
781 : if (tx_size_sqr > TX_32X32 || bs < BLOCK_8X8) return EXT_TX_SET_DCTONLY;
782 : #endif
783 0 : if (use_reduced_set)
784 0 : return is_inter ? EXT_TX_SET_DCT_IDTX : EXT_TX_SET_DTT4_IDTX;
785 0 : if (tx_size_sqr_up == TX_32X32)
786 0 : return is_inter ? EXT_TX_SET_DCT_IDTX : EXT_TX_SET_DCTONLY;
787 0 : if (is_inter)
788 : return (tx_size_sqr == TX_16X16 ? EXT_TX_SET_DTT9_IDTX_1DDCT
789 0 : : EXT_TX_SET_ALL16);
790 : else
791 : return (tx_size_sqr == TX_16X16 ? EXT_TX_SET_DTT4_IDTX
792 0 : : EXT_TX_SET_DTT4_IDTX_1DDCT);
793 : }
794 :
795 0 : static INLINE int get_ext_tx_set(TX_SIZE tx_size, BLOCK_SIZE bs, int is_inter,
796 : int use_reduced_set) {
797 0 : const TxSetType set_type =
798 0 : get_ext_tx_set_type(tx_size, bs, is_inter, use_reduced_set);
799 : return is_inter ? ext_tx_set_index_inter[set_type]
800 0 : : ext_tx_set_index_intra[set_type];
801 : }
802 :
803 : static const int use_intra_ext_tx_for_txsize[EXT_TX_SETS_INTRA][EXT_TX_SIZES] =
804 : {
805 : #if CONFIG_CHROMA_2X2
806 : { 1, 1, 1, 1, 1 }, // unused
807 : { 0, 1, 1, 0, 0 },
808 : { 0, 0, 0, 1, 0 },
809 : #else
810 : { 1, 1, 1, 1 }, // unused
811 : { 1, 1, 0, 0 },
812 : { 0, 0, 1, 0 },
813 : #endif // CONFIG_CHROMA_2X2
814 : };
815 :
816 : static const int use_inter_ext_tx_for_txsize[EXT_TX_SETS_INTER][EXT_TX_SIZES] =
817 : {
818 : #if CONFIG_CHROMA_2X2
819 : { 1, 1, 1, 1, 1 }, // unused
820 : { 0, 1, 1, 0, 0 },
821 : { 0, 0, 0, 1, 0 },
822 : { 0, 0, 0, 0, 1 },
823 : #else
824 : { 1, 1, 1, 1 }, // unused
825 : { 1, 1, 0, 0 },
826 : { 0, 0, 1, 0 },
827 : { 0, 0, 0, 1 },
828 : #endif // CONFIG_CHROMA_2X2
829 : };
830 :
831 : // Transform types used in each intra set
832 : static const int ext_tx_used_intra[EXT_TX_SETS_INTRA][TX_TYPES] = {
833 : { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
834 : { 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0 },
835 : { 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
836 : };
837 :
838 : // Numbers of transform types used in each intra set
839 : static const int ext_tx_cnt_intra[EXT_TX_SETS_INTRA] = { 1, 7, 5 };
840 :
841 : // Transform types used in each inter set
842 : static const int ext_tx_used_inter[EXT_TX_SETS_INTER][TX_TYPES] = {
843 : { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
844 : { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
845 : { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0 },
846 : { 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
847 : };
848 :
849 : // Numbers of transform types used in each inter set
850 : static const int ext_tx_cnt_inter[EXT_TX_SETS_INTER] = { 1, 16, 12, 2 };
851 :
852 : // 1D Transforms used in inter set, this needs to be changed if
853 : // ext_tx_used_inter is changed
854 : static const int ext_tx_used_inter_1D[EXT_TX_SETS_INTER][TX_TYPES_1D] = {
855 : { 1, 0, 0, 0 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 0, 0, 1 },
856 : };
857 :
858 0 : static INLINE int get_ext_tx_types(TX_SIZE tx_size, BLOCK_SIZE bs, int is_inter,
859 : int use_reduced_set) {
860 0 : const int set_type =
861 0 : get_ext_tx_set_type(tx_size, bs, is_inter, use_reduced_set);
862 0 : return num_ext_tx_set[set_type];
863 : }
864 :
865 : #if CONFIG_RECT_TX
866 0 : static INLINE int is_rect_tx_allowed_bsize(BLOCK_SIZE bsize) {
867 : static const char LUT[BLOCK_SIZES] = {
868 : #if CONFIG_CB4X4
869 : 0, // BLOCK_2X2
870 : 0, // BLOCK_2X4
871 : 0, // BLOCK_4X2
872 : #endif
873 : 0, // BLOCK_4X4
874 : 1, // BLOCK_4X8
875 : 1, // BLOCK_8X4
876 : 0, // BLOCK_8X8
877 : 1, // BLOCK_8X16
878 : 1, // BLOCK_16X8
879 : 0, // BLOCK_16X16
880 : 1, // BLOCK_16X32
881 : 1, // BLOCK_32X16
882 : 0, // BLOCK_32X32
883 : 0, // BLOCK_32X64
884 : 0, // BLOCK_64X32
885 : 0, // BLOCK_64X64
886 : #if CONFIG_EXT_PARTITION
887 : 0, // BLOCK_64X128
888 : 0, // BLOCK_128X64
889 : 0, // BLOCK_128X128
890 : #endif // CONFIG_EXT_PARTITION
891 : };
892 :
893 0 : return LUT[bsize];
894 : }
895 :
896 0 : static INLINE int is_rect_tx_allowed(const MACROBLOCKD *xd,
897 : const MB_MODE_INFO *mbmi) {
898 0 : return is_rect_tx_allowed_bsize(mbmi->sb_type) &&
899 0 : !xd->lossless[mbmi->segment_id];
900 : }
901 :
902 : #if CONFIG_RECT_TX_EXT
903 : static INLINE int is_quarter_tx_allowed_bsize(BLOCK_SIZE bsize) {
904 : static const char LUT_QTTX[BLOCK_SIZES] = {
905 : #if CONFIG_CB4X4
906 : 0, // BLOCK_2X2
907 : 0, // BLOCK_2X4
908 : 0, // BLOCK_4X2
909 : #endif
910 : 0, // BLOCK_4X4
911 : 0, // BLOCK_4X8
912 : 0, // BLOCK_8X4
913 : 0, // BLOCK_8X8
914 : 1, // BLOCK_8X16
915 : 1, // BLOCK_16X8
916 : 0, // BLOCK_16X16
917 : 0, // BLOCK_16X32
918 : 0, // BLOCK_32X16
919 : 0, // BLOCK_32X32
920 : 0, // BLOCK_32X64
921 : 0, // BLOCK_64X32
922 : 0, // BLOCK_64X64
923 : #if CONFIG_EXT_PARTITION
924 : 0, // BLOCK_64X128
925 : 0, // BLOCK_128X64
926 : 0, // BLOCK_128X128
927 : #endif // CONFIG_EXT_PARTITION
928 : };
929 :
930 : return LUT_QTTX[bsize];
931 : }
932 :
933 : static INLINE int is_quarter_tx_allowed(const MACROBLOCKD *xd,
934 : const MB_MODE_INFO *mbmi,
935 : int is_inter) {
936 : return is_quarter_tx_allowed_bsize(mbmi->sb_type) && is_inter &&
937 : !xd->lossless[mbmi->segment_id];
938 : }
939 : #endif // CONFIG_RECT_TX_EXT
940 : #endif // CONFIG_RECT_TX
941 : #endif // CONFIG_EXT_TX
942 :
943 0 : static INLINE TX_SIZE tx_size_from_tx_mode(BLOCK_SIZE bsize, TX_MODE tx_mode,
944 : int is_inter) {
945 0 : const TX_SIZE largest_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
946 : #if (CONFIG_VAR_TX || CONFIG_EXT_TX) && CONFIG_RECT_TX
947 0 : const TX_SIZE max_rect_tx_size = max_txsize_rect_lookup[bsize];
948 : #else
949 : const TX_SIZE max_tx_size = max_txsize_lookup[bsize];
950 : #endif // (CONFIG_VAR_TX || CONFIG_EXT_TX) && CONFIG_RECT_TX
951 : (void)is_inter;
952 : #if CONFIG_VAR_TX && CONFIG_RECT_TX
953 : #if CONFIG_CB4X4
954 0 : if (bsize == BLOCK_4X4)
955 0 : return AOMMIN(max_txsize_lookup[bsize], largest_tx_size);
956 : #else
957 : if (bsize < BLOCK_8X8)
958 : return AOMMIN(max_txsize_lookup[bsize], largest_tx_size);
959 : #endif
960 0 : if (txsize_sqr_map[max_rect_tx_size] <= largest_tx_size)
961 0 : return max_rect_tx_size;
962 : else
963 0 : return largest_tx_size;
964 : #elif CONFIG_EXT_TX && CONFIG_RECT_TX
965 : if (txsize_sqr_up_map[max_rect_tx_size] <= largest_tx_size) {
966 : return max_rect_tx_size;
967 : } else {
968 : return largest_tx_size;
969 : }
970 : #else
971 : return AOMMIN(max_tx_size, largest_tx_size);
972 : #endif // CONFIG_VAR_TX && CONFIG_RECT_TX
973 : }
974 :
975 : #if CONFIG_EXT_INTRA
976 : #define MAX_ANGLE_DELTA 3
977 : #define ANGLE_STEP 3
978 : extern const int16_t dr_intra_derivative[90];
979 : static const uint8_t mode_to_angle_map[] = {
980 : 0, 90, 180, 45, 135, 111, 157, 203, 67, 0,
981 : #if CONFIG_ALT_INTRA
982 : 0,
983 : #if CONFIG_SMOOTH_HV
984 : 0, 0,
985 : #endif // CONFIG_SMOOTH_HV
986 : #endif // CONFIG_ALT_INTRA
987 : };
988 : #if CONFIG_INTRA_INTERP
989 : // Returns whether filter selection is needed for a given
990 : // intra prediction angle.
991 : int av1_is_intra_filter_switchable(int angle);
992 : #endif // CONFIG_INTRA_INTERP
993 : #endif // CONFIG_EXT_INTRA
994 :
995 : #define FIXED_TX_TYPE 0
996 :
997 : // Converts block_index for given transform size to index of the block in raster
998 : // order.
999 0 : static INLINE int av1_block_index_to_raster_order(TX_SIZE tx_size,
1000 : int block_idx) {
1001 : // For transform size 4x8, the possible block_idx values are 0 & 2, because
1002 : // block_idx values are incremented in steps of size 'tx_width_unit x
1003 : // tx_height_unit'. But, for this transform size, block_idx = 2 corresponds to
1004 : // block number 1 in raster order, inside an 8x8 MI block.
1005 : // For any other transform size, the two indices are equivalent.
1006 0 : return (tx_size == TX_4X8 && block_idx == 2) ? 1 : block_idx;
1007 : }
1008 :
1009 : // Inverse of above function.
1010 : // Note: only implemented for transform sizes 4x4, 4x8 and 8x4 right now.
1011 0 : static INLINE int av1_raster_order_to_block_index(TX_SIZE tx_size,
1012 : int raster_order) {
1013 0 : assert(tx_size == TX_4X4 || tx_size == TX_4X8 || tx_size == TX_8X4);
1014 : // We ensure that block indices are 0 & 2 if tx size is 4x8 or 8x4.
1015 0 : return (tx_size == TX_4X4) ? raster_order : (raster_order > 0) ? 2 : 0;
1016 : }
1017 :
1018 0 : static INLINE TX_TYPE get_default_tx_type(PLANE_TYPE plane_type,
1019 : const MACROBLOCKD *xd, int block_idx,
1020 : TX_SIZE tx_size) {
1021 0 : const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
1022 :
1023 0 : if (is_inter_block(mbmi) || plane_type != PLANE_TYPE_Y ||
1024 0 : xd->lossless[mbmi->segment_id] || tx_size >= TX_32X32)
1025 0 : return DCT_DCT;
1026 :
1027 0 : return intra_mode_to_tx_type_context[plane_type == PLANE_TYPE_Y
1028 0 : ? get_y_mode(xd->mi[0], block_idx)
1029 0 : : mbmi->uv_mode];
1030 : }
1031 :
1032 0 : static INLINE TX_TYPE get_tx_type(PLANE_TYPE plane_type, const MACROBLOCKD *xd,
1033 : int block, TX_SIZE tx_size) {
1034 0 : const MODE_INFO *const mi = xd->mi[0];
1035 0 : const MB_MODE_INFO *const mbmi = &mi->mbmi;
1036 : #if CONFIG_INTRABC && (!CONFIG_EXT_TX || CONFIG_TXK_SEL)
1037 : // TODO(aconverse@google.com): Handle INTRABC + EXT_TX + TXK_SEL
1038 : if (is_intrabc_block(mbmi)) return DCT_DCT;
1039 : #endif // CONFIG_INTRABC && (!CONFIG_EXT_TX || CONFIG_TXK_SEL)
1040 : #if !CONFIG_TXK_SEL
1041 : #if FIXED_TX_TYPE
1042 : const int block_raster_idx = av1_block_index_to_raster_order(tx_size, block);
1043 : return get_default_tx_type(plane_type, xd, block_raster_idx, tx_size);
1044 : #elif CONFIG_EXT_TX
1045 : #if !CONFIG_CB4X4
1046 : const int block_raster_idx = av1_block_index_to_raster_order(tx_size, block);
1047 : #endif // !CONFIG_CB4X4
1048 0 : if (xd->lossless[mbmi->segment_id] || txsize_sqr_map[tx_size] > TX_32X32 ||
1049 0 : (txsize_sqr_map[tx_size] >= TX_32X32 && !is_inter_block(mbmi)))
1050 0 : return DCT_DCT;
1051 : if (mbmi->sb_type >= BLOCK_8X8 || CONFIG_CB4X4) {
1052 0 : if (plane_type == PLANE_TYPE_Y) {
1053 : #if !ALLOW_INTRA_EXT_TX
1054 : if (is_inter_block(mbmi))
1055 : #endif // ALLOW_INTRA_EXT_TX
1056 0 : return mbmi->tx_type;
1057 : }
1058 :
1059 0 : if (is_inter_block(mbmi)) {
1060 : // UV Inter only
1061 : #if CONFIG_CHROMA_2X2
1062 : if (tx_size < TX_4X4) return DCT_DCT;
1063 : #endif
1064 0 : return (mbmi->tx_type == IDTX && txsize_sqr_map[tx_size] >= TX_32X32)
1065 : ? DCT_DCT
1066 0 : : mbmi->tx_type;
1067 : }
1068 : }
1069 :
1070 : #if CONFIG_CB4X4
1071 : (void)block;
1072 : #if CONFIG_CHROMA_2X2
1073 : if (tx_size < TX_4X4)
1074 : return DCT_DCT;
1075 : else
1076 : #endif
1077 0 : return intra_mode_to_tx_type_context[mbmi->uv_mode];
1078 : #else // CONFIG_CB4X4
1079 :
1080 : // Sub8x8-Inter/Intra OR UV-Intra
1081 : if (is_inter_block(mbmi)) // Sub8x8-Inter
1082 : return DCT_DCT;
1083 : else // Sub8x8 Intra OR UV-Intra
1084 : return intra_mode_to_tx_type_context[plane_type == PLANE_TYPE_Y
1085 : ? get_y_mode(mi, block_raster_idx)
1086 : : mbmi->uv_mode];
1087 : #endif // CONFIG_CB4X4
1088 : #else // CONFIG_EXT_TX
1089 : (void)block;
1090 : if (plane_type != PLANE_TYPE_Y || xd->lossless[mbmi->segment_id] ||
1091 : txsize_sqr_map[tx_size] >= TX_32X32)
1092 : return DCT_DCT;
1093 : return mbmi->tx_type;
1094 : #endif // CONFIG_EXT_TX
1095 : #else // !CONFIG_TXK_SEL
1096 : (void)tx_size;
1097 : TX_TYPE tx_type;
1098 : if (plane_type != PLANE_TYPE_Y || xd->lossless[mbmi->segment_id] ||
1099 : mbmi->tx_size >= TX_32X32) {
1100 : tx_type = DCT_DCT;
1101 : } else {
1102 : tx_type = mbmi->txk_type[block];
1103 : }
1104 : assert(tx_type >= DCT_DCT && tx_type < TX_TYPES);
1105 : return tx_type;
1106 : #endif // !CONFIG_TXK_SEL
1107 : }
1108 :
1109 : void av1_setup_block_planes(MACROBLOCKD *xd, int ss_x, int ss_y);
1110 :
1111 0 : static INLINE int tx_size_to_depth(TX_SIZE tx_size) {
1112 0 : return (int)(tx_size - TX_SIZE_LUMA_MIN);
1113 : }
1114 :
1115 0 : static INLINE TX_SIZE depth_to_tx_size(int depth) {
1116 0 : return (TX_SIZE)(depth + TX_SIZE_LUMA_MIN);
1117 : }
1118 :
1119 0 : static INLINE TX_SIZE get_uv_tx_size(const MB_MODE_INFO *mbmi,
1120 : const struct macroblockd_plane *pd) {
1121 : TX_SIZE uv_txsize;
1122 : #if CONFIG_CHROMA_2X2
1123 : assert(mbmi->tx_size > TX_2X2);
1124 : #endif
1125 :
1126 : #if CONFIG_SUPERTX
1127 : if (supertx_enabled(mbmi))
1128 : return uvsupertx_size_lookup[txsize_sqr_map[mbmi->tx_size]]
1129 : [pd->subsampling_x][pd->subsampling_y];
1130 : #endif // CONFIG_SUPERTX
1131 :
1132 0 : uv_txsize = uv_txsize_lookup[mbmi->sb_type][mbmi->tx_size][pd->subsampling_x]
1133 0 : [pd->subsampling_y];
1134 0 : assert(uv_txsize != TX_INVALID);
1135 0 : return uv_txsize;
1136 : }
1137 :
1138 0 : static INLINE TX_SIZE get_tx_size(int plane, const MACROBLOCKD *xd) {
1139 0 : const MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
1140 0 : const MACROBLOCKD_PLANE *pd = &xd->plane[plane];
1141 0 : const TX_SIZE tx_size = plane ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
1142 0 : return tx_size;
1143 : }
1144 :
1145 : static INLINE BLOCK_SIZE
1146 0 : get_plane_block_size(BLOCK_SIZE bsize, const struct macroblockd_plane *pd) {
1147 0 : return ss_size_lookup[bsize][pd->subsampling_x][pd->subsampling_y];
1148 : }
1149 :
1150 : void av1_reset_skip_context(MACROBLOCKD *xd, int mi_row, int mi_col,
1151 : BLOCK_SIZE bsize);
1152 :
1153 : typedef void (*foreach_transformed_block_visitor)(int plane, int block,
1154 : int blk_row, int blk_col,
1155 : BLOCK_SIZE plane_bsize,
1156 : TX_SIZE tx_size, void *arg);
1157 :
1158 : void av1_foreach_transformed_block_in_plane(
1159 : const MACROBLOCKD *const xd, BLOCK_SIZE bsize, int plane,
1160 : foreach_transformed_block_visitor visit, void *arg);
1161 :
1162 : #if CONFIG_LV_MAP
1163 : void av1_foreach_transformed_block(const MACROBLOCKD *const xd,
1164 : BLOCK_SIZE bsize, int mi_row, int mi_col,
1165 : foreach_transformed_block_visitor visit,
1166 : void *arg);
1167 : #endif
1168 :
1169 : #if CONFIG_DAALA_DIST
1170 : void av1_foreach_8x8_transformed_block_in_yplane(
1171 : const MACROBLOCKD *const xd, BLOCK_SIZE bsize,
1172 : foreach_transformed_block_visitor visit,
1173 : foreach_transformed_block_visitor mi_visit, void *arg);
1174 : #endif
1175 :
1176 : #if CONFIG_COEF_INTERLEAVE
1177 : static INLINE int get_max_4x4_size(int num_4x4, int mb_to_edge,
1178 : int subsampling) {
1179 : return num_4x4 + (mb_to_edge >= 0 ? 0 : mb_to_edge >> (5 + subsampling));
1180 : }
1181 :
1182 : void av1_foreach_transformed_block_interleave(
1183 : const MACROBLOCKD *const xd, BLOCK_SIZE bsize,
1184 : foreach_transformed_block_visitor visit, void *arg);
1185 : #endif
1186 :
1187 : void av1_set_contexts(const MACROBLOCKD *xd, struct macroblockd_plane *pd,
1188 : int plane, TX_SIZE tx_size, int has_eob, int aoff,
1189 : int loff);
1190 :
1191 : #if CONFIG_EXT_INTER
1192 0 : static INLINE int is_interintra_allowed_bsize(const BLOCK_SIZE bsize) {
1193 : #if CONFIG_INTERINTRA
1194 : // TODO(debargha): Should this be bsize < BLOCK_LARGEST?
1195 0 : return (bsize >= BLOCK_8X8) && (bsize < BLOCK_64X64);
1196 : #else
1197 : (void)bsize;
1198 : return 0;
1199 : #endif // CONFIG_INTERINTRA
1200 : }
1201 :
1202 0 : static INLINE int is_interintra_allowed_mode(const PREDICTION_MODE mode) {
1203 : #if CONFIG_INTERINTRA
1204 0 : return (mode >= NEARESTMV) && (mode <= NEWMV);
1205 : #else
1206 : (void)mode;
1207 : return 0;
1208 : #endif // CONFIG_INTERINTRA
1209 : }
1210 :
1211 0 : static INLINE int is_interintra_allowed_ref(const MV_REFERENCE_FRAME rf[2]) {
1212 : #if CONFIG_INTERINTRA
1213 0 : return (rf[0] > INTRA_FRAME) && (rf[1] <= INTRA_FRAME);
1214 : #else
1215 : (void)rf;
1216 : return 0;
1217 : #endif // CONFIG_INTERINTRA
1218 : }
1219 :
1220 0 : static INLINE int is_interintra_allowed(const MB_MODE_INFO *mbmi) {
1221 0 : return is_interintra_allowed_bsize(mbmi->sb_type) &&
1222 0 : is_interintra_allowed_mode(mbmi->mode) &&
1223 0 : is_interintra_allowed_ref(mbmi->ref_frame);
1224 : }
1225 :
1226 0 : static INLINE int is_interintra_allowed_bsize_group(int group) {
1227 : int i;
1228 0 : for (i = 0; i < BLOCK_SIZES; i++) {
1229 0 : if (size_group_lookup[i] == group &&
1230 0 : is_interintra_allowed_bsize((BLOCK_SIZE)i)) {
1231 0 : return 1;
1232 : }
1233 : }
1234 0 : return 0;
1235 : }
1236 :
1237 0 : static INLINE int is_interintra_pred(const MB_MODE_INFO *mbmi) {
1238 0 : return (mbmi->ref_frame[1] == INTRA_FRAME) && is_interintra_allowed(mbmi);
1239 : }
1240 : #endif // CONFIG_EXT_INTER
1241 :
1242 : #if CONFIG_VAR_TX
1243 0 : static INLINE int get_vartx_max_txsize(const MB_MODE_INFO *const mbmi,
1244 : BLOCK_SIZE bsize) {
1245 : #if CONFIG_CB4X4
1246 : (void)mbmi;
1247 0 : return max_txsize_rect_lookup[bsize];
1248 : #endif // CONFIG_C4X4
1249 : return mbmi->sb_type < BLOCK_8X8 ? max_txsize_rect_lookup[mbmi->sb_type]
1250 : : max_txsize_rect_lookup[bsize];
1251 : }
1252 : #endif // CONFIG_VAR_TX
1253 :
1254 : #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
1255 0 : static INLINE int is_motion_variation_allowed_bsize(BLOCK_SIZE bsize) {
1256 0 : return (bsize >= BLOCK_8X8);
1257 : }
1258 :
1259 0 : static INLINE int is_motion_variation_allowed_compound(
1260 : const MB_MODE_INFO *mbmi) {
1261 0 : if (!has_second_ref(mbmi))
1262 0 : return 1;
1263 : else
1264 0 : return 0;
1265 : }
1266 :
1267 : #if CONFIG_MOTION_VAR
1268 : // input: log2 of length, 0(4), 1(8), ...
1269 : static const int max_neighbor_obmc[6] = { 0, 1, 2, 3, 4, 4 };
1270 :
1271 0 : static INLINE int check_num_overlappable_neighbors(const MB_MODE_INFO *mbmi) {
1272 0 : return !(mbmi->overlappable_neighbors[0] == 0 &&
1273 0 : mbmi->overlappable_neighbors[1] == 0);
1274 : }
1275 : #endif
1276 :
1277 0 : static INLINE MOTION_MODE motion_mode_allowed(
1278 : #if CONFIG_GLOBAL_MOTION && SEPARATE_GLOBAL_MOTION
1279 : int block, const WarpedMotionParams *gm_params,
1280 : #endif // CONFIG_GLOBAL_MOTION && SEPARATE_GLOBAL_MOTION
1281 : const MODE_INFO *mi) {
1282 0 : const MB_MODE_INFO *mbmi = &mi->mbmi;
1283 : #if CONFIG_GLOBAL_MOTION && SEPARATE_GLOBAL_MOTION
1284 0 : const TransformationType gm_type = gm_params[mbmi->ref_frame[0]].wmtype;
1285 0 : if (is_global_mv_block(mi, block, gm_type)) return SIMPLE_TRANSLATION;
1286 : #endif // CONFIG_GLOBAL_MOTION && SEPARATE_GLOBAL_MOTION
1287 : #if CONFIG_EXT_INTER
1288 0 : if (is_motion_variation_allowed_bsize(mbmi->sb_type) &&
1289 0 : is_inter_mode(mbmi->mode) && mbmi->ref_frame[1] != INTRA_FRAME &&
1290 0 : is_motion_variation_allowed_compound(mbmi)) {
1291 : #else
1292 : if (is_motion_variation_allowed_bsize(mbmi->sb_type) &&
1293 : is_inter_mode(mbmi->mode) && is_motion_variation_allowed_compound(mbmi)) {
1294 : #endif // CONFIG_EXT_INTER
1295 : #if CONFIG_MOTION_VAR
1296 0 : if (!check_num_overlappable_neighbors(mbmi)) return SIMPLE_TRANSLATION;
1297 : #endif
1298 : #if CONFIG_WARPED_MOTION
1299 0 : if (!has_second_ref(mbmi) && mbmi->num_proj_ref[0] >= 1)
1300 0 : return WARPED_CAUSAL;
1301 : else
1302 : #endif // CONFIG_WARPED_MOTION
1303 : #if CONFIG_MOTION_VAR
1304 0 : return OBMC_CAUSAL;
1305 : #else
1306 : return SIMPLE_TRANSLATION;
1307 : #endif // CONFIG_MOTION_VAR
1308 : } else {
1309 0 : return SIMPLE_TRANSLATION;
1310 : }
1311 : }
1312 :
1313 0 : static INLINE void assert_motion_mode_valid(MOTION_MODE mode,
1314 : #if CONFIG_GLOBAL_MOTION && SEPARATE_GLOBAL_MOTION
1315 : int block,
1316 : const WarpedMotionParams *gm_params,
1317 : #endif // CONFIG_GLOBAL_MOTION && SEPARATE_GLOBAL_MOTION
1318 : const MODE_INFO *mi) {
1319 0 : const MOTION_MODE last_motion_mode_allowed = motion_mode_allowed(
1320 : #if CONFIG_GLOBAL_MOTION && SEPARATE_GLOBAL_MOTION
1321 : block, gm_params,
1322 : #endif // CONFIG_GLOBAL_MOTION && SEPARATE_GLOBAL_MOTION
1323 : mi);
1324 : // Check that the input mode is not illegal
1325 0 : if (last_motion_mode_allowed < mode)
1326 0 : assert(0 && "Illegal motion mode selected");
1327 0 : }
1328 :
1329 : #if CONFIG_MOTION_VAR
1330 0 : static INLINE int is_neighbor_overlappable(const MB_MODE_INFO *mbmi) {
1331 0 : return (is_inter_block(mbmi));
1332 : }
1333 : #endif // CONFIG_MOTION_VAR
1334 : #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
1335 :
1336 : // Returns sub-sampled dimensions of the given block.
1337 : // The output values for 'rows_within_bounds' and 'cols_within_bounds' will
1338 : // differ from 'height' and 'width' when part of the block is outside the right
1339 : // and/or bottom image boundary.
1340 0 : static INLINE void av1_get_block_dimensions(BLOCK_SIZE bsize, int plane,
1341 : const MACROBLOCKD *xd, int *width,
1342 : int *height,
1343 : int *rows_within_bounds,
1344 : int *cols_within_bounds) {
1345 0 : const int block_height = block_size_high[bsize];
1346 0 : const int block_width = block_size_wide[bsize];
1347 0 : const int block_rows = (xd->mb_to_bottom_edge >= 0)
1348 : ? block_height
1349 0 : : (xd->mb_to_bottom_edge >> 3) + block_height;
1350 0 : const int block_cols = (xd->mb_to_right_edge >= 0)
1351 : ? block_width
1352 0 : : (xd->mb_to_right_edge >> 3) + block_width;
1353 0 : const struct macroblockd_plane *const pd = &xd->plane[plane];
1354 0 : assert(IMPLIES(plane == PLANE_TYPE_Y, pd->subsampling_x == 0));
1355 0 : assert(IMPLIES(plane == PLANE_TYPE_Y, pd->subsampling_y == 0));
1356 0 : assert(block_width >= block_cols);
1357 0 : assert(block_height >= block_rows);
1358 0 : if (width) *width = block_width >> pd->subsampling_x;
1359 0 : if (height) *height = block_height >> pd->subsampling_y;
1360 0 : if (rows_within_bounds) *rows_within_bounds = block_rows >> pd->subsampling_y;
1361 0 : if (cols_within_bounds) *cols_within_bounds = block_cols >> pd->subsampling_x;
1362 0 : }
1363 :
1364 : #if CONFIG_GLOBAL_MOTION
1365 0 : static INLINE int is_nontrans_global_motion(const MACROBLOCKD *xd) {
1366 0 : const MODE_INFO *mi = xd->mi[0];
1367 0 : const MB_MODE_INFO *const mbmi = &mi->mbmi;
1368 : int ref;
1369 : #if CONFIG_CB4X4
1370 0 : const int unify_bsize = 1;
1371 : #else
1372 : const int unify_bsize = 0;
1373 : #endif
1374 :
1375 : // First check if all modes are ZEROMV
1376 0 : if (mbmi->sb_type >= BLOCK_8X8 || unify_bsize) {
1377 : #if CONFIG_EXT_INTER
1378 0 : if (mbmi->mode != ZEROMV && mbmi->mode != ZERO_ZEROMV) return 0;
1379 : #else
1380 : if (mbmi->mode != ZEROMV) return 0;
1381 : #endif // CONFIG_EXT_INTER
1382 : } else {
1383 : #if CONFIG_EXT_INTER
1384 0 : if (mi->bmi[0].as_mode != ZEROMV || mi->bmi[1].as_mode != ZEROMV ||
1385 0 : mi->bmi[2].as_mode != ZEROMV || mi->bmi[3].as_mode != ZEROMV ||
1386 0 : mi->bmi[0].as_mode != ZERO_ZEROMV ||
1387 0 : mi->bmi[1].as_mode != ZERO_ZEROMV ||
1388 0 : mi->bmi[2].as_mode != ZERO_ZEROMV || mi->bmi[3].as_mode != ZERO_ZEROMV)
1389 0 : return 0;
1390 : #else
1391 : if (mi->bmi[0].as_mode != ZEROMV || mi->bmi[1].as_mode != ZEROMV ||
1392 : mi->bmi[2].as_mode != ZEROMV || mi->bmi[3].as_mode != ZEROMV)
1393 : return 0;
1394 : #endif // CONFIG_EXT_INTER
1395 : }
1396 :
1397 : #if !GLOBAL_SUB8X8_USED
1398 0 : if (mbmi->sb_type < BLOCK_8X8) return 0;
1399 : #endif
1400 :
1401 : // Now check if all global motion is non translational
1402 0 : for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
1403 0 : if (xd->global_motion[mbmi->ref_frame[ref]].wmtype <= TRANSLATION) return 0;
1404 : }
1405 0 : return 1;
1406 : }
1407 : #endif // CONFIG_GLOBAL_MOTION
1408 :
1409 0 : static INLINE PLANE_TYPE get_plane_type(int plane) {
1410 0 : return (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
1411 : }
1412 :
1413 : #ifdef __cplusplus
1414 : } // extern "C"
1415 : #endif
1416 :
1417 : #endif // AV1_COMMON_BLOCKD_H_
|