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