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/header.h" |
michael@0 | 13 | #include "encodemv.h" |
michael@0 | 14 | #include "vp8/common/entropymode.h" |
michael@0 | 15 | #include "vp8/common/findnearmv.h" |
michael@0 | 16 | #include "mcomp.h" |
michael@0 | 17 | #include "vp8/common/systemdependent.h" |
michael@0 | 18 | #include <assert.h> |
michael@0 | 19 | #include <stdio.h> |
michael@0 | 20 | #include <limits.h> |
michael@0 | 21 | #include "vp8/common/pragmas.h" |
michael@0 | 22 | #include "vpx/vpx_encoder.h" |
michael@0 | 23 | #include "vpx_mem/vpx_mem.h" |
michael@0 | 24 | #include "bitstream.h" |
michael@0 | 25 | |
michael@0 | 26 | #include "defaultcoefcounts.h" |
michael@0 | 27 | #include "vp8/common/common.h" |
michael@0 | 28 | |
michael@0 | 29 | const int vp8cx_base_skip_false_prob[128] = |
michael@0 | 30 | { |
michael@0 | 31 | 255, 255, 255, 255, 255, 255, 255, 255, |
michael@0 | 32 | 255, 255, 255, 255, 255, 255, 255, 255, |
michael@0 | 33 | 255, 255, 255, 255, 255, 255, 255, 255, |
michael@0 | 34 | 255, 255, 255, 255, 255, 255, 255, 255, |
michael@0 | 35 | 255, 255, 255, 255, 255, 255, 255, 255, |
michael@0 | 36 | 255, 255, 255, 255, 255, 255, 255, 255, |
michael@0 | 37 | 255, 255, 255, 255, 255, 255, 255, 255, |
michael@0 | 38 | 251, 248, 244, 240, 236, 232, 229, 225, |
michael@0 | 39 | 221, 217, 213, 208, 204, 199, 194, 190, |
michael@0 | 40 | 187, 183, 179, 175, 172, 168, 164, 160, |
michael@0 | 41 | 157, 153, 149, 145, 142, 138, 134, 130, |
michael@0 | 42 | 127, 124, 120, 117, 114, 110, 107, 104, |
michael@0 | 43 | 101, 98, 95, 92, 89, 86, 83, 80, |
michael@0 | 44 | 77, 74, 71, 68, 65, 62, 59, 56, |
michael@0 | 45 | 53, 50, 47, 44, 41, 38, 35, 32, |
michael@0 | 46 | 30, 28, 26, 24, 22, 20, 18, 16, |
michael@0 | 47 | }; |
michael@0 | 48 | |
michael@0 | 49 | #if defined(SECTIONBITS_OUTPUT) |
michael@0 | 50 | unsigned __int64 Sectionbits[500]; |
michael@0 | 51 | #endif |
michael@0 | 52 | |
michael@0 | 53 | #ifdef VP8_ENTROPY_STATS |
michael@0 | 54 | int intra_mode_stats[10][10][10]; |
michael@0 | 55 | static unsigned int tree_update_hist [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES] [2]; |
michael@0 | 56 | extern unsigned int active_section; |
michael@0 | 57 | #endif |
michael@0 | 58 | |
michael@0 | 59 | #ifdef MODE_STATS |
michael@0 | 60 | int count_mb_seg[4] = { 0, 0, 0, 0 }; |
michael@0 | 61 | #endif |
michael@0 | 62 | |
michael@0 | 63 | |
michael@0 | 64 | static void update_mode( |
michael@0 | 65 | vp8_writer *const w, |
michael@0 | 66 | int n, |
michael@0 | 67 | vp8_token tok [/* n */], |
michael@0 | 68 | vp8_tree tree, |
michael@0 | 69 | vp8_prob Pnew [/* n-1 */], |
michael@0 | 70 | vp8_prob Pcur [/* n-1 */], |
michael@0 | 71 | unsigned int bct [/* n-1 */] [2], |
michael@0 | 72 | const unsigned int num_events[/* n */] |
michael@0 | 73 | ) |
michael@0 | 74 | { |
michael@0 | 75 | unsigned int new_b = 0, old_b = 0; |
michael@0 | 76 | int i = 0; |
michael@0 | 77 | |
michael@0 | 78 | vp8_tree_probs_from_distribution( |
michael@0 | 79 | n--, tok, tree, |
michael@0 | 80 | Pnew, bct, num_events, |
michael@0 | 81 | 256, 1 |
michael@0 | 82 | ); |
michael@0 | 83 | |
michael@0 | 84 | do |
michael@0 | 85 | { |
michael@0 | 86 | new_b += vp8_cost_branch(bct[i], Pnew[i]); |
michael@0 | 87 | old_b += vp8_cost_branch(bct[i], Pcur[i]); |
michael@0 | 88 | } |
michael@0 | 89 | while (++i < n); |
michael@0 | 90 | |
michael@0 | 91 | if (new_b + (n << 8) < old_b) |
michael@0 | 92 | { |
michael@0 | 93 | int j = 0; |
michael@0 | 94 | |
michael@0 | 95 | vp8_write_bit(w, 1); |
michael@0 | 96 | |
michael@0 | 97 | do |
michael@0 | 98 | { |
michael@0 | 99 | const vp8_prob p = Pnew[j]; |
michael@0 | 100 | |
michael@0 | 101 | vp8_write_literal(w, Pcur[j] = p ? p : 1, 8); |
michael@0 | 102 | } |
michael@0 | 103 | while (++j < n); |
michael@0 | 104 | } |
michael@0 | 105 | else |
michael@0 | 106 | vp8_write_bit(w, 0); |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | static void update_mbintra_mode_probs(VP8_COMP *cpi) |
michael@0 | 110 | { |
michael@0 | 111 | VP8_COMMON *const x = & cpi->common; |
michael@0 | 112 | |
michael@0 | 113 | vp8_writer *const w = cpi->bc; |
michael@0 | 114 | |
michael@0 | 115 | { |
michael@0 | 116 | vp8_prob Pnew [VP8_YMODES-1]; |
michael@0 | 117 | unsigned int bct [VP8_YMODES-1] [2]; |
michael@0 | 118 | |
michael@0 | 119 | update_mode( |
michael@0 | 120 | w, VP8_YMODES, vp8_ymode_encodings, vp8_ymode_tree, |
michael@0 | 121 | Pnew, x->fc.ymode_prob, bct, (unsigned int *)cpi->mb.ymode_count |
michael@0 | 122 | ); |
michael@0 | 123 | } |
michael@0 | 124 | { |
michael@0 | 125 | vp8_prob Pnew [VP8_UV_MODES-1]; |
michael@0 | 126 | unsigned int bct [VP8_UV_MODES-1] [2]; |
michael@0 | 127 | |
michael@0 | 128 | update_mode( |
michael@0 | 129 | w, VP8_UV_MODES, vp8_uv_mode_encodings, vp8_uv_mode_tree, |
michael@0 | 130 | Pnew, x->fc.uv_mode_prob, bct, (unsigned int *)cpi->mb.uv_mode_count |
michael@0 | 131 | ); |
michael@0 | 132 | } |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | static void write_ymode(vp8_writer *bc, int m, const vp8_prob *p) |
michael@0 | 136 | { |
michael@0 | 137 | vp8_write_token(bc, vp8_ymode_tree, p, vp8_ymode_encodings + m); |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | static void kfwrite_ymode(vp8_writer *bc, int m, const vp8_prob *p) |
michael@0 | 141 | { |
michael@0 | 142 | vp8_write_token(bc, vp8_kf_ymode_tree, p, vp8_kf_ymode_encodings + m); |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | static void write_uv_mode(vp8_writer *bc, int m, const vp8_prob *p) |
michael@0 | 146 | { |
michael@0 | 147 | vp8_write_token(bc, vp8_uv_mode_tree, p, vp8_uv_mode_encodings + m); |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | |
michael@0 | 151 | static void write_bmode(vp8_writer *bc, int m, const vp8_prob *p) |
michael@0 | 152 | { |
michael@0 | 153 | vp8_write_token(bc, vp8_bmode_tree, p, vp8_bmode_encodings + m); |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | static void write_split(vp8_writer *bc, int x) |
michael@0 | 157 | { |
michael@0 | 158 | vp8_write_token( |
michael@0 | 159 | bc, vp8_mbsplit_tree, vp8_mbsplit_probs, vp8_mbsplit_encodings + x |
michael@0 | 160 | ); |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | void vp8_pack_tokens_c(vp8_writer *w, const TOKENEXTRA *p, int xcount) |
michael@0 | 164 | { |
michael@0 | 165 | const TOKENEXTRA *stop = p + xcount; |
michael@0 | 166 | unsigned int split; |
michael@0 | 167 | unsigned int shift; |
michael@0 | 168 | int count = w->count; |
michael@0 | 169 | unsigned int range = w->range; |
michael@0 | 170 | unsigned int lowvalue = w->lowvalue; |
michael@0 | 171 | |
michael@0 | 172 | while (p < stop) |
michael@0 | 173 | { |
michael@0 | 174 | const int t = p->Token; |
michael@0 | 175 | vp8_token *a = vp8_coef_encodings + t; |
michael@0 | 176 | const vp8_extra_bit_struct *b = vp8_extra_bits + t; |
michael@0 | 177 | int i = 0; |
michael@0 | 178 | const unsigned char *pp = p->context_tree; |
michael@0 | 179 | int v = a->value; |
michael@0 | 180 | int n = a->Len; |
michael@0 | 181 | |
michael@0 | 182 | if (p->skip_eob_node) |
michael@0 | 183 | { |
michael@0 | 184 | n--; |
michael@0 | 185 | i = 2; |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | do |
michael@0 | 189 | { |
michael@0 | 190 | const int bb = (v >> --n) & 1; |
michael@0 | 191 | split = 1 + (((range - 1) * pp[i>>1]) >> 8); |
michael@0 | 192 | i = vp8_coef_tree[i+bb]; |
michael@0 | 193 | |
michael@0 | 194 | if (bb) |
michael@0 | 195 | { |
michael@0 | 196 | lowvalue += split; |
michael@0 | 197 | range = range - split; |
michael@0 | 198 | } |
michael@0 | 199 | else |
michael@0 | 200 | { |
michael@0 | 201 | range = split; |
michael@0 | 202 | } |
michael@0 | 203 | |
michael@0 | 204 | shift = vp8_norm[range]; |
michael@0 | 205 | range <<= shift; |
michael@0 | 206 | count += shift; |
michael@0 | 207 | |
michael@0 | 208 | if (count >= 0) |
michael@0 | 209 | { |
michael@0 | 210 | int offset = shift - count; |
michael@0 | 211 | |
michael@0 | 212 | if ((lowvalue << (offset - 1)) & 0x80000000) |
michael@0 | 213 | { |
michael@0 | 214 | int x = w->pos - 1; |
michael@0 | 215 | |
michael@0 | 216 | while (x >= 0 && w->buffer[x] == 0xff) |
michael@0 | 217 | { |
michael@0 | 218 | w->buffer[x] = (unsigned char)0; |
michael@0 | 219 | x--; |
michael@0 | 220 | } |
michael@0 | 221 | |
michael@0 | 222 | w->buffer[x] += 1; |
michael@0 | 223 | } |
michael@0 | 224 | |
michael@0 | 225 | validate_buffer(w->buffer + w->pos, |
michael@0 | 226 | 1, |
michael@0 | 227 | w->buffer_end, |
michael@0 | 228 | w->error); |
michael@0 | 229 | |
michael@0 | 230 | w->buffer[w->pos++] = (lowvalue >> (24 - offset)); |
michael@0 | 231 | lowvalue <<= offset; |
michael@0 | 232 | shift = count; |
michael@0 | 233 | lowvalue &= 0xffffff; |
michael@0 | 234 | count -= 8 ; |
michael@0 | 235 | } |
michael@0 | 236 | |
michael@0 | 237 | lowvalue <<= shift; |
michael@0 | 238 | } |
michael@0 | 239 | while (n); |
michael@0 | 240 | |
michael@0 | 241 | |
michael@0 | 242 | if (b->base_val) |
michael@0 | 243 | { |
michael@0 | 244 | const int e = p->Extra, L = b->Len; |
michael@0 | 245 | |
michael@0 | 246 | if (L) |
michael@0 | 247 | { |
michael@0 | 248 | const unsigned char *proba = b->prob; |
michael@0 | 249 | const int v2 = e >> 1; |
michael@0 | 250 | int n2 = L; /* number of bits in v2, assumed nonzero */ |
michael@0 | 251 | i = 0; |
michael@0 | 252 | |
michael@0 | 253 | do |
michael@0 | 254 | { |
michael@0 | 255 | const int bb = (v2 >> --n2) & 1; |
michael@0 | 256 | split = 1 + (((range - 1) * proba[i>>1]) >> 8); |
michael@0 | 257 | i = b->tree[i+bb]; |
michael@0 | 258 | |
michael@0 | 259 | if (bb) |
michael@0 | 260 | { |
michael@0 | 261 | lowvalue += split; |
michael@0 | 262 | range = range - split; |
michael@0 | 263 | } |
michael@0 | 264 | else |
michael@0 | 265 | { |
michael@0 | 266 | range = split; |
michael@0 | 267 | } |
michael@0 | 268 | |
michael@0 | 269 | shift = vp8_norm[range]; |
michael@0 | 270 | range <<= shift; |
michael@0 | 271 | count += shift; |
michael@0 | 272 | |
michael@0 | 273 | if (count >= 0) |
michael@0 | 274 | { |
michael@0 | 275 | int offset = shift - count; |
michael@0 | 276 | |
michael@0 | 277 | if ((lowvalue << (offset - 1)) & 0x80000000) |
michael@0 | 278 | { |
michael@0 | 279 | int x = w->pos - 1; |
michael@0 | 280 | |
michael@0 | 281 | while (x >= 0 && w->buffer[x] == 0xff) |
michael@0 | 282 | { |
michael@0 | 283 | w->buffer[x] = (unsigned char)0; |
michael@0 | 284 | x--; |
michael@0 | 285 | } |
michael@0 | 286 | |
michael@0 | 287 | w->buffer[x] += 1; |
michael@0 | 288 | } |
michael@0 | 289 | |
michael@0 | 290 | validate_buffer(w->buffer + w->pos, |
michael@0 | 291 | 1, |
michael@0 | 292 | w->buffer_end, |
michael@0 | 293 | w->error); |
michael@0 | 294 | |
michael@0 | 295 | w->buffer[w->pos++] = (lowvalue >> (24 - offset)); |
michael@0 | 296 | lowvalue <<= offset; |
michael@0 | 297 | shift = count; |
michael@0 | 298 | lowvalue &= 0xffffff; |
michael@0 | 299 | count -= 8 ; |
michael@0 | 300 | } |
michael@0 | 301 | |
michael@0 | 302 | lowvalue <<= shift; |
michael@0 | 303 | } |
michael@0 | 304 | while (n2); |
michael@0 | 305 | } |
michael@0 | 306 | |
michael@0 | 307 | |
michael@0 | 308 | { |
michael@0 | 309 | |
michael@0 | 310 | split = (range + 1) >> 1; |
michael@0 | 311 | |
michael@0 | 312 | if (e & 1) |
michael@0 | 313 | { |
michael@0 | 314 | lowvalue += split; |
michael@0 | 315 | range = range - split; |
michael@0 | 316 | } |
michael@0 | 317 | else |
michael@0 | 318 | { |
michael@0 | 319 | range = split; |
michael@0 | 320 | } |
michael@0 | 321 | |
michael@0 | 322 | range <<= 1; |
michael@0 | 323 | |
michael@0 | 324 | if ((lowvalue & 0x80000000)) |
michael@0 | 325 | { |
michael@0 | 326 | int x = w->pos - 1; |
michael@0 | 327 | |
michael@0 | 328 | while (x >= 0 && w->buffer[x] == 0xff) |
michael@0 | 329 | { |
michael@0 | 330 | w->buffer[x] = (unsigned char)0; |
michael@0 | 331 | x--; |
michael@0 | 332 | } |
michael@0 | 333 | |
michael@0 | 334 | w->buffer[x] += 1; |
michael@0 | 335 | |
michael@0 | 336 | } |
michael@0 | 337 | |
michael@0 | 338 | lowvalue <<= 1; |
michael@0 | 339 | |
michael@0 | 340 | if (!++count) |
michael@0 | 341 | { |
michael@0 | 342 | count = -8; |
michael@0 | 343 | |
michael@0 | 344 | validate_buffer(w->buffer + w->pos, |
michael@0 | 345 | 1, |
michael@0 | 346 | w->buffer_end, |
michael@0 | 347 | w->error); |
michael@0 | 348 | |
michael@0 | 349 | w->buffer[w->pos++] = (lowvalue >> 24); |
michael@0 | 350 | lowvalue &= 0xffffff; |
michael@0 | 351 | } |
michael@0 | 352 | } |
michael@0 | 353 | |
michael@0 | 354 | } |
michael@0 | 355 | |
michael@0 | 356 | ++p; |
michael@0 | 357 | } |
michael@0 | 358 | |
michael@0 | 359 | w->count = count; |
michael@0 | 360 | w->lowvalue = lowvalue; |
michael@0 | 361 | w->range = range; |
michael@0 | 362 | |
michael@0 | 363 | } |
michael@0 | 364 | |
michael@0 | 365 | static void write_partition_size(unsigned char *cx_data, int size) |
michael@0 | 366 | { |
michael@0 | 367 | signed char csize; |
michael@0 | 368 | |
michael@0 | 369 | csize = size & 0xff; |
michael@0 | 370 | *cx_data = csize; |
michael@0 | 371 | csize = (size >> 8) & 0xff; |
michael@0 | 372 | *(cx_data + 1) = csize; |
michael@0 | 373 | csize = (size >> 16) & 0xff; |
michael@0 | 374 | *(cx_data + 2) = csize; |
michael@0 | 375 | |
michael@0 | 376 | } |
michael@0 | 377 | |
michael@0 | 378 | static void pack_tokens_into_partitions_c(VP8_COMP *cpi, unsigned char *cx_data, |
michael@0 | 379 | unsigned char * cx_data_end, |
michael@0 | 380 | int num_part) |
michael@0 | 381 | { |
michael@0 | 382 | |
michael@0 | 383 | int i; |
michael@0 | 384 | unsigned char *ptr = cx_data; |
michael@0 | 385 | unsigned char *ptr_end = cx_data_end; |
michael@0 | 386 | vp8_writer * w; |
michael@0 | 387 | |
michael@0 | 388 | for (i = 0; i < num_part; i++) |
michael@0 | 389 | { |
michael@0 | 390 | int mb_row; |
michael@0 | 391 | |
michael@0 | 392 | w = cpi->bc + i + 1; |
michael@0 | 393 | |
michael@0 | 394 | vp8_start_encode(w, ptr, ptr_end); |
michael@0 | 395 | |
michael@0 | 396 | for (mb_row = i; mb_row < cpi->common.mb_rows; mb_row += num_part) |
michael@0 | 397 | { |
michael@0 | 398 | const TOKENEXTRA *p = cpi->tplist[mb_row].start; |
michael@0 | 399 | const TOKENEXTRA *stop = cpi->tplist[mb_row].stop; |
michael@0 | 400 | int tokens = (int)(stop - p); |
michael@0 | 401 | |
michael@0 | 402 | vp8_pack_tokens_c(w, p, tokens); |
michael@0 | 403 | } |
michael@0 | 404 | |
michael@0 | 405 | vp8_stop_encode(w); |
michael@0 | 406 | ptr += w->pos; |
michael@0 | 407 | } |
michael@0 | 408 | } |
michael@0 | 409 | |
michael@0 | 410 | |
michael@0 | 411 | static void pack_mb_row_tokens_c(VP8_COMP *cpi, vp8_writer *w) |
michael@0 | 412 | { |
michael@0 | 413 | int mb_row; |
michael@0 | 414 | |
michael@0 | 415 | for (mb_row = 0; mb_row < cpi->common.mb_rows; mb_row++) |
michael@0 | 416 | { |
michael@0 | 417 | const TOKENEXTRA *p = cpi->tplist[mb_row].start; |
michael@0 | 418 | const TOKENEXTRA *stop = cpi->tplist[mb_row].stop; |
michael@0 | 419 | int tokens = (int)(stop - p); |
michael@0 | 420 | |
michael@0 | 421 | vp8_pack_tokens_c(w, p, tokens); |
michael@0 | 422 | } |
michael@0 | 423 | |
michael@0 | 424 | } |
michael@0 | 425 | |
michael@0 | 426 | static void write_mv_ref |
michael@0 | 427 | ( |
michael@0 | 428 | vp8_writer *w, MB_PREDICTION_MODE m, const vp8_prob *p |
michael@0 | 429 | ) |
michael@0 | 430 | { |
michael@0 | 431 | #if CONFIG_DEBUG |
michael@0 | 432 | assert(NEARESTMV <= m && m <= SPLITMV); |
michael@0 | 433 | #endif |
michael@0 | 434 | vp8_write_token(w, vp8_mv_ref_tree, p, |
michael@0 | 435 | vp8_mv_ref_encoding_array + (m - NEARESTMV)); |
michael@0 | 436 | } |
michael@0 | 437 | |
michael@0 | 438 | static void write_sub_mv_ref |
michael@0 | 439 | ( |
michael@0 | 440 | vp8_writer *w, B_PREDICTION_MODE m, const vp8_prob *p |
michael@0 | 441 | ) |
michael@0 | 442 | { |
michael@0 | 443 | #if CONFIG_DEBUG |
michael@0 | 444 | assert(LEFT4X4 <= m && m <= NEW4X4); |
michael@0 | 445 | #endif |
michael@0 | 446 | vp8_write_token(w, vp8_sub_mv_ref_tree, p, |
michael@0 | 447 | vp8_sub_mv_ref_encoding_array + (m - LEFT4X4)); |
michael@0 | 448 | } |
michael@0 | 449 | |
michael@0 | 450 | static void write_mv |
michael@0 | 451 | ( |
michael@0 | 452 | vp8_writer *w, const MV *mv, const int_mv *ref, const MV_CONTEXT *mvc |
michael@0 | 453 | ) |
michael@0 | 454 | { |
michael@0 | 455 | MV e; |
michael@0 | 456 | e.row = mv->row - ref->as_mv.row; |
michael@0 | 457 | e.col = mv->col - ref->as_mv.col; |
michael@0 | 458 | |
michael@0 | 459 | vp8_encode_motion_vector(w, &e, mvc); |
michael@0 | 460 | } |
michael@0 | 461 | |
michael@0 | 462 | static void write_mb_features(vp8_writer *w, const MB_MODE_INFO *mi, const MACROBLOCKD *x) |
michael@0 | 463 | { |
michael@0 | 464 | /* Encode the MB segment id. */ |
michael@0 | 465 | if (x->segmentation_enabled && x->update_mb_segmentation_map) |
michael@0 | 466 | { |
michael@0 | 467 | switch (mi->segment_id) |
michael@0 | 468 | { |
michael@0 | 469 | case 0: |
michael@0 | 470 | vp8_write(w, 0, x->mb_segment_tree_probs[0]); |
michael@0 | 471 | vp8_write(w, 0, x->mb_segment_tree_probs[1]); |
michael@0 | 472 | break; |
michael@0 | 473 | case 1: |
michael@0 | 474 | vp8_write(w, 0, x->mb_segment_tree_probs[0]); |
michael@0 | 475 | vp8_write(w, 1, x->mb_segment_tree_probs[1]); |
michael@0 | 476 | break; |
michael@0 | 477 | case 2: |
michael@0 | 478 | vp8_write(w, 1, x->mb_segment_tree_probs[0]); |
michael@0 | 479 | vp8_write(w, 0, x->mb_segment_tree_probs[2]); |
michael@0 | 480 | break; |
michael@0 | 481 | case 3: |
michael@0 | 482 | vp8_write(w, 1, x->mb_segment_tree_probs[0]); |
michael@0 | 483 | vp8_write(w, 1, x->mb_segment_tree_probs[2]); |
michael@0 | 484 | break; |
michael@0 | 485 | |
michael@0 | 486 | /* TRAP.. This should not happen */ |
michael@0 | 487 | default: |
michael@0 | 488 | vp8_write(w, 0, x->mb_segment_tree_probs[0]); |
michael@0 | 489 | vp8_write(w, 0, x->mb_segment_tree_probs[1]); |
michael@0 | 490 | break; |
michael@0 | 491 | } |
michael@0 | 492 | } |
michael@0 | 493 | } |
michael@0 | 494 | void vp8_convert_rfct_to_prob(VP8_COMP *const cpi) |
michael@0 | 495 | { |
michael@0 | 496 | const int *const rfct = cpi->mb.count_mb_ref_frame_usage; |
michael@0 | 497 | const int rf_intra = rfct[INTRA_FRAME]; |
michael@0 | 498 | const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]; |
michael@0 | 499 | |
michael@0 | 500 | /* Calculate the probabilities used to code the ref frame based on usage */ |
michael@0 | 501 | if (!(cpi->prob_intra_coded = rf_intra * 255 / (rf_intra + rf_inter))) |
michael@0 | 502 | cpi->prob_intra_coded = 1; |
michael@0 | 503 | |
michael@0 | 504 | cpi->prob_last_coded = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128; |
michael@0 | 505 | |
michael@0 | 506 | if (!cpi->prob_last_coded) |
michael@0 | 507 | cpi->prob_last_coded = 1; |
michael@0 | 508 | |
michael@0 | 509 | cpi->prob_gf_coded = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) |
michael@0 | 510 | ? (rfct[GOLDEN_FRAME] * 255) / (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) : 128; |
michael@0 | 511 | |
michael@0 | 512 | if (!cpi->prob_gf_coded) |
michael@0 | 513 | cpi->prob_gf_coded = 1; |
michael@0 | 514 | |
michael@0 | 515 | } |
michael@0 | 516 | |
michael@0 | 517 | static void pack_inter_mode_mvs(VP8_COMP *const cpi) |
michael@0 | 518 | { |
michael@0 | 519 | VP8_COMMON *const pc = & cpi->common; |
michael@0 | 520 | vp8_writer *const w = cpi->bc; |
michael@0 | 521 | const MV_CONTEXT *mvc = pc->fc.mvc; |
michael@0 | 522 | |
michael@0 | 523 | |
michael@0 | 524 | MODE_INFO *m = pc->mi; |
michael@0 | 525 | const int mis = pc->mode_info_stride; |
michael@0 | 526 | int mb_row = -1; |
michael@0 | 527 | |
michael@0 | 528 | int prob_skip_false = 0; |
michael@0 | 529 | |
michael@0 | 530 | cpi->mb.partition_info = cpi->mb.pi; |
michael@0 | 531 | |
michael@0 | 532 | vp8_convert_rfct_to_prob(cpi); |
michael@0 | 533 | |
michael@0 | 534 | #ifdef VP8_ENTROPY_STATS |
michael@0 | 535 | active_section = 1; |
michael@0 | 536 | #endif |
michael@0 | 537 | |
michael@0 | 538 | if (pc->mb_no_coeff_skip) |
michael@0 | 539 | { |
michael@0 | 540 | int total_mbs = pc->mb_rows * pc->mb_cols; |
michael@0 | 541 | |
michael@0 | 542 | prob_skip_false = (total_mbs - cpi->mb.skip_true_count ) * 256 / total_mbs; |
michael@0 | 543 | |
michael@0 | 544 | if (prob_skip_false <= 1) |
michael@0 | 545 | prob_skip_false = 1; |
michael@0 | 546 | |
michael@0 | 547 | if (prob_skip_false > 255) |
michael@0 | 548 | prob_skip_false = 255; |
michael@0 | 549 | |
michael@0 | 550 | cpi->prob_skip_false = prob_skip_false; |
michael@0 | 551 | vp8_write_literal(w, prob_skip_false, 8); |
michael@0 | 552 | } |
michael@0 | 553 | |
michael@0 | 554 | vp8_write_literal(w, cpi->prob_intra_coded, 8); |
michael@0 | 555 | vp8_write_literal(w, cpi->prob_last_coded, 8); |
michael@0 | 556 | vp8_write_literal(w, cpi->prob_gf_coded, 8); |
michael@0 | 557 | |
michael@0 | 558 | update_mbintra_mode_probs(cpi); |
michael@0 | 559 | |
michael@0 | 560 | vp8_write_mvprobs(cpi); |
michael@0 | 561 | |
michael@0 | 562 | while (++mb_row < pc->mb_rows) |
michael@0 | 563 | { |
michael@0 | 564 | int mb_col = -1; |
michael@0 | 565 | |
michael@0 | 566 | while (++mb_col < pc->mb_cols) |
michael@0 | 567 | { |
michael@0 | 568 | const MB_MODE_INFO *const mi = & m->mbmi; |
michael@0 | 569 | const MV_REFERENCE_FRAME rf = mi->ref_frame; |
michael@0 | 570 | const MB_PREDICTION_MODE mode = mi->mode; |
michael@0 | 571 | |
michael@0 | 572 | MACROBLOCKD *xd = &cpi->mb.e_mbd; |
michael@0 | 573 | |
michael@0 | 574 | /* Distance of Mb to the various image edges. |
michael@0 | 575 | * These specified to 8th pel as they are always compared to MV |
michael@0 | 576 | * values that are in 1/8th pel units |
michael@0 | 577 | */ |
michael@0 | 578 | xd->mb_to_left_edge = -((mb_col * 16) << 3); |
michael@0 | 579 | xd->mb_to_right_edge = ((pc->mb_cols - 1 - mb_col) * 16) << 3; |
michael@0 | 580 | xd->mb_to_top_edge = -((mb_row * 16) << 3); |
michael@0 | 581 | xd->mb_to_bottom_edge = ((pc->mb_rows - 1 - mb_row) * 16) << 3; |
michael@0 | 582 | |
michael@0 | 583 | #ifdef VP8_ENTROPY_STATS |
michael@0 | 584 | active_section = 9; |
michael@0 | 585 | #endif |
michael@0 | 586 | |
michael@0 | 587 | if (cpi->mb.e_mbd.update_mb_segmentation_map) |
michael@0 | 588 | write_mb_features(w, mi, &cpi->mb.e_mbd); |
michael@0 | 589 | |
michael@0 | 590 | if (pc->mb_no_coeff_skip) |
michael@0 | 591 | vp8_encode_bool(w, m->mbmi.mb_skip_coeff, prob_skip_false); |
michael@0 | 592 | |
michael@0 | 593 | if (rf == INTRA_FRAME) |
michael@0 | 594 | { |
michael@0 | 595 | vp8_write(w, 0, cpi->prob_intra_coded); |
michael@0 | 596 | #ifdef VP8_ENTROPY_STATS |
michael@0 | 597 | active_section = 6; |
michael@0 | 598 | #endif |
michael@0 | 599 | write_ymode(w, mode, pc->fc.ymode_prob); |
michael@0 | 600 | |
michael@0 | 601 | if (mode == B_PRED) |
michael@0 | 602 | { |
michael@0 | 603 | int j = 0; |
michael@0 | 604 | |
michael@0 | 605 | do |
michael@0 | 606 | write_bmode(w, m->bmi[j].as_mode, pc->fc.bmode_prob); |
michael@0 | 607 | while (++j < 16); |
michael@0 | 608 | } |
michael@0 | 609 | |
michael@0 | 610 | write_uv_mode(w, mi->uv_mode, pc->fc.uv_mode_prob); |
michael@0 | 611 | } |
michael@0 | 612 | else /* inter coded */ |
michael@0 | 613 | { |
michael@0 | 614 | int_mv best_mv; |
michael@0 | 615 | vp8_prob mv_ref_p [VP8_MVREFS-1]; |
michael@0 | 616 | |
michael@0 | 617 | vp8_write(w, 1, cpi->prob_intra_coded); |
michael@0 | 618 | |
michael@0 | 619 | if (rf == LAST_FRAME) |
michael@0 | 620 | vp8_write(w, 0, cpi->prob_last_coded); |
michael@0 | 621 | else |
michael@0 | 622 | { |
michael@0 | 623 | vp8_write(w, 1, cpi->prob_last_coded); |
michael@0 | 624 | vp8_write(w, (rf == GOLDEN_FRAME) ? 0 : 1, cpi->prob_gf_coded); |
michael@0 | 625 | } |
michael@0 | 626 | |
michael@0 | 627 | { |
michael@0 | 628 | int_mv n1, n2; |
michael@0 | 629 | int ct[4]; |
michael@0 | 630 | |
michael@0 | 631 | vp8_find_near_mvs(xd, m, &n1, &n2, &best_mv, ct, rf, cpi->common.ref_frame_sign_bias); |
michael@0 | 632 | vp8_clamp_mv2(&best_mv, xd); |
michael@0 | 633 | |
michael@0 | 634 | vp8_mv_ref_probs(mv_ref_p, ct); |
michael@0 | 635 | |
michael@0 | 636 | #ifdef VP8_ENTROPY_STATS |
michael@0 | 637 | accum_mv_refs(mode, ct); |
michael@0 | 638 | #endif |
michael@0 | 639 | |
michael@0 | 640 | } |
michael@0 | 641 | |
michael@0 | 642 | #ifdef VP8_ENTROPY_STATS |
michael@0 | 643 | active_section = 3; |
michael@0 | 644 | #endif |
michael@0 | 645 | |
michael@0 | 646 | write_mv_ref(w, mode, mv_ref_p); |
michael@0 | 647 | |
michael@0 | 648 | switch (mode) /* new, split require MVs */ |
michael@0 | 649 | { |
michael@0 | 650 | case NEWMV: |
michael@0 | 651 | |
michael@0 | 652 | #ifdef VP8_ENTROPY_STATS |
michael@0 | 653 | active_section = 5; |
michael@0 | 654 | #endif |
michael@0 | 655 | |
michael@0 | 656 | write_mv(w, &mi->mv.as_mv, &best_mv, mvc); |
michael@0 | 657 | break; |
michael@0 | 658 | |
michael@0 | 659 | case SPLITMV: |
michael@0 | 660 | { |
michael@0 | 661 | int j = 0; |
michael@0 | 662 | |
michael@0 | 663 | #ifdef MODE_STATS |
michael@0 | 664 | ++count_mb_seg [mi->partitioning]; |
michael@0 | 665 | #endif |
michael@0 | 666 | |
michael@0 | 667 | write_split(w, mi->partitioning); |
michael@0 | 668 | |
michael@0 | 669 | do |
michael@0 | 670 | { |
michael@0 | 671 | B_PREDICTION_MODE blockmode; |
michael@0 | 672 | int_mv blockmv; |
michael@0 | 673 | const int *const L = vp8_mbsplits [mi->partitioning]; |
michael@0 | 674 | int k = -1; /* first block in subset j */ |
michael@0 | 675 | int mv_contz; |
michael@0 | 676 | int_mv leftmv, abovemv; |
michael@0 | 677 | |
michael@0 | 678 | blockmode = cpi->mb.partition_info->bmi[j].mode; |
michael@0 | 679 | blockmv = cpi->mb.partition_info->bmi[j].mv; |
michael@0 | 680 | #if CONFIG_DEBUG |
michael@0 | 681 | while (j != L[++k]) |
michael@0 | 682 | if (k >= 16) |
michael@0 | 683 | assert(0); |
michael@0 | 684 | #else |
michael@0 | 685 | while (j != L[++k]); |
michael@0 | 686 | #endif |
michael@0 | 687 | leftmv.as_int = left_block_mv(m, k); |
michael@0 | 688 | abovemv.as_int = above_block_mv(m, k, mis); |
michael@0 | 689 | mv_contz = vp8_mv_cont(&leftmv, &abovemv); |
michael@0 | 690 | |
michael@0 | 691 | write_sub_mv_ref(w, blockmode, vp8_sub_mv_ref_prob2 [mv_contz]); |
michael@0 | 692 | |
michael@0 | 693 | if (blockmode == NEW4X4) |
michael@0 | 694 | { |
michael@0 | 695 | #ifdef VP8_ENTROPY_STATS |
michael@0 | 696 | active_section = 11; |
michael@0 | 697 | #endif |
michael@0 | 698 | write_mv(w, &blockmv.as_mv, &best_mv, (const MV_CONTEXT *) mvc); |
michael@0 | 699 | } |
michael@0 | 700 | } |
michael@0 | 701 | while (++j < cpi->mb.partition_info->count); |
michael@0 | 702 | } |
michael@0 | 703 | break; |
michael@0 | 704 | default: |
michael@0 | 705 | break; |
michael@0 | 706 | } |
michael@0 | 707 | } |
michael@0 | 708 | |
michael@0 | 709 | ++m; |
michael@0 | 710 | cpi->mb.partition_info++; |
michael@0 | 711 | } |
michael@0 | 712 | |
michael@0 | 713 | ++m; /* skip L prediction border */ |
michael@0 | 714 | cpi->mb.partition_info++; |
michael@0 | 715 | } |
michael@0 | 716 | } |
michael@0 | 717 | |
michael@0 | 718 | |
michael@0 | 719 | static void write_kfmodes(VP8_COMP *cpi) |
michael@0 | 720 | { |
michael@0 | 721 | vp8_writer *const bc = cpi->bc; |
michael@0 | 722 | const VP8_COMMON *const c = & cpi->common; |
michael@0 | 723 | /* const */ |
michael@0 | 724 | MODE_INFO *m = c->mi; |
michael@0 | 725 | |
michael@0 | 726 | int mb_row = -1; |
michael@0 | 727 | int prob_skip_false = 0; |
michael@0 | 728 | |
michael@0 | 729 | if (c->mb_no_coeff_skip) |
michael@0 | 730 | { |
michael@0 | 731 | int total_mbs = c->mb_rows * c->mb_cols; |
michael@0 | 732 | |
michael@0 | 733 | prob_skip_false = (total_mbs - cpi->mb.skip_true_count ) * 256 / total_mbs; |
michael@0 | 734 | |
michael@0 | 735 | if (prob_skip_false <= 1) |
michael@0 | 736 | prob_skip_false = 1; |
michael@0 | 737 | |
michael@0 | 738 | if (prob_skip_false >= 255) |
michael@0 | 739 | prob_skip_false = 255; |
michael@0 | 740 | |
michael@0 | 741 | cpi->prob_skip_false = prob_skip_false; |
michael@0 | 742 | vp8_write_literal(bc, prob_skip_false, 8); |
michael@0 | 743 | } |
michael@0 | 744 | |
michael@0 | 745 | while (++mb_row < c->mb_rows) |
michael@0 | 746 | { |
michael@0 | 747 | int mb_col = -1; |
michael@0 | 748 | |
michael@0 | 749 | while (++mb_col < c->mb_cols) |
michael@0 | 750 | { |
michael@0 | 751 | const int ym = m->mbmi.mode; |
michael@0 | 752 | |
michael@0 | 753 | if (cpi->mb.e_mbd.update_mb_segmentation_map) |
michael@0 | 754 | write_mb_features(bc, &m->mbmi, &cpi->mb.e_mbd); |
michael@0 | 755 | |
michael@0 | 756 | if (c->mb_no_coeff_skip) |
michael@0 | 757 | vp8_encode_bool(bc, m->mbmi.mb_skip_coeff, prob_skip_false); |
michael@0 | 758 | |
michael@0 | 759 | kfwrite_ymode(bc, ym, vp8_kf_ymode_prob); |
michael@0 | 760 | |
michael@0 | 761 | if (ym == B_PRED) |
michael@0 | 762 | { |
michael@0 | 763 | const int mis = c->mode_info_stride; |
michael@0 | 764 | int i = 0; |
michael@0 | 765 | |
michael@0 | 766 | do |
michael@0 | 767 | { |
michael@0 | 768 | const B_PREDICTION_MODE A = above_block_mode(m, i, mis); |
michael@0 | 769 | const B_PREDICTION_MODE L = left_block_mode(m, i); |
michael@0 | 770 | const int bm = m->bmi[i].as_mode; |
michael@0 | 771 | |
michael@0 | 772 | #ifdef VP8_ENTROPY_STATS |
michael@0 | 773 | ++intra_mode_stats [A] [L] [bm]; |
michael@0 | 774 | #endif |
michael@0 | 775 | |
michael@0 | 776 | write_bmode(bc, bm, vp8_kf_bmode_prob [A] [L]); |
michael@0 | 777 | } |
michael@0 | 778 | while (++i < 16); |
michael@0 | 779 | } |
michael@0 | 780 | |
michael@0 | 781 | write_uv_mode(bc, (m++)->mbmi.uv_mode, vp8_kf_uv_mode_prob); |
michael@0 | 782 | } |
michael@0 | 783 | |
michael@0 | 784 | m++; /* skip L prediction border */ |
michael@0 | 785 | } |
michael@0 | 786 | } |
michael@0 | 787 | |
michael@0 | 788 | #if 0 |
michael@0 | 789 | /* This function is used for debugging probability trees. */ |
michael@0 | 790 | static void print_prob_tree(vp8_prob |
michael@0 | 791 | coef_probs[BLOCK_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS][ENTROPY_NODES]) |
michael@0 | 792 | { |
michael@0 | 793 | /* print coef probability tree */ |
michael@0 | 794 | int i,j,k,l; |
michael@0 | 795 | FILE* f = fopen("enc_tree_probs.txt", "a"); |
michael@0 | 796 | fprintf(f, "{\n"); |
michael@0 | 797 | for (i = 0; i < BLOCK_TYPES; i++) |
michael@0 | 798 | { |
michael@0 | 799 | fprintf(f, " {\n"); |
michael@0 | 800 | for (j = 0; j < COEF_BANDS; j++) |
michael@0 | 801 | { |
michael@0 | 802 | fprintf(f, " {\n"); |
michael@0 | 803 | for (k = 0; k < PREV_COEF_CONTEXTS; k++) |
michael@0 | 804 | { |
michael@0 | 805 | fprintf(f, " {"); |
michael@0 | 806 | for (l = 0; l < ENTROPY_NODES; l++) |
michael@0 | 807 | { |
michael@0 | 808 | fprintf(f, "%3u, ", |
michael@0 | 809 | (unsigned int)(coef_probs [i][j][k][l])); |
michael@0 | 810 | } |
michael@0 | 811 | fprintf(f, " }\n"); |
michael@0 | 812 | } |
michael@0 | 813 | fprintf(f, " }\n"); |
michael@0 | 814 | } |
michael@0 | 815 | fprintf(f, " }\n"); |
michael@0 | 816 | } |
michael@0 | 817 | fprintf(f, "}\n"); |
michael@0 | 818 | fclose(f); |
michael@0 | 819 | } |
michael@0 | 820 | #endif |
michael@0 | 821 | |
michael@0 | 822 | static void sum_probs_over_prev_coef_context( |
michael@0 | 823 | const unsigned int probs[PREV_COEF_CONTEXTS][MAX_ENTROPY_TOKENS], |
michael@0 | 824 | unsigned int* out) |
michael@0 | 825 | { |
michael@0 | 826 | int i, j; |
michael@0 | 827 | for (i=0; i < MAX_ENTROPY_TOKENS; ++i) |
michael@0 | 828 | { |
michael@0 | 829 | for (j=0; j < PREV_COEF_CONTEXTS; ++j) |
michael@0 | 830 | { |
michael@0 | 831 | const unsigned int tmp = out[i]; |
michael@0 | 832 | out[i] += probs[j][i]; |
michael@0 | 833 | /* check for wrap */ |
michael@0 | 834 | if (out[i] < tmp) |
michael@0 | 835 | out[i] = UINT_MAX; |
michael@0 | 836 | } |
michael@0 | 837 | } |
michael@0 | 838 | } |
michael@0 | 839 | |
michael@0 | 840 | static int prob_update_savings(const unsigned int *ct, |
michael@0 | 841 | const vp8_prob oldp, const vp8_prob newp, |
michael@0 | 842 | const vp8_prob upd) |
michael@0 | 843 | { |
michael@0 | 844 | const int old_b = vp8_cost_branch(ct, oldp); |
michael@0 | 845 | const int new_b = vp8_cost_branch(ct, newp); |
michael@0 | 846 | const int update_b = 8 + |
michael@0 | 847 | ((vp8_cost_one(upd) - vp8_cost_zero(upd)) >> 8); |
michael@0 | 848 | |
michael@0 | 849 | return old_b - new_b - update_b; |
michael@0 | 850 | } |
michael@0 | 851 | |
michael@0 | 852 | static int independent_coef_context_savings(VP8_COMP *cpi) |
michael@0 | 853 | { |
michael@0 | 854 | MACROBLOCK *const x = & cpi->mb; |
michael@0 | 855 | int savings = 0; |
michael@0 | 856 | int i = 0; |
michael@0 | 857 | do |
michael@0 | 858 | { |
michael@0 | 859 | int j = 0; |
michael@0 | 860 | do |
michael@0 | 861 | { |
michael@0 | 862 | int k = 0; |
michael@0 | 863 | unsigned int prev_coef_count_sum[MAX_ENTROPY_TOKENS] = {0}; |
michael@0 | 864 | int prev_coef_savings[MAX_ENTROPY_TOKENS] = {0}; |
michael@0 | 865 | const unsigned int (*probs)[MAX_ENTROPY_TOKENS]; |
michael@0 | 866 | /* Calculate new probabilities given the constraint that |
michael@0 | 867 | * they must be equal over the prev coef contexts |
michael@0 | 868 | */ |
michael@0 | 869 | |
michael@0 | 870 | probs = (const unsigned int (*)[MAX_ENTROPY_TOKENS]) |
michael@0 | 871 | x->coef_counts[i][j]; |
michael@0 | 872 | |
michael@0 | 873 | /* Reset to default probabilities at key frames */ |
michael@0 | 874 | if (cpi->common.frame_type == KEY_FRAME) |
michael@0 | 875 | probs = default_coef_counts[i][j]; |
michael@0 | 876 | |
michael@0 | 877 | sum_probs_over_prev_coef_context(probs, prev_coef_count_sum); |
michael@0 | 878 | |
michael@0 | 879 | do |
michael@0 | 880 | { |
michael@0 | 881 | /* at every context */ |
michael@0 | 882 | |
michael@0 | 883 | /* calc probs and branch cts for this frame only */ |
michael@0 | 884 | int t = 0; /* token/prob index */ |
michael@0 | 885 | |
michael@0 | 886 | vp8_tree_probs_from_distribution( |
michael@0 | 887 | MAX_ENTROPY_TOKENS, vp8_coef_encodings, vp8_coef_tree, |
michael@0 | 888 | cpi->frame_coef_probs[i][j][k], |
michael@0 | 889 | cpi->frame_branch_ct [i][j][k], |
michael@0 | 890 | prev_coef_count_sum, |
michael@0 | 891 | 256, 1); |
michael@0 | 892 | |
michael@0 | 893 | do |
michael@0 | 894 | { |
michael@0 | 895 | const unsigned int *ct = cpi->frame_branch_ct [i][j][k][t]; |
michael@0 | 896 | const vp8_prob newp = cpi->frame_coef_probs [i][j][k][t]; |
michael@0 | 897 | const vp8_prob oldp = cpi->common.fc.coef_probs [i][j][k][t]; |
michael@0 | 898 | const vp8_prob upd = vp8_coef_update_probs [i][j][k][t]; |
michael@0 | 899 | const int s = prob_update_savings(ct, oldp, newp, upd); |
michael@0 | 900 | |
michael@0 | 901 | if (cpi->common.frame_type != KEY_FRAME || |
michael@0 | 902 | (cpi->common.frame_type == KEY_FRAME && newp != oldp)) |
michael@0 | 903 | prev_coef_savings[t] += s; |
michael@0 | 904 | } |
michael@0 | 905 | while (++t < ENTROPY_NODES); |
michael@0 | 906 | } |
michael@0 | 907 | while (++k < PREV_COEF_CONTEXTS); |
michael@0 | 908 | k = 0; |
michael@0 | 909 | do |
michael@0 | 910 | { |
michael@0 | 911 | /* We only update probabilities if we can save bits, except |
michael@0 | 912 | * for key frames where we have to update all probabilities |
michael@0 | 913 | * to get the equal probabilities across the prev coef |
michael@0 | 914 | * contexts. |
michael@0 | 915 | */ |
michael@0 | 916 | if (prev_coef_savings[k] > 0 || |
michael@0 | 917 | cpi->common.frame_type == KEY_FRAME) |
michael@0 | 918 | savings += prev_coef_savings[k]; |
michael@0 | 919 | } |
michael@0 | 920 | while (++k < ENTROPY_NODES); |
michael@0 | 921 | } |
michael@0 | 922 | while (++j < COEF_BANDS); |
michael@0 | 923 | } |
michael@0 | 924 | while (++i < BLOCK_TYPES); |
michael@0 | 925 | return savings; |
michael@0 | 926 | } |
michael@0 | 927 | |
michael@0 | 928 | static int default_coef_context_savings(VP8_COMP *cpi) |
michael@0 | 929 | { |
michael@0 | 930 | MACROBLOCK *const x = & cpi->mb; |
michael@0 | 931 | int savings = 0; |
michael@0 | 932 | int i = 0; |
michael@0 | 933 | do |
michael@0 | 934 | { |
michael@0 | 935 | int j = 0; |
michael@0 | 936 | do |
michael@0 | 937 | { |
michael@0 | 938 | int k = 0; |
michael@0 | 939 | do |
michael@0 | 940 | { |
michael@0 | 941 | /* at every context */ |
michael@0 | 942 | |
michael@0 | 943 | /* calc probs and branch cts for this frame only */ |
michael@0 | 944 | int t = 0; /* token/prob index */ |
michael@0 | 945 | |
michael@0 | 946 | vp8_tree_probs_from_distribution( |
michael@0 | 947 | MAX_ENTROPY_TOKENS, vp8_coef_encodings, vp8_coef_tree, |
michael@0 | 948 | cpi->frame_coef_probs [i][j][k], |
michael@0 | 949 | cpi->frame_branch_ct [i][j][k], |
michael@0 | 950 | x->coef_counts [i][j][k], |
michael@0 | 951 | 256, 1 |
michael@0 | 952 | ); |
michael@0 | 953 | |
michael@0 | 954 | do |
michael@0 | 955 | { |
michael@0 | 956 | const unsigned int *ct = cpi->frame_branch_ct [i][j][k][t]; |
michael@0 | 957 | const vp8_prob newp = cpi->frame_coef_probs [i][j][k][t]; |
michael@0 | 958 | const vp8_prob oldp = cpi->common.fc.coef_probs [i][j][k][t]; |
michael@0 | 959 | const vp8_prob upd = vp8_coef_update_probs [i][j][k][t]; |
michael@0 | 960 | const int s = prob_update_savings(ct, oldp, newp, upd); |
michael@0 | 961 | |
michael@0 | 962 | if (s > 0) |
michael@0 | 963 | { |
michael@0 | 964 | savings += s; |
michael@0 | 965 | } |
michael@0 | 966 | } |
michael@0 | 967 | while (++t < ENTROPY_NODES); |
michael@0 | 968 | } |
michael@0 | 969 | while (++k < PREV_COEF_CONTEXTS); |
michael@0 | 970 | } |
michael@0 | 971 | while (++j < COEF_BANDS); |
michael@0 | 972 | } |
michael@0 | 973 | while (++i < BLOCK_TYPES); |
michael@0 | 974 | return savings; |
michael@0 | 975 | } |
michael@0 | 976 | |
michael@0 | 977 | void vp8_calc_ref_frame_costs(int *ref_frame_cost, |
michael@0 | 978 | int prob_intra, |
michael@0 | 979 | int prob_last, |
michael@0 | 980 | int prob_garf |
michael@0 | 981 | ) |
michael@0 | 982 | { |
michael@0 | 983 | assert(prob_intra >= 0); |
michael@0 | 984 | assert(prob_intra <= 255); |
michael@0 | 985 | assert(prob_last >= 0); |
michael@0 | 986 | assert(prob_last <= 255); |
michael@0 | 987 | assert(prob_garf >= 0); |
michael@0 | 988 | assert(prob_garf <= 255); |
michael@0 | 989 | ref_frame_cost[INTRA_FRAME] = vp8_cost_zero(prob_intra); |
michael@0 | 990 | ref_frame_cost[LAST_FRAME] = vp8_cost_one(prob_intra) |
michael@0 | 991 | + vp8_cost_zero(prob_last); |
michael@0 | 992 | ref_frame_cost[GOLDEN_FRAME] = vp8_cost_one(prob_intra) |
michael@0 | 993 | + vp8_cost_one(prob_last) |
michael@0 | 994 | + vp8_cost_zero(prob_garf); |
michael@0 | 995 | ref_frame_cost[ALTREF_FRAME] = vp8_cost_one(prob_intra) |
michael@0 | 996 | + vp8_cost_one(prob_last) |
michael@0 | 997 | + vp8_cost_one(prob_garf); |
michael@0 | 998 | |
michael@0 | 999 | } |
michael@0 | 1000 | |
michael@0 | 1001 | int vp8_estimate_entropy_savings(VP8_COMP *cpi) |
michael@0 | 1002 | { |
michael@0 | 1003 | int savings = 0; |
michael@0 | 1004 | |
michael@0 | 1005 | const int *const rfct = cpi->mb.count_mb_ref_frame_usage; |
michael@0 | 1006 | const int rf_intra = rfct[INTRA_FRAME]; |
michael@0 | 1007 | const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]; |
michael@0 | 1008 | int new_intra, new_last, new_garf, oldtotal, newtotal; |
michael@0 | 1009 | int ref_frame_cost[MAX_REF_FRAMES]; |
michael@0 | 1010 | |
michael@0 | 1011 | vp8_clear_system_state(); |
michael@0 | 1012 | |
michael@0 | 1013 | if (cpi->common.frame_type != KEY_FRAME) |
michael@0 | 1014 | { |
michael@0 | 1015 | if (!(new_intra = rf_intra * 255 / (rf_intra + rf_inter))) |
michael@0 | 1016 | new_intra = 1; |
michael@0 | 1017 | |
michael@0 | 1018 | new_last = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128; |
michael@0 | 1019 | |
michael@0 | 1020 | new_garf = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) |
michael@0 | 1021 | ? (rfct[GOLDEN_FRAME] * 255) / (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) : 128; |
michael@0 | 1022 | |
michael@0 | 1023 | |
michael@0 | 1024 | vp8_calc_ref_frame_costs(ref_frame_cost,new_intra,new_last,new_garf); |
michael@0 | 1025 | |
michael@0 | 1026 | newtotal = |
michael@0 | 1027 | rfct[INTRA_FRAME] * ref_frame_cost[INTRA_FRAME] + |
michael@0 | 1028 | rfct[LAST_FRAME] * ref_frame_cost[LAST_FRAME] + |
michael@0 | 1029 | rfct[GOLDEN_FRAME] * ref_frame_cost[GOLDEN_FRAME] + |
michael@0 | 1030 | rfct[ALTREF_FRAME] * ref_frame_cost[ALTREF_FRAME]; |
michael@0 | 1031 | |
michael@0 | 1032 | |
michael@0 | 1033 | /* old costs */ |
michael@0 | 1034 | vp8_calc_ref_frame_costs(ref_frame_cost,cpi->prob_intra_coded, |
michael@0 | 1035 | cpi->prob_last_coded,cpi->prob_gf_coded); |
michael@0 | 1036 | |
michael@0 | 1037 | oldtotal = |
michael@0 | 1038 | rfct[INTRA_FRAME] * ref_frame_cost[INTRA_FRAME] + |
michael@0 | 1039 | rfct[LAST_FRAME] * ref_frame_cost[LAST_FRAME] + |
michael@0 | 1040 | rfct[GOLDEN_FRAME] * ref_frame_cost[GOLDEN_FRAME] + |
michael@0 | 1041 | rfct[ALTREF_FRAME] * ref_frame_cost[ALTREF_FRAME]; |
michael@0 | 1042 | |
michael@0 | 1043 | savings += (oldtotal - newtotal) / 256; |
michael@0 | 1044 | } |
michael@0 | 1045 | |
michael@0 | 1046 | |
michael@0 | 1047 | if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS) |
michael@0 | 1048 | savings += independent_coef_context_savings(cpi); |
michael@0 | 1049 | else |
michael@0 | 1050 | savings += default_coef_context_savings(cpi); |
michael@0 | 1051 | |
michael@0 | 1052 | |
michael@0 | 1053 | return savings; |
michael@0 | 1054 | } |
michael@0 | 1055 | |
michael@0 | 1056 | #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING |
michael@0 | 1057 | int vp8_update_coef_context(VP8_COMP *cpi) |
michael@0 | 1058 | { |
michael@0 | 1059 | int savings = 0; |
michael@0 | 1060 | |
michael@0 | 1061 | |
michael@0 | 1062 | if (cpi->common.frame_type == KEY_FRAME) |
michael@0 | 1063 | { |
michael@0 | 1064 | /* Reset to default counts/probabilities at key frames */ |
michael@0 | 1065 | vp8_copy(cpi->mb.coef_counts, default_coef_counts); |
michael@0 | 1066 | } |
michael@0 | 1067 | |
michael@0 | 1068 | if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS) |
michael@0 | 1069 | savings += independent_coef_context_savings(cpi); |
michael@0 | 1070 | else |
michael@0 | 1071 | savings += default_coef_context_savings(cpi); |
michael@0 | 1072 | |
michael@0 | 1073 | return savings; |
michael@0 | 1074 | } |
michael@0 | 1075 | #endif |
michael@0 | 1076 | |
michael@0 | 1077 | void vp8_update_coef_probs(VP8_COMP *cpi) |
michael@0 | 1078 | { |
michael@0 | 1079 | int i = 0; |
michael@0 | 1080 | #if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING) |
michael@0 | 1081 | vp8_writer *const w = cpi->bc; |
michael@0 | 1082 | #endif |
michael@0 | 1083 | int savings = 0; |
michael@0 | 1084 | |
michael@0 | 1085 | vp8_clear_system_state(); |
michael@0 | 1086 | |
michael@0 | 1087 | do |
michael@0 | 1088 | { |
michael@0 | 1089 | int j = 0; |
michael@0 | 1090 | |
michael@0 | 1091 | do |
michael@0 | 1092 | { |
michael@0 | 1093 | int k = 0; |
michael@0 | 1094 | int prev_coef_savings[ENTROPY_NODES] = {0}; |
michael@0 | 1095 | if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS) |
michael@0 | 1096 | { |
michael@0 | 1097 | for (k = 0; k < PREV_COEF_CONTEXTS; ++k) |
michael@0 | 1098 | { |
michael@0 | 1099 | int t; /* token/prob index */ |
michael@0 | 1100 | for (t = 0; t < ENTROPY_NODES; ++t) |
michael@0 | 1101 | { |
michael@0 | 1102 | const unsigned int *ct = cpi->frame_branch_ct [i][j] |
michael@0 | 1103 | [k][t]; |
michael@0 | 1104 | const vp8_prob newp = cpi->frame_coef_probs[i][j][k][t]; |
michael@0 | 1105 | const vp8_prob oldp = cpi->common.fc.coef_probs[i][j] |
michael@0 | 1106 | [k][t]; |
michael@0 | 1107 | const vp8_prob upd = vp8_coef_update_probs[i][j][k][t]; |
michael@0 | 1108 | |
michael@0 | 1109 | prev_coef_savings[t] += |
michael@0 | 1110 | prob_update_savings(ct, oldp, newp, upd); |
michael@0 | 1111 | } |
michael@0 | 1112 | } |
michael@0 | 1113 | k = 0; |
michael@0 | 1114 | } |
michael@0 | 1115 | do |
michael@0 | 1116 | { |
michael@0 | 1117 | /* note: use result from vp8_estimate_entropy_savings, so no |
michael@0 | 1118 | * need to call vp8_tree_probs_from_distribution here. |
michael@0 | 1119 | */ |
michael@0 | 1120 | |
michael@0 | 1121 | /* at every context */ |
michael@0 | 1122 | |
michael@0 | 1123 | /* calc probs and branch cts for this frame only */ |
michael@0 | 1124 | int t = 0; /* token/prob index */ |
michael@0 | 1125 | |
michael@0 | 1126 | do |
michael@0 | 1127 | { |
michael@0 | 1128 | const vp8_prob newp = cpi->frame_coef_probs [i][j][k][t]; |
michael@0 | 1129 | |
michael@0 | 1130 | vp8_prob *Pold = cpi->common.fc.coef_probs [i][j][k] + t; |
michael@0 | 1131 | const vp8_prob upd = vp8_coef_update_probs [i][j][k][t]; |
michael@0 | 1132 | |
michael@0 | 1133 | int s = prev_coef_savings[t]; |
michael@0 | 1134 | int u = 0; |
michael@0 | 1135 | |
michael@0 | 1136 | if (!(cpi->oxcf.error_resilient_mode & |
michael@0 | 1137 | VPX_ERROR_RESILIENT_PARTITIONS)) |
michael@0 | 1138 | { |
michael@0 | 1139 | s = prob_update_savings( |
michael@0 | 1140 | cpi->frame_branch_ct [i][j][k][t], |
michael@0 | 1141 | *Pold, newp, upd); |
michael@0 | 1142 | } |
michael@0 | 1143 | |
michael@0 | 1144 | if (s > 0) |
michael@0 | 1145 | u = 1; |
michael@0 | 1146 | |
michael@0 | 1147 | /* Force updates on key frames if the new is different, |
michael@0 | 1148 | * so that we can be sure we end up with equal probabilities |
michael@0 | 1149 | * over the prev coef contexts. |
michael@0 | 1150 | */ |
michael@0 | 1151 | if ((cpi->oxcf.error_resilient_mode & |
michael@0 | 1152 | VPX_ERROR_RESILIENT_PARTITIONS) && |
michael@0 | 1153 | cpi->common.frame_type == KEY_FRAME && newp != *Pold) |
michael@0 | 1154 | u = 1; |
michael@0 | 1155 | |
michael@0 | 1156 | #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING |
michael@0 | 1157 | cpi->update_probs[i][j][k][t] = u; |
michael@0 | 1158 | #else |
michael@0 | 1159 | vp8_write(w, u, upd); |
michael@0 | 1160 | #endif |
michael@0 | 1161 | |
michael@0 | 1162 | |
michael@0 | 1163 | #ifdef VP8_ENTROPY_STATS |
michael@0 | 1164 | ++ tree_update_hist [i][j][k][t] [u]; |
michael@0 | 1165 | #endif |
michael@0 | 1166 | |
michael@0 | 1167 | if (u) |
michael@0 | 1168 | { |
michael@0 | 1169 | /* send/use new probability */ |
michael@0 | 1170 | |
michael@0 | 1171 | *Pold = newp; |
michael@0 | 1172 | #if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING) |
michael@0 | 1173 | vp8_write_literal(w, newp, 8); |
michael@0 | 1174 | #endif |
michael@0 | 1175 | |
michael@0 | 1176 | savings += s; |
michael@0 | 1177 | |
michael@0 | 1178 | } |
michael@0 | 1179 | |
michael@0 | 1180 | } |
michael@0 | 1181 | while (++t < ENTROPY_NODES); |
michael@0 | 1182 | |
michael@0 | 1183 | /* Accum token counts for generation of default statistics */ |
michael@0 | 1184 | #ifdef VP8_ENTROPY_STATS |
michael@0 | 1185 | t = 0; |
michael@0 | 1186 | |
michael@0 | 1187 | do |
michael@0 | 1188 | { |
michael@0 | 1189 | context_counters [i][j][k][t] += cpi->coef_counts [i][j][k][t]; |
michael@0 | 1190 | } |
michael@0 | 1191 | while (++t < MAX_ENTROPY_TOKENS); |
michael@0 | 1192 | |
michael@0 | 1193 | #endif |
michael@0 | 1194 | |
michael@0 | 1195 | } |
michael@0 | 1196 | while (++k < PREV_COEF_CONTEXTS); |
michael@0 | 1197 | } |
michael@0 | 1198 | while (++j < COEF_BANDS); |
michael@0 | 1199 | } |
michael@0 | 1200 | while (++i < BLOCK_TYPES); |
michael@0 | 1201 | |
michael@0 | 1202 | } |
michael@0 | 1203 | |
michael@0 | 1204 | #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING |
michael@0 | 1205 | static void pack_coef_probs(VP8_COMP *cpi) |
michael@0 | 1206 | { |
michael@0 | 1207 | int i = 0; |
michael@0 | 1208 | vp8_writer *const w = cpi->bc; |
michael@0 | 1209 | |
michael@0 | 1210 | do |
michael@0 | 1211 | { |
michael@0 | 1212 | int j = 0; |
michael@0 | 1213 | |
michael@0 | 1214 | do |
michael@0 | 1215 | { |
michael@0 | 1216 | int k = 0; |
michael@0 | 1217 | |
michael@0 | 1218 | do |
michael@0 | 1219 | { |
michael@0 | 1220 | int t = 0; /* token/prob index */ |
michael@0 | 1221 | |
michael@0 | 1222 | do |
michael@0 | 1223 | { |
michael@0 | 1224 | const vp8_prob newp = cpi->common.fc.coef_probs [i][j][k][t]; |
michael@0 | 1225 | const vp8_prob upd = vp8_coef_update_probs [i][j][k][t]; |
michael@0 | 1226 | |
michael@0 | 1227 | const char u = cpi->update_probs[i][j][k][t] ; |
michael@0 | 1228 | |
michael@0 | 1229 | vp8_write(w, u, upd); |
michael@0 | 1230 | |
michael@0 | 1231 | if (u) |
michael@0 | 1232 | { |
michael@0 | 1233 | /* send/use new probability */ |
michael@0 | 1234 | vp8_write_literal(w, newp, 8); |
michael@0 | 1235 | } |
michael@0 | 1236 | } |
michael@0 | 1237 | while (++t < ENTROPY_NODES); |
michael@0 | 1238 | } |
michael@0 | 1239 | while (++k < PREV_COEF_CONTEXTS); |
michael@0 | 1240 | } |
michael@0 | 1241 | while (++j < COEF_BANDS); |
michael@0 | 1242 | } |
michael@0 | 1243 | while (++i < BLOCK_TYPES); |
michael@0 | 1244 | } |
michael@0 | 1245 | #endif |
michael@0 | 1246 | |
michael@0 | 1247 | #ifdef PACKET_TESTING |
michael@0 | 1248 | FILE *vpxlogc = 0; |
michael@0 | 1249 | #endif |
michael@0 | 1250 | |
michael@0 | 1251 | static void put_delta_q(vp8_writer *bc, int delta_q) |
michael@0 | 1252 | { |
michael@0 | 1253 | if (delta_q != 0) |
michael@0 | 1254 | { |
michael@0 | 1255 | vp8_write_bit(bc, 1); |
michael@0 | 1256 | vp8_write_literal(bc, abs(delta_q), 4); |
michael@0 | 1257 | |
michael@0 | 1258 | if (delta_q < 0) |
michael@0 | 1259 | vp8_write_bit(bc, 1); |
michael@0 | 1260 | else |
michael@0 | 1261 | vp8_write_bit(bc, 0); |
michael@0 | 1262 | } |
michael@0 | 1263 | else |
michael@0 | 1264 | vp8_write_bit(bc, 0); |
michael@0 | 1265 | } |
michael@0 | 1266 | |
michael@0 | 1267 | void vp8_pack_bitstream(VP8_COMP *cpi, unsigned char *dest, unsigned char * dest_end, unsigned long *size) |
michael@0 | 1268 | { |
michael@0 | 1269 | int i, j; |
michael@0 | 1270 | VP8_HEADER oh; |
michael@0 | 1271 | VP8_COMMON *const pc = & cpi->common; |
michael@0 | 1272 | vp8_writer *const bc = cpi->bc; |
michael@0 | 1273 | MACROBLOCKD *const xd = & cpi->mb.e_mbd; |
michael@0 | 1274 | int extra_bytes_packed = 0; |
michael@0 | 1275 | |
michael@0 | 1276 | unsigned char *cx_data = dest; |
michael@0 | 1277 | unsigned char *cx_data_end = dest_end; |
michael@0 | 1278 | const int *mb_feature_data_bits; |
michael@0 | 1279 | |
michael@0 | 1280 | oh.show_frame = (int) pc->show_frame; |
michael@0 | 1281 | oh.type = (int)pc->frame_type; |
michael@0 | 1282 | oh.version = pc->version; |
michael@0 | 1283 | oh.first_partition_length_in_bytes = 0; |
michael@0 | 1284 | |
michael@0 | 1285 | mb_feature_data_bits = vp8_mb_feature_data_bits; |
michael@0 | 1286 | |
michael@0 | 1287 | bc[0].error = &pc->error; |
michael@0 | 1288 | |
michael@0 | 1289 | validate_buffer(cx_data, 3, cx_data_end, &cpi->common.error); |
michael@0 | 1290 | cx_data += 3; |
michael@0 | 1291 | |
michael@0 | 1292 | #if defined(SECTIONBITS_OUTPUT) |
michael@0 | 1293 | Sectionbits[active_section = 1] += sizeof(VP8_HEADER) * 8 * 256; |
michael@0 | 1294 | #endif |
michael@0 | 1295 | |
michael@0 | 1296 | /* every keyframe send startcode, width, height, scale factor, clamp |
michael@0 | 1297 | * and color type |
michael@0 | 1298 | */ |
michael@0 | 1299 | if (oh.type == KEY_FRAME) |
michael@0 | 1300 | { |
michael@0 | 1301 | int v; |
michael@0 | 1302 | |
michael@0 | 1303 | validate_buffer(cx_data, 7, cx_data_end, &cpi->common.error); |
michael@0 | 1304 | |
michael@0 | 1305 | /* Start / synch code */ |
michael@0 | 1306 | cx_data[0] = 0x9D; |
michael@0 | 1307 | cx_data[1] = 0x01; |
michael@0 | 1308 | cx_data[2] = 0x2a; |
michael@0 | 1309 | |
michael@0 | 1310 | v = (pc->horiz_scale << 14) | pc->Width; |
michael@0 | 1311 | cx_data[3] = v; |
michael@0 | 1312 | cx_data[4] = v >> 8; |
michael@0 | 1313 | |
michael@0 | 1314 | v = (pc->vert_scale << 14) | pc->Height; |
michael@0 | 1315 | cx_data[5] = v; |
michael@0 | 1316 | cx_data[6] = v >> 8; |
michael@0 | 1317 | |
michael@0 | 1318 | |
michael@0 | 1319 | extra_bytes_packed = 7; |
michael@0 | 1320 | cx_data += extra_bytes_packed ; |
michael@0 | 1321 | |
michael@0 | 1322 | vp8_start_encode(bc, cx_data, cx_data_end); |
michael@0 | 1323 | |
michael@0 | 1324 | /* signal clr type */ |
michael@0 | 1325 | vp8_write_bit(bc, 0); |
michael@0 | 1326 | vp8_write_bit(bc, pc->clamp_type); |
michael@0 | 1327 | |
michael@0 | 1328 | } |
michael@0 | 1329 | else |
michael@0 | 1330 | vp8_start_encode(bc, cx_data, cx_data_end); |
michael@0 | 1331 | |
michael@0 | 1332 | |
michael@0 | 1333 | /* Signal whether or not Segmentation is enabled */ |
michael@0 | 1334 | vp8_write_bit(bc, xd->segmentation_enabled); |
michael@0 | 1335 | |
michael@0 | 1336 | /* Indicate which features are enabled */ |
michael@0 | 1337 | if (xd->segmentation_enabled) |
michael@0 | 1338 | { |
michael@0 | 1339 | /* Signal whether or not the segmentation map is being updated. */ |
michael@0 | 1340 | vp8_write_bit(bc, xd->update_mb_segmentation_map); |
michael@0 | 1341 | vp8_write_bit(bc, xd->update_mb_segmentation_data); |
michael@0 | 1342 | |
michael@0 | 1343 | if (xd->update_mb_segmentation_data) |
michael@0 | 1344 | { |
michael@0 | 1345 | signed char Data; |
michael@0 | 1346 | |
michael@0 | 1347 | vp8_write_bit(bc, xd->mb_segement_abs_delta); |
michael@0 | 1348 | |
michael@0 | 1349 | /* For each segmentation feature (Quant and loop filter level) */ |
michael@0 | 1350 | for (i = 0; i < MB_LVL_MAX; i++) |
michael@0 | 1351 | { |
michael@0 | 1352 | /* For each of the segments */ |
michael@0 | 1353 | for (j = 0; j < MAX_MB_SEGMENTS; j++) |
michael@0 | 1354 | { |
michael@0 | 1355 | Data = xd->segment_feature_data[i][j]; |
michael@0 | 1356 | |
michael@0 | 1357 | /* Frame level data */ |
michael@0 | 1358 | if (Data) |
michael@0 | 1359 | { |
michael@0 | 1360 | vp8_write_bit(bc, 1); |
michael@0 | 1361 | |
michael@0 | 1362 | if (Data < 0) |
michael@0 | 1363 | { |
michael@0 | 1364 | Data = - Data; |
michael@0 | 1365 | vp8_write_literal(bc, Data, mb_feature_data_bits[i]); |
michael@0 | 1366 | vp8_write_bit(bc, 1); |
michael@0 | 1367 | } |
michael@0 | 1368 | else |
michael@0 | 1369 | { |
michael@0 | 1370 | vp8_write_literal(bc, Data, mb_feature_data_bits[i]); |
michael@0 | 1371 | vp8_write_bit(bc, 0); |
michael@0 | 1372 | } |
michael@0 | 1373 | } |
michael@0 | 1374 | else |
michael@0 | 1375 | vp8_write_bit(bc, 0); |
michael@0 | 1376 | } |
michael@0 | 1377 | } |
michael@0 | 1378 | } |
michael@0 | 1379 | |
michael@0 | 1380 | if (xd->update_mb_segmentation_map) |
michael@0 | 1381 | { |
michael@0 | 1382 | /* Write the probs used to decode the segment id for each mb */ |
michael@0 | 1383 | for (i = 0; i < MB_FEATURE_TREE_PROBS; i++) |
michael@0 | 1384 | { |
michael@0 | 1385 | int Data = xd->mb_segment_tree_probs[i]; |
michael@0 | 1386 | |
michael@0 | 1387 | if (Data != 255) |
michael@0 | 1388 | { |
michael@0 | 1389 | vp8_write_bit(bc, 1); |
michael@0 | 1390 | vp8_write_literal(bc, Data, 8); |
michael@0 | 1391 | } |
michael@0 | 1392 | else |
michael@0 | 1393 | vp8_write_bit(bc, 0); |
michael@0 | 1394 | } |
michael@0 | 1395 | } |
michael@0 | 1396 | } |
michael@0 | 1397 | |
michael@0 | 1398 | vp8_write_bit(bc, pc->filter_type); |
michael@0 | 1399 | vp8_write_literal(bc, pc->filter_level, 6); |
michael@0 | 1400 | vp8_write_literal(bc, pc->sharpness_level, 3); |
michael@0 | 1401 | |
michael@0 | 1402 | /* Write out loop filter deltas applied at the MB level based on mode |
michael@0 | 1403 | * or ref frame (if they are enabled). |
michael@0 | 1404 | */ |
michael@0 | 1405 | vp8_write_bit(bc, xd->mode_ref_lf_delta_enabled); |
michael@0 | 1406 | |
michael@0 | 1407 | if (xd->mode_ref_lf_delta_enabled) |
michael@0 | 1408 | { |
michael@0 | 1409 | /* Do the deltas need to be updated */ |
michael@0 | 1410 | int send_update = xd->mode_ref_lf_delta_update |
michael@0 | 1411 | || cpi->oxcf.error_resilient_mode; |
michael@0 | 1412 | |
michael@0 | 1413 | vp8_write_bit(bc, send_update); |
michael@0 | 1414 | if (send_update) |
michael@0 | 1415 | { |
michael@0 | 1416 | int Data; |
michael@0 | 1417 | |
michael@0 | 1418 | /* Send update */ |
michael@0 | 1419 | for (i = 0; i < MAX_REF_LF_DELTAS; i++) |
michael@0 | 1420 | { |
michael@0 | 1421 | Data = xd->ref_lf_deltas[i]; |
michael@0 | 1422 | |
michael@0 | 1423 | /* Frame level data */ |
michael@0 | 1424 | if (xd->ref_lf_deltas[i] != xd->last_ref_lf_deltas[i] |
michael@0 | 1425 | || cpi->oxcf.error_resilient_mode) |
michael@0 | 1426 | { |
michael@0 | 1427 | xd->last_ref_lf_deltas[i] = xd->ref_lf_deltas[i]; |
michael@0 | 1428 | vp8_write_bit(bc, 1); |
michael@0 | 1429 | |
michael@0 | 1430 | if (Data > 0) |
michael@0 | 1431 | { |
michael@0 | 1432 | vp8_write_literal(bc, (Data & 0x3F), 6); |
michael@0 | 1433 | vp8_write_bit(bc, 0); /* sign */ |
michael@0 | 1434 | } |
michael@0 | 1435 | else |
michael@0 | 1436 | { |
michael@0 | 1437 | Data = -Data; |
michael@0 | 1438 | vp8_write_literal(bc, (Data & 0x3F), 6); |
michael@0 | 1439 | vp8_write_bit(bc, 1); /* sign */ |
michael@0 | 1440 | } |
michael@0 | 1441 | } |
michael@0 | 1442 | else |
michael@0 | 1443 | vp8_write_bit(bc, 0); |
michael@0 | 1444 | } |
michael@0 | 1445 | |
michael@0 | 1446 | /* Send update */ |
michael@0 | 1447 | for (i = 0; i < MAX_MODE_LF_DELTAS; i++) |
michael@0 | 1448 | { |
michael@0 | 1449 | Data = xd->mode_lf_deltas[i]; |
michael@0 | 1450 | |
michael@0 | 1451 | if (xd->mode_lf_deltas[i] != xd->last_mode_lf_deltas[i] |
michael@0 | 1452 | || cpi->oxcf.error_resilient_mode) |
michael@0 | 1453 | { |
michael@0 | 1454 | xd->last_mode_lf_deltas[i] = xd->mode_lf_deltas[i]; |
michael@0 | 1455 | vp8_write_bit(bc, 1); |
michael@0 | 1456 | |
michael@0 | 1457 | if (Data > 0) |
michael@0 | 1458 | { |
michael@0 | 1459 | vp8_write_literal(bc, (Data & 0x3F), 6); |
michael@0 | 1460 | vp8_write_bit(bc, 0); /* sign */ |
michael@0 | 1461 | } |
michael@0 | 1462 | else |
michael@0 | 1463 | { |
michael@0 | 1464 | Data = -Data; |
michael@0 | 1465 | vp8_write_literal(bc, (Data & 0x3F), 6); |
michael@0 | 1466 | vp8_write_bit(bc, 1); /* sign */ |
michael@0 | 1467 | } |
michael@0 | 1468 | } |
michael@0 | 1469 | else |
michael@0 | 1470 | vp8_write_bit(bc, 0); |
michael@0 | 1471 | } |
michael@0 | 1472 | } |
michael@0 | 1473 | } |
michael@0 | 1474 | |
michael@0 | 1475 | /* signal here is multi token partition is enabled */ |
michael@0 | 1476 | vp8_write_literal(bc, pc->multi_token_partition, 2); |
michael@0 | 1477 | |
michael@0 | 1478 | /* Frame Qbaseline quantizer index */ |
michael@0 | 1479 | vp8_write_literal(bc, pc->base_qindex, 7); |
michael@0 | 1480 | |
michael@0 | 1481 | /* Transmit Dc, Second order and Uv quantizer delta information */ |
michael@0 | 1482 | put_delta_q(bc, pc->y1dc_delta_q); |
michael@0 | 1483 | put_delta_q(bc, pc->y2dc_delta_q); |
michael@0 | 1484 | put_delta_q(bc, pc->y2ac_delta_q); |
michael@0 | 1485 | put_delta_q(bc, pc->uvdc_delta_q); |
michael@0 | 1486 | put_delta_q(bc, pc->uvac_delta_q); |
michael@0 | 1487 | |
michael@0 | 1488 | /* When there is a key frame all reference buffers are updated using |
michael@0 | 1489 | * the new key frame |
michael@0 | 1490 | */ |
michael@0 | 1491 | if (pc->frame_type != KEY_FRAME) |
michael@0 | 1492 | { |
michael@0 | 1493 | /* Should the GF or ARF be updated using the transmitted frame |
michael@0 | 1494 | * or buffer |
michael@0 | 1495 | */ |
michael@0 | 1496 | vp8_write_bit(bc, pc->refresh_golden_frame); |
michael@0 | 1497 | vp8_write_bit(bc, pc->refresh_alt_ref_frame); |
michael@0 | 1498 | |
michael@0 | 1499 | /* If not being updated from current frame should either GF or ARF |
michael@0 | 1500 | * be updated from another buffer |
michael@0 | 1501 | */ |
michael@0 | 1502 | if (!pc->refresh_golden_frame) |
michael@0 | 1503 | vp8_write_literal(bc, pc->copy_buffer_to_gf, 2); |
michael@0 | 1504 | |
michael@0 | 1505 | if (!pc->refresh_alt_ref_frame) |
michael@0 | 1506 | vp8_write_literal(bc, pc->copy_buffer_to_arf, 2); |
michael@0 | 1507 | |
michael@0 | 1508 | /* Indicate reference frame sign bias for Golden and ARF frames |
michael@0 | 1509 | * (always 0 for last frame buffer) |
michael@0 | 1510 | */ |
michael@0 | 1511 | vp8_write_bit(bc, pc->ref_frame_sign_bias[GOLDEN_FRAME]); |
michael@0 | 1512 | vp8_write_bit(bc, pc->ref_frame_sign_bias[ALTREF_FRAME]); |
michael@0 | 1513 | } |
michael@0 | 1514 | |
michael@0 | 1515 | #if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING) |
michael@0 | 1516 | if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS) |
michael@0 | 1517 | { |
michael@0 | 1518 | if (pc->frame_type == KEY_FRAME) |
michael@0 | 1519 | pc->refresh_entropy_probs = 1; |
michael@0 | 1520 | else |
michael@0 | 1521 | pc->refresh_entropy_probs = 0; |
michael@0 | 1522 | } |
michael@0 | 1523 | #endif |
michael@0 | 1524 | |
michael@0 | 1525 | vp8_write_bit(bc, pc->refresh_entropy_probs); |
michael@0 | 1526 | |
michael@0 | 1527 | if (pc->frame_type != KEY_FRAME) |
michael@0 | 1528 | vp8_write_bit(bc, pc->refresh_last_frame); |
michael@0 | 1529 | |
michael@0 | 1530 | #ifdef VP8_ENTROPY_STATS |
michael@0 | 1531 | |
michael@0 | 1532 | if (pc->frame_type == INTER_FRAME) |
michael@0 | 1533 | active_section = 0; |
michael@0 | 1534 | else |
michael@0 | 1535 | active_section = 7; |
michael@0 | 1536 | |
michael@0 | 1537 | #endif |
michael@0 | 1538 | |
michael@0 | 1539 | vp8_clear_system_state(); |
michael@0 | 1540 | |
michael@0 | 1541 | #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING |
michael@0 | 1542 | pack_coef_probs(cpi); |
michael@0 | 1543 | #else |
michael@0 | 1544 | if (pc->refresh_entropy_probs == 0) |
michael@0 | 1545 | { |
michael@0 | 1546 | /* save a copy for later refresh */ |
michael@0 | 1547 | vpx_memcpy(&cpi->common.lfc, &cpi->common.fc, sizeof(cpi->common.fc)); |
michael@0 | 1548 | } |
michael@0 | 1549 | |
michael@0 | 1550 | vp8_update_coef_probs(cpi); |
michael@0 | 1551 | #endif |
michael@0 | 1552 | |
michael@0 | 1553 | #ifdef VP8_ENTROPY_STATS |
michael@0 | 1554 | active_section = 2; |
michael@0 | 1555 | #endif |
michael@0 | 1556 | |
michael@0 | 1557 | /* Write out the mb_no_coeff_skip flag */ |
michael@0 | 1558 | vp8_write_bit(bc, pc->mb_no_coeff_skip); |
michael@0 | 1559 | |
michael@0 | 1560 | if (pc->frame_type == KEY_FRAME) |
michael@0 | 1561 | { |
michael@0 | 1562 | write_kfmodes(cpi); |
michael@0 | 1563 | |
michael@0 | 1564 | #ifdef VP8_ENTROPY_STATS |
michael@0 | 1565 | active_section = 8; |
michael@0 | 1566 | #endif |
michael@0 | 1567 | } |
michael@0 | 1568 | else |
michael@0 | 1569 | { |
michael@0 | 1570 | pack_inter_mode_mvs(cpi); |
michael@0 | 1571 | |
michael@0 | 1572 | #ifdef VP8_ENTROPY_STATS |
michael@0 | 1573 | active_section = 1; |
michael@0 | 1574 | #endif |
michael@0 | 1575 | } |
michael@0 | 1576 | |
michael@0 | 1577 | vp8_stop_encode(bc); |
michael@0 | 1578 | |
michael@0 | 1579 | cx_data += bc->pos; |
michael@0 | 1580 | |
michael@0 | 1581 | oh.first_partition_length_in_bytes = cpi->bc->pos; |
michael@0 | 1582 | |
michael@0 | 1583 | /* update frame tag */ |
michael@0 | 1584 | { |
michael@0 | 1585 | int v = (oh.first_partition_length_in_bytes << 5) | |
michael@0 | 1586 | (oh.show_frame << 4) | |
michael@0 | 1587 | (oh.version << 1) | |
michael@0 | 1588 | oh.type; |
michael@0 | 1589 | |
michael@0 | 1590 | dest[0] = v; |
michael@0 | 1591 | dest[1] = v >> 8; |
michael@0 | 1592 | dest[2] = v >> 16; |
michael@0 | 1593 | } |
michael@0 | 1594 | |
michael@0 | 1595 | *size = VP8_HEADER_SIZE + extra_bytes_packed + cpi->bc->pos; |
michael@0 | 1596 | |
michael@0 | 1597 | cpi->partition_sz[0] = *size; |
michael@0 | 1598 | |
michael@0 | 1599 | #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING |
michael@0 | 1600 | { |
michael@0 | 1601 | const int num_part = (1 << pc->multi_token_partition); |
michael@0 | 1602 | unsigned char * dp = cpi->partition_d[0] + cpi->partition_sz[0]; |
michael@0 | 1603 | |
michael@0 | 1604 | if (num_part > 1) |
michael@0 | 1605 | { |
michael@0 | 1606 | /* write token part sizes (all but last) if more than 1 */ |
michael@0 | 1607 | validate_buffer(dp, 3 * (num_part - 1), cpi->partition_d_end[0], |
michael@0 | 1608 | &pc->error); |
michael@0 | 1609 | |
michael@0 | 1610 | cpi->partition_sz[0] += 3*(num_part-1); |
michael@0 | 1611 | |
michael@0 | 1612 | for(i = 1; i < num_part; i++) |
michael@0 | 1613 | { |
michael@0 | 1614 | write_partition_size(dp, cpi->partition_sz[i]); |
michael@0 | 1615 | dp += 3; |
michael@0 | 1616 | } |
michael@0 | 1617 | } |
michael@0 | 1618 | |
michael@0 | 1619 | if (!cpi->output_partition) |
michael@0 | 1620 | { |
michael@0 | 1621 | /* concatenate partition buffers */ |
michael@0 | 1622 | for(i = 0; i < num_part; i++) |
michael@0 | 1623 | { |
michael@0 | 1624 | vpx_memmove(dp, cpi->partition_d[i+1], cpi->partition_sz[i+1]); |
michael@0 | 1625 | cpi->partition_d[i+1] = dp; |
michael@0 | 1626 | dp += cpi->partition_sz[i+1]; |
michael@0 | 1627 | } |
michael@0 | 1628 | } |
michael@0 | 1629 | |
michael@0 | 1630 | /* update total size */ |
michael@0 | 1631 | *size = 0; |
michael@0 | 1632 | for(i = 0; i < num_part+1; i++) |
michael@0 | 1633 | { |
michael@0 | 1634 | *size += cpi->partition_sz[i]; |
michael@0 | 1635 | } |
michael@0 | 1636 | } |
michael@0 | 1637 | #else |
michael@0 | 1638 | if (pc->multi_token_partition != ONE_PARTITION) |
michael@0 | 1639 | { |
michael@0 | 1640 | int num_part = 1 << pc->multi_token_partition; |
michael@0 | 1641 | |
michael@0 | 1642 | /* partition size table at the end of first partition */ |
michael@0 | 1643 | cpi->partition_sz[0] += 3 * (num_part - 1); |
michael@0 | 1644 | *size += 3 * (num_part - 1); |
michael@0 | 1645 | |
michael@0 | 1646 | validate_buffer(cx_data, 3 * (num_part - 1), cx_data_end, |
michael@0 | 1647 | &pc->error); |
michael@0 | 1648 | |
michael@0 | 1649 | for(i = 1; i < num_part + 1; i++) |
michael@0 | 1650 | { |
michael@0 | 1651 | cpi->bc[i].error = &pc->error; |
michael@0 | 1652 | } |
michael@0 | 1653 | |
michael@0 | 1654 | pack_tokens_into_partitions(cpi, cx_data + 3 * (num_part - 1), |
michael@0 | 1655 | cx_data_end, num_part); |
michael@0 | 1656 | |
michael@0 | 1657 | for(i = 1; i < num_part; i++) |
michael@0 | 1658 | { |
michael@0 | 1659 | cpi->partition_sz[i] = cpi->bc[i].pos; |
michael@0 | 1660 | write_partition_size(cx_data, cpi->partition_sz[i]); |
michael@0 | 1661 | cx_data += 3; |
michael@0 | 1662 | *size += cpi->partition_sz[i]; /* add to total */ |
michael@0 | 1663 | } |
michael@0 | 1664 | |
michael@0 | 1665 | /* add last partition to total size */ |
michael@0 | 1666 | cpi->partition_sz[i] = cpi->bc[i].pos; |
michael@0 | 1667 | *size += cpi->partition_sz[i]; |
michael@0 | 1668 | } |
michael@0 | 1669 | else |
michael@0 | 1670 | { |
michael@0 | 1671 | bc[1].error = &pc->error; |
michael@0 | 1672 | |
michael@0 | 1673 | vp8_start_encode(&cpi->bc[1], cx_data, cx_data_end); |
michael@0 | 1674 | |
michael@0 | 1675 | #if CONFIG_MULTITHREAD |
michael@0 | 1676 | if (cpi->b_multi_threaded) |
michael@0 | 1677 | pack_mb_row_tokens(cpi, &cpi->bc[1]); |
michael@0 | 1678 | else |
michael@0 | 1679 | #endif |
michael@0 | 1680 | pack_tokens(&cpi->bc[1], cpi->tok, cpi->tok_count); |
michael@0 | 1681 | |
michael@0 | 1682 | vp8_stop_encode(&cpi->bc[1]); |
michael@0 | 1683 | |
michael@0 | 1684 | *size += cpi->bc[1].pos; |
michael@0 | 1685 | cpi->partition_sz[1] = cpi->bc[1].pos; |
michael@0 | 1686 | } |
michael@0 | 1687 | #endif |
michael@0 | 1688 | } |
michael@0 | 1689 | |
michael@0 | 1690 | #ifdef VP8_ENTROPY_STATS |
michael@0 | 1691 | void print_tree_update_probs() |
michael@0 | 1692 | { |
michael@0 | 1693 | int i, j, k, l; |
michael@0 | 1694 | FILE *f = fopen("context.c", "a"); |
michael@0 | 1695 | int Sum; |
michael@0 | 1696 | fprintf(f, "\n/* Update probabilities for token entropy tree. */\n\n"); |
michael@0 | 1697 | fprintf(f, "const vp8_prob tree_update_probs[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES] = {\n"); |
michael@0 | 1698 | |
michael@0 | 1699 | for (i = 0; i < BLOCK_TYPES; i++) |
michael@0 | 1700 | { |
michael@0 | 1701 | fprintf(f, " { \n"); |
michael@0 | 1702 | |
michael@0 | 1703 | for (j = 0; j < COEF_BANDS; j++) |
michael@0 | 1704 | { |
michael@0 | 1705 | fprintf(f, " {\n"); |
michael@0 | 1706 | |
michael@0 | 1707 | for (k = 0; k < PREV_COEF_CONTEXTS; k++) |
michael@0 | 1708 | { |
michael@0 | 1709 | fprintf(f, " {"); |
michael@0 | 1710 | |
michael@0 | 1711 | for (l = 0; l < ENTROPY_NODES; l++) |
michael@0 | 1712 | { |
michael@0 | 1713 | Sum = tree_update_hist[i][j][k][l][0] + tree_update_hist[i][j][k][l][1]; |
michael@0 | 1714 | |
michael@0 | 1715 | if (Sum > 0) |
michael@0 | 1716 | { |
michael@0 | 1717 | if (((tree_update_hist[i][j][k][l][0] * 255) / Sum) > 0) |
michael@0 | 1718 | fprintf(f, "%3ld, ", (tree_update_hist[i][j][k][l][0] * 255) / Sum); |
michael@0 | 1719 | else |
michael@0 | 1720 | fprintf(f, "%3ld, ", 1); |
michael@0 | 1721 | } |
michael@0 | 1722 | else |
michael@0 | 1723 | fprintf(f, "%3ld, ", 128); |
michael@0 | 1724 | } |
michael@0 | 1725 | |
michael@0 | 1726 | fprintf(f, "},\n"); |
michael@0 | 1727 | } |
michael@0 | 1728 | |
michael@0 | 1729 | fprintf(f, " },\n"); |
michael@0 | 1730 | } |
michael@0 | 1731 | |
michael@0 | 1732 | fprintf(f, " },\n"); |
michael@0 | 1733 | } |
michael@0 | 1734 | |
michael@0 | 1735 | fprintf(f, "};\n"); |
michael@0 | 1736 | fclose(f); |
michael@0 | 1737 | } |
michael@0 | 1738 | #endif |