Thu, 15 Jan 2015 15:59:08 +0100
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.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (c) 2010 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 | |
michael@0 | 12 | #include "vp8/common/common.h" |
michael@0 | 13 | #include "encodemv.h" |
michael@0 | 14 | #include "vp8/common/entropymode.h" |
michael@0 | 15 | #include "vp8/common/systemdependent.h" |
michael@0 | 16 | |
michael@0 | 17 | #include <math.h> |
michael@0 | 18 | |
michael@0 | 19 | #ifdef VP8_ENTROPY_STATS |
michael@0 | 20 | extern unsigned int active_section; |
michael@0 | 21 | #endif |
michael@0 | 22 | |
michael@0 | 23 | static void encode_mvcomponent( |
michael@0 | 24 | vp8_writer *const w, |
michael@0 | 25 | const int v, |
michael@0 | 26 | const struct mv_context *mvc |
michael@0 | 27 | ) |
michael@0 | 28 | { |
michael@0 | 29 | const vp8_prob *p = mvc->prob; |
michael@0 | 30 | const int x = v < 0 ? -v : v; |
michael@0 | 31 | |
michael@0 | 32 | if (x < mvnum_short) /* Small */ |
michael@0 | 33 | { |
michael@0 | 34 | vp8_write(w, 0, p [mvpis_short]); |
michael@0 | 35 | vp8_treed_write(w, vp8_small_mvtree, p + MVPshort, x, 3); |
michael@0 | 36 | |
michael@0 | 37 | if (!x) |
michael@0 | 38 | return; /* no sign bit */ |
michael@0 | 39 | } |
michael@0 | 40 | else /* Large */ |
michael@0 | 41 | { |
michael@0 | 42 | int i = 0; |
michael@0 | 43 | |
michael@0 | 44 | vp8_write(w, 1, p [mvpis_short]); |
michael@0 | 45 | |
michael@0 | 46 | do |
michael@0 | 47 | vp8_write(w, (x >> i) & 1, p [MVPbits + i]); |
michael@0 | 48 | |
michael@0 | 49 | while (++i < 3); |
michael@0 | 50 | |
michael@0 | 51 | i = mvlong_width - 1; /* Skip bit 3, which is sometimes implicit */ |
michael@0 | 52 | |
michael@0 | 53 | do |
michael@0 | 54 | vp8_write(w, (x >> i) & 1, p [MVPbits + i]); |
michael@0 | 55 | |
michael@0 | 56 | while (--i > 3); |
michael@0 | 57 | |
michael@0 | 58 | if (x & 0xFFF0) |
michael@0 | 59 | vp8_write(w, (x >> 3) & 1, p [MVPbits + 3]); |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | vp8_write(w, v < 0, p [MVPsign]); |
michael@0 | 63 | } |
michael@0 | 64 | #if 0 |
michael@0 | 65 | static int max_mv_r = 0; |
michael@0 | 66 | static int max_mv_c = 0; |
michael@0 | 67 | #endif |
michael@0 | 68 | void vp8_encode_motion_vector(vp8_writer *w, const MV *mv, const MV_CONTEXT *mvc) |
michael@0 | 69 | { |
michael@0 | 70 | |
michael@0 | 71 | #if 0 |
michael@0 | 72 | { |
michael@0 | 73 | if (abs(mv->row >> 1) > max_mv_r) |
michael@0 | 74 | { |
michael@0 | 75 | FILE *f = fopen("maxmv.stt", "a"); |
michael@0 | 76 | max_mv_r = abs(mv->row >> 1); |
michael@0 | 77 | fprintf(f, "New Mv Row Max %6d\n", (mv->row >> 1)); |
michael@0 | 78 | |
michael@0 | 79 | if ((abs(mv->row) / 2) != max_mv_r) |
michael@0 | 80 | fprintf(f, "MV Row conversion error %6d\n", abs(mv->row) / 2); |
michael@0 | 81 | |
michael@0 | 82 | fclose(f); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | if (abs(mv->col >> 1) > max_mv_c) |
michael@0 | 86 | { |
michael@0 | 87 | FILE *f = fopen("maxmv.stt", "a"); |
michael@0 | 88 | fprintf(f, "New Mv Col Max %6d\n", (mv->col >> 1)); |
michael@0 | 89 | max_mv_c = abs(mv->col >> 1); |
michael@0 | 90 | fclose(f); |
michael@0 | 91 | } |
michael@0 | 92 | } |
michael@0 | 93 | #endif |
michael@0 | 94 | |
michael@0 | 95 | encode_mvcomponent(w, mv->row >> 1, &mvc[0]); |
michael@0 | 96 | encode_mvcomponent(w, mv->col >> 1, &mvc[1]); |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | |
michael@0 | 100 | static unsigned int cost_mvcomponent(const int v, const struct mv_context *mvc) |
michael@0 | 101 | { |
michael@0 | 102 | const vp8_prob *p = mvc->prob; |
michael@0 | 103 | const int x = v; |
michael@0 | 104 | unsigned int cost; |
michael@0 | 105 | |
michael@0 | 106 | if (x < mvnum_short) |
michael@0 | 107 | { |
michael@0 | 108 | cost = vp8_cost_zero(p [mvpis_short]) |
michael@0 | 109 | + vp8_treed_cost(vp8_small_mvtree, p + MVPshort, x, 3); |
michael@0 | 110 | |
michael@0 | 111 | if (!x) |
michael@0 | 112 | return cost; |
michael@0 | 113 | } |
michael@0 | 114 | else |
michael@0 | 115 | { |
michael@0 | 116 | int i = 0; |
michael@0 | 117 | cost = vp8_cost_one(p [mvpis_short]); |
michael@0 | 118 | |
michael@0 | 119 | do |
michael@0 | 120 | cost += vp8_cost_bit(p [MVPbits + i], (x >> i) & 1); |
michael@0 | 121 | |
michael@0 | 122 | while (++i < 3); |
michael@0 | 123 | |
michael@0 | 124 | i = mvlong_width - 1; /* Skip bit 3, which is sometimes implicit */ |
michael@0 | 125 | |
michael@0 | 126 | do |
michael@0 | 127 | cost += vp8_cost_bit(p [MVPbits + i], (x >> i) & 1); |
michael@0 | 128 | |
michael@0 | 129 | while (--i > 3); |
michael@0 | 130 | |
michael@0 | 131 | if (x & 0xFFF0) |
michael@0 | 132 | cost += vp8_cost_bit(p [MVPbits + 3], (x >> 3) & 1); |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | return cost; /* + vp8_cost_bit( p [MVPsign], v < 0); */ |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | void vp8_build_component_cost_table(int *mvcost[2], const MV_CONTEXT *mvc, int mvc_flag[2]) |
michael@0 | 139 | { |
michael@0 | 140 | int i = 1; |
michael@0 | 141 | unsigned int cost0 = 0; |
michael@0 | 142 | unsigned int cost1 = 0; |
michael@0 | 143 | |
michael@0 | 144 | vp8_clear_system_state(); |
michael@0 | 145 | |
michael@0 | 146 | i = 1; |
michael@0 | 147 | |
michael@0 | 148 | if (mvc_flag[0]) |
michael@0 | 149 | { |
michael@0 | 150 | mvcost [0] [0] = cost_mvcomponent(0, &mvc[0]); |
michael@0 | 151 | |
michael@0 | 152 | do |
michael@0 | 153 | { |
michael@0 | 154 | cost0 = cost_mvcomponent(i, &mvc[0]); |
michael@0 | 155 | |
michael@0 | 156 | mvcost [0] [i] = cost0 + vp8_cost_zero(mvc[0].prob[MVPsign]); |
michael@0 | 157 | mvcost [0] [-i] = cost0 + vp8_cost_one(mvc[0].prob[MVPsign]); |
michael@0 | 158 | } |
michael@0 | 159 | while (++i <= mv_max); |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | i = 1; |
michael@0 | 163 | |
michael@0 | 164 | if (mvc_flag[1]) |
michael@0 | 165 | { |
michael@0 | 166 | mvcost [1] [0] = cost_mvcomponent(0, &mvc[1]); |
michael@0 | 167 | |
michael@0 | 168 | do |
michael@0 | 169 | { |
michael@0 | 170 | cost1 = cost_mvcomponent(i, &mvc[1]); |
michael@0 | 171 | |
michael@0 | 172 | mvcost [1] [i] = cost1 + vp8_cost_zero(mvc[1].prob[MVPsign]); |
michael@0 | 173 | mvcost [1] [-i] = cost1 + vp8_cost_one(mvc[1].prob[MVPsign]); |
michael@0 | 174 | } |
michael@0 | 175 | while (++i <= mv_max); |
michael@0 | 176 | } |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | |
michael@0 | 180 | /* Motion vector probability table update depends on benefit. |
michael@0 | 181 | * Small correction allows for the fact that an update to an MV probability |
michael@0 | 182 | * may have benefit in subsequent frames as well as the current one. |
michael@0 | 183 | */ |
michael@0 | 184 | #define MV_PROB_UPDATE_CORRECTION -1 |
michael@0 | 185 | |
michael@0 | 186 | |
michael@0 | 187 | static void calc_prob(vp8_prob *p, const unsigned int ct[2]) |
michael@0 | 188 | { |
michael@0 | 189 | const unsigned int tot = ct[0] + ct[1]; |
michael@0 | 190 | |
michael@0 | 191 | if (tot) |
michael@0 | 192 | { |
michael@0 | 193 | const vp8_prob x = ((ct[0] * 255) / tot) & -2; |
michael@0 | 194 | *p = x ? x : 1; |
michael@0 | 195 | } |
michael@0 | 196 | } |
michael@0 | 197 | |
michael@0 | 198 | static void update( |
michael@0 | 199 | vp8_writer *const w, |
michael@0 | 200 | const unsigned int ct[2], |
michael@0 | 201 | vp8_prob *const cur_p, |
michael@0 | 202 | const vp8_prob new_p, |
michael@0 | 203 | const vp8_prob update_p, |
michael@0 | 204 | int *updated |
michael@0 | 205 | ) |
michael@0 | 206 | { |
michael@0 | 207 | const int cur_b = vp8_cost_branch(ct, *cur_p); |
michael@0 | 208 | const int new_b = vp8_cost_branch(ct, new_p); |
michael@0 | 209 | const int cost = 7 + MV_PROB_UPDATE_CORRECTION + ((vp8_cost_one(update_p) - vp8_cost_zero(update_p) + 128) >> 8); |
michael@0 | 210 | |
michael@0 | 211 | if (cur_b - new_b > cost) |
michael@0 | 212 | { |
michael@0 | 213 | *cur_p = new_p; |
michael@0 | 214 | vp8_write(w, 1, update_p); |
michael@0 | 215 | vp8_write_literal(w, new_p >> 1, 7); |
michael@0 | 216 | *updated = 1; |
michael@0 | 217 | |
michael@0 | 218 | } |
michael@0 | 219 | else |
michael@0 | 220 | vp8_write(w, 0, update_p); |
michael@0 | 221 | } |
michael@0 | 222 | |
michael@0 | 223 | static void write_component_probs( |
michael@0 | 224 | vp8_writer *const w, |
michael@0 | 225 | struct mv_context *cur_mvc, |
michael@0 | 226 | const struct mv_context *default_mvc_, |
michael@0 | 227 | const struct mv_context *update_mvc, |
michael@0 | 228 | const unsigned int events [MVvals], |
michael@0 | 229 | unsigned int rc, |
michael@0 | 230 | int *updated |
michael@0 | 231 | ) |
michael@0 | 232 | { |
michael@0 | 233 | vp8_prob *Pcur = cur_mvc->prob; |
michael@0 | 234 | const vp8_prob *default_mvc = default_mvc_->prob; |
michael@0 | 235 | const vp8_prob *Pupdate = update_mvc->prob; |
michael@0 | 236 | unsigned int is_short_ct[2], sign_ct[2]; |
michael@0 | 237 | |
michael@0 | 238 | unsigned int bit_ct [mvlong_width] [2]; |
michael@0 | 239 | |
michael@0 | 240 | unsigned int short_ct [mvnum_short]; |
michael@0 | 241 | unsigned int short_bct [mvnum_short-1] [2]; |
michael@0 | 242 | |
michael@0 | 243 | vp8_prob Pnew [MVPcount]; |
michael@0 | 244 | |
michael@0 | 245 | (void) rc; |
michael@0 | 246 | vp8_copy_array(Pnew, default_mvc, MVPcount); |
michael@0 | 247 | |
michael@0 | 248 | vp8_zero(is_short_ct) |
michael@0 | 249 | vp8_zero(sign_ct) |
michael@0 | 250 | vp8_zero(bit_ct) |
michael@0 | 251 | vp8_zero(short_ct) |
michael@0 | 252 | vp8_zero(short_bct) |
michael@0 | 253 | |
michael@0 | 254 | |
michael@0 | 255 | /* j=0 */ |
michael@0 | 256 | { |
michael@0 | 257 | const int c = events [mv_max]; |
michael@0 | 258 | |
michael@0 | 259 | is_short_ct [0] += c; /* Short vector */ |
michael@0 | 260 | short_ct [0] += c; /* Magnitude distribution */ |
michael@0 | 261 | } |
michael@0 | 262 | |
michael@0 | 263 | /* j: 1 ~ mv_max (1023) */ |
michael@0 | 264 | { |
michael@0 | 265 | int j = 1; |
michael@0 | 266 | |
michael@0 | 267 | do |
michael@0 | 268 | { |
michael@0 | 269 | const int c1 = events [mv_max + j]; /* positive */ |
michael@0 | 270 | const int c2 = events [mv_max - j]; /* negative */ |
michael@0 | 271 | const int c = c1 + c2; |
michael@0 | 272 | int a = j; |
michael@0 | 273 | |
michael@0 | 274 | sign_ct [0] += c1; |
michael@0 | 275 | sign_ct [1] += c2; |
michael@0 | 276 | |
michael@0 | 277 | if (a < mvnum_short) |
michael@0 | 278 | { |
michael@0 | 279 | is_short_ct [0] += c; /* Short vector */ |
michael@0 | 280 | short_ct [a] += c; /* Magnitude distribution */ |
michael@0 | 281 | } |
michael@0 | 282 | else |
michael@0 | 283 | { |
michael@0 | 284 | int k = mvlong_width - 1; |
michael@0 | 285 | is_short_ct [1] += c; /* Long vector */ |
michael@0 | 286 | |
michael@0 | 287 | /* bit 3 not always encoded. */ |
michael@0 | 288 | do |
michael@0 | 289 | bit_ct [k] [(a >> k) & 1] += c; |
michael@0 | 290 | |
michael@0 | 291 | while (--k >= 0); |
michael@0 | 292 | } |
michael@0 | 293 | } |
michael@0 | 294 | while (++j <= mv_max); |
michael@0 | 295 | } |
michael@0 | 296 | |
michael@0 | 297 | calc_prob(Pnew + mvpis_short, is_short_ct); |
michael@0 | 298 | |
michael@0 | 299 | calc_prob(Pnew + MVPsign, sign_ct); |
michael@0 | 300 | |
michael@0 | 301 | { |
michael@0 | 302 | vp8_prob p [mvnum_short - 1]; /* actually only need branch ct */ |
michael@0 | 303 | int j = 0; |
michael@0 | 304 | |
michael@0 | 305 | vp8_tree_probs_from_distribution( |
michael@0 | 306 | 8, vp8_small_mvencodings, vp8_small_mvtree, |
michael@0 | 307 | p, short_bct, short_ct, |
michael@0 | 308 | 256, 1 |
michael@0 | 309 | ); |
michael@0 | 310 | |
michael@0 | 311 | do |
michael@0 | 312 | calc_prob(Pnew + MVPshort + j, short_bct[j]); |
michael@0 | 313 | |
michael@0 | 314 | while (++j < mvnum_short - 1); |
michael@0 | 315 | } |
michael@0 | 316 | |
michael@0 | 317 | { |
michael@0 | 318 | int j = 0; |
michael@0 | 319 | |
michael@0 | 320 | do |
michael@0 | 321 | calc_prob(Pnew + MVPbits + j, bit_ct[j]); |
michael@0 | 322 | |
michael@0 | 323 | while (++j < mvlong_width); |
michael@0 | 324 | } |
michael@0 | 325 | |
michael@0 | 326 | update(w, is_short_ct, Pcur + mvpis_short, Pnew[mvpis_short], *Pupdate++, updated); |
michael@0 | 327 | |
michael@0 | 328 | update(w, sign_ct, Pcur + MVPsign, Pnew[MVPsign], *Pupdate++, updated); |
michael@0 | 329 | |
michael@0 | 330 | { |
michael@0 | 331 | const vp8_prob *const new_p = Pnew + MVPshort; |
michael@0 | 332 | vp8_prob *const cur_p = Pcur + MVPshort; |
michael@0 | 333 | |
michael@0 | 334 | int j = 0; |
michael@0 | 335 | |
michael@0 | 336 | do |
michael@0 | 337 | |
michael@0 | 338 | update(w, short_bct[j], cur_p + j, new_p[j], *Pupdate++, updated); |
michael@0 | 339 | |
michael@0 | 340 | while (++j < mvnum_short - 1); |
michael@0 | 341 | } |
michael@0 | 342 | |
michael@0 | 343 | { |
michael@0 | 344 | const vp8_prob *const new_p = Pnew + MVPbits; |
michael@0 | 345 | vp8_prob *const cur_p = Pcur + MVPbits; |
michael@0 | 346 | |
michael@0 | 347 | int j = 0; |
michael@0 | 348 | |
michael@0 | 349 | do |
michael@0 | 350 | |
michael@0 | 351 | update(w, bit_ct[j], cur_p + j, new_p[j], *Pupdate++, updated); |
michael@0 | 352 | |
michael@0 | 353 | while (++j < mvlong_width); |
michael@0 | 354 | } |
michael@0 | 355 | } |
michael@0 | 356 | |
michael@0 | 357 | void vp8_write_mvprobs(VP8_COMP *cpi) |
michael@0 | 358 | { |
michael@0 | 359 | vp8_writer *const w = cpi->bc; |
michael@0 | 360 | MV_CONTEXT *mvc = cpi->common.fc.mvc; |
michael@0 | 361 | int flags[2] = {0, 0}; |
michael@0 | 362 | #ifdef VP8_ENTROPY_STATS |
michael@0 | 363 | active_section = 4; |
michael@0 | 364 | #endif |
michael@0 | 365 | write_component_probs( |
michael@0 | 366 | w, &mvc[0], &vp8_default_mv_context[0], &vp8_mv_update_probs[0], |
michael@0 | 367 | cpi->mb.MVcount[0], 0, &flags[0] |
michael@0 | 368 | ); |
michael@0 | 369 | write_component_probs( |
michael@0 | 370 | w, &mvc[1], &vp8_default_mv_context[1], &vp8_mv_update_probs[1], |
michael@0 | 371 | cpi->mb.MVcount[1], 1, &flags[1] |
michael@0 | 372 | ); |
michael@0 | 373 | |
michael@0 | 374 | if (flags[0] || flags[1]) |
michael@0 | 375 | vp8_build_component_cost_table(cpi->mb.mvcost, (const MV_CONTEXT *) cpi->common.fc.mvc, flags); |
michael@0 | 376 | |
michael@0 | 377 | #ifdef VP8_ENTROPY_STATS |
michael@0 | 378 | active_section = 5; |
michael@0 | 379 | #endif |
michael@0 | 380 | } |