vp9_onyxc_int.h 13.7 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5 6
 *  that can be found in the LICENSE file in the root of the source
 *  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.
John Koleszar's avatar
John Koleszar committed
9 10
 */

11 12
#ifndef VP9_COMMON_VP9_ONYXC_INT_H_
#define VP9_COMMON_VP9_ONYXC_INT_H_
John Koleszar's avatar
John Koleszar committed
13

Jim Bankoski's avatar
Jim Bankoski committed
14
#include "./vpx_config.h"
15
#include "vpx/internal/vpx_codec_internal.h"
Jim Bankoski's avatar
Jim Bankoski committed
16
#include "./vp9_rtcd.h"
17
#include "vp9/common/vp9_alloccommon.h"
18 19 20 21
#include "vp9/common/vp9_loopfilter.h"
#include "vp9/common/vp9_entropymv.h"
#include "vp9/common/vp9_entropy.h"
#include "vp9/common/vp9_entropymode.h"
22
#include "vp9/common/vp9_frame_buffers.h"
23
#include "vp9/common/vp9_quant_common.h"
24
#include "vp9/common/vp9_thread.h"
James Zern's avatar
James Zern committed
25
#include "vp9/common/vp9_tile_common.h"
26

27
#if CONFIG_VP9_POSTPROC
28
#include "vp9/common/vp9_postproc.h"
29
#endif
John Koleszar's avatar
John Koleszar committed
30

