2010-05-18 19:58:33 +04:00
|
|
|
/*
|
2016-09-02 00:32:49 +03:00
|
|
|
* Copyright (c) 2016, Alliance for Open Media. All rights reserved
|
2010-05-18 19:58:33 +04:00
|
|
|
*
|
2016-09-02 00:32:49 +03:00
|
|
|
* 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.
|
2010-05-18 19:58:33 +04:00
|
|
|
*/
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
#ifndef AOM_DSP_BITWRITER_H_
|
|
|
|
#define AOM_DSP_BITWRITER_H_
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2016-09-21 02:39:01 +03:00
|
|
|
#include <assert.h>
|
|
|
|
#include "./aom_config.h"
|
2016-10-24 16:50:52 +03:00
|
|
|
#if CONFIG_EC_ADAPT && !CONFIG_EC_MULTISYMBOL
|
|
|
|
#error "CONFIG_EC_ADAPT is enabled without enabling CONFIG_EC_MULTISYMBOL"
|
|
|
|
#endif
|
|
|
|
|
2016-09-21 02:39:01 +03:00
|
|
|
#if CONFIG_ANS
|
|
|
|
#include "aom_dsp/buf_ans.h"
|
|
|
|
#elif CONFIG_DAALA_EC
|
|
|
|
#include "aom_dsp/daalaboolwriter.h"
|
|
|
|
#else
|
2016-06-07 01:12:06 +03:00
|
|
|
#include "aom_dsp/dkboolwriter.h"
|
2016-09-21 02:39:01 +03:00
|
|
|
#endif
|
2016-08-23 02:08:15 +03:00
|
|
|
#include "aom_dsp/prob.h"
|
2013-12-17 00:53:09 +04:00
|
|
|
|
2016-11-03 04:30:25 +03:00
|
|
|
#if CONFIG_RD_DEBUG
|
2016-11-07 02:31:49 +03:00
|
|
|
#include "av1/common/blockd.h"
|
2016-11-03 04:30:25 +03:00
|
|
|
#include "av1/encoder/cost.h"
|
|
|
|
#endif
|
|
|
|
|
2014-01-19 00:16:11 +04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2016-09-21 02:39:01 +03:00
|
|
|
#if CONFIG_ANS
|
|
|
|
typedef struct BufAnsCoder aom_writer;
|
2016-03-06 20:42:47 +03:00
|
|
|
#elif CONFIG_DAALA_EC
|
|
|
|
typedef struct daala_writer aom_writer;
|
2016-09-21 02:39:01 +03:00
|
|
|
#else
|
2016-06-07 01:12:06 +03:00
|
|
|
typedef struct aom_dk_writer aom_writer;
|
2016-09-21 02:39:01 +03:00
|
|
|
#endif
|
2011-07-19 17:17:25 +04:00
|
|
|
|
2016-11-07 02:31:49 +03:00
|
|
|
typedef struct TOKEN_STATS {
|
|
|
|
int cost;
|
|
|
|
#if CONFIG_VAR_TX
|
|
|
|
#if CONFIG_RD_DEBUG
|
|
|
|
int txb_coeff_cost_map[TXB_COEFF_COST_MAP_SIZE][TXB_COEFF_COST_MAP_SIZE];
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
} TOKEN_STATS;
|
|
|
|
|
|
|
|
static INLINE void init_token_stats(TOKEN_STATS *token_stats) {
|
|
|
|
#if CONFIG_VAR_TX
|
|
|
|
#if CONFIG_RD_DEBUG
|
|
|
|
int r, c;
|
|
|
|
for (r = 0; r < TXB_COEFF_COST_MAP_SIZE; ++r) {
|
|
|
|
for (c = 0; c < TXB_COEFF_COST_MAP_SIZE; ++c) {
|
|
|
|
token_stats->txb_coeff_cost_map[r][c] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
token_stats->cost = 0;
|
|
|
|
}
|
2016-11-03 04:30:25 +03:00
|
|
|
|
2016-06-07 01:12:06 +03:00
|
|
|
static INLINE void aom_start_encode(aom_writer *bc, uint8_t *buffer) {
|
2016-09-21 02:39:01 +03:00
|
|
|
#if CONFIG_ANS
|
|
|
|
(void)bc;
|
|
|
|
(void)buffer;
|
|
|
|
assert(0 && "buf_ans requires a more complicated startup procedure");
|
2016-03-06 20:42:47 +03:00
|
|
|
#elif CONFIG_DAALA_EC
|
|
|
|
aom_daala_start_encode(bc, buffer);
|
2016-09-21 02:39:01 +03:00
|
|
|
#else
|
2016-06-07 01:12:06 +03:00
|
|
|
aom_dk_start_encode(bc, buffer);
|
2016-09-21 02:39:01 +03:00
|
|
|
#endif
|
2016-06-07 01:12:06 +03:00
|
|
|
}
|
2011-07-19 17:17:25 +04:00
|
|
|
|
2016-09-21 02:39:01 +03:00
|
|
|
static INLINE void aom_stop_encode(aom_writer *bc) {
|
|
|
|
#if CONFIG_ANS
|
|
|
|
(void)bc;
|
|
|
|
assert(0 && "buf_ans requires a more complicated shutdown procedure");
|
2016-03-06 20:42:47 +03:00
|
|
|
#elif CONFIG_DAALA_EC
|
|
|
|
aom_daala_stop_encode(bc);
|
2016-09-21 02:39:01 +03:00
|
|
|
#else
|
|
|
|
aom_dk_stop_encode(bc);
|
|
|
|
#endif
|
|
|
|
}
|
2012-07-14 02:21:29 +04:00
|
|
|
|
2016-06-07 01:12:06 +03:00
|
|
|
static INLINE void aom_write(aom_writer *br, int bit, int probability) {
|
2016-09-21 02:39:01 +03:00
|
|
|
#if CONFIG_ANS
|
2017-01-26 03:41:05 +03:00
|
|
|
buf_rabs_write(br, bit, probability);
|
2016-03-06 20:42:47 +03:00
|
|
|
#elif CONFIG_DAALA_EC
|
2016-11-09 21:40:26 +03:00
|
|
|
// Note this uses raw bits and is not the same as aom_daala_write(r, 128);
|
2016-03-06 20:42:47 +03:00
|
|
|
aom_daala_write(br, bit, probability);
|
2016-09-21 02:39:01 +03:00
|
|
|
#else
|
2016-06-07 01:12:06 +03:00
|
|
|
aom_dk_write(br, bit, probability);
|
2016-09-21 02:39:01 +03:00
|
|
|
#endif
|
2011-07-19 17:17:25 +04:00
|
|
|
}
|
|
|
|
|
2016-11-03 04:30:25 +03:00
|
|
|
static INLINE void aom_write_record(aom_writer *br, int bit, int probability,
|
|
|
|
TOKEN_STATS *token_stats) {
|
|
|
|
aom_write(br, bit, probability);
|
|
|
|
#if CONFIG_RD_DEBUG
|
|
|
|
token_stats->cost += av1_cost_bit(probability, bit);
|
|
|
|
#else
|
|
|
|
(void)token_stats;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
static INLINE void aom_write_bit(aom_writer *w, int bit) {
|
2016-12-14 20:56:20 +03:00
|
|
|
#if CONFIG_ANS
|
2017-01-26 03:41:05 +03:00
|
|
|
buf_rabs_write_bit(w, bit);
|
2016-12-14 20:56:20 +03:00
|
|
|
#elif CONFIG_DAALA_EC
|
2016-11-09 21:40:26 +03:00
|
|
|
aom_daala_write_bit(w, bit);
|
|
|
|
#else
|
2016-09-21 02:39:01 +03:00
|
|
|
aom_write(w, bit, 128); // aom_prob_half
|
2016-11-09 21:40:26 +03:00
|
|
|
#endif
|
2013-05-08 05:19:50 +04:00
|
|
|
}
|
|
|
|
|
2016-11-03 04:30:25 +03:00
|
|
|
static INLINE void aom_write_bit_record(aom_writer *w, int bit,
|
|
|
|
TOKEN_STATS *token_stats) {
|
2016-12-14 20:47:43 +03:00
|
|
|
aom_write_bit(w, bit);
|
|
|
|
#if CONFIG_RD_DEBUG
|
|
|
|
token_stats->cost += av1_cost_bit(128, bit); // aom_prob_half
|
|
|
|
#else
|
|
|
|
(void)token_stats;
|
|
|
|
#endif
|
2016-11-03 04:30:25 +03:00
|
|
|
}
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
static INLINE void aom_write_literal(aom_writer *w, int data, int bits) {
|
2016-09-21 02:39:01 +03:00
|
|
|
int bit;
|
|
|
|
|
|
|
|
for (bit = bits - 1; bit >= 0; bit--) aom_write_bit(w, 1 & (data >> bit));
|
2013-05-08 05:19:50 +04:00
|
|
|
}
|
|
|
|
|
2016-06-19 19:02:33 +03:00
|
|
|
static INLINE void aom_write_tree_bits(aom_writer *w, const aom_tree_index *tr,
|
|
|
|
const aom_prob *probs, int bits, int len,
|
|
|
|
aom_tree_index i) {
|
|
|
|
do {
|
|
|
|
const int bit = (bits >> --len) & 1;
|
|
|
|
aom_write(w, bit, probs[i >> 1]);
|
|
|
|
i = tr[i + bit];
|
|
|
|
} while (len);
|
|
|
|
}
|
|
|
|
|
2016-11-03 04:30:25 +03:00
|
|
|
static INLINE void aom_write_tree_bits_record(aom_writer *w,
|
|
|
|
const aom_tree_index *tr,
|
|
|
|
const aom_prob *probs, int bits,
|
|
|
|
int len, aom_tree_index i,
|
|
|
|
TOKEN_STATS *token_stats) {
|
|
|
|
do {
|
|
|
|
const int bit = (bits >> --len) & 1;
|
|
|
|
aom_write_record(w, bit, probs[i >> 1], token_stats);
|
|
|
|
i = tr[i + bit];
|
|
|
|
} while (len);
|
|
|
|
}
|
|
|
|
|
2016-06-19 19:02:33 +03:00
|
|
|
static INLINE void aom_write_tree(aom_writer *w, const aom_tree_index *tree,
|
|
|
|
const aom_prob *probs, int bits, int len,
|
|
|
|
aom_tree_index i) {
|
2016-03-06 21:41:53 +03:00
|
|
|
#if CONFIG_DAALA_EC
|
|
|
|
daala_write_tree_bits(w, tree, probs, bits, len, i);
|
|
|
|
#else
|
2016-06-19 19:02:33 +03:00
|
|
|
aom_write_tree_bits(w, tree, probs, bits, len, i);
|
2016-03-06 21:41:53 +03:00
|
|
|
#endif
|
2016-06-19 19:02:33 +03:00
|
|
|
}
|
|
|
|
|
2016-11-03 04:30:25 +03:00
|
|
|
static INLINE void aom_write_tree_record(aom_writer *w,
|
|
|
|
const aom_tree_index *tree,
|
|
|
|
const aom_prob *probs, int bits,
|
|
|
|
int len, aom_tree_index i,
|
|
|
|
TOKEN_STATS *token_stats) {
|
|
|
|
#if CONFIG_DAALA_EC
|
|
|
|
(void)token_stats;
|
|
|
|
daala_write_tree_bits(w, tree, probs, bits, len, i);
|
|
|
|
#else
|
|
|
|
aom_write_tree_bits_record(w, tree, probs, bits, len, i, token_stats);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-10-21 00:21:06 +03:00
|
|
|
#if CONFIG_EC_MULTISYMBOL
|
2016-12-27 19:41:26 +03:00
|
|
|
static INLINE void aom_write_cdf(aom_writer *w, int symb,
|
|
|
|
const aom_cdf_prob *cdf, int nsymbs) {
|
2016-11-09 01:12:14 +03:00
|
|
|
#if CONFIG_ANS
|
2016-10-13 01:59:58 +03:00
|
|
|
struct rans_sym s;
|
|
|
|
(void)nsymbs;
|
|
|
|
assert(cdf);
|
2016-08-22 22:44:16 +03:00
|
|
|
s.cum_prob = symb > 0 ? cdf[symb - 1] : 0;
|
|
|
|
s.prob = cdf[symb] - s.cum_prob;
|
2016-10-13 01:59:58 +03:00
|
|
|
buf_rans_write(w, &s);
|
2016-07-25 17:23:33 +03:00
|
|
|
#elif CONFIG_DAALA_EC
|
|
|
|
daala_write_symbol(w, symb, cdf, nsymbs);
|
2016-06-20 20:44:22 +03:00
|
|
|
#else
|
2016-10-21 00:21:06 +03:00
|
|
|
#error \
|
|
|
|
"CONFIG_EC_MULTISYMBOL is selected without a valid backing entropy " \
|
|
|
|
"coder. Enable daala_ec or ans for a valid configuration."
|
2016-06-20 20:44:22 +03:00
|
|
|
#endif
|
2017-01-07 03:15:03 +03:00
|
|
|
}
|
2016-09-23 20:04:17 +03:00
|
|
|
|
2017-01-07 03:15:03 +03:00
|
|
|
static INLINE void aom_write_symbol(aom_writer *w, int symb, aom_cdf_prob *cdf,
|
|
|
|
int nsymbs) {
|
|
|
|
aom_write_cdf(w, symb, cdf, nsymbs);
|
2016-10-10 21:08:10 +03:00
|
|
|
#if CONFIG_EC_ADAPT
|
2016-09-23 20:04:17 +03:00
|
|
|
update_cdf(cdf, symb, nsymbs);
|
|
|
|
#endif
|
2016-06-20 20:44:22 +03:00
|
|
|
}
|
2016-12-21 19:30:46 +03:00
|
|
|
|
|
|
|
#if CONFIG_PVQ
|
2017-01-07 03:15:03 +03:00
|
|
|
static INLINE void aom_write_cdf_unscaled(aom_writer *w, int symb,
|
|
|
|
const aom_cdf_prob *cdf, int nsymbs) {
|
2016-12-21 19:30:46 +03:00
|
|
|
#if CONFIG_DAALA_EC
|
|
|
|
od_ec_encode_cdf_unscaled(&w->ec, symb, cdf, nsymbs);
|
|
|
|
#else
|
|
|
|
#error "CONFIG_PVQ currently requires CONFIG_DAALA_EC."
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
2016-10-21 00:21:06 +03:00
|
|
|
#endif // CONFIG_EC_MULTISYMBOL
|
2016-06-20 20:44:22 +03:00
|
|
|
|
2014-01-19 00:16:11 +04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
} // extern "C"
|
|
|
|
#endif
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
#endif // AOM_DSP_BITWRITER_H_
|