media/libvpx/vp8/decoder/error_concealment.c

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

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

mercurial