1 /**************************************************************************
3 * Copyright 2011 Christian König.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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:
32 * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
33 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
35 * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
36 * See http://libmpeg2.sourceforge.net/ for updates.
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.
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.
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
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 */
65 vl_vlc_restart(struct vl_vlc
*vlc
)
67 vlc
->buf
= (vlc
->ptr
[0] << 24) | (vlc
->ptr
[1] << 16) | (vlc
->ptr
[2] << 8) | vlc
->ptr
[3];
73 vl_vlc_init(struct vl_vlc
*vlc
, const uint8_t *data
, unsigned len
)
76 vlc
->max
= data
+ len
;
81 vl_vlc_getbyte(struct vl_vlc
*vlc
)
84 vlc
->buf
|= vlc
->ptr
[0];
86 return vlc
->ptr
< vlc
->max
;
89 #define vl_vlc_getword(vlc, shift) \
91 (vlc)->buf |= (((vlc)->ptr[0] << 8) | (vlc)->ptr[1]) << (shift); \
95 /* make sure that there are at least 16 valid bits in bit_buf */
96 #define vl_vlc_needbits(vlc) \
98 if ((vlc)->bits >= 0) { \
99 vl_vlc_getword(vlc, (vlc)->bits); \
104 /* make sure that the full 32 bit of the buffer are valid */
106 vl_vlc_need32bits(struct vl_vlc
*vlc
)
108 vl_vlc_needbits(vlc
);
109 if (vlc
->bits
> -8) {
110 unsigned n
= -vlc
->bits
;
112 vlc
->buf
|= *vlc
->ptr
<< 8;
116 if (vlc
->bits
> -16) {
117 unsigned n
= -vlc
->bits
- 8;
119 vlc
->buf
|= *vlc
->ptr
;
125 /* remove num valid bits from bit_buf */
126 #define vl_vlc_dumpbits(vlc, num) \
128 (vlc)->buf <<= (num); \
129 (vlc)->bits += (num); \
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)))
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)))
138 #endif /* vl_vlc_h */