vp9_onyxc_int.h 12 KB

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