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