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
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* \file
|
2013-04-16 08:23:03 +00:00
|
|
|
*/
|
|
|
|
|
2013-09-18 09:16:03 +00:00
|
|
|
#include "search.h"
|
2013-04-16 08:23:03 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-03-18 09:26:42 +00:00
|
|
|
#include <assert.h>
|
2013-09-18 09:16:03 +00:00
|
|
|
|
2013-04-16 08:23:03 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include "bitstream.h"
|
2014-06-05 12:54:58 +00:00
|
|
|
#include "image.h"
|
2014-06-16 09:13:41 +00:00
|
|
|
#include "strategies/strategies-picture.h"
|
2014-11-20 16:38:54 +00:00
|
|
|
#include "strategies/strategies-ipol.h"
|
2013-04-16 12:10:43 +00:00
|
|
|
#include "intra.h"
|
2013-09-05 12:02:53 +00:00
|
|
|
#include "inter.h"
|
2013-04-16 08:23:03 +00:00
|
|
|
#include "filter.h"
|
2014-04-04 10:09:02 +00:00
|
|
|
#include "rdo.h"
|
2014-05-13 06:33:05 +00:00
|
|
|
#include "transform.h"
|
2014-05-15 13:20:35 +00:00
|
|
|
#include "encoder.h"
|
2014-03-06 16:14:01 +00:00
|
|
|
|
2013-09-16 16:18:24 +00:00
|
|
|
// Temporarily for debugging.
|
2013-10-22 16:00:36 +00:00
|
|
|
#define SEARCH_MV_FULL_RADIUS 0
|
2013-09-16 16:18:24 +00:00
|
|
|
|
2013-09-30 07:47:05 +00:00
|
|
|
#define IN_FRAME(x, y, width, height, block_width, block_height) \
|
|
|
|
((x) >= 0 && (y) >= 0 \
|
|
|
|
&& (x) + (block_width) <= (width) \
|
|
|
|
&& (y) + (block_height) <= (height))
|
2013-09-25 14:47:40 +00:00
|
|
|
|
2015-01-14 12:11:12 +00:00
|
|
|
// Cost treshold for doing intra search in inter frames with --rd=0.
|
|
|
|
#ifndef INTRA_TRESHOLD
|
|
|
|
# define INTRA_TRESHOLD 20
|
|
|
|
#endif
|
|
|
|
|
2014-10-15 17:17:38 +00:00
|
|
|
// Extra cost for CU split.
|
|
|
|
// Compensates for missing or incorrect bit costs. Must be recalculated if
|
|
|
|
// bits are added or removed from cu-tree search.
|
|
|
|
#ifndef CU_COST
|
|
|
|
# define CU_COST 3
|
2014-09-17 09:09:15 +00:00
|
|
|
#endif
|
2014-10-15 17:17:38 +00:00
|
|
|
// Disable early cu-split pruning.
|
2014-09-17 09:09:15 +00:00
|
|
|
#ifndef FULL_CU_SPLIT_SEARCH
|
|
|
|
# define FULL_CU_SPLIT_SEARCH false
|
|
|
|
#endif
|
2014-10-15 17:17:38 +00:00
|
|
|
// Modify weight of luma SSD.
|
|
|
|
#ifndef LUMA_MULT
|
|
|
|
# define LUMA_MULT 0.8
|
2014-10-06 18:46:12 +00:00
|
|
|
#endif
|
2014-10-15 17:17:38 +00:00
|
|
|
// Modify weight of chroma SSD.
|
|
|
|
#ifndef CHROMA_MULT
|
|
|
|
# define CHROMA_MULT 1.5
|
2014-10-06 18:46:12 +00:00
|
|
|
#endif
|
2014-10-15 17:17:38 +00:00
|
|
|
// Normalize SAD for comparison against SATD to estimate transform skip
|
|
|
|
// for 4x4 blocks.
|
|
|
|
#ifndef TRSKIP_RATIO
|
|
|
|
# define TRSKIP_RATIO 1.7
|
2014-10-08 14:39:55 +00:00
|
|
|
#endif
|
2014-10-06 18:46:12 +00:00
|
|
|
|
2014-02-21 13:00:20 +00:00
|
|
|
/**
|
2013-10-08 09:30:25 +00:00
|
|
|
* This is used in the hexagon_search to select 3 points to search.
|
2014-02-21 13:00:20 +00:00
|
|
|
*
|
2013-10-08 09:30:25 +00:00
|
|
|
* The start of the hexagonal pattern has been repeated at the end so that
|
|
|
|
* the indices between 1-6 can be used as the start of a 3-point list of new
|
|
|
|
* points to search.
|
2014-02-21 13:00:20 +00:00
|
|
|
*
|
2013-10-08 09:30:25 +00:00
|
|
|
* 6 o-o 1 / 7
|
|
|
|
* / \
|
|
|
|
* 5 o 0 o 2 / 8
|
|
|
|
* \ /
|
|
|
|
* 4 o-o 3
|
|
|
|
*/
|
2015-03-04 11:32:11 +00:00
|
|
|
const vector2d_t large_hexbs[10] = {
|
2013-10-08 09:30:25 +00:00
|
|
|
{ 0, 0 },
|
|
|
|
{ 1, -2 }, { 2, 0 }, { 1, 2 }, { -1, 2 }, { -2, 0 }, { -1, -2 },
|
|
|
|
{ 1, -2 }, { 2, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is used as the last step of the hexagon search.
|
|
|
|
*/
|
2015-03-04 11:32:11 +00:00
|
|
|
const vector2d_t small_hexbs[5] = {
|
2013-10-08 09:30:25 +00:00
|
|
|
{ 0, 0 },
|
|
|
|
{ -1, -1 }, { -1, 0 }, { 1, 0 }, { 1, 1 }
|
|
|
|
};
|
|
|
|
|
2014-05-14 01:42:02 +00:00
|
|
|
/*
|
|
|
|
* 6 7 8
|
|
|
|
* 3 4 5
|
|
|
|
* 0 1 2
|
|
|
|
*/
|
2015-03-04 11:32:11 +00:00
|
|
|
const vector2d_t square[9] = {
|
2014-05-14 01:42:02 +00:00
|
|
|
{ -1, 1 },
|
|
|
|
{ 0, 1 }, { 1, 1 }, { -1, 0 }, { 0, 0 }, { 1, 0 }, { -1, -1 },
|
|
|
|
{ 0, -1 }, { 1, -1 }
|
|
|
|
};
|
2014-03-06 16:14:01 +00:00
|
|
|
|
2014-03-07 13:05:39 +00:00
|
|
|
static uint32_t get_ep_ex_golomb_bitcost(uint32_t symbol, uint32_t count)
|
2013-10-22 10:08:08 +00:00
|
|
|
{
|
2014-03-07 13:05:39 +00:00
|
|
|
int32_t num_bins = 0;
|
|
|
|
while (symbol >= (uint32_t)(1 << count)) {
|
|
|
|
++num_bins;
|
|
|
|
symbol -= 1 << count;
|
|
|
|
++count;
|
|
|
|
}
|
2014-03-10 10:08:09 +00:00
|
|
|
num_bins ++;
|
2014-03-07 13:05:39 +00:00
|
|
|
|
|
|
|
return num_bins;
|
|
|
|
}
|
2013-10-22 10:08:08 +00:00
|
|
|
|
2015-03-04 11:32:11 +00:00
|
|
|
static uint32_t get_mvd_coding_cost(vector2d_t *mvd)
|
2014-03-07 13:05:39 +00:00
|
|
|
{
|
|
|
|
uint32_t bitcost = 0;
|
|
|
|
const int32_t mvd_hor = mvd->x;
|
|
|
|
const int32_t mvd_ver = mvd->y;
|
|
|
|
const int8_t hor_abs_gr0 = mvd_hor != 0;
|
|
|
|
const int8_t ver_abs_gr0 = mvd_ver != 0;
|
|
|
|
const uint32_t mvd_hor_abs = abs(mvd_hor);
|
|
|
|
const uint32_t mvd_ver_abs = abs(mvd_ver);
|
|
|
|
|
|
|
|
// Greater than 0 for x/y
|
|
|
|
bitcost += 2;
|
|
|
|
|
|
|
|
if (hor_abs_gr0) {
|
|
|
|
if (mvd_hor_abs > 1) {
|
2014-03-10 10:08:09 +00:00
|
|
|
bitcost += get_ep_ex_golomb_bitcost(mvd_hor_abs-2, 1) - 2; // TODO: tune the costs
|
2014-03-07 13:05:39 +00:00
|
|
|
}
|
2014-03-10 10:08:09 +00:00
|
|
|
// Greater than 1 + sign
|
2014-03-07 13:05:39 +00:00
|
|
|
bitcost += 2;
|
2013-10-22 10:08:08 +00:00
|
|
|
}
|
2014-03-07 13:05:39 +00:00
|
|
|
|
|
|
|
if (ver_abs_gr0) {
|
|
|
|
if (mvd_ver_abs > 1) {
|
2014-03-10 10:08:09 +00:00
|
|
|
bitcost += get_ep_ex_golomb_bitcost(mvd_ver_abs-2, 1) - 2; // TODO: tune the costs
|
2014-03-07 13:05:39 +00:00
|
|
|
}
|
2014-03-10 10:08:09 +00:00
|
|
|
// Greater than 1 + sign
|
2014-03-07 13:05:39 +00:00
|
|
|
bitcost += 2;
|
2013-10-22 10:08:08 +00:00
|
|
|
}
|
|
|
|
|
2014-03-07 13:05:39 +00:00
|
|
|
return bitcost;
|
|
|
|
}
|
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
static int calc_mvd_cost(const encoder_state_t * const state, int x, int y, int mv_shift,
|
2015-03-18 08:03:06 +00:00
|
|
|
int16_t mv_cand[2][2], inter_merge_cand_t merge_cand[MRG_MAX_NUM_CANDS],
|
2014-03-11 09:35:02 +00:00
|
|
|
int16_t num_cand,int32_t ref_idx, uint32_t *bitcost)
|
2014-03-07 13:05:39 +00:00
|
|
|
{
|
|
|
|
uint32_t temp_bitcost = 0;
|
|
|
|
uint32_t merge_idx;
|
|
|
|
int cand1_cost,cand2_cost;
|
2015-03-04 11:32:11 +00:00
|
|
|
vector2d_t mvd_temp1, mvd_temp2;
|
2014-03-07 13:05:39 +00:00
|
|
|
int8_t merged = 0;
|
|
|
|
int8_t cur_mv_cand = 0;
|
|
|
|
|
2014-05-14 01:42:02 +00:00
|
|
|
x <<= mv_shift;
|
|
|
|
y <<= mv_shift;
|
2014-03-07 13:05:39 +00:00
|
|
|
|
|
|
|
// Check every candidate to find a match
|
2014-04-04 08:08:34 +00:00
|
|
|
for(merge_idx = 0; merge_idx < (uint32_t)num_cand; merge_idx++) {
|
2015-03-24 07:38:24 +00:00
|
|
|
if (merge_cand[merge_idx].dir == 3) continue;
|
|
|
|
if (merge_cand[merge_idx].mv[merge_cand[merge_idx].dir - 1][0] == x &&
|
2015-03-18 08:03:06 +00:00
|
|
|
merge_cand[merge_idx].mv[merge_cand[merge_idx].dir - 1][1] == y &&
|
2015-03-30 11:40:29 +00:00
|
|
|
merge_cand[merge_idx].ref[merge_cand[merge_idx].dir - 1] == ref_idx) {
|
2014-03-10 10:08:09 +00:00
|
|
|
temp_bitcost += merge_idx;
|
2014-03-07 13:05:39 +00:00
|
|
|
merged = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check mvd cost only if mv is not merged
|
|
|
|
if(!merged) {
|
|
|
|
mvd_temp1.x = x - mv_cand[0][0];
|
|
|
|
mvd_temp1.y = y - mv_cand[0][1];
|
|
|
|
cand1_cost = get_mvd_coding_cost(&mvd_temp1);
|
|
|
|
|
|
|
|
mvd_temp2.x = x - mv_cand[1][0];
|
|
|
|
mvd_temp2.y = y - mv_cand[1][1];
|
|
|
|
cand2_cost = get_mvd_coding_cost(&mvd_temp2);
|
|
|
|
|
|
|
|
// Select candidate 1 if it has lower cost
|
|
|
|
if (cand2_cost < cand1_cost) {
|
|
|
|
cur_mv_cand = 1;
|
|
|
|
}
|
|
|
|
temp_bitcost += cur_mv_cand ? cand2_cost : cand1_cost;
|
|
|
|
}
|
2014-03-11 09:35:02 +00:00
|
|
|
*bitcost = temp_bitcost;
|
2015-03-04 15:00:23 +00:00
|
|
|
return temp_bitcost*(int32_t)(state->global->cur_lambda_cost_sqrt+0.5);
|
2013-10-22 10:08:08 +00:00
|
|
|
}
|
2013-10-22 08:40:24 +00:00
|
|
|
|
2015-03-12 15:54:21 +00:00
|
|
|
unsigned tz_pattern_search(const encoder_state_t * const state, const image_t *pic, const image_t *ref, unsigned pattern_type,
|
|
|
|
const vector2d_t *orig, const int iDist, vector2d_t *mv, unsigned best_cost, int *best_dist,
|
2015-03-20 09:28:56 +00:00
|
|
|
int16_t mv_cand[2][2], inter_merge_cand_t merge_cand[MRG_MAX_NUM_CANDS], int16_t num_cand, int32_t ref_idx, uint32_t *best_bitcost,
|
2015-02-11 16:51:21 +00:00
|
|
|
int block_width, int max_lcu_below)
|
2015-01-14 12:54:09 +00:00
|
|
|
{
|
|
|
|
int n_points;
|
|
|
|
int best_index = -1;
|
|
|
|
int i;
|
2015-03-08 17:19:23 +00:00
|
|
|
|
2015-03-12 15:54:21 +00:00
|
|
|
vector2d_t mv_best = { 0, 0 };
|
2015-01-14 12:54:09 +00:00
|
|
|
|
2015-05-04 14:11:11 +00:00
|
|
|
assert(pattern_type < 4);
|
|
|
|
|
2015-02-23 14:59:48 +00:00
|
|
|
//implemented search patterns
|
2015-03-12 15:54:21 +00:00
|
|
|
vector2d_t pattern[4][8] = {
|
2015-02-23 14:59:48 +00:00
|
|
|
//diamond (8 points)
|
2015-02-02 17:14:45 +00:00
|
|
|
//[ ][ ][ ][ ][1][ ][ ][ ][ ]
|
|
|
|
//[ ][ ][ ][ ][ ][ ][ ][ ][ ]
|
|
|
|
//[ ][ ][8][ ][ ][ ][5][ ][ ]
|
|
|
|
//[ ][ ][ ][ ][ ][ ][ ][ ][ ]
|
|
|
|
//[4][ ][ ][ ][o][ ][ ][ ][2]
|
|
|
|
//[ ][ ][ ][ ][ ][ ][ ][ ][ ]
|
|
|
|
//[ ][ ][7][ ][ ][ ][6][ ][ ]
|
|
|
|
//[ ][ ][ ][ ][ ][ ][ ][ ][ ]
|
|
|
|
//[ ][ ][ ][ ][3][ ][ ][ ][ ]
|
|
|
|
{
|
|
|
|
{ 0, iDist }, { iDist, 0 }, { 0, -iDist }, { -iDist, 0 },
|
|
|
|
{ iDist / 2, iDist / 2 }, { iDist / 2, -iDist / 2 }, { -iDist / 2, -iDist / 2 }, { -iDist / 2, iDist / 2 }
|
|
|
|
},
|
|
|
|
|
2015-02-23 14:59:48 +00:00
|
|
|
//square (8 points)
|
2015-02-02 17:14:45 +00:00
|
|
|
//[8][ ][ ][ ][1][ ][ ][ ][2]
|
|
|
|
//[ ][ ][ ][ ][ ][ ][ ][ ][ ]
|
|
|
|
//[ ][ ][ ][ ][ ][ ][ ][ ][ ]
|
|
|
|
//[ ][ ][ ][ ][ ][ ][ ][ ][ ]
|
|
|
|
//[7][ ][ ][ ][o][ ][ ][ ][3]
|
|
|
|
//[ ][ ][ ][ ][ ][ ][ ][ ][ ]
|
|
|
|
//[ ][ ][ ][ ][ ][ ][ ][ ][ ]
|
|
|
|
//[ ][ ][ ][ ][ ][ ][ ][ ][ ]
|
|
|
|
//[6][ ][ ][ ][5][ ][ ][ ][4]
|
|
|
|
{
|
|
|
|
{ 0, iDist }, { iDist, iDist }, { iDist, 0 }, { iDist, -iDist }, { 0, -iDist },
|
|
|
|
{ -iDist, -iDist }, { -iDist, 0 }, { -iDist, iDist }
|
|
|
|
},
|
|
|
|
|
2015-02-23 14:59:48 +00:00
|
|
|
//octagon (8 points)
|
2015-03-08 17:19:23 +00:00
|
|
|
//[ ][ ][5][ ][ ][ ][1][ ][ ]
|
2015-02-02 17:14:45 +00:00
|
|
|
//[ ][ ][ ][ ][ ][ ][ ][ ][ ]
|
|
|
|
//[ ][ ][ ][ ][ ][ ][ ][ ][2]
|
2015-03-08 17:19:23 +00:00
|
|
|
//[4][ ][ ][ ][ ][ ][ ][ ][ ]
|
2015-02-02 17:14:45 +00:00
|
|
|
//[ ][ ][ ][ ][o][ ][ ][ ][ ]
|
|
|
|
//[ ][ ][ ][ ][ ][ ][ ][ ][ ]
|
2015-03-08 17:19:23 +00:00
|
|
|
//[8][ ][ ][ ][ ][ ][ ][ ][6]
|
2015-02-02 17:14:45 +00:00
|
|
|
//[ ][ ][ ][ ][ ][ ][ ][ ][ ]
|
|
|
|
//[ ][ ][7][ ][ ][ ][3][ ][ ]
|
|
|
|
{
|
2015-03-08 17:19:23 +00:00
|
|
|
{ iDist / 2, iDist }, { iDist, iDist / 2 }, { iDist / 2, -iDist }, { -iDist, iDist / 2 },
|
|
|
|
{ -iDist / 2, iDist }, { iDist, -iDist / 2 }, { -iDist / 2, -iDist }, { -iDist, -iDist / 2 }
|
2015-02-02 17:14:45 +00:00
|
|
|
},
|
2015-02-23 14:59:48 +00:00
|
|
|
|
|
|
|
//hexagon (6 points)
|
2015-03-08 17:19:23 +00:00
|
|
|
//[ ][ ][5][ ][ ][ ][1][ ][ ]
|
2015-02-23 14:59:48 +00:00
|
|
|
//[ ][ ][ ][ ][ ][ ][ ][ ][ ]
|
|
|
|
//[ ][ ][ ][ ][ ][ ][ ][ ][ ]
|
|
|
|
//[ ][ ][ ][ ][ ][ ][ ][ ][ ]
|
|
|
|
//[4][ ][ ][ ][o][ ][ ][ ][2]
|
|
|
|
//[ ][ ][ ][ ][ ][ ][ ][ ][ ]
|
|
|
|
//[ ][ ][ ][ ][ ][ ][ ][ ][ ]
|
|
|
|
//[ ][ ][ ][ ][ ][ ][ ][ ][ ]
|
|
|
|
//[ ][ ][6][ ][ ][ ][3][ ][ ]
|
|
|
|
{
|
2015-03-08 17:19:23 +00:00
|
|
|
{ iDist / 2, iDist }, { iDist, 0 }, { iDist / 2, -iDist }, { -iDist, 0 },
|
2015-02-23 14:59:48 +00:00
|
|
|
{ iDist / 2, iDist }, { -iDist / 2, -iDist }, { 0, 0 }, { 0, 0 }
|
|
|
|
}
|
|
|
|
|
2015-01-14 12:54:09 +00:00
|
|
|
};
|
|
|
|
|
2015-02-23 14:59:48 +00:00
|
|
|
//set the number of points to be checked
|
|
|
|
if (iDist == 1)
|
2015-01-14 12:54:09 +00:00
|
|
|
{
|
2015-02-23 14:59:48 +00:00
|
|
|
switch (pattern_type)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
n_points = 4;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
n_points = 4;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
n_points = 4;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
n_points = 8;
|
|
|
|
break;
|
|
|
|
};
|
2015-01-14 12:54:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-23 14:59:48 +00:00
|
|
|
switch (pattern_type)
|
|
|
|
{
|
|
|
|
case 3:
|
|
|
|
n_points = 6;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
n_points = 8;
|
|
|
|
break;
|
|
|
|
};
|
2015-01-14 12:54:09 +00:00
|
|
|
}
|
|
|
|
|
2015-02-23 14:59:48 +00:00
|
|
|
//compute SAD values for all chosen points
|
2015-01-14 12:54:09 +00:00
|
|
|
for (i = 0; i < n_points; i++)
|
|
|
|
{
|
2015-03-12 15:54:21 +00:00
|
|
|
vector2d_t *current = &pattern[pattern_type][i];
|
2015-01-14 12:54:09 +00:00
|
|
|
unsigned cost;
|
|
|
|
uint32_t bitcost;
|
|
|
|
|
|
|
|
{
|
|
|
|
PERFORMANCE_MEASURE_START(_DEBUG_PERF_SEARCH_PIXELS);
|
|
|
|
cost = image_calc_sad(pic, ref, orig->x, orig->y,
|
2015-03-12 15:54:21 +00:00
|
|
|
(state->tile->lcu_offset_x * LCU_WIDTH) + orig->x + mv->x + current->x,
|
|
|
|
(state->tile->lcu_offset_y * LCU_WIDTH) + orig->y + mv->y + current->y,
|
2015-01-14 12:54:09 +00:00
|
|
|
block_width, block_width, max_lcu_below);
|
2015-03-12 15:54:21 +00:00
|
|
|
cost += calc_mvd_cost(state, mv->x + current->x, mv->y + current->y, 2, mv_cand, merge_cand, num_cand, ref_idx, &bitcost);
|
2015-01-14 12:54:09 +00:00
|
|
|
|
2015-03-12 15:54:21 +00:00
|
|
|
PERFORMANCE_MEASURE_END(_DEBUG_PERF_SEARCH_PIXELS, state->encoder_control->threadqueue, "type=sad,step=large_hexbs,frame=%d,tile=%d,ref=%d,px_x=%d-%d,px_y=%d-%d,ref_px_x=%d-%d,ref_px_y=%d-%d", state->global->frame, state->tile->id, ref->poc - state->global->poc, orig->x, orig->x + block_width, orig->y, orig->y + block_width,
|
|
|
|
(state->tile->lcu_offset_x * LCU_WIDTH) + orig->x + mv->x + current->x,
|
|
|
|
(state->tile->lcu_offset_x * LCU_WIDTH) + orig->x + mv->x + current->x + block_width,
|
|
|
|
(state->tile->lcu_offset_y * LCU_WIDTH) + orig->y + mv->y + current->y,
|
|
|
|
(state->tile->lcu_offset_y * LCU_WIDTH) + orig->y + mv->y + current->y + block_width);
|
2015-01-14 12:54:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cost < best_cost)
|
|
|
|
{
|
|
|
|
best_cost = cost;
|
|
|
|
*best_bitcost = bitcost;
|
|
|
|
best_index = i;
|
|
|
|
}
|
2015-02-02 17:14:45 +00:00
|
|
|
|
2015-01-14 12:54:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (best_index >= 0)
|
|
|
|
{
|
2015-03-08 17:19:23 +00:00
|
|
|
mv_best = pattern[pattern_type][best_index];
|
2015-01-14 12:54:09 +00:00
|
|
|
*best_dist = iDist;
|
|
|
|
}
|
2015-03-08 17:19:23 +00:00
|
|
|
|
|
|
|
mv->x += mv_best.x;
|
|
|
|
mv->y += mv_best.y;
|
2015-01-14 12:54:09 +00:00
|
|
|
|
|
|
|
return best_cost;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-03-12 15:54:21 +00:00
|
|
|
unsigned tz_raster_search(const encoder_state_t * const state, const image_t *pic, const image_t *ref,
|
|
|
|
const vector2d_t *orig, vector2d_t *mv, unsigned best_cost,
|
2015-03-20 09:28:56 +00:00
|
|
|
int16_t mv_cand[2][2], inter_merge_cand_t merge_cand[MRG_MAX_NUM_CANDS], int16_t num_cand, int32_t ref_idx, uint32_t *best_bitcost,
|
2015-02-11 16:51:21 +00:00
|
|
|
int block_width, int iSearchRange, int iRaster, int max_lcu_below)
|
2015-01-14 12:54:09 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int k;
|
|
|
|
|
2015-03-12 15:54:21 +00:00
|
|
|
vector2d_t mv_best = { 0, 0 };
|
2015-03-08 17:19:23 +00:00
|
|
|
|
2015-02-23 14:59:48 +00:00
|
|
|
//compute SAD values for every point in the iRaster downsampled version of the current search area
|
2015-01-14 12:54:09 +00:00
|
|
|
for (i = iSearchRange; i >= -iSearchRange; i -= iRaster)
|
|
|
|
{
|
|
|
|
for (k = -iSearchRange; k <= iSearchRange; k += iRaster)
|
|
|
|
{
|
2015-03-12 15:54:21 +00:00
|
|
|
vector2d_t current = { k, i };
|
2015-01-14 12:54:09 +00:00
|
|
|
unsigned cost;
|
|
|
|
uint32_t bitcost;
|
|
|
|
|
|
|
|
{
|
|
|
|
PERFORMANCE_MEASURE_START(_DEBUG_PERF_SEARCH_PIXELS);
|
|
|
|
cost = image_calc_sad(pic, ref, orig->x, orig->y,
|
2015-03-12 15:54:21 +00:00
|
|
|
(state->tile->lcu_offset_x * LCU_WIDTH) + orig->x + mv->x + k,
|
|
|
|
(state->tile->lcu_offset_y * LCU_WIDTH) + orig->y + mv->y + i,
|
2015-01-14 12:54:09 +00:00
|
|
|
block_width, block_width, max_lcu_below);
|
2015-03-12 15:54:21 +00:00
|
|
|
cost += calc_mvd_cost(state, mv->x + k, mv->y + i, 2, mv_cand, merge_cand, num_cand, ref_idx, &bitcost);
|
2015-01-14 12:54:09 +00:00
|
|
|
|
2015-03-12 15:54:21 +00:00
|
|
|
PERFORMANCE_MEASURE_END(_DEBUG_PERF_SEARCH_PIXELS, state->encoder_control->threadqueue, "type=sad,step=large_hexbs,frame=%d,tile=%d,ref=%d,px_x=%d-%d,px_y=%d-%d,ref_px_x=%d-%d,ref_px_y=%d-%d", state->global->frame, state->tile->id, ref->poc - state->global->poc, orig->x, orig->x + block_width, orig->y, orig->y + block_width,
|
|
|
|
(state->tile->lcu_offset_x * LCU_WIDTH) + orig->x + mv->x + k,
|
|
|
|
(state->tile->lcu_offset_x * LCU_WIDTH) + orig->x + mv->x + k + block_width,
|
|
|
|
(state->tile->lcu_offset_y * LCU_WIDTH) + orig->y + mv->y + i,
|
|
|
|
(state->tile->lcu_offset_y * LCU_WIDTH) + orig->y + mv->y + i + block_width);
|
2015-01-14 12:54:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cost < best_cost)
|
|
|
|
{
|
|
|
|
best_cost = cost;
|
|
|
|
*best_bitcost = bitcost;
|
2015-03-08 17:19:23 +00:00
|
|
|
mv_best = current;
|
2015-01-14 12:54:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2015-03-08 17:19:23 +00:00
|
|
|
|
|
|
|
mv->x += mv_best.x;
|
|
|
|
mv->y += mv_best.y;
|
2015-01-14 12:54:09 +00:00
|
|
|
|
|
|
|
return best_cost;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-03-12 15:54:21 +00:00
|
|
|
static unsigned tz_search(const encoder_state_t * const state, unsigned depth,
|
|
|
|
const image_t *pic, const image_t *ref,
|
|
|
|
const vector2d_t *orig, vector2d_t *mv_in_out,
|
2015-03-20 09:28:56 +00:00
|
|
|
int16_t mv_cand[2][2], inter_merge_cand_t merge_cand[MRG_MAX_NUM_CANDS],
|
2015-01-14 12:54:09 +00:00
|
|
|
int16_t num_cand, int32_t ref_idx, uint32_t *bitcost_out)
|
|
|
|
{
|
|
|
|
|
2015-02-11 16:51:21 +00:00
|
|
|
//TZ parameters
|
2015-02-23 14:59:48 +00:00
|
|
|
int iSearchRange = 96; // search range for each stage
|
|
|
|
int iRaster = 5; // search distance limit and downsampling factor for step 3
|
2015-02-25 13:28:45 +00:00
|
|
|
unsigned step2_type = 0; // search patterns for steps 2 and 4
|
|
|
|
unsigned step4_type = 0;
|
2015-02-11 16:51:21 +00:00
|
|
|
bool bRasterRefinementEnable = true; // enable step 4 mode 1
|
|
|
|
bool bStarRefinementEnable = false; // enable step 4 mode 2 (only one mode will be executed)
|
|
|
|
|
2015-01-14 12:54:09 +00:00
|
|
|
int block_width = CU_WIDTH_FROM_DEPTH(depth);
|
|
|
|
|
2015-03-12 15:54:21 +00:00
|
|
|
vector2d_t mv = { mv_in_out->x >> 2, mv_in_out->y >> 2 };
|
2015-01-14 12:54:09 +00:00
|
|
|
|
|
|
|
unsigned best_cost = UINT32_MAX;
|
|
|
|
uint32_t best_bitcost = 0;
|
|
|
|
int iDist;
|
|
|
|
int best_dist = 0;
|
2015-03-08 17:19:23 +00:00
|
|
|
unsigned best_index = num_cand;
|
2015-02-11 16:51:21 +00:00
|
|
|
int max_lcu_below = -1;
|
|
|
|
|
2015-03-12 15:54:21 +00:00
|
|
|
if (state->encoder_control->owf) {
|
2015-02-23 14:59:48 +00:00
|
|
|
max_lcu_below = 1;
|
|
|
|
}
|
|
|
|
|
2015-03-08 17:19:23 +00:00
|
|
|
//step 1, compare (0,0) vector to predicted vectors
|
|
|
|
|
|
|
|
// Check whatever input vector we got, unless its (0, 0) which will be checked later.
|
2015-03-12 15:57:17 +00:00
|
|
|
if (mv.x || mv.y)
|
2015-02-11 16:51:21 +00:00
|
|
|
{
|
2015-03-08 17:19:23 +00:00
|
|
|
PERFORMANCE_MEASURE_START(_DEBUG_PERF_SEARCH_PIXELS);
|
|
|
|
|
|
|
|
best_cost = image_calc_sad(pic, ref, orig->x, orig->y,
|
2015-03-12 15:54:21 +00:00
|
|
|
(state->tile->lcu_offset_x * LCU_WIDTH) + orig->x + mv.x,
|
|
|
|
(state->tile->lcu_offset_y * LCU_WIDTH) + orig->y + mv.y,
|
2015-03-08 17:19:23 +00:00
|
|
|
block_width, block_width, max_lcu_below);
|
2015-03-12 15:54:21 +00:00
|
|
|
best_cost += calc_mvd_cost(state, mv.x, mv.y, 2, mv_cand, merge_cand, num_cand, ref_idx, &best_bitcost);
|
2015-03-08 17:19:23 +00:00
|
|
|
|
2015-03-12 15:54:21 +00:00
|
|
|
PERFORMANCE_MEASURE_END(_DEBUG_PERF_SEARCH_PIXELS, state->encoder_control->threadqueue, "type=sad,step=large_hexbs,frame=%d,tile=%d,ref=%d,px_x=%d-%d,px_y=%d-%d,ref_px_x=%d-%d,ref_px_y=%d-%d", state->global->frame, state->tile->id, ref->poc - state->global->poc, orig->x, orig->x + block_width, orig->y, orig->y + block_width,
|
|
|
|
(state->tile->lcu_offset_x * LCU_WIDTH) + orig->x + mv.x,
|
|
|
|
(state->tile->lcu_offset_x * LCU_WIDTH) + orig->x + mv.x + block_width,
|
|
|
|
(state->tile->lcu_offset_y * LCU_WIDTH) + orig->y + mv.y,
|
|
|
|
(state->tile->lcu_offset_y * LCU_WIDTH) + orig->y + mv.y + block_width);
|
2015-02-11 16:51:21 +00:00
|
|
|
}
|
2015-01-14 12:54:09 +00:00
|
|
|
|
2015-03-08 17:19:23 +00:00
|
|
|
int i;
|
|
|
|
// Select starting point from among merge candidates. These should include
|
|
|
|
// both mv_cand vectors and (0, 0).
|
|
|
|
for (i = 0; i < num_cand; ++i)
|
2015-02-11 16:51:21 +00:00
|
|
|
{
|
2015-03-23 13:17:41 +00:00
|
|
|
if (merge_cand[i].dir == 3) continue;
|
|
|
|
mv.x = merge_cand[i].mv[merge_cand[i].dir - 1][0] >> 2;
|
2015-03-20 09:28:56 +00:00
|
|
|
mv.y = merge_cand[i].mv[merge_cand[i].dir - 1][1] >> 2;
|
2015-02-11 16:51:21 +00:00
|
|
|
|
2015-03-08 17:19:23 +00:00
|
|
|
PERFORMANCE_MEASURE_START(_DEBUG_PERF_SEARCH_PIXELS);
|
2015-02-11 16:51:21 +00:00
|
|
|
|
2015-03-08 17:19:23 +00:00
|
|
|
uint32_t bitcost;
|
|
|
|
unsigned cost = image_calc_sad(pic, ref, orig->x, orig->y,
|
2015-03-12 15:54:21 +00:00
|
|
|
(state->tile->lcu_offset_x * LCU_WIDTH) + orig->x + mv.x,
|
|
|
|
(state->tile->lcu_offset_y * LCU_WIDTH) + orig->y + mv.y,
|
2015-03-08 17:19:23 +00:00
|
|
|
block_width, block_width, max_lcu_below);
|
2015-03-12 15:54:21 +00:00
|
|
|
cost += calc_mvd_cost(state, mv.x, mv.y, 2, mv_cand, merge_cand, num_cand, ref_idx, &bitcost);
|
2015-03-08 17:19:23 +00:00
|
|
|
|
2015-03-12 15:54:21 +00:00
|
|
|
PERFORMANCE_MEASURE_END(_DEBUG_PERF_SEARCH_PIXELS, state->encoder_control->threadqueue, "type=sad,step=large_hexbs,frame=%d,tile=%d,ref=%d,px_x=%d-%d,px_y=%d-%d,ref_px_x=%d-%d,ref_px_y=%d-%d", state->global->frame, state->tile->id, ref->poc - state->global->poc, orig->x, orig->x + block_width, orig->y, orig->y + block_width,
|
|
|
|
(state->tile->lcu_offset_x * LCU_WIDTH) + orig->x + mv.x,
|
|
|
|
(state->tile->lcu_offset_x * LCU_WIDTH) + orig->x + mv.x + block_width,
|
|
|
|
(state->tile->lcu_offset_y * LCU_WIDTH) + orig->y + mv.y,
|
|
|
|
(state->tile->lcu_offset_y * LCU_WIDTH) + orig->y + mv.y + block_width);
|
2015-03-08 17:19:23 +00:00
|
|
|
|
|
|
|
if (cost < best_cost) {
|
|
|
|
best_cost = cost;
|
|
|
|
best_index = i;
|
|
|
|
best_bitcost = bitcost;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (best_index < (unsigned)num_cand) {
|
2015-03-20 09:28:56 +00:00
|
|
|
mv.x = merge_cand[best_index].mv[merge_cand[best_index].dir - 1][0] >> 2;
|
|
|
|
mv.y = merge_cand[best_index].mv[merge_cand[best_index].dir - 1][1] >> 2;
|
2015-03-08 17:19:23 +00:00
|
|
|
} else {
|
|
|
|
mv.x = mv_in_out->x >> 2;
|
|
|
|
mv.y = mv_in_out->y >> 2;
|
2015-02-11 16:51:21 +00:00
|
|
|
}
|
2015-01-14 12:54:09 +00:00
|
|
|
|
2015-03-08 17:19:23 +00:00
|
|
|
//step 2, grid search
|
2015-01-14 12:54:09 +00:00
|
|
|
for (iDist = 1; iDist <= iSearchRange; iDist *= 2)
|
|
|
|
{
|
2015-03-12 15:54:21 +00:00
|
|
|
best_cost = tz_pattern_search(state, pic, ref, step2_type, orig, iDist, &mv, best_cost, &best_dist,
|
2015-02-11 16:51:21 +00:00
|
|
|
mv_cand, merge_cand, num_cand, ref_idx, &best_bitcost, block_width, max_lcu_below);
|
2015-01-14 12:54:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//step 3, raster scan
|
|
|
|
if (best_dist > iRaster)
|
|
|
|
{
|
|
|
|
best_dist = iRaster;
|
|
|
|
|
2015-03-12 15:54:21 +00:00
|
|
|
best_cost = tz_raster_search(state, pic, ref, orig, &mv, best_cost, mv_cand, merge_cand,
|
2015-02-11 16:51:21 +00:00
|
|
|
num_cand, ref_idx, &best_bitcost, block_width, iSearchRange, iRaster, max_lcu_below);
|
2015-01-14 12:54:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//step 4
|
|
|
|
|
|
|
|
//raster refinement
|
|
|
|
if (bRasterRefinementEnable && best_dist > 0)
|
|
|
|
{
|
|
|
|
iDist = best_dist >> 1;
|
2015-02-02 17:14:45 +00:00
|
|
|
while (iDist > 0)
|
2015-01-14 12:54:09 +00:00
|
|
|
{
|
2015-03-12 15:54:21 +00:00
|
|
|
best_cost = tz_pattern_search(state, pic, ref, step4_type, orig, iDist, &mv, best_cost, &best_dist,
|
2015-02-11 16:51:21 +00:00
|
|
|
mv_cand, merge_cand, num_cand, ref_idx, &best_bitcost, block_width, max_lcu_below);
|
2015-01-14 12:54:09 +00:00
|
|
|
|
|
|
|
iDist = iDist >> 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//star refinement (repeat step 2 for the current starting point)
|
|
|
|
if (bStarRefinementEnable && best_dist > 0)
|
|
|
|
{
|
|
|
|
for (iDist = 1; iDist <= iSearchRange; iDist *= 2)
|
|
|
|
{
|
2015-03-12 15:54:21 +00:00
|
|
|
best_cost = tz_pattern_search(state, pic, ref, step4_type, orig, iDist, &mv, best_cost, &best_dist,
|
2015-02-11 16:51:21 +00:00
|
|
|
mv_cand, merge_cand, num_cand, ref_idx, &best_bitcost, block_width, max_lcu_below);
|
2015-01-14 12:54:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-08 17:19:23 +00:00
|
|
|
mv.x = mv.x << 2;
|
|
|
|
mv.y = mv.y << 2;
|
2015-01-14 12:54:09 +00:00
|
|
|
|
2015-03-08 17:19:23 +00:00
|
|
|
*mv_in_out = mv;
|
2015-01-14 12:54:09 +00:00
|
|
|
*bitcost_out = best_bitcost;
|
|
|
|
|
|
|
|
return best_cost;
|
|
|
|
}
|
2014-03-06 16:14:01 +00:00
|
|
|
|
2013-10-22 08:40:24 +00:00
|
|
|
/**
|
|
|
|
* \brief Do motion search using the HEXBS algorithm.
|
|
|
|
*
|
|
|
|
* \param depth log2 depth of the search
|
|
|
|
* \param pic Picture motion vector is searched for.
|
|
|
|
* \param ref Picture motion vector is searched from.
|
|
|
|
* \param orig Top left corner of the searched for block.
|
|
|
|
* \param mv_in_out Predicted mv in and best out. Quarter pixel precision.
|
|
|
|
*
|
|
|
|
* \returns Cost of the motion vector.
|
|
|
|
*
|
|
|
|
* Motion vector is searched by first searching iteratively with the large
|
|
|
|
* hexagon pattern until the best match is at the center of the hexagon.
|
|
|
|
* As a final step a smaller hexagon is used to check the adjacent pixels.
|
|
|
|
*
|
|
|
|
* If a non 0,0 predicted motion vector predictor is given as mv_in_out,
|
|
|
|
* the 0,0 vector is also tried. This is hoped to help in the case where
|
|
|
|
* the predicted motion vector is way off. In the future even more additional
|
|
|
|
* points like 0,0 might be used, such as vectors from top or left.
|
|
|
|
*/
|
2015-03-04 15:00:23 +00:00
|
|
|
static unsigned hexagon_search(const encoder_state_t * const state, unsigned depth,
|
2015-03-04 11:55:06 +00:00
|
|
|
const image_t *pic, const image_t *ref,
|
2015-03-04 11:32:11 +00:00
|
|
|
const vector2d_t *orig, vector2d_t *mv_in_out,
|
2015-03-18 08:03:06 +00:00
|
|
|
int16_t mv_cand[2][2], inter_merge_cand_t merge_cand[MRG_MAX_NUM_CANDS],
|
2014-03-11 09:35:02 +00:00
|
|
|
int16_t num_cand, int32_t ref_idx, uint32_t *bitcost_out)
|
2013-10-08 09:30:25 +00:00
|
|
|
{
|
2015-03-04 11:32:11 +00:00
|
|
|
vector2d_t mv = { mv_in_out->x >> 2, mv_in_out->y >> 2 };
|
2013-10-08 09:30:25 +00:00
|
|
|
int block_width = CU_WIDTH_FROM_DEPTH(depth);
|
2014-02-06 12:36:19 +00:00
|
|
|
unsigned best_cost = UINT32_MAX;
|
2014-03-11 09:35:02 +00:00
|
|
|
uint32_t best_bitcost = 0, bitcost;
|
2013-10-08 09:30:25 +00:00
|
|
|
unsigned i;
|
2013-10-18 13:30:36 +00:00
|
|
|
unsigned best_index = 0; // Index of large_hexbs or finally small_hexbs.
|
2014-06-16 12:27:56 +00:00
|
|
|
int max_lcu_below = -1;
|
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
if (state->encoder_control->owf) {
|
2014-06-16 12:27:56 +00:00
|
|
|
max_lcu_below = 1;
|
|
|
|
}
|
2015-02-23 15:47:36 +00:00
|
|
|
|
2015-03-17 10:49:45 +00:00
|
|
|
// Check mv_in, if it's not in merge candidates.
|
|
|
|
bool mv_in_merge_cand = false;
|
|
|
|
for (int i = 0; i < num_cand; ++i) {
|
2015-03-24 07:38:24 +00:00
|
|
|
if (merge_cand[i].dir == 3) continue;
|
|
|
|
if (merge_cand[i].mv[merge_cand[i].dir - 1][0] >> 2 == mv.x &&
|
|
|
|
merge_cand[i].mv[merge_cand[i].dir - 1][1] >> 2 == mv.y) {
|
2015-03-17 10:49:45 +00:00
|
|
|
mv_in_merge_cand = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mv_in_merge_cand) {
|
2015-02-23 15:47:36 +00:00
|
|
|
PERFORMANCE_MEASURE_START(_DEBUG_PERF_SEARCH_PIXELS);
|
|
|
|
|
|
|
|
best_cost = image_calc_sad(pic, ref, orig->x, orig->y,
|
2015-03-04 15:00:23 +00:00
|
|
|
(state->tile->lcu_offset_x * LCU_WIDTH) + orig->x + mv.x,
|
|
|
|
(state->tile->lcu_offset_y * LCU_WIDTH) + orig->y + mv.y,
|
2015-02-23 15:47:36 +00:00
|
|
|
block_width, block_width, max_lcu_below);
|
2015-03-04 15:00:23 +00:00
|
|
|
best_cost += calc_mvd_cost(state, mv.x, mv.y, 2, mv_cand, merge_cand, num_cand, ref_idx, &bitcost);
|
2015-02-23 15:47:36 +00:00
|
|
|
best_bitcost = bitcost;
|
|
|
|
best_index = num_cand;
|
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
PERFORMANCE_MEASURE_END(_DEBUG_PERF_SEARCH_PIXELS, state->encoder_control->threadqueue, "type=sad,step=large_hexbs,frame=%d,tile=%d,ref=%d,px_x=%d-%d,px_y=%d-%d,ref_px_x=%d-%d,ref_px_y=%d-%d", state->global->frame, state->tile->id, ref->poc - state->global->poc, orig->x, orig->x + block_width, orig->y, orig->y + block_width,
|
|
|
|
(state->tile->lcu_offset_x * LCU_WIDTH) + orig->x + mv.x,
|
|
|
|
(state->tile->lcu_offset_x * LCU_WIDTH) + orig->x + mv.x + block_width,
|
|
|
|
(state->tile->lcu_offset_y * LCU_WIDTH) + orig->y + mv.y,
|
|
|
|
(state->tile->lcu_offset_y * LCU_WIDTH) + orig->y + mv.y + block_width);
|
2015-02-23 15:47:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Select starting point from among merge candidates. These should include
|
|
|
|
// both mv_cand vectors and (0, 0).
|
|
|
|
for (i = 0; i < num_cand; ++i) {
|
2015-03-23 13:17:41 +00:00
|
|
|
if (merge_cand[i].dir == 3) continue;
|
2015-03-18 08:03:06 +00:00
|
|
|
mv.x = merge_cand[i].mv[merge_cand[i].dir - 1][0] >> 2;
|
|
|
|
mv.y = merge_cand[i].mv[merge_cand[i].dir - 1][1] >> 2;
|
2015-02-23 15:47:36 +00:00
|
|
|
|
|
|
|
PERFORMANCE_MEASURE_START(_DEBUG_PERF_SEARCH_PIXELS);
|
|
|
|
|
|
|
|
unsigned cost = image_calc_sad(pic, ref, orig->x, orig->y,
|
2015-03-04 15:00:23 +00:00
|
|
|
(state->tile->lcu_offset_x * LCU_WIDTH) + orig->x + mv.x,
|
|
|
|
(state->tile->lcu_offset_y * LCU_WIDTH) + orig->y + mv.y,
|
2015-02-23 15:47:36 +00:00
|
|
|
block_width, block_width, max_lcu_below);
|
2015-03-04 15:00:23 +00:00
|
|
|
cost += calc_mvd_cost(state, mv.x, mv.y, 2, mv_cand, merge_cand, num_cand, ref_idx, &bitcost);
|
2015-02-23 15:47:36 +00:00
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
PERFORMANCE_MEASURE_END(_DEBUG_PERF_SEARCH_PIXELS, state->encoder_control->threadqueue, "type=sad,step=large_hexbs,frame=%d,tile=%d,ref=%d,px_x=%d-%d,px_y=%d-%d,ref_px_x=%d-%d,ref_px_y=%d-%d", state->global->frame, state->tile->id, ref->poc - state->global->poc, orig->x, orig->x + block_width, orig->y, orig->y + block_width,
|
|
|
|
(state->tile->lcu_offset_x * LCU_WIDTH) + orig->x + mv.x,
|
|
|
|
(state->tile->lcu_offset_x * LCU_WIDTH) + orig->x + mv.x + block_width,
|
|
|
|
(state->tile->lcu_offset_y * LCU_WIDTH) + orig->y + mv.y,
|
|
|
|
(state->tile->lcu_offset_y * LCU_WIDTH) + orig->y + mv.y + block_width);
|
2015-02-23 15:47:36 +00:00
|
|
|
|
|
|
|
if (cost < best_cost) {
|
|
|
|
best_cost = cost;
|
|
|
|
best_index = i;
|
|
|
|
best_bitcost = bitcost;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (best_index < num_cand) {
|
2015-03-18 08:03:06 +00:00
|
|
|
mv.x = merge_cand[best_index].mv[merge_cand[best_index].dir - 1][0] >> 2;
|
|
|
|
mv.y = merge_cand[best_index].mv[merge_cand[best_index].dir - 1][1] >> 2;
|
2015-02-23 15:47:36 +00:00
|
|
|
} else {
|
|
|
|
mv.x = mv_in_out->x >> 2;
|
|
|
|
mv.y = mv_in_out->y >> 2;
|
|
|
|
}
|
2014-07-08 09:42:58 +00:00
|
|
|
|
2013-10-08 09:30:25 +00:00
|
|
|
// Search the initial 7 points of the hexagon.
|
2015-02-23 15:47:36 +00:00
|
|
|
best_index = 0;
|
2013-10-08 09:30:25 +00:00
|
|
|
for (i = 0; i < 7; ++i) {
|
2015-03-04 11:32:11 +00:00
|
|
|
const vector2d_t *pattern = &large_hexbs[i];
|
2014-07-08 09:42:58 +00:00
|
|
|
unsigned cost;
|
|
|
|
{
|
2014-08-11 09:35:36 +00:00
|
|
|
PERFORMANCE_MEASURE_START(_DEBUG_PERF_SEARCH_PIXELS);
|
2014-07-08 09:42:58 +00:00
|
|
|
cost = image_calc_sad(pic, ref, orig->x, orig->y,
|
2015-03-04 15:00:23 +00:00
|
|
|
(state->tile->lcu_offset_x * LCU_WIDTH) + orig->x + mv.x + pattern->x,
|
|
|
|
(state->tile->lcu_offset_y * LCU_WIDTH) + orig->y + mv.y + pattern->y,
|
2014-06-16 12:27:56 +00:00
|
|
|
block_width, block_width, max_lcu_below);
|
2015-03-04 15:00:23 +00:00
|
|
|
cost += calc_mvd_cost(state, mv.x + pattern->x, mv.y + pattern->y, 2, mv_cand,merge_cand,num_cand,ref_idx, &bitcost);
|
2014-08-11 09:35:36 +00:00
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
PERFORMANCE_MEASURE_END(_DEBUG_PERF_SEARCH_PIXELS, state->encoder_control->threadqueue, "type=sad,step=large_hexbs,frame=%d,tile=%d,ref=%d,px_x=%d-%d,px_y=%d-%d,ref_px_x=%d-%d,ref_px_y=%d-%d", state->global->frame, state->tile->id, ref->poc - state->global->poc, orig->x, orig->x + block_width, orig->y, orig->y + block_width,
|
|
|
|
(state->tile->lcu_offset_x * LCU_WIDTH) + orig->x + mv.x + pattern->x,
|
|
|
|
(state->tile->lcu_offset_x * LCU_WIDTH) + orig->x + mv.x + pattern->x + block_width,
|
|
|
|
(state->tile->lcu_offset_y * LCU_WIDTH) + orig->y + mv.y + pattern->y,
|
|
|
|
(state->tile->lcu_offset_y * LCU_WIDTH) + orig->y + mv.y + pattern->y + block_width);
|
2014-07-08 09:42:58 +00:00
|
|
|
}
|
2013-10-22 10:08:08 +00:00
|
|
|
|
2013-10-22 08:40:24 +00:00
|
|
|
if (cost < best_cost) {
|
2014-03-11 09:35:02 +00:00
|
|
|
best_cost = cost;
|
|
|
|
best_index = i;
|
|
|
|
best_bitcost = bitcost;
|
2013-10-08 09:30:25 +00:00
|
|
|
}
|
|
|
|
}
|
2013-10-08 13:11:00 +00:00
|
|
|
|
2013-10-08 09:30:25 +00:00
|
|
|
// Iteratively search the 3 new points around the best match, until the best
|
|
|
|
// match is in the center.
|
|
|
|
while (best_index != 0) {
|
|
|
|
unsigned start; // Starting point of the 3 offsets to be searched.
|
|
|
|
if (best_index == 1) {
|
|
|
|
start = 6;
|
|
|
|
} else if (best_index == 8) {
|
|
|
|
start = 1;
|
|
|
|
} else {
|
|
|
|
start = best_index - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move the center to the best match.
|
2013-10-18 13:43:28 +00:00
|
|
|
mv.x += large_hexbs[best_index].x;
|
|
|
|
mv.y += large_hexbs[best_index].y;
|
2013-10-08 09:30:25 +00:00
|
|
|
best_index = 0;
|
|
|
|
|
|
|
|
// Iterate through the next 3 points.
|
|
|
|
for (i = 0; i < 3; ++i) {
|
2015-03-04 11:32:11 +00:00
|
|
|
const vector2d_t *offset = &large_hexbs[start + i];
|
2014-07-08 09:42:58 +00:00
|
|
|
unsigned cost;
|
|
|
|
{
|
2014-08-11 09:35:36 +00:00
|
|
|
PERFORMANCE_MEASURE_START(_DEBUG_PERF_SEARCH_PIXELS);
|
2014-07-08 09:42:58 +00:00
|
|
|
cost = image_calc_sad(pic, ref, orig->x, orig->y,
|
2015-03-04 15:00:23 +00:00
|
|
|
(state->tile->lcu_offset_x * LCU_WIDTH) + orig->x + mv.x + offset->x,
|
|
|
|
(state->tile->lcu_offset_y * LCU_WIDTH) + orig->y + mv.y + offset->y,
|
2014-06-16 12:27:56 +00:00
|
|
|
block_width, block_width, max_lcu_below);
|
2015-03-04 15:00:23 +00:00
|
|
|
cost += calc_mvd_cost(state, mv.x + offset->x, mv.y + offset->y, 2, mv_cand,merge_cand,num_cand,ref_idx, &bitcost);
|
|
|
|
PERFORMANCE_MEASURE_END(_DEBUG_PERF_SEARCH_PIXELS, state->encoder_control->threadqueue, "type=sad,step=large_hexbs_iterative,frame=%d,tile=%d,ref=%d,px_x=%d-%d,px_y=%d-%d,ref_px_x=%d-%d,ref_px_y=%d-%d", state->global->frame, state->tile->id, ref->poc - state->global->poc, orig->x, orig->x + block_width, orig->y, orig->y + block_width,
|
|
|
|
(state->tile->lcu_offset_x * LCU_WIDTH) + orig->x + mv.x + offset->x,
|
|
|
|
(state->tile->lcu_offset_x * LCU_WIDTH) + orig->x + mv.x + offset->x + block_width,
|
|
|
|
(state->tile->lcu_offset_y * LCU_WIDTH) + orig->y + mv.y + offset->y,
|
|
|
|
(state->tile->lcu_offset_y * LCU_WIDTH) + orig->y + mv.y + offset->y + block_width);
|
2014-07-08 09:42:58 +00:00
|
|
|
}
|
2013-10-22 10:08:08 +00:00
|
|
|
|
2013-10-22 08:40:24 +00:00
|
|
|
if (cost < best_cost) {
|
2014-03-11 09:35:02 +00:00
|
|
|
best_cost = cost;
|
|
|
|
best_index = start + i;
|
|
|
|
best_bitcost = bitcost;
|
2013-10-08 09:30:25 +00:00
|
|
|
}
|
|
|
|
++offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-22 08:40:24 +00:00
|
|
|
// Move the center to the best match.
|
2013-10-18 13:43:28 +00:00
|
|
|
mv.x += large_hexbs[best_index].x;
|
|
|
|
mv.y += large_hexbs[best_index].y;
|
2013-10-08 09:30:25 +00:00
|
|
|
best_index = 0;
|
2013-10-22 08:40:24 +00:00
|
|
|
|
|
|
|
// Do the final step of the search with a small pattern.
|
2013-10-08 09:30:25 +00:00
|
|
|
for (i = 1; i < 5; ++i) {
|
2015-03-04 11:32:11 +00:00
|
|
|
const vector2d_t *offset = &small_hexbs[i];
|
2014-07-08 09:42:58 +00:00
|
|
|
unsigned cost;
|
|
|
|
{
|
2014-08-11 09:35:36 +00:00
|
|
|
PERFORMANCE_MEASURE_START(_DEBUG_PERF_SEARCH_PIXELS);
|
2014-07-08 09:42:58 +00:00
|
|
|
cost = image_calc_sad(pic, ref, orig->x, orig->y,
|
2015-03-04 15:00:23 +00:00
|
|
|
(state->tile->lcu_offset_x * LCU_WIDTH) + orig->x + mv.x + offset->x,
|
|
|
|
(state->tile->lcu_offset_y * LCU_WIDTH) + orig->y + mv.y + offset->y,
|
2014-06-16 12:27:56 +00:00
|
|
|
block_width, block_width, max_lcu_below);
|
2015-03-04 15:00:23 +00:00
|
|
|
cost += calc_mvd_cost(state, mv.x + offset->x, mv.y + offset->y, 2, mv_cand,merge_cand,num_cand,ref_idx, &bitcost);
|
|
|
|
PERFORMANCE_MEASURE_END(_DEBUG_PERF_SEARCH_PIXELS, state->encoder_control->threadqueue, "type=sad,step=small_hexbs,frame=%d,tile=%d,ref=%d,px_x=%d-%d,px_y=%d-%d,ref_px_x=%d-%d,ref_px_y=%d-%d", state->global->frame, state->tile->id, ref->poc - state->global->poc, orig->x, orig->x + block_width, orig->y, orig->y + block_width,
|
|
|
|
(state->tile->lcu_offset_x * LCU_WIDTH) + orig->x + mv.x + offset->x,
|
|
|
|
(state->tile->lcu_offset_x * LCU_WIDTH) + orig->x + mv.x + offset->x + block_width,
|
|
|
|
(state->tile->lcu_offset_y * LCU_WIDTH) + orig->y + mv.y + offset->y,
|
|
|
|
(state->tile->lcu_offset_y * LCU_WIDTH) + orig->y + mv.y + offset->y + block_width);
|
2014-07-08 09:42:58 +00:00
|
|
|
}
|
2013-10-22 10:08:08 +00:00
|
|
|
|
2013-10-08 09:30:25 +00:00
|
|
|
if (cost > 0 && cost < best_cost) {
|
2014-03-11 09:35:02 +00:00
|
|
|
best_cost = cost;
|
|
|
|
best_index = i;
|
|
|
|
best_bitcost = bitcost;
|
2013-10-08 09:30:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-22 08:40:24 +00:00
|
|
|
// Adjust the movement vector according to the final best match.
|
2013-10-18 13:43:28 +00:00
|
|
|
mv.x += small_hexbs[best_index].x;
|
|
|
|
mv.y += small_hexbs[best_index].y;
|
2013-10-22 08:40:24 +00:00
|
|
|
|
|
|
|
// Return final movement vector in quarter-pixel precision.
|
2013-10-18 13:43:28 +00:00
|
|
|
mv_in_out->x = mv.x << 2;
|
|
|
|
mv_in_out->y = mv.y << 2;
|
2013-10-18 13:30:36 +00:00
|
|
|
|
2014-03-11 09:35:02 +00:00
|
|
|
*bitcost_out = best_bitcost;
|
|
|
|
|
2013-10-18 13:30:36 +00:00
|
|
|
return best_cost;
|
2013-10-08 09:30:25 +00:00
|
|
|
}
|
|
|
|
|
2014-03-06 16:14:01 +00:00
|
|
|
|
2014-02-21 13:43:39 +00:00
|
|
|
#if SEARCH_MV_FULL_RADIUS
|
2014-02-21 13:24:57 +00:00
|
|
|
static unsigned search_mv_full(unsigned depth,
|
|
|
|
const picture *pic, const picture *ref,
|
2014-03-07 13:05:39 +00:00
|
|
|
const vector2d *orig, vector2d *mv_in_out,
|
|
|
|
int16_t mv_cand[2][2], int16_t merge_cand[MRG_MAX_NUM_CANDS][3],
|
2014-03-11 09:35:02 +00:00
|
|
|
int16_t num_cand, int32_t ref_idx, uint32_t *bitcost_out)
|
2013-10-22 16:00:36 +00:00
|
|
|
{
|
|
|
|
vector2d mv = { mv_in_out->x >> 2, mv_in_out->y >> 2 };
|
|
|
|
int block_width = CU_WIDTH_FROM_DEPTH(depth);
|
2014-02-06 12:36:19 +00:00
|
|
|
unsigned best_cost = UINT32_MAX;
|
2013-10-22 16:00:36 +00:00
|
|
|
int x, y;
|
2014-03-11 09:35:02 +00:00
|
|
|
uint32_t best_bitcost = 0, bitcost;
|
2013-10-22 16:00:36 +00:00
|
|
|
vector2d min_mv, max_mv;
|
|
|
|
|
|
|
|
/*if (abs(mv.x) > SEARCH_MV_FULL_RADIUS || abs(mv.y) > SEARCH_MV_FULL_RADIUS) {
|
2014-02-21 13:00:20 +00:00
|
|
|
best_cost = calc_sad(pic, ref, orig->x, orig->y,
|
2013-10-22 16:00:36 +00:00
|
|
|
orig->x, orig->y,
|
|
|
|
block_width, block_width);
|
|
|
|
mv.x = 0;
|
|
|
|
mv.y = 0;
|
|
|
|
}*/
|
|
|
|
|
|
|
|
min_mv.x = mv.x - SEARCH_MV_FULL_RADIUS;
|
|
|
|
min_mv.y = mv.y - SEARCH_MV_FULL_RADIUS;
|
|
|
|
max_mv.x = mv.x + SEARCH_MV_FULL_RADIUS;
|
|
|
|
max_mv.y = mv.y + SEARCH_MV_FULL_RADIUS;
|
|
|
|
|
|
|
|
for (y = min_mv.y; y < max_mv.y; ++y) {
|
|
|
|
for (x = min_mv.x; x < max_mv.x; ++x) {
|
|
|
|
unsigned cost = calc_sad(pic, ref, orig->x, orig->y,
|
|
|
|
orig->x + x,
|
|
|
|
orig->y + y,
|
|
|
|
block_width, block_width);
|
2014-04-04 08:32:56 +00:00
|
|
|
cost += calc_mvd_cost(x, y, mv_cand,merge_cand,num_cand,ref_idx, &bitcost);
|
2013-10-22 16:00:36 +00:00
|
|
|
if (cost < best_cost) {
|
2014-03-11 09:35:02 +00:00
|
|
|
best_cost = cost;
|
|
|
|
best_bitcost = bitcost;
|
2013-10-22 16:00:36 +00:00
|
|
|
mv.x = x;
|
|
|
|
mv.y = y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mv_in_out->x = mv.x << 2;
|
|
|
|
mv_in_out->y = mv.y << 2;
|
|
|
|
|
2014-03-11 09:35:02 +00:00
|
|
|
*bitcost_out = best_bitcost;
|
|
|
|
|
2013-10-22 16:00:36 +00:00
|
|
|
return best_cost;
|
|
|
|
}
|
2014-02-21 13:43:39 +00:00
|
|
|
#endif
|
2013-10-22 16:00:36 +00:00
|
|
|
|
2014-05-14 01:49:34 +00:00
|
|
|
/**
|
|
|
|
* \brief Do fractional motion estimation
|
|
|
|
*
|
|
|
|
* \param depth log2 depth of the search
|
|
|
|
* \param pic Picture motion vector is searched for.
|
|
|
|
* \param ref Picture motion vector is searched from.
|
|
|
|
* \param orig Top left corner of the searched for block.
|
|
|
|
* \param mv_in_out Predicted mv in and best out. Quarter pixel precision.
|
|
|
|
*
|
|
|
|
* \returns Cost of the motion vector.
|
|
|
|
*
|
|
|
|
* Algoritm first searches 1/2-pel positions around integer mv and after best match is found,
|
|
|
|
* refines the search by searching best 1/4-pel postion around best 1/2-pel position.
|
|
|
|
*/
|
2015-03-04 15:00:23 +00:00
|
|
|
static unsigned search_frac(const encoder_state_t * const state,
|
|
|
|
unsigned depth,
|
|
|
|
const image_t *pic, const image_t *ref,
|
|
|
|
const vector2d_t *orig, vector2d_t *mv_in_out,
|
2015-03-18 08:03:06 +00:00
|
|
|
int16_t mv_cand[2][2], inter_merge_cand_t merge_cand[MRG_MAX_NUM_CANDS],
|
2015-03-04 15:00:23 +00:00
|
|
|
int16_t num_cand, int32_t ref_idx, uint32_t *bitcost_out)
|
|
|
|
{
|
2014-05-14 01:42:02 +00:00
|
|
|
|
|
|
|
//Set mv to halfpel precision
|
2015-03-04 11:32:11 +00:00
|
|
|
vector2d_t mv = { mv_in_out->x >> 2, mv_in_out->y >> 2 };
|
2014-05-14 01:42:02 +00:00
|
|
|
int block_width = CU_WIDTH_FROM_DEPTH(depth);
|
|
|
|
unsigned best_cost = UINT32_MAX;
|
|
|
|
uint32_t best_bitcost = 0, bitcost;
|
|
|
|
unsigned i;
|
|
|
|
unsigned best_index = 0; // Index of large_hexbs or finally small_hexbs.
|
|
|
|
|
|
|
|
unsigned cost = 0;
|
|
|
|
|
2014-11-06 13:59:10 +00:00
|
|
|
cost_pixel_nxn_func *satd = pixels_get_satd_func(block_width);
|
2014-05-14 01:42:02 +00:00
|
|
|
|
2015-03-04 11:32:11 +00:00
|
|
|
vector2d_t halfpel_offset;
|
2014-05-14 01:42:02 +00:00
|
|
|
|
|
|
|
#define FILTER_SIZE 8
|
|
|
|
#define HALF_FILTER (FILTER_SIZE>>1)
|
|
|
|
|
|
|
|
//create buffer for block + extra for filter
|
|
|
|
int src_stride = block_width+FILTER_SIZE+1;
|
2015-03-04 14:35:17 +00:00
|
|
|
pixel_t src[(LCU_WIDTH+FILTER_SIZE+1) * (LCU_WIDTH+FILTER_SIZE+1)];
|
|
|
|
pixel_t* src_off = &src[HALF_FILTER+HALF_FILTER*(block_width+FILTER_SIZE+1)];
|
2014-05-14 01:42:02 +00:00
|
|
|
|
|
|
|
//destination buffer for interpolation
|
|
|
|
int dst_stride = (block_width+1)*4;
|
2015-03-04 14:35:17 +00:00
|
|
|
pixel_t dst[(LCU_WIDTH+1) * (LCU_WIDTH+1) * 16];
|
|
|
|
pixel_t* dst_off = &dst[dst_stride*4+4];
|
2014-05-14 01:42:02 +00:00
|
|
|
|
|
|
|
extend_borders(orig->x, orig->y, mv.x-1, mv.y-1,
|
2015-03-04 15:00:23 +00:00
|
|
|
state->tile->lcu_offset_x * LCU_WIDTH,
|
|
|
|
state->tile->lcu_offset_y * LCU_WIDTH,
|
2014-11-06 13:59:10 +00:00
|
|
|
ref->y, ref->width, ref->height, FILTER_SIZE, block_width+1, block_width+1, src);
|
2014-05-14 01:42:02 +00:00
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
filter_inter_quarterpel_luma(state->encoder_control, src_off, src_stride, block_width+1,
|
2014-05-14 01:42:02 +00:00
|
|
|
block_width+1, dst, dst_stride, 1, 1);
|
|
|
|
|
|
|
|
|
|
|
|
//Set mv to half-pixel precision
|
|
|
|
mv.x <<= 1;
|
|
|
|
mv.y <<= 1;
|
|
|
|
|
|
|
|
// Search halfpel positions around best integer mv
|
|
|
|
for (i = 0; i < 9; ++i) {
|
2015-03-04 11:32:11 +00:00
|
|
|
const vector2d_t *pattern = &square[i];
|
2014-05-14 01:42:02 +00:00
|
|
|
|
2015-03-04 14:35:17 +00:00
|
|
|
pixel_t tmp_filtered[LCU_WIDTH*LCU_WIDTH];
|
|
|
|
pixel_t tmp_pic[LCU_WIDTH*LCU_WIDTH];
|
2014-05-14 01:42:02 +00:00
|
|
|
|
|
|
|
int y,x;
|
|
|
|
for(y = 0; y < block_width; ++y) {
|
|
|
|
int dst_y = y*4+pattern->y*2;
|
|
|
|
for(x = 0; x < block_width; ++x) {
|
|
|
|
int dst_x = x*4+pattern->x*2;
|
2014-12-11 14:38:43 +00:00
|
|
|
tmp_filtered[y*block_width+x] = dst_off[dst_y*dst_stride+dst_x];
|
|
|
|
tmp_pic[y*block_width+x] = pic->y[orig->x+x + (orig->y+y)*pic->width];
|
2014-05-14 01:42:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cost = satd(tmp_pic,tmp_filtered);
|
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
cost += calc_mvd_cost(state, mv.x + pattern->x, mv.y + pattern->y, 1, mv_cand,merge_cand,num_cand,ref_idx, &bitcost);
|
2014-05-14 01:42:02 +00:00
|
|
|
|
|
|
|
if (cost < best_cost) {
|
|
|
|
best_cost = cost;
|
|
|
|
best_index = i;
|
|
|
|
best_bitcost = bitcost;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Set mv to best match
|
|
|
|
mv.x += square[best_index].x;
|
|
|
|
mv.y += square[best_index].y;
|
|
|
|
|
|
|
|
halfpel_offset.x = square[best_index].x*2;
|
|
|
|
halfpel_offset.y = square[best_index].y*2;
|
|
|
|
|
|
|
|
//Set mv to quarterpel precision
|
|
|
|
mv.x <<= 1;
|
|
|
|
mv.y <<= 1;
|
|
|
|
|
|
|
|
//Search quarterpel points around best halfpel mv
|
|
|
|
for (i = 0; i < 9; ++i) {
|
2015-03-04 11:32:11 +00:00
|
|
|
const vector2d_t *pattern = &square[i];
|
2014-05-14 01:42:02 +00:00
|
|
|
|
2015-03-04 14:35:17 +00:00
|
|
|
pixel_t tmp_filtered[LCU_WIDTH*LCU_WIDTH];
|
|
|
|
pixel_t tmp_pic[LCU_WIDTH*LCU_WIDTH];
|
2014-05-14 01:42:02 +00:00
|
|
|
|
|
|
|
int y,x;
|
|
|
|
for(y = 0; y < block_width; ++y) {
|
|
|
|
int dst_y = y*4+halfpel_offset.y+pattern->y;
|
|
|
|
for(x = 0; x < block_width; ++x) {
|
|
|
|
int dst_x = x*4+halfpel_offset.x+pattern->x;
|
2014-12-11 14:38:43 +00:00
|
|
|
tmp_filtered[y*block_width+x] = dst_off[dst_y*dst_stride+dst_x];
|
|
|
|
tmp_pic[y*block_width+x] = pic->y[orig->x+x + (orig->y+y)*pic->width];
|
2014-05-14 01:42:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cost = satd(tmp_pic,tmp_filtered);
|
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
cost += calc_mvd_cost(state, mv.x + pattern->x, mv.y + pattern->y, 0, mv_cand,merge_cand,num_cand,ref_idx, &bitcost);
|
2014-05-14 01:42:02 +00:00
|
|
|
|
|
|
|
if (cost < best_cost) {
|
|
|
|
best_cost = cost;
|
|
|
|
best_index = i;
|
|
|
|
best_bitcost = bitcost;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Set mv to best final best match
|
|
|
|
mv.x += square[best_index].x;
|
|
|
|
mv.y += square[best_index].y;
|
|
|
|
|
|
|
|
mv_in_out->x = mv.x;
|
|
|
|
mv_in_out->y = mv.y;
|
|
|
|
|
|
|
|
*bitcost_out = best_bitcost;
|
|
|
|
|
|
|
|
|
|
|
|
return best_cost;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-03-03 12:51:36 +00:00
|
|
|
/**
|
|
|
|
* Update lcu to have best modes at this depth.
|
|
|
|
* \return Cost of best mode.
|
|
|
|
*/
|
2015-03-04 15:00:23 +00:00
|
|
|
static int search_cu_inter(const encoder_state_t * const state, int x, int y, int depth, lcu_t *lcu)
|
2014-02-21 13:24:57 +00:00
|
|
|
{
|
2015-03-04 15:00:23 +00:00
|
|
|
const videoframe_t * const frame = state->tile->frame;
|
2014-04-04 08:08:34 +00:00
|
|
|
uint32_t ref_idx = 0;
|
2014-03-03 12:51:36 +00:00
|
|
|
int x_local = (x&0x3f), y_local = (y&0x3f);
|
|
|
|
int x_cu = x>>3;
|
|
|
|
int y_cu = y>>3;
|
|
|
|
int cu_pos = LCU_CU_OFFSET+(x_local>>3) + (y_local>>3)*LCU_T_CU_WIDTH;
|
|
|
|
|
2015-03-04 11:15:45 +00:00
|
|
|
cu_info_t *cur_cu = &lcu->cu[cu_pos];
|
2014-03-03 12:51:36 +00:00
|
|
|
|
2014-03-07 13:05:39 +00:00
|
|
|
int16_t mv_cand[2][2];
|
|
|
|
// Search for merge mode candidate
|
2015-03-18 08:03:06 +00:00
|
|
|
inter_merge_cand_t merge_cand[MRG_MAX_NUM_CANDS];
|
2014-03-07 13:05:39 +00:00
|
|
|
// Get list of candidates
|
2015-03-18 10:35:47 +00:00
|
|
|
int16_t num_cand = inter_get_merge_cand(state, x, y, depth, merge_cand, lcu);
|
2014-03-07 13:05:39 +00:00
|
|
|
|
|
|
|
|
2015-04-23 09:18:05 +00:00
|
|
|
// Default to candidate 0
|
|
|
|
cur_cu->inter.mv_cand[0] = 0;
|
|
|
|
cur_cu->inter.mv_cand[1] = 0;
|
2014-03-07 13:05:39 +00:00
|
|
|
|
2014-02-14 13:49:04 +00:00
|
|
|
cur_cu->inter.cost = UINT_MAX;
|
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
for (ref_idx = 0; ref_idx < state->global->ref->used_size; ref_idx++) {
|
|
|
|
image_t *ref_image = state->global->ref->images[ref_idx];
|
2014-03-11 09:35:02 +00:00
|
|
|
uint32_t temp_bitcost = 0;
|
2014-03-07 13:05:39 +00:00
|
|
|
uint32_t temp_cost = 0;
|
2015-03-17 10:49:45 +00:00
|
|
|
vector2d_t orig, mvd;
|
2014-03-07 13:05:39 +00:00
|
|
|
int32_t merged = 0;
|
2014-03-10 08:16:12 +00:00
|
|
|
uint8_t cu_mv_cand = 0;
|
|
|
|
int8_t merge_idx = 0;
|
2015-03-30 11:40:29 +00:00
|
|
|
int8_t ref_list = state->global->refmap[ref_idx].list-1;
|
|
|
|
int8_t temp_ref_idx = cur_cu->inter.mv_ref[ref_list];
|
2014-03-03 12:51:36 +00:00
|
|
|
orig.x = x_cu * CU_MIN_SIZE_PIXELS;
|
|
|
|
orig.y = y_cu * CU_MIN_SIZE_PIXELS;
|
2014-03-10 08:16:12 +00:00
|
|
|
// Get MV candidates
|
2015-03-30 11:40:29 +00:00
|
|
|
cur_cu->inter.mv_ref[ref_list] = ref_idx;
|
2015-04-02 11:05:24 +00:00
|
|
|
inter_get_mv_cand(state, x, y, depth, mv_cand, cur_cu, lcu, ref_list);
|
2015-03-30 11:40:29 +00:00
|
|
|
cur_cu->inter.mv_ref[ref_list] = temp_ref_idx;
|
2013-12-17 09:32:28 +00:00
|
|
|
|
2015-03-17 10:49:45 +00:00
|
|
|
vector2d_t mv = { 0, 0 };
|
|
|
|
{
|
|
|
|
// Take starting point for MV search from previous frame.
|
|
|
|
// When temporal motion vector candidates are added, there is probably
|
|
|
|
// no point to this anymore, but for now it helps.
|
2015-03-23 07:21:52 +00:00
|
|
|
int mid_x_cu = (x + (LCU_WIDTH >> (depth+1))) / 8;
|
|
|
|
int mid_y_cu = (y + (LCU_WIDTH >> (depth+1))) / 8;
|
2015-03-17 10:49:45 +00:00
|
|
|
cu_info_t *ref_cu = &state->global->ref->cu_arrays[ref_idx]->data[mid_x_cu + mid_y_cu * (frame->width_in_lcu << MAX_DEPTH)];
|
|
|
|
if (ref_cu->type == CU_INTER) {
|
2015-03-31 09:23:46 +00:00
|
|
|
if (ref_cu->inter.mv_dir & 1) {
|
|
|
|
mv.x = ref_cu->inter.mv[0][0];
|
|
|
|
mv.y = ref_cu->inter.mv[0][1];
|
|
|
|
} else {
|
|
|
|
mv.x = ref_cu->inter.mv[1][0];
|
|
|
|
mv.y = ref_cu->inter.mv[1][1];
|
|
|
|
}
|
2015-03-17 10:49:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-06 16:14:01 +00:00
|
|
|
#if SEARCH_MV_FULL_RADIUS
|
2014-06-05 12:54:58 +00:00
|
|
|
temp_cost += search_mv_full(depth, frame, ref_pic, &orig, &mv, mv_cand, merge_cand, num_cand, ref_idx, &temp_bitcost);
|
2014-03-06 16:14:01 +00:00
|
|
|
#else
|
2015-03-19 16:48:10 +00:00
|
|
|
switch (state->encoder_control->cfg->ime_algorithm) {
|
|
|
|
case IME_TZ :
|
|
|
|
temp_cost += tz_search(state, depth, frame->source, ref_image, &orig, &mv, mv_cand, merge_cand, num_cand, ref_idx, &temp_bitcost);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
temp_cost += hexagon_search(state, depth, frame->source, ref_image, &orig, &mv, mv_cand, merge_cand, num_cand, ref_idx, &temp_bitcost);
|
|
|
|
break;
|
|
|
|
}
|
2014-03-06 16:14:01 +00:00
|
|
|
#endif
|
2015-03-04 15:00:23 +00:00
|
|
|
if (state->encoder_control->cfg->fme_level > 0) {
|
|
|
|
temp_cost = search_frac(state, depth, frame->source, ref_image, &orig, &mv, mv_cand, merge_cand, num_cand, ref_idx, &temp_bitcost);
|
2014-11-20 12:59:04 +00:00
|
|
|
}
|
2014-05-14 01:42:02 +00:00
|
|
|
|
2014-03-07 13:05:39 +00:00
|
|
|
merged = 0;
|
|
|
|
// Check every candidate to find a match
|
2014-03-10 08:16:12 +00:00
|
|
|
for(merge_idx = 0; merge_idx < num_cand; merge_idx++) {
|
2015-03-30 11:40:29 +00:00
|
|
|
if (merge_cand[merge_idx].dir != 3 &&
|
|
|
|
merge_cand[merge_idx].mv[merge_cand[merge_idx].dir - 1][0] == mv.x &&
|
|
|
|
merge_cand[merge_idx].mv[merge_cand[merge_idx].dir - 1][1] == mv.y &&
|
2015-04-23 09:18:33 +00:00
|
|
|
(uint32_t)merge_cand[merge_idx].ref[merge_cand[merge_idx].dir - 1] == ref_idx) {
|
2014-03-07 13:05:39 +00:00
|
|
|
merged = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only check when candidates are different
|
|
|
|
if (!merged && (mv_cand[0][0] != mv_cand[1][0] || mv_cand[0][1] != mv_cand[1][1])) {
|
2015-03-04 11:32:11 +00:00
|
|
|
vector2d_t mvd_temp1, mvd_temp2;
|
2014-03-07 13:05:39 +00:00
|
|
|
int cand1_cost,cand2_cost;
|
|
|
|
|
|
|
|
mvd_temp1.x = mv.x - mv_cand[0][0];
|
|
|
|
mvd_temp1.y = mv.y - mv_cand[0][1];
|
|
|
|
cand1_cost = get_mvd_coding_cost(&mvd_temp1);
|
|
|
|
|
|
|
|
mvd_temp2.x = mv.x - mv_cand[1][0];
|
|
|
|
mvd_temp2.y = mv.y - mv_cand[1][1];
|
|
|
|
cand2_cost = get_mvd_coding_cost(&mvd_temp2);
|
|
|
|
|
|
|
|
// Select candidate 1 if it has lower cost
|
|
|
|
if (cand2_cost < cand1_cost) {
|
2014-03-10 08:16:12 +00:00
|
|
|
cu_mv_cand = 1;
|
2014-03-07 13:05:39 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-10 08:16:12 +00:00
|
|
|
mvd.x = mv.x - mv_cand[cu_mv_cand][0];
|
|
|
|
mvd.y = mv.y - mv_cand[cu_mv_cand][1];
|
2014-03-07 13:05:39 +00:00
|
|
|
|
2014-02-14 13:49:04 +00:00
|
|
|
if(temp_cost < cur_cu->inter.cost) {
|
2015-03-20 08:33:05 +00:00
|
|
|
|
|
|
|
// Map reference index to L0/L1 pictures
|
2015-03-30 11:40:29 +00:00
|
|
|
cur_cu->inter.mv_dir = ref_list+1;
|
|
|
|
cur_cu->inter.mv_ref_coded[ref_list] = state->global->refmap[ref_idx].idx;
|
2015-03-19 10:24:52 +00:00
|
|
|
|
2014-03-07 13:05:39 +00:00
|
|
|
cur_cu->merged = merged;
|
2014-03-10 08:16:12 +00:00
|
|
|
cur_cu->merge_idx = merge_idx;
|
2015-03-30 11:40:29 +00:00
|
|
|
cur_cu->inter.mv_ref[ref_list] = ref_idx;
|
|
|
|
cur_cu->inter.mv[ref_list][0] = (int16_t)mv.x;
|
|
|
|
cur_cu->inter.mv[ref_list][1] = (int16_t)mv.y;
|
|
|
|
cur_cu->inter.mvd[ref_list][0] = (int16_t)mvd.x;
|
|
|
|
cur_cu->inter.mvd[ref_list][1] = (int16_t)mvd.y;
|
2014-03-07 13:05:39 +00:00
|
|
|
cur_cu->inter.cost = temp_cost;
|
2015-03-31 09:23:46 +00:00
|
|
|
cur_cu->inter.bitcost = temp_bitcost + cur_cu->inter.mv_dir - 1 + cur_cu->inter.mv_ref_coded[ref_list];
|
2015-04-23 09:18:05 +00:00
|
|
|
cur_cu->inter.mv_cand[ref_list] = cu_mv_cand;
|
2014-02-21 13:00:20 +00:00
|
|
|
}
|
2013-12-17 09:32:28 +00:00
|
|
|
}
|
|
|
|
|
2015-03-30 11:40:29 +00:00
|
|
|
// Search bi-pred positions
|
2015-04-21 11:18:34 +00:00
|
|
|
if (state->global->slicetype == SLICE_B && state->encoder_control->cfg->bipred) {
|
2015-04-21 10:53:17 +00:00
|
|
|
lcu_t *templcu = MALLOC(lcu_t, 1);
|
|
|
|
cost_pixel_nxn_func *satd = pixels_get_satd_func(LCU_WIDTH >> depth);
|
2015-03-31 12:02:43 +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 = num_cand;
|
|
|
|
for (int32_t idx = 0; idx<cutoff*(cutoff - 1); idx++) {
|
|
|
|
uint8_t i = priorityList0[idx];
|
|
|
|
uint8_t j = priorityList1[idx];
|
|
|
|
if (i >= num_cand || j >= num_cand) break;
|
|
|
|
|
|
|
|
// Find one L0 and L1 candidate according to the priority list
|
|
|
|
if ((merge_cand[i].dir & 0x1) && (merge_cand[j].dir & 0x2)) {
|
|
|
|
if (merge_cand[i].ref[0] != merge_cand[j].ref[1] ||
|
|
|
|
merge_cand[i].mv[0][0] != merge_cand[j].mv[1][0] ||
|
2015-04-21 10:53:17 +00:00
|
|
|
merge_cand[i].mv[0][1] != merge_cand[j].mv[1][1]) {
|
|
|
|
uint32_t bitcost[2];
|
|
|
|
uint32_t cost = 0;
|
2015-03-31 12:02:43 +00:00
|
|
|
int8_t cu_mv_cand = 0;
|
2015-04-21 10:53:17 +00:00
|
|
|
int16_t mv[2][2];
|
|
|
|
pixel_t tmp_block[64 * 64];
|
|
|
|
pixel_t tmp_pic[64 * 64];
|
2015-03-31 12:02:43 +00:00
|
|
|
// Force L0 and L1 references
|
|
|
|
if (state->global->refmap[merge_cand[i].ref[0]].list == 2 || state->global->refmap[merge_cand[j].ref[1]].list == 1) continue;
|
2015-04-21 10:53:17 +00:00
|
|
|
|
2015-04-23 11:39:41 +00:00
|
|
|
// TODO: enable fractional pixel bipred search
|
2015-04-21 10:53:17 +00:00
|
|
|
mv[0][0] = merge_cand[i].mv[0][0] & 0xfff8;
|
|
|
|
mv[0][1] = merge_cand[i].mv[0][1] & 0xfff8;
|
|
|
|
mv[1][0] = merge_cand[j].mv[1][0] & 0xfff8;
|
|
|
|
mv[1][1] = merge_cand[j].mv[1][1] & 0xfff8;
|
2015-04-23 11:39:41 +00:00
|
|
|
|
2015-04-21 10:53:17 +00:00
|
|
|
inter_recon_lcu_bipred(state, state->global->ref->images[merge_cand[i].ref[0]], state->global->ref->images[merge_cand[j].ref[1]], x, y, LCU_WIDTH >> depth, mv, templcu);
|
|
|
|
|
|
|
|
for (int ypos = 0; ypos < LCU_WIDTH >> depth; ++ypos) {
|
|
|
|
int dst_y = ypos*(LCU_WIDTH >> depth);
|
|
|
|
for (int xpos = 0; xpos < (LCU_WIDTH >> depth); ++xpos) {
|
|
|
|
tmp_block[dst_y + xpos] = templcu->rec.y[((y + ypos)&(LCU_WIDTH - 1))*LCU_WIDTH + ((x + xpos)&(LCU_WIDTH - 1))];
|
|
|
|
tmp_pic[dst_y + xpos] = frame->source->y[x + xpos + (y + ypos)*frame->source->width];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cost = satd(tmp_pic, tmp_block);
|
|
|
|
|
2015-04-23 11:39:41 +00:00
|
|
|
// TODO: enable fractional pixel bipred search
|
2015-04-21 10:53:17 +00:00
|
|
|
cost += calc_mvd_cost(state, merge_cand[i].mv[0][0] & 0xfff8, merge_cand[i].mv[0][1] & 0xfff8, 0, mv_cand, merge_cand, 0, ref_idx, &bitcost[0]);
|
|
|
|
cost += calc_mvd_cost(state, merge_cand[i].mv[1][0] & 0xfff8, merge_cand[i].mv[1][1] & 0xfff8, 0, mv_cand, merge_cand, 0, ref_idx, &bitcost[1]);
|
|
|
|
|
|
|
|
if (cost < cur_cu->inter.cost) {
|
|
|
|
|
|
|
|
cur_cu->inter.mv_dir = 3;
|
|
|
|
cur_cu->inter.mv_ref_coded[0] = state->global->refmap[merge_cand[i].ref[0]].idx;
|
|
|
|
cur_cu->inter.mv_ref_coded[1] = state->global->refmap[merge_cand[j].ref[1]].idx;
|
|
|
|
|
2015-04-23 09:18:33 +00:00
|
|
|
|
|
|
|
|
2015-04-21 10:53:17 +00:00
|
|
|
cur_cu->inter.mv_ref[0] = merge_cand[i].ref[0];
|
|
|
|
cur_cu->inter.mv_ref[1] = merge_cand[j].ref[1];
|
2015-04-23 11:39:41 +00:00
|
|
|
|
|
|
|
// TODO: enable fractional pixel bipred search
|
2015-04-21 10:53:17 +00:00
|
|
|
cur_cu->inter.mv[0][0] = merge_cand[i].mv[0][0] & 0xfff8;
|
|
|
|
cur_cu->inter.mv[0][1] = merge_cand[i].mv[0][1] & 0xfff8;
|
|
|
|
cur_cu->inter.mv[1][0] = merge_cand[j].mv[1][0] & 0xfff8;
|
|
|
|
cur_cu->inter.mv[1][1] = merge_cand[j].mv[1][1] & 0xfff8;
|
2015-04-23 09:18:33 +00:00
|
|
|
cur_cu->merged = 0;
|
|
|
|
|
|
|
|
// Check every candidate to find a match
|
|
|
|
for(int merge_idx = 0; merge_idx < num_cand; merge_idx++) {
|
|
|
|
if (
|
|
|
|
merge_cand[merge_idx].mv[0][0] == cur_cu->inter.mv[0][0] &&
|
|
|
|
merge_cand[merge_idx].mv[0][1] == cur_cu->inter.mv[0][1] &&
|
|
|
|
merge_cand[merge_idx].mv[1][0] == cur_cu->inter.mv[1][0] &&
|
|
|
|
merge_cand[merge_idx].mv[1][1] == cur_cu->inter.mv[1][1] &&
|
|
|
|
merge_cand[merge_idx].ref[0] == cur_cu->inter.mv_ref[0] &&
|
|
|
|
merge_cand[merge_idx].ref[1] == cur_cu->inter.mv_ref[1]) {
|
|
|
|
cur_cu->merged = 1;
|
|
|
|
cur_cu->merge_idx = merge_idx;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-23 11:39:41 +00:00
|
|
|
// Each motion vector has its own candidate
|
2015-04-21 10:53:17 +00:00
|
|
|
for (int reflist = 0; reflist < 2; reflist++) {
|
|
|
|
cu_mv_cand = 0;
|
|
|
|
inter_get_mv_cand(state, x, y, depth, mv_cand, cur_cu, lcu, reflist);
|
|
|
|
if ((mv_cand[0][0] != mv_cand[1][0] || mv_cand[0][1] != mv_cand[1][1])) {
|
|
|
|
vector2d_t mvd_temp1, mvd_temp2;
|
|
|
|
int cand1_cost, cand2_cost;
|
|
|
|
|
|
|
|
mvd_temp1.x = cur_cu->inter.mv[reflist][0] - mv_cand[0][0];
|
|
|
|
mvd_temp1.y = cur_cu->inter.mv[reflist][1] - mv_cand[0][1];
|
|
|
|
cand1_cost = get_mvd_coding_cost(&mvd_temp1);
|
|
|
|
|
|
|
|
mvd_temp2.x = cur_cu->inter.mv[reflist][0] - mv_cand[1][0];
|
|
|
|
mvd_temp2.y = cur_cu->inter.mv[reflist][1] - mv_cand[1][1];
|
|
|
|
cand2_cost = get_mvd_coding_cost(&mvd_temp2);
|
|
|
|
|
|
|
|
// Select candidate 1 if it has lower cost
|
|
|
|
if (cand2_cost < cand1_cost) {
|
2015-04-23 09:18:05 +00:00
|
|
|
cu_mv_cand = 1;
|
2015-04-21 10:53:17 +00:00
|
|
|
}
|
2015-03-31 12:02:43 +00:00
|
|
|
}
|
2015-04-21 10:53:17 +00:00
|
|
|
cur_cu->inter.mvd[reflist][0] = cur_cu->inter.mv[reflist][0] - mv_cand[cu_mv_cand][0];
|
|
|
|
cur_cu->inter.mvd[reflist][1] = cur_cu->inter.mv[reflist][1] - mv_cand[cu_mv_cand][1];
|
2015-04-23 09:18:05 +00:00
|
|
|
cur_cu->inter.mv_cand[reflist] = cu_mv_cand;
|
2015-03-31 12:02:43 +00:00
|
|
|
}
|
2015-04-21 10:53:17 +00:00
|
|
|
cur_cu->inter.cost = cost;
|
|
|
|
cur_cu->inter.bitcost = bitcost[0] + bitcost[1] + cur_cu->inter.mv_dir - 1 + cur_cu->inter.mv_ref_coded[0] + cur_cu->inter.mv_ref_coded[1];
|
2015-03-31 12:02:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-21 13:00:20 +00:00
|
|
|
}
|
2015-04-21 10:53:17 +00:00
|
|
|
FREE_POINTER(templcu);
|
2013-12-17 09:32:28 +00:00
|
|
|
}
|
|
|
|
|
2014-03-03 12:51:36 +00:00
|
|
|
return cur_cu->inter.cost;
|
2013-12-17 09:32:28 +00:00
|
|
|
}
|
|
|
|
|
2014-03-06 16:14:01 +00:00
|
|
|
|
2014-02-25 11:06:22 +00:00
|
|
|
/**
|
|
|
|
* Copy all non-reference CU data from depth+1 to depth.
|
|
|
|
*/
|
2014-03-12 10:18:33 +00:00
|
|
|
static void work_tree_copy_up(int x_px, int y_px, int depth, lcu_t work_tree[MAX_PU_DEPTH + 1])
|
2014-02-25 11:06:22 +00:00
|
|
|
{
|
2014-02-26 13:09:18 +00:00
|
|
|
// Copy non-reference CUs.
|
|
|
|
{
|
|
|
|
const int x_cu = SUB_SCU(x_px) >> MAX_DEPTH;
|
|
|
|
const int y_cu = SUB_SCU(y_px) >> MAX_DEPTH;
|
|
|
|
const int width_cu = LCU_WIDTH >> MAX_DEPTH >> depth;
|
|
|
|
int x, y;
|
|
|
|
for (y = y_cu; y < y_cu + width_cu; ++y) {
|
|
|
|
for (x = x_cu; x < x_cu + width_cu; ++x) {
|
2015-03-04 11:15:45 +00:00
|
|
|
const cu_info_t *from_cu = &work_tree[depth + 1].cu[LCU_CU_OFFSET + x + y * LCU_T_CU_WIDTH];
|
|
|
|
cu_info_t *to_cu = &work_tree[depth].cu[LCU_CU_OFFSET + x + y * LCU_T_CU_WIDTH];
|
2014-02-26 13:09:18 +00:00
|
|
|
memcpy(to_cu, from_cu, sizeof(*to_cu));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-25 11:06:22 +00:00
|
|
|
|
2014-02-26 13:09:18 +00:00
|
|
|
// Copy reconstructed pixels.
|
|
|
|
{
|
|
|
|
const int x = SUB_SCU(x_px);
|
|
|
|
const int y = SUB_SCU(y_px);
|
|
|
|
const int width_px = LCU_WIDTH >> depth;
|
|
|
|
const int luma_index = x + y * LCU_WIDTH;
|
|
|
|
const int chroma_index = (x / 2) + (y / 2) * (LCU_WIDTH / 2);
|
|
|
|
|
2014-02-27 09:56:16 +00:00
|
|
|
const lcu_yuv_t *from = &work_tree[depth + 1].rec;
|
2014-02-26 13:09:18 +00:00
|
|
|
lcu_yuv_t *to = &work_tree[depth].rec;
|
|
|
|
|
2014-02-27 09:56:16 +00:00
|
|
|
const lcu_coeff_t *from_coeff = &work_tree[depth + 1].coeff;
|
2014-02-27 13:58:47 +00:00
|
|
|
lcu_coeff_t *to_coeff = &work_tree[depth].coeff;
|
2014-02-27 09:56:16 +00:00
|
|
|
|
2014-06-05 12:54:58 +00:00
|
|
|
pixels_blit(&from->y[luma_index], &to->y[luma_index],
|
2014-02-26 13:09:18 +00:00
|
|
|
width_px, width_px, LCU_WIDTH, LCU_WIDTH);
|
2014-06-05 12:54:58 +00:00
|
|
|
pixels_blit(&from->u[chroma_index], &to->u[chroma_index],
|
2014-02-26 13:09:18 +00:00
|
|
|
width_px / 2, width_px / 2, LCU_WIDTH / 2, LCU_WIDTH / 2);
|
2014-06-05 12:54:58 +00:00
|
|
|
pixels_blit(&from->v[chroma_index], &to->v[chroma_index],
|
2014-02-26 13:09:18 +00:00
|
|
|
width_px / 2, width_px / 2, LCU_WIDTH / 2, LCU_WIDTH / 2);
|
2014-02-27 09:56:16 +00:00
|
|
|
|
|
|
|
// Copy coefficients up. They do not have to be copied down because they
|
|
|
|
// are not used for the search.
|
2014-06-05 12:54:58 +00:00
|
|
|
coefficients_blit(&from_coeff->y[luma_index], &to_coeff->y[luma_index],
|
2014-02-27 09:56:16 +00:00
|
|
|
width_px, width_px, LCU_WIDTH, LCU_WIDTH);
|
2014-06-05 12:54:58 +00:00
|
|
|
coefficients_blit(&from_coeff->u[chroma_index], &to_coeff->u[chroma_index],
|
2014-02-27 09:56:16 +00:00
|
|
|
width_px / 2, width_px / 2, LCU_WIDTH / 2, LCU_WIDTH / 2);
|
2014-06-05 12:54:58 +00:00
|
|
|
coefficients_blit(&from_coeff->v[chroma_index], &to_coeff->v[chroma_index],
|
2014-02-27 09:56:16 +00:00
|
|
|
width_px / 2, width_px / 2, LCU_WIDTH / 2, LCU_WIDTH / 2);
|
2014-02-26 13:09:18 +00:00
|
|
|
}
|
2014-02-25 11:06:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy all non-reference CU data from depth to depth+1..MAX_PU_DEPTH.
|
|
|
|
*/
|
2014-03-12 10:18:33 +00:00
|
|
|
static void work_tree_copy_down(int x_px, int y_px, int depth, lcu_t work_tree[MAX_PU_DEPTH + 1])
|
2014-02-25 11:06:22 +00:00
|
|
|
{
|
2014-02-26 15:30:15 +00:00
|
|
|
// TODO: clean up to remove the copy pasta
|
|
|
|
const int width_px = LCU_WIDTH >> depth;
|
2014-03-06 16:14:01 +00:00
|
|
|
|
2014-02-26 15:30:15 +00:00
|
|
|
int d;
|
2014-02-25 11:06:22 +00:00
|
|
|
|
2014-03-12 10:18:33 +00:00
|
|
|
for (d = depth + 1; d < MAX_PU_DEPTH + 1; ++d) {
|
2014-02-26 15:30:15 +00:00
|
|
|
const int x_cu = SUB_SCU(x_px) >> MAX_DEPTH;
|
|
|
|
const int y_cu = SUB_SCU(y_px) >> MAX_DEPTH;
|
|
|
|
const int width_cu = width_px >> MAX_DEPTH;
|
2014-03-06 16:14:01 +00:00
|
|
|
|
2014-02-26 15:30:15 +00:00
|
|
|
int x, y;
|
|
|
|
for (y = y_cu; y < y_cu + width_cu; ++y) {
|
|
|
|
for (x = x_cu; x < x_cu + width_cu; ++x) {
|
2015-03-04 11:15:45 +00:00
|
|
|
const cu_info_t *from_cu = &work_tree[depth].cu[LCU_CU_OFFSET + x + y * LCU_T_CU_WIDTH];
|
|
|
|
cu_info_t *to_cu = &work_tree[d].cu[LCU_CU_OFFSET + x + y * LCU_T_CU_WIDTH];
|
2014-02-26 15:30:15 +00:00
|
|
|
memcpy(to_cu, from_cu, sizeof(*to_cu));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy reconstructed pixels.
|
2014-03-12 10:18:33 +00:00
|
|
|
for (d = depth + 1; d < MAX_PU_DEPTH + 1; ++d) {
|
2014-02-26 15:30:15 +00:00
|
|
|
const int x = SUB_SCU(x_px);
|
|
|
|
const int y = SUB_SCU(y_px);
|
2014-03-06 16:14:01 +00:00
|
|
|
|
2014-02-26 15:30:15 +00:00
|
|
|
const int luma_index = x + y * LCU_WIDTH;
|
|
|
|
const int chroma_index = (x / 2) + (y / 2) * (LCU_WIDTH / 2);
|
|
|
|
|
|
|
|
lcu_yuv_t *from = &work_tree[depth].rec;
|
|
|
|
lcu_yuv_t *to = &work_tree[d].rec;
|
|
|
|
|
2014-06-05 12:54:58 +00:00
|
|
|
pixels_blit(&from->y[luma_index], &to->y[luma_index],
|
2014-02-26 15:30:15 +00:00
|
|
|
width_px, width_px, LCU_WIDTH, LCU_WIDTH);
|
2014-06-05 12:54:58 +00:00
|
|
|
pixels_blit(&from->u[chroma_index], &to->u[chroma_index],
|
2014-02-26 15:30:15 +00:00
|
|
|
width_px / 2, width_px / 2, LCU_WIDTH / 2, LCU_WIDTH / 2);
|
2014-06-05 12:54:58 +00:00
|
|
|
pixels_blit(&from->v[chroma_index], &to->v[chroma_index],
|
2014-02-26 15:30:15 +00:00
|
|
|
width_px / 2, width_px / 2, LCU_WIDTH / 2, LCU_WIDTH / 2);
|
|
|
|
}
|
2014-02-25 11:06:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-05 08:29:30 +00:00
|
|
|
static void lcu_set_trdepth(lcu_t *lcu, int x_px, int y_px, int depth, int tr_depth)
|
|
|
|
{
|
|
|
|
const int width_cu = LCU_CU_WIDTH >> depth;
|
2015-03-04 11:32:11 +00:00
|
|
|
const vector2d_t lcu_cu = { (x_px & (LCU_WIDTH - 1)) / 8, (y_px & (LCU_WIDTH - 1)) / 8 };
|
2015-03-04 11:15:45 +00:00
|
|
|
cu_info_t *const cur_cu = &lcu->cu[lcu_cu.x + lcu_cu.y * LCU_T_CU_WIDTH + LCU_CU_OFFSET];
|
2014-09-05 08:29:30 +00:00
|
|
|
int x, y;
|
|
|
|
|
|
|
|
// Depth 4 doesn't go inside the loop. Set the top-left CU.
|
|
|
|
cur_cu->tr_depth = tr_depth;
|
|
|
|
|
|
|
|
for (y = 0; y < width_cu; ++y) {
|
|
|
|
for (x = 0; x < width_cu; ++x) {
|
2015-03-04 11:15:45 +00:00
|
|
|
cu_info_t *cu = &cur_cu[x + y * LCU_T_CU_WIDTH];
|
2014-09-05 08:29:30 +00:00
|
|
|
cu->tr_depth = tr_depth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void lcu_set_intra_mode(lcu_t *lcu, int x_px, int y_px, int depth, int pred_mode, int chroma_mode, int part_mode)
|
2014-02-27 11:02:24 +00:00
|
|
|
{
|
|
|
|
const int width_cu = LCU_CU_WIDTH >> depth;
|
2014-02-27 11:53:26 +00:00
|
|
|
const int x_cu = SUB_SCU(x_px) >> MAX_DEPTH;
|
|
|
|
const int y_cu = SUB_SCU(y_px) >> MAX_DEPTH;
|
2015-03-04 11:15:45 +00:00
|
|
|
cu_info_t *const lcu_cu = &lcu->cu[LCU_CU_OFFSET];
|
2014-02-27 11:02:24 +00:00
|
|
|
int x, y;
|
2014-03-06 16:14:01 +00:00
|
|
|
|
2014-02-27 11:02:24 +00:00
|
|
|
// NxN can only be applied to a single CU at a time.
|
|
|
|
if (part_mode == SIZE_NxN) {
|
2015-03-04 11:15:45 +00:00
|
|
|
cu_info_t *cu = &lcu_cu[x_cu + y_cu * LCU_T_CU_WIDTH];
|
2014-03-11 17:19:20 +00:00
|
|
|
cu->depth = MAX_DEPTH;
|
2014-02-27 11:02:24 +00:00
|
|
|
cu->type = CU_INTRA;
|
2014-09-05 08:29:30 +00:00
|
|
|
cu->intra[PU_INDEX(x_px / 4, y_px / 4)].mode = pred_mode;
|
|
|
|
cu->intra[PU_INDEX(x_px / 4, y_px / 4)].mode_chroma = chroma_mode;
|
2014-02-27 11:02:24 +00:00
|
|
|
cu->part_size = part_mode;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set mode in every CU covered by part_mode in this depth.
|
|
|
|
for (y = y_cu; y < y_cu + width_cu; ++y) {
|
|
|
|
for (x = x_cu; x < x_cu + width_cu; ++x) {
|
2015-03-04 11:15:45 +00:00
|
|
|
cu_info_t *cu = &lcu_cu[x + y * LCU_T_CU_WIDTH];
|
2014-02-27 11:02:24 +00:00
|
|
|
cu->depth = depth;
|
|
|
|
cu->type = CU_INTRA;
|
|
|
|
cu->intra[0].mode = pred_mode;
|
|
|
|
cu->intra[1].mode = pred_mode;
|
|
|
|
cu->intra[2].mode = pred_mode;
|
|
|
|
cu->intra[3].mode = pred_mode;
|
2014-05-15 15:23:48 +00:00
|
|
|
cu->intra[0].mode_chroma = chroma_mode;
|
2014-02-27 11:02:24 +00:00
|
|
|
cu->part_size = part_mode;
|
2014-03-03 15:42:44 +00:00
|
|
|
cu->coded = 1;
|
2014-02-27 11:02:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-06 16:14:01 +00:00
|
|
|
|
2015-03-04 11:15:45 +00:00
|
|
|
static void lcu_set_inter(lcu_t *lcu, int x_px, int y_px, int depth, cu_info_t *cur_cu)
|
2014-02-25 11:06:22 +00:00
|
|
|
{
|
2014-03-03 12:51:36 +00:00
|
|
|
const int width_cu = LCU_CU_WIDTH >> depth;
|
|
|
|
const int x_cu = SUB_SCU(x_px) >> MAX_DEPTH;
|
|
|
|
const int y_cu = SUB_SCU(y_px) >> MAX_DEPTH;
|
2015-03-04 11:15:45 +00:00
|
|
|
cu_info_t *const lcu_cu = &lcu->cu[LCU_CU_OFFSET];
|
2014-03-03 12:51:36 +00:00
|
|
|
int x, y;
|
|
|
|
// Set mode in every CU covered by part_mode in this depth.
|
|
|
|
for (y = y_cu; y < y_cu + width_cu; ++y) {
|
|
|
|
for (x = x_cu; x < x_cu + width_cu; ++x) {
|
2015-03-04 11:15:45 +00:00
|
|
|
cu_info_t *cu = &lcu_cu[x + y * LCU_T_CU_WIDTH];
|
2014-04-15 04:30:21 +00:00
|
|
|
//Check if this could be moved inside the if
|
2014-03-06 12:52:58 +00:00
|
|
|
cu->coded = 1;
|
2014-04-15 04:30:21 +00:00
|
|
|
if (cu != cur_cu) {
|
|
|
|
cu->depth = cur_cu->depth;
|
|
|
|
cu->type = CU_INTER;
|
|
|
|
cu->tr_depth = cur_cu->tr_depth;
|
|
|
|
cu->merged = cur_cu->merged;
|
|
|
|
cu->skipped = cur_cu->skipped;
|
2015-03-04 11:33:38 +00:00
|
|
|
memcpy(&cu->inter, &cur_cu->inter, sizeof(cur_cu->inter));
|
2014-04-15 04:30:21 +00:00
|
|
|
}
|
2014-03-03 12:51:36 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-25 11:06:22 +00:00
|
|
|
}
|
|
|
|
|
2014-03-06 16:14:01 +00:00
|
|
|
|
2015-03-04 11:15:45 +00:00
|
|
|
static void lcu_set_coeff(lcu_t *lcu, int x_px, int y_px, int depth, cu_info_t *cur_cu)
|
2014-03-06 12:52:58 +00:00
|
|
|
{
|
|
|
|
const int width_cu = LCU_CU_WIDTH >> depth;
|
|
|
|
const int x_cu = SUB_SCU(x_px) >> MAX_DEPTH;
|
|
|
|
const int y_cu = SUB_SCU(y_px) >> MAX_DEPTH;
|
2015-03-04 11:15:45 +00:00
|
|
|
cu_info_t *const lcu_cu = &lcu->cu[LCU_CU_OFFSET];
|
2014-03-06 12:52:58 +00:00
|
|
|
int x, y;
|
|
|
|
int tr_split = cur_cu->tr_depth-cur_cu->depth;
|
|
|
|
|
|
|
|
// Set coeff flags in every CU covered by part_mode in this depth.
|
|
|
|
for (y = y_cu; y < y_cu + width_cu; ++y) {
|
|
|
|
for (x = x_cu; x < x_cu + width_cu; ++x) {
|
2015-03-04 11:15:45 +00:00
|
|
|
cu_info_t *cu = &lcu_cu[x + y * LCU_T_CU_WIDTH];
|
2014-03-06 12:52:58 +00:00
|
|
|
// Use TU top-left CU to propagate coeff flags
|
|
|
|
uint32_t mask = ~((width_cu>>tr_split)-1);
|
2015-03-04 11:15:45 +00:00
|
|
|
cu_info_t *cu_from = &lcu_cu[(x & mask) + (y & mask) * LCU_T_CU_WIDTH];
|
2014-04-15 04:30:21 +00:00
|
|
|
if (cu != cu_from) {
|
|
|
|
// Chroma coeff data is not used, luma is needed for deblocking
|
2014-05-06 11:43:12 +00:00
|
|
|
cu->cbf.y = cu_from->cbf.y;
|
2014-04-15 04:30:21 +00:00
|
|
|
}
|
2014-03-06 12:52:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-25 11:06:22 +00:00
|
|
|
|
2014-03-06 16:14:01 +00:00
|
|
|
|
2014-03-11 13:01:50 +00:00
|
|
|
/**
|
2014-09-05 08:29:30 +00:00
|
|
|
* Calculate RD cost for a Coding Unit.
|
|
|
|
* \return Cost of block
|
|
|
|
* \param ref_cu CU used for prediction parameters.
|
|
|
|
*
|
|
|
|
* Calculates the RDO cost of a single CU that will not be split further.
|
|
|
|
* Takes into account SSD of reconstruction and the cost of encoding whatever
|
|
|
|
* prediction unit data needs to be coded.
|
|
|
|
*/
|
2015-03-04 15:00:23 +00:00
|
|
|
static double cu_rd_cost_luma(const encoder_state_t *const state,
|
2015-03-04 11:15:45 +00:00
|
|
|
const int x_px, const int y_px, const int depth,
|
|
|
|
const cu_info_t *const pred_cu,
|
|
|
|
lcu_t *const lcu)
|
2014-03-11 13:01:50 +00:00
|
|
|
{
|
2014-09-05 08:29:30 +00:00
|
|
|
const int width = LCU_WIDTH >> depth;
|
2014-10-02 09:33:17 +00:00
|
|
|
const uint8_t pu_index = PU_INDEX(x_px / 4, y_px / 4);
|
2014-09-05 08:29:30 +00:00
|
|
|
|
|
|
|
// cur_cu is used for TU parameters.
|
2015-03-04 11:15:45 +00:00
|
|
|
cu_info_t *const tr_cu = &lcu->cu[LCU_CU_OFFSET + (x_px / 8) + (y_px / 8) * LCU_T_CU_WIDTH];
|
2014-09-05 08:29:30 +00:00
|
|
|
|
2014-09-17 08:52:22 +00:00
|
|
|
double coeff_bits = 0;
|
2014-10-02 08:51:34 +00:00
|
|
|
double tr_tree_bits = 0;
|
2014-09-05 08:29:30 +00:00
|
|
|
|
|
|
|
// Check that lcu is not in
|
|
|
|
assert(x_px >= 0 && x_px < LCU_WIDTH);
|
|
|
|
assert(y_px >= 0 && y_px < LCU_WIDTH);
|
|
|
|
|
2014-10-02 09:33:17 +00:00
|
|
|
const uint8_t tr_depth = tr_cu->tr_depth - depth;
|
2014-10-01 09:32:29 +00:00
|
|
|
|
2014-10-02 09:33:17 +00:00
|
|
|
// Add transform_tree split_transform_flag bit cost.
|
2014-10-01 09:32:29 +00:00
|
|
|
bool intra_split_flag = pred_cu->type == CU_INTRA && pred_cu->part_size == SIZE_NxN && depth == 3;
|
2014-09-17 08:52:22 +00:00
|
|
|
if (width <= TR_MAX_WIDTH
|
2014-09-05 08:29:30 +00:00
|
|
|
&& width > TR_MIN_WIDTH
|
2014-10-01 09:32:29 +00:00
|
|
|
&& !intra_split_flag)
|
2014-09-17 08:52:22 +00:00
|
|
|
{
|
2015-03-04 15:00:23 +00:00
|
|
|
const cabac_ctx_t *ctx = &(state->cabac.ctx.trans_subdiv_model[5 - (6 - depth)]);
|
2014-10-02 09:33:17 +00:00
|
|
|
tr_tree_bits += CTX_ENTROPY_FBITS(ctx, tr_depth > 0);
|
2014-09-05 08:29:30 +00:00
|
|
|
}
|
2014-09-17 08:52:22 +00:00
|
|
|
|
2014-10-02 09:33:17 +00:00
|
|
|
if (tr_depth > 0) {
|
2014-09-05 08:29:30 +00:00
|
|
|
int offset = width / 2;
|
2014-09-17 08:52:22 +00:00
|
|
|
double sum = 0;
|
2014-09-05 08:29:30 +00:00
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
sum += cu_rd_cost_luma(state, x_px, y_px, depth + 1, pred_cu, lcu);
|
|
|
|
sum += cu_rd_cost_luma(state, x_px + offset, y_px, depth + 1, pred_cu, lcu);
|
|
|
|
sum += cu_rd_cost_luma(state, x_px, y_px + offset, depth + 1, pred_cu, lcu);
|
|
|
|
sum += cu_rd_cost_luma(state, x_px + offset, y_px + offset, depth + 1, pred_cu, lcu);
|
2014-04-07 11:36:01 +00:00
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
return sum + tr_tree_bits * state->global->cur_lambda_cost;
|
2014-09-05 08:29:30 +00:00
|
|
|
}
|
|
|
|
|
2014-10-02 09:33:17 +00:00
|
|
|
// Add transform_tree cbf_luma bit cost.
|
|
|
|
if (pred_cu->type == CU_INTRA ||
|
|
|
|
tr_depth > 0 ||
|
|
|
|
cbf_is_set(tr_cu->cbf.u, depth) ||
|
|
|
|
cbf_is_set(tr_cu->cbf.v, depth))
|
|
|
|
{
|
2015-03-04 15:00:23 +00:00
|
|
|
const cabac_ctx_t *ctx = &(state->cabac.ctx.qt_cbf_model_luma[!tr_depth]);
|
2014-10-02 09:33:17 +00:00
|
|
|
tr_tree_bits += CTX_ENTROPY_FBITS(ctx, cbf_is_set(pred_cu->cbf.y, depth + pu_index));
|
2014-09-05 08:29:30 +00:00
|
|
|
}
|
2014-03-11 13:01:50 +00:00
|
|
|
|
2014-09-17 08:52:22 +00:00
|
|
|
unsigned ssd = 0;
|
2014-04-07 11:36:01 +00:00
|
|
|
// SSD between reconstruction and original
|
2014-09-17 08:52:22 +00:00
|
|
|
for (int y = y_px; y < y_px + width; ++y) {
|
|
|
|
for (int x = x_px; x < x_px + width; ++x) {
|
2014-04-02 11:51:39 +00:00
|
|
|
int diff = (int)lcu->rec.y[y * LCU_WIDTH + x] - (int)lcu->ref.y[y * LCU_WIDTH + x];
|
2014-09-17 08:52:22 +00:00
|
|
|
ssd += diff*diff;
|
2014-03-11 13:01:50 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-19 09:17:42 +00:00
|
|
|
|
2015-01-20 08:53:03 +00:00
|
|
|
{
|
2015-03-04 14:33:47 +00:00
|
|
|
coeff_t coeff_temp[32 * 32];
|
2014-09-05 08:29:30 +00:00
|
|
|
int8_t luma_scan_mode = get_scan_order(pred_cu->type, pred_cu->intra[PU_INDEX(x_px / 4, y_px / 4)].mode, depth);
|
|
|
|
|
|
|
|
// Code coeffs using cabac to get a better estimate of real coding costs.
|
|
|
|
coefficients_blit(&lcu->coeff.y[(y_px*LCU_WIDTH) + x_px], coeff_temp, width, width, LCU_WIDTH, width);
|
2015-03-04 15:00:23 +00:00
|
|
|
coeff_bits += get_coeff_cost(state, coeff_temp, width, 0, luma_scan_mode);
|
2014-05-19 09:17:42 +00:00
|
|
|
}
|
|
|
|
|
2014-10-02 08:51:34 +00:00
|
|
|
double bits = tr_tree_bits + coeff_bits;
|
2015-03-04 15:00:23 +00:00
|
|
|
return (double)ssd * LUMA_MULT + bits * state->global->cur_lambda_cost;
|
2014-05-19 09:17:42 +00:00
|
|
|
}
|
|
|
|
|
2014-09-05 08:29:30 +00:00
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
static double cu_rd_cost_chroma(const encoder_state_t *const state,
|
2015-03-04 11:15:45 +00:00
|
|
|
const int x_px, const int y_px, const int depth,
|
|
|
|
const cu_info_t *const pred_cu,
|
|
|
|
lcu_t *const lcu)
|
2014-05-19 09:17:42 +00:00
|
|
|
{
|
2015-03-04 11:32:11 +00:00
|
|
|
const vector2d_t lcu_px = { x_px / 2, y_px / 2 };
|
2014-09-05 08:29:30 +00:00
|
|
|
const int width = (depth <= MAX_DEPTH) ? LCU_WIDTH >> (depth + 1) : LCU_WIDTH >> depth;
|
2015-03-04 11:15:45 +00:00
|
|
|
cu_info_t *const tr_cu = &lcu->cu[LCU_CU_OFFSET + (lcu_px.x / 4) + (lcu_px.y / 4)*LCU_T_CU_WIDTH];
|
2014-09-05 08:29:30 +00:00
|
|
|
|
2014-10-02 08:51:34 +00:00
|
|
|
double tr_tree_bits = 0;
|
2014-09-17 08:52:22 +00:00
|
|
|
double coeff_bits = 0;
|
2014-09-05 08:29:30 +00:00
|
|
|
|
|
|
|
assert(x_px >= 0 && x_px < LCU_WIDTH);
|
|
|
|
assert(y_px >= 0 && y_px < LCU_WIDTH);
|
|
|
|
|
2014-10-02 08:51:34 +00:00
|
|
|
if (PU_INDEX(x_px / 4, y_px / 4) != 0) {
|
2014-09-05 08:29:30 +00:00
|
|
|
// For MAX_PU_DEPTH calculate chroma for previous depth for the first
|
|
|
|
// block and return 0 cost for all others.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-02 08:51:34 +00:00
|
|
|
if (depth < MAX_PU_DEPTH) {
|
|
|
|
const int tr_depth = depth - pred_cu->depth;
|
2015-03-04 15:00:23 +00:00
|
|
|
const cabac_ctx_t *ctx = &(state->cabac.ctx.qt_cbf_model_chroma[tr_depth]);
|
2014-10-02 08:51:34 +00:00
|
|
|
if (tr_depth == 0 || cbf_is_set(pred_cu->cbf.u, depth - 1)) {
|
|
|
|
tr_tree_bits += CTX_ENTROPY_FBITS(ctx, cbf_is_set(pred_cu->cbf.u, depth));
|
|
|
|
}
|
|
|
|
if (tr_depth == 0 || cbf_is_set(pred_cu->cbf.v, depth - 1)) {
|
|
|
|
tr_tree_bits += CTX_ENTROPY_FBITS(ctx, cbf_is_set(pred_cu->cbf.v, depth));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-05 08:29:30 +00:00
|
|
|
if (tr_cu->tr_depth > depth) {
|
|
|
|
int offset = LCU_WIDTH >> (depth + 1);
|
2014-09-17 08:52:22 +00:00
|
|
|
int sum = 0;
|
2014-09-05 08:29:30 +00:00
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
sum += cu_rd_cost_chroma(state, x_px, y_px, depth + 1, pred_cu, lcu);
|
|
|
|
sum += cu_rd_cost_chroma(state, x_px + offset, y_px, depth + 1, pred_cu, lcu);
|
|
|
|
sum += cu_rd_cost_chroma(state, x_px, y_px + offset, depth + 1, pred_cu, lcu);
|
|
|
|
sum += cu_rd_cost_chroma(state, x_px + offset, y_px + offset, depth + 1, pred_cu, lcu);
|
2014-05-19 09:17:42 +00:00
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
return sum + tr_tree_bits * state->global->cur_lambda_cost;
|
2014-09-05 08:29:30 +00:00
|
|
|
}
|
2014-05-19 09:17:42 +00:00
|
|
|
|
2014-04-07 11:36:01 +00:00
|
|
|
// Chroma SSD
|
2014-09-17 08:52:22 +00:00
|
|
|
int ssd = 0;
|
|
|
|
for (int y = lcu_px.y; y < lcu_px.y + width; ++y) {
|
|
|
|
for (int x = lcu_px.x; x < lcu_px.x + width; ++x) {
|
2014-09-05 08:29:30 +00:00
|
|
|
int diff = (int)lcu->rec.u[y * LCU_WIDTH_C + x] - (int)lcu->ref.u[y * LCU_WIDTH_C + x];
|
2014-09-17 08:52:22 +00:00
|
|
|
ssd += diff * diff;
|
2014-03-12 08:35:56 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-17 08:52:22 +00:00
|
|
|
for (int y = lcu_px.y; y < lcu_px.y + width; ++y) {
|
|
|
|
for (int x = lcu_px.x; x < lcu_px.x + width; ++x) {
|
|
|
|
int diff = (int)lcu->rec.v[y * LCU_WIDTH_C + x] - (int)lcu->ref.v[y * LCU_WIDTH_C + x];
|
|
|
|
ssd += diff * diff;
|
2014-04-07 11:36:01 +00:00
|
|
|
}
|
2014-03-12 08:35:56 +00:00
|
|
|
}
|
2014-04-04 10:09:02 +00:00
|
|
|
|
2015-01-20 08:53:03 +00:00
|
|
|
{
|
2015-03-04 14:33:47 +00:00
|
|
|
coeff_t coeff_temp[16 * 16];
|
2014-09-05 08:29:30 +00:00
|
|
|
int8_t scan_order = get_scan_order(pred_cu->type, pred_cu->intra[0].mode_chroma, depth);
|
2014-09-17 08:52:22 +00:00
|
|
|
|
2014-09-05 08:29:30 +00:00
|
|
|
coefficients_blit(&lcu->coeff.u[(lcu_px.y*(LCU_WIDTH_C)) + lcu_px.x],
|
2014-09-17 08:52:22 +00:00
|
|
|
coeff_temp, width, width, LCU_WIDTH_C, width);
|
2015-03-04 15:00:23 +00:00
|
|
|
coeff_bits += get_coeff_cost(state, coeff_temp, width, 2, scan_order);
|
2014-09-05 08:29:30 +00:00
|
|
|
|
|
|
|
coefficients_blit(&lcu->coeff.v[(lcu_px.y*(LCU_WIDTH_C)) + lcu_px.x],
|
2014-09-17 08:52:22 +00:00
|
|
|
coeff_temp, width, width, LCU_WIDTH_C, width);
|
2015-03-04 15:00:23 +00:00
|
|
|
coeff_bits += get_coeff_cost(state, coeff_temp, width, 2, scan_order);
|
2014-04-04 10:09:02 +00:00
|
|
|
}
|
|
|
|
|
2014-10-02 08:51:34 +00:00
|
|
|
double bits = tr_tree_bits + coeff_bits;
|
2015-03-04 15:00:23 +00:00
|
|
|
return (double)ssd * CHROMA_MULT + bits * state->global->cur_lambda_cost;
|
2014-03-11 13:01:50 +00:00
|
|
|
}
|
2014-02-25 11:06:22 +00:00
|
|
|
|
2014-05-19 09:17:42 +00:00
|
|
|
|
2014-09-05 08:29:30 +00:00
|
|
|
/**
|
|
|
|
* \brief Perform search for best intra transform split configuration.
|
|
|
|
*
|
|
|
|
* This function does a recursive search for the best intra transform split
|
|
|
|
* configuration for a given intra prediction mode.
|
|
|
|
*
|
|
|
|
* \return RD cost of best transform split configuration. Splits in lcu->cu.
|
|
|
|
* \param depth Current transform depth.
|
|
|
|
* \param max_depth Depth to which TR split will be tried.
|
|
|
|
* \param intra_mode Intra prediction mode.
|
|
|
|
* \param cost_treshold RD cost at which search can be stopped.
|
|
|
|
*/
|
2015-03-04 15:00:23 +00:00
|
|
|
static double search_intra_trdepth(encoder_state_t * const state,
|
2015-03-04 11:15:45 +00:00
|
|
|
int x_px, int y_px, int depth, int max_depth,
|
|
|
|
int intra_mode, int cost_treshold,
|
|
|
|
cu_info_t *const pred_cu,
|
|
|
|
lcu_t *const lcu)
|
2014-09-05 08:29:30 +00:00
|
|
|
{
|
|
|
|
const int width = LCU_WIDTH >> depth;
|
2014-10-01 09:31:10 +00:00
|
|
|
const int width_c = width > TR_MIN_WIDTH ? width / 2 : width;
|
|
|
|
|
2014-09-05 08:29:30 +00:00
|
|
|
const int offset = width / 2;
|
2015-03-04 11:32:11 +00:00
|
|
|
const vector2d_t lcu_px = { x_px & 0x3f, y_px & 0x3f };
|
2015-03-04 11:15:45 +00:00
|
|
|
cu_info_t *const tr_cu = &lcu->cu[LCU_CU_OFFSET + (lcu_px.x >> 3) + (lcu_px.y >> 3)*LCU_T_CU_WIDTH];
|
2014-09-05 08:29:30 +00:00
|
|
|
|
2014-10-01 13:51:49 +00:00
|
|
|
const bool reconstruct_chroma = !(x_px & 4 || y_px & 4);
|
|
|
|
|
2014-10-01 09:31:10 +00:00
|
|
|
struct {
|
2015-03-04 14:35:17 +00:00
|
|
|
pixel_t y[TR_MAX_WIDTH*TR_MAX_WIDTH];
|
|
|
|
pixel_t u[TR_MAX_WIDTH*TR_MAX_WIDTH];
|
|
|
|
pixel_t v[TR_MAX_WIDTH*TR_MAX_WIDTH];
|
2014-10-01 09:31:10 +00:00
|
|
|
} nosplit_pixels;
|
2014-10-01 15:06:28 +00:00
|
|
|
cu_cbf_t nosplit_cbf;
|
2014-09-05 08:29:30 +00:00
|
|
|
|
2014-09-17 08:52:22 +00:00
|
|
|
double split_cost = INT32_MAX;
|
|
|
|
double nosplit_cost = INT32_MAX;
|
2014-09-05 08:29:30 +00:00
|
|
|
|
|
|
|
assert(width >= TR_MIN_WIDTH);
|
|
|
|
|
|
|
|
if (depth > 0) {
|
|
|
|
tr_cu->tr_depth = depth;
|
2014-09-23 12:17:56 +00:00
|
|
|
pred_cu->tr_depth = depth;
|
2014-09-05 08:29:30 +00:00
|
|
|
|
2014-10-01 09:31:10 +00:00
|
|
|
nosplit_cost = 0.0;
|
|
|
|
|
2014-10-01 15:06:28 +00:00
|
|
|
cbf_clear(&pred_cu->cbf.y, depth + PU_INDEX(x_px / 4, y_px / 4));
|
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
intra_recon_lcu_luma(state, x_px, y_px, depth, intra_mode, pred_cu, lcu);
|
|
|
|
nosplit_cost += cu_rd_cost_luma(state, lcu_px.x, lcu_px.y, depth, pred_cu, lcu);
|
2014-09-05 08:29:30 +00:00
|
|
|
|
2014-10-01 13:51:49 +00:00
|
|
|
if (reconstruct_chroma) {
|
2014-10-01 15:06:28 +00:00
|
|
|
cbf_clear(&pred_cu->cbf.u, depth);
|
|
|
|
cbf_clear(&pred_cu->cbf.v, depth);
|
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
intra_recon_lcu_chroma(state, x_px, y_px, depth, intra_mode, pred_cu, lcu);
|
|
|
|
nosplit_cost += cu_rd_cost_chroma(state, lcu_px.x, lcu_px.y, depth, pred_cu, lcu);
|
2014-10-01 09:31:10 +00:00
|
|
|
}
|
2014-09-05 08:29:30 +00:00
|
|
|
|
|
|
|
// Early stop codition for the recursive search.
|
|
|
|
// If the cost of any 1/4th of the transform is already larger than the
|
|
|
|
// whole transform, assume that splitting further is a bad idea.
|
|
|
|
if (nosplit_cost >= cost_treshold) {
|
|
|
|
return nosplit_cost;
|
|
|
|
}
|
|
|
|
|
2014-10-01 15:06:28 +00:00
|
|
|
nosplit_cbf = pred_cu->cbf;
|
|
|
|
|
2014-10-01 09:31:10 +00:00
|
|
|
pixels_blit(lcu->rec.y, nosplit_pixels.y, width, width, LCU_WIDTH, width);
|
2014-10-01 13:51:49 +00:00
|
|
|
if (reconstruct_chroma) {
|
2014-10-01 09:31:10 +00:00
|
|
|
pixels_blit(lcu->rec.u, nosplit_pixels.u, width_c, width_c, LCU_WIDTH_C, width_c);
|
|
|
|
pixels_blit(lcu->rec.v, nosplit_pixels.v, width_c, width_c, LCU_WIDTH_C, width_c);
|
|
|
|
}
|
2014-09-05 08:29:30 +00:00
|
|
|
}
|
|
|
|
|
2014-10-01 13:51:49 +00:00
|
|
|
// Recurse further if all of the following:
|
|
|
|
// - Current depth is less than maximum depth of the search (max_depth).
|
|
|
|
// - Maximum transform hierarchy depth is constrained by clipping
|
|
|
|
// max_depth.
|
|
|
|
// - Min transform size hasn't been reached (MAX_PU_DEPTH).
|
2014-09-05 08:29:30 +00:00
|
|
|
if (depth < max_depth && depth < MAX_PU_DEPTH) {
|
2015-03-04 15:00:23 +00:00
|
|
|
split_cost = 3 * state->global->cur_lambda_cost;
|
2014-09-05 08:29:30 +00:00
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
split_cost += search_intra_trdepth(state, x_px, y_px, depth + 1, max_depth, intra_mode, nosplit_cost, pred_cu, lcu);
|
2014-09-05 08:29:30 +00:00
|
|
|
if (split_cost < nosplit_cost) {
|
2015-03-04 15:00:23 +00:00
|
|
|
split_cost += search_intra_trdepth(state, x_px + offset, y_px, depth + 1, max_depth, intra_mode, nosplit_cost, pred_cu, lcu);
|
2014-09-05 08:29:30 +00:00
|
|
|
}
|
|
|
|
if (split_cost < nosplit_cost) {
|
2015-03-04 15:00:23 +00:00
|
|
|
split_cost += search_intra_trdepth(state, x_px, y_px + offset, depth + 1, max_depth, intra_mode, nosplit_cost, pred_cu, lcu);
|
2014-09-05 08:29:30 +00:00
|
|
|
}
|
|
|
|
if (split_cost < nosplit_cost) {
|
2015-03-04 15:00:23 +00:00
|
|
|
split_cost += search_intra_trdepth(state, x_px + offset, y_px + offset, depth + 1, max_depth, intra_mode, nosplit_cost, pred_cu, lcu);
|
2014-09-05 08:29:30 +00:00
|
|
|
}
|
2014-10-01 09:32:29 +00:00
|
|
|
|
|
|
|
double tr_split_bit = 0.0;
|
|
|
|
double cbf_bits = 0.0;
|
|
|
|
|
2014-10-01 13:51:49 +00:00
|
|
|
// Add bits for split_transform_flag = 1, because transform depth search bypasses
|
|
|
|
// the normal recursion in the cost functions.
|
|
|
|
if (depth >= 1 && depth <= 3) {
|
2015-03-04 15:00:23 +00:00
|
|
|
const cabac_ctx_t *ctx = &(state->cabac.ctx.trans_subdiv_model[5 - (6 - depth)]);
|
2014-10-01 09:32:29 +00:00
|
|
|
tr_split_bit += CTX_ENTROPY_FBITS(ctx, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add cost of cbf chroma bits on transform tree.
|
|
|
|
// All cbf bits are accumulated to pred_cu.cbf and cbf_is_set returns true
|
|
|
|
// if cbf is set at any level >= depth, so cbf chroma is assumed to be 0
|
|
|
|
// if this and any previous transform block has no chroma coefficients.
|
|
|
|
// When searching the first block we don't actually know the real values,
|
|
|
|
// so this will code cbf as 0 and not code the cbf at all for descendants.
|
2014-10-01 13:51:49 +00:00
|
|
|
{
|
|
|
|
const uint8_t tr_depth = depth - pred_cu->depth;
|
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
const cabac_ctx_t *ctx = &(state->cabac.ctx.qt_cbf_model_chroma[tr_depth]);
|
2014-10-01 09:32:29 +00:00
|
|
|
if (tr_depth == 0 || cbf_is_set(pred_cu->cbf.u, depth - 1)) {
|
|
|
|
cbf_bits += CTX_ENTROPY_FBITS(ctx, cbf_is_set(pred_cu->cbf.u, depth));
|
|
|
|
}
|
|
|
|
if (tr_depth == 0 || cbf_is_set(pred_cu->cbf.v, depth - 1)) {
|
|
|
|
cbf_bits += CTX_ENTROPY_FBITS(ctx, cbf_is_set(pred_cu->cbf.v, depth));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
double bits = tr_split_bit + cbf_bits;
|
2015-03-04 15:00:23 +00:00
|
|
|
split_cost += bits * state->global->cur_lambda_cost;
|
2014-09-05 08:29:30 +00:00
|
|
|
} else {
|
|
|
|
assert(width <= TR_MAX_WIDTH);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (depth == 0 || split_cost < nosplit_cost) {
|
|
|
|
return split_cost;
|
|
|
|
} else {
|
|
|
|
lcu_set_trdepth(lcu, x_px, y_px, depth, depth);
|
|
|
|
|
2014-10-01 15:06:28 +00:00
|
|
|
pred_cu->cbf = nosplit_cbf;
|
|
|
|
|
2014-09-05 08:29:30 +00:00
|
|
|
// We only restore the pixel data and not coefficients or cbf data.
|
2014-10-15 11:15:10 +00:00
|
|
|
// The only thing we really need are the border pixels.intra_get_dir_luma_predictor
|
2014-10-01 09:31:10 +00:00
|
|
|
pixels_blit(nosplit_pixels.y, lcu->rec.y, width, width, width, LCU_WIDTH);
|
2014-10-01 13:51:49 +00:00
|
|
|
if (reconstruct_chroma) {
|
2014-10-01 09:31:10 +00:00
|
|
|
pixels_blit(nosplit_pixels.u, lcu->rec.u, width_c, width_c, width_c, LCU_WIDTH_C);
|
|
|
|
pixels_blit(nosplit_pixels.v, lcu->rec.v, width_c, width_c, width_c, LCU_WIDTH_C);
|
|
|
|
}
|
2014-09-05 08:29:30 +00:00
|
|
|
|
|
|
|
return nosplit_cost;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-22 08:00:51 +00:00
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
static double luma_mode_bits(const encoder_state_t *state, int8_t luma_mode, const int8_t *intra_preds)
|
2014-10-15 11:15:10 +00:00
|
|
|
{
|
|
|
|
double mode_bits;
|
|
|
|
|
|
|
|
bool mode_in_preds = false;
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
if (luma_mode == intra_preds[i]) {
|
|
|
|
mode_in_preds = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
const cabac_ctx_t *ctx = &(state->cabac.ctx.intra_mode_model);
|
2014-10-15 11:15:10 +00:00
|
|
|
mode_bits = CTX_ENTROPY_FBITS(ctx, mode_in_preds);
|
|
|
|
|
|
|
|
if (mode_in_preds) {
|
|
|
|
mode_bits += ((luma_mode == intra_preds[0]) ? 1 : 2);
|
|
|
|
} else {
|
|
|
|
mode_bits += 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mode_bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
static double chroma_mode_bits(const encoder_state_t *state, int8_t chroma_mode, int8_t luma_mode)
|
2014-10-15 13:01:58 +00:00
|
|
|
{
|
2015-03-04 15:00:23 +00:00
|
|
|
const cabac_ctx_t *ctx = &(state->cabac.ctx.chroma_pred_model[0]);
|
2014-10-15 13:01:58 +00:00
|
|
|
double mode_bits;
|
|
|
|
if (chroma_mode == luma_mode) {
|
|
|
|
mode_bits = CTX_ENTROPY_FBITS(ctx, 0);
|
|
|
|
} else {
|
|
|
|
mode_bits = 2.0 + CTX_ENTROPY_FBITS(ctx, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mode_bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
static int8_t search_intra_chroma(encoder_state_t * const state,
|
|
|
|
int x_px, int y_px, int depth,
|
|
|
|
int8_t intra_mode,
|
|
|
|
int8_t modes[5], int8_t num_modes,
|
|
|
|
lcu_t *const lcu)
|
2014-10-15 13:01:58 +00:00
|
|
|
{
|
|
|
|
const bool reconstruct_chroma = !(x_px & 4 || y_px & 4);
|
|
|
|
|
|
|
|
if (reconstruct_chroma) {
|
2015-03-04 11:32:11 +00:00
|
|
|
const vector2d_t lcu_px = { x_px & 0x3f, y_px & 0x3f };
|
2015-03-04 11:15:45 +00:00
|
|
|
cu_info_t *const tr_cu = &lcu->cu[LCU_CU_OFFSET + (lcu_px.x >> 3) + (lcu_px.y >> 3)*LCU_T_CU_WIDTH];
|
2014-10-15 13:01:58 +00:00
|
|
|
|
|
|
|
struct {
|
|
|
|
double cost;
|
|
|
|
int8_t mode;
|
|
|
|
} chroma, best_chroma;
|
|
|
|
|
|
|
|
best_chroma.mode = 0;
|
|
|
|
best_chroma.cost = MAX_INT;
|
|
|
|
|
2014-10-15 20:07:28 +00:00
|
|
|
for (int8_t chroma_mode_i = 0; chroma_mode_i < num_modes; ++chroma_mode_i) {
|
|
|
|
chroma.mode = modes[chroma_mode_i];
|
2014-10-15 13:01:58 +00:00
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
intra_recon_lcu_chroma(state, x_px, y_px, depth, chroma.mode, NULL, lcu);
|
|
|
|
chroma.cost = cu_rd_cost_chroma(state, lcu_px.x, lcu_px.y, depth, tr_cu, lcu);
|
2014-10-15 13:01:58 +00:00
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
double mode_bits = chroma_mode_bits(state, chroma.mode, intra_mode);
|
|
|
|
chroma.cost += mode_bits * state->global->cur_lambda_cost;
|
2014-10-15 13:01:58 +00:00
|
|
|
|
|
|
|
if (chroma.cost < best_chroma.cost) {
|
|
|
|
best_chroma = chroma;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return best_chroma.mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 100;
|
|
|
|
}
|
|
|
|
|
2015-02-04 12:27:38 +00:00
|
|
|
/**
|
|
|
|
* \brief Sort modes and costs to ascending order according to costs.
|
|
|
|
*/
|
2015-02-13 13:57:21 +00:00
|
|
|
static INLINE void sort_modes(int8_t *__restrict modes, double *__restrict costs, uint8_t length)
|
2014-05-19 10:18:06 +00:00
|
|
|
{
|
2015-02-13 13:57:21 +00:00
|
|
|
// Length is always between 5 and 23, and is either 21, 17, 9 or 8 about
|
|
|
|
// 60% of the time, so there should be no need for anything more complex
|
|
|
|
// than insertion sort.
|
2015-02-04 12:27:38 +00:00
|
|
|
for (uint8_t i = 1; i < length; ++i) {
|
|
|
|
const double cur_cost = costs[i];
|
|
|
|
const int8_t cur_mode = modes[i];
|
|
|
|
uint8_t j = i;
|
|
|
|
while (j > 0 && cur_cost < costs[j - 1]) {
|
|
|
|
costs[j] = costs[j - 1];
|
|
|
|
modes[j] = modes[j - 1];
|
2014-05-22 08:00:51 +00:00
|
|
|
--j;
|
|
|
|
}
|
2015-02-04 12:27:38 +00:00
|
|
|
costs[j] = cur_cost;
|
|
|
|
modes[j] = cur_mode;
|
2014-05-22 08:00:51 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-19 10:18:06 +00:00
|
|
|
|
2015-02-13 13:57:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Select mode with the smallest cost.
|
|
|
|
*/
|
2015-04-08 12:34:39 +00:00
|
|
|
static INLINE uint8_t select_best_mode_index(const int8_t *modes, const double *costs, uint8_t length)
|
2015-02-13 13:57:21 +00:00
|
|
|
{
|
2015-04-08 12:34:39 +00:00
|
|
|
uint8_t best_index = 0;
|
2015-02-13 13:57:21 +00:00
|
|
|
double best_cost = costs[0];
|
|
|
|
|
|
|
|
for (uint8_t i = 1; i < length; ++i) {
|
|
|
|
if (costs[i] < best_cost) {
|
|
|
|
best_cost = costs[i];
|
2015-04-08 12:34:39 +00:00
|
|
|
best_index = i;
|
2015-02-13 13:57:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:34:39 +00:00
|
|
|
return best_index;
|
2015-02-13 13:57:21 +00:00
|
|
|
}
|
|
|
|
|
2015-01-07 10:48:34 +00:00
|
|
|
/**
|
|
|
|
* \brief Calculate quality of the reconstruction.
|
|
|
|
*
|
|
|
|
* \param pred Predicted pixels in continous memory.
|
|
|
|
* \param orig_block Orignal (target) pixels in continous memory.
|
|
|
|
* \param satd_func SATD function for this block size.
|
|
|
|
* \param sad_func SAD function this block size.
|
|
|
|
* \param width Pixel width of the block.
|
|
|
|
*
|
|
|
|
* \return Estimated RD cost of the reconstruction and signaling the
|
|
|
|
* coefficients of the residual.
|
|
|
|
*/
|
2015-03-04 15:00:23 +00:00
|
|
|
static double get_cost(encoder_state_t * const state,
|
2015-03-04 14:35:17 +00:00
|
|
|
pixel_t *pred, pixel_t *orig_block,
|
2015-01-07 10:48:34 +00:00
|
|
|
cost_pixel_nxn_func *satd_func,
|
|
|
|
cost_pixel_nxn_func *sad_func,
|
|
|
|
int width)
|
2014-10-08 14:39:55 +00:00
|
|
|
{
|
2014-10-09 16:08:47 +00:00
|
|
|
double satd_cost = satd_func(pred, orig_block);
|
2014-10-15 17:17:38 +00:00
|
|
|
if (TRSKIP_RATIO != 0 && width == 4) {
|
2014-10-08 14:39:55 +00:00
|
|
|
// If the mode looks better with SAD than SATD it might be a good
|
|
|
|
// candidate for transform skip. How much better SAD has to be is
|
2014-10-15 17:17:38 +00:00
|
|
|
// controlled by TRSKIP_RATIO.
|
2015-01-07 10:48:34 +00:00
|
|
|
|
|
|
|
// Add the offset bit costs of signaling 'luma and chroma use trskip',
|
|
|
|
// versus signaling 'luma and chroma don't use trskip' to the SAD cost.
|
2015-03-04 15:00:23 +00:00
|
|
|
const cabac_ctx_t *ctx = &state->cabac.ctx.transform_skip_model_luma;
|
2014-10-13 07:48:39 +00:00
|
|
|
double trskip_bits = CTX_ENTROPY_FBITS(ctx, 1) - CTX_ENTROPY_FBITS(ctx, 0);
|
2015-03-04 15:00:23 +00:00
|
|
|
ctx = &state->cabac.ctx.transform_skip_model_chroma;
|
2014-10-13 07:48:39 +00:00
|
|
|
trskip_bits += 2.0 * (CTX_ENTROPY_FBITS(ctx, 1) - CTX_ENTROPY_FBITS(ctx, 0));
|
2015-01-07 10:48:34 +00:00
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
double sad_cost = TRSKIP_RATIO * sad_func(pred, orig_block) + state->global->cur_lambda_cost_sqrt * trskip_bits;
|
2014-10-09 16:08:47 +00:00
|
|
|
if (sad_cost < satd_cost) {
|
|
|
|
return sad_cost;
|
2014-10-08 14:39:55 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-09 16:08:47 +00:00
|
|
|
return satd_cost;
|
2014-10-08 14:39:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
static void search_intra_chroma_rough(encoder_state_t * const state,
|
2014-10-15 19:11:45 +00:00
|
|
|
int x_px, int y_px, int depth,
|
2015-03-04 14:35:17 +00:00
|
|
|
const pixel_t *orig_u, const pixel_t *orig_v, int16_t origstride,
|
|
|
|
const pixel_t *rec_u, const pixel_t *rec_v, int16_t recstride,
|
2014-10-15 19:11:45 +00:00
|
|
|
int8_t luma_mode,
|
2014-10-15 21:42:22 +00:00
|
|
|
int8_t modes[5], double costs[5])
|
2014-10-15 19:11:45 +00:00
|
|
|
{
|
|
|
|
const bool reconstruct_chroma = !(x_px & 4 || y_px & 4);
|
|
|
|
if (!reconstruct_chroma) return;
|
|
|
|
|
|
|
|
const unsigned width = MAX(LCU_WIDTH_C >> depth, TR_MIN_WIDTH);
|
|
|
|
|
2014-10-15 20:07:28 +00:00
|
|
|
for (int i = 0; i < 5; ++i) {
|
|
|
|
costs[i] = 0;
|
|
|
|
}
|
|
|
|
|
2014-10-15 19:11:45 +00:00
|
|
|
cost_pixel_nxn_func *const satd_func = pixels_get_satd_func(width);
|
|
|
|
//cost_pixel_nxn_func *const sad_func = pixels_get_sad_func(width);
|
|
|
|
|
2015-03-04 14:35:17 +00:00
|
|
|
pixel_t _pred[LCU_WIDTH * LCU_WIDTH + 1 + SIMD_ALIGNMENT];
|
|
|
|
pixel_t *pred = ALIGNED_POINTER(_pred, SIMD_ALIGNMENT);
|
2014-10-15 19:11:45 +00:00
|
|
|
|
2015-03-04 14:35:17 +00:00
|
|
|
pixel_t _orig_block[LCU_WIDTH * LCU_WIDTH + 1 + SIMD_ALIGNMENT];
|
|
|
|
pixel_t *orig_block = ALIGNED_POINTER(_orig_block, SIMD_ALIGNMENT);
|
2014-10-15 19:11:45 +00:00
|
|
|
|
|
|
|
pixels_blit(orig_u, orig_block, width, width, origstride, width);
|
|
|
|
for (int i = 0; i < 5; ++i) {
|
2014-10-15 20:07:28 +00:00
|
|
|
if (modes[i] == luma_mode) continue;
|
2015-03-04 15:00:23 +00:00
|
|
|
intra_get_pred(state->encoder_control, rec_u, NULL, recstride, pred, width, modes[i], 1);
|
2014-10-15 19:11:45 +00:00
|
|
|
//costs[i] += get_cost(encoder_state, pred, orig_block, satd_func, sad_func, width);
|
|
|
|
costs[i] += satd_func(pred, orig_block);
|
|
|
|
}
|
|
|
|
|
|
|
|
pixels_blit(orig_v, orig_block, width, width, origstride, width);
|
|
|
|
for (int i = 0; i < 5; ++i) {
|
2014-10-15 20:07:28 +00:00
|
|
|
if (modes[i] == luma_mode) continue;
|
2015-03-04 15:00:23 +00:00
|
|
|
intra_get_pred(state->encoder_control, rec_v, NULL, recstride, pred, width, modes[i], 2);
|
2014-10-15 19:11:45 +00:00
|
|
|
//costs[i] += get_cost(encoder_state, pred, orig_block, satd_func, sad_func, width);
|
|
|
|
costs[i] += satd_func(pred, orig_block);
|
|
|
|
}
|
|
|
|
|
|
|
|
sort_modes(modes, costs, 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-07 10:48:34 +00:00
|
|
|
/**
|
|
|
|
* \brief Order the intra prediction modes according to a fast criteria.
|
|
|
|
*
|
|
|
|
* This function uses SATD to order the intra prediction modes. For 4x4 modes
|
|
|
|
* SAD might be used instead, if the cost given by SAD is much better than the
|
|
|
|
* one given by SATD, to take into account that 4x4 modes can be coded with
|
|
|
|
* transform skip.
|
|
|
|
*
|
|
|
|
* The modes are searched using halving search and the total number of modes
|
|
|
|
* that are tried is dependent on size of the predicted block. More modes
|
|
|
|
* are tried for smaller blocks.
|
|
|
|
*
|
|
|
|
* \param orig Pointer to the top-left corner of current CU in the picture
|
|
|
|
* being encoded.
|
|
|
|
* \param orig_stride Stride of param orig.
|
|
|
|
* \param rec Pointer to the top-left corner of current CU in the picture
|
|
|
|
* being encoded.
|
|
|
|
* \param rec_stride Stride of param rec.
|
|
|
|
* \param width Width of the prediction block.
|
|
|
|
* \param intra_preds Array of the 3 predicted intra modes.
|
|
|
|
*
|
|
|
|
* \param[out] modes The modes ordered according to their RD costs, from best
|
|
|
|
* to worst. The number of modes and costs output is given by parameter
|
|
|
|
* modes_to_check.
|
|
|
|
* \param[out] costs The RD costs of corresponding modes in param modes.
|
|
|
|
*
|
|
|
|
* \return Number of prediction modes in param modes.
|
|
|
|
*/
|
2015-03-04 15:00:23 +00:00
|
|
|
static int8_t search_intra_rough(encoder_state_t * const state,
|
2015-03-04 14:35:17 +00:00
|
|
|
pixel_t *orig, int32_t origstride,
|
|
|
|
pixel_t *rec, int16_t recstride,
|
2014-06-17 12:09:12 +00:00
|
|
|
int width, int8_t *intra_preds,
|
2014-10-06 14:44:15 +00:00
|
|
|
int8_t modes[35], double costs[35])
|
2014-05-22 08:00:51 +00:00
|
|
|
{
|
2014-10-08 14:39:55 +00:00
|
|
|
cost_pixel_nxn_func *satd_func = pixels_get_satd_func(width);
|
|
|
|
cost_pixel_nxn_func *sad_func = pixels_get_sad_func(width);
|
2014-05-19 10:18:06 +00:00
|
|
|
|
|
|
|
// Temporary block arrays
|
2015-03-04 14:35:17 +00:00
|
|
|
pixel_t _pred[LCU_WIDTH * LCU_WIDTH + 1 + SIMD_ALIGNMENT];
|
|
|
|
pixel_t *pred = ALIGNED_POINTER(_pred, SIMD_ALIGNMENT);
|
2014-07-10 14:41:13 +00:00
|
|
|
|
2015-03-04 14:35:17 +00:00
|
|
|
pixel_t _orig_block[LCU_WIDTH * LCU_WIDTH + 1 + SIMD_ALIGNMENT];
|
|
|
|
pixel_t *orig_block = ALIGNED_POINTER(_orig_block, SIMD_ALIGNMENT);
|
2014-07-10 14:41:13 +00:00
|
|
|
|
2015-03-04 14:35:17 +00:00
|
|
|
pixel_t rec_filtered_temp[(LCU_WIDTH * 2 + 8) * (LCU_WIDTH * 2 + 8) + 1];
|
2014-05-19 10:18:06 +00:00
|
|
|
|
2015-03-04 14:35:17 +00:00
|
|
|
pixel_t *recf = &rec_filtered_temp[recstride + 1];
|
2014-05-19 10:18:06 +00:00
|
|
|
|
2014-05-22 08:00:51 +00:00
|
|
|
assert(width == 4 || width == 8 || width == 16 || width == 32);
|
|
|
|
|
2014-05-19 10:18:06 +00:00
|
|
|
// Store original block for SAD computation
|
2014-06-05 12:54:58 +00:00
|
|
|
pixels_blit(orig, orig_block, width, width, origstride, width);
|
2014-05-19 10:18:06 +00:00
|
|
|
|
|
|
|
// Generate filtered reference pixels.
|
|
|
|
{
|
|
|
|
int16_t x, y;
|
|
|
|
for (y = -1; y < recstride; y++) {
|
2014-10-16 09:35:28 +00:00
|
|
|
recf[y*recstride - 1] = rec[y*recstride - 1];
|
2014-10-08 14:39:55 +00:00
|
|
|
}
|
2014-05-19 10:18:06 +00:00
|
|
|
for (x = 0; x < recstride; x++) {
|
2014-10-16 09:35:28 +00:00
|
|
|
recf[x - recstride] = rec[x - recstride];
|
2014-05-19 10:18:06 +00:00
|
|
|
}
|
2014-10-16 09:35:28 +00:00
|
|
|
intra_filter(recf, recstride, width, 0);
|
2014-05-19 10:18:06 +00:00
|
|
|
}
|
2014-06-17 12:09:12 +00:00
|
|
|
|
|
|
|
int8_t modes_selected = 0;
|
2014-06-17 10:11:35 +00:00
|
|
|
unsigned min_cost = UINT_MAX;
|
|
|
|
unsigned max_cost = 0;
|
2014-06-17 12:09:12 +00:00
|
|
|
|
|
|
|
// Initial offset decides how many modes are tried before moving on to the
|
|
|
|
// recursive search.
|
|
|
|
int offset;
|
2015-03-04 15:00:23 +00:00
|
|
|
if (state->encoder_control->full_intra_search) {
|
2014-06-17 12:32:05 +00:00
|
|
|
offset = 1;
|
|
|
|
} else if (width == 4) {
|
2014-06-17 10:20:22 +00:00
|
|
|
offset = 2;
|
|
|
|
} else if (width == 8) {
|
|
|
|
offset = 4;
|
2014-06-17 12:09:12 +00:00
|
|
|
} else {
|
|
|
|
offset = 8;
|
2014-06-17 10:20:22 +00:00
|
|
|
}
|
2014-06-17 09:24:17 +00:00
|
|
|
|
2014-06-17 12:09:12 +00:00
|
|
|
// Calculate SAD for evenly spaced modes to select the starting point for
|
|
|
|
// the recursive search.
|
2014-06-17 10:20:22 +00:00
|
|
|
for (int mode = 2; mode <= 34; mode += offset) {
|
2015-03-04 15:00:23 +00:00
|
|
|
intra_get_pred(state->encoder_control, rec, recf, recstride, pred, width, mode, 0);
|
|
|
|
costs[modes_selected] = get_cost(state, pred, orig_block, satd_func, sad_func, width);
|
2014-06-12 07:28:30 +00:00
|
|
|
modes[modes_selected] = mode;
|
2014-06-17 10:11:35 +00:00
|
|
|
|
|
|
|
min_cost = MIN(min_cost, costs[modes_selected]);
|
|
|
|
max_cost = MAX(max_cost, costs[modes_selected]);
|
|
|
|
|
2014-06-17 09:24:17 +00:00
|
|
|
++modes_selected;
|
2014-06-12 08:37:37 +00:00
|
|
|
}
|
2015-02-13 13:57:21 +00:00
|
|
|
|
2015-04-08 12:34:39 +00:00
|
|
|
int8_t best_mode = modes[select_best_mode_index(modes, costs, modes_selected)];
|
2015-02-13 13:57:21 +00:00
|
|
|
double best_cost = min_cost;
|
2014-06-17 09:24:17 +00:00
|
|
|
|
2014-06-17 12:09:12 +00:00
|
|
|
// Skip recursive search if all modes have the same cost.
|
2014-06-17 10:11:35 +00:00
|
|
|
if (min_cost != max_cost) {
|
2014-06-17 12:09:12 +00:00
|
|
|
// Do a recursive search to find the best mode, always centering on the
|
|
|
|
// current best mode.
|
2014-06-17 10:20:22 +00:00
|
|
|
while (offset > 1) {
|
|
|
|
offset >>= 1;
|
2014-06-17 10:11:35 +00:00
|
|
|
|
2015-02-13 13:57:21 +00:00
|
|
|
int8_t center_node = best_mode;
|
|
|
|
int8_t mode = center_node - offset;
|
2014-06-17 10:11:35 +00:00
|
|
|
if (mode >= 2) {
|
2015-03-04 15:00:23 +00:00
|
|
|
intra_get_pred(state->encoder_control, rec, recf, recstride, pred, width, mode, 0);
|
|
|
|
costs[modes_selected] = get_cost(state, pred, orig_block, satd_func, sad_func, width);
|
2014-06-17 10:11:35 +00:00
|
|
|
modes[modes_selected] = mode;
|
2015-02-13 13:57:21 +00:00
|
|
|
if (costs[modes_selected] < best_cost) {
|
|
|
|
best_cost = costs[modes_selected];
|
|
|
|
best_mode = modes[modes_selected];
|
|
|
|
}
|
2014-06-17 10:11:35 +00:00
|
|
|
++modes_selected;
|
|
|
|
}
|
2014-06-17 09:24:17 +00:00
|
|
|
|
2015-02-13 13:57:21 +00:00
|
|
|
mode = center_node + offset;
|
2014-06-17 10:11:35 +00:00
|
|
|
if (mode <= 34) {
|
2015-03-04 15:00:23 +00:00
|
|
|
intra_get_pred(state->encoder_control, rec, recf, recstride, pred, width, mode, 0);
|
|
|
|
costs[modes_selected] = get_cost(state, pred, orig_block, satd_func, sad_func, width);
|
2014-06-17 10:11:35 +00:00
|
|
|
modes[modes_selected] = mode;
|
2015-02-13 13:57:21 +00:00
|
|
|
if (costs[modes_selected] < best_cost) {
|
|
|
|
best_cost = costs[modes_selected];
|
|
|
|
best_mode = modes[modes_selected];
|
|
|
|
}
|
2014-06-17 10:11:35 +00:00
|
|
|
++modes_selected;
|
|
|
|
}
|
2014-06-17 09:24:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-17 09:51:15 +00:00
|
|
|
int8_t add_modes[5] = {intra_preds[0], intra_preds[1], intra_preds[2], 0, 1};
|
|
|
|
|
|
|
|
// Add DC, planar and missing predicted modes.
|
|
|
|
for (int8_t pred_i = 0; pred_i < 5; ++pred_i) {
|
2014-06-17 09:24:17 +00:00
|
|
|
bool has_mode = false;
|
2014-06-17 09:51:15 +00:00
|
|
|
int8_t mode = add_modes[pred_i];
|
|
|
|
|
2014-06-17 09:24:17 +00:00
|
|
|
for (int mode_i = 0; mode_i < modes_selected; ++mode_i) {
|
2014-06-17 09:51:15 +00:00
|
|
|
if (modes[mode_i] == add_modes[pred_i]) {
|
2014-06-17 09:24:17 +00:00
|
|
|
has_mode = true;
|
|
|
|
break;
|
|
|
|
}
|
2014-06-12 08:37:37 +00:00
|
|
|
}
|
2014-06-17 09:24:17 +00:00
|
|
|
|
|
|
|
if (!has_mode) {
|
2015-03-04 15:00:23 +00:00
|
|
|
intra_get_pred(state->encoder_control, rec, recf, recstride, pred, width, mode, 0);
|
|
|
|
costs[modes_selected] = get_cost(state, pred, orig_block, satd_func, sad_func, width);
|
2014-06-12 08:37:37 +00:00
|
|
|
modes[modes_selected] = mode;
|
2014-06-17 09:24:17 +00:00
|
|
|
++modes_selected;
|
2014-06-12 08:37:37 +00:00
|
|
|
}
|
2014-05-19 10:18:06 +00:00
|
|
|
}
|
|
|
|
|
2014-06-17 09:24:17 +00:00
|
|
|
// Add prediction mode coding cost as the last thing. We don't want this
|
|
|
|
// affecting the halving search.
|
2015-03-04 15:00:23 +00:00
|
|
|
int lambda_cost = (int)(state->global->cur_lambda_cost_sqrt + 0.5);
|
2014-06-17 09:24:17 +00:00
|
|
|
for (int mode_i = 0; mode_i < modes_selected; ++mode_i) {
|
2015-03-04 15:00:23 +00:00
|
|
|
costs[mode_i] += lambda_cost * luma_mode_bits(state, modes[mode_i], intra_preds);
|
2014-06-17 09:24:17 +00:00
|
|
|
}
|
|
|
|
|
2014-06-12 07:28:30 +00:00
|
|
|
return modes_selected;
|
2014-05-22 08:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-07 10:48:34 +00:00
|
|
|
/**
|
|
|
|
* \brief Find best intra mode out of the ones listed in parameter modes.
|
|
|
|
*
|
|
|
|
* This function perform intra search by doing full quantization,
|
|
|
|
* reconstruction and CABAC coding of coefficients. It is very slow
|
|
|
|
* but results in better RD quality than using just the rough search.
|
|
|
|
*
|
|
|
|
* \param x_px Luma picture coordinate.
|
|
|
|
* \param y_px Luma picture coordinate.
|
|
|
|
* \param orig Pointer to the top-left corner of current CU in the picture
|
|
|
|
* being encoded.
|
|
|
|
* \param orig_stride Stride of param orig.
|
|
|
|
* \param rec Pointer to the top-left corner of current CU in the picture
|
|
|
|
* being encoded.
|
|
|
|
* \param rec_stride Stride of param rec.
|
|
|
|
* \param intra_preds Array of the 3 predicted intra modes.
|
|
|
|
* \param modes_to_check How many of the modes in param modes are checked.
|
|
|
|
* \param[in] modes The intra prediction modes that are to be checked.
|
|
|
|
*
|
|
|
|
* \param[out] modes The modes ordered according to their RD costs, from best
|
|
|
|
* to worst. The number of modes and costs output is given by parameter
|
|
|
|
* modes_to_check.
|
|
|
|
* \param[out] costs The RD costs of corresponding modes in param modes.
|
|
|
|
* \param[out] lcu If transform split searching is used, the transform split
|
|
|
|
* information for the best mode is saved in lcu.cu structure.
|
|
|
|
*/
|
2015-03-04 15:00:23 +00:00
|
|
|
static int8_t search_intra_rdo(encoder_state_t * const state,
|
2014-09-05 08:29:30 +00:00
|
|
|
int x_px, int y_px, int depth,
|
2015-03-04 14:35:17 +00:00
|
|
|
pixel_t *orig, int32_t origstride,
|
|
|
|
pixel_t *rec, int16_t recstride,
|
2014-09-05 08:29:30 +00:00
|
|
|
int8_t *intra_preds,
|
|
|
|
int modes_to_check,
|
2014-10-06 14:44:15 +00:00
|
|
|
int8_t modes[35], double costs[35],
|
2014-09-05 08:29:30 +00:00
|
|
|
lcu_t *lcu)
|
2014-05-22 08:00:51 +00:00
|
|
|
{
|
2015-03-04 15:00:23 +00:00
|
|
|
const int tr_depth = CLIP(1, MAX_PU_DEPTH, depth + state->encoder_control->tr_depth_intra);
|
2014-09-05 08:29:30 +00:00
|
|
|
const int width = LCU_WIDTH >> depth;
|
|
|
|
|
2015-03-04 14:35:17 +00:00
|
|
|
pixel_t pred[LCU_WIDTH * LCU_WIDTH + 1];
|
|
|
|
pixel_t orig_block[LCU_WIDTH * LCU_WIDTH + 1];
|
2014-05-22 08:00:51 +00:00
|
|
|
int rdo_mode;
|
|
|
|
int pred_mode;
|
|
|
|
|
2015-03-04 14:35:17 +00:00
|
|
|
pixel_t rec_filtered_temp[(LCU_WIDTH * 2 + 8) * (LCU_WIDTH * 2 + 8) + 1];
|
|
|
|
pixel_t *recf = &rec_filtered_temp[recstride + 1];
|
2014-05-22 10:06:31 +00:00
|
|
|
|
|
|
|
// Generate filtered reference pixels.
|
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
for (y = -1; y < recstride; y++) {
|
2014-10-16 09:35:28 +00:00
|
|
|
recf[y*recstride - 1] = rec[y*recstride - 1];
|
2014-05-22 10:06:31 +00:00
|
|
|
}
|
|
|
|
for (x = 0; x < recstride; x++) {
|
2014-10-16 09:35:28 +00:00
|
|
|
recf[x - recstride] = rec[x - recstride];
|
2014-05-22 10:06:31 +00:00
|
|
|
}
|
2014-10-16 09:35:28 +00:00
|
|
|
intra_filter(recf, recstride, width, 0);
|
2014-05-22 10:06:31 +00:00
|
|
|
}
|
|
|
|
|
2014-06-05 12:54:58 +00:00
|
|
|
pixels_blit(orig, orig_block, width, width, origstride, width);
|
2014-05-19 10:18:06 +00:00
|
|
|
|
2014-05-22 08:00:51 +00:00
|
|
|
// Check that the predicted modes are in the RDO mode list
|
2015-05-04 13:53:56 +00:00
|
|
|
if (modes_to_check < 35) {
|
|
|
|
for (pred_mode = 0; pred_mode < 3; pred_mode++) {
|
|
|
|
int mode_found = 0;
|
|
|
|
for (rdo_mode = 0; rdo_mode < modes_to_check; rdo_mode++) {
|
|
|
|
if (intra_preds[pred_mode] == modes[rdo_mode]) {
|
|
|
|
mode_found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Add this prediction mode to RDO checking
|
|
|
|
if (!mode_found) {
|
|
|
|
modes[modes_to_check] = intra_preds[pred_mode];
|
|
|
|
modes_to_check++;
|
2014-05-19 10:18:06 +00:00
|
|
|
}
|
2014-05-22 08:00:51 +00:00
|
|
|
}
|
2014-05-19 10:18:06 +00:00
|
|
|
}
|
|
|
|
|
2014-05-22 08:00:51 +00:00
|
|
|
for(rdo_mode = 0; rdo_mode < modes_to_check; rdo_mode ++) {
|
2015-03-04 15:00:23 +00:00
|
|
|
int rdo_bitcost = luma_mode_bits(state, modes[rdo_mode], intra_preds);
|
|
|
|
costs[rdo_mode] = rdo_bitcost * (int)(state->global->cur_lambda_cost + 0.5);
|
2014-09-05 08:29:30 +00:00
|
|
|
|
2015-01-07 10:48:34 +00:00
|
|
|
if (0 && width != 4 && tr_depth == depth) {
|
|
|
|
// This code path has been disabled for now because it increases bdrate
|
|
|
|
// by 1-2 %. Possibly due to not taking chroma into account during luma
|
|
|
|
// mode search. Enabling separate chroma search compensates a little,
|
|
|
|
// but not enough.
|
|
|
|
|
|
|
|
// The idea for this code path is, that it would do the same thing as
|
|
|
|
// the more general search_intra_trdepth, but would only handle cases
|
|
|
|
// where transform split or transform skip don't need to be handled.
|
2015-03-04 15:00:23 +00:00
|
|
|
intra_get_pred(state->encoder_control, rec, recf, recstride, pred, width, modes[rdo_mode], 0);
|
|
|
|
costs[rdo_mode] += rdo_cost_intra(state, pred, orig_block, width, modes[rdo_mode], width == 4 ? 1 : 0);
|
2014-09-05 08:29:30 +00:00
|
|
|
} else {
|
|
|
|
// Perform transform split search and save mode RD cost for the best one.
|
2015-03-04 11:15:45 +00:00
|
|
|
cu_info_t pred_cu;
|
2014-09-05 08:29:30 +00:00
|
|
|
pred_cu.depth = depth;
|
|
|
|
pred_cu.type = CU_INTRA;
|
|
|
|
pred_cu.part_size = ((depth == MAX_PU_DEPTH) ? SIZE_NxN : SIZE_2Nx2N);
|
|
|
|
pred_cu.intra[0].mode = modes[rdo_mode];
|
2014-09-23 12:17:56 +00:00
|
|
|
pred_cu.intra[1].mode = modes[rdo_mode];
|
|
|
|
pred_cu.intra[2].mode = modes[rdo_mode];
|
|
|
|
pred_cu.intra[3].mode = modes[rdo_mode];
|
|
|
|
pred_cu.intra[0].mode_chroma = modes[rdo_mode];
|
2015-02-13 09:56:55 +00:00
|
|
|
FILL(pred_cu.cbf, 0);
|
2014-09-05 08:29:30 +00:00
|
|
|
|
|
|
|
// Reset transform split data in lcu.cu for this area.
|
|
|
|
lcu_set_trdepth(lcu, x_px, y_px, depth, depth);
|
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
double mode_cost = search_intra_trdepth(state, x_px, y_px, depth, tr_depth, modes[rdo_mode], MAX_INT, &pred_cu, lcu);
|
2014-10-06 14:44:15 +00:00
|
|
|
costs[rdo_mode] += mode_cost;
|
2014-09-05 08:29:30 +00:00
|
|
|
}
|
2014-05-22 08:00:51 +00:00
|
|
|
}
|
2014-05-19 10:18:06 +00:00
|
|
|
|
2015-01-07 10:48:34 +00:00
|
|
|
// The best transform split hierarchy is not saved anywhere, so to get the
|
|
|
|
// transform split hierarchy the search has to be performed again with the
|
|
|
|
// best mode.
|
2014-09-10 13:06:38 +00:00
|
|
|
if (tr_depth != depth) {
|
2015-03-04 11:15:45 +00:00
|
|
|
cu_info_t pred_cu;
|
2014-09-10 13:06:38 +00:00
|
|
|
pred_cu.depth = depth;
|
|
|
|
pred_cu.type = CU_INTRA;
|
|
|
|
pred_cu.part_size = ((depth == MAX_PU_DEPTH) ? SIZE_NxN : SIZE_2Nx2N);
|
|
|
|
pred_cu.intra[0].mode = modes[0];
|
2014-09-23 12:17:56 +00:00
|
|
|
pred_cu.intra[1].mode = modes[0];
|
|
|
|
pred_cu.intra[2].mode = modes[0];
|
|
|
|
pred_cu.intra[3].mode = modes[0];
|
|
|
|
pred_cu.intra[0].mode_chroma = modes[0];
|
2015-02-13 09:56:55 +00:00
|
|
|
FILL(pred_cu.cbf, 0);
|
2015-03-04 15:00:23 +00:00
|
|
|
search_intra_trdepth(state, x_px, y_px, depth, tr_depth, modes[0], MAX_INT, &pred_cu, lcu);
|
2014-09-10 13:06:38 +00:00
|
|
|
}
|
2015-02-13 13:57:21 +00:00
|
|
|
|
|
|
|
return modes_to_check;
|
2014-05-19 10:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update lcu to have best modes at this depth.
|
|
|
|
* \return Cost of best mode.
|
|
|
|
*/
|
2015-03-04 15:00:23 +00:00
|
|
|
static double search_cu_intra(encoder_state_t * const state,
|
2014-05-19 10:18:06 +00:00
|
|
|
const int x_px, const int y_px,
|
|
|
|
const int depth, lcu_t *lcu)
|
|
|
|
{
|
2015-03-04 15:00:23 +00:00
|
|
|
const videoframe_t * const frame = state->tile->frame;
|
2015-03-04 11:32:11 +00:00
|
|
|
const vector2d_t lcu_px = { x_px & 0x3f, y_px & 0x3f };
|
|
|
|
const vector2d_t lcu_cu = { lcu_px.x >> 3, lcu_px.y >> 3 };
|
2014-05-19 10:18:06 +00:00
|
|
|
const int8_t cu_width = (LCU_WIDTH >> (depth));
|
|
|
|
const int cu_index = LCU_CU_OFFSET + lcu_cu.x + lcu_cu.y * LCU_T_CU_WIDTH;
|
|
|
|
|
2015-03-04 11:15:45 +00:00
|
|
|
cu_info_t *cur_cu = &lcu->cu[cu_index];
|
2014-05-19 10:18:06 +00:00
|
|
|
|
2015-03-04 14:35:17 +00:00
|
|
|
pixel_t rec_buffer[(LCU_WIDTH * 2 + 1) * (LCU_WIDTH * 2 + 1)];
|
|
|
|
pixel_t *cu_in_rec_buffer = &rec_buffer[cu_width * 2 + 8 + 1];
|
2014-05-19 10:18:06 +00:00
|
|
|
|
|
|
|
int8_t candidate_modes[3];
|
|
|
|
|
2015-03-04 11:15:45 +00:00
|
|
|
cu_info_t *left_cu = 0;
|
|
|
|
cu_info_t *above_cu = 0;
|
2014-05-19 10:18:06 +00:00
|
|
|
|
2015-01-07 10:48:34 +00:00
|
|
|
// Select left and top CUs if they are available.
|
|
|
|
// Top CU is not available across LCU boundary.
|
2014-05-19 10:18:06 +00:00
|
|
|
if ((x_px >> 3) > 0) {
|
|
|
|
left_cu = &lcu->cu[cu_index - 1];
|
|
|
|
}
|
|
|
|
if ((y_px >> 3) > 0 && lcu_cu.y != 0) {
|
|
|
|
above_cu = &lcu->cu[cu_index - LCU_T_CU_WIDTH];
|
|
|
|
}
|
|
|
|
intra_get_dir_luma_predictor(x_px, y_px, candidate_modes, cur_cu, left_cu, above_cu);
|
|
|
|
|
2014-09-08 11:01:55 +00:00
|
|
|
if (depth > 0) {
|
2014-05-19 10:18:06 +00:00
|
|
|
// Build reconstructed block to use in prediction with extrapolated borders
|
2015-03-04 15:00:23 +00:00
|
|
|
intra_build_reference_border(state->encoder_control, x_px, y_px, cu_width * 2 + 8,
|
2014-05-19 10:18:06 +00:00
|
|
|
rec_buffer, cu_width * 2 + 8, 0,
|
2014-06-05 12:54:58 +00:00
|
|
|
frame->width,
|
|
|
|
frame->height,
|
2014-05-19 10:18:06 +00:00
|
|
|
lcu);
|
2014-09-08 11:01:55 +00:00
|
|
|
}
|
2014-05-19 10:18:06 +00:00
|
|
|
|
2014-10-15 11:15:10 +00:00
|
|
|
int8_t modes[35];
|
|
|
|
double costs[35];
|
|
|
|
|
2014-05-19 10:18:06 +00:00
|
|
|
// Find best intra mode for 2Nx2N.
|
2015-04-08 12:34:39 +00:00
|
|
|
pixel_t *ref_pixels = &lcu->ref.y[lcu_px.x + lcu_px.y * LCU_WIDTH];
|
|
|
|
unsigned pu_index = PU_INDEX(x_px >> 2, y_px >> 2);
|
|
|
|
|
|
|
|
int8_t number_of_modes;
|
|
|
|
bool skip_rough_search = (depth == 0 || state->encoder_control->rdo >= 3);
|
|
|
|
if (!skip_rough_search) {
|
|
|
|
number_of_modes = search_intra_rough(state,
|
|
|
|
ref_pixels, LCU_WIDTH,
|
|
|
|
cu_in_rec_buffer, cu_width * 2 + 8,
|
|
|
|
cu_width, candidate_modes,
|
|
|
|
modes, costs);
|
|
|
|
} else {
|
|
|
|
number_of_modes = 35;
|
|
|
|
for (int i = 0; i < number_of_modes; ++i) {
|
|
|
|
modes[i] = i;
|
|
|
|
costs[i] = MAX_INT;
|
2014-09-08 11:01:55 +00:00
|
|
|
}
|
2015-04-08 12:34:39 +00:00
|
|
|
}
|
2014-05-22 08:00:51 +00:00
|
|
|
|
2015-04-08 12:34:39 +00:00
|
|
|
// Set transform depth to current depth, meaning no transform splits.
|
|
|
|
lcu_set_trdepth(lcu, x_px, y_px, depth, depth);
|
2015-02-13 13:57:21 +00:00
|
|
|
|
2015-04-08 12:34:39 +00:00
|
|
|
// Refine results with slower search or get some results if rough search was skipped.
|
|
|
|
if (state->encoder_control->rdo >= 2 || skip_rough_search) {
|
|
|
|
int number_of_modes_to_search;
|
|
|
|
if (state->encoder_control->rdo == 3) {
|
|
|
|
number_of_modes_to_search = 35;
|
|
|
|
} else if (state->encoder_control->rdo == 2) {
|
|
|
|
number_of_modes_to_search = (cu_width <= 8) ? 8 : 3;
|
|
|
|
} else {
|
|
|
|
// Check only the predicted modes.
|
|
|
|
number_of_modes_to_search = 0;
|
2014-05-22 08:00:51 +00:00
|
|
|
}
|
2015-04-08 12:34:39 +00:00
|
|
|
int num_modes_to_check = MIN(number_of_modes, number_of_modes_to_search);
|
2014-05-22 08:00:51 +00:00
|
|
|
|
2015-04-08 12:34:39 +00:00
|
|
|
sort_modes(modes, costs, number_of_modes);
|
|
|
|
number_of_modes = search_intra_rdo(state,
|
|
|
|
x_px, y_px, depth,
|
|
|
|
ref_pixels, LCU_WIDTH,
|
|
|
|
cu_in_rec_buffer, cu_width * 2 + 8,
|
|
|
|
candidate_modes,
|
|
|
|
num_modes_to_check,
|
|
|
|
modes, costs, lcu);
|
2014-05-19 10:18:06 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:34:39 +00:00
|
|
|
uint8_t best_mode_i = select_best_mode_index(modes, costs, number_of_modes);
|
|
|
|
cur_cu->intra[pu_index].mode = modes[best_mode_i];
|
|
|
|
|
|
|
|
return costs[best_mode_i];
|
2014-05-19 10:18:06 +00:00
|
|
|
}
|
|
|
|
|
2015-01-14 08:44:52 +00:00
|
|
|
// Return estimate of bits used to code prediction mode of cur_cu.
|
2015-03-04 15:00:23 +00:00
|
|
|
static double calc_mode_bits(const encoder_state_t *state,
|
2015-03-04 11:15:45 +00:00
|
|
|
const cu_info_t * cur_cu,
|
2015-01-14 08:44:52 +00:00
|
|
|
int x, int y)
|
|
|
|
{
|
|
|
|
double mode_bits;
|
|
|
|
|
|
|
|
if (cur_cu->type == CU_INTER) {
|
|
|
|
mode_bits = cur_cu->inter.bitcost;
|
|
|
|
} else {
|
|
|
|
int8_t candidate_modes[3];
|
|
|
|
{
|
2015-03-04 11:15:45 +00:00
|
|
|
const cu_info_t *left_cu = ((x > 8) ? &cur_cu[-1] : NULL);
|
|
|
|
const cu_info_t *above_cu = ((y > 8) ? &cur_cu[-LCU_T_CU_WIDTH] : NULL);
|
2015-01-14 08:44:52 +00:00
|
|
|
intra_get_dir_luma_predictor(x, y, candidate_modes, cur_cu, left_cu, above_cu);
|
|
|
|
}
|
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
mode_bits = luma_mode_bits(state, cur_cu->intra[PU_INDEX(x >> 2, y >> 2)].mode, candidate_modes);
|
2015-01-14 08:44:52 +00:00
|
|
|
if (PU_INDEX(x >> 2, y >> 2) == 0) {
|
2015-03-04 15:00:23 +00:00
|
|
|
mode_bits += chroma_mode_bits(state, cur_cu->intra[0].mode_chroma, cur_cu->intra[PU_INDEX(x >> 2, y >> 2)].mode);
|
2015-01-14 08:44:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mode_bits;
|
|
|
|
}
|
2014-05-19 10:18:06 +00:00
|
|
|
|
2015-01-14 09:16:34 +00:00
|
|
|
static uint8_t get_ctx_cu_split_model(const lcu_t *lcu, int x, int y, int depth)
|
|
|
|
{
|
2015-03-04 11:32:11 +00:00
|
|
|
vector2d_t lcu_cu = { (x & 0x3f) / 8, (y & 0x3f) / 8 };
|
2015-03-04 11:15:45 +00:00
|
|
|
const cu_info_t *cu_array = &(lcu)->cu[LCU_CU_OFFSET];
|
2015-01-14 09:16:34 +00:00
|
|
|
bool condA = x >= 8 && cu_array[(lcu_cu.x - 1) * lcu_cu.y * LCU_T_CU_WIDTH].depth > depth;
|
|
|
|
bool condL = y >= 8 && cu_array[lcu_cu.x * (lcu_cu.y - 1) * LCU_T_CU_WIDTH].depth > depth;
|
|
|
|
return condA + condL;
|
|
|
|
}
|
|
|
|
|
2014-02-25 11:06:22 +00:00
|
|
|
/**
|
|
|
|
* Search every mode from 0 to MAX_PU_DEPTH and return cost of best mode.
|
|
|
|
* - The recursion is started at depth 0 and goes in Z-order to MAX_PU_DEPTH.
|
|
|
|
* - Data structure work_tree is maintained such that the neighbouring SCUs
|
|
|
|
* and pixels to the left and up of current CU are the final CUs decided
|
|
|
|
* via the search. This is done by copying the relevant data to all
|
|
|
|
* relevant levels whenever a decision is made whether to split or not.
|
|
|
|
* - All the final data for the LCU gets eventually copied to depth 0, which
|
|
|
|
* will be the final output of the recursion.
|
|
|
|
*/
|
2015-03-04 15:00:23 +00:00
|
|
|
static double search_cu(encoder_state_t * const state, int x, int y, int depth, lcu_t work_tree[MAX_PU_DEPTH])
|
2014-02-25 11:06:22 +00:00
|
|
|
{
|
2015-03-04 15:00:23 +00:00
|
|
|
const encoder_control_t* ctrl = state->encoder_control;
|
|
|
|
const videoframe_t * const frame = state->tile->frame;
|
2014-02-25 11:06:22 +00:00
|
|
|
int cu_width = LCU_WIDTH >> depth;
|
2014-10-06 14:44:15 +00:00
|
|
|
double cost = MAX_INT;
|
2015-03-04 11:15:45 +00:00
|
|
|
cu_info_t *cur_cu;
|
2014-10-15 19:11:45 +00:00
|
|
|
|
2015-03-04 11:32:11 +00:00
|
|
|
const vector2d_t lcu_px = { x & 0x3f, y & 0x3f };
|
2014-10-15 19:11:45 +00:00
|
|
|
lcu_t *const lcu = &work_tree[depth];
|
|
|
|
|
2014-02-25 13:32:43 +00:00
|
|
|
int x_local = (x&0x3f), y_local = (y&0x3f);
|
2014-08-11 09:35:36 +00:00
|
|
|
#ifdef _DEBUG
|
2014-07-07 08:50:05 +00:00
|
|
|
int debug_split = 0;
|
|
|
|
#endif
|
2014-08-11 09:35:36 +00:00
|
|
|
PERFORMANCE_MEASURE_START(_DEBUG_PERF_SEARCH_CU);
|
2014-03-06 16:14:01 +00:00
|
|
|
|
2014-02-25 11:06:22 +00:00
|
|
|
// Stop recursion if the CU is completely outside the frame.
|
2014-06-05 12:54:58 +00:00
|
|
|
if (x >= frame->width || y >= frame->height) {
|
2014-02-25 11:06:22 +00:00
|
|
|
// Return zero cost because this CU does not have to be coded.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-26 12:57:57 +00:00
|
|
|
cur_cu = &(&work_tree[depth])->cu[LCU_CU_OFFSET+(x_local>>3) + (y_local>>3)*LCU_T_CU_WIDTH];
|
2014-02-27 08:38:48 +00:00
|
|
|
// Assign correct depth
|
2014-03-11 17:19:20 +00:00
|
|
|
cur_cu->depth = depth > MAX_DEPTH ? MAX_DEPTH : depth;
|
|
|
|
cur_cu->tr_depth = depth > 0 ? depth : 1;
|
|
|
|
cur_cu->type = CU_NOTSET;
|
|
|
|
cur_cu->part_size = depth > MAX_DEPTH ? SIZE_NxN : SIZE_2Nx2N;
|
2014-02-25 11:06:22 +00:00
|
|
|
// If the CU is completely inside the frame at this depth, search for
|
|
|
|
// prediction modes at this depth.
|
2014-06-05 12:54:58 +00:00
|
|
|
if (x + cu_width <= frame->width &&
|
|
|
|
y + cu_width <= frame->height)
|
2014-02-25 11:06:22 +00:00
|
|
|
{
|
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
if (state->global->slicetype != SLICE_I &&
|
2015-01-09 10:04:23 +00:00
|
|
|
WITHIN(depth, ctrl->pu_depth_inter.min, ctrl->pu_depth_inter.max))
|
2014-02-25 11:06:22 +00:00
|
|
|
{
|
2015-03-04 15:00:23 +00:00
|
|
|
int mode_cost = search_cu_inter(state, x, y, depth, &work_tree[depth]);
|
2014-03-03 15:42:44 +00:00
|
|
|
if (mode_cost < cost) {
|
2014-02-25 11:06:22 +00:00
|
|
|
cost = mode_cost;
|
2014-02-26 15:50:09 +00:00
|
|
|
cur_cu->type = CU_INTER;
|
2014-02-25 11:06:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-14 12:11:12 +00:00
|
|
|
// Try to skip intra search in rd==0 mode.
|
|
|
|
// This can be quite severe on bdrate. It might be better to do this
|
|
|
|
// decision after reconstructing the inter frame.
|
2015-03-04 15:00:23 +00:00
|
|
|
bool skip_intra = state->encoder_control->rdo == 0
|
2015-01-20 08:19:07 +00:00
|
|
|
&& cur_cu->type != CU_NOTSET
|
|
|
|
&& cost / (cu_width * cu_width) < INTRA_TRESHOLD;
|
2015-01-14 12:11:12 +00:00
|
|
|
if (!skip_intra
|
|
|
|
&& WITHIN(depth, ctrl->pu_depth_intra.min, ctrl->pu_depth_intra.max))
|
|
|
|
{
|
2015-03-04 15:00:23 +00:00
|
|
|
double mode_cost = search_cu_intra(state, x, y, depth, &work_tree[depth]);
|
2014-02-25 11:06:22 +00:00
|
|
|
if (mode_cost < cost) {
|
|
|
|
cost = mode_cost;
|
2014-02-26 15:50:09 +00:00
|
|
|
cur_cu->type = CU_INTRA;
|
2014-02-25 11:06:22 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-06 16:14:01 +00:00
|
|
|
|
2014-02-27 11:02:24 +00:00
|
|
|
// Reconstruct best mode because we need the reconstructed pixels for
|
|
|
|
// mode search of adjacent CUs.
|
2014-02-26 15:50:09 +00:00
|
|
|
if (cur_cu->type == CU_INTRA) {
|
2014-09-05 08:29:30 +00:00
|
|
|
int8_t intra_mode = cur_cu->intra[PU_INDEX(x >> 2, y >> 2)].mode;
|
|
|
|
lcu_set_intra_mode(&work_tree[depth], x, y, depth,
|
|
|
|
intra_mode,
|
2014-10-15 21:42:22 +00:00
|
|
|
intra_mode,
|
2014-05-15 15:22:57 +00:00
|
|
|
cur_cu->part_size);
|
2015-03-04 15:00:23 +00:00
|
|
|
intra_recon_lcu_luma(state, x, y, depth, intra_mode, NULL, &work_tree[depth]);
|
2014-10-15 13:01:58 +00:00
|
|
|
|
|
|
|
if (PU_INDEX(x >> 2, y >> 2) == 0) {
|
|
|
|
int8_t intra_mode_chroma = intra_mode;
|
2014-10-15 20:07:28 +00:00
|
|
|
|
|
|
|
// There is almost no benefit to doing the chroma mode search for
|
|
|
|
// rd2. Possibly because the luma mode search already takes chroma
|
|
|
|
// into account, so there is less of a chanse of luma mode being
|
|
|
|
// really bad for chroma.
|
2015-03-04 15:00:23 +00:00
|
|
|
if (state->encoder_control->rdo == 3) {
|
|
|
|
const videoframe_t * const frame = state->tile->frame;
|
2014-10-15 19:11:45 +00:00
|
|
|
|
|
|
|
double costs[5];
|
2014-10-15 21:42:22 +00:00
|
|
|
int8_t modes[5] = { 0, 26, 10, 1, 34 };
|
|
|
|
if (intra_mode != 0 && intra_mode != 26 && intra_mode != 10 && intra_mode != 1) {
|
|
|
|
modes[4] = intra_mode;
|
|
|
|
}
|
2014-10-15 19:11:45 +00:00
|
|
|
|
2014-10-15 20:07:28 +00:00
|
|
|
// The number of modes to select for slower chroma search. Luma mode
|
|
|
|
// is always one of the modes, so 2 means the final decision is made
|
|
|
|
// between luma mode and one other mode that looks the best
|
|
|
|
// according to search_intra_chroma_rough.
|
2014-10-15 21:42:22 +00:00
|
|
|
const int8_t modes_in_depth[5] = { 1, 1, 1, 1, 2 };
|
|
|
|
int num_modes = modes_in_depth[depth];
|
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
if (state->encoder_control->rdo == 3) {
|
2014-10-16 10:59:20 +00:00
|
|
|
num_modes = 5;
|
|
|
|
}
|
|
|
|
|
2014-10-15 21:42:22 +00:00
|
|
|
if (num_modes != 1 && num_modes != 5) {
|
2015-03-04 14:35:17 +00:00
|
|
|
pixel_t rec_u[(LCU_WIDTH_C * 2 + 8) * (LCU_WIDTH_C * 2 + 8)];
|
|
|
|
pixel_t rec_v[(LCU_WIDTH_C * 2 + 8) * (LCU_WIDTH_C * 2 + 8)];
|
2014-10-15 21:42:22 +00:00
|
|
|
|
|
|
|
const int16_t width_c = MAX(LCU_WIDTH_C >> depth, TR_MIN_WIDTH);
|
|
|
|
const int16_t rec_stride = width_c * 2 + 8;
|
|
|
|
const int16_t out_stride = rec_stride;
|
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
intra_build_reference_border(state->encoder_control,
|
2014-10-15 21:42:22 +00:00
|
|
|
x, y, out_stride,
|
|
|
|
rec_u, rec_stride, COLOR_U,
|
|
|
|
frame->width / 2, frame->height / 2,
|
|
|
|
lcu);
|
2015-03-04 15:00:23 +00:00
|
|
|
intra_build_reference_border(state->encoder_control,
|
2014-10-15 21:42:22 +00:00
|
|
|
x, y, out_stride,
|
|
|
|
rec_v, rec_stride, COLOR_V,
|
|
|
|
frame->width / 2, frame->height / 2,
|
|
|
|
lcu);
|
|
|
|
|
2015-03-04 11:32:11 +00:00
|
|
|
vector2d_t lcu_cpx = { lcu_px.x / 2, lcu_px.y / 2 };
|
2015-03-04 14:35:17 +00:00
|
|
|
pixel_t *ref_u = &lcu->ref.u[lcu_cpx.x + lcu_cpx.y * LCU_WIDTH_C];
|
2015-03-31 12:43:22 +00:00
|
|
|
pixel_t *ref_v = &lcu->ref.v[lcu_cpx.x + lcu_cpx.y * LCU_WIDTH_C];
|
2014-10-15 21:42:22 +00:00
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
search_intra_chroma_rough(state, x, y, depth,
|
2014-10-15 21:42:22 +00:00
|
|
|
ref_u, ref_v, LCU_WIDTH_C,
|
|
|
|
&rec_u[rec_stride + 1], &rec_v[rec_stride + 1], rec_stride,
|
|
|
|
intra_mode, modes, costs);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num_modes > 1) {
|
2015-03-04 15:00:23 +00:00
|
|
|
intra_mode_chroma = search_intra_chroma(state, x, y, depth, intra_mode, modes, num_modes, &work_tree[depth]);
|
2014-10-15 21:42:22 +00:00
|
|
|
lcu_set_intra_mode(&work_tree[depth], x, y, depth,
|
|
|
|
intra_mode, intra_mode_chroma,
|
|
|
|
cur_cu->part_size);
|
|
|
|
}
|
2014-10-15 13:01:58 +00:00
|
|
|
}
|
2014-10-15 20:07:28 +00:00
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
intra_recon_lcu_chroma(state, x, y, depth, intra_mode_chroma, NULL, &work_tree[depth]);
|
2014-10-15 13:01:58 +00:00
|
|
|
}
|
2014-02-26 15:50:09 +00:00
|
|
|
} else if (cur_cu->type == CU_INTER) {
|
2014-10-03 16:29:06 +00:00
|
|
|
// Reset transform depth because intra messes with them.
|
|
|
|
// This will no longer be necessary if the transform depths are not shared.
|
|
|
|
int tr_depth = depth > 0 ? depth : 1;
|
|
|
|
lcu_set_trdepth(&work_tree[depth], x, y, depth, tr_depth);
|
|
|
|
|
2015-03-30 11:40:29 +00:00
|
|
|
if (cur_cu->inter.mv_dir == 3) {
|
2015-04-21 09:05:21 +00:00
|
|
|
inter_recon_lcu_bipred(state, state->global->ref->images[cur_cu->inter.mv_ref[0]], state->global->ref->images[cur_cu->inter.mv_ref[1]], x, y, LCU_WIDTH >> depth, cur_cu->inter.mv, &work_tree[depth]);
|
2015-03-30 11:40:29 +00:00
|
|
|
} else {
|
|
|
|
inter_recon_lcu(state, state->global->ref->images[cur_cu->inter.mv_ref[cur_cu->inter.mv_dir - 1]], x, y, LCU_WIDTH >> depth, cur_cu->inter.mv[cur_cu->inter.mv_dir - 1], &work_tree[depth]);
|
|
|
|
}
|
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
quantize_lcu_luma_residual(state, x, y, depth, NULL, &work_tree[depth]);
|
|
|
|
quantize_lcu_chroma_residual(state, x, y, depth, NULL, &work_tree[depth]);
|
2014-03-06 16:14:01 +00:00
|
|
|
|
2014-10-03 16:29:06 +00:00
|
|
|
int cbf = cbf_is_set(cur_cu->cbf.y, depth) || cbf_is_set(cur_cu->cbf.u, depth) || cbf_is_set(cur_cu->cbf.v, depth);
|
2014-05-06 11:43:12 +00:00
|
|
|
|
|
|
|
if(cur_cu->merged && !cbf) {
|
2014-03-06 16:14:01 +00:00
|
|
|
cur_cu->merged = 0;
|
2014-03-04 10:48:19 +00:00
|
|
|
cur_cu->skipped = 1;
|
2014-03-12 12:13:38 +00:00
|
|
|
// Selecting skip reduces bits needed to code the CU
|
2014-10-21 15:11:39 +00:00
|
|
|
if (cur_cu->inter.bitcost > 1) {
|
|
|
|
cur_cu->inter.bitcost -= 1;
|
|
|
|
}
|
2014-03-04 10:48:19 +00:00
|
|
|
}
|
2014-03-03 15:42:44 +00:00
|
|
|
lcu_set_inter(&work_tree[depth], x, y, depth, cur_cu);
|
2014-03-06 12:52:58 +00:00
|
|
|
lcu_set_coeff(&work_tree[depth], x, y, depth, cur_cu);
|
2014-02-26 15:50:09 +00:00
|
|
|
}
|
2014-02-25 11:06:22 +00:00
|
|
|
}
|
2014-03-11 15:09:08 +00:00
|
|
|
if (cur_cu->type == CU_INTRA || cur_cu->type == CU_INTER) {
|
2015-03-04 15:00:23 +00:00
|
|
|
cost = cu_rd_cost_luma(state, x_local, y_local, depth, cur_cu, &work_tree[depth]);
|
|
|
|
cost += cu_rd_cost_chroma(state, x_local, y_local, depth, cur_cu, &work_tree[depth]);
|
|
|
|
double mode_bits = calc_mode_bits(state, cur_cu, x, y);
|
|
|
|
cost += mode_bits * state->global->cur_lambda_cost;
|
2014-03-11 15:09:08 +00:00
|
|
|
}
|
2014-09-17 09:09:15 +00:00
|
|
|
|
2014-02-25 11:06:22 +00:00
|
|
|
// Recursively split all the way to max search depth.
|
2015-03-04 15:00:23 +00:00
|
|
|
if (depth < ctrl->pu_depth_intra.max || (depth < ctrl->pu_depth_inter.max && state->global->slicetype != SLICE_I)) {
|
2014-02-25 11:06:22 +00:00
|
|
|
int half_cu = cu_width / 2;
|
2014-06-12 07:28:30 +00:00
|
|
|
// Using Cost = lambda * 9 to compensate on the price of the split
|
2015-03-04 15:00:23 +00:00
|
|
|
double split_cost = state->global->cur_lambda_cost * CU_COST;
|
2014-05-06 11:43:12 +00:00
|
|
|
int cbf = cbf_is_set(cur_cu->cbf.y, depth) || cbf_is_set(cur_cu->cbf.u, depth) || cbf_is_set(cur_cu->cbf.v, depth);
|
2014-10-06 13:48:50 +00:00
|
|
|
|
|
|
|
if (depth < MAX_DEPTH) {
|
2015-01-14 09:16:34 +00:00
|
|
|
uint8_t split_model = get_ctx_cu_split_model(lcu, x, y, depth);
|
2014-10-06 13:48:50 +00:00
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
const cabac_ctx_t *ctx = &(state->cabac.ctx.split_flag_model[split_model]);
|
2014-10-06 13:48:50 +00:00
|
|
|
cost += CTX_ENTROPY_FBITS(ctx, 0);
|
|
|
|
split_cost += CTX_ENTROPY_FBITS(ctx, 1);
|
|
|
|
}
|
2014-02-25 11:06:22 +00:00
|
|
|
|
2014-10-06 16:19:51 +00:00
|
|
|
if (cur_cu->type == CU_INTRA && depth == MAX_DEPTH) {
|
2015-03-04 15:00:23 +00:00
|
|
|
const cabac_ctx_t *ctx = &(state->cabac.ctx.part_size_model[0]);
|
2014-10-06 16:19:51 +00:00
|
|
|
cost += CTX_ENTROPY_FBITS(ctx, 1); // 2Nx2N
|
|
|
|
split_cost += CTX_ENTROPY_FBITS(ctx, 0); // NxN
|
|
|
|
}
|
2014-02-25 11:06:22 +00:00
|
|
|
|
2014-03-11 06:55:25 +00:00
|
|
|
// If skip mode was selected for the block, skip further search.
|
|
|
|
// Skip mode means there's no coefficients in the block, so splitting
|
|
|
|
// might not give any better results but takes more time to do.
|
2014-09-17 09:09:15 +00:00
|
|
|
if (cur_cu->type == CU_NOTSET || cbf || FULL_CU_SPLIT_SEARCH) {
|
2015-03-04 15:00:23 +00:00
|
|
|
split_cost += search_cu(state, x, y, depth + 1, work_tree);
|
|
|
|
split_cost += search_cu(state, x + half_cu, y, depth + 1, work_tree);
|
|
|
|
split_cost += search_cu(state, x, y + half_cu, depth + 1, work_tree);
|
|
|
|
split_cost += search_cu(state, x + half_cu, y + half_cu, depth + 1, work_tree);
|
2014-03-12 12:14:42 +00:00
|
|
|
} else {
|
|
|
|
split_cost = INT_MAX;
|
2014-03-11 06:55:25 +00:00
|
|
|
}
|
2014-11-20 15:39:55 +00:00
|
|
|
|
|
|
|
// If no search is not performed for this depth, try just the best mode
|
|
|
|
// of the top left CU from the next depth. This should ensure that 64x64
|
|
|
|
// gets used, at least in the most obvious cases, while avoiding any
|
|
|
|
// searching.
|
2015-01-12 09:47:21 +00:00
|
|
|
if (cur_cu->type == CU_NOTSET && depth < MAX_PU_DEPTH
|
|
|
|
&& x + cu_width <= frame->width && y + cu_width <= frame->height)
|
2014-11-20 15:39:55 +00:00
|
|
|
{
|
2015-03-04 11:32:11 +00:00
|
|
|
vector2d_t lcu_cu = { x_local / 8, y_local / 8 };
|
2015-03-04 11:15:45 +00:00
|
|
|
cu_info_t *cu_array_d1 = &(&work_tree[depth + 1])->cu[LCU_CU_OFFSET];
|
|
|
|
cu_info_t *cu_d1 = &cu_array_d1[(lcu_cu.x + lcu_cu.y * LCU_T_CU_WIDTH)];
|
2014-11-20 15:39:55 +00:00
|
|
|
|
2015-01-12 09:47:21 +00:00
|
|
|
// If the best CU in depth+1 is intra and the biggest it can be, try it.
|
|
|
|
if (cu_d1->type == CU_INTRA && cu_d1->depth == depth + 1) {
|
|
|
|
cost = 0;
|
2014-11-20 15:39:55 +00:00
|
|
|
|
2015-01-12 09:47:21 +00:00
|
|
|
cur_cu->intra[0] = cu_d1->intra[0];
|
|
|
|
cur_cu->type = CU_INTRA;
|
2014-11-20 15:39:55 +00:00
|
|
|
|
2015-01-12 09:47:21 +00:00
|
|
|
lcu_set_trdepth(&work_tree[depth], x, y, depth, cur_cu->tr_depth);
|
|
|
|
lcu_set_intra_mode(&work_tree[depth], x, y, depth,
|
|
|
|
cur_cu->intra[0].mode, cur_cu->intra[0].mode_chroma,
|
|
|
|
cur_cu->part_size);
|
2015-03-04 15:00:23 +00:00
|
|
|
intra_recon_lcu_luma(state, x, y, depth, cur_cu->intra[0].mode, NULL, &work_tree[depth]);
|
|
|
|
intra_recon_lcu_chroma(state, x, y, depth, cur_cu->intra[0].mode_chroma, NULL, &work_tree[depth]);
|
|
|
|
cost += cu_rd_cost_luma(state, x_local, y_local, depth, cur_cu, &work_tree[depth]);
|
|
|
|
cost += cu_rd_cost_chroma(state, x_local, y_local, depth, cur_cu, &work_tree[depth]);
|
2015-01-12 09:47:21 +00:00
|
|
|
|
2015-01-14 09:16:34 +00:00
|
|
|
uint8_t split_model = get_ctx_cu_split_model(lcu, x, y, depth);
|
2015-03-04 15:00:23 +00:00
|
|
|
const cabac_ctx_t *ctx = &(state->cabac.ctx.split_flag_model[split_model]);
|
2015-01-12 09:47:21 +00:00
|
|
|
cost += CTX_ENTROPY_FBITS(ctx, 0);
|
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
double mode_bits = calc_mode_bits(state, cur_cu, x, y);
|
|
|
|
cost += mode_bits * state->global->cur_lambda_cost;
|
2014-11-20 15:39:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-25 11:06:22 +00:00
|
|
|
if (split_cost < cost) {
|
|
|
|
// Copy split modes to this depth.
|
|
|
|
cost = split_cost;
|
|
|
|
work_tree_copy_up(x, y, depth, work_tree);
|
2014-07-07 08:50:05 +00:00
|
|
|
#if _DEBUG
|
|
|
|
debug_split = 1;
|
|
|
|
#endif
|
2014-11-20 15:39:55 +00:00
|
|
|
} else if (depth > 0) {
|
2014-02-25 11:06:22 +00:00
|
|
|
// Copy this CU's mode all the way down for use in adjacent CUs mode
|
|
|
|
// search.
|
|
|
|
work_tree_copy_down(x, y, depth, work_tree);
|
|
|
|
}
|
|
|
|
}
|
2014-07-07 08:50:05 +00:00
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
PERFORMANCE_MEASURE_END(_DEBUG_PERF_SEARCH_CU, state->encoder_control->threadqueue, "type=search_cu,frame=%d,tile=%d,slice=%d,px_x=%d-%d,px_y=%d-%d,depth=%d,split=%d,cur_cu_is_intra=%d", state->global->frame, state->tile->id, state->slice->id,
|
|
|
|
(state->tile->lcu_offset_x * LCU_WIDTH) + x,
|
|
|
|
(state->tile->lcu_offset_x * LCU_WIDTH) + x + (LCU_WIDTH >> depth),
|
|
|
|
(state->tile->lcu_offset_y * LCU_WIDTH) + y,
|
|
|
|
(state->tile->lcu_offset_y * LCU_WIDTH) + y + (LCU_WIDTH >> depth),
|
2014-08-11 09:46:21 +00:00
|
|
|
depth, debug_split, (cur_cu->type==CU_INTRA)?1:0);
|
2014-02-25 11:06:22 +00:00
|
|
|
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize lcu_t for search.
|
|
|
|
* - Copy reference CUs.
|
|
|
|
* - Copy reference pixels from neighbouring LCUs.
|
|
|
|
* - Copy reference pixels from this LCU.
|
|
|
|
*/
|
2015-03-04 15:00:23 +00:00
|
|
|
static void init_lcu_t(const encoder_state_t * const state, const int x, const int y, lcu_t *lcu, const yuv_t *hor_buf, const yuv_t *ver_buf)
|
2014-02-25 11:06:22 +00:00
|
|
|
{
|
2015-03-04 15:00:23 +00:00
|
|
|
const videoframe_t * const frame = state->tile->frame;
|
2014-04-17 07:51:55 +00:00
|
|
|
|
2014-02-25 15:10:46 +00:00
|
|
|
// Copy reference cu_info structs from neighbouring LCUs.
|
|
|
|
{
|
|
|
|
const int x_cu = x >> MAX_DEPTH;
|
|
|
|
const int y_cu = y >> MAX_DEPTH;
|
|
|
|
|
|
|
|
// Use top-left sub-cu of LCU as pointer to lcu->cu array to make things
|
|
|
|
// simpler.
|
2015-03-04 11:15:45 +00:00
|
|
|
cu_info_t *lcu_cu = &lcu->cu[LCU_CU_OFFSET];
|
2014-02-25 15:10:46 +00:00
|
|
|
|
|
|
|
// Copy top CU row.
|
|
|
|
if (y_cu > 0) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < LCU_CU_WIDTH; ++i) {
|
2015-03-04 11:15:45 +00:00
|
|
|
const cu_info_t *from_cu = videoframe_get_cu_const(frame, x_cu + i, y_cu - 1);
|
|
|
|
cu_info_t *to_cu = &lcu_cu[i - LCU_T_CU_WIDTH];
|
2014-02-25 15:10:46 +00:00
|
|
|
memcpy(to_cu, from_cu, sizeof(*to_cu));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Copy left CU column.
|
|
|
|
if (x_cu > 0) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < LCU_CU_WIDTH; ++i) {
|
2015-03-04 11:15:45 +00:00
|
|
|
const cu_info_t *from_cu = videoframe_get_cu_const(frame, x_cu - 1, y_cu + i);
|
|
|
|
cu_info_t *to_cu = &lcu_cu[-1 + i * LCU_T_CU_WIDTH];
|
2014-02-25 15:10:46 +00:00
|
|
|
memcpy(to_cu, from_cu, sizeof(*to_cu));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Copy top-left CU.
|
|
|
|
if (x_cu > 0 && y_cu > 0) {
|
2015-03-04 11:15:45 +00:00
|
|
|
const cu_info_t *from_cu = videoframe_get_cu_const(frame, x_cu - 1, y_cu - 1);
|
|
|
|
cu_info_t *to_cu = &lcu_cu[-1 - LCU_T_CU_WIDTH];
|
2014-02-25 15:10:46 +00:00
|
|
|
memcpy(to_cu, from_cu, sizeof(*to_cu));
|
|
|
|
}
|
2014-03-03 14:08:35 +00:00
|
|
|
|
|
|
|
// Copy top-right CU.
|
2014-06-05 12:54:58 +00:00
|
|
|
if (y_cu > 0 && x + LCU_WIDTH < frame->width) {
|
2015-03-04 11:15:45 +00:00
|
|
|
const cu_info_t *from_cu = videoframe_get_cu_const(frame, x_cu + LCU_CU_WIDTH, y_cu - 1);
|
|
|
|
cu_info_t *to_cu = &lcu->cu[LCU_T_CU_WIDTH*LCU_T_CU_WIDTH];
|
2014-03-03 14:08:35 +00:00
|
|
|
memcpy(to_cu, from_cu, sizeof(*to_cu));
|
|
|
|
}
|
2014-02-25 15:10:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy reference pixels.
|
|
|
|
{
|
2014-06-05 12:54:58 +00:00
|
|
|
const int pic_width = frame->width;
|
2014-02-25 15:10:46 +00:00
|
|
|
// Copy top reference pixels.
|
|
|
|
if (y > 0) {
|
2014-03-18 13:36:48 +00:00
|
|
|
// hor_buf is of size pic_width so there might not be LCU_REF_PX_WIDTH
|
|
|
|
// number of allocated pixels left.
|
|
|
|
int x_max = MIN(LCU_REF_PX_WIDTH, pic_width - x);
|
2014-05-14 07:13:31 +00:00
|
|
|
int x_min_in_lcu = (x>0) ? 0 : 1;
|
2014-06-05 12:54:58 +00:00
|
|
|
memcpy(&lcu->top_ref.y[x_min_in_lcu], &hor_buf->y[OFFSET_HOR_BUF(x, y, frame, x_min_in_lcu-1)], x_max + (1-x_min_in_lcu));
|
|
|
|
memcpy(&lcu->top_ref.u[x_min_in_lcu], &hor_buf->u[OFFSET_HOR_BUF_C(x, y, frame, x_min_in_lcu-1)], x_max / 2 + (1-x_min_in_lcu));
|
|
|
|
memcpy(&lcu->top_ref.v[x_min_in_lcu], &hor_buf->v[OFFSET_HOR_BUF_C(x, y, frame, x_min_in_lcu-1)], x_max / 2 + (1-x_min_in_lcu));
|
2014-02-25 15:10:46 +00:00
|
|
|
}
|
|
|
|
// Copy left reference pixels.
|
|
|
|
if (x > 0) {
|
2014-05-14 07:13:31 +00:00
|
|
|
int y_min_in_lcu = (y>0) ? 0 : 1;
|
2014-06-05 12:54:58 +00:00
|
|
|
memcpy(&lcu->left_ref.y[y_min_in_lcu], &ver_buf->y[OFFSET_VER_BUF(x, y, frame, y_min_in_lcu-1)], LCU_WIDTH + (1-y_min_in_lcu));
|
|
|
|
memcpy(&lcu->left_ref.u[y_min_in_lcu], &ver_buf->u[OFFSET_VER_BUF_C(x, y, frame, y_min_in_lcu-1)], LCU_WIDTH / 2 + (1-y_min_in_lcu));
|
|
|
|
memcpy(&lcu->left_ref.v[y_min_in_lcu], &ver_buf->v[OFFSET_VER_BUF_C(x, y, frame, y_min_in_lcu-1)], LCU_WIDTH / 2 + (1-y_min_in_lcu));
|
2014-02-25 15:10:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy LCU pixels.
|
|
|
|
{
|
2015-03-04 15:00:23 +00:00
|
|
|
const videoframe_t * const frame = state->tile->frame;
|
2014-06-12 05:21:13 +00:00
|
|
|
int x_max = MIN(x + LCU_WIDTH, frame->width) - x;
|
2014-06-05 12:54:58 +00:00
|
|
|
int y_max = MIN(y + LCU_WIDTH, frame->height) - y;
|
2014-02-25 15:10:46 +00:00
|
|
|
|
|
|
|
int x_c = x / 2;
|
|
|
|
int y_c = y / 2;
|
|
|
|
int x_max_c = x_max / 2;
|
|
|
|
int y_max_c = y_max / 2;
|
|
|
|
|
2014-06-12 05:21:13 +00:00
|
|
|
pixels_blit(&frame->source->y[x + y * frame->source->stride], lcu->ref.y,
|
|
|
|
x_max, y_max, frame->source->stride, LCU_WIDTH);
|
|
|
|
pixels_blit(&frame->source->u[x_c + y_c * frame->source->stride/2], lcu->ref.u,
|
|
|
|
x_max_c, y_max_c, frame->source->stride/2, LCU_WIDTH / 2);
|
|
|
|
pixels_blit(&frame->source->v[x_c + y_c * frame->source->stride/2], lcu->ref.v,
|
|
|
|
x_max_c, y_max_c, frame->source->stride/2, LCU_WIDTH / 2);
|
2014-02-25 15:10:46 +00:00
|
|
|
}
|
2014-02-25 11:06:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy CU and pixel data to it's place in picture datastructure.
|
|
|
|
*/
|
2015-03-04 15:00:23 +00:00
|
|
|
static void copy_lcu_to_cu_data(const encoder_state_t * const state, int x_px, int y_px, const lcu_t *lcu)
|
2014-02-25 11:06:22 +00:00
|
|
|
{
|
2014-02-26 10:31:51 +00:00
|
|
|
// Copy non-reference CUs to picture.
|
|
|
|
{
|
|
|
|
const int x_cu = x_px >> MAX_DEPTH;
|
|
|
|
const int y_cu = y_px >> MAX_DEPTH;
|
2015-03-04 15:00:23 +00:00
|
|
|
videoframe_t * const frame = state->tile->frame;
|
2014-02-26 10:31:51 +00:00
|
|
|
|
|
|
|
// Use top-left sub-cu of LCU as pointer to lcu->cu array to make things
|
|
|
|
// simpler.
|
2015-03-04 11:15:45 +00:00
|
|
|
const cu_info_t *const lcu_cu = &lcu->cu[LCU_CU_OFFSET];
|
2014-02-26 10:31:51 +00:00
|
|
|
|
|
|
|
int x, y;
|
|
|
|
for (y = 0; y < LCU_CU_WIDTH; ++y) {
|
|
|
|
for (x = 0; x < LCU_CU_WIDTH; ++x) {
|
2015-03-04 11:15:45 +00:00
|
|
|
const cu_info_t *from_cu = &lcu_cu[x + y * LCU_T_CU_WIDTH];
|
|
|
|
cu_info_t *to_cu = videoframe_get_cu(frame, x_cu + x, y_cu + y);
|
2014-02-26 10:31:51 +00:00
|
|
|
memcpy(to_cu, from_cu, sizeof(*to_cu));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy pixels to picture.
|
|
|
|
{
|
2015-03-04 15:00:23 +00:00
|
|
|
videoframe_t * const pic = state->tile->frame;
|
2014-04-17 07:51:55 +00:00
|
|
|
const int pic_width = pic->width;
|
2014-02-26 10:31:51 +00:00
|
|
|
const int x_max = MIN(x_px + LCU_WIDTH, pic_width) - x_px;
|
2014-04-17 07:51:55 +00:00
|
|
|
const int y_max = MIN(y_px + LCU_WIDTH, pic->height) - y_px;
|
2014-02-27 09:56:16 +00:00
|
|
|
const int luma_index = x_px + y_px * pic_width;
|
|
|
|
const int chroma_index = (x_px / 2) + (y_px / 2) * (pic_width / 2);
|
2014-02-26 10:31:51 +00:00
|
|
|
|
2014-06-12 05:21:13 +00:00
|
|
|
pixels_blit(lcu->rec.y, &pic->rec->y[x_px + y_px * pic->rec->stride],
|
|
|
|
x_max, y_max, LCU_WIDTH, pic->rec->stride);
|
2014-06-05 12:54:58 +00:00
|
|
|
coefficients_blit(lcu->coeff.y, &pic->coeff_y[luma_index],
|
2014-02-26 10:31:51 +00:00
|
|
|
x_max, y_max, LCU_WIDTH, pic_width);
|
|
|
|
|
2014-06-12 05:21:13 +00:00
|
|
|
pixels_blit(lcu->rec.u, &pic->rec->u[(x_px / 2) + (y_px / 2) * (pic->rec->stride / 2)],
|
|
|
|
x_max / 2, y_max / 2, LCU_WIDTH / 2, pic->rec->stride / 2);
|
|
|
|
pixels_blit(lcu->rec.v, &pic->rec->v[(x_px / 2) + (y_px / 2) * (pic->rec->stride / 2)],
|
|
|
|
x_max / 2, y_max / 2, LCU_WIDTH / 2, pic->rec->stride / 2);
|
2014-06-05 12:54:58 +00:00
|
|
|
coefficients_blit(lcu->coeff.u, &pic->coeff_u[chroma_index],
|
2014-02-27 09:56:16 +00:00
|
|
|
x_max / 2, y_max / 2, LCU_WIDTH / 2, pic_width / 2);
|
2014-06-05 12:54:58 +00:00
|
|
|
coefficients_blit(lcu->coeff.v, &pic->coeff_v[chroma_index],
|
2014-02-27 09:56:16 +00:00
|
|
|
x_max / 2, y_max / 2, LCU_WIDTH / 2, pic_width / 2);
|
2014-02-26 10:31:51 +00:00
|
|
|
}
|
2014-02-25 11:06:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search LCU for modes.
|
|
|
|
* - Best mode gets copied to current picture.
|
|
|
|
*/
|
2015-03-04 15:00:23 +00:00
|
|
|
void search_lcu(encoder_state_t * const state, const int x, const int y, const yuv_t * const hor_buf, const yuv_t * const ver_buf)
|
2014-02-25 11:06:22 +00:00
|
|
|
{
|
2014-03-11 17:19:20 +00:00
|
|
|
lcu_t work_tree[MAX_PU_DEPTH + 1];
|
2014-02-25 11:06:22 +00:00
|
|
|
int depth;
|
|
|
|
// Initialize work tree.
|
2014-03-11 17:19:20 +00:00
|
|
|
for (depth = 0; depth <= MAX_PU_DEPTH; ++depth) {
|
2015-02-13 09:56:55 +00:00
|
|
|
FILL(work_tree[depth], 0);
|
2015-03-04 15:00:23 +00:00
|
|
|
init_lcu_t(state, x, y, &work_tree[depth], hor_buf, ver_buf);
|
2014-02-25 11:06:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start search from depth 0.
|
2015-03-04 15:00:23 +00:00
|
|
|
search_cu(state, x, y, 0, work_tree);
|
2014-02-25 11:06:22 +00:00
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
copy_lcu_to_cu_data(state, x, y, &work_tree[0]);
|
2014-02-25 11:06:22 +00:00
|
|
|
}
|