Skip to content

Commit

Permalink
Merge branch 'reflist_changes'
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/encoderstate.c
#	src/search_inter.c
  • Loading branch information
Miika Metsoila committed Sep 18, 2017
2 parents f8c5bb1 + 769b177 commit 7b0101c
Show file tree
Hide file tree
Showing 13 changed files with 224 additions and 160 deletions.
2 changes: 1 addition & 1 deletion src/cu.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ typedef struct
} intra;
struct {
int16_t mv[2][2]; // \brief Motion vectors for L0 and L1
uint8_t mv_ref[2]; // \brief Index of the encoder_control.ref array.
uint8_t mv_ref[2]; // \brief Index of the L0 and L1 array.
uint8_t mv_cand0 : 3; // \brief selected MV candidate
uint8_t mv_cand1 : 3; // \brief selected MV candidate
uint8_t mv_dir : 2; // \brief Probably describes if mv_ref is L0, L1 or both (bi-pred)
Expand Down
19 changes: 7 additions & 12 deletions src/encode_coding_tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -560,15 +560,6 @@ static void encode_inter_prediction_unit(encoder_state_t * const state,
}
} else {
uint32_t ref_list_idx;
uint32_t j;
int ref_list[2] = { 0, 0 };
for (j = 0; j < state->frame->ref->used_size; j++) {
if (state->frame->ref->pocs[j] < state->frame->poc) {
ref_list[0]++;
} else {
ref_list[1]++;
}
}

// Void TEncSbac::codeInterDir( TComDataCU* pcCU, UInt uiAbsPartIdx )
if (state->frame->slicetype == KVZ_SLICE_B)
Expand All @@ -592,16 +583,20 @@ static void encode_inter_prediction_unit(encoder_state_t * const state,

for (ref_list_idx = 0; ref_list_idx < 2; ref_list_idx++) {
if (cur_cu->inter.mv_dir & (1 << ref_list_idx)) {
if (ref_list[ref_list_idx] > 1) {

// size of the current reference index list (L0/L1)
uint8_t ref_LX_size = state->frame->ref_LX_size[ref_list_idx];

if (ref_LX_size > 1) {
// parseRefFrmIdx
int32_t ref_frame = state->frame->refmap[cur_cu->inter.mv_ref[ref_list_idx]].idx;
int32_t ref_frame = cur_cu->inter.mv_ref[ref_list_idx];

cabac->cur_ctx = &(cabac->ctx.cu_ref_pic_model[0]);
CABAC_BIN(cabac, (ref_frame != 0), "ref_idx_lX");

if (ref_frame > 0) {
int32_t i;
int32_t ref_num = ref_list[ref_list_idx] - 2;
int32_t ref_num = ref_LX_size - 2;

cabac->cur_ctx = &(cabac->ctx.cu_ref_pic_model[1]);
ref_frame--;
Expand Down
74 changes: 22 additions & 52 deletions src/encoderstate.c
Original file line number Diff line number Diff line change
Expand Up @@ -945,81 +945,50 @@ static void encoder_state_encode(encoder_state_t * const main_state) {
}


static void encoder_ref_insertion_sort(int reflist[16], int length) {
static void encoder_ref_insertion_sort(const encoder_state_t *const state, uint8_t reflist[16], uint8_t length) {

for (uint8_t i = 1; i < length; ++i) {
const int16_t cur_poc = reflist[i];
int16_t j = i;
while (j > 0 && cur_poc < reflist[j - 1]) {
const uint8_t cur_idx = reflist[i];
const int32_t cur_poc = state->frame->ref->pocs[cur_idx];
int8_t j = i;
while (j > 0 && cur_poc > state->frame->ref->pocs[reflist[j - 1]]) {
reflist[j] = reflist[j - 1];
--j;
}
reflist[j] = cur_poc;
reflist[j] = cur_idx;
}
}

/**
* \brief Return reference picture lists.
* \brief Generate reference picture lists.
*
* \param state main encoder state
* \param ref_list_len_out Returns the lengths of the reference lists.
* \param ref_list_poc_out Returns two lists of POCs of the reference pictures.
*/
void kvz_encoder_get_ref_lists(const encoder_state_t *const state,
int ref_list_len_out[2],
int ref_list_poc_out[2][16])
void kvz_encoder_create_ref_lists(const encoder_state_t *const state)
{
FILL_ARRAY(ref_list_len_out, 0, 2);
// TODO check possibility to add L0 references to L1 list also

FILL_ARRAY(state->frame->ref_LX_size, 0, 2);

// List all pocs of lists
int j = 0;
for (j = 0; j < state->frame->ref->used_size; j++) {
if (state->frame->ref->pocs[j] < state->frame->poc) {
ref_list_poc_out[0][ref_list_len_out[0]] = state->frame->ref->pocs[j];
ref_list_len_out[0]++;
state->frame->ref_LX[0][state->frame->ref_LX_size[0]] = j;
state->frame->ref_LX_size[0] += 1;
} else {
ref_list_poc_out[1][ref_list_len_out[1]] = state->frame->ref->pocs[j];
ref_list_len_out[1]++;
state->frame->ref_LX[1][state->frame->ref_LX_size[1]] = j;
state->frame->ref_LX_size[1] += 1;
}
}

// Fill the rest of ref_list_poc_out array with -1s.
// Fill the rest with -1s.
for (; j < 16; j++) {
ref_list_poc_out[0][j] = -1;
ref_list_poc_out[1][j] = -1;
state->frame->ref_LX[0][j] = (uint8_t) -1;
state->frame->ref_LX[1][j] = (uint8_t) -1;
}

encoder_ref_insertion_sort(ref_list_poc_out[0], ref_list_len_out[0]);
encoder_ref_insertion_sort(ref_list_poc_out[1], ref_list_len_out[1]);
}

static void encoder_state_ref_sort(encoder_state_t *state) {
int ref_list_len[2];
int ref_list_poc[2][16];

kvz_encoder_get_ref_lists(state, ref_list_len, ref_list_poc);

for (int j = 0; j < state->frame->ref->used_size; j++) {
if (state->frame->ref->pocs[j] < state->frame->poc) {
for (int ref_idx = 0; ref_idx < ref_list_len[0]; ref_idx++) {
if (ref_list_poc[0][ref_idx] == state->frame->ref->pocs[j]) {
state->frame->refmap[j].idx = ref_list_len[0] - ref_idx - 1;
break;
}
}
state->frame->refmap[j].list = 1;

} else {
for (int ref_idx = 0; ref_idx < ref_list_len[1]; ref_idx++) {
if (ref_list_poc[1][ref_idx] == state->frame->ref->pocs[j]) {
state->frame->refmap[j].idx = ref_idx;
break;
}
}
state->frame->refmap[j].list = 2;
}
state->frame->refmap[j].poc = state->frame->ref->pocs[j];
}
encoder_ref_insertion_sort(state, state->frame->ref_LX[0], state->frame->ref_LX_size[0]);
}

/**
Expand Down Expand Up @@ -1201,7 +1170,7 @@ static void encoder_state_init_new_frame(encoder_state_t * const state, kvz_pict
}

encoder_state_remove_refs(state);
encoder_state_ref_sort(state);
kvz_encoder_create_ref_lists(state);

if (cfg->target_bitrate > 0 && state->frame->num > cfg->owf) {
normalize_lcu_weights(state);
Expand Down Expand Up @@ -1295,7 +1264,8 @@ void kvz_encoder_prepare(encoder_state_t *state)
kvz_image_list_add(state->frame->ref,
prev_state->tile->frame->rec,
prev_state->tile->frame->cu_array,
prev_state->frame->poc);
prev_state->frame->poc,
prev_state->frame->ref_LX);
kvz_cu_array_free(&state->tile->frame->cu_array);
unsigned height = state->tile->frame->height_in_lcu * LCU_WIDTH;
unsigned width = state->tile->frame->width_in_lcu * LCU_WIDTH;
Expand Down
15 changes: 6 additions & 9 deletions src/encoderstate.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,14 @@ typedef struct encoder_state_config_frame_t {
//! \brief quantization factor
double QP_factor;

//Current picture available references
//! Current pictures available for references
image_list_t *ref;
int8_t ref_list;

struct {
int32_t poc;
int8_t list;
int8_t idx;
} refmap[16];
//! L0 and L1 reference index list
uint8_t ref_LX[2][16];
//! L0 reference index list size
uint8_t ref_LX_size[2];

bool is_idr_frame;
uint8_t pictype;
Expand Down Expand Up @@ -292,9 +291,7 @@ int kvz_encoder_state_match_children_of_previous_frame(encoder_state_t * const s

coeff_scan_order_t kvz_get_scan_order(int8_t cu_type, int intra_mode, int depth);

void kvz_encoder_get_ref_lists(const encoder_state_t *const state,
int ref_list_len_out[2],
int ref_list_poc_out[2][16]);
void kvz_encoder_create_ref_lists(const encoder_state_t *const state);

lcu_stats_t* kvz_get_lcu_stats(encoder_state_t *state, int lcu_x, int lcu_y);

Expand Down
15 changes: 9 additions & 6 deletions src/filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -401,10 +401,13 @@ static void filter_deblock_edge_luma(encoder_state_t * const state,
// Non-zero residual/coeffs and transform boundary
// Neither CU is intra so tr_depth <= MAX_DEPTH.
strength = 1;
} else if (cu_p->inter.mv_dir != 3 && cu_q->inter.mv_dir != 3 && ((abs(cu_q->inter.mv[cu_q->inter.mv_dir - 1][0] - cu_p->inter.mv[cu_p->inter.mv_dir - 1][0]) >= 4) || (abs(cu_q->inter.mv[cu_q->inter.mv_dir - 1][1] - cu_p->inter.mv[cu_p->inter.mv_dir - 1][1]) >= 4))) {
} else if (cu_p->inter.mv_dir != 3 && cu_q->inter.mv_dir != 3 &&
((abs(cu_q->inter.mv[cu_q->inter.mv_dir - 1][0] - cu_p->inter.mv[cu_p->inter.mv_dir - 1][0]) >= 4) ||
(abs(cu_q->inter.mv[cu_q->inter.mv_dir - 1][1] - cu_p->inter.mv[cu_p->inter.mv_dir - 1][1]) >= 4))) {
// Absolute motion vector diff between blocks >= 1 (Integer pixel)
strength = 1;
} else if (cu_p->inter.mv_dir != 3 && cu_q->inter.mv_dir != 3 && cu_q->inter.mv_ref[cu_q->inter.mv_dir - 1] != cu_p->inter.mv_ref[cu_p->inter.mv_dir - 1]) {
} else if (cu_p->inter.mv_dir != 3 && cu_q->inter.mv_dir != 3 &&
cu_q->inter.mv_ref[cu_q->inter.mv_dir - 1] != cu_p->inter.mv_ref[cu_p->inter.mv_dir - 1]) {
strength = 1;
}

Expand All @@ -429,10 +432,10 @@ static void filter_deblock_edge_luma(encoder_state_t * const state,
cu_p->inter.mv[1][0] = 0;
cu_p->inter.mv[1][1] = 0;
}
const int refP0 = (cu_p->inter.mv_dir & 1) ? cu_p->inter.mv_ref[0] : -1;
const int refP1 = (cu_p->inter.mv_dir & 2) ? cu_p->inter.mv_ref[1] : -1;
const int refQ0 = (cu_q->inter.mv_dir & 1) ? cu_q->inter.mv_ref[0] : -1;
const int refQ1 = (cu_q->inter.mv_dir & 2) ? cu_q->inter.mv_ref[1] : -1;
const int refP0 = (cu_p->inter.mv_dir & 1) ? state->frame->ref_LX[0][cu_p->inter.mv_ref[0]] : -1;
const int refP1 = (cu_p->inter.mv_dir & 2) ? state->frame->ref_LX[1][cu_p->inter.mv_ref[1]] : -1;
const int refQ0 = (cu_q->inter.mv_dir & 1) ? state->frame->ref_LX[0][cu_q->inter.mv_ref[0]] : -1;
const int refQ1 = (cu_q->inter.mv_dir & 2) ? state->frame->ref_LX[1][cu_q->inter.mv_ref[1]] : -1;
const int16_t* mvQ0 = cu_q->inter.mv[0];
const int16_t* mvQ1 = cu_q->inter.mv[1];

Expand Down
38 changes: 33 additions & 5 deletions src/imagelist.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ image_list_t * kvz_image_list_alloc(int size)
{
image_list_t *list = (image_list_t *)malloc(sizeof(image_list_t));
list->size = size;
list->images = malloc(sizeof(kvz_picture*) * size);
list->cu_arrays = malloc(sizeof(cu_array_t*) * size);
list->pocs = malloc(sizeof(int32_t) * size);
list->images = malloc(sizeof(kvz_picture*) * size);
list->cu_arrays = malloc(sizeof(cu_array_t*) * size);
list->pocs = malloc(sizeof(int32_t) * size);
list->ref_LXs = malloc(sizeof(*list->ref_LXs) * size);
list->used_size = 0;

return list;
Expand All @@ -55,6 +56,7 @@ int kvz_image_list_resize(image_list_t *list, unsigned size)
list->images = (kvz_picture**)realloc(list->images, sizeof(kvz_picture*) * size);
list->cu_arrays = (cu_array_t**)realloc(list->cu_arrays, sizeof(cu_array_t*) * size);
list->pocs = realloc(list->pocs, sizeof(int32_t) * size);
list->ref_LXs = realloc(list->ref_LXs, sizeof(*list->ref_LXs) * size);
list->size = size;
return size == 0 || (list->images && list->cu_arrays && list->pocs);
}
Expand All @@ -74,17 +76,23 @@ int kvz_image_list_destroy(image_list_t *list)
kvz_cu_array_free(&list->cu_arrays[i]);
list->cu_arrays[i] = NULL;
list->pocs[i] = 0;
for (int j = 0; j < 16; j++) {
list->ref_LXs[i][0][j] = 0;
list->ref_LXs[i][1][j] = 0;
}
}
}

if (list->size > 0) {
free(list->images);
free(list->cu_arrays);
free(list->pocs);
free(list->ref_LXs);
}
list->images = NULL;
list->cu_arrays = NULL;
list->pocs = NULL;
list->ref_LXs = NULL;
free(list);
return 1;
}
Expand All @@ -95,7 +103,7 @@ int kvz_image_list_destroy(image_list_t *list)
* \param picture_list list to use
* \return 1 on success
*/
int kvz_image_list_add(image_list_t *list, kvz_picture *im, cu_array_t *cua, int32_t poc)
int kvz_image_list_add(image_list_t *list, kvz_picture *im, cu_array_t *cua, int32_t poc, uint8_t ref_LX[2][16])
{
int i = 0;
if (KVZ_ATOMIC_INC(&(im->refcount)) == 1) {
Expand All @@ -119,11 +127,19 @@ int kvz_image_list_add(image_list_t *list, kvz_picture *im, cu_array_t *cua, int
list->images[i] = list->images[i - 1];
list->cu_arrays[i] = list->cu_arrays[i - 1];
list->pocs[i] = list->pocs[i - 1];
for (int j = 0; j < 16; j++) {
list->ref_LXs[i][0][j] = list->ref_LXs[i - 1][0][j];
list->ref_LXs[i][1][j] = list->ref_LXs[i - 1][1][j];
}
}

list->images[0] = im;
list->cu_arrays[0] = cua;
list->pocs[0] = poc;
for (int j = 0; j < 16; j++) {
list->ref_LXs[0][0][j] = ref_LX[0][j];
list->ref_LXs[0][1][j] = ref_LX[1][j];
}

list->used_size++;
return 1;
Expand Down Expand Up @@ -152,6 +168,10 @@ int kvz_image_list_rem(image_list_t * const list, const unsigned n)
list->images[n] = NULL;
list->cu_arrays[n] = NULL;
list->pocs[n] = 0;
for (int j = 0; j < 16; j++) {
list->ref_LXs[n][0][j] = 0;
list->ref_LXs[n][1][j] = 0;
}
list->used_size--;
} else {
int i = n;
Expand All @@ -160,10 +180,18 @@ int kvz_image_list_rem(image_list_t * const list, const unsigned n)
list->images[i] = list->images[i + 1];
list->cu_arrays[i] = list->cu_arrays[i + 1];
list->pocs[i] = list->pocs[i + 1];
for (int j = 0; j < 16; j++) {
list->ref_LXs[i][0][j] = list->ref_LXs[i + 1][0][j];
list->ref_LXs[i][1][j] = list->ref_LXs[i + 1][1][j];
}
}
list->images[list->used_size - 1] = NULL;
list->cu_arrays[list->used_size - 1] = NULL;
list->pocs[list->used_size - 1] = 0;
for (int j = 0; j < 16; j++) {
list->ref_LXs[list->used_size - 1][0][j] = 0;
list->ref_LXs[list->used_size - 1][1][j] = 0;
}
list->used_size--;
}

Expand All @@ -177,7 +205,7 @@ int kvz_image_list_copy_contents(image_list_t *target, image_list_t *source) {
}

for (i = source->used_size - 1; i >= 0; --i) {
kvz_image_list_add(target, source->images[i], source->cu_arrays[i], source->pocs[i]);
kvz_image_list_add(target, source->images[i], source->cu_arrays[i], source->pocs[i], source->ref_LXs[i]);
}
return 1;
}
5 changes: 4 additions & 1 deletion src/imagelist.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,17 @@ typedef struct
struct kvz_picture* *images; //!< \brief Pointer to array of picture pointers.
cu_array_t* *cu_arrays;
int32_t *pocs;
uint8_t (*ref_LXs)[2][16]; //!< L0 and L1 reference index list for each image
uint32_t size; //!< \brief Array size.
uint32_t used_size;


} image_list_t;

image_list_t * kvz_image_list_alloc(int size);
int kvz_image_list_resize(image_list_t *list, unsigned size);
int kvz_image_list_destroy(image_list_t *list);
int kvz_image_list_add(image_list_t *list, kvz_picture *im, cu_array_t* cua, int32_t poc);
int kvz_image_list_add(image_list_t *list, kvz_picture *im, cu_array_t* cua, int32_t poc, uint8_t ref_LX[2][16]);
int kvz_image_list_rem(image_list_t *list, unsigned n);

int kvz_image_list_copy_contents(image_list_t *target, image_list_t *source);
Expand Down
Loading

0 comments on commit 7b0101c

Please sign in to comment.