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