media/libvpx/vp9/encoder/vp9_subexp.c

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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

mercurial