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 #include "aom_dsp/bitwriter.h"
12
13 #include "av1/common/common.h"
14 #include "av1/common/entropy.h"
15 #include "av1/encoder/cost.h"
16 #include "av1/encoder/subexp.h"
17
18 static const uint8_t update_bits[255] = {
19 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6,
20 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 8, 8, 8, 8, 8, 8,
21 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
22 8, 8, 8, 8, 8, 8, 8, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
23 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
24 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
25 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11,
26 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
27 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
28 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
29 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
30 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
31 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
32 11, 11, 11, 11, 11, 11, 11, 0,
33 };
34 #define MIN_DELP_BITS 5
35
recenter_nonneg(int v,int m)36 static int recenter_nonneg(int v, int m) {
37 if (v > (m << 1))
38 return v;
39 else if (v >= m)
40 return ((v - m) << 1);
41 else
42 return ((m - v) << 1) - 1;
43 }
44
remap_prob(int v,int m)45 static int remap_prob(int v, int m) {
46 int i;
47 static const uint8_t map_table[MAX_PROB - 1] = {
48 // generated by:
49 // map_table[j] = split_index(j, MAX_PROB - 1, MODULUS_PARAM);
50 20, 21, 22, 23, 24, 25, 0, 26, 27, 28, 29, 30, 31, 32, 33,
51 34, 35, 36, 37, 1, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
52 48, 49, 2, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
53 3, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 4, 74,
54 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 5, 86, 87, 88,
55 89, 90, 91, 92, 93, 94, 95, 96, 97, 6, 98, 99, 100, 101, 102,
56 103, 104, 105, 106, 107, 108, 109, 7, 110, 111, 112, 113, 114, 115, 116,
57 117, 118, 119, 120, 121, 8, 122, 123, 124, 125, 126, 127, 128, 129, 130,
58 131, 132, 133, 9, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144,
59 145, 10, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 11,
60 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 12, 170, 171,
61 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 13, 182, 183, 184, 185,
62 186, 187, 188, 189, 190, 191, 192, 193, 14, 194, 195, 196, 197, 198, 199,
63 200, 201, 202, 203, 204, 205, 15, 206, 207, 208, 209, 210, 211, 212, 213,
64 214, 215, 216, 217, 16, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227,
65 228, 229, 17, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241,
66 18, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 19,
67 };
68 v--;
69 m--;
70 if ((m << 1) <= MAX_PROB)
71 i = recenter_nonneg(v, m) - 1;
72 else
73 i = recenter_nonneg(MAX_PROB - 1 - v, MAX_PROB - 1 - m) - 1;
74
75 i = map_table[i];
76 return i;
77 }
78
prob_diff_update_cost(aom_prob newp,aom_prob oldp)79 static int prob_diff_update_cost(aom_prob newp, aom_prob oldp) {
80 int delp = remap_prob(newp, oldp);
81 return update_bits[delp] << AV1_PROB_COST_SHIFT;
82 }
83
encode_uniform(aom_writer * w,int v)84 static void encode_uniform(aom_writer *w, int v) {
85 const int l = 8;
86 const int m = (1 << l) - 190;
87 if (v < m) {
88 aom_write_literal(w, v, l - 1);
89 } else {
90 aom_write_literal(w, m + ((v - m) >> 1), l - 1);
91 aom_write_literal(w, (v - m) & 1, 1);
92 }
93 }
94
write_bit_gte(aom_writer * w,int word,int test)95 static INLINE int write_bit_gte(aom_writer *w, int word, int test) {
96 aom_write_literal(w, word >= test, 1);
97 return word >= test;
98 }
99
encode_term_subexp(aom_writer * w,int word)100 static void encode_term_subexp(aom_writer *w, int word) {
101 if (!write_bit_gte(w, word, 16)) {
102 aom_write_literal(w, word, 4);
103 } else if (!write_bit_gte(w, word, 32)) {
104 aom_write_literal(w, word - 16, 4);
105 } else if (!write_bit_gte(w, word, 64)) {
106 aom_write_literal(w, word - 32, 5);
107 } else {
108 encode_uniform(w, word - 64);
109 }
110 }
111
av1_write_prob_diff_update(aom_writer * w,aom_prob newp,aom_prob oldp)112 void av1_write_prob_diff_update(aom_writer *w, aom_prob newp, aom_prob oldp) {
113 const int delp = remap_prob(newp, oldp);
114 encode_term_subexp(w, delp);
115 }
116
av1_prob_diff_update_savings_search(const unsigned int * ct,aom_prob oldp,aom_prob * bestp,aom_prob upd,int probwt)117 int av1_prob_diff_update_savings_search(const unsigned int *ct, aom_prob oldp,
118 aom_prob *bestp, aom_prob upd,
119 int probwt) {
120 const uint32_t old_b = cost_branch256(ct, oldp);
121 int bestsavings = 0;
122 aom_prob newp, bestnewp = oldp;
123 const int step = *bestp > oldp ? -1 : 1;
124 const int upd_cost = av1_cost_one(upd) - av1_cost_zero(upd);
125
126 if (old_b > (uint32_t)upd_cost + (MIN_DELP_BITS << AV1_PROB_COST_SHIFT)) {
127 for (newp = *bestp; newp != oldp; newp += step) {
128 const int new_b = cost_branch256(ct, newp);
129 const int update_b = prob_diff_update_cost(newp, oldp) + upd_cost;
130 const int savings = (int)((int64_t)old_b - new_b - update_b * probwt);
131 if (savings > bestsavings) {
132 bestsavings = savings;
133 bestnewp = newp;
134 }
135 }
136 }
137 *bestp = bestnewp;
138 return bestsavings;
139 }
140
av1_cond_prob_diff_update(aom_writer * w,aom_prob * oldp,const unsigned int ct[2],int probwt)141 void av1_cond_prob_diff_update(aom_writer *w, aom_prob *oldp,
142 const unsigned int ct[2], int probwt) {
143 const aom_prob upd = DIFF_UPDATE_PROB;
144 aom_prob newp = get_binary_prob(ct[0], ct[1]);
145 const int savings =
146 av1_prob_diff_update_savings_search(ct, *oldp, &newp, upd, probwt);
147 assert(newp >= 1);
148 if (savings > 0) {
149 aom_write(w, 1, upd);
150 av1_write_prob_diff_update(w, newp, *oldp);
151 *oldp = newp;
152 } else {
153 aom_write(w, 0, upd);
154 }
155 }
156
av1_cond_prob_diff_update_savings(aom_prob * oldp,const unsigned int ct[2],int probwt)157 int av1_cond_prob_diff_update_savings(aom_prob *oldp, const unsigned int ct[2],
158 int probwt) {
159 const aom_prob upd = DIFF_UPDATE_PROB;
160 aom_prob newp = get_binary_prob(ct[0], ct[1]);
161 const int savings =
162 av1_prob_diff_update_savings_search(ct, *oldp, &newp, upd, probwt);
163 return savings;
164 }
165