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