31 32 33 34
#ifdef __cplusplus
extern "C" {
#endif

Dmitry Kovalev's avatar
Dmitry Kovalev committed
35
#define REFS_PER_FRAME 3
John Koleszar's avatar
John Koleszar committed
36

Dmitry Kovalev's avatar
Dmitry Kovalev committed
37 38
#define REF_FRAMES_LOG2 3
#define REF_FRAMES (1 << REF_FRAMES_LOG2)
39

40 41 42 43
// 4 scratch frames for the new frames to support a maximum of 4 cores decoding
// in parallel, 3 for scaled references on the encoder.
// TODO(hkuang): Add ondemand frame buffers instead of hardcoding the number
// of framebuffers.
44 45
// TODO(jkoleszar): These 3 extra references could probably come from the
// normal reference pool.
46
#define FRAME_BUFFERS (REF_FRAMES + 7)
John Koleszar's avatar
John Koleszar committed
47

Dmitry Kovalev's avatar
Dmitry Kovalev committed
48 49
#define FRAME_CONTEXTS_LOG2 2
#define FRAME_CONTEXTS (1 << FRAME_CONTEXTS_LOG2)
50

51 52
#define NUM_PING_PONG_BUFFERS 2

53 54 55 56 57
extern const struct {
  PARTITION_CONTEXT above;
  PARTITION_CONTEXT left;
} partition_context_lookup[BLOCK_SIZES];

58

John Koleszar's avatar
John Koleszar committed
59
typedef enum {
60 61 62 63 64
  SINGLE_REFERENCE      = 0,
  COMPOUND_REFERENCE    = 1,
  REFERENCE_MODE_SELECT = 2,
  REFERENCE_MODES       = 3,
} REFERENCE_MODE;
65

66 67 68 69
typedef struct {
  int_mv mv[2];
  MV_REFERENCE_FRAME ref_frame[2];
} MV_REF;
70 71 72

typedef struct {
  int ref_count;
73 74 75
  MV_REF *mvs;
  int mi_rows;
  int mi_cols;
76
  vpx_codec_frame_buffer_t raw_frame_buffer;
77
  YV12_BUFFER_CONFIG buf;
78 79 80 81 82 83 84 85 86 87 88 89

  // The Following variables will only be used in frame parallel decode.

  // frame_worker_owner indicates which FrameWorker owns this buffer. NULL means
  // that no FrameWorker owns, or is decoding, this buffer.
  VP9Worker *frame_worker_owner;

  // row and col indicate which position frame has been decoded to in real
  // pixel unit. They are reset to -1 when decoding begins and set to INT_MAX
  // when the frame is fully decoded.
  int row;
  int col;
90 91
} RefCntBuffer;

92
typedef struct BufferPool {
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  // Protect BufferPool from being accessed by several FrameWorkers at
  // the same time during frame parallel decode.
  // TODO(hkuang): Try to use atomic variable instead of locking the whole pool.
#if CONFIG_MULTITHREAD
  pthread_mutex_t pool_mutex;
#endif

  // Private data associated with the frame buffer callbacks.
  void *cb_priv;

  vpx_get_frame_buffer_cb_fn_t get_fb_cb;
  vpx_release_frame_buffer_cb_fn_t release_fb_cb;

  RefCntBuffer frame_bufs[FRAME_BUFFERS];

  // Frame buffers allocated internally by the codec.
  InternalFrameBufferList int_frame_buffers;
} BufferPool;

112
typedef struct VP9Common {
John Koleszar's avatar
John Koleszar committed
113
  struct vpx_internal_error_info  error;
114
  vpx_color_space_t color_space;
115 116
  int width;
  int height;
117 118
  int display_width;
  int display_height;
119 120
  int last_width;
  int last_height;
John Koleszar's avatar
John Koleszar committed
121

122 123 124 125 126 127
  // TODO(jkoleszar): this implies chroma ss right now, but could vary per
  // plane. Revisit as part of the future change to YV12_BUFFER_CONFIG to
  // support additional planes.
  int subsampling_x;
  int subsampling_y;

128 129 130 131
#if CONFIG_VP9_HIGHBITDEPTH
  int use_highbitdepth;  // Marks if we need to use 16bit frame buffers.
#endif

John Koleszar's avatar
John Koleszar committed
132
  YV12_BUFFER_CONFIG *frame_to_show;
133 134 135 136
  RefCntBuffer *prev_frame;

  // TODO(hkuang): Combine this with cur_buf in macroblockd.
  RefCntBuffer *cur_frame;
137

Dmitry Kovalev's avatar
Dmitry Kovalev committed
138
  int ref_frame_map[REF_FRAMES]; /* maps fb_idx to reference slot */
139

140 141 142 143
  // Prepare ref_frame_map for the next frame.
  // Only used in frame parallel decode.
  int next_ref_frame_map[REF_FRAMES];

144 145 146
  // TODO(jkoleszar): could expand active_ref_idx to 4, with 0 as intra, and
  // roll new_fb_idx into it.

Dmitry Kovalev's avatar
Dmitry Kovalev committed
147
  // Each frame can reference REFS_PER_FRAME buffers
Dmitry Kovalev's avatar
Dmitry Kovalev committed
148 149
  RefBuffer frame_refs[REFS_PER_FRAME];

150
  int new_fb_idx;
151

152
#if CONFIG_VP9_POSTPROC
John Koleszar's avatar
John Koleszar committed
153
  YV12_BUFFER_CONFIG post_proc_buffer;
154
  YV12_BUFFER_CONFIG post_proc_buffer_int;
155
#endif
Attila Nagy's avatar
Attila Nagy committed
156

Jim Bankoski's avatar
Jim Bankoski committed
157
  FRAME_TYPE last_frame_type;  /* last frame's frame type for motion search.*/
John Koleszar's avatar
John Koleszar committed
158
  FRAME_TYPE frame_type;
John Koleszar's avatar
John Koleszar committed
159

John Koleszar's avatar
John Koleszar committed
160
  int show_frame;
161
  int last_show_frame;
Tero Rintaluoma's avatar
Tero Rintaluoma committed
162
  int show_existing_frame;
John Koleszar's avatar
John Koleszar committed
163

164 165 166
  // Flag signaling that the frame is encoded using only INTRA modes.
  int intra_only;

167 168
  int allow_high_precision_mv;

169
  // Flag signaling that the frame context should be reset to default values.
170 171
  // 0 or 1 implies don't reset, 2 reset just the context specified in the
  // frame header, 3 reset all contexts.
172 173
  int reset_frame_context;

174
  // MBs, mb_rows/cols is in 16-pixel units; mi_rows/cols is in
Jingning Han's avatar
Jingning Han committed
175
  // MODE_INFO (8-pixel) units.
John Koleszar's avatar
John Koleszar committed
176
  int MBs;
177 178
  int mb_rows, mi_rows;
  int mb_cols, mi_cols;
179
  int mi_stride;
John Koleszar's avatar
John Koleszar committed
180

John Koleszar's avatar
John Koleszar committed
181
  /* profile settings */
182
  TX_MODE tx_mode;
John Koleszar's avatar
John Koleszar committed
183

John Koleszar's avatar
John Koleszar committed
184
  int base_qindex;
185 186 187
  int y_dc_delta_q;
  int uv_dc_delta_q;
  int uv_ac_delta_q;
188 189
  int16_t y_dequant[MAX_SEGMENTS][2];
  int16_t uv_dequant[MAX_SEGMENTS][2];
John Koleszar's avatar
John Koleszar committed
190

John Koleszar's avatar
John Koleszar committed
191 192
  /* We allocate a MODE_INFO struct for each macroblock, together with
     an extra row on top and column on the left to simplify prediction. */
193
  int mi_alloc_size;
John Koleszar's avatar
John Koleszar committed
194 195
  MODE_INFO *mip; /* Base of allocated array */
  MODE_INFO *mi;  /* Corresponds to upper left visible macroblock */
196 197 198

  // TODO(agrange): Move prev_mi into encoder structure.
  // prev_mip and prev_mi will only be allocated in VP9 encoder.
John Koleszar's avatar
John Koleszar committed
199 200
  MODE_INFO *prev_mip; /* MODE_INFO array 'mip' from last decoded frame */
  MODE_INFO *prev_mi;  /* 'mi' from last frame (points into prev_mip) */
John Koleszar's avatar
John Koleszar committed
201

202 203 204 205 206
  // Separate mi functions between encoder and decoder.
  int (*alloc_mi)(struct VP9Common *cm, int mi_size);
  void (*free_mi)(struct VP9Common *cm);
  void (*setup_mi)(struct VP9Common *cm);

207 208 209 210 211 212
  // Grid of pointers to 8x8 MODE_INFO structs.  Any 8x8 not in the visible
  // area will be NULL.
  MODE_INFO **mi_grid_base;
  MODE_INFO **mi_grid_visible;
  MODE_INFO **prev_mi_grid_base;
  MODE_INFO **prev_mi_grid_visible;
213 214 215 216

  // Whether to use previous frame's motion vectors for prediction.
  int use_prev_frame_mvs;

John Koleszar's avatar
John Koleszar committed
217
  // Persistent mb segment id map used in prediction.
218 219 220 221 222 223
  int seg_map_idx;
  int prev_seg_map_idx;

  uint8_t *seg_map_array[NUM_PING_PONG_BUFFERS];
  uint8_t *last_frame_seg_map;
  uint8_t *current_frame_seg_map;
224
  int seg_map_alloc_size;
225

226
  INTERP_FILTER interp_filter;
Attila Nagy's avatar
Attila Nagy committed
227

John Koleszar's avatar
John Koleszar committed
228
  loop_filter_info_n lf_info;
Attila Nagy's avatar
Attila Nagy committed
229

230
  int refresh_frame_context;    /* Two state 0 = NO, 1 = YES */
John Koleszar's avatar
John Koleszar committed
231

John Koleszar's avatar
John Koleszar committed
232
  int ref_frame_sign_bias[MAX_REF_FRAMES];    /* Two state 0, 1 */
John Koleszar's avatar
John Koleszar committed
233

234
  struct loopfilter lf;
235
  struct segmentation seg;
236

237 238 239 240
  // TODO(hkuang): Remove this as it is the same as frame_parallel_decode
  // in pbi.
  int frame_parallel_decode;  // frame-based threading.

John Koleszar's avatar
John Koleszar committed
241
  // Context probabilities for reference frame prediction
Ronald S. Bultje's avatar
Ronald S. Bultje committed
242 243
  MV_REFERENCE_FRAME comp_fixed_ref;
  MV_REFERENCE_FRAME comp_var_ref[2];
244
  REFERENCE_MODE reference_mode;
Paul Wilkins's avatar
Paul Wilkins committed
245

246 247
  FRAME_CONTEXT *fc;  /* this frame entropy */
  FRAME_CONTEXT *frame_contexts;   // FRAME_CONTEXTS
248
  unsigned int  frame_context_idx; /* Context to use/update */
249
  FRAME_COUNTS counts;
John Koleszar's avatar
John Koleszar committed
250

John Koleszar's avatar
John Koleszar committed
251
  unsigned int current_video_frame;
252 253
  BITSTREAM_PROFILE profile;

254 255
  // VPX_BITS_8 in profile 0 or 1, VPX_BITS_10 or VPX_BITS_12 in profile 2 or 3.
  vpx_bit_depth_t bit_depth;
256
  vpx_bit_depth_t dequant_bit_depth;  // bit_depth of current dequantizer
John Koleszar's avatar
John Koleszar committed
257

258
#if CONFIG_VP9_POSTPROC
John Koleszar's avatar
John Koleszar committed
259
  struct postproc_state  postproc_state;
260
#endif
261

262
  int error_resilient_mode;
263
  int frame_parallel_decoding_mode;
264

Dmitry Kovalev's avatar
Dmitry Kovalev committed
265
  int log2_tile_cols, log2_tile_rows;
266
  int byte_alignment;
267 268 269 270 271 272 273 274

  // Private data associated with the frame buffer callbacks.
  void *cb_priv;
  vpx_get_frame_buffer_cb_fn_t get_fb_cb;
  vpx_release_frame_buffer_cb_fn_t release_fb_cb;

  // Handles memory for the codec.
  InternalFrameBufferList int_frame_buffers;
275

276 277 278
  // External BufferPool passed from outside.
  BufferPool *buffer_pool;

279
  PARTITION_CONTEXT *above_seg_context;
280
  ENTROPY_CONTEXT *above_context;
281
  int above_context_alloc_cols;
282
} VP9_COMMON;
John Koleszar's avatar
John Koleszar committed
283

284 285 286 287 288
// TODO(hkuang): Don't need to lock the whole pool after implementing atomic
// frame reference count.
void lock_buffer_pool(BufferPool *const pool);
void unlock_buffer_pool(BufferPool *const pool);

289 290 291
static INLINE YV12_BUFFER_CONFIG *get_ref_frame(VP9_COMMON *cm, int index) {
  if (index < 0 || index >= REF_FRAMES)
    return NULL;
292 293
  if (cm->ref_frame_map[index] < 0)
    return NULL;
294
  assert(cm->ref_frame_map[index] < FRAME_BUFFERS);
295
  return &cm->buffer_pool->frame_bufs[cm->ref_frame_map[index]].buf;
296 297
}

298
static INLINE YV12_BUFFER_CONFIG *get_frame_new_buffer(VP9_COMMON *cm) {
299
  return &cm->buffer_pool->frame_bufs[cm->new_fb_idx].buf;
300 301
}

302
static INLINE int get_free_fb(VP9_COMMON *cm) {
303
  RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
304
  int i;
305 306 307 308

  lock_buffer_pool(cm->buffer_pool);
  for (i = 0; i < FRAME_BUFFERS; ++i)
    if (frame_bufs[i].ref_count == 0)
309 310
      break;

311 312 313 314 315 316 317
  if (i != FRAME_BUFFERS) {
    frame_bufs[i].ref_count = 1;
  } else {
    // Reset i to be INVALID_IDX to indicate no free buffer found.
    i = INVALID_IDX;
  }

318
  unlock_buffer_pool(cm->buffer_pool);
319
  return i;
320 321
}

322
static INLINE void ref_cnt_fb(RefCntBuffer *bufs, int *idx, int new_idx) {
323 324
  const int ref_index = *idx;

325 326
  if (ref_index >= 0 && bufs[ref_index].ref_count > 0)
    bufs[ref_index].ref_count--;
327 328 329

  *idx = new_idx;

330
  bufs[new_idx].ref_count++;
331 332
}

333
static INLINE int mi_cols_aligned_to_sb(int n_mis) {
James Zern's avatar
James Zern committed
334
  return ALIGN_POWER_OF_TWO(n_mis, MI_BLOCK_SIZE_LOG2);
335 336
}

337 338 339 340
static INLINE void init_macroblockd(VP9_COMMON *cm, MACROBLOCKD *xd) {
  int i;

  for (i = 0; i < MAX_MB_PLANE; ++i) {
341
    xd->plane[i].dqcoeff = xd->dqcoeff;
342 343
    xd->above_context[i] = cm->above_context +
        i * sizeof(*cm->above_context) * 2 * mi_cols_aligned_to_sb(cm->mi_cols);
344 345 346 347 348 349 350 351

    if (xd->plane[i].plane_type == PLANE_TYPE_Y) {
      memcpy(xd->plane[i].seg_dequant, cm->y_dequant, sizeof(cm->y_dequant));
    } else {
      memcpy(xd->plane[i].seg_dequant, cm->uv_dequant, sizeof(cm->uv_dequant));
    }
    xd->fc = cm->fc;
    xd->frame_parallel_decoding_mode = cm->frame_parallel_decoding_mode;
352 353 354
  }

  xd->above_seg_context = cm->above_seg_context;
355
  xd->mi_stride = cm->mi_stride;
356
  xd->error_info = &cm->error;
357 358
}

359 360 361 362
static INLINE int frame_is_intra_only(const VP9_COMMON *const cm) {
  return cm->frame_type == KEY_FRAME || cm->intra_only;
}

363 364
static INLINE const vp9_prob* get_partition_probs(const VP9_COMMON *cm,
                                                  int ctx) {
365
  return frame_is_intra_only(cm) ? vp9_kf_partition_probs[ctx]
366
                                 : cm->fc->partition_prob[ctx];
367 368
}

369
static INLINE void set_skip_context(MACROBLOCKD *xd, int mi_row, int mi_col) {
370 371 372
  const int above_idx = mi_col * 2;
  const int left_idx = (mi_row * 2) & 15;
  int i;
373
  for (i = 0; i < MAX_MB_PLANE; ++i) {
374
    struct macroblockd_plane *const pd = &xd->plane[i];
375 376
    pd->above_context = &xd->above_context[i][above_idx >> pd->subsampling_x];
    pd->left_context = &xd->left_context[i][left_idx >> pd->subsampling_y];
377 378 379
  }
}

380 381 382 383 384
static INLINE int calc_mi_size(int len) {
  // len is in mi units.
  return len + MI_BLOCK_SIZE;
}

385 386 387 388
static INLINE void 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) {
Yaowu Xu's avatar
Yaowu Xu committed
389
  xd->mb_to_top_edge    = -((mi_row * MI_SIZE) * 8);
James Zern's avatar
James Zern committed
390
  xd->mb_to_bottom_edge = ((mi_rows - bh - mi_row) * MI_SIZE) * 8;
Yaowu Xu's avatar
Yaowu Xu committed
391
  xd->mb_to_left_edge   = -((mi_col * MI_SIZE) * 8);
James Zern's avatar
James Zern committed
392
  xd->mb_to_right_edge  = ((mi_cols - bw - mi_col) * MI_SIZE) * 8;
393 394

  // Are edges available for intra prediction?
395
  xd->up_available    = (mi_row != 0);
James Zern's avatar
James Zern committed
396
  xd->left_available  = (mi_col > tile->mi_col_start);
397
  if (xd->up_available) {
398
    xd->above_mi = xd->mi[-xd->mi_stride];
399 400
    // above_mi may be NULL in VP9 encoder's first pass.
    xd->above_mbmi = xd->above_mi ? &xd->above_mi->mbmi : NULL;
401 402 403 404 405 406
  } else {
    xd->above_mi = NULL;
    xd->above_mbmi = NULL;
  }

  if (xd->left_available) {
407
    xd->left_mi = xd->mi[-1];
408 409
    // left_mi may be NULL in VP9 encoder's first pass.
    xd->left_mbmi = xd->left_mi ? &xd->left_mi->mbmi : NULL;
410 411 412 413
  } else {
    xd->left_mi = NULL;
    xd->left_mbmi = NULL;
  }
414 415
}

