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 int aom_stop_encode(aom_writer *w);
66 
67 int aom_tell_size(aom_writer *w);
68 
aom_write(aom_writer * w,int bit,int probability)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
75 
76   od_ec_encode_bool_q15(&w->ec, bit, p);
77 }
78 
aom_write_bit(aom_writer * w,int bit)79 static INLINE void aom_write_bit(aom_writer *w, int bit) {
80   aom_write(w, bit, 128);  // aom_prob_half
81 }
82 
aom_write_literal(aom_writer * w,int data,int bits)83 static INLINE void aom_write_literal(aom_writer *w, int data, int bits) {
84   int bit;
85 
86   for (bit = bits - 1; bit >= 0; bit--) aom_write_bit(w, 1 & (data >> bit));
87 }
88 
aom_write_cdf(aom_writer * w,int symb,const aom_cdf_prob * cdf,int nsymbs)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
94 
95   od_ec_encode_cdf_q15(&w->ec, symb, cdf, nsymbs);
96 }
97 
aom_write_symbol(aom_writer * w,int symb,aom_cdf_prob * cdf,int 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);
102 }
103 
104 #ifdef __cplusplus
105 }  // extern "C"
106 #endif
107 
108 #endif  // AOM_AOM_DSP_BITWRITER_H_
109