From 80b82269d09a2b2fd793257cb5432ed10bd2eb21 Mon Sep 17 00:00:00 2001 From: Angie Chiang Date: Fri, 24 Feb 2017 11:39:47 -0800 Subject: [PATCH] Add lv_map transform coefficient coding function Change-Id: I70c3659940b5090f030c795df5148ac508e19d2d --- av1/av1_cx.mk | 2 + av1/common/blockd.h | 9 +++ av1/encoder/encodetxb.c | 134 ++++++++++++++++++++++++++++++++++++++++ av1/encoder/encodetxb.h | 36 +++++++++++ 4 files changed, 181 insertions(+) create mode 100644 av1/encoder/encodetxb.c create mode 100644 av1/encoder/encodetxb.h diff --git a/av1/av1_cx.mk b/av1/av1_cx.mk index d9eead5df..09eb401c3 100644 --- a/av1/av1_cx.mk +++ b/av1/av1_cx.mk @@ -54,6 +54,8 @@ AV1_CX_SRCS-yes += encoder/block.h AV1_CX_SRCS-yes += encoder/bitstream.h AV1_CX_SRCS-yes += encoder/encodemb.h AV1_CX_SRCS-yes += encoder/encodemv.h +AV1_CX_SRCS-$(CONFIG_LV_MAP) += encoder/encodetxb.c +AV1_CX_SRCS-$(CONFIG_LV_MAP) += encoder/encodetxb.h AV1_CX_SRCS-yes += encoder/extend.h AV1_CX_SRCS-yes += encoder/firstpass.h AV1_CX_SRCS-yes += encoder/lookahead.c diff --git a/av1/common/blockd.h b/av1/common/blockd.h index e7725ea7c..9616e393a 100644 --- a/av1/common/blockd.h +++ b/av1/common/blockd.h @@ -933,6 +933,15 @@ static INLINE TX_SIZE get_uv_tx_size(const MB_MODE_INFO *mbmi, return uv_txsize; } +static INLINE TX_SIZE get_tx_size(int plane, const MACROBLOCKD *xd, + int block_idx) { + const MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; + const MACROBLOCKD_PLANE *pd = &xd->plane[plane]; + const TX_SIZE tx_size = plane ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size; + (void)block_idx; + return tx_size; +} + static INLINE BLOCK_SIZE get_plane_block_size(BLOCK_SIZE bsize, const struct macroblockd_plane *pd) { return ss_size_lookup[bsize][pd->subsampling_x][pd->subsampling_y]; diff --git a/av1/encoder/encodetxb.c b/av1/encoder/encodetxb.c new file mode 100644 index 000000000..3d1de58a8 --- /dev/null +++ b/av1/encoder/encodetxb.c @@ -0,0 +1,134 @@ +/* + * Copyright (c) 2017, Alliance for Open Media. All rights reserved + * + * This source code is subject to the terms of the BSD 2 Clause License and + * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License + * was not distributed with this source code in the LICENSE file, you can + * obtain it at www.aomedia.org/license/software. If the Alliance for Open + * Media Patent License 1.0 was not distributed with this source code in the + * PATENTS file, you can obtain it at www.aomedia.org/license/patent. + */ + +#include "av1/common/scan.h" +#include "av1/common/txb_common.h" +#include "av1/encoder/encodetxb.h" + +static void write_golomb(aom_writer *w, int level) { + int x = level + 1; + int i = x; + int length = 0; + + while (i) { + i >>= 1; + ++length; + } + assert(length > 0); + + for (i = 0; i < length - 1; ++i) aom_write_bit(w, 0); + + for (i = length - 1; i >= 0; --i) aom_write_bit(w, (x >> i) & 0x01); +} + +void av1_write_coeffs_txb(const AV1_COMMON *const cm, MACROBLOCKD *xd, + aom_writer *w, int block, int plane, + const tran_low_t *tcoeff, uint16_t eob, + TXB_CTX *txb_ctx) { + aom_prob *nz_map; + aom_prob *eob_flag; + MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; + const PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV; + const TX_SIZE tx_size = get_tx_size(plane, xd, block); + const TX_TYPE tx_type = get_tx_type(plane_type, xd, block, tx_size); + const SCAN_ORDER *const scan_order = + get_scan(cm, tx_size, tx_type, is_inter_block(mbmi)); + const int16_t *scan = scan_order->scan; + int c; + int is_nz; + const int bwl = b_width_log2_lookup[txsize_to_bsize[tx_size]] + 2; + const int seg_eob = 16 << (tx_size << 1); + uint8_t txb_mask[32 * 32] = { 0 }; + uint16_t update_eob = 0; + + aom_write(w, eob == 0, cm->fc->txb_skip[tx_size][txb_ctx->txb_skip_ctx]); + + if (eob == 0) return; + + nz_map = cm->fc->nz_map[tx_size][plane_type]; + eob_flag = cm->fc->eob_flag[tx_size][plane_type]; + + for (c = 0; c < eob; ++c) { + int coeff_ctx = get_nz_map_ctx(tcoeff, txb_mask, scan[c], bwl); + int eob_ctx = get_eob_ctx(tcoeff, scan[c], bwl); + + tran_low_t v = tcoeff[scan[c]]; + is_nz = (v != 0); + + if (c == seg_eob - 1) break; + + aom_write(w, is_nz, nz_map[coeff_ctx]); + + if (is_nz) { + aom_write(w, c == (eob - 1), eob_flag[eob_ctx]); + } + txb_mask[scan[c]] = 1; + } + + int i; + for (i = 0; i < NUM_BASE_LEVELS; ++i) { + aom_prob *coeff_base = cm->fc->coeff_base[tx_size][plane_type][i]; + + update_eob = 0; + for (c = eob - 1; c >= 0; --c) { + tran_low_t v = tcoeff[scan[c]]; + tran_low_t level = abs(v); + int sign = (v < 0) ? 1 : 0; + int ctx; + + if (level <= i) continue; + + ctx = get_base_ctx(tcoeff, scan[c], bwl, i + 1); + + if (level == i + 1) { + aom_write(w, 1, coeff_base[ctx]); + if (c == 0) { + aom_write(w, sign, cm->fc->dc_sign[plane_type][txb_ctx->dc_sign_ctx]); + } else { + aom_write_bit(w, sign); + } + continue; + } + aom_write(w, 0, coeff_base[ctx]); + update_eob = AOMMAX(update_eob, c); + } + } + + for (c = update_eob; c >= 0; --c) { + tran_low_t v = tcoeff[scan[c]]; + tran_low_t level = abs(v); + int sign = (v < 0) ? 1 : 0; + int idx; + int ctx; + + if (level <= NUM_BASE_LEVELS) continue; + + if (c == 0) { + aom_write(w, sign, cm->fc->dc_sign[plane_type][txb_ctx->dc_sign_ctx]); + } else { + aom_write_bit(w, sign); + } + + // level is above 1. + ctx = get_level_ctx(tcoeff, scan[c], bwl); + for (idx = 0; idx < COEFF_BASE_RANGE; ++idx) { + if (level == (idx + 1 + NUM_BASE_LEVELS)) { + aom_write(w, 1, cm->fc->coeff_lps[tx_size][plane_type][ctx]); + break; + } + aom_write(w, 0, cm->fc->coeff_lps[tx_size][plane_type][ctx]); + } + if (idx < COEFF_BASE_RANGE) continue; + + // use 0-th order Golomb code to handle the residual level. + write_golomb(w, level - COEFF_BASE_RANGE - 1 - NUM_BASE_LEVELS); + } +} diff --git a/av1/encoder/encodetxb.h b/av1/encoder/encodetxb.h new file mode 100644 index 000000000..ac6754480 --- /dev/null +++ b/av1/encoder/encodetxb.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2017, Alliance for Open Media. All rights reserved + * + * This source code is subject to the terms of the BSD 2 Clause License and + * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License + * was not distributed with this source code in the LICENSE file, you can + * obtain it at www.aomedia.org/license/software. If the Alliance for Open + * Media Patent License 1.0 was not distributed with this source code in the + * PATENTS file, you can obtain it at www.aomedia.org/license/patent. + */ + +#ifndef ENCODETXB_H_ +#define ENCODETXB_H_ + +#include "./aom_config.h" +#include "av1/common/blockd.h" +#include "av1/common/onyxc_int.h" +#include "aom_dsp/bitwriter.h" +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct txb_ctx { + int txb_skip_ctx; + int dc_sign_ctx; +} TXB_CTX; + +void av1_write_coeffs_txb(const AV1_COMMON *const cm, MACROBLOCKD *xd, + aom_writer *w, int block, int plane, + const tran_low_t *tcoeff, uint16_t eob, + TXB_CTX *txb_ctx); +#ifdef __cplusplus +} +#endif + +#endif // COEFFS_CODING_H_ -- GitLab