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