1848b8605Smrg/************************************************************************** 2848b8605Smrg * 3848b8605Smrg * Copyright 2013 Advanced Micro Devices, Inc. 4848b8605Smrg * All Rights Reserved. 5848b8605Smrg * 6848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a 7848b8605Smrg * copy of this software and associated documentation files (the 8848b8605Smrg * "Software"), to deal in the Software without restriction, including 9848b8605Smrg * without limitation the rights to use, copy, modify, merge, publish, 10848b8605Smrg * distribute, sub license, and/or sell copies of the Software, and to 11848b8605Smrg * permit persons to whom the Software is furnished to do so, subject to 12848b8605Smrg * the following conditions: 13848b8605Smrg * 14848b8605Smrg * The above copyright notice and this permission notice (including the 15848b8605Smrg * next paragraph) shall be included in all copies or substantial portions 16848b8605Smrg * of the Software. 17848b8605Smrg * 18848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19848b8605Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20848b8605Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21848b8605Smrg * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR 22848b8605Smrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23848b8605Smrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24848b8605Smrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25848b8605Smrg * 26848b8605Smrg **************************************************************************/ 27848b8605Smrg 28848b8605Smrg/* 29848b8605Smrg * Authors: 30848b8605Smrg * Christian König <christian.koenig@amd.com> 31848b8605Smrg * 32848b8605Smrg */ 33848b8605Smrg 34848b8605Smrg/* 35848b8605Smrg * Functions for reading the raw byte sequence payload of H.264 36848b8605Smrg */ 37848b8605Smrg 38848b8605Smrg#ifndef vl_rbsp_h 39848b8605Smrg#define vl_rbsp_h 40848b8605Smrg 41848b8605Smrg#include "vl/vl_vlc.h" 42848b8605Smrg 43848b8605Smrgstruct vl_rbsp { 44848b8605Smrg struct vl_vlc nal; 45848b8605Smrg unsigned escaped; 46848b8605Smrg}; 47848b8605Smrg 48848b8605Smrg/** 49848b8605Smrg * Initialize the RBSP object 50848b8605Smrg */ 51b8e80941Smrgstatic inline void vl_rbsp_init(struct vl_rbsp *rbsp, struct vl_vlc *nal, unsigned num_bits) 52848b8605Smrg{ 53b8e80941Smrg unsigned valid, bits_left = vl_vlc_bits_left(nal); 54b8e80941Smrg int i; 55848b8605Smrg 56848b8605Smrg /* copy the position */ 57848b8605Smrg rbsp->nal = *nal; 58848b8605Smrg 59848b8605Smrg /* search for the end of the NAL unit */ 60848b8605Smrg while (vl_vlc_search_byte(nal, num_bits, 0x00)) { 61848b8605Smrg if (vl_vlc_peekbits(nal, 24) == 0x000001 || 62848b8605Smrg vl_vlc_peekbits(nal, 32) == 0x00000001) { 63848b8605Smrg vl_vlc_limit(&rbsp->nal, bits_left - vl_vlc_bits_left(nal)); 64b8e80941Smrg break; 65848b8605Smrg } 66848b8605Smrg vl_vlc_eatbits(nal, 8); 67848b8605Smrg } 68b8e80941Smrg 69b8e80941Smrg valid = vl_vlc_valid_bits(&rbsp->nal); 70b8e80941Smrg /* search for the emulation prevention three byte */ 71b8e80941Smrg for (i = 24; i <= valid; i += 8) { 72b8e80941Smrg if ((vl_vlc_peekbits(&rbsp->nal, i) & 0xffffff) == 0x3) { 73b8e80941Smrg vl_vlc_removebits(&rbsp->nal, i - 8, 8); 74b8e80941Smrg i += 8; 75b8e80941Smrg } 76b8e80941Smrg } 77b8e80941Smrg 78b8e80941Smrg valid = vl_vlc_valid_bits(&rbsp->nal); 79b8e80941Smrg 80b8e80941Smrg rbsp->escaped = (valid >= 16) ? 16 : ((valid >= 8) ? 8 : 0); 81848b8605Smrg} 82848b8605Smrg 83848b8605Smrg/** 84848b8605Smrg * Make at least 16 more bits available 85848b8605Smrg */ 86b8e80941Smrgstatic inline void vl_rbsp_fillbits(struct vl_rbsp *rbsp) 87848b8605Smrg{ 88848b8605Smrg unsigned valid = vl_vlc_valid_bits(&rbsp->nal); 89848b8605Smrg unsigned i, bits; 90848b8605Smrg 91848b8605Smrg /* abort if we still have enough bits */ 92848b8605Smrg if (valid >= 32) 93848b8605Smrg return; 94848b8605Smrg 95848b8605Smrg vl_vlc_fillbits(&rbsp->nal); 96848b8605Smrg 97848b8605Smrg /* abort if we have less than 24 bits left in this nal */ 98848b8605Smrg if (vl_vlc_bits_left(&rbsp->nal) < 24) 99848b8605Smrg return; 100848b8605Smrg 101848b8605Smrg /* check that we have enough bits left from the last fillbits */ 102848b8605Smrg assert(valid >= rbsp->escaped); 103848b8605Smrg 104848b8605Smrg /* handle the already escaped bits */ 105848b8605Smrg valid -= rbsp->escaped; 106848b8605Smrg 107848b8605Smrg /* search for the emulation prevention three byte */ 108848b8605Smrg rbsp->escaped = 16; 109848b8605Smrg bits = vl_vlc_valid_bits(&rbsp->nal); 110848b8605Smrg for (i = valid + 24; i <= bits; i += 8) { 111848b8605Smrg if ((vl_vlc_peekbits(&rbsp->nal, i) & 0xffffff) == 0x3) { 112848b8605Smrg vl_vlc_removebits(&rbsp->nal, i - 8, 8); 113848b8605Smrg rbsp->escaped = bits - i; 114848b8605Smrg bits -= 8; 115848b8605Smrg i += 8; 116848b8605Smrg } 117848b8605Smrg } 118848b8605Smrg} 119848b8605Smrg 120848b8605Smrg/** 121848b8605Smrg * Return an unsigned integer from the first n bits 122848b8605Smrg */ 123b8e80941Smrgstatic inline unsigned vl_rbsp_u(struct vl_rbsp *rbsp, unsigned n) 124848b8605Smrg{ 125848b8605Smrg if (n == 0) 126848b8605Smrg return 0; 127848b8605Smrg 128848b8605Smrg vl_rbsp_fillbits(rbsp); 129848b8605Smrg return vl_vlc_get_uimsbf(&rbsp->nal, n); 130848b8605Smrg} 131848b8605Smrg 132848b8605Smrg/** 133848b8605Smrg * Return an unsigned exponential Golomb encoded integer 134848b8605Smrg */ 135b8e80941Smrgstatic inline unsigned vl_rbsp_ue(struct vl_rbsp *rbsp) 136848b8605Smrg{ 137848b8605Smrg unsigned bits = 0; 138848b8605Smrg 139848b8605Smrg vl_rbsp_fillbits(rbsp); 140848b8605Smrg while (!vl_vlc_get_uimsbf(&rbsp->nal, 1)) 141848b8605Smrg ++bits; 142848b8605Smrg 143848b8605Smrg return (1 << bits) - 1 + vl_rbsp_u(rbsp, bits); 144848b8605Smrg} 145848b8605Smrg 146848b8605Smrg/** 147848b8605Smrg * Return an signed exponential Golomb encoded integer 148848b8605Smrg */ 149b8e80941Smrgstatic inline signed vl_rbsp_se(struct vl_rbsp *rbsp) 150848b8605Smrg{ 151848b8605Smrg signed codeNum = vl_rbsp_ue(rbsp); 152848b8605Smrg if (codeNum & 1) 153848b8605Smrg return (codeNum + 1) >> 1; 154848b8605Smrg else 155848b8605Smrg return -(codeNum >> 1); 156848b8605Smrg} 157848b8605Smrg 158848b8605Smrg/** 159848b8605Smrg * Are more data available in the RBSP ? 160848b8605Smrg */ 161b8e80941Smrgstatic inline bool vl_rbsp_more_data(struct vl_rbsp *rbsp) 162848b8605Smrg{ 163848b8605Smrg unsigned bits, value; 164848b8605Smrg 165848b8605Smrg if (vl_vlc_bits_left(&rbsp->nal) > 8) 166848b8605Smrg return TRUE; 167848b8605Smrg 168848b8605Smrg bits = vl_vlc_valid_bits(&rbsp->nal); 169848b8605Smrg value = vl_vlc_peekbits(&rbsp->nal, bits); 170848b8605Smrg if (value == 0 || value == (1 << (bits - 1))) 171848b8605Smrg return FALSE; 172848b8605Smrg 173848b8605Smrg return TRUE; 174848b8605Smrg} 175848b8605Smrg 176848b8605Smrg#endif /* vl_rbsp_h */ 177