1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker *
4*77c1e3ccSAndroid Build Coastguard Worker * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker */
11*77c1e3ccSAndroid Build Coastguard Worker
12*77c1e3ccSAndroid Build Coastguard Worker #ifndef AOM_AOM_DSP_BITWRITER_H_
13*77c1e3ccSAndroid Build Coastguard Worker #define AOM_AOM_DSP_BITWRITER_H_
14*77c1e3ccSAndroid Build Coastguard Worker
15*77c1e3ccSAndroid Build Coastguard Worker #include <assert.h>
16*77c1e3ccSAndroid Build Coastguard Worker
17*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_config.h"
18*77c1e3ccSAndroid Build Coastguard Worker
19*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/entenc.h"
20*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/prob.h"
21*77c1e3ccSAndroid Build Coastguard Worker
22*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_RD_DEBUG
23*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/blockd.h"
24*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/cost.h"
25*77c1e3ccSAndroid Build Coastguard Worker #endif
26*77c1e3ccSAndroid Build Coastguard Worker
27*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_BITSTREAM_DEBUG
28*77c1e3ccSAndroid Build Coastguard Worker #include "aom_util/debug_util.h"
29*77c1e3ccSAndroid Build Coastguard Worker #endif // CONFIG_BITSTREAM_DEBUG
30*77c1e3ccSAndroid Build Coastguard Worker
31*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
32*77c1e3ccSAndroid Build Coastguard Worker extern "C" {
33*77c1e3ccSAndroid Build Coastguard Worker #endif
34*77c1e3ccSAndroid Build Coastguard Worker
35*77c1e3ccSAndroid Build Coastguard Worker struct aom_writer {
36*77c1e3ccSAndroid Build Coastguard Worker unsigned int pos;
37*77c1e3ccSAndroid Build Coastguard Worker uint8_t *buffer;
38*77c1e3ccSAndroid Build Coastguard Worker od_ec_enc ec;
39*77c1e3ccSAndroid Build Coastguard Worker uint8_t allow_update_cdf;
40*77c1e3ccSAndroid Build Coastguard Worker };
41*77c1e3ccSAndroid Build Coastguard Worker
42*77c1e3ccSAndroid Build Coastguard Worker typedef struct aom_writer aom_writer;
43*77c1e3ccSAndroid Build Coastguard Worker
44*77c1e3ccSAndroid Build Coastguard Worker typedef struct TOKEN_STATS {
45*77c1e3ccSAndroid Build Coastguard Worker int cost;
46*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_RD_DEBUG
47*77c1e3ccSAndroid Build Coastguard Worker int txb_coeff_cost_map[TXB_COEFF_COST_MAP_SIZE][TXB_COEFF_COST_MAP_SIZE];
48*77c1e3ccSAndroid Build Coastguard Worker #endif
49*77c1e3ccSAndroid Build Coastguard Worker } TOKEN_STATS;
50*77c1e3ccSAndroid Build Coastguard Worker
init_token_stats(TOKEN_STATS * token_stats)51*77c1e3ccSAndroid Build Coastguard Worker static inline void init_token_stats(TOKEN_STATS *token_stats) {
52*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_RD_DEBUG
53*77c1e3ccSAndroid Build Coastguard Worker int r, c;
54*77c1e3ccSAndroid Build Coastguard Worker for (r = 0; r < TXB_COEFF_COST_MAP_SIZE; ++r) {
55*77c1e3ccSAndroid Build Coastguard Worker for (c = 0; c < TXB_COEFF_COST_MAP_SIZE; ++c) {
56*77c1e3ccSAndroid Build Coastguard Worker token_stats->txb_coeff_cost_map[r][c] = 0;
57*77c1e3ccSAndroid Build Coastguard Worker }
58*77c1e3ccSAndroid Build Coastguard Worker }
59*77c1e3ccSAndroid Build Coastguard Worker #endif
60*77c1e3ccSAndroid Build Coastguard Worker token_stats->cost = 0;
61*77c1e3ccSAndroid Build Coastguard Worker }
62*77c1e3ccSAndroid Build Coastguard Worker
63*77c1e3ccSAndroid Build Coastguard Worker void aom_start_encode(aom_writer *w, uint8_t *buffer);
64*77c1e3ccSAndroid Build Coastguard Worker
65*77c1e3ccSAndroid Build Coastguard Worker // Returns a negative number on error. Caller must check the return value and
66*77c1e3ccSAndroid Build Coastguard Worker // handle error.
67*77c1e3ccSAndroid Build Coastguard Worker int aom_stop_encode(aom_writer *w);
68*77c1e3ccSAndroid Build Coastguard Worker
69*77c1e3ccSAndroid Build Coastguard Worker int aom_tell_size(aom_writer *w);
70*77c1e3ccSAndroid Build Coastguard Worker
aom_write(aom_writer * w,int bit,int probability)71*77c1e3ccSAndroid Build Coastguard Worker static inline void aom_write(aom_writer *w, int bit, int probability) {
72*77c1e3ccSAndroid Build Coastguard Worker int p = (0x7FFFFF - (probability << 15) + probability) >> 8;
73*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_BITSTREAM_DEBUG
74*77c1e3ccSAndroid Build Coastguard Worker aom_cdf_prob cdf[2] = { (aom_cdf_prob)p, 32767 };
75*77c1e3ccSAndroid Build Coastguard Worker bitstream_queue_push(bit, cdf, 2);
76*77c1e3ccSAndroid Build Coastguard Worker #endif
77*77c1e3ccSAndroid Build Coastguard Worker
78*77c1e3ccSAndroid Build Coastguard Worker od_ec_encode_bool_q15(&w->ec, bit, p);
79*77c1e3ccSAndroid Build Coastguard Worker }
80*77c1e3ccSAndroid Build Coastguard Worker
aom_write_bit(aom_writer * w,int bit)81*77c1e3ccSAndroid Build Coastguard Worker static inline void aom_write_bit(aom_writer *w, int bit) {
82*77c1e3ccSAndroid Build Coastguard Worker aom_write(w, bit, 128); // aom_prob_half
83*77c1e3ccSAndroid Build Coastguard Worker }
84*77c1e3ccSAndroid Build Coastguard Worker
aom_write_literal(aom_writer * w,int data,int bits)85*77c1e3ccSAndroid Build Coastguard Worker static inline void aom_write_literal(aom_writer *w, int data, int bits) {
86*77c1e3ccSAndroid Build Coastguard Worker int bit;
87*77c1e3ccSAndroid Build Coastguard Worker
88*77c1e3ccSAndroid Build Coastguard Worker for (bit = bits - 1; bit >= 0; bit--) aom_write_bit(w, 1 & (data >> bit));
89*77c1e3ccSAndroid Build Coastguard Worker }
90*77c1e3ccSAndroid Build Coastguard Worker
aom_write_cdf(aom_writer * w,int symb,const aom_cdf_prob * cdf,int nsymbs)91*77c1e3ccSAndroid Build Coastguard Worker static inline void aom_write_cdf(aom_writer *w, int symb,
92*77c1e3ccSAndroid Build Coastguard Worker const aom_cdf_prob *cdf, int nsymbs) {
93*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_BITSTREAM_DEBUG
94*77c1e3ccSAndroid Build Coastguard Worker bitstream_queue_push(symb, cdf, nsymbs);
95*77c1e3ccSAndroid Build Coastguard Worker #endif
96*77c1e3ccSAndroid Build Coastguard Worker
97*77c1e3ccSAndroid Build Coastguard Worker od_ec_encode_cdf_q15(&w->ec, symb, cdf, nsymbs);
98*77c1e3ccSAndroid Build Coastguard Worker }
99*77c1e3ccSAndroid Build Coastguard Worker
aom_write_symbol(aom_writer * w,int symb,aom_cdf_prob * cdf,int nsymbs)100*77c1e3ccSAndroid Build Coastguard Worker static inline void aom_write_symbol(aom_writer *w, int symb, aom_cdf_prob *cdf,
101*77c1e3ccSAndroid Build Coastguard Worker int nsymbs) {
102*77c1e3ccSAndroid Build Coastguard Worker aom_write_cdf(w, symb, cdf, nsymbs);
103*77c1e3ccSAndroid Build Coastguard Worker if (w->allow_update_cdf) update_cdf(cdf, symb, nsymbs);
104*77c1e3ccSAndroid Build Coastguard Worker }
105*77c1e3ccSAndroid Build Coastguard Worker
106*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
107*77c1e3ccSAndroid Build Coastguard Worker } // extern "C"
108*77c1e3ccSAndroid Build Coastguard Worker #endif
109*77c1e3ccSAndroid Build Coastguard Worker
110*77c1e3ccSAndroid Build Coastguard Worker #endif // AOM_AOM_DSP_BITWRITER_H_
111