[go: nahoru, domu]

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