• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_ONYXC_INT_H_
13 #define AOM_AV1_COMMON_ONYXC_INT_H_
14 
15 #include "config/aom_config.h"
16 #include "config/av1_rtcd.h"
17 
18 #include "aom/internal/aom_codec_internal.h"
19 #include "aom_util/aom_thread.h"
20 #include "av1/common/alloccommon.h"
21 #include "av1/common/av1_loopfilter.h"
22 #include "av1/common/entropy.h"
23 #include "av1/common/entropymode.h"
24 #include "av1/common/entropymv.h"
25 #include "av1/common/enums.h"
26 #include "av1/common/frame_buffers.h"
27 #include "av1/common/mv.h"
28 #include "av1/common/quant_common.h"
29 #include "av1/common/restoration.h"
30 #include "av1/common/tile_common.h"
31 #include "av1/common/timing.h"
32 #include "av1/common/odintrin.h"
33 #include "av1/encoder/hash_motion.h"
34 #include "aom_dsp/grain_synthesis.h"
35 #include "aom_dsp/grain_table.h"
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 #if defined(__clang__) && defined(__has_warning)
41 #if __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough")
42 #define AOM_FALLTHROUGH_INTENDED [[clang::fallthrough]]  // NOLINT
43 #endif
44 #elif defined(__GNUC__) && __GNUC__ >= 7
45 #define AOM_FALLTHROUGH_INTENDED __attribute__((fallthrough))  // NOLINT
46 #endif
47 
48 #ifndef AOM_FALLTHROUGH_INTENDED
49 #define AOM_FALLTHROUGH_INTENDED \
50   do {                           \
51   } while (0)
52 #endif
53 
54 #define CDEF_MAX_STRENGTHS 16
55 
56 /* Constant values while waiting for the sequence header */
57 #define FRAME_ID_LENGTH 15
58 #define DELTA_FRAME_ID_LENGTH 14
59 
60 #define FRAME_CONTEXTS (FRAME_BUFFERS + 1)
61 // Extra frame context which is always kept at default values
62 #define FRAME_CONTEXT_DEFAULTS (FRAME_CONTEXTS - 1)
63 #define PRIMARY_REF_BITS 3
64 #define PRIMARY_REF_NONE 7
65 
66 #define NUM_PING_PONG_BUFFERS 2
67 
68 #define MAX_NUM_TEMPORAL_LAYERS 8
69 #define MAX_NUM_SPATIAL_LAYERS 4
70 /* clang-format off */
71 // clang-format seems to think this is a pointer dereference and not a
72 // multiplication.
73 #define MAX_NUM_OPERATING_POINTS \
74   MAX_NUM_TEMPORAL_LAYERS * MAX_NUM_SPATIAL_LAYERS
75 /* clang-format on*/
76 
77 // TODO(jingning): Turning this on to set up transform coefficient
78 // processing timer.
79 #define TXCOEFF_TIMER 0
80 #define TXCOEFF_COST_TIMER 0
81 
82 enum {
83   SINGLE_REFERENCE = 0,
84   COMPOUND_REFERENCE = 1,
85   REFERENCE_MODE_SELECT = 2,
86   REFERENCE_MODES = 3,
87 } UENUM1BYTE(REFERENCE_MODE);
88 
89 enum {
90   /**
91    * Frame context updates are disabled
92    */
93   REFRESH_FRAME_CONTEXT_DISABLED,
94   /**
95    * Update frame context to values resulting from backward probability
96    * updates based on entropy/counts in the decoded frame
97    */
98   REFRESH_FRAME_CONTEXT_BACKWARD,
99 } UENUM1BYTE(REFRESH_FRAME_CONTEXT_MODE);
100 
101 #define MFMV_STACK_SIZE 3
102 typedef struct {
103   int_mv mfmv0;
104   uint8_t ref_frame_offset;
105 } TPL_MV_REF;
106 
107 typedef struct {
108   int_mv mv;
109   MV_REFERENCE_FRAME ref_frame;
110 } MV_REF;
111 
112 
113 typedef struct RefCntBuffer {
114   // For a RefCntBuffer, the following are reference-holding variables:
115   // - cm->ref_frame_map[]
116   // - cm->cur_frame
117   // - cm->scaled_ref_buf[] (encoder only)
118   // - cm->next_ref_frame_map[] (decoder only)
119   // - pbi->output_frame_index[] (decoder only)
120   // With that definition, 'ref_count' is the number of reference-holding
121   // variables that are currently referencing this buffer.
122   // For example:
123   // - suppose this buffer is at index 'k' in the buffer pool, and
124   // - Total 'n' of the variables / array elements above have value 'k' (that
125   // is, they are pointing to buffer at index 'k').
126   // Then, pool->frame_bufs[k].ref_count = n.
127   int ref_count;
128 
129   unsigned int order_hint;
130   unsigned int ref_order_hints[INTER_REFS_PER_FRAME];
131 
132   MV_REF *mvs;
133   uint8_t *seg_map;
134   struct segmentation seg;
135   int mi_rows;
136   int mi_cols;
137   // Width and height give the size of the buffer (before any upscaling, unlike
138   // the sizes that can be derived from the buf structure)
139   int width;
140   int height;
141   WarpedMotionParams global_motion[REF_FRAMES];
142   int showable_frame;  // frame can be used as show existing frame in future
143   uint8_t film_grain_params_present;
144   aom_film_grain_t film_grain_params;
145   aom_codec_frame_buffer_t raw_frame_buffer;
146   YV12_BUFFER_CONFIG buf;
147   hash_table hash_table;
148   FRAME_TYPE frame_type;
149 
150   // This is only used in the encoder but needs to be indexed per ref frame
151   // so it's extremely convenient to keep it here.
152   int interp_filter_selected[SWITCHABLE];
153 
154   // Inter frame reference frame delta for loop filter
155   int8_t ref_deltas[REF_FRAMES];
156 
157   // 0 = ZERO_MV, MV
158   int8_t mode_deltas[MAX_MODE_LF_DELTAS];
159 
160   FRAME_CONTEXT frame_context;
161 } RefCntBuffer;
162 
163 typedef struct BufferPool {
164 // Protect BufferPool from being accessed by several FrameWorkers at
165 // the same time during frame parallel decode.
166 // TODO(hkuang): Try to use atomic variable instead of locking the whole pool.
167 // TODO(wtc): Remove this. See
168 // https://chromium-review.googlesource.com/c/webm/libvpx/+/560630.
169 #if CONFIG_MULTITHREAD
170   pthread_mutex_t pool_mutex;
171 #endif
172 
173   // Private data associated with the frame buffer callbacks.
174   void *cb_priv;
175 
176   aom_get_frame_buffer_cb_fn_t get_fb_cb;
177   aom_release_frame_buffer_cb_fn_t release_fb_cb;
178 
179   RefCntBuffer frame_bufs[FRAME_BUFFERS];
180 
181   // Frame buffers allocated internally by the codec.
182   InternalFrameBufferList int_frame_buffers;
183 } BufferPool;
184 
185 typedef struct {
186   int cdef_pri_damping;
187   int cdef_sec_damping;
188   int nb_cdef_strengths;
189   int cdef_strengths[CDEF_MAX_STRENGTHS];
190   int cdef_uv_strengths[CDEF_MAX_STRENGTHS];
191   int cdef_bits;
192 } CdefInfo;
193 
194 typedef struct {
195   int delta_q_present_flag;
196   // Resolution of delta quant
197   int delta_q_res;
198   int delta_lf_present_flag;
199   // Resolution of delta lf level
200   int delta_lf_res;
201   // This is a flag for number of deltas of loop filter level
202   // 0: use 1 delta, for y_vertical, y_horizontal, u, and v
203   // 1: use separate deltas for each filter level
204   int delta_lf_multi;
205 } DeltaQInfo;
206 
207 typedef struct {
208   int enable_order_hint;           // 0 - disable order hint, and related tools
209   int order_hint_bits_minus_1;     // dist_wtd_comp, ref_frame_mvs,
210                                    // frame_sign_bias
211                                    // if 0, enable_dist_wtd_comp and
212                                    // enable_ref_frame_mvs must be set as 0.
213   int enable_dist_wtd_comp;        // 0 - disable dist-wtd compound modes
214                                    // 1 - enable it
215   int enable_ref_frame_mvs;        // 0 - disable ref frame mvs
216                                    // 1 - enable it
217 } OrderHintInfo;
218 
219 // Sequence header structure.
220 // Note: All syntax elements of sequence_header_obu that need to be
221 // bit-identical across multiple sequence headers must be part of this struct,
222 // so that consistency is checked by are_seq_headers_consistent() function.
223 typedef struct SequenceHeader {
224   int num_bits_width;
225   int num_bits_height;
226   int max_frame_width;
227   int max_frame_height;
228   uint8_t frame_id_numbers_present_flag;
229   int frame_id_length;
230   int delta_frame_id_length;
231   BLOCK_SIZE sb_size;  // Size of the superblock used for this frame
232   int mib_size;        // Size of the superblock in units of MI blocks
233   int mib_size_log2;   // Log 2 of above.
234 
235   OrderHintInfo order_hint_info;
236 
237   uint8_t force_screen_content_tools;  // 0 - force off
238                                        // 1 - force on
239                                        // 2 - adaptive
240   uint8_t still_picture;               // Video is a single frame still picture
241   uint8_t reduced_still_picture_hdr;   // Use reduced header for still picture
242   uint8_t force_integer_mv;            // 0 - Don't force. MV can use subpel
243                                        // 1 - force to integer
244                                        // 2 - adaptive
245   uint8_t enable_filter_intra;         // enables/disables filterintra
246   uint8_t enable_intra_edge_filter;    // enables/disables edge upsampling
247   uint8_t enable_interintra_compound;  // enables/disables interintra_compound
248   uint8_t enable_masked_compound;      // enables/disables masked compound
249   uint8_t enable_dual_filter;          // 0 - disable dual interpolation filter
250                                        // 1 - enable vert/horz filter selection
251   uint8_t enable_warped_motion;        // 0 - disable warp for the sequence
252                                        // 1 - enable warp for the sequence
253   uint8_t enable_superres;             // 0 - Disable superres for the sequence
254                                        //     and no frame level superres flag
255                                        // 1 - Enable superres for the sequence
256                                        //     enable per-frame superres flag
257   uint8_t enable_cdef;                 // To turn on/off CDEF
258   uint8_t enable_restoration;          // To turn on/off loop restoration
259   BITSTREAM_PROFILE profile;
260 
261   // Operating point info.
262   int operating_points_cnt_minus_1;
263   int operating_point_idc[MAX_NUM_OPERATING_POINTS];
264   uint8_t display_model_info_present_flag;
265   uint8_t decoder_model_info_present_flag;
266   AV1_LEVEL seq_level_idx[MAX_NUM_OPERATING_POINTS];
267   uint8_t tier[MAX_NUM_OPERATING_POINTS];  // seq_tier in the spec. One bit: 0
268                                            // or 1.
269 
270   // Color config.
271   aom_bit_depth_t bit_depth;  // AOM_BITS_8 in profile 0 or 1,
272                               // AOM_BITS_10 or AOM_BITS_12 in profile 2 or 3.
273   uint8_t use_highbitdepth;   // If true, we need to use 16bit frame buffers.
274   uint8_t monochrome;         // Monochorme video
275   aom_color_primaries_t color_primaries;
276   aom_transfer_characteristics_t transfer_characteristics;
277   aom_matrix_coefficients_t matrix_coefficients;
278   int color_range;
279   int subsampling_x;          // Chroma subsampling for x
280   int subsampling_y;          // Chroma subsampling for y
281   aom_chroma_sample_position_t chroma_sample_position;
282   uint8_t separate_uv_delta_q;
283   uint8_t film_grain_params_present;
284 } SequenceHeader;
285 
286 typedef struct {
287     int skip_mode_allowed;
288     int skip_mode_flag;
289     int ref_frame_idx_0;
290     int ref_frame_idx_1;
291 } SkipModeInfo;
292 
293 typedef struct {
294   FRAME_TYPE frame_type;
295   REFERENCE_MODE reference_mode;
296 
297   unsigned int order_hint;
298   unsigned int frame_number;
299   SkipModeInfo skip_mode_info;
300   int refresh_frame_flags;  // Which ref frames are overwritten by this frame
301   int frame_refs_short_signaling;
302 } CurrentFrame;
303 
304 typedef struct AV1Common {
305   CurrentFrame current_frame;
306   struct aom_internal_error_info error;
307   int width;
308   int height;
309   int render_width;
310   int render_height;
311   int timing_info_present;
312   aom_timing_info_t timing_info;
313   int buffer_removal_time_present;
314   aom_dec_model_info_t buffer_model;
315   aom_dec_model_op_parameters_t op_params[MAX_NUM_OPERATING_POINTS + 1];
316   aom_op_timing_info_t op_frame_timing[MAX_NUM_OPERATING_POINTS + 1];
317   uint32_t frame_presentation_time;
318 
319   int context_update_tile_id;
320 
321   // Scale of the current frame with respect to itself.
322   struct scale_factors sf_identity;
323 
324   RefCntBuffer *prev_frame;
325 
326   // TODO(hkuang): Combine this with cur_buf in macroblockd.
327   RefCntBuffer *cur_frame;
328 
329   // For encoder, we have a two-level mapping from reference frame type to the
330   // corresponding buffer in the buffer pool:
331   // * 'remapped_ref_idx[i - 1]' maps reference type 'i' (range: LAST_FRAME ...
332   // EXTREF_FRAME) to a remapped index 'j' (in range: 0 ... REF_FRAMES - 1)
333   // * Later, 'cm->ref_frame_map[j]' maps the remapped index 'j' to a pointer to
334   // the reference counted buffer structure RefCntBuffer, taken from the buffer
335   // pool cm->buffer_pool->frame_bufs.
336   //
337   // LAST_FRAME,                        ...,      EXTREF_FRAME
338   //      |                                           |
339   //      v                                           v
340   // remapped_ref_idx[LAST_FRAME - 1],  ...,  remapped_ref_idx[EXTREF_FRAME - 1]
341   //      |                                           |
342   //      v                                           v
343   // ref_frame_map[],                   ...,     ref_frame_map[]
344   //
345   // Note: INTRA_FRAME always refers to the current frame, so there's no need to
346   // have a remapped index for the same.
347   int remapped_ref_idx[REF_FRAMES];
348 
349   struct scale_factors ref_scale_factors[REF_FRAMES];
350 
351   // For decoder, ref_frame_map[i] maps reference type 'i' to a pointer to
352   // the buffer in the buffer pool 'cm->buffer_pool.frame_bufs'.
353   // For encoder, ref_frame_map[j] (where j = remapped_ref_idx[i]) maps
354   // remapped reference index 'j' (that is, original reference type 'i') to
355   // a pointer to the buffer in the buffer pool 'cm->buffer_pool.frame_bufs'.
356   RefCntBuffer *ref_frame_map[REF_FRAMES];
357 
358   // Prepare ref_frame_map for the next frame.
359   // Only used in frame parallel decode.
360   RefCntBuffer *next_ref_frame_map[REF_FRAMES];
361   FRAME_TYPE last_frame_type; /* last frame's frame type for motion search.*/
362 
363   int show_frame;
364   int showable_frame;  // frame can be used as show existing frame in future
365   int show_existing_frame;
366 
367   uint8_t disable_cdf_update;
368   int allow_high_precision_mv;
369   uint8_t cur_frame_force_integer_mv;  // 0 the default in AOM, 1 only integer
370 
371   uint8_t allow_screen_content_tools;
372   int allow_intrabc;
373   int allow_warped_motion;
374 
375   // MBs, mb_rows/cols is in 16-pixel units; mi_rows/cols is in
376   // MB_MODE_INFO (8-pixel) units.
377   int MBs;
378   int mb_rows, mi_rows;
379   int mb_cols, mi_cols;
380   int mi_stride;
381 
382   /* profile settings */
383   TX_MODE tx_mode;
384 
385 #if CONFIG_ENTROPY_STATS
386   int coef_cdf_category;
387 #endif
388 
389   int base_qindex;
390   int y_dc_delta_q;
391   int u_dc_delta_q;
392   int v_dc_delta_q;
393   int u_ac_delta_q;
394   int v_ac_delta_q;
395 
396   // The dequantizers below are true dequantizers used only in the
397   // dequantization process.  They have the same coefficient
398   // shift/scale as TX.
399   int16_t y_dequant_QTX[MAX_SEGMENTS][2];
400   int16_t u_dequant_QTX[MAX_SEGMENTS][2];
401   int16_t v_dequant_QTX[MAX_SEGMENTS][2];
402 
403   // Global quant matrix tables
404   const qm_val_t *giqmatrix[NUM_QM_LEVELS][3][TX_SIZES_ALL];
405   const qm_val_t *gqmatrix[NUM_QM_LEVELS][3][TX_SIZES_ALL];
406 
407   // Local quant matrix tables for each frame
408   const qm_val_t *y_iqmatrix[MAX_SEGMENTS][TX_SIZES_ALL];
409   const qm_val_t *u_iqmatrix[MAX_SEGMENTS][TX_SIZES_ALL];
410   const qm_val_t *v_iqmatrix[MAX_SEGMENTS][TX_SIZES_ALL];
411 
412   // Encoder
413   int using_qmatrix;
414   int qm_y;
415   int qm_u;
416   int qm_v;
417   int min_qmlevel;
418   int max_qmlevel;
419   int use_quant_b_adapt;
420 
421   /* We allocate a MB_MODE_INFO struct for each macroblock, together with
422      an extra row on top and column on the left to simplify prediction. */
423   int mi_alloc_size;
424   MB_MODE_INFO *mip; /* Base of allocated array */
425   MB_MODE_INFO *mi;  /* Corresponds to upper left visible macroblock */
426 
427   // TODO(agrange): Move prev_mi into encoder structure.
428   // prev_mip and prev_mi will only be allocated in encoder.
429   MB_MODE_INFO *prev_mip; /* MB_MODE_INFO array 'mip' from last decoded frame */
430   MB_MODE_INFO *prev_mi;  /* 'mi' from last frame (points into prev_mip) */
431 
432   // Separate mi functions between encoder and decoder.
433   int (*alloc_mi)(struct AV1Common *cm, int mi_size);
434   void (*free_mi)(struct AV1Common *cm);
435   void (*setup_mi)(struct AV1Common *cm);
436 
437   // Grid of pointers to 8x8 MB_MODE_INFO structs.  Any 8x8 not in the visible
438   // area will be NULL.
439   MB_MODE_INFO **mi_grid_base;
440   MB_MODE_INFO **mi_grid_visible;
441   MB_MODE_INFO **prev_mi_grid_base;
442   MB_MODE_INFO **prev_mi_grid_visible;
443 
444   // Whether to use previous frames' motion vectors for prediction.
445   int allow_ref_frame_mvs;
446 
447   uint8_t *last_frame_seg_map;
448 
449   InterpFilter interp_filter;
450 
451   int switchable_motion_mode;
452 
453   loop_filter_info_n lf_info;
454   // The denominator of the superres scale; the numerator is fixed.
455   uint8_t superres_scale_denominator;
456   int superres_upscaled_width;
457   int superres_upscaled_height;
458   RestorationInfo rst_info[MAX_MB_PLANE];
459 
460   // Pointer to a scratch buffer used by self-guided restoration
461   int32_t *rst_tmpbuf;
462   RestorationLineBuffers *rlbs;
463 
464   // Output of loop restoration
465   YV12_BUFFER_CONFIG rst_frame;
466 
467   // Flag signaling how frame contexts should be updated at the end of
468   // a frame decode
469   REFRESH_FRAME_CONTEXT_MODE refresh_frame_context;
470 
471   int ref_frame_sign_bias[REF_FRAMES]; /* Two state 0, 1 */
472 
473   struct loopfilter lf;
474   struct segmentation seg;
475   int coded_lossless;  // frame is fully lossless at the coded resolution.
476   int all_lossless;    // frame is fully lossless at the upscaled resolution.
477 
478   int reduced_tx_set_used;
479 
480   // Context probabilities for reference frame prediction
481   MV_REFERENCE_FRAME comp_fwd_ref[FWD_REFS];
482   MV_REFERENCE_FRAME comp_bwd_ref[BWD_REFS];
483 
484   FRAME_CONTEXT *fc;              /* this frame entropy */
485   FRAME_CONTEXT *default_frame_context;
486   int primary_ref_frame;
487 
488   int error_resilient_mode;
489 
490   int tile_cols, tile_rows;
491 
492   int max_tile_width_sb;
493   int min_log2_tile_cols;
494   int max_log2_tile_cols;
495   int max_log2_tile_rows;
496   int min_log2_tile_rows;
497   int min_log2_tiles;
498   int max_tile_height_sb;
499   int uniform_tile_spacing_flag;
500   int log2_tile_cols;                        // only valid for uniform tiles
501   int log2_tile_rows;                        // only valid for uniform tiles
502   int tile_col_start_sb[MAX_TILE_COLS + 1];  // valid for 0 <= i <= tile_cols
503   int tile_row_start_sb[MAX_TILE_ROWS + 1];  // valid for 0 <= i <= tile_rows
504   int tile_width, tile_height;               // In MI units
505   int min_inner_tile_width;                  // min width of non-rightmost tile
506 
507   unsigned int large_scale_tile;
508   unsigned int single_tile_decoding;
509 
510   int byte_alignment;
511   int skip_loop_filter;
512   int skip_film_grain;
513 
514   // External BufferPool passed from outside.
515   BufferPool *buffer_pool;
516 
517   PARTITION_CONTEXT **above_seg_context;
518   ENTROPY_CONTEXT **above_context[MAX_MB_PLANE];
519   TXFM_CONTEXT **above_txfm_context;
520   WarpedMotionParams global_motion[REF_FRAMES];
521   aom_film_grain_t film_grain_params;
522 
523   CdefInfo cdef_info;
524   DeltaQInfo delta_q_info;  // Delta Q and Delta LF parameters
525 
526   int num_tg;
527   SequenceHeader seq_params;
528   int current_frame_id;
529   int ref_frame_id[REF_FRAMES];
530   int valid_for_referencing[REF_FRAMES];
531   TPL_MV_REF *tpl_mvs;
532   int tpl_mvs_mem_size;
533   // TODO(jingning): This can be combined with sign_bias later.
534   int8_t ref_frame_side[REF_FRAMES];
535 
536   int is_annexb;
537 
538   int temporal_layer_id;
539   int spatial_layer_id;
540   unsigned int number_temporal_layers;
541   unsigned int number_spatial_layers;
542   int num_allocated_above_context_mi_col;
543   int num_allocated_above_contexts;
544   int num_allocated_above_context_planes;
545 
546 #if TXCOEFF_TIMER
547   int64_t cum_txcoeff_timer;
548   int64_t txcoeff_timer;
549   int txb_count;
550 #endif
551 
552 #if TXCOEFF_COST_TIMER
553   int64_t cum_txcoeff_cost_timer;
554   int64_t txcoeff_cost_timer;
555   int64_t txcoeff_cost_count;
556 #endif
557   const cfg_options_t *options;
558   int is_decoding;
559 } AV1_COMMON;
560 
561 // TODO(hkuang): Don't need to lock the whole pool after implementing atomic
562 // frame reference count.
lock_buffer_pool(BufferPool * const pool)563 static void lock_buffer_pool(BufferPool *const pool) {
564 #if CONFIG_MULTITHREAD
565   pthread_mutex_lock(&pool->pool_mutex);
566 #else
567   (void)pool;
568 #endif
569 }
570 
unlock_buffer_pool(BufferPool * const pool)571 static void unlock_buffer_pool(BufferPool *const pool) {
572 #if CONFIG_MULTITHREAD
573   pthread_mutex_unlock(&pool->pool_mutex);
574 #else
575   (void)pool;
576 #endif
577 }
578 
get_ref_frame(AV1_COMMON * cm,int index)579 static INLINE YV12_BUFFER_CONFIG *get_ref_frame(AV1_COMMON *cm, int index) {
580   if (index < 0 || index >= REF_FRAMES) return NULL;
581   if (cm->ref_frame_map[index] == NULL) return NULL;
582   return &cm->ref_frame_map[index]->buf;
583 }
584 
get_free_fb(AV1_COMMON * cm)585 static INLINE int get_free_fb(AV1_COMMON *cm) {
586   RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
587   int i;
588 
589   lock_buffer_pool(cm->buffer_pool);
590   for (i = 0; i < FRAME_BUFFERS; ++i)
591     if (frame_bufs[i].ref_count == 0) break;
592 
593   if (i != FRAME_BUFFERS) {
594     if (frame_bufs[i].buf.use_external_reference_buffers) {
595       // If this frame buffer's y_buffer, u_buffer, and v_buffer point to the
596       // external reference buffers. Restore the buffer pointers to point to the
597       // internally allocated memory.
598       YV12_BUFFER_CONFIG *ybf = &frame_bufs[i].buf;
599       ybf->y_buffer = ybf->store_buf_adr[0];
600       ybf->u_buffer = ybf->store_buf_adr[1];
601       ybf->v_buffer = ybf->store_buf_adr[2];
602       ybf->use_external_reference_buffers = 0;
603     }
604 
605     frame_bufs[i].ref_count = 1;
606   } else {
607     // We should never run out of free buffers. If this assertion fails, there
608     // is a reference leak.
609     assert(0 && "Ran out of free frame buffers. Likely a reference leak.");
610     // Reset i to be INVALID_IDX to indicate no free buffer found.
611     i = INVALID_IDX;
612   }
613 
614   unlock_buffer_pool(cm->buffer_pool);
615   return i;
616 }
617 
assign_cur_frame_new_fb(AV1_COMMON * const cm)618 static INLINE RefCntBuffer *assign_cur_frame_new_fb(AV1_COMMON *const cm) {
619   // Release the previously-used frame-buffer
620   if (cm->cur_frame != NULL) {
621     --cm->cur_frame->ref_count;
622     cm->cur_frame = NULL;
623   }
624 
625   // Assign a new framebuffer
626   const int new_fb_idx = get_free_fb(cm);
627   if (new_fb_idx == INVALID_IDX) return NULL;
628 
629   cm->cur_frame = &cm->buffer_pool->frame_bufs[new_fb_idx];
630   cm->cur_frame->buf.buf_8bit_valid = 0;
631   av1_zero(cm->cur_frame->interp_filter_selected);
632   return cm->cur_frame;
633 }
634 
635 // Modify 'lhs_ptr' to reference the buffer at 'rhs_ptr', and update the ref
636 // counts accordingly.
assign_frame_buffer_p(RefCntBuffer ** lhs_ptr,RefCntBuffer * rhs_ptr)637 static INLINE void assign_frame_buffer_p(RefCntBuffer **lhs_ptr,
638                                        RefCntBuffer *rhs_ptr) {
639   RefCntBuffer *const old_ptr = *lhs_ptr;
640   if (old_ptr != NULL) {
641     assert(old_ptr->ref_count > 0);
642     // One less reference to the buffer at 'old_ptr', so decrease ref count.
643     --old_ptr->ref_count;
644   }
645 
646   *lhs_ptr = rhs_ptr;
647   // One more reference to the buffer at 'rhs_ptr', so increase ref count.
648   ++rhs_ptr->ref_count;
649 }
650 
frame_is_intra_only(const AV1_COMMON * const cm)651 static INLINE int frame_is_intra_only(const AV1_COMMON *const cm) {
652   return cm->current_frame.frame_type == KEY_FRAME ||
653       cm->current_frame.frame_type == INTRA_ONLY_FRAME;
654 }
655 
frame_is_sframe(const AV1_COMMON * cm)656 static INLINE int frame_is_sframe(const AV1_COMMON *cm) {
657   return cm->current_frame.frame_type == S_FRAME;
658 }
659 
660 // These functions take a reference frame label between LAST_FRAME and
661 // EXTREF_FRAME inclusive.  Note that this is different to the indexing
662 // previously used by the frame_refs[] array.
get_ref_frame_map_idx(const AV1_COMMON * const cm,const MV_REFERENCE_FRAME ref_frame)663 static INLINE int get_ref_frame_map_idx(const AV1_COMMON *const cm,
664                                         const MV_REFERENCE_FRAME ref_frame) {
665   return (ref_frame >= LAST_FRAME && ref_frame <= EXTREF_FRAME)
666              ? cm->remapped_ref_idx[ref_frame - LAST_FRAME]
667              : INVALID_IDX;
668 }
669 
get_ref_frame_buf(const AV1_COMMON * const cm,const MV_REFERENCE_FRAME ref_frame)670 static INLINE RefCntBuffer *get_ref_frame_buf(
671     const AV1_COMMON *const cm, const MV_REFERENCE_FRAME ref_frame) {
672   const int map_idx = get_ref_frame_map_idx(cm, ref_frame);
673   return (map_idx != INVALID_IDX) ? cm->ref_frame_map[map_idx] : NULL;
674 }
675 
676 // Both const and non-const versions of this function are provided so that it
677 // can be used with a const AV1_COMMON if needed.
get_ref_scale_factors_const(const AV1_COMMON * const cm,const MV_REFERENCE_FRAME ref_frame)678 static INLINE const struct scale_factors *get_ref_scale_factors_const(
679     const AV1_COMMON *const cm, const MV_REFERENCE_FRAME ref_frame) {
680   const int map_idx = get_ref_frame_map_idx(cm, ref_frame);
681   return (map_idx != INVALID_IDX) ? &cm->ref_scale_factors[map_idx] : NULL;
682 }
683 
get_ref_scale_factors(AV1_COMMON * const cm,const MV_REFERENCE_FRAME ref_frame)684 static INLINE struct scale_factors *get_ref_scale_factors(
685     AV1_COMMON *const cm, const MV_REFERENCE_FRAME ref_frame) {
686   const int map_idx = get_ref_frame_map_idx(cm, ref_frame);
687   return (map_idx != INVALID_IDX) ? &cm->ref_scale_factors[map_idx] : NULL;
688 }
689 
get_primary_ref_frame_buf(const AV1_COMMON * const cm)690 static INLINE RefCntBuffer *get_primary_ref_frame_buf(
691     const AV1_COMMON *const cm) {
692   if (cm->primary_ref_frame == PRIMARY_REF_NONE) return NULL;
693   const int map_idx = get_ref_frame_map_idx(cm, cm->primary_ref_frame + 1);
694   return (map_idx != INVALID_IDX) ? cm->ref_frame_map[map_idx] : NULL;
695 }
696 
697 // Returns 1 if this frame might allow mvs from some reference frame.
frame_might_allow_ref_frame_mvs(const AV1_COMMON * cm)698 static INLINE int frame_might_allow_ref_frame_mvs(const AV1_COMMON *cm) {
699   return !cm->error_resilient_mode &&
700     cm->seq_params.order_hint_info.enable_ref_frame_mvs &&
701     cm->seq_params.order_hint_info.enable_order_hint &&
702     !frame_is_intra_only(cm);
703 }
704 
705 // Returns 1 if this frame might use warped_motion
frame_might_allow_warped_motion(const AV1_COMMON * cm)706 static INLINE int frame_might_allow_warped_motion(const AV1_COMMON *cm) {
707   return !cm->error_resilient_mode && !frame_is_intra_only(cm) &&
708          cm->seq_params.enable_warped_motion;
709 }
710 
ensure_mv_buffer(RefCntBuffer * buf,AV1_COMMON * cm)711 static INLINE void ensure_mv_buffer(RefCntBuffer *buf, AV1_COMMON *cm) {
712   const int buf_rows = buf->mi_rows;
713   const int buf_cols = buf->mi_cols;
714 
715   if (buf->mvs == NULL || buf_rows != cm->mi_rows || buf_cols != cm->mi_cols) {
716     aom_free(buf->mvs);
717     buf->mi_rows = cm->mi_rows;
718     buf->mi_cols = cm->mi_cols;
719     CHECK_MEM_ERROR(cm, buf->mvs,
720                     (MV_REF *)aom_calloc(
721                         ((cm->mi_rows + 1) >> 1) * ((cm->mi_cols + 1) >> 1),
722                         sizeof(*buf->mvs)));
723     aom_free(buf->seg_map);
724     CHECK_MEM_ERROR(cm, buf->seg_map,
725                     (uint8_t *)aom_calloc(cm->mi_rows * cm->mi_cols,
726                                           sizeof(*buf->seg_map)));
727   }
728 
729   const int mem_size =
730       ((cm->mi_rows + MAX_MIB_SIZE) >> 1) * (cm->mi_stride >> 1);
731   int realloc = cm->tpl_mvs == NULL;
732   if (cm->tpl_mvs) realloc |= cm->tpl_mvs_mem_size < mem_size;
733 
734   if (realloc) {
735     aom_free(cm->tpl_mvs);
736     CHECK_MEM_ERROR(cm, cm->tpl_mvs,
737                     (TPL_MV_REF *)aom_calloc(mem_size, sizeof(*cm->tpl_mvs)));
738     cm->tpl_mvs_mem_size = mem_size;
739   }
740 }
741 
742 void cfl_init(CFL_CTX *cfl, const SequenceHeader *seq_params);
743 
av1_num_planes(const AV1_COMMON * cm)744 static INLINE int av1_num_planes(const AV1_COMMON *cm) {
745   return cm->seq_params.monochrome ? 1 : MAX_MB_PLANE;
746 }
747 
av1_init_above_context(AV1_COMMON * cm,MACROBLOCKD * xd,const int tile_row)748 static INLINE void av1_init_above_context(AV1_COMMON *cm, MACROBLOCKD *xd,
749                                           const int tile_row) {
750   const int num_planes = av1_num_planes(cm);
751   for (int i = 0; i < num_planes; ++i) {
752     xd->above_context[i] = cm->above_context[i][tile_row];
753   }
754   xd->above_seg_context = cm->above_seg_context[tile_row];
755   xd->above_txfm_context = cm->above_txfm_context[tile_row];
756 }
757 
av1_init_macroblockd(AV1_COMMON * cm,MACROBLOCKD * xd,tran_low_t * dqcoeff)758 static INLINE void av1_init_macroblockd(AV1_COMMON *cm, MACROBLOCKD *xd,
759                                         tran_low_t *dqcoeff) {
760   const int num_planes = av1_num_planes(cm);
761   for (int i = 0; i < num_planes; ++i) {
762     xd->plane[i].dqcoeff = dqcoeff;
763 
764     if (xd->plane[i].plane_type == PLANE_TYPE_Y) {
765       memcpy(xd->plane[i].seg_dequant_QTX, cm->y_dequant_QTX,
766              sizeof(cm->y_dequant_QTX));
767       memcpy(xd->plane[i].seg_iqmatrix, cm->y_iqmatrix, sizeof(cm->y_iqmatrix));
768 
769     } else {
770       if (i == AOM_PLANE_U) {
771         memcpy(xd->plane[i].seg_dequant_QTX, cm->u_dequant_QTX,
772                sizeof(cm->u_dequant_QTX));
773         memcpy(xd->plane[i].seg_iqmatrix, cm->u_iqmatrix,
774                sizeof(cm->u_iqmatrix));
775       } else {
776         memcpy(xd->plane[i].seg_dequant_QTX, cm->v_dequant_QTX,
777                sizeof(cm->v_dequant_QTX));
778         memcpy(xd->plane[i].seg_iqmatrix, cm->v_iqmatrix,
779                sizeof(cm->v_iqmatrix));
780       }
781     }
782   }
783   xd->mi_stride = cm->mi_stride;
784   xd->error_info = &cm->error;
785   cfl_init(&xd->cfl, &cm->seq_params);
786 }
787 
set_skip_context(MACROBLOCKD * xd,int mi_row,int mi_col,const int num_planes)788 static INLINE void set_skip_context(MACROBLOCKD *xd, int mi_row, int mi_col,
789                                     const int num_planes) {
790   int i;
791   int row_offset = mi_row;
792   int col_offset = mi_col;
793   for (i = 0; i < num_planes; ++i) {
794     struct macroblockd_plane *const pd = &xd->plane[i];
795     // Offset the buffer pointer
796     const BLOCK_SIZE bsize = xd->mi[0]->sb_type;
797     if (pd->subsampling_y && (mi_row & 0x01) && (mi_size_high[bsize] == 1))
798       row_offset = mi_row - 1;
799     if (pd->subsampling_x && (mi_col & 0x01) && (mi_size_wide[bsize] == 1))
800       col_offset = mi_col - 1;
801     int above_idx = col_offset;
802     int left_idx = row_offset & MAX_MIB_MASK;
803     pd->above_context = &xd->above_context[i][above_idx >> pd->subsampling_x];
804     pd->left_context = &xd->left_context[i][left_idx >> pd->subsampling_y];
805   }
806 }
807 
calc_mi_size(int len)808 static INLINE int calc_mi_size(int len) {
809   // len is in mi units. Align to a multiple of SBs.
810   return ALIGN_POWER_OF_TWO(len, MAX_MIB_SIZE_LOG2);
811 }
812 
set_plane_n4(MACROBLOCKD * const xd,int bw,int bh,const int num_planes)813 static INLINE void set_plane_n4(MACROBLOCKD *const xd, int bw, int bh,
814                                 const int num_planes) {
815   int i;
816   for (i = 0; i < num_planes; i++) {
817     xd->plane[i].width = (bw * MI_SIZE) >> xd->plane[i].subsampling_x;
818     xd->plane[i].height = (bh * MI_SIZE) >> xd->plane[i].subsampling_y;
819 
820     xd->plane[i].width = AOMMAX(xd->plane[i].width, 4);
821     xd->plane[i].height = AOMMAX(xd->plane[i].height, 4);
822   }
823 }
824 
set_mi_row_col(MACROBLOCKD * xd,const TileInfo * const tile,int mi_row,int bh,int mi_col,int bw,int mi_rows,int mi_cols)825 static INLINE void set_mi_row_col(MACROBLOCKD *xd, const TileInfo *const tile,
826                                   int mi_row, int bh, int mi_col, int bw,
827                                   int mi_rows, int mi_cols) {
828   xd->mb_to_top_edge = -((mi_row * MI_SIZE) * 8);
829   xd->mb_to_bottom_edge = ((mi_rows - bh - mi_row) * MI_SIZE) * 8;
830   xd->mb_to_left_edge = -((mi_col * MI_SIZE) * 8);
831   xd->mb_to_right_edge = ((mi_cols - bw - mi_col) * MI_SIZE) * 8;
832 
833   // Are edges available for intra prediction?
834   xd->up_available = (mi_row > tile->mi_row_start);
835 
836   const int ss_x = xd->plane[1].subsampling_x;
837   const int ss_y = xd->plane[1].subsampling_y;
838 
839   xd->left_available = (mi_col > tile->mi_col_start);
840   xd->chroma_up_available = xd->up_available;
841   xd->chroma_left_available = xd->left_available;
842   if (ss_x && bw < mi_size_wide[BLOCK_8X8])
843     xd->chroma_left_available = (mi_col - 1) > tile->mi_col_start;
844   if (ss_y && bh < mi_size_high[BLOCK_8X8])
845     xd->chroma_up_available = (mi_row - 1) > tile->mi_row_start;
846   if (xd->up_available) {
847     xd->above_mbmi = xd->mi[-xd->mi_stride];
848   } else {
849     xd->above_mbmi = NULL;
850   }
851 
852   if (xd->left_available) {
853     xd->left_mbmi = xd->mi[-1];
854   } else {
855     xd->left_mbmi = NULL;
856   }
857 
858   const int chroma_ref = ((mi_row & 0x01) || !(bh & 0x01) || !ss_y) &&
859                          ((mi_col & 0x01) || !(bw & 0x01) || !ss_x);
860   if (chroma_ref) {
861     // To help calculate the "above" and "left" chroma blocks, note that the
862     // current block may cover multiple luma blocks (eg, if partitioned into
863     // 4x4 luma blocks).
864     // First, find the top-left-most luma block covered by this chroma block
865     MB_MODE_INFO **base_mi =
866         &xd->mi[-(mi_row & ss_y) * xd->mi_stride - (mi_col & ss_x)];
867 
868     // Then, we consider the luma region covered by the left or above 4x4 chroma
869     // prediction. We want to point to the chroma reference block in that
870     // region, which is the bottom-right-most mi unit.
871     // This leads to the following offsets:
872     MB_MODE_INFO *chroma_above_mi =
873         xd->chroma_up_available ? base_mi[-xd->mi_stride + ss_x] : NULL;
874     xd->chroma_above_mbmi = chroma_above_mi;
875 
876     MB_MODE_INFO *chroma_left_mi =
877         xd->chroma_left_available ? base_mi[ss_y * xd->mi_stride - 1] : NULL;
878     xd->chroma_left_mbmi = chroma_left_mi;
879   }
880 
881   xd->n4_h = bh;
882   xd->n4_w = bw;
883   xd->is_sec_rect = 0;
884   if (xd->n4_w < xd->n4_h) {
885     // Only mark is_sec_rect as 1 for the last block.
886     // For PARTITION_VERT_4, it would be (0, 0, 0, 1);
887     // For other partitions, it would be (0, 1).
888     if (!((mi_col + xd->n4_w) & (xd->n4_h - 1))) xd->is_sec_rect = 1;
889   }
890 
891   if (xd->n4_w > xd->n4_h)
892     if (mi_row & (xd->n4_w - 1)) xd->is_sec_rect = 1;
893 }
894 
get_y_mode_cdf(FRAME_CONTEXT * tile_ctx,const MB_MODE_INFO * above_mi,const MB_MODE_INFO * left_mi)895 static INLINE aom_cdf_prob *get_y_mode_cdf(FRAME_CONTEXT *tile_ctx,
896                                            const MB_MODE_INFO *above_mi,
897                                            const MB_MODE_INFO *left_mi) {
898   const PREDICTION_MODE above = av1_above_block_mode(above_mi);
899   const PREDICTION_MODE left = av1_left_block_mode(left_mi);
900   const int above_ctx = intra_mode_context[above];
901   const int left_ctx = intra_mode_context[left];
902   return tile_ctx->kf_y_cdf[above_ctx][left_ctx];
903 }
904 
update_partition_context(MACROBLOCKD * xd,int mi_row,int mi_col,BLOCK_SIZE subsize,BLOCK_SIZE bsize)905 static INLINE void update_partition_context(MACROBLOCKD *xd, int mi_row,
906                                             int mi_col, BLOCK_SIZE subsize,
907                                             BLOCK_SIZE bsize) {
908   PARTITION_CONTEXT *const above_ctx = xd->above_seg_context + mi_col;
909   PARTITION_CONTEXT *const left_ctx =
910       xd->left_seg_context + (mi_row & MAX_MIB_MASK);
911 
912   const int bw = mi_size_wide[bsize];
913   const int bh = mi_size_high[bsize];
914   memset(above_ctx, partition_context_lookup[subsize].above, bw);
915   memset(left_ctx, partition_context_lookup[subsize].left, bh);
916 }
917 
is_chroma_reference(int mi_row,int mi_col,BLOCK_SIZE bsize,int subsampling_x,int subsampling_y)918 static INLINE int is_chroma_reference(int mi_row, int mi_col, BLOCK_SIZE bsize,
919                                       int subsampling_x, int subsampling_y) {
920   const int bw = mi_size_wide[bsize];
921   const int bh = mi_size_high[bsize];
922   int ref_pos = ((mi_row & 0x01) || !(bh & 0x01) || !subsampling_y) &&
923                 ((mi_col & 0x01) || !(bw & 0x01) || !subsampling_x);
924   return ref_pos;
925 }
926 
scale_chroma_bsize(BLOCK_SIZE bsize,int subsampling_x,int subsampling_y)927 static INLINE BLOCK_SIZE scale_chroma_bsize(BLOCK_SIZE bsize, int subsampling_x,
928                                             int subsampling_y) {
929   BLOCK_SIZE bs = bsize;
930   switch (bsize) {
931     case BLOCK_4X4:
932       if (subsampling_x == 1 && subsampling_y == 1)
933         bs = BLOCK_8X8;
934       else if (subsampling_x == 1)
935         bs = BLOCK_8X4;
936       else if (subsampling_y == 1)
937         bs = BLOCK_4X8;
938       break;
939     case BLOCK_4X8:
940       if (subsampling_x == 1 && subsampling_y == 1)
941         bs = BLOCK_8X8;
942       else if (subsampling_x == 1)
943         bs = BLOCK_8X8;
944       else if (subsampling_y == 1)
945         bs = BLOCK_4X8;
946       break;
947     case BLOCK_8X4:
948       if (subsampling_x == 1 && subsampling_y == 1)
949         bs = BLOCK_8X8;
950       else if (subsampling_x == 1)
951         bs = BLOCK_8X4;
952       else if (subsampling_y == 1)
953         bs = BLOCK_8X8;
954       break;
955     case BLOCK_4X16:
956       if (subsampling_x == 1 && subsampling_y == 1)
957         bs = BLOCK_8X16;
958       else if (subsampling_x == 1)
959         bs = BLOCK_8X16;
960       else if (subsampling_y == 1)
961         bs = BLOCK_4X16;
962       break;
963     case BLOCK_16X4:
964       if (subsampling_x == 1 && subsampling_y == 1)
965         bs = BLOCK_16X8;
966       else if (subsampling_x == 1)
967         bs = BLOCK_16X4;
968       else if (subsampling_y == 1)
969         bs = BLOCK_16X8;
970       break;
971     default: break;
972   }
973   return bs;
974 }
975 
cdf_element_prob(const aom_cdf_prob * cdf,size_t element)976 static INLINE aom_cdf_prob cdf_element_prob(const aom_cdf_prob *cdf,
977                                             size_t element) {
978   assert(cdf != NULL);
979   return (element > 0 ? cdf[element - 1] : CDF_PROB_TOP) - cdf[element];
980 }
981 
partition_gather_horz_alike(aom_cdf_prob * out,const aom_cdf_prob * const in,BLOCK_SIZE bsize)982 static INLINE void partition_gather_horz_alike(aom_cdf_prob *out,
983                                                const aom_cdf_prob *const in,
984                                                BLOCK_SIZE bsize) {
985   (void)bsize;
986   out[0] = CDF_PROB_TOP;
987   out[0] -= cdf_element_prob(in, PARTITION_HORZ);
988   out[0] -= cdf_element_prob(in, PARTITION_SPLIT);
989   out[0] -= cdf_element_prob(in, PARTITION_HORZ_A);
990   out[0] -= cdf_element_prob(in, PARTITION_HORZ_B);
991   out[0] -= cdf_element_prob(in, PARTITION_VERT_A);
992   if (bsize != BLOCK_128X128) out[0] -= cdf_element_prob(in, PARTITION_HORZ_4);
993   out[0] = AOM_ICDF(out[0]);
994   out[1] = AOM_ICDF(CDF_PROB_TOP);
995 }
996 
partition_gather_vert_alike(aom_cdf_prob * out,const aom_cdf_prob * const in,BLOCK_SIZE bsize)997 static INLINE void partition_gather_vert_alike(aom_cdf_prob *out,
998                                                const aom_cdf_prob *const in,
999                                                BLOCK_SIZE bsize) {
1000   (void)bsize;
1001   out[0] = CDF_PROB_TOP;
1002   out[0] -= cdf_element_prob(in, PARTITION_VERT);
1003   out[0] -= cdf_element_prob(in, PARTITION_SPLIT);
1004   out[0] -= cdf_element_prob(in, PARTITION_HORZ_A);
1005   out[0] -= cdf_element_prob(in, PARTITION_VERT_A);
1006   out[0] -= cdf_element_prob(in, PARTITION_VERT_B);
1007   if (bsize != BLOCK_128X128) out[0] -= cdf_element_prob(in, PARTITION_VERT_4);
1008   out[0] = AOM_ICDF(out[0]);
1009   out[1] = AOM_ICDF(CDF_PROB_TOP);
1010 }
1011 
update_ext_partition_context(MACROBLOCKD * xd,int mi_row,int mi_col,BLOCK_SIZE subsize,BLOCK_SIZE bsize,PARTITION_TYPE partition)1012 static INLINE void update_ext_partition_context(MACROBLOCKD *xd, int mi_row,
1013                                                 int mi_col, BLOCK_SIZE subsize,
1014                                                 BLOCK_SIZE bsize,
1015                                                 PARTITION_TYPE partition) {
1016   if (bsize >= BLOCK_8X8) {
1017     const int hbs = mi_size_wide[bsize] / 2;
1018     BLOCK_SIZE bsize2 = get_partition_subsize(bsize, PARTITION_SPLIT);
1019     switch (partition) {
1020       case PARTITION_SPLIT:
1021         if (bsize != BLOCK_8X8) break;
1022         AOM_FALLTHROUGH_INTENDED;
1023       case PARTITION_NONE:
1024       case PARTITION_HORZ:
1025       case PARTITION_VERT:
1026       case PARTITION_HORZ_4:
1027       case PARTITION_VERT_4:
1028         update_partition_context(xd, mi_row, mi_col, subsize, bsize);
1029         break;
1030       case PARTITION_HORZ_A:
1031         update_partition_context(xd, mi_row, mi_col, bsize2, subsize);
1032         update_partition_context(xd, mi_row + hbs, mi_col, subsize, subsize);
1033         break;
1034       case PARTITION_HORZ_B:
1035         update_partition_context(xd, mi_row, mi_col, subsize, subsize);
1036         update_partition_context(xd, mi_row + hbs, mi_col, bsize2, subsize);
1037         break;
1038       case PARTITION_VERT_A:
1039         update_partition_context(xd, mi_row, mi_col, bsize2, subsize);
1040         update_partition_context(xd, mi_row, mi_col + hbs, subsize, subsize);
1041         break;
1042       case PARTITION_VERT_B:
1043         update_partition_context(xd, mi_row, mi_col, subsize, subsize);
1044         update_partition_context(xd, mi_row, mi_col + hbs, bsize2, subsize);
1045         break;
1046       default: assert(0 && "Invalid partition type");
1047     }
1048   }
1049 }
1050 
partition_plane_context(const MACROBLOCKD * xd,int mi_row,int mi_col,BLOCK_SIZE bsize)1051 static INLINE int partition_plane_context(const MACROBLOCKD *xd, int mi_row,
1052                                           int mi_col, BLOCK_SIZE bsize) {
1053   const PARTITION_CONTEXT *above_ctx = xd->above_seg_context + mi_col;
1054   const PARTITION_CONTEXT *left_ctx =
1055       xd->left_seg_context + (mi_row & MAX_MIB_MASK);
1056   // Minimum partition point is 8x8. Offset the bsl accordingly.
1057   const int bsl = mi_size_wide_log2[bsize] - mi_size_wide_log2[BLOCK_8X8];
1058   int above = (*above_ctx >> bsl) & 1, left = (*left_ctx >> bsl) & 1;
1059 
1060   assert(mi_size_wide_log2[bsize] == mi_size_high_log2[bsize]);
1061   assert(bsl >= 0);
1062 
1063   return (left * 2 + above) + bsl * PARTITION_PLOFFSET;
1064 }
1065 
1066 // Return the number of elements in the partition CDF when
1067 // partitioning the (square) block with luma block size of bsize.
partition_cdf_length(BLOCK_SIZE bsize)1068 static INLINE int partition_cdf_length(BLOCK_SIZE bsize) {
1069   if (bsize <= BLOCK_8X8)
1070     return PARTITION_TYPES;
1071   else if (bsize == BLOCK_128X128)
1072     return EXT_PARTITION_TYPES - 2;
1073   else
1074     return EXT_PARTITION_TYPES;
1075 }
1076 
max_block_wide(const MACROBLOCKD * xd,BLOCK_SIZE bsize,int plane)1077 static INLINE int max_block_wide(const MACROBLOCKD *xd, BLOCK_SIZE bsize,
1078                                  int plane) {
1079   int max_blocks_wide = block_size_wide[bsize];
1080   const struct macroblockd_plane *const pd = &xd->plane[plane];
1081 
1082   if (xd->mb_to_right_edge < 0)
1083     max_blocks_wide += xd->mb_to_right_edge >> (3 + pd->subsampling_x);
1084 
1085   // Scale the width in the transform block unit.
1086   return max_blocks_wide >> tx_size_wide_log2[0];
1087 }
1088 
max_block_high(const MACROBLOCKD * xd,BLOCK_SIZE bsize,int plane)1089 static INLINE int max_block_high(const MACROBLOCKD *xd, BLOCK_SIZE bsize,
1090                                  int plane) {
1091   int max_blocks_high = block_size_high[bsize];
1092   const struct macroblockd_plane *const pd = &xd->plane[plane];
1093 
1094   if (xd->mb_to_bottom_edge < 0)
1095     max_blocks_high += xd->mb_to_bottom_edge >> (3 + pd->subsampling_y);
1096 
1097   // Scale the height in the transform block unit.
1098   return max_blocks_high >> tx_size_high_log2[0];
1099 }
1100 
max_intra_block_width(const MACROBLOCKD * xd,BLOCK_SIZE plane_bsize,int plane,TX_SIZE tx_size)1101 static INLINE int max_intra_block_width(const MACROBLOCKD *xd,
1102                                         BLOCK_SIZE plane_bsize, int plane,
1103                                         TX_SIZE tx_size) {
1104   const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane)
1105                               << tx_size_wide_log2[0];
1106   return ALIGN_POWER_OF_TWO(max_blocks_wide, tx_size_wide_log2[tx_size]);
1107 }
1108 
max_intra_block_height(const MACROBLOCKD * xd,BLOCK_SIZE plane_bsize,int plane,TX_SIZE tx_size)1109 static INLINE int max_intra_block_height(const MACROBLOCKD *xd,
1110                                          BLOCK_SIZE plane_bsize, int plane,
1111                                          TX_SIZE tx_size) {
1112   const int max_blocks_high = max_block_high(xd, plane_bsize, plane)
1113                               << tx_size_high_log2[0];
1114   return ALIGN_POWER_OF_TWO(max_blocks_high, tx_size_high_log2[tx_size]);
1115 }
1116 
av1_zero_above_context(AV1_COMMON * const cm,const MACROBLOCKD * xd,int mi_col_start,int mi_col_end,const int tile_row)1117 static INLINE void av1_zero_above_context(AV1_COMMON *const cm, const MACROBLOCKD *xd,
1118   int mi_col_start, int mi_col_end, const int tile_row) {
1119   const SequenceHeader *const seq_params = &cm->seq_params;
1120   const int num_planes = av1_num_planes(cm);
1121   const int width = mi_col_end - mi_col_start;
1122   const int aligned_width =
1123     ALIGN_POWER_OF_TWO(width, seq_params->mib_size_log2);
1124 
1125   const int offset_y = mi_col_start;
1126   const int width_y = aligned_width;
1127   const int offset_uv = offset_y >> seq_params->subsampling_x;
1128   const int width_uv = width_y >> seq_params->subsampling_x;
1129 
1130   av1_zero_array(cm->above_context[0][tile_row] + offset_y, width_y);
1131   if (num_planes > 1) {
1132     if (cm->above_context[1][tile_row] && cm->above_context[2][tile_row]) {
1133       av1_zero_array(cm->above_context[1][tile_row] + offset_uv, width_uv);
1134       av1_zero_array(cm->above_context[2][tile_row] + offset_uv, width_uv);
1135     } else {
1136       aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
1137                          "Invalid value of planes");
1138     }
1139   }
1140 
1141   av1_zero_array(cm->above_seg_context[tile_row] + mi_col_start, aligned_width);
1142 
1143   memset(cm->above_txfm_context[tile_row] + mi_col_start,
1144     tx_size_wide[TX_SIZES_LARGEST],
1145     aligned_width * sizeof(TXFM_CONTEXT));
1146 }
1147 
av1_zero_left_context(MACROBLOCKD * const xd)1148 static INLINE void av1_zero_left_context(MACROBLOCKD *const xd) {
1149   av1_zero(xd->left_context);
1150   av1_zero(xd->left_seg_context);
1151 
1152   memset(xd->left_txfm_context_buffer, tx_size_high[TX_SIZES_LARGEST],
1153          sizeof(xd->left_txfm_context_buffer));
1154 }
1155 
1156 // Disable array-bounds checks as the TX_SIZE enum contains values larger than
1157 // TX_SIZES_ALL (TX_INVALID) which make extending the array as a workaround
1158 // infeasible. The assert is enough for static analysis and this or other tools
1159 // asan, valgrind would catch oob access at runtime.
1160 #if defined(__GNUC__) && __GNUC__ >= 4
1161 #pragma GCC diagnostic ignored "-Warray-bounds"
1162 #endif
1163 
1164 #if defined(__GNUC__) && __GNUC__ >= 4
1165 #pragma GCC diagnostic warning "-Warray-bounds"
1166 #endif
1167 
set_txfm_ctx(TXFM_CONTEXT * txfm_ctx,uint8_t txs,int len)1168 static INLINE void set_txfm_ctx(TXFM_CONTEXT *txfm_ctx, uint8_t txs, int len) {
1169   int i;
1170   for (i = 0; i < len; ++i) txfm_ctx[i] = txs;
1171 }
1172 
set_txfm_ctxs(TX_SIZE tx_size,int n4_w,int n4_h,int skip,const MACROBLOCKD * xd)1173 static INLINE void set_txfm_ctxs(TX_SIZE tx_size, int n4_w, int n4_h, int skip,
1174                                  const MACROBLOCKD *xd) {
1175   uint8_t bw = tx_size_wide[tx_size];
1176   uint8_t bh = tx_size_high[tx_size];
1177 
1178   if (skip) {
1179     bw = n4_w * MI_SIZE;
1180     bh = n4_h * MI_SIZE;
1181   }
1182 
1183   set_txfm_ctx(xd->above_txfm_context, bw, n4_w);
1184   set_txfm_ctx(xd->left_txfm_context, bh, n4_h);
1185 }
1186 
txfm_partition_update(TXFM_CONTEXT * above_ctx,TXFM_CONTEXT * left_ctx,TX_SIZE tx_size,TX_SIZE txb_size)1187 static INLINE void txfm_partition_update(TXFM_CONTEXT *above_ctx,
1188                                          TXFM_CONTEXT *left_ctx,
1189                                          TX_SIZE tx_size, TX_SIZE txb_size) {
1190   BLOCK_SIZE bsize = txsize_to_bsize[txb_size];
1191   int bh = mi_size_high[bsize];
1192   int bw = mi_size_wide[bsize];
1193   uint8_t txw = tx_size_wide[tx_size];
1194   uint8_t txh = tx_size_high[tx_size];
1195   int i;
1196   for (i = 0; i < bh; ++i) left_ctx[i] = txh;
1197   for (i = 0; i < bw; ++i) above_ctx[i] = txw;
1198 }
1199 
get_sqr_tx_size(int tx_dim)1200 static INLINE TX_SIZE get_sqr_tx_size(int tx_dim) {
1201   switch (tx_dim) {
1202     case 128:
1203     case 64: return TX_64X64; break;
1204     case 32: return TX_32X32; break;
1205     case 16: return TX_16X16; break;
1206     case 8: return TX_8X8; break;
1207     default: return TX_4X4;
1208   }
1209 }
1210 
get_tx_size(int width,int height)1211 static INLINE TX_SIZE get_tx_size(int width, int height) {
1212   if (width == height) {
1213     return get_sqr_tx_size(width);
1214   }
1215   if (width < height) {
1216     if (width + width == height) {
1217       switch (width) {
1218         case 4: return TX_4X8; break;
1219         case 8: return TX_8X16; break;
1220         case 16: return TX_16X32; break;
1221         case 32: return TX_32X64; break;
1222       }
1223     } else {
1224       switch (width) {
1225         case 4: return TX_4X16; break;
1226         case 8: return TX_8X32; break;
1227         case 16: return TX_16X64; break;
1228       }
1229     }
1230   } else {
1231     if (height + height == width) {
1232       switch (height) {
1233         case 4: return TX_8X4; break;
1234         case 8: return TX_16X8; break;
1235         case 16: return TX_32X16; break;
1236         case 32: return TX_64X32; break;
1237       }
1238     } else {
1239       switch (height) {
1240         case 4: return TX_16X4; break;
1241         case 8: return TX_32X8; break;
1242         case 16: return TX_64X16; break;
1243       }
1244     }
1245   }
1246   assert(0);
1247   return TX_4X4;
1248 }
1249 
txfm_partition_context(const TXFM_CONTEXT * const above_ctx,const TXFM_CONTEXT * const left_ctx,BLOCK_SIZE bsize,TX_SIZE tx_size)1250 static INLINE int txfm_partition_context(const TXFM_CONTEXT *const above_ctx,
1251                                          const TXFM_CONTEXT *const left_ctx,
1252                                          BLOCK_SIZE bsize, TX_SIZE tx_size) {
1253   const uint8_t txw = tx_size_wide[tx_size];
1254   const uint8_t txh = tx_size_high[tx_size];
1255   const int above = *above_ctx < txw;
1256   const int left = *left_ctx < txh;
1257   int category = TXFM_PARTITION_CONTEXTS;
1258 
1259   // dummy return, not used by others.
1260   if (tx_size <= TX_4X4) return 0;
1261 
1262   TX_SIZE max_tx_size =
1263       get_sqr_tx_size(AOMMAX(block_size_wide[bsize], block_size_high[bsize]));
1264 
1265   if (max_tx_size >= TX_8X8) {
1266     category =
1267         (txsize_sqr_up_map[tx_size] != max_tx_size && max_tx_size > TX_8X8) +
1268         (TX_SIZES - 1 - max_tx_size) * 2;
1269   }
1270   assert(category != TXFM_PARTITION_CONTEXTS);
1271   return category * 3 + above + left;
1272 }
1273 
1274 // Compute the next partition in the direction of the sb_type stored in the mi
1275 // array, starting with bsize.
get_partition(const AV1_COMMON * const cm,int mi_row,int mi_col,BLOCK_SIZE bsize)1276 static INLINE PARTITION_TYPE get_partition(const AV1_COMMON *const cm,
1277                                            int mi_row, int mi_col,
1278                                            BLOCK_SIZE bsize) {
1279   if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return PARTITION_INVALID;
1280 
1281   const int offset = mi_row * cm->mi_stride + mi_col;
1282   MB_MODE_INFO **mi = cm->mi_grid_visible + offset;
1283   const BLOCK_SIZE subsize = mi[0]->sb_type;
1284 
1285   if (subsize == bsize) return PARTITION_NONE;
1286 
1287   const int bhigh = mi_size_high[bsize];
1288   const int bwide = mi_size_wide[bsize];
1289   const int sshigh = mi_size_high[subsize];
1290   const int sswide = mi_size_wide[subsize];
1291 
1292   if (bsize > BLOCK_8X8 && mi_row + bwide / 2 < cm->mi_rows &&
1293       mi_col + bhigh / 2 < cm->mi_cols) {
1294     // In this case, the block might be using an extended partition
1295     // type.
1296     const MB_MODE_INFO *const mbmi_right = mi[bwide / 2];
1297     const MB_MODE_INFO *const mbmi_below = mi[bhigh / 2 * cm->mi_stride];
1298 
1299     if (sswide == bwide) {
1300       // Smaller height but same width. Is PARTITION_HORZ_4, PARTITION_HORZ or
1301       // PARTITION_HORZ_B. To distinguish the latter two, check if the lower
1302       // half was split.
1303       if (sshigh * 4 == bhigh) return PARTITION_HORZ_4;
1304       assert(sshigh * 2 == bhigh);
1305 
1306       if (mbmi_below->sb_type == subsize)
1307         return PARTITION_HORZ;
1308       else
1309         return PARTITION_HORZ_B;
1310     } else if (sshigh == bhigh) {
1311       // Smaller width but same height. Is PARTITION_VERT_4, PARTITION_VERT or
1312       // PARTITION_VERT_B. To distinguish the latter two, check if the right
1313       // half was split.
1314       if (sswide * 4 == bwide) return PARTITION_VERT_4;
1315       assert(sswide * 2 == bhigh);
1316 
1317       if (mbmi_right->sb_type == subsize)
1318         return PARTITION_VERT;
1319       else
1320         return PARTITION_VERT_B;
1321     } else {
1322       // Smaller width and smaller height. Might be PARTITION_SPLIT or could be
1323       // PARTITION_HORZ_A or PARTITION_VERT_A. If subsize isn't halved in both
1324       // dimensions, we immediately know this is a split (which will recurse to
1325       // get to subsize). Otherwise look down and to the right. With
1326       // PARTITION_VERT_A, the right block will have height bhigh; with
1327       // PARTITION_HORZ_A, the lower block with have width bwide. Otherwise
1328       // it's PARTITION_SPLIT.
1329       if (sswide * 2 != bwide || sshigh * 2 != bhigh) return PARTITION_SPLIT;
1330 
1331       if (mi_size_wide[mbmi_below->sb_type] == bwide) return PARTITION_HORZ_A;
1332       if (mi_size_high[mbmi_right->sb_type] == bhigh) return PARTITION_VERT_A;
1333 
1334       return PARTITION_SPLIT;
1335     }
1336   }
1337   const int vert_split = sswide < bwide;
1338   const int horz_split = sshigh < bhigh;
1339   const int split_idx = (vert_split << 1) | horz_split;
1340   assert(split_idx != 0);
1341 
1342   static const PARTITION_TYPE base_partitions[4] = {
1343     PARTITION_INVALID, PARTITION_HORZ, PARTITION_VERT, PARTITION_SPLIT
1344   };
1345 
1346   return base_partitions[split_idx];
1347 }
1348 
set_sb_size(SequenceHeader * const seq_params,BLOCK_SIZE sb_size)1349 static INLINE void set_sb_size(SequenceHeader *const seq_params,
1350                                BLOCK_SIZE sb_size) {
1351   seq_params->sb_size = sb_size;
1352   seq_params->mib_size = mi_size_wide[seq_params->sb_size];
1353   seq_params->mib_size_log2 = mi_size_wide_log2[seq_params->sb_size];
1354 }
1355 
1356 // Returns true if the frame is fully lossless at the coded resolution.
1357 // Note: If super-resolution is used, such a frame will still NOT be lossless at
1358 // the upscaled resolution.
is_coded_lossless(const AV1_COMMON * cm,const MACROBLOCKD * xd)1359 static INLINE int is_coded_lossless(const AV1_COMMON *cm,
1360                                     const MACROBLOCKD *xd) {
1361   int coded_lossless = 1;
1362   if (cm->seg.enabled) {
1363     for (int i = 0; i < MAX_SEGMENTS; ++i) {
1364       if (!xd->lossless[i]) {
1365         coded_lossless = 0;
1366         break;
1367       }
1368     }
1369   } else {
1370     coded_lossless = xd->lossless[0];
1371   }
1372   return coded_lossless;
1373 }
1374 
is_valid_seq_level_idx(AV1_LEVEL seq_level_idx)1375 static INLINE int is_valid_seq_level_idx(AV1_LEVEL seq_level_idx) {
1376   return seq_level_idx < SEQ_LEVELS || seq_level_idx == SEQ_LEVEL_MAX;
1377 }
1378 
1379 #ifdef __cplusplus
1380 }  // extern "C"
1381 #endif
1382 
1383 #endif  // AOM_AV1_COMMON_ONYXC_INT_H_
1384