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 "vpx_util/vpx_thread.h"
     17 #include "./vp9_rtcd.h"
     18 #include "vp9/common/vp9_alloccommon.h"
     19 #include "vp9/common/vp9_loopfilter.h"
     20 #include "vp9/common/vp9_entropymv.h"
     21 #include "vp9/common/vp9_entropy.h"
     22 #include "vp9/common/vp9_entropymode.h"
     23 #include "vp9/common/vp9_frame_buffers.h"
     24 #include "vp9/common/vp9_quant_common.h"
     25 #include "vp9/common/vp9_tile_common.h"
     26 
     27 #if CONFIG_VP9_POSTPROC
     28 #include "vp9/common/vp9_postproc.h"
     29 #endif
     30 
     31 #ifdef __cplusplus
     32 extern "C" {
     33 #endif
     34 
     35 #define REFS_PER_FRAME 3
     36 
     37 #define REF_FRAMES_LOG2 3
     38 #define REF_FRAMES (1 << REF_FRAMES_LOG2)
     39 
     40 // 4 scratch frames for the new frames to support a maximum of 4 cores decoding
     41 // in parallel, 3 for scaled references on the encoder.
     42 // TODO(hkuang): Add ondemand frame buffers instead of hardcoding the number
     43 // of framebuffers.
     44 // TODO(jkoleszar): These 3 extra references could probably come from the
     45 // normal reference pool.
     46 #define FRAME_BUFFERS (REF_FRAMES + 7)
     47 
     48 #define FRAME_CONTEXTS_LOG2 2
     49 #define FRAME_CONTEXTS (1 << FRAME_CONTEXTS_LOG2)
     50 
     51 #define NUM_PING_PONG_BUFFERS 2
     52 
     53 extern const struct {
     54   PARTITION_CONTEXT above;
     55   PARTITION_CONTEXT left;
     56 } partition_context_lookup[BLOCK_SIZES];
     57 
     58 
     59 typedef enum {
     60   SINGLE_REFERENCE      = 0,
     61   COMPOUND_REFERENCE    = 1,
     62   REFERENCE_MODE_SELECT = 2,
     63   REFERENCE_MODES       = 3,
     64 } REFERENCE_MODE;
     65 
     66 typedef struct {
     67   int_mv mv[2];
     68   MV_REFERENCE_FRAME ref_frame[2];
     69 } MV_REF;
     70 
     71 typedef struct {
     72   int ref_count;
     73   MV_REF *mvs;
     74   int mi_rows;
     75   int mi_cols;
     76   vpx_codec_frame_buffer_t raw_frame_buffer;
     77   YV12_BUFFER_CONFIG buf;
     78 
     79   // The Following variables will only be used in frame parallel decode.
     80 
     81   // frame_worker_owner indicates which FrameWorker owns this buffer. NULL means
     82   // that no FrameWorker owns, or is decoding, this buffer.
     83   VPxWorker *frame_worker_owner;
     84 
     85   // row and col indicate which position frame has been decoded to in real
     86   // pixel unit. They are reset to -1 when decoding begins and set to INT_MAX
     87   // when the frame is fully decoded.
     88   int row;
     89   int col;
     90 } RefCntBuffer;
     91 
     92 typedef struct BufferPool {
     93   // Protect BufferPool from being accessed by several FrameWorkers at
     94   // the same time during frame parallel decode.
     95   // TODO(hkuang): Try to use atomic variable instead of locking the whole pool.
     96 #if CONFIG_MULTITHREAD
     97   pthread_mutex_t pool_mutex;
     98 #endif
     99 
    100   // Private data associated with the frame buffer callbacks.
    101   void *cb_priv;
    102 
    103   vpx_get_frame_buffer_cb_fn_t get_fb_cb;
    104   vpx_release_frame_buffer_cb_fn_t release_fb_cb;
    105 
    106   RefCntBuffer frame_bufs[FRAME_BUFFERS];
    107 
    108   // Frame buffers allocated internally by the codec.
    109   InternalFrameBufferList int_frame_buffers;
    110 } BufferPool;
    111 
    112 typedef struct VP9Common {
    113   struct vpx_internal_error_info  error;
    114   vpx_color_space_t color_space;
    115   vpx_color_range_t color_range;
    116   int width;
    117   int height;
    118   int render_width;
    119   int render_height;
    120   int last_width;
    121   int last_height;
    122 
    123   // TODO(jkoleszar): this implies chroma ss right now, but could vary per
    124   // plane. Revisit as part of the future change to YV12_BUFFER_CONFIG to
    125   // support additional planes.
    126   int subsampling_x;
    127   int subsampling_y;
    128 
    129 #if CONFIG_VP9_HIGHBITDEPTH
    130   int use_highbitdepth;  // Marks if we need to use 16bit frame buffers.
    131 #endif
    132 
    133   YV12_BUFFER_CONFIG *frame_to_show;
    134   RefCntBuffer *prev_frame;
    135 
    136   // TODO(hkuang): Combine this with cur_buf in macroblockd.
    137   RefCntBuffer *cur_frame;
    138 
    139   int ref_frame_map[REF_FRAMES]; /* maps fb_idx to reference slot */
    140 
    141   // Prepare ref_frame_map for the next frame.
    142   // Only used in frame parallel decode.
    143   int next_ref_frame_map[REF_FRAMES];
    144 
    145   // TODO(jkoleszar): could expand active_ref_idx to 4, with 0 as intra, and
    146   // roll new_fb_idx into it.
    147 
    148   // Each frame can reference REFS_PER_FRAME buffers
    149   RefBuffer frame_refs[REFS_PER_FRAME];
    150 
    151   int new_fb_idx;
    152 
    153 #if CONFIG_VP9_POSTPROC
    154   YV12_BUFFER_CONFIG post_proc_buffer;
    155   YV12_BUFFER_CONFIG post_proc_buffer_int;
    156 #endif
    157 
    158   FRAME_TYPE last_frame_type;  /* last frame's frame type for motion search.*/
    159   FRAME_TYPE frame_type;
    160 
    161   int show_frame;
    162   int last_show_frame;
    163   int show_existing_frame;
    164 
    165   // Flag signaling that the frame is encoded using only INTRA modes.
    166   uint8_t intra_only;
    167   uint8_t last_intra_only;
    168 
    169   int allow_high_precision_mv;
    170 
    171   // Flag signaling that the frame context should be reset to default values.
    172   // 0 or 1 implies don't reset, 2 reset just the context specified in the
    173   // frame header, 3 reset all contexts.
    174   int reset_frame_context;
    175 
    176   // MBs, mb_rows/cols is in 16-pixel units; mi_rows/cols is in
    177   // MODE_INFO (8-pixel) units.
    178   int MBs;
    179   int mb_rows, mi_rows;
    180   int mb_cols, mi_cols;
    181   int mi_stride;
    182 
    183   /* profile settings */
    184   TX_MODE tx_mode;
    185 
    186   int base_qindex;
    187   int y_dc_delta_q;
    188   int uv_dc_delta_q;
    189   int uv_ac_delta_q;
    190   int16_t y_dequant[MAX_SEGMENTS][2];
    191   int16_t uv_dequant[MAX_SEGMENTS][2];
    192 
    193   /* We allocate a MODE_INFO struct for each macroblock, together with
    194      an extra row on top and column on the left to simplify prediction. */
    195   int mi_alloc_size;
    196   MODE_INFO *mip; /* Base of allocated array */
    197   MODE_INFO *mi;  /* Corresponds to upper left visible macroblock */
    198 
    199   // TODO(agrange): Move prev_mi into encoder structure.
    200   // prev_mip and prev_mi will only be allocated in VP9 encoder.
    201   MODE_INFO *prev_mip; /* MODE_INFO array 'mip' from last decoded frame */
    202   MODE_INFO *prev_mi;  /* 'mi' from last frame (points into prev_mip) */
    203 
    204   // Separate mi functions between encoder and decoder.
    205   int (*alloc_mi)(struct VP9Common *cm, int mi_size);
    206   void (*free_mi)(struct VP9Common *cm);
    207   void (*setup_mi)(struct VP9Common *cm);
    208 
    209   // Grid of pointers to 8x8 MODE_INFO structs.  Any 8x8 not in the visible
    210   // area will be NULL.
    211   MODE_INFO **mi_grid_base;
    212   MODE_INFO **mi_grid_visible;
    213   MODE_INFO **prev_mi_grid_base;
    214   MODE_INFO **prev_mi_grid_visible;
    215 
    216   // Whether to use previous frame's motion vectors for prediction.
    217   int use_prev_frame_mvs;
    218 
    219   // Persistent mb segment id map used in prediction.
    220   int seg_map_idx;
    221   int prev_seg_map_idx;
    222 
    223   uint8_t *seg_map_array[NUM_PING_PONG_BUFFERS];
    224   uint8_t *last_frame_seg_map;
    225   uint8_t *current_frame_seg_map;
    226   int seg_map_alloc_size;
    227 
    228   INTERP_FILTER interp_filter;
    229 
    230   loop_filter_info_n lf_info;
    231 
    232   int refresh_frame_context;    /* Two state 0 = NO, 1 = YES */
    233 
    234   int ref_frame_sign_bias[MAX_REF_FRAMES];    /* Two state 0, 1 */
    235 
    236   struct loopfilter lf;
    237   struct segmentation seg;
    238 
    239   // TODO(hkuang): Remove this as it is the same as frame_parallel_decode
    240   // in pbi.
    241   int frame_parallel_decode;  // frame-based threading.
    242 
    243   // Context probabilities for reference frame prediction
    244   MV_REFERENCE_FRAME comp_fixed_ref;
    245   MV_REFERENCE_FRAME comp_var_ref[2];
    246   REFERENCE_MODE reference_mode;
    247 
    248   FRAME_CONTEXT *fc;  /* this frame entropy */
    249   FRAME_CONTEXT *frame_contexts;   // FRAME_CONTEXTS
    250   unsigned int  frame_context_idx; /* Context to use/update */
    251   FRAME_COUNTS counts;
    252 
    253   unsigned int current_video_frame;
    254   BITSTREAM_PROFILE profile;
    255 
    256   // VPX_BITS_8 in profile 0 or 1, VPX_BITS_10 or VPX_BITS_12 in profile 2 or 3.
    257   vpx_bit_depth_t bit_depth;
    258   vpx_bit_depth_t dequant_bit_depth;  // bit_depth of current dequantizer
    259 
    260 #if CONFIG_VP9_POSTPROC
    261   struct postproc_state  postproc_state;
    262 #endif
    263 
    264   int error_resilient_mode;
    265   int frame_parallel_decoding_mode;
    266 
    267   int log2_tile_cols, log2_tile_rows;
    268   int byte_alignment;
    269   int skip_loop_filter;
    270 
    271   // Private data associated with the frame buffer callbacks.
    272   void *cb_priv;
    273   vpx_get_frame_buffer_cb_fn_t get_fb_cb;
    274   vpx_release_frame_buffer_cb_fn_t release_fb_cb;
    275 
    276   // Handles memory for the codec.
    277   InternalFrameBufferList int_frame_buffers;
    278 
    279   // External BufferPool passed from outside.
    280   BufferPool *buffer_pool;
    281 
    282   PARTITION_CONTEXT *above_seg_context;
    283   ENTROPY_CONTEXT *above_context;
    284   int above_context_alloc_cols;
    285 } VP9_COMMON;
    286 
    287 // TODO(hkuang): Don't need to lock the whole pool after implementing atomic
    288 // frame reference count.
    289 void lock_buffer_pool(BufferPool *const pool);
    290 void unlock_buffer_pool(BufferPool *const pool);
    291 
    292 static INLINE YV12_BUFFER_CONFIG *get_ref_frame(VP9_COMMON *cm, int index) {
    293   if (index < 0 || index >= REF_FRAMES)
    294     return NULL;
    295   if (cm->ref_frame_map[index] < 0)
    296     return NULL;
    297   assert(cm->ref_frame_map[index] < FRAME_BUFFERS);
    298   return &cm->buffer_pool->frame_bufs[cm->ref_frame_map[index]].buf;
    299 }
    300 
    301 static INLINE YV12_BUFFER_CONFIG *get_frame_new_buffer(VP9_COMMON *cm) {
    302   return &cm->buffer_pool->frame_bufs[cm->new_fb_idx].buf;
    303 }
    304 
    305 static INLINE int get_free_fb(VP9_COMMON *cm) {
    306   RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
    307   int i;
    308 
    309   lock_buffer_pool(cm->buffer_pool);
    310   for (i = 0; i < FRAME_BUFFERS; ++i)
    311     if (frame_bufs[i].ref_count == 0)
    312       break;
    313 
    314   if (i != FRAME_BUFFERS) {
    315     frame_bufs[i].ref_count = 1;
    316   } else {
    317     // Reset i to be INVALID_IDX to indicate no free buffer found.
    318     i = INVALID_IDX;
    319   }
    320 
    321   unlock_buffer_pool(cm->buffer_pool);
    322   return i;
    323 }
    324 
    325 static INLINE void ref_cnt_fb(RefCntBuffer *bufs, int *idx, int new_idx) {
    326   const int ref_index = *idx;
    327 
    328   if (ref_index >= 0 && bufs[ref_index].ref_count > 0)
    329     bufs[ref_index].ref_count--;
    330 
    331   *idx = new_idx;
    332 
    333   bufs[new_idx].ref_count++;
    334 }
    335 
    336 static INLINE int mi_cols_aligned_to_sb(int n_mis) {
    337   return ALIGN_POWER_OF_TWO(n_mis, MI_BLOCK_SIZE_LOG2);
    338 }
    339 
    340 static INLINE int frame_is_intra_only(const VP9_COMMON *const cm) {
    341   return cm->frame_type == KEY_FRAME || cm->intra_only;
    342 }
    343 
    344 static INLINE void set_partition_probs(const VP9_COMMON *const cm,
    345                                        MACROBLOCKD *const xd) {
    346   xd->partition_probs =
    347       frame_is_intra_only(cm) ?
    348           &vp9_kf_partition_probs[0] :
    349           (const vpx_prob (*)[PARTITION_TYPES - 1])cm->fc->partition_prob;
    350 }
    351 
    352 static INLINE void vp9_init_macroblockd(VP9_COMMON *cm, MACROBLOCKD *xd,
    353                                         tran_low_t *dqcoeff) {
    354   int i;
    355 
    356   for (i = 0; i < MAX_MB_PLANE; ++i) {
    357     xd->plane[i].dqcoeff = dqcoeff;
    358     xd->above_context[i] = cm->above_context +
    359         i * sizeof(*cm->above_context) * 2 * mi_cols_aligned_to_sb(cm->mi_cols);
    360 
    361     if (get_plane_type(i) == PLANE_TYPE_Y) {
    362       memcpy(xd->plane[i].seg_dequant, cm->y_dequant, sizeof(cm->y_dequant));
    363     } else {
    364       memcpy(xd->plane[i].seg_dequant, cm->uv_dequant, sizeof(cm->uv_dequant));
    365     }
    366     xd->fc = cm->fc;
    367   }
    368 
    369   xd->above_seg_context = cm->above_seg_context;
    370   xd->mi_stride = cm->mi_stride;
    371   xd->error_info = &cm->error;
    372 
    373   set_partition_probs(cm, xd);
    374 }
    375 
    376 static INLINE const vpx_prob* get_partition_probs(const MACROBLOCKD *xd,
    377                                                   int ctx) {
    378   return xd->partition_probs[ctx];
    379 }
    380 
    381 static INLINE void set_skip_context(MACROBLOCKD *xd, int mi_row, int mi_col) {
    382   const int above_idx = mi_col * 2;
    383   const int left_idx = (mi_row * 2) & 15;
    384   int i;
    385   for (i = 0; i < MAX_MB_PLANE; ++i) {
    386     struct macroblockd_plane *const pd = &xd->plane[i];
    387     pd->above_context = &xd->above_context[i][above_idx >> pd->subsampling_x];
    388     pd->left_context = &xd->left_context[i][left_idx >> pd->subsampling_y];
    389   }
    390 }
    391 
    392 static INLINE int calc_mi_size(int len) {
    393   // len is in mi units.
    394   return len + MI_BLOCK_SIZE;
    395 }
    396 
    397 static INLINE void set_mi_row_col(MACROBLOCKD *xd, const TileInfo *const tile,
    398                                   int mi_row, int bh,
    399                                   int mi_col, int bw,
    400                                   int mi_rows, int mi_cols) {
    401   xd->mb_to_top_edge    = -((mi_row * MI_SIZE) * 8);
    402   xd->mb_to_bottom_edge = ((mi_rows - bh - mi_row) * MI_SIZE) * 8;
    403   xd->mb_to_left_edge   = -((mi_col * MI_SIZE) * 8);
    404   xd->mb_to_right_edge  = ((mi_cols - bw - mi_col) * MI_SIZE) * 8;
    405 
    406   // Are edges available for intra prediction?
    407   xd->up_available    = (mi_row != 0);
    408   xd->left_available  = (mi_col > tile->mi_col_start);
    409   if (xd->up_available) {
    410     xd->above_mi = xd->mi[-xd->mi_stride];
    411     // above_mi may be NULL in VP9 encoder's first pass.
    412     xd->above_mbmi = xd->above_mi ? &xd->above_mi->mbmi : NULL;
    413   } else {
    414     xd->above_mi = NULL;
    415     xd->above_mbmi = NULL;
    416   }
    417 
    418   if (xd->left_available) {
    419     xd->left_mi = xd->mi[-1];
    420     // left_mi may be NULL in VP9 encoder's first pass.
    421     xd->left_mbmi = xd->left_mi ? &xd->left_mi->mbmi : NULL;
    422   } else {
    423     xd->left_mi = NULL;
    424     xd->left_mbmi = NULL;
    425   }
    426 }
    427 
    428 static INLINE void update_partition_context(MACROBLOCKD *xd,
    429                                             int mi_row, int mi_col,
    430                                             BLOCK_SIZE subsize,
    431                                             BLOCK_SIZE bsize) {
    432   PARTITION_CONTEXT *const above_ctx = xd->above_seg_context + mi_col;
    433   PARTITION_CONTEXT *const left_ctx = xd->left_seg_context + (mi_row & MI_MASK);
    434 
    435   // num_4x4_blocks_wide_lookup[bsize] / 2
    436   const int bs = num_8x8_blocks_wide_lookup[bsize];
    437 
    438   // update the partition context at the end notes. set partition bits
    439   // of block sizes larger than the current one to be one, and partition
    440   // bits of smaller block sizes to be zero.
    441   memset(above_ctx, partition_context_lookup[subsize].above, bs);
    442   memset(left_ctx, partition_context_lookup[subsize].left, bs);
    443 }
    444 
    445 static INLINE int partition_plane_context(const MACROBLOCKD *xd,
    446                                           int mi_row, int mi_col,
    447                                           BLOCK_SIZE bsize) {
    448   const PARTITION_CONTEXT *above_ctx = xd->above_seg_context + mi_col;
    449   const PARTITION_CONTEXT *left_ctx = xd->left_seg_context + (mi_row & MI_MASK);
    450   const int bsl = mi_width_log2_lookup[bsize];
    451   int above = (*above_ctx >> bsl) & 1 , left = (*left_ctx >> bsl) & 1;
    452 
    453   assert(b_width_log2_lookup[bsize] == b_height_log2_lookup[bsize]);
    454   assert(bsl >= 0);
    455 
    456   return (left * 2 + above) + bsl * PARTITION_PLOFFSET;
    457 }
    458 
    459 #ifdef __cplusplus
    460 }  // extern "C"
    461 #endif
    462 
    463 #endif  // VP9_COMMON_VP9_ONYXC_INT_H_
    464