Merge remote-tracking branch 'origin/master' into pipe-video
[mesa.git] / src / gallium / auxiliary / vl / vl_vlc.h
1 /**************************************************************************
2 *
3 * Copyright 2011 Christian König.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * This file is based uppon slice_xvmc.c and vlc.h from the xine project,
30 * which in turn is based on mpeg2dec. The following is the original copyright:
31 *
32 * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
33 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
34 *
35 * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
36 * See http://libmpeg2.sourceforge.net/ for updates.
37 *
38 * mpeg2dec is free software; you can redistribute it and/or modify
39 * it under the terms of the GNU General Public License as published by
40 * the Free Software Foundation; either version 2 of the License, or
41 * (at your option) any later version.
42 *
43 * mpeg2dec is distributed in the hope that it will be useful,
44 * but WITHOUT ANY WARRANTY; without even the implied warranty of
45 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46 * GNU General Public License for more details.
47 *
48 * You should have received a copy of the GNU General Public License
49 * along with this program; if not, write to the Free Software
50 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
51 */
52
53 #ifndef vl_vlc_h
54 #define vl_vlc_h
55
56 struct vl_vlc
57 {
58 uint32_t buf; /* current 32 bit working set of buffer */
59 int bits; /* used bits in working set */
60 const uint8_t *ptr; /* buffer with stream data */
61 const uint8_t *max; /* ptr+len of buffer */
62 };
63
64 static inline void
65 vl_vlc_restart(struct vl_vlc *vlc)
66 {
67 vlc->buf = (vlc->ptr[0] << 24) | (vlc->ptr[1] << 16) | (vlc->ptr[2] << 8) | vlc->ptr[3];
68 vlc->bits = -16;
69 vlc->ptr += 4;
70 }
71
72 static inline void
73 vl_vlc_init(struct vl_vlc *vlc, const uint8_t *data, unsigned len)
74 {
75 vlc->ptr = data;
76 vlc->max = data + len;
77 vl_vlc_restart(vlc);
78 }
79
80 static inline bool
81 vl_vlc_getbyte(struct vl_vlc *vlc)
82 {
83 vlc->buf <<= 8;
84 vlc->buf |= vlc->ptr[0];
85 vlc->ptr++;
86 return vlc->ptr < vlc->max;
87 }
88
89 #define vl_vlc_getword(vlc, shift) \
90 do { \
91 (vlc)->buf |= (((vlc)->ptr[0] << 8) | (vlc)->ptr[1]) << (shift); \
92 (vlc)->ptr += 2; \
93 } while (0)
94
95 /* make sure that there are at least 16 valid bits in bit_buf */
96 #define vl_vlc_needbits(vlc) \
97 do { \
98 if ((vlc)->bits >= 0) { \
99 vl_vlc_getword(vlc, (vlc)->bits); \
100 (vlc)->bits -= 16; \
101 } \
102 } while (0)
103
104 /* make sure that the full 32 bit of the buffer are valid */
105 static inline void
106 vl_vlc_need32bits(struct vl_vlc *vlc)
107 {
108 vl_vlc_needbits(vlc);
109 if (vlc->bits > -8) {
110 unsigned n = -vlc->bits;
111 vlc->buf <<= n;
112 vlc->buf |= *vlc->ptr << 8;
113 vlc->bits = -8;
114 vlc->ptr++;
115 }
116 if (vlc->bits > -16) {
117 unsigned n = -vlc->bits - 8;
118 vlc->buf <<= n;
119 vlc->buf |= *vlc->ptr;
120 vlc->bits = -16;
121 vlc->ptr++;
122 }
123 }
124
125 /* remove num valid bits from bit_buf */
126 #define vl_vlc_dumpbits(vlc, num) \
127 do { \
128 (vlc)->buf <<= (num); \
129 (vlc)->bits += (num); \
130 } while (0)
131
132 /* take num bits from the high part of bit_buf and zero extend them */
133 #define vl_vlc_ubits(vlc, num) (((uint32_t)((vlc)->buf)) >> (32 - (num)))
134
135 /* take num bits from the high part of bit_buf and sign extend them */
136 #define vl_vlc_sbits(vlc, num) (((int32_t)((vlc)->buf)) >> (32 - (num)))
137
138 #endif /* vl_vlc_h */