]> granicus.if.org Git - libvpx/blob - vp9/common/vp9_onyxc_int.h
Add segmentation map array for current and last frame segmentation.
[libvpx] / vp9 / common / vp9_onyxc_int.h
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_frame_buffers.h"
22 #include "vp9/common/vp9_quant_common.h"
23 #include "vp9/common/vp9_thread.h"
24 #include "vp9/common/vp9_tile_common.h"
25
26 #if CONFIG_VP9_POSTPROC
27 #include "vp9/common/vp9_postproc.h"
28 #endif
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 #define REFS_PER_FRAME 3
35
36 #define REF_FRAMES_LOG2 3
37 #define REF_FRAMES (1 << REF_FRAMES_LOG2)
38
39 // 1 scratch frame for the new frame, 3 for scaled references on the encoder
40 // TODO(jkoleszar): These 3 extra references could probably come from the
41 // normal reference pool.
42 #define FRAME_BUFFERS (REF_FRAMES + 4)
43
44 #define FRAME_CONTEXTS_LOG2 2
45 #define FRAME_CONTEXTS (1 << FRAME_CONTEXTS_LOG2)
46
47 #define NUM_PING_PONG_BUFFERS 2
48
49 extern const struct {
50   PARTITION_CONTEXT above;
51   PARTITION_CONTEXT left;
52 } partition_context_lookup[BLOCK_SIZES];
53
54
55 typedef enum {
56   SINGLE_REFERENCE      = 0,
57   COMPOUND_REFERENCE    = 1,
58   REFERENCE_MODE_SELECT = 2,
59   REFERENCE_MODES       = 3,
60 } REFERENCE_MODE;
61
62
63 typedef struct {
64   int ref_count;
65   vpx_codec_frame_buffer_t raw_frame_buffer;
66   YV12_BUFFER_CONFIG buf;
67 } RefCntBuffer;
68
69 typedef struct {
70   // Protect BufferPool from being accessed by several FrameWorkers at
71   // the same time during frame parallel decode.
72   // TODO(hkuang): Try to use atomic variable instead of locking the whole pool.
73 #if CONFIG_MULTITHREAD
74   pthread_mutex_t pool_mutex;
75 #endif
76
77   // Private data associated with the frame buffer callbacks.
78   void *cb_priv;
79
80   vpx_get_frame_buffer_cb_fn_t get_fb_cb;
81   vpx_release_frame_buffer_cb_fn_t release_fb_cb;
82
83   RefCntBuffer frame_bufs[FRAME_BUFFERS];
84
85   // Frame buffers allocated internally by the codec.
86   InternalFrameBufferList int_frame_buffers;
87 } BufferPool;
88
89 typedef struct VP9Common {
90   struct vpx_internal_error_info  error;
91
92   DECLARE_ALIGNED(16, int16_t, y_dequant[QINDEX_RANGE][8]);
93   DECLARE_ALIGNED(16, int16_t, uv_dequant[QINDEX_RANGE][8]);
94 #if CONFIG_ALPHA
95   DECLARE_ALIGNED(16, int16_t, a_dequant[QINDEX_RANGE][8]);
96 #endif
97
98   COLOR_SPACE color_space;
99
100   int width;
101   int height;
102   int display_width;
103   int display_height;
104   int last_width;
105   int last_height;
106
107   // TODO(jkoleszar): this implies chroma ss right now, but could vary per
108   // plane. Revisit as part of the future change to YV12_BUFFER_CONFIG to
109   // support additional planes.
110   int subsampling_x;
111   int subsampling_y;
112
113   YV12_BUFFER_CONFIG *frame_to_show;
114
115   int ref_frame_map[REF_FRAMES]; /* maps fb_idx to reference slot */
116
117   // TODO(jkoleszar): could expand active_ref_idx to 4, with 0 as intra, and
118   // roll new_fb_idx into it.
119
120   // Each frame can reference REFS_PER_FRAME buffers
121   RefBuffer frame_refs[REFS_PER_FRAME];
122
123   int new_fb_idx;
124
125   YV12_BUFFER_CONFIG post_proc_buffer;
126
127   FRAME_TYPE last_frame_type;  /* last frame's frame type for motion search.*/
128   FRAME_TYPE frame_type;
129
130   int show_frame;
131   int last_show_frame;
132   int show_existing_frame;
133
134   // Flag signaling that the frame is encoded using only INTRA modes.
135   int intra_only;
136
137   int allow_high_precision_mv;
138
139   // Flag signaling that the frame context should be reset to default values.
140   // 0 or 1 implies don't reset, 2 reset just the context specified in the
141   // frame header, 3 reset all contexts.
142   int reset_frame_context;
143
144   // MBs, mb_rows/cols is in 16-pixel units; mi_rows/cols is in
145   // MODE_INFO (8-pixel) units.
146   int MBs;
147   int mb_rows, mi_rows;
148   int mb_cols, mi_cols;
149   int mi_stride;
150
151   /* profile settings */
152   TX_MODE tx_mode;
153
154   int base_qindex;
155   int y_dc_delta_q;
156   int uv_dc_delta_q;
157   int uv_ac_delta_q;
158 #if CONFIG_ALPHA
159   int a_dc_delta_q;
160   int a_ac_delta_q;
161 #endif
162
163   /* We allocate a MODE_INFO struct for each macroblock, together with
164      an extra row on top and column on the left to simplify prediction. */
165
166   int mi_idx;
167   int prev_mi_idx;
168   MODE_INFO *mip_array[NUM_PING_PONG_BUFFERS];
169   MODE_INFO **mi_grid_base_array[NUM_PING_PONG_BUFFERS];
170
171   MODE_INFO *mip; /* Base of allocated array */
172   MODE_INFO *mi;  /* Corresponds to upper left visible macroblock */
173   MODE_INFO *prev_mip; /* MODE_INFO array 'mip' from last decoded frame */
174   MODE_INFO *prev_mi;  /* 'mi' from last frame (points into prev_mip) */
175
176   MODE_INFO **mi_grid_base;
177   MODE_INFO **mi_grid_visible;
178   MODE_INFO **prev_mi_grid_base;
179   MODE_INFO **prev_mi_grid_visible;
180
181   // Persistent mb segment id map used in prediction.
182   int seg_map_idx;
183   int prev_seg_map_idx;
184
185   uint8_t *seg_map_array[NUM_PING_PONG_BUFFERS];
186   uint8_t *last_frame_seg_map;
187   uint8_t *current_frame_seg_map;
188
189   INTERP_FILTER interp_filter;
190
191   loop_filter_info_n lf_info;
192
193   int refresh_frame_context;    /* Two state 0 = NO, 1 = YES */
194
195   int ref_frame_sign_bias[MAX_REF_FRAMES];    /* Two state 0, 1 */
196
197   struct loopfilter lf;
198   struct segmentation seg;
199
200   // Context probabilities for reference frame prediction
201   int allow_comp_inter_inter;
202   MV_REFERENCE_FRAME comp_fixed_ref;
203   MV_REFERENCE_FRAME comp_var_ref[2];
204   REFERENCE_MODE reference_mode;
205
206   FRAME_CONTEXT fc;  /* this frame entropy */
207   FRAME_CONTEXT frame_contexts[FRAME_CONTEXTS];
208   unsigned int  frame_context_idx; /* Context to use/update */
209   FRAME_COUNTS counts;
210
211   unsigned int current_video_frame;
212   BITSTREAM_PROFILE profile;
213
214   // BITS_8 in versions 0 and 1, BITS_10 or BITS_12 in version 2
215   BIT_DEPTH bit_depth;
216
217 #if CONFIG_VP9_POSTPROC
218   struct postproc_state  postproc_state;
219 #endif
220
221   int error_resilient_mode;
222   int frame_parallel_decoding_mode;
223
224   // Flag indicates if prev_mi can be used in coding:
225   //   0: encoder assumes decoder does not have prev_mi
226   //   1: encoder assumes decoder has and uses prev_mi
227   unsigned int coding_use_prev_mi;
228
229   int log2_tile_cols, log2_tile_rows;
230
231   // External BufferPool passed from outside.
232   BufferPool *buffer_pool;
233
234   PARTITION_CONTEXT *above_seg_context;
235   ENTROPY_CONTEXT *above_context;
236 } VP9_COMMON;
237
238 static INLINE YV12_BUFFER_CONFIG *get_frame_new_buffer(VP9_COMMON *cm) {
239   return &cm->buffer_pool->frame_bufs[cm->new_fb_idx].buf;
240 }
241
242 static INLINE int get_free_fb(VP9_COMMON *cm) {
243   RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
244   int i;
245   for (i = 0; i < FRAME_BUFFERS; ++i)
246     if (frame_bufs[i].ref_count == 0)
247       break;
248
249   assert(i < FRAME_BUFFERS);
250   frame_bufs[i].ref_count = 1;
251   return i;
252 }
253
254 static INLINE void ref_cnt_fb(RefCntBuffer *bufs, int *idx, int new_idx) {
255   const int ref_index = *idx;
256
257   if (ref_index >= 0 && bufs[ref_index].ref_count > 0)
258     bufs[ref_index].ref_count--;
259
260   *idx = new_idx;
261
262   bufs[new_idx].ref_count++;
263 }
264
265 static INLINE int mi_cols_aligned_to_sb(int n_mis) {
266   return ALIGN_POWER_OF_TWO(n_mis, MI_BLOCK_SIZE_LOG2);
267 }
268
269 static INLINE void init_macroblockd(VP9_COMMON *cm, MACROBLOCKD *xd) {
270   int i;
271
272   for (i = 0; i < MAX_MB_PLANE; ++i) {
273     xd->plane[i].dqcoeff = xd->dqcoeff[i];
274     xd->above_context[i] = cm->above_context +
275         i * sizeof(*cm->above_context) * 2 * mi_cols_aligned_to_sb(cm->mi_cols);
276   }
277
278   xd->above_seg_context = cm->above_seg_context;
279   xd->mi_stride = cm->mi_stride;
280 }
281
282 static INLINE const vp9_prob* get_partition_probs(const VP9_COMMON *cm,
283                                                   int ctx) {
284   return cm->frame_type == KEY_FRAME ? vp9_kf_partition_probs[ctx]
285                                      : cm->fc.partition_prob[ctx];
286 }
287
288 static INLINE void set_skip_context(MACROBLOCKD *xd, int mi_row, int mi_col) {
289   const int above_idx = mi_col * 2;
290   const int left_idx = (mi_row * 2) & 15;
291   int i;
292   for (i = 0; i < MAX_MB_PLANE; ++i) {
293     struct macroblockd_plane *const pd = &xd->plane[i];
294     pd->above_context = &xd->above_context[i][above_idx >> pd->subsampling_x];
295     pd->left_context = &xd->left_context[i][left_idx >> pd->subsampling_y];
296   }
297 }
298
299 static INLINE void set_mi_row_col(MACROBLOCKD *xd, const TileInfo *const tile,
300                                   int mi_row, int bh,
301                                   int mi_col, int bw,
302                                   int mi_rows, int mi_cols) {
303   xd->mb_to_top_edge    = -((mi_row * MI_SIZE) * 8);
304   xd->mb_to_bottom_edge = ((mi_rows - bh - mi_row) * MI_SIZE) * 8;
305   xd->mb_to_left_edge   = -((mi_col * MI_SIZE) * 8);
306   xd->mb_to_right_edge  = ((mi_cols - bw - mi_col) * MI_SIZE) * 8;
307
308   // Are edges available for intra prediction?
309   xd->up_available    = (mi_row != 0);
310   xd->left_available  = (mi_col > tile->mi_col_start);
311 }
312
313 static INLINE void set_prev_mi(VP9_COMMON *cm) {
314   const int use_prev_in_find_mv_refs = cm->width == cm->last_width &&
315                                        cm->height == cm->last_height &&
316                                        !cm->intra_only &&
317                                        cm->last_show_frame;
318   // Special case: set prev_mi to NULL when the previous mode info
319   // context cannot be used.
320   cm->prev_mi = use_prev_in_find_mv_refs ?
321                   cm->prev_mip + cm->mi_stride + 1 : NULL;
322 }
323
324 static INLINE int frame_is_intra_only(const VP9_COMMON *const cm) {
325   return cm->frame_type == KEY_FRAME || cm->intra_only;
326 }
327
328 static INLINE void update_partition_context(MACROBLOCKD *xd,
329                                             int mi_row, int mi_col,
330                                             BLOCK_SIZE subsize,
331                                             BLOCK_SIZE bsize) {
332   PARTITION_CONTEXT *const above_ctx = xd->above_seg_context + mi_col;
333   PARTITION_CONTEXT *const left_ctx = xd->left_seg_context + (mi_row & MI_MASK);
334
335   const int bs = num_8x8_blocks_wide_lookup[bsize];
336
337   // update the partition context at the end notes. set partition bits
338   // of block sizes larger than the current one to be one, and partition
339   // bits of smaller block sizes to be zero.
340   vpx_memset(above_ctx, partition_context_lookup[subsize].above, bs);
341   vpx_memset(left_ctx, partition_context_lookup[subsize].left, bs);
342 }
343
344 static INLINE int partition_plane_context(const MACROBLOCKD *xd,
345                                           int mi_row, int mi_col,
346                                           BLOCK_SIZE bsize) {
347   const PARTITION_CONTEXT *above_ctx = xd->above_seg_context + mi_col;
348   const PARTITION_CONTEXT *left_ctx = xd->left_seg_context + (mi_row & MI_MASK);
349
350   const int bsl = mi_width_log2(bsize);
351   const int bs = 1 << bsl;
352   int above = 0, left = 0, i;
353
354   assert(b_width_log2(bsize) == b_height_log2(bsize));
355   assert(bsl >= 0);
356
357   for (i = 0; i < bs; i++) {
358     above |= above_ctx[i];
359     left |= left_ctx[i];
360   }
361   above = (above & bs) > 0;
362   left  = (left & bs) > 0;
363
364   return (left * 2 + above) + bsl * PARTITION_PLOFFSET;
365 }
366
367 #ifdef __cplusplus
368 }  // extern "C"
369 #endif
370
371 #endif  // VP9_COMMON_VP9_ONYXC_INT_H_