Swap alt/gold/new/last frame buffer ptrs instead of copying.
[profile/ivi/libvpx.git] / vp8 / encoder / rdopt.c
1 /*
2  *  Copyright (c) 2010 The VP8 project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10
11
12 #include <stdio.h>
13 #include <math.h>
14 #include <limits.h>
15 #include <assert.h>
16 #include "pragmas.h"
17
18 #include "tokenize.h"
19 #include "treewriter.h"
20 #include "onyx_int.h"
21 #include "modecosts.h"
22 #include "encodeintra.h"
23 #include "entropymode.h"
24 #include "reconinter.h"
25 #include "reconintra.h"
26 #include "reconintra4x4.h"
27 #include "findnearmv.h"
28 #include "encodemb.h"
29 #include "quantize.h"
30 #include "idct.h"
31 #include "g_common.h"
32 #include "variance.h"
33 #include "mcomp.h"
34
35 #include "vpx_mem/vpx_mem.h"
36 #include "dct.h"
37 #include "systemdependent.h"
38
39 #define DIAMONDSEARCH 1
40 #if CONFIG_RUNTIME_CPU_DETECT
41 #define IF_RTCD(x)  (x)
42 #else
43 #define IF_RTCD(x)  NULL
44 #endif
45
46
47 void vp8cx_mb_init_quantizer(VP8_COMP *cpi, MACROBLOCK *x);
48
49
50 #define RDFUNC(RM,DM,R,D,target_rd) ( ((128+(R)*(RM)) >> 8) + (DM)*(D) )
51 /*int  RDFUNC( int RM,int DM, int R, int D, int target_r )
52 {
53     int rd_value;
54
55     rd_value =  ( ((128+(R)*(RM)) >> 8) + (DM)*(D) );
56
57     return rd_value;
58 }*/
59
60 #define UVRDFUNC(RM,DM,R,D,target_r)  RDFUNC(RM,DM,R,D,target_r)
61
62 #define RDCOST(RM,DM,R,D) ( ((128+(R)*(RM)) >> 8) + (DM)*(D) )
63
64 #define MAXF(a,b)            (((a) > (b)) ? (a) : (b))
65
66
67 extern const TOKENEXTRA vp8_dct_value_tokens[DCT_MAX_VALUE*2];
68 extern const TOKENEXTRA *vp8_dct_value_tokens_ptr;
69 extern int vp8_dct_value_cost[DCT_MAX_VALUE*2];
70 extern int *vp8_dct_value_cost_ptr;
71
72
73 const int vp8_auto_speed_thresh[17] =
74 {
75     1000,
76     200,
77     150,
78     130,
79     150,
80     125,
81     120,
82     115,
83     115,
84     115,
85     115,
86     115,
87     115,
88     115,
89     115,
90     115,
91     105
92 };
93
94 const MB_PREDICTION_MODE vp8_mode_order[MAX_MODES] =
95 {
96     ZEROMV,
97     DC_PRED,
98
99     NEARESTMV,
100     NEARMV,
101
102     ZEROMV,
103     NEARESTMV,
104
105     ZEROMV,
106     NEARESTMV,
107
108     NEARMV,
109     NEARMV,
110
111     V_PRED,
112     H_PRED,
113     TM_PRED,
114
115     NEWMV,
116     NEWMV,
117     NEWMV,
118
119     SPLITMV,
120     SPLITMV,
121     SPLITMV,
122
123     B_PRED,
124 };
125
126 const MV_REFERENCE_FRAME vp8_ref_frame_order[MAX_MODES] =
127 {
128     LAST_FRAME,
129     INTRA_FRAME,
130
131     LAST_FRAME,
132     LAST_FRAME,
133
134     GOLDEN_FRAME,
135     GOLDEN_FRAME,
136
137     ALTREF_FRAME,
138     ALTREF_FRAME,
139
140     GOLDEN_FRAME,
141     ALTREF_FRAME,
142
143     INTRA_FRAME,
144     INTRA_FRAME,
145     INTRA_FRAME,
146
147     LAST_FRAME,
148     GOLDEN_FRAME,
149     ALTREF_FRAME,
150
151     LAST_FRAME,
152     GOLDEN_FRAME,
153     ALTREF_FRAME,
154
155     INTRA_FRAME,
156 };
157
158 static void fill_token_costs(
159     unsigned int c      [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [vp8_coef_tokens],
160     const vp8_prob p    [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [vp8_coef_tokens-1]
161 )
162 {
163     int i, j, k;
164
165
166     for (i = 0; i < BLOCK_TYPES; i++)
167         for (j = 0; j < COEF_BANDS; j++)
168             for (k = 0; k < PREV_COEF_CONTEXTS; k++)
169
170                 vp8_cost_tokens((int *)(c [i][j][k]), p [i][j][k], vp8_coef_tree);
171
172 }
173
174 static int rd_iifactor [ 32 ] =  {    4,   4,   3,   2,   1,   0,   0,   0,
175                                       0,   0,   0,   0,   0,   0,   0,   0,
176                                       0,   0,   0,   0,   0,   0,   0,   0,
177                                       0,   0,   0,   0,   0,   0,   0,   0,
178                                  };
179
180
181 // The values in this table should be reviewed
182 static int sad_per_bit16lut[128] =
183 {
184     4,  4, 4, 4,  4, 4, 4, 4,   // 4
185     4,  4, 4, 4,  4, 4, 4, 4,   // 1
186     4,  4, 4, 4,  4, 4, 4, 4,   // 2
187     4,  4, 4, 4,  4, 4, 4, 4,   // 3
188     4,  4, 4, 4,  4, 4, 4, 4,   // 4
189     4,  4, 12, 12, 13, 13, 14, 14, // 5
190     14, 14, 14, 15, 15, 15, 15, 15, // 6
191     15, 15, 15, 15, 15, 15, 15, 15, // 7
192     15, 15, 15, 15, 15, 16, 16, 16, // 8
193     16, 16, 18, 18, 18, 18, 19, 19, // 9
194     19, 19, 19, 19, 19, 19, 19, 19, // 10
195     20, 20, 22, 22, 22, 22, 21, 21, // 11
196     22, 22, 22, 22, 22, 22, 22, 22, // 12
197     22, 22, 22, 22, 22, 22, 22, 22, // 13
198     22, 22, 22, 22, 22, 22, 22, 22, // 14
199     22, 22, 22, 22, 22, 22, 22, 22, // 15
200 };
201
202 static int sad_per_bit4lut[128] =
203 {
204     4,  4, 4, 4,  4, 4, 4, 4,   // 4
205     4,  4, 4, 4,  4, 4, 4, 4,   // 1
206     4,  4, 4, 4,  4, 4, 4, 4,   // 2
207     4,  4, 4, 4,  4, 4, 4, 4,   // 3
208     4,  4, 4, 4,  4, 4, 4, 4,   // 4
209     4,  4, 15, 15, 15, 15, 16, 16, // 5
210     16, 17, 17, 17, 17, 17, 17, 17, // 6
211     17, 17, 19, 19, 22, 22, 21, 21, // 7
212     23, 23, 23, 23, 23, 24, 24, 24, // 8
213     25, 25, 27, 27, 27, 27, 28, 28, // 9
214     28, 28, 29, 29, 29, 29, 29, 29, // 10
215     30, 30, 31, 31, 31, 31, 32, 32, // 11
216     34, 34, 34, 34, 34, 34, 34, 34, // 12
217     34, 34, 34, 34, 34, 34, 34, 34, // 13
218     34, 34, 34, 34, 34, 34, 34, 34, // 14
219     34, 34, 34, 34, 34, 34, 34, 34, // 15
220 };
221
222 void vp8cx_initialize_me_consts(VP8_COMP *cpi, int QIndex)
223 {
224     cpi->mb.sadperbit16 =  sad_per_bit16lut[QIndex];
225     cpi->mb.sadperbit4  =  sad_per_bit4lut[QIndex];
226 }
227
228 void vp8_initialize_rd_consts(VP8_COMP *cpi, int Qvalue)
229 {
230     int q;
231     int i;
232     int *thresh;
233     int threshmult;
234     double capped_q = (Qvalue < 160) ? (double)Qvalue : 160.0;
235     double rdconst = 3.00;
236
237     vp8_clear_system_state();  //__asm emms;
238
239     // Further tests required to see if optimum is different
240     // for key frames, golden frames and arf frames.
241     // if (cpi->common.refresh_golden_frame ||
242     //     cpi->common.refresh_alt_ref_frame)
243     cpi->RDMULT = (int)(rdconst * (capped_q * capped_q));
244
245     // Extend rate multiplier along side quantizer zbin increases
246     if (cpi->zbin_over_quant  > 0)
247     {
248         double oq_factor;
249         double modq;
250
251         // Experimental code using the same basic equation as used for Q above
252         // The units of cpi->zbin_over_quant are 1/128 of Q bin size
253         oq_factor = 1.0 + ((double)0.0015625 * cpi->zbin_over_quant);
254         modq = (int)((double)capped_q * oq_factor);
255         cpi->RDMULT = (int)(rdconst * (modq * modq));
256     }
257
258     if (cpi->pass == 2 && (cpi->common.frame_type != KEY_FRAME))
259     {
260         if (cpi->next_iiratio > 31)
261             cpi->RDMULT += (cpi->RDMULT * rd_iifactor[31]) >> 4;
262         else
263             cpi->RDMULT += (cpi->RDMULT * rd_iifactor[cpi->next_iiratio]) >> 4;
264     }
265
266     if (cpi->RDMULT < 125)
267         cpi->RDMULT = 125;
268
269     cpi->mb.errorperbit = (cpi->RDMULT / 100);
270
271     if (cpi->mb.errorperbit < 1)
272         cpi->mb.errorperbit = 1;
273
274     vp8_set_speed_features(cpi);
275
276     if (cpi->common.simpler_lpf)
277         cpi->common.filter_type = SIMPLE_LOOPFILTER;
278
279     q = (int)pow(Qvalue, 1.25);
280
281     if (q < 8)
282         q = 8;
283
284     if (cpi->ref_frame_flags == VP8_ALT_FLAG)
285     {
286         thresh      = &cpi->rd_threshes[THR_NEWA];
287         threshmult  = cpi->sf.thresh_mult[THR_NEWA];
288     }
289     else if (cpi->ref_frame_flags == VP8_GOLD_FLAG)
290     {
291         thresh      = &cpi->rd_threshes[THR_NEWG];
292         threshmult  = cpi->sf.thresh_mult[THR_NEWG];
293     }
294     else
295     {
296         thresh      = &cpi->rd_threshes[THR_NEWMV];
297         threshmult  = cpi->sf.thresh_mult[THR_NEWMV];
298     }
299
300     if (cpi->RDMULT > 1000)
301     {
302         cpi->RDDIV = 1;
303         cpi->RDMULT /= 100;
304
305         for (i = 0; i < MAX_MODES; i++)
306         {
307             if (cpi->sf.thresh_mult[i] < INT_MAX)
308             {
309                 cpi->rd_threshes[i] = cpi->sf.thresh_mult[i] * q / 100;
310             }
311             else
312             {
313                 cpi->rd_threshes[i] = INT_MAX;
314             }
315
316             cpi->rd_baseline_thresh[i] = cpi->rd_threshes[i];
317         }
318     }
319     else
320     {
321         cpi->RDDIV = 100;
322
323         for (i = 0; i < MAX_MODES; i++)
324         {
325             if (cpi->sf.thresh_mult[i] < (INT_MAX / q))
326             {
327                 cpi->rd_threshes[i] = cpi->sf.thresh_mult[i] * q;
328             }
329             else
330             {
331                 cpi->rd_threshes[i] = INT_MAX;
332             }
333
334             cpi->rd_baseline_thresh[i] = cpi->rd_threshes[i];
335         }
336     }
337
338     fill_token_costs(
339         cpi->mb.token_costs,
340         (const vp8_prob( *)[8][3][11]) cpi->common.fc.coef_probs
341     );
342
343     vp8_init_mode_costs(cpi);
344
345 }
346
347 void vp8_auto_select_speed(VP8_COMP *cpi)
348 {
349     int used = cpi->oxcf.cpu_used;
350
351     int milliseconds_for_compress = (int)(1000000 / cpi->oxcf.frame_rate);
352
353     milliseconds_for_compress = milliseconds_for_compress * (16 - cpi->oxcf.cpu_used) / 16;
354
355 #if 0
356
357     if (0)
358     {
359         FILE *f;
360
361         f = fopen("speed.stt", "a");
362         fprintf(f, " %8ld %10ld %10ld %10ld\n",
363                 cpi->common.current_video_frame, cpi->Speed, milliseconds_for_compress, cpi->avg_pick_mode_time);
364         fclose(f);
365     }
366
367 #endif
368
369     /*
370     // this is done during parameter valid check
371     if( used > 16)
372         used = 16;
373     if( used < -16)
374         used = -16;
375     */
376
377     if (cpi->avg_pick_mode_time < milliseconds_for_compress && (cpi->avg_encode_time - cpi->avg_pick_mode_time) < milliseconds_for_compress)
378     {
379         if (cpi->avg_pick_mode_time == 0)
380         {
381             cpi->Speed = 4;
382         }
383         else
384         {
385             if (milliseconds_for_compress * 100 < cpi->avg_encode_time * 95)
386             {
387                 cpi->Speed          += 2;
388                 cpi->avg_pick_mode_time = 0;
389                 cpi->avg_encode_time = 0;
390
391                 if (cpi->Speed > 16)
392                 {
393                     cpi->Speed = 16;
394                 }
395             }
396
397             if (milliseconds_for_compress * 100 > cpi->avg_encode_time * vp8_auto_speed_thresh[cpi->Speed])
398             {
399                 cpi->Speed          -= 1;
400                 cpi->avg_pick_mode_time = 0;
401                 cpi->avg_encode_time = 0;
402
403                 // In real-time mode, cpi->speed is in [4, 16].
404                 if (cpi->Speed < 4)        //if ( cpi->Speed < 0 )
405                 {
406                     cpi->Speed = 4;        //cpi->Speed = 0;
407                 }
408             }
409         }
410     }
411     else
412     {
413         cpi->Speed += 4;
414
415         if (cpi->Speed > 16)
416             cpi->Speed = 16;
417
418
419         cpi->avg_pick_mode_time = 0;
420         cpi->avg_encode_time = 0;
421     }
422 }
423
424 int vp8_block_error_c(short *coeff, short *dqcoeff)
425 {
426     int i;
427     int error = 0;
428
429     for (i = 0; i < 16; i++)
430     {
431         int this_diff = coeff[i] - dqcoeff[i];
432         error += this_diff * this_diff;
433     }
434
435     return error;
436 }
437
438 int vp8_mbblock_error_c(MACROBLOCK *mb, int dc)
439 {
440     BLOCK  *be;
441     BLOCKD *bd;
442     int i, j;
443     int berror, error = 0;
444
445     for (i = 0; i < 16; i++)
446     {
447         be = &mb->block[i];
448         bd = &mb->e_mbd.block[i];
449
450         berror = 0;
451
452         for (j = dc; j < 16; j++)
453         {
454             int this_diff = be->coeff[j] - bd->dqcoeff[j];
455             berror += this_diff * this_diff;
456         }
457
458         error += berror;
459     }
460
461     return error;
462 }
463
464 int vp8_mbuverror_c(MACROBLOCK *mb)
465 {
466
467     BLOCK  *be;
468     BLOCKD *bd;
469
470
471     int i;
472     int error = 0;
473
474     for (i = 16; i < 24; i++)
475     {
476         be = &mb->block[i];
477         bd = &mb->e_mbd.block[i];
478
479         error += vp8_block_error_c(be->coeff, bd->dqcoeff);
480     }
481
482     return error;
483 }
484
485 #if !(CONFIG_REALTIME_ONLY)
486 static int macro_block_max_error(MACROBLOCK *mb)
487 {
488     int error = 0;
489     int dc = 0;
490     BLOCK  *be;
491     int i, j;
492     int berror;
493
494     dc = !(mb->e_mbd.mbmi.mode == B_PRED || mb->e_mbd.mbmi.mode == SPLITMV);
495
496     for (i = 0; i < 16; i++)
497     {
498         be = &mb->block[i];
499
500         berror = 0;
501
502         for (j = dc; j < 16; j++)
503         {
504             int this_diff = be->coeff[j];
505             berror += this_diff * this_diff;
506         }
507
508         error += berror;
509     }
510
511     for (i = 16; i < 24; i++)
512     {
513         be = &mb->block[i];
514         berror = 0;
515
516         for (j = 0; j < 16; j++)
517         {
518             int this_diff = be->coeff[j];
519             berror += this_diff * this_diff;
520         }
521
522         error += berror;
523     }
524
525     error <<= 2;
526
527     if (dc)
528     {
529         be = &mb->block[24];
530         berror = 0;
531
532         for (j = 0; j < 16; j++)
533         {
534             int this_diff = be->coeff[j];
535             berror += this_diff * this_diff;
536         }
537
538         error += berror;
539     }
540
541     error >>= 4;
542     return error;
543 }
544 #endif
545
546 int VP8_UVSSE(MACROBLOCK *x, const vp8_variance_rtcd_vtable_t *rtcd)
547 {
548     unsigned char *uptr, *vptr;
549     unsigned char *upred_ptr = (*(x->block[16].base_src) + x->block[16].src);
550     unsigned char *vpred_ptr = (*(x->block[20].base_src) + x->block[20].src);
551     int uv_stride = x->block[16].src_stride;
552
553     unsigned int sse1 = 0;
554     unsigned int sse2 = 0;
555     int mv_row;
556     int mv_col;
557     int offset;
558     int pre_stride = x->e_mbd.block[16].pre_stride;
559
560     vp8_build_uvmvs(&x->e_mbd, 0);
561     mv_row = x->e_mbd.block[16].bmi.mv.as_mv.row;
562     mv_col = x->e_mbd.block[16].bmi.mv.as_mv.col;
563
564     offset = (mv_row >> 3) * pre_stride + (mv_col >> 3);
565     uptr = x->e_mbd.pre.u_buffer + offset;
566     vptr = x->e_mbd.pre.v_buffer + offset;
567
568     if ((mv_row | mv_col) & 7)
569     {
570         VARIANCE_INVOKE(rtcd, subpixvar8x8)(uptr, pre_stride, mv_col & 7, mv_row & 7, upred_ptr, uv_stride, &sse2);
571         VARIANCE_INVOKE(rtcd, subpixvar8x8)(vptr, pre_stride, mv_col & 7, mv_row & 7, vpred_ptr, uv_stride, &sse1);
572         sse2 += sse1;
573     }
574     else
575     {
576         VARIANCE_INVOKE(rtcd, subpixvar8x8)(uptr, pre_stride, mv_col & 7, mv_row & 7, upred_ptr, uv_stride, &sse2);
577         VARIANCE_INVOKE(rtcd, subpixvar8x8)(vptr, pre_stride, mv_col & 7, mv_row & 7, vpred_ptr, uv_stride, &sse1);
578         sse2 += sse1;
579     }
580
581     return sse2;
582
583 }
584
585 #if !(CONFIG_REALTIME_ONLY)
586 static int cost_coeffs(MACROBLOCK *mb, BLOCKD *b, int type, ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l)
587 {
588     int c = !type;              /* start at coef 0, unless Y with Y2 */
589     int eob = b->eob;
590     int pt ;    /* surrounding block/prev coef predictor */
591     int cost = 0;
592     short *qcoeff_ptr = b->qcoeff;
593
594     VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
595
596 # define QC( I)  ( qcoeff_ptr [vp8_default_zig_zag1d[I]] )
597
598     for (; c < eob; c++)
599     {
600         int v = QC(c);
601         int t = vp8_dct_value_tokens_ptr[v].Token;
602         cost += mb->token_costs [type] [vp8_coef_bands[c]] [pt] [t];
603         cost += vp8_dct_value_cost_ptr[v];
604         pt = vp8_prev_token_class[t];
605     }
606
607 # undef QC
608
609     if (c < 16)
610         cost += mb->token_costs [type] [vp8_coef_bands[c]] [pt] [DCT_EOB_TOKEN];
611
612     pt = (c != !type); // is eob first coefficient;
613     *a = *l = pt;
614
615     return cost;
616 }
617
618 int vp8_rdcost_mby(MACROBLOCK *mb)
619 {
620     int cost = 0;
621     int b;
622     TEMP_CONTEXT t, t2;
623     int type = 0;
624
625     MACROBLOCKD *x = &mb->e_mbd;
626
627     vp8_setup_temp_context(&t, x->above_context[Y1CONTEXT], x->left_context[Y1CONTEXT], 4);
628     vp8_setup_temp_context(&t2, x->above_context[Y2CONTEXT], x->left_context[Y2CONTEXT], 1);
629
630     if (x->mbmi.mode == SPLITMV)
631         type = 3;
632
633     for (b = 0; b < 16; b++)
634         cost += cost_coeffs(mb, x->block + b, type,
635                             t.a + vp8_block2above[b], t.l + vp8_block2left[b]);
636
637     if (x->mbmi.mode != SPLITMV)
638         cost += cost_coeffs(mb, x->block + 24, 1,
639                             t2.a + vp8_block2above[24], t2.l + vp8_block2left[24]);
640
641     return cost;
642 }
643
644
645 static void rd_pick_intra4x4block(
646     VP8_COMP *cpi,
647     MACROBLOCK *x,
648     BLOCK *be,
649     BLOCKD *b,
650     B_PREDICTION_MODE *best_mode,
651     B_PREDICTION_MODE above,
652     B_PREDICTION_MODE left,
653     ENTROPY_CONTEXT *a,
654     ENTROPY_CONTEXT *l,
655
656     int *bestrate,
657     int *bestratey,
658     int *bestdistortion)
659 {
660     B_PREDICTION_MODE mode;
661     int best_rd = INT_MAX;       // 1<<30
662     int rate = 0;
663     int distortion;
664     unsigned int *mode_costs;
665
666     ENTROPY_CONTEXT ta = *a, tempa = *a;
667     ENTROPY_CONTEXT tl = *l, templ = *l;
668
669
670     if (x->e_mbd.frame_type == KEY_FRAME)
671     {
672         mode_costs  = x->bmode_costs[above][left];
673     }
674     else
675     {
676         mode_costs = x->inter_bmode_costs;
677     }
678
679     for (mode = B_DC_PRED; mode <= B_HU_PRED; mode++)
680     {
681         int this_rd;
682         int ratey;
683
684         rate = mode_costs[mode];
685         vp8_encode_intra4x4block_rd(IF_RTCD(&cpi->rtcd), x, be, b, mode);
686
687         tempa = ta;
688         templ = tl;
689
690         ratey = cost_coeffs(x, b, 3, &tempa, &templ);
691         rate += ratey;
692         distortion = ENCODEMB_INVOKE(IF_RTCD(&cpi->rtcd.encodemb), berr)(be->coeff, b->dqcoeff) >> 2;
693
694         this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
695
696         if (this_rd < best_rd)
697         {
698             *bestrate = rate;
699             *bestratey = ratey;
700             *bestdistortion = distortion;
701             best_rd = this_rd;
702             *best_mode = mode;
703             *a = tempa;
704             *l = templ;
705         }
706     }
707
708     b->bmi.mode = (B_PREDICTION_MODE)(*best_mode);
709     vp8_encode_intra4x4block_rd(IF_RTCD(&cpi->rtcd), x, be, b, b->bmi.mode);
710
711 }
712
713
714 int vp8_rd_pick_intra4x4mby_modes(VP8_COMP *cpi, MACROBLOCK *mb, int *Rate, int *rate_y, int *Distortion)
715 {
716     MACROBLOCKD *const xd = &mb->e_mbd;
717     int i;
718     TEMP_CONTEXT t;
719     int cost = mb->mbmode_cost [xd->frame_type] [B_PRED];
720     int distortion = 0;
721     int tot_rate_y = 0;
722
723     vp8_intra_prediction_down_copy(xd);
724     vp8_setup_temp_context(&t, xd->above_context[Y1CONTEXT], xd->left_context[Y1CONTEXT], 4);
725
726     for (i = 0; i < 16; i++)
727     {
728         MODE_INFO *const mic = xd->mode_info_context;
729         const int mis = xd->mode_info_stride;
730         const B_PREDICTION_MODE A = vp8_above_bmi(mic, i, mis)->mode;
731         const B_PREDICTION_MODE L = vp8_left_bmi(mic, i)->mode;
732         B_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
733         int UNINITIALIZED_IS_SAFE(r), UNINITIALIZED_IS_SAFE(ry), UNINITIALIZED_IS_SAFE(d);
734
735         rd_pick_intra4x4block(
736             cpi, mb, mb->block + i, xd->block + i, &best_mode, A, L,
737             t.a + vp8_block2above[i],
738             t.l + vp8_block2left[i], &r, &ry, &d);
739
740         cost += r;
741         distortion += d;
742         tot_rate_y += ry;
743         mic->bmi[i].mode = xd->block[i].bmi.mode = best_mode;
744     }
745
746     *Rate = cost;
747     *rate_y += tot_rate_y;
748     *Distortion = distortion;
749
750     return RDCOST(mb->rdmult, mb->rddiv, cost, distortion);
751 }
752
753 int vp8_rd_pick_intra16x16mby_mode(VP8_COMP *cpi, MACROBLOCK *x, int *Rate, int *rate_y, int *Distortion)
754 {
755
756     MB_PREDICTION_MODE mode;
757     MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(mode_selected);
758     int rate, ratey;
759     unsigned int distortion;
760     int best_rd = INT_MAX;
761
762     //Y Search for 16x16 intra prediction mode
763     for (mode = DC_PRED; mode <= TM_PRED; mode++)
764     {
765         int this_rd;
766         int dummy;
767         rate = 0;
768
769         x->e_mbd.mbmi.mode = mode;
770
771         rate += x->mbmode_cost[x->e_mbd.frame_type][x->e_mbd.mbmi.mode];
772
773         vp8_encode_intra16x16mbyrd(IF_RTCD(&cpi->rtcd), x);
774
775         ratey = vp8_rdcost_mby(x);
776
777         rate += ratey;
778
779         VARIANCE_INVOKE(&cpi->rtcd.variance, get16x16var)(x->src.y_buffer, x->src.y_stride, x->e_mbd.dst.y_buffer, x->e_mbd.dst.y_stride, &distortion, &dummy);
780
781         this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
782
783         if (this_rd < best_rd)
784         {
785             mode_selected = mode;
786             best_rd = this_rd;
787             *Rate = rate;
788             *rate_y = ratey;
789             *Distortion = (int)distortion;
790         }
791     }
792
793     x->e_mbd.mbmi.mode = mode_selected;
794     return best_rd;
795 }
796
797
798 static int rd_cost_mbuv(MACROBLOCK *mb)
799 {
800     TEMP_CONTEXT t, t2;
801     int b;
802     int cost = 0;
803     MACROBLOCKD *x = &mb->e_mbd;
804
805     vp8_setup_temp_context(&t, x->above_context[UCONTEXT], x->left_context[UCONTEXT], 2);
806     vp8_setup_temp_context(&t2, x->above_context[VCONTEXT], x->left_context[VCONTEXT], 2);
807
808     for (b = 16; b < 20; b++)
809         cost += cost_coeffs(mb, x->block + b, vp8_block2type[b],
810                             t.a + vp8_block2above[b], t.l + vp8_block2left[b]);
811
812     for (b = 20; b < 24; b++)
813         cost += cost_coeffs(mb, x->block + b, vp8_block2type[b],
814                             t2.a + vp8_block2above[b], t2.l + vp8_block2left[b]);
815
816     return cost;
817 }
818
819
820 unsigned int vp8_get_mbuvrecon_error(const vp8_variance_rtcd_vtable_t *rtcd, const MACROBLOCK *x) // sum of squares
821 {
822     unsigned int sse0, sse1;
823     int sum0, sum1;
824     VARIANCE_INVOKE(rtcd, get8x8var)(x->src.u_buffer, x->src.uv_stride, x->e_mbd.dst.u_buffer, x->e_mbd.dst.uv_stride, &sse0, &sum0);
825     VARIANCE_INVOKE(rtcd, get8x8var)(x->src.v_buffer, x->src.uv_stride, x->e_mbd.dst.v_buffer, x->e_mbd.dst.uv_stride, &sse1, &sum1);
826     return (sse0 + sse1);
827 }
828
829 static int vp8_rd_inter_uv(VP8_COMP *cpi, MACROBLOCK *x, int *rate, int *distortion, int fullpixel)
830 {
831     vp8_build_uvmvs(&x->e_mbd, fullpixel);
832     vp8_encode_inter16x16uvrd(IF_RTCD(&cpi->rtcd), x);
833
834
835     *rate       = rd_cost_mbuv(x);
836     *distortion = ENCODEMB_INVOKE(&cpi->rtcd.encodemb, mbuverr)(x) / 4;
837
838     return UVRDFUNC(x->rdmult, x->rddiv, *rate, *distortion, cpi->target_bits_per_mb);
839 }
840
841 int vp8_rd_pick_intra_mbuv_mode(VP8_COMP *cpi, MACROBLOCK *x, int *rate, int *rate_tokenonly, int *distortion)
842 {
843     MB_PREDICTION_MODE mode;
844     MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(mode_selected);
845     int best_rd = INT_MAX;
846     int UNINITIALIZED_IS_SAFE(d), UNINITIALIZED_IS_SAFE(r);
847     int rate_to;
848
849     for (mode = DC_PRED; mode <= TM_PRED; mode++)
850     {
851         int rate;
852         int distortion;
853         int this_rd;
854
855         x->e_mbd.mbmi.uv_mode = mode;
856         vp8_encode_intra16x16mbuvrd(IF_RTCD(&cpi->rtcd), x);
857
858         rate_to = rd_cost_mbuv(x);
859         rate = rate_to + x->intra_uv_mode_cost[x->e_mbd.frame_type][x->e_mbd.mbmi.uv_mode];
860
861         distortion = vp8_get_mbuvrecon_error(IF_RTCD(&cpi->rtcd.variance), x);
862
863         this_rd = UVRDFUNC(x->rdmult, x->rddiv, rate, distortion, cpi->target_bits_per_mb);
864
865         if (this_rd < best_rd)
866         {
867             best_rd = this_rd;
868             d = distortion;
869             r = rate;
870             *rate_tokenonly = rate_to;
871             mode_selected = mode;
872         }
873     }
874
875     *rate = r;
876     *distortion = d;
877
878     x->e_mbd.mbmi.uv_mode = mode_selected;
879     return best_rd;
880 }
881 #endif
882
883 int vp8_cost_mv_ref(MB_PREDICTION_MODE m, const int near_mv_ref_ct[4])
884 {
885     vp8_prob p [VP8_MVREFS-1];
886     assert(NEARESTMV <= m  &&  m <= SPLITMV);
887     vp8_mv_ref_probs(p, near_mv_ref_ct);
888     return vp8_cost_token(vp8_mv_ref_tree, p, VP8_MVREFENCODINGS + m);
889 }
890
891 void vp8_set_mbmode_and_mvs(MACROBLOCK *x, MB_PREDICTION_MODE mb, MV *mv)
892 {
893     int i;
894
895     x->e_mbd.mbmi.mode = mb;
896     x->e_mbd.mbmi.mv.as_mv.row = mv->row;
897     x->e_mbd.mbmi.mv.as_mv.col = mv->col;
898
899     for (i = 0; i < 16; i++)
900     {
901         B_MODE_INFO *bmi = &x->e_mbd.block[i].bmi;
902         bmi->mode = (B_PREDICTION_MODE) mb;
903         bmi->mv.as_mv.row = mv->row;
904         bmi->mv.as_mv.col = mv->col;
905     }
906 }
907
908 #if !(CONFIG_REALTIME_ONLY)
909 int vp8_count_labels(int const *labelings)
910 {
911     int i;
912     int count = 0;
913
914     for (i = 0; i < 16; i++)
915     {
916         if (labelings[i] > count)
917             count = labelings[i];
918     }
919
920     return count + 1;
921 }
922
923
924 static int labels2mode(
925     MACROBLOCK *x,
926     int const *labelings, int which_label,
927     B_PREDICTION_MODE this_mode,
928     MV *this_mv, MV *best_ref_mv,
929     int *mvcost[2]
930 )
931 {
932     MACROBLOCKD *const xd = & x->e_mbd;
933     MODE_INFO *const mic = xd->mode_info_context;
934     const int mis = xd->mode_info_stride;
935
936     int cost = 0;
937     int thismvcost = 0;
938
939     /* We have to be careful retrieving previously-encoded motion vectors.
940        Ones from this macroblock have to be pulled from the BLOCKD array
941        as they have not yet made it to the bmi array in our MB_MODE_INFO. */
942
943     int i = 0;
944
945     do
946     {
947         BLOCKD *const d = xd->block + i;
948         const int row = i >> 2,  col = i & 3;
949
950         B_PREDICTION_MODE m;
951
952         if (labelings[i] != which_label)
953             continue;
954
955         if (col  &&  labelings[i] == labelings[i-1])
956             m = LEFT4X4;
957         else if (row  &&  labelings[i] == labelings[i-4])
958             m = ABOVE4X4;
959         else
960         {
961             // the only time we should do costing for new motion vector or mode
962             // is when we are on a new label  (jbb May 08, 2007)
963             switch (m = this_mode)
964             {
965             case NEW4X4 :
966                 thismvcost  = vp8_mv_bit_cost(this_mv, best_ref_mv, mvcost, 102);
967                 break;
968             case LEFT4X4:
969                 *this_mv = col ? d[-1].bmi.mv.as_mv : vp8_left_bmi(mic, i)->mv.as_mv;
970                 break;
971             case ABOVE4X4:
972                 *this_mv = row ? d[-4].bmi.mv.as_mv : vp8_above_bmi(mic, i, mis)->mv.as_mv;
973                 break;
974             case ZERO4X4:
975                 this_mv->row = this_mv->col = 0;
976                 break;
977             default:
978                 break;
979             }
980
981             if (m == ABOVE4X4)  // replace above with left if same
982             {
983                 const MV mv = col ? d[-1].bmi.mv.as_mv : vp8_left_bmi(mic, i)->mv.as_mv;
984
985                 if (mv.row == this_mv->row  &&  mv.col == this_mv->col)
986                     m = LEFT4X4;
987             }
988
989             cost = x->inter_bmode_costs[ m];
990         }
991
992         d->bmi.mode = m;
993         d->bmi.mv.as_mv = *this_mv;
994
995     }
996     while (++i < 16);
997
998     cost += thismvcost ;
999     return cost;
1000 }
1001
1002 static int rdcost_mbsegment_y(MACROBLOCK *mb, const int *labels, int which_label, TEMP_CONTEXT *t)
1003 {
1004     int cost = 0;
1005     int b;
1006     MACROBLOCKD *x = &mb->e_mbd;
1007
1008
1009     for (b = 0; b < 16; b++)
1010         if (labels[ b] == which_label)
1011             cost += cost_coeffs(mb, x->block + b, 3,
1012                                 t->a + vp8_block2above[b],
1013                                 t->l + vp8_block2left[b]);
1014
1015     return cost;
1016
1017 }
1018 static unsigned int vp8_encode_inter_mb_segment(MACROBLOCK *x, int const *labels, int which_label, const vp8_encodemb_rtcd_vtable_t *rtcd)
1019 {
1020     int i;
1021     unsigned int distortion = 0;
1022
1023     for (i = 0; i < 16; i++)
1024     {
1025         if (labels[i] == which_label)
1026         {
1027             BLOCKD *bd = &x->e_mbd.block[i];
1028             BLOCK *be = &x->block[i];
1029
1030
1031             vp8_build_inter_predictors_b(bd, 16, x->e_mbd.subpixel_predict);
1032             ENCODEMB_INVOKE(rtcd, subb)(be, bd, 16);
1033             x->vp8_short_fdct4x4(be->src_diff, be->coeff, 32);
1034
1035             // set to 0 no way to account for 2nd order DC so discount
1036             //be->coeff[0] = 0;
1037             x->quantize_b(be, bd);
1038
1039             distortion += ENCODEMB_INVOKE(rtcd, berr)(be->coeff, bd->dqcoeff);
1040         }
1041     }
1042
1043     return distortion;
1044 }
1045
1046 static void macro_block_yrd(MACROBLOCK *mb, int *Rate, int *Distortion, const vp8_encodemb_rtcd_vtable_t *rtcd)
1047 {
1048     int b;
1049     MACROBLOCKD *const x = &mb->e_mbd;
1050     BLOCK   *const mb_y2 = mb->block + 24;
1051     BLOCKD *const x_y2  = x->block + 24;
1052     short *Y2DCPtr = mb_y2->src_diff;
1053     BLOCK *beptr;
1054     int d;
1055
1056     ENCODEMB_INVOKE(rtcd, submby)(mb->src_diff, mb->src.y_buffer, mb->e_mbd.predictor, mb->src.y_stride);
1057
1058     // Fdct and building the 2nd order block
1059     for (beptr = mb->block; beptr < mb->block + 16; beptr += 2)
1060     {
1061         mb->vp8_short_fdct8x4(beptr->src_diff, beptr->coeff, 32);
1062         *Y2DCPtr++ = beptr->coeff[0];
1063         *Y2DCPtr++ = beptr->coeff[16];
1064     }
1065
1066     // 2nd order fdct
1067     if (x->mbmi.mode != SPLITMV)
1068     {
1069         mb->short_walsh4x4(mb_y2->src_diff, mb_y2->coeff, 8);
1070     }
1071
1072     // Quantization
1073     for (b = 0; b < 16; b++)
1074     {
1075         mb->quantize_b(&mb->block[b], &mb->e_mbd.block[b]);
1076     }
1077
1078     // DC predication and Quantization of 2nd Order block
1079     if (x->mbmi.mode != SPLITMV)
1080     {
1081
1082         {
1083             mb->quantize_b(mb_y2, x_y2);
1084         }
1085     }
1086
1087     // Distortion
1088     if (x->mbmi.mode == SPLITMV)
1089         d = ENCODEMB_INVOKE(rtcd, mberr)(mb, 0) << 2;
1090     else
1091     {
1092         d = ENCODEMB_INVOKE(rtcd, mberr)(mb, 1) << 2;
1093         d += ENCODEMB_INVOKE(rtcd, berr)(mb_y2->coeff, x_y2->dqcoeff);
1094     }
1095
1096     *Distortion = (d >> 4);
1097
1098     // rate
1099     *Rate = vp8_rdcost_mby(mb);
1100 }
1101
1102 static int vp8_rd_pick_best_mbsegmentation(VP8_COMP *cpi, MACROBLOCK *x, MV *best_ref_mv, int best_rd, int *mdcounts, int *returntotrate, int *returnyrate, int *returndistortion, int compressor_speed, int *mvcost[2], int mvthresh, int fullpixel)
1103 {
1104     int i, segmentation;
1105     B_PREDICTION_MODE this_mode;
1106     MACROBLOCKD *xc = &x->e_mbd;
1107     BLOCK *b = &x->block[0];
1108     BLOCKD *d = &x->e_mbd.block[0];
1109     BLOCK *c = &x->block[0];
1110     BLOCKD *e = &x->e_mbd.block[0];
1111     int const *labels;
1112     int best_segment_rd = INT_MAX;
1113     int best_seg = 0;
1114     int br = 0;
1115     int bd = 0;
1116     int bsr = 0;
1117     int bsd = 0;
1118     int bestsegmentyrate = 0;
1119
1120     // FIX TO Rd error outrange bug PGW 9 june 2004
1121     B_PREDICTION_MODE bmodes[16] = {ZERO4X4, ZERO4X4, ZERO4X4, ZERO4X4,
1122                                     ZERO4X4, ZERO4X4, ZERO4X4, ZERO4X4,
1123                                     ZERO4X4, ZERO4X4, ZERO4X4, ZERO4X4,
1124                                     ZERO4X4, ZERO4X4, ZERO4X4, ZERO4X4
1125                                    };
1126
1127     MV bmvs[16];
1128     int beobs[16];
1129
1130     vpx_memset(beobs, 0, sizeof(beobs));
1131
1132
1133     for (segmentation = 0; segmentation < VP8_NUMMBSPLITS; segmentation++)
1134     {
1135         int label_count;
1136         int this_segment_rd = 0;
1137         int label_mv_thresh;
1138         int rate = 0;
1139         int sbr = 0;
1140         int sbd = 0;
1141         int UNINITIALIZED_IS_SAFE(sseshift);
1142         int segmentyrate = 0;
1143
1144         vp8_variance_fn_ptr_t v_fn_ptr;
1145
1146         TEMP_CONTEXT t;
1147         TEMP_CONTEXT tb;
1148         vp8_setup_temp_context(&t, xc->above_context[Y1CONTEXT], xc->left_context[Y1CONTEXT], 4);
1149
1150         br = 0;
1151         bd = 0;
1152
1153         switch (segmentation)
1154         {
1155         case 0:
1156             v_fn_ptr.vf    = VARIANCE_INVOKE(&cpi->rtcd.variance, var16x8);
1157             v_fn_ptr.svf   = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar16x8);
1158             v_fn_ptr.sdf   = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8);
1159             v_fn_ptr.sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8x3);
1160             v_fn_ptr.sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8x4d);
1161             sseshift = 3;
1162             break;
1163         case 1:
1164             v_fn_ptr.vf    = VARIANCE_INVOKE(&cpi->rtcd.variance, var8x16);
1165             v_fn_ptr.svf   = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar8x16);
1166             v_fn_ptr.sdf   = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16);
1167             v_fn_ptr.sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16x3);
1168             v_fn_ptr.sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16x4d);
1169             sseshift = 3;
1170             break;
1171         case 2:
1172             v_fn_ptr.vf    = VARIANCE_INVOKE(&cpi->rtcd.variance, var8x8);
1173             v_fn_ptr.svf   = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar8x8);
1174             v_fn_ptr.sdf   = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8);
1175             v_fn_ptr.sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8x3);
1176             v_fn_ptr.sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8x4d);
1177             sseshift = 2;
1178             break;
1179         case 3:
1180             v_fn_ptr.vf    = VARIANCE_INVOKE(&cpi->rtcd.variance, var4x4);
1181             v_fn_ptr.svf   = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar4x4);
1182             v_fn_ptr.sdf   = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4);
1183             v_fn_ptr.sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4x3);
1184             v_fn_ptr.sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4x4d);
1185             sseshift = 0;
1186             break;
1187         }
1188
1189         labels = vp8_mbsplits[segmentation];
1190         label_count = vp8_count_labels(labels);
1191
1192         // 64 makes this threshold really big effectively
1193         // making it so that we very rarely check mvs on
1194         // segments.   setting this to 1 would make mv thresh
1195         // roughly equal to what it is for macroblocks
1196         label_mv_thresh = 1 * mvthresh / label_count ;
1197
1198         // Segmentation method overheads
1199         rate = vp8_cost_token(vp8_mbsplit_tree, vp8_mbsplit_probs, vp8_mbsplit_encodings + segmentation);
1200
1201         rate += vp8_cost_mv_ref(SPLITMV, mdcounts);
1202
1203         this_segment_rd += RDFUNC(x->rdmult, x->rddiv, rate, 0, cpi->target_bits_per_mb);
1204         br += rate;
1205
1206         for (i = 0; i < label_count; i++)
1207         {
1208             MV mode_mv[B_MODE_COUNT];
1209             int best_label_rd = INT_MAX;
1210             B_PREDICTION_MODE mode_selected = ZERO4X4;
1211             int j;
1212             int bestlabelyrate = 0;
1213
1214             b = &x->block[0];
1215             d = &x->e_mbd.block[0];
1216
1217
1218             // find first label
1219             for (j = 0; j < 16; j++)
1220                 if (labels[j] == i)
1221                     break;
1222
1223             c = &x->block[j];
1224             e = &x->e_mbd.block[j];
1225
1226             // search for the best motion vector on this segment
1227             for (this_mode = LEFT4X4; this_mode <= NEW4X4 ; this_mode ++)
1228             {
1229                 int distortion;
1230                 int this_rd;
1231                 int num00;
1232                 int labelyrate;
1233
1234                 TEMP_CONTEXT ts;
1235                 vp8_setup_temp_context(&ts, &t.a[0], &t.l[0], 4);
1236
1237                 if (this_mode == NEW4X4)
1238                 {
1239                     int step_param = 0;
1240                     int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
1241                     int n;
1242                     int thissme;
1243                     int bestsme = INT_MAX;
1244                     MV  temp_mv;
1245
1246                     // Is the best so far sufficiently good that we cant justify doing and new motion search.
1247                     if (best_label_rd < label_mv_thresh)
1248                         break;
1249
1250                     {
1251                         int sadpb = x->sadperbit4;
1252
1253                         if (cpi->sf.search_method == HEX)
1254                             bestsme = vp8_hex_search(x, c, e, best_ref_mv, &mode_mv[NEW4X4], step_param, sadpb/*x->errorperbit*/, &num00, v_fn_ptr.vf, v_fn_ptr.sdf, x->mvsadcost, mvcost);
1255                         else
1256                         {
1257                             bestsme = cpi->diamond_search_sad(x, c, e, best_ref_mv, &mode_mv[NEW4X4], step_param, sadpb / 2/*x->errorperbit*/, &num00, &v_fn_ptr, x->mvsadcost, mvcost);
1258
1259                             n = num00;
1260                             num00 = 0;
1261
1262                             while (n < further_steps)
1263                             {
1264                                 n++;
1265
1266                                 if (num00)
1267                                     num00--;
1268                                 else
1269                                 {
1270                                     thissme = cpi->diamond_search_sad(x, c, e, best_ref_mv, &temp_mv, step_param + n, sadpb / 2/*x->errorperbit*/, &num00, &v_fn_ptr, x->mvsadcost, mvcost);
1271
1272                                     if (thissme < bestsme)
1273                                     {
1274                                         bestsme = thissme;
1275                                         mode_mv[NEW4X4].row = temp_mv.row;
1276                                         mode_mv[NEW4X4].col = temp_mv.col;
1277                                     }
1278                                 }
1279                             }
1280                         }
1281
1282                         // Should we do a full search (best quality only)
1283                         if ((compressor_speed == 0) && (bestsme >> sseshift) > 4000)
1284                         {
1285                             thissme = cpi->full_search_sad(x, c, e, best_ref_mv, sadpb / 4, 16, &v_fn_ptr, x->mvcost, x->mvsadcost);
1286
1287                             if (thissme < bestsme)
1288                             {
1289                                 bestsme = thissme;
1290                                 mode_mv[NEW4X4] = e->bmi.mv.as_mv;
1291                             }
1292                             else
1293                             {
1294                                 // The full search result is actually worse so re-instate the previous best vector
1295                                 e->bmi.mv.as_mv = mode_mv[NEW4X4];
1296                             }
1297                         }
1298                     }
1299
1300                     if (bestsme < INT_MAX)
1301                     {
1302                         if (!fullpixel)
1303                             cpi->find_fractional_mv_step(x, c, e, &mode_mv[NEW4X4], best_ref_mv, x->errorperbit / 2, v_fn_ptr.svf, v_fn_ptr.vf, mvcost);
1304                         else
1305                             vp8_skip_fractional_mv_step(x, c, e, &mode_mv[NEW4X4], best_ref_mv, x->errorperbit, v_fn_ptr.svf, v_fn_ptr.vf, mvcost);
1306                     }
1307                 }
1308
1309                 rate = labels2mode(x, labels, i, this_mode, &mode_mv[this_mode], best_ref_mv, mvcost);
1310
1311                 // Trap vectors that reach beyond the UMV borders
1312                 if (((mode_mv[this_mode].row >> 3) < x->mv_row_min) || ((mode_mv[this_mode].row >> 3) > x->mv_row_max) ||
1313                     ((mode_mv[this_mode].col >> 3) < x->mv_col_min) || ((mode_mv[this_mode].col >> 3) > x->mv_col_max))
1314                 {
1315                     continue;
1316                 }
1317
1318                 distortion = vp8_encode_inter_mb_segment(x, labels, i, IF_RTCD(&cpi->rtcd.encodemb)) / 4;
1319
1320                 labelyrate = rdcost_mbsegment_y(x, labels, i, &ts);
1321                 rate += labelyrate;
1322
1323                 this_rd = RDFUNC(x->rdmult, x->rddiv, rate, distortion, cpi->target_bits_per_mb);
1324
1325                 if (this_rd < best_label_rd)
1326                 {
1327                     sbr = rate;
1328                     sbd = distortion;
1329                     bestlabelyrate = labelyrate;
1330                     mode_selected = this_mode;
1331                     best_label_rd = this_rd;
1332                     vp8_setup_temp_context(&tb, &ts.a[0], &ts.l[0], 4);
1333
1334                 }
1335             }
1336
1337             vp8_setup_temp_context(&t, &tb.a[0], &tb.l[0], 4);
1338
1339             labels2mode(x, labels, i, mode_selected, &mode_mv[mode_selected], best_ref_mv, mvcost);
1340
1341             br += sbr;
1342             bd += sbd;
1343             segmentyrate += bestlabelyrate;
1344             this_segment_rd += best_label_rd;
1345
1346             if ((this_segment_rd > best_rd) || (this_segment_rd > best_segment_rd))
1347                 break;
1348         }
1349
1350         if ((this_segment_rd <= best_rd) && (this_segment_rd < best_segment_rd))
1351         {
1352             bsr = br;
1353             bsd = bd;
1354             bestsegmentyrate = segmentyrate;
1355             best_segment_rd = this_segment_rd;
1356             best_seg = segmentation;
1357
1358             // store everything needed to come back to this!!
1359             for (i = 0; i < 16; i++)
1360             {
1361                 BLOCKD *bd = &x->e_mbd.block[i];
1362
1363                 bmvs[i] = bd->bmi.mv.as_mv;
1364                 bmodes[i] = bd->bmi.mode;
1365                 beobs[i] = bd->eob;
1366             }
1367         }
1368     }
1369
1370     // set it to the best
1371     for (i = 0; i < 16; i++)
1372     {
1373         BLOCKD *bd = &x->e_mbd.block[i];
1374
1375         bd->bmi.mv.as_mv = bmvs[i];
1376         bd->bmi.mode = bmodes[i];
1377         bd->eob = beobs[i];
1378     }
1379
1380     // Trap cases where the best split mode has all vectors coded 0,0 (or all the same)
1381     if (FALSE)
1382     {
1383         int allsame = 1;
1384
1385         for (i = 1; i < 16; i++)
1386         {
1387             if ((bmvs[i].col != bmvs[i-1].col) || (bmvs[i].row != bmvs[i-1].row))
1388             {
1389                 allsame = 0;
1390                 break;
1391             }
1392         }
1393
1394         if (allsame)
1395         {
1396             best_segment_rd = INT_MAX;
1397         }
1398     }
1399
1400     *returntotrate = bsr;
1401     *returndistortion = bsd;
1402     *returnyrate = bestsegmentyrate;
1403
1404
1405
1406     // save partitions
1407     labels = vp8_mbsplits[best_seg];
1408     x->e_mbd.mbmi.partitioning = best_seg;
1409     x->e_mbd.mbmi.partition_count = vp8_count_labels(labels);
1410
1411     for (i = 0; i < x->e_mbd.mbmi.partition_count; i++)
1412     {
1413         int j;
1414
1415         for (j = 0; j < 16; j++)
1416         {
1417             if (labels[j] == i)
1418                 break;
1419         }
1420
1421         x->e_mbd.mbmi.partition_bmi[i].mode = x->e_mbd.block[j].bmi.mode;
1422         x->e_mbd.mbmi.partition_bmi[i].mv.as_mv = x->e_mbd.block[j].bmi.mv.as_mv;
1423     }
1424
1425     return best_segment_rd;
1426 }
1427
1428
1429 int vp8_rd_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset, int recon_uvoffset, int *returnrate, int *returndistortion, int *returnintra)
1430 {
1431     BLOCK *b = &x->block[0];
1432     BLOCKD *d = &x->e_mbd.block[0];
1433     MACROBLOCKD *xd = &x->e_mbd;
1434     B_MODE_INFO best_bmodes[16];
1435     MB_MODE_INFO best_mbmode;
1436     MV best_ref_mv;
1437     MV mode_mv[MB_MODE_COUNT];
1438     MB_PREDICTION_MODE this_mode;
1439     int num00;
1440     int best_mode_index = 0;
1441
1442     int i;
1443     int mode_index;
1444     int mdcounts[4];
1445     int rate;
1446     int distortion;
1447     int best_rd = INT_MAX; // 1 << 30;
1448     int ref_frame_cost[MAX_REF_FRAMES];
1449     int rate2, distortion2;
1450     int uv_intra_rate, uv_intra_distortion, uv_intra_rate_tokenonly;
1451     int rate_y, UNINITIALIZED_IS_SAFE(rate_uv);
1452
1453     //int all_rds[MAX_MODES];        // Experimental debug code.
1454     //int all_rates[MAX_MODES];
1455     //int all_dist[MAX_MODES];
1456     //int intermodecost[MAX_MODES];
1457
1458     MB_PREDICTION_MODE uv_intra_mode;
1459     int sse;
1460     int sum;
1461     int uvintra_eob = 0;
1462     int tteob = 0;
1463     int force_no_skip = 0;
1464
1465     *returnintra = INT_MAX;
1466
1467     vpx_memset(&best_mbmode, 0, sizeof(best_mbmode)); // clean
1468
1469     cpi->mbs_tested_so_far++;          // Count of the number of MBs tested so far this frame
1470
1471     x->skip = 0;
1472
1473     ref_frame_cost[INTRA_FRAME]   = vp8_cost_zero(cpi->prob_intra_coded);
1474
1475     // Experimental code
1476     // Adjust the RD multiplier based on the best case distortion we saw in the most recently coded mb
1477     //if ( (cpi->last_mb_distortion) > 0 && (cpi->target_bits_per_mb > 0) )
1478     /*{
1479         int tmprdmult;
1480
1481         //tmprdmult = (cpi->last_mb_distortion * 256) / ((cpi->av_per_frame_bandwidth*256)/cpi->common.MBs);
1482         tmprdmult = (cpi->last_mb_distortion * 256) / cpi->target_bits_per_mb;
1483         //tmprdmult = tmprdmult;
1484
1485         //if ( tmprdmult > cpi->RDMULT * 2 )
1486         //  tmprdmult = cpi->RDMULT * 2;
1487         //else if ( tmprdmult < cpi->RDMULT / 2 )
1488         //  tmprdmult = cpi->RDMULT / 2;
1489
1490         //tmprdmult = (tmprdmult < 25) ? 25 : tmprdmult;
1491
1492         //x->rdmult = tmprdmult;
1493
1494     }*/
1495
1496     // Special case treatment when GF and ARF are not sensible options for reference
1497     if (cpi->ref_frame_flags == VP8_LAST_FLAG)
1498     {
1499         ref_frame_cost[LAST_FRAME]    = vp8_cost_one(cpi->prob_intra_coded)
1500                                         + vp8_cost_zero(255);
1501         ref_frame_cost[GOLDEN_FRAME]  = vp8_cost_one(cpi->prob_intra_coded)
1502                                         + vp8_cost_one(255)
1503                                         + vp8_cost_zero(128);
1504         ref_frame_cost[ALTREF_FRAME]  = vp8_cost_one(cpi->prob_intra_coded)
1505                                         + vp8_cost_one(255)
1506                                         + vp8_cost_one(128);
1507     }
1508     else
1509     {
1510         ref_frame_cost[LAST_FRAME]    = vp8_cost_one(cpi->prob_intra_coded)
1511                                         + vp8_cost_zero(cpi->prob_last_coded);
1512         ref_frame_cost[GOLDEN_FRAME]  = vp8_cost_one(cpi->prob_intra_coded)
1513                                         + vp8_cost_one(cpi->prob_last_coded)
1514                                         + vp8_cost_zero(cpi->prob_gf_coded);
1515         ref_frame_cost[ALTREF_FRAME]  = vp8_cost_one(cpi->prob_intra_coded)
1516                                         + vp8_cost_one(cpi->prob_last_coded)
1517                                         + vp8_cost_one(cpi->prob_gf_coded);
1518     }
1519
1520     vpx_memset(mode_mv, 0, sizeof(mode_mv));
1521
1522     x->e_mbd.mbmi.ref_frame = INTRA_FRAME;
1523     vp8_rd_pick_intra_mbuv_mode(cpi, x, &uv_intra_rate, &uv_intra_rate_tokenonly, &uv_intra_distortion);
1524     uv_intra_mode = x->e_mbd.mbmi.uv_mode;
1525     {
1526         uvintra_eob = 0;
1527
1528         for (i = 16; i < 24; i++)
1529             uvintra_eob += x->e_mbd.block[i].eob;
1530     }
1531
1532     for (mode_index = 0; mode_index < MAX_MODES; mode_index++)
1533     {
1534         int frame_cost;
1535         int this_rd = INT_MAX;
1536         int lf_or_gf = 0;           // Lat Frame (01) or gf/arf (1)
1537         int disable_skip = 0;
1538
1539         force_no_skip = 0;
1540
1541         // Experimental debug code.
1542         // Record of rd values recorded for this MB. -1 indicates not measured
1543         //all_rds[mode_index] = -1;
1544         //all_rates[mode_index] = -1;
1545         //all_dist[mode_index] = -1;
1546         //intermodecost[mode_index] = -1;
1547
1548         // Test best rd so far against threshold for trying this mode.
1549         if (best_rd <= cpi->rd_threshes[mode_index])
1550             continue;
1551
1552
1553
1554         // These variables hold are rolling total cost and distortion for this mode
1555         rate2 = 0;
1556         distortion2 = 0;
1557
1558         // Where skip is allowable add in the default per mb cost for the no skip case.
1559         // where we then decide to skip we have to delete this and replace it with the
1560         // cost of signallying a skip
1561         if (cpi->common.mb_no_coeff_skip)
1562         {
1563             rate2 += vp8_cost_bit(cpi->prob_skip_false, 0);
1564         }
1565
1566         this_mode = vp8_mode_order[mode_index];
1567
1568         x->e_mbd.mbmi.mode = this_mode;
1569         x->e_mbd.mbmi.uv_mode = DC_PRED;
1570         x->e_mbd.mbmi.ref_frame = vp8_ref_frame_order[mode_index];
1571
1572         //Only consider ZEROMV/ALTREF_FRAME for alt ref frame.
1573         if (cpi->is_src_frame_alt_ref)
1574         {
1575             if (this_mode != ZEROMV || x->e_mbd.mbmi.ref_frame != ALTREF_FRAME)
1576                 continue;
1577         }
1578
1579         if (x->e_mbd.mbmi.ref_frame == LAST_FRAME)
1580         {
1581             YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
1582
1583             if (!(cpi->ref_frame_flags & VP8_LAST_FLAG))
1584                 continue;
1585
1586             lf_or_gf = 0;  // Local last frame vs Golden frame flag
1587
1588             // Set up pointers for this macro block into the previous frame recon buffer
1589             x->e_mbd.pre.y_buffer = lst_yv12->y_buffer + recon_yoffset;
1590             x->e_mbd.pre.u_buffer = lst_yv12->u_buffer + recon_uvoffset;
1591             x->e_mbd.pre.v_buffer = lst_yv12->v_buffer + recon_uvoffset;
1592         }
1593         else if (x->e_mbd.mbmi.ref_frame == GOLDEN_FRAME)
1594         {
1595             YV12_BUFFER_CONFIG *gld_yv12 = &cpi->common.yv12_fb[cpi->common.gld_fb_idx];
1596
1597             // not supposed to reference gold frame
1598             if (!(cpi->ref_frame_flags & VP8_GOLD_FLAG))
1599                 continue;
1600
1601             lf_or_gf = 1;  // Local last frame vs Golden frame flag
1602
1603             // Set up pointers for this macro block into the previous frame recon buffer
1604             x->e_mbd.pre.y_buffer = gld_yv12->y_buffer + recon_yoffset;
1605             x->e_mbd.pre.u_buffer = gld_yv12->u_buffer + recon_uvoffset;
1606             x->e_mbd.pre.v_buffer = gld_yv12->v_buffer + recon_uvoffset;
1607         }
1608         else if (x->e_mbd.mbmi.ref_frame == ALTREF_FRAME)
1609         {
1610             YV12_BUFFER_CONFIG *alt_yv12 = &cpi->common.yv12_fb[cpi->common.alt_fb_idx];
1611
1612             // not supposed to reference alt ref frame
1613             if (!(cpi->ref_frame_flags & VP8_ALT_FLAG))
1614                 continue;
1615
1616             //if ( !cpi->source_alt_ref_active )
1617             //  continue;
1618
1619             lf_or_gf = 1;  // Local last frame vs Golden frame flag
1620
1621             // Set up pointers for this macro block into the previous frame recon buffer
1622             x->e_mbd.pre.y_buffer = alt_yv12->y_buffer + recon_yoffset;
1623             x->e_mbd.pre.u_buffer = alt_yv12->u_buffer + recon_uvoffset;
1624             x->e_mbd.pre.v_buffer = alt_yv12->v_buffer + recon_uvoffset;
1625         }
1626
1627         vp8_find_near_mvs(&x->e_mbd,
1628                           x->e_mbd.mode_info_context,
1629                           &mode_mv[NEARESTMV], &mode_mv[NEARMV], &best_ref_mv,
1630                           mdcounts, x->e_mbd.mbmi.ref_frame, cpi->common.ref_frame_sign_bias);
1631
1632
1633         // Estimate the reference frame signaling cost and add it to the rolling cost variable.
1634         frame_cost = ref_frame_cost[x->e_mbd.mbmi.ref_frame];
1635         rate2 += frame_cost;
1636
1637         if (this_mode <= B_PRED)
1638         {
1639             for (i = 0; i < 16; i++)
1640             {
1641                 vpx_memset(&x->e_mbd.block[i].bmi, 0, sizeof(B_MODE_INFO));
1642             }
1643         }
1644
1645         // Check to see if the testing frequency for this mode is at its max
1646         // If so then prevent it from being tested and increase the threshold for its testing
1647         if (cpi->mode_test_hit_counts[mode_index] && (cpi->mode_check_freq[mode_index] > 1))
1648         {
1649             if (cpi->mbs_tested_so_far  <= cpi->mode_check_freq[mode_index] * cpi->mode_test_hit_counts[mode_index])
1650             {
1651                 // Increase the threshold for coding this mode to make it less likely to be chosen
1652                 cpi->rd_thresh_mult[mode_index] += 4;
1653
1654                 if (cpi->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
1655                     cpi->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
1656
1657                 cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
1658
1659                 continue;
1660             }
1661         }
1662
1663         // 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
1664         cpi->mode_test_hit_counts[mode_index] ++;
1665
1666         // Experimental code. Special case for gf and arf zeromv modes. Increase zbin size to supress noise
1667         if (cpi->zbin_mode_boost_enabled)
1668         {
1669             if ((vp8_mode_order[mode_index] == ZEROMV) && (vp8_ref_frame_order[mode_index] != LAST_FRAME))
1670                 cpi->zbin_mode_boost = GF_ZEROMV_ZBIN_BOOST;
1671             else
1672                 cpi->zbin_mode_boost = 0;
1673
1674             vp8cx_mb_init_quantizer(cpi, x);
1675         }
1676
1677         switch (this_mode)
1678         {
1679         case B_PRED:
1680
1681             // Note the rate value returned here includes the cost of coding the BPRED mode : x->mbmode_cost[x->e_mbd.frame_type][BPRED];
1682             vp8_rd_pick_intra4x4mby_modes(cpi, x, &rate, &rate_y, &distortion);
1683             rate2 += rate;
1684             //rate_y = rate;
1685             distortion2 += distortion;
1686             rate2 += uv_intra_rate;
1687             rate_uv = uv_intra_rate_tokenonly;
1688             distortion2 += uv_intra_distortion;
1689             break;
1690
1691         case SPLITMV:
1692         {
1693             int frame_cost_rd = RDFUNC(x->rdmult, x->rddiv, frame_cost, 0, cpi->target_bits_per_mb);
1694             int saved_rate = rate2;
1695
1696             // vp8_rd_pick_best_mbsegmentation looks only at Y and does not account for frame_cost.
1697             // (best_rd - frame_cost_rd) is thus a conservative breakout number.
1698             int breakout_rd = best_rd - frame_cost_rd;
1699             int tmp_rd;
1700
1701             if (x->e_mbd.mbmi.ref_frame == LAST_FRAME)
1702                 tmp_rd = vp8_rd_pick_best_mbsegmentation(cpi, x, &best_ref_mv, breakout_rd, mdcounts, &rate, &rate_y, &distortion, cpi->compressor_speed, x->mvcost, cpi->rd_threshes[THR_NEWMV], cpi->common.full_pixel) ;
1703             else if (x->e_mbd.mbmi.ref_frame == GOLDEN_FRAME)
1704                 tmp_rd = vp8_rd_pick_best_mbsegmentation(cpi, x, &best_ref_mv, breakout_rd, mdcounts, &rate, &rate_y, &distortion, cpi->compressor_speed, x->mvcost, cpi->rd_threshes[THR_NEWG], cpi->common.full_pixel) ;
1705             else
1706                 tmp_rd = vp8_rd_pick_best_mbsegmentation(cpi, x, &best_ref_mv, breakout_rd, mdcounts, &rate, &rate_y, &distortion, cpi->compressor_speed, x->mvcost, cpi->rd_threshes[THR_NEWA], cpi->common.full_pixel) ;
1707
1708             rate2 += rate;
1709             distortion2 += distortion;
1710
1711             // If even the 'Y' rd value of split is higher than best so far then dont bother looking at UV
1712             if (tmp_rd < breakout_rd)
1713             {
1714                 // Now work out UV cost and add it in
1715                 vp8_rd_inter_uv(cpi, x, &rate, &distortion, cpi->common.full_pixel);
1716                 rate2 += rate;
1717                 rate_uv = rate;
1718                 distortion2 += distortion;
1719
1720             }
1721             else
1722             {
1723                 this_rd = INT_MAX;
1724                 disable_skip = 1;
1725             }
1726
1727             // Trap cases where the best split mode has all vectors coded 0,0 (or all the same)
1728             if (0)
1729             {
1730                 int allsame = 1;
1731
1732                 for (i = 1; i < 16; i++)
1733                 {
1734                     BLOCKD *bd = &x->e_mbd.block[i];
1735
1736                     if (bd->bmi.mv.as_int != x->e_mbd.block[0].bmi.mv.as_int)   //(bmvs[i].col != bmvs[i-1].col) || (bmvs[i].row != bmvs[i-1].row ) )
1737                     {
1738                         allsame = 0;
1739                         break;
1740                     }
1741                 }
1742
1743                 if (allsame)
1744                 {
1745                     // reset mode and mv and jump to newmv
1746                     this_mode = NEWMV;
1747                     distortion2 = 0;
1748                     rate2 = saved_rate;
1749                     mode_mv[NEWMV].row = x->e_mbd.block[0].bmi.mv.as_mv.row;
1750                     mode_mv[NEWMV].col = x->e_mbd.block[0].bmi.mv.as_mv.col;
1751                     rate2 += vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv, x->mvcost, 96);
1752                     goto mv_selected;
1753                 }
1754             }
1755
1756             // trap cases where the 8x8s can be promoted to 8x16s or 16x8s
1757             if (0)//x->e_mbd.mbmi.partition_count == 4)
1758             {
1759
1760                 if (x->e_mbd.mbmi.partition_bmi[0].mv.as_int == x->e_mbd.mbmi.partition_bmi[1].mv.as_int
1761                     && x->e_mbd.mbmi.partition_bmi[2].mv.as_int == x->e_mbd.mbmi.partition_bmi[3].mv.as_int)
1762                 {
1763                     const int *labels = vp8_mbsplits[2];
1764                     x->e_mbd.mbmi.partitioning = 0;
1765                     rate -= vp8_cost_token(vp8_mbsplit_tree, vp8_mbsplit_probs, vp8_mbsplit_encodings + 2);
1766                     rate += vp8_cost_token(vp8_mbsplit_tree, vp8_mbsplit_probs, vp8_mbsplit_encodings);
1767                     //rate -=  x->inter_bmode_costs[  x->e_mbd.mbmi.partition_bmi[1]];
1768                     //rate -=  x->inter_bmode_costs[  x->e_mbd.mbmi.partition_bmi[3]];
1769                     x->e_mbd.mbmi.partition_bmi[1] = x->e_mbd.mbmi.partition_bmi[2];
1770                 }
1771             }
1772
1773         }
1774         break;
1775         case DC_PRED:
1776         case V_PRED:
1777         case H_PRED:
1778         case TM_PRED:
1779             x->e_mbd.mbmi.ref_frame = INTRA_FRAME;
1780             vp8_build_intra_predictors_mby_ptr(&x->e_mbd);
1781             {
1782                 macro_block_yrd(x, &rate, &distortion, IF_RTCD(&cpi->rtcd.encodemb)) ;
1783                 rate2 += rate;
1784                 rate_y = rate;
1785                 distortion2 += distortion;
1786                 rate2 += x->mbmode_cost[x->e_mbd.frame_type][x->e_mbd.mbmi.mode];
1787                 rate2 += uv_intra_rate;
1788                 rate_uv = uv_intra_rate_tokenonly;
1789                 distortion2 += uv_intra_distortion;
1790             }
1791             break;
1792
1793         case NEWMV:
1794
1795             // Decrement full search counter
1796             if (cpi->check_freq[lf_or_gf] > 0)
1797                 cpi->check_freq[lf_or_gf] --;
1798
1799             {
1800                 int thissme;
1801                 int bestsme = INT_MAX;
1802                 int step_param = cpi->sf.first_step;
1803                 int search_range;
1804                 int further_steps;
1805                 int n;
1806
1807                 // Work out how long a search we should do
1808                 search_range = MAXF(abs(best_ref_mv.col), abs(best_ref_mv.row)) >> 3;
1809
1810                 if (search_range >= x->vector_range)
1811                     x->vector_range = search_range;
1812                 else if (x->vector_range > cpi->sf.min_fs_radius)
1813                     x->vector_range--;
1814
1815                 // Initial step/diamond search
1816                 {
1817                     int sadpb = x->sadperbit16;
1818
1819                     if (cpi->sf.search_method == HEX)
1820                     {
1821                         bestsme = vp8_hex_search(x, b, d, &best_ref_mv, &d->bmi.mv.as_mv, step_param, sadpb/*x->errorperbit*/, &num00, cpi->fn_ptr.vf, cpi->fn_ptr.sdf, x->mvsadcost, x->mvcost);
1822                         mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
1823                         mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
1824                     }
1825                     else
1826                     {
1827                         bestsme = cpi->diamond_search_sad(x, b, d, &best_ref_mv, &d->bmi.mv.as_mv, step_param, sadpb / 2/*x->errorperbit*/, &num00, &cpi->fn_ptr, x->mvsadcost, x->mvcost); //sadpb < 9
1828                         mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
1829                         mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
1830
1831                         // Further step/diamond searches as necessary
1832                         n = 0;
1833                         further_steps = (cpi->sf.max_step_search_steps - 1) - step_param;
1834
1835                         n = num00;
1836                         num00 = 0;
1837
1838                         while (n < further_steps)
1839                         {
1840                             n++;
1841
1842                             if (num00)
1843                                 num00--;
1844                             else
1845                             {
1846                                 thissme = cpi->diamond_search_sad(x, b, d, &best_ref_mv, &d->bmi.mv.as_mv, step_param + n, sadpb / 4/*x->errorperbit*/, &num00, &cpi->fn_ptr, x->mvsadcost, x->mvcost); //sadpb = 9
1847
1848                                 if (thissme < bestsme)
1849                                 {
1850                                     bestsme = thissme;
1851                                     mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
1852                                     mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
1853                                 }
1854                                 else
1855                                 {
1856                                     d->bmi.mv.as_mv.row = mode_mv[NEWMV].row;
1857                                     d->bmi.mv.as_mv.col = mode_mv[NEWMV].col;
1858                                 }
1859                             }
1860                         }
1861                     }
1862
1863                 }
1864
1865                 // Should we do a full search
1866                 if (!cpi->check_freq[lf_or_gf] || cpi->do_full[lf_or_gf])
1867                 {
1868                     int thissme;
1869                     int full_flag_thresh = 0;
1870
1871                     // Update x->vector_range based on best vector found in step search
1872                     search_range = MAXF(abs(d->bmi.mv.as_mv.row), abs(d->bmi.mv.as_mv.col));
1873
1874                     if (search_range > x->vector_range)
1875                         x->vector_range = search_range;
1876                     else
1877                         search_range = x->vector_range;
1878
1879                     // Apply limits
1880                     search_range = (search_range > cpi->sf.max_fs_radius) ? cpi->sf.max_fs_radius : search_range;
1881                     {
1882                         int sadpb = x->sadperbit16 >> 2;
1883                         thissme = cpi->full_search_sad(x, b, d, &best_ref_mv, sadpb, search_range, &cpi->fn_ptr, x->mvcost, x->mvsadcost);
1884                     }
1885
1886                     // Barrier threshold to initiating full search
1887                     // full_flag_thresh = 10 + (thissme >> 7);
1888                     if ((thissme + full_flag_thresh) < bestsme)
1889                     {
1890                         cpi->do_full[lf_or_gf] ++;
1891                         bestsme = thissme;
1892                     }
1893                     else if (thissme < bestsme)
1894                         bestsme = thissme;
1895                     else
1896                     {
1897                         cpi->do_full[lf_or_gf] = cpi->do_full[lf_or_gf] >> 1;
1898                         cpi->check_freq[lf_or_gf] = cpi->sf.full_freq[lf_or_gf];
1899
1900                         // The full search result is actually worse so re-instate the previous best vector
1901                         d->bmi.mv.as_mv.row = mode_mv[NEWMV].row;
1902                         d->bmi.mv.as_mv.col = mode_mv[NEWMV].col;
1903                     }
1904                 }
1905
1906                 if (bestsme < INT_MAX)
1907                     // cpi->find_fractional_mv_step(x,b,d,&d->bmi.mv.as_mv,&best_ref_mv,x->errorperbit/2,cpi->fn_ptr.svf,cpi->fn_ptr.vf,x->mvcost);  // normal mvc=11
1908                     cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv.as_mv, &best_ref_mv, x->errorperbit / 4, cpi->fn_ptr.svf, cpi->fn_ptr.vf, x->mvcost);
1909
1910                 mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
1911                 mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
1912
1913                 // Add the new motion vector cost to our rolling cost variable
1914                 rate2 += vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv, x->mvcost, 96);
1915
1916             }
1917
1918         case NEARESTMV:
1919         case NEARMV:
1920
1921             // Clip "next_nearest" so that it does not extend to far out of image
1922             if (mode_mv[this_mode].col < (xd->mb_to_left_edge - LEFT_TOP_MARGIN))
1923                 mode_mv[this_mode].col = xd->mb_to_left_edge - LEFT_TOP_MARGIN;
1924             else if (mode_mv[this_mode].col > xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN)
1925                 mode_mv[this_mode].col = xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN;
1926
1927             if (mode_mv[this_mode].row < (xd->mb_to_top_edge - LEFT_TOP_MARGIN))
1928                 mode_mv[this_mode].row = xd->mb_to_top_edge - LEFT_TOP_MARGIN;
1929             else if (mode_mv[this_mode].row > xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN)
1930                 mode_mv[this_mode].row = xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN;
1931
1932             // Do not bother proceeding if the vector (from newmv,nearest or near) is 0,0 as this should then be coded using the zeromv mode.
1933             if (((this_mode == NEARMV) || (this_mode == NEARESTMV)) &&
1934                 ((mode_mv[this_mode].row == 0) && (mode_mv[this_mode].col == 0)))
1935                 continue;
1936
1937         case ZEROMV:
1938
1939         mv_selected:
1940
1941             // Trap vectors that reach beyond the UMV borders
1942             // Note that ALL New MV, Nearest MV Near MV and Zero MV code drops through to this point
1943             // because of the lack of break statements in the previous two cases.
1944             if (((mode_mv[this_mode].row >> 3) < x->mv_row_min) || ((mode_mv[this_mode].row >> 3) > x->mv_row_max) ||
1945                 ((mode_mv[this_mode].col >> 3) < x->mv_col_min) || ((mode_mv[this_mode].col >> 3) > x->mv_col_max))
1946                 continue;
1947
1948             vp8_set_mbmode_and_mvs(x, this_mode, &mode_mv[this_mode]);
1949             vp8_build_inter_predictors_mby(&x->e_mbd);
1950             VARIANCE_INVOKE(&cpi->rtcd.variance, get16x16var)(x->src.y_buffer, x->src.y_stride, x->e_mbd.predictor, 16, (unsigned int *)(&sse), &sum);
1951
1952             if (cpi->active_map_enabled && x->active_ptr[0] == 0)
1953             {
1954                 x->skip = 1;
1955             }
1956             else if (sse < x->encode_breakout)
1957             {
1958                 // Check u and v to make sure skip is ok
1959                 int sse2 = 0;
1960
1961                 sse2 = VP8_UVSSE(x, IF_RTCD(&cpi->rtcd.variance));
1962
1963                 if (sse2 * 2 < x->encode_breakout)
1964                 {
1965                     x->skip = 1;
1966                     distortion2 = sse;
1967                     rate2 = 500;
1968
1969                     disable_skip = 1;    // We have no real rate data so trying to adjust for rate_y and rate_uv below will cause problems.
1970                     this_rd = RDFUNC(x->rdmult, x->rddiv, rate2, distortion2, cpi->target_bits_per_mb);
1971
1972                     break;              // (PGW) Move break here from below - for now at least
1973                 }
1974                 else
1975                     x->skip = 0;
1976             }
1977
1978             //intermodecost[mode_index] = vp8_cost_mv_ref(this_mode, mdcounts);   // Experimental debug code
1979
1980             // Add in the Mv/mode cost
1981             rate2 += vp8_cost_mv_ref(this_mode, mdcounts);
1982
1983             // Y cost and distortion
1984             macro_block_yrd(x, &rate, &distortion, IF_RTCD(&cpi->rtcd.encodemb));
1985             rate2 += rate;
1986             rate_y = rate;
1987             distortion2 += distortion;
1988
1989             // UV cost and distortion
1990             vp8_rd_inter_uv(cpi, x, &rate, &distortion, cpi->common.full_pixel);
1991             rate2 += rate;
1992             rate_uv = rate;
1993             distortion2 += distortion;
1994             break;
1995
1996         default:
1997             break;
1998         }
1999
2000         if (!disable_skip)
2001         {
2002             // Test for the condition where skip block will be activated because there are no non zero coefficients and make any necessary adjustment for rate
2003             if (cpi->common.mb_no_coeff_skip)
2004             {
2005                 tteob = 0;
2006
2007                 for (i = 0; i <= 24; i++)
2008                 {
2009                     tteob += x->e_mbd.block[i].eob;
2010                 }
2011
2012                 if (tteob == 0)
2013                 {
2014 #if 1
2015                     rate2 -= (rate_y + rate_uv);
2016
2017                     // Back out no skip flag costing and add in skip flag costing
2018                     if (cpi->prob_skip_false)
2019                     {
2020                         rate2 += vp8_cost_bit(cpi->prob_skip_false, 1);
2021                         rate2 -= vp8_cost_bit(cpi->prob_skip_false, 0);
2022                     }
2023
2024 #else
2025                     int rateuseskip;
2026                     int ratenotuseskip;
2027
2028
2029
2030                     ratenotuseskip = rate_y + rate_uv + vp8_cost_bit(cpi->prob_skip_false, 0);
2031                     rateuseskip    = vp8_cost_bit(cpi->prob_skip_false, 1);
2032
2033                     if (1) // rateuseskip<ratenotuseskip)
2034                     {
2035                         rate2 -= ratenotuseskip;
2036                         rate2 += rateuseskip;
2037                         force_no_skip = 0;
2038                     }
2039                     else
2040                     {
2041                         force_no_skip = 1;
2042                     }
2043
2044 #endif
2045                 }
2046
2047 #if             0
2048                 else
2049                 {
2050                     int rateuseskip;
2051                     int ratenotuseskip;
2052                     int maxdistortion;
2053                     int minrate;
2054                     int skip_rd;
2055
2056                     // distortion when no coeff is encoded
2057                     maxdistortion = macro_block_max_error(x);
2058
2059                     ratenotuseskip = rate_y + rate_uv + vp8_cost_bit(cpi->prob_skip_false, 0);
2060                     rateuseskip    = vp8_cost_bit(cpi->prob_skip_false, 1);
2061
2062                     minrate         = rateuseskip - ratenotuseskip;
2063
2064                     skip_rd = RDFUNC(x->rdmult, x->rddiv, minrate, maxdistortion - distortion2, cpi->target_bits_per_mb);
2065
2066                     if (skip_rd + 50 < 0 && x->e_mbd.mbmi.ref_frame != INTRA_FRAME && rate_y + rate_uv < 4000)
2067                     {
2068                         force_no_skip = 1;
2069                         rate2       = rate2 + rateuseskip - ratenotuseskip;
2070                         distortion2 =  maxdistortion;
2071                     }
2072                     else
2073                     {
2074                         force_no_skip = 0;
2075                     }
2076
2077                 }
2078
2079 #endif
2080
2081             }
2082
2083             // Calculate the final RD estimate for this mode
2084             this_rd = RDFUNC(x->rdmult, x->rddiv, rate2, distortion2, cpi->target_bits_per_mb);
2085         }
2086
2087         // Experimental debug code.
2088         //all_rds[mode_index] = this_rd;
2089         //all_rates[mode_index] = rate2;
2090         //all_dist[mode_index] = distortion2;
2091
2092         if ((x->e_mbd.mbmi.ref_frame == INTRA_FRAME)  && (this_rd < *returnintra))
2093         {
2094             *returnintra = this_rd ;
2095         }
2096
2097         // Did this mode help.. i.i is it the new best mode
2098         if (this_rd < best_rd || x->skip)
2099         {
2100             // Note index of best mode so far
2101             best_mode_index = mode_index;
2102             x->e_mbd.mbmi.force_no_skip = force_no_skip;
2103
2104             if (this_mode <= B_PRED)
2105             {
2106                 x->e_mbd.mbmi.uv_mode = uv_intra_mode;
2107             }
2108
2109             *returnrate = rate2;
2110             *returndistortion = distortion2;
2111             best_rd = this_rd;
2112             vpx_memcpy(&best_mbmode, &x->e_mbd.mbmi, sizeof(MB_MODE_INFO));
2113
2114             for (i = 0; i < 16; i++)
2115             {
2116                 vpx_memcpy(&best_bmodes[i], &x->e_mbd.block[i].bmi, sizeof(B_MODE_INFO));
2117             }
2118
2119             // Testing this mode gave rise to an improvement in best error score. Lower threshold a bit for next time
2120             cpi->rd_thresh_mult[mode_index] = (cpi->rd_thresh_mult[mode_index] >= (MIN_THRESHMULT + 2)) ? cpi->rd_thresh_mult[mode_index] - 2 : MIN_THRESHMULT;
2121             cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
2122         }
2123
2124         // If the mode did not help improve the best error case then raise the threshold for testing that mode next time around.
2125         else
2126         {
2127             cpi->rd_thresh_mult[mode_index] += 4;
2128
2129             if (cpi->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
2130                 cpi->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
2131
2132             cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
2133         }
2134
2135         if (x->skip)
2136             break;
2137     }
2138
2139     // Reduce the activation RD thresholds for the best choice mode
2140     if ((cpi->rd_baseline_thresh[best_mode_index] > 0) && (cpi->rd_baseline_thresh[best_mode_index] < (INT_MAX >> 2)))
2141     {
2142         int best_adjustment = (cpi->rd_thresh_mult[best_mode_index] >> 2);
2143
2144         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;
2145         cpi->rd_threshes[best_mode_index] = (cpi->rd_baseline_thresh[best_mode_index] >> 7) * cpi->rd_thresh_mult[best_mode_index];
2146
2147         // If we chose a split mode then reset the new MV thresholds as well
2148         /*if ( vp8_mode_order[best_mode_index] == SPLITMV )
2149         {
2150             best_adjustment = 4; //(cpi->rd_thresh_mult[THR_NEWMV] >> 4);
2151             cpi->rd_thresh_mult[THR_NEWMV] = (cpi->rd_thresh_mult[THR_NEWMV] >= (MIN_THRESHMULT+best_adjustment)) ? cpi->rd_thresh_mult[THR_NEWMV]-best_adjustment: MIN_THRESHMULT;
2152             cpi->rd_threshes[THR_NEWMV] = (cpi->rd_baseline_thresh[THR_NEWMV] >> 7) * cpi->rd_thresh_mult[THR_NEWMV];
2153
2154             best_adjustment = 4; //(cpi->rd_thresh_mult[THR_NEWG] >> 4);
2155             cpi->rd_thresh_mult[THR_NEWG] = (cpi->rd_thresh_mult[THR_NEWG] >= (MIN_THRESHMULT+best_adjustment)) ? cpi->rd_thresh_mult[THR_NEWG]-best_adjustment: MIN_THRESHMULT;
2156             cpi->rd_threshes[THR_NEWG] = (cpi->rd_baseline_thresh[THR_NEWG] >> 7) * cpi->rd_thresh_mult[THR_NEWG];
2157
2158             best_adjustment = 4; //(cpi->rd_thresh_mult[THR_NEWA] >> 4);
2159             cpi->rd_thresh_mult[THR_NEWA] = (cpi->rd_thresh_mult[THR_NEWA] >= (MIN_THRESHMULT+best_adjustment)) ? cpi->rd_thresh_mult[THR_NEWA]-best_adjustment: MIN_THRESHMULT;
2160             cpi->rd_threshes[THR_NEWA] = (cpi->rd_baseline_thresh[THR_NEWA] >> 7) * cpi->rd_thresh_mult[THR_NEWA];
2161         }*/
2162
2163     }
2164
2165     // If we have chosen new mv or split then decay the full search check count more quickly.
2166     if ((vp8_mode_order[best_mode_index] == NEWMV) || (vp8_mode_order[best_mode_index] == SPLITMV))
2167     {
2168         int lf_or_gf = (vp8_ref_frame_order[best_mode_index] == LAST_FRAME) ? 0 : 1;
2169
2170         if (cpi->check_freq[lf_or_gf] && !cpi->do_full[lf_or_gf])
2171         {
2172             cpi->check_freq[lf_or_gf] --;
2173         }
2174     }
2175
2176     // Keep a record of best mode index that we chose
2177     cpi->last_best_mode_index = best_mode_index;
2178
2179     // Note how often each mode chosen as best
2180     cpi->mode_chosen_counts[best_mode_index] ++;
2181
2182
2183     if (cpi->is_src_frame_alt_ref && (best_mbmode.mode != ZEROMV || best_mbmode.ref_frame != ALTREF_FRAME))
2184     {
2185         best_mbmode.mode = ZEROMV;
2186         best_mbmode.ref_frame = ALTREF_FRAME;
2187         best_mbmode.mv.as_int = 0;
2188         best_mbmode.uv_mode = 0;
2189         best_mbmode.mb_skip_coeff = (cpi->common.mb_no_coeff_skip) ? 1 : 0;
2190         best_mbmode.partitioning = 0;
2191         best_mbmode.dc_diff = 0;
2192
2193         vpx_memcpy(&x->e_mbd.mbmi, &best_mbmode, sizeof(MB_MODE_INFO));
2194
2195         for (i = 0; i < 16; i++)
2196         {
2197             vpx_memset(&x->e_mbd.block[i].bmi, 0, sizeof(B_MODE_INFO));
2198         }
2199
2200         x->e_mbd.mbmi.mv.as_int = 0;
2201
2202         return best_rd;
2203     }
2204
2205
2206     // macroblock modes
2207     vpx_memcpy(&x->e_mbd.mbmi, &best_mbmode, sizeof(MB_MODE_INFO));
2208
2209     for (i = 0; i < 16; i++)
2210     {
2211         vpx_memcpy(&x->e_mbd.block[i].bmi, &best_bmodes[i], sizeof(B_MODE_INFO));
2212     }
2213
2214     x->e_mbd.mbmi.mv.as_mv = x->e_mbd.block[15].bmi.mv.as_mv;
2215
2216     return best_rd;
2217 }
2218 #endif
2219