blob: 1297a974612f0bea1a13b6f9f95f3ef866048ae8 [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
95 buf_uabs_write(br, bit, probability);
Nathan E. Egge8043cc42016-03-06 12:42:47 -050096#elif CONFIG_DAALA_EC
Nathan E. Egge08c99eb2016-11-09 13:40:26 -050097 // Note this uses raw bits and is not the same as aom_daala_write(r, 128);
Nathan E. Egge8043cc42016-03-06 12:42:47 -050098 aom_daala_write(br, bit, probability);
Alex Converse080a2cc2016-09-20 16:39:01 -070099#else
Alex Converseeb00cb22016-06-06 15:12:06 -0700100 aom_dk_write(br, bit, probability);
Alex Converse080a2cc2016-09-20 16:39:01 -0700101#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700102}
103
Angie Chiangd4022822016-11-02 18:30:25 -0700104static INLINE void aom_write_record(aom_writer *br, int bit, int probability,
105 TOKEN_STATS *token_stats) {
106 aom_write(br, bit, probability);
107#if CONFIG_RD_DEBUG
108 token_stats->cost += av1_cost_bit(probability, bit);
109#else
110 (void)token_stats;
111#endif
112}
113
Yaowu Xuf883b422016-08-30 14:01:10 -0700114static INLINE void aom_write_bit(aom_writer *w, int bit) {
Nathan E. Egge67b99212016-12-14 12:56:20 -0500115#if CONFIG_ANS
116 buf_uabs_write_bit(w, bit);
117#elif CONFIG_DAALA_EC
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. Eggeeeedc632016-06-19 12:02:33 -0400140static INLINE void aom_write_tree_bits(aom_writer *w, const aom_tree_index *tr,
141 const aom_prob *probs, int bits, int len,
142 aom_tree_index i) {
143 do {
144 const int bit = (bits >> --len) & 1;
145 aom_write(w, bit, probs[i >> 1]);
146 i = tr[i + bit];
147 } while (len);
148}
149
Angie Chiangd4022822016-11-02 18:30:25 -0700150static INLINE void aom_write_tree_bits_record(aom_writer *w,
151 const aom_tree_index *tr,
152 const aom_prob *probs, int bits,
153 int len, aom_tree_index i,
154 TOKEN_STATS *token_stats) {
155 do {
156 const int bit = (bits >> --len) & 1;
157 aom_write_record(w, bit, probs[i >> 1], token_stats);
158 i = tr[i + bit];
159 } while (len);
160}
161
Nathan E. Eggeeeedc632016-06-19 12:02:33 -0400162static INLINE void aom_write_tree(aom_writer *w, const aom_tree_index *tree,
163 const aom_prob *probs, int bits, int len,
164 aom_tree_index i) {
Nathan E. Egge43acafd2016-03-06 13:41:53 -0500165#if CONFIG_DAALA_EC
166 daala_write_tree_bits(w, tree, probs, bits, len, i);
167#else
Nathan E. Eggeeeedc632016-06-19 12:02:33 -0400168 aom_write_tree_bits(w, tree, probs, bits, len, i);
Nathan E. Egge43acafd2016-03-06 13:41:53 -0500169#endif
Nathan E. Eggeeeedc632016-06-19 12:02:33 -0400170}
171
Angie Chiangd4022822016-11-02 18:30:25 -0700172static INLINE void aom_write_tree_record(aom_writer *w,
173 const aom_tree_index *tree,
174 const aom_prob *probs, int bits,
175 int len, aom_tree_index i,
176 TOKEN_STATS *token_stats) {
177#if CONFIG_DAALA_EC
178 (void)token_stats;
179 daala_write_tree_bits(w, tree, probs, bits, len, i);
180#else
181 aom_write_tree_bits_record(w, tree, probs, bits, len, i, token_stats);
182#endif
183}
184
Alex Converse58c520a2016-10-20 14:21:06 -0700185#if CONFIG_EC_MULTISYMBOL
Nathan E. Egge58ef5512016-12-27 11:41:26 -0500186static INLINE void aom_write_cdf(aom_writer *w, int symb,
187 const aom_cdf_prob *cdf, int nsymbs) {
Alex Converse1e4e29f2016-11-08 14:12:14 -0800188#if CONFIG_ANS
Alex Conversea1ac9722016-10-12 15:59:58 -0700189 struct rans_sym s;
190 (void)nsymbs;
191 assert(cdf);
Alex Conversee9f70f82016-08-22 12:44:16 -0700192 s.cum_prob = symb > 0 ? cdf[symb - 1] : 0;
193 s.prob = cdf[symb] - s.cum_prob;
Alex Conversea1ac9722016-10-12 15:59:58 -0700194 buf_rans_write(w, &s);
Nathan E. Egge56eeaa52016-07-25 10:23:33 -0400195#elif CONFIG_DAALA_EC
196 daala_write_symbol(w, symb, cdf, nsymbs);
Nathan E. Egge44460142016-06-20 13:44:22 -0400197#else
Alex Converse58c520a2016-10-20 14:21:06 -0700198#error \
199 "CONFIG_EC_MULTISYMBOL is selected without a valid backing entropy " \
200 "coder. Enable daala_ec or ans for a valid configuration."
Nathan E. Egge44460142016-06-20 13:44:22 -0400201#endif
Nathan E. Egge87d44dc2017-01-06 19:15:03 -0500202}
Thomas9ac55082016-09-23 18:04:17 +0100203
Nathan E. Egge87d44dc2017-01-06 19:15:03 -0500204static INLINE void aom_write_symbol(aom_writer *w, int symb, aom_cdf_prob *cdf,
205 int nsymbs) {
206 aom_write_cdf(w, symb, cdf, nsymbs);
Alex Converseaca9feb2016-10-10 11:08:10 -0700207#if CONFIG_EC_ADAPT
Thomas9ac55082016-09-23 18:04:17 +0100208 update_cdf(cdf, symb, nsymbs);
209#endif
Nathan E. Egge44460142016-06-20 13:44:22 -0400210}
Nathan E. Eggec98d2862016-12-21 11:30:46 -0500211
212#if CONFIG_PVQ
Nathan E. Egge87d44dc2017-01-06 19:15:03 -0500213static INLINE void aom_write_cdf_unscaled(aom_writer *w, int symb,
214 const aom_cdf_prob *cdf, int nsymbs) {
Nathan E. Eggec98d2862016-12-21 11:30:46 -0500215#if CONFIG_DAALA_EC
216 od_ec_encode_cdf_unscaled(&w->ec, symb, cdf, nsymbs);
217#else
218#error "CONFIG_PVQ currently requires CONFIG_DAALA_EC."
219#endif
220}
221#endif
Alex Converse58c520a2016-10-20 14:21:06 -0700222#endif // CONFIG_EC_MULTISYMBOL
Nathan E. Egge44460142016-06-20 13:44:22 -0400223
Yaowu Xuc27fc142016-08-22 16:08:15 -0700224#ifdef __cplusplus
225} // extern "C"
226#endif
227
Yaowu Xuf883b422016-08-30 14:01:10 -0700228#endif // AOM_DSP_BITWRITER_H_