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