2010-05-18 17:58:33 +02:00
|
|
|
/*
|
2010-09-09 14:16:39 +02:00
|
|
|
* Copyright (c) 2010 The WebM project authors. All Rights Reserved.
|
2010-05-18 17:58:33 +02:00
|
|
|
*
|
2010-06-18 18:39:21 +02:00
|
|
|
* Use of this source code is governed by a BSD-style license
|
2010-06-04 22:19:40 +02:00
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
2010-06-18 18:39:21 +02:00
|
|
|
* in the file PATENTS. All contributing project authors may
|
2010-06-04 22:19:40 +02:00
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
2010-05-18 17:58:33 +02:00
|
|
|
*/
|
|
|
|
|
2013-06-15 00:11:18 +02:00
|
|
|
#include "vpx_mem/vpx_mem.h"
|
|
|
|
#include "vpx_ports/mem.h"
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-11-27 22:59:17 +01:00
|
|
|
#include "vp9/common/vp9_blockd.h"
|
2013-04-09 19:15:10 +02:00
|
|
|
#include "vp9/common/vp9_common.h"
|
2014-09-06 01:58:24 +02:00
|
|
|
#include "vp9/common/vp9_entropy.h"
|
2015-01-06 02:43:26 +01:00
|
|
|
#if CONFIG_COEFFICIENT_RANGE_CHECKING
|
|
|
|
#include "vp9/common/vp9_idct.h"
|
|
|
|
#endif
|
2011-10-05 12:26:00 +02:00
|
|
|
|
2013-06-15 00:11:18 +02:00
|
|
|
#include "vp9/decoder/vp9_detokenize.h"
|
|
|
|
|
2016-07-27 05:20:13 +02:00
|
|
|
#define EOB_CONTEXT_NODE 0
|
|
|
|
#define ZERO_CONTEXT_NODE 1
|
|
|
|
#define ONE_CONTEXT_NODE 2
|
|
|
|
|
|
|
|
#define INCREMENT_COUNT(token) \
|
|
|
|
do { \
|
|
|
|
if (counts) ++coef_counts[band][ctx][token]; \
|
2013-11-14 01:46:21 +01:00
|
|
|
} while (0)
|
|
|
|
|
2016-09-23 14:03:52 +02:00
|
|
|
static INLINE int read_bool(vpx_reader *r, int prob, BD_VALUE *value,
|
|
|
|
int *count, unsigned int *range) {
|
|
|
|
const unsigned int split = (*range * prob + (256 - prob)) >> CHAR_BIT;
|
|
|
|
const BD_VALUE bigsplit = (BD_VALUE)split << (BD_VALUE_SIZE - CHAR_BIT);
|
|
|
|
|
|
|
|
if (*count < 0) {
|
|
|
|
r->value = *value;
|
|
|
|
r->count = *count;
|
|
|
|
vpx_reader_fill(r);
|
|
|
|
*value = r->value;
|
|
|
|
*count = r->count;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*value >= bigsplit) {
|
|
|
|
*range = *range - split;
|
|
|
|
*value = *value - bigsplit;
|
|
|
|
{
|
|
|
|
const int shift = vpx_norm[*range];
|
|
|
|
*range <<= shift;
|
|
|
|
*value <<= shift;
|
|
|
|
*count -= shift;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
*range = split;
|
|
|
|
{
|
|
|
|
const int shift = vpx_norm[*range];
|
|
|
|
*range <<= shift;
|
|
|
|
*value <<= shift;
|
|
|
|
*count -= shift;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static INLINE int read_coeff(vpx_reader *r, const vpx_prob *probs, int n,
|
|
|
|
BD_VALUE *value, int *count, unsigned int *range) {
|
2014-09-06 01:58:24 +02:00
|
|
|
int i, val = 0;
|
2016-09-23 14:03:52 +02:00
|
|
|
for (i = 0; i < n; ++i)
|
|
|
|
val = (val << 1) | read_bool(r, probs[i], value, count, range);
|
2014-09-06 01:58:24 +02:00
|
|
|
return val;
|
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2016-07-27 05:20:13 +02:00
|
|
|
static int decode_coefs(const MACROBLOCKD *xd, PLANE_TYPE type,
|
2014-09-06 01:58:24 +02:00
|
|
|
tran_low_t *dqcoeff, TX_SIZE tx_size, const int16_t *dq,
|
|
|
|
int ctx, const int16_t *scan, const int16_t *nb,
|
2015-07-20 22:49:15 +02:00
|
|
|
vpx_reader *r) {
|
2015-06-02 18:06:00 +02:00
|
|
|
FRAME_COUNTS *counts = xd->counts;
|
2013-12-05 02:24:18 +01:00
|
|
|
const int max_eob = 16 << (tx_size << 1);
|
2015-06-01 17:18:27 +02:00
|
|
|
const FRAME_CONTEXT *const fc = xd->fc;
|
2016-01-20 01:40:20 +01:00
|
|
|
const int ref = is_inter_block(xd->mi[0]);
|
2013-08-27 23:17:53 +02:00
|
|
|
int band, c = 0;
|
2016-07-27 05:20:13 +02:00
|
|
|
const vpx_prob(*coef_probs)[COEFF_CONTEXTS][UNCONSTRAINED_NODES] =
|
2013-07-29 23:35:55 +02:00
|
|
|
fc->coef_probs[tx_size][type][ref];
|
2015-07-20 22:49:15 +02:00
|
|
|
const vpx_prob *prob;
|
2016-07-27 05:20:13 +02:00
|
|
|
unsigned int(*coef_counts)[COEFF_CONTEXTS][UNCONSTRAINED_NODES + 1];
|
|
|
|
unsigned int(*eob_branch_count)[COEFF_CONTEXTS];
|
2013-12-03 00:44:26 +01:00
|
|
|
uint8_t token_cache[32 * 32];
|
2013-11-14 01:02:06 +01:00
|
|
|
const uint8_t *band_translate = get_band_translate(tx_size);
|
2013-11-19 23:31:38 +01:00
|
|
|
const int dq_shift = (tx_size == TX_32X32);
|
2016-09-23 14:03:52 +02:00
|
|
|
int v;
|
2013-11-19 23:31:38 +01:00
|
|
|
int16_t dqv = dq[0];
|
2015-11-04 00:38:54 +01:00
|
|
|
const uint8_t *const cat6_prob =
|
|
|
|
#if CONFIG_VP9_HIGHBITDEPTH
|
2016-07-27 05:20:13 +02:00
|
|
|
(xd->bd == VPX_BITS_12)
|
|
|
|
? vp9_cat6_prob_high12
|
|
|
|
: (xd->bd == VPX_BITS_10) ? vp9_cat6_prob_high12 + 2 :
|
2015-11-04 00:38:54 +01:00
|
|
|
#endif // CONFIG_VP9_HIGHBITDEPTH
|
2016-07-27 05:20:13 +02:00
|
|
|
vp9_cat6_prob;
|
2015-11-04 00:38:54 +01:00
|
|
|
const int cat6_bits =
|
|
|
|
#if CONFIG_VP9_HIGHBITDEPTH
|
2017-02-14 05:06:18 +01:00
|
|
|
(xd->bd == VPX_BITS_12) ? 18
|
|
|
|
: (xd->bd == VPX_BITS_10) ? 16 :
|
2015-11-04 00:38:54 +01:00
|
|
|
#endif // CONFIG_VP9_HIGHBITDEPTH
|
2017-02-14 05:06:18 +01:00
|
|
|
14;
|
2016-09-23 14:03:52 +02:00
|
|
|
// Keep value, range, and count as locals. The compiler produces better
|
|
|
|
// results with the locals than using r directly.
|
|
|
|
BD_VALUE value = r->value;
|
|
|
|
unsigned int range = r->range;
|
|
|
|
int count = r->count;
|
2014-09-19 19:13:47 +02:00
|
|
|
|
2015-06-05 20:54:04 +02:00
|
|
|
if (counts) {
|
|
|
|
coef_counts = counts->coef[tx_size][type][ref];
|
|
|
|
eob_branch_count = counts->eob_branch[tx_size][type][ref];
|
|
|
|
}
|
|
|
|
|
2013-12-02 23:36:06 +01:00
|
|
|
while (c < max_eob) {
|
2014-09-06 01:58:24 +02:00
|
|
|
int val = -1;
|
2013-11-06 02:25:38 +01:00
|
|
|
band = *band_translate++;
|
2013-12-05 02:24:18 +01:00
|
|
|
prob = coef_probs[band][ctx];
|
2016-07-27 05:20:13 +02:00
|
|
|
if (counts) ++eob_branch_count[band][ctx];
|
2016-09-23 14:03:52 +02:00
|
|
|
if (!read_bool(r, prob[EOB_CONTEXT_NODE], &value, &count, &range)) {
|
2013-12-04 02:23:03 +01:00
|
|
|
INCREMENT_COUNT(EOB_MODEL_TOKEN);
|
2013-04-22 19:58:49 +02:00
|
|
|
break;
|
2013-12-02 23:36:06 +01:00
|
|
|
}
|
2013-05-09 19:47:58 +02:00
|
|
|
|
2016-09-23 14:03:52 +02:00
|
|
|
while (!read_bool(r, prob[ZERO_CONTEXT_NODE], &value, &count, &range)) {
|
2012-11-17 19:35:47 +01:00
|
|
|
INCREMENT_COUNT(ZERO_TOKEN);
|
2013-11-21 21:52:15 +01:00
|
|
|
dqv = dq[1];
|
2013-12-03 00:44:26 +01:00
|
|
|
token_cache[scan[c]] = 0;
|
2012-07-14 00:21:29 +02:00
|
|
|
++c;
|
2016-09-23 14:03:52 +02:00
|
|
|
if (c >= max_eob) {
|
|
|
|
r->value = value;
|
|
|
|
r->range = range;
|
|
|
|
r->count = count;
|
|
|
|
return c; // zero tokens at the end (no eob token)
|
|
|
|
}
|
2013-12-05 02:24:18 +01:00
|
|
|
ctx = get_coef_context(nb, token_cache, c);
|
2013-11-22 01:55:22 +01:00
|
|
|
band = *band_translate++;
|
2013-12-05 02:24:18 +01:00
|
|
|
prob = coef_probs[band][ctx];
|
2013-05-08 19:04:14 +02:00
|
|
|
}
|
|
|
|
|
2016-09-23 14:03:52 +02:00
|
|
|
if (read_bool(r, prob[ONE_CONTEXT_NODE], &value, &count, &range)) {
|
|
|
|
const vpx_prob *p = vp9_pareto8_full[prob[PIVOT_NODE] - 1];
|
2014-09-06 01:58:24 +02:00
|
|
|
INCREMENT_COUNT(TWO_TOKEN);
|
2016-09-23 14:03:52 +02:00
|
|
|
if (read_bool(r, p[0], &value, &count, &range)) {
|
|
|
|
if (read_bool(r, p[3], &value, &count, &range)) {
|
|
|
|
token_cache[scan[c]] = 5;
|
|
|
|
if (read_bool(r, p[5], &value, &count, &range)) {
|
|
|
|
if (read_bool(r, p[7], &value, &count, &range)) {
|
|
|
|
val = CAT6_MIN_VAL +
|
|
|
|
read_coeff(r, cat6_prob, cat6_bits, &value, &count, &range);
|
|
|
|
} else {
|
|
|
|
val = CAT5_MIN_VAL +
|
|
|
|
read_coeff(r, vp9_cat5_prob, 5, &value, &count, &range);
|
|
|
|
}
|
|
|
|
} else if (read_bool(r, p[6], &value, &count, &range)) {
|
|
|
|
val = CAT4_MIN_VAL +
|
|
|
|
read_coeff(r, vp9_cat4_prob, 4, &value, &count, &range);
|
|
|
|
} else {
|
|
|
|
val = CAT3_MIN_VAL +
|
|
|
|
read_coeff(r, vp9_cat3_prob, 3, &value, &count, &range);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
token_cache[scan[c]] = 4;
|
|
|
|
if (read_bool(r, p[4], &value, &count, &range)) {
|
|
|
|
val = CAT2_MIN_VAL +
|
|
|
|
read_coeff(r, vp9_cat2_prob, 2, &value, &count, &range);
|
|
|
|
} else {
|
|
|
|
val = CAT1_MIN_VAL +
|
|
|
|
read_coeff(r, vp9_cat1_prob, 1, &value, &count, &range);
|
|
|
|
}
|
|
|
|
}
|
2016-09-29 05:37:18 +02:00
|
|
|
#if CONFIG_VP9_HIGHBITDEPTH
|
|
|
|
// val may use 18-bits
|
|
|
|
v = (int)(((int64_t)val * dqv) >> dq_shift);
|
|
|
|
#else
|
2016-09-23 14:03:52 +02:00
|
|
|
v = (val * dqv) >> dq_shift;
|
2016-09-29 05:37:18 +02:00
|
|
|
#endif
|
2016-09-23 14:03:52 +02:00
|
|
|
} else {
|
|
|
|
if (read_bool(r, p[1], &value, &count, &range)) {
|
|
|
|
token_cache[scan[c]] = 3;
|
|
|
|
v = ((3 + read_bool(r, p[2], &value, &count, &range)) * dqv) >>
|
|
|
|
dq_shift;
|
|
|
|
} else {
|
|
|
|
token_cache[scan[c]] = 2;
|
|
|
|
v = (2 * dqv) >> dq_shift;
|
|
|
|
}
|
2012-07-14 00:21:29 +02:00
|
|
|
}
|
2016-09-23 14:03:52 +02:00
|
|
|
} else {
|
|
|
|
INCREMENT_COUNT(ONE_TOKEN);
|
|
|
|
token_cache[scan[c]] = 1;
|
|
|
|
v = dqv >> dq_shift;
|
2012-07-14 00:21:29 +02:00
|
|
|
}
|
2014-10-07 20:22:09 +02:00
|
|
|
#if CONFIG_COEFFICIENT_RANGE_CHECKING
|
2015-01-06 02:43:26 +01:00
|
|
|
#if CONFIG_VP9_HIGHBITDEPTH
|
2016-09-27 10:10:06 +02:00
|
|
|
dqcoeff[scan[c]] = highbd_check_range(
|
|
|
|
read_bool(r, 128, &value, &count, &range) ? -v : v, xd->bd);
|
2015-01-06 02:43:26 +01:00
|
|
|
#else
|
2016-09-23 14:03:52 +02:00
|
|
|
dqcoeff[scan[c]] =
|
|
|
|
check_range(read_bool(r, 128, &value, &count, &range) ? -v : v);
|
2015-01-06 02:43:26 +01:00
|
|
|
#endif // CONFIG_VP9_HIGHBITDEPTH
|
2014-10-07 20:22:09 +02:00
|
|
|
#else
|
2016-09-23 14:03:52 +02:00
|
|
|
if (read_bool(r, 128, &value, &count, &range)) {
|
|
|
|
dqcoeff[scan[c]] = -v;
|
|
|
|
} else {
|
|
|
|
dqcoeff[scan[c]] = v;
|
|
|
|
}
|
2015-01-06 02:43:26 +01:00
|
|
|
#endif // CONFIG_COEFFICIENT_RANGE_CHECKING
|
2014-09-06 01:58:24 +02:00
|
|
|
++c;
|
|
|
|
ctx = get_coef_context(nb, token_cache, c);
|
|
|
|
dqv = dq[1];
|
2012-07-14 00:21:29 +02:00
|
|
|
}
|
2012-06-25 21:26:09 +02:00
|
|
|
|
2016-09-23 14:03:52 +02:00
|
|
|
r->value = value;
|
|
|
|
r->range = range;
|
|
|
|
r->count = count;
|
2012-07-14 00:21:29 +02:00
|
|
|
return c;
|
2012-07-10 18:36:56 +02:00
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2016-03-30 00:03:12 +02:00
|
|
|
static void get_ctx_shift(MACROBLOCKD *xd, int *ctx_shift_a, int *ctx_shift_l,
|
|
|
|
int x, int y, unsigned int tx_size_in_blocks) {
|
|
|
|
if (xd->max_blocks_wide) {
|
|
|
|
if (tx_size_in_blocks + x > xd->max_blocks_wide)
|
|
|
|
*ctx_shift_a = (tx_size_in_blocks - (xd->max_blocks_wide - x)) * 8;
|
2015-07-09 14:30:46 +02:00
|
|
|
}
|
2016-03-30 00:03:12 +02:00
|
|
|
if (xd->max_blocks_high) {
|
|
|
|
if (tx_size_in_blocks + y > xd->max_blocks_high)
|
|
|
|
*ctx_shift_l = (tx_size_in_blocks - (xd->max_blocks_high - y)) * 8;
|
2015-07-09 14:30:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-22 22:18:36 +02:00
|
|
|
int vp9_decode_block_tokens(TileWorkerData *twd, int plane,
|
|
|
|
const scan_order *sc, int x, int y, TX_SIZE tx_size,
|
2015-04-28 16:52:06 +02:00
|
|
|
int seg_id) {
|
2016-09-22 22:18:36 +02:00
|
|
|
vpx_reader *r = &twd->bit_reader;
|
|
|
|
MACROBLOCKD *xd = &twd->xd;
|
2013-10-31 21:52:08 +01:00
|
|
|
struct macroblockd_plane *const pd = &xd->plane[plane];
|
2015-06-01 17:18:27 +02:00
|
|
|
const int16_t *const dequant = pd->seg_dequant[seg_id];
|
2016-03-30 00:03:12 +02:00
|
|
|
int eob;
|
|
|
|
ENTROPY_CONTEXT *a = pd->above_context + x;
|
|
|
|
ENTROPY_CONTEXT *l = pd->left_context + y;
|
|
|
|
int ctx;
|
|
|
|
int ctx_shift_a = 0;
|
|
|
|
int ctx_shift_l = 0;
|
|
|
|
|
|
|
|
switch (tx_size) {
|
|
|
|
case TX_4X4:
|
2016-07-27 05:20:13 +02:00
|
|
|
ctx = a[0] != 0;
|
2016-03-30 00:03:12 +02:00
|
|
|
ctx += l[0] != 0;
|
|
|
|
eob = decode_coefs(xd, get_plane_type(plane), pd->dqcoeff, tx_size,
|
|
|
|
dequant, ctx, sc->scan, sc->neighbors, r);
|
|
|
|
a[0] = l[0] = (eob > 0);
|
|
|
|
break;
|
|
|
|
case TX_8X8:
|
|
|
|
get_ctx_shift(xd, &ctx_shift_a, &ctx_shift_l, x, y, 1 << TX_8X8);
|
2016-07-27 05:20:13 +02:00
|
|
|
ctx = !!*(const uint16_t *)a;
|
2016-03-30 00:03:12 +02:00
|
|
|
ctx += !!*(const uint16_t *)l;
|
|
|
|
eob = decode_coefs(xd, get_plane_type(plane), pd->dqcoeff, tx_size,
|
|
|
|
dequant, ctx, sc->scan, sc->neighbors, r);
|
|
|
|
*(uint16_t *)a = ((eob > 0) * 0x0101) >> ctx_shift_a;
|
|
|
|
*(uint16_t *)l = ((eob > 0) * 0x0101) >> ctx_shift_l;
|
|
|
|
break;
|
|
|
|
case TX_16X16:
|
|
|
|
get_ctx_shift(xd, &ctx_shift_a, &ctx_shift_l, x, y, 1 << TX_16X16);
|
2016-07-27 05:20:13 +02:00
|
|
|
ctx = !!*(const uint32_t *)a;
|
2016-03-30 00:03:12 +02:00
|
|
|
ctx += !!*(const uint32_t *)l;
|
|
|
|
eob = decode_coefs(xd, get_plane_type(plane), pd->dqcoeff, tx_size,
|
|
|
|
dequant, ctx, sc->scan, sc->neighbors, r);
|
|
|
|
*(uint32_t *)a = ((eob > 0) * 0x01010101) >> ctx_shift_a;
|
|
|
|
*(uint32_t *)l = ((eob > 0) * 0x01010101) >> ctx_shift_l;
|
|
|
|
break;
|
|
|
|
case TX_32X32:
|
|
|
|
get_ctx_shift(xd, &ctx_shift_a, &ctx_shift_l, x, y, 1 << TX_32X32);
|
|
|
|
// NOTE: casting to uint64_t here is safe because the default memory
|
|
|
|
// alignment is at least 8 bytes and the TX_32X32 is aligned on 8 byte
|
|
|
|
// boundaries.
|
2016-07-27 05:20:13 +02:00
|
|
|
ctx = !!*(const uint64_t *)a;
|
2016-03-30 00:03:12 +02:00
|
|
|
ctx += !!*(const uint64_t *)l;
|
|
|
|
eob = decode_coefs(xd, get_plane_type(plane), pd->dqcoeff, tx_size,
|
|
|
|
dequant, ctx, sc->scan, sc->neighbors, r);
|
|
|
|
*(uint64_t *)a = ((eob > 0) * 0x0101010101010101ULL) >> ctx_shift_a;
|
|
|
|
*(uint64_t *)l = ((eob > 0) * 0x0101010101010101ULL) >> ctx_shift_l;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0 && "Invalid transform size.");
|
|
|
|
eob = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-10-31 21:52:08 +01:00
|
|
|
return eob;
|
2013-03-04 23:12:17 +01:00
|
|
|
}
|