]> granicus.if.org Git - libvpx/blob - vp9/common/vp9_blockd.h
2ca9898c51dff65ea57dea6b416714a95362f4f9
[libvpx] / vp9 / common / vp9_blockd.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
12 #ifndef VP9_COMMON_VP9_BLOCKD_H_
13 #define VP9_COMMON_VP9_BLOCKD_H_
14
15 #include "./vpx_config.h"
16
17 #include "vpx_ports/mem.h"
18 #include "vpx_scale/yv12config.h"
19
20 #include "vp9/common/vp9_common.h"
21 #include "vp9/common/vp9_common_data.h"
22 #include "vp9/common/vp9_convolve.h"
23 #include "vp9/common/vp9_enums.h"
24 #include "vp9/common/vp9_mv.h"
25 #include "vp9/common/vp9_seg_common.h"
26 #include "vp9/common/vp9_treecoder.h"
27
28 #define BLOCK_SIZE_GROUPS   4
29
30 #define PREDICTION_PROBS 3
31
32 #define MBSKIP_CONTEXTS 3
33
34 #define MAX_REF_LF_DELTAS       4
35 #define MAX_MODE_LF_DELTAS      2
36
37 /* Segment Feature Masks */
38 #define MAX_MV_REF_CANDIDATES 2
39
40 #define INTRA_INTER_CONTEXTS 4
41 #define COMP_INTER_CONTEXTS 5
42 #define REF_CONTEXTS 5
43
44 typedef enum {
45   PLANE_TYPE_Y_WITH_DC,
46   PLANE_TYPE_UV,
47 } PLANE_TYPE;
48
49 typedef char ENTROPY_CONTEXT;
50
51 typedef char PARTITION_CONTEXT;
52
53 static INLINE int combine_entropy_contexts(ENTROPY_CONTEXT a,
54                                            ENTROPY_CONTEXT b) {
55   return (a != 0) + (b != 0);
56 }
57
58 typedef enum {
59   KEY_FRAME = 0,
60   INTER_FRAME = 1,
61   NUM_FRAME_TYPES,
62 } FRAME_TYPE;
63
64 typedef enum {
65   EIGHTTAP_SMOOTH,
66   EIGHTTAP,
67   EIGHTTAP_SHARP,
68   BILINEAR,
69   SWITCHABLE  /* should be the last one */
70 } INTERPOLATIONFILTERTYPE;
71
72 typedef enum {
73   DC_PRED,         // Average of above and left pixels
74   V_PRED,          // Vertical
75   H_PRED,          // Horizontal
76   D45_PRED,        // Directional 45  deg = round(arctan(1/1) * 180/pi)
77   D135_PRED,       // Directional 135 deg = 180 - 45
78   D117_PRED,       // Directional 117 deg = 180 - 63
79   D153_PRED,       // Directional 153 deg = 180 - 27
80   D27_PRED,        // Directional 27  deg = round(arctan(1/2) * 180/pi)
81   D63_PRED,        // Directional 63  deg = round(arctan(2/1) * 180/pi)
82   TM_PRED,         // True-motion
83   NEARESTMV,
84   NEARMV,
85   ZEROMV,
86   NEWMV,
87   MB_MODE_COUNT
88 } MB_PREDICTION_MODE;
89
90 static INLINE int is_intra_mode(MB_PREDICTION_MODE mode) {
91   return mode <= TM_PRED;
92 }
93
94 static INLINE int is_inter_mode(MB_PREDICTION_MODE mode) {
95   return mode >= NEARESTMV && mode <= NEWMV;
96 }
97
98 #define VP9_INTRA_MODES (TM_PRED + 1)
99
100 #define VP9_INTER_MODES (1 + NEWMV - NEARESTMV)
101
102 /* For keyframes, intra block modes are predicted by the (already decoded)
103    modes for the Y blocks to the left and above us; for interframes, there
104    is a single probability table. */
105
106 union b_mode_info {
107   MB_PREDICTION_MODE as_mode;
108   int_mv as_mv[2];  // first, second inter predictor motion vectors
109 };
110
111 typedef enum {
112   NONE = -1,
113   INTRA_FRAME = 0,
114   LAST_FRAME = 1,
115   GOLDEN_FRAME = 2,
116   ALTREF_FRAME = 3,
117   MAX_REF_FRAMES = 4
118 } MV_REFERENCE_FRAME;
119
120 static INLINE int b_width_log2(BLOCK_SIZE_TYPE sb_type) {
121   return b_width_log2_lookup[sb_type];
122 }
123 static INLINE int b_height_log2(BLOCK_SIZE_TYPE sb_type) {
124   return b_height_log2_lookup[sb_type];
125 }
126
127 static INLINE int mi_width_log2(BLOCK_SIZE_TYPE sb_type) {
128   return mi_width_log2_lookup[sb_type];
129 }
130
131 static INLINE int mi_height_log2(BLOCK_SIZE_TYPE sb_type) {
132   return mi_height_log2_lookup[sb_type];
133 }
134
135 typedef struct {
136   MB_PREDICTION_MODE mode, uv_mode;
137   MV_REFERENCE_FRAME ref_frame[2];
138   TX_SIZE txfm_size;
139   int_mv mv[2]; // for each reference frame used
140   int_mv ref_mvs[MAX_REF_FRAMES][MAX_MV_REF_CANDIDATES];
141   int_mv best_mv, best_second_mv;
142
143   uint8_t mb_mode_context[MAX_REF_FRAMES];
144
145   unsigned char mb_skip_coeff;                                /* does this mb has coefficients at all, 1=no coefficients, 0=need decode tokens */
146   unsigned char segment_id;           // Segment id for current frame
147
148   // Flags used for prediction status of various bistream signals
149   unsigned char seg_id_predicted;
150
151   // Indicates if the mb is part of the image (1) vs border (0)
152   // This can be useful in determining whether the MB provides
153   // a valid predictor
154   unsigned char mb_in_image;
155
156   INTERPOLATIONFILTERTYPE interp_filter;
157
158   BLOCK_SIZE_TYPE sb_type;
159 } MB_MODE_INFO;
160
161 typedef struct {
162   MB_MODE_INFO mbmi;
163   union b_mode_info bmi[4];
164 } MODE_INFO;
165
166 enum mv_precision {
167   MV_PRECISION_Q3,
168   MV_PRECISION_Q4
169 };
170
171 #define VP9_REF_SCALE_SHIFT 14
172 #define VP9_REF_NO_SCALE 16384
173
174 struct scale_factors {
175   int x_scale_fp;   // horizontal fixed point scale factor
176   int y_scale_fp;   // vertical fixed point scale factor
177   int x_offset_q4;
178   int x_step_q4;
179   int y_offset_q4;
180   int y_step_q4;
181
182   int (*scale_value_x)(int val, const struct scale_factors *scale);
183   int (*scale_value_y)(int val, const struct scale_factors *scale);
184   void (*set_scaled_offsets)(struct scale_factors *scale, int row, int col);
185   MV32 (*scale_mv_q3_to_q4)(const MV *mv, const struct scale_factors *scale);
186   MV32 (*scale_mv_q4)(const MV *mv, const struct scale_factors *scale);
187
188   convolve_fn_t predict[2][2][2];  // horiz, vert, avg
189 };
190
191 #if CONFIG_ALPHA
192 enum { MAX_MB_PLANE = 4 };
193 #else
194 enum { MAX_MB_PLANE = 3 };
195 #endif
196
197 struct buf_2d {
198   uint8_t *buf;
199   int stride;
200 };
201
202 struct macroblockd_plane {
203   DECLARE_ALIGNED(16, int16_t,  qcoeff[64 * 64]);
204   DECLARE_ALIGNED(16, int16_t,  dqcoeff[64 * 64]);
205   DECLARE_ALIGNED(16, uint16_t, eobs[256]);
206   PLANE_TYPE plane_type;
207   int subsampling_x;
208   int subsampling_y;
209   struct buf_2d dst;
210   struct buf_2d pre[2];
211   int16_t *dequant;
212   ENTROPY_CONTEXT *above_context;
213   ENTROPY_CONTEXT *left_context;
214 };
215
216 #define BLOCK_OFFSET(x, i, n) ((x) + (i) * (n))
217
218 typedef struct macroblockd {
219   struct macroblockd_plane plane[MAX_MB_PLANE];
220
221   struct scale_factors scale_factor[2];
222   struct scale_factors scale_factor_uv[2];
223
224   MODE_INFO *prev_mode_info_context;
225   MODE_INFO *mode_info_context;
226   int mode_info_stride;
227
228   FRAME_TYPE frame_type;
229
230   int up_available;
231   int left_available;
232   int right_available;
233
234   struct segmentation seg;
235
236   // partition contexts
237   PARTITION_CONTEXT *above_seg_context;
238   PARTITION_CONTEXT *left_seg_context;
239
240   /* mode_based Loop filter adjustment */
241   unsigned char mode_ref_lf_delta_enabled;
242   unsigned char mode_ref_lf_delta_update;
243
244   /* Delta values have the range +/- MAX_LOOP_FILTER */
245   /* 0 = Intra, Last, GF, ARF */
246   signed char last_ref_lf_deltas[MAX_REF_LF_DELTAS];
247   /* 0 = Intra, Last, GF, ARF */
248   signed char ref_lf_deltas[MAX_REF_LF_DELTAS];
249
250   /* 0 = ZERO_MV, MV */
251   signed char last_mode_lf_deltas[MAX_MODE_LF_DELTAS];
252   /* 0 = ZERO_MV, MV */
253   signed char mode_lf_deltas[MAX_MODE_LF_DELTAS];
254
255   /* Distance of MB away from frame edges */
256   int mb_to_left_edge;
257   int mb_to_right_edge;
258   int mb_to_top_edge;
259   int mb_to_bottom_edge;
260
261   unsigned int frames_since_golden;
262   unsigned int frames_till_alt_ref_frame;
263
264   int lossless;
265   /* Inverse transform function pointers. */
266   void (*inv_txm4x4_1_add)(int16_t *input, uint8_t *dest, int stride);
267   void (*inv_txm4x4_add)(int16_t *input, uint8_t *dest, int stride);
268   void (*itxm_add)(int16_t *input, uint8_t *dest, int stride, int eob);
269
270   struct subpix_fn_table  subpix;
271
272   int allow_high_precision_mv;
273
274   int corrupted;
275
276   int sb_index;   // index of 32x32 block inside the 64x64 block
277   int mb_index;   // index of 16x16 block inside the 32x32 block
278   int b_index;    // index of 8x8 block inside the 16x16 block
279   int ab_index;   // index of 4x4 block inside the 8x8 block
280   int q_index;
281
282 } MACROBLOCKD;
283
284 static INLINE int *get_sb_index(MACROBLOCKD *xd, BLOCK_SIZE_TYPE subsize) {
285   switch (subsize) {
286     case BLOCK_SIZE_SB64X64:
287     case BLOCK_SIZE_SB64X32:
288     case BLOCK_SIZE_SB32X64:
289     case BLOCK_SIZE_SB32X32:
290       return &xd->sb_index;
291     case BLOCK_SIZE_SB32X16:
292     case BLOCK_SIZE_SB16X32:
293     case BLOCK_SIZE_MB16X16:
294       return &xd->mb_index;
295     case BLOCK_SIZE_SB16X8:
296     case BLOCK_SIZE_SB8X16:
297     case BLOCK_SIZE_SB8X8:
298       return &xd->b_index;
299     case BLOCK_SIZE_SB8X4:
300     case BLOCK_SIZE_SB4X8:
301     case BLOCK_SIZE_AB4X4:
302       return &xd->ab_index;
303     default:
304       assert(0);
305       return NULL;
306   }
307 }
308
309 static INLINE void update_partition_context(MACROBLOCKD *xd,
310                                             BLOCK_SIZE_TYPE sb_type,
311                                             BLOCK_SIZE_TYPE sb_size) {
312   const int bsl = b_width_log2(sb_size), bs = (1 << bsl) / 2;
313   const int bwl = b_width_log2(sb_type);
314   const int bhl = b_height_log2(sb_type);
315   const int boffset = b_width_log2(BLOCK_SIZE_SB64X64) - bsl;
316   const char pcval0 = ~(0xe << boffset);
317   const char pcval1 = ~(0xf << boffset);
318   const char pcvalue[2] = {pcval0, pcval1};
319
320   assert(MAX(bwl, bhl) <= bsl);
321
322   // update the partition context at the end notes. set partition bits
323   // of block sizes larger than the current one to be one, and partition
324   // bits of smaller block sizes to be zero.
325   vpx_memset(xd->above_seg_context, pcvalue[bwl == bsl], bs);
326   vpx_memset(xd->left_seg_context, pcvalue[bhl == bsl], bs);
327 }
328
329 static INLINE int partition_plane_context(MACROBLOCKD *xd,
330                                           BLOCK_SIZE_TYPE sb_type) {
331   int bsl = mi_width_log2(sb_type), bs = 1 << bsl;
332   int above = 0, left = 0, i;
333   int boffset = mi_width_log2(BLOCK_SIZE_SB64X64) - bsl;
334
335   assert(mi_width_log2(sb_type) == mi_height_log2(sb_type));
336   assert(bsl >= 0);
337   assert(boffset >= 0);
338
339   for (i = 0; i < bs; i++)
340     above |= (xd->above_seg_context[i] & (1 << boffset));
341   for (i = 0; i < bs; i++)
342     left |= (xd->left_seg_context[i] & (1 << boffset));
343
344   above = (above > 0);
345   left  = (left > 0);
346
347   return (left * 2 + above) + bsl * PARTITION_PLOFFSET;
348 }
349
350 static BLOCK_SIZE_TYPE get_subsize(BLOCK_SIZE_TYPE bsize,
351                                    PARTITION_TYPE partition) {
352   BLOCK_SIZE_TYPE subsize = subsize_lookup[partition][bsize];
353   assert(subsize != BLOCK_SIZE_TYPES);
354   return subsize;
355 }
356
357 extern const TX_TYPE mode2txfm_map[MB_MODE_COUNT];
358
359 static INLINE TX_TYPE get_tx_type_4x4(const MACROBLOCKD *xd, int ib) {
360   MODE_INFO *const mi = xd->mode_info_context;
361   MB_MODE_INFO *const mbmi = &mi->mbmi;
362
363   if (xd->lossless || mbmi->ref_frame[0] != INTRA_FRAME)
364     return DCT_DCT;
365
366   return mode2txfm_map[mbmi->sb_type < BLOCK_SIZE_SB8X8 ?
367                        mi->bmi[ib].as_mode : mbmi->mode];
368 }
369
370 static INLINE TX_TYPE get_tx_type_8x8(const MACROBLOCKD *xd) {
371   return mode2txfm_map[xd->mode_info_context->mbmi.mode];
372 }
373
374 static INLINE TX_TYPE get_tx_type_16x16(const MACROBLOCKD *xd) {
375   return  mode2txfm_map[xd->mode_info_context->mbmi.mode];
376 }
377
378 static void setup_block_dptrs(MACROBLOCKD *xd, int ss_x, int ss_y) {
379   int i;
380
381   for (i = 0; i < MAX_MB_PLANE; i++) {
382     xd->plane[i].plane_type = i ? PLANE_TYPE_UV : PLANE_TYPE_Y_WITH_DC;
383     xd->plane[i].subsampling_x = i ? ss_x : 0;
384     xd->plane[i].subsampling_y = i ? ss_y : 0;
385   }
386 #if CONFIG_ALPHA
387   // TODO(jkoleszar): Using the Y w/h for now
388   xd->plane[3].subsampling_x = 0;
389   xd->plane[3].subsampling_y = 0;
390 #endif
391 }
392
393
394 static INLINE TX_SIZE get_uv_tx_size(const MB_MODE_INFO *mbmi) {
395   const TX_SIZE size = mbmi->txfm_size;
396   const TX_SIZE max_size = max_uv_txsize_lookup[mbmi->sb_type];
397   return (size > max_size ? max_size : size);
398 }
399
400 struct plane_block_idx {
401   int plane;
402   int block;
403 };
404
405 // TODO(jkoleszar): returning a struct so it can be used in a const context,
406 // expect to refactor this further later.
407 static INLINE struct plane_block_idx plane_block_idx(int y_blocks,
408                                                      int b_idx) {
409   const int v_offset = y_blocks * 5 / 4;
410   struct plane_block_idx res;
411
412   if (b_idx < y_blocks) {
413     res.plane = 0;
414     res.block = b_idx;
415   } else if (b_idx < v_offset) {
416     res.plane = 1;
417     res.block = b_idx - y_blocks;
418   } else {
419     assert(b_idx < y_blocks * 3 / 2);
420     res.plane = 2;
421     res.block = b_idx - v_offset;
422   }
423   return res;
424 }
425
426 static INLINE int plane_block_width(BLOCK_SIZE_TYPE bsize,
427                                     const struct macroblockd_plane* plane) {
428   return 4 << (b_width_log2(bsize) - plane->subsampling_x);
429 }
430
431 static INLINE int plane_block_height(BLOCK_SIZE_TYPE bsize,
432                                      const struct macroblockd_plane* plane) {
433   return 4 << (b_height_log2(bsize) - plane->subsampling_y);
434 }
435
436 static INLINE int plane_block_width_log2by4(
437     BLOCK_SIZE_TYPE bsize, const struct macroblockd_plane* plane) {
438   return (b_width_log2(bsize) - plane->subsampling_x);
439 }
440
441 static INLINE int plane_block_height_log2by4(
442     BLOCK_SIZE_TYPE bsize, const struct macroblockd_plane* plane) {
443   return (b_height_log2(bsize) - plane->subsampling_y);
444 }
445
446 typedef void (*foreach_transformed_block_visitor)(int plane, int block,
447                                                   BLOCK_SIZE_TYPE bsize,
448                                                   int ss_txfrm_size,
449                                                   void *arg);
450
451 static INLINE void foreach_transformed_block_in_plane(
452     const MACROBLOCKD* const xd, BLOCK_SIZE_TYPE bsize, int plane,
453     foreach_transformed_block_visitor visit, void *arg) {
454   const int bw = b_width_log2(bsize), bh = b_height_log2(bsize);
455
456   // block and transform sizes, in number of 4x4 blocks log 2 ("*_b")
457   // 4x4=0, 8x8=2, 16x16=4, 32x32=6, 64x64=8
458   // transform size varies per plane, look it up in a common way.
459   const MB_MODE_INFO* mbmi = &xd->mode_info_context->mbmi;
460   const TX_SIZE tx_size = plane ? get_uv_tx_size(mbmi)
461                                 : mbmi->txfm_size;
462   const int block_size_b = bw + bh;
463   const int txfrm_size_b = tx_size * 2;
464
465   // subsampled size of the block
466   const int ss_sum = xd->plane[plane].subsampling_x
467       + xd->plane[plane].subsampling_y;
468   const int ss_block_size = block_size_b - ss_sum;
469
470   const int step = 1 << txfrm_size_b;
471
472   int i;
473
474   assert(txfrm_size_b <= block_size_b);
475   assert(txfrm_size_b <= ss_block_size);
476
477   // If mb_to_right_edge is < 0 we are in a situation in which
478   // the current block size extends into the UMV and we won't
479   // visit the sub blocks that are wholly within the UMV.
480   if (xd->mb_to_right_edge < 0 || xd->mb_to_bottom_edge < 0) {
481     int r, c;
482     const int sw = bw - xd->plane[plane].subsampling_x;
483     const int sh = bh - xd->plane[plane].subsampling_y;
484     int max_blocks_wide = 1 << sw;
485     int max_blocks_high = 1 << sh;
486
487     // xd->mb_to_right_edge is in units of pixels * 8.  This converts
488     // it to 4x4 block sizes.
489     if (xd->mb_to_right_edge < 0)
490       max_blocks_wide +=
491           + (xd->mb_to_right_edge >> (5 + xd->plane[plane].subsampling_x));
492
493     if (xd->mb_to_bottom_edge < 0)
494       max_blocks_high +=
495           + (xd->mb_to_bottom_edge >> (5 + xd->plane[plane].subsampling_y));
496
497     i = 0;
498     // Unlike the normal case - in here we have to keep track of the
499     // row and column of the blocks we use so that we know if we are in
500     // the unrestricted motion border..
501     for (r = 0; r < (1 << sh); r += (1 << tx_size)) {
502       for (c = 0; c < (1 << sw); c += (1 << tx_size)) {
503         if (r < max_blocks_high && c < max_blocks_wide)
504           visit(plane, i, bsize, txfrm_size_b, arg);
505         i += step;
506       }
507     }
508   } else {
509     for (i = 0; i < (1 << ss_block_size); i += step) {
510       visit(plane, i, bsize, txfrm_size_b, arg);
511     }
512   }
513 }
514
515 static INLINE void foreach_transformed_block(
516     const MACROBLOCKD* const xd, BLOCK_SIZE_TYPE bsize,
517     foreach_transformed_block_visitor visit, void *arg) {
518   int plane;
519
520   for (plane = 0; plane < MAX_MB_PLANE; plane++) {
521     foreach_transformed_block_in_plane(xd, bsize, plane,
522                                        visit, arg);
523   }
524 }
525
526 static INLINE void foreach_transformed_block_uv(
527     const MACROBLOCKD* const xd, BLOCK_SIZE_TYPE bsize,
528     foreach_transformed_block_visitor visit, void *arg) {
529   int plane;
530
531   for (plane = 1; plane < MAX_MB_PLANE; plane++) {
532     foreach_transformed_block_in_plane(xd, bsize, plane,
533                                        visit, arg);
534   }
535 }
536
537 // TODO(jkoleszar): In principle, pred_w, pred_h are unnecessary, as we could
538 // calculate the subsampled BLOCK_SIZE_TYPE, but that type isn't defined for
539 // sizes smaller than 16x16 yet.
540 typedef void (*foreach_predicted_block_visitor)(int plane, int block,
541                                                 BLOCK_SIZE_TYPE bsize,
542                                                 int pred_w, int pred_h,
543                                                 void *arg);
544 static INLINE void foreach_predicted_block_in_plane(
545     const MACROBLOCKD* const xd, BLOCK_SIZE_TYPE bsize, int plane,
546     foreach_predicted_block_visitor visit, void *arg) {
547   int i, x, y;
548
549   // block sizes in number of 4x4 blocks log 2 ("*_b")
550   // 4x4=0, 8x8=2, 16x16=4, 32x32=6, 64x64=8
551   // subsampled size of the block
552   const int bwl = b_width_log2(bsize) - xd->plane[plane].subsampling_x;
553   const int bhl = b_height_log2(bsize) - xd->plane[plane].subsampling_y;
554
555   // size of the predictor to use.
556   int pred_w, pred_h;
557
558   if (xd->mode_info_context->mbmi.sb_type < BLOCK_SIZE_SB8X8) {
559     assert(bsize == BLOCK_SIZE_SB8X8);
560     pred_w = 0;
561     pred_h = 0;
562   } else {
563     pred_w = bwl;
564     pred_h = bhl;
565   }
566   assert(pred_w <= bwl);
567   assert(pred_h <= bhl);
568
569   // visit each subblock in raster order
570   i = 0;
571   for (y = 0; y < 1 << bhl; y += 1 << pred_h) {
572     for (x = 0; x < 1 << bwl; x += 1 << pred_w) {
573       visit(plane, i, bsize, pred_w, pred_h, arg);
574       i += 1 << pred_w;
575     }
576     i += (1 << (bwl + pred_h)) - (1 << bwl);
577   }
578 }
579 static INLINE void foreach_predicted_block(
580     const MACROBLOCKD* const xd, BLOCK_SIZE_TYPE bsize,
581     foreach_predicted_block_visitor visit, void *arg) {
582   int plane;
583
584   for (plane = 0; plane < MAX_MB_PLANE; plane++) {
585     foreach_predicted_block_in_plane(xd, bsize, plane, visit, arg);
586   }
587 }
588 static INLINE void foreach_predicted_block_uv(
589     const MACROBLOCKD* const xd, BLOCK_SIZE_TYPE bsize,
590     foreach_predicted_block_visitor visit, void *arg) {
591   int plane;
592
593   for (plane = 1; plane < MAX_MB_PLANE; plane++) {
594     foreach_predicted_block_in_plane(xd, bsize, plane, visit, arg);
595   }
596 }
597 static int raster_block_offset(MACROBLOCKD *xd, BLOCK_SIZE_TYPE bsize,
598                                int plane, int block, int stride) {
599   const int bw = b_width_log2(bsize) - xd->plane[plane].subsampling_x;
600   const int y = 4 * (block >> bw), x = 4 * (block & ((1 << bw) - 1));
601   return y * stride + x;
602 }
603 static int16_t* raster_block_offset_int16(MACROBLOCKD *xd,
604                                          BLOCK_SIZE_TYPE bsize,
605                                          int plane, int block, int16_t *base) {
606   const int stride = plane_block_width(bsize, &xd->plane[plane]);
607   return base + raster_block_offset(xd, bsize, plane, block, stride);
608 }
609 static uint8_t* raster_block_offset_uint8(MACROBLOCKD *xd,
610                                          BLOCK_SIZE_TYPE bsize,
611                                          int plane, int block,
612                                          uint8_t *base, int stride) {
613   return base + raster_block_offset(xd, bsize, plane, block, stride);
614 }
615
616 static int txfrm_block_to_raster_block(MACROBLOCKD *xd,
617                                        BLOCK_SIZE_TYPE bsize,
618                                        int plane, int block,
619                                        int ss_txfrm_size) {
620   const int bwl = b_width_log2(bsize) - xd->plane[plane].subsampling_x;
621   const int txwl = ss_txfrm_size / 2;
622   const int tx_cols_log2 = bwl - txwl;
623   const int tx_cols = 1 << tx_cols_log2;
624   const int raster_mb = block >> ss_txfrm_size;
625   const int x = (raster_mb & (tx_cols - 1)) << (txwl);
626   const int y = raster_mb >> tx_cols_log2 << (txwl);
627   return x + (y << bwl);
628 }
629
630 static void txfrm_block_to_raster_xy(MACROBLOCKD *xd,
631                                      BLOCK_SIZE_TYPE bsize,
632                                      int plane, int block,
633                                      int ss_txfrm_size,
634                                      int *x, int *y) {
635   const int bwl = b_width_log2(bsize) - xd->plane[plane].subsampling_x;
636   const int txwl = ss_txfrm_size / 2;
637   const int tx_cols_log2 = bwl - txwl;
638   const int tx_cols = 1 << tx_cols_log2;
639   const int raster_mb = block >> ss_txfrm_size;
640   *x = (raster_mb & (tx_cols - 1)) << (txwl);
641   *y = raster_mb >> tx_cols_log2 << (txwl);
642 }
643
644 static void extend_for_intra(MACROBLOCKD* const xd, int plane, int block,
645                              BLOCK_SIZE_TYPE bsize, int ss_txfrm_size) {
646   const int bw = plane_block_width(bsize, &xd->plane[plane]);
647   const int bh = plane_block_height(bsize, &xd->plane[plane]);
648   int x, y;
649   txfrm_block_to_raster_xy(xd, bsize, plane, block, ss_txfrm_size, &x, &y);
650   x = x * 4 - 1;
651   y = y * 4 - 1;
652   // Copy a pixel into the umv if we are in a situation where the block size
653   // extends into the UMV.
654   // TODO(JBB): Should be able to do the full extend in place so we don't have
655   // to do this multiple times.
656   if (xd->mb_to_right_edge < 0) {
657     int umv_border_start = bw
658         + (xd->mb_to_right_edge >> (3 + xd->plane[plane].subsampling_x));
659
660     if (x + bw > umv_border_start)
661       vpx_memset(
662           xd->plane[plane].dst.buf + y * xd->plane[plane].dst.stride
663               + umv_border_start,
664           *(xd->plane[plane].dst.buf + y * xd->plane[plane].dst.stride
665               + umv_border_start - 1),
666           bw);
667   }
668   if (xd->mb_to_bottom_edge < 0) {
669     int umv_border_start = bh
670         + (xd->mb_to_bottom_edge >> (3 + xd->plane[plane].subsampling_y));
671     int i;
672     uint8_t c = *(xd->plane[plane].dst.buf
673         + (umv_border_start - 1) * xd->plane[plane].dst.stride + x);
674
675     uint8_t *d = xd->plane[plane].dst.buf
676         + umv_border_start * xd->plane[plane].dst.stride + x;
677
678     if (y + bh > umv_border_start)
679       for (i = 0; i < bh; i++, d += xd->plane[plane].dst.stride)
680         *d = c;
681   }
682 }
683 static void set_contexts_on_border(MACROBLOCKD *xd, BLOCK_SIZE_TYPE bsize,
684                                    int plane, int ss_tx_size, int eob, int aoff,
685                                    int loff, ENTROPY_CONTEXT *A,
686                                    ENTROPY_CONTEXT *L) {
687   const int bw = b_width_log2(bsize), bh = b_height_log2(bsize);
688   const int sw = bw - xd->plane[plane].subsampling_x;
689   const int sh = bh - xd->plane[plane].subsampling_y;
690   int mi_blocks_wide = 1 << sw;
691   int mi_blocks_high = 1 << sh;
692   int tx_size_in_blocks = (1 << ss_tx_size);
693   int above_contexts = tx_size_in_blocks;
694   int left_contexts = tx_size_in_blocks;
695   int pt;
696
697   // xd->mb_to_right_edge is in units of pixels * 8.  This converts
698   // it to 4x4 block sizes.
699   if (xd->mb_to_right_edge < 0) {
700     mi_blocks_wide += (xd->mb_to_right_edge
701         >> (5 + xd->plane[plane].subsampling_x));
702   }
703
704   // this code attempts to avoid copying into contexts that are outside
705   // our border.  Any blocks that do are set to 0...
706   if (above_contexts + aoff > mi_blocks_wide)
707     above_contexts = mi_blocks_wide - aoff;
708
709   if (xd->mb_to_bottom_edge < 0) {
710     mi_blocks_high += (xd->mb_to_bottom_edge
711         >> (5 + xd->plane[plane].subsampling_y));
712   }
713   if (left_contexts + loff > mi_blocks_high) {
714     left_contexts = mi_blocks_high - loff;
715   }
716
717   for (pt = 0; pt < above_contexts; pt++)
718     A[pt] = eob > 0;
719   for (pt = above_contexts; pt < (1 << ss_tx_size); pt++)
720     A[pt] = 0;
721   for (pt = 0; pt < left_contexts; pt++)
722     L[pt] = eob > 0;
723   for (pt = left_contexts; pt < (1 << ss_tx_size); pt++)
724     L[pt] = 0;
725 }
726
727
728 #endif  // VP9_COMMON_VP9_BLOCKD_H_