2013-09-11 19:45:44 +02:00
|
|
|
|
2012-08-24 16:44:01 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 The WebM project authors. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
*/
|
|
|
|
|
2012-11-28 19:41:40 +01:00
|
|
|
#include "vp9/common/vp9_mvref_common.h"
|
2012-08-24 16:44:01 +02:00
|
|
|
|
2016-01-12 14:09:06 +01:00
|
|
|
// This function searches the neighborhood of a given MB/SB
|
2013-04-19 12:10:16 +02:00
|
|
|
// to try and find candidate reference vectors.
|
2014-02-19 14:23:51 +01:00
|
|
|
static void find_mv_refs_idx(const VP9_COMMON *cm, const MACROBLOCKD *xd,
|
2014-03-11 00:02:03 +01:00
|
|
|
MODE_INFO *mi, MV_REFERENCE_FRAME ref_frame,
|
2016-07-27 01:52:55 +02:00
|
|
|
int_mv *mv_ref_list, int block, int mi_row,
|
|
|
|
int mi_col, uint8_t *mode_context) {
|
2013-08-23 21:00:14 +02:00
|
|
|
const int *ref_sign_bias = cm->ref_frame_sign_bias;
|
|
|
|
int i, refmv_count = 0;
|
2016-01-20 01:40:20 +01:00
|
|
|
const POSITION *const mv_ref_search = mv_ref_blocks[mi->sb_type];
|
2013-08-04 04:51:56 +02:00
|
|
|
int different_ref_found = 0;
|
|
|
|
int context_counter = 0;
|
2016-07-27 01:52:55 +02:00
|
|
|
const MV_REF *const prev_frame_mvs =
|
|
|
|
cm->use_prev_frame_mvs
|
|
|
|
? cm->prev_frame->mvs + mi_row * cm->mi_cols + mi_col
|
|
|
|
: NULL;
|
2015-06-16 15:38:34 +02:00
|
|
|
const TileInfo *const tile = &xd->tile;
|
2013-08-04 04:51:56 +02:00
|
|
|
|
|
|
|
// Blank the reference vector list
|
2015-04-24 05:47:40 +02:00
|
|
|
memset(mv_ref_list, 0, sizeof(*mv_ref_list) * MAX_MV_REF_CANDIDATES);
|
2013-08-04 04:51:56 +02:00
|
|
|
|
|
|
|
// The nearest 2 blocks are treated differently
|
|
|
|
// if the size < 8x8 we get the mv from the bmi substructure,
|
|
|
|
// and we also need to keep a mode count.
|
2013-08-23 21:00:14 +02:00
|
|
|
for (i = 0; i < 2; ++i) {
|
2013-12-16 21:56:08 +01:00
|
|
|
const POSITION *const mv_ref = &mv_ref_search[i];
|
2013-10-25 17:18:04 +02:00
|
|
|
if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
|
2016-07-27 01:52:55 +02:00
|
|
|
const MODE_INFO *const candidate_mi =
|
|
|
|
xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
|
2013-08-23 21:00:14 +02:00
|
|
|
// Keep counts for entropy encoding.
|
2016-01-20 01:40:20 +01:00
|
|
|
context_counter += mode_2_counter[candidate_mi->mode];
|
2014-03-01 00:12:33 +01:00
|
|
|
different_ref_found = 1;
|
2013-08-23 21:00:14 +02:00
|
|
|
|
2016-01-20 01:40:20 +01:00
|
|
|
if (candidate_mi->ref_frame[0] == ref_frame)
|
2014-10-21 20:04:52 +02:00
|
|
|
ADD_MV_REF_LIST(get_sub_block_mv(candidate_mi, 0, mv_ref->col, block),
|
|
|
|
refmv_count, mv_ref_list, Done);
|
2016-01-20 01:40:20 +01:00
|
|
|
else if (candidate_mi->ref_frame[1] == ref_frame)
|
2014-10-21 20:04:52 +02:00
|
|
|
ADD_MV_REF_LIST(get_sub_block_mv(candidate_mi, 1, mv_ref->col, block),
|
|
|
|
refmv_count, mv_ref_list, Done);
|
2012-08-24 16:44:01 +02:00
|
|
|
}
|
|
|
|
}
|
2013-04-19 12:10:16 +02:00
|
|
|
|
2013-08-04 04:51:56 +02:00
|
|
|
// Check the rest of the neighbors in much the same way
|
|
|
|
// as before except we don't need to keep track of sub blocks or
|
|
|
|
// mode counts.
|
2013-08-23 21:00:14 +02:00
|
|
|
for (; i < MVREF_NEIGHBOURS; ++i) {
|
2013-12-16 21:56:08 +01:00
|
|
|
const POSITION *const mv_ref = &mv_ref_search[i];
|
2013-10-25 17:18:04 +02:00
|
|
|
if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
|
2016-01-20 01:40:20 +01:00
|
|
|
const MODE_INFO *const candidate_mi =
|
|
|
|
xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
|
2014-03-01 00:12:33 +01:00
|
|
|
different_ref_found = 1;
|
2013-08-23 21:00:14 +02:00
|
|
|
|
2016-01-20 01:40:20 +01:00
|
|
|
if (candidate_mi->ref_frame[0] == ref_frame)
|
|
|
|
ADD_MV_REF_LIST(candidate_mi->mv[0], refmv_count, mv_ref_list, Done);
|
|
|
|
else if (candidate_mi->ref_frame[1] == ref_frame)
|
|
|
|
ADD_MV_REF_LIST(candidate_mi->mv[1], refmv_count, mv_ref_list, Done);
|
2013-01-03 16:14:36 +01:00
|
|
|
}
|
|
|
|
}
|
2013-04-19 12:10:16 +02:00
|
|
|
|
2013-08-04 04:51:56 +02:00
|
|
|
// Check the last frame's mode and mv info.
|
2014-10-28 00:19:04 +01:00
|
|
|
if (cm->use_prev_frame_mvs) {
|
|
|
|
if (prev_frame_mvs->ref_frame[0] == ref_frame) {
|
|
|
|
ADD_MV_REF_LIST(prev_frame_mvs->mv[0], refmv_count, mv_ref_list, Done);
|
|
|
|
} else if (prev_frame_mvs->ref_frame[1] == ref_frame) {
|
|
|
|
ADD_MV_REF_LIST(prev_frame_mvs->mv[1], refmv_count, mv_ref_list, Done);
|
|
|
|
}
|
2013-08-04 04:51:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Since we couldn't find 2 mvs from the same reference frame
|
|
|
|
// go back through the neighbors and find motion vectors from
|
|
|
|
// different reference frames.
|
|
|
|
if (different_ref_found) {
|
2013-08-23 21:00:14 +02:00
|
|
|
for (i = 0; i < MVREF_NEIGHBOURS; ++i) {
|
2013-12-16 21:56:08 +01:00
|
|
|
const POSITION *mv_ref = &mv_ref_search[i];
|
2013-10-25 17:18:04 +02:00
|
|
|
if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
|
2016-01-20 01:40:20 +01:00
|
|
|
const MODE_INFO *const candidate_mi =
|
|
|
|
xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
|
2013-08-23 21:00:14 +02:00
|
|
|
|
|
|
|
// If the candidate is INTRA we don't want to consider its mv.
|
2016-01-20 01:40:20 +01:00
|
|
|
IF_DIFF_REF_FRAME_ADD_MV(candidate_mi, ref_frame, ref_sign_bias,
|
2014-10-21 20:04:52 +02:00
|
|
|
refmv_count, mv_ref_list, Done);
|
2013-08-23 21:00:14 +02:00
|
|
|
}
|
2012-08-24 16:44:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-04 04:51:56 +02:00
|
|
|
// Since we still don't have a candidate we'll try the last frame.
|
2014-10-28 00:19:04 +01:00
|
|
|
if (cm->use_prev_frame_mvs) {
|
|
|
|
if (prev_frame_mvs->ref_frame[0] != ref_frame &&
|
|
|
|
prev_frame_mvs->ref_frame[0] > INTRA_FRAME) {
|
|
|
|
int_mv mv = prev_frame_mvs->mv[0];
|
|
|
|
if (ref_sign_bias[prev_frame_mvs->ref_frame[0]] !=
|
|
|
|
ref_sign_bias[ref_frame]) {
|
|
|
|
mv.as_mv.row *= -1;
|
|
|
|
mv.as_mv.col *= -1;
|
|
|
|
}
|
|
|
|
ADD_MV_REF_LIST(mv, refmv_count, mv_ref_list, Done);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prev_frame_mvs->ref_frame[1] > INTRA_FRAME &&
|
|
|
|
prev_frame_mvs->ref_frame[1] != ref_frame &&
|
|
|
|
prev_frame_mvs->mv[1].as_int != prev_frame_mvs->mv[0].as_int) {
|
|
|
|
int_mv mv = prev_frame_mvs->mv[1];
|
|
|
|
if (ref_sign_bias[prev_frame_mvs->ref_frame[1]] !=
|
|
|
|
ref_sign_bias[ref_frame]) {
|
|
|
|
mv.as_mv.row *= -1;
|
|
|
|
mv.as_mv.col *= -1;
|
|
|
|
}
|
|
|
|
ADD_MV_REF_LIST(mv, refmv_count, mv_ref_list, Done);
|
|
|
|
}
|
|
|
|
}
|
2012-11-12 16:09:25 +01:00
|
|
|
|
2016-07-27 01:52:55 +02:00
|
|
|
Done:
|
2013-08-04 04:51:56 +02:00
|
|
|
|
2015-06-02 14:18:21 +02:00
|
|
|
mode_context[ref_frame] = counter_to_context[context_counter];
|
2013-08-04 04:51:56 +02:00
|
|
|
|
2013-04-19 16:40:36 +02:00
|
|
|
// Clamp vectors
|
2013-08-23 21:00:14 +02:00
|
|
|
for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i)
|
|
|
|
clamp_mv_ref(&mv_ref_list[i].as_mv, xd);
|
2012-08-24 16:44:01 +02:00
|
|
|
}
|
2013-12-20 02:39:57 +01:00
|
|
|
|
2014-02-19 14:23:51 +01:00
|
|
|
void vp9_find_mv_refs(const VP9_COMMON *cm, const MACROBLOCKD *xd,
|
2014-10-10 02:57:45 +02:00
|
|
|
MODE_INFO *mi, MV_REFERENCE_FRAME ref_frame,
|
2016-07-27 01:52:55 +02:00
|
|
|
int_mv *mv_ref_list, int mi_row, int mi_col,
|
2015-06-02 14:18:21 +02:00
|
|
|
uint8_t *mode_context) {
|
2016-07-27 01:52:55 +02:00
|
|
|
find_mv_refs_idx(cm, xd, mi, ref_frame, mv_ref_list, -1, mi_row, mi_col,
|
|
|
|
mode_context);
|
2014-02-19 14:23:51 +01:00
|
|
|
}
|
|
|
|
|
2016-07-27 01:52:55 +02:00
|
|
|
void vp9_find_best_ref_mvs(MACROBLOCKD *xd, int allow_hp, int_mv *mvlist,
|
|
|
|
int_mv *nearest_mv, int_mv *near_mv) {
|
2013-12-20 02:39:57 +01:00
|
|
|
int i;
|
|
|
|
// Make sure all the candidates are properly clamped etc
|
|
|
|
for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i) {
|
|
|
|
lower_mv_precision(&mvlist[i].as_mv, allow_hp);
|
|
|
|
clamp_mv2(&mvlist[i].as_mv, xd);
|
|
|
|
}
|
2015-01-31 00:52:24 +01:00
|
|
|
*nearest_mv = mvlist[0];
|
|
|
|
*near_mv = mvlist[1];
|
2013-12-20 02:39:57 +01:00
|
|
|
}
|
|
|
|
|
2016-07-27 01:52:55 +02:00
|
|
|
void vp9_append_sub8x8_mvs_for_idx(VP9_COMMON *cm, MACROBLOCKD *xd, int block,
|
|
|
|
int ref, int mi_row, int mi_col,
|
2015-06-02 14:18:21 +02:00
|
|
|
int_mv *nearest_mv, int_mv *near_mv,
|
|
|
|
uint8_t *mode_context) {
|
2013-12-20 02:39:57 +01:00
|
|
|
int_mv mv_list[MAX_MV_REF_CANDIDATES];
|
2015-04-21 14:36:58 +02:00
|
|
|
MODE_INFO *const mi = xd->mi[0];
|
2013-12-20 02:39:57 +01:00
|
|
|
b_mode_info *bmi = mi->bmi;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
assert(MAX_MV_REF_CANDIDATES == 2);
|
|
|
|
|
2016-07-27 01:52:55 +02:00
|
|
|
find_mv_refs_idx(cm, xd, mi, mi->ref_frame[ref], mv_list, block, mi_row,
|
|
|
|
mi_col, mode_context);
|
2013-12-20 02:39:57 +01:00
|
|
|
|
2015-01-31 00:52:24 +01:00
|
|
|
near_mv->as_int = 0;
|
2013-12-20 02:39:57 +01:00
|
|
|
switch (block) {
|
|
|
|
case 0:
|
2015-01-31 00:52:24 +01:00
|
|
|
nearest_mv->as_int = mv_list[0].as_int;
|
|
|
|
near_mv->as_int = mv_list[1].as_int;
|
2013-12-20 02:39:57 +01:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
case 2:
|
2015-01-31 00:52:24 +01:00
|
|
|
nearest_mv->as_int = bmi[0].as_mv[ref].as_int;
|
2013-12-20 02:39:57 +01:00
|
|
|
for (n = 0; n < MAX_MV_REF_CANDIDATES; ++n)
|
2015-01-31 00:52:24 +01:00
|
|
|
if (nearest_mv->as_int != mv_list[n].as_int) {
|
|
|
|
near_mv->as_int = mv_list[n].as_int;
|
2013-12-20 02:39:57 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3: {
|
|
|
|
int_mv candidates[2 + MAX_MV_REF_CANDIDATES];
|
|
|
|
candidates[0] = bmi[1].as_mv[ref];
|
|
|
|
candidates[1] = bmi[0].as_mv[ref];
|
|
|
|
candidates[2] = mv_list[0];
|
|
|
|
candidates[3] = mv_list[1];
|
|
|
|
|
2015-01-31 00:52:24 +01:00
|
|
|
nearest_mv->as_int = bmi[2].as_mv[ref].as_int;
|
2013-12-20 02:39:57 +01:00
|
|
|
for (n = 0; n < 2 + MAX_MV_REF_CANDIDATES; ++n)
|
2015-01-31 00:52:24 +01:00
|
|
|
if (nearest_mv->as_int != candidates[n].as_int) {
|
|
|
|
near_mv->as_int = candidates[n].as_int;
|
2013-12-20 02:39:57 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-07-27 01:52:55 +02:00
|
|
|
default: assert(0 && "Invalid block index.");
|
2013-12-20 02:39:57 +01:00
|
|
|
}
|
|
|
|
}
|