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