vl_vlc.h revision 49967950a56de276ffcbaea80acbc9f5bd3207bc
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#include "pipe/p_compiler.h" 57 58struct vl_vlc 59{ 60 uint32_t buf; /* current 32 bit working set of buffer */ 61 int bits; /* used bits in working set */ 62 const uint8_t *ptr; /* buffer with stream data */ 63 const uint8_t *max; /* ptr+len of buffer */ 64}; 65 66static INLINE void 67vl_vlc_restart(struct vl_vlc *vlc) 68{ 69 vlc->buf = (vlc->ptr[0] << 24) | (vlc->ptr[1] << 16) | (vlc->ptr[2] << 8) | vlc->ptr[3]; 70 vlc->bits = -16; 71 vlc->ptr += 4; 72} 73 74static INLINE void 75vl_vlc_init(struct vl_vlc *vlc, const uint8_t *data, unsigned len) 76{ 77 vlc->ptr = data; 78 vlc->max = data + len; 79 vl_vlc_restart(vlc); 80} 81 82static INLINE bool 83vl_vlc_getbyte(struct vl_vlc *vlc) 84{ 85 vlc->buf <<= 8; 86 vlc->buf |= vlc->ptr[0]; 87 vlc->ptr++; 88 return vlc->ptr < vlc->max; 89} 90 91#define vl_vlc_getword(vlc, shift) \ 92do { \ 93 (vlc)->buf |= (((vlc)->ptr[0] << 8) | (vlc)->ptr[1]) << (shift); \ 94 (vlc)->ptr += 2; \ 95} while (0) 96 97/* make sure that there are at least 16 valid bits in bit_buf */ 98#define vl_vlc_needbits(vlc) \ 99do { \ 100 if ((vlc)->bits >= 0) { \ 101 vl_vlc_getword(vlc, (vlc)->bits); \ 102 (vlc)->bits -= 16; \ 103 } \ 104} while (0) 105 106/* make sure that the full 32 bit of the buffer are valid */ 107static INLINE void 108vl_vlc_need32bits(struct vl_vlc *vlc) 109{ 110 vl_vlc_needbits(vlc); 111 if (vlc->bits > -8) { 112 unsigned n = -vlc->bits; 113 vlc->buf <<= n; 114 vlc->buf |= *vlc->ptr << 8; 115 vlc->bits = -8; 116 vlc->ptr++; 117 } 118 if (vlc->bits > -16) { 119 unsigned n = -vlc->bits - 8; 120 vlc->buf <<= n; 121 vlc->buf |= *vlc->ptr; 122 vlc->bits = -16; 123 vlc->ptr++; 124 } 125} 126 127/* remove num valid bits from bit_buf */ 128#define vl_vlc_dumpbits(vlc, num) \ 129do { \ 130 (vlc)->buf <<= (num); \ 131 (vlc)->bits += (num); \ 132} while (0) 133 134/* take num bits from the high part of bit_buf and zero extend them */ 135#define vl_vlc_ubits(vlc, num) (((uint32_t)((vlc)->buf)) >> (32 - (num))) 136 137/* take num bits from the high part of bit_buf and sign extend them */ 138#define vl_vlc_sbits(vlc, num) (((int32_t)((vlc)->buf)) >> (32 - (num))) 139 140#endif /* vl_vlc_h */ 141