| OLD | NEW |
| (Empty) | |
| 1 /* |
| 2 * Copyright (c) 2011 The WebM project authors. All Rights Reserved. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ |
| 10 |
| 11 #include "error_concealment.h" |
| 12 #include "onyxd_int.h" |
| 13 #include "decodemv.h" |
| 14 #include "vpx_mem/vpx_mem.h" |
| 15 #include "vp8/common/recon.h" |
| 16 #include "vp8/common/findnearmv.h" |
| 17 |
| 18 #include <assert.h> |
| 19 |
| 20 #define MIN(x,y) (((x)<(y))?(x):(y)) |
| 21 #define MAX(x,y) (((x)>(y))?(x):(y)) |
| 22 |
| 23 #define FLOOR(x,q) ((x) & -(1 << (q))) |
| 24 |
| 25 #define NUM_NEIGHBORS 20 |
| 26 |
| 27 typedef struct ec_position |
| 28 { |
| 29 int row; |
| 30 int col; |
| 31 } EC_POS; |
| 32 |
| 33 /* |
| 34 * Regenerate the table in Matlab with: |
| 35 * x = meshgrid((1:4), (1:4)); |
| 36 * y = meshgrid((1:4), (1:4))'; |
| 37 * W = round((1./(sqrt(x.^2 + y.^2))*2^7)); |
| 38 * W(1,1) = 0; |
| 39 */ |
| 40 static const int weights_q7[5][5] = { |
| 41 { 0, 128, 64, 43, 32 }, |
| 42 {128, 91, 57, 40, 31 }, |
| 43 { 64, 57, 45, 36, 29 }, |
| 44 { 43, 40, 36, 30, 26 }, |
| 45 { 32, 31, 29, 26, 23 } |
| 46 }; |
| 47 |
| 48 int vp8_alloc_overlap_lists(VP8D_COMP *pbi) |
| 49 { |
| 50 if (pbi->overlaps != NULL) |
| 51 { |
| 52 vpx_free(pbi->overlaps); |
| 53 pbi->overlaps = NULL; |
| 54 } |
| 55 pbi->overlaps = vpx_calloc(pbi->common.mb_rows * pbi->common.mb_cols, |
| 56 sizeof(MB_OVERLAP)); |
| 57 if (pbi->overlaps == NULL) |
| 58 return -1; |
| 59 vpx_memset(pbi->overlaps, 0, |
| 60 sizeof(MB_OVERLAP) * pbi->common.mb_rows * pbi->common.mb_cols); |
| 61 return 0; |
| 62 } |
| 63 |
| 64 void vp8_de_alloc_overlap_lists(VP8D_COMP *pbi) |
| 65 { |
| 66 vpx_free(pbi->overlaps); |
| 67 pbi->overlaps = NULL; |
| 68 } |
| 69 |
| 70 /* Inserts a new overlap area value to the list of overlaps of a block */ |
| 71 static void assign_overlap(OVERLAP_NODE* overlaps, |
| 72 union b_mode_info *bmi, |
| 73 int overlap) |
| 74 { |
| 75 int i; |
| 76 if (overlap <= 0) |
| 77 return; |
| 78 /* Find and assign to the next empty overlap node in the list of overlaps. |
| 79 * Empty is defined as bmi == NULL */ |
| 80 for (i = 0; i < MAX_OVERLAPS; i++) |
| 81 { |
| 82 if (overlaps[i].bmi == NULL) |
| 83 { |
| 84 overlaps[i].bmi = bmi; |
| 85 overlaps[i].overlap = overlap; |
| 86 break; |
| 87 } |
| 88 } |
| 89 } |
| 90 |
| 91 /* Calculates the overlap area between two 4x4 squares, where the first |
| 92 * square has its upper-left corner at (b1_row, b1_col) and the second |
| 93 * square has its upper-left corner at (b2_row, b2_col). Doesn't |
| 94 * properly handle squares which do not overlap. |
| 95 */ |
| 96 static int block_overlap(int b1_row, int b1_col, int b2_row, int b2_col) |
| 97 { |
| 98 const int int_top = MAX(b1_row, b2_row); // top |
| 99 const int int_left = MAX(b1_col, b2_col); // left |
| 100 /* Since each block is 4x4 pixels, adding 4 (Q3) to the left/top edge |
| 101 * gives us the right/bottom edge. |
| 102 */ |
| 103 const int int_right = MIN(b1_col + (4<<3), b2_col + (4<<3)); // right |
| 104 const int int_bottom = MIN(b1_row + (4<<3), b2_row + (4<<3)); // bottom |
| 105 return (int_bottom - int_top) * (int_right - int_left); |
| 106 } |
| 107 |
| 108 /* Calculates the overlap area for all blocks in a macroblock at position |
| 109 * (mb_row, mb_col) in macroblocks, which are being overlapped by a given |
| 110 * overlapping block at position (new_row, new_col) (in pixels, Q3). The |
| 111 * first block being overlapped in the macroblock has position (first_blk_row, |
| 112 * first_blk_col) in blocks relative the upper-left corner of the image. |
| 113 */ |
| 114 static void calculate_overlaps_mb(B_OVERLAP *b_overlaps, union b_mode_info *bmi, |
| 115 int new_row, int new_col, |
| 116 int mb_row, int mb_col, |
| 117 int first_blk_row, int first_blk_col) |
| 118 { |
| 119 /* Find the blocks within this MB (defined by mb_row, mb_col) which are |
| 120 * overlapped by bmi and calculate and assign overlap for each of those |
| 121 * blocks. */ |
| 122 |
| 123 /* Block coordinates relative the upper-left block */ |
| 124 const int rel_ol_blk_row = first_blk_row - mb_row * 4; |
| 125 const int rel_ol_blk_col = first_blk_col - mb_col * 4; |
| 126 /* If the block partly overlaps any previous MB, these coordinates |
| 127 * can be < 0. We don't want to access blocks in previous MBs. |
| 128 */ |
| 129 const int blk_idx = MAX(rel_ol_blk_row,0) * 4 + MAX(rel_ol_blk_col,0); |
| 130 /* Upper left overlapping block */ |
| 131 B_OVERLAP *b_ol_ul = &(b_overlaps[blk_idx]); |
| 132 |
| 133 /* Calculate and assign overlaps for all blocks in this MB |
| 134 * which the motion compensated block overlaps |
| 135 */ |
| 136 /* Avoid calculating overlaps for blocks in later MBs */ |
| 137 int end_row = MIN(4 + mb_row * 4 - first_blk_row, 2); |
| 138 int end_col = MIN(4 + mb_col * 4 - first_blk_col, 2); |
| 139 int row, col; |
| 140 |
| 141 /* Check if new_row and new_col are evenly divisible by 4 (Q3), |
| 142 * and if so we shouldn't check neighboring blocks |
| 143 */ |
| 144 if (new_row >= 0 && (new_row & 0x1F) == 0) |
| 145 end_row = 1; |
| 146 if (new_col >= 0 && (new_col & 0x1F) == 0) |
| 147 end_col = 1; |
| 148 |
| 149 /* Check if the overlapping block partly overlaps a previous MB |
| 150 * and if so, we're overlapping fewer blocks in this MB. |
| 151 */ |
| 152 if (new_row < (mb_row*16)<<3) |
| 153 end_row = 1; |
| 154 if (new_col < (mb_col*16)<<3) |
| 155 end_col = 1; |
| 156 |
| 157 for (row = 0; row < end_row; ++row) |
| 158 { |
| 159 for (col = 0; col < end_col; ++col) |
| 160 { |
| 161 /* input in Q3, result in Q6 */ |
| 162 const int overlap = block_overlap(new_row, new_col, |
| 163 (((first_blk_row + row) * |
| 164 4) << 3), |
| 165 (((first_blk_col + col) * |
| 166 4) << 3)); |
| 167 assign_overlap(b_ol_ul[row * 4 + col].overlaps, bmi, overlap); |
| 168 } |
| 169 } |
| 170 } |
| 171 |
| 172 void vp8_calculate_overlaps(MB_OVERLAP *overlap_ul, |
| 173 int mb_rows, int mb_cols, |
| 174 union b_mode_info *bmi, |
| 175 int b_row, int b_col) |
| 176 { |
| 177 MB_OVERLAP *mb_overlap; |
| 178 int row, col, rel_row, rel_col; |
| 179 int new_row, new_col; |
| 180 int end_row, end_col; |
| 181 int overlap_b_row, overlap_b_col; |
| 182 int overlap_mb_row, overlap_mb_col; |
| 183 |
| 184 /* mb subpixel position */ |
| 185 row = (4 * b_row) << 3; /* Q3 */ |
| 186 col = (4 * b_col) << 3; /* Q3 */ |
| 187 |
| 188 /* reverse compensate for motion */ |
| 189 new_row = row - bmi->mv.as_mv.row; |
| 190 new_col = col - bmi->mv.as_mv.col; |
| 191 |
| 192 if (new_row >= ((16*mb_rows) << 3) || new_col >= ((16*mb_cols) << 3)) |
| 193 { |
| 194 /* the new block ended up outside the frame */ |
| 195 return; |
| 196 } |
| 197 |
| 198 if (new_row <= (-4 << 3) || new_col <= (-4 << 3)) |
| 199 { |
| 200 /* outside the frame */ |
| 201 return; |
| 202 } |
| 203 /* overlapping block's position in blocks */ |
| 204 overlap_b_row = FLOOR(new_row / 4, 3) >> 3; |
| 205 overlap_b_col = FLOOR(new_col / 4, 3) >> 3; |
| 206 |
| 207 /* overlapping block's MB position in MBs |
| 208 * operations are done in Q3 |
| 209 */ |
| 210 overlap_mb_row = FLOOR((overlap_b_row << 3) / 4, 3) >> 3; |
| 211 overlap_mb_col = FLOOR((overlap_b_col << 3) / 4, 3) >> 3; |
| 212 |
| 213 end_row = MIN(mb_rows - overlap_mb_row, 2); |
| 214 end_col = MIN(mb_cols - overlap_mb_col, 2); |
| 215 |
| 216 /* Don't calculate overlap for MBs we don't overlap */ |
| 217 /* Check if the new block row starts at the last block row of the MB */ |
| 218 if (abs(new_row - ((16*overlap_mb_row) << 3)) < ((3*4) << 3)) |
| 219 end_row = 1; |
| 220 /* Check if the new block col starts at the last block col of the MB */ |
| 221 if (abs(new_col - ((16*overlap_mb_col) << 3)) < ((3*4) << 3)) |
| 222 end_col = 1; |
| 223 |
| 224 /* find the MB(s) this block is overlapping */ |
| 225 for (rel_row = 0; rel_row < end_row; ++rel_row) |
| 226 { |
| 227 for (rel_col = 0; rel_col < end_col; ++rel_col) |
| 228 { |
| 229 if (overlap_mb_row + rel_row < 0 || |
| 230 overlap_mb_col + rel_col < 0) |
| 231 continue; |
| 232 mb_overlap = overlap_ul + (overlap_mb_row + rel_row) * mb_cols + |
| 233 overlap_mb_col + rel_col; |
| 234 |
| 235 calculate_overlaps_mb(mb_overlap->overlaps, bmi, |
| 236 new_row, new_col, |
| 237 overlap_mb_row + rel_row, |
| 238 overlap_mb_col + rel_col, |
| 239 overlap_b_row + rel_row, |
| 240 overlap_b_col + rel_col); |
| 241 } |
| 242 } |
| 243 } |
| 244 |
| 245 /* Estimates a motion vector given the overlapping blocks' motion vectors. |
| 246 * Filters out all overlapping blocks which do not refer to the correct |
| 247 * reference frame type. |
| 248 */ |
| 249 static void estimate_mv(const OVERLAP_NODE *overlaps, union b_mode_info *bmi) |
| 250 { |
| 251 int i; |
| 252 int overlap_sum = 0; |
| 253 int row_acc = 0; |
| 254 int col_acc = 0; |
| 255 |
| 256 bmi->mv.as_int = 0; |
| 257 for (i=0; i < MAX_OVERLAPS; ++i) |
| 258 { |
| 259 if (overlaps[i].bmi == NULL) |
| 260 break; |
| 261 col_acc += overlaps[i].overlap * overlaps[i].bmi->mv.as_mv.col; |
| 262 row_acc += overlaps[i].overlap * overlaps[i].bmi->mv.as_mv.row; |
| 263 overlap_sum += overlaps[i].overlap; |
| 264 } |
| 265 if (overlap_sum > 0) |
| 266 { |
| 267 /* Q9 / Q6 = Q3 */ |
| 268 bmi->mv.as_mv.col = col_acc / overlap_sum; |
| 269 bmi->mv.as_mv.row = row_acc / overlap_sum; |
| 270 } |
| 271 else |
| 272 { |
| 273 bmi->mv.as_mv.col = 0; |
| 274 bmi->mv.as_mv.row = 0; |
| 275 } |
| 276 } |
| 277 |
| 278 /* Estimates all motion vectors for a macroblock given the lists of |
| 279 * overlaps for each block. Decides whether or not the MVs must be clamped. |
| 280 */ |
| 281 static void estimate_mb_mvs(const B_OVERLAP *block_overlaps, |
| 282 MODE_INFO *mi, |
| 283 int mb_to_left_edge, |
| 284 int mb_to_right_edge, |
| 285 int mb_to_top_edge, |
| 286 int mb_to_bottom_edge) |
| 287 { |
| 288 int i; |
| 289 int non_zero_count = 0; |
| 290 MV * const filtered_mv = &(mi->mbmi.mv.as_mv); |
| 291 union b_mode_info * const bmi = mi->bmi; |
| 292 filtered_mv->col = 0; |
| 293 filtered_mv->row = 0; |
| 294 for (i = 0; i < 16; ++i) |
| 295 { |
| 296 /* Estimate vectors for all blocks which are overlapped by this type */ |
| 297 /* Interpolate/extrapolate the rest of the block's MVs */ |
| 298 estimate_mv(block_overlaps[i].overlaps, &(bmi[i])); |
| 299 mi->mbmi.need_to_clamp_mvs = vp8_check_mv_bounds(&bmi[i].mv, |
| 300 mb_to_left_edge, |
| 301 mb_to_right_edge, |
| 302 mb_to_top_edge, |
| 303 mb_to_bottom_edge); |
| 304 if (bmi[i].mv.as_int != 0) |
| 305 { |
| 306 ++non_zero_count; |
| 307 filtered_mv->col += bmi[i].mv.as_mv.col; |
| 308 filtered_mv->row += bmi[i].mv.as_mv.row; |
| 309 } |
| 310 } |
| 311 if (non_zero_count > 0) |
| 312 { |
| 313 filtered_mv->col /= non_zero_count; |
| 314 filtered_mv->row /= non_zero_count; |
| 315 } |
| 316 } |
| 317 |
| 318 static void calc_prev_mb_overlaps(MB_OVERLAP *overlaps, MODE_INFO *prev_mi, |
| 319 int mb_row, int mb_col, |
| 320 int mb_rows, int mb_cols) |
| 321 { |
| 322 int sub_row; |
| 323 int sub_col; |
| 324 for (sub_row = 0; sub_row < 4; ++sub_row) |
| 325 { |
| 326 for (sub_col = 0; sub_col < 4; ++sub_col) |
| 327 { |
| 328 vp8_calculate_overlaps( |
| 329 overlaps, mb_rows, mb_cols, |
| 330 &(prev_mi->bmi[sub_row * 4 + sub_col]), |
| 331 4 * mb_row + sub_row, |
| 332 4 * mb_col + sub_col); |
| 333 } |
| 334 } |
| 335 } |
| 336 |
| 337 /* Estimate all missing motion vectors. This function does the same as the one |
| 338 * above, but has different input arguments. */ |
| 339 static void estimate_missing_mvs(MB_OVERLAP *overlaps, |
| 340 MODE_INFO *mi, MODE_INFO *prev_mi, |
| 341 int mb_rows, int mb_cols, |
| 342 unsigned int first_corrupt) |
| 343 { |
| 344 int mb_row, mb_col; |
| 345 vpx_memset(overlaps, 0, sizeof(MB_OVERLAP) * mb_rows * mb_cols); |
| 346 /* First calculate the overlaps for all blocks */ |
| 347 for (mb_row = 0; mb_row < mb_rows; ++mb_row) |
| 348 { |
| 349 for (mb_col = 0; mb_col < mb_cols; ++mb_col) |
| 350 { |
| 351 /* We're only able to use blocks referring to the last frame |
| 352 * when extrapolating new vectors. |
| 353 */ |
| 354 if (prev_mi->mbmi.ref_frame == LAST_FRAME) |
| 355 { |
| 356 calc_prev_mb_overlaps(overlaps, prev_mi, |
| 357 mb_row, mb_col, |
| 358 mb_rows, mb_cols); |
| 359 } |
| 360 ++prev_mi; |
| 361 } |
| 362 ++prev_mi; |
| 363 } |
| 364 |
| 365 mb_row = first_corrupt / mb_cols; |
| 366 mb_col = first_corrupt - mb_row * mb_cols; |
| 367 mi += mb_row*(mb_cols + 1) + mb_col; |
| 368 /* Go through all macroblocks in the current image with missing MVs |
| 369 * and calculate new MVs using the overlaps. |
| 370 */ |
| 371 for (; mb_row < mb_rows; ++mb_row) |
| 372 { |
| 373 int mb_to_top_edge = -((mb_row * 16)) << 3; |
| 374 int mb_to_bottom_edge = ((mb_rows - 1 - mb_row) * 16) << 3; |
| 375 for (; mb_col < mb_cols; ++mb_col) |
| 376 { |
| 377 int mb_to_left_edge = -((mb_col * 16) << 3); |
| 378 int mb_to_right_edge = ((mb_cols - 1 - mb_col) * 16) << 3; |
| 379 const B_OVERLAP *block_overlaps = |
| 380 overlaps[mb_row*mb_cols + mb_col].overlaps; |
| 381 mi->mbmi.ref_frame = LAST_FRAME; |
| 382 mi->mbmi.mode = SPLITMV; |
| 383 mi->mbmi.uv_mode = DC_PRED; |
| 384 mi->mbmi.partitioning = 3; |
| 385 mi->mbmi.segment_id = 0; |
| 386 estimate_mb_mvs(block_overlaps, |
| 387 mi, |
| 388 mb_to_left_edge, |
| 389 mb_to_right_edge, |
| 390 mb_to_top_edge, |
| 391 mb_to_bottom_edge); |
| 392 ++mi; |
| 393 } |
| 394 mb_col = 0; |
| 395 ++mi; |
| 396 } |
| 397 } |
| 398 |
| 399 void vp8_estimate_missing_mvs(VP8D_COMP *pbi) |
| 400 { |
| 401 VP8_COMMON * const pc = &pbi->common; |
| 402 estimate_missing_mvs(pbi->overlaps, |
| 403 pc->mi, pc->prev_mi, |
| 404 pc->mb_rows, pc->mb_cols, |
| 405 pbi->mvs_corrupt_from_mb); |
| 406 } |
| 407 |
| 408 static void assign_neighbor(EC_BLOCK *neighbor, MODE_INFO *mi, int block_idx) |
| 409 { |
| 410 assert(mi->mbmi.ref_frame < MAX_REF_FRAMES); |
| 411 neighbor->ref_frame = mi->mbmi.ref_frame; |
| 412 neighbor->mv = mi->bmi[block_idx].mv.as_mv; |
| 413 } |
| 414 |
| 415 /* Finds the neighboring blocks of a macroblocks. In the general case |
| 416 * 20 blocks are found. If a fewer number of blocks are found due to |
| 417 * image boundaries, those positions in the EC_BLOCK array are left "empty". |
| 418 * The neighbors are enumerated with the upper-left neighbor as the first |
| 419 * element, the second element refers to the neighbor to right of the previous |
| 420 * neighbor, and so on. The last element refers to the neighbor below the first |
| 421 * neighbor. |
| 422 */ |
| 423 static void find_neighboring_blocks(MODE_INFO *mi, |
| 424 EC_BLOCK *neighbors, |
| 425 int mb_row, int mb_col, |
| 426 int mb_rows, int mb_cols, |
| 427 int mi_stride) |
| 428 { |
| 429 int i = 0; |
| 430 int j; |
| 431 if (mb_row > 0) |
| 432 { |
| 433 /* upper left */ |
| 434 if (mb_col > 0) |
| 435 assign_neighbor(&neighbors[i], mi - mi_stride - 1, 15); |
| 436 ++i; |
| 437 /* above */ |
| 438 for (j = 12; j < 16; ++j, ++i) |
| 439 assign_neighbor(&neighbors[i], mi - mi_stride, j); |
| 440 } |
| 441 else |
| 442 i += 5; |
| 443 if (mb_col < mb_cols - 1) |
| 444 { |
| 445 /* upper right */ |
| 446 if (mb_row > 0) |
| 447 assign_neighbor(&neighbors[i], mi - mi_stride + 1, 12); |
| 448 ++i; |
| 449 /* right */ |
| 450 for (j = 0; j <= 12; j += 4, ++i) |
| 451 assign_neighbor(&neighbors[i], mi + 1, j); |
| 452 } |
| 453 else |
| 454 i += 5; |
| 455 if (mb_row < mb_rows - 1) |
| 456 { |
| 457 /* lower right */ |
| 458 if (mb_col < mb_cols - 1) |
| 459 assign_neighbor(&neighbors[i], mi + mi_stride + 1, 0); |
| 460 ++i; |
| 461 /* below */ |
| 462 for (j = 0; j < 4; ++j, ++i) |
| 463 assign_neighbor(&neighbors[i], mi + mi_stride, j); |
| 464 } |
| 465 else |
| 466 i += 5; |
| 467 if (mb_col > 0) |
| 468 { |
| 469 /* lower left */ |
| 470 if (mb_row < mb_rows - 1) |
| 471 assign_neighbor(&neighbors[i], mi + mi_stride - 1, 4); |
| 472 ++i; |
| 473 /* left */ |
| 474 for (j = 3; j < 16; j += 4, ++i) |
| 475 { |
| 476 assign_neighbor(&neighbors[i], mi - 1, j); |
| 477 } |
| 478 } |
| 479 else |
| 480 i += 5; |
| 481 assert(i == 20); |
| 482 } |
| 483 |
| 484 /* Calculates which reference frame type is dominating among the neighbors */ |
| 485 static MV_REFERENCE_FRAME dominant_ref_frame(EC_BLOCK *neighbors) |
| 486 { |
| 487 /* Default to referring to "skip" */ |
| 488 MV_REFERENCE_FRAME dom_ref_frame = LAST_FRAME; |
| 489 int max_ref_frame_cnt = 0; |
| 490 int ref_frame_cnt[MAX_REF_FRAMES] = {0}; |
| 491 int i; |
| 492 /* Count neighboring reference frames */ |
| 493 for (i = 0; i < NUM_NEIGHBORS; ++i) |
| 494 { |
| 495 if (neighbors[i].ref_frame < MAX_REF_FRAMES && |
| 496 neighbors[i].ref_frame != INTRA_FRAME) |
| 497 ++ref_frame_cnt[neighbors[i].ref_frame]; |
| 498 } |
| 499 /* Find maximum */ |
| 500 for (i = 0; i < MAX_REF_FRAMES; ++i) |
| 501 { |
| 502 if (ref_frame_cnt[i] > max_ref_frame_cnt) |
| 503 { |
| 504 dom_ref_frame = i; |
| 505 max_ref_frame_cnt = ref_frame_cnt[i]; |
| 506 } |
| 507 } |
| 508 return dom_ref_frame; |
| 509 } |
| 510 |
| 511 /* Interpolates all motion vectors for a macroblock from the neighboring blocks' |
| 512 * motion vectors. |
| 513 */ |
| 514 static void interpolate_mvs(MACROBLOCKD *mb, |
| 515 EC_BLOCK *neighbors, |
| 516 MV_REFERENCE_FRAME dom_ref_frame) |
| 517 { |
| 518 int row, col, i; |
| 519 MODE_INFO * const mi = mb->mode_info_context; |
| 520 /* Table with the position of the neighboring blocks relative the position |
| 521 * of the upper left block of the current MB. Starting with the upper left |
| 522 * neighbor and going to the right. |
| 523 */ |
| 524 const EC_POS neigh_pos[NUM_NEIGHBORS] = { |
| 525 {-1,-1}, {-1,0}, {-1,1}, {-1,2}, {-1,3}, |
| 526 {-1,4}, {0,4}, {1,4}, {2,4}, {3,4}, |
| 527 {4,4}, {4,3}, {4,2}, {4,1}, {4,0}, |
| 528 {4,-1}, {3,-1}, {2,-1}, {1,-1}, {0,-1} |
| 529 }; |
| 530 for (row = 0; row < 4; ++row) |
| 531 { |
| 532 for (col = 0; col < 4; ++col) |
| 533 { |
| 534 int w_sum = 0; |
| 535 int mv_row_sum = 0; |
| 536 int mv_col_sum = 0; |
| 537 int_mv * const mv = &(mi->bmi[row*4 + col].mv); |
| 538 for (i = 0; i < NUM_NEIGHBORS; ++i) |
| 539 { |
| 540 /* Calculate the weighted sum of neighboring MVs referring |
| 541 * to the dominant frame type. |
| 542 */ |
| 543 const int w = weights_q7[abs(row - neigh_pos[i].row)] |
| 544 [abs(col - neigh_pos[i].col)]; |
| 545 if (neighbors[i].ref_frame != dom_ref_frame) |
| 546 continue; |
| 547 w_sum += w; |
| 548 /* Q7 * Q3 = Q10 */ |
| 549 mv_row_sum += w*neighbors[i].mv.row; |
| 550 mv_col_sum += w*neighbors[i].mv.col; |
| 551 } |
| 552 if (w_sum > 0) |
| 553 { |
| 554 /* Avoid division by zero. |
| 555 * Normalize with the sum of the coefficients |
| 556 * Q3 = Q10 / Q7 |
| 557 */ |
| 558 mv->as_mv.row = mv_row_sum / w_sum; |
| 559 mv->as_mv.col = mv_col_sum / w_sum; |
| 560 |
| 561 mi->mbmi.need_to_clamp_mvs = vp8_check_mv_bounds(mv, |
| 562 mb->mb_to_left_edge, |
| 563 mb->mb_to_right_edge, |
| 564 mb->mb_to_top_edge, |
| 565 mb->mb_to_bottom_edge); |
| 566 } |
| 567 else |
| 568 { |
| 569 mv->as_int = 0; |
| 570 mi->mbmi.need_to_clamp_mvs = 0; |
| 571 } |
| 572 } |
| 573 } |
| 574 } |
| 575 |
| 576 void vp8_interpolate_motion(MACROBLOCKD *mb, |
| 577 int mb_row, int mb_col, |
| 578 int mb_rows, int mb_cols, |
| 579 int mi_stride) |
| 580 { |
| 581 /* Find relevant neighboring blocks */ |
| 582 EC_BLOCK neighbors[NUM_NEIGHBORS]; |
| 583 MV_REFERENCE_FRAME dom_ref_frame; |
| 584 int i; |
| 585 /* Initialize the array. MAX_REF_FRAMES is interpreted as "doesn't exist" */ |
| 586 for (i = 0; i < NUM_NEIGHBORS; ++i) |
| 587 { |
| 588 neighbors[i].ref_frame = MAX_REF_FRAMES; |
| 589 neighbors[i].mv.row = neighbors[i].mv.col = 0; |
| 590 } |
| 591 find_neighboring_blocks(mb->mode_info_context, |
| 592 neighbors, |
| 593 mb_row, mb_col, |
| 594 mb_rows, mb_cols, |
| 595 mb->mode_info_stride); |
| 596 /* Determine the dominant block type */ |
| 597 dom_ref_frame = dominant_ref_frame(neighbors); |
| 598 /* Interpolate MVs for the missing blocks |
| 599 * from the dominating MVs */ |
| 600 interpolate_mvs(mb, neighbors, dom_ref_frame); |
| 601 |
| 602 mb->mode_info_context->mbmi.ref_frame = dom_ref_frame; |
| 603 mb->mode_info_context->mbmi.mode = SPLITMV; |
| 604 mb->mode_info_context->mbmi.uv_mode = DC_PRED; |
| 605 mb->mode_info_context->mbmi.partitioning = 3; |
| 606 mb->mode_info_context->mbmi.segment_id = 0; |
| 607 } |
| 608 |
| 609 void vp8_conceal_corrupt_mb(MACROBLOCKD *xd) |
| 610 { |
| 611 /* This macroblock has corrupt residual, use the motion compensated |
| 612 image (predictor) for concealment */ |
| 613 vp8_recon_copy16x16(xd->predictor, 16, xd->dst.y_buffer, xd->dst.y_stride); |
| 614 vp8_recon_copy8x8(xd->predictor + 256, 8, |
| 615 xd->dst.u_buffer, xd->dst.uv_stride); |
| 616 vp8_recon_copy8x8(xd->predictor + 320, 8, |
| 617 xd->dst.v_buffer, xd->dst.uv_stride); |
| 618 } |
| OLD | NEW |