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