416 417 418 419 420 421
static INLINE void update_partition_context(MACROBLOCKD *xd,
                                            int mi_row, int mi_col,
                                            BLOCK_SIZE subsize,
                                            BLOCK_SIZE bsize) {
  PARTITION_CONTEXT *const above_ctx = xd->above_seg_context + mi_col;
  PARTITION_CONTEXT *const left_ctx = xd->left_seg_context + (mi_row & MI_MASK);
422

423 424
  // num_4x4_blocks_wide_lookup[bsize] / 2
  const int bs = num_8x8_blocks_wide_lookup[bsize];
425 426 427 428

  // update the partition context at the end notes. set partition bits
  // of block sizes larger than the current one to be one, and partition
  // bits of smaller block sizes to be zero.
James Zern's avatar
James Zern committed
429 430
  memset(above_ctx, partition_context_lookup[subsize].above, bs);
  memset(left_ctx, partition_context_lookup[subsize].left, bs);
431 432
}

433 434 435 436 437
static INLINE int partition_plane_context(const MACROBLOCKD *xd,
                                          int mi_row, int mi_col,
                                          BLOCK_SIZE bsize) {
  const PARTITION_CONTEXT *above_ctx = xd->above_seg_context + mi_col;
  const PARTITION_CONTEXT *left_ctx = xd->left_seg_context + (mi_row & MI_MASK);
438
  const int bsl = mi_width_log2_lookup[bsize];
hkuang's avatar
hkuang committed
439
  int above = (*above_ctx >> bsl) & 1 , left = (*left_ctx >> bsl) & 1;
440

441
  assert(b_width_log2_lookup[bsize] == b_height_log2_lookup[bsize]);
442 443 444 445 446
  assert(bsl >= 0);

  return (left * 2 + above) + bsl * PARTITION_PLOFFSET;
}

447 448 449 450
#ifdef __cplusplus
}  // extern "C"
#endif

451
#endif  // VP9_COMMON_VP9_ONYXC_INT_H_