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 12:10:43 +00:00
|
|
|
#include "intra.h"
|
2013-09-05 12:02:53 +00:00
|
|
|
#include "inter.h"
|
2014-04-04 10:09:02 +00:00
|
|
|
#include "rdo.h"
|
2014-05-13 06:33:05 +00:00
|
|
|
#include "transform.h"
|
2015-07-21 09:02:54 +00:00
|
|
|
#include "search_inter.h"
|
2015-07-21 11:28:50 +00:00
|
|
|
#include "search_intra.h"
|
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-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
|
|
|
{
|
2015-05-05 07:58:47 +00:00
|
|
|
assert(depth >= 0 && depth < MAX_PU_DEPTH);
|
|
|
|
|
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
|
|
|
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_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);
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_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);
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_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.
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_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);
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_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);
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_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
|
|
|
{
|
2015-05-05 07:58:47 +00:00
|
|
|
assert(depth >= 0 && depth < MAX_PU_DEPTH);
|
|
|
|
|
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;
|
|
|
|
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_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);
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_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);
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_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
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-26 08:50:27 +00:00
|
|
|
void kvz_lcu_set_trdepth(lcu_t *lcu, int x_px, int y_px, int depth, int tr_depth)
|
2014-09-05 08:29:30 +00:00
|
|
|
{
|
|
|
|
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-08-26 08:50:27 +00:00
|
|
|
double kvz_cu_rd_cost_luma(const encoder_state_t *const state,
|
2015-07-21 11:28:50 +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-08-26 08:50:27 +00:00
|
|
|
sum += kvz_cu_rd_cost_luma(state, x_px, y_px, depth + 1, pred_cu, lcu);
|
|
|
|
sum += kvz_cu_rd_cost_luma(state, x_px + offset, y_px, depth + 1, pred_cu, lcu);
|
|
|
|
sum += kvz_cu_rd_cost_luma(state, x_px, y_px + offset, depth + 1, pred_cu, lcu);
|
|
|
|
sum += kvz_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];
|
2015-08-26 08:50:27 +00:00
|
|
|
int8_t luma_scan_mode = kvz_get_scan_order(pred_cu->type, pred_cu->intra[PU_INDEX(x_px / 4, y_px / 4)].mode, depth);
|
2014-09-05 08:29:30 +00:00
|
|
|
|
|
|
|
// Code coeffs using cabac to get a better estimate of real coding costs.
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_coefficients_blit(&lcu->coeff.y[(y_px*LCU_WIDTH) + x_px], coeff_temp, width, width, LCU_WIDTH, width);
|
|
|
|
coeff_bits += kvz_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-08-26 08:50:27 +00:00
|
|
|
double kvz_cu_rd_cost_chroma(const encoder_state_t *const state,
|
2015-07-21 11:28:50 +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-08-26 08:50:27 +00:00
|
|
|
sum += kvz_cu_rd_cost_chroma(state, x_px, y_px, depth + 1, pred_cu, lcu);
|
|
|
|
sum += kvz_cu_rd_cost_chroma(state, x_px + offset, y_px, depth + 1, pred_cu, lcu);
|
|
|
|
sum += kvz_cu_rd_cost_chroma(state, x_px, y_px + offset, depth + 1, pred_cu, lcu);
|
|
|
|
sum += kvz_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];
|
2015-08-26 08:50:27 +00:00
|
|
|
int8_t scan_order = kvz_get_scan_order(pred_cu->type, pred_cu->intra[0].mode_chroma, depth);
|
2014-09-17 08:52:22 +00:00
|
|
|
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_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-08-26 08:50:27 +00:00
|
|
|
coeff_bits += kvz_get_coeff_cost(state, coeff_temp, width, 2, scan_order);
|
2014-09-05 08:29:30 +00:00
|
|
|
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_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-08-26 08:50:27 +00:00
|
|
|
coeff_bits += kvz_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
|
|
|
|
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-08-26 08:50:27 +00:00
|
|
|
kvz_intra_get_dir_luma_predictor(x, y, candidate_modes, cur_cu, left_cu, above_cu);
|
2015-01-14 08:44:52 +00:00
|
|
|
}
|
|
|
|
|
2015-08-26 08:50:27 +00:00
|
|
|
mode_bits = kvz_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-08-26 08:50:27 +00:00
|
|
|
mode_bits += kvz_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-07-21 11:28:50 +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-05-04 14:37:13 +00:00
|
|
|
static double search_cu(encoder_state_t * const state, int x, int y, int depth, lcu_t work_tree[MAX_PU_DEPTH + 1])
|
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
|
|
|
|
|
|
|
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-08-26 08:50:27 +00:00
|
|
|
int mode_cost = kvz_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-08-26 08:50:27 +00:00
|
|
|
double mode_cost = kvz_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-08-26 08:50:27 +00:00
|
|
|
kvz_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) {
|
2015-08-26 08:50:27 +00:00
|
|
|
intra_mode_chroma = kvz_search_cu_intra_chroma(state, x, y, depth, &work_tree[depth]);
|
2015-07-21 11:28:50 +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-08-26 08:50:27 +00:00
|
|
|
kvz_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;
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_lcu_set_trdepth(&work_tree[depth], x, y, depth, tr_depth);
|
2014-10-03 16:29:06 +00:00
|
|
|
|
2015-03-30 11:40:29 +00:00
|
|
|
if (cur_cu->inter.mv_dir == 3) {
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_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 {
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_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], 0);
|
2015-03-30 11:40:29 +00:00
|
|
|
}
|
|
|
|
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_quantize_lcu_luma_residual(state, x, y, depth, NULL, &work_tree[depth]);
|
|
|
|
kvz_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-08-26 08:50:27 +00:00
|
|
|
cost = kvz_cu_rd_cost_luma(state, x_local, y_local, depth, cur_cu, &work_tree[depth]);
|
|
|
|
cost += kvz_cu_rd_cost_chroma(state, x_local, y_local, depth, cur_cu, &work_tree[depth]);
|
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-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-08-26 08:50:27 +00:00
|
|
|
kvz_lcu_set_trdepth(&work_tree[depth], x, y, depth, cur_cu->tr_depth);
|
2015-01-12 09:47:21 +00:00
|
|
|
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-08-26 08:50:27 +00:00
|
|
|
kvz_intra_recon_lcu_luma(state, x, y, depth, cur_cu->intra[0].mode, NULL, &work_tree[depth]);
|
|
|
|
kvz_intra_recon_lcu_chroma(state, x, y, depth, cur_cu->intra[0].mode_chroma, NULL, &work_tree[depth]);
|
|
|
|
cost += kvz_cu_rd_cost_luma(state, x_local, y_local, depth, cur_cu, &work_tree[depth]);
|
|
|
|
cost += kvz_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-08-26 08:50:27 +00:00
|
|
|
const cu_info_t *from_cu = kvz_videoframe_get_cu_const(frame, x_cu + i, y_cu - 1);
|
2015-03-04 11:15:45 +00:00
|
|
|
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-08-26 08:50:27 +00:00
|
|
|
const cu_info_t *from_cu = kvz_videoframe_get_cu_const(frame, x_cu - 1, y_cu + i);
|
2015-03-04 11:15:45 +00:00
|
|
|
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-08-26 08:50:27 +00:00
|
|
|
const cu_info_t *from_cu = kvz_videoframe_get_cu_const(frame, x_cu - 1, y_cu - 1);
|
2015-03-04 11:15:45 +00:00
|
|
|
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-08-26 08:50:27 +00:00
|
|
|
const cu_info_t *from_cu = kvz_videoframe_get_cu_const(frame, x_cu + LCU_CU_WIDTH, y_cu - 1);
|
2015-03-04 11:15:45 +00:00
|
|
|
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;
|
2015-08-11 05:42:09 +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))*sizeof(kvz_pixel));
|
|
|
|
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))*sizeof(kvz_pixel));
|
|
|
|
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))*sizeof(kvz_pixel));
|
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;
|
2015-08-11 05:42:09 +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))*sizeof(kvz_pixel));
|
|
|
|
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))*sizeof(kvz_pixel));
|
|
|
|
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))*sizeof(kvz_pixel));
|
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;
|
|
|
|
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_pixels_blit(&frame->source->y[x + y * frame->source->stride], lcu->ref.y,
|
2014-06-12 05:21:13 +00:00
|
|
|
x_max, y_max, frame->source->stride, LCU_WIDTH);
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_pixels_blit(&frame->source->u[x_c + y_c * frame->source->stride/2], lcu->ref.u,
|
2014-06-12 05:21:13 +00:00
|
|
|
x_max_c, y_max_c, frame->source->stride/2, LCU_WIDTH / 2);
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_pixels_blit(&frame->source->v[x_c + y_c * frame->source->stride/2], lcu->ref.v,
|
2014-06-12 05:21:13 +00:00
|
|
|
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];
|
2015-08-26 08:50:27 +00:00
|
|
|
cu_info_t *to_cu = kvz_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
|
|
|
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_pixels_blit(lcu->rec.y, &pic->rec->y[x_px + y_px * pic->rec->stride],
|
2014-06-12 05:21:13 +00:00
|
|
|
x_max, y_max, LCU_WIDTH, pic->rec->stride);
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_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);
|
|
|
|
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_pixels_blit(lcu->rec.u, &pic->rec->u[(x_px / 2) + (y_px / 2) * (pic->rec->stride / 2)],
|
2014-06-12 05:21:13 +00:00
|
|
|
x_max / 2, y_max / 2, LCU_WIDTH / 2, pic->rec->stride / 2);
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_pixels_blit(lcu->rec.v, &pic->rec->v[(x_px / 2) + (y_px / 2) * (pic->rec->stride / 2)],
|
2014-06-12 05:21:13 +00:00
|
|
|
x_max / 2, y_max / 2, LCU_WIDTH / 2, pic->rec->stride / 2);
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_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);
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_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-08-26 08:50:27 +00:00
|
|
|
void kvz_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
|
|
|
}
|