2014-01-24 10:37:15 +00:00
|
|
|
/*****************************************************************************
|
2021-11-23 06:46:06 +00:00
|
|
|
* This file is part of uvg266 VVC encoder.
|
2014-02-21 13:00:20 +00:00
|
|
|
*
|
2021-10-07 08:32:59 +00:00
|
|
|
* Copyright (c) 2021, Tampere University, ITU/ISO/IEC, project contributors
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
* are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer in the documentation and/or
|
|
|
|
* other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* * Neither the name of the Tampere University or ITU/ISO/IEC nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived from
|
|
|
|
* this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND ON
|
|
|
|
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
* INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS
|
2014-01-24 10:37:15 +00:00
|
|
|
****************************************************************************/
|
|
|
|
|
2013-09-18 09:16:03 +00:00
|
|
|
#include "cabac.h"
|
2016-08-09 13:16:33 +00:00
|
|
|
|
|
|
|
#include "encoder.h"
|
2016-06-07 08:28:30 +00:00
|
|
|
#include "encoderstate.h"
|
2022-04-28 11:26:05 +00:00
|
|
|
#include "uvg266.h"
|
2012-06-04 10:47:12 +00:00
|
|
|
|
2022-04-28 11:18:09 +00:00
|
|
|
#ifdef UVG_DEBUG_PRINT_CABAC
|
|
|
|
uint32_t uvg_cabac_bins_count = 0;
|
|
|
|
bool uvg_cabac_bins_verbose = true;
|
2018-09-10 08:00:00 +00:00
|
|
|
#endif
|
|
|
|
|
2022-04-28 11:18:09 +00:00
|
|
|
const uint8_t uvg_g_auc_renorm_table[32] =
|
2013-09-19 09:28:57 +00:00
|
|
|
{
|
|
|
|
6, 5, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2,
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
|
|
|
|
};
|
2019-03-27 10:38:19 +00:00
|
|
|
|
2022-04-28 11:18:09 +00:00
|
|
|
static const uint8_t uvg_tb_max[257] = { 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
|
2019-11-22 12:00:09 +00:00
|
|
|
4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
|
|
|
|
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
|
|
|
|
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
|
|
|
|
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7,
|
|
|
|
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
|
|
|
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
|
|
|
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
|
|
|
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
|
|
|
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8 };
|
2019-03-20 13:17:54 +00:00
|
|
|
|
2012-06-04 10:47:12 +00:00
|
|
|
|
2013-09-19 09:28:57 +00:00
|
|
|
/**
|
|
|
|
* \brief Initialize struct cabac_data.
|
|
|
|
*/
|
2022-04-28 11:18:09 +00:00
|
|
|
void uvg_cabac_start(cabac_data_t * const data)
|
2012-06-04 10:47:12 +00:00
|
|
|
{
|
2013-09-19 09:28:57 +00:00
|
|
|
data->low = 0;
|
|
|
|
data->range = 510;
|
|
|
|
data->bits_left = 23;
|
2013-09-18 11:06:45 +00:00
|
|
|
data->num_buffered_bytes = 0;
|
2013-09-19 09:28:57 +00:00
|
|
|
data->buffered_byte = 0xff;
|
2014-04-04 08:49:31 +00:00
|
|
|
data->only_count = 0; // By default, write bits out
|
2012-06-04 10:47:12 +00:00
|
|
|
}
|
|
|
|
|
2013-09-19 09:28:57 +00:00
|
|
|
/**
|
|
|
|
* \brief
|
|
|
|
*/
|
2022-04-28 11:18:09 +00:00
|
|
|
void uvg_cabac_encode_bin(cabac_data_t * const data, const uint32_t bin_value)
|
2012-06-04 10:47:12 +00:00
|
|
|
{
|
2019-03-20 07:06:26 +00:00
|
|
|
uint32_t lps = CTX_LPS(data->cur_ctx, data->range);
|
2014-02-21 13:00:20 +00:00
|
|
|
|
2013-09-19 09:28:57 +00:00
|
|
|
data->range -= lps;
|
2014-02-21 13:00:20 +00:00
|
|
|
|
2013-09-19 09:28:57 +00:00
|
|
|
// Not the Most Probable Symbol?
|
2014-09-23 21:55:28 +00:00
|
|
|
if ((bin_value ? 1 : 0) != CTX_MPS(data->cur_ctx)) {
|
2022-04-28 11:18:09 +00:00
|
|
|
int num_bits = uvg_g_auc_renorm_table[lps >> 3];
|
2013-09-19 09:28:57 +00:00
|
|
|
data->low = (data->low + data->range) << num_bits;
|
2013-09-19 08:05:42 +00:00
|
|
|
data->range = lps << num_bits;
|
2019-03-20 07:06:26 +00:00
|
|
|
|
2013-09-19 08:05:42 +00:00
|
|
|
data->bits_left -= num_bits;
|
2019-03-20 07:06:26 +00:00
|
|
|
if (data->bits_left < 12) {
|
2022-04-28 11:18:09 +00:00
|
|
|
uvg_cabac_write(data);
|
2019-03-20 07:06:26 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (data->range < 256) {
|
|
|
|
data->low <<= 1;
|
|
|
|
data->range <<= 1;
|
|
|
|
data->bits_left--;
|
|
|
|
|
|
|
|
if (data->bits_left < 12) {
|
2022-04-28 11:18:09 +00:00
|
|
|
uvg_cabac_write(data);
|
2019-03-20 07:06:26 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-04 10:47:12 +00:00
|
|
|
}
|
2019-03-20 07:06:26 +00:00
|
|
|
CTX_UPDATE(data->cur_ctx, bin_value);
|
2012-06-04 10:47:12 +00:00
|
|
|
}
|
|
|
|
|
2013-09-19 09:28:57 +00:00
|
|
|
/**
|
|
|
|
* \brief
|
|
|
|
*/
|
2022-04-28 11:18:09 +00:00
|
|
|
void uvg_cabac_write(cabac_data_t * const data)
|
2012-06-04 10:47:12 +00:00
|
|
|
{
|
2013-09-19 08:05:42 +00:00
|
|
|
uint32_t lead_byte = data->low >> (24 - data->bits_left);
|
2013-09-18 11:06:45 +00:00
|
|
|
data->bits_left += 8;
|
2013-09-19 08:05:42 +00:00
|
|
|
data->low &= 0xffffffffu >> data->bits_left;
|
2014-02-21 13:00:20 +00:00
|
|
|
|
2014-04-04 08:49:31 +00:00
|
|
|
// Binary counter mode
|
|
|
|
if(data->only_count) {
|
|
|
|
data->num_buffered_bytes++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-19 09:28:57 +00:00
|
|
|
if (lead_byte == 0xff) {
|
2013-09-18 11:06:45 +00:00
|
|
|
data->num_buffered_bytes++;
|
2013-09-19 09:28:57 +00:00
|
|
|
} else {
|
|
|
|
if (data->num_buffered_bytes > 0) {
|
2013-09-19 08:05:42 +00:00
|
|
|
uint32_t carry = lead_byte >> 8;
|
|
|
|
uint32_t byte = data->buffered_byte + carry;
|
|
|
|
data->buffered_byte = lead_byte & 0xff;
|
2022-04-28 11:18:09 +00:00
|
|
|
uvg_bitstream_put_byte(data->stream, byte);
|
2012-06-04 10:47:12 +00:00
|
|
|
|
2013-09-19 09:28:57 +00:00
|
|
|
byte = (0xff + carry) & 0xff;
|
|
|
|
while (data->num_buffered_bytes > 1) {
|
2022-04-28 11:18:09 +00:00
|
|
|
uvg_bitstream_put_byte(data->stream, byte);
|
2013-09-18 11:06:45 +00:00
|
|
|
data->num_buffered_bytes--;
|
2012-06-04 10:47:12 +00:00
|
|
|
}
|
2013-09-19 09:28:57 +00:00
|
|
|
} else {
|
2013-09-18 11:06:45 +00:00
|
|
|
data->num_buffered_bytes = 1;
|
2013-09-19 08:05:42 +00:00
|
|
|
data->buffered_byte = lead_byte;
|
2012-06-05 11:01:47 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-04 10:47:12 +00:00
|
|
|
}
|
|
|
|
|
2013-09-19 09:28:57 +00:00
|
|
|
/**
|
|
|
|
* \brief
|
|
|
|
*/
|
2022-04-28 11:18:09 +00:00
|
|
|
void uvg_cabac_finish(cabac_data_t * const data)
|
2012-06-04 10:47:12 +00:00
|
|
|
{
|
2014-02-21 09:57:25 +00:00
|
|
|
assert(data->bits_left <= 32);
|
|
|
|
|
2013-09-19 09:28:57 +00:00
|
|
|
if (data->low >> (32 - data->bits_left)) {
|
2022-04-28 11:18:09 +00:00
|
|
|
uvg_bitstream_put_byte(data->stream, data->buffered_byte + 1);
|
2013-09-19 09:28:57 +00:00
|
|
|
while (data->num_buffered_bytes > 1) {
|
2022-04-28 11:18:09 +00:00
|
|
|
uvg_bitstream_put_byte(data->stream, 0);
|
2013-09-18 11:06:45 +00:00
|
|
|
data->num_buffered_bytes--;
|
2012-06-04 10:47:12 +00:00
|
|
|
}
|
2013-09-19 09:28:57 +00:00
|
|
|
data->low -= 1 << (32 - data->bits_left);
|
|
|
|
} else {
|
|
|
|
if (data->num_buffered_bytes > 0) {
|
2022-04-28 11:18:09 +00:00
|
|
|
uvg_bitstream_put_byte(data->stream, data->buffered_byte);
|
2012-06-04 10:47:12 +00:00
|
|
|
}
|
2013-09-19 09:28:57 +00:00
|
|
|
while (data->num_buffered_bytes > 1) {
|
2022-04-28 11:18:09 +00:00
|
|
|
uvg_bitstream_put_byte(data->stream, 0xff);
|
2013-09-18 11:06:45 +00:00
|
|
|
data->num_buffered_bytes--;
|
2012-06-05 11:01:47 +00:00
|
|
|
}
|
2012-06-04 10:47:12 +00:00
|
|
|
}
|
2014-02-21 09:57:25 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
uint8_t bits = (uint8_t)(24 - data->bits_left);
|
2022-04-28 11:18:09 +00:00
|
|
|
uvg_bitstream_put(data->stream, data->low >> 8, bits);
|
2014-02-21 09:57:25 +00:00
|
|
|
}
|
2012-06-04 10:47:12 +00:00
|
|
|
}
|
|
|
|
|
2013-03-07 15:42:00 +00:00
|
|
|
/*!
|
|
|
|
\brief Encode terminating bin
|
|
|
|
\param binValue bin value
|
|
|
|
*/
|
2022-04-28 11:18:09 +00:00
|
|
|
void uvg_cabac_encode_bin_trm(cabac_data_t * const data, const uint8_t bin_value)
|
2012-06-04 10:47:12 +00:00
|
|
|
{
|
2013-09-19 08:05:42 +00:00
|
|
|
data->range -= 2;
|
2013-09-19 09:28:57 +00:00
|
|
|
if(bin_value) {
|
|
|
|
data->low += data->range;
|
2013-09-19 08:05:42 +00:00
|
|
|
data->low <<= 7;
|
|
|
|
data->range = 2 << 7;
|
2013-09-18 11:06:45 +00:00
|
|
|
data->bits_left -= 7;
|
2013-09-19 09:28:57 +00:00
|
|
|
} else if (data->range >= 256) {
|
2012-06-04 10:47:12 +00:00
|
|
|
return;
|
2013-09-19 09:28:57 +00:00
|
|
|
} else {
|
|
|
|
data->low <<= 1;
|
2013-09-19 08:05:42 +00:00
|
|
|
data->range <<= 1;
|
2013-09-18 11:06:45 +00:00
|
|
|
data->bits_left--;
|
2012-06-04 10:47:12 +00:00
|
|
|
}
|
2014-02-21 13:00:20 +00:00
|
|
|
|
2013-09-19 09:28:57 +00:00
|
|
|
if (data->bits_left < 12) {
|
2022-04-28 11:18:09 +00:00
|
|
|
uvg_cabac_write(data);
|
2012-06-04 10:47:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-20 13:17:54 +00:00
|
|
|
/**
|
|
|
|
* \brief encode truncated binary code
|
|
|
|
*/
|
2022-04-28 11:18:09 +00:00
|
|
|
void uvg_cabac_encode_trunc_bin(cabac_data_t * const data, const uint32_t bin_value, const uint32_t max_value) {
|
2019-03-20 13:17:54 +00:00
|
|
|
int thresh;
|
|
|
|
int symbol = bin_value;
|
|
|
|
if (max_value > 256) {
|
|
|
|
int threshVal = 1 << 8;
|
|
|
|
thresh = 8;
|
|
|
|
while (threshVal <= max_value) {
|
|
|
|
thresh++;
|
|
|
|
threshVal <<= 1;
|
|
|
|
}
|
|
|
|
thresh--;
|
|
|
|
} else {
|
2022-04-28 11:18:09 +00:00
|
|
|
thresh = uvg_tb_max[max_value];
|
2019-03-20 13:17:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int val = 1 << thresh;
|
|
|
|
|
|
|
|
int b = max_value - val;
|
|
|
|
if (symbol < val - b) {
|
|
|
|
CABAC_BINS_EP(data, symbol, thresh, "TruncSymbols");
|
|
|
|
} else {
|
|
|
|
symbol += val - b;
|
|
|
|
CABAC_BINS_EP(data, symbol, thresh + 1, "TruncSymbols");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-19 09:28:57 +00:00
|
|
|
/**
|
|
|
|
* \brief
|
|
|
|
*/
|
2022-04-28 11:18:09 +00:00
|
|
|
void uvg_cabac_encode_bin_ep(cabac_data_t * const data, const uint32_t bin_value)
|
2012-06-04 10:47:12 +00:00
|
|
|
{
|
2013-09-19 08:05:42 +00:00
|
|
|
data->low <<= 1;
|
2013-09-19 09:28:57 +00:00
|
|
|
if (bin_value) {
|
2013-09-19 08:05:42 +00:00
|
|
|
data->low += data->range;
|
2012-06-04 10:47:12 +00:00
|
|
|
}
|
2013-09-18 11:06:45 +00:00
|
|
|
data->bits_left--;
|
2012-06-04 10:47:12 +00:00
|
|
|
|
2013-09-19 09:28:57 +00:00
|
|
|
if (data->bits_left < 12) {
|
2022-04-28 11:18:09 +00:00
|
|
|
uvg_cabac_write(data);
|
2012-06-04 10:47:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-27 10:38:19 +00:00
|
|
|
// Import from VTM 4.0
|
2022-04-28 11:18:09 +00:00
|
|
|
void uvg_cabac_encode_aligned_bins_ep(cabac_data_t * const data, uint32_t bin_values, int num_bins)
|
2019-03-27 10:38:19 +00:00
|
|
|
{
|
|
|
|
uint32_t rem_bins = num_bins;
|
|
|
|
while (rem_bins > 0) {
|
|
|
|
//The process of encoding an EP bin is the same as that of coding a normal
|
|
|
|
//bin where the symbol ranges for 1 and 0 are both half the range:
|
|
|
|
//
|
|
|
|
// low = (low + range/2) << 1 (to encode a 1)
|
|
|
|
// low = low << 1 (to encode a 0)
|
|
|
|
//
|
|
|
|
// i.e.
|
|
|
|
// low = (low + (bin * range/2)) << 1
|
|
|
|
//
|
|
|
|
// which is equivalent to:
|
|
|
|
//
|
|
|
|
// low = (low << 1) + (bin * range)
|
|
|
|
//
|
|
|
|
// this can be generalised for multiple bins, producing the following expression:
|
|
|
|
//
|
|
|
|
unsigned bins_to_code = MIN(rem_bins, 8); //code bytes if able to take advantage of the system's byte-write function
|
|
|
|
unsigned bin_mask = (1 << bins_to_code) - 1;
|
2019-07-03 12:55:52 +00:00
|
|
|
unsigned new_bins = (bin_values >> (rem_bins - bins_to_code)) & bin_mask;
|
2019-03-27 10:38:19 +00:00
|
|
|
data->low = (data->low << bins_to_code) + (new_bins << 8); //range is known to be 256
|
|
|
|
rem_bins -= bins_to_code;
|
|
|
|
data->bits_left -= bins_to_code;
|
|
|
|
if (data->bits_left < 12) {
|
2022-04-28 11:18:09 +00:00
|
|
|
uvg_cabac_write(data);
|
2019-03-27 10:38:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-19 09:28:57 +00:00
|
|
|
/**
|
|
|
|
* \brief
|
|
|
|
*/
|
2022-04-28 11:18:09 +00:00
|
|
|
void uvg_cabac_encode_bins_ep(cabac_data_t * const data, uint32_t bin_values, int num_bins)
|
2012-06-04 10:47:12 +00:00
|
|
|
{
|
|
|
|
uint32_t pattern;
|
2019-07-03 12:55:52 +00:00
|
|
|
|
2019-03-27 10:38:19 +00:00
|
|
|
if (data->range == 256) {
|
2022-04-28 11:18:09 +00:00
|
|
|
uvg_cabac_encode_aligned_bins_ep(data, bin_values, num_bins);
|
2019-03-27 10:38:19 +00:00
|
|
|
return;
|
2019-07-03 12:55:52 +00:00
|
|
|
}
|
2013-09-19 09:28:57 +00:00
|
|
|
while (num_bins > 8) {
|
2013-09-19 08:05:42 +00:00
|
|
|
num_bins -= 8;
|
|
|
|
pattern = bin_values >> num_bins;
|
|
|
|
data->low <<= 8;
|
|
|
|
data->low += data->range * pattern;
|
|
|
|
bin_values -= pattern << num_bins;
|
2013-09-18 11:06:45 +00:00
|
|
|
data->bits_left -= 8;
|
2014-02-21 13:00:20 +00:00
|
|
|
|
2013-09-19 09:28:57 +00:00
|
|
|
if(data->bits_left < 12) {
|
2022-04-28 11:18:09 +00:00
|
|
|
uvg_cabac_write(data);
|
2012-06-04 10:47:12 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-21 13:00:20 +00:00
|
|
|
|
2013-09-19 08:05:42 +00:00
|
|
|
data->low <<= num_bins;
|
|
|
|
data->low += data->range * bin_values;
|
|
|
|
data->bits_left -= num_bins;
|
2014-02-21 13:00:20 +00:00
|
|
|
|
2013-09-19 09:28:57 +00:00
|
|
|
if (data->bits_left < 12) {
|
2022-04-28 11:18:09 +00:00
|
|
|
uvg_cabac_write(data);
|
2012-06-04 10:47:12 +00:00
|
|
|
}
|
2012-06-08 12:26:07 +00:00
|
|
|
}
|
2013-02-05 13:48:06 +00:00
|
|
|
|
2013-09-19 09:28:57 +00:00
|
|
|
/**
|
2018-08-30 11:24:03 +00:00
|
|
|
* \brief Coding of remainder abs coeff value.
|
|
|
|
* \param remainder Value of remaining abs coeff
|
|
|
|
* \param rice_param Reference to Rice parameter.
|
2013-09-19 09:28:57 +00:00
|
|
|
*/
|
2022-04-28 11:18:09 +00:00
|
|
|
void uvg_cabac_write_coeff_remain(cabac_data_t * const cabac, const uint32_t remainder, const uint32_t rice_param, const unsigned int cutoff)
|
2013-02-05 13:48:06 +00:00
|
|
|
{
|
2019-11-19 12:32:38 +00:00
|
|
|
const unsigned threshold = cutoff << rice_param;
|
2018-08-30 11:24:03 +00:00
|
|
|
uint32_t bins = remainder;
|
|
|
|
|
|
|
|
if (bins < threshold) {
|
|
|
|
uint32_t length = (bins >> rice_param) + 1;
|
|
|
|
CABAC_BINS_EP(cabac, ((1 << (length)) - 2) , length, "coeff_abs_level_remaining");
|
|
|
|
CABAC_BINS_EP(cabac, bins & ((1 << rice_param) - 1), rice_param, "coeff_abs_level_remaining");
|
2019-11-19 12:32:38 +00:00
|
|
|
} else {
|
|
|
|
const unsigned max_prefix_length = 32 - cutoff - 15/*max_log2_tr_dynamic_range*/;
|
2019-06-05 12:57:18 +00:00
|
|
|
unsigned prefix_length = 0;
|
2019-11-19 12:32:38 +00:00
|
|
|
unsigned code_value = (bins >> rice_param) - cutoff;
|
2019-06-05 12:57:18 +00:00
|
|
|
unsigned suffix_length;
|
|
|
|
if (code_value >= ((1 << max_prefix_length) - 1)) {
|
|
|
|
prefix_length = max_prefix_length;
|
|
|
|
suffix_length = 15 /*max_log2_tr_dynamic_range*/;
|
|
|
|
} else {
|
|
|
|
while (code_value > ((2 << prefix_length) - 2)) {
|
|
|
|
prefix_length++;
|
|
|
|
}
|
|
|
|
suffix_length = prefix_length + rice_param + 1;
|
|
|
|
}
|
2019-11-19 12:32:38 +00:00
|
|
|
const unsigned total_prefix_length = prefix_length + cutoff;
|
2019-06-05 12:57:18 +00:00
|
|
|
const unsigned bit_mask = (1 << rice_param) - 1;
|
|
|
|
const unsigned prefix = (1 << total_prefix_length) - 1;
|
|
|
|
const unsigned suffix = ((code_value - ((1 << prefix_length) - 1)) << rice_param) | (bins & bit_mask);
|
|
|
|
CABAC_BINS_EP(cabac, prefix, total_prefix_length, "coeff_abs_level_remaining");
|
|
|
|
CABAC_BINS_EP(cabac, suffix, suffix_length, "coeff_abs_level_remaining");
|
|
|
|
}
|
2018-08-30 11:24:03 +00:00
|
|
|
|
2013-08-02 13:35:30 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 10:38:19 +00:00
|
|
|
|
2013-09-19 09:28:57 +00:00
|
|
|
/**
|
|
|
|
* \brief
|
|
|
|
*/
|
2022-04-28 11:18:09 +00:00
|
|
|
void uvg_cabac_write_unary_max_symbol(cabac_data_t * const data, cabac_ctx_t * const ctx, uint32_t symbol, const int32_t offset, const uint32_t max_symbol)
|
2013-08-02 13:35:30 +00:00
|
|
|
{
|
2013-09-19 09:28:57 +00:00
|
|
|
int8_t code_last = max_symbol > symbol;
|
2013-08-02 13:35:30 +00:00
|
|
|
|
2013-10-31 22:52:06 +00:00
|
|
|
assert(symbol <= max_symbol);
|
|
|
|
|
2013-09-19 09:28:57 +00:00
|
|
|
if (!max_symbol) return;
|
2014-02-21 13:00:20 +00:00
|
|
|
|
2019-07-05 11:10:11 +00:00
|
|
|
data->cur_ctx = ctx;
|
2014-05-06 14:37:49 +00:00
|
|
|
CABAC_BIN(data, symbol, "ums");
|
2014-02-21 13:00:20 +00:00
|
|
|
|
2013-09-19 09:28:57 +00:00
|
|
|
if (!symbol) return;
|
2014-02-21 13:00:20 +00:00
|
|
|
|
2022-05-04 07:25:03 +00:00
|
|
|
data->cur_ctx = &ctx[offset];
|
|
|
|
|
2013-09-19 09:28:57 +00:00
|
|
|
while (--symbol) {
|
2013-10-31 22:52:06 +00:00
|
|
|
CABAC_BIN(data, 1, "ums");
|
2013-08-02 13:35:30 +00:00
|
|
|
}
|
2013-09-19 09:28:57 +00:00
|
|
|
if (code_last) {
|
2013-10-31 22:52:06 +00:00
|
|
|
CABAC_BIN(data, 0, "ums");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This can be used for Truncated Rice binarization with cRiceParam=0.
|
|
|
|
*/
|
2022-04-28 11:18:09 +00:00
|
|
|
void uvg_cabac_write_unary_max_symbol_ep(cabac_data_t * const data, unsigned int symbol, const unsigned int max_symbol)
|
2013-10-31 22:52:06 +00:00
|
|
|
{
|
|
|
|
/*if (symbol == 0) {
|
|
|
|
CABAC_BIN_EP(data, 0, "ums_ep");
|
|
|
|
} else {
|
|
|
|
// Make a bit-string of (symbol) times 1 and a single 0, except when
|
|
|
|
// symbol == max_symbol.
|
|
|
|
unsigned bins = ((1 << symbol) - 1) << (symbol < max_symbol);
|
|
|
|
CABAC_BINS_EP(data, bins, symbol + (symbol < max_symbol), "ums_ep");
|
|
|
|
}*/
|
2014-02-21 13:00:20 +00:00
|
|
|
|
2013-10-31 22:52:06 +00:00
|
|
|
int8_t code_last = max_symbol > symbol;
|
|
|
|
|
|
|
|
assert(symbol <= max_symbol);
|
|
|
|
|
|
|
|
CABAC_BIN_EP(data, symbol ? 1 : 0, "ums_ep");
|
2014-02-21 13:00:20 +00:00
|
|
|
|
2013-10-31 22:52:06 +00:00
|
|
|
if (!symbol) return;
|
2014-02-21 13:00:20 +00:00
|
|
|
|
2013-10-31 22:52:06 +00:00
|
|
|
while (--symbol) {
|
|
|
|
CABAC_BIN_EP(data, 1, "ums_ep");
|
|
|
|
}
|
|
|
|
if (code_last) {
|
|
|
|
CABAC_BIN_EP(data, 0, "ums_ep");
|
2013-08-02 13:35:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-19 09:28:57 +00:00
|
|
|
/**
|
|
|
|
* \brief
|
|
|
|
*/
|
2022-04-28 11:18:09 +00:00
|
|
|
void uvg_cabac_write_ep_ex_golomb(encoder_state_t * const state,
|
2017-08-03 11:11:36 +00:00
|
|
|
cabac_data_t * const data,
|
|
|
|
uint32_t symbol,
|
|
|
|
uint32_t count)
|
2013-08-02 13:35:30 +00:00
|
|
|
{
|
|
|
|
uint32_t bins = 0;
|
2013-09-19 08:05:42 +00:00
|
|
|
int32_t num_bins = 0;
|
2014-02-21 13:00:20 +00:00
|
|
|
|
2013-09-19 09:28:57 +00:00
|
|
|
while (symbol >= (uint32_t)(1 << count)) {
|
2013-08-02 13:35:30 +00:00
|
|
|
bins = 2 * bins + 1;
|
2013-09-19 09:28:57 +00:00
|
|
|
++num_bins;
|
2013-09-19 08:05:42 +00:00
|
|
|
symbol -= 1 << count;
|
2013-09-19 09:28:57 +00:00
|
|
|
++count;
|
2013-08-02 13:35:30 +00:00
|
|
|
}
|
2013-09-19 09:28:57 +00:00
|
|
|
bins = 2 * bins;
|
|
|
|
++num_bins;
|
2014-02-21 13:00:20 +00:00
|
|
|
|
2016-06-07 08:28:30 +00:00
|
|
|
bins = (bins << count) | symbol;
|
2013-09-19 08:05:42 +00:00
|
|
|
num_bins += count;
|
2020-08-20 12:25:18 +00:00
|
|
|
|
2018-09-26 09:01:23 +00:00
|
|
|
CABAC_BINS_EP(data, bins, num_bins, "ep_ex_golomb");
|
2013-09-19 09:28:57 +00:00
|
|
|
}
|