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 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 struct aom_writer {
32 unsigned int pos;
33 uint8_t *buffer;
34 od_ec_enc ec;
35 uint8_t allow_update_cdf;
36 };
37
38 typedef struct aom_writer aom_writer;
39
40 typedef struct TOKEN_STATS {
41 int cost;
42 #if CONFIG_RD_DEBUG
43 int txb_coeff_cost_map[TXB_COEFF_COST_MAP_SIZE][TXB_COEFF_COST_MAP_SIZE];
44 #endif
45 } TOKEN_STATS;
46
init_token_stats(TOKEN_STATS * token_stats)47 static INLINE void init_token_stats(TOKEN_STATS *token_stats) {
48 #if CONFIG_RD_DEBUG
49 int r, c;
50 for (r = 0; r < TXB_COEFF_COST_MAP_SIZE; ++r) {
51 for (c = 0; c < TXB_COEFF_COST_MAP_SIZE; ++c) {
52 token_stats->txb_coeff_cost_map[r][c] = 0;
53 }
54 }
55 #endif
56 token_stats->cost = 0;
57 }
58
59 void aom_start_encode(aom_writer *w, uint8_t *buffer);
60
61 int aom_stop_encode(aom_writer *w);
62
aom_write(aom_writer * w,int bit,int probability)63 static INLINE void aom_write(aom_writer *w, int bit, int probability) {
64 int p = (0x7FFFFF - (probability << 15) + probability) >> 8;
65 #if CONFIG_BITSTREAM_DEBUG
66 aom_cdf_prob cdf[2] = { (aom_cdf_prob)p, 32767 };
67 /*int queue_r = 0;
68 int frame_idx_r = 0;
69 int queue_w = bitstream_queue_get_write();
70 int frame_idx_w = aom_bitstream_queue_get_frame_writee();
71 if (frame_idx_w == frame_idx_r && queue_w == queue_r) {
72 fprintf(stderr, "\n *** bitstream queue at frame_idx_w %d queue_w %d\n",
73 frame_idx_w, queue_w);
74 }*/
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 /*int queue_r = 0;
95 int frame_idx_r = 0;
96 int queue_w = bitstream_queue_get_write();
97 int frame_idx_w = aom_bitstream_queue_get_frame_writee();
98 if (frame_idx_w == frame_idx_r && queue_w == queue_r) {
99 fprintf(stderr, "\n *** bitstream queue at frame_idx_w %d queue_w %d\n",
100 frame_idx_w, queue_w);
101 }*/
102 bitstream_queue_push(symb, cdf, nsymbs);
103 #endif
104
105 od_ec_encode_cdf_q15(&w->ec, symb, cdf, nsymbs);
106 }
107
aom_write_symbol(aom_writer * w,int symb,aom_cdf_prob * cdf,int nsymbs)108 static INLINE void aom_write_symbol(aom_writer *w, int symb, aom_cdf_prob *cdf,
109 int nsymbs) {
110 aom_write_cdf(w, symb, cdf, nsymbs);
111 if (w->allow_update_cdf) update_cdf(cdf, symb, nsymbs);
112 }
113
114 #ifdef __cplusplus
115 } // extern "C"
116 #endif
117
118 #endif // AOM_AOM_DSP_BITWRITER_H_
119