Add quantization strategy.

This commit is contained in:
Ari Lemmetti 2015-09-25 09:10:12 +03:00
parent a21c5b817f
commit ef0ad292ef
10 changed files with 298 additions and 134 deletions

View file

@ -203,6 +203,7 @@ OBJS = \
strategies/strategies-nal.o \
strategies/strategies-dct.o \
strategies/strategies-ipol.o \
strategies/strategies-quant.o \
strategies/generic/nal-generic.o \
strategies/generic/picture-generic.o \
strategies/sse2/picture-sse2.o \
@ -213,7 +214,8 @@ OBJS = \
strategies/generic/dct-generic.o \
strategies/avx2/dct-avx2.o \
strategies/generic/ipol-generic.o \
strategies/avx2/ipol-avx2.o
strategies/avx2/ipol-avx2.o \
strategies/generic/quant-generic.o
ifndef KVZ_DISABLE_ASM
# Compile C files in x86_asm folder with KVZ_COMPILE_ASM, which will cause

View file

@ -31,6 +31,7 @@
#include "context.h"
#include "cabac.h"
#include "transform.h"
#include "strategies/strategies-quant.h"
#define QUANT_SHIFT 14

View file

@ -0,0 +1,173 @@
/*****************************************************************************
* This file is part of Kvazaar HEVC encoder.
*
* Copyright (C) 2013-2015 Tampere University of Technology and others (see
* COPYING file).
*
* 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.
*
* 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.
*
* You should have received a copy of the GNU General Public License along
* with Kvazaar. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
/*
* \file
*/
#include <stdlib.h>
#include "quant-generic.h"
#include "strategyselector.h"
#include "encoder.h"
#include "transform.h"
#define QUANT_SHIFT 14
/**
* \brief quantize transformed coefficents
*
*/
void kvz_quant_generic(const encoder_state_t * const state, coeff_t *coef, coeff_t *q_coef, int32_t width,
int32_t height, int8_t type, int8_t scan_idx, int8_t block_type)
{
const encoder_control_t * const encoder = state->encoder_control;
const uint32_t log2_block_size = kvz_g_convert_to_bit[width] + 2;
const uint32_t * const scan = kvz_g_sig_last_scan[scan_idx][log2_block_size - 1];
int32_t qp_scaled = kvz_get_scaled_qp(type, state->global->QP, (encoder->bitdepth - 8) * 6);
const uint32_t log2_tr_size = kvz_g_convert_to_bit[width] + 2;
const int32_t scalinglist_type = (block_type == CU_INTRA ? 0 : 3) + (int8_t)("\0\3\1\2"[type]);
const int32_t *quant_coeff = encoder->scaling_list.quant_coeff[log2_tr_size - 2][scalinglist_type][qp_scaled % 6];
const int32_t transform_shift = MAX_TR_DYNAMIC_RANGE - encoder->bitdepth - log2_tr_size; //!< Represents scaling through forward transform
const int32_t q_bits = QUANT_SHIFT + qp_scaled / 6 + transform_shift;
const int32_t add = ((state->global->slicetype == KVZ_SLICE_I) ? 171 : 85) << (q_bits - 9);
const int32_t q_bits8 = q_bits - 8;
uint32_t ac_sum = 0;
for (int32_t n = 0; n < width * height; n++) {
int32_t level;
int32_t sign;
level = coef[n];
sign = (level < 0 ? -1 : 1);
level = ((int64_t)abs(level) * quant_coeff[n] + add) >> q_bits;
ac_sum += level;
level *= sign;
q_coef[n] = (coeff_t)(CLIP(-32768, 32767, level));
}
if (!(encoder->sign_hiding && ac_sum >= 2)) return;
int32_t delta_u[LCU_WIDTH*LCU_WIDTH >> 2];
for (int32_t n = 0; n < width * height; n++) {
int32_t level;
level = coef[n];
level = ((int64_t)abs(level) * quant_coeff[n] + add) >> q_bits;
delta_u[n] = (int32_t)(((int64_t)abs(coef[n]) * quant_coeff[n] - (level << q_bits)) >> q_bits8);
}
if (ac_sum >= 2) {
#define SCAN_SET_SIZE 16
#define LOG2_SCAN_SET_SIZE 4
int32_t n, last_cg = -1, abssum = 0, subset, subpos;
for (subset = (width*height - 1) >> LOG2_SCAN_SET_SIZE; subset >= 0; subset--) {
int32_t first_nz_pos_in_cg = SCAN_SET_SIZE, last_nz_pos_in_cg = -1;
subpos = subset << LOG2_SCAN_SET_SIZE;
abssum = 0;
// Find last coeff pos
for (n = SCAN_SET_SIZE - 1; n >= 0; n--) {
if (q_coef[scan[n + subpos]]) {
last_nz_pos_in_cg = n;
break;
}
}
// First coeff pos
for (n = 0; n <SCAN_SET_SIZE; n++) {
if (q_coef[scan[n + subpos]]) {
first_nz_pos_in_cg = n;
break;
}
}
// Sum all kvz_quant coeffs between first and last
for (n = first_nz_pos_in_cg; n <= last_nz_pos_in_cg; n++) {
abssum += q_coef[scan[n + subpos]];
}
if (last_nz_pos_in_cg >= 0 && last_cg == -1) {
last_cg = 1;
}
if (last_nz_pos_in_cg - first_nz_pos_in_cg >= 4) {
int32_t signbit = (q_coef[scan[subpos + first_nz_pos_in_cg]] > 0 ? 0 : 1);
if (signbit != (abssum & 0x1)) { // compare signbit with sum_parity
int32_t min_cost_inc = 0x7fffffff, min_pos = -1, cur_cost = 0x7fffffff;
int16_t final_change = 0, cur_change = 0;
for (n = (last_cg == 1 ? last_nz_pos_in_cg : SCAN_SET_SIZE - 1); n >= 0; n--) {
uint32_t blkPos = scan[n + subpos];
if (q_coef[blkPos] != 0) {
if (delta_u[blkPos] > 0) {
cur_cost = -delta_u[blkPos];
cur_change = 1;
}
else if (n == first_nz_pos_in_cg && abs(q_coef[blkPos]) == 1) {
cur_cost = 0x7fffffff;
}
else {
cur_cost = delta_u[blkPos];
cur_change = -1;
}
}
else if (n < first_nz_pos_in_cg && ((coef[blkPos] >= 0) ? 0 : 1) != signbit) {
cur_cost = 0x7fffffff;
}
else {
cur_cost = -delta_u[blkPos];
cur_change = 1;
}
if (cur_cost < min_cost_inc) {
min_cost_inc = cur_cost;
final_change = cur_change;
min_pos = blkPos;
}
} // CG loop
if (q_coef[min_pos] == 32767 || q_coef[min_pos] == -32768) {
final_change = -1;
}
if (coef[min_pos] >= 0) q_coef[min_pos] += final_change;
else q_coef[min_pos] -= final_change;
} // Hide
}
if (last_cg == 1) last_cg = 0;
}
#undef SCAN_SET_SIZE
#undef LOG2_SCAN_SET_SIZE
}
}
int kvz_strategy_register_quant_generic(void* opaque, uint8_t bitdepth)
{
bool success = true;
success &= kvz_strategyselector_register(opaque, "quant", "generic", 0, &kvz_quant_generic);
return success;
}

