Home | History | Annotate | Download | only in common
      1 /*
      2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #ifndef VP9_COMMON_VP9_ONYXC_INT_H_
     12 #define VP9_COMMON_VP9_ONYXC_INT_H_
     13 
     14 #include "./vpx_config.h"
     15 #include "vpx/internal/vpx_codec_internal.h"
     16 #include "./vp9_rtcd.h"
     17 #include "vp9/common/vp9_loopfilter.h"
     18 #include "vp9/common/vp9_entropymv.h"
     19 #include "vp9/common/vp9_entropy.h"
     20 #include "vp9/common/vp9_entropymode.h"
     21 #include "vp9/common/vp9_quant_common.h"
     22 #include "vp9/common/vp9_tile_common.h"
     23 
     24 #if CONFIG_VP9_POSTPROC
     25 #include "vp9/common/vp9_postproc.h"
     26 #endif
     27 
     28 #define ALLOWED_REFS_PER_FRAME 3
     29 
     30 #define NUM_REF_FRAMES_LOG2 3
     31 #define NUM_REF_FRAMES (1 << NUM_REF_FRAMES_LOG2)
     32 
     33 // 1 scratch frame for the new frame, 3 for scaled references on the encoder
     34 // TODO(jkoleszar): These 3 extra references could probably come from the
     35 // normal reference pool.
     36 #define NUM_YV12_BUFFERS (NUM_REF_FRAMES + 4)
     37 
     38 #define NUM_FRAME_CONTEXTS_LOG2 2
     39 #define NUM_FRAME_CONTEXTS (1 << NUM_FRAME_CONTEXTS_LOG2)
     40 
     41 typedef struct frame_contexts {
     42   vp9_prob y_mode_prob[BLOCK_SIZE_GROUPS][INTRA_MODES - 1];
     43   vp9_prob uv_mode_prob[INTRA_MODES][INTRA_MODES - 1];
     44   vp9_prob partition_prob[PARTITION_CONTEXTS][PARTITION_TYPES - 1];
     45   vp9_coeff_probs_model coef_probs[TX_SIZES][BLOCK_TYPES];
     46   vp9_prob switchable_interp_prob[SWITCHABLE_FILTER_CONTEXTS]
     47                                  [SWITCHABLE_FILTERS - 1];
     48   vp9_prob inter_mode_probs[INTER_MODE_CONTEXTS][INTER_MODES - 1];
     49   vp9_prob intra_inter_prob[INTRA_INTER_CONTEXTS];
     50   vp9_prob comp_inter_prob[COMP_INTER_CONTEXTS];
     51   vp9_prob single_ref_prob[REF_CONTEXTS][2];
     52   vp9_prob comp_ref_prob[REF_CONTEXTS];
     53   struct tx_probs tx_probs;
     54   vp9_prob mbskip_probs[MBSKIP_CONTEXTS];
     55   nmv_context nmvc;
     56 } FRAME_CONTEXT;
     57 
     58 typedef struct {
     59   unsigned int y_mode[BLOCK_SIZE_GROUPS][INTRA_MODES];
     60   unsigned int uv_mode[INTRA_MODES][INTRA_MODES];
     61   unsigned int partition[PARTITION_CONTEXTS][PARTITION_TYPES];
     62   vp9_coeff_count_model coef[TX_SIZES][BLOCK_TYPES];
     63   unsigned int eob_branch[TX_SIZES][BLOCK_TYPES][REF_TYPES]
     64                          [COEF_BANDS][PREV_COEF_CONTEXTS];
     65   unsigned int switchable_interp[SWITCHABLE_FILTER_CONTEXTS]
     66                                 [SWITCHABLE_FILTERS];
     67   unsigned int inter_mode[INTER_MODE_CONTEXTS][INTER_MODES];
     68   unsigned int intra_inter[INTRA_INTER_CONTEXTS][2];
     69   unsigned int comp_inter[COMP_INTER_CONTEXTS][2];
     70   unsigned int single_ref[REF_CONTEXTS][2][2];
     71   unsigned int comp_ref[REF_CONTEXTS][2];
     72   struct tx_counts tx;
     73   unsigned int mbskip[MBSKIP_CONTEXTS][2];
     74   nmv_context_counts mv;
     75 } FRAME_COUNTS;
     76 
     77 
     78 typedef enum {
     79   SINGLE_PREDICTION_ONLY = 0,
     80   COMP_PREDICTION_ONLY   = 1,
     81   HYBRID_PREDICTION      = 2,
     82   NB_PREDICTION_TYPES    = 3,
     83 } COMPPREDMODE_TYPE;
     84 
     85 typedef struct VP9Common {
     86   struct vpx_internal_error_info  error;
     87 
     88   DECLARE_ALIGNED(16, int16_t, y_dequant[QINDEX_RANGE][8]);
     89   DECLARE_ALIGNED(16, int16_t, uv_dequant[QINDEX_RANGE][8]);
     90 #if CONFIG_ALPHA
     91   DECLARE_ALIGNED(16, int16_t, a_dequant[QINDEX_RANGE][8]);
     92 #endif
     93 
     94   COLOR_SPACE color_space;
     95 
     96   int width;
     97   int height;
     98   int display_width;
     99   int display_height;
    100   int last_width;
    101   int last_height;
    102 
    103   // TODO(jkoleszar): this implies chroma ss right now, but could vary per
    104   // plane. Revisit as part of the future change to YV12_BUFFER_CONFIG to
    105   // support additional planes.
    106   int subsampling_x;
    107   int subsampling_y;
    108 
    109   YV12_BUFFER_CONFIG *frame_to_show;
    110 
    111   YV12_BUFFER_CONFIG yv12_fb[NUM_YV12_BUFFERS];
    112   int fb_idx_ref_cnt[NUM_YV12_BUFFERS]; /* reference counts */
    113   int ref_frame_map[NUM_REF_FRAMES]; /* maps fb_idx to reference slot */
    114 
    115   // TODO(jkoleszar): could expand active_ref_idx to 4, with 0 as intra, and
    116   // roll new_fb_idx into it.
    117 
    118   // Each frame can reference ALLOWED_REFS_PER_FRAME buffers
    119   int active_ref_idx[ALLOWED_REFS_PER_FRAME];
    120   struct scale_factors active_ref_scale[ALLOWED_REFS_PER_FRAME];
    121   struct scale_factors_common active_ref_scale_comm[ALLOWED_REFS_PER_FRAME];
    122   int new_fb_idx;
    123 
    124   YV12_BUFFER_CONFIG post_proc_buffer;
    125 
    126   FRAME_TYPE last_frame_type;  /* last frame's frame type for motion search.*/
    127   FRAME_TYPE frame_type;
    128 
    129   int show_frame;
    130   int last_show_frame;
    131 
    132   // Flag signaling that the frame is encoded using only INTRA modes.
    133   int intra_only;
    134 
    135   int allow_high_precision_mv;
    136 
    137   // Flag signaling that the frame context should be reset to default values.
    138   // 0 or 1 implies don't reset, 2 reset just the context specified in the
    139   // frame header, 3 reset all contexts.
    140   int reset_frame_context;
    141 
    142   int frame_flags;
    143   // MBs, mb_rows/cols is in 16-pixel units; mi_rows/cols is in
    144   // MODE_INFO (8-pixel) units.
    145   int MBs;
    146   int mb_rows, mi_rows;
    147   int mb_cols, mi_cols;
    148   int mode_info_stride;
    149 
    150   /* profile settings */
    151   TX_MODE tx_mode;
    152 
    153   int base_qindex;
    154   int y_dc_delta_q;
    155   int uv_dc_delta_q;
    156   int uv_ac_delta_q;
    157 #if CONFIG_ALPHA
    158   int a_dc_delta_q;
    159   int a_ac_delta_q;
    160 #endif
    161 
    162   /* We allocate a MODE_INFO struct for each macroblock, together with
    163      an extra row on top and column on the left to simplify prediction. */
    164 
    165   MODE_INFO *mip; /* Base of allocated array */
    166   MODE_INFO *mi;  /* Corresponds to upper left visible macroblock */
    167   MODE_INFO *prev_mip; /* MODE_INFO array 'mip' from last decoded frame */
    168   MODE_INFO *prev_mi;  /* 'mi' from last frame (points into prev_mip) */
    169 
    170   MODE_INFO **mi_grid_base;
    171   MODE_INFO **mi_grid_visible;
    172   MODE_INFO **prev_mi_grid_base;
    173   MODE_INFO **prev_mi_grid_visible;
    174 
    175   // Persistent mb segment id map used in prediction.
    176   unsigned char *last_frame_seg_map;
    177 
    178   INTERPOLATION_TYPE mcomp_filter_type;
    179 
    180   loop_filter_info_n lf_info;
    181 
    182   int refresh_frame_context;    /* Two state 0 = NO, 1 = YES */
    183 
    184   int ref_frame_sign_bias[MAX_REF_FRAMES];    /* Two state 0, 1 */
    185 
    186   struct loopfilter lf;
    187   struct segmentation seg;
    188 
    189   // Context probabilities for reference frame prediction
    190   int allow_comp_inter_inter;
    191   MV_REFERENCE_FRAME comp_fixed_ref;
    192   MV_REFERENCE_FRAME comp_var_ref[2];
    193   COMPPREDMODE_TYPE comp_pred_mode;
    194 
    195   FRAME_CONTEXT fc;  /* this frame entropy */
    196   FRAME_CONTEXT frame_contexts[NUM_FRAME_CONTEXTS];
    197   unsigned int  frame_context_idx; /* Context to use/update */
    198   FRAME_COUNTS counts;
    199 
    200   unsigned int current_video_frame;
    201   int version;
    202 
    203 #if CONFIG_VP9_POSTPROC
    204   struct postproc_state  postproc_state;
    205 #endif
    206 
    207   int error_resilient_mode;
    208   int frame_parallel_decoding_mode;
    209 
    210   int log2_tile_cols, log2_tile_rows;
    211 } VP9_COMMON;
    212 
    213 // ref == 0 => LAST_FRAME
    214 // ref == 1 => GOLDEN_FRAME
    215 // ref == 2 => ALTREF_FRAME
    216 static YV12_BUFFER_CONFIG *get_frame_ref_buffer(VP9_COMMON *cm, int ref) {
    217   return &cm->yv12_fb[cm->active_ref_idx[ref]];
    218 }
    219 
    220 static YV12_BUFFER_CONFIG *get_frame_new_buffer(VP9_COMMON *cm) {
    221   return &cm->yv12_fb[cm->new_fb_idx];
    222 }
    223 
    224 static int get_free_fb(VP9_COMMON *cm) {
    225   int i;
    226   for (i = 0; i < NUM_YV12_BUFFERS; i++)
    227     if (cm->fb_idx_ref_cnt[i] == 0)
    228       break;
    229 
    230   assert(i < NUM_YV12_BUFFERS);
    231   cm->fb_idx_ref_cnt[i] = 1;
    232   return i;
    233 }
    234 
    235 static void ref_cnt_fb(int *buf, int *idx, int new_idx) {
    236   if (buf[*idx] > 0)
    237     buf[*idx]--;
    238 
    239   *idx = new_idx;
    240 
    241   buf[new_idx]++;
    242 }
    243 
    244 static int mi_cols_aligned_to_sb(int n_mis) {
    245   return ALIGN_POWER_OF_TWO(n_mis, MI_BLOCK_SIZE_LOG2);
    246 }
    247 
    248 static INLINE const vp9_prob* get_partition_probs(VP9_COMMON *cm, int ctx) {
    249   return cm->frame_type == KEY_FRAME ? vp9_kf_partition_probs[ctx]
    250                                      : cm->fc.partition_prob[ctx];
    251 }
    252 
    253 static INLINE void set_skip_context(
    254     MACROBLOCKD *xd,
    255     ENTROPY_CONTEXT *above_context[MAX_MB_PLANE],
    256     ENTROPY_CONTEXT left_context[MAX_MB_PLANE][16],
    257     int mi_row, int mi_col) {
    258   const int above_idx = mi_col * 2;
    259   const int left_idx = (mi_row * 2) & 15;
    260   int i;
    261   for (i = 0; i < MAX_MB_PLANE; i++) {
    262     struct macroblockd_plane *const pd = &xd->plane[i];
    263     pd->above_context = above_context[i] + (above_idx >> pd->subsampling_x);
    264     pd->left_context = left_context[i] + (left_idx >> pd->subsampling_y);
    265   }
    266 }
    267 
    268 static void set_mi_row_col(MACROBLOCKD *xd, const TileInfo *const tile,
    269                            int mi_row, int bh,
    270                            int mi_col, int bw,
    271                            int mi_rows, int mi_cols) {
    272   xd->mb_to_top_edge    = -((mi_row * MI_SIZE) * 8);
    273   xd->mb_to_bottom_edge = ((mi_rows - bh - mi_row) * MI_SIZE) * 8;
    274   xd->mb_to_left_edge   = -((mi_col * MI_SIZE) * 8);
    275   xd->mb_to_right_edge  = ((mi_cols - bw - mi_col) * MI_SIZE) * 8;
    276 
    277   // Are edges available for intra prediction?
    278   xd->up_available    = (mi_row != 0);
    279   xd->left_available  = (mi_col > tile->mi_col_start);
    280 }
    281 
    282 static void set_prev_mi(VP9_COMMON *cm) {
    283   const int use_prev_in_find_mv_refs = cm->width == cm->last_width &&
    284                                        cm->height == cm->last_height &&
    285                                        !cm->error_resilient_mode &&
    286                                        !cm->intra_only &&
    287                                        cm->last_show_frame;
    288   // Special case: set prev_mi to NULL when the previous mode info
    289   // context cannot be used.
    290   cm->prev_mi = use_prev_in_find_mv_refs ?
    291                   cm->prev_mip + cm->mode_info_stride + 1 : NULL;
    292 }
    293 
    294 static INLINE int frame_is_intra_only(const VP9_COMMON *const cm) {
    295   return cm->frame_type == KEY_FRAME || cm->intra_only;
    296 }
    297 
    298 static INLINE void update_partition_context(
    299     PARTITION_CONTEXT *above_seg_context,
    300     PARTITION_CONTEXT left_seg_context[8],
    301     int mi_row, int mi_col,
    302     BLOCK_SIZE sb_type,
    303     BLOCK_SIZE sb_size) {
    304   PARTITION_CONTEXT *above_ctx = above_seg_context + mi_col;
    305   PARTITION_CONTEXT *left_ctx = left_seg_context + (mi_row & MI_MASK);
    306 
    307   const int bsl = b_width_log2(sb_size), bs = (1 << bsl) / 2;
    308   const int bwl = b_width_log2(sb_type);
    309   const int bhl = b_height_log2(sb_type);
    310   const int boffset = b_width_log2(BLOCK_64X64) - bsl;
    311   const char pcval0 = ~(0xe << boffset);
    312   const char pcval1 = ~(0xf << boffset);
    313   const char pcvalue[2] = {pcval0, pcval1};
    314 
    315   assert(MAX(bwl, bhl) <= bsl);
    316 
    317   // update the partition context at the end notes. set partition bits
    318   // of block sizes larger than the current one to be one, and partition
    319   // bits of smaller block sizes to be zero.
    320   vpx_memset(above_ctx, pcvalue[bwl == bsl], bs);
    321   vpx_memset(left_ctx, pcvalue[bhl == bsl], bs);
    322 }
    323 
    324 static INLINE int partition_plane_context(
    325     const PARTITION_CONTEXT *above_seg_context,
    326     const PARTITION_CONTEXT left_seg_context[8],
    327     int mi_row, int mi_col,
    328     BLOCK_SIZE sb_type) {
    329   const PARTITION_CONTEXT *above_ctx = above_seg_context + mi_col;
    330   const PARTITION_CONTEXT *left_ctx = left_seg_context + (mi_row & MI_MASK);
    331 
    332   int bsl = mi_width_log2(sb_type), bs = 1 << bsl;
    333   int above = 0, left = 0, i;
    334   int boffset = mi_width_log2(BLOCK_64X64) - bsl;
    335 
    336   assert(mi_width_log2(sb_type) == mi_height_log2(sb_type));
    337   assert(bsl >= 0);
    338   assert(boffset >= 0);
    339 
    340   for (i = 0; i < bs; i++)
    341     above |= (above_ctx[i] & (1 << boffset));
    342   for (i = 0; i < bs; i++)
    343     left |= (left_ctx[i] & (1 << boffset));
    344 
    345   above = (above > 0);
    346   left  = (left > 0);
    347 
    348   return (left * 2 + above) + bsl * PARTITION_PLOFFSET;
    349 }
    350 
    351 #endif  // VP9_COMMON_VP9_ONYXC_INT_H_
    352