blob: 78ddc0834838ce50a272e19965ea15a9e0f02d2d [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
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) {
Alex Converse080a2cc2016-09-20 16:39:01 -0700114 aom_write(w, bit, 128); // aom_prob_half
Yaowu Xuc27fc142016-08-22 16:08:15 -0700115}
116
Angie Chiangd4022822016-11-02 18:30:25 -0700117static INLINE void aom_write_bit_record(aom_writer *w, int bit,
118 TOKEN_STATS *token_stats) {
119 aom_write_record(w, bit, 128, token_stats); // aom_prob_half
120}
121
Yaowu Xuf883b422016-08-30 14:01:10 -0700122static INLINE void aom_write_literal(aom_writer *w, int data, int bits) {
Alex Converse080a2cc2016-09-20 16:39:01 -0700123 int bit;
124
125 for (bit = bits - 1; bit >= 0; bit--) aom_write_bit(w, 1 & (data >> bit));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700126}
127
Nathan E. Eggeeeedc632016-06-19 12:02:33 -0400128static INLINE void aom_write_tree_bits(aom_writer *w, const aom_tree_index *tr,
129 const aom_prob *probs, int bits, int len,
130 aom_tree_index i) {
131 do {
132 const int bit = (bits >> --len) & 1;
133 aom_write(w, bit, probs[i >> 1]);
134 i = tr[i + bit];
135 } while (len);
136}
137
Angie Chiangd4022822016-11-02 18:30:25 -0700138static INLINE void aom_write_tree_bits_record(aom_writer *w,
139 const aom_tree_index *tr,
140 const aom_prob *probs, int bits,
141 int len, aom_tree_index i,
142 TOKEN_STATS *token_stats) {
143 do {
144 const int bit = (bits >> --len) & 1;
145 aom_write_record(w, bit, probs[i >> 1], token_stats);
146 i = tr[i + bit];
147 } while (len);
148}
149
Nathan E. Eggeeeedc632016-06-19 12:02:33 -0400150static INLINE void aom_write_tree(aom_writer *w, const aom_tree_index *tree,
151 const aom_prob *probs, int bits, int len,
152 aom_tree_index i) {
Nathan E. Egge43acafd2016-03-06 13:41:53 -0500153#if CONFIG_DAALA_EC
154 daala_write_tree_bits(w, tree, probs, bits, len, i);
155#else
Nathan E. Eggeeeedc632016-06-19 12:02:33 -0400156 aom_write_tree_bits(w, tree, probs, bits, len, i);
Nathan E. Egge43acafd2016-03-06 13:41:53 -0500157#endif
Nathan E. Eggeeeedc632016-06-19 12:02:33 -0400158}
159
Angie Chiangd4022822016-11-02 18:30:25 -0700160static INLINE void aom_write_tree_record(aom_writer *w,
161 const aom_tree_index *tree,
162 const aom_prob *probs, int bits,
163 int len, aom_tree_index i,
164 TOKEN_STATS *token_stats) {
165#if CONFIG_DAALA_EC
166 (void)token_stats;
167 daala_write_tree_bits(w, tree, probs, bits, len, i);
168#else
169 aom_write_tree_bits_record(w, tree, probs, bits, len, i, token_stats);
170#endif
171}
172
Alex Converse58c520a2016-10-20 14:21:06 -0700173#if CONFIG_EC_MULTISYMBOL
Thomas9ac55082016-09-23 18:04:17 +0100174static INLINE void aom_write_symbol(aom_writer *w, int symb, aom_cdf_prob *cdf,
175 int nsymbs) {
Alex Converse1e4e29f2016-11-08 14:12:14 -0800176#if CONFIG_ANS
Alex Conversea1ac9722016-10-12 15:59:58 -0700177 struct rans_sym s;
178 (void)nsymbs;
179 assert(cdf);
Alex Conversee9f70f82016-08-22 12:44:16 -0700180 s.cum_prob = symb > 0 ? cdf[symb - 1] : 0;
181 s.prob = cdf[symb] - s.cum_prob;
Alex Conversea1ac9722016-10-12 15:59:58 -0700182 buf_rans_write(w, &s);
Nathan E. Egge56eeaa52016-07-25 10:23:33 -0400183#elif CONFIG_DAALA_EC
184 daala_write_symbol(w, symb, cdf, nsymbs);
Nathan E. Egge44460142016-06-20 13:44:22 -0400185#else
Alex Converse58c520a2016-10-20 14:21:06 -0700186#error \
187 "CONFIG_EC_MULTISYMBOL is selected without a valid backing entropy " \
188 "coder. Enable daala_ec or ans for a valid configuration."
Nathan E. Egge44460142016-06-20 13:44:22 -0400189#endif
Thomas9ac55082016-09-23 18:04:17 +0100190
Alex Converseaca9feb2016-10-10 11:08:10 -0700191#if CONFIG_EC_ADAPT
Thomas9ac55082016-09-23 18:04:17 +0100192 update_cdf(cdf, symb, nsymbs);
193#endif
Nathan E. Egge44460142016-06-20 13:44:22 -0400194}
Alex Converse58c520a2016-10-20 14:21:06 -0700195#endif // CONFIG_EC_MULTISYMBOL
Nathan E. Egge44460142016-06-20 13:44:22 -0400196
Yaowu Xuc27fc142016-08-22 16:08:15 -0700197#ifdef __cplusplus
198} // extern "C"
199#endif
200
Yaowu Xuf883b422016-08-30 14:01:10 -0700201#endif // AOM_DSP_BITWRITER_H_