dboolhuff.h 3.64 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5
6
 *  that can be found in the LICENSE file in the root of the source
 *  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.
John Koleszar's avatar
John Koleszar committed
9
10
11
 */


12
13
#ifndef VP8_DECODER_DBOOLHUFF_H_
#define VP8_DECODER_DBOOLHUFF_H_
Dmitry Kovalev's avatar
Dmitry Kovalev committed
14

15
16
#include <stddef.h>
#include <limits.h>
Dmitry Kovalev's avatar
Dmitry Kovalev committed
17

18
#include "./vpx_config.h"
John Koleszar's avatar
John Koleszar committed
19
#include "vpx_ports/mem.h"
20
#include "vpx/vp8dx.h"
21
#include "vpx/vpx_integer.h"
John Koleszar's avatar
John Koleszar committed
22

23
24
25
26
#ifdef __cplusplus
extern "C" {
#endif

27
28
typedef size_t VP8_BD_VALUE;

Dmitry Kovalev's avatar
Dmitry Kovalev committed
29
30
#define VP8_BD_VALUE_SIZE ((int)sizeof(VP8_BD_VALUE)*CHAR_BIT)

31
32
33
/*This is meant to be a large, positive constant that can still be efficiently
   loaded as an immediate (on platforms like ARM, for example).
  Even relatively modest values like 100 would work fine.*/
Dmitry Kovalev's avatar
Dmitry Kovalev committed
34
#define VP8_LOTS_OF_BITS (0x40000000)
35

John Koleszar's avatar
John Koleszar committed
36
37
typedef struct
{
38
    const unsigned char *user_buffer_end;
John Koleszar's avatar
John Koleszar committed
39
    const unsigned char *user_buffer;
40
41
42
    VP8_BD_VALUE         value;
    int                  count;
    unsigned int         range;
43
    vpx_decrypt_cb       decrypt_cb;
44
    void                *decrypt_state;
John Koleszar's avatar
John Koleszar committed
45
46
} BOOL_DECODER;

47
DECLARE_ALIGNED(16, extern const unsigned char, vp8_norm[256]);
John Koleszar's avatar
John Koleszar committed
48

Johann's avatar
Johann committed
49
50
int vp8dx_start_decode(BOOL_DECODER *br,
                       const unsigned char *source,
51
                       unsigned int source_sz,
52
                       vpx_decrypt_cb decrypt_cb,
53
                       void *decrypt_state);
Johann's avatar
Johann committed
54
55

void vp8dx_bool_decoder_fill(BOOL_DECODER *br);
56
57


John Koleszar's avatar
John Koleszar committed
58
59
static int vp8dx_decode_bool(BOOL_DECODER *br, int probability) {
    unsigned int bit = 0;
60
    VP8_BD_VALUE value;
John Koleszar's avatar
John Koleszar committed
61
    unsigned int split;
62
63
64
65
    VP8_BD_VALUE bigsplit;
    int count;
    unsigned int range;

Scott LaVarnway's avatar
Scott LaVarnway committed
66
67
68
69
70
    split = 1 + (((br->range - 1) * probability) >> 8);

    if(br->count < 0)
        vp8dx_bool_decoder_fill(br);

71
72
    value = br->value;
    count = br->count;
John Koleszar's avatar
John Koleszar committed
73

74
    bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8);
John Koleszar's avatar
John Koleszar committed
75
76
77
78
79
80
81
82
83
84
85

    range = split;

    if (value >= bigsplit)
    {
        range = br->range - split;
        value = value - bigsplit;
        bit = 1;
    }

    {
86
        register unsigned int shift = vp8_norm[range];
John Koleszar's avatar
John Koleszar committed
87
88
89
90
91
        range <<= shift;
        value <<= shift;
        count -= shift;
    }
    br->value = value;
92
    br->count = count;
John Koleszar's avatar
John Koleszar committed
93
    br->range = range;
Scott LaVarnway's avatar
Scott LaVarnway committed
94

John Koleszar's avatar
John Koleszar committed
95
96
97
    return bit;
}

98
static INLINE int vp8_decode_value(BOOL_DECODER *br, int bits)
John Koleszar's avatar
John Koleszar committed
99
100
101
102
103
104
105
106
107
108
109
{
    int z = 0;
    int bit;

    for (bit = bits - 1; bit >= 0; bit--)
    {
        z |= (vp8dx_decode_bool(br, 0x80) << bit);
    }

    return z;
}
110

111
static INLINE int vp8dx_bool_error(BOOL_DECODER *br)
112
{
113
114
115
116
117
118
119
120
121
122
123
124
125
    /* Check if we have reached the end of the buffer.
     *
     * Variable 'count' stores the number of bits in the 'value' buffer, minus
     * 8. The top byte is part of the algorithm, and the remainder is buffered
     * to be shifted into it. So if count == 8, the top 16 bits of 'value' are
     * occupied, 8 for the algorithm and 8 in the buffer.
     *
     * When reading a byte from the user's buffer, count is filled with 8 and
     * one byte is filled into the value buffer. When we reach the end of the
     * data, count is additionally filled with VP8_LOTS_OF_BITS. So when
     * count == VP8_LOTS_OF_BITS - 1, the user's data has been exhausted.
     */
    if ((br->count > VP8_BD_VALUE_SIZE) && (br->count < VP8_LOTS_OF_BITS))
126
127
128
129
130
131
132
133
134
135
    {
       /* We have tried to decode bits after the end of
        * stream was encountered.
        */
        return 1;
    }

    /* No error. */
    return 0;
}
Dmitry Kovalev's avatar
Dmitry Kovalev committed
136

137
138
139
140
#ifdef __cplusplus
}  // extern "C"
#endif

141
#endif  // VP8_DECODER_DBOOLHUFF_H_