Improved rd_pick_intra4x4block
[aom.git] / vp8 / encoder / rdopt.c
blob0d01d64b5c35b7830a3d53efacaf361fc4c493e1
1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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 */
12 #include <stdio.h>
13 #include <math.h>
14 #include <limits.h>
15 #include <assert.h>
16 #include "pragmas.h"
18 #include "tokenize.h"
19 #include "treewriter.h"
20 #include "onyx_int.h"
21 #include "modecosts.h"
22 #include "encodeintra.h"
23 #include "entropymode.h"
24 #include "reconinter.h"
25 #include "reconintra.h"
26 #include "reconintra4x4.h"
27 #include "findnearmv.h"
28 #include "encodemb.h"
29 #include "quantize.h"
30 #include "idct.h"
31 #include "g_common.h"
32 #include "variance.h"
33 #include "mcomp.h"
35 #include "vpx_mem/vpx_mem.h"
36 #include "dct.h"
37 #include "systemdependent.h"
39 #if CONFIG_RUNTIME_CPU_DETECT
40 #define IF_RTCD(x) (x)
41 #else
42 #define IF_RTCD(x) NULL
43 #endif
46 extern void vp8cx_mb_init_quantizer(VP8_COMP *cpi, MACROBLOCK *x);
47 extern void vp8_update_zbin_extra(VP8_COMP *cpi, MACROBLOCK *x);
50 #define RDCOST(RM,DM,R,D) ( ((128+(R)*(RM)) >> 8) + (DM)*(D) )
52 #define MAXF(a,b) (((a) > (b)) ? (a) : (b))
56 const int vp8_auto_speed_thresh[17] =
58 1000,
59 200,
60 150,
61 130,
62 150,
63 125,
64 120,
65 115,
66 115,
67 115,
68 115,
69 115,
70 115,
71 115,
72 115,
73 115,
74 105
77 const MB_PREDICTION_MODE vp8_mode_order[MAX_MODES] =
79 ZEROMV,
80 DC_PRED,
82 NEARESTMV,
83 NEARMV,
85 ZEROMV,
86 NEARESTMV,
88 ZEROMV,
89 NEARESTMV,
91 NEARMV,
92 NEARMV,
94 V_PRED,
95 H_PRED,
96 TM_PRED,
98 NEWMV,
99 NEWMV,
100 NEWMV,
102 SPLITMV,
103 SPLITMV,
104 SPLITMV,
106 B_PRED,
109 const MV_REFERENCE_FRAME vp8_ref_frame_order[MAX_MODES] =
111 LAST_FRAME,
112 INTRA_FRAME,
114 LAST_FRAME,
115 LAST_FRAME,
117 GOLDEN_FRAME,
118 GOLDEN_FRAME,
120 ALTREF_FRAME,
121 ALTREF_FRAME,
123 GOLDEN_FRAME,
124 ALTREF_FRAME,
126 INTRA_FRAME,
127 INTRA_FRAME,
128 INTRA_FRAME,
130 LAST_FRAME,
131 GOLDEN_FRAME,
132 ALTREF_FRAME,
134 LAST_FRAME,
135 GOLDEN_FRAME,
136 ALTREF_FRAME,
138 INTRA_FRAME,
141 static void fill_token_costs(
142 unsigned int c [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [vp8_coef_tokens],
143 const vp8_prob p [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [vp8_coef_tokens-1]
146 int i, j, k;
149 for (i = 0; i < BLOCK_TYPES; i++)
150 for (j = 0; j < COEF_BANDS; j++)
151 for (k = 0; k < PREV_COEF_CONTEXTS; k++)
153 vp8_cost_tokens((int *)(c [i][j][k]), p [i][j][k], vp8_coef_tree);
157 static int rd_iifactor [ 32 ] = { 4, 4, 3, 2, 1, 0, 0, 0,
158 0, 0, 0, 0, 0, 0, 0, 0,
159 0, 0, 0, 0, 0, 0, 0, 0,
160 0, 0, 0, 0, 0, 0, 0, 0,
164 /* values are now correlated to quantizer */
165 static int sad_per_bit16lut[QINDEX_RANGE] =
167 5, 5, 5, 5, 5, 5, 6, 6,
168 6, 6, 6, 6, 6, 7, 7, 7,
169 7, 7, 7, 7, 8, 8, 8, 8,
170 8, 8, 8, 8, 8, 8, 9, 9,
171 9, 9, 9, 9, 10, 10, 10, 10,
172 10, 10, 11, 11, 11, 11, 11, 11,
173 12, 12, 12, 12, 12, 12, 12, 13,
174 13, 13, 13, 13, 13, 14, 14, 14,
175 14, 14, 15, 15, 15, 15, 15, 15,
176 16, 16, 16, 16, 16, 16, 17, 17,
177 17, 17, 17, 17, 17, 18, 18, 18,
178 18, 18, 19, 19, 19, 19, 19, 19,
179 20, 20, 20, 21, 21, 21, 21, 22,
180 22, 22, 23, 23, 23, 24, 24, 24,
181 25, 25, 26, 26, 27, 27, 27, 28,
182 28, 28, 29, 29, 30, 30, 31, 31
184 static int sad_per_bit4lut[QINDEX_RANGE] =
186 5, 5, 5, 5, 5, 5, 7, 7,
187 7, 7, 7, 7, 7, 8, 8, 8,
188 8, 8, 8, 8, 10, 10, 10, 10,
189 10, 10, 10, 10, 10, 10, 11, 11,
190 11, 11, 11, 11, 13, 13, 13, 13,
191 13, 13, 14, 14, 14, 14, 14, 14,
192 16, 16, 16, 16, 16, 16, 16, 17,
193 17, 17, 17, 17, 17, 19, 19, 19,
194 19, 19, 20, 20, 20, 20, 20, 20,
195 22, 22, 22, 22, 22, 22, 23, 23,
196 23, 23, 23, 23, 23, 25, 25, 25,
197 25, 25, 26, 26, 26, 26, 26, 26,
198 28, 28, 28, 29, 29, 29, 29, 31,
199 31, 31, 32, 32, 32, 34, 34, 34,
200 35, 35, 37, 37, 38, 38, 38, 40,
201 40, 40, 41, 41, 43, 43, 44, 44,
204 void vp8cx_initialize_me_consts(VP8_COMP *cpi, int QIndex)
206 cpi->mb.sadperbit16 = sad_per_bit16lut[QIndex];
207 cpi->mb.sadperbit4 = sad_per_bit4lut[QIndex];
210 void vp8_initialize_rd_consts(VP8_COMP *cpi, int Qvalue)
212 int q;
213 int i;
214 double capped_q = (Qvalue < 160) ? (double)Qvalue : 160.0;
215 double rdconst = 3.00;
217 vp8_clear_system_state(); //__asm emms;
219 // Further tests required to see if optimum is different
220 // for key frames, golden frames and arf frames.
221 // if (cpi->common.refresh_golden_frame ||
222 // cpi->common.refresh_alt_ref_frame)
223 cpi->RDMULT = (int)(rdconst * (capped_q * capped_q));
225 // Extend rate multiplier along side quantizer zbin increases
226 if (cpi->zbin_over_quant > 0)
228 double oq_factor;
229 double modq;
231 // Experimental code using the same basic equation as used for Q above
232 // The units of cpi->zbin_over_quant are 1/128 of Q bin size
233 oq_factor = 1.0 + ((double)0.0015625 * cpi->zbin_over_quant);
234 modq = (int)((double)capped_q * oq_factor);
235 cpi->RDMULT = (int)(rdconst * (modq * modq));
238 if (cpi->pass == 2 && (cpi->common.frame_type != KEY_FRAME))
240 if (cpi->next_iiratio > 31)
241 cpi->RDMULT += (cpi->RDMULT * rd_iifactor[31]) >> 4;
242 else
243 cpi->RDMULT += (cpi->RDMULT * rd_iifactor[cpi->next_iiratio]) >> 4;
246 cpi->mb.errorperbit = (cpi->RDMULT / 100);
247 cpi->mb.errorperbit += (cpi->mb.errorperbit==0);
249 vp8_set_speed_features(cpi);
251 if (cpi->common.simpler_lpf)
252 cpi->common.filter_type = SIMPLE_LOOPFILTER;
254 q = (int)pow(Qvalue, 1.25);
256 if (q < 8)
257 q = 8;
259 if (cpi->RDMULT > 1000)
261 cpi->RDDIV = 1;
262 cpi->RDMULT /= 100;
264 for (i = 0; i < MAX_MODES; i++)
266 if (cpi->sf.thresh_mult[i] < INT_MAX)
268 cpi->rd_threshes[i] = cpi->sf.thresh_mult[i] * q / 100;
270 else
272 cpi->rd_threshes[i] = INT_MAX;
275 cpi->rd_baseline_thresh[i] = cpi->rd_threshes[i];
278 else
280 cpi->RDDIV = 100;
282 for (i = 0; i < MAX_MODES; i++)
284 if (cpi->sf.thresh_mult[i] < (INT_MAX / q))
286 cpi->rd_threshes[i] = cpi->sf.thresh_mult[i] * q;
288 else
290 cpi->rd_threshes[i] = INT_MAX;
293 cpi->rd_baseline_thresh[i] = cpi->rd_threshes[i];
297 fill_token_costs(
298 cpi->mb.token_costs,
299 (const vp8_prob( *)[8][3][11]) cpi->common.fc.coef_probs
302 vp8_init_mode_costs(cpi);
306 void vp8_auto_select_speed(VP8_COMP *cpi)
308 int used = cpi->oxcf.cpu_used;
310 int milliseconds_for_compress = (int)(1000000 / cpi->oxcf.frame_rate);
312 milliseconds_for_compress = milliseconds_for_compress * (16 - cpi->oxcf.cpu_used) / 16;
314 #if 0
316 if (0)
318 FILE *f;
320 f = fopen("speed.stt", "a");
321 fprintf(f, " %8ld %10ld %10ld %10ld\n",
322 cpi->common.current_video_frame, cpi->Speed, milliseconds_for_compress, cpi->avg_pick_mode_time);
323 fclose(f);
326 #endif
329 // this is done during parameter valid check
330 if( used > 16)
331 used = 16;
332 if( used < -16)
333 used = -16;
336 if (cpi->avg_pick_mode_time < milliseconds_for_compress && (cpi->avg_encode_time - cpi->avg_pick_mode_time) < milliseconds_for_compress)
338 if (cpi->avg_pick_mode_time == 0)
340 cpi->Speed = 4;
342 else
344 if (milliseconds_for_compress * 100 < cpi->avg_encode_time * 95)
346 cpi->Speed += 2;
347 cpi->avg_pick_mode_time = 0;
348 cpi->avg_encode_time = 0;
350 if (cpi->Speed > 16)
352 cpi->Speed = 16;
356 if (milliseconds_for_compress * 100 > cpi->avg_encode_time * vp8_auto_speed_thresh[cpi->Speed])
358 cpi->Speed -= 1;
359 cpi->avg_pick_mode_time = 0;
360 cpi->avg_encode_time = 0;
362 // In real-time mode, cpi->speed is in [4, 16].
363 if (cpi->Speed < 4) //if ( cpi->Speed < 0 )
365 cpi->Speed = 4; //cpi->Speed = 0;
370 else
372 cpi->Speed += 4;
374 if (cpi->Speed > 16)
375 cpi->Speed = 16;
378 cpi->avg_pick_mode_time = 0;
379 cpi->avg_encode_time = 0;
383 int vp8_block_error_c(short *coeff, short *dqcoeff)
385 int i;
386 int error = 0;
388 for (i = 0; i < 16; i++)
390 int this_diff = coeff[i] - dqcoeff[i];
391 error += this_diff * this_diff;
394 return error;
397 int vp8_mbblock_error_c(MACROBLOCK *mb, int dc)
399 BLOCK *be;
400 BLOCKD *bd;
401 int i, j;
402 int berror, error = 0;
404 for (i = 0; i < 16; i++)
406 be = &mb->block[i];
407 bd = &mb->e_mbd.block[i];
409 berror = 0;
411 for (j = dc; j < 16; j++)
413 int this_diff = be->coeff[j] - bd->dqcoeff[j];
414 berror += this_diff * this_diff;
417 error += berror;
420 return error;
423 int vp8_mbuverror_c(MACROBLOCK *mb)
426 BLOCK *be;
427 BLOCKD *bd;
430 int i;
431 int error = 0;
433 for (i = 16; i < 24; i++)
435 be = &mb->block[i];
436 bd = &mb->e_mbd.block[i];
438 error += vp8_block_error_c(be->coeff, bd->dqcoeff);
441 return error;
444 int VP8_UVSSE(MACROBLOCK *x, const vp8_variance_rtcd_vtable_t *rtcd)
446 unsigned char *uptr, *vptr;
447 unsigned char *upred_ptr = (*(x->block[16].base_src) + x->block[16].src);
448 unsigned char *vpred_ptr = (*(x->block[20].base_src) + x->block[20].src);
449 int uv_stride = x->block[16].src_stride;
451 unsigned int sse1 = 0;
452 unsigned int sse2 = 0;
453 int mv_row;
454 int mv_col;
455 int offset;
456 int pre_stride = x->e_mbd.block[16].pre_stride;
458 vp8_build_uvmvs(&x->e_mbd, 0);
459 mv_row = x->e_mbd.block[16].bmi.mv.as_mv.row;
460 mv_col = x->e_mbd.block[16].bmi.mv.as_mv.col;
462 offset = (mv_row >> 3) * pre_stride + (mv_col >> 3);
463 uptr = x->e_mbd.pre.u_buffer + offset;
464 vptr = x->e_mbd.pre.v_buffer + offset;
466 if ((mv_row | mv_col) & 7)
468 VARIANCE_INVOKE(rtcd, subpixvar8x8)(uptr, pre_stride, mv_col & 7, mv_row & 7, upred_ptr, uv_stride, &sse2);
469 VARIANCE_INVOKE(rtcd, subpixvar8x8)(vptr, pre_stride, mv_col & 7, mv_row & 7, vpred_ptr, uv_stride, &sse1);
470 sse2 += sse1;
472 else
474 VARIANCE_INVOKE(rtcd, subpixvar8x8)(uptr, pre_stride, mv_col & 7, mv_row & 7, upred_ptr, uv_stride, &sse2);
475 VARIANCE_INVOKE(rtcd, subpixvar8x8)(vptr, pre_stride, mv_col & 7, mv_row & 7, vpred_ptr, uv_stride, &sse1);
476 sse2 += sse1;
479 return sse2;
483 #if !(CONFIG_REALTIME_ONLY)
484 static int cost_coeffs(MACROBLOCK *mb, BLOCKD *b, int type, ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l)
486 int c = !type; /* start at coef 0, unless Y with Y2 */
487 int eob = b->eob;
488 int pt ; /* surrounding block/prev coef predictor */
489 int cost = 0;
490 short *qcoeff_ptr = b->qcoeff;
492 VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
494 # define QC( I) ( qcoeff_ptr [vp8_default_zig_zag1d[I]] )
496 for (; c < eob; c++)
498 int v = QC(c);
499 int t = vp8_dct_value_tokens_ptr[v].Token;
500 cost += mb->token_costs [type] [vp8_coef_bands[c]] [pt] [t];
501 cost += vp8_dct_value_cost_ptr[v];
502 pt = vp8_prev_token_class[t];
505 # undef QC
507 if (c < 16)
508 cost += mb->token_costs [type] [vp8_coef_bands[c]] [pt] [DCT_EOB_TOKEN];
510 pt = (c != !type); // is eob first coefficient;
511 *a = *l = pt;
513 return cost;
516 static int vp8_rdcost_mby(MACROBLOCK *mb)
518 int cost = 0;
519 int b;
520 MACROBLOCKD *x = &mb->e_mbd;
521 ENTROPY_CONTEXT_PLANES t_above, t_left;
522 ENTROPY_CONTEXT *ta;
523 ENTROPY_CONTEXT *tl;
525 vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
526 vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
528 ta = (ENTROPY_CONTEXT *)&t_above;
529 tl = (ENTROPY_CONTEXT *)&t_left;
531 for (b = 0; b < 16; b++)
532 cost += cost_coeffs(mb, x->block + b, 0,
533 ta + vp8_block2above[b], tl + vp8_block2left[b]);
535 cost += cost_coeffs(mb, x->block + 24, 1,
536 ta + vp8_block2above[24], tl + vp8_block2left[24]);
538 return cost;
541 static void macro_block_yrd( MACROBLOCK *mb,
542 int *Rate,
543 int *Distortion,
544 const vp8_encodemb_rtcd_vtable_t *rtcd)
546 int b;
547 MACROBLOCKD *const x = &mb->e_mbd;
548 BLOCK *const mb_y2 = mb->block + 24;
549 BLOCKD *const x_y2 = x->block + 24;
550 short *Y2DCPtr = mb_y2->src_diff;
551 BLOCK *beptr;
552 int d;
554 ENCODEMB_INVOKE(rtcd, submby)( mb->src_diff, mb->src.y_buffer,
555 mb->e_mbd.predictor, mb->src.y_stride );
557 // Fdct and building the 2nd order block
558 for (beptr = mb->block; beptr < mb->block + 16; beptr += 2)
560 mb->vp8_short_fdct8x4(beptr->src_diff, beptr->coeff, 32);
561 *Y2DCPtr++ = beptr->coeff[0];
562 *Y2DCPtr++ = beptr->coeff[16];
565 // 2nd order fdct
566 mb->short_walsh4x4(mb_y2->src_diff, mb_y2->coeff, 8);
568 // Quantization
569 for (b = 0; b < 16; b++)
571 mb->quantize_b(&mb->block[b], &mb->e_mbd.block[b]);
574 // DC predication and Quantization of 2nd Order block
575 mb->quantize_b(mb_y2, x_y2);
577 // Distortion
578 d = ENCODEMB_INVOKE(rtcd, mberr)(mb, 1) << 2;
579 d += ENCODEMB_INVOKE(rtcd, berr)(mb_y2->coeff, x_y2->dqcoeff);
581 *Distortion = (d >> 4);
583 // rate
584 *Rate = vp8_rdcost_mby(mb);
587 static void save_predictor(unsigned char *predictor, unsigned char *dst)
589 int r, c;
590 for (r = 0; r < 4; r++)
592 for (c = 0; c < 4; c++)
594 *dst = predictor[c];
595 dst++;
598 predictor += 16;
601 static void restore_predictor(unsigned char *predictor, unsigned char *dst)
603 int r, c;
604 for (r = 0; r < 4; r++)
606 for (c = 0; c < 4; c++)
608 predictor[c] = *dst;
609 dst++;
612 predictor += 16;
615 static int rd_pick_intra4x4block(
616 VP8_COMP *cpi,
617 MACROBLOCK *x,
618 BLOCK *be,
619 BLOCKD *b,
620 B_PREDICTION_MODE *best_mode,
621 unsigned int *bmode_costs,
622 ENTROPY_CONTEXT *a,
623 ENTROPY_CONTEXT *l,
625 int *bestrate,
626 int *bestratey,
627 int *bestdistortion)
629 B_PREDICTION_MODE mode;
630 int best_rd = INT_MAX;
631 int rate = 0;
632 int distortion;
634 ENTROPY_CONTEXT ta = *a, tempa = *a;
635 ENTROPY_CONTEXT tl = *l, templ = *l;
637 DECLARE_ALIGNED_ARRAY(16, unsigned char, predictor, 16);
638 DECLARE_ALIGNED_ARRAY(16, short, dqcoeff, 16);
640 for (mode = B_DC_PRED; mode <= B_HU_PRED; mode++)
642 int this_rd;
643 int ratey;
645 rate = bmode_costs[mode];
647 vp8_predict_intra4x4(b, mode, b->predictor);
648 ENCODEMB_INVOKE(IF_RTCD(&cpi->rtcd.encodemb), subb)(be, b, 16);
649 x->vp8_short_fdct4x4(be->src_diff, be->coeff, 32);
650 x->quantize_b(be, b);
652 tempa = ta;
653 templ = tl;
655 ratey = cost_coeffs(x, b, 3, &tempa, &templ);
656 rate += ratey;
657 distortion = ENCODEMB_INVOKE(IF_RTCD(&cpi->rtcd.encodemb), berr)(be->coeff, b->dqcoeff) >> 2;
659 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
661 if (this_rd < best_rd)
663 *bestrate = rate;
664 *bestratey = ratey;
665 *bestdistortion = distortion;
666 best_rd = this_rd;
667 *best_mode = mode;
668 *a = tempa;
669 *l = templ;
670 save_predictor(b->predictor, predictor);
671 vpx_memcpy(dqcoeff, b->dqcoeff, 32);
675 b->bmi.mode = (B_PREDICTION_MODE)(*best_mode);
677 restore_predictor(b->predictor, predictor);
678 vpx_memcpy(b->dqcoeff, dqcoeff, 32);
680 IDCT_INVOKE(IF_RTCD(&cpi->rtcd.common->idct), idct16)(b->dqcoeff, b->diff, 32);
681 RECON_INVOKE(IF_RTCD(&cpi->rtcd.common->recon), recon)(b->predictor, b->diff, *(b->base_dst) + b->dst, b->dst_stride);
683 return best_rd;
687 int vp8_rd_pick_intra4x4mby_modes(VP8_COMP *cpi, MACROBLOCK *mb, int *Rate,
688 int *rate_y, int *Distortion, int best_rd)
690 MACROBLOCKD *const xd = &mb->e_mbd;
691 int i;
692 int cost = mb->mbmode_cost [xd->frame_type] [B_PRED];
693 int distortion = 0;
694 int tot_rate_y = 0;
695 int total_rd = 0;
696 ENTROPY_CONTEXT_PLANES t_above, t_left;
697 ENTROPY_CONTEXT *ta;
698 ENTROPY_CONTEXT *tl;
699 unsigned int *bmode_costs;
701 vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
702 vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
704 ta = (ENTROPY_CONTEXT *)&t_above;
705 tl = (ENTROPY_CONTEXT *)&t_left;
707 vp8_intra_prediction_down_copy(xd);
709 bmode_costs = mb->inter_bmode_costs;
711 for (i = 0; i < 16; i++)
713 MODE_INFO *const mic = xd->mode_info_context;
714 const int mis = xd->mode_info_stride;
715 B_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
716 int UNINITIALIZED_IS_SAFE(r), UNINITIALIZED_IS_SAFE(ry), UNINITIALIZED_IS_SAFE(d);
718 if (mb->e_mbd.frame_type == KEY_FRAME)
720 const B_PREDICTION_MODE A = vp8_above_bmi(mic, i, mis)->mode;
721 const B_PREDICTION_MODE L = vp8_left_bmi(mic, i)->mode;
723 bmode_costs = mb->bmode_costs[A][L];
726 total_rd += rd_pick_intra4x4block(
727 cpi, mb, mb->block + i, xd->block + i, &best_mode, bmode_costs,
728 ta + vp8_block2above[i],
729 tl + vp8_block2left[i], &r, &ry, &d);
731 cost += r;
732 distortion += d;
733 tot_rate_y += ry;
734 mic->bmi[i].mode = xd->block[i].bmi.mode = best_mode;
736 if(total_rd >= best_rd)
737 break;
740 if(total_rd >= best_rd)
741 return INT_MAX;
743 *Rate = cost;
744 *rate_y += tot_rate_y;
745 *Distortion = distortion;
747 return RDCOST(mb->rdmult, mb->rddiv, cost, distortion);
749 int vp8_rd_pick_intra16x16mby_mode(VP8_COMP *cpi,
750 MACROBLOCK *x,
751 int *Rate,
752 int *rate_y,
753 int *Distortion)
755 MB_PREDICTION_MODE mode;
756 MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(mode_selected);
757 int rate, ratey;
758 int distortion;
759 int best_rd = INT_MAX;
760 int this_rd;
762 //Y Search for 16x16 intra prediction mode
763 for (mode = DC_PRED; mode <= TM_PRED; mode++)
765 x->e_mbd.mode_info_context->mbmi.mode = mode;
767 vp8_build_intra_predictors_mby_ptr(&x->e_mbd);
769 macro_block_yrd(x, &ratey, &distortion, IF_RTCD(&cpi->rtcd.encodemb));
770 rate = ratey + x->mbmode_cost[x->e_mbd.frame_type]
771 [x->e_mbd.mode_info_context->mbmi.mode];
773 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
775 if (this_rd < best_rd)
777 mode_selected = mode;
778 best_rd = this_rd;
779 *Rate = rate;
780 *rate_y = ratey;
781 *Distortion = distortion;
785 x->e_mbd.mode_info_context->mbmi.mode = mode_selected;
786 return best_rd;
789 static int rd_cost_mbuv(MACROBLOCK *mb)
791 int b;
792 int cost = 0;
793 MACROBLOCKD *x = &mb->e_mbd;
794 ENTROPY_CONTEXT_PLANES t_above, t_left;
795 ENTROPY_CONTEXT *ta;
796 ENTROPY_CONTEXT *tl;
798 vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
799 vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
801 ta = (ENTROPY_CONTEXT *)&t_above;
802 tl = (ENTROPY_CONTEXT *)&t_left;
804 for (b = 16; b < 20; b++)
805 cost += cost_coeffs(mb, x->block + b, vp8_block2type[b],
806 ta + vp8_block2above[b], tl + vp8_block2left[b]);
808 for (b = 20; b < 24; b++)
809 cost += cost_coeffs(mb, x->block + b, vp8_block2type[b],
810 ta + vp8_block2above[b], tl + vp8_block2left[b]);
812 return cost;
816 unsigned int vp8_get_mbuvrecon_error(const vp8_variance_rtcd_vtable_t *rtcd, const MACROBLOCK *x) // sum of squares
818 unsigned int sse0, sse1;
819 int sum0, sum1;
820 VARIANCE_INVOKE(rtcd, get8x8var)(x->src.u_buffer, x->src.uv_stride, x->e_mbd.dst.u_buffer, x->e_mbd.dst.uv_stride, &sse0, &sum0);
821 VARIANCE_INVOKE(rtcd, get8x8var)(x->src.v_buffer, x->src.uv_stride, x->e_mbd.dst.v_buffer, x->e_mbd.dst.uv_stride, &sse1, &sum1);
822 return (sse0 + sse1);
825 static int vp8_rd_inter_uv(VP8_COMP *cpi, MACROBLOCK *x, int *rate, int *distortion, int fullpixel)
827 vp8_build_uvmvs(&x->e_mbd, fullpixel);
828 vp8_encode_inter16x16uvrd(IF_RTCD(&cpi->rtcd), x);
831 *rate = rd_cost_mbuv(x);
832 *distortion = ENCODEMB_INVOKE(&cpi->rtcd.encodemb, mbuverr)(x) / 4;
834 return RDCOST(x->rdmult, x->rddiv, *rate, *distortion);
837 int vp8_rd_pick_intra_mbuv_mode(VP8_COMP *cpi, MACROBLOCK *x, int *rate, int *rate_tokenonly, int *distortion)
839 MB_PREDICTION_MODE mode;
840 MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(mode_selected);
841 int best_rd = INT_MAX;
842 int UNINITIALIZED_IS_SAFE(d), UNINITIALIZED_IS_SAFE(r);
843 int rate_to;
845 for (mode = DC_PRED; mode <= TM_PRED; mode++)
847 int rate;
848 int distortion;
849 int this_rd;
851 x->e_mbd.mode_info_context->mbmi.uv_mode = mode;
852 vp8_encode_intra16x16mbuvrd(IF_RTCD(&cpi->rtcd), x);
854 rate_to = rd_cost_mbuv(x);
855 rate = rate_to + x->intra_uv_mode_cost[x->e_mbd.frame_type][x->e_mbd.mode_info_context->mbmi.uv_mode];
857 distortion = ENCODEMB_INVOKE(&cpi->rtcd.encodemb, mbuverr)(x) / 4;
859 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
861 if (this_rd < best_rd)
863 best_rd = this_rd;
864 d = distortion;
865 r = rate;
866 *rate_tokenonly = rate_to;
867 mode_selected = mode;
871 *rate = r;
872 *distortion = d;
874 x->e_mbd.mode_info_context->mbmi.uv_mode = mode_selected;
875 return best_rd;
877 #endif
879 int vp8_cost_mv_ref(MB_PREDICTION_MODE m, const int near_mv_ref_ct[4])
881 vp8_prob p [VP8_MVREFS-1];
882 assert(NEARESTMV <= m && m <= SPLITMV);
883 vp8_mv_ref_probs(p, near_mv_ref_ct);
884 return vp8_cost_token(vp8_mv_ref_tree, p,
885 vp8_mv_ref_encoding_array - NEARESTMV + m);
888 void vp8_set_mbmode_and_mvs(MACROBLOCK *x, MB_PREDICTION_MODE mb, MV *mv)
890 int i;
892 x->e_mbd.mode_info_context->mbmi.mode = mb;
893 x->e_mbd.mode_info_context->mbmi.mv.as_mv.row = mv->row;
894 x->e_mbd.mode_info_context->mbmi.mv.as_mv.col = mv->col;
896 for (i = 0; i < 16; i++)
898 B_MODE_INFO *bmi = &x->e_mbd.block[i].bmi;
899 bmi->mode = (B_PREDICTION_MODE) mb;
900 bmi->mv.as_mv.row = mv->row;
901 bmi->mv.as_mv.col = mv->col;
905 #if !(CONFIG_REALTIME_ONLY)
906 static int labels2mode(
907 MACROBLOCK *x,
908 int const *labelings, int which_label,
909 B_PREDICTION_MODE this_mode,
910 MV *this_mv, MV *best_ref_mv,
911 int *mvcost[2]
914 MACROBLOCKD *const xd = & x->e_mbd;
915 MODE_INFO *const mic = xd->mode_info_context;
916 const int mis = xd->mode_info_stride;
918 int cost = 0;
919 int thismvcost = 0;
921 /* We have to be careful retrieving previously-encoded motion vectors.
922 Ones from this macroblock have to be pulled from the BLOCKD array
923 as they have not yet made it to the bmi array in our MB_MODE_INFO. */
925 int i = 0;
929 BLOCKD *const d = xd->block + i;
930 const int row = i >> 2, col = i & 3;
932 B_PREDICTION_MODE m;
934 if (labelings[i] != which_label)
935 continue;
937 if (col && labelings[i] == labelings[i-1])
938 m = LEFT4X4;
939 else if (row && labelings[i] == labelings[i-4])
940 m = ABOVE4X4;
941 else
943 // the only time we should do costing for new motion vector or mode
944 // is when we are on a new label (jbb May 08, 2007)
945 switch (m = this_mode)
947 case NEW4X4 :
948 thismvcost = vp8_mv_bit_cost(this_mv, best_ref_mv, mvcost, 102);
949 break;
950 case LEFT4X4:
951 *this_mv = col ? d[-1].bmi.mv.as_mv : vp8_left_bmi(mic, i)->mv.as_mv;
952 break;
953 case ABOVE4X4:
954 *this_mv = row ? d[-4].bmi.mv.as_mv : vp8_above_bmi(mic, i, mis)->mv.as_mv;
955 break;
956 case ZERO4X4:
957 this_mv->row = this_mv->col = 0;
958 break;
959 default:
960 break;
963 if (m == ABOVE4X4) // replace above with left if same
965 const MV mv = col ? d[-1].bmi.mv.as_mv : vp8_left_bmi(mic, i)->mv.as_mv;
967 if (mv.row == this_mv->row && mv.col == this_mv->col)
968 m = LEFT4X4;
971 cost = x->inter_bmode_costs[ m];
974 d->bmi.mode = m;
975 d->bmi.mv.as_mv = *this_mv;
978 while (++i < 16);
980 cost += thismvcost ;
981 return cost;
984 static int rdcost_mbsegment_y(MACROBLOCK *mb, const int *labels,
985 int which_label, ENTROPY_CONTEXT *ta,
986 ENTROPY_CONTEXT *tl)
988 int cost = 0;
989 int b;
990 MACROBLOCKD *x = &mb->e_mbd;
992 for (b = 0; b < 16; b++)
993 if (labels[ b] == which_label)
994 cost += cost_coeffs(mb, x->block + b, 3,
995 ta + vp8_block2above[b],
996 tl + vp8_block2left[b]);
998 return cost;
1001 static unsigned int vp8_encode_inter_mb_segment(MACROBLOCK *x, int const *labels, int which_label, const vp8_encodemb_rtcd_vtable_t *rtcd)
1003 int i;
1004 unsigned int distortion = 0;
1006 for (i = 0; i < 16; i++)
1008 if (labels[i] == which_label)
1010 BLOCKD *bd = &x->e_mbd.block[i];
1011 BLOCK *be = &x->block[i];
1014 vp8_build_inter_predictors_b(bd, 16, x->e_mbd.subpixel_predict);
1015 ENCODEMB_INVOKE(rtcd, subb)(be, bd, 16);
1016 x->vp8_short_fdct4x4(be->src_diff, be->coeff, 32);
1018 // set to 0 no way to account for 2nd order DC so discount
1019 //be->coeff[0] = 0;
1020 x->quantize_b(be, bd);
1022 distortion += ENCODEMB_INVOKE(rtcd, berr)(be->coeff, bd->dqcoeff);
1026 return distortion;
1029 unsigned char vp8_mbsplit_offset2[4][16] = {
1030 { 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
1031 { 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
1032 { 0, 2, 8, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
1033 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
1037 static const unsigned int segmentation_to_sseshift[4] = {3, 3, 2, 0};
1040 typedef struct
1042 MV *ref_mv;
1043 MV *mvp;
1045 int segment_rd;
1046 int segment_num;
1047 int r;
1048 int d;
1049 int segment_yrate;
1050 B_PREDICTION_MODE modes[16];
1051 int_mv mvs[16];
1052 unsigned char eobs[16];
1054 int mvthresh;
1055 int *mdcounts;
1057 MV sv_mvp[4]; // save 4 mvp from 8x8
1058 int sv_istep[2]; // save 2 initial step_param for 16x8/8x16
1060 } BEST_SEG_INFO;
1063 void vp8_rd_check_segment(VP8_COMP *cpi, MACROBLOCK *x, BEST_SEG_INFO *bsi,
1064 unsigned int segmentation)
1066 int i;
1067 int const *labels;
1068 int br = 0;
1069 int bd = 0;
1070 B_PREDICTION_MODE this_mode;
1073 int label_count;
1074 int this_segment_rd = 0;
1075 int label_mv_thresh;
1076 int rate = 0;
1077 int sbr = 0;
1078 int sbd = 0;
1079 int segmentyrate = 0;
1081 vp8_variance_fn_ptr_t *v_fn_ptr;
1083 ENTROPY_CONTEXT_PLANES t_above, t_left;
1084 ENTROPY_CONTEXT *ta;
1085 ENTROPY_CONTEXT *tl;
1086 ENTROPY_CONTEXT_PLANES t_above_b, t_left_b;
1087 ENTROPY_CONTEXT *ta_b;
1088 ENTROPY_CONTEXT *tl_b;
1090 vpx_memcpy(&t_above, x->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
1091 vpx_memcpy(&t_left, x->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
1093 ta = (ENTROPY_CONTEXT *)&t_above;
1094 tl = (ENTROPY_CONTEXT *)&t_left;
1095 ta_b = (ENTROPY_CONTEXT *)&t_above_b;
1096 tl_b = (ENTROPY_CONTEXT *)&t_left_b;
1098 br = 0;
1099 bd = 0;
1101 v_fn_ptr = &cpi->fn_ptr[segmentation];
1102 labels = vp8_mbsplits[segmentation];
1103 label_count = vp8_mbsplit_count[segmentation];
1105 // 64 makes this threshold really big effectively
1106 // making it so that we very rarely check mvs on
1107 // segments. setting this to 1 would make mv thresh
1108 // roughly equal to what it is for macroblocks
1109 label_mv_thresh = 1 * bsi->mvthresh / label_count ;
1111 // Segmentation method overheads
1112 rate = vp8_cost_token(vp8_mbsplit_tree, vp8_mbsplit_probs, vp8_mbsplit_encodings + segmentation);
1113 rate += vp8_cost_mv_ref(SPLITMV, bsi->mdcounts);
1114 this_segment_rd += RDCOST(x->rdmult, x->rddiv, rate, 0);
1115 br += rate;
1117 for (i = 0; i < label_count; i++)
1119 MV mode_mv[B_MODE_COUNT];
1120 int best_label_rd = INT_MAX;
1121 B_PREDICTION_MODE mode_selected = ZERO4X4;
1122 int bestlabelyrate = 0;
1124 // search for the best motion vector on this segment
1125 for (this_mode = LEFT4X4; this_mode <= NEW4X4 ; this_mode ++)
1127 int this_rd;
1128 int distortion;
1129 int labelyrate;
1130 ENTROPY_CONTEXT_PLANES t_above_s, t_left_s;
1131 ENTROPY_CONTEXT *ta_s;
1132 ENTROPY_CONTEXT *tl_s;
1134 vpx_memcpy(&t_above_s, &t_above, sizeof(ENTROPY_CONTEXT_PLANES));
1135 vpx_memcpy(&t_left_s, &t_left, sizeof(ENTROPY_CONTEXT_PLANES));
1137 ta_s = (ENTROPY_CONTEXT *)&t_above_s;
1138 tl_s = (ENTROPY_CONTEXT *)&t_left_s;
1140 if (this_mode == NEW4X4)
1142 int sseshift;
1143 int num00;
1144 int step_param = 0;
1145 int further_steps;
1146 int n;
1147 int thissme;
1148 int bestsme = INT_MAX;
1149 MV temp_mv;
1150 BLOCK *c;
1151 BLOCKD *e;
1153 // Is the best so far sufficiently good that we cant justify doing and new motion search.
1154 if (best_label_rd < label_mv_thresh)
1155 break;
1157 if(cpi->compressor_speed)
1159 if (segmentation == BLOCK_8X16 || segmentation == BLOCK_16X8)
1161 bsi->mvp = &bsi->sv_mvp[i];
1162 if (i==1 && segmentation == BLOCK_16X8) bsi->mvp = &bsi->sv_mvp[2];
1164 step_param = bsi->sv_istep[i];
1167 // use previous block's result as next block's MV predictor.
1168 if (segmentation == BLOCK_4X4 && i>0)
1170 bsi->mvp = &(x->e_mbd.block[i-1].bmi.mv.as_mv);
1171 if (i==4 || i==8 || i==12) bsi->mvp = &(x->e_mbd.block[i-4].bmi.mv.as_mv);
1172 step_param = 2;
1176 further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
1179 int sadpb = x->sadperbit4;
1181 // find first label
1182 n = vp8_mbsplit_offset2[segmentation][i];
1184 c = &x->block[n];
1185 e = &x->e_mbd.block[n];
1187 if (cpi->sf.search_method == HEX)
1188 bestsme = vp8_hex_search(x, c, e, bsi->ref_mv,
1189 &mode_mv[NEW4X4], step_param, sadpb, &num00, v_fn_ptr, x->mvsadcost, x->mvcost, bsi->ref_mv);
1191 else
1193 bestsme = cpi->diamond_search_sad(x, c, e, bsi->mvp,
1194 &mode_mv[NEW4X4], step_param,
1195 sadpb / 2, &num00, v_fn_ptr, x->mvsadcost, x->mvcost, bsi->ref_mv);
1197 n = num00;
1198 num00 = 0;
1200 while (n < further_steps)
1202 n++;
1204 if (num00)
1205 num00--;
1206 else
1208 thissme = cpi->diamond_search_sad(x, c, e, bsi->mvp,
1209 &temp_mv, step_param + n,
1210 sadpb / 2, &num00, v_fn_ptr, x->mvsadcost, x->mvcost, bsi->ref_mv);
1212 if (thissme < bestsme)
1214 bestsme = thissme;
1215 mode_mv[NEW4X4].row = temp_mv.row;
1216 mode_mv[NEW4X4].col = temp_mv.col;
1222 sseshift = segmentation_to_sseshift[segmentation];
1224 // Should we do a full search (best quality only)
1225 if ((cpi->compressor_speed == 0) && (bestsme >> sseshift) > 4000)
1227 thissme = cpi->full_search_sad(x, c, e, bsi->mvp,
1228 sadpb / 4, 16, v_fn_ptr, x->mvcost, x->mvsadcost,bsi->ref_mv);
1230 if (thissme < bestsme)
1232 bestsme = thissme;
1233 mode_mv[NEW4X4] = e->bmi.mv.as_mv;
1235 else
1237 // The full search result is actually worse so re-instate the previous best vector
1238 e->bmi.mv.as_mv = mode_mv[NEW4X4];
1243 if (bestsme < INT_MAX)
1245 if (!cpi->common.full_pixel)
1246 cpi->find_fractional_mv_step(x, c, e, &mode_mv[NEW4X4],
1247 bsi->ref_mv, x->errorperbit / 2, v_fn_ptr, x->mvcost);
1248 else
1249 vp8_skip_fractional_mv_step(x, c, e, &mode_mv[NEW4X4],
1250 bsi->ref_mv, x->errorperbit, v_fn_ptr, x->mvcost);
1252 } /* NEW4X4 */
1254 rate = labels2mode(x, labels, i, this_mode, &mode_mv[this_mode],
1255 bsi->ref_mv, x->mvcost);
1257 // Trap vectors that reach beyond the UMV borders
1258 if (((mode_mv[this_mode].row >> 3) < x->mv_row_min) || ((mode_mv[this_mode].row >> 3) > x->mv_row_max) ||
1259 ((mode_mv[this_mode].col >> 3) < x->mv_col_min) || ((mode_mv[this_mode].col >> 3) > x->mv_col_max))
1261 continue;
1264 distortion = vp8_encode_inter_mb_segment(x, labels, i, IF_RTCD(&cpi->rtcd.encodemb)) / 4;
1266 labelyrate = rdcost_mbsegment_y(x, labels, i, ta_s, tl_s);
1267 rate += labelyrate;
1269 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
1271 if (this_rd < best_label_rd)
1273 sbr = rate;
1274 sbd = distortion;
1275 bestlabelyrate = labelyrate;
1276 mode_selected = this_mode;
1277 best_label_rd = this_rd;
1279 vpx_memcpy(ta_b, ta_s, sizeof(ENTROPY_CONTEXT_PLANES));
1280 vpx_memcpy(tl_b, tl_s, sizeof(ENTROPY_CONTEXT_PLANES));
1283 } /*for each 4x4 mode*/
1285 vpx_memcpy(ta, ta_b, sizeof(ENTROPY_CONTEXT_PLANES));
1286 vpx_memcpy(tl, tl_b, sizeof(ENTROPY_CONTEXT_PLANES));
1288 labels2mode(x, labels, i, mode_selected, &mode_mv[mode_selected],
1289 bsi->ref_mv, x->mvcost);
1291 br += sbr;
1292 bd += sbd;
1293 segmentyrate += bestlabelyrate;
1294 this_segment_rd += best_label_rd;
1296 if (this_segment_rd >= bsi->segment_rd)
1297 break;
1299 } /* for each label */
1301 if (this_segment_rd < bsi->segment_rd)
1303 bsi->r = br;
1304 bsi->d = bd;
1305 bsi->segment_yrate = segmentyrate;
1306 bsi->segment_rd = this_segment_rd;
1307 bsi->segment_num = segmentation;
1309 // store everything needed to come back to this!!
1310 for (i = 0; i < 16; i++)
1312 BLOCKD *bd = &x->e_mbd.block[i];
1314 bsi->mvs[i].as_mv = bd->bmi.mv.as_mv;
1315 bsi->modes[i] = bd->bmi.mode;
1316 bsi->eobs[i] = bd->eob;
1321 static __inline
1322 void vp8_cal_step_param(int sr, int *sp)
1324 int step = 0;
1326 if (sr > MAX_FIRST_STEP) sr = MAX_FIRST_STEP;
1327 else if (sr < 1) sr = 1;
1329 while (sr>>=1)
1330 step++;
1332 *sp = MAX_MVSEARCH_STEPS - 1 - step;
1335 static int vp8_rd_pick_best_mbsegmentation(VP8_COMP *cpi, MACROBLOCK *x,
1336 MV *best_ref_mv, int best_rd,
1337 int *mdcounts, int *returntotrate,
1338 int *returnyrate, int *returndistortion,
1339 int mvthresh)
1341 int i;
1342 BEST_SEG_INFO bsi;
1344 vpx_memset(&bsi, 0, sizeof(bsi));
1346 bsi.segment_rd = best_rd;
1347 bsi.ref_mv = best_ref_mv;
1348 bsi.mvp = best_ref_mv;
1349 bsi.mvthresh = mvthresh;
1350 bsi.mdcounts = mdcounts;
1352 for(i = 0; i < 16; i++)
1354 bsi.modes[i] = ZERO4X4;
1357 if(cpi->compressor_speed == 0)
1359 /* for now, we will keep the original segmentation order
1360 when in best quality mode */
1361 vp8_rd_check_segment(cpi, x, &bsi, BLOCK_16X8);
1362 vp8_rd_check_segment(cpi, x, &bsi, BLOCK_8X16);
1363 vp8_rd_check_segment(cpi, x, &bsi, BLOCK_8X8);
1364 vp8_rd_check_segment(cpi, x, &bsi, BLOCK_4X4);
1366 else
1368 int sr;
1370 vp8_rd_check_segment(cpi, x, &bsi, BLOCK_8X8);
1372 if (bsi.segment_rd < best_rd)
1374 int col_min = (best_ref_mv->col - MAX_FULL_PEL_VAL) >>3;
1375 int col_max = (best_ref_mv->col + MAX_FULL_PEL_VAL) >>3;
1376 int row_min = (best_ref_mv->row - MAX_FULL_PEL_VAL) >>3;
1377 int row_max = (best_ref_mv->row + MAX_FULL_PEL_VAL) >>3;
1379 int tmp_col_min = x->mv_col_min;
1380 int tmp_col_max = x->mv_col_max;
1381 int tmp_row_min = x->mv_row_min;
1382 int tmp_row_max = x->mv_row_max;
1384 /* Get intersection of UMV window and valid MV window to reduce # of checks in diamond search. */
1385 if (x->mv_col_min < col_min )
1386 x->mv_col_min = col_min;
1387 if (x->mv_col_max > col_max )
1388 x->mv_col_max = col_max;
1389 if (x->mv_row_min < row_min )
1390 x->mv_row_min = row_min;
1391 if (x->mv_row_max > row_max )
1392 x->mv_row_max = row_max;
1394 /* Get 8x8 result */
1395 bsi.sv_mvp[0] = bsi.mvs[0].as_mv;
1396 bsi.sv_mvp[1] = bsi.mvs[2].as_mv;
1397 bsi.sv_mvp[2] = bsi.mvs[8].as_mv;
1398 bsi.sv_mvp[3] = bsi.mvs[10].as_mv;
1400 /* Use 8x8 result as 16x8/8x16's predictor MV. Adjust search range according to the closeness of 2 MV. */
1401 /* block 8X16 */
1403 sr = MAXF((abs(bsi.sv_mvp[0].row - bsi.sv_mvp[2].row))>>3, (abs(bsi.sv_mvp[0].col - bsi.sv_mvp[2].col))>>3);
1404 vp8_cal_step_param(sr, &bsi.sv_istep[0]);
1406 sr = MAXF((abs(bsi.sv_mvp[1].row - bsi.sv_mvp[3].row))>>3, (abs(bsi.sv_mvp[1].col - bsi.sv_mvp[3].col))>>3);
1407 vp8_cal_step_param(sr, &bsi.sv_istep[1]);
1409 vp8_rd_check_segment(cpi, x, &bsi, BLOCK_8X16);
1412 /* block 16X8 */
1414 sr = MAXF((abs(bsi.sv_mvp[0].row - bsi.sv_mvp[1].row))>>3, (abs(bsi.sv_mvp[0].col - bsi.sv_mvp[1].col))>>3);
1415 vp8_cal_step_param(sr, &bsi.sv_istep[0]);
1417 sr = MAXF((abs(bsi.sv_mvp[2].row - bsi.sv_mvp[3].row))>>3, (abs(bsi.sv_mvp[2].col - bsi.sv_mvp[3].col))>>3);
1418 vp8_cal_step_param(sr, &bsi.sv_istep[1]);
1420 vp8_rd_check_segment(cpi, x, &bsi, BLOCK_16X8);
1423 /* If 8x8 is better than 16x8/8x16, then do 4x4 search */
1424 /* Not skip 4x4 if speed=0 (good quality) */
1425 if (cpi->sf.no_skip_block4x4_search || bsi.segment_num == BLOCK_8X8) /* || (sv_segment_rd8x8-bsi.segment_rd) < sv_segment_rd8x8>>5) */
1427 bsi.mvp = &bsi.sv_mvp[0];
1428 vp8_rd_check_segment(cpi, x, &bsi, BLOCK_4X4);
1431 /* restore UMV window */
1432 x->mv_col_min = tmp_col_min;
1433 x->mv_col_max = tmp_col_max;
1434 x->mv_row_min = tmp_row_min;
1435 x->mv_row_max = tmp_row_max;
1439 /* set it to the best */
1440 for (i = 0; i < 16; i++)
1442 BLOCKD *bd = &x->e_mbd.block[i];
1444 bd->bmi.mv.as_mv = bsi.mvs[i].as_mv;
1445 bd->bmi.mode = bsi.modes[i];
1446 bd->eob = bsi.eobs[i];
1449 *returntotrate = bsi.r;
1450 *returndistortion = bsi.d;
1451 *returnyrate = bsi.segment_yrate;
1453 /* save partitions */
1454 x->e_mbd.mode_info_context->mbmi.partitioning = bsi.segment_num;
1455 x->partition_info->count = vp8_mbsplit_count[bsi.segment_num];
1457 for (i = 0; i < x->partition_info->count; i++)
1459 int j;
1461 j = vp8_mbsplit_offset2[bsi.segment_num][i];
1463 x->partition_info->bmi[i].mode = x->e_mbd.block[j].bmi.mode;
1464 x->partition_info->bmi[i].mv.as_mv = x->e_mbd.block[j].bmi.mv.as_mv;
1467 return bsi.segment_rd;
1469 #endif
1471 static void swap(int *x,int *y)
1473 int tmp;
1475 tmp = *x;
1476 *x = *y;
1477 *y = tmp;
1480 static void quicksortmv(int arr[],int left, int right)
1482 int lidx,ridx,pivot;
1484 lidx = left;
1485 ridx = right;
1487 if( left < right)
1489 pivot = (left + right)/2;
1491 while(lidx <=pivot && ridx >=pivot)
1493 while(arr[lidx] < arr[pivot] && lidx <= pivot)
1494 lidx++;
1495 while(arr[ridx] > arr[pivot] && ridx >= pivot)
1496 ridx--;
1497 swap(&arr[lidx], &arr[ridx]);
1498 lidx++;
1499 ridx--;
1500 if(lidx-1 == pivot)
1502 ridx++;
1503 pivot = ridx;
1505 else if(ridx+1 == pivot)
1507 lidx--;
1508 pivot = lidx;
1511 quicksortmv(arr, left, pivot - 1);
1512 quicksortmv(arr, pivot + 1, right);
1516 static void quicksortsad(int arr[],int idx[], int left, int right)
1518 int lidx,ridx,pivot;
1520 lidx = left;
1521 ridx = right;
1523 if( left < right)
1525 pivot = (left + right)/2;
1527 while(lidx <=pivot && ridx >=pivot)
1529 while(arr[lidx] < arr[pivot] && lidx <= pivot)
1530 lidx++;
1531 while(arr[ridx] > arr[pivot] && ridx >= pivot)
1532 ridx--;
1533 swap(&arr[lidx], &arr[ridx]);
1534 swap(&idx[lidx], &idx[ridx]);
1535 lidx++;
1536 ridx--;
1537 if(lidx-1 == pivot)
1539 ridx++;
1540 pivot = ridx;
1542 else if(ridx+1 == pivot)
1544 lidx--;
1545 pivot = lidx;
1548 quicksortsad(arr, idx, left, pivot - 1);
1549 quicksortsad(arr, idx, pivot + 1, right);
1553 //The improved MV prediction
1554 void vp8_mv_pred
1556 VP8_COMP *cpi,
1557 MACROBLOCKD *xd,
1558 const MODE_INFO *here,
1559 MV *mvp,
1560 int refframe,
1561 int *ref_frame_sign_bias,
1562 int *sr,
1563 int near_sadidx[]
1566 const MODE_INFO *above = here - xd->mode_info_stride;
1567 const MODE_INFO *left = here - 1;
1568 const MODE_INFO *aboveleft = above - 1;
1569 int_mv near_mvs[8];
1570 int near_ref[8];
1571 int_mv mv;
1572 int vcnt=0;
1573 int find=0;
1574 int mb_offset;
1576 int mvx[8];
1577 int mvy[8];
1578 int i;
1580 mv.as_int = 0;
1582 if(here->mbmi.ref_frame != INTRA_FRAME)
1584 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;
1585 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;
1587 // read in 3 nearby block's MVs from current frame as prediction candidates.
1588 if (above->mbmi.ref_frame != INTRA_FRAME)
1590 near_mvs[vcnt].as_int = above->mbmi.mv.as_int;
1591 mv_bias(ref_frame_sign_bias[above->mbmi.ref_frame], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1592 near_ref[vcnt] = above->mbmi.ref_frame;
1594 vcnt++;
1595 if (left->mbmi.ref_frame != INTRA_FRAME)
1597 near_mvs[vcnt].as_int = left->mbmi.mv.as_int;
1598 mv_bias(ref_frame_sign_bias[left->mbmi.ref_frame], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1599 near_ref[vcnt] = left->mbmi.ref_frame;
1601 vcnt++;
1602 if (aboveleft->mbmi.ref_frame != INTRA_FRAME)
1604 near_mvs[vcnt].as_int = aboveleft->mbmi.mv.as_int;
1605 mv_bias(ref_frame_sign_bias[aboveleft->mbmi.ref_frame], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1606 near_ref[vcnt] = aboveleft->mbmi.ref_frame;
1608 vcnt++;
1610 // read in 5 nearby block's MVs from last frame.
1611 if(cpi->common.last_frame_type != KEY_FRAME)
1613 mb_offset = (-xd->mb_to_top_edge/128 + 1) * (xd->mode_info_stride +1) + (-xd->mb_to_left_edge/128 +1) ;
1615 // current in last frame
1616 if (cpi->lf_ref_frame[mb_offset] != INTRA_FRAME)
1618 near_mvs[vcnt].as_int = cpi->lfmv[mb_offset].as_int;
1619 mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1620 near_ref[vcnt] = cpi->lf_ref_frame[mb_offset];
1622 vcnt++;
1624 // above in last frame
1625 if (cpi->lf_ref_frame[mb_offset - xd->mode_info_stride-1] != INTRA_FRAME)
1627 near_mvs[vcnt].as_int = cpi->lfmv[mb_offset - xd->mode_info_stride-1].as_int;
1628 mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset - xd->mode_info_stride-1], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1629 near_ref[vcnt] = cpi->lf_ref_frame[mb_offset - xd->mode_info_stride-1];
1631 vcnt++;
1633 // left in last frame
1634 if (cpi->lf_ref_frame[mb_offset-1] != INTRA_FRAME)
1636 near_mvs[vcnt].as_int = cpi->lfmv[mb_offset -1].as_int;
1637 mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset -1], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1638 near_ref[vcnt] = cpi->lf_ref_frame[mb_offset - 1];
1640 vcnt++;
1642 // right in last frame
1643 if (cpi->lf_ref_frame[mb_offset +1] != INTRA_FRAME)
1645 near_mvs[vcnt].as_int = cpi->lfmv[mb_offset +1].as_int;
1646 mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset +1], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1647 near_ref[vcnt] = cpi->lf_ref_frame[mb_offset +1];
1649 vcnt++;
1651 // below in last frame
1652 if (cpi->lf_ref_frame[mb_offset + xd->mode_info_stride +1] != INTRA_FRAME)
1654 near_mvs[vcnt].as_int = cpi->lfmv[mb_offset + xd->mode_info_stride +1].as_int;
1655 mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset + xd->mode_info_stride +1], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1656 near_ref[vcnt] = cpi->lf_ref_frame[mb_offset + xd->mode_info_stride +1];
1658 vcnt++;
1661 for(i=0; i< vcnt; i++)
1663 if(near_ref[near_sadidx[i]] != INTRA_FRAME)
1665 if(here->mbmi.ref_frame == near_ref[near_sadidx[i]])
1667 mv.as_int = near_mvs[near_sadidx[i]].as_int;
1668 find = 1;
1669 if (i < 3)
1670 *sr = 3;
1671 else
1672 *sr = 2;
1673 break;
1678 if(!find)
1680 for(i=0; i<vcnt; i++)
1682 mvx[i] = near_mvs[i].as_mv.row;
1683 mvy[i] = near_mvs[i].as_mv.col;
1686 quicksortmv (mvx, 0, vcnt-1);
1687 quicksortmv (mvy, 0, vcnt-1);
1688 mv.as_mv.row = mvx[vcnt/2];
1689 mv.as_mv.col = mvy[vcnt/2];
1691 find = 1;
1692 //sr is set to 0 to allow calling function to decide the search range.
1693 *sr = 0;
1697 /* Set up return values */
1698 *mvp = mv.as_mv;
1699 vp8_clamp_mv(mvp, xd);
1702 void vp8_cal_sad(VP8_COMP *cpi, MACROBLOCKD *xd, MACROBLOCK *x, int recon_yoffset, int near_sadidx[])
1705 int near_sad[8] = {0}; // 0-cf above, 1-cf left, 2-cf aboveleft, 3-lf current, 4-lf above, 5-lf left, 6-lf right, 7-lf below
1707 //calculate sad for current frame 3 nearby MBs.
1708 if( xd->mb_to_top_edge==0 && xd->mb_to_left_edge ==0)
1710 near_sad[0] = near_sad[1] = near_sad[2] = INT_MAX;
1711 }else if(xd->mb_to_top_edge==0)
1712 { //only has left MB for sad calculation.
1713 near_sad[0] = near_sad[2] = INT_MAX;
1714 near_sad[1] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, xd->dst.y_buffer - 16,xd->dst.y_stride, 0x7fffffff);
1715 }else if(xd->mb_to_left_edge ==0)
1716 { //only has left MB for sad calculation.
1717 near_sad[1] = near_sad[2] = INT_MAX;
1718 near_sad[0] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, xd->dst.y_buffer - xd->dst.y_stride *16,xd->dst.y_stride, 0x7fffffff);
1719 }else
1721 near_sad[0] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, xd->dst.y_buffer - xd->dst.y_stride *16,xd->dst.y_stride, 0x7fffffff);
1722 near_sad[1] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, xd->dst.y_buffer - 16,xd->dst.y_stride, 0x7fffffff);
1723 near_sad[2] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, xd->dst.y_buffer - xd->dst.y_stride *16 -16,xd->dst.y_stride, 0x7fffffff);
1726 if(cpi->common.last_frame_type != KEY_FRAME)
1728 //calculate sad for last frame 5 nearby MBs.
1729 unsigned char *pre_y_buffer = cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_buffer + recon_yoffset;
1730 int pre_y_stride = cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_stride;
1732 if(xd->mb_to_top_edge==0) near_sad[4] = INT_MAX;
1733 if(xd->mb_to_left_edge ==0) near_sad[5] = INT_MAX;
1734 if(xd->mb_to_right_edge ==0) near_sad[6] = INT_MAX;
1735 if(xd->mb_to_bottom_edge==0) near_sad[7] = INT_MAX;
1737 if(near_sad[4] != INT_MAX)
1738 near_sad[4] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, pre_y_buffer - pre_y_stride *16, pre_y_stride, 0x7fffffff);
1739 if(near_sad[5] != INT_MAX)
1740 near_sad[5] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, pre_y_buffer - 16, pre_y_stride, 0x7fffffff);
1741 near_sad[3] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, pre_y_buffer, pre_y_stride, 0x7fffffff);
1742 if(near_sad[6] != INT_MAX)
1743 near_sad[6] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, pre_y_buffer + 16, pre_y_stride, 0x7fffffff);
1744 if(near_sad[7] != INT_MAX)
1745 near_sad[7] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, pre_y_buffer + pre_y_stride *16, pre_y_stride, 0x7fffffff);
1748 if(cpi->common.last_frame_type != KEY_FRAME)
1750 quicksortsad(near_sad, near_sadidx, 0, 7);
1751 }else
1753 quicksortsad(near_sad, near_sadidx, 0, 2);
1757 #if !(CONFIG_REALTIME_ONLY)
1758 int vp8_rd_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset, int recon_uvoffset, int *returnrate, int *returndistortion, int *returnintra)
1760 BLOCK *b = &x->block[0];
1761 BLOCKD *d = &x->e_mbd.block[0];
1762 MACROBLOCKD *xd = &x->e_mbd;
1763 B_MODE_INFO best_bmodes[16];
1764 MB_MODE_INFO best_mbmode;
1765 PARTITION_INFO best_partition;
1766 MV best_ref_mv;
1767 MV mode_mv[MB_MODE_COUNT];
1768 MB_PREDICTION_MODE this_mode;
1769 int num00;
1770 int best_mode_index = 0;
1772 int i;
1773 int mode_index;
1774 int mdcounts[4];
1775 int rate;
1776 int distortion;
1777 int best_rd = INT_MAX; // 1 << 30;
1778 int ref_frame_cost[MAX_REF_FRAMES];
1779 int rate2, distortion2;
1780 int uv_intra_rate, uv_intra_distortion, uv_intra_rate_tokenonly;
1781 int rate_y, UNINITIALIZED_IS_SAFE(rate_uv);
1782 int distortion_uv;
1783 int best_yrd = INT_MAX;
1785 //int all_rds[MAX_MODES]; // Experimental debug code.
1786 //int all_rates[MAX_MODES];
1787 //int all_dist[MAX_MODES];
1788 //int intermodecost[MAX_MODES];
1790 MB_PREDICTION_MODE uv_intra_mode;
1792 int force_no_skip = 0;
1794 MV mvp;
1795 int near_sadidx[8] = {0, 1, 2, 3, 4, 5, 6, 7};
1796 int saddone=0;
1797 int sr=0; //search range got from mv_pred(). It uses step_param levels. (0-7)
1799 MV frame_nearest_mv[4];
1800 MV frame_near_mv[4];
1801 MV frame_best_ref_mv[4];
1802 int frame_mdcounts[4][4];
1803 int frame_lf_or_gf[4];
1804 unsigned char *y_buffer[4];
1805 unsigned char *u_buffer[4];
1806 unsigned char *v_buffer[4];
1808 vpx_memset(&best_mbmode, 0, sizeof(best_mbmode));
1810 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
1812 YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
1814 vp8_find_near_mvs(&x->e_mbd, x->e_mbd.mode_info_context, &frame_nearest_mv[LAST_FRAME], &frame_near_mv[LAST_FRAME],
1815 &frame_best_ref_mv[LAST_FRAME], frame_mdcounts[LAST_FRAME], LAST_FRAME, cpi->common.ref_frame_sign_bias);
1817 y_buffer[LAST_FRAME] = lst_yv12->y_buffer + recon_yoffset;
1818 u_buffer[LAST_FRAME] = lst_yv12->u_buffer + recon_uvoffset;
1819 v_buffer[LAST_FRAME] = lst_yv12->v_buffer + recon_uvoffset;
1821 frame_lf_or_gf[LAST_FRAME] = 0;
1824 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1826 YV12_BUFFER_CONFIG *gld_yv12 = &cpi->common.yv12_fb[cpi->common.gld_fb_idx];
1828 vp8_find_near_mvs(&x->e_mbd, x->e_mbd.mode_info_context, &frame_nearest_mv[GOLDEN_FRAME], &frame_near_mv[GOLDEN_FRAME],
1829 &frame_best_ref_mv[GOLDEN_FRAME], frame_mdcounts[GOLDEN_FRAME], GOLDEN_FRAME, cpi->common.ref_frame_sign_bias);
1831 y_buffer[GOLDEN_FRAME] = gld_yv12->y_buffer + recon_yoffset;
1832 u_buffer[GOLDEN_FRAME] = gld_yv12->u_buffer + recon_uvoffset;
1833 v_buffer[GOLDEN_FRAME] = gld_yv12->v_buffer + recon_uvoffset;
1835 frame_lf_or_gf[GOLDEN_FRAME] = 1;
1838 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1840 YV12_BUFFER_CONFIG *alt_yv12 = &cpi->common.yv12_fb[cpi->common.alt_fb_idx];
1842 vp8_find_near_mvs(&x->e_mbd, x->e_mbd.mode_info_context, &frame_nearest_mv[ALTREF_FRAME], &frame_near_mv[ALTREF_FRAME],
1843 &frame_best_ref_mv[ALTREF_FRAME], frame_mdcounts[ALTREF_FRAME], ALTREF_FRAME, cpi->common.ref_frame_sign_bias);
1845 y_buffer[ALTREF_FRAME] = alt_yv12->y_buffer + recon_yoffset;
1846 u_buffer[ALTREF_FRAME] = alt_yv12->u_buffer + recon_uvoffset;
1847 v_buffer[ALTREF_FRAME] = alt_yv12->v_buffer + recon_uvoffset;
1849 frame_lf_or_gf[ALTREF_FRAME] = 1;
1852 *returnintra = INT_MAX;
1853 cpi->mbs_tested_so_far++; // Count of the number of MBs tested so far this frame
1855 x->skip = 0;
1857 ref_frame_cost[INTRA_FRAME] = vp8_cost_zero(cpi->prob_intra_coded);
1859 // Special case treatment when GF and ARF are not sensible options for reference
1860 if (cpi->ref_frame_flags == VP8_LAST_FLAG)
1862 ref_frame_cost[LAST_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
1863 + vp8_cost_zero(255);
1864 ref_frame_cost[GOLDEN_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
1865 + vp8_cost_one(255)
1866 + vp8_cost_zero(128);
1867 ref_frame_cost[ALTREF_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
1868 + vp8_cost_one(255)
1869 + vp8_cost_one(128);
1871 else
1873 ref_frame_cost[LAST_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
1874 + vp8_cost_zero(cpi->prob_last_coded);
1875 ref_frame_cost[GOLDEN_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
1876 + vp8_cost_one(cpi->prob_last_coded)
1877 + vp8_cost_zero(cpi->prob_gf_coded);
1878 ref_frame_cost[ALTREF_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
1879 + vp8_cost_one(cpi->prob_last_coded)
1880 + vp8_cost_one(cpi->prob_gf_coded);
1883 vpx_memset(mode_mv, 0, sizeof(mode_mv));
1885 x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
1886 vp8_rd_pick_intra_mbuv_mode(cpi, x, &uv_intra_rate, &uv_intra_rate_tokenonly, &uv_intra_distortion);
1887 uv_intra_mode = x->e_mbd.mode_info_context->mbmi.uv_mode;
1889 for (mode_index = 0; mode_index < MAX_MODES; mode_index++)
1891 int this_rd = INT_MAX;
1892 int lf_or_gf = 0; // Lat Frame (01) or gf/arf (1)
1893 int disable_skip = 0;
1894 int other_cost = 0;
1896 force_no_skip = 0;
1898 // Experimental debug code.
1899 // Record of rd values recorded for this MB. -1 indicates not measured
1900 //all_rds[mode_index] = -1;
1901 //all_rates[mode_index] = -1;
1902 //all_dist[mode_index] = -1;
1903 //intermodecost[mode_index] = -1;
1905 // Test best rd so far against threshold for trying this mode.
1906 if (best_rd <= cpi->rd_threshes[mode_index])
1907 continue;
1909 // These variables hold are rolling total cost and distortion for this mode
1910 rate2 = 0;
1911 distortion2 = 0;
1913 this_mode = vp8_mode_order[mode_index];
1915 x->e_mbd.mode_info_context->mbmi.mode = this_mode;
1916 x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
1917 x->e_mbd.mode_info_context->mbmi.ref_frame = vp8_ref_frame_order[mode_index];
1919 // Only consider ZEROMV/ALTREF_FRAME for alt ref frame,
1920 // unless ARNR filtering is enabled in which case we want
1921 // an unfiltered alternative
1922 if (cpi->is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0))
1924 if (this_mode != ZEROMV || x->e_mbd.mode_info_context->mbmi.ref_frame != ALTREF_FRAME)
1925 continue;
1928 /* everything but intra */
1929 if (x->e_mbd.mode_info_context->mbmi.ref_frame)
1931 x->e_mbd.pre.y_buffer = y_buffer[x->e_mbd.mode_info_context->mbmi.ref_frame];
1932 x->e_mbd.pre.u_buffer = u_buffer[x->e_mbd.mode_info_context->mbmi.ref_frame];
1933 x->e_mbd.pre.v_buffer = v_buffer[x->e_mbd.mode_info_context->mbmi.ref_frame];
1934 mode_mv[NEARESTMV] = frame_nearest_mv[x->e_mbd.mode_info_context->mbmi.ref_frame];
1935 mode_mv[NEARMV] = frame_near_mv[x->e_mbd.mode_info_context->mbmi.ref_frame];
1936 best_ref_mv = frame_best_ref_mv[x->e_mbd.mode_info_context->mbmi.ref_frame];
1937 vpx_memcpy(mdcounts, frame_mdcounts[x->e_mbd.mode_info_context->mbmi.ref_frame], sizeof(mdcounts));
1938 lf_or_gf = frame_lf_or_gf[x->e_mbd.mode_info_context->mbmi.ref_frame];
1941 if(x->e_mbd.mode_info_context->mbmi.mode == NEWMV)
1943 if(!saddone)
1945 vp8_cal_sad(cpi,xd,x, recon_yoffset ,&near_sadidx[0] );
1946 saddone = 1;
1949 vp8_mv_pred(cpi, &x->e_mbd, x->e_mbd.mode_info_context, &mvp,
1950 x->e_mbd.mode_info_context->mbmi.ref_frame, cpi->common.ref_frame_sign_bias, &sr, &near_sadidx[0]);
1952 /* adjust mvp to make sure it is within MV range */
1953 if(mvp.row > best_ref_mv.row + MAX_FULL_PEL_VAL)
1954 mvp.row = best_ref_mv.row + MAX_FULL_PEL_VAL;
1955 else if(mvp.row < best_ref_mv.row - MAX_FULL_PEL_VAL)
1956 mvp.row = best_ref_mv.row - MAX_FULL_PEL_VAL;
1957 if(mvp.col > best_ref_mv.col + MAX_FULL_PEL_VAL)
1958 mvp.col = best_ref_mv.col + MAX_FULL_PEL_VAL;
1959 else if(mvp.col < best_ref_mv.col - MAX_FULL_PEL_VAL)
1960 mvp.col = best_ref_mv.col - MAX_FULL_PEL_VAL;
1963 // Check to see if the testing frequency for this mode is at its max
1964 // If so then prevent it from being tested and increase the threshold for its testing
1965 if (cpi->mode_test_hit_counts[mode_index] && (cpi->mode_check_freq[mode_index] > 1))
1967 if (cpi->mbs_tested_so_far <= cpi->mode_check_freq[mode_index] * cpi->mode_test_hit_counts[mode_index])
1969 // Increase the threshold for coding this mode to make it less likely to be chosen
1970 cpi->rd_thresh_mult[mode_index] += 4;
1972 if (cpi->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
1973 cpi->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
1975 cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
1977 continue;
1981 // We have now reached the point where we are going to test the current mode so increment the counter for the number of times it has been tested
1982 cpi->mode_test_hit_counts[mode_index] ++;
1984 // Experimental code. Special case for gf and arf zeromv modes. Increase zbin size to supress noise
1985 if (cpi->zbin_mode_boost_enabled)
1987 if ( vp8_ref_frame_order[mode_index] == INTRA_FRAME )
1988 cpi->zbin_mode_boost = 0;
1989 else
1991 if (vp8_mode_order[mode_index] == ZEROMV)
1993 if (vp8_ref_frame_order[mode_index] != LAST_FRAME)
1994 cpi->zbin_mode_boost = GF_ZEROMV_ZBIN_BOOST;
1995 else
1996 cpi->zbin_mode_boost = LF_ZEROMV_ZBIN_BOOST;
1998 else if (vp8_ref_frame_order[mode_index] == SPLITMV)
1999 cpi->zbin_mode_boost = 0;
2000 else
2001 cpi->zbin_mode_boost = MV_ZBIN_BOOST;
2004 vp8_update_zbin_extra(cpi, x);
2007 switch (this_mode)
2009 case B_PRED:
2011 int tmp_rd;
2013 // Note the rate value returned here includes the cost of coding the BPRED mode : x->mbmode_cost[x->e_mbd.frame_type][BPRED];
2014 tmp_rd = vp8_rd_pick_intra4x4mby_modes(cpi, x, &rate, &rate_y, &distortion, best_yrd);
2015 rate2 += rate;
2016 distortion2 += distortion;
2018 if(tmp_rd < best_yrd)
2020 rate2 += uv_intra_rate;
2021 rate_uv = uv_intra_rate_tokenonly;
2022 distortion2 += uv_intra_distortion;
2023 distortion_uv = uv_intra_distortion;
2025 else
2027 this_rd = INT_MAX;
2028 disable_skip = 1;
2031 break;
2033 case SPLITMV:
2035 int tmp_rd;
2036 int this_rd_thresh;
2038 this_rd_thresh = (x->e_mbd.mode_info_context->mbmi.ref_frame == LAST_FRAME) ? cpi->rd_threshes[THR_NEWMV] : cpi->rd_threshes[THR_NEWA];
2039 this_rd_thresh = (x->e_mbd.mode_info_context->mbmi.ref_frame == GOLDEN_FRAME) ? cpi->rd_threshes[THR_NEWG]: this_rd_thresh;
2041 tmp_rd = vp8_rd_pick_best_mbsegmentation(cpi, x, &best_ref_mv,
2042 best_yrd, mdcounts,
2043 &rate, &rate_y, &distortion, this_rd_thresh) ;
2045 rate2 += rate;
2046 distortion2 += distortion;
2048 // If even the 'Y' rd value of split is higher than best so far then dont bother looking at UV
2049 if (tmp_rd < best_yrd)
2051 // Now work out UV cost and add it in
2052 vp8_rd_inter_uv(cpi, x, &rate_uv, &distortion_uv, cpi->common.full_pixel);
2053 rate2 += rate_uv;
2054 distortion2 += distortion_uv;
2056 else
2058 this_rd = INT_MAX;
2059 disable_skip = 1;
2062 break;
2063 case DC_PRED:
2064 case V_PRED:
2065 case H_PRED:
2066 case TM_PRED:
2067 x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
2068 vp8_build_intra_predictors_mby_ptr(&x->e_mbd);
2069 macro_block_yrd(x, &rate_y, &distortion, IF_RTCD(&cpi->rtcd.encodemb)) ;
2070 rate2 += rate_y;
2071 distortion2 += distortion;
2072 rate2 += x->mbmode_cost[x->e_mbd.frame_type][x->e_mbd.mode_info_context->mbmi.mode];
2073 rate2 += uv_intra_rate;
2074 rate_uv = uv_intra_rate_tokenonly;
2075 distortion2 += uv_intra_distortion;
2076 distortion_uv = uv_intra_distortion;
2077 break;
2079 case NEWMV:
2081 // Decrement full search counter
2082 if (cpi->check_freq[lf_or_gf] > 0)
2083 cpi->check_freq[lf_or_gf] --;
2086 int thissme;
2087 int bestsme = INT_MAX;
2088 int step_param = cpi->sf.first_step;
2089 int search_range;
2090 int further_steps;
2091 int n;
2093 int col_min = (best_ref_mv.col - MAX_FULL_PEL_VAL) >>3;
2094 int col_max = (best_ref_mv.col + MAX_FULL_PEL_VAL) >>3;
2095 int row_min = (best_ref_mv.row - MAX_FULL_PEL_VAL) >>3;
2096 int row_max = (best_ref_mv.row + MAX_FULL_PEL_VAL) >>3;
2098 int tmp_col_min = x->mv_col_min;
2099 int tmp_col_max = x->mv_col_max;
2100 int tmp_row_min = x->mv_row_min;
2101 int tmp_row_max = x->mv_row_max;
2103 // Get intersection of UMV window and valid MV window to reduce # of checks in diamond search.
2104 if (x->mv_col_min < col_min )
2105 x->mv_col_min = col_min;
2106 if (x->mv_col_max > col_max )
2107 x->mv_col_max = col_max;
2108 if (x->mv_row_min < row_min )
2109 x->mv_row_min = row_min;
2110 if (x->mv_row_max > row_max )
2111 x->mv_row_max = row_max;
2113 //adjust search range according to sr from mv prediction
2114 if(sr > step_param)
2115 step_param = sr;
2117 // Work out how long a search we should do
2118 search_range = MAXF(abs(best_ref_mv.col), abs(best_ref_mv.row)) >> 3;
2120 if (search_range >= x->vector_range)
2121 x->vector_range = search_range;
2122 else if (x->vector_range > cpi->sf.min_fs_radius)
2123 x->vector_range--;
2125 // Initial step/diamond search
2127 int sadpb = x->sadperbit16;
2129 if (cpi->sf.search_method == HEX)
2131 bestsme = vp8_hex_search(x, b, d, &best_ref_mv, &d->bmi.mv.as_mv, step_param, sadpb/*x->errorperbit*/, &num00, &cpi->fn_ptr[BLOCK_16X16], x->mvsadcost, x->mvcost, &best_ref_mv);
2132 mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
2133 mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
2135 else
2137 bestsme = cpi->diamond_search_sad(x, b, d, &mvp, &d->bmi.mv.as_mv, step_param, sadpb / 2/*x->errorperbit*/, &num00, &cpi->fn_ptr[BLOCK_16X16], x->mvsadcost, x->mvcost, &best_ref_mv); //sadpb < 9
2138 mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
2139 mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
2141 // Further step/diamond searches as necessary
2142 n = 0;
2143 further_steps = (cpi->sf.max_step_search_steps - 1) - step_param;
2145 n = num00;
2146 num00 = 0;
2148 while (n < further_steps)
2150 n++;
2152 if (num00)
2153 num00--;
2154 else
2156 thissme = cpi->diamond_search_sad(x, b, d, &mvp, &d->bmi.mv.as_mv, step_param + n, sadpb / 4/*x->errorperbit*/, &num00, &cpi->fn_ptr[BLOCK_16X16], x->mvsadcost, x->mvcost, &best_ref_mv); //sadpb = 9
2158 if (thissme < bestsme)
2160 bestsme = thissme;
2161 mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
2162 mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
2164 else
2166 d->bmi.mv.as_mv.row = mode_mv[NEWMV].row;
2167 d->bmi.mv.as_mv.col = mode_mv[NEWMV].col;
2175 // Should we do a full search
2176 if (!cpi->check_freq[lf_or_gf] || cpi->do_full[lf_or_gf])
2178 int thissme;
2179 int full_flag_thresh = 0;
2180 MV full_mvp;
2182 full_mvp.row = d->bmi.mv.as_mv.row <<3; // use diamond search result as full search staring point
2183 full_mvp.col = d->bmi.mv.as_mv.col <<3;
2185 // Update x->vector_range based on best vector found in step search
2186 search_range = MAXF(abs((mvp.row>>3) - d->bmi.mv.as_mv.row), abs((mvp.col>>3) - d->bmi.mv.as_mv.col));
2187 //search_range *= 1.4; //didn't improve PSNR
2189 if (search_range > x->vector_range)
2190 x->vector_range = search_range;
2191 else
2192 search_range = x->vector_range;
2194 // Apply limits
2195 search_range = (search_range > cpi->sf.max_fs_radius) ? cpi->sf.max_fs_radius : search_range;
2197 //add this to reduce full search range.
2198 if(sr<=3 && search_range > 8) search_range = 8;
2201 int sadpb = x->sadperbit16 >> 2;
2202 thissme = cpi->full_search_sad(x, b, d, &full_mvp, sadpb, search_range, &cpi->fn_ptr[BLOCK_16X16], x->mvcost, x->mvsadcost,&best_ref_mv);
2205 // Barrier threshold to initiating full search
2206 // full_flag_thresh = 10 + (thissme >> 7);
2207 if ((thissme + full_flag_thresh) < bestsme)
2209 cpi->do_full[lf_or_gf] ++;
2210 bestsme = thissme;
2212 else if (thissme < bestsme)
2213 bestsme = thissme;
2214 else
2216 cpi->do_full[lf_or_gf] = cpi->do_full[lf_or_gf] >> 1;
2217 cpi->check_freq[lf_or_gf] = cpi->sf.full_freq[lf_or_gf];
2219 // The full search result is actually worse so re-instate the previous best vector
2220 d->bmi.mv.as_mv.row = mode_mv[NEWMV].row;
2221 d->bmi.mv.as_mv.col = mode_mv[NEWMV].col;
2225 x->mv_col_min = tmp_col_min;
2226 x->mv_col_max = tmp_col_max;
2227 x->mv_row_min = tmp_row_min;
2228 x->mv_row_max = tmp_row_max;
2230 if (bestsme < INT_MAX)
2231 // cpi->find_fractional_mv_step(x,b,d,&d->bmi.mv.as_mv,&best_ref_mv,x->errorperbit/2,cpi->fn_ptr.svf,cpi->fn_ptr.vf,x->mvcost); // normal mvc=11
2232 cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv.as_mv, &best_ref_mv, x->errorperbit / 4, &cpi->fn_ptr[BLOCK_16X16], x->mvcost);
2234 mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
2235 mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
2237 // Add the new motion vector cost to our rolling cost variable
2238 rate2 += vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv, x->mvcost, 96);
2242 case NEARESTMV:
2243 case NEARMV:
2245 // Clip "next_nearest" so that it does not extend to far out of image
2246 if (mode_mv[this_mode].col < (xd->mb_to_left_edge - LEFT_TOP_MARGIN))
2247 mode_mv[this_mode].col = xd->mb_to_left_edge - LEFT_TOP_MARGIN;
2248 else if (mode_mv[this_mode].col > xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN)
2249 mode_mv[this_mode].col = xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN;
2251 if (mode_mv[this_mode].row < (xd->mb_to_top_edge - LEFT_TOP_MARGIN))
2252 mode_mv[this_mode].row = xd->mb_to_top_edge - LEFT_TOP_MARGIN;
2253 else if (mode_mv[this_mode].row > xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN)
2254 mode_mv[this_mode].row = xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN;
2256 // Do not bother proceeding if the vector (from newmv,nearest or near) is 0,0 as this should then be coded using the zeromv mode.
2257 if (((this_mode == NEARMV) || (this_mode == NEARESTMV)) &&
2258 ((mode_mv[this_mode].row == 0) && (mode_mv[this_mode].col == 0)))
2259 continue;
2261 case ZEROMV:
2263 mv_selected:
2265 // Trap vectors that reach beyond the UMV borders
2266 // Note that ALL New MV, Nearest MV Near MV and Zero MV code drops through to this point
2267 // because of the lack of break statements in the previous two cases.
2268 if (((mode_mv[this_mode].row >> 3) < x->mv_row_min) || ((mode_mv[this_mode].row >> 3) > x->mv_row_max) ||
2269 ((mode_mv[this_mode].col >> 3) < x->mv_col_min) || ((mode_mv[this_mode].col >> 3) > x->mv_col_max))
2270 continue;
2272 vp8_set_mbmode_and_mvs(x, this_mode, &mode_mv[this_mode]);
2273 vp8_build_inter_predictors_mby(&x->e_mbd);
2275 if (cpi->active_map_enabled && x->active_ptr[0] == 0) {
2276 x->skip = 1;
2278 else if (x->encode_breakout)
2280 int sum, sse;
2281 int threshold = (xd->block[0].dequant[1]
2282 * xd->block[0].dequant[1] >>4);
2284 if(threshold < x->encode_breakout)
2285 threshold = x->encode_breakout;
2287 VARIANCE_INVOKE(&cpi->rtcd.variance, get16x16var)
2288 (x->src.y_buffer, x->src.y_stride,
2289 x->e_mbd.predictor, 16, (unsigned int *)(&sse), &sum);
2291 if (sse < threshold)
2293 // Check u and v to make sure skip is ok
2294 int sse2 = 0;
2295 /* If theres is no codeable 2nd order dc
2296 or a very small uniform pixel change change */
2297 if (abs(sum) < (xd->block[24].dequant[0]<<2)||
2298 ((sum * sum>>8) > sse && abs(sum) <128))
2300 sse2 = VP8_UVSSE(x, IF_RTCD(&cpi->rtcd.variance));
2302 if (sse2 * 2 < threshold)
2304 x->skip = 1;
2305 distortion2 = sse + sse2;
2306 rate2 = 500;
2308 /* for best_yrd calculation */
2309 rate_uv = 0;
2310 distortion_uv = sse2;
2312 disable_skip = 1;
2313 this_rd = RDCOST(x->rdmult, x->rddiv, rate2,
2314 distortion2);
2316 break;
2323 //intermodecost[mode_index] = vp8_cost_mv_ref(this_mode, mdcounts); // Experimental debug code
2325 // Add in the Mv/mode cost
2326 rate2 += vp8_cost_mv_ref(this_mode, mdcounts);
2328 // Y cost and distortion
2329 macro_block_yrd(x, &rate_y, &distortion, IF_RTCD(&cpi->rtcd.encodemb));
2330 rate2 += rate_y;
2331 distortion2 += distortion;
2333 // UV cost and distortion
2334 vp8_rd_inter_uv(cpi, x, &rate_uv, &distortion_uv, cpi->common.full_pixel);
2335 rate2 += rate_uv;
2336 distortion2 += distortion_uv;
2337 break;
2339 default:
2340 break;
2343 // Where skip is allowable add in the default per mb cost for the no skip case.
2344 // where we then decide to skip we have to delete this and replace it with the
2345 // cost of signallying a skip
2346 if (cpi->common.mb_no_coeff_skip)
2348 other_cost += vp8_cost_bit(cpi->prob_skip_false, 0);
2349 rate2 += other_cost;
2352 // Estimate the reference frame signaling cost and add it to the rolling cost variable.
2353 rate2 += ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame];
2355 if (!disable_skip)
2357 // Test for the condition where skip block will be activated because there are no non zero coefficients and make any necessary adjustment for rate
2358 if (cpi->common.mb_no_coeff_skip)
2360 int tteob;
2362 tteob = 0;
2364 for (i = 0; i <= 24; i++)
2366 tteob += x->e_mbd.block[i].eob;
2369 if (tteob == 0)
2371 rate2 -= (rate_y + rate_uv);
2372 //for best_yrd calculation
2373 rate_uv = 0;
2375 // Back out no skip flag costing and add in skip flag costing
2376 if (cpi->prob_skip_false)
2378 int prob_skip_cost;
2380 prob_skip_cost = vp8_cost_bit(cpi->prob_skip_false, 1);
2381 prob_skip_cost -= vp8_cost_bit(cpi->prob_skip_false, 0);
2382 rate2 += prob_skip_cost;
2383 other_cost += prob_skip_cost;
2387 // Calculate the final RD estimate for this mode
2388 this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
2391 // Experimental debug code.
2392 //all_rds[mode_index] = this_rd;
2393 //all_rates[mode_index] = rate2;
2394 //all_dist[mode_index] = distortion2;
2396 if ((x->e_mbd.mode_info_context->mbmi.ref_frame == INTRA_FRAME) && (this_rd < *returnintra))
2398 *returnintra = this_rd ;
2401 // Did this mode help.. i.i is it the new best mode
2402 if (this_rd < best_rd || x->skip)
2404 // Note index of best mode so far
2405 best_mode_index = mode_index;
2406 x->e_mbd.mode_info_context->mbmi.force_no_skip = force_no_skip;
2408 if (this_mode <= B_PRED)
2410 x->e_mbd.mode_info_context->mbmi.uv_mode = uv_intra_mode;
2413 other_cost += ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame];
2415 /* Calculate the final y RD estimate for this mode */
2416 best_yrd = RDCOST(x->rdmult, x->rddiv, (rate2-rate_uv-other_cost),
2417 (distortion2-distortion_uv));
2419 *returnrate = rate2;
2420 *returndistortion = distortion2;
2421 best_rd = this_rd;
2422 vpx_memcpy(&best_mbmode, &x->e_mbd.mode_info_context->mbmi, sizeof(MB_MODE_INFO));
2423 vpx_memcpy(&best_partition, x->partition_info, sizeof(PARTITION_INFO));
2425 for (i = 0; i < 16; i++)
2427 vpx_memcpy(&best_bmodes[i], &x->e_mbd.block[i].bmi, sizeof(B_MODE_INFO));
2430 // Testing this mode gave rise to an improvement in best error score. Lower threshold a bit for next time
2431 cpi->rd_thresh_mult[mode_index] = (cpi->rd_thresh_mult[mode_index] >= (MIN_THRESHMULT + 2)) ? cpi->rd_thresh_mult[mode_index] - 2 : MIN_THRESHMULT;
2432 cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
2435 // If the mode did not help improve the best error case then raise the threshold for testing that mode next time around.
2436 else
2438 cpi->rd_thresh_mult[mode_index] += 4;
2440 if (cpi->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
2441 cpi->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
2443 cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
2446 if (x->skip)
2447 break;
2451 // Reduce the activation RD thresholds for the best choice mode
2452 if ((cpi->rd_baseline_thresh[best_mode_index] > 0) && (cpi->rd_baseline_thresh[best_mode_index] < (INT_MAX >> 2)))
2454 int best_adjustment = (cpi->rd_thresh_mult[best_mode_index] >> 2);
2456 cpi->rd_thresh_mult[best_mode_index] = (cpi->rd_thresh_mult[best_mode_index] >= (MIN_THRESHMULT + best_adjustment)) ? cpi->rd_thresh_mult[best_mode_index] - best_adjustment : MIN_THRESHMULT;
2457 cpi->rd_threshes[best_mode_index] = (cpi->rd_baseline_thresh[best_mode_index] >> 7) * cpi->rd_thresh_mult[best_mode_index];
2459 // If we chose a split mode then reset the new MV thresholds as well
2460 /*if ( vp8_mode_order[best_mode_index] == SPLITMV )
2462 best_adjustment = 4; //(cpi->rd_thresh_mult[THR_NEWMV] >> 4);
2463 cpi->rd_thresh_mult[THR_NEWMV] = (cpi->rd_thresh_mult[THR_NEWMV] >= (MIN_THRESHMULT+best_adjustment)) ? cpi->rd_thresh_mult[THR_NEWMV]-best_adjustment: MIN_THRESHMULT;
2464 cpi->rd_threshes[THR_NEWMV] = (cpi->rd_baseline_thresh[THR_NEWMV] >> 7) * cpi->rd_thresh_mult[THR_NEWMV];
2466 best_adjustment = 4; //(cpi->rd_thresh_mult[THR_NEWG] >> 4);
2467 cpi->rd_thresh_mult[THR_NEWG] = (cpi->rd_thresh_mult[THR_NEWG] >= (MIN_THRESHMULT+best_adjustment)) ? cpi->rd_thresh_mult[THR_NEWG]-best_adjustment: MIN_THRESHMULT;
2468 cpi->rd_threshes[THR_NEWG] = (cpi->rd_baseline_thresh[THR_NEWG] >> 7) * cpi->rd_thresh_mult[THR_NEWG];
2470 best_adjustment = 4; //(cpi->rd_thresh_mult[THR_NEWA] >> 4);
2471 cpi->rd_thresh_mult[THR_NEWA] = (cpi->rd_thresh_mult[THR_NEWA] >= (MIN_THRESHMULT+best_adjustment)) ? cpi->rd_thresh_mult[THR_NEWA]-best_adjustment: MIN_THRESHMULT;
2472 cpi->rd_threshes[THR_NEWA] = (cpi->rd_baseline_thresh[THR_NEWA] >> 7) * cpi->rd_thresh_mult[THR_NEWA];
2477 // If we have chosen new mv or split then decay the full search check count more quickly.
2478 if ((vp8_mode_order[best_mode_index] == NEWMV) || (vp8_mode_order[best_mode_index] == SPLITMV))
2480 int lf_or_gf = (vp8_ref_frame_order[best_mode_index] == LAST_FRAME) ? 0 : 1;
2482 if (cpi->check_freq[lf_or_gf] && !cpi->do_full[lf_or_gf])
2484 cpi->check_freq[lf_or_gf] --;
2488 // Keep a record of best mode index that we chose
2489 cpi->last_best_mode_index = best_mode_index;
2491 // Note how often each mode chosen as best
2492 cpi->mode_chosen_counts[best_mode_index] ++;
2495 if (cpi->is_src_frame_alt_ref && (best_mbmode.mode != ZEROMV || best_mbmode.ref_frame != ALTREF_FRAME))
2497 best_mbmode.mode = ZEROMV;
2498 best_mbmode.ref_frame = ALTREF_FRAME;
2499 best_mbmode.mv.as_int = 0;
2500 best_mbmode.uv_mode = 0;
2501 best_mbmode.mb_skip_coeff = (cpi->common.mb_no_coeff_skip) ? 1 : 0;
2502 best_mbmode.partitioning = 0;
2503 best_mbmode.dc_diff = 0;
2505 vpx_memcpy(&x->e_mbd.mode_info_context->mbmi, &best_mbmode, sizeof(MB_MODE_INFO));
2506 vpx_memcpy(x->partition_info, &best_partition, sizeof(PARTITION_INFO));
2508 for (i = 0; i < 16; i++)
2510 vpx_memset(&x->e_mbd.block[i].bmi, 0, sizeof(B_MODE_INFO));
2513 x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
2515 return best_rd;
2519 if(best_mbmode.mode <= B_PRED)
2521 int i;
2522 for (i = 0; i < 16; i++)
2524 best_bmodes[i].mv.as_int = 0;
2528 // macroblock modes
2529 vpx_memcpy(&x->e_mbd.mode_info_context->mbmi, &best_mbmode, sizeof(MB_MODE_INFO));
2530 vpx_memcpy(x->partition_info, &best_partition, sizeof(PARTITION_INFO));
2532 for (i = 0; i < 16; i++)
2534 vpx_memcpy(&x->e_mbd.block[i].bmi, &best_bmodes[i], sizeof(B_MODE_INFO));
2537 x->e_mbd.mode_info_context->mbmi.mv.as_mv = x->e_mbd.block[15].bmi.mv.as_mv;
2539 return best_rd;
2541 #endif