View file

@ -0,0 +1,31 @@
#ifndef STRATEGIES_QUANT_GENERIC_H_
#define STRATEGIES_QUANT_GENERIC_H_
/*****************************************************************************
* This file is part of Kvazaar HEVC encoder.
*
* Copyright (C) 2013-2015 Tampere University of Technology and others (see
* COPYING file).
*
* 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.
*
* 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.
*
* You should have received a copy of the GNU General Public License along
* with Kvazaar. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include <stdint.h>
#include "encoderstate.h"
#define QUANT_SHIFT 14
int kvz_strategy_register_quant_generic(void* opaque, uint8_t bitdepth);
void kvz_quant_generic(const encoder_state_t * const state, coeff_t *coef, coeff_t *q_coef, int32_t width,
int32_t height, int8_t type, int8_t scan_idx, int8_t block_type);
#endif //STRATEGIES_QUANT_GENERIC_H_

View file

@ -0,0 +1,41 @@
/*****************************************************************************
* This file is part of Kvazaar HEVC encoder.
*
* Copyright (C) 2013-2015 Tampere University of Technology and others (see
* COPYING file).
*
* 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.
*
* 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.
*
* You should have received a copy of the GNU General Public License along
* with Kvazaar. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include "strategies-quant.h"
#include "strategyselector.h"
// Define function pointers.
quant_func *kvz_quant;
// Headers for platform optimizations.
#include "generic/quant-generic.h"
#include "avx2/quant-avx2.h"
int kvz_strategy_register_quant(void* opaque, uint8_t bitdepth) {
bool success = true;
success &= kvz_strategy_register_quant_generic(opaque, bitdepth);
if (kvz_g_hardware_flags.intel_flags.avx2) {
success &= kvz_strategy_register_quant_avx2(opaque, bitdepth);
}
return success;
}

View file

@ -0,0 +1,40 @@
#ifndef STRATEGIES_QUANT_H_
#define STRATEGIES_QUANT_H_
/*****************************************************************************
* This file is part of Kvazaar HEVC encoder.
*
* Copyright (C) 2013-2015 Tampere University of Technology and others (see
* COPYING file).
*
* 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.
*
* 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.
*
* You should have received a copy of the GNU General Public License along
* with Kvazaar. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include "encoderstate.h"
// Declare function pointers.
typedef unsigned (quant_func)(const encoder_state_t * const state, coeff_t *coef, coeff_t *q_coef, int32_t width,
int32_t height, int8_t type, int8_t scan_idx, int8_t block_type);
// Declare function pointers.
extern quant_func * kvz_quant;
int kvz_strategy_register_quant(void* opaque, uint8_t bitdepth);
#define STRATEGIES_QUANT_EXPORTS \
{"quant", (void**) &kvz_quant}, \
#endif //STRATEGIES_QUANT_H_

View file

@ -70,6 +70,11 @@ int kvz_strategyselector_init(int32_t cpuid, uint8_t bitdepth) {
return 0;
}
if (!kvz_strategy_register_quant(&strategies, bitdepth)) {
fprintf(stderr, "kvz_strategy_register_quant failed!\n");
return 0;
}
while(cur_strategy_to_select->fptr) {
*(cur_strategy_to_select->fptr) = strategyselector_choose_for(&strategies, cur_strategy_to_select->strategy_type);

View file

@ -148,12 +148,14 @@ int kvz_strategyselector_register(void *opaque, const char *type, const char *st
#include "strategies/strategies-picture.h"
#include "strategies/strategies-dct.h"
#include "strategies/strategies-ipol.h"
#include "strategies/strategies-quant.h"
static const strategy_to_select_t strategies_to_select[] = {
STRATEGIES_NAL_EXPORTS
STRATEGIES_PICTURE_EXPORTS
STRATEGIES_DCT_EXPORTS
STRATEGIES_IPOL_EXPORTS
STRATEGIES_QUANT_EXPORTS
{ NULL, NULL },
};

View file

@ -33,6 +33,8 @@
#include "nal.h"
#include "rdo.h"
#include "strategies/strategies-dct.h"
#include "strategies/strategies-quant.h"
#include "strategies/generic/quant-generic.h"
//////////////////////////////////////////////////////////////////////////
// INITIALIZATIONS
@ -127,137 +129,6 @@ void kvz_itransform2d(const encoder_control_t * const encoder, int16_t *block, i
idct_func(encoder->bitdepth, coeff, block);
}
#define QUANT_SHIFT 14
/**
* \brief quantize transformed coefficents
*
*/
void kvz_quant(const encoder_state_t * const state, coeff_t *coef, coeff_t *q_coef, int32_t width,
int32_t height, int8_t type, int8_t scan_idx, int8_t block_type )
{
const encoder_control_t * const encoder = state->encoder_control;
const uint32_t log2_block_size = kvz_g_convert_to_bit[ width ] + 2;
const uint32_t * const scan = kvz_g_sig_last_scan[ scan_idx ][ log2_block_size - 1 ];
int32_t qp_scaled = kvz_get_scaled_qp(type, state->global->QP, (encoder->bitdepth-8)*6);
const uint32_t log2_tr_size = kvz_g_convert_to_bit[ width ] + 2;
const int32_t scalinglist_type = (block_type == CU_INTRA ? 0 : 3) + (int8_t)("\0\3\1\2"[type]);
const int32_t *quant_coeff = encoder->scaling_list.quant_coeff[log2_tr_size-2][scalinglist_type][qp_scaled%6];
const int32_t transform_shift = MAX_TR_DYNAMIC_RANGE - encoder->bitdepth - log2_tr_size; //!< Represents scaling through forward transform
const int32_t q_bits = QUANT_SHIFT + qp_scaled/6 + transform_shift;
const int32_t add = ((state->global->slicetype == KVZ_SLICE_I) ? 171 : 85) << (q_bits - 9);
const int32_t q_bits8 = q_bits - 8;
uint32_t ac_sum = 0;
for (int32_t n = 0; n < width * height; n++) {
int32_t level;
int32_t sign;
level = coef[n];
sign = (level < 0 ? -1: 1);
level = ((int64_t)abs(level) * quant_coeff[n] + add) >> q_bits;
ac_sum += level;
level *= sign;
q_coef[n] = (coeff_t)(CLIP( -32768, 32767, level));
}
if (!(encoder->sign_hiding && ac_sum >= 2)) return;
int32_t delta_u[LCU_WIDTH*LCU_WIDTH >> 2];
for (int32_t n = 0; n < width * height; n++) {
int32_t level;
level = coef[n];
level = ((int64_t)abs(level) * quant_coeff[n] + add) >> q_bits;
delta_u[n] = (int32_t)(((int64_t)abs(coef[n]) * quant_coeff[n] - (level << q_bits)) >> q_bits8);
}
if(ac_sum >= 2) {
#define SCAN_SET_SIZE 16
#define LOG2_SCAN_SET_SIZE 4
int32_t n,last_cg = -1, abssum = 0, subset, subpos;
for(subset = (width*height - 1)>>LOG2_SCAN_SET_SIZE; subset >= 0; subset--) {
int32_t first_nz_pos_in_cg = SCAN_SET_SIZE, last_nz_pos_in_cg=-1;
subpos = subset<<LOG2_SCAN_SET_SIZE;
abssum = 0;
// Find last coeff pos
for (n = SCAN_SET_SIZE - 1; n >= 0; n--) {
if (q_coef[scan[n + subpos]]) {
last_nz_pos_in_cg = n;
break;
}
}
// First coeff pos
for (n = 0; n <SCAN_SET_SIZE; n++) {
if (q_coef[scan[n + subpos]]) {
first_nz_pos_in_cg = n;
break;
}
}
// Sum all kvz_quant coeffs between first and last
for(n = first_nz_pos_in_cg; n <= last_nz_pos_in_cg; n++) {
abssum += q_coef[scan[n + subpos]];
}
if(last_nz_pos_in_cg >= 0 && last_cg == -1) {
last_cg = 1;
}
if(last_nz_pos_in_cg - first_nz_pos_in_cg >= 4) {
int32_t signbit = (q_coef[scan[subpos + first_nz_pos_in_cg]] > 0 ? 0 : 1) ;
if(signbit != (abssum&0x1)) { // compare signbit with sum_parity
int32_t min_cost_inc = 0x7fffffff, min_pos =-1, cur_cost=0x7fffffff;
int16_t final_change = 0, cur_change=0;
for(n = (last_cg == 1 ? last_nz_pos_in_cg : SCAN_SET_SIZE - 1); n >= 0; n--) {
uint32_t blkPos = scan[n + subpos];
if(q_coef[blkPos] != 0) {
if(delta_u[blkPos] > 0) {
cur_cost = -delta_u[blkPos];
cur_change=1;
} else if(n == first_nz_pos_in_cg && abs(q_coef[blkPos]) == 1) {
cur_cost=0x7fffffff;
} else {
cur_cost = delta_u[blkPos];
cur_change =-1;
}
} else if(n < first_nz_pos_in_cg && ((coef[blkPos] >= 0)?0:1) != signbit) {
cur_cost = 0x7fffffff;
} else {
cur_cost = -delta_u[blkPos];
cur_change = 1;
}
if(cur_cost < min_cost_inc) {
min_cost_inc = cur_cost;
final_change = cur_change;
min_pos = blkPos;
}
} // CG loop
if(q_coef[min_pos] == 32767 || q_coef[min_pos] == -32768) {
final_change = -1;
}
if(coef[min_pos] >= 0) q_coef[min_pos] += final_change;
else q_coef[min_pos] -= final_change;
} // Hide
}
if (last_cg == 1) last_cg=0;
}
#undef SCAN_SET_SIZE
#undef LOG2_SCAN_SET_SIZE
}
}
/**
* \brief inverse quantize transformed and quantized coefficents
*

View file

@ -35,8 +35,6 @@ extern const int16_t kvz_g_inv_quant_scales[6];
void kvz_quant(const encoder_state_t *state, coeff_t *coef, coeff_t *q_coef, int32_t width,
int32_t height, int8_t type, int8_t scan_idx, int8_t block_type);
void kvz_dequant(const encoder_state_t *state, coeff_t *q_coef, coeff_t *coef, int32_t width, int32_t height, int8_t type, int8_t block_type);
void kvz_transformskip(const encoder_control_t *encoder, int16_t *block,int16_t *coeff, int8_t block_size);