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