vp9_onyxc_int.h 14 KB

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