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 "dboolhuff.h"
13#include "vpx_ports/mem.h"
14#include "vpx_mem/vpx_mem.h"
15
16DECLARE_ALIGNED(16, const unsigned char, vp8dx_bitreader_norm[256]) =
17{
18    0, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
19    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
20    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
21    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
22    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
23    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
24    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
25    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
26};
27
28
29int vp8dx_start_decode_c(BOOL_DECODER *br, const unsigned char *source,
30                        unsigned int source_sz)
31{
32    br->user_buffer_end = source+source_sz;
33    br->user_buffer     = source;
34    br->value    = 0;
35    br->count    = -8;
36    br->range    = 255;
37
38    if (source_sz && !source)
39        return 1;
40
41    /* Populate the buffer */
42    vp8dx_bool_decoder_fill_c(br);
43
44    return 0;
45}
46
47
48void vp8dx_bool_decoder_fill_c(BOOL_DECODER *br)
49{
50    const unsigned char *bufptr;
51    const unsigned char *bufend;
52    VP8_BD_VALUE         value;
53    int                  count;
54    bufend = br->user_buffer_end;
55    bufptr = br->user_buffer;
56    value = br->value;
57    count = br->count;
58
59    VP8DX_BOOL_DECODER_FILL(count, value, bufptr, bufend);
60
61    br->user_buffer = bufptr;
62    br->value = value;
63    br->count = count;
64}
65
66#if 0
67/*
68 * Until optimized versions of these functions are available, we
69 * keep the implementation in the header to allow inlining.
70 *
71 * The RTCD-style invocations are still in place so this can
72 * be switched by just uncommenting these functions here and
73 * the DBOOLHUFF_INVOKE calls in the header.
74 */
75int vp8dx_decode_bool_c(BOOL_DECODER *br, int probability)
76{
77    unsigned int bit=0;
78    VP8_BD_VALUE value;
79    unsigned int split;
80    VP8_BD_VALUE bigsplit;
81    int count;
82    unsigned int range;
83
84    value = br->value;
85    count = br->count;
86    range = br->range;
87
88    split = 1 + (((range-1) * probability) >> 8);
89    bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8);
90
91    range = split;
92    if(value >= bigsplit)
93    {
94        range = br->range-split;
95        value = value-bigsplit;
96        bit = 1;
97    }
98
99    /*if(range>=0x80)
100    {
101        br->value = value;
102        br->range = range;
103        return bit;
104    }*/
105
106    {
107        register unsigned int shift = vp8dx_bitreader_norm[range];
108        range <<= shift;
109        value <<= shift;
110        count -= shift;
111    }
112    br->value = value;
113    br->count = count;
114    br->range = range;
115    if (count < 0)
116        vp8dx_bool_decoder_fill_c(br);
117    return bit;
118}
119
120int vp8dx_decode_value_c(BOOL_DECODER *br, int bits)
121{
122    int z = 0;
123    int bit;
124    for ( bit=bits-1; bit>=0; bit-- )
125    {
126        z |= (vp8dx_decode_bool(br, 0x80)<<bit);
127    }
128    return z;
129}
130#endif
131