Merge "KF/GF Pulsing"
[profile/ivi/libvpx.git] / 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_ports/config.h"
14 #include "onyx_int.h"
15 #include "modecosts.h"
16 #include "encodeintra.h"
17 #include "entropymode.h"
18 #include "pickinter.h"
19 #include "findnearmv.h"
20 #include "encodemb.h"
21 #include "reconinter.h"
22 #include "reconintra.h"
23 #include "reconintra4x4.h"
24 #include "g_common.h"
25 #include "variance.h"
26 #include "mcomp.h"
27
28 #include "vpx_mem/vpx_mem.h"
29
30 #if CONFIG_RUNTIME_CPU_DETECT
31 #define IF_RTCD(x) (x)
32 #else
33 #define IF_RTCD(x)  NULL
34 #endif
35
36 extern int VP8_UVSSE(MACROBLOCK *x, const vp8_variance_rtcd_vtable_t *rtcd);
37
38 #ifdef SPEEDSTATS
39 extern unsigned int cnt_pm;
40 #endif
41
42 extern const MV_REFERENCE_FRAME vp8_ref_frame_order[MAX_MODES];
43 extern const MB_PREDICTION_MODE vp8_mode_order[MAX_MODES];
44
45
46 extern unsigned int (*vp8_get16x16pred_error)(unsigned char *src_ptr, int src_stride, unsigned char *ref_ptr, int ref_stride);
47 extern unsigned int (*vp8_get4x4sse_cs)(unsigned char *src_ptr, int  source_stride, unsigned char *ref_ptr, int  recon_stride);
48 extern int vp8_rd_pick_best_mbsegmentation(VP8_COMP *cpi, MACROBLOCK *x, MV *best_ref_mv, int best_rd, int *, int *, int *, int, int *mvcost[2], int, int fullpixel);
49 extern int vp8_cost_mv_ref(MB_PREDICTION_MODE m, const int near_mv_ref_ct[4]);
50 extern void vp8_set_mbmode_and_mvs(MACROBLOCK *x, MB_PREDICTION_MODE mb, MV *mv);
51
52
53 int vp8_skip_fractional_mv_step(MACROBLOCK *mb, BLOCK *b, BLOCKD *d, MV *bestmv, MV *ref_mv, int error_per_bit, const vp8_variance_fn_ptr_t *vfp, int *mvcost[2])
54 {
55     (void) b;
56     (void) d;
57     (void) ref_mv;
58     (void) error_per_bit;
59     (void) vfp;
60     (void) mvcost;
61     bestmv->row <<= 3;
62     bestmv->col <<= 3;
63     return 0;
64 }
65
66
67 static int get_inter_mbpred_error(MACROBLOCK *mb, const vp8_variance_fn_ptr_t *vfp, unsigned int *sse)
68 {
69
70     BLOCK *b = &mb->block[0];
71     BLOCKD *d = &mb->e_mbd.block[0];
72     unsigned char *what = (*(b->base_src) + b->src);
73     int what_stride = b->src_stride;
74     unsigned char *in_what = *(d->base_pre) + d->pre ;
75     int in_what_stride = d->pre_stride;
76     int xoffset = d->bmi.mv.as_mv.col & 7;
77     int yoffset = d->bmi.mv.as_mv.row & 7;
78
79     in_what += (d->bmi.mv.as_mv.row >> 3) * d->pre_stride + (d->bmi.mv.as_mv.col >> 3);
80
81     if (xoffset | yoffset)
82     {
83         return vfp->svf(in_what, in_what_stride, xoffset, yoffset, what, what_stride, sse);
84     }
85     else
86     {
87         return vfp->vf(what, what_stride, in_what, in_what_stride, sse);
88     }
89
90 }
91
92 unsigned int vp8_get16x16pred_error_c
93 (
94     const unsigned char *src_ptr,
95     int src_stride,
96     const unsigned char *ref_ptr,
97     int ref_stride,
98     int max_sad
99 )
100 {
101     unsigned pred_error = 0;
102     int i, j;
103     int sum = 0;
104
105     for (i = 0; i < 16; i++)
106     {
107         int diff;
108
109         for (j = 0; j < 16; j++)
110         {
111             diff = src_ptr[j] - ref_ptr[j];
112             sum += diff;
113             pred_error += diff * diff;
114         }
115
116         src_ptr += src_stride;
117         ref_ptr += ref_stride;
118     }
119
120     pred_error -= sum * sum / 256;
121     return pred_error;
122 }
123
124
125 unsigned int vp8_get4x4sse_cs_c
126 (
127     const unsigned char *src_ptr,
128     int  source_stride,
129     const unsigned char *ref_ptr,
130     int  recon_stride,
131     int max_sad
132 )
133 {
134     int distortion = 0;
135     int r, c;
136
137     for (r = 0; r < 4; r++)
138     {
139         for (c = 0; c < 4; c++)
140         {
141             int diff = src_ptr[c] - ref_ptr[c];
142             distortion += diff * diff;
143         }
144
145         src_ptr += source_stride;
146         ref_ptr += recon_stride;
147     }
148
149     return distortion;
150 }
151
152 static int get_prediction_error(BLOCK *be, BLOCKD *b, const vp8_variance_rtcd_vtable_t *rtcd)
153 {
154     unsigned char *sptr;
155     unsigned char *dptr;
156     sptr = (*(be->base_src) + be->src);
157     dptr = b->predictor;
158
159     return VARIANCE_INVOKE(rtcd, get4x4sse_cs)(sptr, be->src_stride, dptr, 16, 0x7fffffff);
160
161 }
162
163 static int pick_intra4x4block(
164     const VP8_ENCODER_RTCD *rtcd,
165     MACROBLOCK *x,
166     BLOCK *be,
167     BLOCKD *b,
168     B_PREDICTION_MODE *best_mode,
169     B_PREDICTION_MODE above,
170     B_PREDICTION_MODE left,
171     ENTROPY_CONTEXT *a,
172     ENTROPY_CONTEXT *l,
173
174     int *bestrate,
175     int *bestdistortion)
176 {
177     B_PREDICTION_MODE mode;
178     int best_rd = INT_MAX;       // 1<<30
179     int rate;
180     int distortion;
181     unsigned int *mode_costs;
182     (void) l;
183     (void) a;
184
185     if (x->e_mbd.frame_type == KEY_FRAME)
186     {
187         mode_costs = x->bmode_costs[above][left];
188     }
189     else
190     {
191         mode_costs = x->inter_bmode_costs;
192     }
193
194     for (mode = B_DC_PRED; mode <= B_HE_PRED /*B_HU_PRED*/; mode++)
195     {
196         int this_rd;
197
198         rate = mode_costs[mode];
199         vp8_predict_intra4x4(b, mode, b->predictor);
200         distortion = get_prediction_error(be, b, &rtcd->variance);
201         this_rd = RD_ESTIMATE(x->rdmult, x->rddiv, rate, distortion);
202
203         if (this_rd < best_rd)
204         {
205             *bestrate = rate;
206             *bestdistortion = distortion;
207             best_rd = this_rd;
208             *best_mode = mode;
209         }
210     }
211
212     b->bmi.mode = (B_PREDICTION_MODE)(*best_mode);
213     vp8_encode_intra4x4block(rtcd, x, be, b, b->bmi.mode);
214     return best_rd;
215 }
216
217
218 int vp8_pick_intra4x4mby_modes(const VP8_ENCODER_RTCD *rtcd, MACROBLOCK *mb, int *Rate, int *best_dist)
219 {
220     MACROBLOCKD *const xd = &mb->e_mbd;
221     int i;
222     int cost = mb->mbmode_cost [xd->frame_type] [B_PRED];
223     int error = RD_ESTIMATE(mb->rdmult, mb->rddiv, cost, 0); // Rd estimate for the cost of the block prediction mode
224     int distortion = 0;
225     ENTROPY_CONTEXT_PLANES t_above, t_left;
226     ENTROPY_CONTEXT *ta;
227     ENTROPY_CONTEXT *tl;
228
229     vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
230     vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
231
232     ta = (ENTROPY_CONTEXT *)&t_above;
233     tl = (ENTROPY_CONTEXT *)&t_left;
234
235     vp8_intra_prediction_down_copy(xd);
236
237     for (i = 0; i < 16; i++)
238     {
239         MODE_INFO *const mic = xd->mode_info_context;
240         const int mis = xd->mode_info_stride;
241         const B_PREDICTION_MODE A = vp8_above_bmi(mic, i, mis)->mode;
242         const B_PREDICTION_MODE L = vp8_left_bmi(mic, i)->mode;
243         B_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
244         int UNINITIALIZED_IS_SAFE(r), UNINITIALIZED_IS_SAFE(d);
245
246         error += pick_intra4x4block(rtcd,
247                                     mb, mb->block + i, xd->block + i, &best_mode, A, L,
248                                     ta + vp8_block2above[i],
249                                     tl + vp8_block2left[i], &r, &d);
250
251         cost += r;
252         distortion += d;
253
254         mic->bmi[i].mode = xd->block[i].bmi.mode = best_mode;
255
256         // Break out case where we have already exceeded best so far value that was bassed in
257         if (distortion > *best_dist)
258             break;
259     }
260
261     for (i = 0; i < 16; i++)
262         xd->block[i].bmi.mv.as_int = 0;
263
264     *Rate = cost;
265
266     if (i == 16)
267         *best_dist = distortion;
268     else
269         *best_dist = INT_MAX;
270
271
272     return error;
273 }
274
275 int vp8_pick_intra_mbuv_mode(MACROBLOCK *mb)
276 {
277
278     MACROBLOCKD *x = &mb->e_mbd;
279     unsigned char *uabove_row = x->dst.u_buffer - x->dst.uv_stride;
280     unsigned char *vabove_row = x->dst.v_buffer - x->dst.uv_stride;
281     unsigned char *usrc_ptr = (mb->block[16].src + *mb->block[16].base_src);
282     unsigned char *vsrc_ptr = (mb->block[20].src + *mb->block[20].base_src);
283     int uvsrc_stride = mb->block[16].src_stride;
284     unsigned char uleft_col[8];
285     unsigned char vleft_col[8];
286     unsigned char utop_left = uabove_row[-1];
287     unsigned char vtop_left = vabove_row[-1];
288     int i, j;
289     int expected_udc;
290     int expected_vdc;
291     int shift;
292     int Uaverage = 0;
293     int Vaverage = 0;
294     int diff;
295     int pred_error[4] = {0, 0, 0, 0}, best_error = INT_MAX;
296     MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
297
298
299     for (i = 0; i < 8; i++)
300     {
301         uleft_col[i] = x->dst.u_buffer [i* x->dst.uv_stride -1];
302         vleft_col[i] = x->dst.v_buffer [i* x->dst.uv_stride -1];
303     }
304
305     if (!x->up_available && !x->left_available)
306     {
307         expected_udc = 128;
308         expected_vdc = 128;
309     }
310     else
311     {
312         shift = 2;
313
314         if (x->up_available)
315         {
316
317             for (i = 0; i < 8; i++)
318             {
319                 Uaverage += uabove_row[i];
320                 Vaverage += vabove_row[i];
321             }
322
323             shift ++;
324
325         }
326
327         if (x->left_available)
328         {
329             for (i = 0; i < 8; i++)
330             {
331                 Uaverage += uleft_col[i];
332                 Vaverage += vleft_col[i];
333             }
334
335             shift ++;
336
337         }
338
339         expected_udc = (Uaverage + (1 << (shift - 1))) >> shift;
340         expected_vdc = (Vaverage + (1 << (shift - 1))) >> shift;
341     }
342
343
344     for (i = 0; i < 8; i++)
345     {
346         for (j = 0; j < 8; j++)
347         {
348
349             int predu = uleft_col[i] + uabove_row[j] - utop_left;
350             int predv = vleft_col[i] + vabove_row[j] - vtop_left;
351             int u_p, v_p;
352
353             u_p = usrc_ptr[j];
354             v_p = vsrc_ptr[j];
355
356             if (predu < 0)
357                 predu = 0;
358
359             if (predu > 255)
360                 predu = 255;
361
362             if (predv < 0)
363                 predv = 0;
364
365             if (predv > 255)
366                 predv = 255;
367
368
369             diff = u_p - expected_udc;
370             pred_error[DC_PRED] += diff * diff;
371             diff = v_p - expected_vdc;
372             pred_error[DC_PRED] += diff * diff;
373
374
375             diff = u_p - uabove_row[j];
376             pred_error[V_PRED] += diff * diff;
377             diff = v_p - vabove_row[j];
378             pred_error[V_PRED] += diff * diff;
379
380
381             diff = u_p - uleft_col[i];
382             pred_error[H_PRED] += diff * diff;
383             diff = v_p - vleft_col[i];
384             pred_error[H_PRED] += diff * diff;
385
386
387             diff = u_p - predu;
388             pred_error[TM_PRED] += diff * diff;
389             diff = v_p - predv;
390             pred_error[TM_PRED] += diff * diff;
391
392
393         }
394
395         usrc_ptr += uvsrc_stride;
396         vsrc_ptr += uvsrc_stride;
397
398         if (i == 3)
399         {
400             usrc_ptr = (mb->block[18].src + *mb->block[18].base_src);
401             vsrc_ptr = (mb->block[22].src + *mb->block[22].base_src);
402         }
403
404
405
406     }
407
408
409     for (i = DC_PRED; i <= TM_PRED; i++)
410     {
411         if (best_error > pred_error[i])
412         {
413             best_error = pred_error[i];
414             best_mode = (MB_PREDICTION_MODE)i;
415         }
416     }
417
418
419     mb->e_mbd.mode_info_context->mbmi.uv_mode = best_mode;
420     return best_error;
421
422 }
423
424
425 int vp8_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset, int recon_uvoffset, int *returnrate, int *returndistortion, int *returnintra)
426 {
427     BLOCK *b = &x->block[0];
428     BLOCKD *d = &x->e_mbd.block[0];
429     MACROBLOCKD *xd = &x->e_mbd;
430     B_MODE_INFO best_bmodes[16];
431     MB_MODE_INFO best_mbmode;
432     PARTITION_INFO best_partition;
433     MV best_ref_mv1;
434     MV mode_mv[MB_MODE_COUNT];
435     MB_PREDICTION_MODE this_mode;
436     int num00;
437     int i;
438     int mdcounts[4];
439     int best_rd = INT_MAX; // 1 << 30;
440     int best_intra_rd = INT_MAX;
441     int mode_index;
442     int ref_frame_cost[MAX_REF_FRAMES];
443     int rate;
444     int rate2;
445     int distortion2;
446     int bestsme;
447     //int all_rds[MAX_MODES];         // Experimental debug code.
448     int best_mode_index = 0;
449     int sse = INT_MAX;
450
451     MV nearest_mv[4];
452     MV near_mv[4];
453     MV best_ref_mv[4];
454     int MDCounts[4][4];
455     unsigned char *y_buffer[4];
456     unsigned char *u_buffer[4];
457     unsigned char *v_buffer[4];
458
459     int skip_mode[4] = {0, 0, 0, 0};
460
461     vpx_memset(mode_mv, 0, sizeof(mode_mv));
462     vpx_memset(nearest_mv, 0, sizeof(nearest_mv));
463     vpx_memset(near_mv, 0, sizeof(near_mv));
464     vpx_memset(&best_mbmode, 0, sizeof(best_mbmode));
465
466
467     // set up all the refframe dependent pointers.
468     if (cpi->ref_frame_flags & VP8_LAST_FLAG)
469     {
470         YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
471
472         vp8_find_near_mvs(&x->e_mbd, x->e_mbd.mode_info_context, &nearest_mv[LAST_FRAME], &near_mv[LAST_FRAME],
473                           &best_ref_mv[LAST_FRAME], MDCounts[LAST_FRAME], LAST_FRAME, cpi->common.ref_frame_sign_bias);
474
475         y_buffer[LAST_FRAME] = lst_yv12->y_buffer + recon_yoffset;
476         u_buffer[LAST_FRAME] = lst_yv12->u_buffer + recon_uvoffset;
477         v_buffer[LAST_FRAME] = lst_yv12->v_buffer + recon_uvoffset;
478     }
479     else
480         skip_mode[LAST_FRAME] = 1;
481
482     if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
483     {
484         YV12_BUFFER_CONFIG *gld_yv12 = &cpi->common.yv12_fb[cpi->common.gld_fb_idx];
485
486         vp8_find_near_mvs(&x->e_mbd, x->e_mbd.mode_info_context, &nearest_mv[GOLDEN_FRAME], &near_mv[GOLDEN_FRAME],
487                           &best_ref_mv[GOLDEN_FRAME], MDCounts[GOLDEN_FRAME], GOLDEN_FRAME, cpi->common.ref_frame_sign_bias);
488
489         y_buffer[GOLDEN_FRAME] = gld_yv12->y_buffer + recon_yoffset;
490         u_buffer[GOLDEN_FRAME] = gld_yv12->u_buffer + recon_uvoffset;
491         v_buffer[GOLDEN_FRAME] = gld_yv12->v_buffer + recon_uvoffset;
492     }
493     else
494         skip_mode[GOLDEN_FRAME] = 1;
495
496     if (cpi->ref_frame_flags & VP8_ALT_FLAG && cpi->source_alt_ref_active)
497     {
498         YV12_BUFFER_CONFIG *alt_yv12 = &cpi->common.yv12_fb[cpi->common.alt_fb_idx];
499
500         vp8_find_near_mvs(&x->e_mbd, x->e_mbd.mode_info_context, &nearest_mv[ALTREF_FRAME], &near_mv[ALTREF_FRAME],
501                           &best_ref_mv[ALTREF_FRAME], MDCounts[ALTREF_FRAME], ALTREF_FRAME, cpi->common.ref_frame_sign_bias);
502
503         y_buffer[ALTREF_FRAME] = alt_yv12->y_buffer + recon_yoffset;
504         u_buffer[ALTREF_FRAME] = alt_yv12->u_buffer + recon_uvoffset;
505         v_buffer[ALTREF_FRAME] = alt_yv12->v_buffer + recon_uvoffset;
506     }
507     else
508         skip_mode[ALTREF_FRAME] = 1;
509
510     cpi->mbs_tested_so_far++;          // Count of the number of MBs tested so far this frame
511
512     *returnintra = best_intra_rd;
513     x->skip = 0;
514
515     ref_frame_cost[INTRA_FRAME]   = vp8_cost_zero(cpi->prob_intra_coded);
516
517     // Special case treatment when GF and ARF are not sensible options for reference
518     if (cpi->ref_frame_flags == VP8_LAST_FLAG)
519     {
520         ref_frame_cost[LAST_FRAME]    = vp8_cost_one(cpi->prob_intra_coded)
521                                         + vp8_cost_zero(255);
522         ref_frame_cost[GOLDEN_FRAME]  = vp8_cost_one(cpi->prob_intra_coded)
523                                         + vp8_cost_one(255)
524                                         + vp8_cost_zero(128);
525         ref_frame_cost[ALTREF_FRAME]  = vp8_cost_one(cpi->prob_intra_coded)
526                                         + vp8_cost_one(255)
527                                         + vp8_cost_one(128);
528     }
529     else
530     {
531         ref_frame_cost[LAST_FRAME]    = vp8_cost_one(cpi->prob_intra_coded)
532                                         + vp8_cost_zero(cpi->prob_last_coded);
533         ref_frame_cost[GOLDEN_FRAME]  = vp8_cost_one(cpi->prob_intra_coded)
534                                         + vp8_cost_one(cpi->prob_last_coded)
535                                         + vp8_cost_zero(cpi->prob_gf_coded);
536         ref_frame_cost[ALTREF_FRAME]  = vp8_cost_one(cpi->prob_intra_coded)
537                                         + vp8_cost_one(cpi->prob_last_coded)
538                                         + vp8_cost_one(cpi->prob_gf_coded);
539     }
540
541
542
543     best_rd = INT_MAX;
544
545     x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
546
547     // if we encode a new mv this is important
548     // find the best new motion vector
549     for (mode_index = 0; mode_index < MAX_MODES; mode_index++)
550     {
551         int frame_cost;
552         int this_rd = INT_MAX;
553
554         if (best_rd <= cpi->rd_threshes[mode_index])
555             continue;
556
557         x->e_mbd.mode_info_context->mbmi.ref_frame = vp8_ref_frame_order[mode_index];
558
559         if (skip_mode[x->e_mbd.mode_info_context->mbmi.ref_frame])
560             continue;
561
562         // Check to see if the testing frequency for this mode is at its max
563         // If so then prevent it from being tested and increase the threshold for its testing
564         if (cpi->mode_test_hit_counts[mode_index] && (cpi->mode_check_freq[mode_index] > 1))
565         {
566             //if ( (cpi->mbs_tested_so_far / cpi->mode_test_hit_counts[mode_index]) <= cpi->mode_check_freq[mode_index] )
567             if (cpi->mbs_tested_so_far <= (cpi->mode_check_freq[mode_index] * cpi->mode_test_hit_counts[mode_index]))
568             {
569                 // Increase the threshold for coding this mode to make it less likely to be chosen
570                 cpi->rd_thresh_mult[mode_index] += 4;
571
572                 if (cpi->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
573                     cpi->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
574
575                 cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
576
577                 continue;
578             }
579         }
580
581         // We have now reached the point where we are going to test the current mode so increment the counter for the number of times it has been tested
582         cpi->mode_test_hit_counts[mode_index] ++;
583
584         rate2 = 0;
585         distortion2 = 0;
586
587         this_mode = vp8_mode_order[mode_index];
588
589         // Experimental debug code.
590         //all_rds[mode_index] = -1;
591
592         x->e_mbd.mode_info_context->mbmi.mode = this_mode;
593         x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
594
595         // Work out the cost assosciated with selecting the reference frame
596         frame_cost = ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame];
597         rate2 += frame_cost;
598
599         // everything but intra
600         if (x->e_mbd.mode_info_context->mbmi.ref_frame)
601         {
602             x->e_mbd.pre.y_buffer = y_buffer[x->e_mbd.mode_info_context->mbmi.ref_frame];
603             x->e_mbd.pre.u_buffer = u_buffer[x->e_mbd.mode_info_context->mbmi.ref_frame];
604             x->e_mbd.pre.v_buffer = v_buffer[x->e_mbd.mode_info_context->mbmi.ref_frame];
605             mode_mv[NEARESTMV] = nearest_mv[x->e_mbd.mode_info_context->mbmi.ref_frame];
606             mode_mv[NEARMV] = near_mv[x->e_mbd.mode_info_context->mbmi.ref_frame];
607             best_ref_mv1 = best_ref_mv[x->e_mbd.mode_info_context->mbmi.ref_frame];
608             memcpy(mdcounts, MDCounts[x->e_mbd.mode_info_context->mbmi.ref_frame], sizeof(mdcounts));
609         }
610
611         // Only consider ZEROMV/ALTREF_FRAME for alt ref frame,
612         // unless ARNR filtering is enabled in which case we want
613         // an unfiltered alternative
614         if (cpi->is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0))
615         {
616             if (this_mode != ZEROMV || x->e_mbd.mode_info_context->mbmi.ref_frame != ALTREF_FRAME)
617                 continue;
618         }
619
620         switch (this_mode)
621         {
622         case B_PRED:
623             distortion2 = *returndistortion;                    // Best so far passed in as breakout value to vp8_pick_intra4x4mby_modes
624             vp8_pick_intra4x4mby_modes(IF_RTCD(&cpi->rtcd), x, &rate, &distortion2);
625             rate2 += rate;
626             distortion2 = VARIANCE_INVOKE(&cpi->rtcd.variance, get16x16prederror)(x->src.y_buffer, x->src.y_stride, x->e_mbd.predictor, 16, 0x7fffffff);
627
628             if (distortion2 == INT_MAX)
629             {
630                 this_rd = INT_MAX;
631             }
632             else
633             {
634                 this_rd = RD_ESTIMATE(x->rdmult, x->rddiv, rate2, distortion2);
635
636                 if (this_rd < best_intra_rd)
637                 {
638                     best_intra_rd = this_rd;
639                     *returnintra = best_intra_rd ;
640                 }
641             }
642
643             break;
644
645         case SPLITMV:
646
647             // Split MV modes currently not supported when RD is nopt enabled.
648             break;
649
650         case DC_PRED:
651         case V_PRED:
652         case H_PRED:
653         case TM_PRED:
654             vp8_build_intra_predictors_mby_ptr(&x->e_mbd);
655             distortion2 = VARIANCE_INVOKE(&cpi->rtcd.variance, get16x16prederror)(x->src.y_buffer, x->src.y_stride, x->e_mbd.predictor, 16, 0x7fffffff);
656             rate2 += x->mbmode_cost[x->e_mbd.frame_type][x->e_mbd.mode_info_context->mbmi.mode];
657             this_rd = RD_ESTIMATE(x->rdmult, x->rddiv, rate2, distortion2);
658
659             if (this_rd < best_intra_rd)
660             {
661                 best_intra_rd = this_rd;
662                 *returnintra = best_intra_rd ;
663             }
664
665             break;
666
667         case NEWMV:
668         {
669             int thissme;
670             int step_param;
671             int further_steps;
672             int n = 0;
673             int sadpb = x->sadperbit16;
674
675             // Further step/diamond searches as necessary
676             if (cpi->Speed < 8)
677             {
678                 step_param = cpi->sf.first_step + ((cpi->Speed > 5) ? 1 : 0);
679                 further_steps = (cpi->sf.max_step_search_steps - 1) - step_param;
680             }
681             else
682             {
683                 step_param = cpi->sf.first_step + 2;
684                 further_steps = 0;
685             }
686
687 #if 0
688
689             // Initial step Search
690             bestsme = vp8_diamond_search_sad(x, b, d, &best_ref_mv1, &d->bmi.mv.as_mv, step_param, x->errorperbit, &num00, &cpi->fn_ptr, cpi->mb.mvsadcost, cpi->mb.mvcost, &best_ref_mv1);
691             mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
692             mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
693
694             // Further step searches
695             while (n < further_steps)
696             {
697                 n++;
698
699                 if (num00)
700                     num00--;
701                 else
702                 {
703                     thissme = vp8_diamond_search_sad(x, b, d, &best_ref_mv1, &d->bmi.mv.as_mv, step_param + n, x->errorperbit, &num00, &cpi->fn_ptr, cpi->mb.mvsadcost, x->mvcost, &best_ref_mv1);
704
705                     if (thissme < bestsme)
706                     {
707                         bestsme = thissme;
708                         mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
709                         mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
710                     }
711                     else
712                     {
713                         d->bmi.mv.as_mv.row = mode_mv[NEWMV].row;
714                         d->bmi.mv.as_mv.col = mode_mv[NEWMV].col;
715                     }
716                 }
717             }
718
719 #else
720
721             if (cpi->sf.search_method == HEX)
722             {
723                 bestsme = vp8_hex_search(x, b, d, &best_ref_mv1, &d->bmi.mv.as_mv, step_param, sadpb/*x->errorperbit*/, &num00, &cpi->fn_ptr[BLOCK_16X16], x->mvsadcost, x->mvcost);
724                 mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
725                 mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
726             }
727             else
728             {
729                 bestsme = cpi->diamond_search_sad(x, b, d, &best_ref_mv1, &d->bmi.mv.as_mv, step_param, sadpb / 2/*x->errorperbit*/, &num00, &cpi->fn_ptr[BLOCK_16X16], x->mvsadcost, x->mvcost, &best_ref_mv1); //sadpb < 9
730                 mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
731                 mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
732
733                 // Further step/diamond searches as necessary
734                 n = 0;
735                 //further_steps = (cpi->sf.max_step_search_steps - 1) - step_param;
736
737                 n = num00;
738                 num00 = 0;
739
740                 while (n < further_steps)
741                 {
742                     n++;
743
744                     if (num00)
745                         num00--;
746                     else
747                     {
748                         thissme = cpi->diamond_search_sad(x, b, d, &best_ref_mv1, &d->bmi.mv.as_mv, step_param + n, sadpb / 4/*x->errorperbit*/, &num00, &cpi->fn_ptr[BLOCK_16X16], x->mvsadcost, x->mvcost, &best_ref_mv1); //sadpb = 9
749
750                         if (thissme < bestsme)
751                         {
752                             bestsme = thissme;
753                             mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
754                             mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
755                         }
756                         else
757                         {
758                             d->bmi.mv.as_mv.row = mode_mv[NEWMV].row;
759                             d->bmi.mv.as_mv.col = mode_mv[NEWMV].col;
760                         }
761                     }
762                 }
763             }
764
765 #endif
766         }
767
768         if (bestsme < INT_MAX)
769             cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv.as_mv, &best_ref_mv1, x->errorperbit, &cpi->fn_ptr[BLOCK_16X16], cpi->mb.mvcost);
770
771         mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
772         mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
773
774         // mv cost;
775         rate2 += vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv1, cpi->mb.mvcost, 128);
776
777
778         case NEARESTMV:
779         case NEARMV:
780
781             if (mode_mv[this_mode].row == 0 && mode_mv[this_mode].col == 0)
782                 continue;
783
784         case ZEROMV:
785
786             // Trap vectors that reach beyond the UMV borders
787             // Note that ALL New MV, Nearest MV Near MV and Zero MV code drops through to this point
788             // because of the lack of break statements in the previous two cases.
789             if (((mode_mv[this_mode].row >> 3) < x->mv_row_min) || ((mode_mv[this_mode].row >> 3) > x->mv_row_max) ||
790                 ((mode_mv[this_mode].col >> 3) < x->mv_col_min) || ((mode_mv[this_mode].col >> 3) > x->mv_col_max))
791                 continue;
792
793             rate2 += vp8_cost_mv_ref(this_mode, mdcounts);
794             x->e_mbd.mode_info_context->mbmi.mode = this_mode;
795             x->e_mbd.mode_info_context->mbmi.mv.as_mv = mode_mv[this_mode];
796             x->e_mbd.block[0].bmi.mode = this_mode;
797             x->e_mbd.block[0].bmi.mv.as_int = x->e_mbd.mode_info_context->mbmi.mv.as_int;
798
799             distortion2 = get_inter_mbpred_error(x, &cpi->fn_ptr[BLOCK_16X16], (unsigned int *)(&sse));
800
801             this_rd = RD_ESTIMATE(x->rdmult, x->rddiv, rate2, distortion2);
802
803             if (cpi->active_map_enabled && x->active_ptr[0] == 0)
804             {
805                 x->skip = 1;
806             }
807             else if (sse < x->encode_breakout)
808             {
809                 // Check u and v to make sure skip is ok
810                 int sse2 = 0;
811
812                 sse2 = VP8_UVSSE(x, IF_RTCD(&cpi->rtcd.variance));
813
814                 if (sse2 * 2 < x->encode_breakout)
815                     x->skip = 1;
816                 else
817                     x->skip = 0;
818             }
819
820             break;
821         default:
822             break;
823         }
824
825         // Experimental debug code.
826         //all_rds[mode_index] = this_rd;
827
828         if (this_rd < best_rd || x->skip)
829         {
830             // Note index of best mode
831             best_mode_index = mode_index;
832
833             *returnrate = rate2;
834             *returndistortion = distortion2;
835             best_rd = this_rd;
836             vpx_memcpy(&best_mbmode, &x->e_mbd.mode_info_context->mbmi, sizeof(MB_MODE_INFO));
837             vpx_memcpy(&best_partition, x->partition_info, sizeof(PARTITION_INFO));
838
839             if (this_mode == B_PRED || this_mode == SPLITMV)
840                 for (i = 0; i < 16; i++)
841                 {
842                     vpx_memcpy(&best_bmodes[i], &x->e_mbd.block[i].bmi, sizeof(B_MODE_INFO));
843                 }
844             else
845             {
846                 best_bmodes[0].mv = x->e_mbd.block[0].bmi.mv;
847             }
848
849             // Testing this mode gave rise to an improvement in best error score. Lower threshold a bit for next time
850             cpi->rd_thresh_mult[mode_index] = (cpi->rd_thresh_mult[mode_index] >= (MIN_THRESHMULT + 2)) ? cpi->rd_thresh_mult[mode_index] - 2 : MIN_THRESHMULT;
851             cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
852         }
853
854         // If the mode did not help improve the best error case then raise the threshold for testing that mode next time around.
855         else
856         {
857             cpi->rd_thresh_mult[mode_index] += 4;
858
859             if (cpi->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
860                 cpi->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
861
862             cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
863         }
864
865         if (x->skip)
866             break;
867     }
868
869     // Reduce the activation RD thresholds for the best choice mode
870     if ((cpi->rd_baseline_thresh[best_mode_index] > 0) && (cpi->rd_baseline_thresh[best_mode_index] < (INT_MAX >> 2)))
871     {
872         int best_adjustment = (cpi->rd_thresh_mult[best_mode_index] >> 3);
873
874         cpi->rd_thresh_mult[best_mode_index] = (cpi->rd_thresh_mult[best_mode_index] >= (MIN_THRESHMULT + best_adjustment)) ? cpi->rd_thresh_mult[best_mode_index] - best_adjustment : MIN_THRESHMULT;
875         cpi->rd_threshes[best_mode_index] = (cpi->rd_baseline_thresh[best_mode_index] >> 7) * cpi->rd_thresh_mult[best_mode_index];
876     }
877
878     // Keep a record of best mode index for use in next loop
879     cpi->last_best_mode_index = best_mode_index;
880
881     if (best_mbmode.mode <= B_PRED)
882     {
883         x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
884         vp8_pick_intra_mbuv_mode(x);
885         best_mbmode.uv_mode = x->e_mbd.mode_info_context->mbmi.uv_mode;
886     }
887
888
889     {
890         int this_rdbin = (*returndistortion >> 7);
891
892         if (this_rdbin >= 1024)
893         {
894             this_rdbin = 1023;
895         }
896
897         cpi->error_bins[this_rdbin] ++;
898     }
899
900
901     if (cpi->is_src_frame_alt_ref && (best_mbmode.mode != ZEROMV || best_mbmode.ref_frame != ALTREF_FRAME))
902     {
903         best_mbmode.mode = ZEROMV;
904         best_mbmode.ref_frame = ALTREF_FRAME;
905         best_mbmode.mv.as_int = 0;
906         best_mbmode.uv_mode = 0;
907         best_mbmode.mb_skip_coeff = (cpi->common.mb_no_coeff_skip) ? 1 : 0;
908         best_mbmode.partitioning = 0;
909         best_mbmode.dc_diff = 0;
910
911         vpx_memcpy(&x->e_mbd.mode_info_context->mbmi, &best_mbmode, sizeof(MB_MODE_INFO));
912         vpx_memcpy(x->partition_info, &best_partition, sizeof(PARTITION_INFO));
913
914         for (i = 0; i < 16; i++)
915         {
916             vpx_memset(&x->e_mbd.block[i].bmi, 0, sizeof(B_MODE_INFO));
917         }
918
919         x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
920
921         return best_rd;
922     }
923
924
925     // macroblock modes
926     vpx_memcpy(&x->e_mbd.mode_info_context->mbmi, &best_mbmode, sizeof(MB_MODE_INFO));
927     vpx_memcpy(x->partition_info, &best_partition, sizeof(PARTITION_INFO));
928
929     if (x->e_mbd.mode_info_context->mbmi.mode == B_PRED || x->e_mbd.mode_info_context->mbmi.mode == SPLITMV)
930         for (i = 0; i < 16; i++)
931         {
932             vpx_memcpy(&x->e_mbd.block[i].bmi, &best_bmodes[i], sizeof(B_MODE_INFO));
933
934         }
935     else
936     {
937         vp8_set_mbmode_and_mvs(x, x->e_mbd.mode_info_context->mbmi.mode, &best_bmodes[0].mv.as_mv);
938     }
939
940     x->e_mbd.mode_info_context->mbmi.mv.as_mv = x->e_mbd.block[15].bmi.mv.as_mv;
941
942     return best_rd;
943 }