7bd2dfbbebc8f6f9bd073c0690e25ffec79ee778
[profile/ivi/libvpx.git] / vp8 / encoder / rdopt.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
12 #include <stdio.h>
13 #include <math.h>
14 #include <limits.h>
15 #include <assert.h>
16 #include "vp8/common/pragmas.h"
17
18 #include "tokenize.h"
19 #include "treewriter.h"
20 #include "onyx_int.h"
21 #include "modecosts.h"
22 #include "encodeintra.h"
23 #include "vp8/common/entropymode.h"
24 #include "vp8/common/reconinter.h"
25 #include "vp8/common/reconintra.h"
26 #include "vp8/common/reconintra4x4.h"
27 #include "vp8/common/findnearmv.h"
28 #include "encodemb.h"
29 #include "quantize.h"
30 #include "vp8/common/idct.h"
31 #include "vp8/common/g_common.h"
32 #include "variance.h"
33 #include "mcomp.h"
34 #include "rdopt.h"
35 #include "vpx_mem/vpx_mem.h"
36 #include "dct.h"
37 #include "vp8/common/systemdependent.h"
38
39 #if CONFIG_RUNTIME_CPU_DETECT
40 #define IF_RTCD(x)  (x)
41 #else
42 #define IF_RTCD(x)  NULL
43 #endif
44
45
46 extern void vp8cx_mb_init_quantizer(VP8_COMP *cpi, MACROBLOCK *x);
47 extern void vp8_update_zbin_extra(VP8_COMP *cpi, MACROBLOCK *x);
48
49 #define MAXF(a,b)            (((a) > (b)) ? (a) : (b))
50
51 static const int auto_speed_thresh[17] =
52 {
53     1000,
54     200,
55     150,
56     130,
57     150,
58     125,
59     120,
60     115,
61     115,
62     115,
63     115,
64     115,
65     115,
66     115,
67     115,
68     115,
69     105
70 };
71
72 const MB_PREDICTION_MODE vp8_mode_order[MAX_MODES] =
73 {
74     ZEROMV,
75     DC_PRED,
76
77     NEARESTMV,
78     NEARMV,
79
80     ZEROMV,
81     NEARESTMV,
82
83     ZEROMV,
84     NEARESTMV,
85
86     NEARMV,
87     NEARMV,
88
89     V_PRED,
90     H_PRED,
91     TM_PRED,
92
93     NEWMV,
94     NEWMV,
95     NEWMV,
96
97     SPLITMV,
98     SPLITMV,
99     SPLITMV,
100
101     B_PRED,
102 };
103
104 const MV_REFERENCE_FRAME vp8_ref_frame_order[MAX_MODES] =
105 {
106     LAST_FRAME,
107     INTRA_FRAME,
108
109     LAST_FRAME,
110     LAST_FRAME,
111
112     GOLDEN_FRAME,
113     GOLDEN_FRAME,
114
115     ALTREF_FRAME,
116     ALTREF_FRAME,
117
118     GOLDEN_FRAME,
119     ALTREF_FRAME,
120
121     INTRA_FRAME,
122     INTRA_FRAME,
123     INTRA_FRAME,
124
125     LAST_FRAME,
126     GOLDEN_FRAME,
127     ALTREF_FRAME,
128
129     LAST_FRAME,
130     GOLDEN_FRAME,
131     ALTREF_FRAME,
132
133     INTRA_FRAME,
134 };
135
136 static void fill_token_costs(
137     unsigned int c      [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [vp8_coef_tokens],
138     const vp8_prob p    [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [vp8_coef_tokens-1]
139 )
140 {
141     int i, j, k;
142
143
144     for (i = 0; i < BLOCK_TYPES; i++)
145         for (j = 0; j < COEF_BANDS; j++)
146             for (k = 0; k < PREV_COEF_CONTEXTS; k++)
147
148                 vp8_cost_tokens((int *)(c [i][j][k]), p [i][j][k], vp8_coef_tree);
149
150 }
151
152 static int rd_iifactor [ 32 ] =  {    4,   4,   3,   2,   1,   0,   0,   0,
153                                       0,   0,   0,   0,   0,   0,   0,   0,
154                                       0,   0,   0,   0,   0,   0,   0,   0,
155                                       0,   0,   0,   0,   0,   0,   0,   0,
156                                  };
157
158
159 /* values are now correlated to quantizer */
160 static int sad_per_bit16lut[QINDEX_RANGE] =
161 {
162     5,  5,  5,  5,  5,  5,  6,  6,
163     6,  6,  6,  6,  6,  7,  7,  7,
164     7,  7,  7,  7,  8,  8,  8,  8,
165     8,  8,  8,  8,  8,  8,  9,  9,
166     9,  9,  9,  9, 10, 10, 10, 10,
167     10, 10, 11, 11, 11, 11, 11, 11,
168     12, 12, 12, 12, 12, 12, 12, 13,
169     13, 13, 13, 13, 13, 14, 14, 14,
170     14, 14, 15, 15, 15, 15, 15, 15,
171     16, 16, 16, 16, 16, 16, 17, 17,
172     17, 17, 17, 17, 17, 18, 18, 18,
173     18, 18, 19, 19, 19, 19, 19, 19,
174     20, 20, 20, 21, 21, 21, 21, 22,
175     22, 22, 23, 23, 23, 24, 24, 24,
176     25, 25, 26, 26, 27, 27, 27, 28,
177     28, 28, 29, 29, 30, 30, 31, 31
178 };
179 static int sad_per_bit4lut[QINDEX_RANGE] =
180 {
181     5,  5,  5,  5,  5,  5,  7,  7,
182     7,  7,  7,  7,  7,  8,  8,  8,
183     8,  8,  8,  8,  10, 10, 10, 10,
184     10, 10, 10, 10, 10, 10, 11, 11,
185     11, 11, 11, 11, 13, 13, 13, 13,
186     13, 13, 14, 14, 14, 14, 14, 14,
187     16, 16, 16, 16, 16, 16, 16, 17,
188     17, 17, 17, 17, 17, 19, 19, 19,
189     19, 19, 20, 20, 20, 20, 20, 20,
190     22, 22, 22, 22, 22, 22, 23, 23,
191     23, 23, 23, 23, 23, 25, 25, 25,
192     25, 25, 26, 26, 26, 26, 26, 26,
193     28, 28, 28, 29, 29, 29, 29, 31,
194     31, 31, 32, 32, 32, 34, 34, 34,
195     35, 35, 37, 37, 38, 38, 38, 40,
196     40, 40, 41, 41, 43, 43, 44, 44,
197 };
198
199 void vp8cx_initialize_me_consts(VP8_COMP *cpi, int QIndex)
200 {
201     cpi->mb.sadperbit16 =  sad_per_bit16lut[QIndex];
202     cpi->mb.sadperbit4  =  sad_per_bit4lut[QIndex];
203 }
204
205 void vp8_initialize_rd_consts(VP8_COMP *cpi, int Qvalue)
206 {
207     int q;
208     int i;
209     double capped_q = (Qvalue < 160) ? (double)Qvalue : 160.0;
210     double rdconst = 2.70;
211
212     vp8_clear_system_state();  //__asm emms;
213
214     // Further tests required to see if optimum is different
215     // for key frames, golden frames and arf frames.
216     // if (cpi->common.refresh_golden_frame ||
217     //     cpi->common.refresh_alt_ref_frame)
218     cpi->RDMULT = (int)(rdconst * (capped_q * capped_q));
219
220     // Extend rate multiplier along side quantizer zbin increases
221     if (cpi->zbin_over_quant  > 0)
222     {
223         double oq_factor;
224         double modq;
225
226         // Experimental code using the same basic equation as used for Q above
227         // The units of cpi->zbin_over_quant are 1/128 of Q bin size
228         oq_factor = 1.0 + ((double)0.0015625 * cpi->zbin_over_quant);
229         modq = (int)((double)capped_q * oq_factor);
230         cpi->RDMULT = (int)(rdconst * (modq * modq));
231     }
232
233     if (cpi->pass == 2 && (cpi->common.frame_type != KEY_FRAME))
234     {
235         if (cpi->next_iiratio > 31)
236             cpi->RDMULT += (cpi->RDMULT * rd_iifactor[31]) >> 4;
237         else
238             cpi->RDMULT += (cpi->RDMULT * rd_iifactor[cpi->next_iiratio]) >> 4;
239     }
240
241     cpi->mb.errorperbit = (cpi->RDMULT / 100);
242     cpi->mb.errorperbit += (cpi->mb.errorperbit==0);
243
244     vp8_set_speed_features(cpi);
245
246     q = (int)pow(Qvalue, 1.25);
247
248     if (q < 8)
249         q = 8;
250
251     if (cpi->RDMULT > 1000)
252     {
253         cpi->RDDIV = 1;
254         cpi->RDMULT /= 100;
255
256         for (i = 0; i < MAX_MODES; i++)
257         {
258             if (cpi->sf.thresh_mult[i] < INT_MAX)
259             {
260                 cpi->rd_threshes[i] = cpi->sf.thresh_mult[i] * q / 100;
261             }
262             else
263             {
264                 cpi->rd_threshes[i] = INT_MAX;
265             }
266
267             cpi->rd_baseline_thresh[i] = cpi->rd_threshes[i];
268         }
269     }
270     else
271     {
272         cpi->RDDIV = 100;
273
274         for (i = 0; i < MAX_MODES; i++)
275         {
276             if (cpi->sf.thresh_mult[i] < (INT_MAX / q))
277             {
278                 cpi->rd_threshes[i] = cpi->sf.thresh_mult[i] * q;
279             }
280             else
281             {
282                 cpi->rd_threshes[i] = INT_MAX;
283             }
284
285             cpi->rd_baseline_thresh[i] = cpi->rd_threshes[i];
286         }
287     }
288
289     fill_token_costs(
290         cpi->mb.token_costs,
291         (const vp8_prob( *)[8][3][11]) cpi->common.fc.coef_probs
292     );
293
294     vp8_init_mode_costs(cpi);
295
296 }
297
298 void vp8_auto_select_speed(VP8_COMP *cpi)
299 {
300     int milliseconds_for_compress = (int)(1000000 / cpi->oxcf.frame_rate);
301
302     milliseconds_for_compress = milliseconds_for_compress * (16 - cpi->oxcf.cpu_used) / 16;
303
304 #if 0
305
306     if (0)
307     {
308         FILE *f;
309
310         f = fopen("speed.stt", "a");
311         fprintf(f, " %8ld %10ld %10ld %10ld\n",
312                 cpi->common.current_video_frame, cpi->Speed, milliseconds_for_compress, cpi->avg_pick_mode_time);
313         fclose(f);
314     }
315
316 #endif
317
318     /*
319     // this is done during parameter valid check
320     if( cpi->oxcf.cpu_used > 16)
321         cpi->oxcf.cpu_used = 16;
322     if( cpi->oxcf.cpu_used < -16)
323         cpi->oxcf.cpu_used = -16;
324     */
325
326     if (cpi->avg_pick_mode_time < milliseconds_for_compress && (cpi->avg_encode_time - cpi->avg_pick_mode_time) < milliseconds_for_compress)
327     {
328         if (cpi->avg_pick_mode_time == 0)
329         {
330             cpi->Speed = 4;
331         }
332         else
333         {
334             if (milliseconds_for_compress * 100 < cpi->avg_encode_time * 95)
335             {
336                 cpi->Speed          += 2;
337                 cpi->avg_pick_mode_time = 0;
338                 cpi->avg_encode_time = 0;
339
340                 if (cpi->Speed > 16)
341                 {
342                     cpi->Speed = 16;
343                 }
344             }
345
346             if (milliseconds_for_compress * 100 > cpi->avg_encode_time * auto_speed_thresh[cpi->Speed])
347             {
348                 cpi->Speed          -= 1;
349                 cpi->avg_pick_mode_time = 0;
350                 cpi->avg_encode_time = 0;
351
352                 // In real-time mode, cpi->speed is in [4, 16].
353                 if (cpi->Speed < 4)        //if ( cpi->Speed < 0 )
354                 {
355                     cpi->Speed = 4;        //cpi->Speed = 0;
356                 }
357             }
358         }
359     }
360     else
361     {
362         cpi->Speed += 4;
363
364         if (cpi->Speed > 16)
365             cpi->Speed = 16;
366
367
368         cpi->avg_pick_mode_time = 0;
369         cpi->avg_encode_time = 0;
370     }
371 }
372
373 int vp8_block_error_c(short *coeff, short *dqcoeff)
374 {
375     int i;
376     int error = 0;
377
378     for (i = 0; i < 16; i++)
379     {
380         int this_diff = coeff[i] - dqcoeff[i];
381         error += this_diff * this_diff;
382     }
383
384     return error;
385 }
386
387 int vp8_mbblock_error_c(MACROBLOCK *mb, int dc)
388 {
389     BLOCK  *be;
390     BLOCKD *bd;
391     int i, j;
392     int berror, error = 0;
393
394     for (i = 0; i < 16; i++)
395     {
396         be = &mb->block[i];
397         bd = &mb->e_mbd.block[i];
398
399         berror = 0;
400
401         for (j = dc; j < 16; j++)
402         {
403             int this_diff = be->coeff[j] - bd->dqcoeff[j];
404             berror += this_diff * this_diff;
405         }
406
407         error += berror;
408     }
409
410     return error;
411 }
412
413 int vp8_mbuverror_c(MACROBLOCK *mb)
414 {
415
416     BLOCK  *be;
417     BLOCKD *bd;
418
419
420     int i;
421     int error = 0;
422
423     for (i = 16; i < 24; i++)
424     {
425         be = &mb->block[i];
426         bd = &mb->e_mbd.block[i];
427
428         error += vp8_block_error_c(be->coeff, bd->dqcoeff);
429     }
430
431     return error;
432 }
433
434 int VP8_UVSSE(MACROBLOCK *x, const vp8_variance_rtcd_vtable_t *rtcd)
435 {
436     unsigned char *uptr, *vptr;
437     unsigned char *upred_ptr = (*(x->block[16].base_src) + x->block[16].src);
438     unsigned char *vpred_ptr = (*(x->block[20].base_src) + x->block[20].src);
439     int uv_stride = x->block[16].src_stride;
440
441     unsigned int sse1 = 0;
442     unsigned int sse2 = 0;
443     int mv_row;
444     int mv_col;
445     int offset;
446     int pre_stride = x->e_mbd.block[16].pre_stride;
447
448     vp8_build_uvmvs(&x->e_mbd, 0);
449     mv_row = x->e_mbd.block[16].bmi.mv.as_mv.row;
450     mv_col = x->e_mbd.block[16].bmi.mv.as_mv.col;
451
452     offset = (mv_row >> 3) * pre_stride + (mv_col >> 3);
453     uptr = x->e_mbd.pre.u_buffer + offset;
454     vptr = x->e_mbd.pre.v_buffer + offset;
455
456     if ((mv_row | mv_col) & 7)
457     {
458         VARIANCE_INVOKE(rtcd, subpixvar8x8)(uptr, pre_stride, mv_col & 7, mv_row & 7, upred_ptr, uv_stride, &sse2);
459         VARIANCE_INVOKE(rtcd, subpixvar8x8)(vptr, pre_stride, mv_col & 7, mv_row & 7, vpred_ptr, uv_stride, &sse1);
460         sse2 += sse1;
461     }
462     else
463     {
464         VARIANCE_INVOKE(rtcd, subpixvar8x8)(uptr, pre_stride, mv_col & 7, mv_row & 7, upred_ptr, uv_stride, &sse2);
465         VARIANCE_INVOKE(rtcd, subpixvar8x8)(vptr, pre_stride, mv_col & 7, mv_row & 7, vpred_ptr, uv_stride, &sse1);
466         sse2 += sse1;
467     }
468
469     return sse2;
470
471 }
472
473 static int cost_coeffs(MACROBLOCK *mb, BLOCKD *b, int type, ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l)
474 {
475     int c = !type;              /* start at coef 0, unless Y with Y2 */
476     int eob = b->eob;
477     int pt ;    /* surrounding block/prev coef predictor */
478     int cost = 0;
479     short *qcoeff_ptr = b->qcoeff;
480
481     VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
482
483 # define QC( I)  ( qcoeff_ptr [vp8_default_zig_zag1d[I]] )
484
485     for (; c < eob; c++)
486     {
487         int v = QC(c);
488         int t = vp8_dct_value_tokens_ptr[v].Token;
489         cost += mb->token_costs [type] [vp8_coef_bands[c]] [pt] [t];
490         cost += vp8_dct_value_cost_ptr[v];
491         pt = vp8_prev_token_class[t];
492     }
493
494 # undef QC
495
496     if (c < 16)
497         cost += mb->token_costs [type] [vp8_coef_bands[c]] [pt] [DCT_EOB_TOKEN];
498
499     pt = (c != !type); // is eob first coefficient;
500     *a = *l = pt;
501
502     return cost;
503 }
504
505 static int vp8_rdcost_mby(MACROBLOCK *mb)
506 {
507     int cost = 0;
508     int b;
509     MACROBLOCKD *x = &mb->e_mbd;
510     ENTROPY_CONTEXT_PLANES t_above, t_left;
511     ENTROPY_CONTEXT *ta;
512     ENTROPY_CONTEXT *tl;
513
514     vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
515     vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
516
517     ta = (ENTROPY_CONTEXT *)&t_above;
518     tl = (ENTROPY_CONTEXT *)&t_left;
519
520     for (b = 0; b < 16; b++)
521         cost += cost_coeffs(mb, x->block + b, PLANE_TYPE_Y_NO_DC,
522                     ta + vp8_block2above[b], tl + vp8_block2left[b]);
523
524     cost += cost_coeffs(mb, x->block + 24, PLANE_TYPE_Y2,
525                 ta + vp8_block2above[24], tl + vp8_block2left[24]);
526
527     return cost;
528 }
529
530 static void macro_block_yrd( MACROBLOCK *mb,
531                              int *Rate,
532                              int *Distortion,
533                              const vp8_encodemb_rtcd_vtable_t *rtcd)
534 {
535     int b;
536     MACROBLOCKD *const x = &mb->e_mbd;
537     BLOCK   *const mb_y2 = mb->block + 24;
538     BLOCKD *const x_y2  = x->block + 24;
539     short *Y2DCPtr = mb_y2->src_diff;
540     BLOCK *beptr;
541     int d;
542
543     ENCODEMB_INVOKE(rtcd, submby)( mb->src_diff, mb->src.y_buffer,
544                                    mb->e_mbd.predictor, mb->src.y_stride );
545
546     // Fdct and building the 2nd order block
547     for (beptr = mb->block; beptr < mb->block + 16; beptr += 2)
548     {
549         mb->vp8_short_fdct8x4(beptr->src_diff, beptr->coeff, 32);
550         *Y2DCPtr++ = beptr->coeff[0];
551         *Y2DCPtr++ = beptr->coeff[16];
552     }
553
554     // 2nd order fdct
555     mb->short_walsh4x4(mb_y2->src_diff, mb_y2->coeff, 8);
556
557     // Quantization
558     for (b = 0; b < 16; b++)
559     {
560         mb->quantize_b(&mb->block[b], &mb->e_mbd.block[b]);
561     }
562
563     // DC predication and Quantization of 2nd Order block
564     mb->quantize_b(mb_y2, x_y2);
565
566     // Distortion
567     d = ENCODEMB_INVOKE(rtcd, mberr)(mb, 1) << 2;
568     d += ENCODEMB_INVOKE(rtcd, berr)(mb_y2->coeff, x_y2->dqcoeff);
569
570     *Distortion = (d >> 4);
571
572     // rate
573     *Rate = vp8_rdcost_mby(mb);
574 }
575
576 static void copy_predictor(unsigned char *dst, const unsigned char *predictor)
577 {
578     const unsigned int *p = (const unsigned int *)predictor;
579     unsigned int *d = (unsigned int *)dst;
580     d[0] = p[0];
581     d[4] = p[4];
582     d[8] = p[8];
583     d[12] = p[12];
584 }
585 static int rd_pick_intra4x4block(
586     VP8_COMP *cpi,
587     MACROBLOCK *x,
588     BLOCK *be,
589     BLOCKD *b,
590     B_PREDICTION_MODE *best_mode,
591     unsigned int *bmode_costs,
592     ENTROPY_CONTEXT *a,
593     ENTROPY_CONTEXT *l,
594
595     int *bestrate,
596     int *bestratey,
597     int *bestdistortion)
598 {
599     B_PREDICTION_MODE mode;
600     int best_rd = INT_MAX;
601     int rate = 0;
602     int distortion;
603
604     ENTROPY_CONTEXT ta = *a, tempa = *a;
605     ENTROPY_CONTEXT tl = *l, templ = *l;
606     /*
607      * The predictor buffer is a 2d buffer with a stride of 16.  Create
608      * a temp buffer that meets the stride requirements, but we are only
609      * interested in the left 4x4 block
610      * */
611     DECLARE_ALIGNED_ARRAY(16, unsigned char,  best_predictor, 16*4);
612     DECLARE_ALIGNED_ARRAY(16, short, best_dqcoeff, 16);
613
614     for (mode = B_DC_PRED; mode <= B_HU_PRED; mode++)
615     {
616         int this_rd;
617         int ratey;
618
619         rate = bmode_costs[mode];
620
621         RECON_INVOKE(&cpi->rtcd.common->recon, intra4x4_predict)
622                      (b, mode, b->predictor);
623         ENCODEMB_INVOKE(IF_RTCD(&cpi->rtcd.encodemb), subb)(be, b, 16);
624         x->vp8_short_fdct4x4(be->src_diff, be->coeff, 32);
625         x->quantize_b(be, b);
626
627         tempa = ta;
628         templ = tl;
629
630         ratey = cost_coeffs(x, b, PLANE_TYPE_Y_WITH_DC, &tempa, &templ);
631         rate += ratey;
632         distortion = ENCODEMB_INVOKE(IF_RTCD(&cpi->rtcd.encodemb), berr)(be->coeff, b->dqcoeff) >> 2;
633
634         this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
635
636         if (this_rd < best_rd)
637         {
638             *bestrate = rate;
639             *bestratey = ratey;
640             *bestdistortion = distortion;
641             best_rd = this_rd;
642             *best_mode = mode;
643             *a = tempa;
644             *l = templ;
645             copy_predictor(best_predictor, b->predictor);
646             vpx_memcpy(best_dqcoeff, b->dqcoeff, 32);
647         }
648     }
649
650     b->bmi.mode = (B_PREDICTION_MODE)(*best_mode);
651
652     IDCT_INVOKE(IF_RTCD(&cpi->rtcd.common->idct), idct16)(best_dqcoeff, b->diff, 32);
653     RECON_INVOKE(IF_RTCD(&cpi->rtcd.common->recon), recon)(best_predictor, b->diff, *(b->base_dst) + b->dst, b->dst_stride);
654
655     return best_rd;
656 }
657
658 int vp8_rd_pick_intra4x4mby_modes(VP8_COMP *cpi, MACROBLOCK *mb, int *Rate,
659                                   int *rate_y, int *Distortion, int best_rd)
660 {
661     MACROBLOCKD *const xd = &mb->e_mbd;
662     int i;
663     int cost = mb->mbmode_cost [xd->frame_type] [B_PRED];
664     int distortion = 0;
665     int tot_rate_y = 0;
666     long long total_rd = 0;
667     ENTROPY_CONTEXT_PLANES t_above, t_left;
668     ENTROPY_CONTEXT *ta;
669     ENTROPY_CONTEXT *tl;
670     unsigned int *bmode_costs;
671
672     vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
673     vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
674
675     ta = (ENTROPY_CONTEXT *)&t_above;
676     tl = (ENTROPY_CONTEXT *)&t_left;
677
678     vp8_intra_prediction_down_copy(xd);
679
680     bmode_costs = mb->inter_bmode_costs;
681
682     for (i = 0; i < 16; i++)
683     {
684         MODE_INFO *const mic = xd->mode_info_context;
685         const int mis = xd->mode_info_stride;
686         B_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
687         int UNINITIALIZED_IS_SAFE(r), UNINITIALIZED_IS_SAFE(ry), UNINITIALIZED_IS_SAFE(d);
688
689         if (mb->e_mbd.frame_type == KEY_FRAME)
690         {
691             const B_PREDICTION_MODE A = vp8_above_bmi(mic, i, mis)->mode;
692             const B_PREDICTION_MODE L = vp8_left_bmi(mic, i)->mode;
693
694             bmode_costs  = mb->bmode_costs[A][L];
695         }
696
697         total_rd += rd_pick_intra4x4block(
698             cpi, mb, mb->block + i, xd->block + i, &best_mode, bmode_costs,
699             ta + vp8_block2above[i],
700             tl + vp8_block2left[i], &r, &ry, &d);
701
702         cost += r;
703         distortion += d;
704         tot_rate_y += ry;
705         mic->bmi[i].mode = xd->block[i].bmi.mode = best_mode;
706
707         if(total_rd >= (long long)best_rd)
708             break;
709     }
710
711     if(total_rd >= (long long)best_rd)
712         return INT_MAX;
713
714     *Rate = cost;
715     *rate_y += tot_rate_y;
716     *Distortion = distortion;
717
718     return RDCOST(mb->rdmult, mb->rddiv, cost, distortion);
719 }
720 int vp8_rd_pick_intra16x16mby_mode(VP8_COMP *cpi,
721                                    MACROBLOCK *x,
722                                    int *Rate,
723                                    int *rate_y,
724                                    int *Distortion)
725 {
726     MB_PREDICTION_MODE mode;
727     MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(mode_selected);
728     int rate, ratey;
729     int distortion;
730     int best_rd = INT_MAX;
731     int this_rd;
732
733     //Y Search for 16x16 intra prediction mode
734     for (mode = DC_PRED; mode <= TM_PRED; mode++)
735     {
736         x->e_mbd.mode_info_context->mbmi.mode = mode;
737
738         RECON_INVOKE(&cpi->common.rtcd.recon, build_intra_predictors_mby)
739             (&x->e_mbd);
740
741         macro_block_yrd(x, &ratey, &distortion, IF_RTCD(&cpi->rtcd.encodemb));
742         rate = ratey + x->mbmode_cost[x->e_mbd.frame_type]
743                                      [x->e_mbd.mode_info_context->mbmi.mode];
744
745         this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
746
747         if (this_rd < best_rd)
748         {
749             mode_selected = mode;
750             best_rd = this_rd;
751             *Rate = rate;
752             *rate_y = ratey;
753             *Distortion = distortion;
754         }
755     }
756
757     x->e_mbd.mode_info_context->mbmi.mode = mode_selected;
758     return best_rd;
759 }
760
761 static int rd_cost_mbuv(MACROBLOCK *mb)
762 {
763     int b;
764     int cost = 0;
765     MACROBLOCKD *x = &mb->e_mbd;
766     ENTROPY_CONTEXT_PLANES t_above, t_left;
767     ENTROPY_CONTEXT *ta;
768     ENTROPY_CONTEXT *tl;
769
770     vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
771     vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
772
773     ta = (ENTROPY_CONTEXT *)&t_above;
774     tl = (ENTROPY_CONTEXT *)&t_left;
775
776     for (b = 16; b < 24; b++)
777         cost += cost_coeffs(mb, x->block + b, PLANE_TYPE_UV,
778                     ta + vp8_block2above[b], tl + vp8_block2left[b]);
779
780     return cost;
781 }
782
783
784 static int vp8_rd_inter_uv(VP8_COMP *cpi, MACROBLOCK *x, int *rate, int *distortion, int fullpixel)
785 {
786     vp8_build_uvmvs(&x->e_mbd, fullpixel);
787     vp8_encode_inter16x16uvrd(IF_RTCD(&cpi->rtcd), x);
788
789
790     *rate       = rd_cost_mbuv(x);
791     *distortion = ENCODEMB_INVOKE(&cpi->rtcd.encodemb, mbuverr)(x) / 4;
792
793     return RDCOST(x->rdmult, x->rddiv, *rate, *distortion);
794 }
795
796 void vp8_rd_pick_intra_mbuv_mode(VP8_COMP *cpi, MACROBLOCK *x, int *rate, int *rate_tokenonly, int *distortion)
797 {
798     MB_PREDICTION_MODE mode;
799     MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(mode_selected);
800     int best_rd = INT_MAX;
801     int UNINITIALIZED_IS_SAFE(d), UNINITIALIZED_IS_SAFE(r);
802     int rate_to;
803
804     for (mode = DC_PRED; mode <= TM_PRED; mode++)
805     {
806         int rate;
807         int distortion;
808         int this_rd;
809
810         x->e_mbd.mode_info_context->mbmi.uv_mode = mode;
811         RECON_INVOKE(&cpi->rtcd.common->recon, build_intra_predictors_mbuv)
812                      (&x->e_mbd);
813         ENCODEMB_INVOKE(IF_RTCD(&cpi->rtcd.encodemb), submbuv)(x->src_diff,
814                       x->src.u_buffer, x->src.v_buffer, x->e_mbd.predictor,
815                       x->src.uv_stride);
816         vp8_transform_mbuv(x);
817         vp8_quantize_mbuv(x);
818
819         rate_to = rd_cost_mbuv(x);
820         rate = rate_to + x->intra_uv_mode_cost[x->e_mbd.frame_type][x->e_mbd.mode_info_context->mbmi.uv_mode];
821
822         distortion = ENCODEMB_INVOKE(&cpi->rtcd.encodemb, mbuverr)(x) / 4;
823
824         this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
825
826         if (this_rd < best_rd)
827         {
828             best_rd = this_rd;
829             d = distortion;
830             r = rate;
831             *rate_tokenonly = rate_to;
832             mode_selected = mode;
833         }
834     }
835
836     *rate = r;
837     *distortion = d;
838
839     x->e_mbd.mode_info_context->mbmi.uv_mode = mode_selected;
840 }
841
842 int vp8_cost_mv_ref(MB_PREDICTION_MODE m, const int near_mv_ref_ct[4])
843 {
844     vp8_prob p [VP8_MVREFS-1];
845     assert(NEARESTMV <= m  &&  m <= SPLITMV);
846     vp8_mv_ref_probs(p, near_mv_ref_ct);
847     return vp8_cost_token(vp8_mv_ref_tree, p,
848                           vp8_mv_ref_encoding_array - NEARESTMV + m);
849 }
850
851 void vp8_set_mbmode_and_mvs(MACROBLOCK *x, MB_PREDICTION_MODE mb, int_mv *mv)
852 {
853     int i;
854
855     x->e_mbd.mode_info_context->mbmi.mode = mb;
856     x->e_mbd.mode_info_context->mbmi.mv.as_int = mv->as_int;
857
858     for (i = 0; i < 16; i++)
859     {
860         B_MODE_INFO *bmi = &x->e_mbd.block[i].bmi;
861         bmi->mode = (B_PREDICTION_MODE) mb;
862         bmi->mv.as_int = mv->as_int;
863     }
864 }
865
866 static int labels2mode(
867     MACROBLOCK *x,
868     int const *labelings, int which_label,
869     B_PREDICTION_MODE this_mode,
870     int_mv *this_mv, int_mv *best_ref_mv,
871     int *mvcost[2]
872 )
873 {
874     MACROBLOCKD *const xd = & x->e_mbd;
875     MODE_INFO *const mic = xd->mode_info_context;
876     const int mis = xd->mode_info_stride;
877
878     int cost = 0;
879     int thismvcost = 0;
880
881     /* We have to be careful retrieving previously-encoded motion vectors.
882        Ones from this macroblock have to be pulled from the BLOCKD array
883        as they have not yet made it to the bmi array in our MB_MODE_INFO. */
884
885     int i = 0;
886
887     do
888     {
889         BLOCKD *const d = xd->block + i;
890         const int row = i >> 2,  col = i & 3;
891
892         B_PREDICTION_MODE m;
893
894         if (labelings[i] != which_label)
895             continue;
896
897         if (col  &&  labelings[i] == labelings[i-1])
898             m = LEFT4X4;
899         else if (row  &&  labelings[i] == labelings[i-4])
900             m = ABOVE4X4;
901         else
902         {
903             // the only time we should do costing for new motion vector or mode
904             // is when we are on a new label  (jbb May 08, 2007)
905             switch (m = this_mode)
906             {
907             case NEW4X4 :
908                 thismvcost  = vp8_mv_bit_cost(this_mv, best_ref_mv, mvcost, 102);
909                 break;
910             case LEFT4X4:
911                 this_mv->as_int = col ? d[-1].bmi.mv.as_int : vp8_left_bmi(mic, i)->mv.as_int;
912                 break;
913             case ABOVE4X4:
914                 this_mv->as_int = row ? d[-4].bmi.mv.as_int : vp8_above_bmi(mic, i, mis)->mv.as_int;
915                 break;
916             case ZERO4X4:
917                 this_mv->as_int = 0;
918                 break;
919             default:
920                 break;
921             }
922
923             if (m == ABOVE4X4)  // replace above with left if same
924             {
925                 int_mv left_mv;
926                 left_mv.as_int = col ? d[-1].bmi.mv.as_int :
927                                         vp8_left_bmi(mic, i)->mv.as_int;
928
929                 if (left_mv.as_int == this_mv->as_int)
930                     m = LEFT4X4;
931             }
932
933             cost = x->inter_bmode_costs[ m];
934         }
935
936         d->bmi.mode = m;
937         d->bmi.mv.as_int = this_mv->as_int;
938
939     }
940     while (++i < 16);
941
942     cost += thismvcost ;
943     return cost;
944 }
945
946 static int rdcost_mbsegment_y(MACROBLOCK *mb, const int *labels,
947                               int which_label, ENTROPY_CONTEXT *ta,
948                               ENTROPY_CONTEXT *tl)
949 {
950     int cost = 0;
951     int b;
952     MACROBLOCKD *x = &mb->e_mbd;
953
954     for (b = 0; b < 16; b++)
955         if (labels[ b] == which_label)
956             cost += cost_coeffs(mb, x->block + b, PLANE_TYPE_Y_WITH_DC,
957                                 ta + vp8_block2above[b],
958                                 tl + vp8_block2left[b]);
959
960     return cost;
961
962 }
963 static unsigned int vp8_encode_inter_mb_segment(MACROBLOCK *x, int const *labels, int which_label, const vp8_encodemb_rtcd_vtable_t *rtcd)
964 {
965     int i;
966     unsigned int distortion = 0;
967
968     for (i = 0; i < 16; i++)
969     {
970         if (labels[i] == which_label)
971         {
972             BLOCKD *bd = &x->e_mbd.block[i];
973             BLOCK *be = &x->block[i];
974
975
976             vp8_build_inter_predictors_b(bd, 16, x->e_mbd.subpixel_predict);
977             ENCODEMB_INVOKE(rtcd, subb)(be, bd, 16);
978             x->vp8_short_fdct4x4(be->src_diff, be->coeff, 32);
979
980             // set to 0 no way to account for 2nd order DC so discount
981             //be->coeff[0] = 0;
982             x->quantize_b(be, bd);
983
984             distortion += ENCODEMB_INVOKE(rtcd, berr)(be->coeff, bd->dqcoeff);
985         }
986     }
987
988     return distortion;
989 }
990
991
992 static const unsigned int segmentation_to_sseshift[4] = {3, 3, 2, 0};
993
994
995 typedef struct
996 {
997   int_mv *ref_mv;
998   int_mv mvp;
999
1000   int segment_rd;
1001   int segment_num;
1002   int r;
1003   int d;
1004   int segment_yrate;
1005   B_PREDICTION_MODE modes[16];
1006   int_mv mvs[16];
1007   unsigned char eobs[16];
1008
1009   int mvthresh;
1010   int *mdcounts;
1011
1012   int_mv sv_mvp[4];     // save 4 mvp from 8x8
1013   int sv_istep[2];  // save 2 initial step_param for 16x8/8x16
1014
1015 } BEST_SEG_INFO;
1016
1017
1018 static void rd_check_segment(VP8_COMP *cpi, MACROBLOCK *x,
1019                              BEST_SEG_INFO *bsi, unsigned int segmentation)
1020 {
1021     int i;
1022     int const *labels;
1023     int br = 0;
1024     int bd = 0;
1025     B_PREDICTION_MODE this_mode;
1026
1027
1028     int label_count;
1029     int this_segment_rd = 0;
1030     int label_mv_thresh;
1031     int rate = 0;
1032     int sbr = 0;
1033     int sbd = 0;
1034     int segmentyrate = 0;
1035
1036     vp8_variance_fn_ptr_t *v_fn_ptr;
1037
1038     ENTROPY_CONTEXT_PLANES t_above, t_left;
1039     ENTROPY_CONTEXT *ta;
1040     ENTROPY_CONTEXT *tl;
1041     ENTROPY_CONTEXT_PLANES t_above_b, t_left_b;
1042     ENTROPY_CONTEXT *ta_b;
1043     ENTROPY_CONTEXT *tl_b;
1044
1045     vpx_memcpy(&t_above, x->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
1046     vpx_memcpy(&t_left, x->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
1047
1048     ta = (ENTROPY_CONTEXT *)&t_above;
1049     tl = (ENTROPY_CONTEXT *)&t_left;
1050     ta_b = (ENTROPY_CONTEXT *)&t_above_b;
1051     tl_b = (ENTROPY_CONTEXT *)&t_left_b;
1052
1053     br = 0;
1054     bd = 0;
1055
1056     v_fn_ptr = &cpi->fn_ptr[segmentation];
1057     labels = vp8_mbsplits[segmentation];
1058     label_count = vp8_mbsplit_count[segmentation];
1059
1060     // 64 makes this threshold really big effectively
1061     // making it so that we very rarely check mvs on
1062     // segments.   setting this to 1 would make mv thresh
1063     // roughly equal to what it is for macroblocks
1064     label_mv_thresh = 1 * bsi->mvthresh / label_count ;
1065
1066     // Segmentation method overheads
1067     rate = vp8_cost_token(vp8_mbsplit_tree, vp8_mbsplit_probs, vp8_mbsplit_encodings + segmentation);
1068     rate += vp8_cost_mv_ref(SPLITMV, bsi->mdcounts);
1069     this_segment_rd += RDCOST(x->rdmult, x->rddiv, rate, 0);
1070     br += rate;
1071
1072     for (i = 0; i < label_count; i++)
1073     {
1074         int_mv mode_mv[B_MODE_COUNT];
1075         int best_label_rd = INT_MAX;
1076         B_PREDICTION_MODE mode_selected = ZERO4X4;
1077         int bestlabelyrate = 0;
1078
1079         // search for the best motion vector on this segment
1080         for (this_mode = LEFT4X4; this_mode <= NEW4X4 ; this_mode ++)
1081         {
1082             int this_rd;
1083             int distortion;
1084             int labelyrate;
1085             ENTROPY_CONTEXT_PLANES t_above_s, t_left_s;
1086             ENTROPY_CONTEXT *ta_s;
1087             ENTROPY_CONTEXT *tl_s;
1088
1089             vpx_memcpy(&t_above_s, &t_above, sizeof(ENTROPY_CONTEXT_PLANES));
1090             vpx_memcpy(&t_left_s, &t_left, sizeof(ENTROPY_CONTEXT_PLANES));
1091
1092             ta_s = (ENTROPY_CONTEXT *)&t_above_s;
1093             tl_s = (ENTROPY_CONTEXT *)&t_left_s;
1094
1095             if (this_mode == NEW4X4)
1096             {
1097                 int sseshift;
1098                 int num00;
1099                 int step_param = 0;
1100                 int further_steps;
1101                 int n;
1102                 int thissme;
1103                 int bestsme = INT_MAX;
1104                 int_mv  temp_mv;
1105                 BLOCK *c;
1106                 BLOCKD *e;
1107
1108                 // Is the best so far sufficiently good that we cant justify doing and new motion search.
1109                 if (best_label_rd < label_mv_thresh)
1110                     break;
1111
1112                 if(cpi->compressor_speed)
1113                 {
1114                     if (segmentation == BLOCK_8X16 || segmentation == BLOCK_16X8)
1115                     {
1116                         bsi->mvp.as_int = bsi->sv_mvp[i].as_int;
1117                         if (i==1 && segmentation == BLOCK_16X8)
1118                           bsi->mvp.as_int = bsi->sv_mvp[2].as_int;
1119
1120                         step_param = bsi->sv_istep[i];
1121                     }
1122
1123                     // use previous block's result as next block's MV predictor.
1124                     if (segmentation == BLOCK_4X4 && i>0)
1125                     {
1126                         bsi->mvp.as_int = x->e_mbd.block[i-1].bmi.mv.as_int;
1127                         if (i==4 || i==8 || i==12)
1128                             bsi->mvp.as_int = x->e_mbd.block[i-4].bmi.mv.as_int;
1129                         step_param = 2;
1130                     }
1131                 }
1132
1133                 further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
1134
1135                 {
1136                     int sadpb = x->sadperbit4;
1137
1138                     // find first label
1139                     n = vp8_mbsplit_offset[segmentation][i];
1140
1141                     c = &x->block[n];
1142                     e = &x->e_mbd.block[n];
1143
1144                     if (cpi->sf.search_method == HEX)
1145                         bestsme = vp8_hex_search(x, c, e, bsi->ref_mv,
1146                                                  &mode_mv[NEW4X4], step_param, sadpb, &num00, v_fn_ptr, x->mvsadcost, x->mvcost, bsi->ref_mv);
1147
1148                     else
1149                     {
1150                         bestsme = cpi->diamond_search_sad(x, c, e, &bsi->mvp,
1151                                                           &mode_mv[NEW4X4], step_param,
1152                                                           sadpb / 2, &num00, v_fn_ptr, x->mvcost, bsi->ref_mv);
1153
1154                         n = num00;
1155                         num00 = 0;
1156
1157                         while (n < further_steps)
1158                         {
1159                             n++;
1160
1161                             if (num00)
1162                                 num00--;
1163                             else
1164                             {
1165                                 thissme = cpi->diamond_search_sad(x, c, e, &bsi->mvp,
1166                                                                   &temp_mv, step_param + n,
1167                                                                   sadpb / 2, &num00, v_fn_ptr, x->mvcost, bsi->ref_mv);
1168
1169                                 if (thissme < bestsme)
1170                                 {
1171                                     bestsme = thissme;
1172                                     mode_mv[NEW4X4].as_int = temp_mv.as_int;
1173                                 }
1174                             }
1175                         }
1176                     }
1177
1178                     sseshift = segmentation_to_sseshift[segmentation];
1179
1180                     // Should we do a full search (best quality only)
1181                     if ((cpi->compressor_speed == 0) && (bestsme >> sseshift) > 4000)
1182                     {
1183                         int_mv full_mvp;
1184
1185                         full_mvp.as_mv.row = bsi->mvp.as_mv.row >>3;
1186                         full_mvp.as_mv.col = bsi->mvp.as_mv.col >>3;
1187
1188                         thissme = cpi->full_search_sad(x, c, e, &full_mvp,
1189                                                        sadpb / 4, 16, v_fn_ptr, x->mvcost, bsi->ref_mv);
1190
1191                         if (thissme < bestsme)
1192                         {
1193                             bestsme = thissme;
1194                             mode_mv[NEW4X4].as_int = e->bmi.mv.as_int;
1195                         }
1196                         else
1197                         {
1198                             // The full search result is actually worse so re-instate the previous best vector
1199                             e->bmi.mv.as_int = mode_mv[NEW4X4].as_int;
1200                         }
1201                     }
1202                 }
1203
1204                 if (bestsme < INT_MAX)
1205                 {
1206                     int distortion;
1207                     unsigned int sse;
1208
1209                     if (!cpi->common.full_pixel)
1210                         cpi->find_fractional_mv_step(x, c, e, &mode_mv[NEW4X4],
1211                                                      bsi->ref_mv, x->errorperbit / 2, v_fn_ptr, x->mvcost, &distortion, &sse);
1212                     else
1213                         vp8_skip_fractional_mv_step(x, c, e, &mode_mv[NEW4X4],
1214                                                     bsi->ref_mv, x->errorperbit, v_fn_ptr, x->mvcost, &distortion, &sse);
1215                 }
1216             } /* NEW4X4 */
1217
1218             rate = labels2mode(x, labels, i, this_mode, &mode_mv[this_mode],
1219                                bsi->ref_mv, x->mvcost);
1220
1221             // Trap vectors that reach beyond the UMV borders
1222             if (((mode_mv[this_mode].as_mv.row >> 3) < x->mv_row_min) || ((mode_mv[this_mode].as_mv.row >> 3) > x->mv_row_max) ||
1223                 ((mode_mv[this_mode].as_mv.col >> 3) < x->mv_col_min) || ((mode_mv[this_mode].as_mv.col >> 3) > x->mv_col_max))
1224             {
1225                 continue;
1226             }
1227
1228             distortion = vp8_encode_inter_mb_segment(x, labels, i, IF_RTCD(&cpi->rtcd.encodemb)) / 4;
1229
1230             labelyrate = rdcost_mbsegment_y(x, labels, i, ta_s, tl_s);
1231             rate += labelyrate;
1232
1233             this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
1234
1235             if (this_rd < best_label_rd)
1236             {
1237                 sbr = rate;
1238                 sbd = distortion;
1239                 bestlabelyrate = labelyrate;
1240                 mode_selected = this_mode;
1241                 best_label_rd = this_rd;
1242
1243                 vpx_memcpy(ta_b, ta_s, sizeof(ENTROPY_CONTEXT_PLANES));
1244                 vpx_memcpy(tl_b, tl_s, sizeof(ENTROPY_CONTEXT_PLANES));
1245
1246             }
1247         } /*for each 4x4 mode*/
1248
1249         vpx_memcpy(ta, ta_b, sizeof(ENTROPY_CONTEXT_PLANES));
1250         vpx_memcpy(tl, tl_b, sizeof(ENTROPY_CONTEXT_PLANES));
1251
1252         labels2mode(x, labels, i, mode_selected, &mode_mv[mode_selected],
1253                     bsi->ref_mv, x->mvcost);
1254
1255         br += sbr;
1256         bd += sbd;
1257         segmentyrate += bestlabelyrate;
1258         this_segment_rd += best_label_rd;
1259
1260         if (this_segment_rd >= bsi->segment_rd)
1261             break;
1262
1263     } /* for each label */
1264
1265     if (this_segment_rd < bsi->segment_rd)
1266     {
1267         bsi->r = br;
1268         bsi->d = bd;
1269         bsi->segment_yrate = segmentyrate;
1270         bsi->segment_rd = this_segment_rd;
1271         bsi->segment_num = segmentation;
1272
1273         // store everything needed to come back to this!!
1274         for (i = 0; i < 16; i++)
1275         {
1276             BLOCKD *bd = &x->e_mbd.block[i];
1277
1278             bsi->mvs[i].as_mv = bd->bmi.mv.as_mv;
1279             bsi->modes[i] = bd->bmi.mode;
1280             bsi->eobs[i] = bd->eob;
1281         }
1282     }
1283 }
1284
1285 static __inline
1286 void vp8_cal_step_param(int sr, int *sp)
1287 {
1288     int step = 0;
1289
1290     if (sr > MAX_FIRST_STEP) sr = MAX_FIRST_STEP;
1291     else if (sr < 1) sr = 1;
1292
1293     while (sr>>=1)
1294         step++;
1295
1296     *sp = MAX_MVSEARCH_STEPS - 1 - step;
1297 }
1298
1299 static int vp8_rd_pick_best_mbsegmentation(VP8_COMP *cpi, MACROBLOCK *x,
1300                                            int_mv *best_ref_mv, int best_rd,
1301                                            int *mdcounts, int *returntotrate,
1302                                            int *returnyrate, int *returndistortion,
1303                                            int mvthresh)
1304 {
1305     int i;
1306     BEST_SEG_INFO bsi;
1307
1308     vpx_memset(&bsi, 0, sizeof(bsi));
1309
1310     bsi.segment_rd = best_rd;
1311     bsi.ref_mv = best_ref_mv;
1312     bsi.mvp.as_int = best_ref_mv->as_int;
1313     bsi.mvthresh = mvthresh;
1314     bsi.mdcounts = mdcounts;
1315
1316     for(i = 0; i < 16; i++)
1317     {
1318         bsi.modes[i] = ZERO4X4;
1319     }
1320
1321     if(cpi->compressor_speed == 0)
1322     {
1323         /* for now, we will keep the original segmentation order
1324            when in best quality mode */
1325         rd_check_segment(cpi, x, &bsi, BLOCK_16X8);
1326         rd_check_segment(cpi, x, &bsi, BLOCK_8X16);
1327         rd_check_segment(cpi, x, &bsi, BLOCK_8X8);
1328         rd_check_segment(cpi, x, &bsi, BLOCK_4X4);
1329     }
1330     else
1331     {
1332         int sr;
1333
1334         rd_check_segment(cpi, x, &bsi, BLOCK_8X8);
1335
1336         if (bsi.segment_rd < best_rd)
1337         {
1338             int col_min = (best_ref_mv->as_mv.col - MAX_FULL_PEL_VAL) >>3;
1339             int col_max = (best_ref_mv->as_mv.col + MAX_FULL_PEL_VAL) >>3;
1340             int row_min = (best_ref_mv->as_mv.row - MAX_FULL_PEL_VAL) >>3;
1341             int row_max = (best_ref_mv->as_mv.row + MAX_FULL_PEL_VAL) >>3;
1342
1343             int tmp_col_min = x->mv_col_min;
1344             int tmp_col_max = x->mv_col_max;
1345             int tmp_row_min = x->mv_row_min;
1346             int tmp_row_max = x->mv_row_max;
1347
1348             /* Get intersection of UMV window and valid MV window to reduce # of checks in diamond search. */
1349             if (x->mv_col_min < col_min )
1350                 x->mv_col_min = col_min;
1351             if (x->mv_col_max > col_max )
1352                 x->mv_col_max = col_max;
1353             if (x->mv_row_min < row_min )
1354                 x->mv_row_min = row_min;
1355             if (x->mv_row_max > row_max )
1356                 x->mv_row_max = row_max;
1357
1358             /* Get 8x8 result */
1359             bsi.sv_mvp[0].as_int = bsi.mvs[0].as_int;
1360             bsi.sv_mvp[1].as_int = bsi.mvs[2].as_int;
1361             bsi.sv_mvp[2].as_int = bsi.mvs[8].as_int;
1362             bsi.sv_mvp[3].as_int = bsi.mvs[10].as_int;
1363
1364             /* Use 8x8 result as 16x8/8x16's predictor MV. Adjust search range according to the closeness of 2 MV. */
1365             /* block 8X16 */
1366             {
1367                 sr = MAXF((abs(bsi.sv_mvp[0].as_mv.row - bsi.sv_mvp[2].as_mv.row))>>3, (abs(bsi.sv_mvp[0].as_mv.col - bsi.sv_mvp[2].as_mv.col))>>3);
1368                 vp8_cal_step_param(sr, &bsi.sv_istep[0]);
1369
1370                 sr = MAXF((abs(bsi.sv_mvp[1].as_mv.row - bsi.sv_mvp[3].as_mv.row))>>3, (abs(bsi.sv_mvp[1].as_mv.col - bsi.sv_mvp[3].as_mv.col))>>3);
1371                 vp8_cal_step_param(sr, &bsi.sv_istep[1]);
1372
1373                 rd_check_segment(cpi, x, &bsi, BLOCK_8X16);
1374             }
1375
1376             /* block 16X8 */
1377             {
1378                 sr = MAXF((abs(bsi.sv_mvp[0].as_mv.row - bsi.sv_mvp[1].as_mv.row))>>3, (abs(bsi.sv_mvp[0].as_mv.col - bsi.sv_mvp[1].as_mv.col))>>3);
1379                 vp8_cal_step_param(sr, &bsi.sv_istep[0]);
1380
1381                 sr = MAXF((abs(bsi.sv_mvp[2].as_mv.row - bsi.sv_mvp[3].as_mv.row))>>3, (abs(bsi.sv_mvp[2].as_mv.col - bsi.sv_mvp[3].as_mv.col))>>3);
1382                 vp8_cal_step_param(sr, &bsi.sv_istep[1]);
1383
1384                 rd_check_segment(cpi, x, &bsi, BLOCK_16X8);
1385             }
1386
1387             /* If 8x8 is better than 16x8/8x16, then do 4x4 search */
1388             /* Not skip 4x4 if speed=0 (good quality) */
1389             if (cpi->sf.no_skip_block4x4_search || bsi.segment_num == BLOCK_8X8)  /* || (sv_segment_rd8x8-bsi.segment_rd) < sv_segment_rd8x8>>5) */
1390             {
1391                 bsi.mvp.as_int = bsi.sv_mvp[0].as_int;
1392                 rd_check_segment(cpi, x, &bsi, BLOCK_4X4);
1393             }
1394
1395             /* restore UMV window */
1396             x->mv_col_min = tmp_col_min;
1397             x->mv_col_max = tmp_col_max;
1398             x->mv_row_min = tmp_row_min;
1399             x->mv_row_max = tmp_row_max;
1400         }
1401     }
1402
1403     /* set it to the best */
1404     for (i = 0; i < 16; i++)
1405     {
1406         BLOCKD *bd = &x->e_mbd.block[i];
1407
1408         bd->bmi.mv.as_mv = bsi.mvs[i].as_mv;
1409         bd->bmi.mode = bsi.modes[i];
1410         bd->eob = bsi.eobs[i];
1411     }
1412
1413     *returntotrate = bsi.r;
1414     *returndistortion = bsi.d;
1415     *returnyrate = bsi.segment_yrate;
1416
1417     /* save partitions */
1418     x->e_mbd.mode_info_context->mbmi.partitioning = bsi.segment_num;
1419     x->partition_info->count = vp8_mbsplit_count[bsi.segment_num];
1420
1421     for (i = 0; i < x->partition_info->count; i++)
1422     {
1423         int j;
1424
1425         j = vp8_mbsplit_offset[bsi.segment_num][i];
1426
1427         x->partition_info->bmi[i].mode = x->e_mbd.block[j].bmi.mode;
1428         x->partition_info->bmi[i].mv.as_mv = x->e_mbd.block[j].bmi.mv.as_mv;
1429     }
1430
1431     return bsi.segment_rd;
1432 }
1433
1434 static void insertsortmv(int arr[], int len)
1435 {
1436     int i, j, k;
1437
1438     for ( i = 1 ; i <= len-1 ; i++ )
1439     {
1440         for ( j = 0 ; j < i ; j++ )
1441         {
1442             if ( arr[j] > arr[i] )
1443             {
1444                 int temp;
1445
1446                 temp = arr[i];
1447
1448                 for ( k = i; k >j; k--)
1449                     arr[k] = arr[k - 1] ;
1450
1451                 arr[j] = temp ;
1452             }
1453         }
1454     }
1455 }
1456
1457 static void insertsortsad(int arr[],int idx[], int len)
1458 {
1459     int i, j, k;
1460
1461     for ( i = 1 ; i <= len-1 ; i++ )
1462     {
1463         for ( j = 0 ; j < i ; j++ )
1464         {
1465             if ( arr[j] > arr[i] )
1466             {
1467                 int temp, tempi;
1468
1469                 temp = arr[i];
1470                 tempi = idx[i];
1471
1472                 for ( k = i; k >j; k--)
1473                 {
1474                     arr[k] = arr[k - 1] ;
1475                     idx[k] = idx[k - 1];
1476                 }
1477
1478                 arr[j] = temp ;
1479                 idx[j] = tempi;
1480             }
1481         }
1482     }
1483 }
1484
1485 //The improved MV prediction
1486 void vp8_mv_pred
1487 (
1488     VP8_COMP *cpi,
1489     MACROBLOCKD *xd,
1490     const MODE_INFO *here,
1491     int_mv *mvp,
1492     int refframe,
1493     int *ref_frame_sign_bias,
1494     int *sr,
1495     int near_sadidx[]
1496 )
1497 {
1498     const MODE_INFO *above = here - xd->mode_info_stride;
1499     const MODE_INFO *left = here - 1;
1500     const MODE_INFO *aboveleft = above - 1;
1501     int_mv           near_mvs[8];
1502     int              near_ref[8];
1503     int_mv           mv;
1504     int              vcnt=0;
1505     int              find=0;
1506     int              mb_offset;
1507
1508     int              mvx[8];
1509     int              mvy[8];
1510     int              i;
1511
1512     mv.as_int = 0;
1513
1514     if(here->mbmi.ref_frame != INTRA_FRAME)
1515     {
1516         near_mvs[0].as_int = near_mvs[1].as_int = near_mvs[2].as_int = near_mvs[3].as_int = near_mvs[4].as_int = near_mvs[5].as_int = near_mvs[6].as_int = near_mvs[7].as_int = 0;
1517         near_ref[0] = near_ref[1] = near_ref[2] = near_ref[3] = near_ref[4] = near_ref[5] = near_ref[6] = near_ref[7] = 0;
1518
1519         // read in 3 nearby block's MVs from current frame as prediction candidates.
1520         if (above->mbmi.ref_frame != INTRA_FRAME)
1521         {
1522             near_mvs[vcnt].as_int = above->mbmi.mv.as_int;
1523             mv_bias(ref_frame_sign_bias[above->mbmi.ref_frame], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1524             near_ref[vcnt] =  above->mbmi.ref_frame;
1525         }
1526         vcnt++;
1527         if (left->mbmi.ref_frame != INTRA_FRAME)
1528         {
1529             near_mvs[vcnt].as_int = left->mbmi.mv.as_int;
1530             mv_bias(ref_frame_sign_bias[left->mbmi.ref_frame], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1531             near_ref[vcnt] =  left->mbmi.ref_frame;
1532         }
1533         vcnt++;
1534         if (aboveleft->mbmi.ref_frame != INTRA_FRAME)
1535         {
1536             near_mvs[vcnt].as_int = aboveleft->mbmi.mv.as_int;
1537             mv_bias(ref_frame_sign_bias[aboveleft->mbmi.ref_frame], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1538             near_ref[vcnt] =  aboveleft->mbmi.ref_frame;
1539         }
1540         vcnt++;
1541
1542         // read in 5 nearby block's MVs from last frame.
1543         if(cpi->common.last_frame_type != KEY_FRAME)
1544         {
1545             mb_offset = (-xd->mb_to_top_edge/128 + 1) * (xd->mode_info_stride +1) + (-xd->mb_to_left_edge/128 +1) ;
1546
1547             // current in last frame
1548             if (cpi->lf_ref_frame[mb_offset] != INTRA_FRAME)
1549             {
1550                 near_mvs[vcnt].as_int = cpi->lfmv[mb_offset].as_int;
1551                 mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1552                 near_ref[vcnt] =  cpi->lf_ref_frame[mb_offset];
1553             }
1554             vcnt++;
1555
1556             // above in last frame
1557             if (cpi->lf_ref_frame[mb_offset - xd->mode_info_stride-1] != INTRA_FRAME)
1558             {
1559                 near_mvs[vcnt].as_int = cpi->lfmv[mb_offset - xd->mode_info_stride-1].as_int;
1560                 mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset - xd->mode_info_stride-1], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1561                 near_ref[vcnt] =  cpi->lf_ref_frame[mb_offset - xd->mode_info_stride-1];
1562             }
1563             vcnt++;
1564
1565             // left in last frame
1566             if (cpi->lf_ref_frame[mb_offset-1] != INTRA_FRAME)
1567             {
1568                 near_mvs[vcnt].as_int = cpi->lfmv[mb_offset -1].as_int;
1569                 mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset -1], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1570                 near_ref[vcnt] =  cpi->lf_ref_frame[mb_offset - 1];
1571             }
1572             vcnt++;
1573
1574             // right in last frame
1575             if (cpi->lf_ref_frame[mb_offset +1] != INTRA_FRAME)
1576             {
1577                 near_mvs[vcnt].as_int = cpi->lfmv[mb_offset +1].as_int;
1578                 mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset +1], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1579                 near_ref[vcnt] =  cpi->lf_ref_frame[mb_offset +1];
1580             }
1581             vcnt++;
1582
1583             // below in last frame
1584             if (cpi->lf_ref_frame[mb_offset + xd->mode_info_stride +1] != INTRA_FRAME)
1585             {
1586                 near_mvs[vcnt].as_int = cpi->lfmv[mb_offset + xd->mode_info_stride +1].as_int;
1587                 mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset + xd->mode_info_stride +1], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1588                 near_ref[vcnt] =  cpi->lf_ref_frame[mb_offset + xd->mode_info_stride +1];
1589             }
1590             vcnt++;
1591         }
1592
1593         for(i=0; i< vcnt; i++)
1594         {
1595             if(near_ref[near_sadidx[i]] != INTRA_FRAME)
1596             {
1597                 if(here->mbmi.ref_frame == near_ref[near_sadidx[i]])
1598                 {
1599                     mv.as_int = near_mvs[near_sadidx[i]].as_int;
1600                     find = 1;
1601                     if (i < 3)
1602                         *sr = 3;
1603                     else
1604                         *sr = 2;
1605                     break;
1606                 }
1607             }
1608         }
1609
1610         if(!find)
1611         {
1612             for(i=0; i<vcnt; i++)
1613             {
1614                 mvx[i] = near_mvs[i].as_mv.row;
1615                 mvy[i] = near_mvs[i].as_mv.col;
1616             }
1617
1618             insertsortmv(mvx, vcnt);
1619             insertsortmv(mvy, vcnt);
1620             mv.as_mv.row = mvx[vcnt/2];
1621             mv.as_mv.col = mvy[vcnt/2];
1622
1623             find = 1;
1624             //sr is set to 0 to allow calling function to decide the search range.
1625             *sr = 0;
1626         }
1627     }
1628
1629     /* Set up return values */
1630     mvp->as_int = mv.as_int;
1631     vp8_clamp_mv2(mvp, xd);
1632 }
1633
1634 void vp8_cal_sad(VP8_COMP *cpi, MACROBLOCKD *xd, MACROBLOCK *x, int recon_yoffset, int near_sadidx[])
1635 {
1636
1637     int near_sad[8] = {0}; // 0-cf above, 1-cf left, 2-cf aboveleft, 3-lf current, 4-lf above, 5-lf left, 6-lf right, 7-lf below
1638
1639     //calculate sad for current frame 3 nearby MBs.
1640     if( xd->mb_to_top_edge==0 && xd->mb_to_left_edge ==0)
1641     {
1642         near_sad[0] = near_sad[1] = near_sad[2] = INT_MAX;
1643     }else if(xd->mb_to_top_edge==0)
1644     {   //only has left MB for sad calculation.
1645         near_sad[0] = near_sad[2] = INT_MAX;
1646         near_sad[1] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, xd->dst.y_buffer - 16,xd->dst.y_stride, 0x7fffffff);
1647     }else if(xd->mb_to_left_edge ==0)
1648     {   //only has left MB for sad calculation.
1649         near_sad[1] = near_sad[2] = INT_MAX;
1650         near_sad[0] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, xd->dst.y_buffer - xd->dst.y_stride *16,xd->dst.y_stride, 0x7fffffff);
1651     }else
1652     {
1653         near_sad[0] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, xd->dst.y_buffer - xd->dst.y_stride *16,xd->dst.y_stride, 0x7fffffff);
1654         near_sad[1] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, xd->dst.y_buffer - 16,xd->dst.y_stride, 0x7fffffff);
1655         near_sad[2] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, xd->dst.y_buffer - xd->dst.y_stride *16 -16,xd->dst.y_stride, 0x7fffffff);
1656     }
1657
1658     if(cpi->common.last_frame_type != KEY_FRAME)
1659     {
1660         //calculate sad for last frame 5 nearby MBs.
1661         unsigned char *pre_y_buffer = cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_buffer + recon_yoffset;
1662         int pre_y_stride = cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_stride;
1663
1664         if(xd->mb_to_top_edge==0) near_sad[4] = INT_MAX;
1665         if(xd->mb_to_left_edge ==0) near_sad[5] = INT_MAX;
1666         if(xd->mb_to_right_edge ==0) near_sad[6] = INT_MAX;
1667         if(xd->mb_to_bottom_edge==0) near_sad[7] = INT_MAX;
1668
1669         if(near_sad[4] != INT_MAX)
1670             near_sad[4] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, pre_y_buffer - pre_y_stride *16, pre_y_stride, 0x7fffffff);
1671         if(near_sad[5] != INT_MAX)
1672             near_sad[5] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, pre_y_buffer - 16, pre_y_stride, 0x7fffffff);
1673         near_sad[3] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, pre_y_buffer, pre_y_stride, 0x7fffffff);
1674         if(near_sad[6] != INT_MAX)
1675             near_sad[6] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, pre_y_buffer + 16, pre_y_stride, 0x7fffffff);
1676         if(near_sad[7] != INT_MAX)
1677             near_sad[7] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, pre_y_buffer + pre_y_stride *16, pre_y_stride, 0x7fffffff);
1678     }
1679
1680     if(cpi->common.last_frame_type != KEY_FRAME)
1681     {
1682         insertsortsad(near_sad, near_sadidx, 8);
1683     }else
1684     {
1685         insertsortsad(near_sad, near_sadidx, 3);
1686     }
1687 }
1688
1689 void vp8_rd_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset, int recon_uvoffset, int *returnrate, int *returndistortion, int *returnintra)
1690 {
1691     BLOCK *b = &x->block[0];
1692     BLOCKD *d = &x->e_mbd.block[0];
1693     MACROBLOCKD *xd = &x->e_mbd;
1694     B_MODE_INFO best_bmodes[16];
1695     MB_MODE_INFO best_mbmode;
1696     PARTITION_INFO best_partition;
1697     int_mv best_ref_mv;
1698     int_mv mode_mv[MB_MODE_COUNT];
1699     MB_PREDICTION_MODE this_mode;
1700     int num00;
1701     int best_mode_index = 0;
1702
1703     int i;
1704     int mode_index;
1705     int mdcounts[4];
1706     int rate;
1707     int distortion;
1708     int best_rd = INT_MAX;
1709     int best_intra_rd = INT_MAX;
1710     int ref_frame_cost[MAX_REF_FRAMES];
1711     int rate2, distortion2;
1712     int uv_intra_rate, uv_intra_distortion, uv_intra_rate_tokenonly;
1713     int rate_y, UNINITIALIZED_IS_SAFE(rate_uv);
1714     int distortion_uv;
1715     int best_yrd = INT_MAX;
1716
1717     //int all_rds[MAX_MODES];        // Experimental debug code.
1718     //int all_rates[MAX_MODES];
1719     //int all_dist[MAX_MODES];
1720     //int intermodecost[MAX_MODES];
1721
1722     MB_PREDICTION_MODE uv_intra_mode;
1723     int_mv mvp;
1724     int near_sadidx[8] = {0, 1, 2, 3, 4, 5, 6, 7};
1725     int saddone=0;
1726     int sr=0;    //search range got from mv_pred(). It uses step_param levels. (0-7)
1727
1728     int_mv frame_nearest_mv[4];
1729     int_mv frame_near_mv[4];
1730     int_mv frame_best_ref_mv[4];
1731     int frame_mdcounts[4][4];
1732     int frame_lf_or_gf[4];
1733     unsigned char *y_buffer[4];
1734     unsigned char *u_buffer[4];
1735     unsigned char *v_buffer[4];
1736
1737     vpx_memset(&best_mbmode, 0, sizeof(best_mbmode));
1738
1739     if (cpi->ref_frame_flags & VP8_LAST_FLAG)
1740     {
1741         YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
1742
1743         vp8_find_near_mvs(&x->e_mbd, x->e_mbd.mode_info_context, &frame_nearest_mv[LAST_FRAME], &frame_near_mv[LAST_FRAME],
1744                           &frame_best_ref_mv[LAST_FRAME], frame_mdcounts[LAST_FRAME], LAST_FRAME, cpi->common.ref_frame_sign_bias);
1745
1746         y_buffer[LAST_FRAME] = lst_yv12->y_buffer + recon_yoffset;
1747         u_buffer[LAST_FRAME] = lst_yv12->u_buffer + recon_uvoffset;
1748         v_buffer[LAST_FRAME] = lst_yv12->v_buffer + recon_uvoffset;
1749
1750         frame_lf_or_gf[LAST_FRAME] = 0;
1751     }
1752
1753     if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1754     {
1755         YV12_BUFFER_CONFIG *gld_yv12 = &cpi->common.yv12_fb[cpi->common.gld_fb_idx];
1756
1757         vp8_find_near_mvs(&x->e_mbd, x->e_mbd.mode_info_context, &frame_nearest_mv[GOLDEN_FRAME], &frame_near_mv[GOLDEN_FRAME],
1758                           &frame_best_ref_mv[GOLDEN_FRAME], frame_mdcounts[GOLDEN_FRAME], GOLDEN_FRAME, cpi->common.ref_frame_sign_bias);
1759
1760         y_buffer[GOLDEN_FRAME] = gld_yv12->y_buffer + recon_yoffset;
1761         u_buffer[GOLDEN_FRAME] = gld_yv12->u_buffer + recon_uvoffset;
1762         v_buffer[GOLDEN_FRAME] = gld_yv12->v_buffer + recon_uvoffset;
1763
1764         frame_lf_or_gf[GOLDEN_FRAME] = 1;
1765     }
1766
1767     if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1768     {
1769         YV12_BUFFER_CONFIG *alt_yv12 = &cpi->common.yv12_fb[cpi->common.alt_fb_idx];
1770
1771         vp8_find_near_mvs(&x->e_mbd, x->e_mbd.mode_info_context, &frame_nearest_mv[ALTREF_FRAME], &frame_near_mv[ALTREF_FRAME],
1772                           &frame_best_ref_mv[ALTREF_FRAME], frame_mdcounts[ALTREF_FRAME], ALTREF_FRAME, cpi->common.ref_frame_sign_bias);
1773
1774         y_buffer[ALTREF_FRAME] = alt_yv12->y_buffer + recon_yoffset;
1775         u_buffer[ALTREF_FRAME] = alt_yv12->u_buffer + recon_uvoffset;
1776         v_buffer[ALTREF_FRAME] = alt_yv12->v_buffer + recon_uvoffset;
1777
1778         frame_lf_or_gf[ALTREF_FRAME] = 1;
1779     }
1780
1781     *returnintra = INT_MAX;
1782     cpi->mbs_tested_so_far++;          // Count of the number of MBs tested so far this frame
1783
1784     x->skip = 0;
1785
1786     ref_frame_cost[INTRA_FRAME]   = vp8_cost_zero(cpi->prob_intra_coded);
1787
1788     // Special case treatment when GF and ARF are not sensible options for reference
1789     if (cpi->ref_frame_flags == VP8_LAST_FLAG)
1790     {
1791         ref_frame_cost[LAST_FRAME]    = vp8_cost_one(cpi->prob_intra_coded)
1792                                         + vp8_cost_zero(255);
1793         ref_frame_cost[GOLDEN_FRAME]  = vp8_cost_one(cpi->prob_intra_coded)
1794                                         + vp8_cost_one(255)
1795                                         + vp8_cost_zero(128);
1796         ref_frame_cost[ALTREF_FRAME]  = vp8_cost_one(cpi->prob_intra_coded)
1797                                         + vp8_cost_one(255)
1798                                         + vp8_cost_one(128);
1799     }
1800     else
1801     {
1802         ref_frame_cost[LAST_FRAME]    = vp8_cost_one(cpi->prob_intra_coded)
1803                                         + vp8_cost_zero(cpi->prob_last_coded);
1804         ref_frame_cost[GOLDEN_FRAME]  = vp8_cost_one(cpi->prob_intra_coded)
1805                                         + vp8_cost_one(cpi->prob_last_coded)
1806                                         + vp8_cost_zero(cpi->prob_gf_coded);
1807         ref_frame_cost[ALTREF_FRAME]  = vp8_cost_one(cpi->prob_intra_coded)
1808                                         + vp8_cost_one(cpi->prob_last_coded)
1809                                         + vp8_cost_one(cpi->prob_gf_coded);
1810     }
1811
1812     vpx_memset(mode_mv, 0, sizeof(mode_mv));
1813
1814     x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
1815     vp8_rd_pick_intra_mbuv_mode(cpi, x, &uv_intra_rate, &uv_intra_rate_tokenonly, &uv_intra_distortion);
1816     uv_intra_mode = x->e_mbd.mode_info_context->mbmi.uv_mode;
1817
1818     for (mode_index = 0; mode_index < MAX_MODES; mode_index++)
1819     {
1820         int this_rd = INT_MAX;
1821         int lf_or_gf = 0;           // Lat Frame (01) or gf/arf (1)
1822         int disable_skip = 0;
1823         int other_cost = 0;
1824
1825         // Experimental debug code.
1826         // Record of rd values recorded for this MB. -1 indicates not measured
1827         //all_rds[mode_index] = -1;
1828         //all_rates[mode_index] = -1;
1829         //all_dist[mode_index] = -1;
1830         //intermodecost[mode_index] = -1;
1831
1832         // Test best rd so far against threshold for trying this mode.
1833         if (best_rd <= cpi->rd_threshes[mode_index])
1834             continue;
1835
1836         // These variables hold are rolling total cost and distortion for this mode
1837         rate2 = 0;
1838         distortion2 = 0;
1839
1840         this_mode = vp8_mode_order[mode_index];
1841
1842         x->e_mbd.mode_info_context->mbmi.mode = this_mode;
1843         x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
1844         x->e_mbd.mode_info_context->mbmi.ref_frame = vp8_ref_frame_order[mode_index];
1845
1846         // Only consider ZEROMV/ALTREF_FRAME for alt ref frame,
1847         // unless ARNR filtering is enabled in which case we want
1848         // an unfiltered alternative
1849         if (cpi->is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0))
1850         {
1851             if (this_mode != ZEROMV || x->e_mbd.mode_info_context->mbmi.ref_frame != ALTREF_FRAME)
1852                 continue;
1853         }
1854
1855         /* everything but intra */
1856         if (x->e_mbd.mode_info_context->mbmi.ref_frame)
1857         {
1858             x->e_mbd.pre.y_buffer = y_buffer[x->e_mbd.mode_info_context->mbmi.ref_frame];
1859             x->e_mbd.pre.u_buffer = u_buffer[x->e_mbd.mode_info_context->mbmi.ref_frame];
1860             x->e_mbd.pre.v_buffer = v_buffer[x->e_mbd.mode_info_context->mbmi.ref_frame];
1861             mode_mv[NEARESTMV] = frame_nearest_mv[x->e_mbd.mode_info_context->mbmi.ref_frame];
1862             mode_mv[NEARMV] = frame_near_mv[x->e_mbd.mode_info_context->mbmi.ref_frame];
1863             best_ref_mv = frame_best_ref_mv[x->e_mbd.mode_info_context->mbmi.ref_frame];
1864             vpx_memcpy(mdcounts, frame_mdcounts[x->e_mbd.mode_info_context->mbmi.ref_frame], sizeof(mdcounts));
1865             lf_or_gf = frame_lf_or_gf[x->e_mbd.mode_info_context->mbmi.ref_frame];
1866         }
1867
1868         if(x->e_mbd.mode_info_context->mbmi.mode == NEWMV)
1869         {
1870             if(!saddone)
1871             {
1872                 vp8_cal_sad(cpi,xd,x, recon_yoffset ,&near_sadidx[0] );
1873                 saddone = 1;
1874             }
1875
1876             vp8_mv_pred(cpi, &x->e_mbd, x->e_mbd.mode_info_context, &mvp,
1877                         x->e_mbd.mode_info_context->mbmi.ref_frame, cpi->common.ref_frame_sign_bias, &sr, &near_sadidx[0]);
1878
1879             /* adjust mvp to make sure it is within MV range */
1880             vp8_clamp_mv(&mvp,
1881                          best_ref_mv.as_mv.row - MAX_FULL_PEL_VAL,
1882                          best_ref_mv.as_mv.row + MAX_FULL_PEL_VAL,
1883                          best_ref_mv.as_mv.col - MAX_FULL_PEL_VAL,
1884                          best_ref_mv.as_mv.col + MAX_FULL_PEL_VAL);
1885         }
1886
1887         // Check to see if the testing frequency for this mode is at its max
1888         // If so then prevent it from being tested and increase the threshold for its testing
1889         if (cpi->mode_test_hit_counts[mode_index] && (cpi->mode_check_freq[mode_index] > 1))
1890         {
1891             if (cpi->mbs_tested_so_far  <= cpi->mode_check_freq[mode_index] * cpi->mode_test_hit_counts[mode_index])
1892             {
1893                 // Increase the threshold for coding this mode to make it less likely to be chosen
1894                 cpi->rd_thresh_mult[mode_index] += 4;
1895
1896                 if (cpi->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
1897                     cpi->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
1898
1899                 cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
1900
1901                 continue;
1902             }
1903         }
1904
1905         // We have now reached the point where we are going to test the current mode so increment the counter for the number of times it has been tested
1906         cpi->mode_test_hit_counts[mode_index] ++;
1907
1908         // Experimental code. Special case for gf and arf zeromv modes. Increase zbin size to supress noise
1909         if (cpi->zbin_mode_boost_enabled)
1910         {
1911             if ( vp8_ref_frame_order[mode_index] == INTRA_FRAME )
1912                 cpi->zbin_mode_boost = 0;
1913             else
1914             {
1915                 if (vp8_mode_order[mode_index] == ZEROMV)
1916                 {
1917                     if (vp8_ref_frame_order[mode_index] != LAST_FRAME)
1918                         cpi->zbin_mode_boost = GF_ZEROMV_ZBIN_BOOST;
1919                     else
1920                         cpi->zbin_mode_boost = LF_ZEROMV_ZBIN_BOOST;
1921                 }
1922                 else if (vp8_mode_order[mode_index] == SPLITMV)
1923                     cpi->zbin_mode_boost = 0;
1924                 else
1925                     cpi->zbin_mode_boost = MV_ZBIN_BOOST;
1926             }
1927
1928             vp8_update_zbin_extra(cpi, x);
1929         }
1930
1931         switch (this_mode)
1932         {
1933         case B_PRED:
1934         {
1935             int tmp_rd;
1936
1937             // Note the rate value returned here includes the cost of coding the BPRED mode : x->mbmode_cost[x->e_mbd.frame_type][BPRED];
1938             tmp_rd = vp8_rd_pick_intra4x4mby_modes(cpi, x, &rate, &rate_y, &distortion, best_yrd);
1939             rate2 += rate;
1940             distortion2 += distortion;
1941
1942             if(tmp_rd < best_yrd)
1943             {
1944                 rate2 += uv_intra_rate;
1945                 rate_uv = uv_intra_rate_tokenonly;
1946                 distortion2 += uv_intra_distortion;
1947                 distortion_uv = uv_intra_distortion;
1948             }
1949             else
1950             {
1951                 this_rd = INT_MAX;
1952                 disable_skip = 1;
1953             }
1954         }
1955         break;
1956
1957         case SPLITMV:
1958         {
1959             int tmp_rd;
1960             int this_rd_thresh;
1961
1962             this_rd_thresh = (x->e_mbd.mode_info_context->mbmi.ref_frame == LAST_FRAME) ? cpi->rd_threshes[THR_NEWMV] : cpi->rd_threshes[THR_NEWA];
1963             this_rd_thresh = (x->e_mbd.mode_info_context->mbmi.ref_frame == GOLDEN_FRAME) ? cpi->rd_threshes[THR_NEWG]: this_rd_thresh;
1964
1965             tmp_rd = vp8_rd_pick_best_mbsegmentation(cpi, x, &best_ref_mv,
1966                                                      best_yrd, mdcounts,
1967                                                      &rate, &rate_y, &distortion, this_rd_thresh) ;
1968
1969             rate2 += rate;
1970             distortion2 += distortion;
1971
1972             // If even the 'Y' rd value of split is higher than best so far then dont bother looking at UV
1973             if (tmp_rd < best_yrd)
1974             {
1975                 // Now work out UV cost and add it in
1976                 vp8_rd_inter_uv(cpi, x, &rate_uv, &distortion_uv, cpi->common.full_pixel);
1977                 rate2 += rate_uv;
1978                 distortion2 += distortion_uv;
1979             }
1980             else
1981             {
1982                 this_rd = INT_MAX;
1983                 disable_skip = 1;
1984             }
1985         }
1986         break;
1987         case DC_PRED:
1988         case V_PRED:
1989         case H_PRED:
1990         case TM_PRED:
1991             x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
1992             RECON_INVOKE(&cpi->common.rtcd.recon, build_intra_predictors_mby)
1993                 (&x->e_mbd);
1994             macro_block_yrd(x, &rate_y, &distortion, IF_RTCD(&cpi->rtcd.encodemb)) ;
1995             rate2 += rate_y;
1996             distortion2 += distortion;
1997             rate2 += x->mbmode_cost[x->e_mbd.frame_type][x->e_mbd.mode_info_context->mbmi.mode];
1998             rate2 += uv_intra_rate;
1999             rate_uv = uv_intra_rate_tokenonly;
2000             distortion2 += uv_intra_distortion;
2001             distortion_uv = uv_intra_distortion;
2002             break;
2003
2004         case NEWMV:
2005         {
2006             int thissme;
2007             int bestsme = INT_MAX;
2008             int step_param = cpi->sf.first_step;
2009             int further_steps;
2010             int n;
2011             int do_refine=1;   /* If last step (1-away) of n-step search doesn't pick the center point as the best match,
2012                                   we will do a final 1-away diamond refining search  */
2013
2014             int sadpb = x->sadperbit16;
2015
2016             int col_min = (best_ref_mv.as_mv.col - MAX_FULL_PEL_VAL) >>3;
2017             int col_max = (best_ref_mv.as_mv.col + MAX_FULL_PEL_VAL) >>3;
2018             int row_min = (best_ref_mv.as_mv.row - MAX_FULL_PEL_VAL) >>3;
2019             int row_max = (best_ref_mv.as_mv.row + MAX_FULL_PEL_VAL) >>3;
2020
2021             int tmp_col_min = x->mv_col_min;
2022             int tmp_col_max = x->mv_col_max;
2023             int tmp_row_min = x->mv_row_min;
2024             int tmp_row_max = x->mv_row_max;
2025
2026             // Get intersection of UMV window and valid MV window to reduce # of checks in diamond search.
2027             if (x->mv_col_min < col_min )
2028                 x->mv_col_min = col_min;
2029             if (x->mv_col_max > col_max )
2030                 x->mv_col_max = col_max;
2031             if (x->mv_row_min < row_min )
2032                 x->mv_row_min = row_min;
2033             if (x->mv_row_max > row_max )
2034                 x->mv_row_max = row_max;
2035
2036             //adjust search range according to sr from mv prediction
2037             if(sr > step_param)
2038                 step_param = sr;
2039
2040             // Initial step/diamond search
2041             if (cpi->sf.search_method == HEX)
2042             {
2043                 bestsme = vp8_hex_search(x, b, d, &best_ref_mv, &d->bmi.mv, step_param, sadpb/*x->errorperbit*/, &num00, &cpi->fn_ptr[BLOCK_16X16], x->mvsadcost, x->mvcost, &best_ref_mv);
2044                 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
2045             }
2046             else
2047             {
2048                 bestsme = cpi->diamond_search_sad(x, b, d, &mvp, &d->bmi.mv, step_param, sadpb / 2/*x->errorperbit*/, &num00, &cpi->fn_ptr[BLOCK_16X16], x->mvcost, &best_ref_mv); //sadpb < 9
2049                 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
2050
2051                 // Further step/diamond searches as necessary
2052                 n = 0;
2053                 further_steps = (cpi->sf.max_step_search_steps - 1) - step_param;
2054
2055                 n = num00;
2056                 num00 = 0;
2057
2058                 /* If there won't be more n-step search, check to see if refining search is needed. */
2059                 if (n > further_steps)
2060                     do_refine = 0;
2061
2062                 while (n < further_steps)
2063                 {
2064                     n++;
2065
2066                     if (num00)
2067                         num00--;
2068                     else
2069                     {
2070                         thissme = cpi->diamond_search_sad(x, b, d, &mvp, &d->bmi.mv, step_param + n, sadpb / 4/*x->errorperbit*/, &num00, &cpi->fn_ptr[BLOCK_16X16], x->mvcost, &best_ref_mv); //sadpb = 9
2071
2072                         /* check to see if refining search is needed. */
2073                         if (num00 > (further_steps-n))
2074                             do_refine = 0;
2075
2076                         if (thissme < bestsme)
2077                         {
2078                             bestsme = thissme;
2079                             mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
2080                         }
2081                         else
2082                         {
2083                             d->bmi.mv.as_int = mode_mv[NEWMV].as_int;
2084                         }
2085                     }
2086                 }
2087             }
2088
2089             /* final 1-away diamond refining search */
2090             if (do_refine == 1)
2091             {
2092                 int search_range;
2093
2094                 //It seems not a good way to set search_range. Need further investigation.
2095                 //search_range = MAXF(abs((mvp.row>>3) - d->bmi.mv.as_mv.row), abs((mvp.col>>3) - d->bmi.mv.as_mv.col));
2096                 search_range = 8;
2097
2098                 //thissme = cpi->full_search_sad(x, b, d, &d->bmi.mv.as_mv, sadpb, search_range, &cpi->fn_ptr[BLOCK_16X16], x->mvcost, &best_ref_mv);
2099                 thissme = cpi->refining_search_sad(x, b, d, &d->bmi.mv, sadpb/4, search_range, &cpi->fn_ptr[BLOCK_16X16], x->mvcost, &best_ref_mv);
2100
2101                 if (thissme < bestsme)
2102                 {
2103                     bestsme = thissme;
2104                     mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
2105                 }
2106                 else
2107                 {
2108                     d->bmi.mv.as_int = mode_mv[NEWMV].as_int;
2109                 }
2110             }
2111
2112             x->mv_col_min = tmp_col_min;
2113             x->mv_col_max = tmp_col_max;
2114             x->mv_row_min = tmp_row_min;
2115             x->mv_row_max = tmp_row_max;
2116
2117             if (bestsme < INT_MAX)
2118             {
2119                 int dis; /* TODO: use dis in distortion calculation later. */
2120                 unsigned int sse;
2121                 cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv, &best_ref_mv, x->errorperbit / 4, &cpi->fn_ptr[BLOCK_16X16], x->mvcost, &dis, &sse);
2122             }
2123
2124             mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
2125
2126             // Add the new motion vector cost to our rolling cost variable
2127             rate2 += vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv, x->mvcost, 96);
2128         }
2129
2130         case NEARESTMV:
2131         case NEARMV:
2132             // Clip "next_nearest" so that it does not extend to far out of image
2133             vp8_clamp_mv2(&mode_mv[this_mode], xd);
2134
2135             // Do not bother proceeding if the vector (from newmv,nearest or near) is 0,0 as this should then be coded using the zeromv mode.
2136             if (((this_mode == NEARMV) || (this_mode == NEARESTMV)) && (mode_mv[this_mode].as_int == 0))
2137                 continue;
2138
2139         case ZEROMV:
2140
2141             // Trap vectors that reach beyond the UMV borders
2142             // Note that ALL New MV, Nearest MV Near MV and Zero MV code drops through to this point
2143             // because of the lack of break statements in the previous two cases.
2144             if (((mode_mv[this_mode].as_mv.row >> 3) < x->mv_row_min) || ((mode_mv[this_mode].as_mv.row >> 3) > x->mv_row_max) ||
2145                 ((mode_mv[this_mode].as_mv.col >> 3) < x->mv_col_min) || ((mode_mv[this_mode].as_mv.col >> 3) > x->mv_col_max))
2146                 continue;
2147
2148             vp8_set_mbmode_and_mvs(x, this_mode, &mode_mv[this_mode]);
2149             vp8_build_inter16x16_predictors_mby(&x->e_mbd);
2150
2151             if (cpi->active_map_enabled && x->active_ptr[0] == 0) {
2152                 x->skip = 1;
2153             }
2154             else if (x->encode_breakout)
2155             {
2156                 int sum;
2157                 unsigned int sse;
2158                 int threshold = (xd->block[0].dequant[1]
2159                             * xd->block[0].dequant[1] >>4);
2160
2161                 if(threshold < x->encode_breakout)
2162                     threshold = x->encode_breakout;
2163
2164                 VARIANCE_INVOKE(&cpi->rtcd.variance, get16x16var)
2165                     (x->src.y_buffer, x->src.y_stride,
2166                      x->e_mbd.predictor, 16, &sse, &sum);
2167
2168                 if (sse < threshold)
2169                 {
2170                     // Check u and v to make sure skip is ok
2171                     int sse2 = 0;
2172                     /* If theres is no codeable 2nd order dc
2173                        or a very small uniform pixel change change */
2174                     if (abs(sum) < (xd->block[24].dequant[0]<<2)||
2175                         ((sum * sum>>8) > sse && abs(sum) <128))
2176                     {
2177                         sse2 = VP8_UVSSE(x, IF_RTCD(&cpi->rtcd.variance));
2178
2179                         if (sse2 * 2 < threshold)
2180                         {
2181                             x->skip = 1;
2182                             distortion2 = sse + sse2;
2183                             rate2 = 500;
2184
2185                             /* for best_yrd calculation */
2186                             rate_uv = 0;
2187                             distortion_uv = sse2;
2188
2189                             disable_skip = 1;
2190                             this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
2191
2192                             break;
2193                         }
2194                     }
2195                 }
2196             }
2197
2198
2199             //intermodecost[mode_index] = vp8_cost_mv_ref(this_mode, mdcounts);   // Experimental debug code
2200
2201             // Add in the Mv/mode cost
2202             rate2 += vp8_cost_mv_ref(this_mode, mdcounts);
2203
2204             // Y cost and distortion
2205             macro_block_yrd(x, &rate_y, &distortion, IF_RTCD(&cpi->rtcd.encodemb));
2206             rate2 += rate_y;
2207             distortion2 += distortion;
2208
2209             // UV cost and distortion
2210             vp8_rd_inter_uv(cpi, x, &rate_uv, &distortion_uv, cpi->common.full_pixel);
2211             rate2 += rate_uv;
2212             distortion2 += distortion_uv;
2213             break;
2214
2215         default:
2216             break;
2217         }
2218
2219         // Where skip is allowable add in the default per mb cost for the no skip case.
2220         // where we then decide to skip we have to delete this and replace it with the
2221         // cost of signallying a skip
2222         if (cpi->common.mb_no_coeff_skip)
2223         {
2224             other_cost += vp8_cost_bit(cpi->prob_skip_false, 0);
2225             rate2 += other_cost;
2226         }
2227
2228         // Estimate the reference frame signaling cost and add it to the rolling cost variable.
2229         rate2 += ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame];
2230
2231         if (!disable_skip)
2232         {
2233             // Test for the condition where skip block will be activated because there are no non zero coefficients and make any necessary adjustment for rate
2234             if (cpi->common.mb_no_coeff_skip)
2235             {
2236                 int tteob;
2237
2238                 tteob = 0;
2239
2240                 for (i = 0; i <= 24; i++)
2241                 {
2242                     tteob += x->e_mbd.block[i].eob;
2243                 }
2244
2245                 if (tteob == 0)
2246                 {
2247                     rate2 -= (rate_y + rate_uv);
2248                     //for best_yrd calculation
2249                     rate_uv = 0;
2250
2251                     // Back out no skip flag costing and add in skip flag costing
2252                     if (cpi->prob_skip_false)
2253                     {
2254                         int prob_skip_cost;
2255
2256                         prob_skip_cost = vp8_cost_bit(cpi->prob_skip_false, 1);
2257                         prob_skip_cost -= vp8_cost_bit(cpi->prob_skip_false, 0);
2258                         rate2 += prob_skip_cost;
2259                         other_cost += prob_skip_cost;
2260                     }
2261                 }
2262             }
2263             // Calculate the final RD estimate for this mode
2264             this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
2265         }
2266
2267         // Experimental debug code.
2268         //all_rds[mode_index] = this_rd;
2269         //all_rates[mode_index] = rate2;
2270         //all_dist[mode_index] = distortion2;
2271
2272         // Keep record of best intra distortion
2273         if ((x->e_mbd.mode_info_context->mbmi.ref_frame == INTRA_FRAME) &&
2274             (this_rd < best_intra_rd) )
2275         {
2276             best_intra_rd = this_rd;
2277             *returnintra = distortion2 ;
2278         }
2279
2280         // Did this mode help.. i.i is it the new best mode
2281         if (this_rd < best_rd || x->skip)
2282         {
2283             // Note index of best mode so far
2284             best_mode_index = mode_index;
2285
2286             if (this_mode <= B_PRED)
2287             {
2288                 x->e_mbd.mode_info_context->mbmi.uv_mode = uv_intra_mode;
2289             }
2290
2291             other_cost += ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame];
2292
2293             /* Calculate the final y RD estimate for this mode */
2294             best_yrd = RDCOST(x->rdmult, x->rddiv, (rate2-rate_uv-other_cost),
2295                               (distortion2-distortion_uv));
2296
2297             *returnrate = rate2;
2298             *returndistortion = distortion2;
2299             best_rd = this_rd;
2300             vpx_memcpy(&best_mbmode, &x->e_mbd.mode_info_context->mbmi, sizeof(MB_MODE_INFO));
2301             vpx_memcpy(&best_partition, x->partition_info, sizeof(PARTITION_INFO));
2302
2303             for (i = 0; i < 16; i++)
2304             {
2305                 vpx_memcpy(&best_bmodes[i], &x->e_mbd.block[i].bmi, sizeof(B_MODE_INFO));
2306             }
2307
2308             // Testing this mode gave rise to an improvement in best error score. Lower threshold a bit for next time
2309             cpi->rd_thresh_mult[mode_index] = (cpi->rd_thresh_mult[mode_index] >= (MIN_THRESHMULT + 2)) ? cpi->rd_thresh_mult[mode_index] - 2 : MIN_THRESHMULT;
2310             cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
2311         }
2312
2313         // If the mode did not help improve the best error case then raise the threshold for testing that mode next time around.
2314         else
2315         {
2316             cpi->rd_thresh_mult[mode_index] += 4;
2317
2318             if (cpi->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
2319                 cpi->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
2320
2321             cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
2322         }
2323
2324         if (x->skip)
2325             break;
2326
2327     }
2328
2329     // Reduce the activation RD thresholds for the best choice mode
2330     if ((cpi->rd_baseline_thresh[best_mode_index] > 0) && (cpi->rd_baseline_thresh[best_mode_index] < (INT_MAX >> 2)))
2331     {
2332         int best_adjustment = (cpi->rd_thresh_mult[best_mode_index] >> 2);
2333
2334         cpi->rd_thresh_mult[best_mode_index] = (cpi->rd_thresh_mult[best_mode_index] >= (MIN_THRESHMULT + best_adjustment)) ? cpi->rd_thresh_mult[best_mode_index] - best_adjustment : MIN_THRESHMULT;
2335         cpi->rd_threshes[best_mode_index] = (cpi->rd_baseline_thresh[best_mode_index] >> 7) * cpi->rd_thresh_mult[best_mode_index];
2336
2337         // If we chose a split mode then reset the new MV thresholds as well
2338         /*if ( vp8_mode_order[best_mode_index] == SPLITMV )
2339         {
2340             best_adjustment = 4; //(cpi->rd_thresh_mult[THR_NEWMV] >> 4);
2341             cpi->rd_thresh_mult[THR_NEWMV] = (cpi->rd_thresh_mult[THR_NEWMV] >= (MIN_THRESHMULT+best_adjustment)) ? cpi->rd_thresh_mult[THR_NEWMV]-best_adjustment: MIN_THRESHMULT;
2342             cpi->rd_threshes[THR_NEWMV] = (cpi->rd_baseline_thresh[THR_NEWMV] >> 7) * cpi->rd_thresh_mult[THR_NEWMV];
2343
2344             best_adjustment = 4; //(cpi->rd_thresh_mult[THR_NEWG] >> 4);
2345             cpi->rd_thresh_mult[THR_NEWG] = (cpi->rd_thresh_mult[THR_NEWG] >= (MIN_THRESHMULT+best_adjustment)) ? cpi->rd_thresh_mult[THR_NEWG]-best_adjustment: MIN_THRESHMULT;
2346             cpi->rd_threshes[THR_NEWG] = (cpi->rd_baseline_thresh[THR_NEWG] >> 7) * cpi->rd_thresh_mult[THR_NEWG];
2347
2348             best_adjustment = 4; //(cpi->rd_thresh_mult[THR_NEWA] >> 4);
2349             cpi->rd_thresh_mult[THR_NEWA] = (cpi->rd_thresh_mult[THR_NEWA] >= (MIN_THRESHMULT+best_adjustment)) ? cpi->rd_thresh_mult[THR_NEWA]-best_adjustment: MIN_THRESHMULT;
2350             cpi->rd_threshes[THR_NEWA] = (cpi->rd_baseline_thresh[THR_NEWA] >> 7) * cpi->rd_thresh_mult[THR_NEWA];
2351         }*/
2352
2353     }
2354
2355     // Keep a record of best mode index that we chose
2356     cpi->last_best_mode_index = best_mode_index;
2357
2358     // Note how often each mode chosen as best
2359     cpi->mode_chosen_counts[best_mode_index] ++;
2360
2361
2362     if (cpi->is_src_frame_alt_ref && (best_mbmode.mode != ZEROMV || best_mbmode.ref_frame != ALTREF_FRAME))
2363     {
2364         best_mbmode.mode = ZEROMV;
2365         best_mbmode.ref_frame = ALTREF_FRAME;
2366         best_mbmode.mv.as_int = 0;
2367         best_mbmode.uv_mode = 0;
2368         best_mbmode.mb_skip_coeff = (cpi->common.mb_no_coeff_skip) ? 1 : 0;
2369         best_mbmode.partitioning = 0;
2370
2371         vpx_memcpy(&x->e_mbd.mode_info_context->mbmi, &best_mbmode, sizeof(MB_MODE_INFO));
2372         vpx_memcpy(x->partition_info, &best_partition, sizeof(PARTITION_INFO));
2373
2374         for (i = 0; i < 16; i++)
2375         {
2376             vpx_memset(&x->e_mbd.block[i].bmi, 0, sizeof(B_MODE_INFO));
2377         }
2378
2379         x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
2380         return;
2381     }
2382
2383
2384     if(best_mbmode.mode <= B_PRED)
2385     {
2386         int i;
2387         for (i = 0; i < 16; i++)
2388         {
2389             best_bmodes[i].mv.as_int = 0;
2390         }
2391     }
2392
2393     // macroblock modes
2394     vpx_memcpy(&x->e_mbd.mode_info_context->mbmi, &best_mbmode, sizeof(MB_MODE_INFO));
2395     vpx_memcpy(x->partition_info, &best_partition, sizeof(PARTITION_INFO));
2396
2397     for (i = 0; i < 16; i++)
2398     {
2399         vpx_memcpy(&x->e_mbd.block[i].bmi, &best_bmodes[i], sizeof(B_MODE_INFO));
2400     }
2401
2402     x->e_mbd.mode_info_context->mbmi.mv.as_mv = x->e_mbd.block[15].bmi.mv.as_mv;
2403 }