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