Path: blob/21.2-virgl/src/gallium/auxiliary/vl/vl_rbsp.h
4565 views
/**************************************************************************1*2* Copyright 2013 Advanced Micro Devices, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627/*28* Authors:29* Christian König <[email protected]>30*31*/3233/*34* Functions for reading the raw byte sequence payload of H.26435*/3637#ifndef vl_rbsp_h38#define vl_rbsp_h3940#include "vl/vl_vlc.h"4142struct vl_rbsp {43struct vl_vlc nal;44unsigned escaped;45};4647/**48* Initialize the RBSP object49*/50static inline void vl_rbsp_init(struct vl_rbsp *rbsp, struct vl_vlc *nal, unsigned num_bits)51{52unsigned valid, bits_left = vl_vlc_bits_left(nal);53int i;5455/* copy the position */56rbsp->nal = *nal;5758/* search for the end of the NAL unit */59while (vl_vlc_search_byte(nal, num_bits, 0x00)) {60if (vl_vlc_peekbits(nal, 24) == 0x000001 ||61vl_vlc_peekbits(nal, 32) == 0x00000001) {62vl_vlc_limit(&rbsp->nal, bits_left - vl_vlc_bits_left(nal));63break;64}65vl_vlc_eatbits(nal, 8);66}6768valid = vl_vlc_valid_bits(&rbsp->nal);69/* search for the emulation prevention three byte */70for (i = 24; i <= valid; i += 8) {71if ((vl_vlc_peekbits(&rbsp->nal, i) & 0xffffff) == 0x3) {72vl_vlc_removebits(&rbsp->nal, i - 8, 8);73i += 8;74}75}7677valid = vl_vlc_valid_bits(&rbsp->nal);7879rbsp->escaped = (valid >= 16) ? 16 : ((valid >= 8) ? 8 : 0);80}8182/**83* Make at least 16 more bits available84*/85static inline void vl_rbsp_fillbits(struct vl_rbsp *rbsp)86{87unsigned valid = vl_vlc_valid_bits(&rbsp->nal);88unsigned i, bits;8990/* abort if we still have enough bits */91if (valid >= 32)92return;9394vl_vlc_fillbits(&rbsp->nal);9596/* abort if we have less than 24 bits left in this nal */97if (vl_vlc_bits_left(&rbsp->nal) < 24)98return;99100/* check that we have enough bits left from the last fillbits */101assert(valid >= rbsp->escaped);102103/* handle the already escaped bits */104valid -= rbsp->escaped;105106/* search for the emulation prevention three byte */107rbsp->escaped = 16;108bits = vl_vlc_valid_bits(&rbsp->nal);109for (i = valid + 24; i <= bits; i += 8) {110if ((vl_vlc_peekbits(&rbsp->nal, i) & 0xffffff) == 0x3) {111vl_vlc_removebits(&rbsp->nal, i - 8, 8);112rbsp->escaped = bits - i;113bits -= 8;114i += 8;115}116}117}118119/**120* Return an unsigned integer from the first n bits121*/122static inline unsigned vl_rbsp_u(struct vl_rbsp *rbsp, unsigned n)123{124if (n == 0)125return 0;126127vl_rbsp_fillbits(rbsp);128return vl_vlc_get_uimsbf(&rbsp->nal, n);129}130131/**132* Return an unsigned exponential Golomb encoded integer133*/134static inline unsigned vl_rbsp_ue(struct vl_rbsp *rbsp)135{136unsigned bits = 0;137138vl_rbsp_fillbits(rbsp);139while (!vl_vlc_get_uimsbf(&rbsp->nal, 1))140++bits;141142return (1 << bits) - 1 + vl_rbsp_u(rbsp, bits);143}144145/**146* Return an signed exponential Golomb encoded integer147*/148static inline signed vl_rbsp_se(struct vl_rbsp *rbsp)149{150signed codeNum = vl_rbsp_ue(rbsp);151if (codeNum & 1)152return (codeNum + 1) >> 1;153else154return -(codeNum >> 1);155}156157/**158* Are more data available in the RBSP ?159*/160static inline bool vl_rbsp_more_data(struct vl_rbsp *rbsp)161{162unsigned bits, value;163164if (vl_vlc_bits_left(&rbsp->nal) > 8)165return TRUE;166167bits = vl_vlc_valid_bits(&rbsp->nal);168value = vl_vlc_peekbits(&rbsp->nal, bits);169if (value == 0 || value == (1 << (bits - 1)))170return FALSE;171172return TRUE;173}174175#endif /* vl_rbsp_h */176177178