diff --git a/src/Makefile b/src/Makefile index 31276e14..b12dddc6 100644 --- a/src/Makefile +++ b/src/Makefile @@ -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 diff --git a/src/rdo.c b/src/rdo.c index 12780aa4..eb687ef5 100644 --- a/src/rdo.c +++ b/src/rdo.c @@ -31,6 +31,7 @@ #include "context.h" #include "cabac.h" #include "transform.h" +#include "strategies/strategies-quant.h" #define QUANT_SHIFT 14 diff --git a/src/strategies/generic/quant-generic.c b/src/strategies/generic/quant-generic.c new file mode 100644 index 00000000..e9f0c9fa --- /dev/null +++ b/src/strategies/generic/quant-generic.c @@ -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 . + ****************************************************************************/ + +/* + * \file + */ + +#include + +#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 = 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; +} diff --git a/src/strategies/generic/quant-generic.h b/src/strategies/generic/quant-generic.h new file mode 100644 index 00000000..2309d9b7 --- /dev/null +++ b/src/strategies/generic/quant-generic.h @@ -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 . + ****************************************************************************/ +#include +#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_ diff --git a/src/strategies/strategies-quant.c b/src/strategies/strategies-quant.c new file mode 100644 index 00000000..dbc5e9fc --- /dev/null +++ b/src/strategies/strategies-quant.c @@ -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 . + ****************************************************************************/ + +#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; +} \ No newline at end of file diff --git a/src/strategies/strategies-quant.h b/src/strategies/strategies-quant.h new file mode 100644 index 00000000..6501f105 --- /dev/null +++ b/src/strategies/strategies-quant.h @@ -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 . + ****************************************************************************/ + +#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_ diff --git a/src/strategyselector.c b/src/strategyselector.c index 778f49ea..5b5429d1 100644 --- a/src/strategyselector.c +++ b/src/strategyselector.c @@ -69,6 +69,11 @@ int kvz_strategyselector_init(int32_t cpuid, uint8_t bitdepth) { fprintf(stderr, "kvz_strategy_register_ipol failed!\n"); 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); diff --git a/src/strategyselector.h b/src/strategyselector.h index c8a828b3..b410b1d0 100644 --- a/src/strategyselector.h +++ b/src/strategyselector.h @@ -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 }, }; diff --git a/src/transform.c b/src/transform.c index 163b6273..55f73dff 100644 --- a/src/transform.c +++ b/src/transform.c @@ -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<= 0; n--) { - if (q_coef[scan[n + subpos]]) { - last_nz_pos_in_cg = n; - break; - } - } - - // First coeff pos - for (n = 0; n = 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 * diff --git a/src/transform.h b/src/transform.h index cd00de7b..c4e19a47 100644 --- a/src/transform.h +++ b/src/transform.h @@ -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);