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 VPX_VP9_COMMON_VP9_ONYXC_INT_H_
12 #define VPX_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 // 1 scratch frame for the new frame, REFS_PER_FRAME for scaled references on
41 // the encoder.
42 #define FRAME_BUFFERS (REF_FRAMES + 1 + REFS_PER_FRAME)
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 typedef enum {
55   SINGLE_REFERENCE = 0,
56   COMPOUND_REFERENCE = 1,
57   REFERENCE_MODE_SELECT = 2,
58   REFERENCE_MODES = 3,
59 } REFERENCE_MODE;
60 
61 typedef struct {
62   int_mv mv[2];
63   MV_REFERENCE_FRAME ref_frame[2];
64 } MV_REF;
65 
66 typedef struct {
67   int ref_count;
68   MV_REF *mvs;
69   int mi_rows;
70   int mi_cols;
71   uint8_t released;
72   int frame_index;
73   vpx_codec_frame_buffer_t raw_frame_buffer;
74   YV12_BUFFER_CONFIG buf;
75 } RefCntBuffer;
76 
77 typedef 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 
90 typedef 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   int cur_show_frame_fb_idx;
132 
133 #if CONFIG_VP9_POSTPROC
134   YV12_BUFFER_CONFIG post_proc_buffer;
135   YV12_BUFFER_CONFIG post_proc_buffer_int;
136 #endif
137 
138   FRAME_TYPE last_frame_type; /* last frame's frame type for motion search.*/
139   FRAME_TYPE frame_type;
140 
141   int show_frame;
142   int last_show_frame;
143   int show_existing_frame;
144 
145   // Flag signaling that the frame is encoded using only INTRA modes.
146   uint8_t intra_only;
147   uint8_t last_intra_only;
148 
149   int allow_high_precision_mv;
150 
151   // Flag signaling that the frame context should be reset to default values.
152   // 0 or 1 implies don't reset, 2 reset just the context specified in the
153   // frame header, 3 reset all contexts.
154   int reset_frame_context;
155 
156   // MBs, mb_rows/cols is in 16-pixel units; mi_rows/cols is in
157   // MODE_INFO (8-pixel) units.
158   int MBs;
159   int mb_rows, mi_rows;
160   int mb_cols, mi_cols;
161   int mi_stride;
162 
163   /* profile settings */
164   TX_MODE tx_mode;
165 
166   int base_qindex;
167   int y_dc_delta_q;
168   int uv_dc_delta_q;
169   int uv_ac_delta_q;
170   int16_t y_dequant[MAX_SEGMENTS][2];
171   int16_t uv_dequant[MAX_SEGMENTS][2];
172 
173   /* We allocate a MODE_INFO struct for each macroblock, together with
174      an extra row on top and column on the left to simplify prediction. */
175   int mi_alloc_size;
176   MODE_INFO *mip; /* Base of allocated array */
177   MODE_INFO *mi;  /* Corresponds to upper left visible macroblock */
178 
179   // TODO(agrange): Move prev_mi into encoder structure.
180   // prev_mip and prev_mi will only be allocated in VP9 encoder.
181   MODE_INFO *prev_mip; /* MODE_INFO array 'mip' from last decoded frame */
182   MODE_INFO *prev_mi;  /* 'mi' from last frame (points into prev_mip) */
183 
184   // Separate mi functions between encoder and decoder.
185   int (*alloc_mi)(struct VP9Common *cm, int mi_size);
186   void (*free_mi)(struct VP9Common *cm);
187   void (*setup_mi)(struct VP9Common *cm);
188 
189   // Grid of pointers to 8x8 MODE_INFO structs.  Any 8x8 not in the visible
190   // area will be NULL.
191   MODE_INFO **mi_grid_base;
192   MODE_INFO **mi_grid_visible;
193   MODE_INFO **prev_mi_grid_base;
194   MODE_INFO **prev_mi_grid_visible;
195 
196   // Whether to use previous frame's motion vectors for prediction.
197   int use_prev_frame_mvs;
198 
199   // Persistent mb segment id map used in prediction.
200   int seg_map_idx;
201   int prev_seg_map_idx;
202 
203   uint8_t *seg_map_array[NUM_PING_PONG_BUFFERS];
204   uint8_t *last_frame_seg_map;
205   uint8_t *current_frame_seg_map;
206   int seg_map_alloc_size;
207 
208   INTERP_FILTER interp_filter;
209 
210   loop_filter_info_n lf_info;
211 
212   int refresh_frame_context; /* Two state 0 = NO, 1 = YES */
213 
214   int ref_frame_sign_bias[MAX_REF_FRAMES]; /* Two state 0, 1 */
215 
216   struct loopfilter lf;
217   struct segmentation seg;
218 
219   // Context probabilities for reference frame prediction
220   MV_REFERENCE_FRAME comp_fixed_ref;
221   MV_REFERENCE_FRAME comp_var_ref[2];
222   REFERENCE_MODE reference_mode;
223 
224   FRAME_CONTEXT *fc;              /* this frame entropy */
225   FRAME_CONTEXT *frame_contexts;  // FRAME_CONTEXTS
226   unsigned int frame_context_idx; /* Context to use/update */
227   FRAME_COUNTS counts;
228 
229   unsigned int current_video_frame;
230   BITSTREAM_PROFILE profile;
231 
232   // VPX_BITS_8 in profile 0 or 1, VPX_BITS_10 or VPX_BITS_12 in profile 2 or 3.
233   vpx_bit_depth_t bit_depth;
234   vpx_bit_depth_t dequant_bit_depth;  // bit_depth of current dequantizer
235 
236 #if CONFIG_VP9_POSTPROC
237   struct postproc_state postproc_state;
238 #endif
239 
240   int error_resilient_mode;
241   int frame_parallel_decoding_mode;
242 
243   int log2_tile_cols, log2_tile_rows;
244   int byte_alignment;
245   int skip_loop_filter;
246 
247   // External BufferPool passed from outside.
248   BufferPool *buffer_pool;
249 
250   PARTITION_CONTEXT *above_seg_context;
251   ENTROPY_CONTEXT *above_context;
252   int above_context_alloc_cols;
253 
254   int lf_row;
255 } VP9_COMMON;
256 
257 typedef struct {
258   int frame_width;
259   int frame_height;
260   int render_frame_width;
261   int render_frame_height;
262   int mi_rows;
263   int mi_cols;
264   int mb_rows;
265   int mb_cols;
266   int num_mbs;
267   vpx_bit_depth_t bit_depth;
268 } FRAME_INFO;
269 
init_frame_info(FRAME_INFO * frame_info,const VP9_COMMON * cm)270 static INLINE void init_frame_info(FRAME_INFO *frame_info,
271                                    const VP9_COMMON *cm) {
272   frame_info->frame_width = cm->width;
273   frame_info->frame_height = cm->height;
274   frame_info->render_frame_width = cm->render_width;
275   frame_info->render_frame_height = cm->render_height;
276   frame_info->mi_cols = cm->mi_cols;
277   frame_info->mi_rows = cm->mi_rows;
278   frame_info->mb_cols = cm->mb_cols;
279   frame_info->mb_rows = cm->mb_rows;
280   frame_info->num_mbs = cm->MBs;
281   frame_info->bit_depth = cm->bit_depth;
282   // TODO(angiebird): Figure out how to get subsampling_x/y here
283 }
284 
get_buf_frame(VP9_COMMON * cm,int index)285 static INLINE YV12_BUFFER_CONFIG *get_buf_frame(VP9_COMMON *cm, int index) {
286   if (index < 0 || index >= FRAME_BUFFERS) return NULL;
287   if (cm->error.error_code != VPX_CODEC_OK) return NULL;
288   return &cm->buffer_pool->frame_bufs[index].buf;
289 }
290 
get_ref_frame(VP9_COMMON * cm,int index)291 static INLINE YV12_BUFFER_CONFIG *get_ref_frame(VP9_COMMON *cm, int index) {
292   if (index < 0 || index >= REF_FRAMES) return NULL;
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 
get_frame_new_buffer(VP9_COMMON * cm)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 
get_free_fb(VP9_COMMON * cm)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   for (i = 0; i < FRAME_BUFFERS; ++i)
307     if (frame_bufs[i].ref_count == 0) break;
308 
309   if (i != FRAME_BUFFERS) {
310     frame_bufs[i].ref_count = 1;
311   } else {
312     // Reset i to be INVALID_IDX to indicate no free buffer found.
313     i = INVALID_IDX;
314   }
315 
316   return i;
317 }
318 
ref_cnt_fb(RefCntBuffer * bufs,int * idx,int new_idx)319 static INLINE void ref_cnt_fb(RefCntBuffer *bufs, int *idx, int new_idx) {
320   const int ref_index = *idx;
321 
322   if (ref_index >= 0 && bufs[ref_index].ref_count > 0)
323     bufs[ref_index].ref_count--;
324 
325   *idx = new_idx;
326 
327   bufs[new_idx].ref_count++;
328 }
329 
mi_cols_aligned_to_sb(int n_mis)330 static INLINE int mi_cols_aligned_to_sb(int n_mis) {
331   return ALIGN_POWER_OF_TWO(n_mis, MI_BLOCK_SIZE_LOG2);
332 }
333 
frame_is_intra_only(const VP9_COMMON * const cm)334 static INLINE int frame_is_intra_only(const VP9_COMMON *const cm) {
335   return cm->frame_type == KEY_FRAME || cm->intra_only;
336 }
337 
set_partition_probs(const VP9_COMMON * const cm,MACROBLOCKD * const xd)338 static INLINE void set_partition_probs(const VP9_COMMON *const cm,
339                                        MACROBLOCKD *const xd) {
340   xd->partition_probs =
341       frame_is_intra_only(cm)
342           ? &vp9_kf_partition_probs[0]
343           : (const vpx_prob(*)[PARTITION_TYPES - 1]) cm->fc->partition_prob;
344 }
345 
vp9_init_macroblockd(VP9_COMMON * cm,MACROBLOCKD * xd,tran_low_t * dqcoeff)346 static INLINE void vp9_init_macroblockd(VP9_COMMON *cm, MACROBLOCKD *xd,
347                                         tran_low_t *dqcoeff) {
348   int i;
349 
350   for (i = 0; i < MAX_MB_PLANE; ++i) {
351     xd->plane[i].dqcoeff = dqcoeff;
352     xd->above_context[i] =
353         cm->above_context +
354         i * sizeof(*cm->above_context) * 2 * mi_cols_aligned_to_sb(cm->mi_cols);
355 
356     if (get_plane_type(i) == PLANE_TYPE_Y) {
357       memcpy(xd->plane[i].seg_dequant, cm->y_dequant, sizeof(cm->y_dequant));
358     } else {
359       memcpy(xd->plane[i].seg_dequant, cm->uv_dequant, sizeof(cm->uv_dequant));
360     }
361     xd->fc = cm->fc;
362   }
363 
364   xd->above_seg_context = cm->above_seg_context;
365   xd->mi_stride = cm->mi_stride;
366   xd->error_info = &cm->error;
367 
368   set_partition_probs(cm, xd);
369 }
370 
get_partition_probs(const MACROBLOCKD * xd,int ctx)371 static INLINE const vpx_prob *get_partition_probs(const MACROBLOCKD *xd,
372                                                   int ctx) {
373   return xd->partition_probs[ctx];
374 }
375 
set_skip_context(MACROBLOCKD * xd,int mi_row,int mi_col)376 static INLINE void set_skip_context(MACROBLOCKD *xd, int mi_row, int mi_col) {
377   const int above_idx = mi_col * 2;
378   const int left_idx = (mi_row * 2) & 15;
379   int i;
380   for (i = 0; i < MAX_MB_PLANE; ++i) {
381     struct macroblockd_plane *const pd = &xd->plane[i];
382     pd->above_context = &xd->above_context[i][above_idx >> pd->subsampling_x];
383     pd->left_context = &xd->left_context[i][left_idx >> pd->subsampling_y];
384   }
385 }
386 
calc_mi_size(int len)387 static INLINE int calc_mi_size(int len) {
388   // len is in mi units.
389   return len + MI_BLOCK_SIZE;
390 }
391 
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)392 static INLINE void set_mi_row_col(MACROBLOCKD *xd, const TileInfo *const tile,
393                                   int mi_row, int bh, int mi_col, int bw,
394                                   int mi_rows, int mi_cols) {
395   xd->mb_to_top_edge = -((mi_row * MI_SIZE) * 8);
396   xd->mb_to_bottom_edge = ((mi_rows - bh - mi_row) * MI_SIZE) * 8;
397   xd->mb_to_left_edge = -((mi_col * MI_SIZE) * 8);
398   xd->mb_to_right_edge = ((mi_cols - bw - mi_col) * MI_SIZE) * 8;
399 
400   // Are edges available for intra prediction?
401   xd->above_mi = (mi_row != 0) ? xd->mi[-xd->mi_stride] : NULL;
402   xd->left_mi = (mi_col > tile->mi_col_start) ? xd->mi[-1] : NULL;
403 }
404 
update_partition_context(MACROBLOCKD * xd,int mi_row,int mi_col,BLOCK_SIZE subsize,BLOCK_SIZE bsize)405 static INLINE void update_partition_context(MACROBLOCKD *xd, int mi_row,
406                                             int mi_col, BLOCK_SIZE subsize,
407                                             BLOCK_SIZE bsize) {
408   PARTITION_CONTEXT *const above_ctx = xd->above_seg_context + mi_col;
409   PARTITION_CONTEXT *const left_ctx = xd->left_seg_context + (mi_row & MI_MASK);
410 
411   // num_4x4_blocks_wide_lookup[bsize] / 2
412   const int bs = num_8x8_blocks_wide_lookup[bsize];
413 
414   // update the partition context at the end notes. set partition bits
415   // of block sizes larger than the current one to be one, and partition
416   // bits of smaller block sizes to be zero.
417   memset(above_ctx, partition_context_lookup[subsize].above, bs);
418   memset(left_ctx, partition_context_lookup[subsize].left, bs);
419 }
420 
partition_plane_context(const MACROBLOCKD * xd,int mi_row,int mi_col,BLOCK_SIZE bsize)421 static INLINE int partition_plane_context(const MACROBLOCKD *xd, int mi_row,
422                                           int mi_col, BLOCK_SIZE bsize) {
423   const PARTITION_CONTEXT *above_ctx = xd->above_seg_context + mi_col;
424   const PARTITION_CONTEXT *left_ctx = xd->left_seg_context + (mi_row & MI_MASK);
425   const int bsl = mi_width_log2_lookup[bsize];
426   int above = (*above_ctx >> bsl) & 1, left = (*left_ctx >> bsl) & 1;
427 
428   assert(b_width_log2_lookup[bsize] == b_height_log2_lookup[bsize]);
429   assert(bsl >= 0);
430 
431   return (left * 2 + above) + bsl * PARTITION_PLOFFSET;
432 }
433 
434 #ifdef __cplusplus
435 }  // extern "C"
436 #endif
437 
438 #endif  // VPX_VP9_COMMON_VP9_ONYXC_INT_H_
439