OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2010 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 */ |
| 10 |
| 11 #include <assert.h> |
| 12 #include "vp9/encoder/vp9_boolhuff.h" |
| 13 |
| 14 #if defined(SECTIONBITS_OUTPUT) |
| 15 unsigned __int64 Sectionbits[500]; |
| 16 |
| 17 #endif |
| 18 |
| 19 #ifdef ENTROPY_STATS |
| 20 unsigned int active_section = 0; |
| 21 #endif |
| 22 |
| 23 const unsigned int vp9_prob_cost[256] = { |
| 24 2047, 2047, 1791, 1641, 1535, 1452, 1385, 1328, 1279, 1235, 1196, 1161, 1129,
1099, 1072, 1046, |
| 25 1023, 1000, 979, 959, 940, 922, 905, 889, 873, 858, 843, 829, 816,
803, 790, 778, |
| 26 767, 755, 744, 733, 723, 713, 703, 693, 684, 675, 666, 657, 649,
641, 633, 625, |
| 27 617, 609, 602, 594, 587, 580, 573, 567, 560, 553, 547, 541, 534,
528, 522, 516, |
| 28 511, 505, 499, 494, 488, 483, 477, 472, 467, 462, 457, 452, 447,
442, 437, 433, |
| 29 428, 424, 419, 415, 410, 406, 401, 397, 393, 389, 385, 381, 377,
373, 369, 365, |
| 30 361, 357, 353, 349, 346, 342, 338, 335, 331, 328, 324, 321, 317,
314, 311, 307, |
| 31 304, 301, 297, 294, 291, 288, 285, 281, 278, 275, 272, 269, 266,
263, 260, 257, |
| 32 255, 252, 249, 246, 243, 240, 238, 235, 232, 229, 227, 224, 221,
219, 216, 214, |
| 33 211, 208, 206, 203, 201, 198, 196, 194, 191, 189, 186, 184, 181,
179, 177, 174, |
| 34 172, 170, 168, 165, 163, 161, 159, 156, 154, 152, 150, 148, 145,
143, 141, 139, |
| 35 137, 135, 133, 131, 129, 127, 125, 123, 121, 119, 117, 115, 113,
111, 109, 107, |
| 36 105, 103, 101, 99, 97, 95, 93, 92, 90, 88, 86, 84, 82,
81, 79, 77, |
| 37 75, 73, 72, 70, 68, 66, 65, 63, 61, 60, 58, 56, 55,
53, 51, 50, |
| 38 48, 46, 45, 43, 41, 40, 38, 37, 35, 33, 32, 30, 29,
27, 25, 24, |
| 39 22, 21, 19, 18, 16, 15, 13, 12, 10, 9, 7, 6, 4,
3, 1, 1 |
| 40 }; |
| 41 |
| 42 void vp9_start_encode(BOOL_CODER *br, unsigned char *source) { |
| 43 |
| 44 br->lowvalue = 0; |
| 45 br->range = 255; |
| 46 br->value = 0; |
| 47 br->count = -24; |
| 48 br->buffer = source; |
| 49 br->pos = 0; |
| 50 } |
| 51 |
| 52 void vp9_stop_encode(BOOL_CODER *br) { |
| 53 int i; |
| 54 |
| 55 for (i = 0; i < 32; i++) |
| 56 encode_bool(br, 0, 128); |
| 57 } |
| 58 |
| 59 |
| 60 void vp9_encode_value(BOOL_CODER *br, int data, int bits) { |
| 61 int bit; |
| 62 |
| 63 for (bit = bits - 1; bit >= 0; bit--) |
| 64 encode_bool(br, (1 & (data >> bit)), 0x80); |
| 65 } |
| 66 |
| 67 void vp9_encode_unsigned_max(BOOL_CODER *br, int data, int max) { |
| 68 assert(data <= max); |
| 69 while (max) { |
| 70 encode_bool(br, data & 1, 128); |
| 71 data >>= 1; |
| 72 max >>= 1; |
| 73 } |
| 74 } |
| 75 |
| 76 int vp9_recenter_nonneg(int v, int m) { |
| 77 if (v > (m << 1)) return v; |
| 78 else if (v >= m) return ((v - m) << 1); |
| 79 else return ((m - v) << 1) - 1; |
| 80 } |
| 81 |
| 82 static int get_unsigned_bits(unsigned num_values) { |
| 83 int cat = 0; |
| 84 if ((num_values--) <= 1) return 0; |
| 85 while (num_values > 0) { |
| 86 cat++; |
| 87 num_values >>= 1; |
| 88 } |
| 89 return cat; |
| 90 } |
| 91 |
| 92 void vp9_encode_uniform(BOOL_CODER *br, int v, int n) { |
| 93 int l = get_unsigned_bits(n); |
| 94 int m; |
| 95 if (l == 0) return; |
| 96 m = (1 << l) - n; |
| 97 if (v < m) |
| 98 vp9_encode_value(br, v, l - 1); |
| 99 else { |
| 100 vp9_encode_value(br, m + ((v - m) >> 1), l - 1); |
| 101 vp9_encode_value(br, (v - m) & 1, 1); |
| 102 } |
| 103 } |
| 104 |
| 105 int vp9_count_uniform(int v, int n) { |
| 106 int l = get_unsigned_bits(n); |
| 107 int m; |
| 108 if (l == 0) return 0; |
| 109 m = (1 << l) - n; |
| 110 if (v < m) |
| 111 return l - 1; |
| 112 else |
| 113 return l; |
| 114 } |
| 115 |
| 116 void vp9_encode_term_subexp(BOOL_CODER *br, int word, int k, int num_syms) { |
| 117 int i = 0; |
| 118 int mk = 0; |
| 119 while (1) { |
| 120 int b = (i ? k + i - 1 : k); |
| 121 int a = (1 << b); |
| 122 if (num_syms <= mk + 3 * a) { |
| 123 vp9_encode_uniform(br, word - mk, num_syms - mk); |
| 124 break; |
| 125 } else { |
| 126 int t = (word >= mk + a); |
| 127 vp9_encode_value(br, t, 1); |
| 128 if (t) { |
| 129 i = i + 1; |
| 130 mk += a; |
| 131 } else { |
| 132 vp9_encode_value(br, word - mk, b); |
| 133 break; |
| 134 } |
| 135 } |
| 136 } |
| 137 } |
| 138 |
| 139 int vp9_count_term_subexp(int word, int k, int num_syms) { |
| 140 int count = 0; |
| 141 int i = 0; |
| 142 int mk = 0; |
| 143 while (1) { |
| 144 int b = (i ? k + i - 1 : k); |
| 145 int a = (1 << b); |
| 146 if (num_syms <= mk + 3 * a) { |
| 147 count += vp9_count_uniform(word - mk, num_syms - mk); |
| 148 break; |
| 149 } else { |
| 150 int t = (word >= mk + a); |
| 151 count++; |
| 152 if (t) { |
| 153 i = i + 1; |
| 154 mk += a; |
| 155 } else { |
| 156 count += b; |
| 157 break; |
| 158 } |
| 159 } |
| 160 } |
| 161 return count; |
| 162 } |
OLD | NEW |