2 * Copyright (c) 2011 The WebM project authors. All Rights Reserved.
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.
11 #include "error_concealment.h"
12 #include "onyxd_int.h"
14 #include "vpx_mem/vpx_mem.h"
15 #include "vp8/common/recon.h"
16 #include "vp8/common/findnearmv.h"
20 #define MIN(x,y) (((x)<(y))?(x):(y))
21 #define MAX(x,y) (((x)>(y))?(x):(y))
23 #define FLOOR(x,q) ((x) & -(1 << (q)))
25 #define NUM_NEIGHBORS 20
27 typedef struct ec_position
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));
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 }
48 int vp8_alloc_overlap_lists(VP8D_COMP *pbi)
50 if (pbi->overlaps != NULL)
52 vpx_free(pbi->overlaps);
55 pbi->overlaps = vpx_calloc(pbi->common.mb_rows * pbi->common.mb_cols,
57 if (pbi->overlaps == NULL)
59 vpx_memset(pbi->overlaps, 0,
60 sizeof(MB_OVERLAP) * pbi->common.mb_rows * pbi->common.mb_cols);
64 void vp8_de_alloc_overlap_lists(VP8D_COMP *pbi)
66 vpx_free(pbi->overlaps);
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,
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++)
82 if (overlaps[i].bmi == NULL)
84 overlaps[i].bmi = bmi;
85 overlaps[i].overlap = overlap;
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.
96 static int block_overlap(int b1_row, int b1_col, int b2_row, int b2_col)
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.
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);
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.
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)
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
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.
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]);
133 /* Calculate and assign overlaps for all blocks in this MB
134 * which the motion compensated block overlaps
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);
141 /* Check if new_row and new_col are evenly divisible by 4 (Q3),
142 * and if so we shouldn't check neighboring blocks
144 if (new_row >= 0 && (new_row & 0x1F) == 0)
146 if (new_col >= 0 && (new_col & 0x1F) == 0)
149 /* Check if the overlapping block partly overlaps a previous MB
150 * and if so, we're overlapping fewer blocks in this MB.
152 if (new_row < (mb_row*16)<<3)
154 if (new_col < (mb_col*16)<<3)
157 for (row = 0; row < end_row; ++row)
159 for (col = 0; col < end_col; ++col)
161 /* input in Q3, result in Q6 */
162 const int overlap = block_overlap(new_row, new_col,
163 (((first_blk_row + row) *
165 (((first_blk_col + col) *
167 assign_overlap(b_ol_ul[row * 4 + col].overlaps, bmi, overlap);
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)
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;
184 /* mb subpixel position */
185 row = (4 * b_row) << 3; /* Q3 */
186 col = (4 * b_col) << 3; /* Q3 */
188 /* reverse compensate for motion */
189 new_row = row - bmi->mv.as_mv.row;
190 new_col = col - bmi->mv.as_mv.col;
192 if (new_row >= ((16*mb_rows) << 3) || new_col >= ((16*mb_cols) << 3))
194 /* the new block ended up outside the frame */
198 if (new_row <= (-4 << 3) || new_col <= (-4 << 3))
200 /* outside the frame */
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;
207 /* overlapping block's MB position in MBs
208 * operations are done in Q3
210 overlap_mb_row = FLOOR((overlap_b_row << 3) / 4, 3) >> 3;
211 overlap_mb_col = FLOOR((overlap_b_col << 3) / 4, 3) >> 3;
213 end_row = MIN(mb_rows - overlap_mb_row, 2);
214 end_col = MIN(mb_cols - overlap_mb_col, 2);
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))
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))
224 /* find the MB(s) this block is overlapping */
225 for (rel_row = 0; rel_row < end_row; ++rel_row)
227 for (rel_col = 0; rel_col < end_col; ++rel_col)
229 if (overlap_mb_row + rel_row < 0 ||
230 overlap_mb_col + rel_col < 0)
232 mb_overlap = overlap_ul + (overlap_mb_row + rel_row) * mb_cols +
233 overlap_mb_col + rel_col;
235 calculate_overlaps_mb(mb_overlap->overlaps, bmi,
237 overlap_mb_row + rel_row,
238 overlap_mb_col + rel_col,
239 overlap_b_row + rel_row,
240 overlap_b_col + rel_col);
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.
249 static void estimate_mv(const OVERLAP_NODE *overlaps, union b_mode_info *bmi)
257 for (i=0; i < MAX_OVERLAPS; ++i)
259 if (overlaps[i].bmi == NULL)
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;
268 bmi->mv.as_mv.col = col_acc / overlap_sum;
269 bmi->mv.as_mv.row = row_acc / overlap_sum;
273 bmi->mv.as_mv.col = 0;
274 bmi->mv.as_mv.row = 0;
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.
281 static void estimate_mb_mvs(const B_OVERLAP *block_overlaps,
284 int mb_to_right_edge,
286 int mb_to_bottom_edge)
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)
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,
304 if (bmi[i].mv.as_int != 0)
307 filtered_mv->col += bmi[i].mv.as_mv.col;
308 filtered_mv->row += bmi[i].mv.as_mv.row;
311 if (non_zero_count > 0)
313 filtered_mv->col /= non_zero_count;
314 filtered_mv->row /= non_zero_count;
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)
324 for (sub_row = 0; sub_row < 4; ++sub_row)
326 for (sub_col = 0; sub_col < 4; ++sub_col)
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);
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)
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)
349 for (mb_col = 0; mb_col < mb_cols; ++mb_col)
351 /* We're only able to use blocks referring to the last frame
352 * when extrapolating new vectors.
354 if (prev_mi->mbmi.ref_frame == LAST_FRAME)
356 calc_prev_mb_overlaps(overlaps, prev_mi,
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.
371 for (; mb_row < mb_rows; ++mb_row)
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)
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,
399 void vp8_estimate_missing_mvs(VP8D_COMP *pbi)
401 VP8_COMMON * const pc = &pbi->common;
402 estimate_missing_mvs(pbi->overlaps,
404 pc->mb_rows, pc->mb_cols,
405 pbi->mvs_corrupt_from_mb);
408 static void assign_neighbor(EC_BLOCK *neighbor, MODE_INFO *mi, int block_idx)
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;
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
423 static void find_neighboring_blocks(MODE_INFO *mi,
425 int mb_row, int mb_col,
426 int mb_rows, int mb_cols,
435 assign_neighbor(&neighbors[i], mi - mi_stride - 1, 15);
438 for (j = 12; j < 16; ++j, ++i)
439 assign_neighbor(&neighbors[i], mi - mi_stride, j);
443 if (mb_col < mb_cols - 1)
447 assign_neighbor(&neighbors[i], mi - mi_stride + 1, 12);
450 for (j = 0; j <= 12; j += 4, ++i)
451 assign_neighbor(&neighbors[i], mi + 1, j);
455 if (mb_row < mb_rows - 1)
458 if (mb_col < mb_cols - 1)
459 assign_neighbor(&neighbors[i], mi + mi_stride + 1, 0);
462 for (j = 0; j < 4; ++j, ++i)
463 assign_neighbor(&neighbors[i], mi + mi_stride, j);
470 if (mb_row < mb_rows - 1)
471 assign_neighbor(&neighbors[i], mi + mi_stride - 1, 4);
474 for (j = 3; j < 16; j += 4, ++i)
476 assign_neighbor(&neighbors[i], mi - 1, j);
484 /* Calculates which reference frame type is dominating among the neighbors */
485 static MV_REFERENCE_FRAME dominant_ref_frame(EC_BLOCK *neighbors)
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};
492 /* Count neighboring reference frames */
493 for (i = 0; i < NUM_NEIGHBORS; ++i)
495 if (neighbors[i].ref_frame < MAX_REF_FRAMES &&
496 neighbors[i].ref_frame != INTRA_FRAME)
497 ++ref_frame_cnt[neighbors[i].ref_frame];
500 for (i = 0; i < MAX_REF_FRAMES; ++i)
502 if (ref_frame_cnt[i] > max_ref_frame_cnt)
505 max_ref_frame_cnt = ref_frame_cnt[i];
508 return dom_ref_frame;
511 /* Interpolates all motion vectors for a macroblock from the neighboring blocks'
514 static void interpolate_mvs(MACROBLOCKD *mb,
516 MV_REFERENCE_FRAME dom_ref_frame)
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.
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}
530 for (row = 0; row < 4; ++row)
532 for (col = 0; col < 4; ++col)
537 int_mv * const mv = &(mi->bmi[row*4 + col].mv);
538 for (i = 0; i < NUM_NEIGHBORS; ++i)
540 /* Calculate the weighted sum of neighboring MVs referring
541 * to the dominant frame type.
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)
549 mv_row_sum += w*neighbors[i].mv.row;
550 mv_col_sum += w*neighbors[i].mv.col;
554 /* Avoid division by zero.
555 * Normalize with the sum of the coefficients
558 mv->as_mv.row = mv_row_sum / w_sum;
559 mv->as_mv.col = mv_col_sum / w_sum;
561 mi->mbmi.need_to_clamp_mvs = vp8_check_mv_bounds(mv,
563 mb->mb_to_right_edge,
565 mb->mb_to_bottom_edge);
570 mi->bmi[row*4 + col].as_mode = NEW4X4;
571 mi->mbmi.need_to_clamp_mvs = 0;
577 void vp8_interpolate_motion(MACROBLOCKD *mb,
578 int mb_row, int mb_col,
579 int mb_rows, int mb_cols,
582 /* Find relevant neighboring blocks */
583 EC_BLOCK neighbors[NUM_NEIGHBORS];
584 MV_REFERENCE_FRAME dom_ref_frame;
586 /* Initialize the array. MAX_REF_FRAMES is interpreted as "doesn't exist" */
587 for (i = 0; i < NUM_NEIGHBORS; ++i)
589 neighbors[i].ref_frame = MAX_REF_FRAMES;
590 neighbors[i].mv.row = neighbors[i].mv.col = 0;
592 find_neighboring_blocks(mb->mode_info_context,
596 mb->mode_info_stride);
597 /* Determine the dominant block type */
598 dom_ref_frame = dominant_ref_frame(neighbors);
599 /* Interpolate MVs for the missing blocks
600 * from the dominating MVs */
601 interpolate_mvs(mb, neighbors, dom_ref_frame);
603 mb->mode_info_context->mbmi.ref_frame = dom_ref_frame;
604 mb->mode_info_context->mbmi.mode = SPLITMV;
605 mb->mode_info_context->mbmi.uv_mode = DC_PRED;
606 mb->mode_info_context->mbmi.partitioning = 3;
607 mb->mode_info_context->mbmi.segment_id = 0;
610 void vp8_conceal_corrupt_mb(MACROBLOCKD *xd)
612 /* This macroblock has corrupt residual, use the motion compensated
613 image (predictor) for concealment */
614 vp8_recon_copy16x16(xd->predictor, 16, xd->dst.y_buffer, xd->dst.y_stride);
615 vp8_recon_copy8x8(xd->predictor + 256, 8,
616 xd->dst.u_buffer, xd->dst.uv_stride);
617 vp8_recon_copy8x8(xd->predictor + 320, 8,
618 xd->dst.v_buffer, xd->dst.uv_stride);