media/libvpx/vp8/decoder/error_concealment.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libvpx/vp8/decoder/error_concealment.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,598 @@
     1.4 +/*
     1.5 + *  Copyright (c) 2011 The WebM project authors. All Rights Reserved.
     1.6 + *
     1.7 + *  Use of this source code is governed by a BSD-style license
     1.8 + *  that can be found in the LICENSE file in the root of the source
     1.9 + *  tree. An additional intellectual property rights grant can be found
    1.10 + *  in the file PATENTS.  All contributing project authors may
    1.11 + *  be found in the AUTHORS file in the root of the source tree.
    1.12 + */
    1.13 +
    1.14 +#include <assert.h>
    1.15 +
    1.16 +#include "error_concealment.h"
    1.17 +#include "onyxd_int.h"
    1.18 +#include "decodemv.h"
    1.19 +#include "vpx_mem/vpx_mem.h"
    1.20 +#include "vp8/common/findnearmv.h"
    1.21 +
    1.22 +#define MIN(x,y) (((x)<(y))?(x):(y))
    1.23 +#define MAX(x,y) (((x)>(y))?(x):(y))
    1.24 +
    1.25 +#define FLOOR(x,q) ((x) & -(1 << (q)))
    1.26 +
    1.27 +#define NUM_NEIGHBORS 20
    1.28 +
    1.29 +typedef struct ec_position
    1.30 +{
    1.31 +    int row;
    1.32 +    int col;
    1.33 +} EC_POS;
    1.34 +
    1.35 +/*
    1.36 + * Regenerate the table in Matlab with:
    1.37 + * x = meshgrid((1:4), (1:4));
    1.38 + * y = meshgrid((1:4), (1:4))';
    1.39 + * W = round((1./(sqrt(x.^2 + y.^2))*2^7));
    1.40 + * W(1,1) = 0;
    1.41 + */
    1.42 +static const int weights_q7[5][5] = {
    1.43 +       {  0,   128,    64,    43,    32 },
    1.44 +       {128,    91,    57,    40,    31 },
    1.45 +       { 64,    57,    45,    36,    29 },
    1.46 +       { 43,    40,    36,    30,    26 },
    1.47 +       { 32,    31,    29,    26,    23 }
    1.48 +};
    1.49 +
    1.50 +int vp8_alloc_overlap_lists(VP8D_COMP *pbi)
    1.51 +{
    1.52 +    if (pbi->overlaps != NULL)
    1.53 +    {
    1.54 +        vpx_free(pbi->overlaps);
    1.55 +        pbi->overlaps = NULL;
    1.56 +    }
    1.57 +
    1.58 +    pbi->overlaps = vpx_calloc(pbi->common.mb_rows * pbi->common.mb_cols,
    1.59 +                               sizeof(MB_OVERLAP));
    1.60 +
    1.61 +    if (pbi->overlaps == NULL)
    1.62 +        return -1;
    1.63 +
    1.64 +    return 0;
    1.65 +}
    1.66 +
    1.67 +void vp8_de_alloc_overlap_lists(VP8D_COMP *pbi)
    1.68 +{
    1.69 +    vpx_free(pbi->overlaps);
    1.70 +    pbi->overlaps = NULL;
    1.71 +}
    1.72 +
    1.73 +/* Inserts a new overlap area value to the list of overlaps of a block */
    1.74 +static void assign_overlap(OVERLAP_NODE* overlaps,
    1.75 +                           union b_mode_info *bmi,
    1.76 +                           int overlap)
    1.77 +{
    1.78 +    int i;
    1.79 +    if (overlap <= 0)
    1.80 +        return;
    1.81 +    /* Find and assign to the next empty overlap node in the list of overlaps.
    1.82 +     * Empty is defined as bmi == NULL */
    1.83 +    for (i = 0; i < MAX_OVERLAPS; i++)
    1.84 +    {
    1.85 +        if (overlaps[i].bmi == NULL)
    1.86 +        {
    1.87 +            overlaps[i].bmi = bmi;
    1.88 +            overlaps[i].overlap = overlap;
    1.89 +            break;
    1.90 +        }
    1.91 +    }
    1.92 +}
    1.93 +
    1.94 +/* Calculates the overlap area between two 4x4 squares, where the first
    1.95 + * square has its upper-left corner at (b1_row, b1_col) and the second
    1.96 + * square has its upper-left corner at (b2_row, b2_col). Doesn't
    1.97 + * properly handle squares which do not overlap.
    1.98 + */
    1.99 +static int block_overlap(int b1_row, int b1_col, int b2_row, int b2_col)
   1.100 +{
   1.101 +    const int int_top = MAX(b1_row, b2_row); // top
   1.102 +    const int int_left = MAX(b1_col, b2_col); // left
   1.103 +    /* Since each block is 4x4 pixels, adding 4 (Q3) to the left/top edge
   1.104 +     * gives us the right/bottom edge.
   1.105 +     */
   1.106 +    const int int_right = MIN(b1_col + (4<<3), b2_col + (4<<3)); // right
   1.107 +    const int int_bottom = MIN(b1_row + (4<<3), b2_row + (4<<3)); // bottom
   1.108 +    return (int_bottom - int_top) * (int_right - int_left);
   1.109 +}
   1.110 +
   1.111 +/* Calculates the overlap area for all blocks in a macroblock at position
   1.112 + * (mb_row, mb_col) in macroblocks, which are being overlapped by a given
   1.113 + * overlapping block at position (new_row, new_col) (in pixels, Q3). The
   1.114 + * first block being overlapped in the macroblock has position (first_blk_row,
   1.115 + * first_blk_col) in blocks relative the upper-left corner of the image.
   1.116 + */
   1.117 +static void calculate_overlaps_mb(B_OVERLAP *b_overlaps, union b_mode_info *bmi,
   1.118 +                                  int new_row, int new_col,
   1.119 +                                  int mb_row, int mb_col,
   1.120 +                                  int first_blk_row, int first_blk_col)
   1.121 +{
   1.122 +    /* Find the blocks within this MB (defined by mb_row, mb_col) which are
   1.123 +     * overlapped by bmi and calculate and assign overlap for each of those
   1.124 +     * blocks. */
   1.125 +
   1.126 +    /* Block coordinates relative the upper-left block */
   1.127 +    const int rel_ol_blk_row = first_blk_row - mb_row * 4;
   1.128 +    const int rel_ol_blk_col = first_blk_col - mb_col * 4;
   1.129 +    /* If the block partly overlaps any previous MB, these coordinates
   1.130 +     * can be < 0. We don't want to access blocks in previous MBs.
   1.131 +     */
   1.132 +    const int blk_idx = MAX(rel_ol_blk_row,0) * 4 + MAX(rel_ol_blk_col,0);
   1.133 +    /* Upper left overlapping block */
   1.134 +    B_OVERLAP *b_ol_ul = &(b_overlaps[blk_idx]);
   1.135 +
   1.136 +    /* Calculate and assign overlaps for all blocks in this MB
   1.137 +     * which the motion compensated block overlaps
   1.138 +     */
   1.139 +    /* Avoid calculating overlaps for blocks in later MBs */
   1.140 +    int end_row = MIN(4 + mb_row * 4 - first_blk_row, 2);
   1.141 +    int end_col = MIN(4 + mb_col * 4 - first_blk_col, 2);
   1.142 +    int row, col;
   1.143 +
   1.144 +    /* Check if new_row and new_col are evenly divisible by 4 (Q3),
   1.145 +     * and if so we shouldn't check neighboring blocks
   1.146 +     */
   1.147 +    if (new_row >= 0 && (new_row & 0x1F) == 0)
   1.148 +        end_row = 1;
   1.149 +    if (new_col >= 0 && (new_col & 0x1F) == 0)
   1.150 +        end_col = 1;
   1.151 +
   1.152 +    /* Check if the overlapping block partly overlaps a previous MB
   1.153 +     * and if so, we're overlapping fewer blocks in this MB.
   1.154 +     */
   1.155 +    if (new_row < (mb_row*16)<<3)
   1.156 +        end_row = 1;
   1.157 +    if (new_col < (mb_col*16)<<3)
   1.158 +        end_col = 1;
   1.159 +
   1.160 +    for (row = 0; row < end_row; ++row)
   1.161 +    {
   1.162 +        for (col = 0; col < end_col; ++col)
   1.163 +        {
   1.164 +            /* input in Q3, result in Q6 */
   1.165 +            const int overlap = block_overlap(new_row, new_col,
   1.166 +                                                  (((first_blk_row + row) *
   1.167 +                                                      4) << 3),
   1.168 +                                                  (((first_blk_col + col) *
   1.169 +                                                      4) << 3));
   1.170 +            assign_overlap(b_ol_ul[row * 4 + col].overlaps, bmi, overlap);
   1.171 +        }
   1.172 +    }
   1.173 +}
   1.174 +
   1.175 +void vp8_calculate_overlaps(MB_OVERLAP *overlap_ul,
   1.176 +                            int mb_rows, int mb_cols,
   1.177 +                            union b_mode_info *bmi,
   1.178 +                            int b_row, int b_col)
   1.179 +{
   1.180 +    MB_OVERLAP *mb_overlap;
   1.181 +    int row, col, rel_row, rel_col;
   1.182 +    int new_row, new_col;
   1.183 +    int end_row, end_col;
   1.184 +    int overlap_b_row, overlap_b_col;
   1.185 +    int overlap_mb_row, overlap_mb_col;
   1.186 +
   1.187 +    /* mb subpixel position */
   1.188 +    row = (4 * b_row) << 3; /* Q3 */
   1.189 +    col = (4 * b_col) << 3; /* Q3 */
   1.190 +
   1.191 +    /* reverse compensate for motion */
   1.192 +    new_row = row - bmi->mv.as_mv.row;
   1.193 +    new_col = col - bmi->mv.as_mv.col;
   1.194 +
   1.195 +    if (new_row >= ((16*mb_rows) << 3) || new_col >= ((16*mb_cols) << 3))
   1.196 +    {
   1.197 +        /* the new block ended up outside the frame */
   1.198 +        return;
   1.199 +    }
   1.200 +
   1.201 +    if (new_row <= (-4 << 3) || new_col <= (-4 << 3))
   1.202 +    {
   1.203 +        /* outside the frame */
   1.204 +        return;
   1.205 +    }
   1.206 +    /* overlapping block's position in blocks */
   1.207 +    overlap_b_row = FLOOR(new_row / 4, 3) >> 3;
   1.208 +    overlap_b_col = FLOOR(new_col / 4, 3) >> 3;
   1.209 +
   1.210 +    /* overlapping block's MB position in MBs
   1.211 +     * operations are done in Q3
   1.212 +     */
   1.213 +    overlap_mb_row = FLOOR((overlap_b_row << 3) / 4, 3) >> 3;
   1.214 +    overlap_mb_col = FLOOR((overlap_b_col << 3) / 4, 3) >> 3;
   1.215 +
   1.216 +    end_row = MIN(mb_rows - overlap_mb_row, 2);
   1.217 +    end_col = MIN(mb_cols - overlap_mb_col, 2);
   1.218 +
   1.219 +    /* Don't calculate overlap for MBs we don't overlap */
   1.220 +    /* Check if the new block row starts at the last block row of the MB */
   1.221 +    if (abs(new_row - ((16*overlap_mb_row) << 3)) < ((3*4) << 3))
   1.222 +        end_row = 1;
   1.223 +    /* Check if the new block col starts at the last block col of the MB */
   1.224 +    if (abs(new_col - ((16*overlap_mb_col) << 3)) < ((3*4) << 3))
   1.225 +        end_col = 1;
   1.226 +
   1.227 +    /* find the MB(s) this block is overlapping */
   1.228 +    for (rel_row = 0; rel_row < end_row; ++rel_row)
   1.229 +    {
   1.230 +        for (rel_col = 0; rel_col < end_col; ++rel_col)
   1.231 +        {
   1.232 +            if (overlap_mb_row + rel_row < 0 ||
   1.233 +                overlap_mb_col + rel_col < 0)
   1.234 +                continue;
   1.235 +            mb_overlap = overlap_ul + (overlap_mb_row + rel_row) * mb_cols +
   1.236 +                 overlap_mb_col + rel_col;
   1.237 +
   1.238 +            calculate_overlaps_mb(mb_overlap->overlaps, bmi,
   1.239 +                                  new_row, new_col,
   1.240 +                                  overlap_mb_row + rel_row,
   1.241 +                                  overlap_mb_col + rel_col,
   1.242 +                                  overlap_b_row + rel_row,
   1.243 +                                  overlap_b_col + rel_col);
   1.244 +        }
   1.245 +    }
   1.246 +}
   1.247 +
   1.248 +/* Estimates a motion vector given the overlapping blocks' motion vectors.
   1.249 + * Filters out all overlapping blocks which do not refer to the correct
   1.250 + * reference frame type.
   1.251 + */
   1.252 +static void estimate_mv(const OVERLAP_NODE *overlaps, union b_mode_info *bmi)
   1.253 +{
   1.254 +    int i;
   1.255 +    int overlap_sum = 0;
   1.256 +    int row_acc = 0;
   1.257 +    int col_acc = 0;
   1.258 +
   1.259 +    bmi->mv.as_int = 0;
   1.260 +    for (i=0; i < MAX_OVERLAPS; ++i)
   1.261 +    {
   1.262 +        if (overlaps[i].bmi == NULL)
   1.263 +            break;
   1.264 +        col_acc += overlaps[i].overlap * overlaps[i].bmi->mv.as_mv.col;
   1.265 +        row_acc += overlaps[i].overlap * overlaps[i].bmi->mv.as_mv.row;
   1.266 +        overlap_sum += overlaps[i].overlap;
   1.267 +    }
   1.268 +    if (overlap_sum > 0)
   1.269 +    {
   1.270 +        /* Q9 / Q6 = Q3 */
   1.271 +        bmi->mv.as_mv.col = col_acc / overlap_sum;
   1.272 +        bmi->mv.as_mv.row = row_acc / overlap_sum;
   1.273 +    }
   1.274 +    else
   1.275 +    {
   1.276 +        bmi->mv.as_mv.col = 0;
   1.277 +        bmi->mv.as_mv.row = 0;
   1.278 +    }
   1.279 +}
   1.280 +
   1.281 +/* Estimates all motion vectors for a macroblock given the lists of
   1.282 + * overlaps for each block. Decides whether or not the MVs must be clamped.
   1.283 + */
   1.284 +static void estimate_mb_mvs(const B_OVERLAP *block_overlaps,
   1.285 +                            MODE_INFO *mi,
   1.286 +                            int mb_to_left_edge,
   1.287 +                            int mb_to_right_edge,
   1.288 +                            int mb_to_top_edge,
   1.289 +                            int mb_to_bottom_edge)
   1.290 +{
   1.291 +    int row, col;
   1.292 +    int non_zero_count = 0;
   1.293 +    MV * const filtered_mv = &(mi->mbmi.mv.as_mv);
   1.294 +    union b_mode_info * const bmi = mi->bmi;
   1.295 +    filtered_mv->col = 0;
   1.296 +    filtered_mv->row = 0;
   1.297 +    mi->mbmi.need_to_clamp_mvs = 0;
   1.298 +    for (row = 0; row < 4; ++row)
   1.299 +    {
   1.300 +        int this_b_to_top_edge = mb_to_top_edge + ((row*4)<<3);
   1.301 +        int this_b_to_bottom_edge = mb_to_bottom_edge - ((row*4)<<3);
   1.302 +        for (col = 0; col < 4; ++col)
   1.303 +        {
   1.304 +            int i = row * 4 + col;
   1.305 +            int this_b_to_left_edge = mb_to_left_edge + ((col*4)<<3);
   1.306 +            int this_b_to_right_edge = mb_to_right_edge - ((col*4)<<3);
   1.307 +            /* Estimate vectors for all blocks which are overlapped by this */
   1.308 +            /* type. Interpolate/extrapolate the rest of the block's MVs */
   1.309 +            estimate_mv(block_overlaps[i].overlaps, &(bmi[i]));
   1.310 +            mi->mbmi.need_to_clamp_mvs |= vp8_check_mv_bounds(
   1.311 +                                                         &bmi[i].mv,
   1.312 +                                                         this_b_to_left_edge,
   1.313 +                                                         this_b_to_right_edge,
   1.314 +                                                         this_b_to_top_edge,
   1.315 +                                                         this_b_to_bottom_edge);
   1.316 +            if (bmi[i].mv.as_int != 0)
   1.317 +            {
   1.318 +                ++non_zero_count;
   1.319 +                filtered_mv->col += bmi[i].mv.as_mv.col;
   1.320 +                filtered_mv->row += bmi[i].mv.as_mv.row;
   1.321 +            }
   1.322 +        }
   1.323 +    }
   1.324 +    if (non_zero_count > 0)
   1.325 +    {
   1.326 +        filtered_mv->col /= non_zero_count;
   1.327 +        filtered_mv->row /= non_zero_count;
   1.328 +    }
   1.329 +}
   1.330 +
   1.331 +static void calc_prev_mb_overlaps(MB_OVERLAP *overlaps, MODE_INFO *prev_mi,
   1.332 +                                    int mb_row, int mb_col,
   1.333 +                                    int mb_rows, int mb_cols)
   1.334 +{
   1.335 +    int sub_row;
   1.336 +    int sub_col;
   1.337 +    for (sub_row = 0; sub_row < 4; ++sub_row)
   1.338 +    {
   1.339 +        for (sub_col = 0; sub_col < 4; ++sub_col)
   1.340 +        {
   1.341 +            vp8_calculate_overlaps(
   1.342 +                                overlaps, mb_rows, mb_cols,
   1.343 +                                &(prev_mi->bmi[sub_row * 4 + sub_col]),
   1.344 +                                4 * mb_row + sub_row,
   1.345 +                                4 * mb_col + sub_col);
   1.346 +        }
   1.347 +    }
   1.348 +}
   1.349 +
   1.350 +/* Estimate all missing motion vectors. This function does the same as the one
   1.351 + * above, but has different input arguments. */
   1.352 +static void estimate_missing_mvs(MB_OVERLAP *overlaps,
   1.353 +                                 MODE_INFO *mi, MODE_INFO *prev_mi,
   1.354 +                                 int mb_rows, int mb_cols,
   1.355 +                                 unsigned int first_corrupt)
   1.356 +{
   1.357 +    int mb_row, mb_col;
   1.358 +    vpx_memset(overlaps, 0, sizeof(MB_OVERLAP) * mb_rows * mb_cols);
   1.359 +    /* First calculate the overlaps for all blocks */
   1.360 +    for (mb_row = 0; mb_row < mb_rows; ++mb_row)
   1.361 +    {
   1.362 +        for (mb_col = 0; mb_col < mb_cols; ++mb_col)
   1.363 +        {
   1.364 +            /* We're only able to use blocks referring to the last frame
   1.365 +             * when extrapolating new vectors.
   1.366 +             */
   1.367 +            if (prev_mi->mbmi.ref_frame == LAST_FRAME)
   1.368 +            {
   1.369 +                calc_prev_mb_overlaps(overlaps, prev_mi,
   1.370 +                                      mb_row, mb_col,
   1.371 +                                      mb_rows, mb_cols);
   1.372 +            }
   1.373 +            ++prev_mi;
   1.374 +        }
   1.375 +        ++prev_mi;
   1.376 +    }
   1.377 +
   1.378 +    mb_row = first_corrupt / mb_cols;
   1.379 +    mb_col = first_corrupt - mb_row * mb_cols;
   1.380 +    mi += mb_row*(mb_cols + 1) + mb_col;
   1.381 +    /* Go through all macroblocks in the current image with missing MVs
   1.382 +     * and calculate new MVs using the overlaps.
   1.383 +     */
   1.384 +    for (; mb_row < mb_rows; ++mb_row)
   1.385 +    {
   1.386 +        int mb_to_top_edge = -((mb_row * 16)) << 3;
   1.387 +        int mb_to_bottom_edge = ((mb_rows - 1 - mb_row) * 16) << 3;
   1.388 +        for (; mb_col < mb_cols; ++mb_col)
   1.389 +        {
   1.390 +            int mb_to_left_edge = -((mb_col * 16) << 3);
   1.391 +            int mb_to_right_edge = ((mb_cols - 1 - mb_col) * 16) << 3;
   1.392 +            const B_OVERLAP *block_overlaps =
   1.393 +                    overlaps[mb_row*mb_cols + mb_col].overlaps;
   1.394 +            mi->mbmi.ref_frame = LAST_FRAME;
   1.395 +            mi->mbmi.mode = SPLITMV;
   1.396 +            mi->mbmi.uv_mode = DC_PRED;
   1.397 +            mi->mbmi.partitioning = 3;
   1.398 +            mi->mbmi.segment_id = 0;
   1.399 +            estimate_mb_mvs(block_overlaps,
   1.400 +                            mi,
   1.401 +                            mb_to_left_edge,
   1.402 +                            mb_to_right_edge,
   1.403 +                            mb_to_top_edge,
   1.404 +                            mb_to_bottom_edge);
   1.405 +            ++mi;
   1.406 +        }
   1.407 +        mb_col = 0;
   1.408 +        ++mi;
   1.409 +    }
   1.410 +}
   1.411 +
   1.412 +void vp8_estimate_missing_mvs(VP8D_COMP *pbi)
   1.413 +{
   1.414 +    VP8_COMMON * const pc = &pbi->common;
   1.415 +    estimate_missing_mvs(pbi->overlaps,
   1.416 +                         pc->mi, pc->prev_mi,
   1.417 +                         pc->mb_rows, pc->mb_cols,
   1.418 +                         pbi->mvs_corrupt_from_mb);
   1.419 +}
   1.420 +
   1.421 +static void assign_neighbor(EC_BLOCK *neighbor, MODE_INFO *mi, int block_idx)
   1.422 +{
   1.423 +    assert(mi->mbmi.ref_frame < MAX_REF_FRAMES);
   1.424 +    neighbor->ref_frame = mi->mbmi.ref_frame;
   1.425 +    neighbor->mv = mi->bmi[block_idx].mv.as_mv;
   1.426 +}
   1.427 +
   1.428 +/* Finds the neighboring blocks of a macroblocks. In the general case
   1.429 + * 20 blocks are found. If a fewer number of blocks are found due to
   1.430 + * image boundaries, those positions in the EC_BLOCK array are left "empty".
   1.431 + * The neighbors are enumerated with the upper-left neighbor as the first
   1.432 + * element, the second element refers to the neighbor to right of the previous
   1.433 + * neighbor, and so on. The last element refers to the neighbor below the first
   1.434 + * neighbor.
   1.435 + */
   1.436 +static void find_neighboring_blocks(MODE_INFO *mi,
   1.437 +                                    EC_BLOCK *neighbors,
   1.438 +                                    int mb_row, int mb_col,
   1.439 +                                    int mb_rows, int mb_cols,
   1.440 +                                    int mi_stride)
   1.441 +{
   1.442 +    int i = 0;
   1.443 +    int j;
   1.444 +    if (mb_row > 0)
   1.445 +    {
   1.446 +        /* upper left */
   1.447 +        if (mb_col > 0)
   1.448 +            assign_neighbor(&neighbors[i], mi - mi_stride - 1, 15);
   1.449 +        ++i;
   1.450 +        /* above */
   1.451 +        for (j = 12; j < 16; ++j, ++i)
   1.452 +            assign_neighbor(&neighbors[i], mi - mi_stride, j);
   1.453 +    }
   1.454 +    else
   1.455 +        i += 5;
   1.456 +    if (mb_col < mb_cols - 1)
   1.457 +    {
   1.458 +        /* upper right */
   1.459 +        if (mb_row > 0)
   1.460 +            assign_neighbor(&neighbors[i], mi - mi_stride + 1, 12);
   1.461 +        ++i;
   1.462 +        /* right */
   1.463 +        for (j = 0; j <= 12; j += 4, ++i)
   1.464 +            assign_neighbor(&neighbors[i], mi + 1, j);
   1.465 +    }
   1.466 +    else
   1.467 +        i += 5;
   1.468 +    if (mb_row < mb_rows - 1)
   1.469 +    {
   1.470 +        /* lower right */
   1.471 +        if (mb_col < mb_cols - 1)
   1.472 +            assign_neighbor(&neighbors[i], mi + mi_stride + 1, 0);
   1.473 +        ++i;
   1.474 +        /* below */
   1.475 +        for (j = 0; j < 4; ++j, ++i)
   1.476 +            assign_neighbor(&neighbors[i], mi + mi_stride, j);
   1.477 +    }
   1.478 +    else
   1.479 +        i += 5;
   1.480 +    if (mb_col > 0)
   1.481 +    {
   1.482 +        /* lower left */
   1.483 +        if (mb_row < mb_rows - 1)
   1.484 +            assign_neighbor(&neighbors[i], mi + mi_stride - 1, 4);
   1.485 +        ++i;
   1.486 +        /* left */
   1.487 +        for (j = 3; j < 16; j += 4, ++i)
   1.488 +        {
   1.489 +            assign_neighbor(&neighbors[i], mi - 1, j);
   1.490 +        }
   1.491 +    }
   1.492 +    else
   1.493 +        i += 5;
   1.494 +    assert(i == 20);
   1.495 +}
   1.496 +
   1.497 +/* Interpolates all motion vectors for a macroblock from the neighboring blocks'
   1.498 + * motion vectors.
   1.499 + */
   1.500 +static void interpolate_mvs(MACROBLOCKD *mb,
   1.501 +                         EC_BLOCK *neighbors,
   1.502 +                         MV_REFERENCE_FRAME dom_ref_frame)
   1.503 +{
   1.504 +    int row, col, i;
   1.505 +    MODE_INFO * const mi = mb->mode_info_context;
   1.506 +    /* Table with the position of the neighboring blocks relative the position
   1.507 +     * of the upper left block of the current MB. Starting with the upper left
   1.508 +     * neighbor and going to the right.
   1.509 +     */
   1.510 +    const EC_POS neigh_pos[NUM_NEIGHBORS] = {
   1.511 +                                        {-1,-1}, {-1,0}, {-1,1}, {-1,2}, {-1,3},
   1.512 +                                        {-1,4}, {0,4}, {1,4}, {2,4}, {3,4},
   1.513 +                                        {4,4}, {4,3}, {4,2}, {4,1}, {4,0},
   1.514 +                                        {4,-1}, {3,-1}, {2,-1}, {1,-1}, {0,-1}
   1.515 +                                      };
   1.516 +    mi->mbmi.need_to_clamp_mvs = 0;
   1.517 +    for (row = 0; row < 4; ++row)
   1.518 +    {
   1.519 +        int mb_to_top_edge = mb->mb_to_top_edge + ((row*4)<<3);
   1.520 +        int mb_to_bottom_edge = mb->mb_to_bottom_edge - ((row*4)<<3);
   1.521 +        for (col = 0; col < 4; ++col)
   1.522 +        {
   1.523 +            int mb_to_left_edge = mb->mb_to_left_edge + ((col*4)<<3);
   1.524 +            int mb_to_right_edge = mb->mb_to_right_edge - ((col*4)<<3);
   1.525 +            int w_sum = 0;
   1.526 +            int mv_row_sum = 0;
   1.527 +            int mv_col_sum = 0;
   1.528 +            int_mv * const mv = &(mi->bmi[row*4 + col].mv);
   1.529 +            mv->as_int = 0;
   1.530 +            for (i = 0; i < NUM_NEIGHBORS; ++i)
   1.531 +            {
   1.532 +                /* Calculate the weighted sum of neighboring MVs referring
   1.533 +                 * to the dominant frame type.
   1.534 +                 */
   1.535 +                const int w = weights_q7[abs(row - neigh_pos[i].row)]
   1.536 +                                        [abs(col - neigh_pos[i].col)];
   1.537 +                if (neighbors[i].ref_frame != dom_ref_frame)
   1.538 +                    continue;
   1.539 +                w_sum += w;
   1.540 +                /* Q7 * Q3 = Q10 */
   1.541 +                mv_row_sum += w*neighbors[i].mv.row;
   1.542 +                mv_col_sum += w*neighbors[i].mv.col;
   1.543 +            }
   1.544 +            if (w_sum > 0)
   1.545 +            {
   1.546 +                /* Avoid division by zero.
   1.547 +                 * Normalize with the sum of the coefficients
   1.548 +                 * Q3 = Q10 / Q7
   1.549 +                 */
   1.550 +                mv->as_mv.row = mv_row_sum / w_sum;
   1.551 +                mv->as_mv.col = mv_col_sum / w_sum;
   1.552 +                mi->mbmi.need_to_clamp_mvs |= vp8_check_mv_bounds(
   1.553 +                                                            mv,
   1.554 +                                                            mb_to_left_edge,
   1.555 +                                                            mb_to_right_edge,
   1.556 +                                                            mb_to_top_edge,
   1.557 +                                                            mb_to_bottom_edge);
   1.558 +            }
   1.559 +        }
   1.560 +    }
   1.561 +}
   1.562 +
   1.563 +void vp8_interpolate_motion(MACROBLOCKD *mb,
   1.564 +                        int mb_row, int mb_col,
   1.565 +                        int mb_rows, int mb_cols,
   1.566 +                        int mi_stride)
   1.567 +{
   1.568 +    /* Find relevant neighboring blocks */
   1.569 +    EC_BLOCK neighbors[NUM_NEIGHBORS];
   1.570 +    int i;
   1.571 +    /* Initialize the array. MAX_REF_FRAMES is interpreted as "doesn't exist" */
   1.572 +    for (i = 0; i < NUM_NEIGHBORS; ++i)
   1.573 +    {
   1.574 +        neighbors[i].ref_frame = MAX_REF_FRAMES;
   1.575 +        neighbors[i].mv.row = neighbors[i].mv.col = 0;
   1.576 +    }
   1.577 +    find_neighboring_blocks(mb->mode_info_context,
   1.578 +                                neighbors,
   1.579 +                                mb_row, mb_col,
   1.580 +                                mb_rows, mb_cols,
   1.581 +                                mb->mode_info_stride);
   1.582 +    /* Interpolate MVs for the missing blocks from the surrounding
   1.583 +     * blocks which refer to the last frame. */
   1.584 +    interpolate_mvs(mb, neighbors, LAST_FRAME);
   1.585 +
   1.586 +    mb->mode_info_context->mbmi.ref_frame = LAST_FRAME;
   1.587 +    mb->mode_info_context->mbmi.mode = SPLITMV;
   1.588 +    mb->mode_info_context->mbmi.uv_mode = DC_PRED;
   1.589 +    mb->mode_info_context->mbmi.partitioning = 3;
   1.590 +    mb->mode_info_context->mbmi.segment_id = 0;
   1.591 +}
   1.592 +
   1.593 +void vp8_conceal_corrupt_mb(MACROBLOCKD *xd)
   1.594 +{
   1.595 +    /* This macroblock has corrupt residual, use the motion compensated
   1.596 +       image (predictor) for concealment */
   1.597 +
   1.598 +    /* The build predictor functions now output directly into the dst buffer,
   1.599 +     * so the copies are no longer necessary */
   1.600 +
   1.601 +}

mercurial