media/libvpx/vp9/encoder/vp9_subexp.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /*
michael@0 2 * Copyright (c) 2013 The WebM project authors. All Rights Reserved.
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license
michael@0 5 * that can be found in the LICENSE file in the root of the source
michael@0 6 * tree. An additional intellectual property rights grant can be found
michael@0 7 * in the file PATENTS. All contributing project authors may
michael@0 8 * be found in the AUTHORS file in the root of the source tree.
michael@0 9 */
michael@0 10
michael@0 11 #include "vp9/common/vp9_common.h"
michael@0 12 #include "vp9/common/vp9_entropy.h"
michael@0 13
michael@0 14 #include "vp9/encoder/vp9_boolhuff.h"
michael@0 15 #include "vp9/encoder/vp9_treewriter.h"
michael@0 16
michael@0 17 #define vp9_cost_upd ((int)(vp9_cost_one(upd) - vp9_cost_zero(upd)) >> 8)
michael@0 18 #define vp9_cost_upd256 ((int)(vp9_cost_one(upd) - vp9_cost_zero(upd)))
michael@0 19
michael@0 20 static int update_bits[255];
michael@0 21
michael@0 22 static int count_uniform(int v, int n) {
michael@0 23 int l = get_unsigned_bits(n);
michael@0 24 int m;
michael@0 25 if (l == 0) return 0;
michael@0 26 m = (1 << l) - n;
michael@0 27 if (v < m)
michael@0 28 return l - 1;
michael@0 29 else
michael@0 30 return l;
michael@0 31 }
michael@0 32
michael@0 33 static int split_index(int i, int n, int modulus) {
michael@0 34 int max1 = (n - 1 - modulus / 2) / modulus + 1;
michael@0 35 if (i % modulus == modulus / 2)
michael@0 36 i = i / modulus;
michael@0 37 else
michael@0 38 i = max1 + i - (i + modulus - modulus / 2) / modulus;
michael@0 39 return i;
michael@0 40 }
michael@0 41
michael@0 42 static int recenter_nonneg(int v, int m) {
michael@0 43 if (v > (m << 1))
michael@0 44 return v;
michael@0 45 else if (v >= m)
michael@0 46 return ((v - m) << 1);
michael@0 47 else
michael@0 48 return ((m - v) << 1) - 1;
michael@0 49 }
michael@0 50
michael@0 51 static int remap_prob(int v, int m) {
michael@0 52 int i;
michael@0 53 static const int map_table[MAX_PROB - 1] = {
michael@0 54 // generated by:
michael@0 55 // map_table[j] = split_index(j, MAX_PROB - 1, MODULUS_PARAM);
michael@0 56 20, 21, 22, 23, 24, 25, 0, 26, 27, 28, 29, 30, 31, 32, 33,
michael@0 57 34, 35, 36, 37, 1, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
michael@0 58 48, 49, 2, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
michael@0 59 3, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 4, 74,
michael@0 60 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 5, 86, 87, 88,
michael@0 61 89, 90, 91, 92, 93, 94, 95, 96, 97, 6, 98, 99, 100, 101, 102,
michael@0 62 103, 104, 105, 106, 107, 108, 109, 7, 110, 111, 112, 113, 114, 115, 116,
michael@0 63 117, 118, 119, 120, 121, 8, 122, 123, 124, 125, 126, 127, 128, 129, 130,
michael@0 64 131, 132, 133, 9, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144,
michael@0 65 145, 10, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 11,
michael@0 66 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 12, 170, 171,
michael@0 67 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 13, 182, 183, 184, 185,
michael@0 68 186, 187, 188, 189, 190, 191, 192, 193, 14, 194, 195, 196, 197, 198, 199,
michael@0 69 200, 201, 202, 203, 204, 205, 15, 206, 207, 208, 209, 210, 211, 212, 213,
michael@0 70 214, 215, 216, 217, 16, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227,
michael@0 71 228, 229, 17, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241,
michael@0 72 18, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 19,
michael@0 73 };
michael@0 74 v--;
michael@0 75 m--;
michael@0 76 if ((m << 1) <= MAX_PROB)
michael@0 77 i = recenter_nonneg(v, m) - 1;
michael@0 78 else
michael@0 79 i = recenter_nonneg(MAX_PROB - 1 - v, MAX_PROB - 1 - m) - 1;
michael@0 80
michael@0 81 i = map_table[i];
michael@0 82 return i;
michael@0 83 }
michael@0 84
michael@0 85 static int count_term_subexp(int word, int k, int num_syms) {
michael@0 86 int count = 0;
michael@0 87 int i = 0;
michael@0 88 int mk = 0;
michael@0 89 while (1) {
michael@0 90 int b = (i ? k + i - 1 : k);
michael@0 91 int a = (1 << b);
michael@0 92 if (num_syms <= mk + 3 * a) {
michael@0 93 count += count_uniform(word - mk, num_syms - mk);
michael@0 94 break;
michael@0 95 } else {
michael@0 96 int t = (word >= mk + a);
michael@0 97 count++;
michael@0 98 if (t) {
michael@0 99 i = i + 1;
michael@0 100 mk += a;
michael@0 101 } else {
michael@0 102 count += b;
michael@0 103 break;
michael@0 104 }
michael@0 105 }
michael@0 106 }
michael@0 107 return count;
michael@0 108 }
michael@0 109
michael@0 110 static int prob_diff_update_cost(vp9_prob newp, vp9_prob oldp) {
michael@0 111 int delp = remap_prob(newp, oldp);
michael@0 112 return update_bits[delp] * 256;
michael@0 113 }
michael@0 114
michael@0 115 static void encode_uniform(vp9_writer *w, int v, int n) {
michael@0 116 int l = get_unsigned_bits(n);
michael@0 117 int m;
michael@0 118 if (l == 0)
michael@0 119 return;
michael@0 120 m = (1 << l) - n;
michael@0 121 if (v < m) {
michael@0 122 vp9_write_literal(w, v, l - 1);
michael@0 123 } else {
michael@0 124 vp9_write_literal(w, m + ((v - m) >> 1), l - 1);
michael@0 125 vp9_write_literal(w, (v - m) & 1, 1);
michael@0 126 }
michael@0 127 }
michael@0 128
michael@0 129 static void encode_term_subexp(vp9_writer *w, int word, int k, int num_syms) {
michael@0 130 int i = 0;
michael@0 131 int mk = 0;
michael@0 132 while (1) {
michael@0 133 int b = (i ? k + i - 1 : k);
michael@0 134 int a = (1 << b);
michael@0 135 if (num_syms <= mk + 3 * a) {
michael@0 136 encode_uniform(w, word - mk, num_syms - mk);
michael@0 137 break;
michael@0 138 } else {
michael@0 139 int t = (word >= mk + a);
michael@0 140 vp9_write_literal(w, t, 1);
michael@0 141 if (t) {
michael@0 142 i = i + 1;
michael@0 143 mk += a;
michael@0 144 } else {
michael@0 145 vp9_write_literal(w, word - mk, b);
michael@0 146 break;
michael@0 147 }
michael@0 148 }
michael@0 149 }
michael@0 150 }
michael@0 151
michael@0 152 void vp9_write_prob_diff_update(vp9_writer *w, vp9_prob newp, vp9_prob oldp) {
michael@0 153 const int delp = remap_prob(newp, oldp);
michael@0 154 encode_term_subexp(w, delp, SUBEXP_PARAM, 255);
michael@0 155 }
michael@0 156
michael@0 157 void vp9_compute_update_table() {
michael@0 158 int i;
michael@0 159 for (i = 0; i < 254; i++)
michael@0 160 update_bits[i] = count_term_subexp(i, SUBEXP_PARAM, 255);
michael@0 161 }
michael@0 162
michael@0 163 int vp9_prob_diff_update_savings_search(const unsigned int *ct,
michael@0 164 vp9_prob oldp, vp9_prob *bestp,
michael@0 165 vp9_prob upd) {
michael@0 166 const int old_b = cost_branch256(ct, oldp);
michael@0 167 int bestsavings = 0;
michael@0 168 vp9_prob newp, bestnewp = oldp;
michael@0 169 const int step = *bestp > oldp ? -1 : 1;
michael@0 170
michael@0 171 for (newp = *bestp; newp != oldp; newp += step) {
michael@0 172 const int new_b = cost_branch256(ct, newp);
michael@0 173 const int update_b = prob_diff_update_cost(newp, oldp) + vp9_cost_upd256;
michael@0 174 const int savings = old_b - new_b - update_b;
michael@0 175 if (savings > bestsavings) {
michael@0 176 bestsavings = savings;
michael@0 177 bestnewp = newp;
michael@0 178 }
michael@0 179 }
michael@0 180 *bestp = bestnewp;
michael@0 181 return bestsavings;
michael@0 182 }
michael@0 183
michael@0 184 int vp9_prob_diff_update_savings_search_model(const unsigned int *ct,
michael@0 185 const vp9_prob *oldp,
michael@0 186 vp9_prob *bestp,
michael@0 187 vp9_prob upd,
michael@0 188 int b, int r) {
michael@0 189 int i, old_b, new_b, update_b, savings, bestsavings, step;
michael@0 190 int newp;
michael@0 191 vp9_prob bestnewp, newplist[ENTROPY_NODES], oldplist[ENTROPY_NODES];
michael@0 192 vp9_model_to_full_probs(oldp, oldplist);
michael@0 193 vpx_memcpy(newplist, oldp, sizeof(vp9_prob) * UNCONSTRAINED_NODES);
michael@0 194 for (i = UNCONSTRAINED_NODES, old_b = 0; i < ENTROPY_NODES; ++i)
michael@0 195 old_b += cost_branch256(ct + 2 * i, oldplist[i]);
michael@0 196 old_b += cost_branch256(ct + 2 * PIVOT_NODE, oldplist[PIVOT_NODE]);
michael@0 197
michael@0 198 bestsavings = 0;
michael@0 199 bestnewp = oldp[PIVOT_NODE];
michael@0 200
michael@0 201 step = (*bestp > oldp[PIVOT_NODE] ? -1 : 1);
michael@0 202
michael@0 203 for (newp = *bestp; newp != oldp[PIVOT_NODE]; newp += step) {
michael@0 204 if (newp < 1 || newp > 255)
michael@0 205 continue;
michael@0 206 newplist[PIVOT_NODE] = newp;
michael@0 207 vp9_model_to_full_probs(newplist, newplist);
michael@0 208 for (i = UNCONSTRAINED_NODES, new_b = 0; i < ENTROPY_NODES; ++i)
michael@0 209 new_b += cost_branch256(ct + 2 * i, newplist[i]);
michael@0 210 new_b += cost_branch256(ct + 2 * PIVOT_NODE, newplist[PIVOT_NODE]);
michael@0 211 update_b = prob_diff_update_cost(newp, oldp[PIVOT_NODE]) +
michael@0 212 vp9_cost_upd256;
michael@0 213 savings = old_b - new_b - update_b;
michael@0 214 if (savings > bestsavings) {
michael@0 215 bestsavings = savings;
michael@0 216 bestnewp = newp;
michael@0 217 }
michael@0 218 }
michael@0 219 *bestp = bestnewp;
michael@0 220 return bestsavings;
michael@0 221 }
michael@0 222
michael@0 223 void vp9_cond_prob_diff_update(vp9_writer *w, vp9_prob *oldp,
michael@0 224 const unsigned int ct[2]) {
michael@0 225 const vp9_prob upd = DIFF_UPDATE_PROB;
michael@0 226 vp9_prob newp = get_binary_prob(ct[0], ct[1]);
michael@0 227 const int savings = vp9_prob_diff_update_savings_search(ct, *oldp, &newp,
michael@0 228 upd);
michael@0 229 assert(newp >= 1);
michael@0 230 if (savings > 0) {
michael@0 231 vp9_write(w, 1, upd);
michael@0 232 vp9_write_prob_diff_update(w, newp, *oldp);
michael@0 233 *oldp = newp;
michael@0 234 } else {
michael@0 235 vp9_write(w, 0, upd);
michael@0 236 }
michael@0 237 }

mercurial