]> granicus.if.org Git - libvpx/blob - vp9/encoder/vp9_encodeframe.c
Merge "Enable dual buffer rd search and encoding scheme"
[libvpx] / vp9 / encoder / vp9_encodeframe.c
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 #include <limits.h>
12 #include <math.h>
13 #include <stdio.h>
14
15 #include "./vp9_rtcd.h"
16 #include "./vpx_config.h"
17
18 #include "vpx_ports/vpx_timer.h"
19
20 #include "vp9/common/vp9_common.h"
21 #include "vp9/common/vp9_entropy.h"
22 #include "vp9/common/vp9_entropymode.h"
23 #include "vp9/common/vp9_extend.h"
24 #include "vp9/common/vp9_findnearmv.h"
25 #include "vp9/common/vp9_idct.h"
26 #include "vp9/common/vp9_mvref_common.h"
27 #include "vp9/common/vp9_pred_common.h"
28 #include "vp9/common/vp9_quant_common.h"
29 #include "vp9/common/vp9_reconintra.h"
30 #include "vp9/common/vp9_reconinter.h"
31 #include "vp9/common/vp9_seg_common.h"
32 #include "vp9/common/vp9_tile_common.h"
33 #include "vp9/encoder/vp9_encodeframe.h"
34 #include "vp9/encoder/vp9_encodeintra.h"
35 #include "vp9/encoder/vp9_encodemb.h"
36 #include "vp9/encoder/vp9_encodemv.h"
37 #include "vp9/encoder/vp9_onyx_int.h"
38 #include "vp9/encoder/vp9_rdopt.h"
39 #include "vp9/encoder/vp9_segmentation.h"
40 #include "vp9/common/vp9_systemdependent.h"
41 #include "vp9/encoder/vp9_tokenize.h"
42 #include "vp9/encoder/vp9_vaq.h"
43
44
45 #define DBG_PRNT_SEGMAP 0
46
47
48 // #define ENC_DEBUG
49 #ifdef ENC_DEBUG
50 int enc_debug = 0;
51 #endif
52
53 static INLINE uint8_t *get_sb_index(MACROBLOCK *x, BLOCK_SIZE subsize) {
54   switch (subsize) {
55     case BLOCK_64X64:
56     case BLOCK_64X32:
57     case BLOCK_32X64:
58     case BLOCK_32X32:
59       return &x->sb_index;
60     case BLOCK_32X16:
61     case BLOCK_16X32:
62     case BLOCK_16X16:
63       return &x->mb_index;
64     case BLOCK_16X8:
65     case BLOCK_8X16:
66     case BLOCK_8X8:
67       return &x->b_index;
68     case BLOCK_8X4:
69     case BLOCK_4X8:
70     case BLOCK_4X4:
71       return &x->ab_index;
72     default:
73       assert(0);
74       return NULL;
75   }
76 }
77
78 static void encode_superblock(VP9_COMP *cpi, TOKENEXTRA **t, int output_enabled,
79                               int mi_row, int mi_col, BLOCK_SIZE bsize);
80
81 static void adjust_act_zbin(VP9_COMP *cpi, MACROBLOCK *x);
82
83 /* activity_avg must be positive, or flat regions could get a zero weight
84  *  (infinite lambda), which confounds analysis.
85  * This also avoids the need for divide by zero checks in
86  *  vp9_activity_masking().
87  */
88 #define ACTIVITY_AVG_MIN (64)
89
90 /* Motion vector component magnitude threshold for defining fast motion. */
91 #define FAST_MOTION_MV_THRESH (24)
92
93 /* This is used as a reference when computing the source variance for the
94  *  purposes of activity masking.
95  * Eventually this should be replaced by custom no-reference routines,
96  *  which will be faster.
97  */
98 static const uint8_t VP9_VAR_OFFS[64] = {
99   128, 128, 128, 128, 128, 128, 128, 128,
100   128, 128, 128, 128, 128, 128, 128, 128,
101   128, 128, 128, 128, 128, 128, 128, 128,
102   128, 128, 128, 128, 128, 128, 128, 128,
103   128, 128, 128, 128, 128, 128, 128, 128,
104   128, 128, 128, 128, 128, 128, 128, 128,
105   128, 128, 128, 128, 128, 128, 128, 128,
106   128, 128, 128, 128, 128, 128, 128, 128
107 };
108
109 static unsigned int get_sby_perpixel_variance(VP9_COMP *cpi, MACROBLOCK *x,
110                                               BLOCK_SIZE bs) {
111   unsigned int var, sse;
112   var = cpi->fn_ptr[bs].vf(x->plane[0].src.buf,
113                            x->plane[0].src.stride,
114                            VP9_VAR_OFFS, 0, &sse);
115   return (var + (1 << (num_pels_log2_lookup[bs] - 1))) >>
116       num_pels_log2_lookup[bs];
117 }
118
119 // Original activity measure from Tim T's code.
120 static unsigned int tt_activity_measure(MACROBLOCK *x) {
121   unsigned int act;
122   unsigned int sse;
123   /* TODO: This could also be done over smaller areas (8x8), but that would
124    *  require extensive changes elsewhere, as lambda is assumed to be fixed
125    *  over an entire MB in most of the code.
126    * Another option is to compute four 8x8 variances, and pick a single
127    *  lambda using a non-linear combination (e.g., the smallest, or second
128    *  smallest, etc.).
129    */
130   act = vp9_variance16x16(x->plane[0].src.buf, x->plane[0].src.stride,
131                           VP9_VAR_OFFS, 0, &sse);
132   act <<= 4;
133
134   /* If the region is flat, lower the activity some more. */
135   if (act < 8 << 12)
136     act = act < 5 << 12 ? act : 5 << 12;
137
138   return act;
139 }
140
141 // Stub for alternative experimental activity measures.
142 static unsigned int alt_activity_measure(MACROBLOCK *x, int use_dc_pred) {
143   return vp9_encode_intra(x, use_dc_pred);
144 }
145
146 // Measure the activity of the current macroblock
147 // What we measure here is TBD so abstracted to this function
148 #define ALT_ACT_MEASURE 1
149 static unsigned int mb_activity_measure(MACROBLOCK *x, int mb_row, int mb_col) {
150   unsigned int mb_activity;
151
152   if (ALT_ACT_MEASURE) {
153     int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
154
155     // Or use and alternative.
156     mb_activity = alt_activity_measure(x, use_dc_pred);
157   } else {
158     // Original activity measure from Tim T's code.
159     mb_activity = tt_activity_measure(x);
160   }
161
162   if (mb_activity < ACTIVITY_AVG_MIN)
163     mb_activity = ACTIVITY_AVG_MIN;
164
165   return mb_activity;
166 }
167
168 // Calculate an "average" mb activity value for the frame
169 #define ACT_MEDIAN 0
170 static void calc_av_activity(VP9_COMP *cpi, int64_t activity_sum) {
171 #if ACT_MEDIAN
172   // Find median: Simple n^2 algorithm for experimentation
173   {
174     unsigned int median;
175     unsigned int i, j;
176     unsigned int *sortlist;
177     unsigned int tmp;
178
179     // Create a list to sort to
180     CHECK_MEM_ERROR(&cpi->common, sortlist, vpx_calloc(sizeof(unsigned int),
181                     cpi->common.MBs));
182
183     // Copy map to sort list
184     vpx_memcpy(sortlist, cpi->mb_activity_map,
185         sizeof(unsigned int) * cpi->common.MBs);
186
187     // Ripple each value down to its correct position
188     for (i = 1; i < cpi->common.MBs; i ++) {
189       for (j = i; j > 0; j --) {
190         if (sortlist[j] < sortlist[j - 1]) {
191           // Swap values
192           tmp = sortlist[j - 1];
193           sortlist[j - 1] = sortlist[j];
194           sortlist[j] = tmp;
195         } else {
196           break;
197         }
198       }
199     }
200
201     // Even number MBs so estimate median as mean of two either side.
202     median = (1 + sortlist[cpi->common.MBs >> 1] +
203         sortlist[(cpi->common.MBs >> 1) + 1]) >> 1;
204
205     cpi->activity_avg = median;
206
207     vpx_free(sortlist);
208   }
209 #else
210   // Simple mean for now
211   cpi->activity_avg = (unsigned int) (activity_sum / cpi->common.MBs);
212 #endif  // ACT_MEDIAN
213
214   if (cpi->activity_avg < ACTIVITY_AVG_MIN)
215     cpi->activity_avg = ACTIVITY_AVG_MIN;
216
217   // Experimental code: return fixed value normalized for several clips
218   if (ALT_ACT_MEASURE)
219     cpi->activity_avg = 100000;
220 }
221
222 #define USE_ACT_INDEX   0
223 #define OUTPUT_NORM_ACT_STATS   0
224
225 #if USE_ACT_INDEX
226 // Calculate an activity index for each mb
227 static void calc_activity_index(VP9_COMP *cpi, MACROBLOCK *x) {
228   VP9_COMMON *const cm = &cpi->common;
229   int mb_row, mb_col;
230
231   int64_t act;
232   int64_t a;
233   int64_t b;
234
235 #if OUTPUT_NORM_ACT_STATS
236   FILE *f = fopen("norm_act.stt", "a");
237   fprintf(f, "\n%12d\n", cpi->activity_avg);
238 #endif
239
240   // Reset pointers to start of activity map
241   x->mb_activity_ptr = cpi->mb_activity_map;
242
243   // Calculate normalized mb activity number.
244   for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) {
245     // for each macroblock col in image
246     for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
247       // Read activity from the map
248       act = *(x->mb_activity_ptr);
249
250       // Calculate a normalized activity number
251       a = act + 4 * cpi->activity_avg;
252       b = 4 * act + cpi->activity_avg;
253
254       if (b >= a)
255       *(x->activity_ptr) = (int)((b + (a >> 1)) / a) - 1;
256       else
257       *(x->activity_ptr) = 1 - (int)((a + (b >> 1)) / b);
258
259 #if OUTPUT_NORM_ACT_STATS
260       fprintf(f, " %6d", *(x->mb_activity_ptr));
261 #endif
262       // Increment activity map pointers
263       x->mb_activity_ptr++;
264     }
265
266 #if OUTPUT_NORM_ACT_STATS
267     fprintf(f, "\n");
268 #endif
269   }
270
271 #if OUTPUT_NORM_ACT_STATS
272   fclose(f);
273 #endif
274 }
275 #endif  // USE_ACT_INDEX
276
277 // Loop through all MBs. Note activity of each, average activity and
278 // calculate a normalized activity for each
279 static void build_activity_map(VP9_COMP *cpi) {
280   MACROBLOCK * const x = &cpi->mb;
281   MACROBLOCKD *xd = &x->e_mbd;
282   VP9_COMMON * const cm = &cpi->common;
283
284 #if ALT_ACT_MEASURE
285   YV12_BUFFER_CONFIG *new_yv12 = get_frame_new_buffer(cm);
286   int recon_yoffset;
287   int recon_y_stride = new_yv12->y_stride;
288 #endif
289
290   int mb_row, mb_col;
291   unsigned int mb_activity;
292   int64_t activity_sum = 0;
293
294   x->mb_activity_ptr = cpi->mb_activity_map;
295
296   // for each macroblock row in image
297   for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) {
298 #if ALT_ACT_MEASURE
299     // reset above block coeffs
300     xd->up_available = (mb_row != 0);
301     recon_yoffset = (mb_row * recon_y_stride * 16);
302 #endif
303     // for each macroblock col in image
304     for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
305 #if ALT_ACT_MEASURE
306       xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset;
307       xd->left_available = (mb_col != 0);
308       recon_yoffset += 16;
309 #endif
310
311       // measure activity
312       mb_activity = mb_activity_measure(x, mb_row, mb_col);
313
314       // Keep frame sum
315       activity_sum += mb_activity;
316
317       // Store MB level activity details.
318       *x->mb_activity_ptr = mb_activity;
319
320       // Increment activity map pointer
321       x->mb_activity_ptr++;
322
323       // adjust to the next column of source macroblocks
324       x->plane[0].src.buf += 16;
325     }
326
327     // adjust to the next row of mbs
328     x->plane[0].src.buf += 16 * x->plane[0].src.stride - 16 * cm->mb_cols;
329   }
330
331   // Calculate an "average" MB activity
332   calc_av_activity(cpi, activity_sum);
333
334 #if USE_ACT_INDEX
335   // Calculate an activity index number of each mb
336   calc_activity_index(cpi, x);
337 #endif
338 }
339
340 // Macroblock activity masking
341 void vp9_activity_masking(VP9_COMP *cpi, MACROBLOCK *x) {
342 #if USE_ACT_INDEX
343   x->rdmult += *(x->mb_activity_ptr) * (x->rdmult >> 2);
344   x->errorperbit = x->rdmult * 100 / (110 * x->rddiv);
345   x->errorperbit += (x->errorperbit == 0);
346 #else
347   int64_t a;
348   int64_t b;
349   int64_t act = *(x->mb_activity_ptr);
350
351   // Apply the masking to the RD multiplier.
352   a = act + (2 * cpi->activity_avg);
353   b = (2 * act) + cpi->activity_avg;
354
355   x->rdmult = (unsigned int) (((int64_t) x->rdmult * b + (a >> 1)) / a);
356   x->errorperbit = x->rdmult * 100 / (110 * x->rddiv);
357   x->errorperbit += (x->errorperbit == 0);
358 #endif
359
360   // Activity based Zbin adjustment
361   adjust_act_zbin(cpi, x);
362 }
363
364 static void update_state(VP9_COMP *cpi, PICK_MODE_CONTEXT *ctx,
365                          BLOCK_SIZE bsize, int output_enabled) {
366   int i, x_idx, y;
367   VP9_COMMON *const cm = &cpi->common;
368   MACROBLOCK *const x = &cpi->mb;
369   MACROBLOCKD *const xd = &x->e_mbd;
370   struct macroblock_plane *const p = x->plane;
371   struct macroblockd_plane *const pd = xd->plane;
372   MODE_INFO *mi = &ctx->mic;
373   MB_MODE_INFO *const mbmi = &xd->mi_8x8[0]->mbmi;
374   MODE_INFO *mi_addr = xd->mi_8x8[0];
375
376   int mb_mode_index = ctx->best_mode_index;
377   const int mis = cm->mode_info_stride;
378   const int mi_width = num_8x8_blocks_wide_lookup[bsize];
379   const int mi_height = num_8x8_blocks_high_lookup[bsize];
380
381   assert(mi->mbmi.mode < MB_MODE_COUNT);
382   assert(mi->mbmi.ref_frame[0] < MAX_REF_FRAMES);
383   assert(mi->mbmi.ref_frame[1] < MAX_REF_FRAMES);
384   assert(mi->mbmi.sb_type == bsize);
385
386   *mi_addr = *mi;
387
388   for (i = 0; i < MAX_MB_PLANE; ++i) {
389     p[i].coeff = ctx->coeff_pbuf[i][1];
390     pd[i].qcoeff = ctx->qcoeff_pbuf[i][1];
391     pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][1];
392     pd[i].eobs = ctx->eobs_pbuf[i][1];
393   }
394
395   // Restore the coding context of the MB to that that was in place
396   // when the mode was picked for it
397   for (y = 0; y < mi_height; y++)
398     for (x_idx = 0; x_idx < mi_width; x_idx++)
399       if ((xd->mb_to_right_edge >> (3 + MI_SIZE_LOG2)) + mi_width > x_idx
400           && (xd->mb_to_bottom_edge >> (3 + MI_SIZE_LOG2)) + mi_height > y)
401         xd->mi_8x8[x_idx + y * mis] = mi_addr;
402
403   if (cpi->sf.variance_adaptive_quantization) {
404     vp9_mb_init_quantizer(cpi, x);
405   }
406
407   // FIXME(rbultje) I'm pretty sure this should go to the end of this block
408   // (i.e. after the output_enabled)
409   if (bsize < BLOCK_32X32) {
410     if (bsize < BLOCK_16X16)
411       ctx->tx_rd_diff[ALLOW_16X16] = ctx->tx_rd_diff[ALLOW_8X8];
412     ctx->tx_rd_diff[ALLOW_32X32] = ctx->tx_rd_diff[ALLOW_16X16];
413   }
414
415   if (is_inter_block(mbmi) && mbmi->sb_type < BLOCK_8X8) {
416     mbmi->mv[0].as_int = mi->bmi[3].as_mv[0].as_int;
417     mbmi->mv[1].as_int = mi->bmi[3].as_mv[1].as_int;
418   }
419
420   x->skip = ctx->skip;
421   vpx_memcpy(x->zcoeff_blk[mbmi->tx_size], ctx->zcoeff_blk,
422              sizeof(uint8_t) * ctx->num_4x4_blk);
423
424   if (!output_enabled)
425     return;
426
427   if (!vp9_segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
428     for (i = 0; i < TX_MODES; i++)
429       cpi->rd_tx_select_diff[i] += ctx->tx_rd_diff[i];
430   }
431
432   if (frame_is_intra_only(cm)) {
433 #if CONFIG_INTERNAL_STATS
434     static const int kf_mode_index[] = {
435       THR_DC /*DC_PRED*/,
436       THR_V_PRED /*V_PRED*/,
437       THR_H_PRED /*H_PRED*/,
438       THR_D45_PRED /*D45_PRED*/,
439       THR_D135_PRED /*D135_PRED*/,
440       THR_D117_PRED /*D117_PRED*/,
441       THR_D153_PRED /*D153_PRED*/,
442       THR_D207_PRED /*D207_PRED*/,
443       THR_D63_PRED /*D63_PRED*/,
444       THR_TM /*TM_PRED*/,
445     };
446     cpi->mode_chosen_counts[kf_mode_index[mi->mbmi.mode]]++;
447 #endif
448   } else {
449     // Note how often each mode chosen as best
450     cpi->mode_chosen_counts[mb_mode_index]++;
451     if (is_inter_block(mbmi)
452         && (mbmi->sb_type < BLOCK_8X8 || mbmi->mode == NEWMV)) {
453       int_mv best_mv[2];
454       const MV_REFERENCE_FRAME rf1 = mbmi->ref_frame[0];
455       const MV_REFERENCE_FRAME rf2 = mbmi->ref_frame[1];
456       best_mv[0].as_int = ctx->best_ref_mv.as_int;
457       best_mv[1].as_int = ctx->second_best_ref_mv.as_int;
458       if (mbmi->mode == NEWMV) {
459         best_mv[0].as_int = mbmi->ref_mvs[rf1][0].as_int;
460         if (rf2 > 0)
461           best_mv[1].as_int = mbmi->ref_mvs[rf2][0].as_int;
462       }
463       mbmi->best_mv[0].as_int = best_mv[0].as_int;
464       mbmi->best_mv[1].as_int = best_mv[1].as_int;
465       vp9_update_mv_count(cpi, x, best_mv);
466     }
467
468     if (cm->mcomp_filter_type == SWITCHABLE && is_inter_mode(mbmi->mode)) {
469       const int ctx = vp9_get_pred_context_switchable_interp(xd);
470       ++cm->counts.switchable_interp[ctx][mbmi->interp_filter];
471     }
472
473     cpi->rd_comp_pred_diff[SINGLE_PREDICTION_ONLY] += ctx->single_pred_diff;
474     cpi->rd_comp_pred_diff[COMP_PREDICTION_ONLY] += ctx->comp_pred_diff;
475     cpi->rd_comp_pred_diff[HYBRID_PREDICTION] += ctx->hybrid_pred_diff;
476
477     for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++)
478       cpi->rd_filter_diff[i] += ctx->best_filter_diff[i];
479   }
480 }
481
482 void vp9_setup_src_planes(MACROBLOCK *x, const YV12_BUFFER_CONFIG *src,
483                           int mi_row, int mi_col) {
484   uint8_t *const buffers[4] = {src->y_buffer, src->u_buffer, src->v_buffer,
485                                src->alpha_buffer};
486   const int strides[4] = {src->y_stride, src->uv_stride, src->uv_stride,
487                           src->alpha_stride};
488   int i;
489
490   for (i = 0; i < MAX_MB_PLANE; i++)
491     setup_pred_plane(&x->plane[i].src, buffers[i], strides[i], mi_row, mi_col,
492                      NULL, x->e_mbd.plane[i].subsampling_x,
493                      x->e_mbd.plane[i].subsampling_y);
494 }
495
496 static void set_offsets(VP9_COMP *cpi, const TileInfo *const tile,
497                         int mi_row, int mi_col, BLOCK_SIZE bsize) {
498   MACROBLOCK *const x = &cpi->mb;
499   VP9_COMMON *const cm = &cpi->common;
500   MACROBLOCKD *const xd = &x->e_mbd;
501   MB_MODE_INFO *mbmi;
502   const int dst_fb_idx = cm->new_fb_idx;
503   const int idx_str = xd->mode_info_stride * mi_row + mi_col;
504   const int mi_width = num_8x8_blocks_wide_lookup[bsize];
505   const int mi_height = num_8x8_blocks_high_lookup[bsize];
506   const int mb_row = mi_row >> 1;
507   const int mb_col = mi_col >> 1;
508   const int idx_map = mb_row * cm->mb_cols + mb_col;
509   const struct segmentation *const seg = &cm->seg;
510
511   set_skip_context(xd, cpi->above_context, cpi->left_context, mi_row, mi_col);
512
513   // Activity map pointer
514   x->mb_activity_ptr = &cpi->mb_activity_map[idx_map];
515   x->active_ptr = cpi->active_map + idx_map;
516
517   xd->mi_8x8 = cm->mi_grid_visible + idx_str;
518   xd->prev_mi_8x8 = cm->prev_mi_grid_visible + idx_str;
519
520   // Special case: if prev_mi is NULL, the previous mode info context
521   // cannot be used.
522   xd->last_mi = cm->prev_mi ? xd->prev_mi_8x8[0] : NULL;
523
524   xd->mi_8x8[0] = cm->mi + idx_str;
525
526   mbmi = &xd->mi_8x8[0]->mbmi;
527
528   // Set up destination pointers
529   setup_dst_planes(xd, &cm->yv12_fb[dst_fb_idx], mi_row, mi_col);
530
531   // Set up limit values for MV components
532   // mv beyond the range do not produce new/different prediction block
533   x->mv_row_min = -(((mi_row + mi_height) * MI_SIZE) + VP9_INTERP_EXTEND);
534   x->mv_col_min = -(((mi_col + mi_width) * MI_SIZE) + VP9_INTERP_EXTEND);
535   x->mv_row_max = (cm->mi_rows - mi_row) * MI_SIZE + VP9_INTERP_EXTEND;
536   x->mv_col_max = (cm->mi_cols - mi_col) * MI_SIZE + VP9_INTERP_EXTEND;
537
538   // Set up distance of MB to edge of frame in 1/8th pel units
539   assert(!(mi_col & (mi_width - 1)) && !(mi_row & (mi_height - 1)));
540   set_mi_row_col(xd, tile, mi_row, mi_height, mi_col, mi_width,
541                  cm->mi_rows, cm->mi_cols);
542
543   /* set up source buffers */
544   vp9_setup_src_planes(x, cpi->Source, mi_row, mi_col);
545
546   /* R/D setup */
547   x->rddiv = cpi->RDDIV;
548   x->rdmult = cpi->RDMULT;
549
550   /* segment ID */
551   if (seg->enabled) {
552     if (!cpi->sf.variance_adaptive_quantization) {
553       uint8_t *map = seg->update_map ? cpi->segmentation_map
554           : cm->last_frame_seg_map;
555       mbmi->segment_id = vp9_get_segment_id(cm, map, bsize, mi_row, mi_col);
556     }
557     vp9_mb_init_quantizer(cpi, x);
558
559     if (seg->enabled && cpi->seg0_cnt > 0
560         && !vp9_segfeature_active(seg, 0, SEG_LVL_REF_FRAME)
561         && vp9_segfeature_active(seg, 1, SEG_LVL_REF_FRAME)) {
562       cpi->seg0_progress = (cpi->seg0_idx << 16) / cpi->seg0_cnt;
563     } else {
564       const int y = mb_row & ~3;
565       const int x = mb_col & ~3;
566       const int p16 = ((mb_row & 1) << 1) + (mb_col & 1);
567       const int p32 = ((mb_row & 2) << 2) + ((mb_col & 2) << 1);
568       const int tile_progress = tile->mi_col_start * cm->mb_rows >> 1;
569       const int mb_cols = (tile->mi_col_end - tile->mi_col_start) >> 1;
570
571       cpi->seg0_progress = ((y * mb_cols + x * 4 + p32 + p16 + tile_progress)
572           << 16) / cm->MBs;
573     }
574
575     x->encode_breakout = cpi->segment_encode_breakout[mbmi->segment_id];
576   } else {
577     mbmi->segment_id = 0;
578     x->encode_breakout = cpi->oxcf.encode_breakout;
579   }
580 }
581
582 static void pick_sb_modes(VP9_COMP *cpi, const TileInfo *const tile,
583                           int mi_row, int mi_col,
584                           int *totalrate, int64_t *totaldist,
585                           BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx,
586                           int64_t best_rd) {
587   VP9_COMMON *const cm = &cpi->common;
588   MACROBLOCK *const x = &cpi->mb;
589   MACROBLOCKD *const xd = &x->e_mbd;
590   struct macroblock_plane *const p = x->plane;
591   struct macroblockd_plane *const pd = xd->plane;
592   int i;
593   int orig_rdmult = x->rdmult;
594   double rdmult_ratio;
595
596   vp9_clear_system_state();  // __asm emms;
597   rdmult_ratio = 1.0;  // avoid uninitialized warnings
598
599   // Use the lower precision, but faster, 32x32 fdct for mode selection.
600   x->use_lp32x32fdct = 1;
601
602   if (bsize < BLOCK_8X8) {
603     // When ab_index = 0 all sub-blocks are handled, so for ab_index != 0
604     // there is nothing to be done.
605     if (x->ab_index != 0) {
606       *totalrate = 0;
607       *totaldist = 0;
608       return;
609     }
610   }
611
612   set_offsets(cpi, tile, mi_row, mi_col, bsize);
613   xd->mi_8x8[0]->mbmi.sb_type = bsize;
614
615   for (i = 0; i < MAX_MB_PLANE; ++i) {
616     p[i].coeff = ctx->coeff_pbuf[i][0];
617     pd[i].qcoeff = ctx->qcoeff_pbuf[i][0];
618     pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][0];
619     pd[i].eobs = ctx->eobs_pbuf[i][0];
620   }
621   ctx->is_coded = 0;
622
623   // Set to zero to make sure we do not use the previous encoded frame stats
624   xd->mi_8x8[0]->mbmi.skip_coeff = 0;
625
626   x->source_variance = get_sby_perpixel_variance(cpi, x, bsize);
627
628   if (cpi->sf.variance_adaptive_quantization) {
629     int energy;
630     if (bsize <= BLOCK_16X16) {
631       energy = x->mb_energy;
632     } else {
633       energy = vp9_block_energy(cpi, x, bsize);
634     }
635
636     xd->mi_8x8[0]->mbmi.segment_id = vp9_vaq_segment_id(energy);
637     rdmult_ratio = vp9_vaq_rdmult_ratio(energy);
638     vp9_mb_init_quantizer(cpi, x);
639   }
640
641   if (cpi->oxcf.tuning == VP8_TUNE_SSIM)
642     vp9_activity_masking(cpi, x);
643
644   if (cpi->sf.variance_adaptive_quantization) {
645     vp9_clear_system_state();  // __asm emms;
646     x->rdmult = round(x->rdmult * rdmult_ratio);
647   }
648
649   // Find best coding mode & reconstruct the MB so it is available
650   // as a predictor for MBs that follow in the SB
651   if (frame_is_intra_only(cm)) {
652     vp9_rd_pick_intra_mode_sb(cpi, x, totalrate, totaldist, bsize, ctx,
653                               best_rd);
654   } else {
655     if (bsize >= BLOCK_8X8)
656       vp9_rd_pick_inter_mode_sb(cpi, x, tile, mi_row, mi_col,
657                                 totalrate, totaldist, bsize, ctx, best_rd);
658     else
659       vp9_rd_pick_inter_mode_sub8x8(cpi, x, tile, mi_row, mi_col, totalrate,
660                                     totaldist, bsize, ctx, best_rd);
661   }
662
663   if (cpi->sf.variance_adaptive_quantization) {
664     x->rdmult = orig_rdmult;
665     if (*totalrate != INT_MAX) {
666       vp9_clear_system_state();  // __asm emms;
667       *totalrate = round(*totalrate * rdmult_ratio);
668     }
669   }
670 }
671
672 static void update_stats(VP9_COMP *cpi) {
673   VP9_COMMON *const cm = &cpi->common;
674   MACROBLOCK *const x = &cpi->mb;
675   MACROBLOCKD *const xd = &x->e_mbd;
676   MODE_INFO *mi = xd->mi_8x8[0];
677   MB_MODE_INFO *const mbmi = &mi->mbmi;
678
679   if (!frame_is_intra_only(cm)) {
680     const int seg_ref_active = vp9_segfeature_active(&cm->seg, mbmi->segment_id,
681                                                      SEG_LVL_REF_FRAME);
682
683     if (!seg_ref_active)
684       cpi->intra_inter_count[vp9_get_pred_context_intra_inter(xd)]
685                             [is_inter_block(mbmi)]++;
686
687     // If the segment reference feature is enabled we have only a single
688     // reference frame allowed for the segment so exclude it from
689     // the reference frame counts used to work out probabilities.
690     if (is_inter_block(mbmi) && !seg_ref_active) {
691       if (cm->comp_pred_mode == HYBRID_PREDICTION)
692         cpi->comp_inter_count[vp9_get_pred_context_comp_inter_inter(cm, xd)]
693                              [has_second_ref(mbmi)]++;
694
695       if (has_second_ref(mbmi)) {
696         cpi->comp_ref_count[vp9_get_pred_context_comp_ref_p(cm, xd)]
697                            [mbmi->ref_frame[0] == GOLDEN_FRAME]++;
698       } else {
699         cpi->single_ref_count[vp9_get_pred_context_single_ref_p1(xd)][0]
700                              [mbmi->ref_frame[0] != LAST_FRAME]++;
701         if (mbmi->ref_frame[0] != LAST_FRAME)
702           cpi->single_ref_count[vp9_get_pred_context_single_ref_p2(xd)][1]
703                                [mbmi->ref_frame[0] != GOLDEN_FRAME]++;
704       }
705     }
706   }
707 }
708
709 static BLOCK_SIZE *get_sb_partitioning(MACROBLOCK *x, BLOCK_SIZE bsize) {
710   switch (bsize) {
711     case BLOCK_64X64:
712       return &x->sb64_partitioning;
713     case BLOCK_32X32:
714       return &x->sb_partitioning[x->sb_index];
715     case BLOCK_16X16:
716       return &x->mb_partitioning[x->sb_index][x->mb_index];
717     case BLOCK_8X8:
718       return &x->b_partitioning[x->sb_index][x->mb_index][x->b_index];
719     default:
720       assert(0);
721       return NULL;
722   }
723 }
724
725 static void restore_context(VP9_COMP *cpi, int mi_row, int mi_col,
726                             ENTROPY_CONTEXT a[16 * MAX_MB_PLANE],
727                             ENTROPY_CONTEXT l[16 * MAX_MB_PLANE],
728                             PARTITION_CONTEXT sa[8], PARTITION_CONTEXT sl[8],
729                             BLOCK_SIZE bsize) {
730   MACROBLOCK *const x = &cpi->mb;
731   MACROBLOCKD *const xd = &x->e_mbd;
732   int p;
733   const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
734   const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
735   int mi_width = num_8x8_blocks_wide_lookup[bsize];
736   int mi_height = num_8x8_blocks_high_lookup[bsize];
737   for (p = 0; p < MAX_MB_PLANE; p++) {
738     vpx_memcpy(
739         cpi->above_context[p] + ((mi_col * 2) >> xd->plane[p].subsampling_x),
740         a + num_4x4_blocks_wide * p,
741         (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_wide) >>
742         xd->plane[p].subsampling_x);
743     vpx_memcpy(
744         cpi->left_context[p]
745             + ((mi_row & MI_MASK) * 2 >> xd->plane[p].subsampling_y),
746         l + num_4x4_blocks_high * p,
747         (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_high) >>
748         xd->plane[p].subsampling_y);
749   }
750   vpx_memcpy(cpi->above_seg_context + mi_col, sa,
751              sizeof(*cpi->above_seg_context) * mi_width);
752   vpx_memcpy(cpi->left_seg_context + (mi_row & MI_MASK), sl,
753              sizeof(cpi->left_seg_context[0]) * mi_height);
754 }
755 static void save_context(VP9_COMP *cpi, int mi_row, int mi_col,
756                          ENTROPY_CONTEXT a[16 * MAX_MB_PLANE],
757                          ENTROPY_CONTEXT l[16 * MAX_MB_PLANE],
758                          PARTITION_CONTEXT sa[8], PARTITION_CONTEXT sl[8],
759                          BLOCK_SIZE bsize) {
760   const MACROBLOCK *const x = &cpi->mb;
761   const MACROBLOCKD *const xd = &x->e_mbd;
762   int p;
763   const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
764   const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
765   int mi_width = num_8x8_blocks_wide_lookup[bsize];
766   int mi_height = num_8x8_blocks_high_lookup[bsize];
767
768   // buffer the above/left context information of the block in search.
769   for (p = 0; p < MAX_MB_PLANE; ++p) {
770     vpx_memcpy(
771         a + num_4x4_blocks_wide * p,
772         cpi->above_context[p] + (mi_col * 2 >> xd->plane[p].subsampling_x),
773         (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_wide) >>
774         xd->plane[p].subsampling_x);
775     vpx_memcpy(
776         l + num_4x4_blocks_high * p,
777         cpi->left_context[p]
778             + ((mi_row & MI_MASK) * 2 >> xd->plane[p].subsampling_y),
779         (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_high) >>
780         xd->plane[p].subsampling_y);
781   }
782   vpx_memcpy(sa, cpi->above_seg_context + mi_col,
783              sizeof(*cpi->above_seg_context) * mi_width);
784   vpx_memcpy(sl, cpi->left_seg_context + (mi_row & MI_MASK),
785              sizeof(cpi->left_seg_context[0]) * mi_height);
786 }
787
788 static void encode_b(VP9_COMP *cpi, const TileInfo *const tile,
789                      TOKENEXTRA **tp, int mi_row, int mi_col,
790                      int output_enabled, BLOCK_SIZE bsize, int sub_index) {
791   VP9_COMMON *const cm = &cpi->common;
792   MACROBLOCK *const x = &cpi->mb;
793
794   if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
795     return;
796
797   if (sub_index != -1)
798     *get_sb_index(x, bsize) = sub_index;
799
800   if (bsize < BLOCK_8X8) {
801     // When ab_index = 0 all sub-blocks are handled, so for ab_index != 0
802     // there is nothing to be done.
803     if (x->ab_index > 0)
804       return;
805   }
806   set_offsets(cpi, tile, mi_row, mi_col, bsize);
807   update_state(cpi, get_block_context(x, bsize), bsize, output_enabled);
808   encode_superblock(cpi, tp, output_enabled, mi_row, mi_col, bsize);
809
810   if (output_enabled) {
811     update_stats(cpi);
812
813     (*tp)->token = EOSB_TOKEN;
814     (*tp)++;
815   }
816 }
817
818 static void encode_sb(VP9_COMP *cpi, const TileInfo *const tile,
819                       TOKENEXTRA **tp, int mi_row, int mi_col,
820                       int output_enabled, BLOCK_SIZE bsize) {
821   VP9_COMMON *const cm = &cpi->common;
822   MACROBLOCK *const x = &cpi->mb;
823   BLOCK_SIZE c1 = BLOCK_8X8;
824   const int bsl = b_width_log2(bsize), bs = (1 << bsl) / 4;
825   int pl = 0;
826   PARTITION_TYPE partition;
827   BLOCK_SIZE subsize;
828   int i;
829
830   if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
831     return;
832
833   c1 = BLOCK_4X4;
834   if (bsize >= BLOCK_8X8) {
835     pl = partition_plane_context(cpi->above_seg_context, cpi->left_seg_context,
836                                  mi_row, mi_col, bsize);
837     c1 = *(get_sb_partitioning(x, bsize));
838   }
839   partition = partition_lookup[bsl][c1];
840
841   switch (partition) {
842     case PARTITION_NONE:
843       if (output_enabled && bsize >= BLOCK_8X8)
844         cpi->partition_count[pl][PARTITION_NONE]++;
845       encode_b(cpi, tile, tp, mi_row, mi_col, output_enabled, c1, -1);
846       break;
847     case PARTITION_VERT:
848       if (output_enabled)
849         cpi->partition_count[pl][PARTITION_VERT]++;
850       encode_b(cpi, tile, tp, mi_row, mi_col, output_enabled, c1, 0);
851       encode_b(cpi, tile, tp, mi_row, mi_col + bs, output_enabled, c1, 1);
852       break;
853     case PARTITION_HORZ:
854       if (output_enabled)
855         cpi->partition_count[pl][PARTITION_HORZ]++;
856       encode_b(cpi, tile, tp, mi_row, mi_col, output_enabled, c1, 0);
857       encode_b(cpi, tile, tp, mi_row + bs, mi_col, output_enabled, c1, 1);
858       break;
859     case PARTITION_SPLIT:
860       subsize = get_subsize(bsize, PARTITION_SPLIT);
861
862       if (output_enabled)
863         cpi->partition_count[pl][PARTITION_SPLIT]++;
864
865       for (i = 0; i < 4; i++) {
866         const int x_idx = i & 1, y_idx = i >> 1;
867
868         *get_sb_index(x, subsize) = i;
869         encode_sb(cpi, tile, tp, mi_row + y_idx * bs, mi_col + x_idx * bs,
870                   output_enabled, subsize);
871       }
872       break;
873     default:
874       assert(0);
875       break;
876   }
877
878   if (partition != PARTITION_SPLIT || bsize == BLOCK_8X8)
879     update_partition_context(cpi->above_seg_context, cpi->left_seg_context,
880                              mi_row, mi_col, c1, bsize);
881 }
882
883 // Check to see if the given partition size is allowed for a specified number
884 // of 8x8 block rows and columns remaining in the image.
885 // If not then return the largest allowed partition size
886 static BLOCK_SIZE find_partition_size(BLOCK_SIZE bsize,
887                                       int rows_left, int cols_left,
888                                       int *bh, int *bw) {
889   if ((rows_left <= 0) || (cols_left <= 0)) {
890     return MIN(bsize, BLOCK_8X8);
891   } else {
892     for (; bsize > 0; --bsize) {
893       *bh = num_8x8_blocks_high_lookup[bsize];
894       *bw = num_8x8_blocks_wide_lookup[bsize];
895       if ((*bh <= rows_left) && (*bw <= cols_left)) {
896         break;
897       }
898     }
899   }
900   return bsize;
901 }
902
903 // This function attempts to set all mode info entries in a given SB64
904 // to the same block partition size.
905 // However, at the bottom and right borders of the image the requested size
906 // may not be allowed in which case this code attempts to choose the largest
907 // allowable partition.
908 static void set_partitioning(VP9_COMP *cpi, const TileInfo *const tile,
909                              MODE_INFO **mi_8x8, int mi_row, int mi_col) {
910   VP9_COMMON *const cm = &cpi->common;
911   BLOCK_SIZE bsize = cpi->sf.always_this_block_size;
912   const int mis = cm->mode_info_stride;
913   int row8x8_remaining = tile->mi_row_end - mi_row;
914   int col8x8_remaining = tile->mi_col_end - mi_col;
915   int block_row, block_col;
916   MODE_INFO * mi_upper_left = cm->mi + mi_row * mis + mi_col;
917   int bh = num_8x8_blocks_high_lookup[bsize];
918   int bw = num_8x8_blocks_wide_lookup[bsize];
919
920   assert((row8x8_remaining > 0) && (col8x8_remaining > 0));
921
922   // Apply the requested partition size to the SB64 if it is all "in image"
923   if ((col8x8_remaining >= MI_BLOCK_SIZE) &&
924       (row8x8_remaining >= MI_BLOCK_SIZE)) {
925     for (block_row = 0; block_row < MI_BLOCK_SIZE; block_row += bh) {
926       for (block_col = 0; block_col < MI_BLOCK_SIZE; block_col += bw) {
927         int index = block_row * mis + block_col;
928         mi_8x8[index] = mi_upper_left + index;
929         mi_8x8[index]->mbmi.sb_type = bsize;
930       }
931     }
932   } else {
933     // Else this is a partial SB64.
934     for (block_row = 0; block_row < MI_BLOCK_SIZE; block_row += bh) {
935       for (block_col = 0; block_col < MI_BLOCK_SIZE; block_col += bw) {
936         int index = block_row * mis + block_col;
937         // Find a partition size that fits
938         bsize = find_partition_size(cpi->sf.always_this_block_size,
939                                     (row8x8_remaining - block_row),
940                                     (col8x8_remaining - block_col), &bh, &bw);
941         mi_8x8[index] = mi_upper_left + index;
942         mi_8x8[index]->mbmi.sb_type = bsize;
943       }
944     }
945   }
946 }
947
948 static void copy_partitioning(VP9_COMP *cpi, MODE_INFO **mi_8x8,
949                               MODE_INFO **prev_mi_8x8) {
950   VP9_COMMON *const cm = &cpi->common;
951   const int mis = cm->mode_info_stride;
952   int block_row, block_col;
953
954   for (block_row = 0; block_row < 8; ++block_row) {
955     for (block_col = 0; block_col < 8; ++block_col) {
956       MODE_INFO * prev_mi = prev_mi_8x8[block_row * mis + block_col];
957       BLOCK_SIZE sb_type = prev_mi ? prev_mi->mbmi.sb_type : 0;
958       ptrdiff_t offset;
959
960       if (prev_mi) {
961         offset = prev_mi - cm->prev_mi;
962         mi_8x8[block_row * mis + block_col] = cm->mi + offset;
963         mi_8x8[block_row * mis + block_col]->mbmi.sb_type = sb_type;
964       }
965     }
966   }
967 }
968
969 static int sb_has_motion(VP9_COMP *cpi, MODE_INFO **prev_mi_8x8) {
970   VP9_COMMON *const cm = &cpi->common;
971   const int mis = cm->mode_info_stride;
972   int block_row, block_col;
973
974   if (cm->prev_mi) {
975     for (block_row = 0; block_row < 8; ++block_row) {
976       for (block_col = 0; block_col < 8; ++block_col) {
977         MODE_INFO * prev_mi = prev_mi_8x8[block_row * mis + block_col];
978         if (prev_mi) {
979           if (abs(prev_mi->mbmi.mv[0].as_mv.row) >= 8 ||
980               abs(prev_mi->mbmi.mv[0].as_mv.col) >= 8)
981             return 1;
982         }
983       }
984     }
985   }
986   return 0;
987 }
988
989 static void rd_use_partition(VP9_COMP *cpi,
990                              const TileInfo *const tile,
991                              MODE_INFO **mi_8x8,
992                              TOKENEXTRA **tp, int mi_row, int mi_col,
993                              BLOCK_SIZE bsize, int *rate, int64_t *dist,
994                              int do_recon) {
995   VP9_COMMON *const cm = &cpi->common;
996   MACROBLOCK *const x = &cpi->mb;
997   const int mis = cm->mode_info_stride;
998   int bsl = b_width_log2(bsize);
999   const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
1000   const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
1001   int ms = num_4x4_blocks_wide / 2;
1002   int mh = num_4x4_blocks_high / 2;
1003   int bss = (1 << bsl) / 4;
1004   int i, pl;
1005   PARTITION_TYPE partition = PARTITION_NONE;
1006   BLOCK_SIZE subsize;
1007   ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
1008   PARTITION_CONTEXT sl[8], sa[8];
1009   int last_part_rate = INT_MAX;
1010   int64_t last_part_dist = INT_MAX;
1011   int split_rate = INT_MAX;
1012   int64_t split_dist = INT_MAX;
1013   int none_rate = INT_MAX;
1014   int64_t none_dist = INT_MAX;
1015   int chosen_rate = INT_MAX;
1016   int64_t chosen_dist = INT_MAX;
1017   BLOCK_SIZE sub_subsize = BLOCK_4X4;
1018   int splits_below = 0;
1019   BLOCK_SIZE bs_type = mi_8x8[0]->mbmi.sb_type;
1020
1021   if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
1022     return;
1023
1024   partition = partition_lookup[bsl][bs_type];
1025
1026   subsize = get_subsize(bsize, partition);
1027
1028   if (bsize < BLOCK_8X8) {
1029     // When ab_index = 0 all sub-blocks are handled, so for ab_index != 0
1030     // there is nothing to be done.
1031     if (x->ab_index != 0) {
1032       *rate = 0;
1033       *dist = 0;
1034       return;
1035     }
1036   } else {
1037     *(get_sb_partitioning(x, bsize)) = subsize;
1038   }
1039   save_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1040
1041   if (bsize == BLOCK_16X16) {
1042     set_offsets(cpi, tile, mi_row, mi_col, bsize);
1043     x->mb_energy = vp9_block_energy(cpi, x, bsize);
1044   }
1045
1046   x->fast_ms = 0;
1047   x->subblock_ref = 0;
1048
1049   if (cpi->sf.adjust_partitioning_from_last_frame) {
1050     // Check if any of the sub blocks are further split.
1051     if (partition == PARTITION_SPLIT && subsize > BLOCK_8X8) {
1052       sub_subsize = get_subsize(subsize, PARTITION_SPLIT);
1053       splits_below = 1;
1054       for (i = 0; i < 4; i++) {
1055         int jj = i >> 1, ii = i & 0x01;
1056         MODE_INFO * this_mi = mi_8x8[jj * bss * mis + ii * bss];
1057         if (this_mi && this_mi->mbmi.sb_type >= sub_subsize) {
1058           splits_below = 0;
1059         }
1060       }
1061     }
1062
1063     // If partition is not none try none unless each of the 4 splits are split
1064     // even further..
1065     if (partition != PARTITION_NONE && !splits_below &&
1066         mi_row + (ms >> 1) < cm->mi_rows &&
1067         mi_col + (ms >> 1) < cm->mi_cols) {
1068       *(get_sb_partitioning(x, bsize)) = bsize;
1069       pick_sb_modes(cpi, tile, mi_row, mi_col, &none_rate, &none_dist, bsize,
1070                     get_block_context(x, bsize), INT64_MAX);
1071
1072       pl = partition_plane_context(cpi->above_seg_context,
1073                                    cpi->left_seg_context,
1074                                    mi_row, mi_col, bsize);
1075       none_rate += x->partition_cost[pl][PARTITION_NONE];
1076
1077       restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1078       mi_8x8[0]->mbmi.sb_type = bs_type;
1079       *(get_sb_partitioning(x, bsize)) = subsize;
1080     }
1081   }
1082
1083   switch (partition) {
1084     case PARTITION_NONE:
1085       pick_sb_modes(cpi, tile, mi_row, mi_col, &last_part_rate, &last_part_dist,
1086                     bsize, get_block_context(x, bsize), INT64_MAX);
1087       break;
1088     case PARTITION_HORZ:
1089       *get_sb_index(x, subsize) = 0;
1090       pick_sb_modes(cpi, tile, mi_row, mi_col, &last_part_rate, &last_part_dist,
1091                     subsize, get_block_context(x, subsize), INT64_MAX);
1092       if (last_part_rate != INT_MAX &&
1093           bsize >= BLOCK_8X8 && mi_row + (mh >> 1) < cm->mi_rows) {
1094         int rt = 0;
1095         int64_t dt = 0;
1096         update_state(cpi, get_block_context(x, subsize), subsize, 0);
1097         encode_superblock(cpi, tp, 0, mi_row, mi_col, subsize);
1098         *get_sb_index(x, subsize) = 1;
1099         pick_sb_modes(cpi, tile, mi_row + (ms >> 1), mi_col, &rt, &dt, subsize,
1100                       get_block_context(x, subsize), INT64_MAX);
1101         if (rt == INT_MAX || dt == INT_MAX) {
1102           last_part_rate = INT_MAX;
1103           last_part_dist = INT_MAX;
1104           break;
1105         }
1106
1107         last_part_rate += rt;
1108         last_part_dist += dt;
1109       }
1110       break;
1111     case PARTITION_VERT:
1112       *get_sb_index(x, subsize) = 0;
1113       pick_sb_modes(cpi, tile, mi_row, mi_col, &last_part_rate, &last_part_dist,
1114                     subsize, get_block_context(x, subsize), INT64_MAX);
1115       if (last_part_rate != INT_MAX &&
1116           bsize >= BLOCK_8X8 && mi_col + (ms >> 1) < cm->mi_cols) {
1117         int rt = 0;
1118         int64_t dt = 0;
1119         update_state(cpi, get_block_context(x, subsize), subsize, 0);
1120         encode_superblock(cpi, tp, 0, mi_row, mi_col, subsize);
1121         *get_sb_index(x, subsize) = 1;
1122         pick_sb_modes(cpi, tile, mi_row, mi_col + (ms >> 1), &rt, &dt, subsize,
1123                       get_block_context(x, subsize), INT64_MAX);
1124         if (rt == INT_MAX || dt == INT_MAX) {
1125           last_part_rate = INT_MAX;
1126           last_part_dist = INT_MAX;
1127           break;
1128         }
1129         last_part_rate += rt;
1130         last_part_dist += dt;
1131       }
1132       break;
1133     case PARTITION_SPLIT:
1134       // Split partition.
1135       last_part_rate = 0;
1136       last_part_dist = 0;
1137       for (i = 0; i < 4; i++) {
1138         int x_idx = (i & 1) * (ms >> 1);
1139         int y_idx = (i >> 1) * (ms >> 1);
1140         int jj = i >> 1, ii = i & 0x01;
1141         int rt;
1142         int64_t dt;
1143
1144         if ((mi_row + y_idx >= cm->mi_rows) || (mi_col + x_idx >= cm->mi_cols))
1145           continue;
1146
1147         *get_sb_index(x, subsize) = i;
1148
1149         rd_use_partition(cpi, tile, mi_8x8 + jj * bss * mis + ii * bss, tp,
1150                          mi_row + y_idx, mi_col + x_idx, subsize, &rt, &dt,
1151                          i != 3);
1152         if (rt == INT_MAX || dt == INT_MAX) {
1153           last_part_rate = INT_MAX;
1154           last_part_dist = INT_MAX;
1155           break;
1156         }
1157         last_part_rate += rt;
1158         last_part_dist += dt;
1159       }
1160       break;
1161     default:
1162       assert(0);
1163   }
1164
1165   pl = partition_plane_context(cpi->above_seg_context, cpi->left_seg_context,
1166                                mi_row, mi_col, bsize);
1167   if (last_part_rate < INT_MAX)
1168     last_part_rate += x->partition_cost[pl][partition];
1169
1170   if (cpi->sf.adjust_partitioning_from_last_frame
1171       && partition != PARTITION_SPLIT && bsize > BLOCK_8X8
1172       && (mi_row + ms < cm->mi_rows || mi_row + (ms >> 1) == cm->mi_rows)
1173       && (mi_col + ms < cm->mi_cols || mi_col + (ms >> 1) == cm->mi_cols)) {
1174     BLOCK_SIZE split_subsize = get_subsize(bsize, PARTITION_SPLIT);
1175     split_rate = 0;
1176     split_dist = 0;
1177     restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1178
1179     // Split partition.
1180     for (i = 0; i < 4; i++) {
1181       int x_idx = (i & 1) * (num_4x4_blocks_wide >> 2);
1182       int y_idx = (i >> 1) * (num_4x4_blocks_wide >> 2);
1183       int rt = 0;
1184       int64_t dt = 0;
1185       ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
1186       PARTITION_CONTEXT sl[8], sa[8];
1187
1188       if ((mi_row + y_idx >= cm->mi_rows) || (mi_col + x_idx >= cm->mi_cols))
1189         continue;
1190
1191       *get_sb_index(x, split_subsize) = i;
1192       *get_sb_partitioning(x, bsize) = split_subsize;
1193       *get_sb_partitioning(x, split_subsize) = split_subsize;
1194
1195       save_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1196
1197       pick_sb_modes(cpi, tile, mi_row + y_idx, mi_col + x_idx, &rt, &dt,
1198                     split_subsize, get_block_context(x, split_subsize),
1199                     INT64_MAX);
1200
1201       restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1202
1203       if (rt == INT_MAX || dt == INT_MAX) {
1204         split_rate = INT_MAX;
1205         split_dist = INT_MAX;
1206         break;
1207       }
1208
1209       if (i != 3)
1210         encode_sb(cpi, tile, tp,  mi_row + y_idx, mi_col + x_idx, 0,
1211                   split_subsize);
1212
1213       split_rate += rt;
1214       split_dist += dt;
1215       pl = partition_plane_context(cpi->above_seg_context,
1216                                    cpi->left_seg_context,
1217                                    mi_row + y_idx, mi_col + x_idx, bsize);
1218       split_rate += x->partition_cost[pl][PARTITION_NONE];
1219     }
1220     pl = partition_plane_context(cpi->above_seg_context, cpi->left_seg_context,
1221                                  mi_row, mi_col, bsize);
1222     if (split_rate < INT_MAX) {
1223       split_rate += x->partition_cost[pl][PARTITION_SPLIT];
1224
1225       chosen_rate = split_rate;
1226       chosen_dist = split_dist;
1227     }
1228   }
1229
1230   // If last_part is better set the partitioning to that...
1231   if (RDCOST(x->rdmult, x->rddiv, last_part_rate, last_part_dist)
1232       < RDCOST(x->rdmult, x->rddiv, chosen_rate, chosen_dist)) {
1233     mi_8x8[0]->mbmi.sb_type = bsize;
1234     if (bsize >= BLOCK_8X8)
1235       *(get_sb_partitioning(x, bsize)) = subsize;
1236     chosen_rate = last_part_rate;
1237     chosen_dist = last_part_dist;
1238   }
1239   // If none was better set the partitioning to that...
1240   if (RDCOST(x->rdmult, x->rddiv, chosen_rate, chosen_dist)
1241       > RDCOST(x->rdmult, x->rddiv, none_rate, none_dist)) {
1242     if (bsize >= BLOCK_8X8)
1243       *(get_sb_partitioning(x, bsize)) = bsize;
1244     chosen_rate = none_rate;
1245     chosen_dist = none_dist;
1246   }
1247
1248   restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1249
1250   // We must have chosen a partitioning and encoding or we'll fail later on.
1251   // No other opportunities for success.
1252   if ( bsize == BLOCK_64X64)
1253     assert(chosen_rate < INT_MAX && chosen_dist < INT_MAX);
1254
1255   if (do_recon)
1256     encode_sb(cpi, tile, tp, mi_row, mi_col, bsize == BLOCK_64X64, bsize);
1257
1258   *rate = chosen_rate;
1259   *dist = chosen_dist;
1260 }
1261
1262 static const BLOCK_SIZE min_partition_size[BLOCK_SIZES] = {
1263   BLOCK_4X4, BLOCK_4X4, BLOCK_4X4, BLOCK_4X4,
1264   BLOCK_4X4, BLOCK_4X4, BLOCK_8X8, BLOCK_8X8,
1265   BLOCK_8X8, BLOCK_16X16, BLOCK_16X16, BLOCK_16X16, BLOCK_16X16
1266 };
1267
1268 static const BLOCK_SIZE max_partition_size[BLOCK_SIZES] = {
1269   BLOCK_8X8, BLOCK_16X16, BLOCK_16X16, BLOCK_16X16,
1270   BLOCK_32X32, BLOCK_32X32, BLOCK_32X32, BLOCK_64X64,
1271   BLOCK_64X64, BLOCK_64X64, BLOCK_64X64, BLOCK_64X64, BLOCK_64X64
1272 };
1273
1274 // Look at all the mode_info entries for blocks that are part of this
1275 // partition and find the min and max values for sb_type.
1276 // At the moment this is designed to work on a 64x64 SB but could be
1277 // adjusted to use a size parameter.
1278 //
1279 // The min and max are assumed to have been initialized prior to calling this
1280 // function so repeat calls can accumulate a min and max of more than one sb64.
1281 static void get_sb_partition_size_range(VP9_COMP *cpi, MODE_INFO ** mi_8x8,
1282                                         BLOCK_SIZE * min_block_size,
1283                                         BLOCK_SIZE * max_block_size ) {
1284   MACROBLOCKD *const xd = &cpi->mb.e_mbd;
1285   int sb_width_in_blocks = MI_BLOCK_SIZE;
1286   int sb_height_in_blocks  = MI_BLOCK_SIZE;
1287   int i, j;
1288   int index = 0;
1289
1290   // Check the sb_type for each block that belongs to this region.
1291   for (i = 0; i < sb_height_in_blocks; ++i) {
1292     for (j = 0; j < sb_width_in_blocks; ++j) {
1293       MODE_INFO * mi = mi_8x8[index+j];
1294       BLOCK_SIZE sb_type = mi ? mi->mbmi.sb_type : 0;
1295       *min_block_size = MIN(*min_block_size, sb_type);
1296       *max_block_size = MAX(*max_block_size, sb_type);
1297     }
1298     index += xd->mode_info_stride;
1299   }
1300 }
1301
1302 // Look at neighboring blocks and set a min and max partition size based on
1303 // what they chose.
1304 static void rd_auto_partition_range(VP9_COMP *cpi, const TileInfo *const tile,
1305                                     int row, int col,
1306                                     BLOCK_SIZE *min_block_size,
1307                                     BLOCK_SIZE *max_block_size) {
1308   VP9_COMMON * const cm = &cpi->common;
1309   MACROBLOCKD *const xd = &cpi->mb.e_mbd;
1310   MODE_INFO ** mi_8x8 = xd->mi_8x8;
1311   MODE_INFO ** prev_mi_8x8 = xd->prev_mi_8x8;
1312
1313   const int left_in_image = xd->left_available && mi_8x8[-1];
1314   const int above_in_image = xd->up_available &&
1315                              mi_8x8[-xd->mode_info_stride];
1316   MODE_INFO ** above_sb64_mi_8x8;
1317   MODE_INFO ** left_sb64_mi_8x8;
1318
1319   int row8x8_remaining = tile->mi_row_end - row;
1320   int col8x8_remaining = tile->mi_col_end - col;
1321   int bh, bw;
1322
1323   // Trap case where we do not have a prediction.
1324   if (!left_in_image && !above_in_image &&
1325       ((cm->frame_type == KEY_FRAME) || !cm->prev_mi)) {
1326     *min_block_size = BLOCK_4X4;
1327     *max_block_size = BLOCK_64X64;
1328   } else {
1329     // Default "min to max" and "max to min"
1330     *min_block_size = BLOCK_64X64;
1331     *max_block_size = BLOCK_4X4;
1332
1333     // NOTE: each call to get_sb_partition_size_range() uses the previous
1334     // passed in values for min and max as a starting point.
1335     //
1336     // Find the min and max partition used in previous frame at this location
1337     if (cm->prev_mi && (cm->frame_type != KEY_FRAME)) {
1338       get_sb_partition_size_range(cpi, prev_mi_8x8,
1339                                   min_block_size, max_block_size);
1340     }
1341
1342     // Find the min and max partition sizes used in the left SB64
1343     if (left_in_image) {
1344       left_sb64_mi_8x8 = &mi_8x8[-MI_BLOCK_SIZE];
1345       get_sb_partition_size_range(cpi, left_sb64_mi_8x8,
1346                                   min_block_size, max_block_size);
1347     }
1348
1349     // Find the min and max partition sizes used in the above SB64.
1350     if (above_in_image) {
1351       above_sb64_mi_8x8 = &mi_8x8[-xd->mode_info_stride * MI_BLOCK_SIZE];
1352       get_sb_partition_size_range(cpi, above_sb64_mi_8x8,
1353                                   min_block_size, max_block_size);
1354     }
1355   }
1356
1357   // Give a bit of leaway either side of the observed min and max
1358   *min_block_size = min_partition_size[*min_block_size];
1359   *max_block_size = max_partition_size[*max_block_size];
1360
1361   // Check border cases where max and min from neighbours may not be legal.
1362   *max_block_size = find_partition_size(*max_block_size,
1363                                         row8x8_remaining, col8x8_remaining,
1364                                         &bh, &bw);
1365   *min_block_size = MIN(*min_block_size, *max_block_size);
1366 }
1367
1368 static void compute_fast_motion_search_level(VP9_COMP *cpi, BLOCK_SIZE bsize) {
1369   VP9_COMMON *const cm = &cpi->common;
1370   MACROBLOCK *const x = &cpi->mb;
1371
1372   // Only use 8x8 result for non HD videos.
1373   // int use_8x8 = (MIN(cpi->common.width, cpi->common.height) < 720) ? 1 : 0;
1374   int use_8x8 = 1;
1375
1376   if (cm->frame_type && !cpi->is_src_frame_alt_ref &&
1377       ((use_8x8 && bsize == BLOCK_16X16) ||
1378       bsize == BLOCK_32X32 || bsize == BLOCK_64X64)) {
1379     int ref0 = 0, ref1 = 0, ref2 = 0, ref3 = 0;
1380     PICK_MODE_CONTEXT *block_context = NULL;
1381
1382     if (bsize == BLOCK_16X16) {
1383       block_context = x->sb8x8_context[x->sb_index][x->mb_index];
1384     } else if (bsize == BLOCK_32X32) {
1385       block_context = x->mb_context[x->sb_index];
1386     } else if (bsize == BLOCK_64X64) {
1387       block_context = x->sb32_context;
1388     }
1389
1390     if (block_context) {
1391       ref0 = block_context[0].mic.mbmi.ref_frame[0];
1392       ref1 = block_context[1].mic.mbmi.ref_frame[0];
1393       ref2 = block_context[2].mic.mbmi.ref_frame[0];
1394       ref3 = block_context[3].mic.mbmi.ref_frame[0];
1395     }
1396
1397     // Currently, only consider 4 inter reference frames.
1398     if (ref0 && ref1 && ref2 && ref3) {
1399       int d01, d23, d02, d13;
1400
1401       // Motion vectors for the four subblocks.
1402       int16_t mvr0 = block_context[0].mic.mbmi.mv[0].as_mv.row;
1403       int16_t mvc0 = block_context[0].mic.mbmi.mv[0].as_mv.col;
1404       int16_t mvr1 = block_context[1].mic.mbmi.mv[0].as_mv.row;
1405       int16_t mvc1 = block_context[1].mic.mbmi.mv[0].as_mv.col;
1406       int16_t mvr2 = block_context[2].mic.mbmi.mv[0].as_mv.row;
1407       int16_t mvc2 = block_context[2].mic.mbmi.mv[0].as_mv.col;
1408       int16_t mvr3 = block_context[3].mic.mbmi.mv[0].as_mv.row;
1409       int16_t mvc3 = block_context[3].mic.mbmi.mv[0].as_mv.col;
1410
1411       // Adjust sign if ref is alt_ref.
1412       if (cm->ref_frame_sign_bias[ref0]) {
1413         mvr0 *= -1;
1414         mvc0 *= -1;
1415       }
1416
1417       if (cm->ref_frame_sign_bias[ref1]) {
1418         mvr1 *= -1;
1419         mvc1 *= -1;
1420       }
1421
1422       if (cm->ref_frame_sign_bias[ref2]) {
1423         mvr2 *= -1;
1424         mvc2 *= -1;
1425       }
1426
1427       if (cm->ref_frame_sign_bias[ref3]) {
1428         mvr3 *= -1;
1429         mvc3 *= -1;
1430       }
1431
1432       // Calculate mv distances.
1433       d01 = MAX(abs(mvr0 - mvr1), abs(mvc0 - mvc1));
1434       d23 = MAX(abs(mvr2 - mvr3), abs(mvc2 - mvc3));
1435       d02 = MAX(abs(mvr0 - mvr2), abs(mvc0 - mvc2));
1436       d13 = MAX(abs(mvr1 - mvr3), abs(mvc1 - mvc3));
1437
1438       if (d01 < FAST_MOTION_MV_THRESH && d23 < FAST_MOTION_MV_THRESH &&
1439           d02 < FAST_MOTION_MV_THRESH && d13 < FAST_MOTION_MV_THRESH) {
1440         // Set fast motion search level.
1441         x->fast_ms = 1;
1442
1443         if (ref0 == ref1 && ref1 == ref2 && ref2 == ref3 &&
1444             d01 < 2 && d23 < 2 && d02 < 2 && d13 < 2) {
1445           // Set fast motion search level.
1446           x->fast_ms = 2;
1447
1448           if (!d01 && !d23 && !d02 && !d13) {
1449             x->fast_ms = 3;
1450             x->subblock_ref = ref0;
1451           }
1452         }
1453       }
1454     }
1455   }
1456 }
1457
1458 static INLINE void store_pred_mv(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx) {
1459   vpx_memcpy(ctx->pred_mv, x->pred_mv, sizeof(x->pred_mv));
1460 }
1461
1462 static INLINE void load_pred_mv(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx) {
1463   vpx_memcpy(x->pred_mv, ctx->pred_mv, sizeof(x->pred_mv));
1464 }
1465
1466 // TODO(jingning,jimbankoski,rbultje): properly skip partition types that are
1467 // unlikely to be selected depending on previous rate-distortion optimization
1468 // results, for encoding speed-up.
1469 static void rd_pick_partition(VP9_COMP *cpi, const TileInfo *const tile,
1470                               TOKENEXTRA **tp, int mi_row,
1471                               int mi_col, BLOCK_SIZE bsize, int *rate,
1472                               int64_t *dist, int do_recon, int64_t best_rd) {
1473   VP9_COMMON *const cm = &cpi->common;
1474   MACROBLOCK *const x = &cpi->mb;
1475   const int ms = num_8x8_blocks_wide_lookup[bsize] / 2;
1476   ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
1477   PARTITION_CONTEXT sl[8], sa[8];
1478   TOKENEXTRA *tp_orig = *tp;
1479   int i, pl;
1480   BLOCK_SIZE subsize;
1481   int this_rate, sum_rate = 0, best_rate = INT_MAX;
1482   int64_t this_dist, sum_dist = 0, best_dist = INT64_MAX;
1483   int64_t sum_rd = 0;
1484   int do_split = bsize >= BLOCK_8X8;
1485   int do_rect = 1;
1486   // Override skipping rectangular partition operations for edge blocks
1487   const int force_horz_split = (mi_row + ms >= cm->mi_rows);
1488   const int force_vert_split = (mi_col + ms >= cm->mi_cols);
1489
1490   int partition_none_allowed = !force_horz_split && !force_vert_split;
1491   int partition_horz_allowed = !force_vert_split && bsize >= BLOCK_8X8;
1492   int partition_vert_allowed = !force_horz_split && bsize >= BLOCK_8X8;
1493
1494   int partition_split_done = 0;
1495   (void) *tp_orig;
1496
1497   if (bsize < BLOCK_8X8) {
1498     // When ab_index = 0 all sub-blocks are handled, so for ab_index != 0
1499     // there is nothing to be done.
1500     if (x->ab_index != 0) {
1501       *rate = 0;
1502       *dist = 0;
1503       return;
1504     }
1505   }
1506   assert(num_8x8_blocks_wide_lookup[bsize] ==
1507              num_8x8_blocks_high_lookup[bsize]);
1508
1509   if (bsize == BLOCK_16X16) {
1510     set_offsets(cpi, tile, mi_row, mi_col, bsize);
1511     x->mb_energy = vp9_block_energy(cpi, x, bsize);
1512   }
1513
1514   // Determine partition types in search according to the speed features.
1515   // The threshold set here has to be of square block size.
1516   if (cpi->sf.auto_min_max_partition_size) {
1517     partition_none_allowed &= (bsize <= cpi->sf.max_partition_size &&
1518                                bsize >= cpi->sf.min_partition_size);
1519     partition_horz_allowed &= ((bsize <= cpi->sf.max_partition_size &&
1520                                 bsize >  cpi->sf.min_partition_size) ||
1521                                 force_horz_split);
1522     partition_vert_allowed &= ((bsize <= cpi->sf.max_partition_size &&
1523                                 bsize >  cpi->sf.min_partition_size) ||
1524                                 force_vert_split);
1525     do_split &= bsize > cpi->sf.min_partition_size;
1526   }
1527   if (cpi->sf.use_square_partition_only) {
1528     partition_horz_allowed &= force_horz_split;
1529     partition_vert_allowed &= force_vert_split;
1530   }
1531
1532   save_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1533
1534   if (cpi->sf.disable_split_var_thresh && partition_none_allowed) {
1535     unsigned int source_variancey;
1536     vp9_setup_src_planes(x, cpi->Source, mi_row, mi_col);
1537     source_variancey = get_sby_perpixel_variance(cpi, x, bsize);
1538     if (source_variancey < cpi->sf.disable_split_var_thresh) {
1539       do_split = 0;
1540       if (source_variancey < cpi->sf.disable_split_var_thresh / 2)
1541         do_rect = 0;
1542     }
1543   }
1544
1545   // PARTITION_NONE
1546   if (partition_none_allowed) {
1547     pick_sb_modes(cpi, tile, mi_row, mi_col, &this_rate, &this_dist, bsize,
1548                   get_block_context(x, bsize), best_rd);
1549     if (this_rate != INT_MAX) {
1550       if (bsize >= BLOCK_8X8) {
1551         pl = partition_plane_context(cpi->above_seg_context,
1552                                      cpi->left_seg_context,
1553                                      mi_row, mi_col, bsize);
1554         this_rate += x->partition_cost[pl][PARTITION_NONE];
1555       }
1556       sum_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_dist);
1557       if (sum_rd < best_rd) {
1558         int64_t stop_thresh = 2048;
1559
1560         best_rate = this_rate;
1561         best_dist = this_dist;
1562         best_rd = sum_rd;
1563         if (bsize >= BLOCK_8X8)
1564           *(get_sb_partitioning(x, bsize)) = bsize;
1565
1566         // Adjust threshold according to partition size.
1567         stop_thresh >>= 8 - (b_width_log2_lookup[bsize] +
1568             b_height_log2_lookup[bsize]);
1569
1570         // If obtained distortion is very small, choose current partition
1571         // and stop splitting.
1572         if (this_dist < stop_thresh) {
1573           do_split = 0;
1574           do_rect = 0;
1575         }
1576       }
1577     }
1578     restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1579   }
1580
1581   // store estimated motion vector
1582   if (cpi->sf.adaptive_motion_search)
1583     store_pred_mv(x, get_block_context(x, bsize));
1584
1585   // PARTITION_SPLIT
1586   sum_rd = 0;
1587   // TODO(jingning): use the motion vectors given by the above search as
1588   // the starting point of motion search in the following partition type check.
1589   if (do_split) {
1590     subsize = get_subsize(bsize, PARTITION_SPLIT);
1591     for (i = 0; i < 4 && sum_rd < best_rd; ++i) {
1592       const int x_idx = (i & 1) * ms;
1593       const int y_idx = (i >> 1) * ms;
1594
1595       if (mi_row + y_idx >= cm->mi_rows || mi_col + x_idx >= cm->mi_cols)
1596         continue;
1597
1598       *get_sb_index(x, subsize) = i;
1599       if (cpi->sf.adaptive_motion_search)
1600         load_pred_mv(x, get_block_context(x, bsize));
1601       rd_pick_partition(cpi, tile, tp, mi_row + y_idx, mi_col + x_idx, subsize,
1602                         &this_rate, &this_dist, i != 3, best_rd - sum_rd);
1603
1604       if (this_rate == INT_MAX) {
1605         sum_rd = INT64_MAX;
1606       } else {
1607         sum_rate += this_rate;
1608         sum_dist += this_dist;
1609         sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
1610       }
1611     }
1612     if (sum_rd < best_rd && i == 4) {
1613       pl = partition_plane_context(cpi->above_seg_context,
1614                                    cpi->left_seg_context,
1615                                    mi_row, mi_col, bsize);
1616       sum_rate += x->partition_cost[pl][PARTITION_SPLIT];
1617       sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
1618       if (sum_rd < best_rd) {
1619         best_rate = sum_rate;
1620         best_dist = sum_dist;
1621         best_rd = sum_rd;
1622         *(get_sb_partitioning(x, bsize)) = subsize;
1623       }
1624     } else {
1625       // skip rectangular partition test when larger block size
1626       // gives better rd cost
1627       if (cpi->sf.less_rectangular_check)
1628         do_rect &= !partition_none_allowed;
1629     }
1630     partition_split_done = 1;
1631     restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1632   }
1633
1634   x->fast_ms = 0;
1635   x->subblock_ref = 0;
1636
1637   if (partition_split_done &&
1638       cpi->sf.using_small_partition_info) {
1639     compute_fast_motion_search_level(cpi, bsize);
1640   }
1641
1642   // PARTITION_HORZ
1643   if (partition_horz_allowed && do_rect) {
1644     subsize = get_subsize(bsize, PARTITION_HORZ);
1645     *get_sb_index(x, subsize) = 0;
1646     if (cpi->sf.adaptive_motion_search)
1647       load_pred_mv(x, get_block_context(x, bsize));
1648     pick_sb_modes(cpi, tile, mi_row, mi_col, &sum_rate, &sum_dist, subsize,
1649                   get_block_context(x, subsize), best_rd);
1650     sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
1651
1652     if (sum_rd < best_rd && mi_row + ms < cm->mi_rows) {
1653       update_state(cpi, get_block_context(x, subsize), subsize, 0);
1654       encode_superblock(cpi, tp, 0, mi_row, mi_col, subsize);
1655
1656       *get_sb_index(x, subsize) = 1;
1657       if (cpi->sf.adaptive_motion_search)
1658         load_pred_mv(x, get_block_context(x, bsize));
1659       pick_sb_modes(cpi, tile, mi_row + ms, mi_col, &this_rate,
1660                     &this_dist, subsize, get_block_context(x, subsize),
1661                     best_rd - sum_rd);
1662       if (this_rate == INT_MAX) {
1663         sum_rd = INT64_MAX;
1664       } else {
1665         sum_rate += this_rate;
1666         sum_dist += this_dist;
1667         sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
1668       }
1669     }
1670     if (sum_rd < best_rd) {
1671       pl = partition_plane_context(cpi->above_seg_context,
1672                                    cpi->left_seg_context,
1673                                    mi_row, mi_col, bsize);
1674       sum_rate += x->partition_cost[pl][PARTITION_HORZ];
1675       sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
1676       if (sum_rd < best_rd) {
1677         best_rd = sum_rd;
1678         best_rate = sum_rate;
1679         best_dist = sum_dist;
1680         *(get_sb_partitioning(x, bsize)) = subsize;
1681       }
1682     }
1683     restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1684   }
1685
1686   // PARTITION_VERT
1687   if (partition_vert_allowed && do_rect) {
1688     subsize = get_subsize(bsize, PARTITION_VERT);
1689
1690     *get_sb_index(x, subsize) = 0;
1691     if (cpi->sf.adaptive_motion_search)
1692       load_pred_mv(x, get_block_context(x, bsize));
1693     pick_sb_modes(cpi, tile, mi_row, mi_col, &sum_rate, &sum_dist, subsize,
1694                   get_block_context(x, subsize), best_rd);
1695     sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
1696     if (sum_rd < best_rd && mi_col + ms < cm->mi_cols) {
1697       update_state(cpi, get_block_context(x, subsize), subsize, 0);
1698       encode_superblock(cpi, tp, 0, mi_row, mi_col, subsize);
1699
1700       *get_sb_index(x, subsize) = 1;
1701       if (cpi->sf.adaptive_motion_search)
1702         load_pred_mv(x, get_block_context(x, bsize));
1703       pick_sb_modes(cpi, tile, mi_row, mi_col + ms, &this_rate,
1704                     &this_dist, subsize, get_block_context(x, subsize),
1705                     best_rd - sum_rd);
1706       if (this_rate == INT_MAX) {
1707         sum_rd = INT64_MAX;
1708       } else {
1709         sum_rate += this_rate;
1710         sum_dist += this_dist;
1711         sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
1712       }
1713     }
1714     if (sum_rd < best_rd) {
1715       pl = partition_plane_context(cpi->above_seg_context,
1716                                    cpi->left_seg_context,
1717                                    mi_row, mi_col, bsize);
1718       sum_rate += x->partition_cost[pl][PARTITION_VERT];
1719       sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
1720       if (sum_rd < best_rd) {
1721         best_rate = sum_rate;
1722         best_dist = sum_dist;
1723         best_rd = sum_rd;
1724         *(get_sb_partitioning(x, bsize)) = subsize;
1725       }
1726     }
1727     restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1728   }
1729
1730
1731   *rate = best_rate;
1732   *dist = best_dist;
1733
1734   if (best_rate < INT_MAX && best_dist < INT64_MAX && do_recon)
1735     encode_sb(cpi, tile, tp, mi_row, mi_col, bsize == BLOCK_64X64, bsize);
1736   if (bsize == BLOCK_64X64) {
1737     assert(tp_orig < *tp);
1738     assert(best_rate < INT_MAX);
1739     assert(best_dist < INT_MAX);
1740   } else {
1741     assert(tp_orig == *tp);
1742   }
1743 }
1744
1745 // Examines 64x64 block and chooses a best reference frame
1746 static void rd_pick_reference_frame(VP9_COMP *cpi, const TileInfo *const tile,
1747                                     int mi_row, int mi_col) {
1748   VP9_COMMON * const cm = &cpi->common;
1749   MACROBLOCK * const x = &cpi->mb;
1750   int bsl = b_width_log2(BLOCK_64X64), bs = 1 << bsl;
1751   int ms = bs / 2;
1752   ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
1753   PARTITION_CONTEXT sl[8], sa[8];
1754   int pl;
1755   int r;
1756   int64_t d;
1757
1758   save_context(cpi, mi_row, mi_col, a, l, sa, sl, BLOCK_64X64);
1759
1760   // Default is non mask (all reference frames allowed.
1761   cpi->ref_frame_mask = 0;
1762
1763   // Do RD search for 64x64.
1764   if ((mi_row + (ms >> 1) < cm->mi_rows) &&
1765       (mi_col + (ms >> 1) < cm->mi_cols)) {
1766     cpi->set_ref_frame_mask = 1;
1767     pick_sb_modes(cpi, tile, mi_row, mi_col, &r, &d, BLOCK_64X64,
1768                   get_block_context(x, BLOCK_64X64), INT64_MAX);
1769     pl = partition_plane_context(cpi->above_seg_context, cpi->left_seg_context,
1770                                  mi_row, mi_col, BLOCK_64X64);
1771     r += x->partition_cost[pl][PARTITION_NONE];
1772
1773     *(get_sb_partitioning(x, BLOCK_64X64)) = BLOCK_64X64;
1774     cpi->set_ref_frame_mask = 0;
1775   }
1776
1777   restore_context(cpi, mi_row, mi_col, a, l, sa, sl, BLOCK_64X64);
1778 }
1779
1780 static void encode_sb_row(VP9_COMP *cpi, const TileInfo *const tile,
1781                           int mi_row, TOKENEXTRA **tp) {
1782   VP9_COMMON * const cm = &cpi->common;
1783   int mi_col;
1784
1785   // Initialize the left context for the new SB row
1786   vpx_memset(&cpi->left_context, 0, sizeof(cpi->left_context));
1787   vpx_memset(cpi->left_seg_context, 0, sizeof(cpi->left_seg_context));
1788
1789   // Code each SB in the row
1790   for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
1791        mi_col += MI_BLOCK_SIZE) {
1792     int dummy_rate;
1793     int64_t dummy_dist;
1794
1795     vp9_zero(cpi->mb.pred_mv);
1796
1797     if (cpi->sf.reference_masking)
1798       rd_pick_reference_frame(cpi, tile, mi_row, mi_col);
1799
1800     if (cpi->sf.use_lastframe_partitioning ||
1801         cpi->sf.use_one_partition_size_always ) {
1802       const int idx_str = cm->mode_info_stride * mi_row + mi_col;
1803       MODE_INFO **mi_8x8 = cm->mi_grid_visible + idx_str;
1804       MODE_INFO **prev_mi_8x8 = cm->prev_mi_grid_visible + idx_str;
1805
1806       cpi->mb.source_variance = UINT_MAX;
1807       if (cpi->sf.use_one_partition_size_always) {
1808         set_offsets(cpi, tile, mi_row, mi_col, BLOCK_64X64);
1809         set_partitioning(cpi, tile, mi_8x8, mi_row, mi_col);
1810         rd_use_partition(cpi, tile, mi_8x8, tp, mi_row, mi_col, BLOCK_64X64,
1811                          &dummy_rate, &dummy_dist, 1);
1812       } else {
1813         if ((cpi->common.current_video_frame
1814             % cpi->sf.last_partitioning_redo_frequency) == 0
1815             || cm->prev_mi == 0
1816             || cpi->common.show_frame == 0
1817             || cpi->common.frame_type == KEY_FRAME
1818             || cpi->is_src_frame_alt_ref
1819             || ((cpi->sf.use_lastframe_partitioning ==
1820                  LAST_FRAME_PARTITION_LOW_MOTION) &&
1821                  sb_has_motion(cpi, prev_mi_8x8))) {
1822           // If required set upper and lower partition size limits
1823           if (cpi->sf.auto_min_max_partition_size) {
1824             set_offsets(cpi, tile, mi_row, mi_col, BLOCK_64X64);
1825             rd_auto_partition_range(cpi, tile, mi_row, mi_col,
1826                                     &cpi->sf.min_partition_size,
1827                                     &cpi->sf.max_partition_size);
1828           }
1829           rd_pick_partition(cpi, tile, tp, mi_row, mi_col, BLOCK_64X64,
1830                             &dummy_rate, &dummy_dist, 1, INT64_MAX);
1831         } else {
1832           copy_partitioning(cpi, mi_8x8, prev_mi_8x8);
1833           rd_use_partition(cpi, tile, mi_8x8, tp, mi_row, mi_col, BLOCK_64X64,
1834                            &dummy_rate, &dummy_dist, 1);
1835         }
1836       }
1837     } else {
1838       // If required set upper and lower partition size limits
1839       if (cpi->sf.auto_min_max_partition_size) {
1840         set_offsets(cpi, tile, mi_row, mi_col, BLOCK_64X64);
1841         rd_auto_partition_range(cpi, tile, mi_row, mi_col,
1842                                 &cpi->sf.min_partition_size,
1843                                 &cpi->sf.max_partition_size);
1844       }
1845       rd_pick_partition(cpi, tile, tp, mi_row, mi_col, BLOCK_64X64,
1846                         &dummy_rate, &dummy_dist, 1, INT64_MAX);
1847     }
1848   }
1849 }
1850
1851 static void init_encode_frame_mb_context(VP9_COMP *cpi) {
1852   MACROBLOCK *const x = &cpi->mb;
1853   VP9_COMMON *const cm = &cpi->common;
1854   MACROBLOCKD *const xd = &x->e_mbd;
1855   const int aligned_mi_cols = mi_cols_aligned_to_sb(cm->mi_cols);
1856
1857   x->act_zbin_adj = 0;
1858   cpi->seg0_idx = 0;
1859
1860   xd->mode_info_stride = cm->mode_info_stride;
1861
1862   // reset intra mode contexts
1863   if (frame_is_intra_only(cm))
1864     vp9_init_mbmode_probs(cm);
1865
1866   // Copy data over into macro block data structures.
1867   vp9_setup_src_planes(x, cpi->Source, 0, 0);
1868
1869   // TODO(jkoleszar): are these initializations required?
1870   setup_pre_planes(xd, 0, &cm->yv12_fb[cm->ref_frame_map[cpi->lst_fb_idx]],
1871                    0, 0, NULL);
1872   setup_dst_planes(xd, get_frame_new_buffer(cm), 0, 0);
1873
1874   setup_block_dptrs(&x->e_mbd, cm->subsampling_x, cm->subsampling_y);
1875
1876   xd->mi_8x8[0]->mbmi.mode = DC_PRED;
1877   xd->mi_8x8[0]->mbmi.uv_mode = DC_PRED;
1878
1879   vp9_zero(cpi->y_mode_count);
1880   vp9_zero(cpi->y_uv_mode_count);
1881   vp9_zero(cm->counts.inter_mode);
1882   vp9_zero(cpi->partition_count);
1883   vp9_zero(cpi->intra_inter_count);
1884   vp9_zero(cpi->comp_inter_count);
1885   vp9_zero(cpi->single_ref_count);
1886   vp9_zero(cpi->comp_ref_count);
1887   vp9_zero(cm->counts.tx);
1888   vp9_zero(cm->counts.mbskip);
1889
1890   // Note: this memset assumes above_context[0], [1] and [2]
1891   // are allocated as part of the same buffer.
1892   vpx_memset(cpi->above_context[0], 0,
1893              sizeof(*cpi->above_context[0]) *
1894              2 * aligned_mi_cols * MAX_MB_PLANE);
1895   vpx_memset(cpi->above_seg_context, 0,
1896              sizeof(*cpi->above_seg_context) * aligned_mi_cols);
1897 }
1898
1899 static void switch_lossless_mode(VP9_COMP *cpi, int lossless) {
1900   if (lossless) {
1901     // printf("Switching to lossless\n");
1902     cpi->mb.fwd_txm4x4 = vp9_fwht4x4;
1903     cpi->mb.e_mbd.itxm_add = vp9_iwht4x4_add;
1904     cpi->mb.optimize = 0;
1905     cpi->common.lf.filter_level = 0;
1906     cpi->zbin_mode_boost_enabled = 0;
1907     cpi->common.tx_mode = ONLY_4X4;
1908   } else {
1909     // printf("Not lossless\n");
1910     cpi->mb.fwd_txm4x4 = vp9_fdct4x4;
1911     cpi->mb.e_mbd.itxm_add = vp9_idct4x4_add;
1912   }
1913 }
1914
1915 static void switch_tx_mode(VP9_COMP *cpi) {
1916   if (cpi->sf.tx_size_search_method == USE_LARGESTALL &&
1917       cpi->common.tx_mode >= ALLOW_32X32)
1918     cpi->common.tx_mode = ALLOW_32X32;
1919 }
1920
1921 static void encode_frame_internal(VP9_COMP *cpi) {
1922   int mi_row;
1923   MACROBLOCK * const x = &cpi->mb;
1924   VP9_COMMON * const cm = &cpi->common;
1925   MACROBLOCKD * const xd = &x->e_mbd;
1926
1927 //  fprintf(stderr, "encode_frame_internal frame %d (%d) type %d\n",
1928 //           cpi->common.current_video_frame, cpi->common.show_frame,
1929 //           cm->frame_type);
1930
1931 // debug output
1932 #if DBG_PRNT_SEGMAP
1933   {
1934     FILE *statsfile;
1935     statsfile = fopen("segmap2.stt", "a");
1936     fprintf(statsfile, "\n");
1937     fclose(statsfile);
1938   }
1939 #endif
1940
1941   vp9_zero(cm->counts.switchable_interp);
1942   vp9_zero(cpi->tx_stepdown_count);
1943
1944   xd->mi_8x8 = cm->mi_grid_visible;
1945   // required for vp9_frame_init_quantizer
1946   xd->mi_8x8[0] = cm->mi;
1947
1948   xd->last_mi = cm->prev_mi;
1949
1950   vp9_zero(cpi->NMVcount);
1951   vp9_zero(cpi->coef_counts);
1952   vp9_zero(cm->counts.eob_branch);
1953
1954   cpi->mb.e_mbd.lossless = cm->base_qindex == 0 && cm->y_dc_delta_q == 0
1955       && cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
1956   switch_lossless_mode(cpi, cpi->mb.e_mbd.lossless);
1957
1958   vp9_frame_init_quantizer(cpi);
1959
1960   vp9_initialize_rd_consts(cpi);
1961   vp9_initialize_me_consts(cpi, cm->base_qindex);
1962   switch_tx_mode(cpi);
1963
1964   if (cpi->oxcf.tuning == VP8_TUNE_SSIM) {
1965     // Initialize encode frame context.
1966     init_encode_frame_mb_context(cpi);
1967
1968     // Build a frame level activity map
1969     build_activity_map(cpi);
1970   }
1971
1972   // Re-initialize encode frame context.
1973   init_encode_frame_mb_context(cpi);
1974
1975   vp9_zero(cpi->rd_comp_pred_diff);
1976   vp9_zero(cpi->rd_filter_diff);
1977   vp9_zero(cpi->rd_tx_select_diff);
1978   vp9_zero(cpi->rd_tx_select_threshes);
1979
1980   set_prev_mi(cm);
1981
1982   {
1983     struct vpx_usec_timer emr_timer;
1984     vpx_usec_timer_start(&emr_timer);
1985
1986     {
1987       // Take tiles into account and give start/end MB
1988       int tile_col, tile_row;
1989       TOKENEXTRA *tp = cpi->tok;
1990       const int tile_cols = 1 << cm->log2_tile_cols;
1991       const int tile_rows = 1 << cm->log2_tile_rows;
1992
1993       for (tile_row = 0; tile_row < tile_rows; tile_row++) {
1994         for (tile_col = 0; tile_col < tile_cols; tile_col++) {
1995           TileInfo tile;
1996           TOKENEXTRA *tp_old = tp;
1997
1998           // For each row of SBs in the frame
1999           vp9_tile_init(&tile, cm, tile_row, tile_col);
2000           for (mi_row = tile.mi_row_start;
2001                mi_row < tile.mi_row_end; mi_row += 8)
2002             encode_sb_row(cpi, &tile, mi_row, &tp);
2003
2004           cpi->tok_count[tile_row][tile_col] = (unsigned int)(tp - tp_old);
2005           assert(tp - cpi->tok <= get_token_alloc(cm->mb_rows, cm->mb_cols));
2006         }
2007       }
2008     }
2009
2010     vpx_usec_timer_mark(&emr_timer);
2011     cpi->time_encode_sb_row += vpx_usec_timer_elapsed(&emr_timer);
2012   }
2013
2014   if (cpi->sf.skip_encode_sb) {
2015     int j;
2016     unsigned int intra_count = 0, inter_count = 0;
2017     for (j = 0; j < INTRA_INTER_CONTEXTS; ++j) {
2018       intra_count += cpi->intra_inter_count[j][0];
2019       inter_count += cpi->intra_inter_count[j][1];
2020     }
2021     cpi->sf.skip_encode_frame = ((intra_count << 2) < inter_count);
2022     cpi->sf.skip_encode_frame &= (cm->frame_type != KEY_FRAME);
2023     cpi->sf.skip_encode_frame &= cm->show_frame;
2024   } else {
2025     cpi->sf.skip_encode_frame = 0;
2026   }
2027
2028 #if 0
2029   // Keep record of the total distortion this time around for future use
2030   cpi->last_frame_distortion = cpi->frame_distortion;
2031 #endif
2032 }
2033
2034 static int check_dual_ref_flags(VP9_COMP *cpi) {
2035   const int ref_flags = cpi->ref_frame_flags;
2036
2037   if (vp9_segfeature_active(&cpi->common.seg, 1, SEG_LVL_REF_FRAME)) {
2038     return 0;
2039   } else {
2040     return (!!(ref_flags & VP9_GOLD_FLAG) + !!(ref_flags & VP9_LAST_FLAG)
2041         + !!(ref_flags & VP9_ALT_FLAG)) >= 2;
2042   }
2043 }
2044
2045 static int get_skip_flag(MODE_INFO **mi_8x8, int mis, int ymbs, int xmbs) {
2046   int x, y;
2047
2048   for (y = 0; y < ymbs; y++) {
2049     for (x = 0; x < xmbs; x++) {
2050       if (!mi_8x8[y * mis + x]->mbmi.skip_coeff)
2051         return 0;
2052     }
2053   }
2054
2055   return 1;
2056 }
2057
2058 static void set_txfm_flag(MODE_INFO **mi_8x8, int mis, int ymbs, int xmbs,
2059                           TX_SIZE tx_size) {
2060   int x, y;
2061
2062   for (y = 0; y < ymbs; y++) {
2063     for (x = 0; x < xmbs; x++)
2064       mi_8x8[y * mis + x]->mbmi.tx_size = tx_size;
2065   }
2066 }
2067
2068 static void reset_skip_txfm_size_b(VP9_COMP *cpi, MODE_INFO **mi_8x8,
2069                                    int mis, TX_SIZE max_tx_size, int bw, int bh,
2070                                    int mi_row, int mi_col, BLOCK_SIZE bsize) {
2071   VP9_COMMON * const cm = &cpi->common;
2072
2073   if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) {
2074     return;
2075   } else {
2076     MB_MODE_INFO * const mbmi = &mi_8x8[0]->mbmi;
2077     if (mbmi->tx_size > max_tx_size) {
2078       const int ymbs = MIN(bh, cm->mi_rows - mi_row);
2079       const int xmbs = MIN(bw, cm->mi_cols - mi_col);
2080
2081       assert(vp9_segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP) ||
2082              get_skip_flag(mi_8x8, mis, ymbs, xmbs));
2083       set_txfm_flag(mi_8x8, mis, ymbs, xmbs, max_tx_size);
2084     }
2085   }
2086 }
2087
2088 static void reset_skip_txfm_size_sb(VP9_COMP *cpi, MODE_INFO **mi_8x8,
2089                                     TX_SIZE max_tx_size, int mi_row, int mi_col,
2090                                     BLOCK_SIZE bsize) {
2091   VP9_COMMON * const cm = &cpi->common;
2092   const int mis = cm->mode_info_stride;
2093   int bw, bh;
2094   const int bs = num_8x8_blocks_wide_lookup[bsize], hbs = bs / 2;
2095
2096   if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
2097     return;
2098
2099   bw = num_8x8_blocks_wide_lookup[mi_8x8[0]->mbmi.sb_type];
2100   bh = num_8x8_blocks_high_lookup[mi_8x8[0]->mbmi.sb_type];
2101
2102   if (bw == bs && bh == bs) {
2103     reset_skip_txfm_size_b(cpi, mi_8x8, mis, max_tx_size, bs, bs, mi_row,
2104                            mi_col, bsize);
2105   } else if (bw == bs && bh < bs) {
2106     reset_skip_txfm_size_b(cpi, mi_8x8, mis, max_tx_size, bs, hbs, mi_row,
2107                            mi_col, bsize);
2108     reset_skip_txfm_size_b(cpi, mi_8x8 + hbs * mis, mis, max_tx_size, bs, hbs,
2109                            mi_row + hbs, mi_col, bsize);
2110   } else if (bw < bs && bh == bs) {
2111     reset_skip_txfm_size_b(cpi, mi_8x8, mis, max_tx_size, hbs, bs, mi_row,
2112                            mi_col, bsize);
2113     reset_skip_txfm_size_b(cpi, mi_8x8 + hbs, mis, max_tx_size, hbs, bs, mi_row,
2114                            mi_col + hbs, bsize);
2115
2116   } else {
2117     const BLOCK_SIZE subsize = subsize_lookup[PARTITION_SPLIT][bsize];
2118     int n;
2119
2120     assert(bw < bs && bh < bs);
2121
2122     for (n = 0; n < 4; n++) {
2123       const int mi_dc = hbs * (n & 1);
2124       const int mi_dr = hbs * (n >> 1);
2125
2126       reset_skip_txfm_size_sb(cpi, &mi_8x8[mi_dr * mis + mi_dc], max_tx_size,
2127                               mi_row + mi_dr, mi_col + mi_dc, subsize);
2128     }
2129   }
2130 }
2131
2132 static void reset_skip_txfm_size(VP9_COMP *cpi, TX_SIZE txfm_max) {
2133   VP9_COMMON * const cm = &cpi->common;
2134   int mi_row, mi_col;
2135   const int mis = cm->mode_info_stride;
2136 //  MODE_INFO *mi, *mi_ptr = cm->mi;
2137   MODE_INFO **mi_8x8, **mi_ptr = cm->mi_grid_visible;
2138
2139   for (mi_row = 0; mi_row < cm->mi_rows; mi_row += 8, mi_ptr += 8 * mis) {
2140     mi_8x8 = mi_ptr;
2141     for (mi_col = 0; mi_col < cm->mi_cols; mi_col += 8, mi_8x8 += 8) {
2142       reset_skip_txfm_size_sb(cpi, mi_8x8, txfm_max, mi_row, mi_col,
2143                               BLOCK_64X64);
2144     }
2145   }
2146 }
2147
2148 static int get_frame_type(VP9_COMP *cpi) {
2149   int frame_type;
2150   if (frame_is_intra_only(&cpi->common))
2151     frame_type = 0;
2152   else if (cpi->is_src_frame_alt_ref && cpi->refresh_golden_frame)
2153     frame_type = 3;
2154   else if (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)
2155     frame_type = 1;
2156   else
2157     frame_type = 2;
2158   return frame_type;
2159 }
2160
2161 static void select_tx_mode(VP9_COMP *cpi) {
2162   if (cpi->oxcf.lossless) {
2163     cpi->common.tx_mode = ONLY_4X4;
2164   } else if (cpi->common.current_video_frame == 0) {
2165     cpi->common.tx_mode = TX_MODE_SELECT;
2166   } else {
2167     if (cpi->sf.tx_size_search_method == USE_LARGESTALL) {
2168       cpi->common.tx_mode = ALLOW_32X32;
2169     } else if (cpi->sf.tx_size_search_method == USE_FULL_RD) {
2170       int frame_type = get_frame_type(cpi);
2171       cpi->common.tx_mode =
2172           cpi->rd_tx_select_threshes[frame_type][ALLOW_32X32]
2173           > cpi->rd_tx_select_threshes[frame_type][TX_MODE_SELECT] ?
2174           ALLOW_32X32 : TX_MODE_SELECT;
2175     } else {
2176       unsigned int total = 0;
2177       int i;
2178       for (i = 0; i < TX_SIZES; ++i)
2179         total += cpi->tx_stepdown_count[i];
2180       if (total) {
2181         double fraction = (double)cpi->tx_stepdown_count[0] / total;
2182         cpi->common.tx_mode = fraction > 0.90 ? ALLOW_32X32 : TX_MODE_SELECT;
2183         // printf("fraction = %f\n", fraction);
2184       }  // else keep unchanged
2185     }
2186   }
2187 }
2188
2189 void vp9_encode_frame(VP9_COMP *cpi) {
2190   VP9_COMMON * const cm = &cpi->common;
2191
2192   // In the longer term the encoder should be generalized to match the
2193   // decoder such that we allow compound where one of the 3 buffers has a
2194   // different sign bias and that buffer is then the fixed ref. However, this
2195   // requires further work in the rd loop. For now the only supported encoder
2196   // side behavior is where the ALT ref buffer has opposite sign bias to
2197   // the other two.
2198   if (!frame_is_intra_only(cm)) {
2199     if ((cm->ref_frame_sign_bias[ALTREF_FRAME]
2200          == cm->ref_frame_sign_bias[GOLDEN_FRAME])
2201         || (cm->ref_frame_sign_bias[ALTREF_FRAME]
2202             == cm->ref_frame_sign_bias[LAST_FRAME])) {
2203       cm->allow_comp_inter_inter = 0;
2204     } else {
2205       cm->allow_comp_inter_inter = 1;
2206       cm->comp_fixed_ref = ALTREF_FRAME;
2207       cm->comp_var_ref[0] = LAST_FRAME;
2208       cm->comp_var_ref[1] = GOLDEN_FRAME;
2209     }
2210   }
2211
2212   if (cpi->sf.RD) {
2213     int i, pred_type;
2214     INTERPOLATION_TYPE filter_type;
2215     /*
2216      * This code does a single RD pass over the whole frame assuming
2217      * either compound, single or hybrid prediction as per whatever has
2218      * worked best for that type of frame in the past.
2219      * It also predicts whether another coding mode would have worked
2220      * better that this coding mode. If that is the case, it remembers
2221      * that for subsequent frames.
2222      * It does the same analysis for transform size selection also.
2223      */
2224     int frame_type = get_frame_type(cpi);
2225
2226     /* prediction (compound, single or hybrid) mode selection */
2227     if (frame_type == 3 || !cm->allow_comp_inter_inter)
2228       pred_type = SINGLE_PREDICTION_ONLY;
2229     else if (cpi->rd_prediction_type_threshes[frame_type][1]
2230              > cpi->rd_prediction_type_threshes[frame_type][0]
2231              && cpi->rd_prediction_type_threshes[frame_type][1]
2232              > cpi->rd_prediction_type_threshes[frame_type][2]
2233              && check_dual_ref_flags(cpi) && cpi->static_mb_pct == 100)
2234       pred_type = COMP_PREDICTION_ONLY;
2235     else if (cpi->rd_prediction_type_threshes[frame_type][0]
2236              > cpi->rd_prediction_type_threshes[frame_type][2])
2237       pred_type = SINGLE_PREDICTION_ONLY;
2238     else
2239       pred_type = HYBRID_PREDICTION;
2240
2241     /* filter type selection */
2242     // FIXME(rbultje) for some odd reason, we often select smooth_filter
2243     // as default filter for ARF overlay frames. This is a REALLY BAD
2244     // IDEA so we explicitly disable it here.
2245     if (frame_type != 3 &&
2246         cpi->rd_filter_threshes[frame_type][1] >
2247             cpi->rd_filter_threshes[frame_type][0] &&
2248         cpi->rd_filter_threshes[frame_type][1] >
2249             cpi->rd_filter_threshes[frame_type][2] &&
2250         cpi->rd_filter_threshes[frame_type][1] >
2251             cpi->rd_filter_threshes[frame_type][SWITCHABLE_FILTERS]) {
2252       filter_type = EIGHTTAP_SMOOTH;
2253     } else if (cpi->rd_filter_threshes[frame_type][2] >
2254             cpi->rd_filter_threshes[frame_type][0] &&
2255         cpi->rd_filter_threshes[frame_type][2] >
2256             cpi->rd_filter_threshes[frame_type][SWITCHABLE_FILTERS]) {
2257       filter_type = EIGHTTAP_SHARP;
2258     } else if (cpi->rd_filter_threshes[frame_type][0] >
2259                   cpi->rd_filter_threshes[frame_type][SWITCHABLE_FILTERS]) {
2260       filter_type = EIGHTTAP;
2261     } else {
2262       filter_type = SWITCHABLE;
2263     }
2264
2265     cpi->mb.e_mbd.lossless = 0;
2266     if (cpi->oxcf.lossless) {
2267       cpi->mb.e_mbd.lossless = 1;
2268     }
2269
2270     /* transform size selection (4x4, 8x8, 16x16 or select-per-mb) */
2271     select_tx_mode(cpi);
2272     cpi->common.comp_pred_mode = pred_type;
2273     cpi->common.mcomp_filter_type = filter_type;
2274     encode_frame_internal(cpi);
2275
2276     for (i = 0; i < NB_PREDICTION_TYPES; ++i) {
2277       const int diff = (int) (cpi->rd_comp_pred_diff[i] / cpi->common.MBs);
2278       cpi->rd_prediction_type_threshes[frame_type][i] += diff;
2279       cpi->rd_prediction_type_threshes[frame_type][i] >>= 1;
2280     }
2281
2282     for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++) {
2283       const int64_t diff = cpi->rd_filter_diff[i] / cpi->common.MBs;
2284       cpi->rd_filter_threshes[frame_type][i] =
2285           (cpi->rd_filter_threshes[frame_type][i] + diff) / 2;
2286     }
2287
2288     for (i = 0; i < TX_MODES; ++i) {
2289       int64_t pd = cpi->rd_tx_select_diff[i];
2290       int diff;
2291       if (i == TX_MODE_SELECT)
2292         pd -= RDCOST(cpi->mb.rdmult, cpi->mb.rddiv,
2293                      2048 * (TX_SIZES - 1), 0);
2294       diff = (int) (pd / cpi->common.MBs);
2295       cpi->rd_tx_select_threshes[frame_type][i] += diff;
2296       cpi->rd_tx_select_threshes[frame_type][i] /= 2;
2297     }
2298
2299     if (cpi->common.comp_pred_mode == HYBRID_PREDICTION) {
2300       int single_count_zero = 0;
2301       int comp_count_zero = 0;
2302
2303       for (i = 0; i < COMP_INTER_CONTEXTS; i++) {
2304         single_count_zero += cpi->comp_inter_count[i][0];
2305         comp_count_zero += cpi->comp_inter_count[i][1];
2306       }
2307
2308       if (comp_count_zero == 0) {
2309         cpi->common.comp_pred_mode = SINGLE_PREDICTION_ONLY;
2310         vp9_zero(cpi->comp_inter_count);
2311       } else if (single_count_zero == 0) {
2312         cpi->common.comp_pred_mode = COMP_PREDICTION_ONLY;
2313         vp9_zero(cpi->comp_inter_count);
2314       }
2315     }
2316
2317     if (cpi->common.tx_mode == TX_MODE_SELECT) {
2318       int count4x4 = 0;
2319       int count8x8_lp = 0, count8x8_8x8p = 0;
2320       int count16x16_16x16p = 0, count16x16_lp = 0;
2321       int count32x32 = 0;
2322
2323       for (i = 0; i < TX_SIZE_CONTEXTS; ++i) {
2324         count4x4 += cm->counts.tx.p32x32[i][TX_4X4];
2325         count4x4 += cm->counts.tx.p16x16[i][TX_4X4];
2326         count4x4 += cm->counts.tx.p8x8[i][TX_4X4];
2327
2328         count8x8_lp += cm->counts.tx.p32x32[i][TX_8X8];
2329         count8x8_lp += cm->counts.tx.p16x16[i][TX_8X8];
2330         count8x8_8x8p += cm->counts.tx.p8x8[i][TX_8X8];
2331
2332         count16x16_16x16p += cm->counts.tx.p16x16[i][TX_16X16];
2333         count16x16_lp += cm->counts.tx.p32x32[i][TX_16X16];
2334         count32x32 += cm->counts.tx.p32x32[i][TX_32X32];
2335       }
2336
2337       if (count4x4 == 0 && count16x16_lp == 0 && count16x16_16x16p == 0
2338           && count32x32 == 0) {
2339         cpi->common.tx_mode = ALLOW_8X8;
2340         reset_skip_txfm_size(cpi, TX_8X8);
2341       } else if (count8x8_8x8p == 0 && count16x16_16x16p == 0
2342                  && count8x8_lp == 0 && count16x16_lp == 0 && count32x32 == 0) {
2343         cpi->common.tx_mode = ONLY_4X4;
2344         reset_skip_txfm_size(cpi, TX_4X4);
2345       } else if (count8x8_lp == 0 && count16x16_lp == 0 && count4x4 == 0) {
2346         cpi->common.tx_mode = ALLOW_32X32;
2347       } else if (count32x32 == 0 && count8x8_lp == 0 && count4x4 == 0) {
2348         cpi->common.tx_mode = ALLOW_16X16;
2349         reset_skip_txfm_size(cpi, TX_16X16);
2350       }
2351     }
2352   } else {
2353     encode_frame_internal(cpi);
2354   }
2355 }
2356
2357 static void sum_intra_stats(VP9_COMP *cpi, const MODE_INFO *mi) {
2358   const MB_PREDICTION_MODE y_mode = mi->mbmi.mode;
2359   const MB_PREDICTION_MODE uv_mode = mi->mbmi.uv_mode;
2360   const BLOCK_SIZE bsize = mi->mbmi.sb_type;
2361
2362   ++cpi->y_uv_mode_count[y_mode][uv_mode];
2363
2364   if (bsize < BLOCK_8X8) {
2365     int idx, idy;
2366     const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
2367     const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
2368     for (idy = 0; idy < 2; idy += num_4x4_blocks_high)
2369       for (idx = 0; idx < 2; idx += num_4x4_blocks_wide)
2370         ++cpi->y_mode_count[0][mi->bmi[idy * 2 + idx].as_mode];
2371   } else {
2372     ++cpi->y_mode_count[size_group_lookup[bsize]][y_mode];
2373   }
2374 }
2375
2376 // Experimental stub function to create a per MB zbin adjustment based on
2377 // some previously calculated measure of MB activity.
2378 static void adjust_act_zbin(VP9_COMP *cpi, MACROBLOCK *x) {
2379 #if USE_ACT_INDEX
2380   x->act_zbin_adj = *(x->mb_activity_ptr);
2381 #else
2382   int64_t a;
2383   int64_t b;
2384   int64_t act = *(x->mb_activity_ptr);
2385
2386   // Apply the masking to the RD multiplier.
2387   a = act + 4 * cpi->activity_avg;
2388   b = 4 * act + cpi->activity_avg;
2389
2390   if (act > cpi->activity_avg)
2391     x->act_zbin_adj = (int) (((int64_t) b + (a >> 1)) / a) - 1;
2392   else
2393     x->act_zbin_adj = 1 - (int) (((int64_t) a + (b >> 1)) / b);
2394 #endif
2395 }
2396 static void encode_superblock(VP9_COMP *cpi, TOKENEXTRA **t, int output_enabled,
2397                               int mi_row, int mi_col, BLOCK_SIZE bsize) {
2398   VP9_COMMON * const cm = &cpi->common;
2399   MACROBLOCK * const x = &cpi->mb;
2400   MACROBLOCKD * const xd = &x->e_mbd;
2401   MODE_INFO **mi_8x8 = xd->mi_8x8;
2402   MODE_INFO *mi = mi_8x8[0];
2403   MB_MODE_INFO *mbmi = &mi->mbmi;
2404   PICK_MODE_CONTEXT *ctx = get_block_context(x, bsize);
2405   unsigned int segment_id = mbmi->segment_id;
2406   const int mis = cm->mode_info_stride;
2407   const int mi_width = num_8x8_blocks_wide_lookup[bsize];
2408   const int mi_height = num_8x8_blocks_high_lookup[bsize];
2409   x->skip_optimize = ctx->is_coded;
2410   ctx->is_coded = 1;
2411   x->use_lp32x32fdct = cpi->sf.use_lp32x32fdct;
2412   x->skip_encode = (!output_enabled && cpi->sf.skip_encode_frame &&
2413                     xd->q_index < QIDX_SKIP_THRESH);
2414   if (x->skip_encode)
2415     return;
2416
2417   if (cm->frame_type == KEY_FRAME) {
2418     if (cpi->oxcf.tuning == VP8_TUNE_SSIM) {
2419       adjust_act_zbin(cpi, x);
2420       vp9_update_zbin_extra(cpi, x);
2421     }
2422   } else {
2423     vp9_setup_interp_filters(xd, mbmi->interp_filter, cm);
2424
2425     if (cpi->oxcf.tuning == VP8_TUNE_SSIM) {
2426       // Adjust the zbin based on this MB rate.
2427       adjust_act_zbin(cpi, x);
2428     }
2429
2430     // Experimental code. Special case for gf and arf zeromv modes.
2431     // Increase zbin size to suppress noise
2432     cpi->zbin_mode_boost = 0;
2433     if (cpi->zbin_mode_boost_enabled) {
2434       if (is_inter_block(mbmi)) {
2435         if (mbmi->mode == ZEROMV) {
2436           if (mbmi->ref_frame[0] != LAST_FRAME)
2437             cpi->zbin_mode_boost = GF_ZEROMV_ZBIN_BOOST;
2438           else
2439             cpi->zbin_mode_boost = LF_ZEROMV_ZBIN_BOOST;
2440         } else if (mbmi->sb_type < BLOCK_8X8) {
2441           cpi->zbin_mode_boost = SPLIT_MV_ZBIN_BOOST;
2442         } else {
2443           cpi->zbin_mode_boost = MV_ZBIN_BOOST;
2444         }
2445       } else {
2446         cpi->zbin_mode_boost = INTRA_ZBIN_BOOST;
2447       }
2448     }
2449
2450     vp9_update_zbin_extra(cpi, x);
2451   }
2452
2453   if (!is_inter_block(mbmi)) {
2454     vp9_encode_intra_block_y(x, MAX(bsize, BLOCK_8X8));
2455     vp9_encode_intra_block_uv(x, MAX(bsize, BLOCK_8X8));
2456     if (output_enabled)
2457       sum_intra_stats(cpi, mi);
2458   } else {
2459     int idx = cm->ref_frame_map[get_ref_frame_idx(cpi, mbmi->ref_frame[0])];
2460     YV12_BUFFER_CONFIG *ref_fb = &cm->yv12_fb[idx];
2461     YV12_BUFFER_CONFIG *second_ref_fb = NULL;
2462     if (has_second_ref(mbmi)) {
2463       idx = cm->ref_frame_map[get_ref_frame_idx(cpi, mbmi->ref_frame[1])];
2464       second_ref_fb = &cm->yv12_fb[idx];
2465     }
2466
2467     assert(cm->frame_type != KEY_FRAME);
2468
2469     setup_pre_planes(xd, 0, ref_fb, mi_row, mi_col,
2470                      &xd->scale_factor[0]);
2471     setup_pre_planes(xd, 1, second_ref_fb, mi_row, mi_col,
2472                      &xd->scale_factor[1]);
2473
2474     vp9_build_inter_predictors_sb(xd, mi_row, mi_col, MAX(bsize, BLOCK_8X8));
2475   }
2476
2477   if (!is_inter_block(mbmi)) {
2478     vp9_tokenize_sb(cpi, t, !output_enabled, MAX(bsize, BLOCK_8X8));
2479   } else if (!x->skip) {
2480     vp9_encode_sb(x, MAX(bsize, BLOCK_8X8));
2481     vp9_tokenize_sb(cpi, t, !output_enabled, MAX(bsize, BLOCK_8X8));
2482   } else {
2483     int mb_skip_context = xd->left_available ? mi_8x8[-1]->mbmi.skip_coeff : 0;
2484     mb_skip_context += mi_8x8[-mis] ? mi_8x8[-mis]->mbmi.skip_coeff : 0;
2485
2486     mbmi->skip_coeff = 1;
2487     if (output_enabled)
2488       cm->counts.mbskip[mb_skip_context][1]++;
2489     reset_skip_context(xd, MAX(bsize, BLOCK_8X8));
2490   }
2491
2492   if (output_enabled) {
2493     if (cm->tx_mode == TX_MODE_SELECT &&
2494         mbmi->sb_type >= BLOCK_8X8  &&
2495         !(is_inter_block(mbmi) &&
2496             (mbmi->skip_coeff ||
2497              vp9_segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)))) {
2498       const uint8_t context = vp9_get_pred_context_tx_size(xd);
2499       ++get_tx_counts(max_txsize_lookup[bsize],
2500                       context, &cm->counts.tx)[mbmi->tx_size];
2501     } else {
2502       int x, y;
2503       TX_SIZE sz = tx_mode_to_biggest_tx_size[cm->tx_mode];
2504       assert(sizeof(tx_mode_to_biggest_tx_size) /
2505              sizeof(tx_mode_to_biggest_tx_size[0]) == TX_MODES);
2506       // The new intra coding scheme requires no change of transform size
2507       if (is_inter_block(&mi->mbmi)) {
2508         if (sz == TX_32X32 && bsize < BLOCK_32X32)
2509           sz = TX_16X16;
2510         if (sz == TX_16X16 && bsize < BLOCK_16X16)
2511           sz = TX_8X8;
2512         if (sz == TX_8X8 && bsize < BLOCK_8X8)
2513           sz = TX_4X4;
2514       } else if (bsize >= BLOCK_8X8) {
2515         sz = mbmi->tx_size;
2516       } else {
2517         sz = TX_4X4;
2518       }
2519
2520       for (y = 0; y < mi_height; y++)
2521         for (x = 0; x < mi_width; x++)
2522           if (mi_col + x < cm->mi_cols && mi_row + y < cm->mi_rows)
2523             mi_8x8[mis * y + x]->mbmi.tx_size = sz;
2524     }
2525   }
2526 }