av1_common_int.h (66195B)
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 <stdbool.h> 16 17 #include "config/aom_config.h" 18 #include "config/av1_rtcd.h" 19 20 #include "aom/internal/aom_codec_internal.h" 21 #include "aom_dsp/flow_estimation/corner_detect.h" 22 #include "aom_util/aom_pthread.h" 23 #include "av1/common/alloccommon.h" 24 #include "av1/common/av1_loopfilter.h" 25 #include "av1/common/entropy.h" 26 #include "av1/common/entropymode.h" 27 #include "av1/common/entropymv.h" 28 #include "av1/common/enums.h" 29 #include "av1/common/frame_buffers.h" 30 #include "av1/common/mv.h" 31 #include "av1/common/quant_common.h" 32 #include "av1/common/restoration.h" 33 #include "av1/common/tile_common.h" 34 #include "av1/common/timing.h" 35 #include "aom_dsp/grain_params.h" 36 #include "aom_dsp/grain_table.h" 37 #include "aom_dsp/odintrin.h" 38 #ifdef __cplusplus 39 extern "C" { 40 #endif 41 42 #if defined(__clang__) && defined(__has_warning) 43 #if __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough") 44 #define AOM_FALLTHROUGH_INTENDED [[clang::fallthrough]] // NOLINT 45 #endif 46 #elif defined(__GNUC__) && __GNUC__ >= 7 47 #define AOM_FALLTHROUGH_INTENDED __attribute__((fallthrough)) // NOLINT 48 #endif 49 50 #ifndef AOM_FALLTHROUGH_INTENDED 51 #define AOM_FALLTHROUGH_INTENDED \ 52 do { \ 53 } while (0) 54 #endif 55 56 #define CDEF_MAX_STRENGTHS 16 57 58 /* Constant values while waiting for the sequence header */ 59 #define FRAME_ID_LENGTH 15 60 #define DELTA_FRAME_ID_LENGTH 14 61 62 #define FRAME_CONTEXTS (FRAME_BUFFERS + 1) 63 // Extra frame context which is always kept at default values 64 #define FRAME_CONTEXT_DEFAULTS (FRAME_CONTEXTS - 1) 65 #define PRIMARY_REF_BITS 3 66 #define PRIMARY_REF_NONE 7 67 68 #define NUM_PING_PONG_BUFFERS 2 69 70 #define MAX_NUM_TEMPORAL_LAYERS 8 71 #define MAX_NUM_SPATIAL_LAYERS 4 72 /* clang-format off */ 73 // clang-format seems to think this is a pointer dereference and not a 74 // multiplication. 75 #define MAX_NUM_OPERATING_POINTS \ 76 (MAX_NUM_TEMPORAL_LAYERS * MAX_NUM_SPATIAL_LAYERS) 77 /* clang-format on */ 78 79 // TODO(jingning): Turning this on to set up transform coefficient 80 // processing timer. 81 #define TXCOEFF_TIMER 0 82 #define TXCOEFF_COST_TIMER 0 83 84 /*!\cond */ 85 86 enum { 87 SINGLE_REFERENCE = 0, 88 COMPOUND_REFERENCE = 1, 89 REFERENCE_MODE_SELECT = 2, 90 REFERENCE_MODES = 3, 91 } UENUM1BYTE(REFERENCE_MODE); 92 93 enum { 94 /** 95 * Frame context updates are disabled 96 */ 97 REFRESH_FRAME_CONTEXT_DISABLED, 98 /** 99 * Update frame context to values resulting from backward probability 100 * updates based on entropy/counts in the decoded frame 101 */ 102 REFRESH_FRAME_CONTEXT_BACKWARD, 103 } UENUM1BYTE(REFRESH_FRAME_CONTEXT_MODE); 104 105 #define MFMV_STACK_SIZE 3 106 typedef struct { 107 int_mv mfmv0; 108 uint8_t ref_frame_offset; 109 } TPL_MV_REF; 110 111 typedef struct { 112 int_mv mv; 113 MV_REFERENCE_FRAME ref_frame; 114 } MV_REF; 115 116 typedef struct RefCntBuffer { 117 // For a RefCntBuffer, the following are reference-holding variables: 118 // - cm->ref_frame_map[] 119 // - cm->cur_frame 120 // - cm->scaled_ref_buf[] (encoder only) 121 // - pbi->output_frame_index[] (decoder only) 122 // With that definition, 'ref_count' is the number of reference-holding 123 // variables that are currently referencing this buffer. 124 // For example: 125 // - suppose this buffer is at index 'k' in the buffer pool, and 126 // - Total 'n' of the variables / array elements above have value 'k' (that 127 // is, they are pointing to buffer at index 'k'). 128 // Then, pool->frame_bufs[k].ref_count = n. 129 int ref_count; 130 131 unsigned int order_hint; 132 unsigned int ref_order_hints[INTER_REFS_PER_FRAME]; 133 134 // These variables are used only in encoder and compare the absolute 135 // display order hint to compute the relative distance and overcome 136 // the limitation of get_relative_dist() which returns incorrect 137 // distance when a very old frame is used as a reference. 138 unsigned int display_order_hint; 139 unsigned int ref_display_order_hint[INTER_REFS_PER_FRAME]; 140 // Frame's level within the hierarchical structure. 141 unsigned int pyramid_level; 142 MV_REF *mvs; 143 uint8_t *seg_map; 144 struct segmentation seg; 145 int mi_rows; 146 int mi_cols; 147 // Width and height give the size of the buffer (before any upscaling, unlike 148 // the sizes that can be derived from the buf structure) 149 int width; 150 int height; 151 WarpedMotionParams global_motion[REF_FRAMES]; 152 int showable_frame; // frame can be used as show existing frame in future 153 uint8_t film_grain_params_present; 154 aom_film_grain_t film_grain_params; 155 aom_codec_frame_buffer_t raw_frame_buffer; 156 YV12_BUFFER_CONFIG buf; 157 int temporal_id; // Temporal layer ID of the frame 158 int spatial_id; // Spatial layer ID of the frame 159 FRAME_TYPE frame_type; 160 161 // This is only used in the encoder but needs to be indexed per ref frame 162 // so it's extremely convenient to keep it here. 163 int interp_filter_selected[SWITCHABLE]; 164 165 // Inter frame reference frame delta for loop filter 166 int8_t ref_deltas[REF_FRAMES]; 167 168 // 0 = ZERO_MV, MV 169 int8_t mode_deltas[MAX_MODE_LF_DELTAS]; 170 171 FRAME_CONTEXT frame_context; 172 173 int filter_level[2]; 174 } RefCntBuffer; 175 176 typedef struct BufferPool { 177 // Protect BufferPool from being accessed by several FrameWorkers at 178 // the same time during frame parallel decode. 179 // TODO(hkuang): Try to use atomic variable instead of locking the whole pool. 180 // TODO(wtc): Remove this. See 181 // https://chromium-review.googlesource.com/c/webm/libvpx/+/560630. 182 #if CONFIG_MULTITHREAD 183 pthread_mutex_t pool_mutex; 184 #endif 185 186 // Private data associated with the frame buffer callbacks. 187 void *cb_priv; 188 189 aom_get_frame_buffer_cb_fn_t get_fb_cb; 190 aom_release_frame_buffer_cb_fn_t release_fb_cb; 191 192 RefCntBuffer *frame_bufs; 193 uint8_t num_frame_bufs; 194 195 // Frame buffers allocated internally by the codec. 196 InternalFrameBufferList int_frame_buffers; 197 } BufferPool; 198 199 /*!\endcond */ 200 201 /*!\brief Parameters related to CDEF */ 202 typedef struct { 203 //! CDEF column line buffer 204 uint16_t *colbuf[MAX_MB_PLANE]; 205 //! CDEF top & bottom line buffer 206 uint16_t *linebuf[MAX_MB_PLANE]; 207 //! CDEF intermediate buffer 208 uint16_t *srcbuf; 209 //! CDEF column line buffer sizes 210 size_t allocated_colbuf_size[MAX_MB_PLANE]; 211 //! CDEF top and bottom line buffer sizes 212 size_t allocated_linebuf_size[MAX_MB_PLANE]; 213 //! CDEF intermediate buffer size 214 size_t allocated_srcbuf_size; 215 //! CDEF damping factor 216 int cdef_damping; 217 //! Number of CDEF strength values 218 int nb_cdef_strengths; 219 //! CDEF strength values for luma 220 int cdef_strengths[CDEF_MAX_STRENGTHS]; 221 //! CDEF strength values for chroma 222 int cdef_uv_strengths[CDEF_MAX_STRENGTHS]; 223 //! Number of CDEF strength values in bits 224 int cdef_bits; 225 //! Number of rows in the frame in 4 pixel 226 int allocated_mi_rows; 227 //! Number of CDEF workers 228 int allocated_num_workers; 229 } CdefInfo; 230 231 /*!\cond */ 232 233 typedef struct { 234 int delta_q_present_flag; 235 // Resolution of delta quant 236 int delta_q_res; 237 int delta_lf_present_flag; 238 // Resolution of delta lf level 239 int delta_lf_res; 240 // This is a flag for number of deltas of loop filter level 241 // 0: use 1 delta, for y_vertical, y_horizontal, u, and v 242 // 1: use separate deltas for each filter level 243 int delta_lf_multi; 244 } DeltaQInfo; 245 246 typedef struct { 247 int enable_order_hint; // 0 - disable order hint, and related tools 248 int order_hint_bits_minus_1; // dist_wtd_comp, ref_frame_mvs, 249 // frame_sign_bias 250 // if 0, enable_dist_wtd_comp and 251 // enable_ref_frame_mvs must be set as 0. 252 int enable_dist_wtd_comp; // 0 - disable dist-wtd compound modes 253 // 1 - enable it 254 int enable_ref_frame_mvs; // 0 - disable ref frame mvs 255 // 1 - enable it 256 } OrderHintInfo; 257 258 // Sequence header structure. 259 // Note: All syntax elements of sequence_header_obu that need to be 260 // bit-identical across multiple sequence headers must be part of this struct, 261 // so that consistency is checked by are_seq_headers_consistent() function. 262 // One exception is the last member 'op_params' that is ignored by 263 // are_seq_headers_consistent() function. 264 typedef struct SequenceHeader { 265 int num_bits_width; 266 int num_bits_height; 267 int max_frame_width; 268 int max_frame_height; 269 // Whether current and reference frame IDs are signaled in the bitstream. 270 // Frame id numbers are additional information that do not affect the 271 // decoding process, but provide decoders with a way of detecting missing 272 // reference frames so that appropriate action can be taken. 273 uint8_t frame_id_numbers_present_flag; 274 int frame_id_length; 275 int delta_frame_id_length; 276 BLOCK_SIZE sb_size; // Size of the superblock used for this frame 277 int mib_size; // Size of the superblock in units of MI blocks 278 int mib_size_log2; // Log 2 of above. 279 280 OrderHintInfo order_hint_info; 281 282 uint8_t force_screen_content_tools; // 0 - force off 283 // 1 - force on 284 // 2 - adaptive 285 uint8_t still_picture; // Video is a single frame still picture 286 uint8_t reduced_still_picture_hdr; // Use reduced header for still picture 287 uint8_t force_integer_mv; // 0 - Don't force. MV can use subpel 288 // 1 - force to integer 289 // 2 - adaptive 290 uint8_t enable_filter_intra; // enables/disables filterintra 291 uint8_t enable_intra_edge_filter; // enables/disables edge upsampling 292 uint8_t enable_interintra_compound; // enables/disables interintra_compound 293 uint8_t enable_masked_compound; // enables/disables masked compound 294 uint8_t enable_dual_filter; // 0 - disable dual interpolation filter 295 // 1 - enable vert/horz filter selection 296 uint8_t enable_warped_motion; // 0 - disable warp for the sequence 297 // 1 - enable warp for the sequence 298 uint8_t enable_superres; // 0 - Disable superres for the sequence 299 // and no frame level superres flag 300 // 1 - Enable superres for the sequence 301 // enable per-frame superres flag 302 uint8_t enable_cdef; // To turn on/off CDEF 303 uint8_t enable_restoration; // To turn on/off loop restoration 304 BITSTREAM_PROFILE profile; 305 306 // Color config. 307 aom_bit_depth_t bit_depth; // AOM_BITS_8 in profile 0 or 1, 308 // AOM_BITS_10 or AOM_BITS_12 in profile 2 or 3. 309 uint8_t use_highbitdepth; // If true, we need to use 16bit frame buffers. 310 uint8_t monochrome; // Monochrome video 311 aom_color_primaries_t color_primaries; 312 aom_transfer_characteristics_t transfer_characteristics; 313 aom_matrix_coefficients_t matrix_coefficients; 314 int color_range; 315 int subsampling_x; // Chroma subsampling for x 316 int subsampling_y; // Chroma subsampling for y 317 aom_chroma_sample_position_t chroma_sample_position; 318 uint8_t separate_uv_delta_q; 319 uint8_t film_grain_params_present; 320 321 // Operating point info. 322 int operating_points_cnt_minus_1; 323 int operating_point_idc[MAX_NUM_OPERATING_POINTS]; 324 // True if operating_point_idc[op] is not equal to 0 for any value of op from 325 // 0 to operating_points_cnt_minus_1. 326 bool has_nonzero_operating_point_idc; 327 int timing_info_present; 328 aom_timing_info_t timing_info; 329 uint8_t decoder_model_info_present_flag; 330 aom_dec_model_info_t decoder_model_info; 331 uint8_t display_model_info_present_flag; 332 AV1_LEVEL seq_level_idx[MAX_NUM_OPERATING_POINTS]; 333 uint8_t tier[MAX_NUM_OPERATING_POINTS]; // seq_tier in spec. One bit: 0 or 1. 334 335 // IMPORTANT: the op_params member must be at the end of the struct so that 336 // are_seq_headers_consistent() can be implemented with a memcmp() call. 337 // TODO(urvang): We probably don't need the +1 here. 338 aom_dec_model_op_parameters_t op_params[MAX_NUM_OPERATING_POINTS + 1]; 339 } SequenceHeader; 340 341 typedef struct { 342 int skip_mode_allowed; 343 int skip_mode_flag; 344 int ref_frame_idx_0; 345 int ref_frame_idx_1; 346 } SkipModeInfo; 347 348 typedef struct { 349 FRAME_TYPE frame_type; 350 REFERENCE_MODE reference_mode; 351 352 unsigned int order_hint; 353 unsigned int display_order_hint; 354 // Frame's level within the hierarchical structure. 355 unsigned int pyramid_level; 356 unsigned int frame_number; 357 SkipModeInfo skip_mode_info; 358 int refresh_frame_flags; // Which ref frames are overwritten by this frame 359 int frame_refs_short_signaling; 360 } CurrentFrame; 361 362 /*!\endcond */ 363 364 /*! 365 * \brief Frame level features. 366 */ 367 typedef struct { 368 /*! 369 * If true, CDF update in the symbol encoding/decoding process is disabled. 370 */ 371 bool disable_cdf_update; 372 /*! 373 * If true, motion vectors are specified to eighth pel precision; and 374 * if false, motion vectors are specified to quarter pel precision. 375 */ 376 bool allow_high_precision_mv; 377 /*! 378 * If true, force integer motion vectors; if false, use the default. 379 */ 380 bool cur_frame_force_integer_mv; 381 /*! 382 * If true, palette tool and/or intra block copy tools may be used. 383 */ 384 bool allow_screen_content_tools; 385 bool allow_intrabc; /*!< If true, intra block copy tool may be used. */ 386 bool allow_warped_motion; /*!< If true, frame may use warped motion mode. */ 387 /*! 388 * If true, using previous frames' motion vectors for prediction is allowed. 389 */ 390 bool allow_ref_frame_mvs; 391 /*! 392 * If true, frame is fully lossless at coded resolution. 393 * */ 394 bool coded_lossless; 395 /*! 396 * If true, frame is fully lossless at upscaled resolution. 397 */ 398 bool all_lossless; 399 /*! 400 * If true, the frame is restricted to a reduced subset of the full set of 401 * transform types. 402 */ 403 bool reduced_tx_set_used; 404 /*! 405 * If true, error resilient mode is enabled. 406 * Note: Error resilient mode allows the syntax of a frame to be parsed 407 * independently of previously decoded frames. 408 */ 409 bool error_resilient_mode; 410 /*! 411 * If false, only MOTION_MODE that may be used is SIMPLE_TRANSLATION; 412 * if true, all MOTION_MODES may be used. 413 */ 414 bool switchable_motion_mode; 415 TX_MODE tx_mode; /*!< Transform mode at frame level. */ 416 InterpFilter interp_filter; /*!< Interpolation filter at frame level. */ 417 /*! 418 * The reference frame that contains the CDF values and other state that 419 * should be loaded at the start of the frame. 420 */ 421 int primary_ref_frame; 422 /*! 423 * Byte alignment of the planes in the reference buffers. 424 */ 425 int byte_alignment; 426 /*! 427 * Flag signaling how frame contexts should be updated at the end of 428 * a frame decode. 429 */ 430 REFRESH_FRAME_CONTEXT_MODE refresh_frame_context; 431 } FeatureFlags; 432 433 /*! 434 * \brief Params related to tiles. 435 */ 436 typedef struct CommonTileParams { 437 int cols; /*!< number of tile columns that frame is divided into */ 438 int rows; /*!< number of tile rows that frame is divided into */ 439 int max_width_sb; /*!< maximum tile width in superblock units. */ 440 int max_height_sb; /*!< maximum tile height in superblock units. */ 441 442 /*! 443 * Min width of non-rightmost tile in MI units. Only valid if cols > 1. 444 */ 445 int min_inner_width; 446 447 /*! 448 * If true, tiles are uniformly spaced with power-of-two number of rows and 449 * columns. 450 * If false, tiles have explicitly configured widths and heights. 451 */ 452 int uniform_spacing; 453 454 /** 455 * \name Members only valid when uniform_spacing == 1 456 */ 457 /**@{*/ 458 int log2_cols; /*!< log2 of 'cols'. */ 459 int log2_rows; /*!< log2 of 'rows'. */ 460 int width; /*!< tile width in MI units */ 461 int height; /*!< tile height in MI units */ 462 /**@}*/ 463 464 /*! 465 * Min num of tile columns possible based on 'max_width_sb' and frame width. 466 */ 467 int min_log2_cols; 468 /*! 469 * Min num of tile rows possible based on 'max_height_sb' and frame height. 470 */ 471 int min_log2_rows; 472 /*! 473 * Max num of tile columns possible based on frame width. 474 */ 475 int max_log2_cols; 476 /*! 477 * Max num of tile rows possible based on frame height. 478 */ 479 int max_log2_rows; 480 /*! 481 * log2 of min number of tiles (same as min_log2_cols + min_log2_rows). 482 */ 483 int min_log2; 484 /*! 485 * col_start_sb[i] is the start position of tile column i in superblock units. 486 * valid for 0 <= i <= cols 487 */ 488 int col_start_sb[MAX_TILE_COLS + 1]; 489 /*! 490 * row_start_sb[i] is the start position of tile row i in superblock units. 491 * valid for 0 <= i <= rows 492 */ 493 int row_start_sb[MAX_TILE_ROWS + 1]; 494 /*! 495 * If true, we are using large scale tile mode. 496 */ 497 unsigned int large_scale; 498 /*! 499 * Only relevant when large_scale == 1. 500 * If true, the independent decoding of a single tile or a section of a frame 501 * is allowed. 502 */ 503 unsigned int single_tile_decoding; 504 } CommonTileParams; 505 506 typedef struct CommonModeInfoParams CommonModeInfoParams; 507 /*! 508 * \brief Params related to MB_MODE_INFO arrays and related info. 509 */ 510 struct CommonModeInfoParams { 511 /*! 512 * Number of rows in the frame in 16 pixel units. 513 * This is computed from frame height aligned to a multiple of 8. 514 */ 515 int mb_rows; 516 /*! 517 * Number of cols in the frame in 16 pixel units. 518 * This is computed from frame width aligned to a multiple of 8. 519 */ 520 int mb_cols; 521 522 /*! 523 * Total MBs = mb_rows * mb_cols. 524 */ 525 int MBs; 526 527 /*! 528 * Number of rows in the frame in 4 pixel (MB_MODE_INFO) units. 529 * This is computed from frame height aligned to a multiple of 8. 530 */ 531 int mi_rows; 532 /*! 533 * Number of cols in the frame in 4 pixel (MB_MODE_INFO) units. 534 * This is computed from frame width aligned to a multiple of 8. 535 */ 536 int mi_cols; 537 538 /*! 539 * An array of MB_MODE_INFO structs for every 'mi_alloc_bsize' sized block 540 * in the frame. 541 * Note: This array should be treated like a scratch memory, and should NOT be 542 * accessed directly, in most cases. Please use 'mi_grid_base' array instead. 543 */ 544 MB_MODE_INFO *mi_alloc; 545 /*! 546 * Number of allocated elements in 'mi_alloc'. 547 */ 548 int mi_alloc_size; 549 /*! 550 * Stride for 'mi_alloc' array. 551 */ 552 int mi_alloc_stride; 553 /*! 554 * The minimum block size that each element in 'mi_alloc' can correspond to. 555 * For decoder, this is always BLOCK_4X4. 556 * For encoder, this is BLOCK_8X8 for resolution >= 4k case or REALTIME mode 557 * case. Otherwise, this is BLOCK_4X4. 558 */ 559 BLOCK_SIZE mi_alloc_bsize; 560 561 /*! 562 * Grid of pointers to 4x4 MB_MODE_INFO structs allocated in 'mi_alloc'. 563 * It's possible that: 564 * - Multiple pointers in the grid point to the same element in 'mi_alloc' 565 * (for example, for all 4x4 blocks that belong to the same partition block). 566 * - Some pointers can be NULL (for example, for blocks outside visible area). 567 */ 568 MB_MODE_INFO **mi_grid_base; 569 /*! 570 * Number of allocated elements in 'mi_grid_base' (and 'tx_type_map' also). 571 */ 572 int mi_grid_size; 573 /*! 574 * Stride for 'mi_grid_base' (and 'tx_type_map' also). 575 */ 576 int mi_stride; 577 578 /*! 579 * An array of tx types for each 4x4 block in the frame. 580 * Number of allocated elements is same as 'mi_grid_size', and stride is 581 * same as 'mi_grid_size'. So, indexing into 'tx_type_map' is same as that of 582 * 'mi_grid_base'. 583 */ 584 TX_TYPE *tx_type_map; 585 586 /** 587 * \name Function pointers to allow separate logic for encoder and decoder. 588 */ 589 /**@{*/ 590 /*! 591 * Free the memory allocated to arrays in 'mi_params'. 592 * \param[in,out] mi_params object containing common mode info parameters 593 */ 594 void (*free_mi)(struct CommonModeInfoParams *mi_params); 595 /*! 596 * Initialize / reset appropriate arrays in 'mi_params'. 597 * \param[in,out] mi_params object containing common mode info parameters 598 */ 599 void (*setup_mi)(struct CommonModeInfoParams *mi_params); 600 /*! 601 * Allocate required memory for arrays in 'mi_params'. 602 * \param[in,out] mi_params object containing common mode info 603 * parameters 604 * \param width frame width 605 * \param height frame height 606 * \param min_partition_size minimum partition size allowed while 607 * encoding 608 */ 609 void (*set_mb_mi)(struct CommonModeInfoParams *mi_params, int width, 610 int height, BLOCK_SIZE min_partition_size); 611 /**@}*/ 612 }; 613 614 typedef struct CommonQuantParams CommonQuantParams; 615 /*! 616 * \brief Parameters related to quantization at the frame level. 617 */ 618 struct CommonQuantParams { 619 /*! 620 * Base qindex of the frame in the range 0 to 255. 621 */ 622 int base_qindex; 623 624 /*! 625 * Sharpness adjustment in the quantization process. 626 */ 627 int sharpness; 628 629 /*! 630 * Delta of qindex (from base_qindex) for Y plane DC coefficient. 631 * Note: y_ac_delta_q is implicitly 0. 632 */ 633 int y_dc_delta_q; 634 635 /*! 636 * Delta of qindex (from base_qindex) for U plane DC coefficients. 637 */ 638 int u_dc_delta_q; 639 /*! 640 * Delta of qindex (from base_qindex) for U plane AC coefficients. 641 */ 642 int v_dc_delta_q; 643 644 /*! 645 * Delta of qindex (from base_qindex) for V plane DC coefficients. 646 * Same as those for U plane if cm->seq_params->separate_uv_delta_q == 0. 647 */ 648 int u_ac_delta_q; 649 /*! 650 * Delta of qindex (from base_qindex) for V plane AC coefficients. 651 * Same as those for U plane if cm->seq_params->separate_uv_delta_q == 0. 652 */ 653 int v_ac_delta_q; 654 655 /* 656 * Note: The qindex per superblock may have a delta from the qindex obtained 657 * at frame level from parameters above, based on 'cm->delta_q_info'. 658 */ 659 660 /** 661 * \name True dequantizers. 662 * The dequantizers below are true dequantizers used only in the 663 * dequantization process. They have the same coefficient 664 * shift/scale as TX. 665 */ 666 /**@{*/ 667 int16_t y_dequant_QTX[MAX_SEGMENTS][2]; /*!< Dequant for Y plane */ 668 int16_t u_dequant_QTX[MAX_SEGMENTS][2]; /*!< Dequant for U plane */ 669 int16_t v_dequant_QTX[MAX_SEGMENTS][2]; /*!< Dequant for V plane */ 670 /**@}*/ 671 672 /** 673 * \name Global quantization matrix tables. 674 */ 675 /**@{*/ 676 /*! 677 * Global dequantization matrix table. 678 */ 679 const qm_val_t *giqmatrix[NUM_QM_LEVELS][3][TX_SIZES_ALL]; 680 /*! 681 * Global quantization matrix table. 682 */ 683 const qm_val_t *gqmatrix[NUM_QM_LEVELS][3][TX_SIZES_ALL]; 684 /**@}*/ 685 686 /** 687 * \name Local dequantization matrix tables for each frame. 688 */ 689 /**@{*/ 690 /*! 691 * Local dequant matrix for Y plane. 692 */ 693 const qm_val_t *y_iqmatrix[MAX_SEGMENTS][TX_SIZES_ALL]; 694 /*! 695 * Local dequant matrix for U plane. 696 */ 697 const qm_val_t *u_iqmatrix[MAX_SEGMENTS][TX_SIZES_ALL]; 698 /*! 699 * Local dequant matrix for V plane. 700 */ 701 const qm_val_t *v_iqmatrix[MAX_SEGMENTS][TX_SIZES_ALL]; 702 /**@}*/ 703 704 /*! 705 * Flag indicating whether quantization matrices are being used: 706 * - If true, qm_level_y, qm_level_u and qm_level_v indicate the level 707 * indices to be used to access appropriate global quant matrix tables. 708 * - If false, we implicitly use level index 'NUM_QM_LEVELS - 1'. 709 */ 710 bool using_qmatrix; 711 /** 712 * \name Valid only when using_qmatrix == true 713 * Indicate the level indices to be used to access appropriate global quant 714 * matrix tables. 715 */ 716 /**@{*/ 717 int qmatrix_level_y; /*!< Level index for Y plane */ 718 int qmatrix_level_u; /*!< Level index for U plane */ 719 int qmatrix_level_v; /*!< Level index for V plane */ 720 /**@}*/ 721 }; 722 723 typedef struct CommonContexts CommonContexts; 724 /*! 725 * \brief Contexts used for transmitting various symbols in the bitstream. 726 */ 727 struct CommonContexts { 728 /*! 729 * Context used by 'FRAME_CONTEXT.partition_cdf' to transmit partition type. 730 * partition[i][j] is the context for ith tile row, jth mi_col. 731 */ 732 PARTITION_CONTEXT **partition; 733 734 /*! 735 * Context used to derive context for multiple symbols: 736 * - 'TXB_CTX.txb_skip_ctx' used by 'FRAME_CONTEXT.txb_skip_cdf' to transmit 737 * to transmit skip_txfm flag. 738 * - 'TXB_CTX.dc_sign_ctx' used by 'FRAME_CONTEXT.dc_sign_cdf' to transmit 739 * sign. 740 * entropy[i][j][k] is the context for ith plane, jth tile row, kth mi_col. 741 */ 742 ENTROPY_CONTEXT **entropy[MAX_MB_PLANE]; 743 744 /*! 745 * Context used to derive context for 'FRAME_CONTEXT.txfm_partition_cdf' to 746 * transmit 'is_split' flag to indicate if this transform block should be 747 * split into smaller sub-blocks. 748 * txfm[i][j] is the context for ith tile row, jth mi_col. 749 */ 750 TXFM_CONTEXT **txfm; 751 752 /*! 753 * Dimensions that were used to allocate the arrays above. 754 * If these dimensions change, the arrays may have to be re-allocated. 755 */ 756 int num_planes; /*!< Corresponds to av1_num_planes(cm) */ 757 int num_tile_rows; /*!< Corresponds to cm->tiles.row */ 758 int num_mi_cols; /*!< Corresponds to cm->mi_params.mi_cols */ 759 }; 760 761 /*! 762 * \brief Top level common structure used by both encoder and decoder. 763 */ 764 typedef struct AV1Common { 765 /*! 766 * Information about the current frame that is being coded. 767 */ 768 CurrentFrame current_frame; 769 /*! 770 * Code and details about current error status. 771 */ 772 struct aom_internal_error_info *error; 773 774 /*! 775 * AV1 allows two types of frame scaling operations: 776 * 1. Frame super-resolution: that allows coding a frame at lower resolution 777 * and after decoding the frame, normatively scales and restores the frame -- 778 * inside the coding loop. 779 * 2. Frame resize: that allows coding frame at lower/higher resolution, and 780 * then non-normatively upscale the frame at the time of rendering -- outside 781 * the coding loop. 782 * Hence, the need for 3 types of dimensions. 783 */ 784 785 /** 786 * \name Coded frame dimensions. 787 */ 788 /**@{*/ 789 int width; /*!< Coded frame width */ 790 int height; /*!< Coded frame height */ 791 /**@}*/ 792 793 /** 794 * \name Rendered frame dimensions. 795 * Dimensions after applying both super-resolution and resize to the coded 796 * frame. Different from coded dimensions if super-resolution and/or resize 797 * are being used for this frame. 798 */ 799 /**@{*/ 800 int render_width; /*!< Rendered frame width */ 801 int render_height; /*!< Rendered frame height */ 802 /**@}*/ 803 804 /** 805 * \name Super-resolved frame dimensions. 806 * Frame dimensions after applying super-resolution to the coded frame (if 807 * present), but before applying resize. 808 * Larger than the coded dimensions if super-resolution is being used for 809 * this frame. 810 * Different from rendered dimensions if resize is being used for this frame. 811 */ 812 /**@{*/ 813 int superres_upscaled_width; /*!< Super-resolved frame width */ 814 int superres_upscaled_height; /*!< Super-resolved frame height */ 815 /**@}*/ 816 817 /*! 818 * The denominator of the superres scale used by this frame. 819 * Note: The numerator is fixed to be SCALE_NUMERATOR. 820 */ 821 uint8_t superres_scale_denominator; 822 823 /*! 824 * buffer_removal_times[op_num] specifies the frame removal time in units of 825 * DecCT clock ticks counted from the removal time of the last random access 826 * point for operating point op_num. 827 * TODO(urvang): We probably don't need the +1 here. 828 */ 829 uint32_t buffer_removal_times[MAX_NUM_OPERATING_POINTS + 1]; 830 /*! 831 * Presentation time of the frame in clock ticks DispCT counted from the 832 * removal time of the last random access point for the operating point that 833 * is being decoded. 834 */ 835 uint32_t frame_presentation_time; 836 837 /*! 838 * Buffer where previous frame is stored. 839 */ 840 RefCntBuffer *prev_frame; 841 842 /*! 843 * Buffer into which the current frame will be stored and other related info. 844 * TODO(hkuang): Combine this with cur_buf in macroblockd. 845 */ 846 RefCntBuffer *cur_frame; 847 848 /*! 849 * For encoder, we have a two-level mapping from reference frame type to the 850 * corresponding buffer in the buffer pool: 851 * * 'remapped_ref_idx[i - 1]' maps reference type 'i' (range: LAST_FRAME ... 852 * EXTREF_FRAME) to a remapped index 'j' (in range: 0 ... REF_FRAMES - 1) 853 * * Later, 'cm->ref_frame_map[j]' maps the remapped index 'j' to a pointer to 854 * the reference counted buffer structure RefCntBuffer, taken from the buffer 855 * pool cm->buffer_pool->frame_bufs. 856 * 857 * LAST_FRAME, ..., EXTREF_FRAME 858 * | | 859 * v v 860 * remapped_ref_idx[LAST_FRAME - 1], ..., remapped_ref_idx[EXTREF_FRAME - 1] 861 * | | 862 * v v 863 * ref_frame_map[], ..., ref_frame_map[] 864 * 865 * Note: INTRA_FRAME always refers to the current frame, so there's no need to 866 * have a remapped index for the same. 867 */ 868 int remapped_ref_idx[REF_FRAMES]; 869 870 /*! 871 * Scale of the current frame with respect to itself. 872 * This is currently used for intra block copy, which behaves like an inter 873 * prediction mode, where the reference frame is the current frame itself. 874 */ 875 struct scale_factors sf_identity; 876 877 /*! 878 * Scale factors of the reference frame with respect to the current frame. 879 * This is required for generating inter prediction and will be non-identity 880 * for a reference frame, if it has different dimensions than the coded 881 * dimensions of the current frame. 882 */ 883 struct scale_factors ref_scale_factors[REF_FRAMES]; 884 885 /*! 886 * For decoder, ref_frame_map[i] maps reference type 'i' to a pointer to 887 * the buffer in the buffer pool 'cm->buffer_pool.frame_bufs'. 888 * For encoder, ref_frame_map[j] (where j = remapped_ref_idx[i]) maps 889 * remapped reference index 'j' (that is, original reference type 'i') to 890 * a pointer to the buffer in the buffer pool 'cm->buffer_pool.frame_bufs'. 891 */ 892 RefCntBuffer *ref_frame_map[REF_FRAMES]; 893 894 /*! 895 * If true, this frame is actually shown after decoding. 896 * If false, this frame is coded in the bitstream, but not shown. It is only 897 * used as a reference for other frames coded later. 898 */ 899 int show_frame; 900 901 /*! 902 * If true, this frame can be used as a show-existing frame for other frames 903 * coded later. 904 * When 'show_frame' is true, this is always true for all non-keyframes. 905 * When 'show_frame' is false, this value is transmitted in the bitstream. 906 */ 907 int showable_frame; 908 909 /*! 910 * If true, show an existing frame coded before, instead of actually coding a 911 * frame. The existing frame comes from one of the existing reference buffers, 912 * as signaled in the bitstream. 913 */ 914 int show_existing_frame; 915 916 /*! 917 * Whether some features are allowed or not. 918 */ 919 FeatureFlags features; 920 921 /*! 922 * Params related to MB_MODE_INFO arrays and related info. 923 */ 924 CommonModeInfoParams mi_params; 925 926 #if CONFIG_ENTROPY_STATS 927 /*! 928 * Context type used by token CDFs, in the range 0 .. (TOKEN_CDF_Q_CTXS - 1). 929 */ 930 int coef_cdf_category; 931 #endif // CONFIG_ENTROPY_STATS 932 933 /*! 934 * Quantization params. 935 */ 936 CommonQuantParams quant_params; 937 938 /*! 939 * Segmentation info for current frame. 940 */ 941 struct segmentation seg; 942 943 /*! 944 * Segmentation map for previous frame. 945 */ 946 uint8_t *last_frame_seg_map; 947 948 /** 949 * \name Deblocking filter parameters. 950 */ 951 /**@{*/ 952 loop_filter_info_n lf_info; /*!< Loop filter info */ 953 struct loopfilter lf; /*!< Loop filter parameters */ 954 /**@}*/ 955 956 /** 957 * \name Loop Restoration filter parameters. 958 */ 959 /**@{*/ 960 RestorationInfo rst_info[MAX_MB_PLANE]; /*!< Loop Restoration filter info */ 961 int32_t *rst_tmpbuf; /*!< Scratch buffer for self-guided restoration */ 962 RestorationLineBuffers *rlbs; /*!< Line buffers needed by loop restoration */ 963 YV12_BUFFER_CONFIG rst_frame; /*!< Stores the output of loop restoration */ 964 /**@}*/ 965 966 /*! 967 * CDEF (Constrained Directional Enhancement Filter) parameters. 968 */ 969 CdefInfo cdef_info; 970 971 /*! 972 * Parameters for film grain synthesis. 973 */ 974 aom_film_grain_t film_grain_params; 975 976 /*! 977 * Parameters for delta quantization and delta loop filter level. 978 */ 979 DeltaQInfo delta_q_info; 980 981 /*! 982 * Global motion parameters for each reference frame. 983 */ 984 WarpedMotionParams global_motion[REF_FRAMES]; 985 986 /*! 987 * Elements part of the sequence header, that are applicable for all the 988 * frames in the video. 989 */ 990 SequenceHeader *seq_params; 991 992 /*! 993 * Current CDFs of all the symbols for the current frame. 994 */ 995 FRAME_CONTEXT *fc; 996 /*! 997 * Default CDFs used when features.primary_ref_frame = PRIMARY_REF_NONE 998 * (e.g. for a keyframe). These default CDFs are defined by the bitstream and 999 * copied from default CDF tables for each symbol. 1000 */ 1001 FRAME_CONTEXT *default_frame_context; 1002 1003 /*! 1004 * Parameters related to tiling. 1005 */ 1006 CommonTileParams tiles; 1007 1008 /*! 1009 * External BufferPool passed from outside. 1010 */ 1011 BufferPool *buffer_pool; 1012 1013 /*! 1014 * Above context buffers and their sizes. 1015 * Note: above contexts are allocated in this struct, as their size is 1016 * dependent on frame width, while left contexts are declared and allocated in 1017 * MACROBLOCKD struct, as they have a fixed size. 1018 */ 1019 CommonContexts above_contexts; 1020 1021 /** 1022 * \name Signaled when cm->seq_params->frame_id_numbers_present_flag == 1 1023 */ 1024 /**@{*/ 1025 int current_frame_id; /*!< frame ID for the current frame. */ 1026 int ref_frame_id[REF_FRAMES]; /*!< frame IDs for the reference frames. */ 1027 /**@}*/ 1028 1029 /*! 1030 * Motion vectors provided by motion field estimation. 1031 * tpl_mvs[row * stride + col] stores MV for block at [mi_row, mi_col] where: 1032 * mi_row = 2 * row, 1033 * mi_col = 2 * col, and 1034 * stride = cm->mi_params.mi_stride / 2 1035 */ 1036 TPL_MV_REF *tpl_mvs; 1037 /*! 1038 * Allocated size of 'tpl_mvs' array. Refer to 'ensure_mv_buffer()' function. 1039 */ 1040 int tpl_mvs_mem_size; 1041 /*! 1042 * ref_frame_sign_bias[k] is 1 if relative distance between reference 'k' and 1043 * current frame is positive; and 0 otherwise. 1044 */ 1045 int ref_frame_sign_bias[REF_FRAMES]; 1046 /*! 1047 * ref_frame_side[k] is 1 if relative distance between reference 'k' and 1048 * current frame is positive, -1 if relative distance is 0; and 0 otherwise. 1049 * TODO(jingning): This can be combined with sign_bias later. 1050 */ 1051 int8_t ref_frame_side[REF_FRAMES]; 1052 1053 /*! 1054 * Temporal layer ID of this frame 1055 * (in the range 0 ... (number_temporal_layers - 1)). 1056 */ 1057 int temporal_layer_id; 1058 1059 /*! 1060 * Spatial layer ID of this frame 1061 * (in the range 0 ... (number_spatial_layers - 1)). 1062 */ 1063 int spatial_layer_id; 1064 1065 #if TXCOEFF_TIMER 1066 int64_t cum_txcoeff_timer; 1067 int64_t txcoeff_timer; 1068 int txb_count; 1069 #endif // TXCOEFF_TIMER 1070 1071 #if TXCOEFF_COST_TIMER 1072 int64_t cum_txcoeff_cost_timer; 1073 int64_t txcoeff_cost_timer; 1074 int64_t txcoeff_cost_count; 1075 #endif // TXCOEFF_COST_TIMER 1076 } AV1_COMMON; 1077 1078 /*!\cond */ 1079 1080 // TODO(hkuang): Don't need to lock the whole pool after implementing atomic 1081 // frame reference count. 1082 static void lock_buffer_pool(BufferPool *const pool) { 1083 #if CONFIG_MULTITHREAD 1084 pthread_mutex_lock(&pool->pool_mutex); 1085 #else 1086 (void)pool; 1087 #endif 1088 } 1089 1090 static void unlock_buffer_pool(BufferPool *const pool) { 1091 #if CONFIG_MULTITHREAD 1092 pthread_mutex_unlock(&pool->pool_mutex); 1093 #else 1094 (void)pool; 1095 #endif 1096 } 1097 1098 static inline YV12_BUFFER_CONFIG *get_ref_frame(AV1_COMMON *cm, int index) { 1099 if (index < 0 || index >= REF_FRAMES) return NULL; 1100 if (cm->ref_frame_map[index] == NULL) return NULL; 1101 return &cm->ref_frame_map[index]->buf; 1102 } 1103 1104 static inline int get_free_fb(AV1_COMMON *cm) { 1105 RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs; 1106 int i; 1107 1108 lock_buffer_pool(cm->buffer_pool); 1109 const int num_frame_bufs = cm->buffer_pool->num_frame_bufs; 1110 for (i = 0; i < num_frame_bufs; ++i) 1111 if (frame_bufs[i].ref_count == 0) break; 1112 1113 if (i != num_frame_bufs) { 1114 if (frame_bufs[i].buf.use_external_reference_buffers) { 1115 // If this frame buffer's y_buffer, u_buffer, and v_buffer point to the 1116 // external reference buffers. Restore the buffer pointers to point to the 1117 // internally allocated memory. 1118 YV12_BUFFER_CONFIG *ybf = &frame_bufs[i].buf; 1119 ybf->y_buffer = ybf->store_buf_adr[0]; 1120 ybf->u_buffer = ybf->store_buf_adr[1]; 1121 ybf->v_buffer = ybf->store_buf_adr[2]; 1122 ybf->use_external_reference_buffers = 0; 1123 } 1124 1125 frame_bufs[i].ref_count = 1; 1126 } else { 1127 // We should never run out of free buffers. If this assertion fails, there 1128 // is a reference leak. 1129 assert(0 && "Ran out of free frame buffers. Likely a reference leak."); 1130 // Reset i to be INVALID_IDX to indicate no free buffer found. 1131 i = INVALID_IDX; 1132 } 1133 1134 unlock_buffer_pool(cm->buffer_pool); 1135 return i; 1136 } 1137 1138 static inline RefCntBuffer *assign_cur_frame_new_fb(AV1_COMMON *const cm) { 1139 // Release the previously-used frame-buffer 1140 if (cm->cur_frame != NULL) { 1141 --cm->cur_frame->ref_count; 1142 cm->cur_frame = NULL; 1143 } 1144 1145 // Assign a new framebuffer 1146 const int new_fb_idx = get_free_fb(cm); 1147 if (new_fb_idx == INVALID_IDX) return NULL; 1148 1149 cm->cur_frame = &cm->buffer_pool->frame_bufs[new_fb_idx]; 1150 #if CONFIG_AV1_ENCODER && !CONFIG_REALTIME_ONLY 1151 aom_invalidate_pyramid(cm->cur_frame->buf.y_pyramid); 1152 av1_invalidate_corner_list(cm->cur_frame->buf.corners); 1153 #endif // CONFIG_AV1_ENCODER && !CONFIG_REALTIME_ONLY 1154 av1_zero(cm->cur_frame->interp_filter_selected); 1155 return cm->cur_frame; 1156 } 1157 1158 // Modify 'lhs_ptr' to reference the buffer at 'rhs_ptr', and update the ref 1159 // counts accordingly. 1160 static inline void assign_frame_buffer_p(RefCntBuffer **lhs_ptr, 1161 RefCntBuffer *rhs_ptr) { 1162 RefCntBuffer *const old_ptr = *lhs_ptr; 1163 if (old_ptr != NULL) { 1164 assert(old_ptr->ref_count > 0); 1165 // One less reference to the buffer at 'old_ptr', so decrease ref count. 1166 --old_ptr->ref_count; 1167 } 1168 1169 *lhs_ptr = rhs_ptr; 1170 // One more reference to the buffer at 'rhs_ptr', so increase ref count. 1171 ++rhs_ptr->ref_count; 1172 } 1173 1174 static inline int frame_is_intra_only(const AV1_COMMON *const cm) { 1175 return cm->current_frame.frame_type == KEY_FRAME || 1176 cm->current_frame.frame_type == INTRA_ONLY_FRAME; 1177 } 1178 1179 static inline int frame_is_sframe(const AV1_COMMON *cm) { 1180 return cm->current_frame.frame_type == S_FRAME; 1181 } 1182 1183 // These functions take a reference frame label between LAST_FRAME and 1184 // EXTREF_FRAME inclusive. Note that this is different to the indexing 1185 // previously used by the frame_refs[] array. 1186 static inline int get_ref_frame_map_idx(const AV1_COMMON *const cm, 1187 const MV_REFERENCE_FRAME ref_frame) { 1188 return (ref_frame >= LAST_FRAME && ref_frame <= EXTREF_FRAME) 1189 ? cm->remapped_ref_idx[ref_frame - LAST_FRAME] 1190 : INVALID_IDX; 1191 } 1192 1193 static inline RefCntBuffer *get_ref_frame_buf( 1194 const AV1_COMMON *const cm, const MV_REFERENCE_FRAME ref_frame) { 1195 const int map_idx = get_ref_frame_map_idx(cm, ref_frame); 1196 return (map_idx != INVALID_IDX) ? cm->ref_frame_map[map_idx] : NULL; 1197 } 1198 1199 // Both const and non-const versions of this function are provided so that it 1200 // can be used with a const AV1_COMMON if needed. 1201 static inline const struct scale_factors *get_ref_scale_factors_const( 1202 const AV1_COMMON *const cm, const MV_REFERENCE_FRAME ref_frame) { 1203 const int map_idx = get_ref_frame_map_idx(cm, ref_frame); 1204 return (map_idx != INVALID_IDX) ? &cm->ref_scale_factors[map_idx] : NULL; 1205 } 1206 1207 static inline struct scale_factors *get_ref_scale_factors( 1208 AV1_COMMON *const cm, const MV_REFERENCE_FRAME ref_frame) { 1209 const int map_idx = get_ref_frame_map_idx(cm, ref_frame); 1210 return (map_idx != INVALID_IDX) ? &cm->ref_scale_factors[map_idx] : NULL; 1211 } 1212 1213 static inline RefCntBuffer *get_primary_ref_frame_buf( 1214 const AV1_COMMON *const cm) { 1215 const int primary_ref_frame = cm->features.primary_ref_frame; 1216 if (primary_ref_frame == PRIMARY_REF_NONE) return NULL; 1217 const int map_idx = get_ref_frame_map_idx(cm, primary_ref_frame + 1); 1218 return (map_idx != INVALID_IDX) ? cm->ref_frame_map[map_idx] : NULL; 1219 } 1220 1221 // Returns 1 if this frame might allow mvs from some reference frame. 1222 static inline int frame_might_allow_ref_frame_mvs(const AV1_COMMON *cm) { 1223 return !cm->features.error_resilient_mode && 1224 cm->seq_params->order_hint_info.enable_ref_frame_mvs && 1225 cm->seq_params->order_hint_info.enable_order_hint && 1226 !frame_is_intra_only(cm); 1227 } 1228 1229 // Returns 1 if this frame might use warped_motion 1230 static inline int frame_might_allow_warped_motion(const AV1_COMMON *cm) { 1231 return !cm->features.error_resilient_mode && !frame_is_intra_only(cm) && 1232 cm->seq_params->enable_warped_motion; 1233 } 1234 1235 static inline void ensure_mv_buffer(RefCntBuffer *buf, AV1_COMMON *cm) { 1236 const int buf_rows = buf->mi_rows; 1237 const int buf_cols = buf->mi_cols; 1238 const CommonModeInfoParams *const mi_params = &cm->mi_params; 1239 1240 if (buf->mvs == NULL || buf_rows != mi_params->mi_rows || 1241 buf_cols != mi_params->mi_cols) { 1242 aom_free(buf->mvs); 1243 buf->mi_rows = mi_params->mi_rows; 1244 buf->mi_cols = mi_params->mi_cols; 1245 CHECK_MEM_ERROR(cm, buf->mvs, 1246 (MV_REF *)aom_calloc(((mi_params->mi_rows + 1) >> 1) * 1247 ((mi_params->mi_cols + 1) >> 1), 1248 sizeof(*buf->mvs))); 1249 aom_free(buf->seg_map); 1250 CHECK_MEM_ERROR( 1251 cm, buf->seg_map, 1252 (uint8_t *)aom_calloc(mi_params->mi_rows * mi_params->mi_cols, 1253 sizeof(*buf->seg_map))); 1254 } 1255 1256 const int mem_size = 1257 ((mi_params->mi_rows + MAX_MIB_SIZE) >> 1) * (mi_params->mi_stride >> 1); 1258 1259 if (cm->tpl_mvs == NULL || cm->tpl_mvs_mem_size < mem_size) { 1260 aom_free(cm->tpl_mvs); 1261 CHECK_MEM_ERROR(cm, cm->tpl_mvs, 1262 (TPL_MV_REF *)aom_calloc(mem_size, sizeof(*cm->tpl_mvs))); 1263 cm->tpl_mvs_mem_size = mem_size; 1264 } 1265 } 1266 1267 #if !CONFIG_REALTIME_ONLY || CONFIG_AV1_DECODER 1268 void cfl_init(CFL_CTX *cfl, const SequenceHeader *seq_params); 1269 #endif 1270 1271 static inline int av1_num_planes(const AV1_COMMON *cm) { 1272 return cm->seq_params->monochrome ? 1 : MAX_MB_PLANE; 1273 } 1274 1275 static inline void av1_init_above_context(CommonContexts *above_contexts, 1276 int num_planes, int tile_row, 1277 MACROBLOCKD *xd) { 1278 for (int i = 0; i < num_planes; ++i) { 1279 xd->above_entropy_context[i] = above_contexts->entropy[i][tile_row]; 1280 } 1281 xd->above_partition_context = above_contexts->partition[tile_row]; 1282 xd->above_txfm_context = above_contexts->txfm[tile_row]; 1283 } 1284 1285 static inline void av1_init_macroblockd(AV1_COMMON *cm, MACROBLOCKD *xd) { 1286 const int num_planes = av1_num_planes(cm); 1287 const CommonQuantParams *const quant_params = &cm->quant_params; 1288 1289 for (int i = 0; i < num_planes; ++i) { 1290 if (xd->plane[i].plane_type == PLANE_TYPE_Y) { 1291 memcpy(xd->plane[i].seg_dequant_QTX, quant_params->y_dequant_QTX, 1292 sizeof(quant_params->y_dequant_QTX)); 1293 memcpy(xd->plane[i].seg_iqmatrix, quant_params->y_iqmatrix, 1294 sizeof(quant_params->y_iqmatrix)); 1295 1296 } else { 1297 if (i == AOM_PLANE_U) { 1298 memcpy(xd->plane[i].seg_dequant_QTX, quant_params->u_dequant_QTX, 1299 sizeof(quant_params->u_dequant_QTX)); 1300 memcpy(xd->plane[i].seg_iqmatrix, quant_params->u_iqmatrix, 1301 sizeof(quant_params->u_iqmatrix)); 1302 } else { 1303 memcpy(xd->plane[i].seg_dequant_QTX, quant_params->v_dequant_QTX, 1304 sizeof(quant_params->v_dequant_QTX)); 1305 memcpy(xd->plane[i].seg_iqmatrix, quant_params->v_iqmatrix, 1306 sizeof(quant_params->v_iqmatrix)); 1307 } 1308 } 1309 } 1310 xd->mi_stride = cm->mi_params.mi_stride; 1311 xd->error_info = cm->error; 1312 #if !CONFIG_REALTIME_ONLY || CONFIG_AV1_DECODER 1313 cfl_init(&xd->cfl, cm->seq_params); 1314 #endif 1315 } 1316 1317 static inline void set_entropy_context(MACROBLOCKD *xd, int mi_row, int mi_col, 1318 const int num_planes) { 1319 int i; 1320 int row_offset = mi_row; 1321 int col_offset = mi_col; 1322 for (i = 0; i < num_planes; ++i) { 1323 struct macroblockd_plane *const pd = &xd->plane[i]; 1324 // Offset the buffer pointer 1325 const BLOCK_SIZE bsize = xd->mi[0]->bsize; 1326 if (pd->subsampling_y && (mi_row & 0x01) && (mi_size_high[bsize] == 1)) 1327 row_offset = mi_row - 1; 1328 if (pd->subsampling_x && (mi_col & 0x01) && (mi_size_wide[bsize] == 1)) 1329 col_offset = mi_col - 1; 1330 int above_idx = col_offset; 1331 int left_idx = row_offset & MAX_MIB_MASK; 1332 pd->above_entropy_context = 1333 &xd->above_entropy_context[i][above_idx >> pd->subsampling_x]; 1334 pd->left_entropy_context = 1335 &xd->left_entropy_context[i][left_idx >> pd->subsampling_y]; 1336 } 1337 } 1338 1339 static inline int calc_mi_size(int len) { 1340 // len is in mi units. Align to a multiple of SBs. 1341 return ALIGN_POWER_OF_TWO(len, MAX_MIB_SIZE_LOG2); 1342 } 1343 1344 static inline void set_plane_n4(MACROBLOCKD *const xd, int bw, int bh, 1345 const int num_planes) { 1346 int i; 1347 for (i = 0; i < num_planes; i++) { 1348 xd->plane[i].width = (bw * MI_SIZE) >> xd->plane[i].subsampling_x; 1349 xd->plane[i].height = (bh * MI_SIZE) >> xd->plane[i].subsampling_y; 1350 1351 xd->plane[i].width = AOMMAX(xd->plane[i].width, 4); 1352 xd->plane[i].height = AOMMAX(xd->plane[i].height, 4); 1353 } 1354 } 1355 1356 static inline void set_mi_row_col(MACROBLOCKD *xd, const TileInfo *const tile, 1357 int mi_row, int bh, int mi_col, int bw, 1358 int mi_rows, int mi_cols) { 1359 xd->mb_to_top_edge = -GET_MV_SUBPEL(mi_row * MI_SIZE); 1360 xd->mb_to_bottom_edge = GET_MV_SUBPEL((mi_rows - bh - mi_row) * MI_SIZE); 1361 xd->mb_to_left_edge = -GET_MV_SUBPEL((mi_col * MI_SIZE)); 1362 xd->mb_to_right_edge = GET_MV_SUBPEL((mi_cols - bw - mi_col) * MI_SIZE); 1363 1364 xd->mi_row = mi_row; 1365 xd->mi_col = mi_col; 1366 1367 // Are edges available for intra prediction? 1368 xd->up_available = (mi_row > tile->mi_row_start); 1369 1370 const int ss_x = xd->plane[1].subsampling_x; 1371 const int ss_y = xd->plane[1].subsampling_y; 1372 1373 xd->left_available = (mi_col > tile->mi_col_start); 1374 xd->chroma_up_available = xd->up_available; 1375 xd->chroma_left_available = xd->left_available; 1376 if (ss_x && bw < mi_size_wide[BLOCK_8X8]) 1377 xd->chroma_left_available = (mi_col - 1) > tile->mi_col_start; 1378 if (ss_y && bh < mi_size_high[BLOCK_8X8]) 1379 xd->chroma_up_available = (mi_row - 1) > tile->mi_row_start; 1380 if (xd->up_available) { 1381 xd->above_mbmi = xd->mi[-xd->mi_stride]; 1382 } else { 1383 xd->above_mbmi = NULL; 1384 } 1385 1386 if (xd->left_available) { 1387 xd->left_mbmi = xd->mi[-1]; 1388 } else { 1389 xd->left_mbmi = NULL; 1390 } 1391 1392 const int chroma_ref = ((mi_row & 0x01) || !(bh & 0x01) || !ss_y) && 1393 ((mi_col & 0x01) || !(bw & 0x01) || !ss_x); 1394 xd->is_chroma_ref = chroma_ref; 1395 if (chroma_ref) { 1396 // To help calculate the "above" and "left" chroma blocks, note that the 1397 // current block may cover multiple luma blocks (e.g., if partitioned into 1398 // 4x4 luma blocks). 1399 // First, find the top-left-most luma block covered by this chroma block 1400 MB_MODE_INFO **base_mi = 1401 &xd->mi[-(mi_row & ss_y) * xd->mi_stride - (mi_col & ss_x)]; 1402 1403 // Then, we consider the luma region covered by the left or above 4x4 chroma 1404 // prediction. We want to point to the chroma reference block in that 1405 // region, which is the bottom-right-most mi unit. 1406 // This leads to the following offsets: 1407 MB_MODE_INFO *chroma_above_mi = 1408 xd->chroma_up_available ? base_mi[-xd->mi_stride + ss_x] : NULL; 1409 xd->chroma_above_mbmi = chroma_above_mi; 1410 1411 MB_MODE_INFO *chroma_left_mi = 1412 xd->chroma_left_available ? base_mi[ss_y * xd->mi_stride - 1] : NULL; 1413 xd->chroma_left_mbmi = chroma_left_mi; 1414 } 1415 1416 xd->height = bh; 1417 xd->width = bw; 1418 1419 xd->is_last_vertical_rect = 0; 1420 if (xd->width < xd->height) { 1421 if (!((mi_col + xd->width) & (xd->height - 1))) { 1422 xd->is_last_vertical_rect = 1; 1423 } 1424 } 1425 1426 xd->is_first_horizontal_rect = 0; 1427 if (xd->width > xd->height) 1428 if (!(mi_row & (xd->width - 1))) xd->is_first_horizontal_rect = 1; 1429 } 1430 1431 static inline aom_cdf_prob *get_y_mode_cdf(FRAME_CONTEXT *tile_ctx, 1432 const MB_MODE_INFO *above_mi, 1433 const MB_MODE_INFO *left_mi) { 1434 const PREDICTION_MODE above = av1_above_block_mode(above_mi); 1435 const PREDICTION_MODE left = av1_left_block_mode(left_mi); 1436 const int above_ctx = intra_mode_context[above]; 1437 const int left_ctx = intra_mode_context[left]; 1438 return tile_ctx->kf_y_cdf[above_ctx][left_ctx]; 1439 } 1440 1441 static inline void update_partition_context(MACROBLOCKD *xd, int mi_row, 1442 int mi_col, BLOCK_SIZE subsize, 1443 BLOCK_SIZE bsize) { 1444 PARTITION_CONTEXT *const above_ctx = xd->above_partition_context + mi_col; 1445 PARTITION_CONTEXT *const left_ctx = 1446 xd->left_partition_context + (mi_row & MAX_MIB_MASK); 1447 1448 const int bw = mi_size_wide[bsize]; 1449 const int bh = mi_size_high[bsize]; 1450 memset(above_ctx, partition_context_lookup[subsize].above, bw); 1451 memset(left_ctx, partition_context_lookup[subsize].left, bh); 1452 } 1453 1454 static inline int is_chroma_reference(int mi_row, int mi_col, BLOCK_SIZE bsize, 1455 int subsampling_x, int subsampling_y) { 1456 assert(bsize < BLOCK_SIZES_ALL); 1457 const int bw = mi_size_wide[bsize]; 1458 const int bh = mi_size_high[bsize]; 1459 int ref_pos = ((mi_row & 0x01) || !(bh & 0x01) || !subsampling_y) && 1460 ((mi_col & 0x01) || !(bw & 0x01) || !subsampling_x); 1461 return ref_pos; 1462 } 1463 1464 static inline aom_cdf_prob cdf_element_prob(const aom_cdf_prob *cdf, 1465 size_t element) { 1466 assert(cdf != NULL); 1467 return (element > 0 ? cdf[element - 1] : CDF_PROB_TOP) - cdf[element]; 1468 } 1469 1470 static inline void partition_gather_horz_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_HORZ); 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_HORZ_B); 1479 out[0] -= cdf_element_prob(in, PARTITION_VERT_A); 1480 if (bsize != BLOCK_128X128) out[0] -= cdf_element_prob(in, PARTITION_HORZ_4); 1481 out[0] = AOM_ICDF(out[0]); 1482 out[1] = AOM_ICDF(CDF_PROB_TOP); 1483 } 1484 1485 static inline void partition_gather_vert_alike(aom_cdf_prob *out, 1486 const aom_cdf_prob *const in, 1487 BLOCK_SIZE bsize) { 1488 (void)bsize; 1489 out[0] = CDF_PROB_TOP; 1490 out[0] -= cdf_element_prob(in, PARTITION_VERT); 1491 out[0] -= cdf_element_prob(in, PARTITION_SPLIT); 1492 out[0] -= cdf_element_prob(in, PARTITION_HORZ_A); 1493 out[0] -= cdf_element_prob(in, PARTITION_VERT_A); 1494 out[0] -= cdf_element_prob(in, PARTITION_VERT_B); 1495 if (bsize != BLOCK_128X128) out[0] -= cdf_element_prob(in, PARTITION_VERT_4); 1496 out[0] = AOM_ICDF(out[0]); 1497 out[1] = AOM_ICDF(CDF_PROB_TOP); 1498 } 1499 1500 static inline void update_ext_partition_context(MACROBLOCKD *xd, int mi_row, 1501 int mi_col, BLOCK_SIZE subsize, 1502 BLOCK_SIZE bsize, 1503 PARTITION_TYPE partition) { 1504 if (bsize >= BLOCK_8X8) { 1505 const int hbs = mi_size_wide[bsize] / 2; 1506 BLOCK_SIZE bsize2 = get_partition_subsize(bsize, PARTITION_SPLIT); 1507 switch (partition) { 1508 case PARTITION_SPLIT: 1509 if (bsize != BLOCK_8X8) break; 1510 AOM_FALLTHROUGH_INTENDED; 1511 case PARTITION_NONE: 1512 case PARTITION_HORZ: 1513 case PARTITION_VERT: 1514 case PARTITION_HORZ_4: 1515 case PARTITION_VERT_4: 1516 update_partition_context(xd, mi_row, mi_col, subsize, bsize); 1517 break; 1518 case PARTITION_HORZ_A: 1519 update_partition_context(xd, mi_row, mi_col, bsize2, subsize); 1520 update_partition_context(xd, mi_row + hbs, mi_col, subsize, subsize); 1521 break; 1522 case PARTITION_HORZ_B: 1523 update_partition_context(xd, mi_row, mi_col, subsize, subsize); 1524 update_partition_context(xd, mi_row + hbs, mi_col, bsize2, subsize); 1525 break; 1526 case PARTITION_VERT_A: 1527 update_partition_context(xd, mi_row, mi_col, bsize2, subsize); 1528 update_partition_context(xd, mi_row, mi_col + hbs, subsize, subsize); 1529 break; 1530 case PARTITION_VERT_B: 1531 update_partition_context(xd, mi_row, mi_col, subsize, subsize); 1532 update_partition_context(xd, mi_row, mi_col + hbs, bsize2, subsize); 1533 break; 1534 default: assert(0 && "Invalid partition type"); 1535 } 1536 } 1537 } 1538 1539 static inline int partition_plane_context(const MACROBLOCKD *xd, int mi_row, 1540 int mi_col, BLOCK_SIZE bsize) { 1541 const PARTITION_CONTEXT *above_ctx = xd->above_partition_context + mi_col; 1542 const PARTITION_CONTEXT *left_ctx = 1543 xd->left_partition_context + (mi_row & MAX_MIB_MASK); 1544 // Minimum partition point is 8x8. Offset the bsl accordingly. 1545 const int bsl = mi_size_wide_log2[bsize] - mi_size_wide_log2[BLOCK_8X8]; 1546 int above = (*above_ctx >> bsl) & 1, left = (*left_ctx >> bsl) & 1; 1547 1548 assert(mi_size_wide_log2[bsize] == mi_size_high_log2[bsize]); 1549 assert(bsl >= 0); 1550 1551 return (left * 2 + above) + bsl * PARTITION_PLOFFSET; 1552 } 1553 1554 // Return the number of elements in the partition CDF when 1555 // partitioning the (square) block with luma block size of bsize. 1556 static inline int partition_cdf_length(BLOCK_SIZE bsize) { 1557 if (bsize <= BLOCK_8X8) 1558 return PARTITION_TYPES; 1559 else if (bsize == BLOCK_128X128) 1560 return EXT_PARTITION_TYPES - 2; 1561 else 1562 return EXT_PARTITION_TYPES; 1563 } 1564 1565 static inline int max_block_wide(const MACROBLOCKD *xd, BLOCK_SIZE bsize, 1566 int plane) { 1567 assert(bsize < BLOCK_SIZES_ALL); 1568 int max_blocks_wide = block_size_wide[bsize]; 1569 1570 if (xd->mb_to_right_edge < 0) { 1571 const struct macroblockd_plane *const pd = &xd->plane[plane]; 1572 max_blocks_wide += xd->mb_to_right_edge >> (3 + pd->subsampling_x); 1573 } 1574 1575 // Scale the width in the transform block unit. 1576 return max_blocks_wide >> MI_SIZE_LOG2; 1577 } 1578 1579 static inline int max_block_high(const MACROBLOCKD *xd, BLOCK_SIZE bsize, 1580 int plane) { 1581 int max_blocks_high = block_size_high[bsize]; 1582 1583 if (xd->mb_to_bottom_edge < 0) { 1584 const struct macroblockd_plane *const pd = &xd->plane[plane]; 1585 max_blocks_high += xd->mb_to_bottom_edge >> (3 + pd->subsampling_y); 1586 } 1587 1588 // Scale the height in the transform block unit. 1589 return max_blocks_high >> MI_SIZE_LOG2; 1590 } 1591 1592 static inline void av1_zero_above_context(AV1_COMMON *const cm, 1593 const MACROBLOCKD *xd, 1594 int mi_col_start, int mi_col_end, 1595 const int tile_row) { 1596 const SequenceHeader *const seq_params = cm->seq_params; 1597 const int num_planes = av1_num_planes(cm); 1598 const int width = mi_col_end - mi_col_start; 1599 const int aligned_width = 1600 ALIGN_POWER_OF_TWO(width, seq_params->mib_size_log2); 1601 const int offset_y = mi_col_start; 1602 const int width_y = aligned_width; 1603 const int offset_uv = offset_y >> seq_params->subsampling_x; 1604 const int width_uv = width_y >> seq_params->subsampling_x; 1605 CommonContexts *const above_contexts = &cm->above_contexts; 1606 1607 av1_zero_array(above_contexts->entropy[0][tile_row] + offset_y, width_y); 1608 if (num_planes > 1) { 1609 if (above_contexts->entropy[1][tile_row] && 1610 above_contexts->entropy[2][tile_row]) { 1611 av1_zero_array(above_contexts->entropy[1][tile_row] + offset_uv, 1612 width_uv); 1613 av1_zero_array(above_contexts->entropy[2][tile_row] + offset_uv, 1614 width_uv); 1615 } else { 1616 aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME, 1617 "Invalid value of planes"); 1618 } 1619 } 1620 1621 av1_zero_array(above_contexts->partition[tile_row] + mi_col_start, 1622 aligned_width); 1623 1624 memset(above_contexts->txfm[tile_row] + mi_col_start, 1625 tx_size_wide[TX_SIZES_LARGEST], aligned_width * sizeof(TXFM_CONTEXT)); 1626 } 1627 1628 static inline void av1_zero_left_context(MACROBLOCKD *const xd) { 1629 av1_zero(xd->left_entropy_context); 1630 av1_zero(xd->left_partition_context); 1631 1632 memset(xd->left_txfm_context_buffer, tx_size_high[TX_SIZES_LARGEST], 1633 sizeof(xd->left_txfm_context_buffer)); 1634 } 1635 1636 static inline void set_txfm_ctx(TXFM_CONTEXT *txfm_ctx, uint8_t txs, int len) { 1637 int i; 1638 for (i = 0; i < len; ++i) txfm_ctx[i] = txs; 1639 } 1640 1641 static inline void set_txfm_ctxs(TX_SIZE tx_size, int n4_w, int n4_h, int skip, 1642 const MACROBLOCKD *xd) { 1643 uint8_t bw = tx_size_wide[tx_size]; 1644 uint8_t bh = tx_size_high[tx_size]; 1645 1646 if (skip) { 1647 bw = n4_w * MI_SIZE; 1648 bh = n4_h * MI_SIZE; 1649 } 1650 1651 set_txfm_ctx(xd->above_txfm_context, bw, n4_w); 1652 set_txfm_ctx(xd->left_txfm_context, bh, n4_h); 1653 } 1654 1655 static inline int get_mi_grid_idx(const CommonModeInfoParams *const mi_params, 1656 int mi_row, int mi_col) { 1657 return mi_row * mi_params->mi_stride + mi_col; 1658 } 1659 1660 static inline int get_alloc_mi_idx(const CommonModeInfoParams *const mi_params, 1661 int mi_row, int mi_col) { 1662 const int mi_alloc_size_1d = mi_size_wide[mi_params->mi_alloc_bsize]; 1663 const int mi_alloc_row = mi_row / mi_alloc_size_1d; 1664 const int mi_alloc_col = mi_col / mi_alloc_size_1d; 1665 1666 return mi_alloc_row * mi_params->mi_alloc_stride + mi_alloc_col; 1667 } 1668 1669 // For this partition block, set pointers in mi_params->mi_grid_base and xd->mi. 1670 static inline void set_mi_offsets(const CommonModeInfoParams *const mi_params, 1671 MACROBLOCKD *const xd, int mi_row, 1672 int mi_col) { 1673 // 'mi_grid_base' should point to appropriate memory in 'mi'. 1674 const int mi_grid_idx = get_mi_grid_idx(mi_params, mi_row, mi_col); 1675 const int mi_alloc_idx = get_alloc_mi_idx(mi_params, mi_row, mi_col); 1676 mi_params->mi_grid_base[mi_grid_idx] = &mi_params->mi_alloc[mi_alloc_idx]; 1677 // 'xd->mi' should point to an offset in 'mi_grid_base'; 1678 xd->mi = mi_params->mi_grid_base + mi_grid_idx; 1679 // 'xd->tx_type_map' should point to an offset in 'mi_params->tx_type_map'. 1680 xd->tx_type_map = mi_params->tx_type_map + mi_grid_idx; 1681 xd->tx_type_map_stride = mi_params->mi_stride; 1682 } 1683 1684 static inline void txfm_partition_update(TXFM_CONTEXT *above_ctx, 1685 TXFM_CONTEXT *left_ctx, 1686 TX_SIZE tx_size, TX_SIZE txb_size) { 1687 BLOCK_SIZE bsize = txsize_to_bsize[txb_size]; 1688 int bh = mi_size_high[bsize]; 1689 int bw = mi_size_wide[bsize]; 1690 uint8_t txw = tx_size_wide[tx_size]; 1691 uint8_t txh = tx_size_high[tx_size]; 1692 int i; 1693 for (i = 0; i < bh; ++i) left_ctx[i] = txh; 1694 for (i = 0; i < bw; ++i) above_ctx[i] = txw; 1695 } 1696 1697 static inline TX_SIZE get_sqr_tx_size(int tx_dim) { 1698 switch (tx_dim) { 1699 case 128: 1700 case 64: return TX_64X64; break; 1701 case 32: return TX_32X32; break; 1702 case 16: return TX_16X16; break; 1703 case 8: return TX_8X8; break; 1704 default: return TX_4X4; 1705 } 1706 } 1707 1708 static inline TX_SIZE get_tx_size(int width, int height) { 1709 if (width == height) { 1710 return get_sqr_tx_size(width); 1711 } 1712 if (width < height) { 1713 if (width + width == height) { 1714 switch (width) { 1715 case 4: return TX_4X8; break; 1716 case 8: return TX_8X16; break; 1717 case 16: return TX_16X32; break; 1718 case 32: return TX_32X64; break; 1719 } 1720 } else { 1721 switch (width) { 1722 case 4: return TX_4X16; break; 1723 case 8: return TX_8X32; break; 1724 case 16: return TX_16X64; break; 1725 } 1726 } 1727 } else { 1728 if (height + height == width) { 1729 switch (height) { 1730 case 4: return TX_8X4; break; 1731 case 8: return TX_16X8; break; 1732 case 16: return TX_32X16; break; 1733 case 32: return TX_64X32; break; 1734 } 1735 } else { 1736 switch (height) { 1737 case 4: return TX_16X4; break; 1738 case 8: return TX_32X8; break; 1739 case 16: return TX_64X16; break; 1740 } 1741 } 1742 } 1743 assert(0); 1744 return TX_4X4; 1745 } 1746 1747 static inline int txfm_partition_context(const TXFM_CONTEXT *const above_ctx, 1748 const TXFM_CONTEXT *const left_ctx, 1749 BLOCK_SIZE bsize, TX_SIZE tx_size) { 1750 const uint8_t txw = tx_size_wide[tx_size]; 1751 const uint8_t txh = tx_size_high[tx_size]; 1752 const int above = *above_ctx < txw; 1753 const int left = *left_ctx < txh; 1754 int category = TXFM_PARTITION_CONTEXTS; 1755 1756 // dummy return, not used by others. 1757 if (tx_size <= TX_4X4) return 0; 1758 1759 TX_SIZE max_tx_size = 1760 get_sqr_tx_size(AOMMAX(block_size_wide[bsize], block_size_high[bsize])); 1761 1762 if (max_tx_size >= TX_8X8) { 1763 category = 1764 (txsize_sqr_up_map[tx_size] != max_tx_size && max_tx_size > TX_8X8) + 1765 (TX_SIZES - 1 - max_tx_size) * 2; 1766 } 1767 assert(category != TXFM_PARTITION_CONTEXTS); 1768 return category * 3 + above + left; 1769 } 1770 1771 // Compute the next partition in the direction of the sb_type stored in the mi 1772 // array, starting with bsize. 1773 static inline PARTITION_TYPE get_partition(const AV1_COMMON *const cm, 1774 int mi_row, int mi_col, 1775 BLOCK_SIZE bsize) { 1776 const CommonModeInfoParams *const mi_params = &cm->mi_params; 1777 if (mi_row >= mi_params->mi_rows || mi_col >= mi_params->mi_cols) 1778 return PARTITION_INVALID; 1779 1780 const int offset = mi_row * mi_params->mi_stride + mi_col; 1781 MB_MODE_INFO **mi = mi_params->mi_grid_base + offset; 1782 const BLOCK_SIZE subsize = mi[0]->bsize; 1783 1784 assert(bsize < BLOCK_SIZES_ALL); 1785 1786 if (subsize == bsize) return PARTITION_NONE; 1787 1788 const int bhigh = mi_size_high[bsize]; 1789 const int bwide = mi_size_wide[bsize]; 1790 const int sshigh = mi_size_high[subsize]; 1791 const int sswide = mi_size_wide[subsize]; 1792 1793 if (bsize > BLOCK_8X8 && mi_row + bwide / 2 < mi_params->mi_rows && 1794 mi_col + bhigh / 2 < mi_params->mi_cols) { 1795 // In this case, the block might be using an extended partition 1796 // type. 1797 const MB_MODE_INFO *const mbmi_right = mi[bwide / 2]; 1798 const MB_MODE_INFO *const mbmi_below = mi[bhigh / 2 * mi_params->mi_stride]; 1799 1800 if (sswide == bwide) { 1801 // Smaller height but same width. Is PARTITION_HORZ_4, PARTITION_HORZ or 1802 // PARTITION_HORZ_B. To distinguish the latter two, check if the lower 1803 // half was split. 1804 if (sshigh * 4 == bhigh) return PARTITION_HORZ_4; 1805 assert(sshigh * 2 == bhigh); 1806 1807 if (mbmi_below->bsize == subsize) 1808 return PARTITION_HORZ; 1809 else 1810 return PARTITION_HORZ_B; 1811 } else if (sshigh == bhigh) { 1812 // Smaller width but same height. Is PARTITION_VERT_4, PARTITION_VERT or 1813 // PARTITION_VERT_B. To distinguish the latter two, check if the right 1814 // half was split. 1815 if (sswide * 4 == bwide) return PARTITION_VERT_4; 1816 assert(sswide * 2 == bwide); 1817 1818 if (mbmi_right->bsize == subsize) 1819 return PARTITION_VERT; 1820 else 1821 return PARTITION_VERT_B; 1822 } else { 1823 // Smaller width and smaller height. Might be PARTITION_SPLIT or could be 1824 // PARTITION_HORZ_A or PARTITION_VERT_A. If subsize isn't halved in both 1825 // dimensions, we immediately know this is a split (which will recurse to 1826 // get to subsize). Otherwise look down and to the right. With 1827 // PARTITION_VERT_A, the right block will have height bhigh; with 1828 // PARTITION_HORZ_A, the lower block with have width bwide. Otherwise 1829 // it's PARTITION_SPLIT. 1830 if (sswide * 2 != bwide || sshigh * 2 != bhigh) return PARTITION_SPLIT; 1831 1832 if (mi_size_wide[mbmi_below->bsize] == bwide) return PARTITION_HORZ_A; 1833 if (mi_size_high[mbmi_right->bsize] == bhigh) return PARTITION_VERT_A; 1834 1835 return PARTITION_SPLIT; 1836 } 1837 } 1838 const int vert_split = sswide < bwide; 1839 const int horz_split = sshigh < bhigh; 1840 const int split_idx = (vert_split << 1) | horz_split; 1841 assert(split_idx != 0); 1842 1843 static const PARTITION_TYPE base_partitions[4] = { 1844 PARTITION_INVALID, PARTITION_HORZ, PARTITION_VERT, PARTITION_SPLIT 1845 }; 1846 1847 return base_partitions[split_idx]; 1848 } 1849 1850 static inline void set_sb_size(SequenceHeader *const seq_params, 1851 BLOCK_SIZE sb_size) { 1852 seq_params->sb_size = sb_size; 1853 seq_params->mib_size = mi_size_wide[seq_params->sb_size]; 1854 seq_params->mib_size_log2 = mi_size_wide_log2[seq_params->sb_size]; 1855 } 1856 1857 // Returns true if the frame is fully lossless at the coded resolution. 1858 // Note: If super-resolution is used, such a frame will still NOT be lossless at 1859 // the upscaled resolution. 1860 static inline int is_coded_lossless(const AV1_COMMON *cm, 1861 const MACROBLOCKD *xd) { 1862 int coded_lossless = 1; 1863 if (cm->seg.enabled) { 1864 for (int i = 0; i < MAX_SEGMENTS; ++i) { 1865 if (!xd->lossless[i]) { 1866 coded_lossless = 0; 1867 break; 1868 } 1869 } 1870 } else { 1871 coded_lossless = xd->lossless[0]; 1872 } 1873 return coded_lossless; 1874 } 1875 1876 static inline int is_valid_seq_level_idx(AV1_LEVEL seq_level_idx) { 1877 return seq_level_idx == SEQ_LEVEL_MAX || 1878 (seq_level_idx < SEQ_LEVELS && 1879 // The following levels are currently undefined. 1880 seq_level_idx != SEQ_LEVEL_2_2 && seq_level_idx != SEQ_LEVEL_2_3 && 1881 seq_level_idx != SEQ_LEVEL_3_2 && seq_level_idx != SEQ_LEVEL_3_3 && 1882 seq_level_idx != SEQ_LEVEL_4_2 && seq_level_idx != SEQ_LEVEL_4_3 1883 #if !CONFIG_CWG_C013 1884 && seq_level_idx != SEQ_LEVEL_7_0 && seq_level_idx != SEQ_LEVEL_7_1 && 1885 seq_level_idx != SEQ_LEVEL_7_2 && seq_level_idx != SEQ_LEVEL_7_3 && 1886 seq_level_idx != SEQ_LEVEL_8_0 && seq_level_idx != SEQ_LEVEL_8_1 && 1887 seq_level_idx != SEQ_LEVEL_8_2 && seq_level_idx != SEQ_LEVEL_8_3 1888 #endif 1889 ); 1890 } 1891 1892 /*!\endcond */ 1893 1894 #ifdef __cplusplus 1895 } // extern "C" 1896 #endif 1897 1898 #endif // AOM_AV1_COMMON_AV1_COMMON_INT_H_