cdef: Improve cdef_filter_block_8x8_{8,16}.
[aom.git] / aom_dsp / bitwriter.h
blobfb33909968deb5212a6c28c7888a13539affd68f
1 /*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
4 * 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.
12 #ifndef AOM_AOM_DSP_BITWRITER_H_
13 #define AOM_AOM_DSP_BITWRITER_H_
15 #include <assert.h>
17 #include "config/aom_config.h"
19 #include "aom_dsp/entenc.h"
20 #include "aom_dsp/prob.h"
22 #if CONFIG_RD_DEBUG
23 #include "av1/common/blockd.h"
24 #include "av1/encoder/cost.h"
25 #endif
27 #if CONFIG_BITSTREAM_DEBUG
28 #include "aom_util/debug_util.h"
29 #endif // CONFIG_BITSTREAM_DEBUG
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
35 struct aom_writer {
36 unsigned int pos;
37 uint8_t *buffer;
38 od_ec_enc ec;
39 uint8_t allow_update_cdf;
42 typedef struct aom_writer aom_writer;
44 typedef struct TOKEN_STATS {
45 int cost;
46 #if CONFIG_RD_DEBUG
47 int txb_coeff_cost_map[TXB_COEFF_COST_MAP_SIZE][TXB_COEFF_COST_MAP_SIZE];
48 #endif
49 } TOKEN_STATS;
51 static INLINE void init_token_stats(TOKEN_STATS *token_stats) {
52 #if CONFIG_RD_DEBUG
53 int r, c;
54 for (r = 0; r < TXB_COEFF_COST_MAP_SIZE; ++r) {
55 for (c = 0; c < TXB_COEFF_COST_MAP_SIZE; ++c) {
56 token_stats->txb_coeff_cost_map[r][c] = 0;
59 #endif
60 token_stats->cost = 0;
63 void aom_start_encode(aom_writer *w, uint8_t *buffer);
65 int aom_stop_encode(aom_writer *w);
67 int aom_tell_size(aom_writer *w);
69 static INLINE void aom_write(aom_writer *w, int bit, int probability) {
70 int p = (0x7FFFFF - (probability << 15) + probability) >> 8;
71 #if CONFIG_BITSTREAM_DEBUG
72 aom_cdf_prob cdf[2] = { (aom_cdf_prob)p, 32767 };
73 bitstream_queue_push(bit, cdf, 2);
74 #endif
76 od_ec_encode_bool_q15(&w->ec, bit, p);
79 static INLINE void aom_write_bit(aom_writer *w, int bit) {
80 aom_write(w, bit, 128); // aom_prob_half
83 static INLINE void aom_write_literal(aom_writer *w, int data, int bits) {
84 int bit;
86 for (bit = bits - 1; bit >= 0; bit--) aom_write_bit(w, 1 & (data >> bit));
89 static INLINE void aom_write_cdf(aom_writer *w, int symb,
90 const aom_cdf_prob *cdf, int nsymbs) {
91 #if CONFIG_BITSTREAM_DEBUG
92 bitstream_queue_push(symb, cdf, nsymbs);
93 #endif
95 od_ec_encode_cdf_q15(&w->ec, symb, cdf, nsymbs);
98 static INLINE void aom_write_symbol(aom_writer *w, int symb, aom_cdf_prob *cdf,
99 int nsymbs) {
100 aom_write_cdf(w, symb, cdf, nsymbs);
101 if (w->allow_update_cdf) update_cdf(cdf, symb, nsymbs);
104 #ifdef __cplusplus
105 } // extern "C"
106 #endif
108 #endif // AOM_AOM_DSP_BITWRITER_H_