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