1 /*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3 *
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.
10 */
11
12 #ifndef AOM_AOM_DSP_BITWRITER_H_
13 #define AOM_AOM_DSP_BITWRITER_H_
14
15 #include <assert.h>
16
17 #include "config/aom_config.h"
18
19 #include "aom_dsp/entenc.h"
20 #include "aom_dsp/prob.h"
21
22 #if CONFIG_RD_DEBUG
23 #include "av1/common/blockd.h"
24 #include "av1/encoder/cost.h"
25 #endif
26
27 #if CONFIG_BITSTREAM_DEBUG
28 #include "aom_util/debug_util.h"
29 #endif // CONFIG_BITSTREAM_DEBUG
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 struct aom_writer {
36 unsigned int pos;
37 uint8_t *buffer;
38 od_ec_enc ec;
39 uint8_t allow_update_cdf;
40 };
41
42 typedef struct aom_writer aom_writer;
43
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;
50
init_token_stats(TOKEN_STATS * 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;
57 }
58 }
59 #endif
60 token_stats->cost = 0;
61 }
62
63 void aom_start_encode(aom_writer *w, uint8_t *buffer);
64
65 // Returns a negative number on error. Caller must check the return value and
66 // handle error.
67 int aom_stop_encode(aom_writer *w);
68
69 int aom_tell_size(aom_writer *w);
70
aom_write(aom_writer * w,int bit,int probability)71 static inline void aom_write(aom_writer *w, int bit, int probability) {
72 int p = (0x7FFFFF - (probability << 15) + probability) >> 8;
73 #if CONFIG_BITSTREAM_DEBUG
74 aom_cdf_prob cdf[2] = { (aom_cdf_prob)p, 32767 };
75 bitstream_queue_push(bit, cdf, 2);
76 #endif
77
78 od_ec_encode_bool_q15(&w->ec, bit, p);
79 }
80
aom_write_bit(aom_writer * w,int bit)81 static inline void aom_write_bit(aom_writer *w, int bit) {
82 aom_write(w, bit, 128); // aom_prob_half
83 }
84
aom_write_literal(aom_writer * w,int data,int bits)85 static inline void aom_write_literal(aom_writer *w, int data, int bits) {
86 int bit;
87
88 for (bit = bits - 1; bit >= 0; bit--) aom_write_bit(w, 1 & (data >> bit));
89 }
90
aom_write_cdf(aom_writer * w,int symb,const aom_cdf_prob * cdf,int nsymbs)91 static inline void aom_write_cdf(aom_writer *w, int symb,
92 const aom_cdf_prob *cdf, int nsymbs) {
93 #if CONFIG_BITSTREAM_DEBUG
94 bitstream_queue_push(symb, cdf, nsymbs);
95 #endif
96
97 od_ec_encode_cdf_q15(&w->ec, symb, cdf, nsymbs);
98 }
99
aom_write_symbol(aom_writer * w,int symb,aom_cdf_prob * cdf,int nsymbs)100 static inline void aom_write_symbol(aom_writer *w, int symb, aom_cdf_prob *cdf,
101 int nsymbs) {
102 aom_write_cdf(w, symb, cdf, nsymbs);
103 if (w->allow_update_cdf) update_cdf(cdf, symb, nsymbs);
104 }
105
106 #ifdef __cplusplus
107 } // extern "C"
108 #endif
109
110 #endif // AOM_AOM_DSP_BITWRITER_H_
111