blob: 41296abf904e87499a32484d0a34f9bcf987d25a [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
Yaowu Xuf883b422016-08-30 14:01:10 -070012#ifndef AOM_DSP_BITWRITER_H_
13#define AOM_DSP_BITWRITER_H_
Yaowu Xuc27fc142016-08-22 16:08:15 -070014
Alex Converse080a2cc2016-09-20 16:39:01 -070015#include <assert.h>
16#include "./aom_config.h"
Nathan E. Eggebaaaa162016-10-24 09:50:52 -040017#if CONFIG_EC_ADAPT && !CONFIG_EC_MULTISYMBOL
18#error "CONFIG_EC_ADAPT is enabled without enabling CONFIG_EC_MULTISYMBOL"
19#endif
20
Alex Converse080a2cc2016-09-20 16:39:01 -070021#if CONFIG_ANS
22#include "aom_dsp/buf_ans.h"
23#elif CONFIG_DAALA_EC
24#include "aom_dsp/daalaboolwriter.h"
25#else
Alex Converseeb00cb22016-06-06 15:12:06 -070026#include "aom_dsp/dkboolwriter.h"
Alex Converse080a2cc2016-09-20 16:39:01 -070027#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070028#include "aom_dsp/prob.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070029
Angie Chiangd4022822016-11-02 18:30:25 -070030#if CONFIG_RD_DEBUG
Angie Chiangd02001d2016-11-06 15:31:49 -080031#include "av1/common/blockd.h"
Angie Chiangd4022822016-11-02 18:30:25 -070032#include "av1/encoder/cost.h"
33#endif
34
Yaowu Xuc27fc142016-08-22 16:08:15 -070035#ifdef __cplusplus
36extern "C" {
37#endif
38
Alex Converse080a2cc2016-09-20 16:39:01 -070039#if CONFIG_ANS
40typedef struct BufAnsCoder aom_writer;
Nathan E. Egge8043cc42016-03-06 12:42:47 -050041#elif CONFIG_DAALA_EC
42typedef struct daala_writer aom_writer;
Alex Converse080a2cc2016-09-20 16:39:01 -070043#else
Alex Converseeb00cb22016-06-06 15:12:06 -070044typedef struct aom_dk_writer aom_writer;
Alex Converse080a2cc2016-09-20 16:39:01 -070045#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070046
Angie Chiangd02001d2016-11-06 15:31:49 -080047typedef struct TOKEN_STATS {
48 int cost;
49#if CONFIG_VAR_TX
50#if CONFIG_RD_DEBUG
51 int txb_coeff_cost_map[TXB_COEFF_COST_MAP_SIZE][TXB_COEFF_COST_MAP_SIZE];
52#endif
53#endif
54} TOKEN_STATS;
55
56static INLINE void init_token_stats(TOKEN_STATS *token_stats) {
57#if CONFIG_VAR_TX
58#if CONFIG_RD_DEBUG
59 int r, c;
60 for (r = 0; r < TXB_COEFF_COST_MAP_SIZE; ++r) {
61 for (c = 0; c < TXB_COEFF_COST_MAP_SIZE; ++c) {
62 token_stats->txb_coeff_cost_map[r][c] = 0;
63 }
64 }
65#endif
66#endif
67 token_stats->cost = 0;
68}
Angie Chiangd4022822016-11-02 18:30:25 -070069
Alex Converseeb00cb22016-06-06 15:12:06 -070070static INLINE void aom_start_encode(aom_writer *bc, uint8_t *buffer) {
Alex Converse080a2cc2016-09-20 16:39:01 -070071#if CONFIG_ANS
72 (void)bc;
73 (void)buffer;
74 assert(0 && "buf_ans requires a more complicated startup procedure");
Nathan E. Egge8043cc42016-03-06 12:42:47 -050075#elif CONFIG_DAALA_EC
76 aom_daala_start_encode(bc, buffer);
Alex Converse080a2cc2016-09-20 16:39:01 -070077#else
Alex Converseeb00cb22016-06-06 15:12:06 -070078 aom_dk_start_encode(bc, buffer);
Alex Converse080a2cc2016-09-20 16:39:01 -070079#endif
Alex Converseeb00cb22016-06-06 15:12:06 -070080}
81
Alex Converse080a2cc2016-09-20 16:39:01 -070082static INLINE void aom_stop_encode(aom_writer *bc) {
83#if CONFIG_ANS
84 (void)bc;
85 assert(0 && "buf_ans requires a more complicated shutdown procedure");
Nathan E. Egge8043cc42016-03-06 12:42:47 -050086#elif CONFIG_DAALA_EC
87 aom_daala_stop_encode(bc);
Alex Converse080a2cc2016-09-20 16:39:01 -070088#else
89 aom_dk_stop_encode(bc);
90#endif
91}
Yaowu Xuc27fc142016-08-22 16:08:15 -070092
Yaowu Xuf883b422016-08-30 14:01:10 -070093static INLINE void aom_write(aom_writer *br, int bit, int probability) {
Alex Converse080a2cc2016-09-20 16:39:01 -070094#if CONFIG_ANS
Alex Conversec54692b2017-01-25 16:41:05 -080095 buf_rabs_write(br, bit, probability);
Nathan E. Egge8043cc42016-03-06 12:42:47 -050096#elif CONFIG_DAALA_EC
97 aom_daala_write(br, bit, probability);
Alex Converse080a2cc2016-09-20 16:39:01 -070098#else
Alex Converseeb00cb22016-06-06 15:12:06 -070099 aom_dk_write(br, bit, probability);
Alex Converse080a2cc2016-09-20 16:39:01 -0700100#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700101}
102
Angie Chiangd4022822016-11-02 18:30:25 -0700103static INLINE void aom_write_record(aom_writer *br, int bit, int probability,
104 TOKEN_STATS *token_stats) {
105 aom_write(br, bit, probability);
106#if CONFIG_RD_DEBUG
107 token_stats->cost += av1_cost_bit(probability, bit);
108#else
109 (void)token_stats;
110#endif
111}
112
Yaowu Xuf883b422016-08-30 14:01:10 -0700113static INLINE void aom_write_bit(aom_writer *w, int bit) {
Nathan E. Egge67b99212016-12-14 12:56:20 -0500114#if CONFIG_ANS
Alex Conversec54692b2017-01-25 16:41:05 -0800115 buf_rabs_write_bit(w, bit);
Nathan E. Egge24f1a902017-02-14 13:29:44 -0500116#elif CONFIG_DAALA_EC && CONFIG_RAWBITS
117 // Note this uses raw bits and is not the same as aom_daala_write(r, 128);
Nathan E. Egge08c99eb2016-11-09 13:40:26 -0500118 aom_daala_write_bit(w, bit);
119#else
Alex Converse080a2cc2016-09-20 16:39:01 -0700120 aom_write(w, bit, 128); // aom_prob_half
Nathan E. Egge08c99eb2016-11-09 13:40:26 -0500121#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700122}
123
Angie Chiangd4022822016-11-02 18:30:25 -0700124static INLINE void aom_write_bit_record(aom_writer *w, int bit,
125 TOKEN_STATS *token_stats) {
Nathan E. Egge90b305a2016-12-14 12:47:43 -0500126 aom_write_bit(w, bit);
127#if CONFIG_RD_DEBUG
128 token_stats->cost += av1_cost_bit(128, bit); // aom_prob_half
129#else
130 (void)token_stats;
131#endif
Angie Chiangd4022822016-11-02 18:30:25 -0700132}
133
Yaowu Xuf883b422016-08-30 14:01:10 -0700134static INLINE void aom_write_literal(aom_writer *w, int data, int bits) {
Alex Converse080a2cc2016-09-20 16:39:01 -0700135 int bit;
136
137 for (bit = bits - 1; bit >= 0; bit--) aom_write_bit(w, 1 & (data >> bit));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700138}
139
Nathan E. Eggec4e9e472017-02-12 19:34:33 -0500140static INLINE void aom_write_tree_as_bits(aom_writer *w,
141 const aom_tree_index *tr,
142 const aom_prob *probs, int bits,
143 int len, aom_tree_index i) {
Nathan E. Eggeeeedc632016-06-19 12:02:33 -0400144 do {
145 const int bit = (bits >> --len) & 1;
146 aom_write(w, bit, probs[i >> 1]);
147 i = tr[i + bit];
148 } while (len);
149}
150
Nathan E. Eggec4e9e472017-02-12 19:34:33 -0500151static INLINE void aom_write_tree_as_bits_record(
152 aom_writer *w, const aom_tree_index *tr, const aom_prob *probs, int bits,
153 int len, aom_tree_index i, TOKEN_STATS *token_stats) {
Angie Chiangd4022822016-11-02 18:30:25 -0700154 do {
155 const int bit = (bits >> --len) & 1;
156 aom_write_record(w, bit, probs[i >> 1], token_stats);
157 i = tr[i + bit];
158 } while (len);
159}
160
Alex Converse58c520a2016-10-20 14:21:06 -0700161#if CONFIG_EC_MULTISYMBOL
Nathan E. Egge58ef5512016-12-27 11:41:26 -0500162static INLINE void aom_write_cdf(aom_writer *w, int symb,
163 const aom_cdf_prob *cdf, int nsymbs) {
Alex Converse1e4e29f2016-11-08 14:12:14 -0800164#if CONFIG_ANS
Alex Conversea1ac9722016-10-12 15:59:58 -0700165 (void)nsymbs;
166 assert(cdf);
Alex Converse8c687d22017-02-07 15:55:24 -0800167 const aom_cdf_prob cum_prob = symb > 0 ? cdf[symb - 1] : 0;
168 const aom_cdf_prob prob = cdf[symb] - cum_prob;
169 buf_rans_write(w, cum_prob, prob);
Nathan E. Egge56eeaa52016-07-25 10:23:33 -0400170#elif CONFIG_DAALA_EC
171 daala_write_symbol(w, symb, cdf, nsymbs);
Nathan E. Egge44460142016-06-20 13:44:22 -0400172#else
Alex Converse58c520a2016-10-20 14:21:06 -0700173#error \
174 "CONFIG_EC_MULTISYMBOL is selected without a valid backing entropy " \
175 "coder. Enable daala_ec or ans for a valid configuration."
Nathan E. Egge44460142016-06-20 13:44:22 -0400176#endif
Nathan E. Egge87d44dc2017-01-06 19:15:03 -0500177}
Thomas9ac55082016-09-23 18:04:17 +0100178
Nathan E. Egge87d44dc2017-01-06 19:15:03 -0500179static INLINE void aom_write_symbol(aom_writer *w, int symb, aom_cdf_prob *cdf,
180 int nsymbs) {
181 aom_write_cdf(w, symb, cdf, nsymbs);
Alex Converseaca9feb2016-10-10 11:08:10 -0700182#if CONFIG_EC_ADAPT
Thomas9ac55082016-09-23 18:04:17 +0100183 update_cdf(cdf, symb, nsymbs);
184#endif
Nathan E. Egge44460142016-06-20 13:44:22 -0400185}
Nathan E. Eggec98d2862016-12-21 11:30:46 -0500186
187#if CONFIG_PVQ
Nathan E. Egge87d44dc2017-01-06 19:15:03 -0500188static INLINE void aom_write_cdf_unscaled(aom_writer *w, int symb,
189 const aom_cdf_prob *cdf, int nsymbs) {
Nathan E. Eggec98d2862016-12-21 11:30:46 -0500190#if CONFIG_DAALA_EC
191 od_ec_encode_cdf_unscaled(&w->ec, symb, cdf, nsymbs);
192#else
193#error "CONFIG_PVQ currently requires CONFIG_DAALA_EC."
194#endif
195}
196#endif
Nathan E. Egge0f11c782017-02-12 19:04:04 -0500197
198static INLINE void aom_write_tree_as_cdf(aom_writer *w,
199 const aom_tree_index *tree,
200 const aom_prob *probs, int bits,
201 int len, aom_tree_index i) {
202 aom_tree_index root;
203 root = i;
204 do {
205 aom_cdf_prob cdf[16];
206 aom_tree_index index[16];
207 int path[16];
208 int dist[16];
209 int nsymbs;
210 int symb;
211 int j;
212 /* Compute the CDF of the binary tree using the given probabilities. */
213 nsymbs = tree_to_cdf(tree, probs, root, cdf, index, path, dist);
214 /* Find the symbol to code. */
215 symb = -1;
216 for (j = 0; j < nsymbs; j++) {
217 /* If this symbol codes a leaf node, */
218 if (index[j] <= 0) {
219 if (len == dist[j] && path[j] == bits) {
220 symb = j;
221 break;
222 }
223 } else {
224 if (len > dist[j] && path[j] == bits >> (len - dist[j])) {
225 symb = j;
226 break;
227 }
228 }
229 }
230 OD_ASSERT(symb != -1);
231 aom_write_cdf(w, symb, cdf, nsymbs);
232 bits &= (1 << (len - dist[symb])) - 1;
233 len -= dist[symb];
234 } while (len);
235}
236
Alex Converse58c520a2016-10-20 14:21:06 -0700237#endif // CONFIG_EC_MULTISYMBOL
Nathan E. Egge44460142016-06-20 13:44:22 -0400238
Nathan E. Egge0f11c782017-02-12 19:04:04 -0500239static INLINE void aom_write_tree(aom_writer *w, const aom_tree_index *tree,
240 const aom_prob *probs, int bits, int len,
241 aom_tree_index i) {
242#if CONFIG_EC_MULTISYMBOL
243 aom_write_tree_as_cdf(w, tree, probs, bits, len, i);
244#else
Nathan E. Eggec4e9e472017-02-12 19:34:33 -0500245 aom_write_tree_as_bits(w, tree, probs, bits, len, i);
Nathan E. Egge0f11c782017-02-12 19:04:04 -0500246#endif
247}
248
249static INLINE void aom_write_tree_record(aom_writer *w,
250 const aom_tree_index *tree,
251 const aom_prob *probs, int bits,
252 int len, aom_tree_index i,
253 TOKEN_STATS *token_stats) {
254#if CONFIG_EC_MULTISYMBOL
255 (void)token_stats;
256 aom_write_tree_as_cdf(w, tree, probs, bits, len, i);
257#else
Nathan E. Eggec4e9e472017-02-12 19:34:33 -0500258 aom_write_tree_as_bits_record(w, tree, probs, bits, len, i, token_stats);
Nathan E. Egge0f11c782017-02-12 19:04:04 -0500259#endif
260}
261
Yaowu Xuc27fc142016-08-22 16:08:15 -0700262#ifdef __cplusplus
263} // extern "C"
264#endif
265
Yaowu Xuf883b422016-08-30 14:01:10 -0700266#endif // AOM_DSP_BITWRITER_H_