1/*
2 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11
12#include <stdio.h>
13#include <math.h>
14#include <limits.h>
15#include <assert.h>
16#include "vpx_config.h"
17#include "vp8_rtcd.h"
18#include "vp8/common/pragmas.h"
19#include "tokenize.h"
20#include "treewriter.h"
21#include "onyx_int.h"
22#include "modecosts.h"
23#include "encodeintra.h"
24#include "pickinter.h"
25#include "vp8/common/entropymode.h"
26#include "vp8/common/reconinter.h"
27#include "vp8/common/reconintra4x4.h"
28#include "vp8/common/findnearmv.h"
29#include "vp8/common/quant_common.h"
30#include "encodemb.h"
31#include "quantize.h"
32#include "vp8/common/variance.h"
33#include "mcomp.h"
34#include "rdopt.h"
35#include "vpx_mem/vpx_mem.h"
36#include "vp8/common/systemdependent.h"
37#if CONFIG_TEMPORAL_DENOISING
38#include "denoising.h"
39#endif
40extern void vp8_update_zbin_extra(VP8_COMP *cpi, MACROBLOCK *x);
41
42#define MAXF(a,b)            (((a) > (b)) ? (a) : (b))
43
44typedef struct rate_distortion_struct
45{
46    int rate2;
47    int rate_y;
48    int rate_uv;
49    int distortion2;
50    int distortion_uv;
51} RATE_DISTORTION;
52
53typedef struct best_mode_struct
54{
55  int yrd;
56  int rd;
57  int intra_rd;
58  MB_MODE_INFO mbmode;
59  union b_mode_info bmodes[16];
60  PARTITION_INFO partition;
61} BEST_MODE;
62
63static const int auto_speed_thresh[17] =
64{
65    1000,
66    200,
67    150,
68    130,
69    150,
70    125,
71    120,
72    115,
73    115,
74    115,
75    115,
76    115,
77    115,
78    115,
79    115,
80    115,
81    105
82};
83
84const MB_PREDICTION_MODE vp8_mode_order[MAX_MODES] =
85{
86    ZEROMV,
87    DC_PRED,
88
89    NEARESTMV,
90    NEARMV,
91
92    ZEROMV,
93    NEARESTMV,
94
95    ZEROMV,
96    NEARESTMV,
97
98    NEARMV,
99    NEARMV,
100
101    V_PRED,
102    H_PRED,
103    TM_PRED,
104
105    NEWMV,
106    NEWMV,
107    NEWMV,
108
109    SPLITMV,
110    SPLITMV,
111    SPLITMV,
112
113    B_PRED,
114};
115
116/* This table determines the search order in reference frame priority order,
117 * which may not necessarily match INTRA,LAST,GOLDEN,ARF
118 */
119const int vp8_ref_frame_order[MAX_MODES] =
120{
121    1,
122    0,
123
124    1,
125    1,
126
127    2,
128    2,
129
130    3,
131    3,
132
133    2,
134    3,
135
136    0,
137    0,
138    0,
139
140    1,
141    2,
142    3,
143
144    1,
145    2,
146    3,
147
148    0,
149};
150
151static void fill_token_costs(
152    int c[BLOCK_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS][MAX_ENTROPY_TOKENS],
153    const vp8_prob p[BLOCK_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS][ENTROPY_NODES]
154)
155{
156    int i, j, k;
157
158
159    for (i = 0; i < BLOCK_TYPES; i++)
160        for (j = 0; j < COEF_BANDS; j++)
161            for (k = 0; k < PREV_COEF_CONTEXTS; k++)
162
163                /* check for pt=0 and band > 1 if block type 0
164                 * and 0 if blocktype 1
165                 */
166                if (k == 0 && j > (i == 0))
167                    vp8_cost_tokens2(c[i][j][k], p [i][j][k], vp8_coef_tree, 2);
168                else
169                    vp8_cost_tokens(c[i][j][k], p [i][j][k], vp8_coef_tree);
170}
171
172static const int rd_iifactor[32] =
173{
174    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/* values are now correlated to quantizer */
181static const int sad_per_bit16lut[QINDEX_RANGE] =
182{
183    2,  2,  2,  2,  2,  2,  2,  2,
184    2,  2,  2,  2,  2,  2,  2,  2,
185    3,  3,  3,  3,  3,  3,  3,  3,
186    3,  3,  3,  3,  3,  3,  4,  4,
187    4,  4,  4,  4,  4,  4,  4,  4,
188    4,  4,  5,  5,  5,  5,  5,  5,
189    5,  5,  5,  5,  5,  5,  6,  6,
190    6,  6,  6,  6,  6,  6,  6,  6,
191    6,  6,  7,  7,  7,  7,  7,  7,
192    7,  7,  7,  7,  7,  7,  8,  8,
193    8,  8,  8,  8,  8,  8,  8,  8,
194    8,  8,  9,  9,  9,  9,  9,  9,
195    9,  9,  9,  9,  9,  9,  10, 10,
196    10, 10, 10, 10, 10, 10, 11, 11,
197    11, 11, 11, 11, 12, 12, 12, 12,
198    12, 12, 13, 13, 13, 13, 14, 14
199};
200static const int sad_per_bit4lut[QINDEX_RANGE] =
201{
202    2,  2,  2,  2,  2,  2,  3,  3,
203    3,  3,  3,  3,  3,  3,  3,  3,
204    3,  3,  3,  3,  4,  4,  4,  4,
205    4,  4,  4,  4,  4,  4,  5,  5,
206    5,  5,  5,  5,  6,  6,  6,  6,
207    6,  6,  6,  6,  6,  6,  6,  6,
208    7,  7,  7,  7,  7,  7,  7,  7,
209    7,  7,  7,  7,  7,  8,  8,  8,
210    8,  8,  9,  9,  9,  9,  9,  9,
211    10, 10, 10, 10, 10, 10, 10, 10,
212    11, 11, 11, 11, 11, 11, 11, 11,
213    12, 12, 12, 12, 12, 12, 12, 12,
214    13, 13, 13, 13, 13, 13, 13, 14,
215    14, 14, 14, 14, 15, 15, 15, 15,
216    16, 16, 16, 16, 17, 17, 17, 18,
217    18, 18, 19, 19, 19, 20, 20, 20,
218};
219
220void vp8cx_initialize_me_consts(VP8_COMP *cpi, int QIndex)
221{
222    cpi->mb.sadperbit16 =  sad_per_bit16lut[QIndex];
223    cpi->mb.sadperbit4  =  sad_per_bit4lut[QIndex];
224}
225
226void vp8_initialize_rd_consts(VP8_COMP *cpi, MACROBLOCK *x, int Qvalue)
227{
228    int q;
229    int i;
230    double capped_q = (Qvalue < 160) ? (double)Qvalue : 160.0;
231    double rdconst = 2.80;
232
233    vp8_clear_system_state();
234
235    /* Further tests required to see if optimum is different
236     * for key frames, golden frames and arf frames.
237     */
238    cpi->RDMULT = (int)(rdconst * (capped_q * capped_q));
239
240    /* Extend rate multiplier along side quantizer zbin increases */
241    if (cpi->mb.zbin_over_quant  > 0)
242    {
243        double oq_factor;
244        double modq;
245
246        /* Experimental code using the same basic equation as used for Q above
247         * The units of cpi->mb.zbin_over_quant are 1/128 of Q bin size
248         */
249        oq_factor = 1.0 + ((double)0.0015625 * cpi->mb.zbin_over_quant);
250        modq = (int)((double)capped_q * oq_factor);
251        cpi->RDMULT = (int)(rdconst * (modq * modq));
252    }
253
254    if (cpi->pass == 2 && (cpi->common.frame_type != KEY_FRAME))
255    {
256        if (cpi->twopass.next_iiratio > 31)
257            cpi->RDMULT += (cpi->RDMULT * rd_iifactor[31]) >> 4;
258        else
259            cpi->RDMULT +=
260                (cpi->RDMULT * rd_iifactor[cpi->twopass.next_iiratio]) >> 4;
261    }
262
263    cpi->mb.errorperbit = (cpi->RDMULT / 110);
264    cpi->mb.errorperbit += (cpi->mb.errorperbit==0);
265
266    vp8_set_speed_features(cpi);
267
268    for (i = 0; i < MAX_MODES; i++)
269    {
270        x->mode_test_hit_counts[i] = 0;
271    }
272
273    q = (int)pow(Qvalue, 1.25);
274
275    if (q < 8)
276        q = 8;
277
278    if (cpi->RDMULT > 1000)
279    {
280        cpi->RDDIV = 1;
281        cpi->RDMULT /= 100;
282
283        for (i = 0; i < MAX_MODES; i++)
284        {
285            if (cpi->sf.thresh_mult[i] < INT_MAX)
286            {
287                x->rd_threshes[i] = cpi->sf.thresh_mult[i] * q / 100;
288            }
289            else
290            {
291                x->rd_threshes[i] = INT_MAX;
292            }
293
294            cpi->rd_baseline_thresh[i] = x->rd_threshes[i];
295        }
296    }
297    else
298    {
299        cpi->RDDIV = 100;
300
301        for (i = 0; i < MAX_MODES; i++)
302        {
303            if (cpi->sf.thresh_mult[i] < (INT_MAX / q))
304            {
305                x->rd_threshes[i] = cpi->sf.thresh_mult[i] * q;
306            }
307            else
308            {
309                x->rd_threshes[i] = INT_MAX;
310            }
311
312            cpi->rd_baseline_thresh[i] = x->rd_threshes[i];
313        }
314    }
315
316    {
317      /* build token cost array for the type of frame we have now */
318      FRAME_CONTEXT *l = &cpi->lfc_n;
319
320      if(cpi->common.refresh_alt_ref_frame)
321          l = &cpi->lfc_a;
322      else if(cpi->common.refresh_golden_frame)
323          l = &cpi->lfc_g;
324
325      fill_token_costs(
326          cpi->mb.token_costs,
327          (const vp8_prob( *)[8][3][11]) l->coef_probs
328      );
329      /*
330      fill_token_costs(
331          cpi->mb.token_costs,
332          (const vp8_prob( *)[8][3][11]) cpi->common.fc.coef_probs);
333      */
334
335
336      /* TODO make these mode costs depend on last,alt or gold too.  (jbb) */
337      vp8_init_mode_costs(cpi);
338    }
339
340}
341
342void vp8_auto_select_speed(VP8_COMP *cpi)
343{
344    int milliseconds_for_compress = (int)(1000000 / cpi->framerate);
345
346    milliseconds_for_compress = milliseconds_for_compress * (16 - cpi->oxcf.cpu_used) / 16;
347
348#if 0
349
350    if (0)
351    {
352        FILE *f;
353
354        f = fopen("speed.stt", "a");
355        fprintf(f, " %8ld %10ld %10ld %10ld\n",
356                cpi->common.current_video_frame, cpi->Speed, milliseconds_for_compress, cpi->avg_pick_mode_time);
357        fclose(f);
358    }
359
360#endif
361
362    if (cpi->avg_pick_mode_time < milliseconds_for_compress && (cpi->avg_encode_time - cpi->avg_pick_mode_time) < milliseconds_for_compress)
363    {
364        if (cpi->avg_pick_mode_time == 0)
365        {
366            cpi->Speed = 4;
367        }
368        else
369        {
370            if (milliseconds_for_compress * 100 < cpi->avg_encode_time * 95)
371            {
372                cpi->Speed          += 2;
373                cpi->avg_pick_mode_time = 0;
374                cpi->avg_encode_time = 0;
375
376                if (cpi->Speed > 16)
377                {
378                    cpi->Speed = 16;
379                }
380            }
381
382            if (milliseconds_for_compress * 100 > cpi->avg_encode_time * auto_speed_thresh[cpi->Speed])
383            {
384                cpi->Speed          -= 1;
385                cpi->avg_pick_mode_time = 0;
386                cpi->avg_encode_time = 0;
387
388                /* In real-time mode, cpi->speed is in [4, 16]. */
389                if (cpi->Speed < 4)
390                {
391                    cpi->Speed = 4;
392                }
393            }
394        }
395    }
396    else
397    {
398        cpi->Speed += 4;
399
400        if (cpi->Speed > 16)
401            cpi->Speed = 16;
402
403
404        cpi->avg_pick_mode_time = 0;
405        cpi->avg_encode_time = 0;
406    }
407}
408
409int vp8_block_error_c(short *coeff, short *dqcoeff)
410{
411    int i;
412    int error = 0;
413
414    for (i = 0; i < 16; i++)
415    {
416        int this_diff = coeff[i] - dqcoeff[i];
417        error += this_diff * this_diff;
418    }
419
420    return error;
421}
422
423int vp8_mbblock_error_c(MACROBLOCK *mb, int dc)
424{
425    BLOCK  *be;
426    BLOCKD *bd;
427    int i, j;
428    int berror, error = 0;
429
430    for (i = 0; i < 16; i++)
431    {
432        be = &mb->block[i];
433        bd = &mb->e_mbd.block[i];
434
435        berror = 0;
436
437        for (j = dc; j < 16; j++)
438        {
439            int this_diff = be->coeff[j] - bd->dqcoeff[j];
440            berror += this_diff * this_diff;
441        }
442
443        error += berror;
444    }
445
446    return error;
447}
448
449int vp8_mbuverror_c(MACROBLOCK *mb)
450{
451
452    BLOCK  *be;
453    BLOCKD *bd;
454
455
456    int i;
457    int error = 0;
458
459    for (i = 16; i < 24; i++)
460    {
461        be = &mb->block[i];
462        bd = &mb->e_mbd.block[i];
463
464        error += vp8_block_error_c(be->coeff, bd->dqcoeff);
465    }
466
467    return error;
468}
469
470int VP8_UVSSE(MACROBLOCK *x)
471{
472    unsigned char *uptr, *vptr;
473    unsigned char *upred_ptr = (*(x->block[16].base_src) + x->block[16].src);
474    unsigned char *vpred_ptr = (*(x->block[20].base_src) + x->block[20].src);
475    int uv_stride = x->block[16].src_stride;
476
477    unsigned int sse1 = 0;
478    unsigned int sse2 = 0;
479    int mv_row = x->e_mbd.mode_info_context->mbmi.mv.as_mv.row;
480    int mv_col = x->e_mbd.mode_info_context->mbmi.mv.as_mv.col;
481    int offset;
482    int pre_stride = x->e_mbd.pre.uv_stride;
483
484    if (mv_row < 0)
485        mv_row -= 1;
486    else
487        mv_row += 1;
488
489    if (mv_col < 0)
490        mv_col -= 1;
491    else
492        mv_col += 1;
493
494    mv_row /= 2;
495    mv_col /= 2;
496
497    offset = (mv_row >> 3) * pre_stride + (mv_col >> 3);
498    uptr = x->e_mbd.pre.u_buffer + offset;
499    vptr = x->e_mbd.pre.v_buffer + offset;
500
501    if ((mv_row | mv_col) & 7)
502    {
503        vp8_sub_pixel_variance8x8(uptr, pre_stride,
504            mv_col & 7, mv_row & 7, upred_ptr, uv_stride, &sse2);
505        vp8_sub_pixel_variance8x8(vptr, pre_stride,
506            mv_col & 7, mv_row & 7, vpred_ptr, uv_stride, &sse1);
507        sse2 += sse1;
508    }
509    else
510    {
511        vp8_variance8x8(uptr, pre_stride,
512            upred_ptr, uv_stride, &sse2);
513        vp8_variance8x8(vptr, pre_stride,
514            vpred_ptr, uv_stride, &sse1);
515        sse2 += sse1;
516    }
517    return sse2;
518
519}
520
521static int cost_coeffs(MACROBLOCK *mb, BLOCKD *b, int type, ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l)
522{
523    int c = !type;              /* start at coef 0, unless Y with Y2 */
524    int eob = (int)(*b->eob);
525    int pt ;    /* surrounding block/prev coef predictor */
526    int cost = 0;
527    short *qcoeff_ptr = b->qcoeff;
528
529    VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
530
531    assert(eob <= 16);
532    for (; c < eob; c++)
533    {
534        const int v = qcoeff_ptr[vp8_default_zig_zag1d[c]];
535        const int t = vp8_dct_value_tokens_ptr[v].Token;
536        cost += mb->token_costs [type] [vp8_coef_bands[c]] [pt] [t];
537        cost += vp8_dct_value_cost_ptr[v];
538        pt = vp8_prev_token_class[t];
539    }
540
541    if (c < 16)
542        cost += mb->token_costs [type] [vp8_coef_bands[c]] [pt] [DCT_EOB_TOKEN];
543
544    pt = (c != !type); /* is eob first coefficient; */
545    *a = *l = pt;
546
547    return cost;
548}
549
550static int vp8_rdcost_mby(MACROBLOCK *mb)
551{
552    int cost = 0;
553    int b;
554    MACROBLOCKD *x = &mb->e_mbd;
555    ENTROPY_CONTEXT_PLANES t_above, t_left;
556    ENTROPY_CONTEXT *ta;
557    ENTROPY_CONTEXT *tl;
558
559    vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
560    vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
561
562    ta = (ENTROPY_CONTEXT *)&t_above;
563    tl = (ENTROPY_CONTEXT *)&t_left;
564
565    for (b = 0; b < 16; b++)
566        cost += cost_coeffs(mb, x->block + b, PLANE_TYPE_Y_NO_DC,
567                    ta + vp8_block2above[b], tl + vp8_block2left[b]);
568
569    cost += cost_coeffs(mb, x->block + 24, PLANE_TYPE_Y2,
570                ta + vp8_block2above[24], tl + vp8_block2left[24]);
571
572    return cost;
573}
574
575static void macro_block_yrd( MACROBLOCK *mb,
576                             int *Rate,
577                             int *Distortion)
578{
579    int b;
580    MACROBLOCKD *const x = &mb->e_mbd;
581    BLOCK   *const mb_y2 = mb->block + 24;
582    BLOCKD *const x_y2  = x->block + 24;
583    short *Y2DCPtr = mb_y2->src_diff;
584    BLOCK *beptr;
585    int d;
586
587    vp8_subtract_mby( mb->src_diff, *(mb->block[0].base_src),
588        mb->block[0].src_stride,  mb->e_mbd.predictor, 16);
589
590    /* Fdct and building the 2nd order block */
591    for (beptr = mb->block; beptr < mb->block + 16; beptr += 2)
592    {
593        mb->short_fdct8x4(beptr->src_diff, beptr->coeff, 32);
594        *Y2DCPtr++ = beptr->coeff[0];
595        *Y2DCPtr++ = beptr->coeff[16];
596    }
597
598    /* 2nd order fdct */
599    mb->short_walsh4x4(mb_y2->src_diff, mb_y2->coeff, 8);
600
601    /* Quantization */
602    for (b = 0; b < 16; b++)
603    {
604        mb->quantize_b(&mb->block[b], &mb->e_mbd.block[b]);
605    }
606
607    /* DC predication and Quantization of 2nd Order block */
608    mb->quantize_b(mb_y2, x_y2);
609
610    /* Distortion */
611    d = vp8_mbblock_error(mb, 1) << 2;
612    d += vp8_block_error(mb_y2->coeff, x_y2->dqcoeff);
613
614    *Distortion = (d >> 4);
615
616    /* rate */
617    *Rate = vp8_rdcost_mby(mb);
618}
619
620static void copy_predictor(unsigned char *dst, const unsigned char *predictor)
621{
622    const unsigned int *p = (const unsigned int *)predictor;
623    unsigned int *d = (unsigned int *)dst;
624    d[0] = p[0];
625    d[4] = p[4];
626    d[8] = p[8];
627    d[12] = p[12];
628}
629static int rd_pick_intra4x4block(
630    MACROBLOCK *x,
631    BLOCK *be,
632    BLOCKD *b,
633    B_PREDICTION_MODE *best_mode,
634    const int *bmode_costs,
635    ENTROPY_CONTEXT *a,
636    ENTROPY_CONTEXT *l,
637
638    int *bestrate,
639    int *bestratey,
640    int *bestdistortion)
641{
642    B_PREDICTION_MODE mode;
643    int best_rd = INT_MAX;
644    int rate = 0;
645    int distortion;
646
647    ENTROPY_CONTEXT ta = *a, tempa = *a;
648    ENTROPY_CONTEXT tl = *l, templ = *l;
649    /*
650     * The predictor buffer is a 2d buffer with a stride of 16.  Create
651     * a temp buffer that meets the stride requirements, but we are only
652     * interested in the left 4x4 block
653     * */
654    DECLARE_ALIGNED_ARRAY(16, unsigned char,  best_predictor, 16*4);
655    DECLARE_ALIGNED_ARRAY(16, short, best_dqcoeff, 16);
656    int dst_stride = x->e_mbd.dst.y_stride;
657    unsigned char *dst = x->e_mbd.dst.y_buffer + b->offset;
658
659    unsigned char *Above = dst - dst_stride;
660    unsigned char *yleft = dst - 1;
661    unsigned char top_left = Above[-1];
662
663    for (mode = B_DC_PRED; mode <= B_HU_PRED; mode++)
664    {
665        int this_rd;
666        int ratey;
667
668        rate = bmode_costs[mode];
669
670        vp8_intra4x4_predict(Above, yleft, dst_stride, mode,
671                             b->predictor, 16, top_left);
672        vp8_subtract_b(be, b, 16);
673        x->short_fdct4x4(be->src_diff, be->coeff, 32);
674        x->quantize_b(be, b);
675
676        tempa = ta;
677        templ = tl;
678
679        ratey = cost_coeffs(x, b, PLANE_TYPE_Y_WITH_DC, &tempa, &templ);
680        rate += ratey;
681        distortion = vp8_block_error(be->coeff, b->dqcoeff) >> 2;
682
683        this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
684
685        if (this_rd < best_rd)
686        {
687            *bestrate = rate;
688            *bestratey = ratey;
689            *bestdistortion = distortion;
690            best_rd = this_rd;
691            *best_mode = mode;
692            *a = tempa;
693            *l = templ;
694            copy_predictor(best_predictor, b->predictor);
695            vpx_memcpy(best_dqcoeff, b->dqcoeff, 32);
696        }
697    }
698    b->bmi.as_mode = *best_mode;
699
700    vp8_short_idct4x4llm(best_dqcoeff, best_predictor, 16, dst, dst_stride);
701
702    return best_rd;
703}
704
705static int rd_pick_intra4x4mby_modes(MACROBLOCK *mb, int *Rate,
706                                     int *rate_y, int *Distortion, int best_rd)
707{
708    MACROBLOCKD *const xd = &mb->e_mbd;
709    int i;
710    int cost = mb->mbmode_cost [xd->frame_type] [B_PRED];
711    int distortion = 0;
712    int tot_rate_y = 0;
713    int64_t total_rd = 0;
714    ENTROPY_CONTEXT_PLANES t_above, t_left;
715    ENTROPY_CONTEXT *ta;
716    ENTROPY_CONTEXT *tl;
717    const int *bmode_costs;
718
719    vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
720    vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
721
722    ta = (ENTROPY_CONTEXT *)&t_above;
723    tl = (ENTROPY_CONTEXT *)&t_left;
724
725    intra_prediction_down_copy(xd, xd->dst.y_buffer - xd->dst.y_stride + 16);
726
727    bmode_costs = mb->inter_bmode_costs;
728
729    for (i = 0; i < 16; i++)
730    {
731        MODE_INFO *const mic = xd->mode_info_context;
732        const int mis = xd->mode_info_stride;
733        B_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
734        int UNINITIALIZED_IS_SAFE(r), UNINITIALIZED_IS_SAFE(ry), UNINITIALIZED_IS_SAFE(d);
735
736        if (mb->e_mbd.frame_type == KEY_FRAME)
737        {
738            const B_PREDICTION_MODE A = above_block_mode(mic, i, mis);
739            const B_PREDICTION_MODE L = left_block_mode(mic, i);
740
741            bmode_costs  = mb->bmode_costs[A][L];
742        }
743
744        total_rd += rd_pick_intra4x4block(
745            mb, mb->block + i, xd->block + i, &best_mode, bmode_costs,
746            ta + vp8_block2above[i],
747            tl + vp8_block2left[i], &r, &ry, &d);
748
749        cost += r;
750        distortion += d;
751        tot_rate_y += ry;
752
753        mic->bmi[i].as_mode = best_mode;
754
755        if(total_rd >= (int64_t)best_rd)
756            break;
757    }
758
759    if(total_rd >= (int64_t)best_rd)
760        return INT_MAX;
761
762    *Rate = cost;
763    *rate_y = tot_rate_y;
764    *Distortion = distortion;
765
766    return RDCOST(mb->rdmult, mb->rddiv, cost, distortion);
767}
768
769
770static int rd_pick_intra16x16mby_mode(MACROBLOCK *x,
771                                      int *Rate,
772                                      int *rate_y,
773                                      int *Distortion)
774{
775    MB_PREDICTION_MODE mode;
776    MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(mode_selected);
777    int rate, ratey;
778    int distortion;
779    int best_rd = INT_MAX;
780    int this_rd;
781    MACROBLOCKD *xd = &x->e_mbd;
782
783    /* Y Search for 16x16 intra prediction mode */
784    for (mode = DC_PRED; mode <= TM_PRED; mode++)
785    {
786        xd->mode_info_context->mbmi.mode = mode;
787
788        vp8_build_intra_predictors_mby_s(xd,
789                                         xd->dst.y_buffer - xd->dst.y_stride,
790                                         xd->dst.y_buffer - 1,
791                                         xd->dst.y_stride,
792                                         xd->predictor,
793                                         16);
794
795        macro_block_yrd(x, &ratey, &distortion);
796        rate = ratey + x->mbmode_cost[xd->frame_type]
797                                     [xd->mode_info_context->mbmi.mode];
798
799        this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
800
801        if (this_rd < best_rd)
802        {
803            mode_selected = mode;
804            best_rd = this_rd;
805            *Rate = rate;
806            *rate_y = ratey;
807            *Distortion = distortion;
808        }
809    }
810
811    xd->mode_info_context->mbmi.mode = mode_selected;
812    return best_rd;
813}
814
815static int rd_cost_mbuv(MACROBLOCK *mb)
816{
817    int b;
818    int cost = 0;
819    MACROBLOCKD *x = &mb->e_mbd;
820    ENTROPY_CONTEXT_PLANES t_above, t_left;
821    ENTROPY_CONTEXT *ta;
822    ENTROPY_CONTEXT *tl;
823
824    vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
825    vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
826
827    ta = (ENTROPY_CONTEXT *)&t_above;
828    tl = (ENTROPY_CONTEXT *)&t_left;
829
830    for (b = 16; b < 24; b++)
831        cost += cost_coeffs(mb, x->block + b, PLANE_TYPE_UV,
832                    ta + vp8_block2above[b], tl + vp8_block2left[b]);
833
834    return cost;
835}
836
837
838static int rd_inter16x16_uv(VP8_COMP *cpi, MACROBLOCK *x, int *rate,
839                            int *distortion, int fullpixel)
840{
841    vp8_build_inter16x16_predictors_mbuv(&x->e_mbd);
842    vp8_subtract_mbuv(x->src_diff,
843        x->src.u_buffer, x->src.v_buffer, x->src.uv_stride,
844        &x->e_mbd.predictor[256], &x->e_mbd.predictor[320], 8);
845
846    vp8_transform_mbuv(x);
847    vp8_quantize_mbuv(x);
848
849    *rate       = rd_cost_mbuv(x);
850    *distortion = vp8_mbuverror(x) / 4;
851
852    return RDCOST(x->rdmult, x->rddiv, *rate, *distortion);
853}
854
855static int rd_inter4x4_uv(VP8_COMP *cpi, MACROBLOCK *x, int *rate,
856                          int *distortion, int fullpixel)
857{
858    vp8_build_inter4x4_predictors_mbuv(&x->e_mbd);
859    vp8_subtract_mbuv(x->src_diff,
860        x->src.u_buffer, x->src.v_buffer, x->src.uv_stride,
861        &x->e_mbd.predictor[256], &x->e_mbd.predictor[320], 8);
862
863    vp8_transform_mbuv(x);
864    vp8_quantize_mbuv(x);
865
866    *rate       = rd_cost_mbuv(x);
867    *distortion = vp8_mbuverror(x) / 4;
868
869    return RDCOST(x->rdmult, x->rddiv, *rate, *distortion);
870}
871
872static void rd_pick_intra_mbuv_mode(MACROBLOCK *x, int *rate,
873                                    int *rate_tokenonly, int *distortion)
874{
875    MB_PREDICTION_MODE mode;
876    MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(mode_selected);
877    int best_rd = INT_MAX;
878    int UNINITIALIZED_IS_SAFE(d), UNINITIALIZED_IS_SAFE(r);
879    int rate_to;
880    MACROBLOCKD *xd = &x->e_mbd;
881
882    for (mode = DC_PRED; mode <= TM_PRED; mode++)
883    {
884        int this_rate;
885        int this_distortion;
886        int this_rd;
887
888        xd->mode_info_context->mbmi.uv_mode = mode;
889
890        vp8_build_intra_predictors_mbuv_s(xd,
891                                          xd->dst.u_buffer - xd->dst.uv_stride,
892                                          xd->dst.v_buffer - xd->dst.uv_stride,
893                                          xd->dst.u_buffer - 1,
894                                          xd->dst.v_buffer - 1,
895                                          xd->dst.uv_stride,
896                                          &xd->predictor[256], &xd->predictor[320],
897                                          8);
898
899
900        vp8_subtract_mbuv(x->src_diff,
901                      x->src.u_buffer, x->src.v_buffer, x->src.uv_stride,
902                      &xd->predictor[256], &xd->predictor[320], 8);
903        vp8_transform_mbuv(x);
904        vp8_quantize_mbuv(x);
905
906        rate_to = rd_cost_mbuv(x);
907        this_rate = rate_to + x->intra_uv_mode_cost[xd->frame_type][xd->mode_info_context->mbmi.uv_mode];
908
909        this_distortion = vp8_mbuverror(x) / 4;
910
911        this_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_distortion);
912
913        if (this_rd < best_rd)
914        {
915            best_rd = this_rd;
916            d = this_distortion;
917            r = this_rate;
918            *rate_tokenonly = rate_to;
919            mode_selected = mode;
920        }
921    }
922
923    *rate = r;
924    *distortion = d;
925
926    xd->mode_info_context->mbmi.uv_mode = mode_selected;
927}
928
929int vp8_cost_mv_ref(MB_PREDICTION_MODE m, const int near_mv_ref_ct[4])
930{
931    vp8_prob p [VP8_MVREFS-1];
932    assert(NEARESTMV <= m  &&  m <= SPLITMV);
933    vp8_mv_ref_probs(p, near_mv_ref_ct);
934    return vp8_cost_token(vp8_mv_ref_tree, p,
935                          vp8_mv_ref_encoding_array + (m - NEARESTMV));
936}
937
938void vp8_set_mbmode_and_mvs(MACROBLOCK *x, MB_PREDICTION_MODE mb, int_mv *mv)
939{
940    x->e_mbd.mode_info_context->mbmi.mode = mb;
941    x->e_mbd.mode_info_context->mbmi.mv.as_int = mv->as_int;
942}
943
944static int labels2mode(
945    MACROBLOCK *x,
946    int const *labelings, int which_label,
947    B_PREDICTION_MODE this_mode,
948    int_mv *this_mv, int_mv *best_ref_mv,
949    int *mvcost[2]
950)
951{
952    MACROBLOCKD *const xd = & x->e_mbd;
953    MODE_INFO *const mic = xd->mode_info_context;
954    const int mis = xd->mode_info_stride;
955
956    int cost = 0;
957    int thismvcost = 0;
958
959    /* We have to be careful retrieving previously-encoded motion vectors.
960       Ones from this macroblock have to be pulled from the BLOCKD array
961       as they have not yet made it to the bmi array in our MB_MODE_INFO. */
962
963    int i = 0;
964
965    do
966    {
967        BLOCKD *const d = xd->block + i;
968        const int row = i >> 2,  col = i & 3;
969
970        B_PREDICTION_MODE m;
971
972        if (labelings[i] != which_label)
973            continue;
974
975        if (col  &&  labelings[i] == labelings[i-1])
976            m = LEFT4X4;
977        else if (row  &&  labelings[i] == labelings[i-4])
978            m = ABOVE4X4;
979        else
980        {
981            /* the only time we should do costing for new motion vector
982             * or mode is when we are on a new label  (jbb May 08, 2007)
983             */
984            switch (m = this_mode)
985            {
986            case NEW4X4 :
987                thismvcost  = vp8_mv_bit_cost(this_mv, best_ref_mv, mvcost, 102);
988                break;
989            case LEFT4X4:
990                this_mv->as_int = col ? d[-1].bmi.mv.as_int : left_block_mv(mic, i);
991                break;
992            case ABOVE4X4:
993                this_mv->as_int = row ? d[-4].bmi.mv.as_int : above_block_mv(mic, i, mis);
994                break;
995            case ZERO4X4:
996                this_mv->as_int = 0;
997                break;
998            default:
999                break;
1000            }
1001
1002            if (m == ABOVE4X4)  /* replace above with left if same */
1003            {
1004                int_mv left_mv;
1005
1006                left_mv.as_int = col ? d[-1].bmi.mv.as_int :
1007                                        left_block_mv(mic, i);
1008
1009                if (left_mv.as_int == this_mv->as_int)
1010                    m = LEFT4X4;
1011            }
1012
1013            cost = x->inter_bmode_costs[ m];
1014        }
1015
1016        d->bmi.mv.as_int = this_mv->as_int;
1017
1018        x->partition_info->bmi[i].mode = m;
1019        x->partition_info->bmi[i].mv.as_int = this_mv->as_int;
1020
1021    }
1022    while (++i < 16);
1023
1024    cost += thismvcost ;
1025    return cost;
1026}
1027
1028static int rdcost_mbsegment_y(MACROBLOCK *mb, const int *labels,
1029                              int which_label, ENTROPY_CONTEXT *ta,
1030                              ENTROPY_CONTEXT *tl)
1031{
1032    int cost = 0;
1033    int b;
1034    MACROBLOCKD *x = &mb->e_mbd;
1035
1036    for (b = 0; b < 16; b++)
1037        if (labels[ b] == which_label)
1038            cost += cost_coeffs(mb, x->block + b, PLANE_TYPE_Y_WITH_DC,
1039                                ta + vp8_block2above[b],
1040                                tl + vp8_block2left[b]);
1041
1042    return cost;
1043
1044}
1045static unsigned int vp8_encode_inter_mb_segment(MACROBLOCK *x, int const *labels, int which_label)
1046{
1047    int i;
1048    unsigned int distortion = 0;
1049    int pre_stride = x->e_mbd.pre.y_stride;
1050    unsigned char *base_pre = x->e_mbd.pre.y_buffer;
1051
1052
1053    for (i = 0; i < 16; i++)
1054    {
1055        if (labels[i] == which_label)
1056        {
1057            BLOCKD *bd = &x->e_mbd.block[i];
1058            BLOCK *be = &x->block[i];
1059
1060            vp8_build_inter_predictors_b(bd, 16, base_pre, pre_stride, x->e_mbd.subpixel_predict);
1061            vp8_subtract_b(be, bd, 16);
1062            x->short_fdct4x4(be->src_diff, be->coeff, 32);
1063            x->quantize_b(be, bd);
1064
1065            distortion += vp8_block_error(be->coeff, bd->dqcoeff);
1066        }
1067    }
1068
1069    return distortion;
1070}
1071
1072
1073static const unsigned int segmentation_to_sseshift[4] = {3, 3, 2, 0};
1074
1075
1076typedef struct
1077{
1078  int_mv *ref_mv;
1079  int_mv mvp;
1080
1081  int segment_rd;
1082  int segment_num;
1083  int r;
1084  int d;
1085  int segment_yrate;
1086  B_PREDICTION_MODE modes[16];
1087  int_mv mvs[16];
1088  unsigned char eobs[16];
1089
1090  int mvthresh;
1091  int *mdcounts;
1092
1093  int_mv sv_mvp[4]; /* save 4 mvp from 8x8 */
1094  int sv_istep[2];  /* save 2 initial step_param for 16x8/8x16 */
1095
1096} BEST_SEG_INFO;
1097
1098
1099static void rd_check_segment(VP8_COMP *cpi, MACROBLOCK *x,
1100                             BEST_SEG_INFO *bsi, unsigned int segmentation)
1101{
1102    int i;
1103    int const *labels;
1104    int br = 0;
1105    int bd = 0;
1106    B_PREDICTION_MODE this_mode;
1107
1108
1109    int label_count;
1110    int this_segment_rd = 0;
1111    int label_mv_thresh;
1112    int rate = 0;
1113    int sbr = 0;
1114    int sbd = 0;
1115    int segmentyrate = 0;
1116
1117    vp8_variance_fn_ptr_t *v_fn_ptr;
1118
1119    ENTROPY_CONTEXT_PLANES t_above, t_left;
1120    ENTROPY_CONTEXT *ta;
1121    ENTROPY_CONTEXT *tl;
1122    ENTROPY_CONTEXT_PLANES t_above_b, t_left_b;
1123    ENTROPY_CONTEXT *ta_b;
1124    ENTROPY_CONTEXT *tl_b;
1125
1126    vpx_memcpy(&t_above, x->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
1127    vpx_memcpy(&t_left, x->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
1128
1129    ta = (ENTROPY_CONTEXT *)&t_above;
1130    tl = (ENTROPY_CONTEXT *)&t_left;
1131    ta_b = (ENTROPY_CONTEXT *)&t_above_b;
1132    tl_b = (ENTROPY_CONTEXT *)&t_left_b;
1133
1134    br = 0;
1135    bd = 0;
1136
1137    v_fn_ptr = &cpi->fn_ptr[segmentation];
1138    labels = vp8_mbsplits[segmentation];
1139    label_count = vp8_mbsplit_count[segmentation];
1140
1141    /* 64 makes this threshold really big effectively making it so that we
1142     * very rarely check mvs on segments.   setting this to 1 would make mv
1143     * thresh roughly equal to what it is for macroblocks
1144     */
1145    label_mv_thresh = 1 * bsi->mvthresh / label_count ;
1146
1147    /* Segmentation method overheads */
1148    rate = vp8_cost_token(vp8_mbsplit_tree, vp8_mbsplit_probs, vp8_mbsplit_encodings + segmentation);
1149    rate += vp8_cost_mv_ref(SPLITMV, bsi->mdcounts);
1150    this_segment_rd += RDCOST(x->rdmult, x->rddiv, rate, 0);
1151    br += rate;
1152
1153    for (i = 0; i < label_count; i++)
1154    {
1155        int_mv mode_mv[B_MODE_COUNT];
1156        int best_label_rd = INT_MAX;
1157        B_PREDICTION_MODE mode_selected = ZERO4X4;
1158        int bestlabelyrate = 0;
1159
1160        /* search for the best motion vector on this segment */
1161        for (this_mode = LEFT4X4; this_mode <= NEW4X4 ; this_mode ++)
1162        {
1163            int this_rd;
1164            int distortion;
1165            int labelyrate;
1166            ENTROPY_CONTEXT_PLANES t_above_s, t_left_s;
1167            ENTROPY_CONTEXT *ta_s;
1168            ENTROPY_CONTEXT *tl_s;
1169
1170            vpx_memcpy(&t_above_s, &t_above, sizeof(ENTROPY_CONTEXT_PLANES));
1171            vpx_memcpy(&t_left_s, &t_left, sizeof(ENTROPY_CONTEXT_PLANES));
1172
1173            ta_s = (ENTROPY_CONTEXT *)&t_above_s;
1174            tl_s = (ENTROPY_CONTEXT *)&t_left_s;
1175
1176            if (this_mode == NEW4X4)
1177            {
1178                int sseshift;
1179                int num00;
1180                int step_param = 0;
1181                int further_steps;
1182                int n;
1183                int thissme;
1184                int bestsme = INT_MAX;
1185                int_mv  temp_mv;
1186                BLOCK *c;
1187                BLOCKD *e;
1188
1189                /* Is the best so far sufficiently good that we cant justify
1190                 * doing a new motion search.
1191                 */
1192                if (best_label_rd < label_mv_thresh)
1193                    break;
1194
1195                if(cpi->compressor_speed)
1196                {
1197                    if (segmentation == BLOCK_8X16 || segmentation == BLOCK_16X8)
1198                    {
1199                        bsi->mvp.as_int = bsi->sv_mvp[i].as_int;
1200                        if (i==1 && segmentation == BLOCK_16X8)
1201                          bsi->mvp.as_int = bsi->sv_mvp[2].as_int;
1202
1203                        step_param = bsi->sv_istep[i];
1204                    }
1205
1206                    /* use previous block's result as next block's MV
1207                     * predictor.
1208                     */
1209                    if (segmentation == BLOCK_4X4 && i>0)
1210                    {
1211                        bsi->mvp.as_int = x->e_mbd.block[i-1].bmi.mv.as_int;
1212                        if (i==4 || i==8 || i==12)
1213                            bsi->mvp.as_int = x->e_mbd.block[i-4].bmi.mv.as_int;
1214                        step_param = 2;
1215                    }
1216                }
1217
1218                further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
1219
1220                {
1221                    int sadpb = x->sadperbit4;
1222                    int_mv mvp_full;
1223
1224                    mvp_full.as_mv.row = bsi->mvp.as_mv.row >>3;
1225                    mvp_full.as_mv.col = bsi->mvp.as_mv.col >>3;
1226
1227                    /* find first label */
1228                    n = vp8_mbsplit_offset[segmentation][i];
1229
1230                    c = &x->block[n];
1231                    e = &x->e_mbd.block[n];
1232
1233                    {
1234                        bestsme = cpi->diamond_search_sad(x, c, e, &mvp_full,
1235                                                &mode_mv[NEW4X4], step_param,
1236                                                sadpb, &num00, v_fn_ptr,
1237                                                x->mvcost, bsi->ref_mv);
1238
1239                        n = num00;
1240                        num00 = 0;
1241
1242                        while (n < further_steps)
1243                        {
1244                            n++;
1245
1246                            if (num00)
1247                                num00--;
1248                            else
1249                            {
1250                                thissme = cpi->diamond_search_sad(x, c, e,
1251                                                    &mvp_full, &temp_mv,
1252                                                    step_param + n, sadpb,
1253                                                    &num00, v_fn_ptr,
1254                                                    x->mvcost, bsi->ref_mv);
1255
1256                                if (thissme < bestsme)
1257                                {
1258                                    bestsme = thissme;
1259                                    mode_mv[NEW4X4].as_int = temp_mv.as_int;
1260                                }
1261                            }
1262                        }
1263                    }
1264
1265                    sseshift = segmentation_to_sseshift[segmentation];
1266
1267                    /* Should we do a full search (best quality only) */
1268                    if ((cpi->compressor_speed == 0) && (bestsme >> sseshift) > 4000)
1269                    {
1270                        /* Check if mvp_full is within the range. */
1271                        vp8_clamp_mv(&mvp_full, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
1272
1273                        thissme = cpi->full_search_sad(x, c, e, &mvp_full,
1274                                                       sadpb, 16, v_fn_ptr,
1275                                                       x->mvcost, bsi->ref_mv);
1276
1277                        if (thissme < bestsme)
1278                        {
1279                            bestsme = thissme;
1280                            mode_mv[NEW4X4].as_int = e->bmi.mv.as_int;
1281                        }
1282                        else
1283                        {
1284                            /* The full search result is actually worse so
1285                             * re-instate the previous best vector
1286                             */
1287                            e->bmi.mv.as_int = mode_mv[NEW4X4].as_int;
1288                        }
1289                    }
1290                }
1291
1292                if (bestsme < INT_MAX)
1293                {
1294                    int disto;
1295                    unsigned int sse;
1296                    cpi->find_fractional_mv_step(x, c, e, &mode_mv[NEW4X4],
1297                        bsi->ref_mv, x->errorperbit, v_fn_ptr, x->mvcost,
1298                        &disto, &sse);
1299                }
1300            } /* NEW4X4 */
1301
1302            rate = labels2mode(x, labels, i, this_mode, &mode_mv[this_mode],
1303                               bsi->ref_mv, x->mvcost);
1304
1305            /* Trap vectors that reach beyond the UMV borders */
1306            if (((mode_mv[this_mode].as_mv.row >> 3) < x->mv_row_min) || ((mode_mv[this_mode].as_mv.row >> 3) > x->mv_row_max) ||
1307                ((mode_mv[this_mode].as_mv.col >> 3) < x->mv_col_min) || ((mode_mv[this_mode].as_mv.col >> 3) > x->mv_col_max))
1308            {
1309                continue;
1310            }
1311
1312            distortion = vp8_encode_inter_mb_segment(x, labels, i) / 4;
1313
1314            labelyrate = rdcost_mbsegment_y(x, labels, i, ta_s, tl_s);
1315            rate += labelyrate;
1316
1317            this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
1318
1319            if (this_rd < best_label_rd)
1320            {
1321                sbr = rate;
1322                sbd = distortion;
1323                bestlabelyrate = labelyrate;
1324                mode_selected = this_mode;
1325                best_label_rd = this_rd;
1326
1327                vpx_memcpy(ta_b, ta_s, sizeof(ENTROPY_CONTEXT_PLANES));
1328                vpx_memcpy(tl_b, tl_s, sizeof(ENTROPY_CONTEXT_PLANES));
1329
1330            }
1331        } /*for each 4x4 mode*/
1332
1333        vpx_memcpy(ta, ta_b, sizeof(ENTROPY_CONTEXT_PLANES));
1334        vpx_memcpy(tl, tl_b, sizeof(ENTROPY_CONTEXT_PLANES));
1335
1336        labels2mode(x, labels, i, mode_selected, &mode_mv[mode_selected],
1337                    bsi->ref_mv, x->mvcost);
1338
1339        br += sbr;
1340        bd += sbd;
1341        segmentyrate += bestlabelyrate;
1342        this_segment_rd += best_label_rd;
1343
1344        if (this_segment_rd >= bsi->segment_rd)
1345            break;
1346
1347    } /* for each label */
1348
1349    if (this_segment_rd < bsi->segment_rd)
1350    {
1351        bsi->r = br;
1352        bsi->d = bd;
1353        bsi->segment_yrate = segmentyrate;
1354        bsi->segment_rd = this_segment_rd;
1355        bsi->segment_num = segmentation;
1356
1357        /* store everything needed to come back to this!! */
1358        for (i = 0; i < 16; i++)
1359        {
1360            bsi->mvs[i].as_mv = x->partition_info->bmi[i].mv.as_mv;
1361            bsi->modes[i] = x->partition_info->bmi[i].mode;
1362            bsi->eobs[i] = x->e_mbd.eobs[i];
1363        }
1364    }
1365}
1366
1367static
1368void vp8_cal_step_param(int sr, int *sp)
1369{
1370    int step = 0;
1371
1372    if (sr > MAX_FIRST_STEP) sr = MAX_FIRST_STEP;
1373    else if (sr < 1) sr = 1;
1374
1375    while (sr>>=1)
1376        step++;
1377
1378    *sp = MAX_MVSEARCH_STEPS - 1 - step;
1379}
1380
1381static int vp8_rd_pick_best_mbsegmentation(VP8_COMP *cpi, MACROBLOCK *x,
1382                                           int_mv *best_ref_mv, int best_rd,
1383                                           int *mdcounts, int *returntotrate,
1384                                           int *returnyrate, int *returndistortion,
1385                                           int mvthresh)
1386{
1387    int i;
1388    BEST_SEG_INFO bsi;
1389
1390    vpx_memset(&bsi, 0, sizeof(bsi));
1391
1392    bsi.segment_rd = best_rd;
1393    bsi.ref_mv = best_ref_mv;
1394    bsi.mvp.as_int = best_ref_mv->as_int;
1395    bsi.mvthresh = mvthresh;
1396    bsi.mdcounts = mdcounts;
1397
1398    for(i = 0; i < 16; i++)
1399    {
1400        bsi.modes[i] = ZERO4X4;
1401    }
1402
1403    if(cpi->compressor_speed == 0)
1404    {
1405        /* for now, we will keep the original segmentation order
1406           when in best quality mode */
1407        rd_check_segment(cpi, x, &bsi, BLOCK_16X8);
1408        rd_check_segment(cpi, x, &bsi, BLOCK_8X16);
1409        rd_check_segment(cpi, x, &bsi, BLOCK_8X8);
1410        rd_check_segment(cpi, x, &bsi, BLOCK_4X4);
1411    }
1412    else
1413    {
1414        int sr;
1415
1416        rd_check_segment(cpi, x, &bsi, BLOCK_8X8);
1417
1418        if (bsi.segment_rd < best_rd)
1419        {
1420            int col_min = ((best_ref_mv->as_mv.col+7)>>3) - MAX_FULL_PEL_VAL;
1421            int row_min = ((best_ref_mv->as_mv.row+7)>>3) - MAX_FULL_PEL_VAL;
1422            int col_max = (best_ref_mv->as_mv.col>>3) + MAX_FULL_PEL_VAL;
1423            int row_max = (best_ref_mv->as_mv.row>>3) + MAX_FULL_PEL_VAL;
1424
1425            int tmp_col_min = x->mv_col_min;
1426            int tmp_col_max = x->mv_col_max;
1427            int tmp_row_min = x->mv_row_min;
1428            int tmp_row_max = x->mv_row_max;
1429
1430            /* Get intersection of UMV window and valid MV window to reduce # of checks in diamond search. */
1431            if (x->mv_col_min < col_min )
1432                x->mv_col_min = col_min;
1433            if (x->mv_col_max > col_max )
1434                x->mv_col_max = col_max;
1435            if (x->mv_row_min < row_min )
1436                x->mv_row_min = row_min;
1437            if (x->mv_row_max > row_max )
1438                x->mv_row_max = row_max;
1439
1440            /* Get 8x8 result */
1441            bsi.sv_mvp[0].as_int = bsi.mvs[0].as_int;
1442            bsi.sv_mvp[1].as_int = bsi.mvs[2].as_int;
1443            bsi.sv_mvp[2].as_int = bsi.mvs[8].as_int;
1444            bsi.sv_mvp[3].as_int = bsi.mvs[10].as_int;
1445
1446            /* Use 8x8 result as 16x8/8x16's predictor MV. Adjust search range according to the closeness of 2 MV. */
1447            /* block 8X16 */
1448            {
1449                sr = MAXF((abs(bsi.sv_mvp[0].as_mv.row - bsi.sv_mvp[2].as_mv.row))>>3, (abs(bsi.sv_mvp[0].as_mv.col - bsi.sv_mvp[2].as_mv.col))>>3);
1450                vp8_cal_step_param(sr, &bsi.sv_istep[0]);
1451
1452                sr = MAXF((abs(bsi.sv_mvp[1].as_mv.row - bsi.sv_mvp[3].as_mv.row))>>3, (abs(bsi.sv_mvp[1].as_mv.col - bsi.sv_mvp[3].as_mv.col))>>3);
1453                vp8_cal_step_param(sr, &bsi.sv_istep[1]);
1454
1455                rd_check_segment(cpi, x, &bsi, BLOCK_8X16);
1456            }
1457
1458            /* block 16X8 */
1459            {
1460                sr = MAXF((abs(bsi.sv_mvp[0].as_mv.row - bsi.sv_mvp[1].as_mv.row))>>3, (abs(bsi.sv_mvp[0].as_mv.col - bsi.sv_mvp[1].as_mv.col))>>3);
1461                vp8_cal_step_param(sr, &bsi.sv_istep[0]);
1462
1463                sr = MAXF((abs(bsi.sv_mvp[2].as_mv.row - bsi.sv_mvp[3].as_mv.row))>>3, (abs(bsi.sv_mvp[2].as_mv.col - bsi.sv_mvp[3].as_mv.col))>>3);
1464                vp8_cal_step_param(sr, &bsi.sv_istep[1]);
1465
1466                rd_check_segment(cpi, x, &bsi, BLOCK_16X8);
1467            }
1468
1469            /* If 8x8 is better than 16x8/8x16, then do 4x4 search */
1470            /* Not skip 4x4 if speed=0 (good quality) */
1471            if (cpi->sf.no_skip_block4x4_search || bsi.segment_num == BLOCK_8X8)  /* || (sv_segment_rd8x8-bsi.segment_rd) < sv_segment_rd8x8>>5) */
1472            {
1473                bsi.mvp.as_int = bsi.sv_mvp[0].as_int;
1474                rd_check_segment(cpi, x, &bsi, BLOCK_4X4);
1475            }
1476
1477            /* restore UMV window */
1478            x->mv_col_min = tmp_col_min;
1479            x->mv_col_max = tmp_col_max;
1480            x->mv_row_min = tmp_row_min;
1481            x->mv_row_max = tmp_row_max;
1482        }
1483    }
1484
1485    /* set it to the best */
1486    for (i = 0; i < 16; i++)
1487    {
1488        BLOCKD *bd = &x->e_mbd.block[i];
1489
1490        bd->bmi.mv.as_int = bsi.mvs[i].as_int;
1491        *bd->eob = bsi.eobs[i];
1492    }
1493
1494    *returntotrate = bsi.r;
1495    *returndistortion = bsi.d;
1496    *returnyrate = bsi.segment_yrate;
1497
1498    /* save partitions */
1499    x->e_mbd.mode_info_context->mbmi.partitioning = bsi.segment_num;
1500    x->partition_info->count = vp8_mbsplit_count[bsi.segment_num];
1501
1502    for (i = 0; i < x->partition_info->count; i++)
1503    {
1504        int j;
1505
1506        j = vp8_mbsplit_offset[bsi.segment_num][i];
1507
1508        x->partition_info->bmi[i].mode = bsi.modes[j];
1509        x->partition_info->bmi[i].mv.as_mv = bsi.mvs[j].as_mv;
1510    }
1511    /*
1512     * used to set x->e_mbd.mode_info_context->mbmi.mv.as_int
1513     */
1514    x->partition_info->bmi[15].mv.as_int = bsi.mvs[15].as_int;
1515
1516    return bsi.segment_rd;
1517}
1518
1519/* The improved MV prediction */
1520void vp8_mv_pred
1521(
1522    VP8_COMP *cpi,
1523    MACROBLOCKD *xd,
1524    const MODE_INFO *here,
1525    int_mv *mvp,
1526    int refframe,
1527    int *ref_frame_sign_bias,
1528    int *sr,
1529    int near_sadidx[]
1530)
1531{
1532    const MODE_INFO *above = here - xd->mode_info_stride;
1533    const MODE_INFO *left = here - 1;
1534    const MODE_INFO *aboveleft = above - 1;
1535    int_mv           near_mvs[8];
1536    int              near_ref[8];
1537    int_mv           mv;
1538    int              vcnt=0;
1539    int              find=0;
1540    int              mb_offset;
1541
1542    int              mvx[8];
1543    int              mvy[8];
1544    int              i;
1545
1546    mv.as_int = 0;
1547
1548    if(here->mbmi.ref_frame != INTRA_FRAME)
1549    {
1550        near_mvs[0].as_int = near_mvs[1].as_int = near_mvs[2].as_int = near_mvs[3].as_int = near_mvs[4].as_int = near_mvs[5].as_int = near_mvs[6].as_int = near_mvs[7].as_int = 0;
1551        near_ref[0] = near_ref[1] = near_ref[2] = near_ref[3] = near_ref[4] = near_ref[5] = near_ref[6] = near_ref[7] = 0;
1552
1553        /* read in 3 nearby block's MVs from current frame as prediction
1554         * candidates.
1555         */
1556        if (above->mbmi.ref_frame != INTRA_FRAME)
1557        {
1558            near_mvs[vcnt].as_int = above->mbmi.mv.as_int;
1559            mv_bias(ref_frame_sign_bias[above->mbmi.ref_frame], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1560            near_ref[vcnt] =  above->mbmi.ref_frame;
1561        }
1562        vcnt++;
1563        if (left->mbmi.ref_frame != INTRA_FRAME)
1564        {
1565            near_mvs[vcnt].as_int = left->mbmi.mv.as_int;
1566            mv_bias(ref_frame_sign_bias[left->mbmi.ref_frame], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1567            near_ref[vcnt] =  left->mbmi.ref_frame;
1568        }
1569        vcnt++;
1570        if (aboveleft->mbmi.ref_frame != INTRA_FRAME)
1571        {
1572            near_mvs[vcnt].as_int = aboveleft->mbmi.mv.as_int;
1573            mv_bias(ref_frame_sign_bias[aboveleft->mbmi.ref_frame], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1574            near_ref[vcnt] =  aboveleft->mbmi.ref_frame;
1575        }
1576        vcnt++;
1577
1578        /* read in 5 nearby block's MVs from last frame. */
1579        if(cpi->common.last_frame_type != KEY_FRAME)
1580        {
1581            mb_offset = (-xd->mb_to_top_edge/128 + 1) * (xd->mode_info_stride +1) + (-xd->mb_to_left_edge/128 +1) ;
1582
1583            /* current in last frame */
1584            if (cpi->lf_ref_frame[mb_offset] != INTRA_FRAME)
1585            {
1586                near_mvs[vcnt].as_int = cpi->lfmv[mb_offset].as_int;
1587                mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1588                near_ref[vcnt] =  cpi->lf_ref_frame[mb_offset];
1589            }
1590            vcnt++;
1591
1592            /* above in last frame */
1593            if (cpi->lf_ref_frame[mb_offset - xd->mode_info_stride-1] != INTRA_FRAME)
1594            {
1595                near_mvs[vcnt].as_int = cpi->lfmv[mb_offset - xd->mode_info_stride-1].as_int;
1596                mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset - xd->mode_info_stride-1], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1597                near_ref[vcnt] =  cpi->lf_ref_frame[mb_offset - xd->mode_info_stride-1];
1598            }
1599            vcnt++;
1600
1601            /* left in last frame */
1602            if (cpi->lf_ref_frame[mb_offset-1] != INTRA_FRAME)
1603            {
1604                near_mvs[vcnt].as_int = cpi->lfmv[mb_offset -1].as_int;
1605                mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset -1], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1606                near_ref[vcnt] =  cpi->lf_ref_frame[mb_offset - 1];
1607            }
1608            vcnt++;
1609
1610            /* right in last frame */
1611            if (cpi->lf_ref_frame[mb_offset +1] != INTRA_FRAME)
1612            {
1613                near_mvs[vcnt].as_int = cpi->lfmv[mb_offset +1].as_int;
1614                mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset +1], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1615                near_ref[vcnt] =  cpi->lf_ref_frame[mb_offset +1];
1616            }
1617            vcnt++;
1618
1619            /* below in last frame */
1620            if (cpi->lf_ref_frame[mb_offset + xd->mode_info_stride +1] != INTRA_FRAME)
1621            {
1622                near_mvs[vcnt].as_int = cpi->lfmv[mb_offset + xd->mode_info_stride +1].as_int;
1623                mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset + xd->mode_info_stride +1], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1624                near_ref[vcnt] =  cpi->lf_ref_frame[mb_offset + xd->mode_info_stride +1];
1625            }
1626            vcnt++;
1627        }
1628
1629        for(i=0; i< vcnt; i++)
1630        {
1631            if(near_ref[near_sadidx[i]] != INTRA_FRAME)
1632            {
1633                if(here->mbmi.ref_frame == near_ref[near_sadidx[i]])
1634                {
1635                    mv.as_int = near_mvs[near_sadidx[i]].as_int;
1636                    find = 1;
1637                    if (i < 3)
1638                        *sr = 3;
1639                    else
1640                        *sr = 2;
1641                    break;
1642                }
1643            }
1644        }
1645
1646        if(!find)
1647        {
1648            for(i=0; i<vcnt; i++)
1649            {
1650                mvx[i] = near_mvs[i].as_mv.row;
1651                mvy[i] = near_mvs[i].as_mv.col;
1652            }
1653
1654            insertsortmv(mvx, vcnt);
1655            insertsortmv(mvy, vcnt);
1656            mv.as_mv.row = mvx[vcnt/2];
1657            mv.as_mv.col = mvy[vcnt/2];
1658
1659            find = 1;
1660            /* sr is set to 0 to allow calling function to decide the search
1661             * range.
1662             */
1663            *sr = 0;
1664        }
1665    }
1666
1667    /* Set up return values */
1668    mvp->as_int = mv.as_int;
1669    vp8_clamp_mv2(mvp, xd);
1670}
1671
1672void vp8_cal_sad(VP8_COMP *cpi, MACROBLOCKD *xd, MACROBLOCK *x, int recon_yoffset, int near_sadidx[])
1673{
1674    /* near_sad indexes:
1675     *   0-cf above, 1-cf left, 2-cf aboveleft,
1676     *   3-lf current, 4-lf above, 5-lf left, 6-lf right, 7-lf below
1677     */
1678    int near_sad[8] = {0};
1679    BLOCK *b = &x->block[0];
1680    unsigned char *src_y_ptr = *(b->base_src);
1681
1682    /* calculate sad for current frame 3 nearby MBs. */
1683    if( xd->mb_to_top_edge==0 && xd->mb_to_left_edge ==0)
1684    {
1685        near_sad[0] = near_sad[1] = near_sad[2] = INT_MAX;
1686    }else if(xd->mb_to_top_edge==0)
1687    {   /* only has left MB for sad calculation. */
1688        near_sad[0] = near_sad[2] = INT_MAX;
1689        near_sad[1] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, xd->dst.y_buffer - 16,xd->dst.y_stride, UINT_MAX);
1690    }else if(xd->mb_to_left_edge ==0)
1691    {   /* only has left MB for sad calculation. */
1692        near_sad[1] = near_sad[2] = INT_MAX;
1693        near_sad[0] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, xd->dst.y_buffer - xd->dst.y_stride *16,xd->dst.y_stride, UINT_MAX);
1694    }else
1695    {
1696        near_sad[0] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, xd->dst.y_buffer - xd->dst.y_stride *16,xd->dst.y_stride, UINT_MAX);
1697        near_sad[1] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, xd->dst.y_buffer - 16,xd->dst.y_stride, UINT_MAX);
1698        near_sad[2] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, xd->dst.y_buffer - xd->dst.y_stride *16 -16,xd->dst.y_stride, UINT_MAX);
1699    }
1700
1701    if(cpi->common.last_frame_type != KEY_FRAME)
1702    {
1703        /* calculate sad for last frame 5 nearby MBs. */
1704        unsigned char *pre_y_buffer = cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_buffer + recon_yoffset;
1705        int pre_y_stride = cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_stride;
1706
1707        if(xd->mb_to_top_edge==0) near_sad[4] = INT_MAX;
1708        if(xd->mb_to_left_edge ==0) near_sad[5] = INT_MAX;
1709        if(xd->mb_to_right_edge ==0) near_sad[6] = INT_MAX;
1710        if(xd->mb_to_bottom_edge==0) near_sad[7] = INT_MAX;
1711
1712        if(near_sad[4] != INT_MAX)
1713            near_sad[4] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, pre_y_buffer - pre_y_stride *16, pre_y_stride, UINT_MAX);
1714        if(near_sad[5] != INT_MAX)
1715            near_sad[5] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, pre_y_buffer - 16, pre_y_stride, UINT_MAX);
1716        near_sad[3] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, pre_y_buffer, pre_y_stride, UINT_MAX);
1717        if(near_sad[6] != INT_MAX)
1718            near_sad[6] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, pre_y_buffer + 16, pre_y_stride, UINT_MAX);
1719        if(near_sad[7] != INT_MAX)
1720            near_sad[7] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, pre_y_buffer + pre_y_stride *16, pre_y_stride, UINT_MAX);
1721    }
1722
1723    if(cpi->common.last_frame_type != KEY_FRAME)
1724    {
1725        insertsortsad(near_sad, near_sadidx, 8);
1726    }else
1727    {
1728        insertsortsad(near_sad, near_sadidx, 3);
1729    }
1730}
1731
1732static void rd_update_mvcount(MACROBLOCK *x, int_mv *best_ref_mv)
1733{
1734    if (x->e_mbd.mode_info_context->mbmi.mode == SPLITMV)
1735    {
1736        int i;
1737
1738        for (i = 0; i < x->partition_info->count; i++)
1739        {
1740            if (x->partition_info->bmi[i].mode == NEW4X4)
1741            {
1742                x->MVcount[0][mv_max+((x->partition_info->bmi[i].mv.as_mv.row
1743                                          - best_ref_mv->as_mv.row) >> 1)]++;
1744                x->MVcount[1][mv_max+((x->partition_info->bmi[i].mv.as_mv.col
1745                                          - best_ref_mv->as_mv.col) >> 1)]++;
1746            }
1747        }
1748    }
1749    else if (x->e_mbd.mode_info_context->mbmi.mode == NEWMV)
1750    {
1751        x->MVcount[0][mv_max+((x->e_mbd.mode_info_context->mbmi.mv.as_mv.row
1752                                          - best_ref_mv->as_mv.row) >> 1)]++;
1753        x->MVcount[1][mv_max+((x->e_mbd.mode_info_context->mbmi.mv.as_mv.col
1754                                          - best_ref_mv->as_mv.col) >> 1)]++;
1755    }
1756}
1757
1758static int evaluate_inter_mode_rd(int mdcounts[4],
1759                                  RATE_DISTORTION* rd,
1760                                  int* disable_skip,
1761                                  VP8_COMP *cpi, MACROBLOCK *x)
1762{
1763    MB_PREDICTION_MODE this_mode = x->e_mbd.mode_info_context->mbmi.mode;
1764    BLOCK *b = &x->block[0];
1765    MACROBLOCKD *xd = &x->e_mbd;
1766    int distortion;
1767    vp8_build_inter16x16_predictors_mby(&x->e_mbd, x->e_mbd.predictor, 16);
1768
1769    if (cpi->active_map_enabled && x->active_ptr[0] == 0) {
1770        x->skip = 1;
1771    }
1772    else if (x->encode_breakout)
1773    {
1774        unsigned int sse;
1775        unsigned int var;
1776        unsigned int threshold = (xd->block[0].dequant[1]
1777                    * xd->block[0].dequant[1] >>4);
1778
1779        if(threshold < x->encode_breakout)
1780            threshold = x->encode_breakout;
1781
1782        var = vp8_variance16x16
1783                (*(b->base_src), b->src_stride,
1784                x->e_mbd.predictor, 16, &sse);
1785
1786        if (sse < threshold)
1787        {
1788             unsigned int q2dc = xd->block[24].dequant[0];
1789            /* If theres is no codeable 2nd order dc
1790               or a very small uniform pixel change change */
1791            if ((sse - var < q2dc * q2dc >>4) ||
1792                (sse /2 > var && sse-var < 64))
1793            {
1794                /* Check u and v to make sure skip is ok */
1795                unsigned int sse2 = VP8_UVSSE(x);
1796                if (sse2 * 2 < threshold)
1797                {
1798                    x->skip = 1;
1799                    rd->distortion2 = sse + sse2;
1800                    rd->rate2 = 500;
1801
1802                    /* for best_yrd calculation */
1803                    rd->rate_uv = 0;
1804                    rd->distortion_uv = sse2;
1805
1806                    *disable_skip = 1;
1807                    return RDCOST(x->rdmult, x->rddiv, rd->rate2,
1808                                  rd->distortion2);
1809                }
1810            }
1811        }
1812    }
1813
1814
1815    /* Add in the Mv/mode cost */
1816    rd->rate2 += vp8_cost_mv_ref(this_mode, mdcounts);
1817
1818    /* Y cost and distortion */
1819    macro_block_yrd(x, &rd->rate_y, &distortion);
1820    rd->rate2 += rd->rate_y;
1821    rd->distortion2 += distortion;
1822
1823    /* UV cost and distortion */
1824    rd_inter16x16_uv(cpi, x, &rd->rate_uv, &rd->distortion_uv,
1825                     cpi->common.full_pixel);
1826    rd->rate2 += rd->rate_uv;
1827    rd->distortion2 += rd->distortion_uv;
1828    return INT_MAX;
1829}
1830
1831static int calculate_final_rd_costs(int this_rd,
1832                                    RATE_DISTORTION* rd,
1833                                    int* other_cost,
1834                                    int disable_skip,
1835                                    int uv_intra_tteob,
1836                                    int intra_rd_penalty,
1837                                    VP8_COMP *cpi, MACROBLOCK *x)
1838{
1839    MB_PREDICTION_MODE this_mode = x->e_mbd.mode_info_context->mbmi.mode;
1840
1841    /* Where skip is allowable add in the default per mb cost for the no
1842     * skip case. where we then decide to skip we have to delete this and
1843     * replace it with the cost of signalling a skip
1844     */
1845    if (cpi->common.mb_no_coeff_skip)
1846    {
1847        *other_cost += vp8_cost_bit(cpi->prob_skip_false, 0);
1848        rd->rate2 += *other_cost;
1849    }
1850
1851    /* Estimate the reference frame signaling cost and add it
1852     * to the rolling cost variable.
1853     */
1854    rd->rate2 +=
1855        x->ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame];
1856
1857    if (!disable_skip)
1858    {
1859        /* Test for the condition where skip block will be activated
1860         * because there are no non zero coefficients and make any
1861         * necessary adjustment for rate
1862         */
1863        if (cpi->common.mb_no_coeff_skip)
1864        {
1865            int i;
1866            int tteob;
1867            int has_y2_block = (this_mode!=SPLITMV && this_mode!=B_PRED);
1868
1869            tteob = 0;
1870            if(has_y2_block)
1871                tteob += x->e_mbd.eobs[24];
1872
1873            for (i = 0; i < 16; i++)
1874                tteob += (x->e_mbd.eobs[i] > has_y2_block);
1875
1876            if (x->e_mbd.mode_info_context->mbmi.ref_frame)
1877            {
1878                for (i = 16; i < 24; i++)
1879                    tteob += x->e_mbd.eobs[i];
1880            }
1881            else
1882                tteob += uv_intra_tteob;
1883
1884            if (tteob == 0)
1885            {
1886                rd->rate2 -= (rd->rate_y + rd->rate_uv);
1887                /* for best_yrd calculation */
1888                rd->rate_uv = 0;
1889
1890                /* Back out no skip flag costing and add in skip flag costing */
1891                if (cpi->prob_skip_false)
1892                {
1893                    int prob_skip_cost;
1894
1895                    prob_skip_cost = vp8_cost_bit(cpi->prob_skip_false, 1);
1896                    prob_skip_cost -= vp8_cost_bit(cpi->prob_skip_false, 0);
1897                    rd->rate2 += prob_skip_cost;
1898                    *other_cost += prob_skip_cost;
1899                }
1900            }
1901        }
1902        /* Calculate the final RD estimate for this mode */
1903        this_rd = RDCOST(x->rdmult, x->rddiv, rd->rate2, rd->distortion2);
1904        if (this_rd < INT_MAX && x->e_mbd.mode_info_context->mbmi.ref_frame
1905                                 == INTRA_FRAME)
1906            this_rd += intra_rd_penalty;
1907    }
1908    return this_rd;
1909}
1910
1911static void update_best_mode(BEST_MODE* best_mode, int this_rd,
1912                             RATE_DISTORTION* rd, int other_cost, MACROBLOCK *x)
1913{
1914    MB_PREDICTION_MODE this_mode = x->e_mbd.mode_info_context->mbmi.mode;
1915
1916    other_cost +=
1917    x->ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame];
1918
1919    /* Calculate the final y RD estimate for this mode */
1920    best_mode->yrd = RDCOST(x->rdmult, x->rddiv, (rd->rate2-rd->rate_uv-other_cost),
1921                      (rd->distortion2-rd->distortion_uv));
1922
1923    best_mode->rd = this_rd;
1924    vpx_memcpy(&best_mode->mbmode, &x->e_mbd.mode_info_context->mbmi, sizeof(MB_MODE_INFO));
1925    vpx_memcpy(&best_mode->partition, x->partition_info, sizeof(PARTITION_INFO));
1926
1927    if ((this_mode == B_PRED) || (this_mode == SPLITMV))
1928    {
1929        int i;
1930        for (i = 0; i < 16; i++)
1931        {
1932            best_mode->bmodes[i] = x->e_mbd.block[i].bmi;
1933        }
1934    }
1935}
1936
1937void vp8_rd_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset,
1938                            int recon_uvoffset, int *returnrate,
1939                            int *returndistortion, int *returnintra)
1940{
1941    BLOCK *b = &x->block[0];
1942    BLOCKD *d = &x->e_mbd.block[0];
1943    MACROBLOCKD *xd = &x->e_mbd;
1944    int_mv best_ref_mv_sb[2];
1945    int_mv mode_mv_sb[2][MB_MODE_COUNT];
1946    int_mv best_ref_mv;
1947    int_mv *mode_mv;
1948    MB_PREDICTION_MODE this_mode;
1949    int num00;
1950    int best_mode_index = 0;
1951    BEST_MODE best_mode;
1952
1953    int i;
1954    int mode_index;
1955    int mdcounts[4];
1956    int rate;
1957    RATE_DISTORTION rd;
1958    int uv_intra_rate, uv_intra_distortion, uv_intra_rate_tokenonly;
1959    int uv_intra_tteob = 0;
1960    int uv_intra_done = 0;
1961
1962    MB_PREDICTION_MODE uv_intra_mode = 0;
1963    int_mv mvp;
1964    int near_sadidx[8] = {0, 1, 2, 3, 4, 5, 6, 7};
1965    int saddone=0;
1966    /* search range got from mv_pred(). It uses step_param levels. (0-7) */
1967    int sr=0;
1968
1969    unsigned char *plane[4][3];
1970    int ref_frame_map[4];
1971    int sign_bias = 0;
1972
1973    int intra_rd_penalty =  10* vp8_dc_quant(cpi->common.base_qindex,
1974                                             cpi->common.y1dc_delta_q);
1975
1976#if CONFIG_TEMPORAL_DENOISING
1977    unsigned int zero_mv_sse = INT_MAX, best_sse = INT_MAX,
1978            best_rd_sse = INT_MAX;
1979#endif
1980
1981    mode_mv = mode_mv_sb[sign_bias];
1982    best_ref_mv.as_int = 0;
1983    best_mode.rd = INT_MAX;
1984    best_mode.yrd = INT_MAX;
1985    best_mode.intra_rd = INT_MAX;
1986    vpx_memset(mode_mv_sb, 0, sizeof(mode_mv_sb));
1987    vpx_memset(&best_mode.mbmode, 0, sizeof(best_mode.mbmode));
1988    vpx_memset(&best_mode.bmodes, 0, sizeof(best_mode.bmodes));
1989
1990    /* Setup search priorities */
1991    get_reference_search_order(cpi, ref_frame_map);
1992
1993    /* Check to see if there is at least 1 valid reference frame that we need
1994     * to calculate near_mvs.
1995     */
1996    if (ref_frame_map[1] > 0)
1997    {
1998        sign_bias = vp8_find_near_mvs_bias(&x->e_mbd,
1999                                           x->e_mbd.mode_info_context,
2000                                           mode_mv_sb,
2001                                           best_ref_mv_sb,
2002                                           mdcounts,
2003                                           ref_frame_map[1],
2004                                           cpi->common.ref_frame_sign_bias);
2005
2006        mode_mv = mode_mv_sb[sign_bias];
2007        best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int;
2008    }
2009
2010    get_predictor_pointers(cpi, plane, recon_yoffset, recon_uvoffset);
2011
2012    *returnintra = INT_MAX;
2013    /* Count of the number of MBs tested so far this frame */
2014    x->mbs_tested_so_far++;
2015
2016    x->skip = 0;
2017
2018    for (mode_index = 0; mode_index < MAX_MODES; mode_index++)
2019    {
2020        int this_rd = INT_MAX;
2021        int disable_skip = 0;
2022        int other_cost = 0;
2023        int this_ref_frame = ref_frame_map[vp8_ref_frame_order[mode_index]];
2024
2025        /* Test best rd so far against threshold for trying this mode. */
2026        if (best_mode.rd <= x->rd_threshes[mode_index])
2027            continue;
2028
2029        if (this_ref_frame < 0)
2030            continue;
2031
2032        /* These variables hold are rolling total cost and distortion for
2033         * this mode
2034         */
2035        rd.rate2 = 0;
2036        rd.distortion2 = 0;
2037
2038        this_mode = vp8_mode_order[mode_index];
2039
2040        x->e_mbd.mode_info_context->mbmi.mode = this_mode;
2041        x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame;
2042
2043        /* Only consider ZEROMV/ALTREF_FRAME for alt ref frame,
2044         * unless ARNR filtering is enabled in which case we want
2045         * an unfiltered alternative
2046         */
2047        if (cpi->is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0))
2048        {
2049            if (this_mode != ZEROMV || x->e_mbd.mode_info_context->mbmi.ref_frame != ALTREF_FRAME)
2050                continue;
2051        }
2052
2053        /* everything but intra */
2054        if (x->e_mbd.mode_info_context->mbmi.ref_frame)
2055        {
2056            x->e_mbd.pre.y_buffer = plane[this_ref_frame][0];
2057            x->e_mbd.pre.u_buffer = plane[this_ref_frame][1];
2058            x->e_mbd.pre.v_buffer = plane[this_ref_frame][2];
2059
2060            if (sign_bias != cpi->common.ref_frame_sign_bias[this_ref_frame])
2061            {
2062                sign_bias = cpi->common.ref_frame_sign_bias[this_ref_frame];
2063                mode_mv = mode_mv_sb[sign_bias];
2064                best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int;
2065            }
2066        }
2067
2068        /* Check to see if the testing frequency for this mode is at its
2069         * max If so then prevent it from being tested and increase the
2070         * threshold for its testing
2071         */
2072        if (x->mode_test_hit_counts[mode_index] && (cpi->mode_check_freq[mode_index] > 1))
2073        {
2074            if (x->mbs_tested_so_far  <= cpi->mode_check_freq[mode_index] * x->mode_test_hit_counts[mode_index])
2075            {
2076                /* Increase the threshold for coding this mode to make it
2077                 * less likely to be chosen
2078                 */
2079                x->rd_thresh_mult[mode_index] += 4;
2080
2081                if (x->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
2082                    x->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
2083
2084                x->rd_threshes[mode_index] =
2085                    (cpi->rd_baseline_thresh[mode_index] >> 7) *
2086                    x->rd_thresh_mult[mode_index];
2087
2088                continue;
2089            }
2090        }
2091
2092        /* We have now reached the point where we are going to test the
2093         * current mode so increment the counter for the number of times
2094         * it has been tested
2095         */
2096        x->mode_test_hit_counts[mode_index] ++;
2097
2098        /* Experimental code. Special case for gf and arf zeromv modes.
2099         * Increase zbin size to supress noise
2100         */
2101        if (x->zbin_mode_boost_enabled)
2102        {
2103            if ( this_ref_frame == INTRA_FRAME )
2104                x->zbin_mode_boost = 0;
2105            else
2106            {
2107                if (vp8_mode_order[mode_index] == ZEROMV)
2108                {
2109                    if (this_ref_frame != LAST_FRAME)
2110                        x->zbin_mode_boost = GF_ZEROMV_ZBIN_BOOST;
2111                    else
2112                        x->zbin_mode_boost = LF_ZEROMV_ZBIN_BOOST;
2113                }
2114                else if (vp8_mode_order[mode_index] == SPLITMV)
2115                    x->zbin_mode_boost = 0;
2116                else
2117                    x->zbin_mode_boost = MV_ZBIN_BOOST;
2118            }
2119
2120            vp8_update_zbin_extra(cpi, x);
2121        }
2122
2123        if(!uv_intra_done && this_ref_frame == INTRA_FRAME)
2124        {
2125            rd_pick_intra_mbuv_mode(x, &uv_intra_rate,
2126                                    &uv_intra_rate_tokenonly,
2127                                    &uv_intra_distortion);
2128            uv_intra_mode = x->e_mbd.mode_info_context->mbmi.uv_mode;
2129
2130            /*
2131             * Total of the eobs is used later to further adjust rate2. Since uv
2132             * block's intra eobs will be overwritten when we check inter modes,
2133             * we need to save uv_intra_tteob here.
2134             */
2135            for (i = 16; i < 24; i++)
2136                uv_intra_tteob += x->e_mbd.eobs[i];
2137
2138            uv_intra_done = 1;
2139        }
2140
2141        switch (this_mode)
2142        {
2143        case B_PRED:
2144        {
2145            int tmp_rd;
2146
2147            /* Note the rate value returned here includes the cost of
2148             * coding the BPRED mode: x->mbmode_cost[x->e_mbd.frame_type][BPRED]
2149             */
2150            int distortion;
2151            tmp_rd = rd_pick_intra4x4mby_modes(x, &rate, &rd.rate_y, &distortion, best_mode.yrd);
2152            rd.rate2 += rate;
2153            rd.distortion2 += distortion;
2154
2155            if(tmp_rd < best_mode.yrd)
2156            {
2157                rd.rate2 += uv_intra_rate;
2158                rd.rate_uv = uv_intra_rate_tokenonly;
2159                rd.distortion2 += uv_intra_distortion;
2160                rd.distortion_uv = uv_intra_distortion;
2161            }
2162            else
2163            {
2164                this_rd = INT_MAX;
2165                disable_skip = 1;
2166            }
2167        }
2168        break;
2169
2170        case SPLITMV:
2171        {
2172            int tmp_rd;
2173            int this_rd_thresh;
2174            int distortion;
2175
2176            this_rd_thresh = (vp8_ref_frame_order[mode_index] == 1) ?
2177                x->rd_threshes[THR_NEW1] : x->rd_threshes[THR_NEW3];
2178            this_rd_thresh = (vp8_ref_frame_order[mode_index] == 2) ?
2179                x->rd_threshes[THR_NEW2] : this_rd_thresh;
2180
2181            tmp_rd = vp8_rd_pick_best_mbsegmentation(cpi, x, &best_ref_mv,
2182                                                     best_mode.yrd, mdcounts,
2183                                                     &rate, &rd.rate_y, &distortion, this_rd_thresh) ;
2184
2185            rd.rate2 += rate;
2186            rd.distortion2 += distortion;
2187
2188            /* If even the 'Y' rd value of split is higher than best so far
2189             * then dont bother looking at UV
2190             */
2191            if (tmp_rd < best_mode.yrd)
2192            {
2193                /* Now work out UV cost and add it in */
2194                rd_inter4x4_uv(cpi, x, &rd.rate_uv, &rd.distortion_uv, cpi->common.full_pixel);
2195                rd.rate2 += rd.rate_uv;
2196                rd.distortion2 += rd.distortion_uv;
2197            }
2198            else
2199            {
2200                this_rd = INT_MAX;
2201                disable_skip = 1;
2202            }
2203        }
2204        break;
2205        case DC_PRED:
2206        case V_PRED:
2207        case H_PRED:
2208        case TM_PRED:
2209        {
2210            int distortion;
2211            x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
2212
2213            vp8_build_intra_predictors_mby_s(xd,
2214                                             xd->dst.y_buffer - xd->dst.y_stride,
2215                                             xd->dst.y_buffer - 1,
2216                                             xd->dst.y_stride,
2217                                             xd->predictor,
2218                                             16);
2219            macro_block_yrd(x, &rd.rate_y, &distortion) ;
2220            rd.rate2 += rd.rate_y;
2221            rd.distortion2 += distortion;
2222            rd.rate2 += x->mbmode_cost[x->e_mbd.frame_type][x->e_mbd.mode_info_context->mbmi.mode];
2223            rd.rate2 += uv_intra_rate;
2224            rd.rate_uv = uv_intra_rate_tokenonly;
2225            rd.distortion2 += uv_intra_distortion;
2226            rd.distortion_uv = uv_intra_distortion;
2227        }
2228        break;
2229
2230        case NEWMV:
2231        {
2232            int thissme;
2233            int bestsme = INT_MAX;
2234            int step_param = cpi->sf.first_step;
2235            int further_steps;
2236            int n;
2237            int do_refine=1;   /* If last step (1-away) of n-step search doesn't pick the center point as the best match,
2238                                  we will do a final 1-away diamond refining search  */
2239
2240            int sadpb = x->sadperbit16;
2241            int_mv mvp_full;
2242
2243            int col_min = ((best_ref_mv.as_mv.col+7)>>3) - MAX_FULL_PEL_VAL;
2244            int row_min = ((best_ref_mv.as_mv.row+7)>>3) - MAX_FULL_PEL_VAL;
2245            int col_max = (best_ref_mv.as_mv.col>>3) + MAX_FULL_PEL_VAL;
2246            int row_max = (best_ref_mv.as_mv.row>>3) + MAX_FULL_PEL_VAL;
2247
2248            int tmp_col_min = x->mv_col_min;
2249            int tmp_col_max = x->mv_col_max;
2250            int tmp_row_min = x->mv_row_min;
2251            int tmp_row_max = x->mv_row_max;
2252
2253            if(!saddone)
2254            {
2255                vp8_cal_sad(cpi,xd,x, recon_yoffset ,&near_sadidx[0] );
2256                saddone = 1;
2257            }
2258
2259            vp8_mv_pred(cpi, &x->e_mbd, x->e_mbd.mode_info_context, &mvp,
2260                        x->e_mbd.mode_info_context->mbmi.ref_frame, cpi->common.ref_frame_sign_bias, &sr, &near_sadidx[0]);
2261
2262            mvp_full.as_mv.col = mvp.as_mv.col>>3;
2263            mvp_full.as_mv.row = mvp.as_mv.row>>3;
2264
2265            /* Get intersection of UMV window and valid MV window to
2266             * reduce # of checks in diamond search.
2267             */
2268            if (x->mv_col_min < col_min )
2269                x->mv_col_min = col_min;
2270            if (x->mv_col_max > col_max )
2271                x->mv_col_max = col_max;
2272            if (x->mv_row_min < row_min )
2273                x->mv_row_min = row_min;
2274            if (x->mv_row_max > row_max )
2275                x->mv_row_max = row_max;
2276
2277            /* adjust search range according to sr from mv prediction */
2278            if(sr > step_param)
2279                step_param = sr;
2280
2281            /* Initial step/diamond search */
2282            {
2283                bestsme = cpi->diamond_search_sad(x, b, d, &mvp_full, &d->bmi.mv,
2284                                        step_param, sadpb, &num00,
2285                                        &cpi->fn_ptr[BLOCK_16X16],
2286                                        x->mvcost, &best_ref_mv);
2287                mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
2288
2289                /* Further step/diamond searches as necessary */
2290                n = 0;
2291                further_steps = (cpi->sf.max_step_search_steps - 1) - step_param;
2292
2293                n = num00;
2294                num00 = 0;
2295
2296                /* If there won't be more n-step search, check to see if refining search is needed. */
2297                if (n > further_steps)
2298                    do_refine = 0;
2299
2300                while (n < further_steps)
2301                {
2302                    n++;
2303
2304                    if (num00)
2305                        num00--;
2306                    else
2307                    {
2308                        thissme = cpi->diamond_search_sad(x, b, d, &mvp_full,
2309                                    &d->bmi.mv, step_param + n, sadpb, &num00,
2310                                    &cpi->fn_ptr[BLOCK_16X16], x->mvcost,
2311                                    &best_ref_mv);
2312
2313                        /* check to see if refining search is needed. */
2314                        if (num00 > (further_steps-n))
2315                            do_refine = 0;
2316
2317                        if (thissme < bestsme)
2318                        {
2319                            bestsme = thissme;
2320                            mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
2321                        }
2322                        else
2323                        {
2324                            d->bmi.mv.as_int = mode_mv[NEWMV].as_int;
2325                        }
2326                    }
2327                }
2328            }
2329
2330            /* final 1-away diamond refining search */
2331            if (do_refine == 1)
2332            {
2333                int search_range;
2334
2335                search_range = 8;
2336
2337                thissme = cpi->refining_search_sad(x, b, d, &d->bmi.mv, sadpb,
2338                                       search_range, &cpi->fn_ptr[BLOCK_16X16],
2339                                       x->mvcost, &best_ref_mv);
2340
2341                if (thissme < bestsme)
2342                {
2343                    bestsme = thissme;
2344                    mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
2345                }
2346                else
2347                {
2348                    d->bmi.mv.as_int = mode_mv[NEWMV].as_int;
2349                }
2350            }
2351
2352            x->mv_col_min = tmp_col_min;
2353            x->mv_col_max = tmp_col_max;
2354            x->mv_row_min = tmp_row_min;
2355            x->mv_row_max = tmp_row_max;
2356
2357            if (bestsme < INT_MAX)
2358            {
2359                int dis; /* TODO: use dis in distortion calculation later. */
2360                unsigned int sse;
2361                cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv, &best_ref_mv,
2362                                             x->errorperbit,
2363                                             &cpi->fn_ptr[BLOCK_16X16],
2364                                             x->mvcost, &dis, &sse);
2365            }
2366
2367            mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
2368
2369            /* Add the new motion vector cost to our rolling cost variable */
2370            rd.rate2 += vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv, x->mvcost, 96);
2371        }
2372
2373        case NEARESTMV:
2374        case NEARMV:
2375            /* Clip "next_nearest" so that it does not extend to far out
2376             * of image
2377             */
2378            vp8_clamp_mv2(&mode_mv[this_mode], xd);
2379
2380            /* Do not bother proceeding if the vector (from newmv, nearest
2381             * or near) is 0,0 as this should then be coded using the zeromv
2382             * mode.
2383             */
2384            if (((this_mode == NEARMV) || (this_mode == NEARESTMV)) && (mode_mv[this_mode].as_int == 0))
2385                continue;
2386
2387        case ZEROMV:
2388
2389            /* Trap vectors that reach beyond the UMV borders
2390             * Note that ALL New MV, Nearest MV Near MV and Zero MV code
2391             * drops through to this point because of the lack of break
2392             * statements in the previous two cases.
2393             */
2394            if (((mode_mv[this_mode].as_mv.row >> 3) < x->mv_row_min) || ((mode_mv[this_mode].as_mv.row >> 3) > x->mv_row_max) ||
2395                ((mode_mv[this_mode].as_mv.col >> 3) < x->mv_col_min) || ((mode_mv[this_mode].as_mv.col >> 3) > x->mv_col_max))
2396                continue;
2397
2398            vp8_set_mbmode_and_mvs(x, this_mode, &mode_mv[this_mode]);
2399            this_rd = evaluate_inter_mode_rd(mdcounts, &rd,
2400                                             &disable_skip, cpi, x);
2401            break;
2402
2403        default:
2404            break;
2405        }
2406
2407        this_rd = calculate_final_rd_costs(this_rd, &rd, &other_cost,
2408                                           disable_skip, uv_intra_tteob,
2409                                           intra_rd_penalty, cpi, x);
2410
2411        /* Keep record of best intra distortion */
2412        if ((x->e_mbd.mode_info_context->mbmi.ref_frame == INTRA_FRAME) &&
2413            (this_rd < best_mode.intra_rd) )
2414        {
2415          best_mode.intra_rd = this_rd;
2416            *returnintra = rd.distortion2 ;
2417        }
2418#if CONFIG_TEMPORAL_DENOISING
2419        if (cpi->oxcf.noise_sensitivity)
2420        {
2421            unsigned int sse;
2422            vp8_get_inter_mbpred_error(x,&cpi->fn_ptr[BLOCK_16X16],&sse,
2423                                   mode_mv[this_mode]);
2424
2425            if (sse < best_rd_sse)
2426                best_rd_sse = sse;
2427
2428            /* Store for later use by denoiser. */
2429            if (this_mode == ZEROMV && sse < zero_mv_sse )
2430            {
2431                zero_mv_sse = sse;
2432                x->best_zeromv_reference_frame =
2433                        x->e_mbd.mode_info_context->mbmi.ref_frame;
2434            }
2435
2436            /* Store the best NEWMV in x for later use in the denoiser. */
2437            if (x->e_mbd.mode_info_context->mbmi.mode == NEWMV &&
2438                    sse < best_sse)
2439            {
2440                best_sse = sse;
2441                vp8_get_inter_mbpred_error(x,&cpi->fn_ptr[BLOCK_16X16],&best_sse,
2442                                       mode_mv[this_mode]);
2443                x->best_sse_inter_mode = NEWMV;
2444                x->best_sse_mv = x->e_mbd.mode_info_context->mbmi.mv;
2445                x->need_to_clamp_best_mvs =
2446                    x->e_mbd.mode_info_context->mbmi.need_to_clamp_mvs;
2447                x->best_reference_frame =
2448                    x->e_mbd.mode_info_context->mbmi.ref_frame;
2449            }
2450        }
2451#endif
2452
2453        /* Did this mode help.. i.i is it the new best mode */
2454        if (this_rd < best_mode.rd || x->skip)
2455        {
2456            /* Note index of best mode so far */
2457            best_mode_index = mode_index;
2458            *returnrate = rd.rate2;
2459            *returndistortion = rd.distortion2;
2460            if (this_mode <= B_PRED)
2461            {
2462                x->e_mbd.mode_info_context->mbmi.uv_mode = uv_intra_mode;
2463                /* required for left and above block mv */
2464                x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
2465            }
2466            update_best_mode(&best_mode, this_rd, &rd, other_cost, x);
2467
2468
2469            /* Testing this mode gave rise to an improvement in best error
2470             * score. Lower threshold a bit for next time
2471             */
2472            x->rd_thresh_mult[mode_index] =
2473                (x->rd_thresh_mult[mode_index] >= (MIN_THRESHMULT + 2)) ?
2474                    x->rd_thresh_mult[mode_index] - 2 : MIN_THRESHMULT;
2475        }
2476
2477        /* If the mode did not help improve the best error case then raise
2478         * the threshold for testing that mode next time around.
2479         */
2480        else
2481        {
2482            x->rd_thresh_mult[mode_index] += 4;
2483
2484            if (x->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
2485                x->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
2486        }
2487        x->rd_threshes[mode_index] =
2488            (cpi->rd_baseline_thresh[mode_index] >> 7) *
2489                x->rd_thresh_mult[mode_index];
2490
2491        if (x->skip)
2492            break;
2493
2494    }
2495
2496    /* Reduce the activation RD thresholds for the best choice mode */
2497    if ((cpi->rd_baseline_thresh[best_mode_index] > 0) && (cpi->rd_baseline_thresh[best_mode_index] < (INT_MAX >> 2)))
2498    {
2499        int best_adjustment = (x->rd_thresh_mult[best_mode_index] >> 2);
2500
2501        x->rd_thresh_mult[best_mode_index] =
2502            (x->rd_thresh_mult[best_mode_index] >=
2503                (MIN_THRESHMULT + best_adjustment)) ?
2504                    x->rd_thresh_mult[best_mode_index] - best_adjustment :
2505                    MIN_THRESHMULT;
2506        x->rd_threshes[best_mode_index] =
2507            (cpi->rd_baseline_thresh[best_mode_index] >> 7) *
2508                x->rd_thresh_mult[best_mode_index];
2509    }
2510
2511#if CONFIG_TEMPORAL_DENOISING
2512    if (cpi->oxcf.noise_sensitivity)
2513    {
2514        if (x->best_sse_inter_mode == DC_PRED)
2515        {
2516            /* No best MV found. */
2517            x->best_sse_inter_mode = best_mode.mbmode.mode;
2518            x->best_sse_mv = best_mode.mbmode.mv;
2519            x->need_to_clamp_best_mvs = best_mode.mbmode.need_to_clamp_mvs;
2520            x->best_reference_frame = best_mode.mbmode.ref_frame;
2521            best_sse = best_rd_sse;
2522        }
2523        vp8_denoiser_denoise_mb(&cpi->denoiser, x, best_sse, zero_mv_sse,
2524                                recon_yoffset, recon_uvoffset);
2525
2526
2527        /* Reevaluate ZEROMV after denoising. */
2528        if (best_mode.mbmode.ref_frame == INTRA_FRAME &&
2529            x->best_zeromv_reference_frame != INTRA_FRAME)
2530        {
2531            int this_rd = INT_MAX;
2532            int disable_skip = 0;
2533            int other_cost = 0;
2534            int this_ref_frame = x->best_zeromv_reference_frame;
2535            rd.rate2 = x->ref_frame_cost[this_ref_frame] +
2536                    vp8_cost_mv_ref(ZEROMV, mdcounts);
2537            rd.distortion2 = 0;
2538
2539            /* set up the proper prediction buffers for the frame */
2540            x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame;
2541            x->e_mbd.pre.y_buffer = plane[this_ref_frame][0];
2542            x->e_mbd.pre.u_buffer = plane[this_ref_frame][1];
2543            x->e_mbd.pre.v_buffer = plane[this_ref_frame][2];
2544
2545            x->e_mbd.mode_info_context->mbmi.mode = ZEROMV;
2546            x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
2547            x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
2548
2549            this_rd = evaluate_inter_mode_rd(mdcounts, &rd, &disable_skip, cpi, x);
2550            this_rd = calculate_final_rd_costs(this_rd, &rd, &other_cost,
2551                                               disable_skip, uv_intra_tteob,
2552                                               intra_rd_penalty, cpi, x);
2553            if (this_rd < best_mode.rd || x->skip)
2554            {
2555                /* Note index of best mode so far */
2556                best_mode_index = mode_index;
2557                *returnrate = rd.rate2;
2558                *returndistortion = rd.distortion2;
2559                update_best_mode(&best_mode, this_rd, &rd, other_cost, x);
2560            }
2561        }
2562
2563    }
2564#endif
2565
2566    if (cpi->is_src_frame_alt_ref &&
2567        (best_mode.mbmode.mode != ZEROMV || best_mode.mbmode.ref_frame != ALTREF_FRAME))
2568    {
2569        x->e_mbd.mode_info_context->mbmi.mode = ZEROMV;
2570        x->e_mbd.mode_info_context->mbmi.ref_frame = ALTREF_FRAME;
2571        x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
2572        x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
2573        x->e_mbd.mode_info_context->mbmi.mb_skip_coeff =
2574                                        (cpi->common.mb_no_coeff_skip);
2575        x->e_mbd.mode_info_context->mbmi.partitioning = 0;
2576        return;
2577    }
2578
2579
2580    /* macroblock modes */
2581    vpx_memcpy(&x->e_mbd.mode_info_context->mbmi, &best_mode.mbmode, sizeof(MB_MODE_INFO));
2582
2583    if (best_mode.mbmode.mode == B_PRED)
2584    {
2585        for (i = 0; i < 16; i++)
2586            xd->mode_info_context->bmi[i].as_mode = best_mode.bmodes[i].as_mode;
2587    }
2588
2589    if (best_mode.mbmode.mode == SPLITMV)
2590    {
2591        for (i = 0; i < 16; i++)
2592            xd->mode_info_context->bmi[i].mv.as_int = best_mode.bmodes[i].mv.as_int;
2593
2594        vpx_memcpy(x->partition_info, &best_mode.partition, sizeof(PARTITION_INFO));
2595
2596        x->e_mbd.mode_info_context->mbmi.mv.as_int =
2597                                      x->partition_info->bmi[15].mv.as_int;
2598    }
2599
2600    if (sign_bias
2601        != cpi->common.ref_frame_sign_bias[xd->mode_info_context->mbmi.ref_frame])
2602        best_ref_mv.as_int = best_ref_mv_sb[!sign_bias].as_int;
2603
2604    rd_update_mvcount(x, &best_ref_mv);
2605}
2606
2607void vp8_rd_pick_intra_mode(MACROBLOCK *x, int *rate_)
2608{
2609    int error4x4, error16x16;
2610    int rate4x4, rate16x16 = 0, rateuv;
2611    int dist4x4, dist16x16, distuv;
2612    int rate;
2613    int rate4x4_tokenonly = 0;
2614    int rate16x16_tokenonly = 0;
2615    int rateuv_tokenonly = 0;
2616
2617    x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
2618
2619    rd_pick_intra_mbuv_mode(x, &rateuv, &rateuv_tokenonly, &distuv);
2620    rate = rateuv;
2621
2622    error16x16 = rd_pick_intra16x16mby_mode(x, &rate16x16, &rate16x16_tokenonly,
2623                                            &dist16x16);
2624
2625    error4x4 = rd_pick_intra4x4mby_modes(x, &rate4x4, &rate4x4_tokenonly,
2626                                         &dist4x4, error16x16);
2627
2628    if (error4x4 < error16x16)
2629    {
2630        x->e_mbd.mode_info_context->mbmi.mode = B_PRED;
2631        rate += rate4x4;
2632    }
2633    else
2634    {
2635        rate += rate16x16;
2636    }
2637
2638    *rate_ = rate;
2639}
2640