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