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 <limits.h>
13 #include "vpx_config.h"
14 #include "onyx_int.h"
15 #include "modecosts.h"
16 #include "encodeintra.h"
17 #include "vp8/common/entropymode.h"
18 #include "pickinter.h"
19 #include "vp8/common/findnearmv.h"
20 #include "encodemb.h"
21 #include "vp8/common/reconinter.h"
22 #include "vp8/common/reconintra4x4.h"
23 #include "vp8/common/variance.h"
24 #include "mcomp.h"
25 #include "rdopt.h"
26 #include "vpx_mem/vpx_mem.h"
27 #if CONFIG_TEMPORAL_DENOISING
28 #include "denoising.h"
29 #endif
30 
31 extern int VP8_UVSSE(MACROBLOCK *x);
32 
33 #ifdef SPEEDSTATS
34 extern unsigned int cnt_pm;
35 #endif
36 
37 extern const int vp8_ref_frame_order[MAX_MODES];
38 extern const MB_PREDICTION_MODE vp8_mode_order[MAX_MODES];
39 
40 extern int vp8_cost_mv_ref(MB_PREDICTION_MODE m, const int near_mv_ref_ct[4]);
41 
42 
vp8_skip_fractional_mv_step(MACROBLOCK * mb,BLOCK * b,BLOCKD * d,int_mv * bestmv,int_mv * ref_mv,int error_per_bit,const vp8_variance_fn_ptr_t * vfp,int * mvcost[2],int * distortion,unsigned int * sse)43 int vp8_skip_fractional_mv_step(MACROBLOCK *mb, BLOCK *b, BLOCKD *d,
44                                 int_mv *bestmv, int_mv *ref_mv,
45                                 int error_per_bit,
46                                 const vp8_variance_fn_ptr_t *vfp,
47                                 int *mvcost[2], int *distortion,
48                                 unsigned int *sse)
49 {
50     (void) b;
51     (void) d;
52     (void) ref_mv;
53     (void) error_per_bit;
54     (void) vfp;
55     (void) mvcost;
56     (void) distortion;
57     (void) sse;
58     (void) mb;
59     bestmv->as_mv.row <<= 3;
60     bestmv->as_mv.col <<= 3;
61     return 0;
62 }
63 
64 
vp8_get_inter_mbpred_error(MACROBLOCK * mb,const vp8_variance_fn_ptr_t * vfp,unsigned int * sse,int_mv this_mv)65 int vp8_get_inter_mbpred_error(MACROBLOCK *mb,
66                                   const vp8_variance_fn_ptr_t *vfp,
67                                   unsigned int *sse,
68                                   int_mv this_mv)
69 {
70 
71     BLOCK *b = &mb->block[0];
72     BLOCKD *d = &mb->e_mbd.block[0];
73     unsigned char *what = (*(b->base_src) + b->src);
74     int what_stride = b->src_stride;
75     int pre_stride = mb->e_mbd.pre.y_stride;
76     unsigned char *in_what = mb->e_mbd.pre.y_buffer + d->offset ;
77     int in_what_stride = pre_stride;
78     int xoffset = this_mv.as_mv.col & 7;
79     int yoffset = this_mv.as_mv.row & 7;
80 
81     in_what += (this_mv.as_mv.row >> 3) * pre_stride + (this_mv.as_mv.col >> 3);
82 
83     if (xoffset | yoffset)
84     {
85         return vfp->svf(in_what, in_what_stride, xoffset, yoffset, what, what_stride, sse);
86     }
87     else
88     {
89         return vfp->vf(what, what_stride, in_what, in_what_stride, sse);
90     }
91 
92 }
93 
94 
vp8_get4x4sse_cs_c(const unsigned char * src_ptr,int source_stride,const unsigned char * ref_ptr,int recon_stride)95 unsigned int vp8_get4x4sse_cs_c
96 (
97     const unsigned char *src_ptr,
98     int  source_stride,
99     const unsigned char *ref_ptr,
100     int  recon_stride
101 )
102 {
103     int distortion = 0;
104     int r, c;
105 
106     for (r = 0; r < 4; r++)
107     {
108         for (c = 0; c < 4; c++)
109         {
110             int diff = src_ptr[c] - ref_ptr[c];
111             distortion += diff * diff;
112         }
113 
114         src_ptr += source_stride;
115         ref_ptr += recon_stride;
116     }
117 
118     return distortion;
119 }
120 
get_prediction_error(BLOCK * be,BLOCKD * b)121 static int get_prediction_error(BLOCK *be, BLOCKD *b)
122 {
123     unsigned char *sptr;
124     unsigned char *dptr;
125     sptr = (*(be->base_src) + be->src);
126     dptr = b->predictor;
127 
128     return vp8_get4x4sse_cs(sptr, be->src_stride, dptr, 16);
129 
130 }
131 
pick_intra4x4block(MACROBLOCK * x,int ib,B_PREDICTION_MODE * best_mode,const int * mode_costs,int * bestrate,int * bestdistortion)132 static int pick_intra4x4block(
133     MACROBLOCK *x,
134     int ib,
135     B_PREDICTION_MODE *best_mode,
136     const int *mode_costs,
137 
138     int *bestrate,
139     int *bestdistortion)
140 {
141 
142     BLOCKD *b = &x->e_mbd.block[ib];
143     BLOCK *be = &x->block[ib];
144     int dst_stride = x->e_mbd.dst.y_stride;
145     unsigned char *dst = x->e_mbd.dst.y_buffer + b->offset;
146     B_PREDICTION_MODE mode;
147     int best_rd = INT_MAX;
148     int rate;
149     int distortion;
150 
151     unsigned char *Above = dst - dst_stride;
152     unsigned char *yleft = dst - 1;
153     unsigned char top_left = Above[-1];
154 
155     for (mode = B_DC_PRED; mode <= B_HE_PRED; mode++)
156     {
157         int this_rd;
158 
159         rate = mode_costs[mode];
160 
161         vp8_intra4x4_predict(Above, yleft, dst_stride, mode,
162                              b->predictor, 16, top_left);
163         distortion = get_prediction_error(be, b);
164         this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
165 
166         if (this_rd < best_rd)
167         {
168             *bestrate = rate;
169             *bestdistortion = distortion;
170             best_rd = this_rd;
171             *best_mode = mode;
172         }
173     }
174 
175     b->bmi.as_mode = *best_mode;
176     vp8_encode_intra4x4block(x, ib);
177     return best_rd;
178 }
179 
180 
pick_intra4x4mby_modes(MACROBLOCK * mb,int * Rate,int * best_dist)181 static int pick_intra4x4mby_modes
182 (
183     MACROBLOCK *mb,
184     int *Rate,
185     int *best_dist
186 )
187 {
188     MACROBLOCKD *const xd = &mb->e_mbd;
189     int i;
190     int cost = mb->mbmode_cost [xd->frame_type] [B_PRED];
191     int error;
192     int distortion = 0;
193     const int *bmode_costs;
194 
195     intra_prediction_down_copy(xd, xd->dst.y_buffer - xd->dst.y_stride + 16);
196 
197     bmode_costs = mb->inter_bmode_costs;
198 
199     for (i = 0; i < 16; i++)
200     {
201         MODE_INFO *const mic = xd->mode_info_context;
202         const int mis = xd->mode_info_stride;
203 
204         B_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
205         int UNINITIALIZED_IS_SAFE(r), UNINITIALIZED_IS_SAFE(d);
206         d = 0; r = 0; best_mode = 0;
207 
208         if (mb->e_mbd.frame_type == KEY_FRAME)
209         {
210             const B_PREDICTION_MODE A = above_block_mode(mic, i, mis);
211             const B_PREDICTION_MODE L = left_block_mode(mic, i);
212 
213             bmode_costs  = mb->bmode_costs[A][L];
214         }
215 
216 
217         pick_intra4x4block(mb, i, &best_mode, bmode_costs, &r, &d);
218 
219         cost += r;
220         distortion += d;
221         mic->bmi[i].as_mode = best_mode;
222 
223         /* Break out case where we have already exceeded best so far value
224          * that was passed in
225          */
226         if (distortion > *best_dist)
227             break;
228     }
229 
230     *Rate = cost;
231 
232     if (i == 16)
233     {
234         *best_dist = distortion;
235         error = RDCOST(mb->rdmult, mb->rddiv, cost, distortion);
236     }
237     else
238     {
239         *best_dist = INT_MAX;
240         error = INT_MAX;
241     }
242 
243     return error;
244 }
245 
pick_intra_mbuv_mode(MACROBLOCK * mb)246 static void pick_intra_mbuv_mode(MACROBLOCK *mb)
247 {
248 
249     MACROBLOCKD *x = &mb->e_mbd;
250     unsigned char *uabove_row = x->dst.u_buffer - x->dst.uv_stride;
251     unsigned char *vabove_row = x->dst.v_buffer - x->dst.uv_stride;
252     unsigned char *usrc_ptr = (mb->block[16].src + *mb->block[16].base_src);
253     unsigned char *vsrc_ptr = (mb->block[20].src + *mb->block[20].base_src);
254     int uvsrc_stride = mb->block[16].src_stride;
255     unsigned char uleft_col[8];
256     unsigned char vleft_col[8];
257     unsigned char utop_left = uabove_row[-1];
258     unsigned char vtop_left = vabove_row[-1];
259     int i, j;
260     int expected_udc;
261     int expected_vdc;
262     int shift;
263     int Uaverage = 0;
264     int Vaverage = 0;
265     int diff;
266     int pred_error[4] = {0, 0, 0, 0}, best_error = INT_MAX;
267     MB_PREDICTION_MODE best_mode = DC_PRED;
268 
269 
270     for (i = 0; i < 8; i++)
271     {
272         uleft_col[i] = x->dst.u_buffer [i* x->dst.uv_stride -1];
273         vleft_col[i] = x->dst.v_buffer [i* x->dst.uv_stride -1];
274     }
275 
276     if (!x->up_available && !x->left_available)
277     {
278         expected_udc = 128;
279         expected_vdc = 128;
280     }
281     else
282     {
283         shift = 2;
284 
285         if (x->up_available)
286         {
287 
288             for (i = 0; i < 8; i++)
289             {
290                 Uaverage += uabove_row[i];
291                 Vaverage += vabove_row[i];
292             }
293 
294             shift ++;
295 
296         }
297 
298         if (x->left_available)
299         {
300             for (i = 0; i < 8; i++)
301             {
302                 Uaverage += uleft_col[i];
303                 Vaverage += vleft_col[i];
304             }
305 
306             shift ++;
307 
308         }
309 
310         expected_udc = (Uaverage + (1 << (shift - 1))) >> shift;
311         expected_vdc = (Vaverage + (1 << (shift - 1))) >> shift;
312     }
313 
314 
315     for (i = 0; i < 8; i++)
316     {
317         for (j = 0; j < 8; j++)
318         {
319 
320             int predu = uleft_col[i] + uabove_row[j] - utop_left;
321             int predv = vleft_col[i] + vabove_row[j] - vtop_left;
322             int u_p, v_p;
323 
324             u_p = usrc_ptr[j];
325             v_p = vsrc_ptr[j];
326 
327             if (predu < 0)
328                 predu = 0;
329 
330             if (predu > 255)
331                 predu = 255;
332 
333             if (predv < 0)
334                 predv = 0;
335 
336             if (predv > 255)
337                 predv = 255;
338 
339 
340             diff = u_p - expected_udc;
341             pred_error[DC_PRED] += diff * diff;
342             diff = v_p - expected_vdc;
343             pred_error[DC_PRED] += diff * diff;
344 
345 
346             diff = u_p - uabove_row[j];
347             pred_error[V_PRED] += diff * diff;
348             diff = v_p - vabove_row[j];
349             pred_error[V_PRED] += diff * diff;
350 
351 
352             diff = u_p - uleft_col[i];
353             pred_error[H_PRED] += diff * diff;
354             diff = v_p - vleft_col[i];
355             pred_error[H_PRED] += diff * diff;
356 
357 
358             diff = u_p - predu;
359             pred_error[TM_PRED] += diff * diff;
360             diff = v_p - predv;
361             pred_error[TM_PRED] += diff * diff;
362 
363 
364         }
365 
366         usrc_ptr += uvsrc_stride;
367         vsrc_ptr += uvsrc_stride;
368 
369         if (i == 3)
370         {
371             usrc_ptr = (mb->block[18].src + *mb->block[18].base_src);
372             vsrc_ptr = (mb->block[22].src + *mb->block[22].base_src);
373         }
374 
375 
376 
377     }
378 
379 
380     for (i = DC_PRED; i <= TM_PRED; i++)
381     {
382         if (best_error > pred_error[i])
383         {
384             best_error = pred_error[i];
385             best_mode = (MB_PREDICTION_MODE)i;
386         }
387     }
388 
389 
390     mb->e_mbd.mode_info_context->mbmi.uv_mode = best_mode;
391 
392 }
393 
update_mvcount(MACROBLOCK * x,int_mv * best_ref_mv)394 static void update_mvcount(MACROBLOCK *x, int_mv *best_ref_mv)
395 {
396     MACROBLOCKD *xd = &x->e_mbd;
397     /* Split MV modes currently not supported when RD is nopt enabled,
398      * therefore, only need to modify MVcount in NEWMV mode. */
399     if (xd->mode_info_context->mbmi.mode == NEWMV)
400     {
401         x->MVcount[0][mv_max+((xd->mode_info_context->mbmi.mv.as_mv.row -
402                                       best_ref_mv->as_mv.row) >> 1)]++;
403         x->MVcount[1][mv_max+((xd->mode_info_context->mbmi.mv.as_mv.col -
404                                       best_ref_mv->as_mv.col) >> 1)]++;
405     }
406 }
407 
408 
409 #if CONFIG_MULTI_RES_ENCODING
410 static
get_lower_res_motion_info(VP8_COMP * cpi,MACROBLOCKD * xd,int * dissim,int * parent_ref_frame,MB_PREDICTION_MODE * parent_mode,int_mv * parent_ref_mv,int mb_row,int mb_col)411 void get_lower_res_motion_info(VP8_COMP *cpi, MACROBLOCKD *xd, int *dissim,
412                                int *parent_ref_frame,
413                                MB_PREDICTION_MODE *parent_mode,
414                                int_mv *parent_ref_mv, int mb_row, int mb_col)
415 {
416     LOWER_RES_MB_INFO* store_mode_info
417                           = ((LOWER_RES_FRAME_INFO*)cpi->oxcf.mr_low_res_mode_info)->mb_info;
418     unsigned int parent_mb_index;
419 
420     /* Consider different down_sampling_factor.  */
421     {
422         /* TODO: Removed the loop that supports special down_sampling_factor
423          * such as 2, 4, 8. Will revisit it if needed.
424          * Should also try using a look-up table to see if it helps
425          * performance. */
426         int parent_mb_row, parent_mb_col;
427 
428         parent_mb_row = mb_row*cpi->oxcf.mr_down_sampling_factor.den
429                     /cpi->oxcf.mr_down_sampling_factor.num;
430         parent_mb_col = mb_col*cpi->oxcf.mr_down_sampling_factor.den
431                     /cpi->oxcf.mr_down_sampling_factor.num;
432         parent_mb_index = parent_mb_row*cpi->mr_low_res_mb_cols + parent_mb_col;
433     }
434 
435     /* Read lower-resolution mode & motion result from memory.*/
436     *parent_ref_frame = store_mode_info[parent_mb_index].ref_frame;
437     *parent_mode =  store_mode_info[parent_mb_index].mode;
438     *dissim = store_mode_info[parent_mb_index].dissim;
439 
440     /* For highest-resolution encoder, adjust dissim value. Lower its quality
441      * for good performance. */
442     if (cpi->oxcf.mr_encoder_id == (cpi->oxcf.mr_total_resolutions - 1))
443         *dissim>>=1;
444 
445     if(*parent_ref_frame != INTRA_FRAME)
446     {
447         /* Consider different down_sampling_factor.
448          * The result can be rounded to be more precise, but it takes more time.
449          */
450         (*parent_ref_mv).as_mv.row = store_mode_info[parent_mb_index].mv.as_mv.row
451                                   *cpi->oxcf.mr_down_sampling_factor.num
452                                   /cpi->oxcf.mr_down_sampling_factor.den;
453         (*parent_ref_mv).as_mv.col = store_mode_info[parent_mb_index].mv.as_mv.col
454                                   *cpi->oxcf.mr_down_sampling_factor.num
455                                   /cpi->oxcf.mr_down_sampling_factor.den;
456 
457         vp8_clamp_mv2(parent_ref_mv, xd);
458     }
459 }
460 #endif
461 
check_for_encode_breakout(unsigned int sse,MACROBLOCK * x)462 static void check_for_encode_breakout(unsigned int sse, MACROBLOCK* x)
463 {
464     MACROBLOCKD *xd = &x->e_mbd;
465 
466     unsigned int threshold = (xd->block[0].dequant[1]
467         * xd->block[0].dequant[1] >>4);
468 
469     if(threshold < x->encode_breakout)
470         threshold = x->encode_breakout;
471 
472     if (sse < threshold )
473     {
474         /* Check u and v to make sure skip is ok */
475         unsigned int sse2 = 0;
476 
477         sse2 = VP8_UVSSE(x);
478 
479         if (sse2 * 2 < x->encode_breakout)
480             x->skip = 1;
481         else
482             x->skip = 0;
483     }
484 }
485 
evaluate_inter_mode(unsigned int * sse,int rate2,int * distortion2,VP8_COMP * cpi,MACROBLOCK * x,int rd_adj)486 static int evaluate_inter_mode(unsigned int* sse, int rate2, int* distortion2,
487                                VP8_COMP *cpi, MACROBLOCK *x, int rd_adj)
488 {
489     MB_PREDICTION_MODE this_mode = x->e_mbd.mode_info_context->mbmi.mode;
490     int_mv mv = x->e_mbd.mode_info_context->mbmi.mv;
491     int this_rd;
492     /* Exit early and don't compute the distortion if this macroblock
493      * is marked inactive. */
494     if (cpi->active_map_enabled && x->active_ptr[0] == 0)
495     {
496         *sse = 0;
497         *distortion2 = 0;
498         x->skip = 1;
499         return INT_MAX;
500     }
501 
502     if((this_mode != NEWMV) ||
503         !(cpi->sf.half_pixel_search) || cpi->common.full_pixel==1)
504         *distortion2 = vp8_get_inter_mbpred_error(x,
505                                               &cpi->fn_ptr[BLOCK_16X16],
506                                               sse, mv);
507 
508     this_rd = RDCOST(x->rdmult, x->rddiv, rate2, *distortion2);
509 
510     /* Adjust rd to bias to ZEROMV */
511     if(this_mode == ZEROMV)
512     {
513         /* Bias to ZEROMV on LAST_FRAME reference when it is available. */
514         if ((cpi->ref_frame_flags & VP8_LAST_FRAME &
515             cpi->common.refresh_last_frame)
516             && x->e_mbd.mode_info_context->mbmi.ref_frame != LAST_FRAME)
517             rd_adj = 100;
518 
519         // rd_adj <= 100
520         this_rd = ((int64_t)this_rd) * rd_adj / 100;
521     }
522 
523     check_for_encode_breakout(*sse, x);
524     return this_rd;
525 }
526 
calculate_zeromv_rd_adjustment(VP8_COMP * cpi,MACROBLOCK * x,int * rd_adjustment)527 static void calculate_zeromv_rd_adjustment(VP8_COMP *cpi, MACROBLOCK *x,
528                                     int *rd_adjustment)
529 {
530     MODE_INFO *mic = x->e_mbd.mode_info_context;
531     int_mv mv_l, mv_a, mv_al;
532     int local_motion_check = 0;
533 
534     if (cpi->lf_zeromv_pct > 40)
535     {
536         /* left mb */
537         mic -= 1;
538         mv_l = mic->mbmi.mv;
539 
540         if (mic->mbmi.ref_frame != INTRA_FRAME)
541             if( abs(mv_l.as_mv.row) < 8 && abs(mv_l.as_mv.col) < 8)
542                 local_motion_check++;
543 
544         /* above-left mb */
545         mic -= x->e_mbd.mode_info_stride;
546         mv_al = mic->mbmi.mv;
547 
548         if (mic->mbmi.ref_frame != INTRA_FRAME)
549             if( abs(mv_al.as_mv.row) < 8 && abs(mv_al.as_mv.col) < 8)
550                 local_motion_check++;
551 
552         /* above mb */
553         mic += 1;
554         mv_a = mic->mbmi.mv;
555 
556         if (mic->mbmi.ref_frame != INTRA_FRAME)
557             if( abs(mv_a.as_mv.row) < 8 && abs(mv_a.as_mv.col) < 8)
558                 local_motion_check++;
559 
560         if (((!x->e_mbd.mb_to_top_edge || !x->e_mbd.mb_to_left_edge)
561             && local_motion_check >0) ||  local_motion_check >2 )
562             *rd_adjustment = 80;
563         else if (local_motion_check > 0)
564             *rd_adjustment = 90;
565     }
566 }
567 
vp8_pick_inter_mode(VP8_COMP * cpi,MACROBLOCK * x,int recon_yoffset,int recon_uvoffset,int * returnrate,int * returndistortion,int * returnintra,int mb_row,int mb_col)568 void vp8_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset,
569                          int recon_uvoffset, int *returnrate,
570                          int *returndistortion, int *returnintra, int mb_row,
571                          int mb_col)
572 {
573     BLOCK *b = &x->block[0];
574     BLOCKD *d = &x->e_mbd.block[0];
575     MACROBLOCKD *xd = &x->e_mbd;
576     MB_MODE_INFO best_mbmode;
577 
578     int_mv best_ref_mv_sb[2];
579     int_mv mode_mv_sb[2][MB_MODE_COUNT];
580     int_mv best_ref_mv;
581     int_mv *mode_mv;
582     MB_PREDICTION_MODE this_mode;
583     int num00;
584     int mdcounts[4];
585     int best_rd = INT_MAX;
586     int rd_adjustment = 100;
587     int best_intra_rd = INT_MAX;
588     int mode_index;
589     int rate;
590     int rate2;
591     int distortion2;
592     int bestsme = INT_MAX;
593     int best_mode_index = 0;
594     unsigned int sse = INT_MAX, best_rd_sse = INT_MAX;
595 #if CONFIG_TEMPORAL_DENOISING
596     unsigned int zero_mv_sse = INT_MAX, best_sse = INT_MAX;
597 #endif
598 
599     int sf_improved_mv_pred = cpi->sf.improved_mv_pred;
600     int_mv mvp;
601 
602     int near_sadidx[8] = {0, 1, 2, 3, 4, 5, 6, 7};
603     int saddone=0;
604     /* search range got from mv_pred(). It uses step_param levels. (0-7) */
605     int sr=0;
606 
607     unsigned char *plane[4][3];
608     int ref_frame_map[4];
609     int sign_bias = 0;
610 
611     (void)mb_row;
612     (void)mb_col;
613 #if CONFIG_MULTI_RES_ENCODING
614     int dissim = INT_MAX;
615     int parent_ref_frame = 0;
616     int parent_ref_valid = cpi->oxcf.mr_encoder_id && cpi->mr_low_res_mv_avail;
617     int_mv parent_ref_mv;
618     MB_PREDICTION_MODE parent_mode = 0;
619     if (parent_ref_valid)
620     {
621         int parent_ref_flag;
622 
623         get_lower_res_motion_info(cpi, xd, &dissim, &parent_ref_frame,
624                                   &parent_mode, &parent_ref_mv, mb_row, mb_col);
625 
626         /* TODO(jkoleszar): The references available (ref_frame_flags) to the
627          * lower res encoder should match those available to this encoder, but
628          * there seems to be a situation where this mismatch can happen in the
629          * case of frame dropping and temporal layers. For example,
630          * GOLD being disallowed in ref_frame_flags, but being returned as
631          * parent_ref_frame.
632          *
633          * In this event, take the conservative approach of disabling the
634          * lower res info for this MB.
635          */
636         parent_ref_flag = 0;
637         if (parent_ref_frame == LAST_FRAME)
638             parent_ref_flag = (cpi->ref_frame_flags & VP8_LAST_FRAME);
639         else if (parent_ref_frame == GOLDEN_FRAME)
640             parent_ref_flag = (cpi->ref_frame_flags & VP8_GOLD_FRAME);
641         else if (parent_ref_frame == ALTREF_FRAME)
642             parent_ref_flag = (cpi->ref_frame_flags & VP8_ALTR_FRAME);
643 
644         //assert(!parent_ref_frame || parent_ref_flag);
645         if (parent_ref_frame && !parent_ref_flag)
646             parent_ref_valid = 0;
647     }
648 #endif
649 
650     mode_mv = mode_mv_sb[sign_bias];
651     best_ref_mv.as_int = 0;
652     vpx_memset(mode_mv_sb, 0, sizeof(mode_mv_sb));
653     vpx_memset(&best_mbmode, 0, sizeof(best_mbmode));
654 
655     /* Setup search priorities */
656 #if CONFIG_MULTI_RES_ENCODING
657     if (parent_ref_valid && parent_ref_frame && dissim < 8)
658     {
659         ref_frame_map[0] = -1;
660         ref_frame_map[1] = parent_ref_frame;
661         ref_frame_map[2] = -1;
662         ref_frame_map[3] = -1;
663     } else
664 #endif
665     get_reference_search_order(cpi, ref_frame_map);
666 
667     /* Check to see if there is at least 1 valid reference frame that we need
668      * to calculate near_mvs.
669      */
670     if (ref_frame_map[1] > 0)
671     {
672         sign_bias = vp8_find_near_mvs_bias(&x->e_mbd,
673                                            x->e_mbd.mode_info_context,
674                                            mode_mv_sb,
675                                            best_ref_mv_sb,
676                                            mdcounts,
677                                            ref_frame_map[1],
678                                            cpi->common.ref_frame_sign_bias);
679 
680         mode_mv = mode_mv_sb[sign_bias];
681         best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int;
682     }
683 
684     get_predictor_pointers(cpi, plane, recon_yoffset, recon_uvoffset);
685 
686     /* Count of the number of MBs tested so far this frame */
687     x->mbs_tested_so_far++;
688 
689     *returnintra = INT_MAX;
690     x->skip = 0;
691 
692     x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
693 
694     /* If the frame has big static background and current MB is in low
695      * motion area, its mode decision is biased to ZEROMV mode.
696      */
697     calculate_zeromv_rd_adjustment(cpi, x, &rd_adjustment);
698 
699     /* if we encode a new mv this is important
700      * find the best new motion vector
701      */
702     for (mode_index = 0; mode_index < MAX_MODES; mode_index++)
703     {
704         int frame_cost;
705         int this_rd = INT_MAX;
706         int this_ref_frame = ref_frame_map[vp8_ref_frame_order[mode_index]];
707 
708         if (best_rd <= x->rd_threshes[mode_index])
709             continue;
710 
711         if (this_ref_frame < 0)
712             continue;
713 
714         x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame;
715 
716         /* everything but intra */
717         if (x->e_mbd.mode_info_context->mbmi.ref_frame)
718         {
719             x->e_mbd.pre.y_buffer = plane[this_ref_frame][0];
720             x->e_mbd.pre.u_buffer = plane[this_ref_frame][1];
721             x->e_mbd.pre.v_buffer = plane[this_ref_frame][2];
722 
723             if (sign_bias != cpi->common.ref_frame_sign_bias[this_ref_frame])
724             {
725                 sign_bias = cpi->common.ref_frame_sign_bias[this_ref_frame];
726                 mode_mv = mode_mv_sb[sign_bias];
727                 best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int;
728             }
729 
730 #if CONFIG_MULTI_RES_ENCODING
731             if (parent_ref_valid)
732             {
733                 if (vp8_mode_order[mode_index] == NEARESTMV &&
734                     mode_mv[NEARESTMV].as_int ==0)
735                     continue;
736                 if (vp8_mode_order[mode_index] == NEARMV &&
737                     mode_mv[NEARMV].as_int ==0)
738                     continue;
739 
740                 if (vp8_mode_order[mode_index] == NEWMV && parent_mode == ZEROMV
741                     && best_ref_mv.as_int==0)
742                     continue;
743                 else if(vp8_mode_order[mode_index] == NEWMV && dissim==0
744                     && best_ref_mv.as_int==parent_ref_mv.as_int)
745                     continue;
746             }
747 #endif
748         }
749 
750         /* Check to see if the testing frequency for this mode is at its max
751          * If so then prevent it from being tested and increase the threshold
752          * for its testing */
753         if (x->mode_test_hit_counts[mode_index] &&
754                                          (cpi->mode_check_freq[mode_index] > 1))
755         {
756             if (x->mbs_tested_so_far <= (cpi->mode_check_freq[mode_index] *
757                                          x->mode_test_hit_counts[mode_index]))
758             {
759                 /* Increase the threshold for coding this mode to make it less
760                  * likely to be chosen */
761                 x->rd_thresh_mult[mode_index] += 4;
762 
763                 if (x->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
764                     x->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
765 
766                 x->rd_threshes[mode_index] =
767                                  (cpi->rd_baseline_thresh[mode_index] >> 7) *
768                                  x->rd_thresh_mult[mode_index];
769                 continue;
770             }
771         }
772 
773         /* We have now reached the point where we are going to test the current
774          * mode so increment the counter for the number of times it has been
775          * tested */
776         x->mode_test_hit_counts[mode_index] ++;
777 
778         rate2 = 0;
779         distortion2 = 0;
780 
781         this_mode = vp8_mode_order[mode_index];
782 
783         x->e_mbd.mode_info_context->mbmi.mode = this_mode;
784         x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
785 
786         /* Work out the cost assosciated with selecting the reference frame */
787         frame_cost =
788             x->ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame];
789         rate2 += frame_cost;
790 
791         /* Only consider ZEROMV/ALTREF_FRAME for alt ref frame,
792          * unless ARNR filtering is enabled in which case we want
793          * an unfiltered alternative */
794         if (cpi->is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0))
795         {
796             if (this_mode != ZEROMV ||
797                 x->e_mbd.mode_info_context->mbmi.ref_frame != ALTREF_FRAME)
798                 continue;
799         }
800 
801         switch (this_mode)
802         {
803         case B_PRED:
804             /* Pass best so far to pick_intra4x4mby_modes to use as breakout */
805             distortion2 = best_rd_sse;
806             pick_intra4x4mby_modes(x, &rate, &distortion2);
807 
808             if (distortion2 == INT_MAX)
809             {
810                 this_rd = INT_MAX;
811             }
812             else
813             {
814                 rate2 += rate;
815                 distortion2 = vp8_variance16x16(
816                                     *(b->base_src), b->src_stride,
817                                     x->e_mbd.predictor, 16, &sse);
818                 this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
819 
820                 if (this_rd < best_intra_rd)
821                 {
822                     best_intra_rd = this_rd;
823                     *returnintra = distortion2;
824                 }
825             }
826 
827             break;
828 
829         case SPLITMV:
830 
831             /* Split MV modes currently not supported when RD is not enabled. */
832             break;
833 
834         case DC_PRED:
835         case V_PRED:
836         case H_PRED:
837         case TM_PRED:
838             vp8_build_intra_predictors_mby_s(xd,
839                                              xd->dst.y_buffer - xd->dst.y_stride,
840                                              xd->dst.y_buffer - 1,
841                                              xd->dst.y_stride,
842                                              xd->predictor,
843                                              16);
844             distortion2 = vp8_variance16x16
845                                           (*(b->base_src), b->src_stride,
846                                           x->e_mbd.predictor, 16, &sse);
847             rate2 += x->mbmode_cost[x->e_mbd.frame_type][x->e_mbd.mode_info_context->mbmi.mode];
848             this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
849 
850             if (this_rd < best_intra_rd)
851             {
852                 best_intra_rd = this_rd;
853                 *returnintra = distortion2;
854             }
855             break;
856 
857         case NEWMV:
858         {
859             int thissme;
860             int step_param;
861             int further_steps;
862             int n = 0;
863             int sadpb = x->sadperbit16;
864             int_mv mvp_full;
865 
866             int col_min = ((best_ref_mv.as_mv.col+7)>>3) - MAX_FULL_PEL_VAL;
867             int row_min = ((best_ref_mv.as_mv.row+7)>>3) - MAX_FULL_PEL_VAL;
868             int col_max = (best_ref_mv.as_mv.col>>3)
869                          + MAX_FULL_PEL_VAL;
870             int row_max = (best_ref_mv.as_mv.row>>3)
871                          + MAX_FULL_PEL_VAL;
872 
873             int tmp_col_min = x->mv_col_min;
874             int tmp_col_max = x->mv_col_max;
875             int tmp_row_min = x->mv_row_min;
876             int tmp_row_max = x->mv_row_max;
877 
878             int speed_adjust = (cpi->Speed > 5) ? ((cpi->Speed >= 8)? 3 : 2) : 1;
879 
880             /* Further step/diamond searches as necessary */
881             step_param = cpi->sf.first_step + speed_adjust;
882 
883 #if CONFIG_MULTI_RES_ENCODING
884             /* If lower-res drops this frame, then higher-res encoder does
885                motion search without any previous knowledge. Also, since
886                last frame motion info is not stored, then we can not
887                use improved_mv_pred. */
888             if (cpi->oxcf.mr_encoder_id && !parent_ref_valid)
889                 sf_improved_mv_pred = 0;
890 
891             if (parent_ref_valid && parent_ref_frame)
892             {
893                 /* Use parent MV as predictor. Adjust search range
894                  * accordingly.
895                  */
896                 mvp.as_int = parent_ref_mv.as_int;
897                 mvp_full.as_mv.col = parent_ref_mv.as_mv.col>>3;
898                 mvp_full.as_mv.row = parent_ref_mv.as_mv.row>>3;
899 
900                 if(dissim <=32) step_param += 3;
901                 else if(dissim <=128) step_param += 2;
902                 else step_param += 1;
903             }else
904 #endif
905             {
906                 if(sf_improved_mv_pred)
907                 {
908                     if(!saddone)
909                     {
910                         vp8_cal_sad(cpi,xd,x, recon_yoffset ,&near_sadidx[0] );
911                         saddone = 1;
912                     }
913 
914                     vp8_mv_pred(cpi, &x->e_mbd, x->e_mbd.mode_info_context,
915                                 &mvp,x->e_mbd.mode_info_context->mbmi.ref_frame,
916                                 cpi->common.ref_frame_sign_bias, &sr,
917                                 &near_sadidx[0]);
918 
919                     sr += speed_adjust;
920                     /* adjust search range according to sr from mv prediction */
921                     if(sr > step_param)
922                         step_param = sr;
923 
924                     mvp_full.as_mv.col = mvp.as_mv.col>>3;
925                     mvp_full.as_mv.row = mvp.as_mv.row>>3;
926                 }else
927                 {
928                     mvp.as_int = best_ref_mv.as_int;
929                     mvp_full.as_mv.col = best_ref_mv.as_mv.col>>3;
930                     mvp_full.as_mv.row = best_ref_mv.as_mv.row>>3;
931                 }
932             }
933 
934 #if CONFIG_MULTI_RES_ENCODING
935             if (parent_ref_valid && parent_ref_frame && dissim <= 2 &&
936                 MAX(abs(best_ref_mv.as_mv.row - parent_ref_mv.as_mv.row),
937                     abs(best_ref_mv.as_mv.col - parent_ref_mv.as_mv.col)) <= 4)
938             {
939                 d->bmi.mv.as_int = mvp_full.as_int;
940                 mode_mv[NEWMV].as_int = mvp_full.as_int;
941 
942                 cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv, &best_ref_mv,
943                                              x->errorperbit,
944                                              &cpi->fn_ptr[BLOCK_16X16],
945                                              cpi->mb.mvcost,
946                                              &distortion2,&sse);
947             }else
948 #endif
949             {
950                 /* Get intersection of UMV window and valid MV window to
951                  * reduce # of checks in diamond search. */
952                 if (x->mv_col_min < col_min )
953                     x->mv_col_min = col_min;
954                 if (x->mv_col_max > col_max )
955                     x->mv_col_max = col_max;
956                 if (x->mv_row_min < row_min )
957                     x->mv_row_min = row_min;
958                 if (x->mv_row_max > row_max )
959                     x->mv_row_max = row_max;
960 
961                 further_steps = (cpi->Speed >= 8)?
962                            0: (cpi->sf.max_step_search_steps - 1 - step_param);
963 
964                 if (cpi->sf.search_method == HEX)
965                 {
966 #if CONFIG_MULTI_RES_ENCODING
967                 /* TODO: In higher-res pick_inter_mode, step_param is used to
968                  * modify hex search range. Here, set step_param to 0 not to
969                  * change the behavior in lowest-resolution encoder.
970                  * Will improve it later.
971                  */
972                  /* Set step_param to 0 to ensure large-range motion search
973                     when encoder drops this frame at lower-resolution.
974                   */
975                 if (!parent_ref_valid)
976                     step_param = 0;
977 #endif
978                     bestsme = vp8_hex_search(x, b, d, &mvp_full, &d->bmi.mv,
979                                           step_param, sadpb,
980                                           &cpi->fn_ptr[BLOCK_16X16],
981                                           x->mvsadcost, x->mvcost, &best_ref_mv);
982                     mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
983                 }
984                 else
985                 {
986                     bestsme = cpi->diamond_search_sad(x, b, d, &mvp_full,
987                                           &d->bmi.mv, step_param, sadpb, &num00,
988                                           &cpi->fn_ptr[BLOCK_16X16],
989                                           x->mvcost, &best_ref_mv);
990                     mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
991 
992                     /* Further step/diamond searches as necessary */
993                     n = num00;
994                     num00 = 0;
995 
996                     while (n < further_steps)
997                     {
998                         n++;
999 
1000                         if (num00)
1001                             num00--;
1002                         else
1003                         {
1004                             thissme =
1005                             cpi->diamond_search_sad(x, b, d, &mvp_full,
1006                                                     &d->bmi.mv,
1007                                                     step_param + n,
1008                                                     sadpb, &num00,
1009                                                     &cpi->fn_ptr[BLOCK_16X16],
1010                                                     x->mvcost, &best_ref_mv);
1011                             if (thissme < bestsme)
1012                             {
1013                                 bestsme = thissme;
1014                                 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
1015                             }
1016                             else
1017                             {
1018                                 d->bmi.mv.as_int = mode_mv[NEWMV].as_int;
1019                             }
1020                         }
1021                     }
1022                 }
1023 
1024                 x->mv_col_min = tmp_col_min;
1025                 x->mv_col_max = tmp_col_max;
1026                 x->mv_row_min = tmp_row_min;
1027                 x->mv_row_max = tmp_row_max;
1028 
1029                 if (bestsme < INT_MAX)
1030                     cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv,
1031                                              &best_ref_mv, x->errorperbit,
1032                                              &cpi->fn_ptr[BLOCK_16X16],
1033                                              cpi->mb.mvcost,
1034                                              &distortion2,&sse);
1035             }
1036 
1037             mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
1038 
1039             /* mv cost; */
1040             rate2 += vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv,
1041                                      cpi->mb.mvcost, 128);
1042         }
1043 
1044         case NEARESTMV:
1045         case NEARMV:
1046 
1047             if (mode_mv[this_mode].as_int == 0)
1048                 continue;
1049 
1050         case ZEROMV:
1051 
1052             /* Trap vectors that reach beyond the UMV borders
1053              * Note that ALL New MV, Nearest MV Near MV and Zero MV code drops
1054              * through to this point because of the lack of break statements
1055              * in the previous two cases.
1056              */
1057             if (((mode_mv[this_mode].as_mv.row >> 3) < x->mv_row_min) ||
1058                 ((mode_mv[this_mode].as_mv.row >> 3) > x->mv_row_max) ||
1059                 ((mode_mv[this_mode].as_mv.col >> 3) < x->mv_col_min) ||
1060                 ((mode_mv[this_mode].as_mv.col >> 3) > x->mv_col_max))
1061                 continue;
1062 
1063             rate2 += vp8_cost_mv_ref(this_mode, mdcounts);
1064             x->e_mbd.mode_info_context->mbmi.mv.as_int =
1065                                                     mode_mv[this_mode].as_int;
1066             this_rd = evaluate_inter_mode(&sse, rate2, &distortion2, cpi, x,
1067                                           rd_adjustment);
1068 
1069             break;
1070         default:
1071             break;
1072         }
1073 
1074 #if CONFIG_TEMPORAL_DENOISING
1075         if (cpi->oxcf.noise_sensitivity)
1076         {
1077 
1078             /* Store for later use by denoiser. */
1079             if (this_mode == ZEROMV && sse < zero_mv_sse )
1080             {
1081                 zero_mv_sse = sse;
1082                 x->best_zeromv_reference_frame =
1083                         x->e_mbd.mode_info_context->mbmi.ref_frame;
1084             }
1085 
1086             /* Store the best NEWMV in x for later use in the denoiser. */
1087             if (x->e_mbd.mode_info_context->mbmi.mode == NEWMV &&
1088                     sse < best_sse)
1089             {
1090                 best_sse = sse;
1091                 x->best_sse_inter_mode = NEWMV;
1092                 x->best_sse_mv = x->e_mbd.mode_info_context->mbmi.mv;
1093                 x->need_to_clamp_best_mvs =
1094                     x->e_mbd.mode_info_context->mbmi.need_to_clamp_mvs;
1095                 x->best_reference_frame =
1096                     x->e_mbd.mode_info_context->mbmi.ref_frame;
1097             }
1098         }
1099 #endif
1100 
1101         if (this_rd < best_rd || x->skip)
1102         {
1103             /* Note index of best mode */
1104             best_mode_index = mode_index;
1105 
1106             *returnrate = rate2;
1107             *returndistortion = distortion2;
1108             best_rd_sse = sse;
1109             best_rd = this_rd;
1110             vpx_memcpy(&best_mbmode, &x->e_mbd.mode_info_context->mbmi,
1111                        sizeof(MB_MODE_INFO));
1112 
1113             /* Testing this mode gave rise to an improvement in best error
1114              * score. Lower threshold a bit for next time
1115              */
1116             x->rd_thresh_mult[mode_index] =
1117                      (x->rd_thresh_mult[mode_index] >= (MIN_THRESHMULT + 2)) ?
1118                      x->rd_thresh_mult[mode_index] - 2 : MIN_THRESHMULT;
1119             x->rd_threshes[mode_index] =
1120                                    (cpi->rd_baseline_thresh[mode_index] >> 7) *
1121                                    x->rd_thresh_mult[mode_index];
1122         }
1123 
1124         /* If the mode did not help improve the best error case then raise the
1125          * threshold for testing that mode next time around.
1126          */
1127         else
1128         {
1129             x->rd_thresh_mult[mode_index] += 4;
1130 
1131             if (x->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
1132                 x->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
1133 
1134             x->rd_threshes[mode_index] =
1135                          (cpi->rd_baseline_thresh[mode_index] >> 7) *
1136                          x->rd_thresh_mult[mode_index];
1137         }
1138 
1139         if (x->skip)
1140             break;
1141     }
1142 
1143     /* Reduce the activation RD thresholds for the best choice mode */
1144     if ((cpi->rd_baseline_thresh[best_mode_index] > 0) && (cpi->rd_baseline_thresh[best_mode_index] < (INT_MAX >> 2)))
1145     {
1146         int best_adjustment = (x->rd_thresh_mult[best_mode_index] >> 3);
1147 
1148         x->rd_thresh_mult[best_mode_index] =
1149                         (x->rd_thresh_mult[best_mode_index]
1150                         >= (MIN_THRESHMULT + best_adjustment)) ?
1151                         x->rd_thresh_mult[best_mode_index] - best_adjustment :
1152                         MIN_THRESHMULT;
1153         x->rd_threshes[best_mode_index] =
1154                         (cpi->rd_baseline_thresh[best_mode_index] >> 7) *
1155                         x->rd_thresh_mult[best_mode_index];
1156     }
1157 
1158 
1159     {
1160         int this_rdbin = (*returndistortion >> 7);
1161 
1162         if (this_rdbin >= 1024)
1163         {
1164             this_rdbin = 1023;
1165         }
1166 
1167         x->error_bins[this_rdbin] ++;
1168     }
1169 
1170 #if CONFIG_TEMPORAL_DENOISING
1171     if (cpi->oxcf.noise_sensitivity)
1172     {
1173         if (x->best_sse_inter_mode == DC_PRED)
1174         {
1175             /* No best MV found. */
1176             x->best_sse_inter_mode = best_mbmode.mode;
1177             x->best_sse_mv = best_mbmode.mv;
1178             x->need_to_clamp_best_mvs = best_mbmode.need_to_clamp_mvs;
1179             x->best_reference_frame = best_mbmode.ref_frame;
1180             best_sse = best_rd_sse;
1181         }
1182         vp8_denoiser_denoise_mb(&cpi->denoiser, x, best_sse, zero_mv_sse,
1183                                 recon_yoffset, recon_uvoffset);
1184 
1185 
1186         /* Reevaluate ZEROMV after denoising. */
1187         if (best_mbmode.ref_frame == INTRA_FRAME &&
1188             x->best_zeromv_reference_frame != INTRA_FRAME)
1189         {
1190             int this_rd = 0;
1191             int this_ref_frame = x->best_zeromv_reference_frame;
1192             rate2 = x->ref_frame_cost[this_ref_frame] +
1193                     vp8_cost_mv_ref(ZEROMV, mdcounts);
1194             distortion2 = 0;
1195 
1196             /* set up the proper prediction buffers for the frame */
1197             x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame;
1198             x->e_mbd.pre.y_buffer = plane[this_ref_frame][0];
1199             x->e_mbd.pre.u_buffer = plane[this_ref_frame][1];
1200             x->e_mbd.pre.v_buffer = plane[this_ref_frame][2];
1201 
1202             x->e_mbd.mode_info_context->mbmi.mode = ZEROMV;
1203             x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
1204             x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
1205             this_rd = evaluate_inter_mode(&sse, rate2, &distortion2, cpi, x,
1206                                           rd_adjustment);
1207 
1208             if (this_rd < best_rd)
1209             {
1210                 vpx_memcpy(&best_mbmode, &x->e_mbd.mode_info_context->mbmi,
1211                            sizeof(MB_MODE_INFO));
1212             }
1213         }
1214 
1215     }
1216 #endif
1217 
1218     if (cpi->is_src_frame_alt_ref &&
1219         (best_mbmode.mode != ZEROMV || best_mbmode.ref_frame != ALTREF_FRAME))
1220     {
1221         x->e_mbd.mode_info_context->mbmi.mode = ZEROMV;
1222         x->e_mbd.mode_info_context->mbmi.ref_frame = ALTREF_FRAME;
1223         x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
1224         x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
1225         x->e_mbd.mode_info_context->mbmi.mb_skip_coeff =
1226                                         (cpi->common.mb_no_coeff_skip);
1227         x->e_mbd.mode_info_context->mbmi.partitioning = 0;
1228 
1229         return;
1230     }
1231 
1232     /* set to the best mb mode, this copy can be skip if x->skip since it
1233      * already has the right content */
1234     if (!x->skip)
1235         vpx_memcpy(&x->e_mbd.mode_info_context->mbmi, &best_mbmode,
1236                    sizeof(MB_MODE_INFO));
1237 
1238     if (best_mbmode.mode <= B_PRED)
1239     {
1240         /* set mode_info_context->mbmi.uv_mode */
1241         pick_intra_mbuv_mode(x);
1242     }
1243 
1244     if (sign_bias
1245       != cpi->common.ref_frame_sign_bias[xd->mode_info_context->mbmi.ref_frame])
1246         best_ref_mv.as_int = best_ref_mv_sb[!sign_bias].as_int;
1247 
1248     update_mvcount(x, &best_ref_mv);
1249 }
1250 
1251 
vp8_pick_intra_mode(MACROBLOCK * x,int * rate_)1252 void vp8_pick_intra_mode(MACROBLOCK *x, int *rate_)
1253 {
1254     int error4x4, error16x16 = INT_MAX;
1255     int rate, best_rate = 0, distortion, best_sse = 0;
1256     MB_PREDICTION_MODE mode, best_mode = DC_PRED;
1257     int this_rd;
1258     unsigned int sse;
1259     BLOCK *b = &x->block[0];
1260     MACROBLOCKD *xd = &x->e_mbd;
1261 
1262     xd->mode_info_context->mbmi.ref_frame = INTRA_FRAME;
1263 
1264     pick_intra_mbuv_mode(x);
1265 
1266     for (mode = DC_PRED; mode <= TM_PRED; mode ++)
1267     {
1268         xd->mode_info_context->mbmi.mode = mode;
1269         vp8_build_intra_predictors_mby_s(xd,
1270                                          xd->dst.y_buffer - xd->dst.y_stride,
1271                                          xd->dst.y_buffer - 1,
1272                                          xd->dst.y_stride,
1273                                          xd->predictor,
1274                                          16);
1275         distortion = vp8_variance16x16
1276             (*(b->base_src), b->src_stride, xd->predictor, 16, &sse);
1277         rate = x->mbmode_cost[xd->frame_type][mode];
1278         this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
1279 
1280         if (error16x16 > this_rd)
1281         {
1282             error16x16 = this_rd;
1283             best_mode = mode;
1284             best_sse = sse;
1285             best_rate = rate;
1286         }
1287     }
1288     xd->mode_info_context->mbmi.mode = best_mode;
1289 
1290     error4x4 = pick_intra4x4mby_modes(x, &rate,
1291                                       &best_sse);
1292     if (error4x4 < error16x16)
1293     {
1294         xd->mode_info_context->mbmi.mode = B_PRED;
1295         best_rate = rate;
1296     }
1297 
1298     *rate_ = best_rate;
1299 }
1300