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 |
| 12 #include "vp9/decoder/vp9_dboolhuff.h" |
| 13 #include "vpx_ports/mem.h" |
| 14 #include "vpx_mem/vpx_mem.h" |
| 15 |
| 16 int vp9_start_decode(BOOL_DECODER *br, |
| 17 const unsigned char *source, |
| 18 unsigned int source_sz) { |
| 19 br->user_buffer_end = source + source_sz; |
| 20 br->user_buffer = source; |
| 21 br->value = 0; |
| 22 br->count = -8; |
| 23 br->range = 255; |
| 24 |
| 25 if (source_sz && !source) |
| 26 return 1; |
| 27 |
| 28 /* Populate the buffer */ |
| 29 vp9_bool_decoder_fill(br); |
| 30 |
| 31 return 0; |
| 32 } |
| 33 |
| 34 |
| 35 void vp9_bool_decoder_fill(BOOL_DECODER *br) { |
| 36 const unsigned char *bufptr; |
| 37 const unsigned char *bufend; |
| 38 VP9_BD_VALUE value; |
| 39 int count; |
| 40 bufend = br->user_buffer_end; |
| 41 bufptr = br->user_buffer; |
| 42 value = br->value; |
| 43 count = br->count; |
| 44 |
| 45 VP9DX_BOOL_DECODER_FILL(count, value, bufptr, bufend); |
| 46 |
| 47 br->user_buffer = bufptr; |
| 48 br->value = value; |
| 49 br->count = count; |
| 50 } |
| 51 |
| 52 |
| 53 static int get_unsigned_bits(unsigned num_values) { |
| 54 int cat = 0; |
| 55 if ((num_values--) <= 1) return 0; |
| 56 while (num_values > 0) { |
| 57 cat++; |
| 58 num_values >>= 1; |
| 59 } |
| 60 return cat; |
| 61 } |
| 62 |
| 63 int vp9_inv_recenter_nonneg(int v, int m) { |
| 64 if (v > (m << 1)) return v; |
| 65 else if ((v & 1) == 0) return (v >> 1) + m; |
| 66 else return m - ((v + 1) >> 1); |
| 67 } |
| 68 |
| 69 int vp9_decode_uniform(BOOL_DECODER *br, int n) { |
| 70 int v; |
| 71 int l = get_unsigned_bits(n); |
| 72 int m = (1 << l) - n; |
| 73 if (!l) return 0; |
| 74 v = decode_value(br, l - 1); |
| 75 if (v < m) |
| 76 return v; |
| 77 else |
| 78 return (v << 1) - m + decode_value(br, 1); |
| 79 } |
| 80 |
| 81 int vp9_decode_term_subexp(BOOL_DECODER *br, int k, int num_syms) { |
| 82 int i = 0, mk = 0, word; |
| 83 while (1) { |
| 84 int b = (i ? k + i - 1 : k); |
| 85 int a = (1 << b); |
| 86 if (num_syms <= mk + 3 * a) { |
| 87 word = vp9_decode_uniform(br, num_syms - mk) + mk; |
| 88 break; |
| 89 } else { |
| 90 if (decode_value(br, 1)) { |
| 91 i++; |
| 92 mk += a; |
| 93 } else { |
| 94 word = decode_value(br, b) + mk; |
| 95 break; |
| 96 } |
| 97 } |
| 98 } |
| 99 return word; |
| 100 } |
| 101 |
| 102 int vp9_decode_unsigned_max(BOOL_DECODER *br, int max) { |
| 103 int data = 0, bit = 0, lmax = max; |
| 104 |
| 105 while (lmax) { |
| 106 data |= decode_bool(br, 128) << bit++; |
| 107 lmax >>= 1; |
| 108 } |
| 109 if (data > max) |
| 110 return max; |
| 111 return data; |
| 112 } |
OLD | NEW |