summaryrefslogtreecommitdiffstats
path: root/src/gallium/auxiliary/vl/vl_vlc.h
blob: 8c5b3aca47d0b83fd9a132f2456deeb719405977 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**************************************************************************
 *
 * Copyright 2011 Christian König.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/

/**
 * This file is based uppon slice_xvmc.c and vlc.h from the xine project,
 * which in turn is based on mpeg2dec. The following is the original copyright:
 *
 * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
 *
 * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
 * See http://libmpeg2.sourceforge.net/ for updates.
 *
 * mpeg2dec is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * mpeg2dec is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef vl_vlc_h
#define vl_vlc_h

struct vl_vlc
{
   uint32_t buf; /* current 32 bit working set of buffer */
   int bits;     /* used bits in working set */
   const uint8_t *ptr; /* buffer with stream data */
   const uint8_t *max; /* ptr+len of buffer */
};

static inline void
vl_vlc_restart(struct vl_vlc *vlc)
{
   vlc->buf = (vlc->ptr[0] << 24) | (vlc->ptr[1] << 16) | (vlc->ptr[2] << 8) | vlc->ptr[3];
   vlc->bits = -16;
   vlc->ptr += 4;
}

static inline void
vl_vlc_init(struct vl_vlc *vlc, const uint8_t *data, unsigned len)
{
   vlc->ptr = data;
   vlc->max = data + len;
   vl_vlc_restart(vlc);
}

static inline bool
vl_vlc_getbyte(struct vl_vlc *vlc)
{
   vlc->buf <<= 8;
   vlc->buf |= vlc->ptr[0];
   vlc->ptr++;
   return vlc->ptr < vlc->max;
}

#define vl_vlc_getword(vlc, shift)                                      \
do {                                                                    \
   (vlc)->buf |= (((vlc)->ptr[0] << 8) | (vlc)->ptr[1]) << (shift);     \
   (vlc)->ptr += 2;                                                     \
} while (0)

/* make sure that there are at least 16 valid bits in bit_buf */
#define vl_vlc_needbits(vlc)                    \
do {                                            \
    if ((vlc)->bits >= 0) {                      \
	vl_vlc_getword(vlc, (vlc)->bits);       \
	(vlc)->bits -= 16;                      \
    }                                           \
} while (0)

/* make sure that the full 32 bit of the buffer are valid */
static inline void
vl_vlc_need32bits(struct vl_vlc *vlc)
{
   vl_vlc_needbits(vlc);
   if (vlc->bits > -8) {
      unsigned n = -vlc->bits;
      vlc->buf <<= n;
      vlc->buf |= *vlc->ptr << 8;
      vlc->bits = -8;
      vlc->ptr++;
   }
   if (vlc->bits > -16) {
      unsigned n = -vlc->bits - 8;
      vlc->buf <<= n;
      vlc->buf |= *vlc->ptr;
      vlc->bits = -16;
      vlc->ptr++;
   }
}

/* remove num valid bits from bit_buf */
#define vl_vlc_dumpbits(vlc, num)       \
do {					\
    (vlc)->buf <<= (num);		\
    (vlc)->bits += (num);		\
} while (0)

/* take num bits from the high part of bit_buf and zero extend them */
#define vl_vlc_ubits(vlc, num) (((uint32_t)((vlc)->buf)) >> (32 - (num)))

/* take num bits from the high part of bit_buf and sign extend them */
#define vl_vlc_sbits(vlc, num) (((int32_t)((vlc)->buf)) >> (32 - (num)))

#endif /* vl_vlc_h */