2014-01-24 10:37:15 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* This file is part of Kvazaar HEVC encoder.
|
2014-02-21 13:00:20 +00:00
|
|
|
*
|
2015-02-23 11:18:48 +00:00
|
|
|
* Copyright (C) 2013-2015 Tampere University of Technology and others (see
|
2014-01-24 10:37:15 +00:00
|
|
|
* COPYING file).
|
|
|
|
*
|
2015-02-23 11:18:48 +00:00
|
|
|
* Kvazaar is free software: you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU Lesser General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2.1 of the License, or (at your
|
|
|
|
* option) any later version.
|
2014-01-24 10:37:15 +00:00
|
|
|
*
|
2015-02-23 11:18:48 +00:00
|
|
|
* Kvazaar is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
|
|
|
|
* more details.
|
2014-01-24 10:37:15 +00:00
|
|
|
*
|
2015-02-23 11:18:48 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with Kvazaar. If not, see <http://www.gnu.org/licenses/>.
|
2014-01-24 10:37:15 +00:00
|
|
|
****************************************************************************/
|
|
|
|
|
2013-09-18 09:16:03 +00:00
|
|
|
#include "inter.h"
|
|
|
|
|
2013-04-24 07:35:27 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2016-08-10 11:03:25 +00:00
|
|
|
#include <limits.h>
|
2013-09-18 09:16:03 +00:00
|
|
|
|
2016-04-01 14:14:23 +00:00
|
|
|
#include "encoder.h"
|
|
|
|
#include "imagelist.h"
|
2015-04-30 15:50:56 +00:00
|
|
|
#include "strategies/generic/picture-generic.h"
|
2016-04-01 14:14:23 +00:00
|
|
|
#include "strategies/strategies-ipol.h"
|
2016-08-09 13:16:33 +00:00
|
|
|
#include "videoframe.h"
|
2016-04-01 14:14:23 +00:00
|
|
|
|
2013-04-24 07:35:27 +00:00
|
|
|
|
2015-11-04 08:32:20 +00:00
|
|
|
static void inter_recon_frac_luma(const encoder_state_t * const state,
|
|
|
|
const kvz_picture * const ref,
|
|
|
|
int32_t xpos,
|
|
|
|
int32_t ypos,
|
|
|
|
int32_t block_width,
|
2015-11-04 08:55:50 +00:00
|
|
|
int32_t block_height,
|
2015-11-04 08:32:20 +00:00
|
|
|
const int16_t mv_param[2],
|
|
|
|
lcu_t *lcu)
|
2015-07-28 17:00:16 +00:00
|
|
|
{
|
|
|
|
int mv_frac_x = (mv_param[0] & 3);
|
|
|
|
int mv_frac_y = (mv_param[1] & 3);
|
|
|
|
|
2015-08-11 12:53:40 +00:00
|
|
|
#define FILTER_SIZE_Y 8 //Luma filter size
|
2015-07-28 17:00:16 +00:00
|
|
|
|
|
|
|
// Fractional luma 1/4-pel
|
2016-06-07 03:03:21 +00:00
|
|
|
kvz_extended_block src = {0, 0, 0, 0};
|
2015-07-28 17:00:16 +00:00
|
|
|
|
|
|
|
// Fractional luma
|
2015-11-04 08:55:50 +00:00
|
|
|
kvz_get_extended_block(xpos,
|
|
|
|
ypos,
|
|
|
|
mv_param[0] >> 2,
|
|
|
|
mv_param[1] >> 2,
|
|
|
|
state->tile->lcu_offset_x * LCU_WIDTH,
|
|
|
|
state->tile->lcu_offset_y * LCU_WIDTH,
|
|
|
|
ref->y,
|
|
|
|
ref->width,
|
|
|
|
ref->height,
|
|
|
|
FILTER_SIZE_Y,
|
|
|
|
block_width,
|
|
|
|
block_height,
|
|
|
|
&src);
|
2016-04-06 16:55:42 +00:00
|
|
|
kvz_sample_quarterpel_luma(state->encoder_control,
|
2015-11-04 08:55:50 +00:00
|
|
|
src.orig_topleft,
|
|
|
|
src.stride,
|
|
|
|
block_width,
|
|
|
|
block_height,
|
|
|
|
lcu->rec.y + (ypos%LCU_WIDTH)*LCU_WIDTH + (xpos%LCU_WIDTH),
|
|
|
|
LCU_WIDTH,
|
|
|
|
mv_frac_x,
|
|
|
|
mv_frac_y,
|
|
|
|
mv_param);
|
2015-08-14 15:39:39 +00:00
|
|
|
|
|
|
|
if (src.malloc_used) free(src.buffer);
|
2015-07-28 17:00:16 +00:00
|
|
|
}
|
|
|
|
|
2015-11-04 08:32:20 +00:00
|
|
|
static void inter_recon_14bit_frac_luma(const encoder_state_t * const state,
|
|
|
|
const kvz_picture * const ref,
|
|
|
|
int32_t xpos,
|
|
|
|
int32_t ypos,
|
|
|
|
int32_t block_width,
|
2015-11-04 08:55:50 +00:00
|
|
|
int32_t block_height,
|
2015-11-04 08:32:20 +00:00
|
|
|
const int16_t mv_param[2],
|
|
|
|
hi_prec_buf_t *hi_prec_out)
|
2015-08-04 13:14:40 +00:00
|
|
|
{
|
|
|
|
int mv_frac_x = (mv_param[0] & 3);
|
|
|
|
int mv_frac_y = (mv_param[1] & 3);
|
|
|
|
|
|
|
|
#define FILTER_SIZE_Y 8 //Luma filter size
|
|
|
|
|
|
|
|
// Fractional luma 1/4-pel
|
2016-06-07 03:03:21 +00:00
|
|
|
kvz_extended_block src = { 0, 0, 0, 0 };
|
2015-08-04 13:14:40 +00:00
|
|
|
|
|
|
|
// Fractional luma
|
2015-11-04 08:55:50 +00:00
|
|
|
kvz_get_extended_block(xpos,
|
|
|
|
ypos,
|
|
|
|
mv_param[0] >> 2,
|
|
|
|
mv_param[1] >> 2,
|
|
|
|
state->tile->lcu_offset_x * LCU_WIDTH,
|
|
|
|
state->tile->lcu_offset_y * LCU_WIDTH,
|
|
|
|
ref->y,
|
|
|
|
ref->width,
|
|
|
|
ref->height,
|
|
|
|
FILTER_SIZE_Y,
|
|
|
|
block_width,
|
|
|
|
block_height,
|
|
|
|
&src);
|
2016-04-06 16:55:42 +00:00
|
|
|
kvz_sample_14bit_quarterpel_luma(state->encoder_control,
|
2015-11-04 08:55:50 +00:00
|
|
|
src.orig_topleft,
|
|
|
|
src.stride,
|
|
|
|
block_width,
|
|
|
|
block_height,
|
|
|
|
hi_prec_out->y + (ypos%LCU_WIDTH)*LCU_WIDTH + (xpos%LCU_WIDTH),
|
|
|
|
LCU_WIDTH,
|
|
|
|
mv_frac_x,
|
|
|
|
mv_frac_y,
|
|
|
|
mv_param);
|
2015-08-14 15:39:39 +00:00
|
|
|
|
|
|
|
if (src.malloc_used) free(src.buffer);
|
2015-08-04 13:14:40 +00:00
|
|
|
}
|
|
|
|
|
2015-11-04 08:32:20 +00:00
|
|
|
static void inter_recon_frac_chroma(const encoder_state_t * const state,
|
|
|
|
const kvz_picture * const ref,
|
|
|
|
int32_t xpos,
|
|
|
|
int32_t ypos,
|
|
|
|
int32_t block_width,
|
|
|
|
int32_t block_height,
|
|
|
|
const int16_t mv_param[2],
|
|
|
|
lcu_t *lcu)
|
2015-07-28 17:00:16 +00:00
|
|
|
{
|
|
|
|
int mv_frac_x = (mv_param[0] & 7);
|
|
|
|
int mv_frac_y = (mv_param[1] & 7);
|
|
|
|
|
|
|
|
// Translate to chroma
|
|
|
|
xpos >>= 1;
|
|
|
|
ypos >>= 1;
|
|
|
|
block_width >>= 1;
|
2015-10-05 09:17:00 +00:00
|
|
|
block_height >>= 1;
|
2015-07-28 17:00:16 +00:00
|
|
|
|
|
|
|
#define FILTER_SIZE_C 4 //Chroma filter size
|
|
|
|
|
|
|
|
// Fractional chroma 1/8-pel
|
2016-06-07 03:03:21 +00:00
|
|
|
kvz_extended_block src_u = { 0, 0, 0, 0 };
|
|
|
|
kvz_extended_block src_v = { 0, 0, 0, 0 };
|
2015-07-28 17:00:16 +00:00
|
|
|
|
|
|
|
//Fractional chroma U
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_get_extended_block(xpos, ypos, (mv_param[0] >> 2) >> 1, (mv_param[1] >> 2) >> 1, state->tile->lcu_offset_x * LCU_WIDTH_C, state->tile->lcu_offset_y * LCU_WIDTH_C,
|
2015-10-05 09:17:00 +00:00
|
|
|
ref->u, ref->width >> 1, ref->height >> 1, FILTER_SIZE_C, block_width, block_height, &src_u);
|
2016-04-06 16:55:42 +00:00
|
|
|
kvz_sample_octpel_chroma(state->encoder_control, src_u.orig_topleft, src_u.stride, block_width,
|
2015-10-05 09:17:00 +00:00
|
|
|
block_height, lcu->rec.u + (ypos % LCU_WIDTH_C)*LCU_WIDTH_C + (xpos % LCU_WIDTH_C), LCU_WIDTH_C, mv_frac_x, mv_frac_y, mv_param);
|
2015-07-28 17:00:16 +00:00
|
|
|
|
|
|
|
//Fractional chroma V
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_get_extended_block(xpos, ypos, (mv_param[0] >> 2) >> 1, (mv_param[1] >> 2) >> 1, state->tile->lcu_offset_x * LCU_WIDTH_C, state->tile->lcu_offset_y * LCU_WIDTH_C,
|
2015-10-05 09:17:00 +00:00
|
|
|
ref->v, ref->width >> 1, ref->height >> 1, FILTER_SIZE_C, block_width, block_height, &src_v);
|
2016-04-06 16:55:42 +00:00
|
|
|
kvz_sample_octpel_chroma(state->encoder_control, src_v.orig_topleft, src_v.stride, block_width,
|
2015-10-05 09:17:00 +00:00
|
|
|
block_height, lcu->rec.v + (ypos % LCU_WIDTH_C)*LCU_WIDTH_C + (xpos % LCU_WIDTH_C), LCU_WIDTH_C, mv_frac_x, mv_frac_y, mv_param);
|
2015-08-14 15:39:39 +00:00
|
|
|
|
|
|
|
if (src_u.malloc_used) free(src_u.buffer);
|
|
|
|
if (src_v.malloc_used) free(src_v.buffer);
|
2015-07-28 17:00:16 +00:00
|
|
|
}
|
|
|
|
|
2015-11-04 08:32:20 +00:00
|
|
|
static void inter_recon_14bit_frac_chroma(const encoder_state_t * const state,
|
|
|
|
const kvz_picture * const ref,
|
|
|
|
int32_t xpos,
|
|
|
|
int32_t ypos,
|
|
|
|
int32_t block_width,
|
2015-11-04 08:55:50 +00:00
|
|
|
int32_t block_height,
|
2015-11-04 08:32:20 +00:00
|
|
|
const int16_t mv_param[2],
|
|
|
|
hi_prec_buf_t *hi_prec_out)
|
2015-08-04 13:14:40 +00:00
|
|
|
{
|
|
|
|
int mv_frac_x = (mv_param[0] & 7);
|
|
|
|
int mv_frac_y = (mv_param[1] & 7);
|
|
|
|
|
|
|
|
// Translate to chroma
|
|
|
|
xpos >>= 1;
|
|
|
|
ypos >>= 1;
|
|
|
|
block_width >>= 1;
|
2015-11-04 08:55:50 +00:00
|
|
|
block_height >>= 1;
|
2015-08-04 13:14:40 +00:00
|
|
|
|
|
|
|
#define FILTER_SIZE_C 4 //Chroma filter size
|
|
|
|
|
|
|
|
// Fractional chroma 1/8-pel
|
2016-06-07 03:03:21 +00:00
|
|
|
kvz_extended_block src_u = { 0, 0, 0, 0 };
|
|
|
|
kvz_extended_block src_v = { 0, 0, 0, 0 };
|
2015-08-04 13:14:40 +00:00
|
|
|
|
|
|
|
//Fractional chroma U
|
2015-11-04 08:55:50 +00:00
|
|
|
kvz_get_extended_block(xpos,
|
|
|
|
ypos,
|
|
|
|
(mv_param[0] >> 2) >> 1,
|
|
|
|
(mv_param[1] >> 2) >> 1,
|
|
|
|
state->tile->lcu_offset_x * LCU_WIDTH_C,
|
|
|
|
state->tile->lcu_offset_y * LCU_WIDTH_C,
|
|
|
|
ref->u,
|
|
|
|
ref->width >> 1,
|
|
|
|
ref->height >> 1,
|
|
|
|
FILTER_SIZE_C,
|
|
|
|
block_width,
|
|
|
|
block_height,
|
|
|
|
&src_u);
|
2016-04-06 16:55:42 +00:00
|
|
|
kvz_sample_14bit_octpel_chroma(state->encoder_control,
|
2015-11-04 08:55:50 +00:00
|
|
|
src_u.orig_topleft,
|
|
|
|
src_u.stride,
|
|
|
|
block_width,
|
|
|
|
block_height,
|
|
|
|
hi_prec_out->u + (ypos % LCU_WIDTH_C)*LCU_WIDTH_C + (xpos % LCU_WIDTH_C),
|
|
|
|
LCU_WIDTH_C,
|
|
|
|
mv_frac_x,
|
|
|
|
mv_frac_y,
|
|
|
|
mv_param);
|
2015-08-04 13:14:40 +00:00
|
|
|
|
|
|
|
//Fractional chroma V
|
2015-11-04 08:55:50 +00:00
|
|
|
kvz_get_extended_block(xpos,
|
|
|
|
ypos,
|
|
|
|
(mv_param[0] >> 2) >> 1,
|
|
|
|
(mv_param[1] >> 2) >> 1,
|
|
|
|
state->tile->lcu_offset_x * LCU_WIDTH_C,
|
|
|
|
state->tile->lcu_offset_y * LCU_WIDTH_C,
|
|
|
|
ref->v,
|
|
|
|
ref->width >> 1,
|
|
|
|
ref->height >> 1,
|
|
|
|
FILTER_SIZE_C,
|
|
|
|
block_width,
|
|
|
|
block_height,
|
|
|
|
&src_v);
|
2016-04-06 16:55:42 +00:00
|
|
|
kvz_sample_14bit_octpel_chroma(state->encoder_control,
|
2015-11-04 08:55:50 +00:00
|
|
|
src_v.orig_topleft,
|
|
|
|
src_v.stride,
|
|
|
|
block_width,
|
|
|
|
block_height,
|
|
|
|
hi_prec_out->v + (ypos % LCU_WIDTH_C)*LCU_WIDTH_C + (xpos % LCU_WIDTH_C),
|
|
|
|
LCU_WIDTH_C,
|
|
|
|
mv_frac_x,
|
|
|
|
mv_frac_y,
|
|
|
|
mv_param);
|
2015-08-14 15:39:39 +00:00
|
|
|
|
|
|
|
if (src_u.malloc_used) free(src_u.buffer);
|
|
|
|
if (src_v.malloc_used) free(src_v.buffer);
|
2015-08-04 13:14:40 +00:00
|
|
|
}
|
|
|
|
|
2016-08-24 14:39:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Copy from frame with extended border.
|
|
|
|
*
|
|
|
|
* \param ref_buf pointer to the start of ref buffer
|
|
|
|
* \param ref_stride stride of ref buffer
|
|
|
|
* \param ref_width width of frame
|
|
|
|
* \param ref_height height of frame
|
|
|
|
* \param rec_buf pointer to the start of pu in rec buffer
|
|
|
|
* \param rec_stride stride of rec buffer
|
|
|
|
* \param width width of copied block
|
|
|
|
* \param height height of copied block
|
|
|
|
* \param mv_in_frame coordinates of copied block in frame coordinates
|
|
|
|
*/
|
|
|
|
static void inter_cp_with_ext_border(const kvz_pixel *ref_buf, int ref_stride,
|
|
|
|
int ref_width, int ref_height,
|
|
|
|
kvz_pixel *rec_buf, int rec_stride,
|
|
|
|
int width, int height,
|
|
|
|
const vector2d_t *mv_in_frame)
|
|
|
|
{
|
|
|
|
for (int y = mv_in_frame->y; y < mv_in_frame->y + height; ++y) {
|
|
|
|
for (int x = mv_in_frame->x; x < mv_in_frame->x + width; ++x) {
|
|
|
|
vector2d_t in_frame = {
|
|
|
|
CLIP(0, ref_width - 1, x),
|
|
|
|
CLIP(0, ref_height - 1, y),
|
|
|
|
};
|
|
|
|
vector2d_t in_pu = {
|
|
|
|
x - mv_in_frame->x,
|
|
|
|
y - mv_in_frame->y,
|
|
|
|
};
|
|
|
|
int pu_index = in_pu.y * rec_stride + in_pu.x;
|
|
|
|
int frame_index = in_frame.y * ref_stride + in_frame.x;
|
|
|
|
rec_buf[pu_index] = ref_buf[frame_index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-19 12:08:30 +00:00
|
|
|
/**
|
|
|
|
* \brief Reconstruct inter block
|
2016-05-23 02:17:35 +00:00
|
|
|
*
|
|
|
|
* \param state encoder state
|
|
|
|
* \param ref picture to copy the data from
|
|
|
|
* \param xpos block x position
|
|
|
|
* \param ypos block y position
|
|
|
|
* \param width block width
|
|
|
|
* \param height block height
|
|
|
|
* \param mv_param motion vector
|
|
|
|
* \param lcu destination lcu
|
|
|
|
* \param hi_prec_out destination of high precision output (null if not needed)
|
2013-09-12 15:50:11 +00:00
|
|
|
*/
|
2015-09-16 07:37:45 +00:00
|
|
|
void kvz_inter_recon_lcu(const encoder_state_t * const state,
|
|
|
|
const kvz_picture * const ref,
|
|
|
|
int32_t xpos,
|
|
|
|
int32_t ypos,
|
|
|
|
int32_t width,
|
|
|
|
int32_t height,
|
|
|
|
const int16_t mv_param[2],
|
|
|
|
lcu_t *lcu,
|
|
|
|
hi_prec_buf_t *hi_prec_out)
|
2013-09-12 15:50:11 +00:00
|
|
|
{
|
2016-08-24 14:39:37 +00:00
|
|
|
const vector2d_t tile_in_frame = {
|
|
|
|
state->tile->lcu_offset_x * LCU_WIDTH,
|
|
|
|
state->tile->lcu_offset_y * LCU_WIDTH
|
|
|
|
};
|
|
|
|
const vector2d_t pu_in_tile = { xpos, ypos };
|
|
|
|
const vector2d_t pu_in_lcu = { xpos % LCU_WIDTH, ypos % LCU_WIDTH };
|
|
|
|
|
|
|
|
const vector2d_t mv_in_pu = { mv_param[0] >> 2, mv_param[1] >> 2 };
|
|
|
|
const vector2d_t mv_in_frame = {
|
|
|
|
mv_in_pu.x + pu_in_tile.x + tile_in_frame.x,
|
|
|
|
mv_in_pu.y + pu_in_tile.y + tile_in_frame.y
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool mv_is_outside_frame = mv_in_frame.x < 0 ||
|
|
|
|
mv_in_frame.y < 0 ||
|
|
|
|
mv_in_frame.x + width > ref->width ||
|
|
|
|
mv_in_frame.y + height > ref->height;
|
|
|
|
|
|
|
|
// With 420, odd coordinates need interpolation.
|
|
|
|
const int8_t fractional_chroma = (mv_in_pu.x & 1) || (mv_in_pu.y & 1);
|
|
|
|
const int8_t fractional_luma = ((mv_param[0] & 3) || (mv_param[1] & 3));
|
|
|
|
|
|
|
|
// Generate prediction for luma.
|
|
|
|
if (fractional_luma) {
|
|
|
|
// With a fractional MV, do interpolation.
|
2017-02-05 09:59:21 +00:00
|
|
|
if (state->encoder_control->cfg.bipred && hi_prec_out) {
|
2016-08-24 14:39:37 +00:00
|
|
|
inter_recon_14bit_frac_luma(state, ref,
|
|
|
|
pu_in_tile.x, pu_in_tile.y,
|
|
|
|
width, height,
|
|
|
|
mv_param, hi_prec_out);
|
2015-08-04 13:14:40 +00:00
|
|
|
} else {
|
2016-08-24 14:39:37 +00:00
|
|
|
inter_recon_frac_luma(state, ref,
|
|
|
|
pu_in_tile.x, pu_in_tile.y,
|
|
|
|
width, height,
|
|
|
|
mv_param, lcu);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// With an integer MV, copy pixels directly from the reference.
|
|
|
|
const int lcu_pu_index = pu_in_lcu.y * LCU_WIDTH + pu_in_lcu.x;
|
|
|
|
if (mv_is_outside_frame) {
|
|
|
|
inter_cp_with_ext_border(ref->y, ref->width,
|
|
|
|
ref->width, ref->height,
|
|
|
|
&lcu->rec.y[lcu_pu_index], LCU_WIDTH,
|
|
|
|
width, height,
|
|
|
|
&mv_in_frame);
|
|
|
|
} else {
|
|
|
|
const int frame_mv_index = mv_in_frame.y * ref->width + mv_in_frame.x;
|
|
|
|
kvz_pixels_blit(&ref->y[frame_mv_index],
|
|
|
|
&lcu->rec.y[lcu_pu_index],
|
|
|
|
width, height,
|
|
|
|
ref->width, LCU_WIDTH);
|
2015-08-04 13:14:40 +00:00
|
|
|
}
|
2015-07-28 17:00:16 +00:00
|
|
|
}
|
2014-05-14 01:42:02 +00:00
|
|
|
|
2016-08-25 13:05:46 +00:00
|
|
|
if (state->encoder_control->chroma_format == KVZ_CSP_400) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-24 14:39:37 +00:00
|
|
|
// Generate prediction for chroma.
|
|
|
|
if (fractional_luma || fractional_chroma) {
|
|
|
|
// With a fractional MV, do interpolation.
|
2017-02-05 09:59:21 +00:00
|
|
|
if (state->encoder_control->cfg.bipred && hi_prec_out) {
|
2016-08-24 14:39:37 +00:00
|
|
|
inter_recon_14bit_frac_chroma(state, ref,
|
|
|
|
pu_in_tile.x, pu_in_tile.y,
|
|
|
|
width, height,
|
|
|
|
mv_param, hi_prec_out);
|
|
|
|
} else {
|
|
|
|
inter_recon_frac_chroma(state, ref,
|
|
|
|
pu_in_tile.x, pu_in_tile.y,
|
|
|
|
width, height,
|
|
|
|
mv_param, lcu);
|
2014-05-14 01:42:02 +00:00
|
|
|
}
|
2016-08-24 14:39:37 +00:00
|
|
|
} else {
|
|
|
|
// With an integer MV, copy pixels directly from the reference.
|
|
|
|
const int lcu_pu_index_c = pu_in_lcu.y / 2 * LCU_WIDTH_C + pu_in_lcu.x / 2;
|
|
|
|
const vector2d_t mv_in_frame_c = { mv_in_frame.x / 2, mv_in_frame.y / 2 };
|
|
|
|
|
|
|
|
if (mv_is_outside_frame) {
|
|
|
|
inter_cp_with_ext_border(ref->u, ref->width / 2,
|
|
|
|
ref->width / 2, ref->height / 2,
|
|
|
|
&lcu->rec.u[lcu_pu_index_c], LCU_WIDTH_C,
|
|
|
|
width / 2, height / 2,
|
|
|
|
&mv_in_frame_c);
|
|
|
|
inter_cp_with_ext_border(ref->v, ref->width / 2,
|
|
|
|
ref->width / 2, ref->height / 2,
|
|
|
|
&lcu->rec.v[lcu_pu_index_c], LCU_WIDTH_C,
|
|
|
|
width / 2, height / 2,
|
|
|
|
&mv_in_frame_c);
|
|
|
|
} else {
|
|
|
|
const int frame_mv_index = mv_in_frame_c.y * ref->width / 2 + mv_in_frame_c.x;
|
|
|
|
|
|
|
|
kvz_pixels_blit(&ref->u[frame_mv_index],
|
|
|
|
&lcu->rec.u[lcu_pu_index_c],
|
|
|
|
width / 2, height / 2,
|
|
|
|
ref->width / 2, LCU_WIDTH_C);
|
|
|
|
kvz_pixels_blit(&ref->v[frame_mv_index],
|
|
|
|
&lcu->rec.v[lcu_pu_index_c],
|
|
|
|
width / 2, height / 2,
|
|
|
|
ref->width / 2, LCU_WIDTH_C);
|
2013-09-12 15:50:11 +00:00
|
|
|
}
|
|
|
|
}
|
2013-09-16 13:37:24 +00:00
|
|
|
}
|
|
|
|
|
2015-04-21 09:05:21 +00:00
|
|
|
/**
|
2016-05-23 02:17:35 +00:00
|
|
|
* \brief Reconstruct bi-pred inter block
|
|
|
|
*
|
|
|
|
* \param state encoder state
|
|
|
|
* \param ref1 reference picture to copy the data from
|
|
|
|
* \param ref2 other reference picture to copy the data from
|
|
|
|
* \param xpos block x position
|
|
|
|
* \param ypos block y position
|
|
|
|
* \param width block width
|
|
|
|
* \param height block height
|
|
|
|
* \param mv_param motion vectors
|
|
|
|
* \param lcu destination lcu
|
|
|
|
*/
|
2015-09-16 07:37:45 +00:00
|
|
|
void kvz_inter_recon_lcu_bipred(const encoder_state_t * const state,
|
|
|
|
const kvz_picture * ref1,
|
|
|
|
const kvz_picture * ref2,
|
|
|
|
int32_t xpos,
|
|
|
|
int32_t ypos,
|
|
|
|
int32_t width,
|
|
|
|
int32_t height,
|
|
|
|
int16_t mv_param[2][2],
|
2015-11-04 08:32:20 +00:00
|
|
|
lcu_t* lcu)
|
|
|
|
{
|
2015-08-04 13:14:40 +00:00
|
|
|
kvz_pixel temp_lcu_y[LCU_WIDTH*LCU_WIDTH];
|
|
|
|
kvz_pixel temp_lcu_u[LCU_WIDTH_C*LCU_WIDTH_C];
|
|
|
|
kvz_pixel temp_lcu_v[LCU_WIDTH_C*LCU_WIDTH_C];
|
2015-04-21 09:05:21 +00:00
|
|
|
int temp_x, temp_y;
|
2015-07-28 17:00:16 +00:00
|
|
|
int shift = 15 - KVZ_BIT_DEPTH;
|
2015-04-30 15:50:56 +00:00
|
|
|
int offset = 1 << (shift - 1);
|
2015-04-23 11:39:41 +00:00
|
|
|
|
2015-08-04 13:14:40 +00:00
|
|
|
const int hi_prec_luma_rec0 = mv_param[0][0] & 3 || mv_param[0][1] & 3;
|
|
|
|
const int hi_prec_luma_rec1 = mv_param[1][0] & 3 || mv_param[1][1] & 3;
|
|
|
|
|
|
|
|
const int hi_prec_chroma_rec0 = mv_param[0][0] & 7 || mv_param[0][1] & 7;
|
|
|
|
const int hi_prec_chroma_rec1 = mv_param[1][0] & 7 || mv_param[1][1] & 7;
|
|
|
|
|
|
|
|
hi_prec_buf_t* high_precision_rec0 = 0;
|
|
|
|
hi_prec_buf_t* high_precision_rec1 = 0;
|
2015-08-26 08:50:27 +00:00
|
|
|
if (hi_prec_chroma_rec0) high_precision_rec0 = kvz_hi_prec_buf_t_alloc(LCU_WIDTH*LCU_WIDTH);
|
|
|
|
if (hi_prec_chroma_rec1) high_precision_rec1 = kvz_hi_prec_buf_t_alloc(LCU_WIDTH*LCU_WIDTH);
|
2015-04-23 11:39:41 +00:00
|
|
|
//Reconstruct both predictors
|
2015-09-16 07:37:45 +00:00
|
|
|
kvz_inter_recon_lcu(state, ref1, xpos, ypos, width, height, mv_param[0], lcu, high_precision_rec0);
|
2015-08-04 13:14:40 +00:00
|
|
|
if (!hi_prec_luma_rec0){
|
|
|
|
memcpy(temp_lcu_y, lcu->rec.y, sizeof(kvz_pixel) * 64 * 64);
|
|
|
|
}
|
|
|
|
if (!hi_prec_chroma_rec0){
|
|
|
|
memcpy(temp_lcu_u, lcu->rec.u, sizeof(kvz_pixel) * 32 * 32);
|
|
|
|
memcpy(temp_lcu_v, lcu->rec.v, sizeof(kvz_pixel) * 32 * 32);
|
|
|
|
}
|
2015-09-16 07:37:45 +00:00
|
|
|
kvz_inter_recon_lcu(state, ref2, xpos, ypos, width, height, mv_param[1], lcu, high_precision_rec1);
|
2015-04-23 11:39:41 +00:00
|
|
|
|
|
|
|
// After reconstruction, merge the predictors by taking an average of each pixel
|
2015-09-16 07:37:45 +00:00
|
|
|
for (temp_y = 0; temp_y < height; ++temp_y) {
|
2015-04-21 09:05:21 +00:00
|
|
|
int y_in_lcu = ((ypos + temp_y) & ((LCU_WIDTH)-1));
|
|
|
|
for (temp_x = 0; temp_x < width; ++temp_x) {
|
|
|
|
int x_in_lcu = ((xpos + temp_x) & ((LCU_WIDTH)-1));
|
2015-08-04 13:14:40 +00:00
|
|
|
int16_t sample0_y = (hi_prec_luma_rec0 ? high_precision_rec0->y[y_in_lcu * LCU_WIDTH + x_in_lcu] : (temp_lcu_y[y_in_lcu * LCU_WIDTH + x_in_lcu] << (14 - KVZ_BIT_DEPTH)));
|
|
|
|
int16_t sample1_y = (hi_prec_luma_rec1 ? high_precision_rec1->y[y_in_lcu * LCU_WIDTH + x_in_lcu] : (lcu->rec.y[y_in_lcu * LCU_WIDTH + x_in_lcu] << (14 - KVZ_BIT_DEPTH)));
|
2015-08-26 08:50:27 +00:00
|
|
|
lcu->rec.y[y_in_lcu * LCU_WIDTH + x_in_lcu] = (kvz_pixel)kvz_fast_clip_32bit_to_pixel((sample0_y + sample1_y + offset) >> shift);
|
2015-04-21 09:05:21 +00:00
|
|
|
}
|
2015-08-04 13:14:40 +00:00
|
|
|
|
2015-04-21 09:05:21 +00:00
|
|
|
}
|
2015-09-16 07:37:45 +00:00
|
|
|
for (temp_y = 0; temp_y < height >> 1; ++temp_y) {
|
2015-04-21 09:05:21 +00:00
|
|
|
int y_in_lcu = (((ypos >> 1) + temp_y) & (LCU_WIDTH_C - 1));
|
2015-08-04 13:14:40 +00:00
|
|
|
for (temp_x = 0; temp_x < width >> 1; ++temp_x) {
|
2015-04-21 09:05:21 +00:00
|
|
|
int x_in_lcu = (((xpos >> 1) + temp_x) & (LCU_WIDTH_C - 1));
|
2015-08-04 13:14:40 +00:00
|
|
|
int16_t sample0_u = (hi_prec_chroma_rec0 ? high_precision_rec0->u[y_in_lcu * LCU_WIDTH_C + x_in_lcu] : (temp_lcu_u[y_in_lcu * LCU_WIDTH_C + x_in_lcu] << (14 - KVZ_BIT_DEPTH)));
|
|
|
|
int16_t sample1_u = (hi_prec_chroma_rec1 ? high_precision_rec1->u[y_in_lcu * LCU_WIDTH_C + x_in_lcu] : (lcu->rec.u[y_in_lcu * LCU_WIDTH_C + x_in_lcu] << (14 - KVZ_BIT_DEPTH)));
|
2015-08-26 08:50:27 +00:00
|
|
|
lcu->rec.u[y_in_lcu * LCU_WIDTH_C + x_in_lcu] = (kvz_pixel)kvz_fast_clip_32bit_to_pixel((sample0_u + sample1_u + offset) >> shift);
|
2015-04-21 09:05:21 +00:00
|
|
|
|
2015-08-04 13:14:40 +00:00
|
|
|
int16_t sample0_v = (hi_prec_chroma_rec0 ? high_precision_rec0->v[y_in_lcu * LCU_WIDTH_C + x_in_lcu] : (temp_lcu_v[y_in_lcu * LCU_WIDTH_C + x_in_lcu] << (14 - KVZ_BIT_DEPTH)));
|
|
|
|
int16_t sample1_v = (hi_prec_chroma_rec1 ? high_precision_rec1->v[y_in_lcu * LCU_WIDTH_C + x_in_lcu] : (lcu->rec.v[y_in_lcu * LCU_WIDTH_C + x_in_lcu] << (14 - KVZ_BIT_DEPTH)));
|
2015-08-26 08:50:27 +00:00
|
|
|
lcu->rec.v[y_in_lcu * LCU_WIDTH_C + x_in_lcu] = (kvz_pixel)kvz_fast_clip_32bit_to_pixel((sample0_v + sample1_v + offset) >> shift);
|
2015-04-21 09:05:21 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-26 08:50:27 +00:00
|
|
|
if (high_precision_rec0 != 0) kvz_hi_prec_buf_t_free(high_precision_rec0);
|
|
|
|
if (high_precision_rec1 != 0) kvz_hi_prec_buf_t_free(high_precision_rec1);
|
2015-04-21 09:05:21 +00:00
|
|
|
}
|
|
|
|
|
2015-04-23 11:39:41 +00:00
|
|
|
/**
|
2016-05-23 02:17:35 +00:00
|
|
|
* \brief Clear unused L0/L1 motion vectors and reference
|
2015-04-23 11:39:41 +00:00
|
|
|
* \param cu coding unit to clear
|
|
|
|
*/
|
2016-05-23 02:17:35 +00:00
|
|
|
static void inter_clear_cu_unused(cu_info_t* cu)
|
|
|
|
{
|
2015-08-31 10:01:24 +00:00
|
|
|
for (unsigned i = 0; i < 2; ++i) {
|
|
|
|
if (cu->inter.mv_dir & (1 << i)) continue;
|
|
|
|
|
|
|
|
cu->inter.mv[i][0] = 0;
|
|
|
|
cu->inter.mv[i][1] = 0;
|
|
|
|
cu->inter.mv_ref[i] = 255;
|
2015-04-23 09:18:33 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-21 09:05:21 +00:00
|
|
|
|
2013-09-19 12:08:30 +00:00
|
|
|
/**
|
2016-05-10 00:47:20 +00:00
|
|
|
* \brief Check whether a0 mv cand block is coded before the current block.
|
|
|
|
* \param x x-coordinate of the current block (in pixels)
|
|
|
|
* \param y y-coordinate of the current block (in pixels)
|
|
|
|
* \param width width of the current block (in pixels)
|
|
|
|
* \param height height of the current block (in pixels)
|
|
|
|
* \return True, if the a0 mv candidate block is coded before the
|
|
|
|
* current block. Otherwise false.
|
|
|
|
*/
|
|
|
|
static bool is_a0_cand_coded(int x, int y, int width, int height)
|
|
|
|
{
|
|
|
|
int size = MIN(width & ~(width - 1), height & ~(height - 1));
|
|
|
|
|
|
|
|
if (height != size) {
|
|
|
|
// For SMP and AMP blocks the situation is equivalent to a square block
|
|
|
|
// at the lower left corner of the PU.
|
|
|
|
y = y + height - size;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (size < LCU_WIDTH) {
|
|
|
|
const int parent_size = 2 * size;
|
|
|
|
const int cu_index = (x % parent_size != 0) + 2 * (y % parent_size != 0);
|
|
|
|
switch (cu_index) {
|
|
|
|
case 0:
|
|
|
|
// A0 is in the CU directly left of the parent CU so it has been
|
|
|
|
// coded already.
|
|
|
|
// +---+---+
|
|
|
|
// | X | |
|
|
|
|
// |---+---+
|
|
|
|
// A0 | | |
|
|
|
|
// +---+---+
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
// A0 is in the CU that will be coded after the current CU.
|
|
|
|
// +---+---+
|
|
|
|
// | | X |
|
|
|
|
// |---+---+
|
|
|
|
// |A0 | |
|
|
|
|
// +---+---+
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
// +---+---+
|
|
|
|
// | | |
|
|
|
|
// |---+---+
|
|
|
|
// | X | |
|
|
|
|
// +---+---+
|
|
|
|
// A0
|
|
|
|
|
|
|
|
// Move to the parent block.
|
|
|
|
y -= size;
|
|
|
|
size = parent_size;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
// A0 is in the CU directly down of the parent CU so is has not
|
|
|
|
// been coded yet.
|
|
|
|
// +---+---+
|
|
|
|
// | | |
|
|
|
|
// |---+---+
|
|
|
|
// | | X |
|
|
|
|
// +---+---+
|
|
|
|
// A0
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// For 64x64 blocks A0 candidate is located outside the LCU.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Check whether b0 mv cand block is coded before the current block.
|
|
|
|
* \param x x-coordinate of the current block (in pixels)
|
|
|
|
* \param y y-coordinate of the current block (in pixels)
|
|
|
|
* \param width width of the current block (in pixels)
|
|
|
|
* \param height height of the current block (in pixels)
|
|
|
|
* \return True, if the b0 mv candidate block is coded before the
|
|
|
|
* current block. Otherwise false.
|
2013-09-19 12:08:30 +00:00
|
|
|
*/
|
2016-05-10 00:47:20 +00:00
|
|
|
static bool is_b0_cand_coded(int x, int y, int width, int height)
|
|
|
|
{
|
|
|
|
int size = MIN(width & ~(width - 1), height & ~(height - 1));
|
|
|
|
|
|
|
|
if (width != size) {
|
|
|
|
// For SMP and AMP blocks the situation is equivalent to a square block
|
|
|
|
// at the upper right corner of the PU.
|
|
|
|
x = x + width - size;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (size < LCU_WIDTH) {
|
|
|
|
const int parent_size = 2 * size;
|
|
|
|
const int cu_index = (x % parent_size != 0) + 2 * (y % parent_size != 0);
|
|
|
|
switch (cu_index) {
|
|
|
|
case 0:
|
|
|
|
// B0 is in the CU directly above the parent CU so it has been
|
|
|
|
// coded already.
|
|
|
|
// B0
|
|
|
|
// +---+---+
|
|
|
|
// | X | |
|
|
|
|
// |---+---+
|
|
|
|
// | | |
|
|
|
|
// +---+---+
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
// B0
|
|
|
|
// +---+---+
|
|
|
|
// | | X |
|
|
|
|
// |---+---+
|
|
|
|
// | | |
|
|
|
|
// +---+---+
|
|
|
|
|
|
|
|
// Move to the parent block.
|
|
|
|
x -= size;
|
|
|
|
size = parent_size;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
// +---+---+
|
|
|
|
// | |B0 |
|
|
|
|
// |---+---+
|
|
|
|
// | X | |
|
|
|
|
// +---+---+
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
// B0 is in the CU directly right of the parent CU so is has not
|
|
|
|
// been coded yet.
|
|
|
|
// +---+---+
|
|
|
|
// | | | B0
|
|
|
|
// |---+---+
|
|
|
|
// | | X |
|
|
|
|
// +---+---+
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The LCU to the right and up of the current LCU has been coded already.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-14 05:49:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Get merge candidates for current block
|
|
|
|
* \param encoder encoder control struct to use
|
|
|
|
* \param x block x position in SCU
|
|
|
|
* \param y block y position in SCU
|
|
|
|
* \param width current block width
|
|
|
|
* \param height current block height
|
|
|
|
* \param H candidate H
|
|
|
|
* \param C1 candidate C1
|
|
|
|
*/
|
2016-08-10 11:03:25 +00:00
|
|
|
static void kvz_inter_get_temporal_merge_candidates(const encoder_state_t * const state,
|
2016-06-14 05:49:35 +00:00
|
|
|
int32_t x,
|
|
|
|
int32_t y,
|
|
|
|
int32_t width,
|
|
|
|
int32_t height,
|
|
|
|
cu_info_t **C3,
|
2016-06-16 08:41:38 +00:00
|
|
|
cu_info_t **H) {
|
2016-06-14 05:49:35 +00:00
|
|
|
/*
|
|
|
|
Predictor block locations
|
|
|
|
_________
|
|
|
|
|CurrentPU|
|
|
|
|
| |C0|__ |
|
|
|
|
| |C3| |
|
|
|
|
|_________|_
|
2016-06-16 08:41:38 +00:00
|
|
|
|H|
|
2016-06-14 05:49:35 +00:00
|
|
|
*/
|
|
|
|
|
2016-06-16 08:41:38 +00:00
|
|
|
*C3 = NULL;
|
|
|
|
*H = NULL;
|
|
|
|
|
2016-06-14 05:49:35 +00:00
|
|
|
// Find temporal reference, closest POC
|
2016-08-10 11:03:25 +00:00
|
|
|
if (state->frame->ref->used_size) {
|
2016-06-14 05:49:35 +00:00
|
|
|
uint32_t poc_diff = UINT_MAX;
|
|
|
|
int32_t closest_ref = 0;
|
|
|
|
|
2016-08-10 11:03:25 +00:00
|
|
|
for (int temporal_cand = 0; temporal_cand < state->frame->ref->used_size; temporal_cand++) {
|
|
|
|
int td = state->frame->poc - state->frame->ref->pocs[temporal_cand];
|
2016-06-14 05:49:35 +00:00
|
|
|
|
2016-09-30 11:59:47 +00:00
|
|
|
td = td < 0 ? UINT_MAX : td;
|
2016-06-14 05:49:35 +00:00
|
|
|
if (td < poc_diff) {
|
|
|
|
closest_ref = temporal_cand;
|
|
|
|
poc_diff = td;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-10 11:03:25 +00:00
|
|
|
cu_array_t *ref_cu_array = state->frame->ref->cu_arrays[closest_ref];
|
2016-06-27 07:11:02 +00:00
|
|
|
int cu_per_width = ref_cu_array->width / SCU_WIDTH;
|
2016-06-14 05:49:35 +00:00
|
|
|
|
|
|
|
uint32_t xColBr = x + width;
|
|
|
|
uint32_t yColBr = y + height;
|
|
|
|
|
|
|
|
// H must be available
|
2016-06-16 08:41:38 +00:00
|
|
|
if (xColBr < state->encoder_control->in.width &&
|
|
|
|
yColBr < state->encoder_control->in.height) {
|
2016-06-14 05:49:35 +00:00
|
|
|
int32_t H_offset = -1;
|
|
|
|
|
2016-06-17 07:44:20 +00:00
|
|
|
// Y inside the current CTU / LCU
|
|
|
|
if (yColBr % LCU_WIDTH != 0) {
|
2016-06-27 07:11:02 +00:00
|
|
|
H_offset = ((xColBr >> 4) << 4) / SCU_WIDTH +
|
|
|
|
(((yColBr >> 4) << 4) / SCU_WIDTH) * cu_per_width;
|
2016-06-14 05:49:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (H_offset >= 0) {
|
|
|
|
// Only use when it's inter block
|
|
|
|
if (ref_cu_array->data[H_offset].type == CU_INTER) {
|
|
|
|
*H = &ref_cu_array->data[H_offset];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uint32_t xColCtr = x + (width / 2);
|
|
|
|
uint32_t yColCtr = y + (height / 2);
|
|
|
|
|
|
|
|
// C3 must be inside the LCU, in the center position of current CU
|
2016-06-16 08:41:38 +00:00
|
|
|
if (xColCtr < state->encoder_control->in.width && yColCtr < state->encoder_control->in.height) {
|
2016-06-27 07:11:02 +00:00
|
|
|
uint32_t C3_offset = ((xColCtr >> 4) << 4) / SCU_WIDTH + ((((yColCtr >> 4) << 4) / SCU_WIDTH) * cu_per_width);
|
2016-06-14 05:49:35 +00:00
|
|
|
if (ref_cu_array->data[C3_offset].type == CU_INTER) {
|
|
|
|
*C3 = &ref_cu_array->data[C3_offset];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 00:47:20 +00:00
|
|
|
/**
|
|
|
|
* \brief Get merge candidates for current block.
|
|
|
|
*
|
|
|
|
* The output parameters b0, b1, b2, a0, a1 are pointed to the
|
|
|
|
* corresponding cu_info_t struct in lcu->cu, or set to NULL, if the
|
|
|
|
* candidate is not available.
|
|
|
|
*
|
|
|
|
* \param x block x position in pixels
|
|
|
|
* \param y block y position in pixels
|
|
|
|
* \param width block width in pixels
|
|
|
|
* \param height block height in pixels
|
|
|
|
* \param picture_width tile width in pixels
|
|
|
|
* \param picture_height tile height in pixels
|
|
|
|
* \param b0 Returns the b0 candidate.
|
|
|
|
* \param b1 Returns the b1 candidate.
|
|
|
|
* \param b2 Returns the b2 candidate.
|
|
|
|
* \param a0 Returns the a0 candidate.
|
|
|
|
* \param a1 Returns the a1 candidate.
|
|
|
|
* \param lcu current LCU
|
|
|
|
*/
|
|
|
|
static void get_spatial_merge_candidates(int32_t x,
|
|
|
|
int32_t y,
|
|
|
|
int32_t width,
|
|
|
|
int32_t height,
|
|
|
|
int32_t picture_width,
|
|
|
|
int32_t picture_height,
|
|
|
|
cu_info_t **b0,
|
|
|
|
cu_info_t **b1,
|
|
|
|
cu_info_t **b2,
|
|
|
|
cu_info_t **a0,
|
|
|
|
cu_info_t **a1,
|
|
|
|
lcu_t *lcu)
|
2013-09-16 13:37:24 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
Predictor block locations
|
2013-09-18 07:15:05 +00:00
|
|
|
____ _______
|
2013-09-16 13:37:24 +00:00
|
|
|
|B2|______|B1|B0|
|
|
|
|
| |
|
|
|
|
| Cur CU |
|
|
|
|
__| |
|
|
|
|
|A1|_________|
|
|
|
|
|A0|
|
2014-02-21 13:00:20 +00:00
|
|
|
*/
|
2016-01-12 11:27:58 +00:00
|
|
|
int32_t x_local = SUB_SCU(x); //!< coordinates from top-left of this LCU
|
|
|
|
int32_t y_local = SUB_SCU(y);
|
2013-09-19 12:08:30 +00:00
|
|
|
// A0 and A1 availability testing
|
2014-03-03 14:08:35 +00:00
|
|
|
if (x != 0) {
|
2016-01-12 11:27:58 +00:00
|
|
|
*a1 = LCU_GET_CU_AT_PX(lcu, x_local - 1, y_local + height - 1);
|
2015-11-02 08:26:02 +00:00
|
|
|
// Do not check (*a1)->coded because the block above is always coded before
|
|
|
|
// the current one and the flag is not set when searching an SMP block.
|
|
|
|
if ((*a1)->type == CU_INTER) {
|
|
|
|
inter_clear_cu_unused(*a1);
|
|
|
|
} else {
|
|
|
|
*a1 = NULL;
|
|
|
|
}
|
2013-09-16 13:37:24 +00:00
|
|
|
|
2016-01-12 11:27:58 +00:00
|
|
|
if (y_local + height < LCU_WIDTH && y + height < picture_height) {
|
|
|
|
*a0 = LCU_GET_CU_AT_PX(lcu, x_local - 1, y_local + height);
|
2016-05-10 00:47:20 +00:00
|
|
|
if ((*a0)->type == CU_INTER && is_a0_cand_coded(x, y, width, height)) {
|
2015-11-02 08:26:02 +00:00
|
|
|
inter_clear_cu_unused(*a0);
|
|
|
|
} else {
|
|
|
|
*a0 = NULL;
|
|
|
|
}
|
2013-09-16 13:37:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-19 12:08:30 +00:00
|
|
|
// B0, B1 and B2 availability testing
|
2014-03-03 14:08:35 +00:00
|
|
|
if (y != 0) {
|
2016-05-10 00:47:20 +00:00
|
|
|
if (x + width < picture_width) {
|
2016-01-12 11:27:58 +00:00
|
|
|
if (x_local + width < LCU_WIDTH) {
|
|
|
|
*b0 = LCU_GET_CU_AT_PX(lcu, x_local + width, y_local - 1);
|
|
|
|
} else if (y_local == 0) {
|
2016-05-10 00:47:20 +00:00
|
|
|
// Special case, top-right CU
|
|
|
|
*b0 = LCU_GET_TOP_RIGHT_CU(lcu);
|
|
|
|
}
|
2013-10-02 12:10:40 +00:00
|
|
|
}
|
2016-05-10 00:47:20 +00:00
|
|
|
if ((*b0) && (*b0)->type == CU_INTER && is_b0_cand_coded(x, y, width, height)) {
|
2015-11-02 08:26:02 +00:00
|
|
|
inter_clear_cu_unused(*b0);
|
|
|
|
} else {
|
|
|
|
*b0 = NULL;
|
|
|
|
}
|
2014-03-06 16:14:01 +00:00
|
|
|
|
2016-01-12 11:27:58 +00:00
|
|
|
*b1 = LCU_GET_CU_AT_PX(lcu, x_local + width - 1, y_local - 1);
|
2015-11-02 08:26:02 +00:00
|
|
|
// Do not check (*b1)->coded because the block to the left is always coded
|
|
|
|
// before the current one and the flag is not set when searching an SMP
|
|
|
|
// block.
|
|
|
|
if ((*b1)->type == CU_INTER) {
|
|
|
|
inter_clear_cu_unused(*b1);
|
|
|
|
} else {
|
|
|
|
*b1 = NULL;
|
|
|
|
}
|
2013-09-16 13:37:24 +00:00
|
|
|
|
2014-03-04 13:32:15 +00:00
|
|
|
if (x != 0) {
|
2016-01-12 11:27:58 +00:00
|
|
|
*b2 = LCU_GET_CU_AT_PX(lcu, x_local - 1, y_local - 1);
|
2015-11-02 08:26:02 +00:00
|
|
|
// Do not check (*b2)->coded because the block above and to the left is
|
|
|
|
// always coded before the current one.
|
|
|
|
if ((*b2)->type == CU_INTER) {
|
|
|
|
inter_clear_cu_unused(*b2);
|
|
|
|
} else {
|
|
|
|
*b2 = NULL;
|
|
|
|
}
|
2013-09-16 13:37:24 +00:00
|
|
|
}
|
|
|
|
}
|
2013-10-11 08:59:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-17 01:25:45 +00:00
|
|
|
* \brief Get merge candidates for current block.
|
|
|
|
*
|
|
|
|
* The output parameters b0, b1, b2, a0, a1 are pointed to the
|
|
|
|
* corresponding cu_info_t struct in lcu->cu, or set to NULL, if the
|
|
|
|
* candidate is not available.
|
|
|
|
*
|
2015-12-16 12:53:18 +00:00
|
|
|
* \param cua cu information
|
2016-05-17 01:25:45 +00:00
|
|
|
* \param x block x position in pixels
|
|
|
|
* \param y block y position in pixels
|
|
|
|
* \param width block width in pixels
|
|
|
|
* \param height block height in pixels
|
|
|
|
* \param picture_width tile width in pixels
|
|
|
|
* \param picture_height tile height in pixels
|
|
|
|
* \param b0 Returns the b0 candidate.
|
|
|
|
* \param b1 Returns the b1 candidate.
|
|
|
|
* \param b2 Returns the b2 candidate.
|
|
|
|
* \param a0 Returns the a0 candidate.
|
|
|
|
* \param a1 Returns the a1 candidate.
|
2013-10-11 08:59:10 +00:00
|
|
|
*/
|
2015-12-16 12:53:18 +00:00
|
|
|
static void get_spatial_merge_candidates_cua(const cu_array_t *cua,
|
|
|
|
int32_t x,
|
2016-05-17 01:25:45 +00:00
|
|
|
int32_t y,
|
|
|
|
int32_t width,
|
|
|
|
int32_t height,
|
|
|
|
int32_t picture_width,
|
|
|
|
int32_t picture_height,
|
|
|
|
const cu_info_t **b0,
|
|
|
|
const cu_info_t **b1,
|
|
|
|
const cu_info_t **b2,
|
|
|
|
const cu_info_t **a0,
|
2015-12-16 12:53:18 +00:00
|
|
|
const cu_info_t **a1)
|
2016-05-17 01:25:45 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
Predictor block locations
|
|
|
|
____ _______
|
|
|
|
|B2|______|B1|B0|
|
|
|
|
| |
|
|
|
|
| Cur CU |
|
|
|
|
__| |
|
|
|
|
|A1|_________|
|
|
|
|
|A0|
|
|
|
|
*/
|
2015-12-16 12:53:18 +00:00
|
|
|
int32_t x_local = SUB_SCU(x); //!< coordinates from top-left of this LCU
|
|
|
|
int32_t y_local = SUB_SCU(y);
|
2016-05-17 01:25:45 +00:00
|
|
|
// A0 and A1 availability testing
|
|
|
|
if (x != 0) {
|
2016-01-12 07:21:00 +00:00
|
|
|
*a1 = kvz_cu_array_at_const(cua, x - 1, y + height - 1);
|
2015-12-16 12:53:18 +00:00
|
|
|
// The block above is always coded before the current one.
|
2016-05-17 01:25:45 +00:00
|
|
|
if ((*a1)->type != CU_INTER) {
|
|
|
|
*a1 = NULL;
|
|
|
|
}
|
|
|
|
|
2015-12-16 12:53:18 +00:00
|
|
|
if (y_local + height < LCU_WIDTH && y + height < picture_height) {
|
2016-01-12 07:21:00 +00:00
|
|
|
*a0 = kvz_cu_array_at_const(cua, x - 1, y + height);
|
2016-05-17 01:25:45 +00:00
|
|
|
if ((*a0)->type != CU_INTER || !is_a0_cand_coded(x, y, width, height)) {
|
|
|
|
*a0 = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// B0, B1 and B2 availability testing
|
|
|
|
if (y != 0) {
|
2015-12-16 12:53:18 +00:00
|
|
|
if (x + width < picture_width && (x_local + width < LCU_WIDTH || y_local == 0)) {
|
2016-01-12 07:21:00 +00:00
|
|
|
*b0 = kvz_cu_array_at_const(cua, x + width, y - 1);
|
2016-05-17 01:25:45 +00:00
|
|
|
if ((*b0)->type != CU_INTER || !is_b0_cand_coded(x, y, width, height)) {
|
|
|
|
*b0 = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-12 07:21:00 +00:00
|
|
|
*b1 = kvz_cu_array_at_const(cua, x + width - 1, y - 1);
|
2015-12-16 12:53:18 +00:00
|
|
|
// The block to the left is always coded before the current one.
|
2016-05-17 01:25:45 +00:00
|
|
|
if ((*b1)->type != CU_INTER) {
|
|
|
|
*b1 = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x != 0) {
|
2016-01-12 07:21:00 +00:00
|
|
|
*b2 = kvz_cu_array_at_const(cua, x - 1, y - 1);
|
2015-12-16 12:53:18 +00:00
|
|
|
// The block above and to the left is always coded before the current
|
|
|
|
// one.
|
2016-05-17 01:25:45 +00:00
|
|
|
if ((*b2)->type != CU_INTER) {
|
|
|
|
*b2 = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Pick two mv candidates from the spatial candidates.
|
|
|
|
*/
|
|
|
|
static void get_mv_cand_from_spatial(const encoder_state_t * const state,
|
2016-06-16 08:41:38 +00:00
|
|
|
int32_t x,
|
|
|
|
int32_t y,
|
|
|
|
int32_t width,
|
|
|
|
int32_t height,
|
2016-05-17 01:25:45 +00:00
|
|
|
const cu_info_t *b0,
|
|
|
|
const cu_info_t *b1,
|
|
|
|
const cu_info_t *b2,
|
|
|
|
const cu_info_t *a0,
|
|
|
|
const cu_info_t *a1,
|
2016-06-16 08:41:38 +00:00
|
|
|
const cu_info_t *c3,
|
|
|
|
const cu_info_t *h,
|
2016-05-17 01:25:45 +00:00
|
|
|
const cu_info_t *cur_cu,
|
|
|
|
int8_t reflist,
|
|
|
|
int16_t mv_cand[2][2])
|
2014-02-21 13:00:20 +00:00
|
|
|
{
|
2013-10-11 08:59:10 +00:00
|
|
|
uint8_t candidates = 0;
|
2014-02-14 13:50:30 +00:00
|
|
|
uint8_t b_candidates = 0;
|
2015-04-02 11:05:24 +00:00
|
|
|
int8_t reflist2nd = !reflist;
|
2013-10-11 08:59:10 +00:00
|
|
|
|
2014-02-14 15:13:18 +00:00
|
|
|
#define CALCULATE_SCALE(cu,tb,td) ((tb * ((0x4000 + (abs(td)>>1))/td) + 32) >> 6)
|
2016-08-10 00:46:23 +00:00
|
|
|
#define APPLY_MV_SCALING(cu, cand, list) {int td = state->frame->poc - state->frame->ref->pocs[(cu)->inter.mv_ref[list]];\
|
|
|
|
int tb = state->frame->poc - state->frame->ref->pocs[cur_cu->inter.mv_ref[reflist]];\
|
2014-02-14 15:13:18 +00:00
|
|
|
if (td != tb) { \
|
|
|
|
int scale = CALCULATE_SCALE(cu,tb,td); \
|
2015-03-30 11:40:29 +00:00
|
|
|
mv_cand[cand][0] = ((scale * (cu)->inter.mv[list][0] + 127 + (scale * (cu)->inter.mv[list][0] < 0)) >> 8 ); \
|
|
|
|
mv_cand[cand][1] = ((scale * (cu)->inter.mv[list][1] + 127 + (scale * (cu)->inter.mv[list][1] < 0)) >> 8 ); }}
|
2014-02-14 13:50:30 +00:00
|
|
|
|
2013-09-19 12:08:30 +00:00
|
|
|
// Left predictors
|
2015-11-02 08:26:02 +00:00
|
|
|
if (a0 && (
|
2015-04-02 11:05:24 +00:00
|
|
|
((a0->inter.mv_dir & 1) && a0->inter.mv_ref[0] == cur_cu->inter.mv_ref[reflist]) ||
|
|
|
|
((a0->inter.mv_dir & 2) && a0->inter.mv_ref[1] == cur_cu->inter.mv_ref[reflist]))) {
|
|
|
|
if (a0->inter.mv_dir & (1 << reflist) && a0->inter.mv_ref[reflist] == cur_cu->inter.mv_ref[reflist]) {
|
|
|
|
mv_cand[candidates][0] = a0->inter.mv[reflist][0];
|
|
|
|
mv_cand[candidates][1] = a0->inter.mv[reflist][1];
|
2015-03-30 11:40:29 +00:00
|
|
|
} else {
|
2015-04-02 11:05:24 +00:00
|
|
|
mv_cand[candidates][0] = a0->inter.mv[reflist2nd][0];
|
|
|
|
mv_cand[candidates][1] = a0->inter.mv[reflist2nd][1];
|
2015-03-30 11:40:29 +00:00
|
|
|
}
|
2013-09-16 13:37:24 +00:00
|
|
|
candidates++;
|
2015-11-02 08:26:02 +00:00
|
|
|
} else if (a1 && (
|
2015-04-02 11:05:24 +00:00
|
|
|
((a1->inter.mv_dir & 1) && a1->inter.mv_ref[0] == cur_cu->inter.mv_ref[reflist]) ||
|
|
|
|
((a1->inter.mv_dir & 2) && a1->inter.mv_ref[1] == cur_cu->inter.mv_ref[reflist]))) {
|
|
|
|
if (a1->inter.mv_dir & (1 << reflist) && a1->inter.mv_ref[reflist] == cur_cu->inter.mv_ref[reflist]) {
|
|
|
|
mv_cand[candidates][0] = a1->inter.mv[reflist][0];
|
|
|
|
mv_cand[candidates][1] = a1->inter.mv[reflist][1];
|
2015-03-30 11:40:29 +00:00
|
|
|
} else {
|
2015-04-02 11:05:24 +00:00
|
|
|
mv_cand[candidates][0] = a1->inter.mv[reflist2nd][0];
|
|
|
|
mv_cand[candidates][1] = a1->inter.mv[reflist2nd][1];
|
2015-03-30 11:40:29 +00:00
|
|
|
}
|
2013-09-16 13:37:24 +00:00
|
|
|
candidates++;
|
|
|
|
}
|
|
|
|
|
2014-02-14 13:50:30 +00:00
|
|
|
if(!candidates) {
|
|
|
|
// Left predictors
|
2015-11-02 08:26:02 +00:00
|
|
|
if (a0) {
|
2015-04-02 11:05:24 +00:00
|
|
|
if (a0->inter.mv_dir & (1 << reflist)) {
|
|
|
|
mv_cand[candidates][0] = a0->inter.mv[reflist][0];
|
|
|
|
mv_cand[candidates][1] = a0->inter.mv[reflist][1];
|
|
|
|
APPLY_MV_SCALING(a0, candidates, reflist);
|
2015-03-30 11:40:29 +00:00
|
|
|
} else {
|
2015-04-02 11:05:24 +00:00
|
|
|
mv_cand[candidates][0] = a0->inter.mv[reflist2nd][0];
|
|
|
|
mv_cand[candidates][1] = a0->inter.mv[reflist2nd][1];
|
|
|
|
APPLY_MV_SCALING(a0, candidates, reflist2nd);
|
2015-03-30 11:40:29 +00:00
|
|
|
}
|
2014-02-14 13:50:30 +00:00
|
|
|
candidates++;
|
2015-11-02 08:26:02 +00:00
|
|
|
} else if (a1) {
|
2015-04-02 11:05:24 +00:00
|
|
|
if (a1->inter.mv_dir & (1 << reflist)) {
|
|
|
|
mv_cand[candidates][0] = a1->inter.mv[reflist][0];
|
|
|
|
mv_cand[candidates][1] = a1->inter.mv[reflist][1];
|
|
|
|
APPLY_MV_SCALING(a1, candidates, reflist);
|
2015-03-30 11:40:29 +00:00
|
|
|
} else {
|
2015-04-02 11:05:24 +00:00
|
|
|
mv_cand[candidates][0] = a1->inter.mv[reflist2nd][0];
|
|
|
|
mv_cand[candidates][1] = a1->inter.mv[reflist2nd][1];
|
|
|
|
APPLY_MV_SCALING(a1, candidates, reflist2nd);
|
2015-03-30 11:40:29 +00:00
|
|
|
}
|
2014-02-14 13:50:30 +00:00
|
|
|
candidates++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-19 12:08:30 +00:00
|
|
|
// Top predictors
|
2015-11-02 08:26:02 +00:00
|
|
|
if (b0 && (
|
2015-04-02 11:05:24 +00:00
|
|
|
((b0->inter.mv_dir & 1) && b0->inter.mv_ref[0] == cur_cu->inter.mv_ref[reflist]) ||
|
|
|
|
((b0->inter.mv_dir & 2) && b0->inter.mv_ref[1] == cur_cu->inter.mv_ref[reflist]))) {
|
|
|
|
if (b0->inter.mv_dir & (1 << reflist) && b0->inter.mv_ref[reflist] == cur_cu->inter.mv_ref[reflist]) {
|
|
|
|
mv_cand[candidates][0] = b0->inter.mv[reflist][0];
|
|
|
|
mv_cand[candidates][1] = b0->inter.mv[reflist][1];
|
2015-03-30 11:40:29 +00:00
|
|
|
} else {
|
2015-04-02 11:05:24 +00:00
|
|
|
mv_cand[candidates][0] = b0->inter.mv[reflist2nd][0];
|
|
|
|
mv_cand[candidates][1] = b0->inter.mv[reflist2nd][1];
|
2015-03-30 11:40:29 +00:00
|
|
|
}
|
2014-02-14 13:50:30 +00:00
|
|
|
b_candidates++;
|
2015-11-02 08:26:02 +00:00
|
|
|
} else if (b1 && (
|
2015-04-02 11:05:24 +00:00
|
|
|
((b1->inter.mv_dir & 1) && b1->inter.mv_ref[0] == cur_cu->inter.mv_ref[reflist]) ||
|
|
|
|
((b1->inter.mv_dir & 2) && b1->inter.mv_ref[1] == cur_cu->inter.mv_ref[reflist]))) {
|
|
|
|
if (b1->inter.mv_dir & (1 << reflist) && b1->inter.mv_ref[reflist] == cur_cu->inter.mv_ref[reflist]) {
|
|
|
|
mv_cand[candidates][0] = b1->inter.mv[reflist][0];
|
|
|
|
mv_cand[candidates][1] = b1->inter.mv[reflist][1];
|
2015-03-30 11:40:29 +00:00
|
|
|
} else {
|
2015-04-02 11:05:24 +00:00
|
|
|
mv_cand[candidates][0] = b1->inter.mv[reflist2nd][0];
|
|
|
|
mv_cand[candidates][1] = b1->inter.mv[reflist2nd][1];
|
2015-03-30 11:40:29 +00:00
|
|
|
}
|
2014-02-21 13:00:20 +00:00
|
|
|
b_candidates++;
|
2015-11-02 08:26:02 +00:00
|
|
|
} else if (b2 && (
|
2015-04-02 11:05:24 +00:00
|
|
|
((b2->inter.mv_dir & 1) && b2->inter.mv_ref[0] == cur_cu->inter.mv_ref[reflist]) ||
|
|
|
|
((b2->inter.mv_dir & 2) && b2->inter.mv_ref[1] == cur_cu->inter.mv_ref[reflist]))) {
|
|
|
|
if (b2->inter.mv_dir & (1 << reflist) && b2->inter.mv_ref[reflist] == cur_cu->inter.mv_ref[reflist]) {
|
|
|
|
mv_cand[candidates][0] = b2->inter.mv[reflist][0];
|
|
|
|
mv_cand[candidates][1] = b2->inter.mv[reflist][1];
|
2015-03-30 11:40:29 +00:00
|
|
|
} else {
|
2015-04-02 11:05:24 +00:00
|
|
|
mv_cand[candidates][0] = b2->inter.mv[reflist2nd][0];
|
|
|
|
mv_cand[candidates][1] = b2->inter.mv[reflist2nd][1];
|
2015-03-30 11:40:29 +00:00
|
|
|
}
|
2014-02-17 09:13:12 +00:00
|
|
|
b_candidates++;
|
2014-02-14 13:50:30 +00:00
|
|
|
}
|
|
|
|
candidates += b_candidates;
|
|
|
|
|
2014-02-17 15:39:40 +00:00
|
|
|
// When a1 or a0 is available, we dont check for secondary B candidates
|
2015-11-02 08:26:02 +00:00
|
|
|
if (a1 || a0) {
|
2014-02-18 15:27:45 +00:00
|
|
|
b_candidates = 1;
|
|
|
|
} else if(candidates != 2) {
|
|
|
|
b_candidates = 0;
|
|
|
|
}
|
2014-02-17 15:39:40 +00:00
|
|
|
|
2014-02-14 13:50:30 +00:00
|
|
|
if(!b_candidates) {
|
|
|
|
// Top predictors
|
2015-11-02 08:26:02 +00:00
|
|
|
if (b0) {
|
2015-04-02 11:05:24 +00:00
|
|
|
if (b0->inter.mv_dir & (1 << reflist)) {
|
|
|
|
mv_cand[candidates][0] = b0->inter.mv[reflist][0];
|
|
|
|
mv_cand[candidates][1] = b0->inter.mv[reflist][1];
|
|
|
|
APPLY_MV_SCALING(b0, candidates, reflist);
|
2015-03-30 11:40:29 +00:00
|
|
|
} else {
|
2015-04-02 11:05:24 +00:00
|
|
|
mv_cand[candidates][0] = b0->inter.mv[reflist2nd][0];
|
|
|
|
mv_cand[candidates][1] = b0->inter.mv[reflist2nd][1];
|
|
|
|
APPLY_MV_SCALING(b0, candidates, reflist2nd);
|
2015-03-30 11:40:29 +00:00
|
|
|
}
|
2014-02-14 13:50:30 +00:00
|
|
|
candidates++;
|
2015-11-02 08:26:02 +00:00
|
|
|
} else if (b1) {
|
2015-04-02 11:05:24 +00:00
|
|
|
if (b1->inter.mv_dir & (1 << reflist)) {
|
|
|
|
mv_cand[candidates][0] = b1->inter.mv[reflist][0];
|
|
|
|
mv_cand[candidates][1] = b1->inter.mv[reflist][1];
|
|
|
|
APPLY_MV_SCALING(b1, candidates, reflist);
|
2015-03-30 11:40:29 +00:00
|
|
|
} else {
|
2015-04-02 11:05:24 +00:00
|
|
|
mv_cand[candidates][0] = b1->inter.mv[reflist2nd][0];
|
|
|
|
mv_cand[candidates][1] = b1->inter.mv[reflist2nd][1];
|
|
|
|
APPLY_MV_SCALING(b1, candidates, reflist2nd);
|
2015-03-30 11:40:29 +00:00
|
|
|
}
|
2014-02-14 13:50:30 +00:00
|
|
|
candidates++;
|
2015-11-02 08:26:02 +00:00
|
|
|
} else if (b2) {
|
2015-04-02 11:05:24 +00:00
|
|
|
if (b2->inter.mv_dir & (1 << reflist)) {
|
|
|
|
mv_cand[candidates][0] = b2->inter.mv[reflist][0];
|
|
|
|
mv_cand[candidates][1] = b2->inter.mv[reflist][1];
|
|
|
|
APPLY_MV_SCALING(b2, candidates, reflist);
|
2015-03-30 11:40:29 +00:00
|
|
|
} else {
|
2015-04-02 11:05:24 +00:00
|
|
|
mv_cand[candidates][0] = b2->inter.mv[reflist2nd][0];
|
|
|
|
mv_cand[candidates][1] = b2->inter.mv[reflist2nd][1];
|
|
|
|
APPLY_MV_SCALING(b2, candidates, reflist2nd);
|
2015-03-30 11:40:29 +00:00
|
|
|
}
|
2014-02-17 09:13:12 +00:00
|
|
|
candidates++;
|
2014-02-14 13:50:30 +00:00
|
|
|
}
|
2013-09-16 13:37:24 +00:00
|
|
|
}
|
|
|
|
|
2013-09-19 12:08:30 +00:00
|
|
|
// Remove identical candidate
|
2013-09-18 07:15:05 +00:00
|
|
|
if(candidates == 2 && mv_cand[0][0] == mv_cand[1][0] && mv_cand[0][1] == mv_cand[1][1]) {
|
|
|
|
candidates = 1;
|
|
|
|
}
|
|
|
|
|
2017-02-05 09:59:21 +00:00
|
|
|
if (state->encoder_control->cfg.tmvp_enable) {
|
2016-08-10 08:58:15 +00:00
|
|
|
/*
|
|
|
|
Predictor block locations
|
2016-09-30 11:59:47 +00:00
|
|
|
__________
|
2016-08-10 08:58:15 +00:00
|
|
|
|CurrentPU|
|
|
|
|
| |C0|__ |
|
|
|
|
| |C3| |
|
|
|
|
|_________|_
|
2016-09-30 11:59:47 +00:00
|
|
|
|H|
|
2016-08-10 08:58:15 +00:00
|
|
|
*/
|
|
|
|
|
2016-08-10 11:03:25 +00:00
|
|
|
if (state->frame->poc > 1 && state->frame->ref->used_size && candidates < AMVP_MAX_NUM_CANDS) {
|
2016-06-14 05:57:07 +00:00
|
|
|
|
2016-08-10 11:03:25 +00:00
|
|
|
const cu_info_t *selected_CU = (h != NULL) ? h : (c3 != NULL) ? c3 : NULL;
|
2016-06-14 05:57:07 +00:00
|
|
|
|
2016-08-10 08:58:15 +00:00
|
|
|
if (selected_CU) {
|
2016-11-04 09:40:22 +00:00
|
|
|
int td, tb;
|
|
|
|
|
|
|
|
// ToDo: allow custom reference lists
|
|
|
|
// Without GOP, assume that every frame is a reference frame in an order
|
|
|
|
if (!state->encoder_control->cfg->gop_len) {
|
|
|
|
td = selected_CU->inter.mv_ref[reflist] + 1;
|
|
|
|
tb = cur_cu->inter.mv_ref[reflist] + 1;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Handle with GOP
|
|
|
|
//ToDo: allow other than L0[0] for prediction
|
|
|
|
|
|
|
|
//Fetch ref idx of the selected CU in L0[0] ref list
|
|
|
|
uint32_t poc_diff = UINT_MAX;
|
|
|
|
uint32_t closest_ref = 0;
|
|
|
|
uint32_t closest_ref_poc = 0;
|
|
|
|
|
|
|
|
for (int temporal_cand = 0; temporal_cand < state->frame->ref->used_size; temporal_cand++) {
|
|
|
|
int ref_poc = state->frame->ref->pocs[temporal_cand];
|
|
|
|
int td = state->frame->poc - ref_poc;
|
|
|
|
|
|
|
|
td = td < 0 ? UINT_MAX : td;
|
|
|
|
if (td < poc_diff) {
|
|
|
|
closest_ref = temporal_cand;
|
|
|
|
poc_diff = td;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closest_ref_poc = state->frame->ref->pocs[closest_ref];
|
|
|
|
|
|
|
|
uint32_t colocated_ref_mv_ref = selected_CU->inter.mv_ref[reflist];
|
|
|
|
|
|
|
|
if (!selected_CU->inter.mv_dir & (1 << reflist)) {
|
|
|
|
colocated_ref_mv_ref = selected_CU->inter.mv_ref[reflist2nd];
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t colocated_ref_frame = state->frame->num+1;
|
|
|
|
uint32_t gop_len = state->encoder_control->cfg->gop_len;
|
|
|
|
uint32_t base_POC = state->frame->poc - (state->frame->poc % gop_len);
|
|
|
|
|
|
|
|
// Search the POC using the GOP structure
|
|
|
|
for (; colocated_ref_frame > 0; colocated_ref_frame--) {
|
|
|
|
if (base_POC + state->encoder_control->cfg->gop[colocated_ref_frame % gop_len].poc_offset == closest_ref_poc) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (colocated_ref_frame % gop_len == 0) base_POC -= gop_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t colocated_pocs[MAX_REF_PIC_COUNT];
|
|
|
|
base_POC = closest_ref_poc - (closest_ref_poc % gop_len);
|
|
|
|
|
|
|
|
// Reconstruct ref list
|
|
|
|
uint32_t max_refs = (state->encoder_control->cfg->gop[colocated_ref_frame % gop_len].ref_neg_count + state->encoder_control->cfg->gop[colocated_ref_frame % gop_len].ref_pos_count);
|
|
|
|
uint32_t current_ref = 0;
|
|
|
|
for (int32_t temp_frame = colocated_ref_frame-1; temp_frame >= 0; temp_frame--) {
|
|
|
|
if (state->encoder_control->cfg->gop[temp_frame % gop_len].is_ref) {
|
|
|
|
colocated_pocs[current_ref] = base_POC + state->encoder_control->cfg->gop[temp_frame % gop_len].poc_offset;
|
|
|
|
if (current_ref == max_refs) break;
|
|
|
|
current_ref++;
|
|
|
|
}
|
|
|
|
if (temp_frame == 0) {
|
|
|
|
colocated_pocs[current_ref] = 0;
|
|
|
|
current_ref++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (temp_frame % gop_len == 0) base_POC -= gop_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
td = closest_ref_poc - colocated_pocs[selected_CU->inter.mv_ref[colocated_ref_mv_ref]];
|
|
|
|
tb = state->frame->poc - state->frame->ref->pocs[cur_cu->inter.mv_ref[reflist]];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int scale = CALCULATE_SCALE(NULL, tb, td);
|
|
|
|
int used_reflist = reflist;
|
|
|
|
|
2016-09-30 11:59:47 +00:00
|
|
|
// If the selected CU does not have the correct list (L0/L1) vector, use the other
|
|
|
|
if (!selected_CU->inter.mv_dir & (1 << reflist)) {
|
2016-11-04 09:40:22 +00:00
|
|
|
//td = state->frame->poc - state->previous_encoder_state->frame->ref->pocs[selected_CU->inter.mv_ref[reflist2nd]];
|
|
|
|
used_reflist = reflist2nd;
|
2016-09-30 11:59:47 +00:00
|
|
|
}
|
2016-06-14 05:57:07 +00:00
|
|
|
|
2016-11-04 09:40:22 +00:00
|
|
|
|
|
|
|
mv_cand[candidates][0] = ((scale * selected_CU->inter.mv[used_reflist][0] + 127 + (scale * selected_CU->inter.mv[used_reflist][0] < 0)) >> 8);
|
|
|
|
mv_cand[candidates][1] = ((scale * selected_CU->inter.mv[used_reflist][1] + 127 + (scale * selected_CU->inter.mv[used_reflist][1] < 0)) >> 8);
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-06-14 05:57:07 +00:00
|
|
|
|
2016-08-10 08:58:15 +00:00
|
|
|
candidates++;
|
|
|
|
}
|
|
|
|
#undef CALCULATE_SCALE
|
2016-06-14 05:57:07 +00:00
|
|
|
}
|
2013-09-16 13:37:24 +00:00
|
|
|
}
|
|
|
|
|
2013-09-19 12:08:30 +00:00
|
|
|
// Fill with (0,0)
|
2013-10-11 13:12:04 +00:00
|
|
|
while (candidates < AMVP_MAX_NUM_CANDS) {
|
2013-09-16 13:37:24 +00:00
|
|
|
mv_cand[candidates][0] = 0;
|
|
|
|
mv_cand[candidates][1] = 0;
|
|
|
|
candidates++;
|
|
|
|
}
|
2014-02-14 13:50:30 +00:00
|
|
|
#undef CALCULATE_SCALE
|
|
|
|
#undef APPLY_MV_SCALING
|
2013-09-19 12:08:30 +00:00
|
|
|
}
|
2013-10-11 13:12:04 +00:00
|
|
|
|
2016-05-17 01:25:45 +00:00
|
|
|
/**
|
|
|
|
* \brief Get MV prediction for current block.
|
|
|
|
*
|
|
|
|
* \param state encoder state
|
|
|
|
* \param x block x position in pixels
|
|
|
|
* \param y block y position in pixels
|
|
|
|
* \param width block width in pixels
|
|
|
|
* \param height block height in pixels
|
|
|
|
* \param mv_cand Return the motion vector candidates.
|
|
|
|
* \param cur_cu current CU
|
|
|
|
* \param lcu current LCU
|
|
|
|
* \param reflist reflist index (either 0 or 1)
|
|
|
|
*/
|
|
|
|
void kvz_inter_get_mv_cand(const encoder_state_t * const state,
|
|
|
|
int32_t x,
|
|
|
|
int32_t y,
|
|
|
|
int32_t width,
|
|
|
|
int32_t height,
|
|
|
|
int16_t mv_cand[2][2],
|
|
|
|
cu_info_t* cur_cu,
|
|
|
|
lcu_t *lcu,
|
|
|
|
int8_t reflist)
|
|
|
|
{
|
2016-06-16 08:41:38 +00:00
|
|
|
cu_info_t *b0, *b1, *b2, *a0, *a1, *c3, *h;
|
2016-06-27 07:11:02 +00:00
|
|
|
b0 = b1 = b2 = a0 = a1 = c3 = h = NULL;
|
2016-05-17 01:25:45 +00:00
|
|
|
get_spatial_merge_candidates(x, y, width, height,
|
|
|
|
state->tile->frame->width, state->tile->frame->height,
|
|
|
|
&b0, &b1, &b2, &a0, &a1, lcu);
|
2016-06-16 08:41:38 +00:00
|
|
|
kvz_inter_get_temporal_merge_candidates(state, x, y, width, height, &c3, &h);
|
|
|
|
get_mv_cand_from_spatial(state, x, y, width, height, b0, b1, b2, a0, a1, c3, h, cur_cu, reflist, mv_cand);
|
2016-05-17 01:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Get MV prediction for current block using state->tile->frame->cu_array.
|
|
|
|
*
|
|
|
|
* \param state encoder state
|
|
|
|
* \param x block x position in pixels
|
|
|
|
* \param y block y position in pixels
|
|
|
|
* \param width block width in pixels
|
|
|
|
* \param height block height in pixels
|
|
|
|
* \param mv_cand Return the motion vector candidates.
|
|
|
|
* \param cur_cu current CU
|
|
|
|
* \param reflist reflist index (either 0 or 1)
|
|
|
|
*/
|
|
|
|
void kvz_inter_get_mv_cand_cua(const encoder_state_t * const state,
|
|
|
|
int32_t x,
|
|
|
|
int32_t y,
|
|
|
|
int32_t width,
|
|
|
|
int32_t height,
|
|
|
|
int16_t mv_cand[2][2],
|
|
|
|
const cu_info_t* cur_cu,
|
|
|
|
int8_t reflist)
|
|
|
|
{
|
2016-08-10 11:03:25 +00:00
|
|
|
const cu_info_t *b0, *b1, *b2, *a0, *a1;
|
|
|
|
cu_info_t *c3, *h;
|
2016-06-27 07:11:02 +00:00
|
|
|
b0 = b1 = b2 = a0 = a1 = c3 = h = NULL;
|
2016-06-16 08:41:38 +00:00
|
|
|
|
2016-05-17 01:25:45 +00:00
|
|
|
const cu_array_t *cua = state->tile->frame->cu_array;
|
2015-12-16 12:53:18 +00:00
|
|
|
get_spatial_merge_candidates_cua(cua,
|
|
|
|
x, y, width, height,
|
2016-05-17 01:25:45 +00:00
|
|
|
state->tile->frame->width, state->tile->frame->height,
|
2015-12-16 12:53:18 +00:00
|
|
|
&b0, &b1, &b2, &a0, &a1);
|
2016-06-16 08:41:38 +00:00
|
|
|
kvz_inter_get_temporal_merge_candidates(state, x, y, width, height, &c3, &h);
|
|
|
|
get_mv_cand_from_spatial(state, x, y, width, height, b0, b1, b2, a0, a1, c3, h, cur_cu, reflist, mv_cand);
|
2016-05-17 01:25:45 +00:00
|
|
|
}
|
|
|
|
|
2013-10-11 13:12:04 +00:00
|
|
|
/**
|
|
|
|
* \brief Get merge predictions for current block
|
2015-10-12 11:44:03 +00:00
|
|
|
* \param state the encoder state
|
|
|
|
* \param x block x position in SCU
|
|
|
|
* \param y block y position in SCU
|
|
|
|
* \param width block width
|
|
|
|
* \param height block height
|
|
|
|
* \param use_a1 true, if candidate a1 can be used
|
|
|
|
* \param use_b1 true, if candidate b1 can be used
|
|
|
|
* \param mv_cand Returns the merge candidates.
|
|
|
|
* \param lcu lcu containing the block
|
|
|
|
* \return number of merge candidates
|
2013-10-11 13:12:04 +00:00
|
|
|
*/
|
2015-10-12 11:44:03 +00:00
|
|
|
uint8_t kvz_inter_get_merge_cand(const encoder_state_t * const state,
|
|
|
|
int32_t x, int32_t y,
|
|
|
|
int32_t width, int32_t height,
|
|
|
|
bool use_a1, bool use_b1,
|
|
|
|
inter_merge_cand_t mv_cand[MRG_MAX_NUM_CANDS],
|
|
|
|
lcu_t *lcu)
|
2014-02-21 13:00:20 +00:00
|
|
|
{
|
2013-10-11 13:12:04 +00:00
|
|
|
uint8_t candidates = 0;
|
|
|
|
int8_t duplicate = 0;
|
|
|
|
|
2015-03-04 11:15:45 +00:00
|
|
|
cu_info_t *b0, *b1, *b2, *a0, *a1;
|
2014-02-14 13:50:30 +00:00
|
|
|
int8_t zero_idx = 0;
|
2013-10-11 13:12:04 +00:00
|
|
|
b0 = b1 = b2 = a0 = a1 = NULL;
|
2016-05-10 00:47:20 +00:00
|
|
|
get_spatial_merge_candidates(x, y, width, height,
|
|
|
|
state->tile->frame->width, state->tile->frame->height,
|
|
|
|
&b0, &b1, &b2, &a0, &a1, lcu);
|
2014-02-21 13:00:20 +00:00
|
|
|
|
2015-10-12 11:44:03 +00:00
|
|
|
if (!use_a1) a1 = NULL;
|
|
|
|
if (!use_b1) b1 = NULL;
|
2013-10-11 13:12:04 +00:00
|
|
|
|
2015-11-02 08:26:02 +00:00
|
|
|
#define CHECK_DUPLICATE(CU1,CU2) {duplicate = 0; if ((CU2) && \
|
2015-04-23 09:18:33 +00:00
|
|
|
(CU1)->inter.mv_dir == (CU2)->inter.mv_dir && \
|
2015-03-31 09:23:46 +00:00
|
|
|
(!(((CU1)->inter.mv_dir & 1) && ((CU2)->inter.mv_dir & 1)) || \
|
2015-03-30 11:40:29 +00:00
|
|
|
((CU1)->inter.mv[0][0] == (CU2)->inter.mv[0][0] && \
|
2015-04-23 09:18:33 +00:00
|
|
|
(CU1)->inter.mv[0][1] == (CU2)->inter.mv[0][1] && \
|
|
|
|
(CU1)->inter.mv_ref[0] == (CU2)->inter.mv_ref[0]) ) && \
|
|
|
|
(!(((CU1)->inter.mv_dir & 2) && ((CU2)->inter.mv_dir & 2) ) || \
|
2015-03-30 11:40:29 +00:00
|
|
|
((CU1)->inter.mv[1][0] == (CU2)->inter.mv[1][0] && \
|
2015-04-23 09:18:33 +00:00
|
|
|
(CU1)->inter.mv[1][1] == (CU2)->inter.mv[1][1] && \
|
|
|
|
(CU1)->inter.mv_ref[1] == (CU2)->inter.mv_ref[1]) ) \
|
2015-03-30 11:40:29 +00:00
|
|
|
) duplicate = 1; }
|
|
|
|
|
2015-11-02 08:26:02 +00:00
|
|
|
if (a1) {
|
2015-03-30 11:40:29 +00:00
|
|
|
mv_cand[candidates].mv[0][0] = a1->inter.mv[0][0];
|
|
|
|
mv_cand[candidates].mv[0][1] = a1->inter.mv[0][1];
|
|
|
|
mv_cand[candidates].mv[1][0] = a1->inter.mv[1][0];
|
|
|
|
mv_cand[candidates].mv[1][1] = a1->inter.mv[1][1];
|
|
|
|
mv_cand[candidates].ref[0] = a1->inter.mv_ref[0];
|
|
|
|
mv_cand[candidates].ref[1] = a1->inter.mv_ref[1];
|
2015-03-18 08:03:06 +00:00
|
|
|
mv_cand[candidates].dir = a1->inter.mv_dir;
|
|
|
|
candidates++;
|
2013-10-11 13:12:04 +00:00
|
|
|
}
|
|
|
|
|
2015-11-02 08:26:02 +00:00
|
|
|
if (b1) {
|
2014-02-18 14:59:45 +00:00
|
|
|
if(candidates) CHECK_DUPLICATE(b1, a1);
|
2013-10-11 13:12:04 +00:00
|
|
|
if(!duplicate) {
|
2015-03-30 11:40:29 +00:00
|
|
|
mv_cand[candidates].mv[0][0] = b1->inter.mv[0][0];
|
|
|
|
mv_cand[candidates].mv[0][1] = b1->inter.mv[0][1];
|
|
|
|
mv_cand[candidates].mv[1][0] = b1->inter.mv[1][0];
|
|
|
|
mv_cand[candidates].mv[1][1] = b1->inter.mv[1][1];
|
|
|
|
mv_cand[candidates].ref[0] = b1->inter.mv_ref[0];
|
|
|
|
mv_cand[candidates].ref[1] = b1->inter.mv_ref[1];
|
2015-03-18 08:03:06 +00:00
|
|
|
mv_cand[candidates].dir = b1->inter.mv_dir;
|
2013-10-11 13:12:04 +00:00
|
|
|
candidates++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-02 08:26:02 +00:00
|
|
|
if (b0) {
|
2014-02-18 14:59:45 +00:00
|
|
|
if(candidates) CHECK_DUPLICATE(b0,b1);
|
2013-10-11 13:12:04 +00:00
|
|
|
if(!duplicate) {
|
2015-03-30 11:40:29 +00:00
|
|
|
mv_cand[candidates].mv[0][0] = b0->inter.mv[0][0];
|
|
|
|
mv_cand[candidates].mv[0][1] = b0->inter.mv[0][1];
|
|
|
|
mv_cand[candidates].mv[1][0] = b0->inter.mv[1][0];
|
|
|
|
mv_cand[candidates].mv[1][1] = b0->inter.mv[1][1];
|
|
|
|
mv_cand[candidates].ref[0] = b0->inter.mv_ref[0];
|
|
|
|
mv_cand[candidates].ref[1] = b0->inter.mv_ref[1];
|
2015-03-18 08:03:06 +00:00
|
|
|
mv_cand[candidates].dir = b0->inter.mv_dir;
|
2013-10-11 13:12:04 +00:00
|
|
|
candidates++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-02 08:26:02 +00:00
|
|
|
if (a0) {
|
2014-02-18 14:59:45 +00:00
|
|
|
if(candidates) CHECK_DUPLICATE(a0,a1);
|
2013-10-11 13:12:04 +00:00
|
|
|
if(!duplicate) {
|
2015-03-30 11:40:29 +00:00
|
|
|
mv_cand[candidates].mv[0][0] = a0->inter.mv[0][0];
|
|
|
|
mv_cand[candidates].mv[0][1] = a0->inter.mv[0][1];
|
|
|
|
mv_cand[candidates].mv[1][0] = a0->inter.mv[1][0];
|
|
|
|
mv_cand[candidates].mv[1][1] = a0->inter.mv[1][1];
|
|
|
|
mv_cand[candidates].ref[0] = a0->inter.mv_ref[0];
|
|
|
|
mv_cand[candidates].ref[1] = a0->inter.mv_ref[1];
|
2015-03-18 08:03:06 +00:00
|
|
|
mv_cand[candidates].dir = a0->inter.mv_dir;
|
2013-10-11 13:12:04 +00:00
|
|
|
candidates++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-23 13:50:11 +00:00
|
|
|
if (candidates != 4) {
|
2015-11-02 08:26:02 +00:00
|
|
|
if (b2) {
|
2014-02-18 14:59:45 +00:00
|
|
|
CHECK_DUPLICATE(b2,a1);
|
2013-10-23 13:50:11 +00:00
|
|
|
if(!duplicate) {
|
2014-02-18 14:59:45 +00:00
|
|
|
CHECK_DUPLICATE(b2,b1);
|
2013-10-23 13:50:11 +00:00
|
|
|
if(!duplicate) {
|
2015-03-30 11:40:29 +00:00
|
|
|
mv_cand[candidates].mv[0][0] = b2->inter.mv[0][0];
|
|
|
|
mv_cand[candidates].mv[0][1] = b2->inter.mv[0][1];
|
|
|
|
mv_cand[candidates].mv[1][0] = b2->inter.mv[1][0];
|
|
|
|
mv_cand[candidates].mv[1][1] = b2->inter.mv[1][1];
|
|
|
|
mv_cand[candidates].ref[0] = b2->inter.mv_ref[0];
|
|
|
|
mv_cand[candidates].ref[1] = b2->inter.mv_ref[1];
|
2015-03-18 08:03:06 +00:00
|
|
|
mv_cand[candidates].dir = b2->inter.mv_dir;
|
2013-10-23 13:50:11 +00:00
|
|
|
candidates++;
|
|
|
|
}
|
|
|
|
}
|
2013-10-11 13:12:04 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-04 09:40:22 +00:00
|
|
|
return candidates;
|
2017-02-05 09:59:21 +00:00
|
|
|
if (state->encoder_control->cfg.tmvp_enable) {
|
2016-06-14 05:57:44 +00:00
|
|
|
|
2016-08-10 11:03:25 +00:00
|
|
|
if (candidates < MRG_MAX_NUM_CANDS && state->frame->ref->used_size) {
|
2016-06-14 05:57:44 +00:00
|
|
|
|
2016-08-10 11:03:25 +00:00
|
|
|
cu_info_t *c3 = NULL;
|
|
|
|
cu_info_t *h = NULL;
|
2016-08-10 08:58:15 +00:00
|
|
|
|
2016-08-10 11:03:25 +00:00
|
|
|
kvz_inter_get_temporal_merge_candidates(state, x, y, width, height, &c3, &h);
|
2016-06-14 05:57:44 +00:00
|
|
|
|
2016-08-10 11:03:25 +00:00
|
|
|
const cu_info_t *selected_CU = (h != NULL) ? h : (c3 != NULL) ? c3 : NULL;
|
2016-06-14 05:57:44 +00:00
|
|
|
|
2016-08-10 08:58:15 +00:00
|
|
|
if (selected_CU) {
|
2016-09-30 11:59:47 +00:00
|
|
|
uint32_t poc_diff = UINT_MAX;
|
|
|
|
for (int temporal_cand = 0; temporal_cand < state->frame->ref->used_size; temporal_cand++) {
|
|
|
|
int td = state->frame->poc - state->frame->ref->pocs[temporal_cand];
|
|
|
|
td = td < 0 ? UINT_MAX : td;
|
|
|
|
if (td < poc_diff) {
|
|
|
|
poc_diff = td;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-04 09:40:22 +00:00
|
|
|
int tb = state->frame->poc - state->frame->ref->pocs[selected_CU->inter.mv_ref[0]];
|
|
|
|
int td = poc_diff;
|
2016-08-10 08:58:15 +00:00
|
|
|
|
2016-11-04 09:40:22 +00:00
|
|
|
int scale = ((tb * ((0x4000 + (abs(td) >> 1)) / td) + 32) >> 6);
|
2016-09-30 11:59:47 +00:00
|
|
|
|
2016-11-04 09:40:22 +00:00
|
|
|
mv_cand[candidates].mv[0][0] = ((scale * selected_CU->inter.mv[0][0] + 127 + (scale * selected_CU->inter.mv[0][0] < 0)) >> 8);
|
|
|
|
mv_cand[candidates].mv[0][1] = ((scale * selected_CU->inter.mv[0][1] + 127 + (scale * selected_CU->inter.mv[0][1] < 0)) >> 8);
|
|
|
|
mv_cand[candidates].mv[1][0] = ((scale * selected_CU->inter.mv[1][0] + 127 + (scale * selected_CU->inter.mv[1][0] < 0)) >> 8);
|
|
|
|
mv_cand[candidates].mv[1][1] = ((scale * selected_CU->inter.mv[1][1] + 127 + (scale * selected_CU->inter.mv[1][1] < 0)) >> 8);
|
2016-09-30 11:59:47 +00:00
|
|
|
|
|
|
|
|
2016-08-10 08:58:15 +00:00
|
|
|
mv_cand[candidates].dir = selected_CU->inter.mv_dir;
|
2016-11-04 09:40:22 +00:00
|
|
|
mv_cand[candidates].ref[0] = selected_CU->inter.mv_ref[0];
|
|
|
|
mv_cand[candidates].ref[1] = selected_CU->inter.mv_ref[1];
|
2016-08-10 08:58:15 +00:00
|
|
|
candidates++;
|
|
|
|
}
|
2016-06-14 05:57:44 +00:00
|
|
|
}
|
2013-10-11 13:12:04 +00:00
|
|
|
}
|
|
|
|
|
2016-08-10 00:46:23 +00:00
|
|
|
if (candidates < MRG_MAX_NUM_CANDS && state->frame->slicetype == KVZ_SLICE_B) {
|
2015-03-18 08:03:06 +00:00
|
|
|
#define NUM_PRIORITY_LIST 12;
|
|
|
|
static const uint8_t priorityList0[] = { 0, 1, 0, 2, 1, 2, 0, 3, 1, 3, 2, 3 };
|
|
|
|
static const uint8_t priorityList1[] = { 1, 0, 2, 0, 2, 1, 3, 0, 3, 1, 3, 2 };
|
|
|
|
uint8_t cutoff = candidates;
|
|
|
|
for (int32_t idx = 0; idx<cutoff*(cutoff - 1) && candidates != MRG_MAX_NUM_CANDS; idx++) {
|
|
|
|
uint8_t i = priorityList0[idx];
|
|
|
|
uint8_t j = priorityList1[idx];
|
2015-03-20 08:33:05 +00:00
|
|
|
if (i >= candidates || j >= candidates) break;
|
2015-03-18 08:03:06 +00:00
|
|
|
|
|
|
|
// Find one L0 and L1 candidate according to the priority list
|
|
|
|
if ((mv_cand[i].dir & 0x1) && (mv_cand[j].dir & 0x2)) {
|
|
|
|
mv_cand[candidates].dir = 3;
|
|
|
|
|
|
|
|
// get Mv from cand[i] and cand[j]
|
|
|
|
mv_cand[candidates].mv[0][0] = mv_cand[i].mv[0][0];
|
|
|
|
mv_cand[candidates].mv[0][1] = mv_cand[i].mv[0][1];
|
|
|
|
mv_cand[candidates].mv[1][0] = mv_cand[j].mv[1][0];
|
|
|
|
mv_cand[candidates].mv[1][1] = mv_cand[j].mv[1][1];
|
2015-03-30 11:40:29 +00:00
|
|
|
mv_cand[candidates].ref[0] = mv_cand[i].ref[0];
|
|
|
|
mv_cand[candidates].ref[1] = mv_cand[j].ref[1];
|
2015-03-18 08:03:06 +00:00
|
|
|
|
2015-03-30 11:40:29 +00:00
|
|
|
if (mv_cand[i].ref[0] == mv_cand[j].ref[1] &&
|
2015-03-18 08:03:06 +00:00
|
|
|
mv_cand[i].mv[0][0] == mv_cand[j].mv[1][0] &&
|
|
|
|
mv_cand[i].mv[0][1] == mv_cand[j].mv[1][1]) {
|
|
|
|
// Not a candidate
|
|
|
|
} else {
|
|
|
|
candidates++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-10 00:46:23 +00:00
|
|
|
int num_ref = state->frame->ref->used_size;
|
2015-03-20 08:33:05 +00:00
|
|
|
|
2016-08-10 00:46:23 +00:00
|
|
|
if (candidates < MRG_MAX_NUM_CANDS && state->frame->slicetype == KVZ_SLICE_B) {
|
2015-03-20 08:33:05 +00:00
|
|
|
int j;
|
|
|
|
int ref_negative = 0;
|
|
|
|
int ref_positive = 0;
|
2016-08-10 00:46:23 +00:00
|
|
|
for (j = 0; j < state->frame->ref->used_size; j++) {
|
|
|
|
if (state->frame->ref->pocs[j] < state->frame->poc) {
|
2015-03-20 08:33:05 +00:00
|
|
|
ref_negative++;
|
|
|
|
} else {
|
|
|
|
ref_positive++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
num_ref = MIN(ref_negative, ref_positive);
|
|
|
|
}
|
2015-04-23 09:18:33 +00:00
|
|
|
|
2013-11-05 10:49:39 +00:00
|
|
|
// Add (0,0) prediction
|
2015-03-20 08:33:05 +00:00
|
|
|
while (candidates != MRG_MAX_NUM_CANDS) {
|
2015-03-18 08:03:06 +00:00
|
|
|
mv_cand[candidates].mv[0][0] = 0;
|
|
|
|
mv_cand[candidates].mv[0][1] = 0;
|
2015-03-30 11:40:29 +00:00
|
|
|
mv_cand[candidates].ref[0] = (zero_idx>=num_ref-1)?0:zero_idx;
|
|
|
|
mv_cand[candidates].ref[1] = mv_cand[candidates].ref[0];
|
2015-03-18 08:03:06 +00:00
|
|
|
mv_cand[candidates].dir = 1;
|
2016-08-10 00:46:23 +00:00
|
|
|
if (state->frame->slicetype == KVZ_SLICE_B) {
|
2015-03-18 08:03:06 +00:00
|
|
|
mv_cand[candidates].mv[1][0] = 0;
|
|
|
|
mv_cand[candidates].mv[1][1] = 0;
|
|
|
|
mv_cand[candidates].dir = 3;
|
|
|
|
}
|
2014-02-14 13:50:30 +00:00
|
|
|
zero_idx++;
|
2013-10-11 13:12:04 +00:00
|
|
|
candidates++;
|
|
|
|
}
|
2013-11-05 10:49:39 +00:00
|
|
|
|
2013-10-11 13:12:04 +00:00
|
|
|
return candidates;
|
|
|
|
}
|