root/vic/trunk/huffman.h @ 963

Revision 963, 3.7 KB (checked in by piers, 15 years ago)

Added for pvh codec

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2 * Copyright (c) 1993-1994 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *      This product includes software developed by the Network Research
16 *      Group at Lawrence Berkeley Laboratory.
17 * 4. Neither the name of the University nor of the Laboratory may be used
18 *    to endorse or promote products derived from this software without
19 *    specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#) $Header$ (LBL)
34 */
35
36#ifndef lib_huffman_h
37#define lib_huffman_h
38
39struct huffent {
40        int val;
41        int nb;
42};
43
44#define HUFF_SYM_ILLEGAL (0x8000 >> 5)
45#define HUFF_MASK(s) ((1 << (s)) - 1)
46
47/*
48 * Read the next 16 bits off the bit string into the bit buffer.
49 * Skip over zero-stuffed ff's but make no attempt to verify
50 * that they aren't some other marker (which should not be in the
51 * middle of a block anyway).
52 */
53#define HUFF_READ(bb, bs) \
54{ \
55        register int v; \
56        register const u_char *cp = (bs); \
57 \
58        bb <<= 16; \
59        v = *cp++; \
60        bb |= v << 8; \
61        v = *cp++; \
62        bb |= v; \
63        (bs) = cp; \
64 \
65}
66
67#define GET_BITS(n, nbb, bb, bs, result) \
68{ \
69        nbb -= n; \
70        if (nbb < 0) { \
71                HUFF_READ(bb, bs); \
72                nbb += 16; \
73        } \
74        (result) = (((bb) >> (nbb)) & HUFF_MASK(n)); \
75/*printf("gb %d(%x)\n", n, (result));*/\
76}
77
78#define SKIP_BITS(n, nbb, bb) \
79{ \
80        nbb -= n; \
81        if (nbb < 0)  { \
82                HUFFRQ(bb); \
83                nbb += 16; \
84        } \
85}
86
87#define HUFF_DECODE(ht, maxlen, nbb, bb, bs, result) { \
88        register int s__, v__; \
89 \
90         /*XXX*/ \
91        while (nbb < maxlen) { \
92                bb <<= 8; \
93                bb |= *(bs)++; \
94                nbb += 8; \
95        } \
96        s__ = maxlen; \
97        v__ = (bb >> (nbb - s__)) & HUFF_MASK(s__); \
98        s__ = (ht)[v__]; \
99        nbb -= (s__ & 0x1f); \
100        result = s__ >> 5; \
101}
102
103/*XXX use 64-bits if available */
104#define HUFF_NBIT 32
105
106#if BYTE_ORDER == LITTLE_ENDIAN
107#define HUFF_STORE_BITS(bs, bb) \
108        ((u_char*)bs)[0] = bb >> 24; \
109        ((u_char*)bs)[1] = bb >> 16; \
110        ((u_char*)bs)[2] = bb >> 8; \
111        ((u_char*)bs)[3] = bb;
112#else
113#define HUFF_STORE_BITS(bs, bb) *(u_int*)(bs) = (bb);
114#endif
115
116#define PUT_BITS(bits, n, nbb, bb, bs) \
117{ \
118if (0) printf("pb %d(%x %d%d%d)\n", n, bits, (bits >> 2) & 1, (bits >> 1) & 1, bits & 1); \
119        nbb += (n); \
120        if (nbb > HUFF_NBIT)  { \
121                int extra = (nbb) - HUFF_NBIT; \
122                bb |= (bits) >> extra; \
123                HUFF_STORE_BITS(bs, bb) \
124                bs += (HUFF_NBIT / 8); \
125                bb = (bits) << HUFF_NBIT - extra; \
126                nbb = extra; \
127        } else \
128                bb |= (bits) << HUFF_NBIT - (nbb); \
129}
130
131#endif
Note: See TracBrowser for help on using the browser.