1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libvpx/vp9/encoder/vp9_subexp.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,237 @@ 1.4 +/* 1.5 + * Copyright (c) 2013 The WebM project authors. All Rights Reserved. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license 1.8 + * that can be found in the LICENSE file in the root of the source 1.9 + * tree. An additional intellectual property rights grant can be found 1.10 + * in the file PATENTS. All contributing project authors may 1.11 + * be found in the AUTHORS file in the root of the source tree. 1.12 + */ 1.13 + 1.14 +#include "vp9/common/vp9_common.h" 1.15 +#include "vp9/common/vp9_entropy.h" 1.16 + 1.17 +#include "vp9/encoder/vp9_boolhuff.h" 1.18 +#include "vp9/encoder/vp9_treewriter.h" 1.19 + 1.20 +#define vp9_cost_upd ((int)(vp9_cost_one(upd) - vp9_cost_zero(upd)) >> 8) 1.21 +#define vp9_cost_upd256 ((int)(vp9_cost_one(upd) - vp9_cost_zero(upd))) 1.22 + 1.23 +static int update_bits[255]; 1.24 + 1.25 +static int count_uniform(int v, int n) { 1.26 + int l = get_unsigned_bits(n); 1.27 + int m; 1.28 + if (l == 0) return 0; 1.29 + m = (1 << l) - n; 1.30 + if (v < m) 1.31 + return l - 1; 1.32 + else 1.33 + return l; 1.34 +} 1.35 + 1.36 +static int split_index(int i, int n, int modulus) { 1.37 + int max1 = (n - 1 - modulus / 2) / modulus + 1; 1.38 + if (i % modulus == modulus / 2) 1.39 + i = i / modulus; 1.40 + else 1.41 + i = max1 + i - (i + modulus - modulus / 2) / modulus; 1.42 + return i; 1.43 +} 1.44 + 1.45 +static int recenter_nonneg(int v, int m) { 1.46 + if (v > (m << 1)) 1.47 + return v; 1.48 + else if (v >= m) 1.49 + return ((v - m) << 1); 1.50 + else 1.51 + return ((m - v) << 1) - 1; 1.52 +} 1.53 + 1.54 +static int remap_prob(int v, int m) { 1.55 + int i; 1.56 + static const int map_table[MAX_PROB - 1] = { 1.57 + // generated by: 1.58 + // map_table[j] = split_index(j, MAX_PROB - 1, MODULUS_PARAM); 1.59 + 20, 21, 22, 23, 24, 25, 0, 26, 27, 28, 29, 30, 31, 32, 33, 1.60 + 34, 35, 36, 37, 1, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 1.61 + 48, 49, 2, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 1.62 + 3, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 4, 74, 1.63 + 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 5, 86, 87, 88, 1.64 + 89, 90, 91, 92, 93, 94, 95, 96, 97, 6, 98, 99, 100, 101, 102, 1.65 + 103, 104, 105, 106, 107, 108, 109, 7, 110, 111, 112, 113, 114, 115, 116, 1.66 + 117, 118, 119, 120, 121, 8, 122, 123, 124, 125, 126, 127, 128, 129, 130, 1.67 + 131, 132, 133, 9, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 1.68 + 145, 10, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 11, 1.69 + 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 12, 170, 171, 1.70 + 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 13, 182, 183, 184, 185, 1.71 + 186, 187, 188, 189, 190, 191, 192, 193, 14, 194, 195, 196, 197, 198, 199, 1.72 + 200, 201, 202, 203, 204, 205, 15, 206, 207, 208, 209, 210, 211, 212, 213, 1.73 + 214, 215, 216, 217, 16, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 1.74 + 228, 229, 17, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 1.75 + 18, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 19, 1.76 + }; 1.77 + v--; 1.78 + m--; 1.79 + if ((m << 1) <= MAX_PROB) 1.80 + i = recenter_nonneg(v, m) - 1; 1.81 + else 1.82 + i = recenter_nonneg(MAX_PROB - 1 - v, MAX_PROB - 1 - m) - 1; 1.83 + 1.84 + i = map_table[i]; 1.85 + return i; 1.86 +} 1.87 + 1.88 +static int count_term_subexp(int word, int k, int num_syms) { 1.89 + int count = 0; 1.90 + int i = 0; 1.91 + int mk = 0; 1.92 + while (1) { 1.93 + int b = (i ? k + i - 1 : k); 1.94 + int a = (1 << b); 1.95 + if (num_syms <= mk + 3 * a) { 1.96 + count += count_uniform(word - mk, num_syms - mk); 1.97 + break; 1.98 + } else { 1.99 + int t = (word >= mk + a); 1.100 + count++; 1.101 + if (t) { 1.102 + i = i + 1; 1.103 + mk += a; 1.104 + } else { 1.105 + count += b; 1.106 + break; 1.107 + } 1.108 + } 1.109 + } 1.110 + return count; 1.111 +} 1.112 + 1.113 +static int prob_diff_update_cost(vp9_prob newp, vp9_prob oldp) { 1.114 + int delp = remap_prob(newp, oldp); 1.115 + return update_bits[delp] * 256; 1.116 +} 1.117 + 1.118 +static void encode_uniform(vp9_writer *w, int v, int n) { 1.119 + int l = get_unsigned_bits(n); 1.120 + int m; 1.121 + if (l == 0) 1.122 + return; 1.123 + m = (1 << l) - n; 1.124 + if (v < m) { 1.125 + vp9_write_literal(w, v, l - 1); 1.126 + } else { 1.127 + vp9_write_literal(w, m + ((v - m) >> 1), l - 1); 1.128 + vp9_write_literal(w, (v - m) & 1, 1); 1.129 + } 1.130 +} 1.131 + 1.132 +static void encode_term_subexp(vp9_writer *w, int word, int k, int num_syms) { 1.133 + int i = 0; 1.134 + int mk = 0; 1.135 + while (1) { 1.136 + int b = (i ? k + i - 1 : k); 1.137 + int a = (1 << b); 1.138 + if (num_syms <= mk + 3 * a) { 1.139 + encode_uniform(w, word - mk, num_syms - mk); 1.140 + break; 1.141 + } else { 1.142 + int t = (word >= mk + a); 1.143 + vp9_write_literal(w, t, 1); 1.144 + if (t) { 1.145 + i = i + 1; 1.146 + mk += a; 1.147 + } else { 1.148 + vp9_write_literal(w, word - mk, b); 1.149 + break; 1.150 + } 1.151 + } 1.152 + } 1.153 +} 1.154 + 1.155 +void vp9_write_prob_diff_update(vp9_writer *w, vp9_prob newp, vp9_prob oldp) { 1.156 + const int delp = remap_prob(newp, oldp); 1.157 + encode_term_subexp(w, delp, SUBEXP_PARAM, 255); 1.158 +} 1.159 + 1.160 +void vp9_compute_update_table() { 1.161 + int i; 1.162 + for (i = 0; i < 254; i++) 1.163 + update_bits[i] = count_term_subexp(i, SUBEXP_PARAM, 255); 1.164 +} 1.165 + 1.166 +int vp9_prob_diff_update_savings_search(const unsigned int *ct, 1.167 + vp9_prob oldp, vp9_prob *bestp, 1.168 + vp9_prob upd) { 1.169 + const int old_b = cost_branch256(ct, oldp); 1.170 + int bestsavings = 0; 1.171 + vp9_prob newp, bestnewp = oldp; 1.172 + const int step = *bestp > oldp ? -1 : 1; 1.173 + 1.174 + for (newp = *bestp; newp != oldp; newp += step) { 1.175 + const int new_b = cost_branch256(ct, newp); 1.176 + const int update_b = prob_diff_update_cost(newp, oldp) + vp9_cost_upd256; 1.177 + const int savings = old_b - new_b - update_b; 1.178 + if (savings > bestsavings) { 1.179 + bestsavings = savings; 1.180 + bestnewp = newp; 1.181 + } 1.182 + } 1.183 + *bestp = bestnewp; 1.184 + return bestsavings; 1.185 +} 1.186 + 1.187 +int vp9_prob_diff_update_savings_search_model(const unsigned int *ct, 1.188 + const vp9_prob *oldp, 1.189 + vp9_prob *bestp, 1.190 + vp9_prob upd, 1.191 + int b, int r) { 1.192 + int i, old_b, new_b, update_b, savings, bestsavings, step; 1.193 + int newp; 1.194 + vp9_prob bestnewp, newplist[ENTROPY_NODES], oldplist[ENTROPY_NODES]; 1.195 + vp9_model_to_full_probs(oldp, oldplist); 1.196 + vpx_memcpy(newplist, oldp, sizeof(vp9_prob) * UNCONSTRAINED_NODES); 1.197 + for (i = UNCONSTRAINED_NODES, old_b = 0; i < ENTROPY_NODES; ++i) 1.198 + old_b += cost_branch256(ct + 2 * i, oldplist[i]); 1.199 + old_b += cost_branch256(ct + 2 * PIVOT_NODE, oldplist[PIVOT_NODE]); 1.200 + 1.201 + bestsavings = 0; 1.202 + bestnewp = oldp[PIVOT_NODE]; 1.203 + 1.204 + step = (*bestp > oldp[PIVOT_NODE] ? -1 : 1); 1.205 + 1.206 + for (newp = *bestp; newp != oldp[PIVOT_NODE]; newp += step) { 1.207 + if (newp < 1 || newp > 255) 1.208 + continue; 1.209 + newplist[PIVOT_NODE] = newp; 1.210 + vp9_model_to_full_probs(newplist, newplist); 1.211 + for (i = UNCONSTRAINED_NODES, new_b = 0; i < ENTROPY_NODES; ++i) 1.212 + new_b += cost_branch256(ct + 2 * i, newplist[i]); 1.213 + new_b += cost_branch256(ct + 2 * PIVOT_NODE, newplist[PIVOT_NODE]); 1.214 + update_b = prob_diff_update_cost(newp, oldp[PIVOT_NODE]) + 1.215 + vp9_cost_upd256; 1.216 + savings = old_b - new_b - update_b; 1.217 + if (savings > bestsavings) { 1.218 + bestsavings = savings; 1.219 + bestnewp = newp; 1.220 + } 1.221 + } 1.222 + *bestp = bestnewp; 1.223 + return bestsavings; 1.224 +} 1.225 + 1.226 +void vp9_cond_prob_diff_update(vp9_writer *w, vp9_prob *oldp, 1.227 + const unsigned int ct[2]) { 1.228 + const vp9_prob upd = DIFF_UPDATE_PROB; 1.229 + vp9_prob newp = get_binary_prob(ct[0], ct[1]); 1.230 + const int savings = vp9_prob_diff_update_savings_search(ct, *oldp, &newp, 1.231 + upd); 1.232 + assert(newp >= 1); 1.233 + if (savings > 0) { 1.234 + vp9_write(w, 1, upd); 1.235 + vp9_write_prob_diff_update(w, newp, *oldp); 1.236 + *oldp = newp; 1.237 + } else { 1.238 + vp9_write(w, 0, upd); 1.239 + } 1.240 +}