Merge "configure: add --extra-cxxflags option"
[platform/upstream/libvpx.git] / vp10 / common / 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 VP10_COMMON_ONYXC_INT_H_
12 #define VP10_COMMON_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 "./vp10_rtcd.h"
18 #include "vp10/common/alloccommon.h"
19 #include "vp10/common/loopfilter.h"
20 #include "vp10/common/entropymv.h"
21 #include "vp10/common/entropy.h"
22 #include "vp10/common/entropymode.h"
23 #include "vp10/common/frame_buffers.h"
24 #include "vp10/common/quant_common.h"
25 #include "vp10/common/tile_common.h"
26
27 #if CONFIG_VP9_POSTPROC
28 #include "vp10/common/postproc.h"
29 #endif
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 #define REFS_PER_FRAME (ALTREF_FRAME - LAST_FRAME + 1)
36
37 #define REF_FRAMES_LOG2 3
38 #define REF_FRAMES (1 << REF_FRAMES_LOG2)
39
40 // 4 scratch frames for the new frames to support a maximum of 4 cores decoding
41 // in parallel, 3 for scaled references on the encoder.
42 // TODO(hkuang): Add ondemand frame buffers instead of hardcoding the number
43 // of framebuffers.
44 // TODO(jkoleszar): These 3 extra references could probably come from the
45 // normal reference pool.
46 #define FRAME_BUFFERS (REF_FRAMES + 7)
47
48 #define FRAME_CONTEXTS_LOG2 2
49 #define FRAME_CONTEXTS (1 << FRAME_CONTEXTS_LOG2)
50
51 #define NUM_PING_PONG_BUFFERS 2
52
53 typedef enum {
54   SINGLE_REFERENCE      = 0,
55   COMPOUND_REFERENCE    = 1,
56   REFERENCE_MODE_SELECT = 2,
57   REFERENCE_MODES       = 3,
58 } REFERENCE_MODE;
59
60 typedef enum {
61   RESET_FRAME_CONTEXT_NONE = 0,
62   RESET_FRAME_CONTEXT_CURRENT = 1,
63   RESET_FRAME_CONTEXT_ALL = 2,
64 } RESET_FRAME_CONTEXT_MODE;
65
66 typedef enum {
67   /**
68    * Don't update frame context
69    */
70   REFRESH_FRAME_CONTEXT_OFF,
71   /**
72    * Update frame context to values resulting from forward probability
73    * updates signaled in the frame header
74    */
75   REFRESH_FRAME_CONTEXT_FORWARD,
76   /**
77    * Update frame context to values resulting from backward probability
78    * updates based on entropy/counts in the decoded frame
79    */
80   REFRESH_FRAME_CONTEXT_BACKWARD,
81 } REFRESH_FRAME_CONTEXT_MODE;
82
83 typedef struct {
84   int_mv mv[2];
85   MV_REFERENCE_FRAME ref_frame[2];
86 } MV_REF;
87
88 typedef struct {
89   int ref_count;
90   MV_REF *mvs;
91   int mi_rows;
92   int mi_cols;
93   vpx_codec_frame_buffer_t raw_frame_buffer;
94   YV12_BUFFER_CONFIG buf;
95
96   // The Following variables will only be used in frame parallel decode.
97
98   // frame_worker_owner indicates which FrameWorker owns this buffer. NULL means
99   // that no FrameWorker owns, or is decoding, this buffer.
100   VPxWorker *frame_worker_owner;
101
102   // row and col indicate which position frame has been decoded to in real
103   // pixel unit. They are reset to -1 when decoding begins and set to INT_MAX
104   // when the frame is fully decoded.
105   int row;
106   int col;
107 } RefCntBuffer;
108
109 typedef struct BufferPool {
110   // Protect BufferPool from being accessed by several FrameWorkers at
111   // the same time during frame parallel decode.
112   // TODO(hkuang): Try to use atomic variable instead of locking the whole pool.
113 #if CONFIG_MULTITHREAD
114   pthread_mutex_t pool_mutex;
115 #endif
116
117   // Private data associated with the frame buffer callbacks.
118   void *cb_priv;
119
120   vpx_get_frame_buffer_cb_fn_t get_fb_cb;
121   vpx_release_frame_buffer_cb_fn_t release_fb_cb;
122
123   RefCntBuffer frame_bufs[FRAME_BUFFERS];
124
125   // Frame buffers allocated internally by the codec.
126   InternalFrameBufferList int_frame_buffers;
127 } BufferPool;
128
129 typedef struct VP10Common {
130   struct vpx_internal_error_info  error;
131   vpx_color_space_t color_space;
132   int color_range;
133   int width;
134   int height;
135   int display_width;
136   int display_height;
137   int last_width;
138   int last_height;
139
140   // TODO(jkoleszar): this implies chroma ss right now, but could vary per
141   // plane. Revisit as part of the future change to YV12_BUFFER_CONFIG to
142   // support additional planes.
143   int subsampling_x;
144   int subsampling_y;
145
146 #if CONFIG_VP9_HIGHBITDEPTH
147   int use_highbitdepth;  // Marks if we need to use 16bit frame buffers.
148 #endif
149
150   YV12_BUFFER_CONFIG *frame_to_show;
151   RefCntBuffer *prev_frame;
152
153   // TODO(hkuang): Combine this with cur_buf in macroblockd.
154   RefCntBuffer *cur_frame;
155
156   int ref_frame_map[REF_FRAMES]; /* maps fb_idx to reference slot */
157
158   // Prepare ref_frame_map for the next frame.
159   // Only used in frame parallel decode.
160   int next_ref_frame_map[REF_FRAMES];
161
162   // TODO(jkoleszar): could expand active_ref_idx to 4, with 0 as intra, and
163   // roll new_fb_idx into it.
164
165   // Each frame can reference REFS_PER_FRAME buffers
166   RefBuffer frame_refs[REFS_PER_FRAME];
167
168   int new_fb_idx;
169
170 #if CONFIG_VP9_POSTPROC
171   YV12_BUFFER_CONFIG post_proc_buffer;
172   YV12_BUFFER_CONFIG post_proc_buffer_int;
173 #endif
174
175   FRAME_TYPE last_frame_type;  /* last frame's frame type for motion search.*/
176   FRAME_TYPE frame_type;
177
178   int show_frame;
179   int last_show_frame;
180   int show_existing_frame;
181
182   // Flag signaling that the frame is encoded using only INTRA modes.
183   uint8_t intra_only;
184   uint8_t last_intra_only;
185
186   int allow_high_precision_mv;
187
188   // Flag signaling which frame contexts should be reset to default values.
189   RESET_FRAME_CONTEXT_MODE reset_frame_context;
190
191   // MBs, mb_rows/cols is in 16-pixel units; mi_rows/cols is in
192   // MODE_INFO (8-pixel) units.
193   int MBs;
194   int mb_rows, mi_rows;
195   int mb_cols, mi_cols;
196   int mi_stride;
197
198   /* profile settings */
199   TX_MODE tx_mode;
200
201   int base_qindex;
202   int y_dc_delta_q;
203   int uv_dc_delta_q;
204   int uv_ac_delta_q;
205   int16_t y_dequant[MAX_SEGMENTS][2];
206   int16_t uv_dequant[MAX_SEGMENTS][2];
207
208   /* We allocate a MODE_INFO struct for each macroblock, together with
209      an extra row on top and column on the left to simplify prediction. */
210   int mi_alloc_size;
211   MODE_INFO *mip; /* Base of allocated array */
212   MODE_INFO *mi;  /* Corresponds to upper left visible macroblock */
213
214   // TODO(agrange): Move prev_mi into encoder structure.
215   // prev_mip and prev_mi will only be allocated in VP9 encoder.
216   MODE_INFO *prev_mip; /* MODE_INFO array 'mip' from last decoded frame */
217   MODE_INFO *prev_mi;  /* 'mi' from last frame (points into prev_mip) */
218
219   // Separate mi functions between encoder and decoder.
220   int (*alloc_mi)(struct VP10Common *cm, int mi_size);
221   void (*free_mi)(struct VP10Common *cm);
222   void (*setup_mi)(struct VP10Common *cm);
223
224   // Grid of pointers to 8x8 MODE_INFO structs.  Any 8x8 not in the visible
225   // area will be NULL.
226   MODE_INFO **mi_grid_base;
227   MODE_INFO **mi_grid_visible;
228   MODE_INFO **prev_mi_grid_base;
229   MODE_INFO **prev_mi_grid_visible;
230
231   // Whether to use previous frame's motion vectors for prediction.
232   int use_prev_frame_mvs;
233
234   // Persistent mb segment id map used in prediction.
235   int seg_map_idx;
236   int prev_seg_map_idx;
237
238   uint8_t *seg_map_array[NUM_PING_PONG_BUFFERS];
239   uint8_t *last_frame_seg_map;
240   uint8_t *current_frame_seg_map;
241   int seg_map_alloc_size;
242
243   INTERP_FILTER interp_filter;
244
245   loop_filter_info_n lf_info;
246
247   // Flag signaling how frame contexts should be updated at the end of
248   // a frame decode
249   REFRESH_FRAME_CONTEXT_MODE refresh_frame_context;
250
251   int ref_frame_sign_bias[MAX_REF_FRAMES];    /* Two state 0, 1 */
252
253   struct loopfilter lf;
254   struct segmentation seg;
255
256   int frame_parallel_decode;  // frame-based threading.
257
258   // Context probabilities for reference frame prediction
259   MV_REFERENCE_FRAME comp_fixed_ref;
260   MV_REFERENCE_FRAME comp_var_ref[2];
261   REFERENCE_MODE reference_mode;
262
263   FRAME_CONTEXT *fc;  /* this frame entropy */
264   FRAME_CONTEXT *frame_contexts;   // FRAME_CONTEXTS
265   unsigned int  frame_context_idx; /* Context to use/update */
266   FRAME_COUNTS counts;
267
268   unsigned int current_video_frame;
269   BITSTREAM_PROFILE profile;
270
271   // VPX_BITS_8 in profile 0 or 1, VPX_BITS_10 or VPX_BITS_12 in profile 2 or 3.
272   vpx_bit_depth_t bit_depth;
273   vpx_bit_depth_t dequant_bit_depth;  // bit_depth of current dequantizer
274
275 #if CONFIG_VP9_POSTPROC
276   struct postproc_state  postproc_state;
277 #endif
278
279   int error_resilient_mode;
280
281   int log2_tile_cols, log2_tile_rows;
282   int byte_alignment;
283   int skip_loop_filter;
284
285   // Private data associated with the frame buffer callbacks.
286   void *cb_priv;
287   vpx_get_frame_buffer_cb_fn_t get_fb_cb;
288   vpx_release_frame_buffer_cb_fn_t release_fb_cb;
289
290   // Handles memory for the codec.
291   InternalFrameBufferList int_frame_buffers;
292
293   // External BufferPool passed from outside.
294   BufferPool *buffer_pool;
295
296   PARTITION_CONTEXT *above_seg_context;
297   ENTROPY_CONTEXT *above_context;
298   int above_context_alloc_cols;
299 } VP10_COMMON;
300
301 // TODO(hkuang): Don't need to lock the whole pool after implementing atomic
302 // frame reference count.
303 static void lock_buffer_pool(BufferPool *const pool) {
304 #if CONFIG_MULTITHREAD
305   pthread_mutex_lock(&pool->pool_mutex);
306 #else
307   (void)pool;
308 #endif
309 }
310
311 static void unlock_buffer_pool(BufferPool *const pool) {
312 #if CONFIG_MULTITHREAD
313   pthread_mutex_unlock(&pool->pool_mutex);
314 #else
315   (void)pool;
316 #endif
317 }
318
319 static INLINE YV12_BUFFER_CONFIG *get_ref_frame(VP10_COMMON *cm, int index) {
320   if (index < 0 || index >= REF_FRAMES)
321     return NULL;
322   if (cm->ref_frame_map[index] < 0)
323     return NULL;
324   assert(cm->ref_frame_map[index] < FRAME_BUFFERS);
325   return &cm->buffer_pool->frame_bufs[cm->ref_frame_map[index]].buf;
326 }
327
328 static INLINE YV12_BUFFER_CONFIG *get_frame_new_buffer(VP10_COMMON *cm) {
329   return &cm->buffer_pool->frame_bufs[cm->new_fb_idx].buf;
330 }
331
332 static INLINE int get_free_fb(VP10_COMMON *cm) {
333   RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
334   int i;
335
336   lock_buffer_pool(cm->buffer_pool);
337   for (i = 0; i < FRAME_BUFFERS; ++i)
338     if (frame_bufs[i].ref_count == 0)
339       break;
340
341   if (i != FRAME_BUFFERS) {
342     frame_bufs[i].ref_count = 1;
343   } else {
344     // Reset i to be INVALID_IDX to indicate no free buffer found.
345     i = INVALID_IDX;
346   }
347
348   unlock_buffer_pool(cm->buffer_pool);
349   return i;
350 }
351
352 static INLINE void ref_cnt_fb(RefCntBuffer *bufs, int *idx, int new_idx) {
353   const int ref_index = *idx;
354
355   if (ref_index >= 0 && bufs[ref_index].ref_count > 0)
356     bufs[ref_index].ref_count--;
357
358   *idx = new_idx;
359
360   bufs[new_idx].ref_count++;
361 }
362
363 static INLINE int mi_cols_aligned_to_sb(int n_mis) {
364   return ALIGN_POWER_OF_TWO(n_mis, MI_BLOCK_SIZE_LOG2);
365 }
366
367 static INLINE int frame_is_intra_only(const VP10_COMMON *const cm) {
368   return cm->frame_type == KEY_FRAME || cm->intra_only;
369 }
370
371 static INLINE void set_partition_probs(const VP10_COMMON *const cm,
372                                        MACROBLOCKD *const xd) {
373   xd->partition_probs =
374       frame_is_intra_only(cm) ?
375           &vp10_kf_partition_probs[0] :
376           (const vpx_prob (*)[PARTITION_TYPES - 1])cm->fc->partition_prob;
377 }
378
379 static INLINE void vp10_init_macroblockd(VP10_COMMON *cm, MACROBLOCKD *xd,
380                                         tran_low_t *dqcoeff) {
381   int i;
382
383   for (i = 0; i < MAX_MB_PLANE; ++i) {
384     xd->plane[i].dqcoeff = dqcoeff;
385     xd->above_context[i] = cm->above_context +
386         i * sizeof(*cm->above_context) * 2 * mi_cols_aligned_to_sb(cm->mi_cols);
387
388     if (xd->plane[i].plane_type == PLANE_TYPE_Y) {
389       memcpy(xd->plane[i].seg_dequant, cm->y_dequant, sizeof(cm->y_dequant));
390     } else {
391       memcpy(xd->plane[i].seg_dequant, cm->uv_dequant, sizeof(cm->uv_dequant));
392     }
393     xd->fc = cm->fc;
394   }
395
396   xd->above_seg_context = cm->above_seg_context;
397   xd->mi_stride = cm->mi_stride;
398   xd->error_info = &cm->error;
399
400   set_partition_probs(cm, xd);
401 }
402
403 static INLINE const vpx_prob* get_partition_probs(const MACROBLOCKD *xd,
404                                                   int ctx) {
405   return xd->partition_probs[ctx];
406 }
407
408 static INLINE void set_skip_context(MACROBLOCKD *xd, int mi_row, int mi_col) {
409   const int above_idx = mi_col * 2;
410   const int left_idx = (mi_row * 2) & 15;
411   int i;
412   for (i = 0; i < MAX_MB_PLANE; ++i) {
413     struct macroblockd_plane *const pd = &xd->plane[i];
414     pd->above_context = &xd->above_context[i][above_idx >> pd->subsampling_x];
415     pd->left_context = &xd->left_context[i][left_idx >> pd->subsampling_y];
416   }
417 }
418
419 static INLINE int calc_mi_size(int len) {
420   // len is in mi units.
421   return len + MI_BLOCK_SIZE;
422 }
423
424 static INLINE void set_mi_row_col(MACROBLOCKD *xd, const TileInfo *const tile,
425                                   int mi_row, int bh,
426                                   int mi_col, int bw,
427                                   int mi_rows, int mi_cols) {
428   xd->mb_to_top_edge    = -((mi_row * MI_SIZE) * 8);
429   xd->mb_to_bottom_edge = ((mi_rows - bh - mi_row) * MI_SIZE) * 8;
430   xd->mb_to_left_edge   = -((mi_col * MI_SIZE) * 8);
431   xd->mb_to_right_edge  = ((mi_cols - bw - mi_col) * MI_SIZE) * 8;
432
433   // Are edges available for intra prediction?
434   xd->up_available    = (mi_row != 0);
435   xd->left_available  = (mi_col > tile->mi_col_start);
436   if (xd->up_available) {
437     xd->above_mi = xd->mi[-xd->mi_stride];
438     // above_mi may be NULL in VP9 encoder's first pass.
439     xd->above_mbmi = xd->above_mi ? &xd->above_mi->mbmi : NULL;
440   } else {
441     xd->above_mi = NULL;
442     xd->above_mbmi = NULL;
443   }
444
445   if (xd->left_available) {
446     xd->left_mi = xd->mi[-1];
447     // left_mi may be NULL in VP9 encoder's first pass.
448     xd->left_mbmi = xd->left_mi ? &xd->left_mi->mbmi : NULL;
449   } else {
450     xd->left_mi = NULL;
451     xd->left_mbmi = NULL;
452   }
453 }
454
455 static INLINE void update_partition_context(MACROBLOCKD *xd,
456                                             int mi_row, int mi_col,
457                                             BLOCK_SIZE subsize,
458                                             BLOCK_SIZE bsize) {
459   PARTITION_CONTEXT *const above_ctx = xd->above_seg_context + mi_col;
460   PARTITION_CONTEXT *const left_ctx = xd->left_seg_context + (mi_row & MI_MASK);
461
462   // num_4x4_blocks_wide_lookup[bsize] / 2
463   const int bs = num_8x8_blocks_wide_lookup[bsize];
464
465   // update the partition context at the end notes. set partition bits
466   // of block sizes larger than the current one to be one, and partition
467   // bits of smaller block sizes to be zero.
468   memset(above_ctx, partition_context_lookup[subsize].above, bs);
469   memset(left_ctx, partition_context_lookup[subsize].left, bs);
470 }
471
472 static INLINE int partition_plane_context(const MACROBLOCKD *xd,
473                                           int mi_row, int mi_col,
474                                           BLOCK_SIZE bsize) {
475   const PARTITION_CONTEXT *above_ctx = xd->above_seg_context + mi_col;
476   const PARTITION_CONTEXT *left_ctx = xd->left_seg_context + (mi_row & MI_MASK);
477   const int bsl = mi_width_log2_lookup[bsize];
478   int above = (*above_ctx >> bsl) & 1 , left = (*left_ctx >> bsl) & 1;
479
480   assert(b_width_log2_lookup[bsize] == b_height_log2_lookup[bsize]);
481   assert(bsl >= 0);
482
483   return (left * 2 + above) + bsl * PARTITION_PLOFFSET;
484 }
485
486 #ifdef __cplusplus
487 }  // extern "C"
488 #endif
489
490 #endif  // VP10_COMMON_ONYXC_INT_H_