bitwriter.h 5.14 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
Yaowu Xu's avatar
Yaowu Xu committed
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
John Koleszar's avatar
John Koleszar committed
3
 *
Yaowu Xu's avatar
Yaowu Xu committed
4
5
6
7
8
9
 * 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.
John Koleszar's avatar
John Koleszar committed
10
11
 */

Yaowu Xu's avatar
Yaowu Xu committed
12
13
#ifndef AOM_DSP_BITWRITER_H_
#define AOM_DSP_BITWRITER_H_
John Koleszar's avatar
John Koleszar committed
14

15
16
#include <assert.h>
#include "./aom_config.h"
17
18
19
20
#if CONFIG_EC_ADAPT && !CONFIG_EC_MULTISYMBOL
#error "CONFIG_EC_ADAPT is enabled without enabling CONFIG_EC_MULTISYMBOL"
#endif

21
22
23
24
25
#if CONFIG_ANS
#include "aom_dsp/buf_ans.h"
#elif CONFIG_DAALA_EC
#include "aom_dsp/daalaboolwriter.h"
#else
26
#include "aom_dsp/dkboolwriter.h"
27
#endif
28
#include "aom_dsp/prob.h"
29

30
31
32
33
#if CONFIG_RD_DEBUG
#include "av1/encoder/cost.h"
#endif

34
35
36
37
#ifdef __cplusplus
extern "C" {
#endif

38
39
#if CONFIG_ANS
typedef struct BufAnsCoder aom_writer;
40
41
#elif CONFIG_DAALA_EC
typedef struct daala_writer aom_writer;
42
#else
43
typedef struct aom_dk_writer aom_writer;
44
#endif
45

46
47
typedef struct TOKEN_STATS { int64_t cost; } TOKEN_STATS;

48
static INLINE void aom_start_encode(aom_writer *bc, uint8_t *buffer) {
49
50
51
52
#if CONFIG_ANS
  (void)bc;
  (void)buffer;
  assert(0 && "buf_ans requires a more complicated startup procedure");
53
54
#elif CONFIG_DAALA_EC
  aom_daala_start_encode(bc, buffer);
55
#else
56
  aom_dk_start_encode(bc, buffer);
57
#endif
58
}
59

60
61
62
63
static INLINE void aom_stop_encode(aom_writer *bc) {
#if CONFIG_ANS
  (void)bc;
  assert(0 && "buf_ans requires a more complicated shutdown procedure");
64
65
#elif CONFIG_DAALA_EC
  aom_daala_stop_encode(bc);
66
67
68
69
#else
  aom_dk_stop_encode(bc);
#endif
}
John Koleszar's avatar
John Koleszar committed
70

71
static INLINE void aom_write(aom_writer *br, int bit, int probability) {
72
73
#if CONFIG_ANS
  buf_uabs_write(br, bit, probability);
74
75
#elif CONFIG_DAALA_EC
  aom_daala_write(br, bit, probability);
76
#else
77
  aom_dk_write(br, bit, probability);
78
#endif
79
80
}

81
82
83
84
85
86
87
88
89
90
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
}

Yaowu Xu's avatar
Yaowu Xu committed
91
static INLINE void aom_write_bit(aom_writer *w, int bit) {
92
  aom_write(w, bit, 128);  // aom_prob_half
93
94
}

95
96
97
98
99
static INLINE void aom_write_bit_record(aom_writer *w, int bit,
                                        TOKEN_STATS *token_stats) {
  aom_write_record(w, bit, 128, token_stats);  // aom_prob_half
}

Yaowu Xu's avatar
Yaowu Xu committed
100
static INLINE void aom_write_literal(aom_writer *w, int data, int bits) {
101
102
103
  int bit;

  for (bit = bits - 1; bit >= 0; bit--) aom_write_bit(w, 1 & (data >> bit));
104
105
}

106
107
108
109
110
111
112
113
114
115
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);
}

116
117
118
119
120
121
122
123
124
125
126
127
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);
}

128
129
130
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) {
131
132
133
#if CONFIG_DAALA_EC
  daala_write_tree_bits(w, tree, probs, bits, len, i);
#else
134
  aom_write_tree_bits(w, tree, probs, bits, len, i);
135
#endif
136
137
}

138
139
140
141
142
143
144
145
146
147
148
149
150
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
}

151
#if CONFIG_EC_MULTISYMBOL
152
153
static INLINE void aom_write_symbol(aom_writer *w, int symb, aom_cdf_prob *cdf,
                                    int nsymbs) {
154
#if CONFIG_RANS
155
156
157
  struct rans_sym s;
  (void)nsymbs;
  assert(cdf);
158
159
  s.cum_prob = symb > 0 ? cdf[symb - 1] : 0;
  s.prob = cdf[symb] - s.cum_prob;
160
  buf_rans_write(w, &s);
161
162
#elif CONFIG_DAALA_EC
  daala_write_symbol(w, symb, cdf, nsymbs);
163
#else
164
165
166
#error \
    "CONFIG_EC_MULTISYMBOL is selected without a valid backing entropy " \
  "coder. Enable daala_ec or ans for a valid configuration."
167
#endif
168

169
#if CONFIG_EC_ADAPT
170
171
  update_cdf(cdf, symb, nsymbs);
#endif
172
}
173
#endif  // CONFIG_EC_MULTISYMBOL
174

175
176
177
178
#ifdef __cplusplus
}  // extern "C"
#endif

Yaowu Xu's avatar
Yaowu Xu committed
179
#endif  // AOM_DSP_BITWRITER_H_