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